s2dblocks

[archived] statusbar blocks for dwm
git clone https://hhvn.uk/s2dblocks
git clone git://hhvn.uk/s2dblocks
Log | Files | Refs

df.c (1406B)


      1 #include <stdio.h>
      2 #include <string.h>
      3 #include <stdlib.h>
      4 #include <assert.h>
      5 #ifdef __linux__
      6 #include <mntent.h>
      7 #include <sys/statvfs.h>
      8 #endif /* __linux__ */
      9 #include "status2d.h"
     10 
     11 #define MAXDISKS 64
     12 
     13 int
     14 main(void) {
     15 	char *names[MAXDISKS];
     16 	int percent[MAXDISKS];
     17 	int i;
     18 
     19 	memset(names, 0, sizeof(names));
     20 	memset(percent, 0, sizeof(percent));
     21 #ifdef __linux__
     22 	unsigned long long used, total;
     23 	struct mntent *mnt;
     24 	struct statvfs fst;
     25 	FILE *f;
     26 	char *tmp, *p;
     27 	int j;
     28 
     29 	f = setmntent("/etc/mtab", "r");
     30 	i = 0;
     31 	while ((mnt = getmntent(f))) {
     32 		if (strchr(mnt->mnt_fsname, '/')) {
     33 			if (statvfs(mnt->mnt_dir, &fst) == -1)
     34 				continue;
     35 			used = fst.f_blocks - fst.f_bfree;
     36 			total = used + fst.f_bavail;
     37 			percent[i] = 100 * used / total;
     38 			tmp = mnt->mnt_fsname;
     39 			if (*tmp == '/')
     40 				tmp++;
     41 			while (p = strchr(tmp, '/'))
     42 				tmp = p + 1;
     43 			names[i] = strdup(tmp);
     44 			i++;
     45 		}
     46 	}
     47 	endmntent(f);
     48 #else
     49 	s2d_print("no handler for this OS");
     50 	s2d_finish();
     51 #endif
     52 	for (i = 0; names[i] && i < MAXDISKS; i++) {
     53 		s2d_bg(BRBG);
     54 		if (percent[i] >= 99)
     55 			s2d_fg(RED);
     56 		else if (percent[i] >= 90)
     57 			s2d_fg(ORANGE);
     58 		else if (percent[i] >= 80)
     59 			s2d_fg(YELLOW);
     60 		else
     61 			s2d_fg(GREEN);
     62 		s2d_bar(0, 2, 3, bar_height - 4, 1, percent[i]);
     63 		s2d_reset(1, 1);
     64 		s2d_forward(-1);
     65 		s2d_forward(2);
     66 		s2d_print("%s%s", names[i], names[i+1] ? " " : "");
     67 		free(names[i]);
     68 	}
     69 	s2d_finish();
     70 }