Ye Olde Brick, hacked one very late night in 2010.
[sysstatus.git] / statuses / netif.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <fcntl.h>
4 #include <unistd.h>
5 #include "netif.h"
6 #include "tools.h"
7 #include "../config.h"
8
9 void status_netif()
10 {
11         char stline[16];
12         int stfile;
13         size_t stlen;
14         
15         double ifsum;
16         int ifsumpower;
17         
18         
19         stfile = open("/sys/class/net/" IFNAME "/carrier", 0);
20         if (stfile != -1)
21         {
22                 stlen = read(stfile, stline, sizeof(stline) - 1);
23                 close(stfile);
24                 if (stline[0] == '1')
25                         fputs("^fg(yellow)", stdout);
26                 else
27                         fputs("^fg(red)", stdout);
28         }
29         
30         stfile = open("/sys/class/net/" IFNAME "/statistics/rx_bytes", 0);
31         if (stfile != -1)
32         {
33                 stlen = read(stfile, stline, sizeof(stline) - 1);
34                 close(stfile);
35                 stline[stlen] = '\0';
36                 ifsum = atof(stline);
37         }
38
39         stfile = open("/sys/class/net/" IFNAME "/statistics/tx_bytes", 0);
40         if (stfile != -1)
41         {
42                 stlen = read(stfile, stline, sizeof(stline) - 1);
43                 close(stfile);
44                 stline[stlen] = '\0';
45                 ifsum += atof(stline);
46         }
47         
48         for(ifsumpower = 0; ifsum >= 1024.0; ifsumpower++)
49                 ifsum = ifsum / 1024;
50         
51         printf(" %s: %.*f %c ", IFNAME, ifsumpower ? ifsumpower - 1 : ifsumpower, ifsum, powertochar(ifsumpower));
52 }