-
Notifications
You must be signed in to change notification settings - Fork 1
/
nm.c
36 lines (26 loc) · 936 Bytes
/
nm.c
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
#include "nm.h"
#include "arg.h"
#include <stdio.h>
#define TO_KBITS(b) ((float) (b) / 125)
static unsigned long long int netReCache = 0;
static unsigned long long int netSeCache = 0;
void
nm(Args *arg, char *buff, int bufflen)
{
(void) arg;
unsigned long long int bytes_re, bytes_se;
char *fmt = "⬆ %.2fKi ⬇ %.2fKi";
FILE *fr = fopen("/sys/class/net/eth0/statistics/rx_bytes", "r");
if (!fr) return;
fscanf(fr, "%llu", &bytes_re);
fclose(fr);
FILE *fs = fopen("/sys/class/net/eth0/statistics/tx_bytes", "r");
if (!fs) return;
fscanf(fs, "%llu", &bytes_se);
fclose(fs);
unsigned long long int reDiff = bytes_re - netReCache;
unsigned long long int seDiff = bytes_se - netSeCache;
snprintf(buff, bufflen, fmt, TO_KBITS(seDiff), TO_KBITS(reDiff));
netReCache = bytes_re;
netSeCache = bytes_se;
}