[PATCH] Add df(1)

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

---
 Makefile |  1 +
 df.c     | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 54 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..2b4cebf
--- /dev/null
+++ b/df.c
_AT_@ -0,0 +1,53 @@
+#include <sys/statvfs.h>
+#include <stdio.h>
+#include <stdlib.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 s;
+	unsigned long long total, used, avail;
+	int capacity = 0;
+	int bs;
+
+	statvfs(dir, &s);
+
+	bs = s.f_bsize ? s.f_bsize : 1;
+	total = s.f_blocks * bs / 512;
+	avail = s.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 %7d%%  %s\n",
+	       fsname, total, used, avail, capacity,
+	       dir);
+}
-- 
1.8.3.3
--17pEHd4RhPHOinZp--
Received on Mon Sep 17 2001 - 00:00:00 CEST

This archive was generated by hypermail 2.3.0 : Tue Jul 23 2013 - 16:00:04 CEST