Cleaned up 'netif'.
authornorly <ny-git@enpas.org>
Tue, 14 Feb 2012 01:27:32 +0000 (01:27 +0000)
committernorly <ny-git@enpas.org>
Tue, 14 Feb 2012 01:27:32 +0000 (01:27 +0000)
Replaced 'netif' with 'netif_named'.
Fixed memory leak in new 'netif'.
Outsourced raw file reading to tools.c.

config.h
statuses/Makefile
statuses/netif.c [new file with mode: 0644]
statuses/netif.h [new file with mode: 0644]
statuses/netif_named.c [deleted file]
statuses/netif_named.h [deleted file]
statuses/tools.c
statuses/tools.h
sysstatus.c

index 7879911f1fba1415c85cea3489b9aa377cf9f64c..58495c77514dc76059c7040df23c70608417efa7 100644 (file)
--- a/config.h
+++ b/config.h
@@ -9,6 +9,6 @@
 #define CPU_HISTORY_SIZE 10
 #define NUM_CPUS 2
 
-#define IFNAME "ppp0"
+#define NETIF_BASEDIR "/sys/class/net/"
 
 #endif
index fa660695097ca29672270271af14bc0f3a85ead2..d3f8a46f5aa697093304b6d497d3f201879f10f1 100644 (file)
@@ -1,4 +1,4 @@
-STATUSES=cpuusage.o datetime.o memusage.o netif_named.o power.o temp.o uptime.o volume_alsa.o
+STATUSES=cpuusage.o datetime.o memusage.o netif.o power.o temp.o uptime.o volume_alsa.o
 OTHERS=tools.o
 
 all: *.c *.h Makefile $(STATUSES) $(OTHERS)
diff --git a/statuses/netif.c b/statuses/netif.c
new file mode 100644 (file)
index 0000000..cd131f2
--- /dev/null
@@ -0,0 +1,81 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <string.h>
+#include "netif.h"
+#include "tools.h"
+#include "../config.h"
+
+#ifndef NETIF_BASEDIR
+       #define NETIF_BASEDIR "/sys/class/net/"
+#endif
+
+
+void status_netif(char *ifname)
+{
+       char ifpath[256];
+       int ifpathlen;
+
+       char stline[16];
+       size_t stlen;
+
+       double ifsum = 0.0;
+       int ifsumpower;
+
+
+       // 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)
+       {
+               if (stline[0] == '1')
+               {
+                       fputs("^fg(yellow)", stdout);
+               }
+               else
+               {
+                       //fputs("^fg(red)", stdout);
+                       return;
+               }
+       }
+
+       strcpy(&ifpath[ifpathlen], "/statistics/rx_bytes");
+       stlen = fileRead(stline, sizeof(stline), ifpath);
+       if (stlen > 0)
+               ifsum = atof(stline);
+
+       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));
+}
diff --git a/statuses/netif.h b/statuses/netif.h
new file mode 100644 (file)
index 0000000..92ccc1f
--- /dev/null
@@ -0,0 +1,7 @@
+
+#ifndef __NETIF_H__
+#define __NETIF_H__
+
+void status_netif(char *ifname);
+
+#endif
diff --git a/statuses/netif_named.c b/statuses/netif_named.c
deleted file mode 100644 (file)
index 3fa90f1..0000000
+++ /dev/null
@@ -1,76 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <string.h>
-#include "netif_named.h"
-#include "tools.h"
-
-#define NETIF_BASEDIR "/sys/class/net/"
-
-void status_netif_named(char *ifname)
-{
-       char stline[16];
-       int stfile;
-       size_t stlen;
-       
-       double ifsum;
-       int ifsumpower;
-       
-       char *stfilename = NULL;
-       
-       stfilename = malloc(sizeof(NETIF_BASEDIR) - 1 + strlen(ifname) + sizeof("/statistics/rx_bytes"));
-       
-       strcpy(stfilename, NETIF_BASEDIR);
-       strcat(stfilename, ifname);
-       
-       if (access(stfilename, F_OK))
-       {
-               //printf(" ^fg(grey)[%s] ", ifname);
-               return;
-       }
-       
-       strcat(stfilename, "/carrier");
-       stfile = open(stfilename, 0);
-       if (stfile != -1)
-       {
-               stlen = read(stfile, stline, sizeof(stline) - 1);
-               close(stfile);
-               if (stline[0] == '1')
-                       fputs("^fg(yellow)", stdout);
-               else
-               {
-                       //fputs("^fg(red)", stdout);
-                       return;
-               }
-       }
-       
-       strcpy(stfilename, NETIF_BASEDIR);
-       strcat(stfilename, ifname);
-       strcat(stfilename, "/statistics/rx_bytes");
-       stfile = open(stfilename, 0);
-       if (stfile != -1)
-       {
-               stlen = read(stfile, stline, sizeof(stline) - 1);
-               close(stfile);
-               stline[stlen] = '\0';
-               ifsum = atof(stline);
-       }
-
-       strcpy(stfilename, NETIF_BASEDIR);
-       strcat(stfilename, ifname);
-       strcat(stfilename, "/statistics/tx_bytes");
-       stfile = open(stfilename, 0);
-       if (stfile != -1)
-       {
-               stlen = read(stfile, stline, sizeof(stline) - 1);
-               close(stfile);
-               stline[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));
-}
diff --git a/statuses/netif_named.h b/statuses/netif_named.h
deleted file mode 100644 (file)
index adb4a3e..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-
-#ifndef __NETIF_NAMED_H__
-#define __NETIF_NAMED_H__
-
-void status_netif_named(char *ifname);
-
-#endif
index 1e0385d34a94705b15904444796a7eae80d86cb0..7a4a677fc270a8ee4aebfcf2a28e65ef8353a29c 100644 (file)
@@ -1,6 +1,9 @@
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
 #include "tools.h"
 
