Xinqi Bao's Git

ram: fixed int overflow on pagetok macro
[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 warn("popen '%s':", cmd);
16 return NULL;
17 }
18 p = fgets(buf, sizeof(buf) - 1, fp);
19 if (pclose(fp) < 0) {
20 warn("pclose '%s':", cmd);
21 return NULL;
22 }
23 if (!p) {
24 return NULL;
25 }
26 if ((p = strrchr(buf, '\n'))) {
27 p[0] = '\0';
28 }
29
30 return buf[0] ? buf : NULL;
31 }