summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Stapelberg <michael@stapelberg.de>2009-10-11 23:27:26 +0200
committerMichael Stapelberg <michael@stapelberg.de>2009-10-11 23:27:26 +0200
commit783707906c102c6b30f18a3837cec523974b882c (patch)
tree4df9f8d993637a5042ac6570cd30d52558633d88 /src
parentf947d0a446b1b99020722cbc71127fc0c06086b2 (diff)
Implement disk info (%free/%used/%total)
Diffstat (limited to 'src')
-rw-r--r--src/print_disk_info.c67
1 files changed, 67 insertions, 0 deletions
diff --git a/src/print_disk_info.c b/src/print_disk_info.c
new file mode 100644
index 0000000..84524c3
--- /dev/null
+++ b/src/print_disk_info.c
@@ -0,0 +1,67 @@
+// vim:ts=8:expandtab
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+#include <stdint.h>
+#include <sys/statvfs.h>
+#include <sys/types.h>
+
+#include "i3status.h"
+
+#define TERABYTE (1024ULL * 1024 * 1024 * 1024)
+#define GIGABYTE (1024ULL * 1024 * 1024)
+#define MEGABYTE (1024ULL * 1024)
+#define KILOBYTE (1024ULL)
+
+void print_bytes_human(uint64_t bytes) {
+ /* 1 TB */
+ if (bytes > TERABYTE)
+ printf("%f TB", (double)bytes / TERABYTE);
+ else if (bytes > GIGABYTE)
+ printf("%.01f GB", (double)bytes / GIGABYTE);
+ else if (bytes > MEGABYTE)
+ printf("%.01f MB", (double)bytes / MEGABYTE);
+ else if (bytes > KILOBYTE)
+ printf("%.01f KB", (double)bytes / KILOBYTE);
+ else {
+ printf("%.01f B", (double)bytes);
+ }
+
+}
+
+/*
+ * Just parses /proc/net/wireless looking for lines beginning with
+ * wlan_interface, extracting the quality of the link and adding the
+ * current IP address of wlan_interface.
+ *
+ */
+void print_disk_info(const char *path, const char *format) {
+ const char *walk;
+ struct statvfs buf;
+
+ if (statvfs(path, &buf) == -1)
+ return;
+
+ for (walk = format; *walk != '\0'; walk++) {
+ if (*walk != '%') {
+ putchar(*walk);
+ continue;
+ }
+
+ if (BEGINS_WITH(walk+1, "free")) {
+ print_bytes_human(buf.f_bsize * buf.f_bfree);
+ walk += strlen("free");
+ }
+
+ if (BEGINS_WITH(walk+1, "used")) {
+ print_bytes_human(buf.f_bsize * (buf.f_blocks - buf.f_bfree));
+ walk += strlen("used");
+ }
+
+ if (BEGINS_WITH(walk+1, "total")) {
+ print_bytes_human(buf.f_bsize * buf.f_blocks);
+ walk += strlen("total");
+ }
+ }
+}