popen.c 503 B

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