Xinqi Bao's Git

Add the percent sign to *_perc functions
[slstatus.git] / components / netspeeds.c
index 5af5a94..32e78d6 100644 (file)
 
                oldrxbytes = rxbytes;
 
-               snprintf(path, sizeof(path),
-                        "/sys/class/net/%s/statistics/rx_bytes", interface);
+               if (esnprintf(path, sizeof(path),
+                             "/sys/class/net/%s/statistics/rx_bytes",
+                             interface) < 0) {
+                       return NULL;
+               }
                if (pscanf(path, "%llu", &rxbytes) != 1) {
                        return NULL;
                }
 
-               return oldrxbytes ? fmt_scaled((rxbytes - oldrxbytes) /
-                                              interval * 1000) : NULL;
+               return oldrxbytes ? fmt_scaled((rxbytes - oldrxbytes) *
+                                              1000 / interval) : NULL;
        }
 
        const char *
 
                oldtxbytes = txbytes;
 
-               snprintf(path, sizeof(path),
-                        "/sys/class/net/%s/statistics/tx_bytes", interface);
+               if (esnprintf(path, sizeof(path),
+                             "/sys/class/net/%s/statistics/tx_bytes",
+                             interface) < 0) {
+                       return NULL;
+               }
                if (pscanf(path, "%llu", &txbytes) != 1) {
                        return NULL;
                }
 
-               return oldtxbytes ? fmt_scaled((txbytes - oldtxbytes) /
-                                              interval * 1000) : NULL;
+               return oldtxbytes ? fmt_scaled((txbytes - oldtxbytes) *
+                                              1000 / interval) : NULL;
        }
 #elif defined(__OpenBSD__)
        #include <string.h>
                uint64_t oldrxbytes;
                static uint64_t rxbytes = 0;
                extern const unsigned int interval;
-               char if_ok = 0;
+               int if_ok = 0;
+
+               oldrxbytes = rxbytes;
 
                if (getifaddrs(&ifal) == -1) {
                        warn("getifaddrs failed");
                        return NULL;
                }
-               oldrxbytes = rxbytes;
+               rxbytes = 0;
                for (ifa = ifal; ifa; ifa = ifa->ifa_next) {
                        if (!strcmp(ifa->ifa_name, interface) &&
                           (ifd = (struct if_data *)ifa->ifa_data)) {
@@ -80,8 +88,8 @@
                        return NULL;
                }
 
-               return oldrxbytes ? fmt_scaled((rxbytes - oldrxbytes) /
-                                              interval * 1000) : NULL;
+               return oldrxbytes ? fmt_scaled((rxbytes - oldrxbytes) *
+                                              1000 / interval) : NULL;
        }
 
        const char *
                uint64_t oldtxbytes;
                static uint64_t txbytes = 0;
                extern const unsigned int interval;
-               char if_ok = 0;
+               int if_ok = 0;
+
+               oldtxbytes = txbytes;
 
                if (getifaddrs(&ifal) == -1) {
                        warn("getifaddrs failed");
                        return NULL;
                }
-               oldtxbytes = txbytes;
+               txbytes = 0;
                for (ifa = ifal; ifa; ifa = ifa->ifa_next) {
                        if (!strcmp(ifa->ifa_name, interface) &&
                           (ifd = (struct if_data *)ifa->ifa_data)) {
                        return NULL;
                }
 
-               return oldtxbytes ? fmt_scaled((txbytes - oldtxbytes) /
-                                              interval * 1000) : NULL;
+               return oldtxbytes ? fmt_scaled((txbytes - oldtxbytes) *
+                                              1000 / interval) : NULL;
        }
 #endif