]> git.sur5r.net Git - i3/i3status/commitdiff
Beautify the code for getting ip address/ethernet speed, only use one static socket
authorMichael Stapelberg <michael+git@stapelberg.de>
Sun, 25 Jan 2009 18:11:53 +0000 (19:11 +0100)
committerMichael Stapelberg <michael+git@stapelberg.de>
Sun, 25 Jan 2009 18:11:53 +0000 (19:11 +0100)
wmiistatus.c

index afd3965938b465d7e40a6e622e729e57e0e9d63e..696f766ca6437e4d46fefba366ee1c1cc368fabd 100644 (file)
@@ -59,6 +59,9 @@
 #undef _IS_WMIISTATUS_C
 #include "config.h"
 
 #undef _IS_WMIISTATUS_C
 #include "config.h"
 
+/* socket file descriptor for general purposes */
+static int general_socket;
+
 /*
  * This function just concats two strings in place, it should only be used
  * for concatting order to the name of a file or concatting color codes.
 /*
  * This function just concats two strings in place, it should only be used
  * for concatting order to the name of a file or concatting color codes.
@@ -336,33 +339,20 @@ static char *get_wireless_info() {
 static const char *get_ip_address(const char *interface) {
        static char part[512];
        struct ifreq ifr;
 static const char *get_ip_address(const char *interface) {
        static char part[512];
        struct ifreq ifr;
-       int fd;
+       struct sockaddr_in addr;
+       socklen_t len = sizeof(struct sockaddr_in);
        memset(part, 0, sizeof(part));
 
        memset(part, 0, sizeof(part));
 
-       if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
-               die("Could not create socket");
-
-       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)) {
-               close(fd);
-               return NULL;
-       }
-
        (void)strcpy(ifr.ifr_name, interface);
        ifr.ifr_addr.sa_family = AF_INET;
        (void)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));
-               (void)inet_ntop(AF_INET, &addr.sin_addr.s_addr, part, (socklen_t)sizeof(struct sockaddr_in));
-               if (strlen(part) == 0)
-                       snprintf(part, sizeof(part), "no IP");
-       }
+       if (ioctl(general_socket, SIOCGIFADDR, &ifr) < 0)
+               return NULL;
+
+       memcpy(&addr, &ifr.ifr_addr, len);
+       (void)inet_ntop(AF_INET, &addr.sin_addr.s_addr, part, len);
+       if (strlen(part) == 0)
+               (void)snprintf(part, sizeof(part), "no IP");
 
 
-       (void)close(fd);
        return part;
 }
 
        return part;
 }
 
@@ -379,20 +369,15 @@ static char *get_eth_info() {
                /* This code path requires root privileges */
                struct ifreq ifr;
                struct ethtool_cmd ecmd;
                /* This code path requires root privileges */
                struct ifreq ifr;
                struct ethtool_cmd ecmd;
-               int fd, err;
-
-               if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
-                       write_error_to_statusbar("Could not open socket");
+               int err;
 
                ecmd.cmd = ETHTOOL_GSET;
                (void)memset(&ifr, 0, sizeof(ifr));
                ifr.ifr_data = (caddr_t)&ecmd;
                (void)strcpy(ifr.ifr_name, eth_interface);
 
                ecmd.cmd = ETHTOOL_GSET;
                (void)memset(&ifr, 0, sizeof(ifr));
                ifr.ifr_data = (caddr_t)&ecmd;
                (void)strcpy(ifr.ifr_name, eth_interface);
-               if ((err = ioctl(fd, SIOCETHTOOL, &ifr)) == 0)
+               if ((err = ioctl(general_socket, SIOCETHTOOL, &ifr)) == 0)
                        ethspeed = (ecmd.speed == 65535 ? 0 : ecmd.speed);
                else get_ethspeed = false;
                        ethspeed = (ecmd.speed == 65535 ? 0 : ecmd.speed);
                else get_ethspeed = false;
-
-               (void)close(fd);
        }
 
        if (ip_address == NULL)
        }
 
        if (ip_address == NULL)
@@ -462,6 +447,9 @@ int main(int argc, char *argv[]) {
 
        setup();
 
 
        setup();
 
+       if ((general_socket = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
+               die("Could not create socket");
+
        while (1) {
                for (i = 0; i < num_run_watches; i += 2) {
                        bool running = process_runs(run_watches[i+1]);
        while (1) {
                for (i = 0; i < num_run_watches; i += 2) {
                        bool running = process_runs(run_watches[i+1]);