1 /* epopen.c
2    A version of popen that goes through ixsspawn.
3 
4    Copyright (C) 1992 Ian Lance Taylor
5 
6    This file is part of the Taylor UUCP package.
7 
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2 of the
11    License, or (at your option) any later version.
12 
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17 
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
21 
22    The author of the program may be contacted at ian@airs.com.
23    */
24 
25 #include "uucp.h"
26 
27 #include "sysdep.h"
28 
29 #include <errno.h>
30 
31 /* A version of popen that goes through ixsspawn.  This actually takes
32    an array of arguments rather than a string, and takes a boolean
33    read/write value rather than a string.  It sets *pipid to the
34    process ID of the child.  */
35 
36 FILE *
espopen(pazargs,frd,pipid)37 espopen (pazargs, frd, pipid)
38      const char **pazargs;
39      boolean frd;
40      pid_t *pipid;
41 {
42   int aidescs[3];
43   pid_t ipid;
44   FILE *eret;
45 
46   if (frd)
47     {
48       aidescs[0] = SPAWN_NULL;
49       aidescs[1] = SPAWN_READ_PIPE;
50     }
51   else
52     {
53       aidescs[0] = SPAWN_WRITE_PIPE;
54       aidescs[1] = SPAWN_NULL;
55     }
56   aidescs[2] = SPAWN_NULL;
57 
58   ipid = ixsspawn (pazargs, aidescs, TRUE, FALSE,
59 		   (const char *) NULL, FALSE, TRUE,
60 		   (const char *) NULL, (const char *) NULL,
61 		   (const char *) NULL);
62   if (ipid < 0)
63     return NULL;
64 
65   if (frd)
66     eret = fdopen (aidescs[1], (char *) "r");
67   else
68     eret = fdopen (aidescs[0], (char *) "w");
69   if (eret == NULL)
70     {
71       int ierr;
72 
73       ierr = errno;
74       (void) close (frd ? aidescs[1] : aidescs[0]);
75       (void) kill (ipid, SIGKILL);
76       (void) ixswait ((unsigned long) ipid, (const char *) NULL);
77       errno = ierr;
78       return NULL;
79     }
80 
81   *pipid = ipid;
82 
83   return eret;
84 }
85