X-Git-Url: https://git.xinqibao.xyz/slstatus.git/blobdiff_plain/1d257999ed6049dce4d1305c4dc3304ea9910ca7..a4beda8eb9bdf34d055ba52c39b15b9580b27e6a:/slstatus.c diff --git a/slstatus.c b/slstatus.c index 628c0f4..441c517 100644 --- a/slstatus.c +++ b/slstatus.c @@ -1,12 +1,10 @@ /* See LICENSE file for copyright and license details. */ #include -#include #include #include #include #include -#include #include #include #include @@ -29,14 +27,12 @@ #include "strlcat.h" #include "strlcpy.h" -typedef char *(*op_fun)(); struct arg { - op_fun func; + char *(*func)(); const char *format; const char *args; }; -static void setstatus(const char *); static char *smprintf(const char *, ...); static char *battery_perc(const char *); static char *cpu_perc(void); @@ -67,28 +63,27 @@ static Display *dpy; #include "config.h" -static void -setstatus(const char *str) -{ - /* set WM_NAME via X11 */ - XStoreName(dpy, DefaultRootWindow(dpy), str); - XSync(dpy, False); -} - static char * smprintf(const char *fmt, ...) { - /* FIXME: This code should have - bound checks, it is vulnerable to - buffer overflows */ va_list ap; - char *ret = NULL; + char *ret; + int len; va_start(ap, fmt); - if (vasprintf(&ret, fmt, ap) < 0) - return NULL; + len = vsnprintf(NULL, 0, fmt, ap); + va_end(ap); + ret = malloc(++len); + if (ret == NULL) { + perror("malloc"); + exit(1); + } + + va_start(ap, fmt); + vsnprintf(ret, len, fmt, ap); va_end(ap); + return ret; } @@ -110,16 +105,21 @@ battery_perc(const char *battery) strlcat(batteryfullfile, "/", sizeof(batteryfullfile)); strlcat(batteryfullfile, BATTERY_FULL, sizeof(batteryfullfile)); - if (!(fp = fopen(batterynowfile, "r"))) { - fprintf(stderr, "Error opening battery file: %s.\n", batterynowfile); + fp = fopen(batterynowfile, "r"); + if (fp == NULL ) { + fprintf(stderr, "Error opening battery file: %s: %s\n", + batterynowfile, + strerror(errno)); return smprintf(UNKNOWN_STR); } fscanf(fp, "%i", &now); fclose(fp); - if (!(fp = fopen(batteryfullfile, "r"))) { - fprintf(stderr, "Error opening battery file.\n"); + fp = fopen(batteryfullfile, "r"); + if (fp == NULL) { + fprintf(stderr, "Error opening battery file: %s\n", + strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -136,21 +136,23 @@ cpu_perc(void) { int perc; long double a[4], b[4]; - FILE *fp; + FILE *fp = fopen("/proc/stat","r"); - if (!(fp = fopen("/proc/stat","r"))) { - fprintf(stderr, "Error opening stat file.\n"); + if (fp == NULL) { + fprintf(stderr, "Error opening stat file: %s\n", + strerror(errno)); return smprintf(UNKNOWN_STR); } fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &a[0], &a[1], &a[2], &a[3]); fclose(fp); - /* wait a second (for avg values) */ sleep(1); - if (!(fp = fopen("/proc/stat","r"))) { - fprintf(stderr, "Error opening stat file.\n"); + fp = fopen("/proc/stat","r"); + if (fp == NULL) { + fprintf(stderr, "Error opening stat file: %s\n", + strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -179,7 +181,8 @@ disk_free(const char *mountpoint) struct statvfs fs; if (statvfs(mountpoint, &fs) < 0) { - fprintf(stderr, "Could not get filesystem info.\n"); + fprintf(stderr, "Could not get filesystem info: %s\n", + strerror(errno)); return smprintf(UNKNOWN_STR); } return smprintf("%f", (float)fs.f_bsize * (float)fs.f_bfree / 1024 / 1024 / 1024); @@ -192,7 +195,8 @@ disk_perc(const char *mountpoint) struct statvfs fs; if (statvfs(mountpoint, &fs) < 0) { - fprintf(stderr, "Could not get filesystem info.\n"); + fprintf(stderr, "Could not get filesystem info: %s\n", + strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -206,7 +210,8 @@ disk_total(const char *mountpoint) struct statvfs fs; if (statvfs(mountpoint, &fs) < 0) { - fprintf(stderr, "Could not get filesystem info.\n"); + fprintf(stderr, "Could not get filesystem info: %s\n", + strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -219,7 +224,8 @@ disk_used(const char *mountpoint) struct statvfs fs; if (statvfs(mountpoint, &fs) < 0) { - fprintf(stderr, "Could not get filesystem info.\n"); + fprintf(stderr, "Could not get filesystem info: %s\n", + strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -230,10 +236,11 @@ static char * entropy(void) { int entropy = 0; - FILE *fp; + FILE *fp = fopen("/proc/sys/kernel/random/entropy_avail", "r"); - if (!(fp = fopen("/proc/sys/kernel/random/entropy_avail", "r"))) { - fprintf(stderr, "Could not open entropy file.\n"); + if (fp == NULL) { + fprintf(stderr, "Could not open entropy file: %s\n", + strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -245,22 +252,25 @@ entropy(void) static char * gid(void) { - gid_t gid = getgid(); - return smprintf("%d", gid); + return smprintf("%d", getgid()); } static char * hostname(void) { char hostname[HOST_NAME_MAX]; - FILE *fp; + FILE *fp = fopen("/proc/sys/kernel/hostname", "r"); - if (!(fp = fopen("/proc/sys/kernel/hostname", "r"))) { - fprintf(stderr, "Could not open hostname file.\n"); + if (fp == NULL) { + fprintf(stderr, "Could not open hostname file: %s\n", + strerror(errno)); return smprintf(UNKNOWN_STR); } - fscanf(fp, "%s\n", hostname); + fgets(hostname, sizeof(hostname), fp); + /* FIXME: needs improvement */ + memset(&hostname[strlen(hostname)-1], '\0', + sizeof(hostname) - strlen(hostname)); fclose(fp); return smprintf("%s", hostname); } @@ -273,7 +283,8 @@ ip(const char *interface) char host[NI_MAXHOST]; if (getifaddrs(&ifaddr) == -1) { - fprintf(stderr, "Error getting IP address.\n"); + fprintf(stderr, "Error getting IP address: %s\n", + strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -282,7 +293,8 @@ ip(const char *interface) if (ifa->ifa_addr == NULL) continue; - s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST); + s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST, + NULL, 0, NI_NUMERICHOST); if ((strcmp(ifa->ifa_name, interface) == 0) && (ifa->ifa_addr->sa_family == AF_INET)) { if (s != 0) { @@ -316,10 +328,11 @@ static char * ram_free(void) { long free; - FILE *fp; + FILE *fp = fopen("/proc/meminfo", "r"); - if (!(fp = fopen("/proc/meminfo", "r"))) { - fprintf(stderr, "Error opening meminfo file.\n"); + if (fp == NULL) { + fprintf(stderr, "Error opening meminfo file: %s\n", + strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -333,10 +346,11 @@ ram_perc(void) { int perc; long total, free, buffers, cached; - FILE *fp; + FILE *fp = fopen("/proc/meminfo", "r"); - if (!(fp = fopen("/proc/meminfo", "r"))) { - fprintf(stderr, "Error opening meminfo file.\n"); + if (fp == NULL) { + fprintf(stderr, "Error opening meminfo file: %s\n", + strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -354,10 +368,11 @@ static char * ram_total(void) { long total; - FILE *fp; + FILE *fp = fopen("/proc/meminfo", "r"); - if (!(fp = fopen("/proc/meminfo", "r"))) { - fprintf(stderr, "Error opening meminfo file.\n"); + if (fp == NULL) { + fprintf(stderr, "Error opening meminfo file: %s\n", + strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -370,10 +385,11 @@ static char * ram_used(void) { long free, total, buffers, cached, used; - FILE *fp; + FILE *fp = fopen("/proc/meminfo", "r"); - if (!(fp = fopen("/proc/meminfo", "r"))) { - fprintf(stderr, "Error opening meminfo file.\n"); + if (fp == NULL) { + fprintf(stderr, "Error opening meminfo file: %s\n", + strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -391,15 +407,16 @@ static char * run_command(const char* command) { int good; - FILE *fp; + FILE *fp = popen(command, "r"); char buffer[64]; - if (!(fp = popen(command, "r"))) { - fprintf(stderr, "Could not get command output for: %s.\n", command); + if (fp == NULL) { + fprintf(stderr, "Could not get command output for: %s: %s\n", + command, strerror(errno)); return smprintf(UNKNOWN_STR); } - fgets(buffer, sizeof(buffer) - 1, fp); + fgets(buffer, sizeof(buffer)-1, fp); pclose(fp); for (int i = 0 ; i != sizeof(buffer); i++) { if (buffer[i] == '\0') { @@ -408,7 +425,7 @@ run_command(const char* command) } } if (good) - buffer[strlen(buffer) - 1] = '\0'; + buffer[strlen(buffer)-1] = '\0'; return smprintf("%s", buffer); } @@ -416,10 +433,11 @@ static char * temp(const char *file) { int temperature; - FILE *fp; + FILE *fp = fopen(file, "r"); - if (!(fp = fopen(file, "r"))) { - fprintf(stderr, "Could not open temperature file.\n"); + if (fp == NULL) { + fprintf(stderr, "Could not open temperature file: %s\n", + strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -445,101 +463,77 @@ uptime(void) static char * username(void) { - register struct passwd *pw; - register uid_t uid; + uid_t uid = geteuid(); + struct passwd *pw = getpwuid(uid); - uid = geteuid(); - pw = getpwuid(uid); - - if (pw) + if (pw == NULL) return smprintf("%s", pw->pw_name); - else { - fprintf(stderr, "Could not get username.\n"); - return smprintf(UNKNOWN_STR); - } + fprintf(stderr, "Could not get username: %s\n", + strerror(errno)); return smprintf(UNKNOWN_STR); } static char * uid(void) { - register uid_t uid; - - uid = geteuid(); - - if (uid) - return smprintf("%d", uid); - else { - fprintf(stderr, "Could not get uid.\n"); - return smprintf(UNKNOWN_STR); - } - - return smprintf(UNKNOWN_STR); + return smprintf("%d", geteuid()); } -static char * -vol_perc(const char *soundcard) -{ - int mute = 0; - long vol = 0, max = 0, min = 0; +static char * +vol_perc(const char *snd_card) +{ /* thanks to botika for this function */ + long int vol, max, min; snd_mixer_t *handle; - snd_mixer_elem_t *pcm_mixer, *mas_mixer; - snd_mixer_selem_id_t *vol_info, *mute_info; + snd_mixer_elem_t *elem; + snd_mixer_selem_id_t *s_elem; snd_mixer_open(&handle, 0); - snd_mixer_attach(handle, soundcard); + snd_mixer_attach(handle, snd_card); snd_mixer_selem_register(handle, NULL, NULL); snd_mixer_load(handle); + snd_mixer_selem_id_malloc(&s_elem); + snd_mixer_selem_id_set_name(s_elem, "Master"); + elem = snd_mixer_find_selem(handle, s_elem); - snd_mixer_selem_id_malloc(&vol_info); - snd_mixer_selem_id_malloc(&mute_info); - if (vol_info == NULL || mute_info == NULL) { - fprintf(stderr, "Could not get alsa volume.\n"); + if (elem == NULL) { + snd_mixer_selem_id_free(s_elem); + snd_mixer_close(handle); + perror("alsa error: "); return smprintf(UNKNOWN_STR); } - snd_mixer_selem_id_set_name(vol_info, ALSA_CHANNEL); - snd_mixer_selem_id_set_name(mute_info, ALSA_CHANNEL); - pcm_mixer = snd_mixer_find_selem(handle, vol_info); - mas_mixer = snd_mixer_find_selem(handle, mute_info); - - snd_mixer_selem_get_playback_volume_range((snd_mixer_elem_t *)pcm_mixer, &min, &max); - snd_mixer_selem_get_playback_volume((snd_mixer_elem_t *)pcm_mixer, SND_MIXER_SCHN_MONO, &vol); - snd_mixer_selem_get_playback_switch(mas_mixer, SND_MIXER_SCHN_MONO, &mute); - - if (vol_info) - snd_mixer_selem_id_free(vol_info); - if (mute_info) - snd_mixer_selem_id_free(mute_info); - if (handle) - snd_mixer_close(handle); - if (!mute) - return smprintf("mute"); - else - return smprintf("%d%%", (vol * 100) / max); + 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_id_free(s_elem); + snd_mixer_close(handle); + + return smprintf("%d", (vol * 100) / max); } static char * wifi_perc(const char *wificard) { - int bufsize = 255; int strength; - char buf[bufsize]; + char buf[255]; char *datastart; char path[64]; char status[5]; - char needle[sizeof wificard + 1]; + char needle[strlen(wificard)+2]; FILE *fp; - memset(path, 0, sizeof path); - strlcat(path, "/sys/class/net/", sizeof(path)); + strlcpy(path, "/sys/class/net/", sizeof(path)); strlcat(path, wificard, sizeof(path)); strlcat(path, "/operstate", sizeof(path)); - if(!(fp = fopen(path, "r"))) { - fprintf(stderr, "Error opening wifi operstate file.\n"); + fp = fopen(path, "r"); + + if(fp == NULL) { + fprintf(stderr, "Error opening wifi operstate file: %s\n", + strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -548,17 +542,21 @@ wifi_perc(const char *wificard) if(strcmp(status, "up\n") != 0) return smprintf(UNKNOWN_STR); - if (!(fp = fopen("/proc/net/wireless", "r"))) { - fprintf(stderr, "Error opening wireless file.\n"); + fp = fopen("/proc/net/wireless", "r"); + if (fp == NULL) { + fprintf(stderr, "Error opening wireless file: %s\n", + strerror(errno)); return smprintf(UNKNOWN_STR); } strlcpy(needle, wificard, sizeof(needle)); strlcat(needle, ":", sizeof(needle)); - fgets(buf, bufsize, fp); - fgets(buf, bufsize, fp); - fgets(buf, bufsize, fp); - if ((datastart = strstr(buf, needle)) != NULL) { + fgets(buf, sizeof(buf), fp); + fgets(buf, sizeof(buf), fp); + fgets(buf, sizeof(buf), fp); + + datastart = strstr(buf, needle); + if (datastart != NULL) { datastart = strstr(buf, ":"); sscanf(datastart + 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", &strength); } @@ -571,19 +569,21 @@ static char * wifi_essid(const char *wificard) { char id[IW_ESSID_MAX_SIZE+1]; - int sockfd; + int sockfd = socket(AF_INET, SOCK_DGRAM, 0); struct iwreq wreq; memset(&wreq, 0, sizeof(struct iwreq)); wreq.u.essid.length = IW_ESSID_MAX_SIZE+1; sprintf(wreq.ifr_name, wificard); - if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { - fprintf(stderr, "Cannot open socket for interface: %s\n", wificard); + if(sockfd == -1) { + fprintf(stderr, "Cannot open socket for interface: %s: %s\n", + wificard, strerror(errno)); return smprintf(UNKNOWN_STR); } wreq.u.essid.pointer = id; if (ioctl(sockfd,SIOCGIWESSID, &wreq) == -1) { - fprintf(stderr, "Get ESSID ioctl failed for interface %s\n", wificard); + fprintf(stderr, "Get ESSID ioctl failed for interface %s: %s\n", + wificard, strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -601,10 +601,8 @@ main(void) char *res, *element; struct arg argument; - if (!(dpy = XOpenDisplay(0x0))) { - fprintf(stderr, "Cannot open display!\n"); - exit(1); - } + stderr = stderr; + dpy = XOpenDisplay(NULL); for (;;) { memset(status_string, 0, sizeof(status_string)); @@ -623,11 +621,11 @@ main(void) free(res); free(element); } - - setstatus(status_string); - sleep(UPDATE_INTERVAL -1); } + XStoreName(dpy, DefaultRootWindow(dpy), status_string); + XSync(dpy, False); XCloseDisplay(dpy); + return 0; }