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.
 
 
 
 

93 lines
2.0 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, "/operstate");
  24. fp = fopen(path, "r");
  25. if (fp == NULL) {
  26. fprintf(stderr, "fopen '%s': %s\n", path, 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. fp = fopen("/proc/net/wireless", "r");
  35. if (fp == NULL) {
  36. fprintf(stderr, "fopen '/proc/net/wireless': %s\n", 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. if ((datastart = strstr(buf, iface)) == NULL)
  47. return NULL;
  48. datastart = (datastart+(strlen(iface)+1));
  49. sscanf(datastart + 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", &cur);
  50. perc = (float)cur / total * 100.0;
  51. return bprintf("%.0f", perc);
  52. }
  53. const char *
  54. wifi_essid(const char *iface)
  55. {
  56. static char id[IW_ESSID_MAX_SIZE+1];
  57. int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  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 == -1) {
  63. fprintf(stderr, "socket 'AF_INET': %s\n", strerror(errno));
  64. return NULL;
  65. }
  66. wreq.u.essid.pointer = id;
  67. if (ioctl(sockfd,SIOCGIWESSID, &wreq) == -1) {
  68. fprintf(stderr, "ioctl 'SIOCGIWESSID': %s\n", strerror(errno));
  69. close(sockfd);
  70. return NULL;
  71. }
  72. close(sockfd);
  73. if (strcmp(id, "") == 0)
  74. return NULL;
  75. else
  76. return id;
  77. }
  78. #endif