]> git.sur5r.net Git - i3/i3status/blobdiff - wmiistatus.c
Fix warnings due to unused variables on NetBSD
[i3/i3status] / wmiistatus.c
index 15434b97dc98e4968c1ee75ed6dbe18f924033d8..13f9e85996d3d2490afe6f0022d2a66d1ddcff7c 100644 (file)
 #include <glob.h>
 #include <dirent.h>
 #include <getopt.h>
+
 #ifdef LINUX
 #include <linux/ethtool.h>
 #include <linux/sockios.h>
+#else
+/* TODO: correctly check for *BSD */
+#include <sys/param.h>
+#include <sys/sysctl.h>
+#include <sys/resource.h>
 #endif
 
 #define _IS_WMIISTATUS_C
@@ -296,7 +302,7 @@ static char *get_wireless_info() {
        memset(part, 0, sizeof(part));
 
        if ((fd = open("/proc/net/wireless", O_RDONLY)) == -1)
-               die("Could not open /proc/net/wireless");
+               die("Could not open /proc/net/wireless\n");
        (void)read(fd, buf, sizeof(buf));
        (void)close(fd);
 
@@ -384,19 +390,19 @@ static char *get_eth_info() {
 }
 
 /*
- * Checks if the PID in path is still valid by checking if /proc/<pid> exists
+ * Checks if the PID in path is still valid by checking:
+ *  (Linux) if /proc/<pid> exists
+ *  (NetBSD) if sysctl returns process infos for this pid
  *
  */
 static bool process_runs(const char *path) {
-       char pidbuf[512],
-            procbuf[512];
+       char pidbuf[16];
        static glob_t globbuf;
-       struct stat statbuf;
        int fd;
        memset(pidbuf, 0, sizeof(pidbuf));
 
        if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0)
-               die("glob() failed");
+               die("glob() failed\n");
        fd = open((globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path), O_RDONLY);
        globfree(&globbuf);
        if (fd < 0)
@@ -404,15 +410,26 @@ static bool process_runs(const char *path) {
        (void)read(fd, pidbuf, sizeof(pidbuf));
        (void)close(fd);
 
+#ifdef LINUX
+       struct stat statbuf;
+       char procbuf[512];
        (void)snprintf(procbuf, sizeof(procbuf), "/proc/%ld", strtol(pidbuf, NULL, 10));
        return (stat(procbuf, &statbuf) >= 0);
+#else
+       /* TODO: correctly check for NetBSD. Evaluate if this runs on OpenBSD/FreeBSD */
+       struct kinfo_proc info;
+       size_t length = sizeof(struct kinfo_proc);
+       int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, strtol(pidbuf, NULL, 10) };
+       if (sysctl(mib, 4, &info, &length, NULL, 0) < 0)
+               return false;
+       return (length != 0);
+#endif
 }
 
 int main(int argc, char *argv[]) {
        char part[512],
             pathbuf[512];
        unsigned int i;
-       int load_avg;
 
        char *configfile = PREFIX "/etc/wmiistatus.conf";
        int o, option_index = 0;
@@ -437,7 +454,7 @@ int main(int argc, char *argv[]) {
        setup();
 
        if ((general_socket = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
-               die("Could not create socket");
+               die("Could not create socket\n");
 
        while (1) {
                for (i = 0; i < num_run_watches; i += 2) {
@@ -462,11 +479,26 @@ int main(int argc, char *argv[]) {
                        write_to_statusbar(concat(order[ORDER_BATTERY], "battery"), get_battery_info());
 
                /* Get load */
+#ifdef LINUX
+               int load_avg;
                if ((load_avg = open("/proc/loadavg", O_RDONLY)) == -1)
-                       die("Could not open /proc/loadavg");
+                       die("Could not open /proc/loadavg\n");
                (void)read(load_avg, part, sizeof(part));
                (void)close(load_avg);
                *skip_character(part, ' ', 3) = '\0';
+#else
+               /* TODO: correctly check for NetBSD, check if it works the same on *BSD */
+               struct loadavg load;
+               size_t length = sizeof(struct loadavg);
+               int mib[2] = { CTL_VM, VM_LOADAVG };
+               if (sysctl(mib, 2, &load, &length, NULL, 0) < 0)
+                       die("Could not sysctl({ CTL_VM, VM_LOADAVG })\n");
+               double scale = load.fscale;
+               (void)snprintf(part, sizeof(part), "%.02f %.02f %.02f",
+                               (double)load.ldavg[0] / scale,
+                               (double)load.ldavg[1] / scale,
+                               (double)load.ldavg[2] / scale);
+#endif
                write_to_statusbar(concat(order[ORDER_LOAD], "load"), part);
 
                if (time_format) {