Xinqi Bao's Git

ram: Move up includes
[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 fp = popen(cmd, "r");
15 if (fp == NULL) {
16 fprintf(stderr, "popen '%s': %s\n", cmd, strerror(errno));
17 return NULL;
18 }
19 p = fgets(buf, sizeof(buf) - 1, fp);
20 pclose(fp);
21 if (!p)
22 return NULL;
23 if ((p = strrchr(buf, '\n')) != NULL)
24 p[0] = '\0';
25
26 return buf[0] ? buf : NULL;
27 }