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.
 
 
 
 

162 lines
3.4 KiB

  1. /* See LICENSE file for copyright and license details. */
  2. #include <errno.h>
  3. #include <ifaddrs.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <sys/ioctl.h>
  7. #include <sys/socket.h>
  8. #include <unistd.h>
  9. #include "../util.h"
  10. #if defined(__linux__)
  11. #include <limits.h>
  12. #include <linux/wireless.h>
  13. const char *
  14. wifi_perc(const char *iface)
  15. {
  16. int i, cur;
  17. int total = 70; /* the max of /proc/net/wireless */
  18. char *p, *datastart;
  19. char path[PATH_MAX];
  20. char status[5];
  21. FILE *fp;
  22. snprintf(path, sizeof(path), "%s%s%s", "/sys/class/net/", iface,
  23. "/operstate");
  24. if (!(fp = fopen(path, "r"))) {
  25. warn("fopen '%s':", path);
  26. return NULL;
  27. }
  28. p = fgets(status, 5, fp);
  29. fclose(fp);
  30. if(!p || strcmp(status, "up\n") != 0) {
  31. return NULL;
  32. }
  33. if (!(fp = fopen("/proc/net/wireless", "r"))) {
  34. warn("fopen '/proc/net/wireless':");
  35. return NULL;
  36. }
  37. for (i = 0; i < 3; i++) {
  38. if (!(p = fgets(buf, sizeof(buf) - 1, fp)))
  39. break;
  40. }
  41. fclose(fp);
  42. if (i < 2 || !p) {
  43. return NULL;
  44. }
  45. if (!(datastart = strstr(buf, iface))) {
  46. return NULL;
  47. }
  48. datastart = (datastart+(strlen(iface)+1));
  49. sscanf(datastart + 1, " %*d %d %*d %*d\t\t %*d\t "
  50. "%*d\t\t%*d\t\t %*d\t %*d\t\t %*d", &cur);
  51. return bprintf("%d", (int)((float)cur / total * 100));
  52. }
  53. const char *
  54. wifi_essid(const char *iface)
  55. {
  56. static char id[IW_ESSID_MAX_SIZE+1];
  57. int sockfd;
  58. struct iwreq wreq;
  59. memset(&wreq, 0, sizeof(struct iwreq));
  60. wreq.u.essid.length = IW_ESSID_MAX_SIZE+1;
  61. snprintf(wreq.ifr_name, sizeof(wreq.ifr_name), "%s", iface);
  62. if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
  63. warn("socket 'AF_INET':");
  64. return NULL;
  65. }
  66. wreq.u.essid.pointer = id;
  67. if (ioctl(sockfd,SIOCGIWESSID, &wreq) < 0) {
  68. warn("ioctl 'SIOCGIWESSID':");
  69. close(sockfd);
  70. return NULL;
  71. }
  72. close(sockfd);
  73. if (!strcmp(id, "")) {
  74. return NULL;
  75. }
  76. return id;
  77. }
  78. #elif defined(__OpenBSD__)
  79. #include <net/if.h>
  80. #include <net/if_media.h>
  81. #include <net80211/ieee80211.h>
  82. #include <sys/select.h> /* before <sys/ieee80211_ioctl.h> for NBBY */
  83. #include <net80211/ieee80211_ioctl.h>
  84. #include <stdlib.h>
  85. #include <sys/types.h>
  86. static int
  87. load_ieee80211_nodereq(const char *iface, struct ieee80211_nodereq *nr)
  88. {
  89. struct ieee80211_bssid bssid;
  90. int sockfd;
  91. memset(&bssid, 0, sizeof(bssid));
  92. memset(nr, 0, sizeof(struct ieee80211_nodereq));
  93. if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
  94. warn("socket 'AF_INET':");
  95. return 0;
  96. }
  97. strlcpy(bssid.i_name, iface, sizeof(bssid.i_name));
  98. if ((ioctl(sockfd, SIOCG80211BSSID, &bssid)) < 0) {
  99. warn("ioctl 'SIOCG80211BSSID':");
  100. close(sockfd);
  101. return 0;
  102. }
  103. strlcpy(nr->nr_ifname, iface, sizeof(nr->nr_ifname));
  104. memcpy(&nr->nr_macaddr, bssid.i_bssid, sizeof(nr->nr_macaddr));
  105. if ((ioctl(sockfd, SIOCG80211NODE, nr)) < 0 && nr->nr_rssi) {
  106. warn("ioctl 'SIOCG80211NODE':");
  107. close(sockfd);
  108. return 0;
  109. }
  110. return close(sockfd), 1;
  111. }
  112. const char *
  113. wifi_perc(const char *iface)
  114. {
  115. struct ieee80211_nodereq nr;
  116. int q;
  117. if (load_ieee80211_nodereq(iface, &nr)) {
  118. if (nr.nr_max_rssi) {
  119. q = IEEE80211_NODEREQ_RSSI(&nr);
  120. } else {
  121. q = nr.nr_rssi >= -50 ? 100 : (nr.nr_rssi <= -100 ? 0 :
  122. (2 * (nr.nr_rssi + 100)));
  123. }
  124. return bprintf("%d", q);
  125. }
  126. return NULL;
  127. }
  128. const char *
  129. wifi_essid(const char *iface)
  130. {
  131. struct ieee80211_nodereq nr;
  132. if (load_ieee80211_nodereq(iface, &nr)) {
  133. return bprintf("%s", nr.nr_nwid);
  134. }
  135. return NULL;
  136. }
  137. #endif