Xinqi Bao's Git
1 /* See LICENSE file for copyright and license details. */
11 netspeed_rx(const char *interface
)
14 static uintmax_t rxbytes
;
15 extern const unsigned int interval
;
20 if (esnprintf(path
, sizeof(path
),
21 "/sys/class/net/%s/statistics/rx_bytes",
25 if (pscanf(path
, "%ju", &rxbytes
) != 1) {
28 if (oldrxbytes
== 0) {
32 return fmt_human((rxbytes
- oldrxbytes
) * 1000 / interval
,
37 netspeed_tx(const char *interface
)
40 static uintmax_t txbytes
;
41 extern const unsigned int interval
;
46 if (esnprintf(path
, sizeof(path
),
47 "/sys/class/net/%s/statistics/tx_bytes",
51 if (pscanf(path
, "%ju", &txbytes
) != 1) {
54 if (oldtxbytes
== 0) {
58 return fmt_human((txbytes
- oldtxbytes
) * 1000 / interval
,
61 #elif defined(__OpenBSD__) | defined(__FreeBSD__)
64 #include <sys/types.h>
65 #include <sys/socket.h>
69 netspeed_rx(const char *interface
)
71 struct ifaddrs
*ifal
, *ifa
;
74 static uintmax_t rxbytes
;
75 extern const unsigned int interval
;
80 if (getifaddrs(&ifal
) == -1) {
81 warn("getifaddrs failed");
85 for (ifa
= ifal
; ifa
; ifa
= ifa
->ifa_next
) {
86 if (!strcmp(ifa
->ifa_name
, interface
) &&
87 (ifd
= (struct if_data
*)ifa
->ifa_data
)) {
88 rxbytes
+= ifd
->ifi_ibytes
, if_ok
= 1;
93 warn("reading 'if_data' failed");
96 if (oldrxbytes
== 0) {
100 return fmt_human((rxbytes
- oldrxbytes
) * 1000 / interval
,
105 netspeed_tx(const char *interface
)
107 struct ifaddrs
*ifal
, *ifa
;
109 uintmax_t oldtxbytes
;
110 static uintmax_t txbytes
;
111 extern const unsigned int interval
;
114 oldtxbytes
= txbytes
;
116 if (getifaddrs(&ifal
) == -1) {
117 warn("getifaddrs failed");
121 for (ifa
= ifal
; ifa
; ifa
= ifa
->ifa_next
) {
122 if (!strcmp(ifa
->ifa_name
, interface
) &&
123 (ifd
= (struct if_data
*)ifa
->ifa_data
)) {
124 txbytes
+= ifd
->ifi_obytes
, if_ok
= 1;
129 warn("reading 'if_data' failed");
132 if (oldtxbytes
== 0) {
136 return fmt_human((txbytes
- oldtxbytes
) * 1000 / interval
,