1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
|
/* popen.c -- popen/pclose for OS/2. */
/* Set to 0 for distribution.
Search for "DIAGNOSTIC" in the code to see what it's for. */
#define DIAGNOSTIC 0
#include <process.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <ctype.h>
#include <string.h>
#include <fcntl.h>
#include "config.h"
#include "os2inc.h"
#define LL_VAL ULONG
#define LL_KEY PID /* also ULONG, really */
#define STDIN 0
#define STDOUT 1
#define STDERR 2
/* ********************************************************************* *
* *
* First, a little linked-list library to help keep track of pipes: *
* *
* ********************************************************************* */
/* Map integer PID's onto integer termination codes. */
struct pid_list
{
HFILE pid; /* key */
ULONG term_code; /* val */
struct pid_list *next; /* duh */
};
static struct pid_list *pid_ll = (struct pid_list *) NULL;
/* The ll_*() functions all make use of the global var `pid_ll'. */
void
ll_insert (HFILE key, ULONG val)
{
struct pid_list *new;
new = (struct pid_list *) malloc (sizeof (*new));
new->pid = key;
new->term_code = val;
new->next = pid_ll;
pid_ll = new;
}
void
ll_delete (int key)
{
struct pid_list *this, *last;
this = pid_ll;
last = (struct pid_list *) NULL;
while (this)
{
if (this->pid == key)
{
/* Delete this node and leave. */
if (last)
last->next = this->next;
else
pid_ll = this->next;
free (this);
return;
}
/* Else no match, so try the next one. */
last = this;
this = this->next;
}
}
ULONG
ll_lookup (HFILE key)
{
struct pid_list *this = pid_ll;
while (this)
{
if (this->pid == key)
return this->term_code;
/* Else no match, so try the next one. */
this = this->next;
}
/* Zero is special in this context anyway. */
return 0;
}
#if DIAGNOSTIC
ULONG
ll_length ()
{
struct pid_list *this = pid_ll;
unsigned long int len;
for (len = 0; this; len++)
this = this->next;
return len;
}
ULONG
ll_print ()
{
struct pid_list *this = pid_ll;
unsigned long int i;
for (i = 0; this; i++)
{
printf ("pid_ll[%d] == (%5d --> %5d)\n",
i, this->pid, this->term_code);
this = this->next;
}
if (i == 0)
printf ("No entries.\n");
return i;
}
#endif /* DIAGNOSTIC */
/* ********************************************************************* *
* *
* End of linked-list library, beginning of popen/pclose: *
* *
* ********************************************************************* */
/*
* Routine: popen
* Returns: FILE pointer to pipe.
* Action : Exec program connected via pipe, connect a FILE * to the
* pipe and return it.
* Params : Command - Program to run
* Mode - Mode to open pipe. "r" implies pipe is connected
* to the programs stdout, "w" connects to stdin.
*/
FILE *
popen (const char *Command, const char *Mode)
{
HFILE End1, End2, Std, Old1, Old2, Tmp;
FILE *File;
char Fail[256],
*Args,
CmdLine[256],
*CmdExe;
RESULTCODES
Result;
int Rc;
if (DosCreatePipe (&End1, &End2, 4096))
return NULL;
Std = (*Mode == 'w') ? STDIN : STDOUT ;
if (Std == STDIN)
{
Tmp = End1; End1 = End2; End2 = Tmp;
}
Old1 = -1; /* save stdin or stdout */
DosDupHandle (Std, &Old1);
DosSetFHState (Old1, OPEN_FLAGS_NOINHERIT);
Tmp = Std; /* redirect stdin or stdout */
DosDupHandle (End2, &Tmp);
if (Std == 1)
{
Old2 = -1; /* save stderr */
DosDupHandle (STDERR, &Old2);
DosSetFHState (Old2, OPEN_FLAGS_NOINHERIT);
Tmp = STDERR;
DosDupHandle (End2, &Tmp);
}
DosClose (End2);
DosSetFHState (End1, OPEN_FLAGS_NOINHERIT);
if ((CmdExe = getenv ("COMSPEC")) == NULL )
CmdExe = "cmd.exe";
strcpy (CmdLine, CmdExe);
Args = CmdLine + strlen (CmdLine) + 1; /* skip zero */
strcpy (Args, "/c ");
strcat (Args, Command);
Args[strlen (Args) + 1] = '\0'; /* two zeroes */
Rc = DosExecPgm (Fail, sizeof (Fail), EXEC_ASYNCRESULT,
(unsigned char *) CmdLine, 0, &Result,
(unsigned char *) CmdExe);
Tmp = Std; /* restore stdin or stdout */
DosDupHandle (Old1, &Tmp);
DosClose (Old1);
if (Std == STDOUT)
{
Tmp = STDERR; /* restore stderr */
DosDupHandle (Old2, &Tmp);
DosClose (Old2);
}
if (Rc)
{
DosClose (End1);
return NULL;
}
#ifdef __WATCOMC__
/* Watcom does not allow mixing operating system handles and
* C library handles, so we have to convert.
*/
File = fdopen (_hdopen (End1, *Mode == 'w'? O_WRONLY : O_RDONLY), Mode);
#else
File = fdopen (End1, Mode);
#endif
ll_insert ((LL_KEY) End1, (LL_VAL) Result.codeTerminate);
return File;
}
/*
* Routine: popenRW
* Returns: PID of child process
* Action : Exec program connected via pipe, connect int fd's to
* both the stdin and stdout of the process.
* Params : Command - Program to run
* Pipes - Array of 2 ints to store the pipe descriptors
* Pipe[0] writes to child's stdin,
* Pipe[1] reads from child's stdout/stderr
*/
int
popenRW (const char **argv, int *pipes)
{
HFILE Out1, Out2, In1, In2;
HFILE Old0 = -1, Old1 = -1, Old2 = -1, Tmp;
int pid;
if (DosCreatePipe (&Out2, &Out1, 4096))
return FALSE;
if (DosCreatePipe (&In1, &In2, 4096))
{
DosClose (Out1);
DosClose (Out2);
return FALSE;
}
/* Save std{in,out,err} */
DosDupHandle (STDIN, &Old0);
DosSetFHState (Old1, OPEN_FLAGS_NOINHERIT);
DosDupHandle (STDOUT, &Old1);
DosSetFHState (Old2, OPEN_FLAGS_NOINHERIT);
DosDupHandle (STDERR, &Old2);
DosSetFHState (Old2, OPEN_FLAGS_NOINHERIT);
/* Redirect std{in,out,err} */
Tmp = STDIN;
DosDupHandle (In1, &Tmp);
Tmp = STDOUT;
DosDupHandle (Out1, &Tmp);
Tmp = STDERR;
DosDupHandle (Out1, &Tmp);
/* Close file handles not needed in child */
DosClose (In1);
DosClose (Out1);
DosSetFHState (In2, OPEN_FLAGS_NOINHERIT);
DosSetFHState (Out2, OPEN_FLAGS_NOINHERIT);
/* Spawn we now our hoary brood. */
pid = spawnvp (P_NOWAIT, argv[0], argv);
/* Restore std{in,out,err} */
Tmp = STDIN;
DosDupHandle (Old0, &Tmp);
DosClose (Old0);
Tmp = STDOUT;
DosDupHandle (Old1, &Tmp);
DosClose (Old1);
Tmp = STDERR;
DosDupHandle (Old2, &Tmp);
DosClose (Old2);
if(pid < 0)
{
DosClose (In2);
DosClose (Out2);
return -1;
}
pipes[0] = In2;
_setmode (pipes[0], O_BINARY);
pipes[1] = Out2;
_setmode (pipes[1], O_BINARY);
/* Save ID of write-to-child pipe for pclose() */
ll_insert ((LL_KEY) In2, (LL_VAL) pid);
return pid;
}
/*
* Routine: pclose
* Returns: TRUE on success
* Action : Close a pipe opened with popen();
* Params : Pipe - pipe to close
*/
int
pclose (FILE *Pipe)
{
RESULTCODES rc;
PID pid, pid1;
int Handle = fileno (Pipe);
fclose (Pipe);
rc.codeTerminate = -1;
pid1 = (PID) ll_lookup ((LL_KEY) Handle);
/* if pid1 is zero, something's seriously wrong */
if (pid1 != 0)
{
DosWaitChild (DCWA_PROCESSTREE, DCWW_WAIT, &rc, &pid, pid1);
ll_delete ((LL_KEY) Handle);
}
return rc.codeTerminate == 0 ? rc.codeResult : -1;
}
#if DIAGNOSTIC
void
main ()
{
FILE *fp1, *fp2, *fp3;
int c;
ll_print ();
fp1 = popen ("gcc --version", "r");
ll_print ();
fp2 = popen ("link386 /?", "r");
ll_print ();
fp3 = popen ("dir", "r");
ll_print ();
while ((c = getc (fp1)) != EOF)
printf ("%c", c);
while ((c = getc (fp2)) != EOF)
printf ("%c", c);
while ((c = getc (fp3)) != EOF)
printf ("%c", c);
pclose (fp1);
ll_print ();
pclose (fp2);
ll_print ();
pclose (fp3);
ll_print ();
return;
}
#endif /* DIAGNOSTIC */
|