More cleanup, 'temp' and 'uptime' use fileRead().
[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         {
30                 statusError("status_netif",
31                         "ifpath buffer too small",
32                         ifname);
33                 return;
34         }
35         strcpy(ifpath, NETIF_BASEDIR);
36         strcat(ifpath, ifname);
37
38
39         // Is the interface up?
40         if (access(ifpath, F_OK))
41         {
42                 //printf(" ^fg(grey)[%s] ", ifname);
43                 return;
44         }
45
46
47         strcpy(&ifpath[ifpathlen], "/carrier");
48         stlen = fileRead(stline, sizeof(stline), ifpath);
49         if (stlen > 0)
50         {
51                 if (stline[0] == '1')
52                 {
53                         fputs("^fg(yellow)", stdout);
54                 }
55                 else
56                 {
57                         //fputs("^fg(red)", stdout);
58                         return;
59                 }
60         }
61
62         strcpy(&ifpath[ifpathlen], "/statistics/rx_bytes");
63         stlen = fileRead(stline, sizeof(stline), ifpath);
64         if (stlen > 0)
65                 ifsum = atof(stline);
66
67         strcpy(&ifpath[ifpathlen], "/statistics/tx_bytes");
68         stlen = fileRead(stline, sizeof(stline), ifpath);
69         if (stlen > 0)
70                 ifsum += atof(stline);
71
72
73         for(ifsumpower = 0; ifsum >= 1024.0; ifsumpower++)
74                 ifsum = ifsum / 1024;
75
76         printf(" %s: %.*f %c ", ifname,
77                                 ifsumpower ? ifsumpower - 1 : ifsumpower,
78                                 ifsum,
79                                 powerToChar(ifsumpower));
80 }