My slstatus configuration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

128 rivejä
2.8 KiB

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