Xinqi Bao's Git

uptime: Add missing brace
[slstatus.git] / components / run_command.c
1 /* See LICENSE file for copyright and license details. */
2 #include <errno.h>
3 #include <stdio.h>
4 #include <string.h>
5
6 #include "../util.h"
7
8 const char *
9 run_command(const char *cmd)
10 {
11 char *p;
12 FILE *fp;
13
14 if (!(fp = popen(cmd, "r"))) {
15 fprintf(stderr, "popen '%s': %s\n", cmd, strerror(errno));
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 }