--- Makefile | 1 + df.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 df.c diff --git a/Makefile b/Makefile index 2e31f33..bb981ce 100644 --- a/Makefile +++ b/Makefile _AT_@ -38,6 +38,7 @@ SRC = \ comm.c \ cp.c \ date.c \ + df.c \ dirname.c \ echo.c \ env.c \ diff --git a/df.c b/df.c new file mode 100644 index 0000000..d78df72 --- /dev/null +++ b/df.c _AT_@ -0,0 +1,65 @@ +#include <sys/stat.h> +#include <sys/statvfs.h> +#include <unistd.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <mntent.h> +#include "util.h" + +static void mnt_show(const char *fsname, const char *dir, + const char *type); + +int +main(void) +{ + FILE *fp; + struct mntent *me; + + fp = setmntent("/proc/mounts", "r"); + if (!fp) + eprintf("fopen /proc/mounts:"); + + printf("Filesystem 512-blocks Used Avail Capacity Mounted on\n"); + while ((me = getmntent(fp))) + mnt_show(me->mnt_fsname, me->mnt_dir, + me->mnt_type); + endmntent(fp); + return 0; +} + +static void +mnt_show(const char *fsname, const char *dir, + const char *type) +{ + struct statvfs svfs; + struct stat s; + unsigned long long total, used, avail; + int capacity; + int bs; + + stat(dir, &s); + statvfs(dir, &svfs); + + if (!s.st_dev || !svfs.f_blocks) + return; + + if (svfs.f_bsize) + bs = svfs.f_bsize; + else + bs = 1; + + total = svfs.f_blocks * bs / 512; + avail = svfs.f_bfree * bs / 512; + used = total - avail; + + if (used + avail) { + capacity = (used * 100) / (used + avail); + if (used * 100 != capacity * (used + avail)) + capacity++; + } + + printf("%-12s %9llu %9llu %9llu %5d%% %s\n", + fsname, total, used, avail, capacity, + dir); +} -- 1.8.3.3 --bg08WKrSYDhXBjb5--Received on Mon Sep 17 2001 - 00:00:00 CEST
This archive was generated by hypermail 2.3.0 : Tue Jul 23 2013 - 13:12:05 CEST