test
[eleutheria.git] / ipc / popen_ls.c
blob61d2e28789df577cae0212d8aecb6b492fab11cb
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/wait.h> /* for waitpid() macros */
5 #define MAX_STR 100
7 int main(void)
9 char str[MAX_STR];
10 int ret;
11 FILE *fp;
13 /* Initiate pipe stream to ls(1) */
14 fp = popen("ls", "r");
15 if (fp == NULL) {
16 perror("popen()");
17 exit(EXIT_FAILURE);
20 /* Read from stream */
21 while (fgets(str, MAX_STR, fp) != NULL)
22 printf("%s", str);
24 /* Close pipe stream */
25 ret = pclose(fp);
26 if (ret == -1) {
27 perror("pclose()");
28 exit(EXIT_FAILURE);
30 else {
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().
37 if (WIFEXITED(ret)) {
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 */
45 #ifdef WIFCONTINUED
46 else if (WIFCONTINUED(ret)) {
47 printf("Child continued\n");
49 #endif
50 /* Non standard case -- may never happen */
51 else {
52 printf("Unexpected return value, ret = 0x%x\n", ret);
56 return EXIT_SUCCESS;