Xinqi Bao's Git

Remove units from numbers
[slstatus.git] / components / netspeeds.c
1 /* See LICENSE file for copyright and license details. */
2 #include <stdio.h>
3 #include <limits.h>
4
5 #include "../util.h"
6
7 #if defined(__linux__)
8 #include <stdint.h>
9
10 const char *
11 netspeed_rx(const char *interface)
12 {
13 uint64_t oldrxbytes;
14 static uint64_t rxbytes;
15 extern const unsigned int interval;
16 char path[PATH_MAX];
17
18 oldrxbytes = rxbytes;
19
20 if (esnprintf(path, sizeof(path),
21 "/sys/class/net/%s/statistics/rx_bytes",
22 interface) < 0) {
23 return NULL;
24 }
25 if (pscanf(path, "%llu", &rxbytes) != 1) {
26 return NULL;
27 }
28 if (oldrxbytes == 0) {
29 return NULL;
30 }
31
32 return fmt_human_2((rxbytes - oldrxbytes) * 1000 / interval);
33 }
34
35 const char *
36 netspeed_tx(const char *interface)
37 {
38 uint64_t oldtxbytes;
39 static uint64_t txbytes;
40 extern const unsigned int interval;
41 char path[PATH_MAX];
42
43 oldtxbytes = txbytes;
44
45 if (esnprintf(path, sizeof(path),
46 "/sys/class/net/%s/statistics/tx_bytes",
47 interface) < 0) {
48 return NULL;
49 }
50 if (pscanf(path, "%llu", &txbytes) != 1) {
51 return NULL;
52 }
53 if (oldtxbytes == 0) {
54 return NULL;
55 }
56
57 return fmt_human_2((txbytes - oldtxbytes) * 1000 / interval);
58 }
59 #elif defined(__OpenBSD__)
60 #include <string.h>
61 #include <ifaddrs.h>
62 #include <sys/types.h>
63 #include <sys/socket.h>
64 #include <net/if.h>
65
66 const char *
67 netspeed_rx(const char *interface)
68 {
69 struct ifaddrs *ifal, *ifa;
70 struct if_data *ifd;
71 uint64_t oldrxbytes;
72 static uint64_t rxbytes;
73 extern const unsigned int interval;
74 int if_ok = 0;
75
76 oldrxbytes = rxbytes;
77
78 if (getifaddrs(&ifal) == -1) {
79 warn("getifaddrs failed");
80 return NULL;
81 }
82 rxbytes = 0;
83 for (ifa = ifal; ifa; ifa = ifa->ifa_next) {
84 if (!strcmp(ifa->ifa_name, interface) &&
85 (ifd = (struct if_data *)ifa->ifa_data)) {
86 rxbytes += ifd->ifi_ibytes, if_ok = 1;
87 }
88 }
89 freeifaddrs(ifal);
90 if (!if_ok) {
91 warn("reading 'if_data' failed");
92 return NULL;
93 }
94 if (oldrxbytes == 0) {
95 return NULL;
96 }
97
98 return fmt_human_2((rxbytes - oldrxbytes) * 1000 / interval);
99 }
100
101 const char *
102 netspeed_tx(const char *interface)
103 {
104 struct ifaddrs *ifal, *ifa;
105 struct if_data *ifd;
106 uint64_t oldtxbytes;
107 static uint64_t txbytes;
108 extern const unsigned int interval;
109 int if_ok = 0;
110
111 oldtxbytes = txbytes;
112
113 if (getifaddrs(&ifal) == -1) {
114 warn("getifaddrs failed");
115 return NULL;
116 }
117 txbytes = 0;
118 for (ifa = ifal; ifa; ifa = ifa->ifa_next) {
119 if (!strcmp(ifa->ifa_name, interface) &&
120 (ifd = (struct if_data *)ifa->ifa_data)) {
121 txbytes += ifd->ifi_obytes, if_ok = 1;
122 }
123 }
124 freeifaddrs(ifal);
125 if (!if_ok) {
126 warn("reading 'if_data' failed");
127 return NULL;
128 }
129 if (oldtxbytes == 0) {
130 return NULL;
131 }
132
133 return fmt_human_2((txbytes - oldtxbytes) * 1000 / interval);
134 }
135 #endif