popen.c 502 B

123456789101112131415161718192021222324252627282930
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "test_helpers.h"
  4. int main(void) {
  5. FILE *fp;
  6. int status;
  7. char path[256];
  8. fp = popen("ls -1 example_dir", "r");
  9. if (fp == NULL) {
  10. perror("popen");
  11. exit(EXIT_FAILURE);
  12. }
  13. while (fgets(path, 256, fp) != NULL) {
  14. printf("%s", path);
  15. }
  16. status = pclose(fp);
  17. if (status == -1) {
  18. perror("pclose");
  19. exit(EXIT_FAILURE);
  20. } else {
  21. printf("status %x\n", status);
  22. }
  23. }