Xinqi Bao's Git

4a6389620fdd8ca8169e0bc03f92b74f1e0f669b
[slstatus.git] / components / run_command.c
1 /* See LICENSE file for copyright and license details. */
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 fprintf(stderr, "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 }