[PATCH] Add df(1)

From: sin <sin_AT_2f30.org>
Date: Mon, 22 Jul 2013 13:17:32 +0100

---
 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
_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..75be614
--- /dev/null
+++ b/df.c
_AT_@ -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
--TB36FDmn/VVEgNH/--
Received on Mon Sep 17 2001 - 00:00:00 CEST

This archive was generated by hypermail 2.3.0 : Fri Jul 26 2013 - 13:12:03 CEST