run_command.c 484 B

12345678910111213141516171819202122232425262728293031
  1. /* See LICENSE file for copyright and license details. */
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "../util.h"
  5. const char *
  6. run_command(const char *cmd)
  7. {
  8. char *p;
  9. FILE *fp;
  10. if (!(fp = popen(cmd, "r"))) {
  11. warn("popen '%s':", cmd);
  12. return NULL;
  13. }
  14. p = fgets(buf, sizeof(buf) - 1, fp);
  15. if (pclose(fp) < 0) {
  16. warn("pclose '%s':", cmd);
  17. return NULL;
  18. }
  19. if (!p) {
  20. return NULL;
  21. }
  22. if ((p = strrchr(buf, '\n'))) {
  23. p[0] = '\0';
  24. }
  25. return buf[0] ? buf : NULL;
  26. }