config.h (1265B)
1 #include <time.h> 2 #include <stdio.h> 3 #include <assert.h> 4 5 #define MAXBAR 256 6 #define SEP " | " 7 8 enum units { 9 KB = 0, 10 MB = 1, 11 GB = 2, 12 } unit; 13 14 float 15 human(float num) { 16 unit = 0; 17 while ((num / 1000) > 1) { 18 num /= 1000; 19 unit++; 20 } 21 return num; 22 } 23 24 char * 25 unitstr() { 26 switch (unit) { 27 case KB: return "kb"; 28 case MB: return "mb"; 29 case GB: return "gb"; 30 } 31 return ""; 32 } 33 34 char * 35 get_time() { 36 time_t t = time(NULL); 37 struct tm *tval = localtime(&t); 38 static char timestr[32]; 39 strftime(timestr, 32, "date: %m/%d %H:%M", tval); 40 return timestr; 41 } 42 43 char * 44 get_mem() { 45 static char memstr[32]; 46 float total, available, used; 47 char line[128] = {0}, *tok, *usedstr, *totalstr; 48 FILE *f = fopen("/proc/meminfo", "r"); /* only works on linux systems */ 49 50 while (fgets(line, 128, f)) { 51 assert((tok = strtok(line, ":"))); 52 if (strcmp(tok, "MemTotal") == 0) 53 total = atoi(strtok(NULL, "")); 54 else if (strcmp(tok, "MemAvailable") == 0) 55 available = atoi(strtok(NULL, "")); 56 } 57 fclose(f); 58 59 used = human(total - available); 60 usedstr = unitstr(); 61 total = human(total); 62 totalstr = unitstr(); 63 64 snprintf(memstr, 32, "mem: %.1f%s/%.1f%s", used, usedstr, total, totalstr); 65 return memstr; 66 } 67 68 struct entry { 69 char *(*func)(); 70 } entrys[] = { 71 {get_mem}, 72 {get_time}, 73 };