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.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* See LICENSE file for copyright and license details. */
  2. #include <err.h>
  3. #include <ifaddrs.h>
  4. #include <linux/wireless.h>
  5. #include <sys/socket.h>
  6. #include <stdio.h>
  7. #include <limits.h>
  8. #include <string.h>
  9. #include <sys/ioctl.h>
  10. #include <unistd.h>
  11. #include "../util.h"
  12. const char *
  13. wifi_perc(const char *iface)
  14. {
  15. int i, cur;
  16. float perc;
  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, "/operstate");
  23. fp = fopen(path, "r");
  24. if (fp == NULL) {
  25. warn("Failed to open file %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. fp = fopen("/proc/net/wireless", "r");
  34. if (fp == NULL) {
  35. warn("Failed to open file /proc/net/wireless");
  36. return NULL;
  37. }
  38. for (i = 0; i < 3; i++) {
  39. if (!(p = fgets(buf, sizeof(buf) - 1, fp)))
  40. break;
  41. }
  42. fclose(fp);
  43. if (i < 2 || !p)
  44. return NULL;
  45. if ((datastart = strstr(buf, iface)) == NULL)
  46. return NULL;
  47. datastart = (datastart+(strlen(iface)+1));
  48. sscanf(datastart + 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", &cur);
  49. perc = (float)cur / total * 100.0;
  50. return bprintf("%.0f", perc);
  51. }
  52. const char *
  53. wifi_essid(const char *iface)
  54. {
  55. static char id[IW_ESSID_MAX_SIZE+1];
  56. int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  57. struct iwreq wreq;
  58. memset(&wreq, 0, sizeof(struct iwreq));
  59. wreq.u.essid.length = IW_ESSID_MAX_SIZE+1;
  60. snprintf(wreq.ifr_name, sizeof(wreq.ifr_name), "%s", iface);
  61. if (sockfd == -1) {
  62. warn("Failed to get ESSID for interface %s", iface);
  63. return NULL;
  64. }
  65. wreq.u.essid.pointer = id;
  66. if (ioctl(sockfd,SIOCGIWESSID, &wreq) == -1) {
  67. warn("Failed to get ESSID for interface %s", iface);
  68. return NULL;
  69. }
  70. close(sockfd);
  71. if (strcmp(id, "") == 0)
  72. return NULL;
  73. else
  74. return id;
  75. }