Xinqi Bao's Git

fixed disk percent
[slstatus.git] / slstatus.c
index 658bc79..75413a1 100644 (file)
@@ -2,10 +2,15 @@
 
 /* global libraries */
 #include <alsa/asoundlib.h>
 
 /* global libraries */
 #include <alsa/asoundlib.h>
+#include <fcntl.h>
+#include <locale.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 <sys/statvfs.h>
 #include <time.h>
 #include <unistd.h>
 #include <X11/Xlib.h>
 #include <time.h>
 #include <unistd.h>
 #include <X11/Xlib.h>
 /* 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();
 char *get_cpu_temperature();
 char *get_cpu_usage();
 char *get_datetime();
 void setstatus(char *str);
 char *smprintf(char *fmt, ...);
 char *get_battery();
 char *get_cpu_temperature();
 char *get_cpu_usage();
 char *get_datetime();
+char *get_diskusage();
 char *get_ram_usage();
 char *get_volume();
 char *get_wifi_signal();
 char *get_ram_usage();
 char *get_volume();
 char *get_wifi_signal();
@@ -27,6 +40,25 @@ char *get_wifi_signal();
 /* global variables */
 static Display *dpy;
 
 /* global variables */
 static Display *dpy;
 
+/* check configured paths */
+int
+config_check()
+{
+    struct stat fs;
+
+    /* check all files in the config.h file */
+    CHECK_FILE(batterynowfile, fs);
+    CHECK_FILE(batteryfullfile, fs);
+    CHECK_FILE(tempfile, fs);
+
+    /* check update interval */
+    if (update_interval < 1)
+        return -1;
+
+    /* exit successfully */
+    return 0;
+}
+
 /* set statusbar (WM_NAME) */
 void
 setstatus(char *str)
 /* set statusbar (WM_NAME) */
 void
 setstatus(char *str)
@@ -59,7 +91,7 @@ get_battery()
     /* open battery now file */
     if (!(fp = fopen(batterynowfile, "r"))) {
         fprintf(stderr, "Error opening battery file.");
     /* 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 */
@@ -71,7 +103,7 @@ 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 */
@@ -97,7 +129,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 */
@@ -121,7 +153,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 */
@@ -136,7 +168,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 */
@@ -162,15 +194,38 @@ get_datetime()
 
     /* get time in format */
     time(&tm);
 
     /* get time in format */
     time(&tm);
+    setlocale(LC_TIME, "");
     if(!strftime(buf, bufsize, timeformat, localtime(&tm))) {
     if(!strftime(buf, bufsize, timeformat, localtime(&tm))) {
-      fprintf(stderr, "Strftime failed.\n");
-        exit(1);
+        setlocale(LC_TIME, "C");
+        fprintf(stderr, "Strftime failed.\n");
+        return smprintf("n/a");
     }
 
     }
 
+    setlocale(LC_TIME, "C");
     /* return time */
     return smprintf("%s", buf);
 }
 
     /* return time */
     return smprintf("%s", buf);
 }
 
+/* disk usage percentage */
+char *
+get_diskusage()
+{
+    int perc = 0;
+    struct statvfs fs;
+    
+    /* try to open mountpoint */
+    if (statvfs(mountpath, &fs) < 0) {
+        fprintf(stderr, "Could not get filesystem info.\n");
+        return smprintf("n/a");
+    }
+
+    /* calculate percent */
+    perc = 100 * (1.0f - ((float)fs.f_bavail / (float)fs.f_blocks));
+
+    /* return perc */
+    return smprintf("%d%%", perc);
+}
+
 /* ram percentage */
 char *
 get_ram_usage()
 /* ram percentage */
 char *
 get_ram_usage()
@@ -182,7 +237,7 @@ get_ram_usage()
     /* open meminfo file */
     if (!(fp = fopen("/proc/meminfo", "r"))) {
         fprintf(stderr, "Error opening meminfo file.");
     /* 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 */
@@ -263,7 +318,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 */
@@ -274,13 +329,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 */
@@ -310,10 +365,17 @@ main()
     char *cpu_temperature = NULL;
     char *cpu_usage = NULL;
     char *datetime = NULL;
     char *cpu_temperature = NULL;
     char *cpu_usage = NULL;
     char *datetime = NULL;
+    char *diskusage = NULL;
     char *ram_usage = NULL;
     char *volume = NULL;
     char *wifi_signal = NULL;
 
     char *ram_usage = NULL;
     char *volume = NULL;
     char *wifi_signal = NULL;
 
+    /* check config for sanity */
+    if (config_check() < 0) {
+        fprintf(stderr, "Config error, please check paths and interval 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");
@@ -327,6 +389,7 @@ main()
         cpu_temperature = get_cpu_temperature();
         cpu_usage = get_cpu_usage();
         datetime = get_datetime();
         cpu_temperature = get_cpu_temperature();
         cpu_usage = get_cpu_usage();
         datetime = get_datetime();
+        diskusage = get_diskusage();
         ram_usage = get_ram_usage();
         volume = get_volume();
         wifi_signal = get_wifi_signal();
         ram_usage = get_ram_usage();
         volume = get_volume();
         wifi_signal = get_wifi_signal();
@@ -340,10 +403,13 @@ main()
         free(cpu_temperature);
         free(cpu_usage);
         free(datetime);
         free(cpu_temperature);
         free(cpu_usage);
         free(datetime);
+        free(diskusage);
         free(ram_usage);
         free(volume);
         free(wifi_signal);
         free(ram_usage);
         free(volume);
         free(wifi_signal);
-        sleep(update_interval);
+
+        /* wait, "update_interval - 1" because of get_cpu_usage() which uses 1 second */
+        sleep(update_interval -1);
     }
 
     /* close display */
     }
 
     /* close display */