X-Git-Url: https://git.xinqibao.xyz/slstatus.git/blobdiff_plain/50219004d6a631b156be04e4ce4b235622a6db93..a97e8439181fe6b5e2e8a5621c228128e0e526e0:/slstatus.c diff --git a/slstatus.c b/slstatus.c index 3297e1d..2fc4fea 100644 --- a/slstatus.c +++ b/slstatus.c @@ -3,12 +3,14 @@ /* global libraries */ #include #include +#include #include #include #include #include #include #include +#include #include #include #include @@ -16,14 +18,21 @@ /* local libraries */ #include "config.h" +/* check file macro */ +#define CHECK_FILE(X,Y) do { \ + if (stat(X,&Y) < 0) return -1; \ + if (!S_ISREG(Y.st_mode)) return -1; \ +} while (0); + /* functions */ -void setstatus(char *str); int config_check(); +void setstatus(char *str); char *smprintf(char *fmt, ...); char *get_battery(); char *get_cpu_temperature(); char *get_cpu_usage(); char *get_datetime(); +char *get_diskusage(); char *get_ram_usage(); char *get_volume(); char *get_wifi_signal(); @@ -31,6 +40,25 @@ char *get_wifi_signal(); /* global variables */ static Display *dpy; +/* check configured paths */ +int +config_check() +{ + struct stat fs; + + /* check all files in the config.h file */ + CHECK_FILE(batterynowfile, fs); + CHECK_FILE(batteryfullfile, fs); + CHECK_FILE(tempfile, fs); + + /* check update interval */ + if (update_interval < 1) + return -1; + + /* exit successfully */ + return 0; +} + /* set statusbar (WM_NAME) */ void setstatus(char *str) @@ -53,22 +81,6 @@ smprintf(char *fmt, ...) return ret; } -#define CHECK_FILE(X,Y) do { \ - if (stat(X,&Y) < 0) return -1; \ - if (!S_ISREG(Y.st_mode)) return -1; \ -} while (0); - -/* check configured paths */ -int -config_check() -{ - struct stat fs; - CHECK_FILE(batterynowfile, fs); - CHECK_FILE(batteryfullfile, fs); - CHECK_FILE(tempfile, fs); - return 0; -} - /* battery percentage */ char * get_battery() @@ -182,15 +194,38 @@ get_datetime() /* get time in format */ time(&tm); + setlocale(LC_TIME, ""); if(!strftime(buf, bufsize, timeformat, localtime(&tm))) { + setlocale(LC_TIME, "C"); fprintf(stderr, "Strftime failed.\n"); return smprintf("n/a"); } + setlocale(LC_TIME, "C"); /* return time */ return smprintf("%s", buf); } +/* disk usage percentage */ +char * +get_diskusage() +{ + int perc = 0; + struct statvfs fs; + + /* try to open mountpoint */ + if (statvfs(mountpath, &fs) < 0) { + fprintf(stderr, "Could not get filesystem info.\n"); + return smprintf("n/a"); + } + + /* calculate percent */ + perc = 100 * (1.0f - ((float)fs.f_bavail / (float)fs.f_blocks)); + + /* return perc */ + return smprintf("%d%%", perc); +} + /* ram percentage */ char * get_ram_usage() @@ -330,15 +365,17 @@ main() char *cpu_temperature = NULL; char *cpu_usage = NULL; char *datetime = NULL; + char *diskusage = NULL; char *ram_usage = NULL; char *volume = NULL; char *wifi_signal = NULL; /* check config for sanity */ if (config_check() < 0) { - fprintf(stderr, "Config error, please check paths and recompile\n"); + fprintf(stderr, "Config error, please check paths and interval and recompile!\n"); exit(1); } + /* open display */ if (!(dpy = XOpenDisplay(0x0))) { fprintf(stderr, "Cannot open display!\n"); @@ -352,6 +389,7 @@ main() cpu_temperature = get_cpu_temperature(); cpu_usage = get_cpu_usage(); datetime = get_datetime(); + diskusage = get_diskusage(); ram_usage = get_ram_usage(); volume = get_volume(); wifi_signal = get_wifi_signal(); @@ -365,10 +403,13 @@ main() free(cpu_temperature); free(cpu_usage); free(datetime); + free(diskusage); free(ram_usage); free(volume); free(wifi_signal); - sleep(update_interval); + + /* wait, "update_interval - 1" because of get_cpu_usage() which uses 1 second */ + sleep(update_interval -1); } /* close display */