]> git.sur5r.net Git - i3/i3status/blob - src/get_wireless_info.c
529ba71a44e2d242c9df7f6e2a0149f4105c5608
[i3/i3status] / src / get_wireless_info.c
1 // vim:ts=8:expandtab
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <ctype.h>
6 #include <limits.h>
7
8 #include "i3status.h"
9
10 /*
11  * Just parses /proc/net/wireless looking for lines beginning with
12  * wlan_interface, extracting the quality of the link and adding the
13  * current IP address of wlan_interface.
14  *
15  */
16 const char *get_wireless_info() {
17         char buf[1024];
18         static char part[512];
19         char *interfaces;
20         memset(buf, 0, sizeof(buf));
21         memset(part, 0, sizeof(part));
22
23         if (!slurp("/proc/net/wireless", buf, sizeof(buf)))
24                 die("Could not open \"/proc/net/wireless\"\n");
25
26         interfaces = skip_character(buf, '\n', 1) + 1;
27         while ((interfaces = skip_character(interfaces, '\n', 1)+1) < buf+strlen(buf)) {
28                 while (isspace((int)*interfaces))
29                         interfaces++;
30                 if (!BEGINS_WITH(interfaces, wlan_interface))
31                         continue;
32                 int quality;
33                 if (sscanf(interfaces, "%*[^:]: 0000 %d", &quality) != 1)
34                         continue;
35                 if ((quality == UCHAR_MAX) || (quality == 0)) {
36                         if (use_colors)
37                                 (void)snprintf(part, sizeof(part), "%sW: down%s", color("#FF0000"), endcolor());
38                         else (void)snprintf(part, sizeof(part), "W: down");
39                 } else (void)snprintf(part, sizeof(part), "%sW: (%03d%%) %s%s",
40                                 color("#00FF00"), quality, get_ip_addr(wlan_interface), endcolor());
41                 return part;
42         }
43
44         return part;
45 }
46