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