summaryrefslogtreecommitdiff
path: root/Userland/Utilities/file.cpp
diff options
context:
space:
mode:
authorValtteri Koskivuori <vkoskiv@gmail.com>2021-06-15 20:29:04 +0300
committerAndreas Kling <kling@serenityos.org>2021-06-15 21:30:55 +0200
commitc69ea44397b16a3ed2079c24a9a81692097f44c2 (patch)
tree21bdbbb911606a921efc23853e53f0ace1547f8f /Userland/Utilities/file.cpp
parent1414c7b049c5a8858f400a5e677cd20894463b32 (diff)
downloadserenity-c69ea44397b16a3ed2079c24a9a81692097f44c2.zip
Userland: Teach the file utility that empty files also exist
Previously, empty files with no identifiable file type extension would show up as `text/plain`. This fixes it up to show empty files as what they really are - full of nothing.
Diffstat (limited to 'Userland/Utilities/file.cpp')
-rw-r--r--Userland/Utilities/file.cpp10
1 files changed, 10 insertions, 0 deletions
diff --git a/Userland/Utilities/file.cpp b/Userland/Utilities/file.cpp
index e9e2414db7..e35d676089 100644
--- a/Userland/Utilities/file.cpp
+++ b/Userland/Utilities/file.cpp
@@ -12,6 +12,7 @@
#include <LibCore/MimeData.h>
#include <LibGfx/ImageDecoder.h>
#include <stdio.h>
+#include <sys/stat.h>
#include <unistd.h>
static Optional<String> description_only(String description, [[maybe_unused]] const String& path)
@@ -121,8 +122,17 @@ int main(int argc, char** argv)
continue;
}
+ struct stat file_stat;
+ if (lstat(path, &file_stat) < 0) {
+ perror("lstat");
+ return 1;
+ }
+
+ auto file_size_in_bytes = file_stat.st_size;
if (file->is_directory()) {
outln("{}: directory", path);
+ } else if (!file_size_in_bytes) {
+ outln("{}: empty", path);
} else {
// Read accounts for longest possible offset + signature we currently match against.
auto bytes = file->read(0x9006);