fixed output from commands without a newline end.

This is useful when using commands such as tr -d '\n' to strip newlines
from output.
This commit is contained in:
aidan 2020-08-27 04:47:07 +00:00
parent 904e407908
commit 500b1fbb0f
1 changed files with 6 additions and 5 deletions

View File

@ -50,16 +50,17 @@ static void (*writestatus) () = setroot;
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;
char c;
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);