Hi, Incorporated Steve's changes as well to make it compile/work on OpenBSD. I realize #ifdef's are terrible but for now it should do the trick until we come up with a proper solution to this.
Thanks, sin
>From 34e65ec1b392a90f7d46c90c2d67a25dd6219a4d Mon Sep 17 00:00:00 2001 From: sin <s...@2f30.org> Date: Mon, 22 Jul 2013 13:17:32 +0100 Subject: [PATCH] Add df(1) --- Makefile | 1 + df.c | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 df.c diff --git a/Makefile b/Makefile index 2e31f33..bb981ce 100644 --- a/Makefile +++ b/Makefile @@ -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..75be614 --- /dev/null +++ b/df.c @@ -0,0 +1,90 @@ +#ifdef __OpenBSD__ +#include <sys/param.h> +#include <sys/mount.h> +#else +#include <mntent.h> +#endif + +#include <sys/statvfs.h> +#include <stdio.h> +#include <stdlib.h> +#include "util.h" + +static void mnt_show(const char *fsname, const char *dir); + +static void +usage(void) +{ + eprintf("usage: %s\n", argv0); +} + +int +main(int argc, char *argv[]) +{ +#ifdef __OpenBSD__ + struct statfs *mntbuf; + int len, i; +#else + FILE *fp; + struct mntent *me; +#endif + + ARGBEGIN { + case 'a': + break; + case 's': + case 'h': + case 'i': + eprintf("not implemented\n"); + default: + usage(); + } ARGEND; + + printf("Filesystem 512-blocks Used Avail Capacity Mounted on\n"); + +#ifdef __OpenBSD__ + len = getmntinfo(&mntbuf, MNT_WAIT); + if (len == 0) + eprintf("gemntinfo:"); + + for (i = 0; i < len; i++) + mnt_show(mntbuf[i].f_mntfromname, + mntbuf[i].f_mntonname); +#else + fp = setmntent("/proc/mounts", "r"); + if (!fp) + eprintf("fopen /proc/mounts:"); + + while ((me = getmntent(fp))) + mnt_show(me->mnt_fsname, me->mnt_dir); + + endmntent(fp); +#endif + return 0; +} + +static void +mnt_show(const char *fsname, const char *dir) +{ + struct statvfs s; + unsigned long long total, used, avail; + int capacity = 0; + int bs; + + statvfs(dir, &s); + + bs = s.f_frsize / 512; + total = s.f_blocks * bs; + avail = s.f_bfree * bs; + used = total - avail; + + if (used + avail) { + capacity = (used * 100) / (used + avail); + if (used * 100 != capacity * (used + avail)) + capacity++; + } + + printf("%-12s %9llu %9llu %9llu %7d%% %s\n", + fsname, total, used, avail, capacity, + dir); +} -- 1.8.3.4