diff options
Diffstat (limited to 'slstatus.c')
-rw-r--r-- | slstatus.c | 101 |
1 files changed, 50 insertions, 51 deletions
@@ -15,12 +15,12 @@ /* functions */ void setstatus(char *str); +char *smprintf(char *fmt, ...); char *get_battery(); char *get_cpu_temperature(); char *get_cpu_usage(); char *get_datetime(); char *get_ram_usage(); -char *smprintf(char *fmt, ...); char *get_volume(); char *get_wifi_signal(); @@ -35,11 +35,36 @@ setstatus(char *str) XSync(dpy, False); } +/* smprintf function */ +char * +smprintf(char *fmt, ...) +{ + va_list fmtargs; + char *ret; + int len; + + va_start(fmtargs, fmt); + len = vsnprintf(NULL, 0, fmt, fmtargs); + va_end(fmtargs); + + ret = malloc(++len); + if (ret == NULL) { + fprintf(stderr, "Malloc error."); + exit(1); + } + + va_start(fmtargs, fmt); + vsnprintf(ret, len, fmt, fmtargs); + va_end(fmtargs); + + return ret; +} + /* battery percentage */ char * get_battery() { - int battery_now, battery_full, battery_perc; + int now, full, perc; FILE *fp; /* open battery now file */ @@ -49,7 +74,7 @@ get_battery() } /* read value */ - fscanf(fp, "%i", &battery_now); + fscanf(fp, "%i", &now); /* close battery now file */ fclose(fp); @@ -61,16 +86,16 @@ get_battery() } /* read value */ - fscanf(fp, "%i", &battery_full); + fscanf(fp, "%i", &full); /* close battery full file */ fclose(fp); /* calculate percent */ - battery_perc = battery_now / (battery_full / 100); + perc = now / (full / 100); - /* return batt_perc as string */ - return smprintf("%d%%", battery_perc); + /* return perc as string */ + return smprintf("%d%%", perc); } /* cpu temperature */ @@ -100,7 +125,7 @@ get_cpu_temperature() char * get_cpu_usage() { - int cpu_perc; + int perc; long double a[4], b[4]; FILE *fp; @@ -132,10 +157,10 @@ get_cpu_usage() fclose(fp); /* calculate avg in this second */ - cpu_perc = 100 * ((b[0]+b[1]+b[2]) - (a[0]+a[1]+a[2])) / ((b[0]+b[1]+b[2]+b[3]) - (a[0]+a[1]+a[2]+a[3])); + perc = 100 * ((b[0]+b[1]+b[2]) - (a[0]+a[1]+a[2])) / ((b[0]+b[1]+b[2]+b[3]) - (a[0]+a[1]+a[2]+a[3])); - /* return cpu_perc as string */ - return smprintf("%d%%", cpu_perc); + /* return perc as string */ + return smprintf("%d%%", perc); } /* date and time */ @@ -154,14 +179,14 @@ get_datetime() } /* return time */ - return buf; + return smprintf("%s", buf); } /* ram percentage */ char * get_ram_usage() { - int ram_perc; + int perc; long total, free, buffers, cached; FILE *fp; @@ -181,35 +206,10 @@ get_ram_usage() fclose(fp); /* calculate percentage */ - ram_perc = 100 * ((total - free) - (buffers + cached)) / total; + perc = 100 * ((total - free) - (buffers + cached)) / total; - /* return ram_perc as string */ - return smprintf("%d%%",ram_perc); -} - -/* smprintf function */ -char * -smprintf(char *fmt, ...) -{ - va_list fmtargs; - char *ret; - int len; - - va_start(fmtargs, fmt); - len = vsnprintf(NULL, 0, fmt, fmtargs); - va_end(fmtargs); - - ret = malloc(++len); - if (ret == NULL) { - fprintf(stderr, "Malloc error."); - exit(1); - } - - va_start(fmtargs, fmt); - vsnprintf(ret, len, fmt, fmtargs); - va_end(fmtargs); - - return ret; + /* return perc as string */ + return smprintf("%d%%", perc); } /* alsa volume percentage */ @@ -266,7 +266,6 @@ get_wifi_signal() FILE *fp; /* generate the path name */ - memset(path, 0, sizeof path); strcat(path, path_start); strcat(path, wificard); @@ -318,13 +317,13 @@ int main() { char status[1024]; - char *wifi_signal = NULL; char *battery = NULL; - char *cpu_usage = NULL; char *cpu_temperature = NULL; + char *cpu_usage = NULL; + char *datetime = NULL; char *ram_usage = NULL; char *volume = NULL; - char *datetime = NULL; + char *wifi_signal = NULL; /* open display */ if (!(dpy = XOpenDisplay(0x0))) { @@ -335,26 +334,26 @@ main() /* return status every second */ for (;;) { /* assign the values */ - wifi_signal = get_wifi_signal(); battery = get_battery(); - cpu_usage = get_cpu_usage(); cpu_temperature = get_cpu_temperature(); + cpu_usage = get_cpu_usage(); + datetime = get_datetime(); ram_usage = get_ram_usage(); volume = get_volume(); - datetime = get_datetime(); + wifi_signal = get_wifi_signal(); /* return the status */ sprintf(status, FORMATSTRING, ARGUMENTS); setstatus(status); /* free the values */ - free(wifi_signal); free(battery); - free(cpu_usage); free(cpu_temperature); + free(cpu_usage); + free(datetime); free(ram_usage); free(volume); - free(datetime); + free(wifi_signal); } /* close display */ |