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.
 
 
 
 

98 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 <limits.h>
  6. #include <linux/wireless.h>
  7. #include <sys/socket.h>
  8. #include <stdio.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. 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,
  23. "/operstate");
  24. if (!(fp = fopen(path, "r"))) {
  25. fprintf(stderr, "fopen '%s': %s\n", path,
  26. 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. if (!(fp = fopen("/proc/net/wireless", "r"))) {
  35. fprintf(stderr, "fopen '/proc/net/wireless': %s\n",
  36. 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. }
  47. if (!(datastart = strstr(buf, iface))) {
  48. return NULL;
  49. }
  50. datastart = (datastart+(strlen(iface)+1));
  51. sscanf(datastart + 1, " %*d %d %*d %*d\t\t %*d\t "
  52. "%*d\t\t%*d\t\t %*d\t %*d\t\t %*d", &cur);
  53. return bprintf("%d", (int)((float)cur / total * 100));
  54. }
  55. const char *
  56. wifi_essid(const char *iface)
  57. {
  58. static char id[IW_ESSID_MAX_SIZE+1];
  59. int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  60. struct iwreq wreq;
  61. memset(&wreq, 0, sizeof(struct iwreq));
  62. wreq.u.essid.length = IW_ESSID_MAX_SIZE+1;
  63. snprintf(wreq.ifr_name, sizeof(wreq.ifr_name), "%s", iface);
  64. if (sockfd < 0) {
  65. fprintf(stderr, "socket 'AF_INET': %s\n",
  66. strerror(errno));
  67. return NULL;
  68. }
  69. wreq.u.essid.pointer = id;
  70. if (ioctl(sockfd,SIOCGIWESSID, &wreq) < 0) {
  71. fprintf(stderr, "ioctl 'SIOCGIWESSID': %s\n", strerror(errno));
  72. close(sockfd);
  73. return NULL;
  74. }
  75. close(sockfd);
  76. if (!strcmp(id, "")) {
  77. return NULL;
  78. }
  79. return id;
  80. }
  81. #elif defined(__OpenBSD__)
  82. /* unimplemented */
  83. #endif