123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- #include "config.h"
- #if HAVE_UNISTD_H == 1
- #define _POSIX_SOURCE
- #include <unistd.h>
- #endif
- #include <stdio.h>
- #if HAVE_SYS_WAIT_H == 1
- #include <sys/wait.h>
- #endif
- #ifndef EXTERN
- # ifdef __cplusplus
- # define EXTERN extern "C"
- # else
- # define EXTERN extern
- # endif
- #endif
- int
- util_pipefork(
- char * const *argv,
- FILE **toCommand,
- FILE **fromCommand,
- int *pid )
- {
- #if HAVE_SYS_WAIT_H == 1
- int forkpid, waitPid;
- int topipe[2], frompipe[2];
- char buffer[1024];
- int status;
-
- if (pipe(topipe)) return(0);
- if (pipe(frompipe)) return(0);
- if ((forkpid = fork()) == 0) {
-
- (void) dup2(topipe[0], fileno(stdin));
- (void) dup2(frompipe[1], fileno(stdout));
- (void) close(topipe[0]);
- (void) close(topipe[1]);
- (void) close(frompipe[0]);
- (void) close(frompipe[1]);
- (void) execvp(argv[0], argv);
- (void) sprintf(buffer, "util_pipefork: can not exec %s", argv[0]);
- perror(buffer);
- (void) _exit(1);
- }
- if (pid) {
- *pid = forkpid;
- }
- waitPid = waitpid(-1, &status, WNOHANG);
-
- if (waitPid == forkpid && WIFEXITED(status)) {
- return 0;
- }
- if ((*toCommand = fdopen(topipe[1], "w")) == NULL) {
- return 0;
- }
- if ((*fromCommand = fdopen(frompipe[0], "r")) == NULL) {
- return 0;
- }
- (void) close(topipe[0]);
- (void) close(frompipe[1]);
- return 1;
- #else
- (void) argv;
- (void) toCommand;
- (void) fromCommand;
- (void) pid;
- (void) fprintf(stderr,
- "util_pipefork: not implemented on your operating system\n");
- return 0;
- #endif
- }
|