diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-02-21 14:48:00 +0100 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-02-21 14:48:00 +0100 |
commit | 43075e5878e4ef6e12c9cd94c96d0954239125f4 (patch) | |
tree | e03e616841221aaa13d25f04729a77ebaace9e1a /Userland | |
parent | 7d288aafb25f44a63cf1e4c3e262b4906bc12b21 (diff) | |
download | serenity-43075e5878e4ef6e12c9cd94c96d0954239125f4.zip |
Add a simple /bin/df which gathers its info from /proc/df.
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/.gitignore | 1 | ||||
-rw-r--r-- | Userland/Makefile | 5 | ||||
-rw-r--r-- | Userland/df.cpp | 55 |
3 files changed, 61 insertions, 0 deletions
diff --git a/Userland/.gitignore b/Userland/.gitignore index 4c4f51262a..45015daa24 100644 --- a/Userland/.gitignore +++ b/Userland/.gitignore @@ -32,3 +32,4 @@ top chmod pape ln +df diff --git a/Userland/Makefile b/Userland/Makefile index 4759c51e24..7232c194e4 100644 --- a/Userland/Makefile +++ b/Userland/Makefile @@ -27,6 +27,7 @@ OBJS = \ dmesg.o \ chmod.o \ top.o \ + df.o \ ln.o \ rm.o @@ -61,6 +62,7 @@ APPS = \ chmod \ top \ ln \ + df \ rm ARCH_FLAGS = @@ -174,6 +176,9 @@ top: top.o ln: ln.o $(LD) -o $@ $(LDFLAGS) $< ../LibC/LibC.a +df: df.o + $(LD) -o $@ $(LDFLAGS) $< ../LibC/LibC.a + .cpp.o: @echo "CXX $<"; $(CXX) $(CXXFLAGS) -o $@ -c $< diff --git a/Userland/df.cpp b/Userland/df.cpp new file mode 100644 index 0000000000..bce2e2268a --- /dev/null +++ b/Userland/df.cpp @@ -0,0 +1,55 @@ +#include <stdio.h> +#include <unistd.h> +#include <fcntl.h> +#include <stdlib.h> +#include <AK/AKString.h> +#include <AK/Vector.h> + +struct FileSystem { + String fs; + size_t total_block_count { 0 }; + size_t free_block_count { 0 }; + size_t total_inode_count { 0 }; + size_t free_inode_count { 0 }; + String mount_point; +}; + +int main(int, char**) +{ + FILE* fp = fopen("/proc/df", "r"); + if (!fp) { + perror("failed to open /proc/df"); + return 1; + } + printf("Filesystem Blocks Used Available Mount point\n"); + for (;;) { + char buf[4096]; + char* ptr = fgets(buf, sizeof(buf), fp); + if (!ptr) + break; + auto parts = String(buf, Chomp).split(','); + if (parts.size() < 6) + break; + bool ok; + String fs = parts[0]; + unsigned total_block_count = parts[1].to_uint(ok); + ASSERT(ok); + unsigned free_block_count = parts[2].to_uint(ok); + ASSERT(ok); + unsigned total_inode_count = parts[3].to_uint(ok); + ASSERT(ok); + unsigned free_inode_count = parts[4].to_uint(ok); + ASSERT(ok); + String mount_point = parts[5]; + + printf("% 10s", fs.characters()); + printf("%10u ", total_block_count); + printf("%10u ", total_block_count - free_block_count); + printf("%10u ", free_block_count); + printf("%s", mount_point.characters()); + printf("\n"); + } + int rc = fclose(fp); + ASSERT(rc == 0); + return 0; +} |