-char powertochar(int power)
+char powerToChar(int power)
 {
        switch(power)
        {
@@ -19,6 +22,36 @@ char powertochar(int power)
                case 6:
                        return 'E';
        }
-       
+
        return '?';
 }
+
+
+void statusError(char *where, char *what, char *extra)
+{
+       fprintf(stderr, "%s: %s", where, what);
+       if (extra)
+               fprintf(stderr, " -- %s", extra);
+       fputs("\n", stderr);
+}
+
+
+ssize_t fileRead(char *buf, size_t bufsize, char *file)
+{
+       int fd;
+       int readbytes;
+
+       fd = open(file, 0);
+       if (fd < 0)
+               return -1;
+
+       readbytes = read(fd, buf, bufsize - 1);
+       close(fd);
+
+       if (readbytes > 0)
+               buf[readbytes] = '\0';
+       else
+               buf[0] = '\0';
+
+       return readbytes;
+}
\ No newline at end of file
index 1b9f344ef52b14e8319dad26697ef5f7326b7ed9..802b9f19b6878e4ac1df47ae7ce9be3bf6a2b93d 100644 (file)
@@ -2,6 +2,8 @@
 #ifndef __TOOLS_H__
 #define __TOOLS_H__
 
-char powertochar(int power);
+char powerToChar(int power);
+void statusError(char *where, char *what, char *extra);
+ssize_t fileRead(char *buf, size_t bufsize, char *file);
 
 #endif
index bbdb2feebbc40069d0f634c5a353cc549f7951e8..4e5d6c85bdea92b93c2262f87172daa4206c0ca2 100644 (file)
@@ -5,7 +5,7 @@
 #include "statuses/uptime.h"
 #include "statuses/memusage.h"
 #include "statuses/cpuusage.h"
-#include "statuses/netif_named.h"
+#include "statuses/netif.h"
 #include "statuses/power.h"
 #include "statuses/volume_alsa.h"
 #include "statuses/temp.h"
@@ -22,11 +22,11 @@ void updatestatus()
 
        status_memusage();
 
-       status_netif_named("eth0");
-       status_netif_named("eth1");
-       status_netif_named("wlan0");
-       status_netif_named("wlan1");
-       status_netif_named("ppp0");
+       status_netif("eth0");
+       status_netif("eth1");
+       status_netif("wlan0");
+       status_netif("wlan1");
+       status_netif("ppp0");
 
        status_temp("GPU: ", "/sys/class/hwmon/hwmon0/device/temp4_input");
        status_temp("CPU: ", "/sys/class/hwmon/hwmon0/device/temp2_input");
@@ -43,16 +43,14 @@ int main()
 {
        struct timeval tv;
 
-       updatestatus();
-
        for(;;)
        {
+               updatestatus();
+
                tv.tv_sec = UPDATE_SECS;
                tv.tv_usec = 0;
 
                select(0, NULL, NULL, NULL, &tv);
-
-               updatestatus();
        }
 
        return 0;