1 #include <libc/stubs.h>
12 #include <sys/fsext.h>
18 /* hold file pointer, command, mode, and the status of the command */
22 struct pipe_list *next;
26 /* static, global list pointer */
27 static struct pipe_list *pl = NULL;
30 popen (const char *cm, const char *md) /* program name, pipe mode */
37 if ((l1 = (struct pipe_list *) malloc (sizeof (*l1)))
38 && (temp_name = malloc (L_tmpnam)) && tmpnam (temp_name))
46 /* if caller wants to read */
47 if (md[0] == 'r' && (fd = dup (fileno (stdout))) >= 0)
49 if ((l1->fp = freopen (temp_name, "wb", stdout)))
51 l1->exit_status = system (cm);
52 if (dup2 (fd, fileno (stdout)) >= 0)
53 l1->fp = fopen (temp_name, md);
57 /* if caller wants to write */
58 else if (md[0] == 'w' && (l1->command = malloc (1 + strlen (cm))))
60 strcpy (l1->command, cm);
61 l1->fp = fopen (temp_name, md);
66 l1->fp->_flag |= _IORMONCL; /* remove on close */
67 l1->fp->_name_to_remove = temp_name;
80 struct pipe_list *l1, **l2; /* list pointers */
81 int retval=-1; /* function return value */
83 for (l2 = &pl; *l2 && (*l2)->fp != pp; l2 = &((*l2)->next))
89 /* if pipe was opened to write */
94 close (fileno (l1->fp));
96 if ((fd = dup (fileno (stdin))) >= 0
97 && (freopen (l1->fp->_name_to_remove, "rb", stdin)))
99 retval = system (l1->command);
100 dup2 (fd, fileno (stdin));
106 /* if pipe was opened to read, return the exit status we saved */
107 retval = l1->exit_status;
109 fclose (l1->fp); /* this removes the temp file */
111 return retval; /* retval==0 ? OK : ERROR */
116 #define EXECF_SPAWN 0
120 convretcode (int rc,char *prog,int fl)
122 if (rc < 0 && PL_dowarn)
123 warn ("Can't %s \"%s\": %s",fl ? "exec" : "spawn",prog,Strerror (errno));
132 do_aspawn (SV *really,SV **mark,SV **sp)
136 char **a,*tmps,**argv;
140 a=argv=(char**) alloca ((sp-mark+3)*sizeof (char*));
144 *a++ = SvPVx(*mark, PL_na);
149 if (argv[0][0] != '/' && argv[0][0] != '\\'
150 && !(argv[0][0] && argv[0][1] == ':'
151 && (argv[0][2] == '/' || argv[0][2] != '\\'))
152 ) /* will swawnvp use PATH? */
153 TAINT_ENV(); /* testing IFS here is overkill, probably */
155 if (really && *(tmps = SvPV(really, PL_na)))
156 rc=spawnvp (P_WAIT,tmps,argv);
158 rc=spawnvp (P_WAIT,argv[0],argv);
160 return convretcode (rc,argv[0],EXECF_SPAWN);
163 #define EXTRA "\x00\x00\x00\x00\x00\x00"
166 do_spawn2 (char *cmd,int execf)
168 char **a,*s,*shell,*metachars;
171 if ((shell=getenv("SHELL"))==NULL && (shell=getenv("COMSPEC"))==NULL)
172 shell="c:\\command.com" EXTRA;
174 unixysh=_is_unixy_shell (shell);
175 metachars=unixysh ? "$&*(){}[]'\";\\?>|<~`\n" EXTRA : "*?[|<>\"\\" EXTRA;
177 while (*cmd && isSPACE(*cmd))
180 if (strnEQ (cmd,"/bin/sh",7) && isSPACE (cmd[7]))
183 /* save an extra exec if possible */
184 /* see if there are shell metacharacters in it */
185 if (strstr (cmd,"..."))
189 if (*cmd=='.' && isSPACE (cmd[1]))
191 if (strnEQ (cmd,"exec",4) && isSPACE (cmd[4]))
193 for (s=cmd; *s && isALPHA (*s); s++) ; /* catch VAR=val gizmo */
198 if (strchr (metachars,*s))
200 if (*s=='\n' && s[1]=='\0')
206 if (execf==EXECF_EXEC)
207 return convretcode (execl (shell,shell,unixysh ? "-c" : "/c",cmd,NULL),cmd,execf);
208 return convretcode (system (cmd),cmd,execf);
211 New (1303,PL_Argv,(s-cmd)/2+2,char*);
212 PL_Cmd=savepvn (cmd,s-cmd);
214 for (s=PL_Cmd; *s;) {
215 while (*s && isSPACE (*s)) s++;
218 while (*s && !isSPACE (*s)) s++;
226 if (execf==EXECF_EXEC)
227 rc=execvp (PL_Argv[0],PL_Argv);
229 rc=spawnvp (P_WAIT,PL_Argv[0],PL_Argv);
230 return convretcode (rc,PL_Argv[0],execf);
236 return do_spawn2 (cmd,EXECF_SPAWN);
242 do_spawn2 (cmd,EXECF_EXEC);
255 #define MAXOPENGLOBS 10
257 static struct globinfo myglobs[MAXOPENGLOBS];
259 static struct globinfo *
263 for (ic=0; ic<MAXOPENGLOBS; ic++)
264 if (myglobs[ic].fd==fd)
270 glob_handler (__FSEXT_Fnumber n,int *rv,va_list args)
278 char *p1,*pattern,*name=va_arg (args,char*);
282 if (strnNE (name,"/dev/dosglob/",13))
284 if ((gi=searchfd (-1)) == NULL)
287 pattern=alloca (strlen (name+=13)+1);
288 strcpy (pattern,name);
295 if ((p1=strchr (pattern,' '))!=NULL)
297 glob (pattern,ic,0,&pglob);
299 if ((pattern=p1)!=NULL)
302 for (ic=len=0; ic<pglob.gl_pathc; ic++)
303 len+=1+strlen (pglob.gl_pathv[ic]);
306 if ((gi->matches=p1=(char*) malloc (gi->size=len))==NULL)
308 for (ic=0; ic<pglob.gl_pathc; ic++)
310 strcpy (p1,pglob.gl_pathv[ic]);
316 if ((gi->matches=strdup (name))==NULL)
318 gi->size=strlen (name)+1;
321 gi->fd=*rv=__FSEXT_alloc_fd (glob_handler);
326 int fd=va_arg (args,int);
327 char *buf=va_arg (args,char*);
328 size_t siz=va_arg (args,size_t);
330 if ((gi=searchfd (fd))==NULL)
334 if (siz+ic>=gi->size)
336 memcpy (buf,ic+gi->matches,siz);
343 int fd=va_arg (args,int);
345 if ((gi=searchfd (fd))==NULL)
363 croak ("Usage: Dos::GetCwd()");
365 char tmp[PATH_MAX+2];
366 ST(0)=sv_newmortal ();
367 if (getcwd (tmp,PATH_MAX+1)!=NULL)
368 sv_setpv ((SV*)ST(0),tmp);
377 XSRETURN_IV (_USE_LFN);
383 char *file = __FILE__;
387 newXS ("Dos::GetCwd",dos_GetCwd,file);
388 newXS ("Dos::UseLFN",dos_UseLFN,file);
390 /* install my File System Extension for globbing */
391 __FSEXT_add_open_handler (glob_handler);
392 memset (myglobs,-1,sizeof (myglobs));
395 static char *perlprefix;
397 #define PERL5 "/perl5"
399 char *djgpp_pathexp (const char *p)
401 static char expp[PATH_MAX];
402 strcpy (expp,perlprefix);
406 strcat (expp,"/bin");
409 strcat (expp,"/lib" PERL5 "/site");
412 strcat (expp,"/lib" PERL5);
419 Perl_DJGPP_init (int *argcp,char ***argvp)
423 perlprefix=strdup (**argvp);
425 if ((p=strrchr (perlprefix,'/'))!=NULL)
428 if (strEQ (p-4,"/bin"))
432 strcpy (perlprefix,"..");