Reindent and restyle
[sysstatus.git] / statuses / netif.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <string.h>
5 #include "netif.h"
6 #include "tools.h"
7 #include "../config.h"
8
9 #ifndef NETIF_BASEDIR
10   #define NETIF_BASEDIR "/sys/class/net/"
11 #endif
12
13
14 void status_netif(char *ifname)
15 {
16   char ifpath[256];
17   int ifpathlen;
18
19   char stline[16];
20   ssize_t stlen;
21
22   double ifsum = 0.0;
23   int ifsumpower;
24
25
26   /* Prepare path */
27   ifpathlen = sizeof(NETIF_BASEDIR) - 1 + strlen(ifname);
28   if (ifpathlen + 1 + sizeof("/statistics/rx_bytes") >= sizeof(ifpath)) {
29     statusError("status_netif",
30                 "ifpath buffer too small",
31                 ifname);
32     return;
33   }
34   strcpy(ifpath, NETIF_BASEDIR);
35   strcat(ifpath, ifname);
36
37
38   /* Is the interface up? */
39   if (access(ifpath, F_OK)) {
40     //printf(" ^fg(grey)[%s] ", ifname);
41     return;
42   }
43
44
45   strcpy(&ifpath[ifpathlen], "/carrier");
46   stlen = fileRead(stline, sizeof(stline), ifpath);
47   if (stlen > 0) {
48     if (stline[0] == '1') {
49       fputs("^fg(yellow)", stdout);
50     } else {
51       //fputs("^fg(red)", stdout);
52       return;
53     }
54   } else {
55     return;
56   }
57
58   strcpy(&ifpath[ifpathlen], "/statistics/rx_bytes");
59   stlen = fileRead(stline, sizeof(stline), ifpath);
60   if (stlen > 0) {
61     ifsum = atof(stline);
62   }
63
64   strcpy(&ifpath[ifpathlen], "/statistics/tx_bytes");
65   stlen = fileRead(stline, sizeof(stline), ifpath);
66   if (stlen > 0) {
67     ifsum += atof(stline);
68   }
69
70
71   for(ifsumpower = 0; ifsum >= 1024.0; ifsumpower++) {
72     ifsum = ifsum / 1024;
73   }
74
75   printf(" %s: %.*f %c ", ifname,
76                           ifsumpower ? ifsumpower - 1 : ifsumpower,
77                           ifsum,
78                           powerToChar(ifsumpower));
79 }