run_command.c (484B)
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 if (!(fp = popen(cmd, "r"))) { 14 warn("popen '%s':", cmd); 15 return NULL; 16 } 17 p = fgets(buf, sizeof(buf) - 1, fp); 18 if (pclose(fp) < 0) { 19 warn("pclose '%s':", cmd); 20 return NULL; 21 } 22 if (!p) { 23 return NULL; 24 } 25 if ((p = strrchr(buf, '\n'))) { 26 p[0] = '\0'; 27 } 28 29 return buf[0] ? buf : NULL; 30 }