Xinqi Bao's Git
projects
/
slstatus.git
/ blobdiff
summary
|
log
|
commit
|
diff
|
tree
raw
|
inline
| side by side
fixed some small things (update_interval) interferes with get_cpu_usage() and reordered)
[slstatus.git]
/
slstatus.c
diff --git
a/slstatus.c
b/slstatus.c
index
4f2a2fe
..
86d27eb
100644
(file)
--- a/
slstatus.c
+++ b/
slstatus.c
@@
-2,10
+2,13
@@
/* global libraries */
#include <alsa/asoundlib.h>
/* global libraries */
#include <alsa/asoundlib.h>
+#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <time.h>
#include <unistd.h>
#include <X11/Xlib.h>
@@
-13,7
+16,14
@@
/* local libraries */
#include "config.h"
/* local libraries */
#include "config.h"
+/* check file macro */
+#define CHECK_FILE(X,Y) do { \
+ if (stat(X,&Y) < 0) return -1; \
+ if (!S_ISREG(Y.st_mode)) return -1; \
+} while (0);
+
/* functions */
/* functions */
+int config_check();
void setstatus(char *str);
char *smprintf(char *fmt, ...);
char *get_battery();
void setstatus(char *str);
char *smprintf(char *fmt, ...);
char *get_battery();
@@
-27,6
+37,17
@@
char *get_wifi_signal();
/* global variables */
static Display *dpy;
/* global variables */
static Display *dpy;
+/* check configured paths */
+int
+config_check()
+{
+ struct stat fs;
+ CHECK_FILE(batterynowfile, fs);
+ CHECK_FILE(batteryfullfile, fs);
+ CHECK_FILE(tempfile, fs);
+ return 0;
+}
+
/* set statusbar (WM_NAME) */
void
setstatus(char *str)
/* set statusbar (WM_NAME) */
void
setstatus(char *str)
@@
-40,21
+61,10
@@
char *
smprintf(char *fmt, ...)
{
va_list fmtargs;
smprintf(char *fmt, ...)
{
va_list fmtargs;
- char *ret;
- int len;
-
+ char *ret = NULL;
va_start(fmtargs, fmt);
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);
+ if (vasprintf(&ret, fmt, fmtargs) < 0)
+ return NULL;
va_end(fmtargs);
return ret;
va_end(fmtargs);
return ret;
@@
-64,17
+74,17
@@
smprintf(char *fmt, ...)
char *
get_battery()
{
char *
get_battery()
{
- int
battery_now, battery_full, battery_
perc;
+ int
now, full,
perc;
FILE *fp;
/* open battery now file */
if (!(fp = fopen(batterynowfile, "r"))) {
fprintf(stderr, "Error opening battery file.");
FILE *fp;
/* open battery now file */
if (!(fp = fopen(batterynowfile, "r"))) {
fprintf(stderr, "Error opening battery file.");
-
exit(1
);
+
return smprintf("n/a"
);
}
/* read value */
}
/* read value */
- fscanf(fp, "%i", &
battery_
now);
+ fscanf(fp, "%i", &now);
/* close battery now file */
fclose(fp);
/* close battery now file */
fclose(fp);
@@
-82,20
+92,20
@@
get_battery()
/* open battery full file */
if (!(fp = fopen(batteryfullfile, "r"))) {
fprintf(stderr, "Error opening battery file.");
/* open battery full file */
if (!(fp = fopen(batteryfullfile, "r"))) {
fprintf(stderr, "Error opening battery file.");
-
exit(1
);
+
return smprintf("n/a"
);
}
/* read value */
}
/* read value */
- fscanf(fp, "%i", &
battery_
full);
+ fscanf(fp, "%i", &full);
/* close battery full file */
fclose(fp);
/* calculate percent */
/* 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 */
}
/* cpu temperature */
@@
-108,7
+118,7
@@
get_cpu_temperature()
/* open temperature file */
if (!(fp = fopen(tempfile, "r"))) {
fprintf(stderr, "Could not open temperature file.\n");
/* open temperature file */
if (!(fp = fopen(tempfile, "r"))) {
fprintf(stderr, "Could not open temperature file.\n");
-
exit(1
);
+
return smprintf("n/a"
);
}
/* extract temperature */
}
/* extract temperature */
@@
-125,14
+135,14
@@
get_cpu_temperature()
char *
get_cpu_usage()
{
char *
get_cpu_usage()
{
- int
cpu_
perc;
+ int perc;
long double a[4], b[4];
FILE *fp;
/* open stat file */
if (!(fp = fopen("/proc/stat","r"))) {
fprintf(stderr, "Error opening stat file.");
long double a[4], b[4];
FILE *fp;
/* open stat file */
if (!(fp = fopen("/proc/stat","r"))) {
fprintf(stderr, "Error opening stat file.");
-
exit(1
);
+
return smprintf("n/a"
);
}
/* read values */
}
/* read values */
@@
-147,7
+157,7
@@
get_cpu_usage()
/* open stat file */
if (!(fp = fopen("/proc/stat","r"))) {
fprintf(stderr, "Error opening stat file.");
/* open stat file */
if (!(fp = fopen("/proc/stat","r"))) {
fprintf(stderr, "Error opening stat file.");
-
exit(1
);
+
return smprintf("n/a"
);
}
/* read values */
}
/* read values */
@@
-157,10
+167,10
@@
get_cpu_usage()
fclose(fp);
/* calculate avg in this second */
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 */
}
/* date and time */
@@
-168,32
+178,32
@@
char *
get_datetime()
{
time_t tm;
get_datetime()
{
time_t tm;
- size_t bufsize =
19
;
+ size_t bufsize =
64
;
char *buf = malloc(bufsize);
/* get time in format */
time(&tm);
if(!strftime(buf, bufsize, timeformat, localtime(&tm))) {
fprintf(stderr, "Strftime failed.\n");
char *buf = malloc(bufsize);
/* get time in format */
time(&tm);
if(!strftime(buf, bufsize, timeformat, localtime(&tm))) {
fprintf(stderr, "Strftime failed.\n");
-
exit(1
);
+
return smprintf("n/a"
);
}
/* return time */
}
/* return time */
- return
buf
;
+ return
smprintf("%s", buf)
;
}
/* ram percentage */
char *
get_ram_usage()
{
}
/* ram percentage */
char *
get_ram_usage()
{
- int
ram_
perc;
+ int perc;
long total, free, buffers, cached;
FILE *fp;
/* open meminfo file */
if (!(fp = fopen("/proc/meminfo", "r"))) {
fprintf(stderr, "Error opening meminfo file.");
long total, free, buffers, cached;
FILE *fp;
/* open meminfo file */
if (!(fp = fopen("/proc/meminfo", "r"))) {
fprintf(stderr, "Error opening meminfo file.");
-
exit(1
);
+
return smprintf("n/a"
);
}
/* read the values */
}
/* read the values */
@@
-206,10
+216,10
@@
get_ram_usage()
fclose(fp);
/* calculate percentage */
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);
+ /* return perc as string */
+ return smprintf("%d%%",
perc);
}
/* alsa volume percentage */
}
/* alsa volume percentage */
@@
-266,7
+276,6
@@
get_wifi_signal()
FILE *fp;
/* generate the path name */
FILE *fp;
/* generate the path name */
-
memset(path, 0, sizeof path);
strcat(path, path_start);
strcat(path, wificard);
memset(path, 0, sizeof path);
strcat(path, path_start);
strcat(path, wificard);
@@
-275,7
+284,7
@@
get_wifi_signal()
/* open wifi file */
if(!(fp = fopen(path, "r"))) {
fprintf(stderr, "Error opening wifi operstate file.");
/* open wifi file */
if(!(fp = fopen(path, "r"))) {
fprintf(stderr, "Error opening wifi operstate file.");
-
exit(1
);
+
return smprintf("n/a"
);
}
/* read the status */
}
/* read the status */
@@
-286,13
+295,13
@@
get_wifi_signal()
/* check if interface down */
if(strcmp(status, "up\n") != 0){
/* check if interface down */
if(strcmp(status, "up\n") != 0){
- return
"n/a"
;
+ return
smprintf("n/a")
;
}
/* open wifi file */
if (!(fp = fopen("/proc/net/wireless", "r"))) {
fprintf(stderr, "Error opening wireless file.");
}
/* open wifi file */
if (!(fp = fopen("/proc/net/wireless", "r"))) {
fprintf(stderr, "Error opening wireless file.");
-
exit(1
);
+
return smprintf("n/a"
);
}
/* extract the signal strength */
}
/* extract the signal strength */
@@
-326,6
+335,12
@@
main()
char *volume = NULL;
char *wifi_signal = NULL;
char *volume = NULL;
char *wifi_signal = NULL;
+ /* check config for sanity */
+ if (config_check() < 0) {
+ fprintf(stderr, "Config error, please check paths and recompile\n");
+ exit(1);
+ }
+
/* open display */
if (!(dpy = XOpenDisplay(0x0))) {
fprintf(stderr, "Cannot open display!\n");
/* open display */
if (!(dpy = XOpenDisplay(0x0))) {
fprintf(stderr, "Cannot open display!\n");
@@
-355,6
+370,9
@@
main()
free(ram_usage);
free(volume);
free(wifi_signal);
free(ram_usage);
free(volume);
free(wifi_signal);
+
+ /* wait, "update_interval - 1" because of get_cpu_usage() which uses 1 second */
+ sleep(update_interval -1);
}
/* close display */
}
/* close display */