summaryrefslogtreecommitdiff
path: root/src/get_wireless_info.c
blob: 529ba71a44e2d242c9df7f6e2a0149f4105c5608 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// vim:ts=8:expandtab
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>

#include "i3status.h"

/*
 * Just parses /proc/net/wireless looking for lines beginning with
 * wlan_interface, extracting the quality of the link and adding the
 * current IP address of wlan_interface.
 *
 */
const char *get_wireless_info() {
        char buf[1024];
        static char part[512];
        char *interfaces;
        memset(buf, 0, sizeof(buf));
        memset(part, 0, sizeof(part));

        if (!slurp("/proc/net/wireless", buf, sizeof(buf)))
                die("Could not open \"/proc/net/wireless\"\n");

        interfaces = skip_character(buf, '\n', 1) + 1;
        while ((interfaces = skip_character(interfaces, '\n', 1)+1) < buf+strlen(buf)) {
                while (isspace((int)*interfaces))
                        interfaces++;
                if (!BEGINS_WITH(interfaces, wlan_interface))
                        continue;
                int quality;
                if (sscanf(interfaces, "%*[^:]: 0000 %d", &quality) != 1)
                        continue;
                if ((quality == UCHAR_MAX) || (quality == 0)) {
                        if (use_colors)
                                (void)snprintf(part, sizeof(part), "%sW: down%s", color("#FF0000"), endcolor());
                        else (void)snprintf(part, sizeof(part), "W: down");
                } else (void)snprintf(part, sizeof(part), "%sW: (%03d%%) %s%s",
                                color("#00FF00"), quality, get_ip_addr(wlan_interface), endcolor());
                return part;
        }

        return part;
}