]> git.armaanb.net Git - dwmblocks.git/commitdiff
fixed output from commands without a newline end.
authoraidan <torrinfail@gmail.com>
Thu, 27 Aug 2020 04:47:07 +0000 (04:47 +0000)
committeraidan <torrinfail@gmail.com>
Thu, 27 Aug 2020 04:47:07 +0000 (04:47 +0000)
This is useful when using commands such as tr -d '\n' to strip newlines
from output.

dwmblocks.c

index 5abd10d2515604d4784d18938eccecb74e0c2027..b6f696ff745162a907f296c5f8a04d1d528f6a0f 100644 (file)
@@ -50,16 +50,17 @@ static void (*writestatus) () = setroot;
 void getcmd(const Block *block, char *output)
 {
        strcpy(output, block->icon);
 void getcmd(const Block *block, char *output)
 {
        strcpy(output, block->icon);
-       char *cmd = block->command;
-       FILE *cmdf = popen(cmd,"r");
+       FILE *cmdf = popen(block->command, "r");
        if (!cmdf)
                return;
        if (!cmdf)
                return;
-       char c;
        int i = strlen(block->icon);
        fgets(output+i, CMDLENGTH-i-delimLen, cmdf);
        i = strlen(output);
        int i = strlen(block->icon);
        fgets(output+i, CMDLENGTH-i-delimLen, cmdf);
        i = strlen(output);
-       if (delim[0] != '\0' && --i)
-                strncpy(output+i, delim, delimLen); 
+       if (delim[0] != '\0') {
+               //only chop off newline if one is present at the end
+               i = output[i-1] == '\n' ? i-1 : i;
+               strncpy(output+i, delim, delimLen); 
+       }
        else
                output[i++] = '\0';
        pclose(cmdf);
        else
                output[i++] = '\0';
        pclose(cmdf);