X-Git-Url: https://git.xinqibao.xyz/slstatus.git/blobdiff_plain/2e012d533e49c55c79f808fa213e91d15fb64568..9bef4e889a547ed909a23bb57489bc71da80e472:/slstatus.c diff --git a/slstatus.c b/slstatus.c index 379be53..9559226 100644 --- a/slstatus.c +++ b/slstatus.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -26,45 +27,49 @@ #undef strlcat #undef strlcpy -#include "arg.h" -#include "strlcat.h" -#include "strlcpy.h" -#include "concat.h" +#include "extern/arg.h" +#include "extern/strlcat.h" +#include "extern/strlcpy.h" +#include "extern/concat.h" struct arg { char *(*func)(); - const char *format; + const char *fmt; const char *args; }; -static char *smprintf(const char *, ...); -static char *battery_perc(const char *); -static char *battery_state(const char *); +static char *smprintf(const char *fmt, ...); +static char *battery_perc(const char *bat); +static char *battery_state(const char *bat); static char *cpu_perc(void); -static char *datetime(const char *); -static char *disk_free(const char *); -static char *disk_perc(const char *); -static char *disk_total(const char *); -static char *disk_used(const char *); +static char *datetime(const char *fmt); +static char *disk_free(const char *mnt); +static char *disk_perc(const char *mnt); +static char *disk_total(const char *mnt); +static char *disk_used(const char *mnt); static char *entropy(void); static char *gid(void); static char *hostname(void); -static char *ip(const char *); +static char *ip(const char *iface); static char *load_avg(void); static char *ram_free(void); static char *ram_perc(void); static char *ram_used(void); static char *ram_total(void); -static char *run_command(const char *); -static char *temp(const char *); +static char *run_command(const char *cmd); +static char *swap_free(void); +static char *swap_perc(void); +static char *swap_used(void); +static char *swap_total(void); +static char *temp(const char *file); static char *uid(void); static char *uptime(void); static char *username(void); -static char *vol_perc(const char *); -static char *wifi_perc(const char *); -static char *wifi_essid(const char *); -static void set_status(const char *); -static void sighandler(const int); +static char *vol_perc(const char *card); +static char *wifi_perc(const char *iface); +static char *wifi_essid(const char *iface); +static void set_status(const char *str); +static void sighandler(const int signo); static void usage(void); char *argv0; @@ -89,8 +94,7 @@ smprintf(const char *fmt, ...) ret = malloc(++len); if (ret == NULL) { - warn("Malloc failed."); - exit(1); + err(1, "malloc"); } va_start(ap, fmt); @@ -270,16 +274,11 @@ static char * hostname(void) { char buf[HOST_NAME_MAX]; - FILE *fp; - fp = fopen("/proc/sys/kernel/hostname", "r"); - if (fp == NULL) { - warn("Failed to open file /proc/sys/kernel/hostname"); + if (gethostname(buf, sizeof(buf)) == -1) { + warn("hostname"); return smprintf(UNKNOWN_STR); } - fgets(buf, sizeof(buf), fp); - buf[strlen(buf)-1] = '\0'; - fclose(fp); return smprintf("%s", buf); } @@ -406,20 +405,161 @@ static char * run_command(const char *cmd) { FILE *fp; - char buf[64] = "\0"; + char buf[1024] = "n/a"; fp = popen(cmd, "r"); if (fp == NULL) { warn("Failed to get command output for %s", cmd); return smprintf(UNKNOWN_STR); } - fgets(buf, sizeof(buf), fp); - buf[sizeof(buf)-1] = '\0'; + fgets(buf, sizeof(buf)-1, fp); pclose(fp); + buf[strlen(buf)] = '\0'; + strtok(buf, "\n"); + return smprintf("%s", buf); } +static char * +swap_free(void) +{ + long total, free; + FILE *fp; + char buf[2048]; + size_t bytes_read; + char *match; + + fp = fopen("/proc/meminfo", "r"); + if (fp == NULL) { + warn("Failed to open file /proc/meminfo"); + return smprintf(UNKNOWN_STR); + } + bytes_read = fread(buf, sizeof(char), sizeof(buf), fp); + buf[bytes_read] = '\0'; + fclose(fp); + if (bytes_read == 0 || bytes_read == sizeof(buf)) { + warn("Failed to read /proc/meminfo\n"); + return smprintf(UNKNOWN_STR); + } + + match = strstr(buf, "SwapTotal"); + sscanf(match, "SwapTotal: %ld kB\n", &total); + if (total == 0) { + return smprintf(UNKNOWN_STR); + } + + match = strstr(buf, "SwapFree"); + sscanf(match, "SwapFree: %ld kB\n", &free); + + return smprintf("%f", (float)free / 1024 / 1024); +} + +static char * +swap_perc(void) +{ + long total, free, cached; + FILE *fp; + char buf[2048]; + size_t bytes_read; + char *match; + + fp = fopen("/proc/meminfo", "r"); + if (fp == NULL) { + warn("Failed to open file /proc/meminfo"); + return smprintf(UNKNOWN_STR); + } + bytes_read = fread(buf, sizeof(char), sizeof(buf), fp); + buf[bytes_read] = '\0'; + fclose(fp); + if (bytes_read == 0 || bytes_read == sizeof(buf)) { + warn("Failed to read /proc/meminfo\n"); + return smprintf(UNKNOWN_STR); + } + + match = strstr(buf, "SwapTotal"); + sscanf(match, "SwapTotal: %ld kB\n", &total); + if (total == 0) { + return smprintf(UNKNOWN_STR); + } + + match = strstr(buf, "SwapCached"); + sscanf(match, "SwapCached: %ld kB\n", &cached); + + match = strstr(buf, "SwapFree"); + sscanf(match, "SwapFree: %ld kB\n", &free); + + return smprintf("%d%%", 100 * (total - free - cached) / total); +} + +static char * +swap_total(void) +{ + long total; + FILE *fp; + char buf[2048]; + size_t bytes_read; + char *match; + + fp = fopen("/proc/meminfo", "r"); + if (fp == NULL) { + warn("Failed to open file /proc/meminfo"); + return smprintf(UNKNOWN_STR); + } + bytes_read = fread(buf, sizeof(char), sizeof(buf), fp); + buf[bytes_read] = '\0'; + fclose(fp); + if (bytes_read == 0 || bytes_read == sizeof(buf)) { + warn("Failed to read /proc/meminfo\n"); + return smprintf(UNKNOWN_STR); + } + + match = strstr(buf, "SwapTotal"); + sscanf(match, "SwapTotal: %ld kB\n", &total); + if (total == 0) { + return smprintf(UNKNOWN_STR); + } + + return smprintf("%f", (float)total / 1024 / 1024); +} + +static char * +swap_used(void) +{ + long total, free, cached; + FILE *fp; + char buf[2048]; + size_t bytes_read; + char *match; + + fp = fopen("/proc/meminfo", "r"); + if (fp == NULL) { + warn("Failed to open file /proc/meminfo"); + return smprintf(UNKNOWN_STR); + } + bytes_read = fread(buf, sizeof(char), sizeof(buf), fp); + buf[bytes_read] = '\0'; + fclose(fp); + if (bytes_read == 0 || bytes_read == sizeof(buf)) { + warn("Failed to read /proc/meminfo\n"); + return smprintf(UNKNOWN_STR); + } + + match = strstr(buf, "SwapTotal"); + sscanf(match, "SwapTotal: %ld kB\n", &total); + if (total == 0) { + return smprintf(UNKNOWN_STR); + } + + match = strstr(buf, "SwapCached"); + sscanf(match, "SwapCached: %ld kB\n", &cached); + + match = strstr(buf, "SwapFree"); + sscanf(match, "SwapFree: %ld kB\n", &free); + + return smprintf("%f", (float)(total - free - cached) / 1024 / 1024); +} + static char * temp(const char *file) { @@ -475,6 +615,7 @@ uid(void) static char * vol_perc(const char *card) { + int mute; long int vol, max, min; snd_mixer_t *handle; snd_mixer_elem_t *elem; @@ -498,11 +639,17 @@ vol_perc(const char *card) snd_mixer_handle_events(handle); snd_mixer_selem_get_playback_volume_range(elem, &min, &max); snd_mixer_selem_get_playback_volume(elem, 0, &vol); + snd_mixer_selem_get_playback_switch(elem, 0, &mute); snd_mixer_selem_id_free(s_elem); snd_mixer_close(handle); - return smprintf("%d%%", ((uint_fast16_t)(vol * 100) / max)); + if (!mute) + return smprintf("mute"); + else if (max == 0) + return smprintf("0%%"); + else + return smprintf("%lu%%", ((uint_fast16_t)(vol * 100) / max)); } static char * @@ -592,15 +739,10 @@ sighandler(const int signo) static void usage(void) { - fprintf(stderr, - "slstatus (c) 2016, drkhsh\n" - "usage: %s [-dho]\n", - argv0); + fprintf(stderr, "usage: %s [-dhov]\n", argv0); exit(1); } - - int main(int argc, char *argv[]) { @@ -617,6 +759,9 @@ main(int argc, char *argv[]) case 'o': oflag = 1; break; + case 'v': + printf("slstatus %s (C) 2016 slstatus engineers\n", VERSION); + return 0; default: usage(); } ARGEND @@ -624,8 +769,8 @@ main(int argc, char *argv[]) if (dflag && oflag) { usage(); } - if (dflag) { - (void)daemon(1, 1); + if (dflag && daemon(1, 1) < 0) { + err(1, "daemon"); } memset(&act, 0, sizeof(act)); @@ -637,6 +782,8 @@ main(int argc, char *argv[]) dpy = XOpenDisplay(NULL); } + setlocale(LC_ALL, ""); + while (!done) { status_string[0] = '\0'; @@ -647,7 +794,7 @@ main(int argc, char *argv[]) } else { res = argument.func(argument.args); } - element = smprintf(argument.format, res); + element = smprintf(argument.fmt, res); if (element == NULL) { element = smprintf(UNKNOWN_STR); warnx("Failed to format output");