run_command.c (501B)
1 /* See LICENSE file for copyright and license details. */ 2 #include <stdio.h> 3 #include <string.h> 4 5 #include "../slstatus.h" 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 19 p = fgets(buf, sizeof(buf) - 1, fp); 20 if (pclose(fp) < 0) { 21 warn("pclose '%s':", cmd); 22 return NULL; 23 } 24 if (!p) 25 return NULL; 26 27 if ((p = strrchr(buf, '\n'))) 28 p[0] = '\0'; 29 30 return buf[0] ? buf : NULL; 31 }