]>
git.saurik.com Git - bison.git/blob - lib/readpipe.c
1 /* Open a pipe to read from a program without intermediary sh.
2 Copyright (C) 1992, 1997 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by David MacKenzie. */
36 /* Open a pipe to read from a program without intermediary sh. Checks
39 stream = readpipe ("progname", "arg1", "arg2", (char *) 0);
45 readpipe (char *progname
, ...)
59 /* Copy arguments into `args'. */
61 va_start (ap
, progname
);
64 progname
= va_arg (ap
, char *);
66 args
[argno
++] = progname
;
67 while ((args
[argno
++] = va_arg (ap
, char *)) != NULL
)
76 case 0: /* Child. Write to pipe. */
77 close (fds
[0]); /* Not needed. */
78 if (fds
[1] != 1) /* Redirect 1 (stdout) only if needed. */
80 close (1); /* We don't want the old stdout. */
81 if (dup (fds
[1]) == 0)/* Maybe stdin was closed. */
83 dup (fds
[1]); /* Guaranteed to dup to 1 (stdout). */
86 close (fds
[1]); /* No longer needed. */
88 execvp (args
[0], args
);
89 _exit (2); /* 2 for `cmp'. */
92 default: /* Parent. Read from pipe. */
93 close (fds
[1]); /* Not needed. */
94 return fdopen (fds
[0], "r");