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.
 
 
 
 

86 wiersze
1.8 KiB

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