]> git.sur5r.net Git - i3/i3status/blobdiff - src/print_wireless_info.c
fix: use SYSCONFDIR in error message
[i3/i3status] / src / print_wireless_info.c
index d229538d7586f7a86b810a05df3032b23b3bfbe5..8b2d21064ac559e69392555b02f14e4aab8ef391 100644 (file)
@@ -1,10 +1,13 @@
 // vim:ts=4:sw=4:expandtab
+#include <config.h>
 #include <stdio.h>
 #include <string.h>
+#include <stdlib.h>
 #include <yajl/yajl_gen.h>
 #include <yajl/yajl_version.h>
+#include <sys/socket.h>
 
-#ifdef LINUX
+#ifdef __linux__
 #include <errno.h>
 #include <net/if.h>
 #include <netlink/netlink.h>
@@ -22,7 +25,6 @@
 #ifdef __FreeBSD__
 #include <sys/param.h>
 #include <sys/ioctl.h>
-#include <sys/socket.h>
 #include <ifaddrs.h>
 #include <net/if.h>
 #include <net/if_media.h>
@@ -35,8 +37,8 @@
 #ifdef __DragonFly__
 #include <sys/param.h>
 #include <sys/ioctl.h>
-#include <sys/socket.h>
 #include <ifaddrs.h>
+#include <stdlib.h>
 #include <net/if.h>
 #include <net/if_media.h>
 #include <netproto/802_11/ieee80211.h>
@@ -47,7 +49,6 @@
 
 #ifdef __OpenBSD__
 #include <sys/ioctl.h>
-#include <sys/socket.h>
 #include <net/if.h>
 #include <sys/select.h>
 #include <sys/types.h>
@@ -79,7 +80,7 @@ typedef struct {
 #ifdef IW_ESSID_MAX_SIZE
     char essid[IW_ESSID_MAX_SIZE + 1];
 #endif
-#ifdef LINUX
+#ifdef __linux__
     uint8_t bssid[ETH_ALEN];
 #endif
     int quality;
@@ -93,7 +94,7 @@ typedef struct {
     double frequency;
 } wireless_info_t;
 
-#ifdef LINUX
+#ifdef __linux__
 // Like iw_print_bitrate, but without the dependency on libiw.
 static void print_bitrate(char *buffer, int buflen, int bitrate) {
     const int kilo = 1e3;
@@ -273,7 +274,7 @@ static int gwi_scan_cb(struct nl_msg *msg, void *data) {
 static int get_wireless_info(const char *interface, wireless_info_t *info) {
     memset(info, 0, sizeof(wireless_info_t));
 
-#ifdef LINUX
+#ifdef __linux__
     struct nl_sock *sk = nl_socket_alloc();
     if (genl_connect(sk) != 0)
         goto error1;
@@ -479,7 +480,7 @@ error1:
  * | 127.0.0.1    | no IP        | IPv4      | ok                |
  * | 127.0.0.1    | ::1/128      | IPv4      | ok                |
  */
-void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down) {
+void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down, const char *format_quality) {
     const char *walk;
     char *outwalk = buffer;
     wireless_info_t info;
@@ -489,7 +490,10 @@ void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface,
     char *ipv4_address = sstrdup(get_ip_addr(interface, AF_INET));
     char *ipv6_address = sstrdup(get_ip_addr(interface, AF_INET6));
 
-    // Removing '%' and following characters from IPv6
+    /*
+     * Removing '%' and following characters from IPv6 since the interface identifier is redundant,
+     * as the output already includes the interface name.
+    */
     if (ipv6_address != NULL) {
         char *prct_ptr = strstr(ipv6_address, "%");
         if (prct_ptr != NULL) {
@@ -530,22 +534,19 @@ void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface,
     for (; *walk != '\0'; walk++) {
         if (*walk != '%') {
             *(outwalk++) = *walk;
-            continue;
-        }
 
-        if (BEGINS_WITH(walk + 1, "quality")) {
+        } else if (BEGINS_WITH(walk + 1, "quality")) {
             if (info.flags & WIRELESS_INFO_FLAG_HAS_QUALITY) {
                 if (info.quality_max)
-                    outwalk += sprintf(outwalk, "%3d%s", PERCENT_VALUE(info.quality, info.quality_max), pct_mark);
+                    outwalk += sprintf(outwalk, format_quality, PERCENT_VALUE(info.quality, info.quality_max), pct_mark);
                 else
                     outwalk += sprintf(outwalk, "%d", info.quality);
             } else {
                 *(outwalk++) = '?';
             }
             walk += strlen("quality");
-        }
 
-        if (BEGINS_WITH(walk + 1, "signal")) {
+        } else if (BEGINS_WITH(walk + 1, "signal")) {
             if (info.flags & WIRELESS_INFO_FLAG_HAS_SIGNAL) {
                 if (info.signal_level_max)
                     outwalk += sprintf(outwalk, "%3d%s", PERCENT_VALUE(info.signal_level, info.signal_level_max), pct_mark);
@@ -555,9 +556,8 @@ void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface,
                 *(outwalk++) = '?';
             }
             walk += strlen("signal");
-        }
 
-        if (BEGINS_WITH(walk + 1, "noise")) {
+        } else if (BEGINS_WITH(walk + 1, "noise")) {
             if (info.flags & WIRELESS_INFO_FLAG_HAS_NOISE) {
                 if (info.noise_level_max)
                     outwalk += sprintf(outwalk, "%3d%s", PERCENT_VALUE(info.noise_level, info.noise_level_max), pct_mark);
@@ -567,9 +567,8 @@ void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface,
                 *(outwalk++) = '?';
             }
             walk += strlen("noise");
-        }
 
-        if (BEGINS_WITH(walk + 1, "essid")) {
+        } else if (BEGINS_WITH(walk + 1, "essid")) {
 #ifdef IW_ESSID_MAX_SIZE
             if (info.flags & WIRELESS_INFO_FLAG_HAS_ESSID)
                 maybe_escape_markup(info.essid, &outwalk);
@@ -577,23 +576,20 @@ void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface,
 #endif
                 *(outwalk++) = '?';
             walk += strlen("essid");
-        }
 
-        if (BEGINS_WITH(walk + 1, "frequency")) {
+        } else if (BEGINS_WITH(walk + 1, "frequency")) {
             if (info.flags & WIRELESS_INFO_FLAG_HAS_FREQUENCY)
                 outwalk += sprintf(outwalk, "%1.1f GHz", info.frequency / 1e9);
             else
                 *(outwalk++) = '?';
             walk += strlen("frequency");
-        }
 
-        if (BEGINS_WITH(walk + 1, "ip")) {
+        } else if (BEGINS_WITH(walk + 1, "ip")) {
             outwalk += sprintf(outwalk, "%s", ip_address);
             walk += strlen("ip");
         }
-
-#ifdef LINUX
-        if (BEGINS_WITH(walk + 1, "bitrate")) {
+#ifdef __linux__
+        else if (BEGINS_WITH(walk + 1, "bitrate")) {
             char br_buffer[128];
 
             print_bitrate(br_buffer, sizeof(br_buffer), info.bitrate);
@@ -602,6 +598,9 @@ void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface,
             walk += strlen("bitrate");
         }
 #endif
+        else {
+            *(outwalk++) = '%';
+        }
     }
 
 out: