X-Git-Url: https://git.xinqibao.xyz/slstatus.git/blobdiff_plain/6820631175868c277effa7cc05f9cb3197b72654..367f8a8c44fc9dc05a0f7a2c1500111ca97f669c:/slstatus.c diff --git a/slstatus.c b/slstatus.c index c7e9303..0c4605f 100644 --- a/slstatus.c +++ b/slstatus.c @@ -1,7 +1,5 @@ /* See LICENSE file for copyright and license details. */ -#include #include -#include #include #include #include @@ -10,6 +8,7 @@ #include #include "arg.h" +#include "slstatus.h" #include "util.h" struct arg { @@ -18,45 +17,8 @@ struct arg { const char *args; }; -static const char *battery_perc(const char *bat); -static const char *battery_power(const char *bat); -static const char *battery_state(const char *bat); -static const char *cpu_freq(void); -static const char *cpu_perc(void); -static const char *cpu_iowait(void); -static const char *datetime(const char *fmt); -static const char *disk_free(const char *mnt); -static const char *disk_perc(const char *mnt); -static const char *disk_total(const char *mnt); -static const char *disk_used(const char *mnt); -static const char *entropy(void); -static const char *gid(void); -static const char *hostname(void); -static const char *ipv4(const char *iface); -static const char *ipv6(const char *iface); -static const char *kernel_release(void); -static const char *keyboard_indicators(void); -static const char *load_avg(const char *fmt); -static const char *num_files(const char *dir); -static const char *ram_free(void); -static const char *ram_perc(void); -static const char *ram_used(void); -static const char *ram_total(void); -static const char *run_command(const char *cmd); -static const char *swap_free(void); -static const char *swap_perc(void); -static const char *swap_used(void); -static const char *swap_total(void); -static const char *temp(const char *file); -static const char *uid(void); -static const char *uptime(void); -static const char *username(void); -static const char *vol_perc(const char *card); -static const char *wifi_perc(const char *iface); -static const char *wifi_essid(const char *iface); - -char *argv0; -static unsigned short int done; +char buf[1024]; +static int done; static Display *dpy; #include "config.h" @@ -64,6 +26,8 @@ static Display *dpy; static void terminate(const int signo) { + (void)signo; + done = 1; } @@ -78,8 +42,7 @@ difftimespec(struct timespec *res, struct timespec *a, struct timespec *b) static void usage(void) { - fprintf(stderr, "usage: %s [-s]\n", argv0); - exit(1); + die("usage: %s [-s]", argv0); } int @@ -88,9 +51,11 @@ main(int argc, char *argv[]) struct sigaction act; struct timespec start, current, diff, intspec, wait; size_t i, len; - int sflag = 0; + int sflag, ret; char status[MAXLEN]; + const char *res; + sflag = 0; ARGBEGIN { case 's': sflag = 1; @@ -103,40 +68,53 @@ main(int argc, char *argv[]) usage(); } - setlocale(LC_ALL, ""); - memset(&act, 0, sizeof(act)); act.sa_handler = terminate; sigaction(SIGINT, &act, NULL); sigaction(SIGTERM, &act, NULL); + if (sflag) { + setbuf(stdout, NULL); + } + if (!sflag && !(dpy = XOpenDisplay(NULL))) { - fprintf(stderr, "slstatus: cannot open display"); - return 1; + die("XOpenDisplay: Failed to open display"); } while (!done) { - clock_gettime(CLOCK_MONOTONIC, &start); + if (clock_gettime(CLOCK_MONOTONIC, &start) < 0) { + die("clock_gettime:"); + } status[0] = '\0'; for (i = len = 0; i < LEN(args); i++) { - len += snprintf(status + len, sizeof(status) - len, - args[i].fmt, args[i].func(args[i].args)); - - if (len >= sizeof(status)) { - status[sizeof(status) - 1] = '\0'; + if (!(res = args[i].func(args[i].args))) { + res = unknown_str; + } + if ((ret = snprintf(status + len, sizeof(status) - len, + args[i].fmt, res)) < 0) { + warn("snprintf:"); + break; + } else if ((size_t)ret >= sizeof(status) - len) { + warn("snprintf: Output truncated"); + break; } + len += ret; } if (sflag) { printf("%s\n", status); } else { - XStoreName(dpy, DefaultRootWindow(dpy), status); - XSync(dpy, False); + if (XStoreName(dpy, DefaultRootWindow(dpy), status) < 0) { + die("XStoreName: Allocation failed"); + } + XFlush(dpy); } if (!done) { - clock_gettime(CLOCK_MONOTONIC, ¤t); + if (clock_gettime(CLOCK_MONOTONIC, ¤t) < 0) { + die("clock_gettime:"); + } difftimespec(&diff, ¤t, &start); intspec.tv_sec = interval / 1000; @@ -144,14 +122,19 @@ main(int argc, char *argv[]) difftimespec(&wait, &intspec, &diff); if (wait.tv_sec >= 0) { - nanosleep(&wait, NULL); + if (nanosleep(&wait, NULL) < 0 && + errno != EINTR) { + die("nanosleep:"); + } } } } if (!sflag) { XStoreName(dpy, DefaultRootWindow(dpy), NULL); - XCloseDisplay(dpy); + if (XCloseDisplay(dpy) < 0) { + die("XCloseDisplay: Failed to close display"); + } } return 0;