From: Michael Stapelberg Date: Sat, 4 Oct 2008 16:51:27 +0000 (+0200) Subject: Get IP address for WLAN interface aswell X-Git-Tag: 1.0~57 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=79dfdafb212e9933f32630a3548f05a4a2d786d1;p=i3%2Fi3status Get IP address for WLAN interface aswell --- diff --git a/wmiistatus.c b/wmiistatus.c index 33575a5..269f681 100644 --- a/wmiistatus.c +++ b/wmiistatus.c @@ -157,6 +157,8 @@ static char *get_wireless_info() { memset(part, '\0', sizeof(part)); int fd = open("/proc/net/wireless", O_RDONLY); + if (fd == -1) + die("Could not open /proc/net/wireless"); read(fd, buf, sizeof(buf)); close(fd); @@ -174,8 +176,12 @@ static char *get_wireless_info() { /* For some reason, I get 255 sometimes */ if ((quality == 255) || (quality == 0)) snprintf(part, sizeof(part), "W: down"); - else snprintf(part, sizeof(part), "W: (%02d%%) ", quality); - // TODO: get IP address + else { + snprintf(part, sizeof(part), "W: (%02d%%) ", quality); + char *ip_address = get_ip_address(wlan_interface); + strcpy(part+strlen(part), ip_address); + } + return part; } interfaces = skip_character(interfaces, '\n', 1) + 1; @@ -184,39 +190,49 @@ static char *get_wireless_info() { return part; } -static char *get_eth_info() { +static char *get_ip_address(const char *interface) { static char part[512]; struct ifreq ifr; memset(part, '\0', sizeof(part)); int fd = socket(AF_INET, SOCK_DGRAM, 0); - strcpy(ifr.ifr_name, eth_interface); + strcpy(ifr.ifr_name, interface); if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) die("Could not get interface flags (SIOCGIFFLAGS)"); if (!(ifr.ifr_flags & IFF_UP) || !(ifr.ifr_flags & IFF_RUNNING)) { - sprintf(part, "E: down"); close(fd); - return part; + return NULL; } - strcpy(ifr.ifr_name, eth_interface); + strcpy(ifr.ifr_name, interface); ifr.ifr_addr.sa_family = AF_INET; if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) { struct sockaddr_in addr; memcpy(&addr, &ifr.ifr_addr, sizeof(struct sockaddr_in)); - inet_ntop(AF_INET, &addr.sin_addr.s_addr, part+3, sizeof(struct sockaddr_in)); - strncpy(part, "E: ", strlen("E: ")); + inet_ntop(AF_INET, &addr.sin_addr.s_addr, part, sizeof(struct sockaddr_in)); if (strlen(part) == 0) - sprintf(part, "E: no IP"); + sprintf(part, "no IP"); } close(fd); return part; } +static char *get_eth_info() { + static char part[512]; + char *ip_address = get_ip_address(eth_interface); + + if (ip_address == NULL) + snprintf(part, sizeof(part), "E: down"); + else snprintf(part, sizeof(part), "E: %s", ip_address); + + return part; +} + + int main(void) { char part[512], *end; diff --git a/wmiistatus.h b/wmiistatus.h index 9556b4f..de876ad 100644 --- a/wmiistatus.h +++ b/wmiistatus.h @@ -11,4 +11,5 @@ static char *skip_character(char *input, char character, int amount); static void push_part(const char *input, const int n); static char *get_battery_info(void); static char *get_wireless_info(void); +static char *get_ip_address(const char *interface); static char *get_eth_info(void);