1 #define PERLIO_NOT_STDIO 0
4 /* hold file pointer, command, mode, and the status of the command */
8 struct pipe_list *next;
12 /* static, global list pointer */
13 static struct pipe_list *pl = NULL;
16 djgpp_popen (const char *cm, const char *md) /* program name, pipe mode */
23 if ((l1 = (struct pipe_list *) malloc (sizeof (*l1)))
24 && (temp_name = malloc (L_tmpnam)) && tmpnam (temp_name))
32 /* if caller wants to read */
33 if (md[0] == 'r' && (fd = dup (fileno (stdout))) >= 0)
35 if ((l1->fp = freopen (temp_name, "wb", stdout)))
37 l1->exit_status = system (cm);
38 if (dup2 (fd, fileno (stdout)) >= 0)
39 l1->fp = fopen (temp_name, md);
43 /* if caller wants to write */
44 else if (md[0] == 'w' && (l1->command = malloc (1 + strlen (cm))))
46 strcpy (l1->command, cm);
47 l1->fp = fopen (temp_name, md);
52 l1->fp->_flag |= _IORMONCL; /* remove on close */
53 l1->fp->_name_to_remove = temp_name;
64 djgpp_pclose (FILE *pp)
66 struct pipe_list *l1, **l2; /* list pointers */
67 int retval=-1; /* function return value */
69 for (l2 = &pl; *l2 && (*l2)->fp != pp; l2 = &((*l2)->next))
75 /* if pipe was opened to write */
80 close (fileno (l1->fp));
82 if ((fd = dup (fileno (stdin))) >= 0
83 && (freopen (l1->fp->_name_to_remove, "rb", stdin)))
85 retval = system (l1->command);
86 dup2 (fd, fileno (stdin));
92 /* if pipe was opened to read, return the exit status we saved */
93 retval = l1->exit_status;
95 fclose (l1->fp); /* this removes the temp file */
97 return retval; /* retval==0 ? OK : ERROR */
102 #define EXECF_SPAWN 0
106 convretcode (pTHX_ int rc,char *prog,int fl)
108 if (rc < 0 && ckWARN(WARN_EXEC))
109 Perl_warner(aTHX_ packWARN(WARN_EXEC),"Can't %s \"%s\": %s",
110 fl ? "exec" : "spawn",prog,Strerror (errno));
117 do_aspawn (pTHX_ SV *really,SV **mark,SV **sp)
120 char **a,*tmps,**argv;
125 a=argv=(char**) alloca ((sp-mark+3)*sizeof (char*));
129 *a++ = SvPVx(*mark, n_a);
134 if (argv[0][0] != '/' && argv[0][0] != '\\'
135 && !(argv[0][0] && argv[0][1] == ':'
136 && (argv[0][2] == '/' || argv[0][2] != '\\'))
137 ) /* will swawnvp use PATH? */
138 TAINT_ENV(); /* testing IFS here is overkill, probably */
140 if (really && *(tmps = SvPV(really, n_a)))
141 rc=spawnvp (P_WAIT,tmps,argv);
143 rc=spawnvp (P_WAIT,argv[0],argv);
145 return convretcode (rc,argv[0],EXECF_SPAWN);
148 #define EXTRA "\x00\x00\x00\x00\x00\x00"
151 do_spawn2 (pTHX_ char *cmd,int execf)
153 char **argv,**a,*s,*shell,*metachars;
154 int rc,unixysh,result;
157 if ((shell=getenv("SHELL"))==NULL && (shell=getenv("COMSPEC"))==NULL)
158 shell="c:\\command.com" EXTRA;
160 unixysh=_is_unixy_shell (shell);
161 metachars=unixysh ? "$&*(){}[]'\";\\?>|<~`\n" EXTRA : "*?[|<>\"\\" EXTRA;
163 while (*cmd && isSPACE(*cmd))
166 if (strBEGINs (cmd,"/bin/sh") && isSPACE (cmd[7]))
169 /* save an extra exec if possible */
170 /* see if there are shell metacharacters in it */
171 if (strstr (cmd,"..."))
175 if (*cmd=='.' && isSPACE (cmd[1]))
177 if (strBEGINs (cmd,"exec") && isSPACE (cmd[4]))
179 for (s=cmd; *s && isALPHA (*s); s++) ; /* catch VAR=val gizmo */
184 if (strchr (metachars,*s))
186 if (*s=='\n' && s[1]=='\0')
192 if (execf==EXECF_EXEC)
193 result = convretcode (execl (shell,shell,unixysh ? "-c" : "/c",cmd,NULL),cmd,execf);
195 result = convretcode (system (cmd),cmd,execf);
199 Newx (argv,(s-cmd)/2+2,char*);
201 cmd=savepvn (cmd,s-cmd);
205 while (*s && isSPACE (*s)) s++;
208 while (*s && !isSPACE (*s)) s++;
218 if (execf==EXECF_EXEC)
219 rc=execvp (argv[0],argv);
221 rc=spawnvp (P_WAIT,argv[0],argv);
222 result = convretcode (rc,argv[0],execf);
229 do_spawn (pTHX_ char *cmd)
231 return do_spawn2 (aTHX_ cmd,EXECF_SPAWN);
235 Perl_do_exec (pTHX_ const char *cmd)
237 do_spawn2 (aTHX_ cmd,EXECF_EXEC);
251 #define MAXOPENGLOBS 10
253 static struct globinfo myglobs[MAXOPENGLOBS];
255 static struct globinfo *
259 for (ic=0; ic<MAXOPENGLOBS; ic++)
260 if (myglobs[ic].fd==fd)
266 glob_handler (__FSEXT_Fnumber n,int *rv,va_list args)
274 char *p1,*pattern,*name=va_arg (args,char*);
278 if (! strBEGINs (name,"/dev/dosglob/"))
280 if ((gi=searchfd (-1)) == NULL)
284 pattern=alloca (strlen (name+=13)+1);
285 strcpy (pattern,name);
292 if ((p1=strchr (pattern,' '))!=NULL)
294 glob (pattern,ic,0,&pglob);
296 if ((pattern=p1)!=NULL)
299 for (ic=len=0; ic<pglob.gl_pathc; ic++)
300 len+=1+strlen (pglob.gl_pathv[ic]);
303 if ((gi->matches=p1=(char*) malloc (gi->size=len))==NULL)
305 for (ic=0; ic<pglob.gl_pathc; ic++)
307 strcpy (p1,pglob.gl_pathv[ic]);
313 if ((gi->matches=strdup (name))==NULL)
315 gi->size=strlen (name)+1;
318 gi->fd=*rv=__FSEXT_alloc_fd (glob_handler);
323 int fd=va_arg (args,int);
324 char *buf=va_arg (args,char*);
325 size_t siz=va_arg (args,size_t);
327 if ((gi=searchfd (fd))==NULL)
330 if (siz+gi->pos > gi->size)
331 siz = gi->size - gi->pos;
332 memcpy (buf,gi->pos+gi->matches,siz);
339 int fd=va_arg (args,int);
341 if ((gi=searchfd (fd))==NULL)
359 Perl_croak (aTHX_ "Usage: Dos::GetCwd()");
361 char tmp[PATH_MAX+2];
362 ST(0)=sv_newmortal ();
363 if (getcwd (tmp,PATH_MAX+1)!=NULL)
364 sv_setpv ((SV*)ST(0),tmp);
374 XSRETURN_IV (_USE_LFN);
381 Perl_croak_nocontext("Usage: Cwd::sys_cwd()");
385 RETVAL = getcwd(p, MAXPATHLEN);
386 ST(0) = sv_newmortal();
387 sv_setpv((SV*)ST(0), RETVAL);
394 Perl_init_os_extras(pTHX)
396 char *file = __FILE__;
400 newXS ("Dos::GetCwd",dos_GetCwd,file);
401 newXS ("Dos::UseLFN",dos_UseLFN,file);
402 newXS ("Cwd::sys_cwd",XS_Cwd_sys_cwd,file);
404 /* install my File System Extension for globbing */
405 __FSEXT_add_open_handler (glob_handler);
406 memset (myglobs,-1,sizeof (myglobs));
409 static char *perlprefix;
411 #define PERL5 "/perl5"
414 djgpp_pathexp (const char *p)
416 static char expp[PATH_MAX];
417 strcpy (expp,perlprefix);
421 strcat (expp,"/bin");
424 strcat (expp,"/lib" PERL5 "/site");
427 strcat (expp,"/lib" PERL5);
434 Perl_DJGPP_init (int *argcp,char ***argvp)
438 perlprefix=strdup (**argvp);
440 if ((p=strrchr (perlprefix,'/'))!=NULL)
443 if (strEQ (p-4,"/bin"))
447 strcpy (perlprefix,"..");
451 djgpp_fflush (FILE *fp)
455 if ((res = fflush(fp)) == 0 && fp) {
457 if (Fstat(fileno(fp), &s) == 0 && !S_ISSOCK(s.st_mode))
458 res = fsync(fileno(fp));
461 * If the flush succeeded but set end-of-file, we need to clear
462 * the error because our caller may check ferror(). BTW, this
463 * probably means we just flushed an empty file.
465 if (res == 0 && fp && ferror(fp) == EOF) clearerr(fp);
470 int djgpp_get_stream_mode(FILE *f)
472 extern char *__file_handle_modes;
474 int mode = __file_handle_modes[fileno(f)];
475 if (f->_flag & _IORW)
476 return mode | O_RDWR;
477 if (f->_flag & _IOWRT)
478 return mode | O_WRONLY;
479 return mode | O_RDONLY;