run_command.c 501 B

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