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.
 
 
 
 

101 lines
2.1 KiB

  1. /* See LICENSE file for copyright and license details. */
  2. #if defined(__linux__)
  3. #include <errno.h>
  4. #include <ifaddrs.h>
  5. #include <linux/wireless.h>
  6. #include <sys/socket.h>
  7. #include <stdio.h>
  8. #include <limits.h>
  9. #include <string.h>
  10. #include <sys/ioctl.h>
  11. #include <unistd.h>
  12. #include "../util.h"
  13. const char *
  14. wifi_perc(const char *iface)
  15. {
  16. int i, cur;
  17. float perc;
  18. int total = 70; /* the max of /proc/net/wireless */
  19. char *p, *datastart;
  20. char path[PATH_MAX];
  21. char status[5];
  22. FILE *fp;
  23. snprintf(path, sizeof(path), "%s%s%s", "/sys/class/net/", iface,
  24. "/operstate");
  25. if (!(fp = fopen(path, "r"))) {
  26. fprintf(stderr, "fopen '%s': %s\n", path,
  27. strerror(errno));
  28. return NULL;
  29. }
  30. p = fgets(status, 5, fp);
  31. fclose(fp);
  32. if(!p || strcmp(status, "up\n") != 0) {
  33. return NULL;
  34. }
  35. if (!(fp = fopen("/proc/net/wireless", "r"))) {
  36. fprintf(stderr, "fopen '/proc/net/wireless': %s\n",
  37. strerror(errno));
  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. perc = (float)cur / total * 100.0;
  55. return bprintf("%.0f", perc);
  56. }
  57. const char *
  58. wifi_essid(const char *iface)
  59. {
  60. static char id[IW_ESSID_MAX_SIZE+1];
  61. int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  62. struct iwreq wreq;
  63. memset(&wreq, 0, sizeof(struct iwreq));
  64. wreq.u.essid.length = IW_ESSID_MAX_SIZE+1;
  65. snprintf(wreq.ifr_name, sizeof(wreq.ifr_name), "%s", iface);
  66. if (sockfd < 0) {
  67. fprintf(stderr, "socket 'AF_INET': %s\n",
  68. strerror(errno));
  69. return NULL;
  70. }
  71. wreq.u.essid.pointer = id;
  72. if (ioctl(sockfd,SIOCGIWESSID, &wreq) < 0) {
  73. fprintf(stderr, "ioctl 'SIOCGIWESSID': %s\n", strerror(errno));
  74. close(sockfd);
  75. return NULL;
  76. }
  77. close(sockfd);
  78. if (!strcmp(id, "")) {
  79. return NULL;
  80. }
  81. return id;
  82. }
  83. #elif defined(__OpenBSD__)
  84. /* unimplemented */
  85. #endif