popen.c 452 B

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