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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
|
#include <libc/stubs.h>
#include <io.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <libc/file.h>
#include <process.h>
#include <fcntl.h>
#include <glob.h>
#include <sys/fsext.h>
#include <crt0.h>
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
/* hold file pointer, command, mode, and the status of the command */
struct pipe_list {
FILE *fp;
int exit_status;
struct pipe_list *next;
char *command, mode;
};
/* static, global list pointer */
static struct pipe_list *pl = NULL;
FILE *
popen (const char *cm, const char *md) /* program name, pipe mode */
{
struct pipe_list *l1;
int fd;
char *temp_name=NULL;
/* make new node */
if ((l1 = (struct pipe_list *) malloc (sizeof (*l1)))
&& (temp_name = malloc (L_tmpnam)) && tmpnam (temp_name))
{
l1->fp = NULL;
l1->command = NULL;
l1->next = pl;
l1->exit_status = -1;
l1->mode = md[0];
/* if caller wants to read */
if (md[0] == 'r' && (fd = dup (fileno (stdout))) >= 0)
{
if ((l1->fp = freopen (temp_name, "wb", stdout)))
{
l1->exit_status = system (cm);
if (dup2 (fd, fileno (stdout)) >= 0)
l1->fp = fopen (temp_name, md);
}
close (fd);
}
/* if caller wants to write */
else if (md[0] == 'w' && (l1->command = malloc (1 + strlen (cm))))
{
strcpy (l1->command, cm);
l1->fp = fopen (temp_name, md);
}
if (l1->fp)
{
l1->fp->_flag |= _IORMONCL; /* remove on close */
l1->fp->_name_to_remove = temp_name;
return (pl = l1)->fp;
}
free (l1->command);
}
free (temp_name);
free (l1);
return NULL;
}
int
pclose (FILE *pp)
{
struct pipe_list *l1, **l2; /* list pointers */
int retval=-1; /* function return value */
for (l2 = &pl; *l2 && (*l2)->fp != pp; l2 = &((*l2)->next))
;
if (!(l1 = *l2))
return retval;
*l2 = l1->next;
/* if pipe was opened to write */
if (l1->mode == 'w')
{
int fd;
fflush (l1->fp);
close (fileno (l1->fp));
if ((fd = dup (fileno (stdin))) >= 0
&& (freopen (l1->fp->_name_to_remove, "rb", stdin)))
{
retval = system (l1->command);
dup2 (fd, fileno (stdin));
}
close (fd);
free (l1->command);
}
else
/* if pipe was opened to read, return the exit status we saved */
retval = l1->exit_status;
fclose (l1->fp); /* this removes the temp file */
free (l1);
return retval; /* retval==0 ? OK : ERROR */
}
/**/
#define EXECF_SPAWN 0
#define EXECF_EXEC 1
static int
convretcode (pTHX_ int rc,char *prog,int fl)
{
if (rc < 0 && PL_dowarn)
Perl_warn (aTHX_ "Can't %s \"%s\": %s",fl ? "exec" : "spawn",prog,Strerror (errno));
if (rc > 0)
return rc <<= 8;
if (rc < 0)
return 255 << 8;
return 0;
}
int
do_aspawn (pTHX_ SV *really,SV **mark,SV **sp)
{
dTHR;
int rc;
char **a,*tmps,**argv;
STRLEN n_a;
if (sp<=mark)
return -1;
a=argv=(char**) alloca ((sp-mark+3)*sizeof (char*));
while (++mark <= sp)
if (*mark)
*a++ = SvPVx(*mark, n_a);
else
*a++ = "";
*a = Nullch;
if (argv[0][0] != '/' && argv[0][0] != '\\'
&& !(argv[0][0] && argv[0][1] == ':'
&& (argv[0][2] == '/' || argv[0][2] != '\\'))
) /* will swawnvp use PATH? */
TAINT_ENV(); /* testing IFS here is overkill, probably */
if (really && *(tmps = SvPV(really, n_a)))
rc=spawnvp (P_WAIT,tmps,argv);
else
rc=spawnvp (P_WAIT,argv[0],argv);
return convretcode (rc,argv[0],EXECF_SPAWN);
}
#define EXTRA "\x00\x00\x00\x00\x00\x00"
int
do_spawn2 (pTHX_ char *cmd,int execf)
{
char **a,*s,*shell,*metachars;
int rc,unixysh;
if ((shell=getenv("SHELL"))==NULL && (shell=getenv("COMSPEC"))==NULL)
shell="c:\\command.com" EXTRA;
unixysh=_is_unixy_shell (shell);
metachars=unixysh ? "$&*(){}[]'\";\\?>|<~`\n" EXTRA : "*?[|<>\"\\" EXTRA;
while (*cmd && isSPACE(*cmd))
cmd++;
if (strnEQ (cmd,"/bin/sh",7) && isSPACE (cmd[7]))
cmd+=5;
/* save an extra exec if possible */
/* see if there are shell metacharacters in it */
if (strstr (cmd,"..."))
goto doshell;
if (unixysh)
{
if (*cmd=='.' && isSPACE (cmd[1]))
goto doshell;
if (strnEQ (cmd,"exec",4) && isSPACE (cmd[4]))
goto doshell;
for (s=cmd; *s && isALPHA (*s); s++) ; /* catch VAR=val gizmo */
if (*s=='=')
goto doshell;
}
for (s=cmd; *s; s++)
if (strchr (metachars,*s))
{
if (*s=='\n' && s[1]=='\0')
{
*s='\0';
break;
}
doshell:
if (execf==EXECF_EXEC)
return convretcode (execl (shell,shell,unixysh ? "-c" : "/c",cmd,NULL),cmd,execf);
return convretcode (system (cmd),cmd,execf);
}
New (1303,PL_Argv,(s-cmd)/2+2,char*);
PL_Cmd=savepvn (cmd,s-cmd);
a=PL_Argv;
for (s=PL_Cmd; *s;) {
while (*s && isSPACE (*s)) s++;
if (*s)
*(a++)=s;
while (*s && !isSPACE (*s)) s++;
if (*s)
*s++='\0';
}
*a=Nullch;
if (!PL_Argv[0])
return -1;
if (execf==EXECF_EXEC)
rc=execvp (PL_Argv[0],PL_Argv);
else
rc=spawnvp (P_WAIT,PL_Argv[0],PL_Argv);
return convretcode (rc,PL_Argv[0],execf);
}
int
do_spawn (pTHX_ char *cmd)
{
return do_spawn2 (aTHX_ cmd,EXECF_SPAWN);
}
bool
Perl_do_exec (pTHX_ char *cmd)
{
do_spawn2 (aTHX_ cmd,EXECF_EXEC);
return FALSE;
}
/**/
struct globinfo
{
int fd;
char *matches;
size_t size;
};
#define MAXOPENGLOBS 10
static struct globinfo myglobs[MAXOPENGLOBS];
static struct globinfo *
searchfd (int fd)
{
int ic;
for (ic=0; ic<MAXOPENGLOBS; ic++)
if (myglobs[ic].fd==fd)
return myglobs+ic;
return NULL;
}
static int
glob_handler (__FSEXT_Fnumber n,int *rv,va_list args)
{
unsigned ic;
struct globinfo *gi;
switch (n)
{
case __FSEXT_open:
{
char *p1,*pattern,*name=va_arg (args,char*);
STRLEN len;
glob_t pglob;
if (strnNE (name,"/dev/dosglob/",13))
break;
if ((gi=searchfd (-1)) == NULL)
break;
pattern=alloca (strlen (name+=13)+1);
strcpy (pattern,name);
if (!_USE_LFN)
strlwr (pattern);
ic=pglob.gl_pathc=0;
pglob.gl_pathv=NULL;
while (pattern)
{
if ((p1=strchr (pattern,' '))!=NULL)
*p1=0;
glob (pattern,ic,0,&pglob);
ic=GLOB_APPEND;
if ((pattern=p1)!=NULL)
pattern++;
}
for (ic=len=0; ic<pglob.gl_pathc; ic++)
len+=1+strlen (pglob.gl_pathv[ic]);
if (len)
{
if ((gi->matches=p1=(char*) malloc (gi->size=len))==NULL)
break;
for (ic=0; ic<pglob.gl_pathc; ic++)
{
strcpy (p1,pglob.gl_pathv[ic]);
p1+=strlen (p1)+1;
}
}
else
{
if ((gi->matches=strdup (name))==NULL)
break;
gi->size=strlen (name)+1;
}
globfree (&pglob);
gi->fd=*rv=__FSEXT_alloc_fd (glob_handler);
return 1;
}
case __FSEXT_read:
{
int fd=va_arg (args,int);
char *buf=va_arg (args,char*);
size_t siz=va_arg (args,size_t);
if ((gi=searchfd (fd))==NULL)
break;
ic=tell (fd);
if (siz+ic>=gi->size)
siz=gi->size-ic;
memcpy (buf,ic+gi->matches,siz);
lseek (fd,siz,1);
*rv=siz;
return 1;
}
case __FSEXT_close:
{
int fd=va_arg (args,int);
if ((gi=searchfd (fd))==NULL)
break;
free (gi->matches);
gi->fd=-1;
break;
}
default:
break;
}
return 0;
}
static
XS(dos_GetCwd)
{
dXSARGS;
if (items)
Perl_croak (aTHX_ "Usage: Dos::GetCwd()");
{
char tmp[PATH_MAX+2];
ST(0)=sv_newmortal ();
if (getcwd (tmp,PATH_MAX+1)!=NULL)
sv_setpv ((SV*)ST(0),tmp);
}
XSRETURN (1);
}
static
XS(dos_UseLFN)
{
dXSARGS;
XSRETURN_IV (_USE_LFN);
}
void
Perl_init_os_extras(pTHX)
{
char *file = __FILE__;
dXSUB_SYS;
newXS ("Dos::GetCwd",dos_GetCwd,file);
newXS ("Dos::UseLFN",dos_UseLFN,file);
/* install my File System Extension for globbing */
__FSEXT_add_open_handler (glob_handler);
memset (myglobs,-1,sizeof (myglobs));
}
static char *perlprefix;
#define PERL5 "/perl5"
char *djgpp_pathexp (const char *p)
{
static char expp[PATH_MAX];
strcpy (expp,perlprefix);
switch (p[0])
{
case 'B':
strcat (expp,"/bin");
break;
case 'S':
strcat (expp,"/lib" PERL5 "/site");
break;
default:
strcat (expp,"/lib" PERL5);
break;
}
return expp;
}
void
Perl_DJGPP_init (int *argcp,char ***argvp)
{
char *p;
perlprefix=strdup (**argvp);
strlwr (perlprefix);
if ((p=strrchr (perlprefix,'/'))!=NULL)
{
*p=0;
if (strEQ (p-4,"/bin"))
p[-4]=0;
}
else
strcpy (perlprefix,"..");
}
|