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 (pTHX_ int rc,char *prog,int fl)
122 if (rc < 0 && ckWARN(WARN_EXEC))
123 Perl_warner(aTHX_ WARN_EXEC,"Can't %s \"%s\": %s",
124 fl ? "exec" : "spawn",prog,Strerror (errno));
131 do_aspawn (pTHX_ SV *really,SV **mark,SV **sp)
134 char **a,*tmps,**argv;
139 a=argv=(char**) alloca ((sp-mark+3)*sizeof (char*));
143 *a++ = SvPVx(*mark, n_a);
148 if (argv[0][0] != '/' && argv[0][0] != '\\'
149 && !(argv[0][0] && argv[0][1] == ':'
150 && (argv[0][2] == '/' || argv[0][2] != '\\'))
151 ) /* will swawnvp use PATH? */
152 TAINT_ENV(); /* testing IFS here is overkill, probably */
154 if (really && *(tmps = SvPV(really, n_a)))
155 rc=spawnvp (P_WAIT,tmps,argv);
157 rc=spawnvp (P_WAIT,argv[0],argv);
159 return convretcode (rc,argv[0],EXECF_SPAWN);
162 #define EXTRA "\x00\x00\x00\x00\x00\x00"
165 do_spawn2 (pTHX_ char *cmd,int execf)
167 char **a,*s,*shell,*metachars;
170 if ((shell=getenv("SHELL"))==NULL && (shell=getenv("COMSPEC"))==NULL)
171 shell="c:\\command.com" EXTRA;
173 unixysh=_is_unixy_shell (shell);
174 metachars=unixysh ? "$&*(){}[]'\";\\?>|<~`\n" EXTRA : "*?[|<>\"\\" EXTRA;
176 while (*cmd && isSPACE(*cmd))
179 if (strnEQ (cmd,"/bin/sh",7) && isSPACE (cmd[7]))
182 /* save an extra exec if possible */
183 /* see if there are shell metacharacters in it */
184 if (strstr (cmd,"..."))
188 if (*cmd=='.' && isSPACE (cmd[1]))
190 if (strnEQ (cmd,"exec",4) && isSPACE (cmd[4]))
192 for (s=cmd; *s && isALPHA (*s); s++) ; /* catch VAR=val gizmo */
197 if (strchr (metachars,*s))
199 if (*s=='\n' && s[1]=='\0')
205 if (execf==EXECF_EXEC)
206 return convretcode (execl (shell,shell,unixysh ? "-c" : "/c",cmd,NULL),cmd,execf);
207 return convretcode (system (cmd),cmd,execf);
210 New (1303,PL_Argv,(s-cmd)/2+2,char*);
211 PL_Cmd=savepvn (cmd,s-cmd);
213 for (s=PL_Cmd; *s;) {
214 while (*s && isSPACE (*s)) s++;
217 while (*s && !isSPACE (*s)) s++;
225 if (execf==EXECF_EXEC)
226 rc=execvp (PL_Argv[0],PL_Argv);
228 rc=spawnvp (P_WAIT,PL_Argv[0],PL_Argv);
229 return convretcode (rc,PL_Argv[0],execf);
233 do_spawn (pTHX_ char *cmd)
235 return do_spawn2 (aTHX_ cmd,EXECF_SPAWN);
239 Perl_do_exec (pTHX_ char *cmd)
241 do_spawn2 (aTHX_ 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)
288 pattern=alloca (strlen (name+=13)+1);
289 strcpy (pattern,name);
296 if ((p1=strchr (pattern,' '))!=NULL)
298 glob (pattern,ic,0,&pglob);
300 if ((pattern=p1)!=NULL)
303 for (ic=len=0; ic<pglob.gl_pathc; ic++)
304 len+=1+strlen (pglob.gl_pathv[ic]);
307 if ((gi->matches=p1=(char*) malloc (gi->size=len))==NULL)
309 for (ic=0; ic<pglob.gl_pathc; ic++)
311 strcpy (p1,pglob.gl_pathv[ic]);
317 if ((gi->matches=strdup (name))==NULL)
319 gi->size=strlen (name)+1;
322 gi->fd=*rv=__FSEXT_alloc_fd (glob_handler);
327 int fd=va_arg (args,int);
328 char *buf=va_arg (args,char*);
329 size_t siz=va_arg (args,size_t);
331 if ((gi=searchfd (fd))==NULL)
334 if (siz+gi->pos > gi->size)
335 siz = gi->size - gi->pos;
336 memcpy (buf,gi->pos+gi->matches,siz);
343 int fd=va_arg (args,int);
345 if ((gi=searchfd (fd))==NULL)
363 Perl_croak (aTHX_ "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);
381 Perl_init_os_extras(pTHX)
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,"..");
436 djgpp_fflush (FILE *fp)
440 if ((res = fflush(fp)) == 0 && fp) {
442 if (Fstat(fileno(fp), &s) == 0 && !S_ISSOCK(s.st_mode))
443 res = fsync(fileno(fp));
446 * If the flush succeeded but set end-of-file, we need to clear
447 * the error because our caller may check ferror(). BTW, this
448 * probably means we just flushed an empty file.
450 if (res == 0 && fp && ferror(fp) == EOF) clearerr(fp);