My slstatus configuration
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

167 строки
3.6 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/socket.h>
  7. #include <sys/ioctl.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. fprintf(stderr, "fopen '%s': %s\n", path,
  26. strerror(errno));
  27. return NULL;
  28. }
  29. p = fgets(status, 5, fp);
  30. fclose(fp);
  31. if(!p || strcmp(status, "up\n") != 0) {
  32. return NULL;
  33. }
  34. if (!(fp = fopen("/proc/net/wireless", "r"))) {
  35. fprintf(stderr, "fopen '/proc/net/wireless': %s\n",
  36. strerror(errno));
  37. return NULL;
  38. }
  39. for (i = 0; i < 3; i++) {
  40. if (!(p = fgets(buf, sizeof(buf) - 1, fp)))
  41. break;
  42. }
  43. fclose(fp);
  44. if (i < 2 || !p) {
  45. return NULL;
  46. }
  47. if (!(datastart = strstr(buf, iface))) {
  48. return NULL;
  49. }
  50. datastart = (datastart+(strlen(iface)+1));
  51. sscanf(datastart + 1, " %*d %d %*d %*d\t\t %*d\t "
  52. "%*d\t\t%*d\t\t %*d\t %*d\t\t %*d", &cur);
  53. return bprintf("%d", (int)((float)cur / total * 100));
  54. }
  55. const char *
  56. wifi_essid(const char *iface)
  57. {
  58. static char id[IW_ESSID_MAX_SIZE+1];
  59. int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  60. struct iwreq wreq;
  61. memset(&wreq, 0, sizeof(struct iwreq));
  62. wreq.u.essid.length = IW_ESSID_MAX_SIZE+1;
  63. snprintf(wreq.ifr_name, sizeof(wreq.ifr_name), "%s", iface);
  64. if (sockfd < 0) {
  65. fprintf(stderr, "socket 'AF_INET': %s\n",
  66. strerror(errno));
  67. return NULL;
  68. }
  69. wreq.u.essid.pointer = id;
  70. if (ioctl(sockfd,SIOCGIWESSID, &wreq) < 0) {
  71. fprintf(stderr, "ioctl 'SIOCGIWESSID': %s\n", strerror(errno));
  72. close(sockfd);
  73. return NULL;
  74. }
  75. close(sockfd);
  76. if (!strcmp(id, "")) {
  77. return NULL;
  78. }
  79. return id;
  80. }
  81. #elif defined(__OpenBSD__)
  82. #include <net/if.h>
  83. #include <net/if_media.h>
  84. #include <net80211/ieee80211.h>
  85. #include <net80211/ieee80211_ioctl.h>
  86. #include <stdlib.h>
  87. #include <sys/types.h>
  88. static int
  89. load_ieee80211_nodereq(const char *iface, struct ieee80211_nodereq *nr)
  90. {
  91. struct ieee80211_bssid bssid;
  92. int sockfd;
  93. memset(&bssid, 0, sizeof(bssid));
  94. memset(nr, 0, sizeof(struct ieee80211_nodereq));
  95. if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
  96. fprintf(stderr, "socket 'AF_INET': %s\n",
  97. strerror(errno));
  98. return 0;
  99. }
  100. strlcpy(bssid.i_name, iface, sizeof(bssid.i_name));
  101. if ((ioctl(sockfd, SIOCG80211BSSID, &bssid)) < 0) {
  102. fprintf(stderr, "ioctl 'SIOCG80211BSSID': %s\n",
  103. strerror(errno));
  104. close(sockfd);
  105. return 0;
  106. }
  107. strlcpy(nr->nr_ifname, iface, sizeof(nr->nr_ifname));
  108. memmove(&nr->nr_macaddr, bssid.i_bssid, sizeof(nr->nr_macaddr));
  109. if ((ioctl(sockfd, SIOCG80211NODE, nr)) < 0 && nr->nr_rssi) {
  110. fprintf(stderr, "ioctl 'SIOCG80211NODE': %s\n",
  111. strerror(errno));
  112. close(sockfd);
  113. return 0;
  114. }
  115. return close(sockfd), 1;
  116. }
  117. const char *
  118. wifi_perc(const char *iface)
  119. {
  120. struct ieee80211_nodereq nr;
  121. int q;
  122. if (load_ieee80211_nodereq(iface, &nr)) {
  123. if (nr.nr_max_rssi) {
  124. q = IEEE80211_NODEREQ_RSSI(&nr);
  125. } else {
  126. q = nr.nr_rssi >= -50 ? 100 : (nr.nr_rssi <= -100 ? 0 :
  127. (2 * (nr.nr_rssi + 100)));
  128. }
  129. return bprintf("%d", q);
  130. }
  131. return NULL;
  132. }
  133. const char *
  134. wifi_essid(const char *iface)
  135. {
  136. struct ieee80211_nodereq nr;
  137. if (load_ieee80211_nodereq(iface, &nr)) {
  138. return bprintf("%s", nr.nr_nwid);
  139. }
  140. return NULL;
  141. }
  142. #endif