X-Git-Url: https://git.xinqibao.xyz/slstatus.git/blobdiff_plain/b4f55e7170576a2dd800018c8690832bbc7f9bf0..aa70b6633016ed35f9fa3d91c26751e973ac5d63:/slstatus.c diff --git a/slstatus.c b/slstatus.c index 40aaa19..a9aeecb 100644 --- a/slstatus.c +++ b/slstatus.c @@ -29,7 +29,7 @@ #include "strlcat.h" #include "strlcpy.h" -typedef char *(*op_fun) (const char *); +typedef char *(*op_fun)(); struct arg { op_fun func; const char *format; @@ -78,13 +78,23 @@ setstatus(const char *str) static char * smprintf(const char *fmt, ...) { - va_list fmtargs; - char *ret = NULL; + va_list ap; + char *ret; + int len; - va_start(fmtargs, fmt); - if (vasprintf(&ret, fmt, fmtargs) < 0) - return NULL; - va_end(fmtargs); + va_start(ap, fmt); + 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; } @@ -93,31 +103,36 @@ static char * battery_perc(const char *battery) { int now, full, perc; - char batterynowfile[64] = ""; - char batteryfullfile[64] = ""; + char batterynowfile[64]; + char batteryfullfile[64]; FILE *fp; - strlcat(batterynowfile, batterypath, sizeof(batterynowfile)); + strlcpy(batterynowfile, BATTERY_PATH, sizeof(batterynowfile)); strlcat(batterynowfile, battery, sizeof(batterynowfile)); strlcat(batterynowfile, "/", sizeof(batterynowfile)); - strlcat(batterynowfile, batterynow, sizeof(batterynowfile)); + strlcat(batterynowfile, BATTERY_NOW, sizeof(batterynowfile)); - strlcat(batteryfullfile, batterypath, sizeof(batteryfullfile)); + strlcpy(batteryfullfile, BATTERY_PATH, sizeof(batteryfullfile)); strlcat(batteryfullfile, battery, sizeof(batteryfullfile)); strlcat(batteryfullfile, "/", sizeof(batteryfullfile)); - strlcat(batteryfullfile, batteryfull, sizeof(batteryfullfile)); + strlcat(batteryfullfile, BATTERY_FULL, sizeof(batteryfullfile)); - if (!(fp = fopen(batterynowfile, "r"))) { - fprintf(stderr, "Error opening battery file: %s.\n", batterynowfile); - return smprintf(unknowntext); + 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"); - return smprintf(unknowntext); + fp = fopen(batteryfullfile, "r"); + if (fp == NULL) { + fprintf(stderr, "Error opening battery file: %s\n", + strerror(errno)); + return smprintf(UNKNOWN_STR); } fscanf(fp, "%i", &full); @@ -133,11 +148,12 @@ 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"); - return smprintf(unknowntext); + 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]); @@ -146,9 +162,11 @@ cpu_perc(void) /* wait a second (for avg values) */ sleep(1); - if (!(fp = fopen("/proc/stat","r"))) { - fprintf(stderr, "Error opening stat file.\n"); - return smprintf(unknowntext); + fp = fopen("/proc/stat","r"); + if (fp == NULL) { + fprintf(stderr, "Error opening stat file: %s\n", + strerror(errno)); + return smprintf(UNKNOWN_STR); } fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &b[0], &b[1], &b[2], &b[3]); @@ -160,27 +178,14 @@ cpu_perc(void) static char * datetime(const char *timeformat) { - time_t tm; - size_t bufsize = 64; - char *buf = malloc(bufsize); - if (buf == NULL) { - fprintf(stderr, "Failed to get date/time.\n"); - return smprintf(unknowntext); - } + time_t t; + char timestr[80]; - time(&tm); - setlocale(LC_TIME, ""); - if (!strftime(buf, bufsize, timeformat, localtime(&tm))) { - setlocale(LC_TIME, "C"); - free(buf); - fprintf(stderr, "Strftime failed.\n"); - return smprintf(unknowntext); - } + t = time(NULL); + if (strftime(timestr, sizeof(timestr), timeformat, localtime(&t)) == 0) + return smprintf(UNKNOWN_STR); - setlocale(LC_TIME, "C"); - char *ret = smprintf("%s", buf); - free(buf); - return ret; + return smprintf("%s", timestr); } static char * @@ -189,8 +194,9 @@ disk_free(const char *mountpoint) struct statvfs fs; if (statvfs(mountpoint, &fs) < 0) { - fprintf(stderr, "Could not get filesystem info.\n"); - return smprintf(unknowntext); + 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); } @@ -202,8 +208,9 @@ disk_perc(const char *mountpoint) struct statvfs fs; if (statvfs(mountpoint, &fs) < 0) { - fprintf(stderr, "Could not get filesystem info.\n"); - return smprintf(unknowntext); + fprintf(stderr, "Could not get filesystem info: %s\n", + strerror(errno)); + return smprintf(UNKNOWN_STR); } perc = 100 * (1.0f - ((float)fs.f_bfree / (float)fs.f_blocks)); @@ -216,8 +223,9 @@ disk_total(const char *mountpoint) struct statvfs fs; if (statvfs(mountpoint, &fs) < 0) { - fprintf(stderr, "Could not get filesystem info.\n"); - return smprintf(unknowntext); + 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_blocks / 1024 / 1024 / 1024); @@ -229,8 +237,9 @@ disk_used(const char *mountpoint) struct statvfs fs; if (statvfs(mountpoint, &fs) < 0) { - fprintf(stderr, "Could not get filesystem info.\n"); - return smprintf(unknowntext); + 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_blocks - (float)fs.f_bfree) / 1024 / 1024 / 1024); @@ -240,11 +249,12 @@ 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"); - return smprintf(unknowntext); + if (fp == NULL) { + fprintf(stderr, "Could not open entropy file: %s\n", + strerror(errno)); + return smprintf(UNKNOWN_STR); } fscanf(fp, "%d", &entropy); @@ -255,28 +265,26 @@ entropy(void) static char * gid(void) { - gid_t gid; - - if ((gid = getgid()) < 0) { - fprintf(stderr, "Could no get gid.\n"); - return smprintf(unknowntext); - } else - return smprintf("%d", gid); - return smprintf(unknowntext); + gid_t gid = getgid(); + return smprintf("%d", gid); } 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"); - return smprintf(unknowntext); + 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); } @@ -289,8 +297,9 @@ ip(const char *interface) char host[NI_MAXHOST]; if (getifaddrs(&ifaddr) == -1) { - fprintf(stderr, "Error getting IP address.\n"); - return smprintf(unknowntext); + fprintf(stderr, "Error getting IP address: %s\n", + strerror(errno)); + return smprintf(UNKNOWN_STR); } /* get the ip address */ @@ -298,12 +307,13 @@ 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) { fprintf(stderr, "Error getting IP address.\n"); - return smprintf(unknowntext); + return smprintf(UNKNOWN_STR); } return smprintf("%s", host); } @@ -312,7 +322,7 @@ ip(const char *interface) /* free the address */ freeifaddrs(ifaddr); - return smprintf(unknowntext); + return smprintf(UNKNOWN_STR); } static char * @@ -322,7 +332,7 @@ load_avg(void) if (getloadavg(avgs, 3) < 0) { fprintf(stderr, "Error getting load avg.\n"); - return smprintf(unknowntext); + return smprintf(UNKNOWN_STR); } return smprintf("%.2f %.2f %.2f", avgs[0], avgs[1], avgs[2]); @@ -332,11 +342,12 @@ 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"); - return smprintf(unknowntext); + if (fp == NULL) { + fprintf(stderr, "Error opening meminfo file: %s\n", + strerror(errno)); + return smprintf(UNKNOWN_STR); } fscanf(fp, "MemFree: %ld kB\n", &free); @@ -349,11 +360,12 @@ 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"); - return smprintf(unknowntext); + if (fp == NULL) { + fprintf(stderr, "Error opening meminfo file: %s\n", + strerror(errno)); + return smprintf(UNKNOWN_STR); } fscanf(fp, "MemTotal: %ld kB\n", &total); @@ -370,11 +382,12 @@ 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"); - return smprintf(unknowntext); + if (fp == NULL) { + fprintf(stderr, "Error opening meminfo file: %s\n", + strerror(errno)); + return smprintf(UNKNOWN_STR); } fscanf(fp, "MemTotal: %ld kB\n", &total); @@ -386,11 +399,12 @@ 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"); - return smprintf(unknowntext); + if (fp == NULL) { + fprintf(stderr, "Error opening meminfo file: %s\n", + strerror(errno)); + return smprintf(UNKNOWN_STR); } fscanf(fp, "MemTotal: %ld kB\n", &total); @@ -407,15 +421,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); - return smprintf(unknowntext); + 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') { @@ -424,7 +439,7 @@ run_command(const char* command) } } if (good) - buffer[strlen(buffer) - 1] = '\0'; + buffer[strlen(buffer)-1] = '\0'; return smprintf("%s", buffer); } @@ -432,11 +447,12 @@ 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"); - return smprintf(unknowntext); + if (fp == NULL) { + fprintf(stderr, "Could not open temperature file: %s\n", + strerror(errno)); + return smprintf(UNKNOWN_STR); } fscanf(fp, "%d", &temperature); @@ -470,16 +486,18 @@ username(void) if (pw) return smprintf("%s", pw->pw_name); else { - fprintf(stderr, "Could not get username.\n"); - return smprintf(unknowntext); + fprintf(stderr, "Could not get username: %s\n", + strerror(errno)); + return smprintf(UNKNOWN_STR); } - return smprintf(unknowntext); + return smprintf(UNKNOWN_STR); } static char * uid(void) { + /* FIXME: WHY USE register modifier? */ register uid_t uid; uid = geteuid(); @@ -488,10 +506,10 @@ uid(void) return smprintf("%d", uid); else { fprintf(stderr, "Could not get uid.\n"); - return smprintf(unknowntext); + return smprintf(UNKNOWN_STR); } - return smprintf(unknowntext); + return smprintf(UNKNOWN_STR); } @@ -513,10 +531,10 @@ vol_perc(const char *soundcard) snd_mixer_selem_id_malloc(&mute_info); if (vol_info == NULL || mute_info == NULL) { fprintf(stderr, "Could not get alsa volume.\n"); - return smprintf(unknowntext); + return smprintf(UNKNOWN_STR); } - snd_mixer_selem_id_set_name(vol_info, channel); - snd_mixer_selem_id_set_name(mute_info, channel); + 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); @@ -540,41 +558,46 @@ vol_perc(const char *soundcard) 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"); - return smprintf(unknowntext); + fp = fopen(path, "r"); + + if(fp == NULL) { + fprintf(stderr, "Error opening wifi operstate file: %s\n", + strerror(errno)); + return smprintf(UNKNOWN_STR); } fgets(status, 5, fp); fclose(fp); if(strcmp(status, "up\n") != 0) - return smprintf(unknowntext); + return smprintf(UNKNOWN_STR); - if (!(fp = fopen("/proc/net/wireless", "r"))) { - fprintf(stderr, "Error opening wireless file.\n"); - return smprintf(unknowntext); + 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); } @@ -587,24 +610,26 @@ 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); - return smprintf(unknowntext); + 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); - return smprintf(unknowntext); + fprintf(stderr, "Get ESSID ioctl failed for interface %s: %s\n", + wificard, strerror(errno)); + return smprintf(UNKNOWN_STR); } if (strcmp((char *)wreq.u.essid.pointer, "") == 0) - return smprintf(unknowntext); + return smprintf(UNKNOWN_STR); else return smprintf("%s", (char *)wreq.u.essid.pointer); } @@ -612,25 +637,28 @@ wifi_essid(const char *wificard) int main(void) { - char status_string[1024]; + size_t i; + char status_string[4096]; + char *res, *element; struct arg argument; - if (!(dpy = XOpenDisplay(0x0))) { + dpy = XOpenDisplay(0x0); + if (!dpy) { fprintf(stderr, "Cannot open display!\n"); exit(1); } for (;;) { memset(status_string, 0, sizeof(status_string)); - for (size_t i = 0; i < sizeof(args) / sizeof(args[0]); ++i) { + for (i = 0; i < sizeof(args) / sizeof(args[0]); ++i) { argument = args[i]; if (argument.args == NULL) - char *res = argument.func(); + res = argument.func(); else - char *res = argument.func(argument.args); - char *element = smprintf(argument.format, res); + res = argument.func(argument.args); + element = smprintf(argument.format, res); if (element == NULL) { - element = smprintf(unknowntext); + element = smprintf(UNKNOWN_STR); fprintf(stderr, "Failed to format output.\n"); } strlcat(status_string, element, sizeof(status_string)); @@ -639,7 +667,7 @@ main(void) } setstatus(status_string); - sleep(update_interval -1); + sleep(UPDATE_INTERVAL -1); } XCloseDisplay(dpy);