Xinqi Bao's Git

removed strlcat dependency (was used only once)
[slstatus.git] / slstatus.c
index 06c1e77..64f7eab 100644 (file)
 #include <unistd.h>
 #include <X11/Xlib.h>
 
-#undef strlcat
-
 #include "extern/arg.h"
-#include "extern/strlcat.h"
-#include "extern/concat.h"
 
 struct arg {
        char *(*func)();
@@ -69,10 +65,9 @@ static char *vol_perc(const char *card);
 static char *wifi_perc(const char *iface);
 static char *wifi_essid(const char *iface);
 static void sighandler(const int signo);
-static void usage(int);
+static void usage(const int eval);
 
 char *argv0;
-char concat[];
 static unsigned short int delay = 0;
 static unsigned short int done;
 static unsigned short int dflag, oflag;
@@ -107,12 +102,13 @@ static char *
 battery_perc(const char *bat)
 {
        int perc;
+       char path[PATH_MAX];
        FILE *fp;
 
-       ccat(3, "/sys/class/power_supply/", bat, "/capacity");
-       fp = fopen(concat, "r");
+       snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/", bat, "/capacity");
+       fp = fopen(path, "r");
        if (fp == NULL) {
-               warn("Failed to open file %s", concat);
+               warn("Failed to open file %s", path);
                return smprintf("%s", UNKNOWN_STR);
        }
        fscanf(fp, "%i", &perc);
@@ -124,13 +120,14 @@ battery_perc(const char *bat)
 static char *
 battery_state(const char *bat)
 {
+       char path[PATH_MAX];
        char state[12];
        FILE *fp;
 
-       ccat(3, "/sys/class/power_supply/", bat, "/status");
-       fp = fopen(concat, "r");
+       snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/", bat, "/status");
+       fp = fopen(path, "r");
        if (fp == NULL) {
-               warn("Failed to open file %s", concat);
+               warn("Failed to open file %s", path);
                return smprintf("%s", UNKNOWN_STR);
        }
        fscanf(fp, "%12s", state);
@@ -426,7 +423,7 @@ run_command(const char *cmd)
        }
        fgets(buf, sizeof(buf), fp);
        pclose(fp);
-       buf[strlen(buf)] = '\0';
+       buf[sizeof(buf)] = '\0';
 
        if ((nlptr = strstr(buf, "\n")) != NULL) {
                nlptr[0] = '\0';
@@ -673,13 +670,14 @@ wifi_perc(const char *iface)
        int perc;
        char buf[255];
        char *datastart;
+       char path[PATH_MAX];
        char status[5];
        FILE *fp;
 
-       ccat(3, "/sys/class/net/", iface, "/operstate");
-       fp = fopen(concat, "r");
+       snprintf(path, sizeof(path), "%s%s%s", "/sys/class/net/", iface, "/operstate");
+       fp = fopen(path, "r");
        if (fp == NULL) {
-               warn("Failed to open file %s", concat);
+               warn("Failed to open file %s", path);
                return smprintf("%s", UNKNOWN_STR);
        }
        fgets(status, 5, fp);
@@ -693,13 +691,13 @@ wifi_perc(const char *iface)
                warn("Failed to open file /proc/net/wireless");
                return smprintf("%s", UNKNOWN_STR);
        }
-       ccat(2, iface, ":");
+
        fgets(buf, sizeof(buf), fp);
        fgets(buf, sizeof(buf), fp);
        fgets(buf, sizeof(buf), fp);
        fclose(fp);
 
-       if ((datastart = strstr(buf, concat)) == NULL) {
+       if ((datastart = strstr(buf, iface)) == NULL) {
                return smprintf("%s", UNKNOWN_STR);
        }
        datastart = (datastart+(strlen(iface)+1));
@@ -746,7 +744,7 @@ sighandler(const int signo)
 }
 
 static void
-usage(int eval)
+usage(const int eval)
 {
        fprintf(stderr, "usage: %s [-d] [-o] [-v] [-h]\n", argv0);
        exit(eval);
@@ -769,7 +767,7 @@ main(int argc, char *argv[])
                        oflag = 1;
                        break;
                case 'v':
-                       printf("slstatus (C) 2016 slstatus engineers\n");
+                       printf("slstatus (C) 2016-2017 slstatus engineers\n");
                        return 0;
                case 'h':
                        usage(0);
@@ -810,9 +808,7 @@ main(int argc, char *argv[])
                                element = smprintf("%s", UNKNOWN_STR);
                                warnx("Failed to format output");
                        }
-                       if (strlcat(status_string, element, sizeof(status_string)) >= sizeof(status_string)) {
-                               warnx("Output too long");
-                       }
+                       strncat(status_string, element, sizeof(status_string) - strlen(status_string) - 1);
                        free(res);
                        free(element);
                }