1 #include <err.h> /* for errx() */
4 #include <sys/wait.h> /* for waitpid() macros */
14 /* Initiate pipe stream to ``ls'' */
15 fp
= popen("ls", "r");
17 errx(EXIT_FAILURE
, "popen()");
19 /* Read from stream */
20 while (fgets(str
, MAX_STR
, fp
) != NULL
) {
24 /* Close pipe stream */
27 errx(EXIT_FAILURE
, "pclose()");
31 * Use macros described under wait() to inspect the return value
32 * of pclose() in order to determine success/failure of command
33 * executed by popen().
37 printf("Child exited, ret = %d\n", WEXITSTATUS(ret
));
38 } else if (WIFSIGNALED(ret
)) {
39 printf("Child killed, signal %d\n", WTERMSIG(ret
));
40 } else if (WIFSTOPPED(ret
)) {
41 printf("Child stopped, signal %d\n", WSTOPSIG(ret
));
43 /* Not all implementations support this */
45 else if (WIFCONTINUED(ret
)) {
46 printf("Child continued\n");
49 /* Non standard case -- may never happen */
51 printf("Unexpected return value, ret = 0x%x\n", ret
);