]> git.saurik.com Git - bison.git/blame - lib/readpipe.c
* src/assoc.c, src/asssoc.h (assoc_t, assoc_to_string): New.
[bison.git] / lib / readpipe.c
CommitLineData
11ee57d9
AD
1/* Open a pipe to read from a program without intermediary sh.
2 Copyright (C) 1992, 1997 Free Software Foundation, Inc.
3
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)
7 any later version.
8
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.
13
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. */
17
18/* Written by David MacKenzie. */
19
20#if HAVE_CONFIG_H
21# include <config.h>
22#endif
23
24#include <stdio.h>
25
26#if __STDC__
27#include <stdarg.h>
28#else
29#include <varargs.h>
30#endif
31
32#if HAVE_UNISTD_H
33# include <unistd.h>
34#endif
35
36/* Open a pipe to read from a program without intermediary sh. Checks
37 PATH. Sample use:
38
39 stream = readpipe ("progname", "arg1", "arg2", (char *) 0);
40
41 Return 0 on error. */
42
43#if __STDC__
44FILE *
45readpipe (char *progname, ...)
46#else
47FILE *
48readpipe (va_alist)
49#endif
50{
51#if ! __STDC__
52 char *progname;
53#endif
54 int fds[2];
55 va_list ap;
56 char *args[100];
57 int argno = 0;
58
59 /* Copy arguments into `args'. */
60#if __STDC__
61 va_start (ap, progname);
62#else
63 va_start (ap);
64 progname = va_arg (ap, char *);
65#endif
66 args[argno++] = progname;
67 while ((args[argno++] = va_arg (ap, char *)) != NULL)
68 ;
69 va_end (ap);
70
71 if (pipe (fds) == -1)
72 return 0;
73
74 switch (fork ())
75 {
76 case 0: /* Child. Write to pipe. */
77 close (fds[0]); /* Not needed. */
78 if (fds[1] != 1) /* Redirect 1 (stdout) only if needed. */
79 {
80 close (1); /* We don't want the old stdout. */
81 if (dup (fds[1]) == 0)/* Maybe stdin was closed. */
82 {
83 dup (fds[1]); /* Guaranteed to dup to 1 (stdout). */
84 close (0);
85 }
86 close (fds[1]); /* No longer needed. */
87 }
88 execvp (args[0], args);
89 _exit (2); /* 2 for `cmp'. */
90 case -1: /* Error. */
91 return 0;
92 default: /* Parent. Read from pipe. */
93 close (fds[1]); /* Not needed. */
94 return fdopen (fds[0], "r");
95 }
96}