summaryrefslogtreecommitdiff
path: root/statuses/netif.c
blob: 944fe2c220bbe86d92bdaa05a08a1bcde1809999 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include "netif.h"
#include "tools.h"
#include "../config.h"

void status_netif()
{
	char stline[16];
	int stfile;
	size_t stlen;
	
	double ifsum;
	int ifsumpower;
	
	
	stfile = open("/sys/class/net/" IFNAME "/carrier", 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);
	}
	
	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';
		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';
		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));
}