summaryrefslogtreecommitdiff
path: root/statuses/netif_named.c
blob: 3fa90f139031e24f54d3b6fd78ff49deb199c57c (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#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));
}