Xinqi Bao's Git

Split into multiple files
[slstatus.git] / run_command.c
1 #include <err.h>
2 #include <stdio.h>
3 #include <string.h>
4
5 #include "util.h"
6
7 const char *
8 run_command(const char *cmd)
9 {
10 char *p;
11 FILE *fp;
12
13 fp = popen(cmd, "r");
14 if (fp == NULL) {
15 warn("Failed to get command output for %s", cmd);
16 return NULL;
17 }
18 p = fgets(buf, sizeof(buf) - 1, fp);
19 pclose(fp);
20 if (!p)
21 return NULL;
22 if ((p = strrchr(buf, '\n')) != NULL)
23 p[0] = '\0';
24
25 return buf[0] ? buf : NULL;
26 }