X-Git-Url: https://git.xinqibao.xyz/slstatus.git/blobdiff_plain/b51721c65a8ec2067c599c076d2db6b4a29f567a..db63d3dc96f59f48fa818715e8fc89a623de6111:/slstatus.c?ds=inline diff --git a/slstatus.c b/slstatus.c index 369c6ff..fe27be4 100644 --- a/slstatus.c +++ b/slstatus.c @@ -3,6 +3,7 @@ /* global libraries */ #include #include +#include #include #include #include @@ -16,9 +17,15 @@ /* 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(); @@ -31,6 +38,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 +79,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() @@ -79,7 +89,7 @@ get_battery() /* open battery now file */ if (!(fp = fopen(batterynowfile, "r"))) { fprintf(stderr, "Error opening battery file."); - exit(1); + return smprintf("n/a"); } /* read value */ @@ -91,7 +101,7 @@ get_battery() /* open battery full file */ if (!(fp = fopen(batteryfullfile, "r"))) { fprintf(stderr, "Error opening battery file."); - exit(1); + return smprintf("n/a"); } /* read value */ @@ -117,7 +127,7 @@ get_cpu_temperature() /* open temperature file */ if (!(fp = fopen(tempfile, "r"))) { fprintf(stderr, "Could not open temperature file.\n"); - exit(1); + return smprintf("n/a"); } /* extract temperature */ @@ -141,7 +151,7 @@ get_cpu_usage() /* open stat file */ if (!(fp = fopen("/proc/stat","r"))) { fprintf(stderr, "Error opening stat file."); - exit(1); + return smprintf("n/a"); } /* read values */ @@ -156,7 +166,7 @@ get_cpu_usage() /* open stat file */ if (!(fp = fopen("/proc/stat","r"))) { fprintf(stderr, "Error opening stat file."); - exit(1); + return smprintf("n/a"); } /* read values */ @@ -182,11 +192,14 @@ get_datetime() /* get time in format */ time(&tm); + setlocale(LC_TIME, ""); if(!strftime(buf, bufsize, timeformat, localtime(&tm))) { - fprintf(stderr, "Strftime failed.\n"); - exit(1); + setlocale(LC_TIME, "C"); + fprintf(stderr, "Strftime failed.\n"); + return smprintf("n/a"); } + setlocale(LC_TIME, "C"); /* return time */ return smprintf("%s", buf); } @@ -202,7 +215,7 @@ get_ram_usage() /* open meminfo file */ if (!(fp = fopen("/proc/meminfo", "r"))) { fprintf(stderr, "Error opening meminfo file."); - exit(1); + return smprintf("n/a"); } /* read the values */ @@ -283,7 +296,7 @@ get_wifi_signal() /* open wifi file */ if(!(fp = fopen(path, "r"))) { fprintf(stderr, "Error opening wifi operstate file."); - exit(1); + return smprintf("n/a"); } /* read the status */ @@ -294,13 +307,13 @@ get_wifi_signal() /* check if interface down */ if(strcmp(status, "up\n") != 0){ - return "n/a"; + return smprintf("n/a"); } /* open wifi file */ if (!(fp = fopen("/proc/net/wireless", "r"))) { fprintf(stderr, "Error opening wireless file."); - exit(1); + return smprintf("n/a"); } /* extract the signal strength */ @@ -336,9 +349,10 @@ main() /* 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"); @@ -368,7 +382,9 @@ main() 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 */