#include #include #include #include #include #include main() { int pipefds[2]; pid_t pid; char *const parmList[] = {"/bin/ls", "-l", "/u/userid/dirname", NULL }; char lsout[200]; /* buffer for out ls output */ int amt; int status; /* status code from ls */ fclose(stdout); /* avoid stdio interfering with fd 1 */ fclose(stdin); /* avoid stdio interfering with fd 0 */ close(STDOUT_FILENO); /* make sure fd's 0 & 1 are avail */ close(STDIN_FILENO); pipe(pipefds); /* create both ends of a pipe */ if (pipefds[0] == STDOUT_FILENO) { fprintf(stderr, "Warning: Input fileno is 1, should not occur.\n"); pipefds[0] = dup(pipefds[0]); close(STDOUT_FILENO); } if (pipefds[1] != STDOUT_FILENO) { fprintf(stderr, "Warning: Output fileno is not 1, should not occur.\n"); dup2(pipefds[1],STDOUT_FILENO); /* make write end of pipe fd 1 */ close(pipefds[1]); /* close write end */ } pid = oeattach("/bin/ls", parmList); /* run ls command as subtask */ close(STDOUT_FILENO); /* close write end of pipe in parent */ for(;;) { /* read from the pipe */ amt = read(pipefds[0], lsout, sizeof(lsout)); if (amt <= 0) break; fwrite(lsout, 1, amt, stderr); /* write ls output to stderr */ } wait(&status); /* wait for ls to complete */ close(pipefds[0]); /* close pipe input end */ if (WIFEXITED(status)) exit(WEXITSTATUS(status)); else /* if ls failed, use kill to fail the same way */ kill(0, WTERMSIG(status)); }