More cleanup, 'temp' and 'uptime' use fileRead().
[sysstatus.git] / statuses / netif.c
index 944fe2c220bbe86d92bdaa05a08a1bcde1809999..865e4a7a22962d7c5b471cd92f8dc370264e07d1 100644 (file)
@@ -1,52 +1,80 @@
 #include <stdio.h>
 #include <stdlib.h>
-#include <fcntl.h>
 #include <unistd.h>
+#include <string.h>
 #include "netif.h"
 #include "tools.h"
 #include "../config.h"
 
-void status_netif()
+#ifndef NETIF_BASEDIR
+       #define NETIF_BASEDIR "/sys/class/net/"
+#endif
+
+
+void status_netif(char *ifname)
 {
+       char ifpath[256];
+       int ifpathlen;
+
        char stline[16];
-       int stfile;
-       size_t stlen;
-       
-       double ifsum;
+       ssize_t stlen;
+
+       double ifsum = 0.0;
        int ifsumpower;
-       
-       
-       stfile = open("/sys/class/net/" IFNAME "/carrier", 0);
-       if (stfile != -1)
+
+
+       // Prepare path
+       ifpathlen = sizeof(NETIF_BASEDIR) - 1 + strlen(ifname);
+       if (ifpathlen + 1 + sizeof("/statistics/rx_bytes") >= sizeof(ifpath))
+       {
+               statusError("status_netif",
+                       "ifpath buffer too small",
+                       ifname);
+               return;
+       }
+       strcpy(ifpath, NETIF_BASEDIR);
+       strcat(ifpath, ifname);
+
+
+       // Is the interface up?
+       if (access(ifpath, F_OK))
+       {
+               //printf(" ^fg(grey)[%s] ", ifname);
+               return;
+       }
+
+
+       strcpy(&ifpath[ifpathlen], "/carrier");
+       stlen = fileRead(stline, sizeof(stline), ifpath);
+       if (stlen > 0)
        {
-               stlen = read(stfile, stline, sizeof(stline) - 1);
-               close(stfile);
                if (stline[0] == '1')
+               {
                        fputs("^fg(yellow)", stdout);
+               }
                else
-                       fputs("^fg(red)", stdout);
+               {
+                       //fputs("^fg(red)", stdout);
+                       return;
+               }
        }
-       
-       stfile = open("/sys/class/net/" IFNAME "/statistics/rx_bytes", 0);
-       if (stfile != -1)
-       {
-               stlen = read(stfile, stline, sizeof(stline) - 1);
-               close(stfile);
-               stline[stlen] = '\0';
+
+       strcpy(&ifpath[ifpathlen], "/statistics/rx_bytes");
+       stlen = fileRead(stline, sizeof(stline), ifpath);
+       if (stlen > 0)
                ifsum = atof(stline);
-       }
 
-       stfile = open("/sys/class/net/" IFNAME "/statistics/tx_bytes", 0);
-       if (stfile != -1)
-       {
-               stlen = read(stfile, stline, sizeof(stline) - 1);
-               close(stfile);
-               stline[stlen] = '\0';
+       strcpy(&ifpath[ifpathlen], "/statistics/tx_bytes");
+       stlen = fileRead(stline, sizeof(stline), ifpath);
+       if (stlen > 0)
                ifsum += atof(stline);
-       }
-       
+
+
        for(ifsumpower = 0; ifsum >= 1024.0; ifsumpower++)
                ifsum = ifsum / 1024;
-       
-       printf(" %s: %.*f %c ", IFNAME, ifsumpower ? ifsumpower - 1 : ifsumpower, ifsum, powertochar(ifsumpower));
+
+       printf(" %s: %.*f %c ", ifname,
+                               ifsumpower ? ifsumpower - 1 : ifsumpower,
+                               ifsum,
+                               powerToChar(ifsumpower));
 }