My slstatus configuration
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

wifi.c 1.7 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include <err.h>
  2. #include <ifaddrs.h>
  3. #include <linux/wireless.h>
  4. #include <sys/socket.h>
  5. #include <stdio.h>
  6. #include <limits.h>
  7. #include <string.h>
  8. #include <sys/ioctl.h>
  9. #include <unistd.h>
  10. #include "util.h"
  11. const char *
  12. wifi_perc(const char *iface)
  13. {
  14. int i, perc;
  15. char *p, *datastart;
  16. char path[PATH_MAX];
  17. char status[5];
  18. FILE *fp;
  19. snprintf(path, sizeof(path), "%s%s%s", "/sys/class/net/", iface, "/operstate");
  20. fp = fopen(path, "r");
  21. if (fp == NULL) {
  22. warn("Failed to open file %s", path);
  23. return NULL;
  24. }
  25. p = fgets(status, 5, fp);
  26. fclose(fp);
  27. if(!p || strcmp(status, "up\n") != 0) {
  28. return NULL;
  29. }
  30. fp = fopen("/proc/net/wireless", "r");
  31. if (fp == NULL) {
  32. warn("Failed to open file /proc/net/wireless");
  33. return NULL;
  34. }
  35. for (i = 0; i < 3; i++) {
  36. if (!(p = fgets(buf, sizeof(buf) - 1, fp)))
  37. break;
  38. }
  39. fclose(fp);
  40. if (i < 2 || !p)
  41. return NULL;
  42. if ((datastart = strstr(buf, iface)) == NULL)
  43. return NULL;
  44. datastart = (datastart+(strlen(iface)+1));
  45. sscanf(datastart + 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", &perc);
  46. return bprintf("%d", perc);
  47. }
  48. const char *
  49. wifi_essid(const char *iface)
  50. {
  51. static char id[IW_ESSID_MAX_SIZE+1];
  52. int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  53. struct iwreq wreq;
  54. memset(&wreq, 0, sizeof(struct iwreq));
  55. wreq.u.essid.length = IW_ESSID_MAX_SIZE+1;
  56. snprintf(wreq.ifr_name, sizeof(wreq.ifr_name), "%s", iface);
  57. if (sockfd == -1) {
  58. warn("Failed to get ESSID for interface %s", iface);
  59. return NULL;
  60. }
  61. wreq.u.essid.pointer = id;
  62. if (ioctl(sockfd,SIOCGIWESSID, &wreq) == -1) {
  63. warn("Failed to get ESSID for interface %s", iface);
  64. return NULL;
  65. }
  66. close(sockfd);
  67. if (strcmp(id, "") == 0)
  68. return NULL;
  69. else
  70. return id;
  71. }