source: icGREP/icgrep-devel/cudd-2.5.1/util/pipefork.c @ 5820

Last change on this file since 5820 was 4597, checked in by nmedfort, 4 years ago

Upload of the CUDD library.

File size: 2.1 KB
Line 
1/*
2 * Revision Control Information
3 *
4 * $Id: pipefork.c,v 1.7 2012/02/05 05:34:04 fabio Exp fabio $
5 *
6 */
7/* LINTLIBRARY */
8
9#include "util.h"
10#include <sys/wait.h>
11
12/*
13 * util_pipefork - fork a command and set up pipes to and from
14 *
15 * Rick L Spickelmier, 3/23/86
16 * Richard Rudell, 4/6/86
17 * Rick L Spickelmier, 4/30/90, got rid of slimey vfork semantics
18 *
19 * Returns:
20 *   1 for success, with toCommand and fromCommand pointing to the streams
21 *   0 for failure
22 */
23
24/* ARGSUSED */
25int
26util_pipefork(
27  char * const *argv,           /* normal argv argument list */
28  FILE **toCommand,     /* pointer to the sending stream */
29  FILE **fromCommand,   /* pointer to the reading stream */
30  int *pid)
31{
32#ifdef UNIX
33    int forkpid, waitPid;
34    int topipe[2], frompipe[2];
35    char buffer[1024];
36    int status;
37
38    /* create the PIPES...
39     * fildes[0] for reading from command
40     * fildes[1] for writing to command
41     */
42    if (pipe(topipe)) return(0);
43    if (pipe(frompipe)) return(0);
44
45#ifdef __CYGWIN32__
46    if ((forkpid = fork()) == 0) {
47#else
48    if ((forkpid = vfork()) == 0) {
49#endif
50        /* child here, connect the pipes */
51        (void) dup2(topipe[0], fileno(stdin));
52        (void) dup2(frompipe[1], fileno(stdout));
53
54        (void) close(topipe[0]);
55        (void) close(topipe[1]);
56        (void) close(frompipe[0]);
57        (void) close(frompipe[1]);
58
59        (void) execvp(argv[0], argv);
60        (void) sprintf(buffer, "util_pipefork: can not exec %s", argv[0]);
61        perror(buffer);
62        (void) _exit(1);
63    }
64
65    if (pid) {
66        *pid = forkpid;
67    }
68
69#ifdef __CYGWIN32__
70    waitPid = waitpid(-1, &status, WNOHANG);
71#else
72    waitPid = wait3(&status, WNOHANG, NULL);
73#endif
74
75    /* parent here, use slimey vfork() semantics to get return status */
76    if (waitPid == forkpid && WIFEXITED(status)) {
77        return 0;
78    }
79    if ((*toCommand = fdopen(topipe[1], "w")) == NULL) {
80        return 0;
81    }
82    if ((*fromCommand = fdopen(frompipe[0], "r")) == NULL) {
83        return 0;
84    }
85    (void) close(topipe[0]);
86    (void) close(frompipe[1]);
87    return 1;
88#else
89    (void) fprintf(stderr, 
90        "util_pipefork: not implemented on your operating system\n");
91    return 0;
92#endif
93}
Note: See TracBrowser for help on using the repository browser.