My slstatus configuration
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

netspeeds.c 2.5 KiB

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