Xinqi Bao's Git

fixed run_command()
[slstatus.git] / slstatus.c
index fd27570..57ef9a9 100644 (file)
@@ -88,8 +88,9 @@ smprintf(const char *fmt, ...)
        va_end(ap);
 
        ret = malloc(++len);
-       if (ret == NULL)
+       if (ret == NULL) {
                err(1, "malloc");
+       }
 
        va_start(ap, fmt);
        vsnprintf(ret, len, fmt, ap);
@@ -268,16 +269,11 @@ static char *
 hostname(void)
 {
        char buf[HOST_NAME_MAX];
-       FILE *fp;
 
-       fp = fopen("/proc/sys/kernel/hostname", "r");
-       if (fp == NULL) {
-               warn("Failed to open file /proc/sys/kernel/hostname");
+       if (gethostname(buf, sizeof(buf)) == -1) {
+               warn("hostname");
                return smprintf(UNKNOWN_STR);
        }
-       fgets(buf, sizeof(buf), fp);
-       buf[strlen(buf)-1] = '\0';
-       fclose(fp);
 
        return smprintf("%s", buf);
 }
@@ -404,17 +400,19 @@ static char *
 run_command(const char *cmd)
 {
        FILE *fp;
-       char buf[64] = "n/a";
+       char buf[1024] = "n/a";
 
        fp = popen(cmd, "r");
        if (fp == NULL) {
                warn("Failed to get command output for %s", cmd);
                return smprintf(UNKNOWN_STR);
        }
-       fgets(buf, sizeof(buf), fp);
-       buf[sizeof(buf)-1] = '\0';
+       fgets(buf, sizeof(buf)-1, fp);
        pclose(fp);
 
+       buf[strlen(buf)] = '\0';
+       strtok(buf, "\n");
+
        return smprintf("%s", buf);
 }
 
@@ -500,7 +498,10 @@ vol_perc(const char *card)
        snd_mixer_selem_id_free(s_elem);
        snd_mixer_close(handle);
 
-       return smprintf("%d%%", ((uint_fast16_t)(vol * 100) / max));
+       if (max == 0)
+               return smprintf("0%%");
+       else
+               return smprintf("%lu%%", ((uint_fast16_t)(vol * 100) / max));
 }
 
 static char *