My slstatus configuration
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

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