Update Changes.
[p5sagit/p5-mst-13.2.git] / doio.c
1 /*    doio.c
2  *
3  *    Copyright (c) 1991-2001, Larry Wall
4  *
5  *    You may distribute under the terms of either the GNU General Public
6  *    License or the Artistic License, as specified in the README file.
7  *
8  */
9
10 /*
11  * "Far below them they saw the white waters pour into a foaming bowl, and
12  * then swirl darkly about a deep oval basin in the rocks, until they found
13  * their way out again through a narrow gate, and flowed away, fuming and
14  * chattering, into calmer and more level reaches."
15  */
16
17 #include "EXTERN.h"
18 #define PERL_IN_DOIO_C
19 #include "perl.h"
20
21 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
22 #ifndef HAS_SEM
23 #include <sys/ipc.h>
24 #endif
25 #ifdef HAS_MSG
26 #include <sys/msg.h>
27 #endif
28 #ifdef HAS_SHM
29 #include <sys/shm.h>
30 # ifndef HAS_SHMAT_PROTOTYPE
31     extern Shmat_t shmat (int, char *, int);
32 # endif
33 #endif
34 #endif
35
36 #ifdef I_UTIME
37 #  if defined(_MSC_VER) || defined(__MINGW32__)
38 #    include <sys/utime.h>
39 #  else
40 #    include <utime.h>
41 #  endif
42 #endif
43
44 #ifdef O_EXCL
45 #  define OPEN_EXCL O_EXCL
46 #else
47 #  define OPEN_EXCL 0
48 #endif
49
50 #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
51 #include <signal.h>
52 #endif
53
54 bool
55 Perl_do_open(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
56              int rawmode, int rawperm, PerlIO *supplied_fp)
57 {
58     return do_openn(gv, name, len, as_raw, rawmode, rawperm,
59                     supplied_fp, (SV **) NULL, 0);
60 }
61
62 bool
63 Perl_do_open9(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
64               int rawmode, int rawperm, PerlIO *supplied_fp, SV *svs,
65               I32 num_svs)
66 {
67     return do_openn(gv, name, len, as_raw, rawmode, rawperm,
68                     supplied_fp, &svs, 1);
69 }
70
71 bool
72 Perl_do_openn(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
73               int rawmode, int rawperm, PerlIO *supplied_fp, SV **svp,
74               I32 num_svs)
75 {
76     register IO *io = GvIOn(gv);
77     PerlIO *saveifp = Nullfp;
78     PerlIO *saveofp = Nullfp;
79     char savetype = IoTYPE_CLOSED;
80     int writing = 0;
81     PerlIO *fp;
82     int fd;
83     int result;
84     bool was_fdopen = FALSE;
85     bool in_raw = 0, in_crlf = 0, out_raw = 0, out_crlf = 0;
86     char *type  = NULL;
87     char *deftype = NULL;
88     char mode[4];               /* stdio file mode ("r\0", "rb\0", "r+b\0" etc.) */
89     SV *svs = (num_svs) ? *svp : Nullsv;
90
91     Zero(mode,sizeof(mode),char);
92     PL_forkprocess = 1;         /* assume true if no fork */
93
94     /* Collect default raw/crlf info from the op */
95     if (PL_op && PL_op->op_type == OP_OPEN) {
96         /* set up disciplines */
97         U8 flags = PL_op->op_private;
98         in_raw = (flags & OPpOPEN_IN_RAW);
99         in_crlf = (flags & OPpOPEN_IN_CRLF);
100         out_raw = (flags & OPpOPEN_OUT_RAW);
101         out_crlf = (flags & OPpOPEN_OUT_CRLF);
102     }
103
104     /* If currently open - close before we re-open */
105     if (IoIFP(io)) {
106         fd = PerlIO_fileno(IoIFP(io));
107         if (IoTYPE(io) == IoTYPE_STD)
108             result = 0;
109         else if (fd <= PL_maxsysfd) {
110             saveifp = IoIFP(io);
111             saveofp = IoOFP(io);
112             savetype = IoTYPE(io);
113             result = 0;
114         }
115         else if (IoTYPE(io) == IoTYPE_PIPE)
116             result = PerlProc_pclose(IoIFP(io));
117         else if (IoIFP(io) != IoOFP(io)) {
118             if (IoOFP(io)) {
119                 result = PerlIO_close(IoOFP(io));
120                 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
121             }
122             else
123                 result = PerlIO_close(IoIFP(io));
124         }
125         else
126             result = PerlIO_close(IoIFP(io));
127         if (result == EOF && fd > PL_maxsysfd)
128             PerlIO_printf(Perl_error_log,
129                           "Warning: unable to close filehandle %s properly.\n",
130                           GvENAME(gv));
131         IoOFP(io) = IoIFP(io) = Nullfp;
132     }
133
134     if (as_raw) {
135         /* sysopen style args, i.e. integer mode and permissions */
136
137 #if defined(USE_64_BIT_RAWIO) && defined(O_LARGEFILE)
138         rawmode |= O_LARGEFILE;
139 #endif
140
141 #ifndef O_ACCMODE
142 #define O_ACCMODE 3             /* Assume traditional implementation */
143 #endif
144
145         switch (result = rawmode & O_ACCMODE) {
146         case O_RDONLY:
147              IoTYPE(io) = IoTYPE_RDONLY;
148              break;
149         case O_WRONLY:
150              IoTYPE(io) = IoTYPE_WRONLY;
151              break;
152         case O_RDWR:
153         default:
154              IoTYPE(io) = IoTYPE_RDWR;
155              break;
156         }
157
158         writing = (result > 0);
159         fd = PerlLIO_open3(name, rawmode, rawperm);
160
161         if (fd == -1)
162             fp = NULL;
163         else {
164             STRLEN ix = 0;
165             if (result == O_RDONLY) {
166                 mode[ix++] = 'r';
167             }
168 #ifdef O_APPEND
169             else if (rawmode & O_APPEND) {
170                 mode[ix++] = 'a';
171                 if (result != O_WRONLY)
172                     mode[ix++] = '+';
173             }
174 #endif
175             else {
176                 if (result == O_WRONLY)
177                     mode[ix++] = 'w';
178                 else {
179                     mode[ix++] = 'r';
180                     mode[ix++] = '+';
181                 }
182             }
183             if (rawmode & O_BINARY)
184                 mode[ix++] = 'b';
185             mode[ix] = '\0';
186             fp = PerlIO_fdopen(fd, mode);
187             if (!fp)
188                 PerlLIO_close(fd);
189         }
190     }
191     else {
192         /* Regular (non-sys) open */
193         char *oname = name;
194         STRLEN olen = len;
195         char *tend;
196         int dodup = 0;
197
198         type = savepvn(name, len);
199         tend = type+len;
200         SAVEFREEPV(type);
201         /* Loose trailing white space */
202         while (tend > type && isSPACE(tend[-1]))
203             *tend-- = '\0';
204         if (num_svs) {
205             /* New style explict name, type is just mode and discipline/layer info */
206             STRLEN l;
207             name = SvPV(svs, l) ;
208             len = (I32)l;
209             name = savepvn(name, len);
210             SAVEFREEPV(name);
211             /*SUPPRESS 530*/
212             for (; isSPACE(*type); type++) ;
213         }
214         else {
215             name = type;
216             len  = tend-type;
217         }
218         IoTYPE(io) = *type;
219         if (*type == IoTYPE_RDWR && (!num_svs || tend > type+1 && tend[-1] != IoTYPE_PIPE)) { /* scary */
220             mode[1] = *type++;
221             writing = 1;
222         }
223
224         if (*type == IoTYPE_PIPE) {
225             if (num_svs) {
226                 if (type[1] != IoTYPE_STD) {
227                   unknown_desr:
228                     Perl_croak(aTHX_ "Unknown open() mode '%.*s'", (int)olen, oname);
229                 }
230                 type++;
231             }
232             /*SUPPRESS 530*/
233             for (type++; isSPACE(*type); type++) ;
234             if (!num_svs) {
235                 name = type;
236                 len = tend-type;
237             }
238             if (*name == '\0') { /* command is missing 19990114 */
239                 if (ckWARN(WARN_PIPE))
240                     Perl_warner(aTHX_ WARN_PIPE, "Missing command in piped open");
241                 errno = EPIPE;
242                 goto say_false;
243             }
244             if (strNE(name,"-") || num_svs)
245                 TAINT_ENV();
246             TAINT_PROPER("piped open");
247             if (!num_svs && name[len-1] == '|') {
248                 name[--len] = '\0' ;
249                 if (ckWARN(WARN_PIPE))
250                     Perl_warner(aTHX_ WARN_PIPE, "Can't open bidirectional pipe");
251             }
252             mode[0] = 'w';
253             writing = 1;
254             if (out_raw)
255                 strcat(mode, "b");
256             else if (out_crlf)
257                 strcat(mode, "t");
258             fp = PerlProc_popen(name,mode);
259         }
260         else if (*type == IoTYPE_WRONLY) {
261             TAINT_PROPER("open");
262             type++;
263             if (*type == IoTYPE_WRONLY) {
264                 /* Two IoTYPE_WRONLYs in a row make for an IoTYPE_APPEND. */
265                 mode[0] = IoTYPE(io) = IoTYPE_APPEND;
266                 type++;
267             }
268             else
269                 mode[0] = 'w';
270             writing = 1;
271
272             if (out_raw)
273                 strcat(mode, "b");
274             else if (out_crlf)
275                 strcat(mode, "t");
276
277             if (*type == '&') {
278                 name = type;
279               duplicity:
280                 if (num_svs)
281                     goto unknown_desr;
282                 dodup = 1;
283                 name++;
284                 if (*name == '=') {
285                     dodup = 0;
286                     name++;
287                 }
288                 if (!*name && supplied_fp)
289                     fp = supplied_fp;
290                 else {
291                     /*SUPPRESS 530*/
292                     for (; isSPACE(*name); name++) ;
293                     if (isDIGIT(*name))
294                         fd = atoi(name);
295                     else {
296                         IO* thatio;
297                         gv = gv_fetchpv(name,FALSE,SVt_PVIO);
298                         thatio = GvIO(gv);
299                         if (!thatio) {
300 #ifdef EINVAL
301                             SETERRNO(EINVAL,SS$_IVCHAN);
302 #endif
303                             goto say_false;
304                         }
305                         if (IoIFP(thatio)) {
306                             PerlIO *fp = IoIFP(thatio);
307                             /* Flush stdio buffer before dup. --mjd
308                              * Unfortunately SEEK_CURing 0 seems to
309                              * be optimized away on most platforms;
310                              * only Solaris and Linux seem to flush
311                              * on that. --jhi */
312 #ifdef USE_SFIO
313                             /* sfio fails to clear error on next
314                                sfwrite, contrary to documentation.
315                                -- Nick Clark */
316                             if (PerlIO_seek(fp, 0, SEEK_CUR) == -1)
317                                 PerlIO_clearerr(fp);
318 #endif
319                             /* On the other hand, do all platforms
320                              * take gracefully to flushing a read-only
321                              * filehandle?  Perhaps we should do
322                              * fsetpos(src)+fgetpos(dst)?  --nik */
323                             PerlIO_flush(fp);
324                             fd = PerlIO_fileno(fp);
325                             /* When dup()ing STDIN, STDOUT or STDERR
326                              * explicitly set appropriate access mode */
327                             if (IoIFP(thatio) == PerlIO_stdout()
328                                 || IoIFP(thatio) == PerlIO_stderr())
329                                 IoTYPE(io) = IoTYPE_WRONLY;
330                             else if (IoIFP(thatio) == PerlIO_stdin())
331                                 IoTYPE(io) = IoTYPE_RDONLY;
332                             /* When dup()ing a socket, say result is
333                              * one as well */
334                             else if (IoTYPE(thatio) == IoTYPE_SOCKET)
335                                 IoTYPE(io) = IoTYPE_SOCKET;
336                         }
337                         else
338                             fd = -1;
339                     }
340                     if (dodup)
341                         fd = PerlLIO_dup(fd);
342                     else
343                         was_fdopen = TRUE;
344                     if (!(fp = PerlIO_fdopen(fd,mode))) {
345                         if (dodup)
346                             PerlLIO_close(fd);
347                     }
348                 }
349             }
350             else {
351                 /*SUPPRESS 530*/
352                 for (; isSPACE(*type); type++) ;
353                 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
354                     /*SUPPRESS 530*/
355                     type++;
356                     fp = PerlIO_stdout();
357                     IoTYPE(io) = IoTYPE_STD;
358                 }
359                 else  {
360                     fp = PerlIO_open((num_svs ? name : type), mode);
361                 }
362             }
363         }
364         else if (*type == IoTYPE_RDONLY) {
365             /*SUPPRESS 530*/
366             for (type++; isSPACE(*type); type++) ;
367             mode[0] = 'r';
368             if (in_raw)
369                 strcat(mode, "b");
370             else if (in_crlf)
371                 strcat(mode, "t");
372
373             if (*type == '&') {
374                 name = type;
375                 goto duplicity;
376             }
377             if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
378                 /*SUPPRESS 530*/
379                 type++;
380                 fp = PerlIO_stdin();
381                 IoTYPE(io) = IoTYPE_STD;
382             }
383             else
384                 fp = PerlIO_open((num_svs ? name : type), mode);
385         }
386         else if ((num_svs && type[0] == IoTYPE_STD && type[1] == IoTYPE_PIPE) ||
387                  (!num_svs && tend > type+1 && tend[-1] == IoTYPE_PIPE)) {
388             if (num_svs) {
389                 type += 2;   /* skip over '-|' */
390             }
391             else {
392                 *--tend = '\0';
393                 while (tend > type && isSPACE(tend[-1]))
394                     *--tend = '\0';
395                 /*SUPPRESS 530*/
396                 for (; isSPACE(*type); type++) ;
397                 name = type;
398                 len  = tend-type;
399             }
400             if (*name == '\0') { /* command is missing 19990114 */
401                 if (ckWARN(WARN_PIPE))
402                     Perl_warner(aTHX_ WARN_PIPE, "Missing command in piped open");
403                 errno = EPIPE;
404                 goto say_false;
405             }
406             if (strNE(name,"-") || num_svs)
407                 TAINT_ENV();
408             TAINT_PROPER("piped open");
409             mode[0] = 'r';
410             if (in_raw)
411                 strcat(mode, "b");
412             else if (in_crlf)
413                 strcat(mode, "t");
414             fp = PerlProc_popen(name,mode);
415             IoTYPE(io) = IoTYPE_PIPE;
416         }
417         else {
418             if (num_svs)
419                 goto unknown_desr;
420             name = type;
421             IoTYPE(io) = IoTYPE_RDONLY;
422             /*SUPPRESS 530*/
423             for (; isSPACE(*name); name++) ;
424             mode[0] = 'r';
425             if (in_raw)
426                 strcat(mode, "b");
427             else if (in_crlf)
428                 strcat(mode, "t");
429             if (strEQ(name,"-")) {
430                 fp = PerlIO_stdin();
431                 IoTYPE(io) = IoTYPE_STD;
432             }
433             else {
434                 fp = PerlIO_open(name,mode);
435             }
436         }
437     }
438     if (!fp) {
439         if (ckWARN(WARN_NEWLINE) && IoTYPE(io) == IoTYPE_RDONLY && strchr(name, '\n'))
440             Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "open");
441         goto say_false;
442     }
443     if (IoTYPE(io) && IoTYPE(io) != IoTYPE_PIPE && IoTYPE(io) != IoTYPE_STD) {
444         if (PerlLIO_fstat(PerlIO_fileno(fp),&PL_statbuf) < 0) {
445             (void)PerlIO_close(fp);
446             goto say_false;
447         }
448         if (S_ISSOCK(PL_statbuf.st_mode))
449             IoTYPE(io) = IoTYPE_SOCKET; /* in case a socket was passed in to us */
450 #ifdef HAS_SOCKET
451         else if (
452 #ifdef S_IFMT
453             !(PL_statbuf.st_mode & S_IFMT)
454 #else
455             !PL_statbuf.st_mode
456 #endif
457             && IoTYPE(io) != IoTYPE_WRONLY  /* Dups of STD* filehandles already have */
458             && IoTYPE(io) != IoTYPE_RDONLY  /* type so they aren't marked as sockets */
459         ) {                                 /* on OS's that return 0 on fstat()ed pipe */
460             char tmpbuf[256];
461             Sock_size_t buflen = sizeof tmpbuf;
462             if (PerlSock_getsockname(PerlIO_fileno(fp), (struct sockaddr *)tmpbuf,
463                             &buflen) >= 0
464                   || errno != ENOTSOCK)
465                 IoTYPE(io) = IoTYPE_SOCKET; /* some OS's return 0 on fstat()ed socket */
466                                 /* but some return 0 for streams too, sigh */
467         }
468 #endif
469     }
470     if (saveifp) {              /* must use old fp? */
471         /* If fd is less that PL_maxsysfd i.e. STDIN..STDERR
472            then dup the new fileno down
473          */
474         fd = PerlIO_fileno(saveifp);
475         if (saveofp) {
476             PerlIO_flush(saveofp);      /* emulate PerlIO_close() */
477             if (saveofp != saveifp) {   /* was a socket? */
478                 PerlIO_close(saveofp);
479                 /* This looks very suspect - NI-S 24 Nov 2000 */
480                 if (fd > 2)
481                     Safefree(saveofp);  /* ??? */
482             }
483         }
484         if (fd != PerlIO_fileno(fp)) {
485             Pid_t pid;
486             SV *sv;
487
488             PerlLIO_dup2(PerlIO_fileno(fp), fd);
489 #ifdef VMS
490             if (fd != PerlIO_fileno(PerlIO_stdin())) {
491               char newname[FILENAME_MAX+1];
492               if (fgetname(fp, newname)) {
493                 if (fd == PerlIO_fileno(PerlIO_stdout())) Perl_vmssetuserlnm("SYS$OUTPUT", newname);
494                 if (fd == PerlIO_fileno(PerlIO_stderr())) Perl_vmssetuserlnm("SYS$ERROR",  newname);
495               }
496             }
497 #endif
498             LOCK_FDPID_MUTEX;
499             sv = *av_fetch(PL_fdpid,PerlIO_fileno(fp),TRUE);
500             (void)SvUPGRADE(sv, SVt_IV);
501             pid = SvIVX(sv);
502             SvIVX(sv) = 0;
503             sv = *av_fetch(PL_fdpid,fd,TRUE);
504             UNLOCK_FDPID_MUTEX;
505             (void)SvUPGRADE(sv, SVt_IV);
506             SvIVX(sv) = pid;
507             if (!was_fdopen)
508                 PerlIO_close(fp);
509
510         }
511         fp = saveifp;
512         PerlIO_clearerr(fp);
513     }
514 #if defined(HAS_FCNTL) && defined(F_SETFD)
515     {
516         int save_errno = errno;
517         fd = PerlIO_fileno(fp);
518         fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
519         errno = save_errno;
520     }
521 #endif
522     IoIFP(io) = fp;
523     if (!num_svs) {
524         /* Need to supply default type info from open.pm */
525         SV *layers = PL_curcop->cop_io;
526         type = NULL;
527         if (layers) {
528             STRLEN len;
529             type = SvPV(layers,len);
530             if (type && mode[0] != 'r') {
531                 /* Skip to write part */
532                 char *s = strchr(type,0);
533                 if (s && (s-type) < len) {
534                     type = s+1;
535                 }
536             }
537         }
538     }
539     if (type) {
540         while (isSPACE(*type)) type++;
541         if (*type) {
542            errno = 0;
543            if (PerlIO_apply_layers(aTHX_ IoIFP(io),mode,type) != 0) {
544                 goto say_false;
545            }
546         }
547     }
548
549     IoFLAGS(io) &= ~IOf_NOLINE;
550     if (writing) {
551         if (IoTYPE(io) == IoTYPE_SOCKET
552             || (IoTYPE(io) == IoTYPE_WRONLY && S_ISCHR(PL_statbuf.st_mode)) )
553         {
554             mode[0] = 'w';
555             if (!(IoOFP(io) = PerlIO_fdopen(PerlIO_fileno(fp),mode))) {
556                 PerlIO_close(fp);
557                 IoIFP(io) = Nullfp;
558                 goto say_false;
559             }
560             if (type && *type) {
561                 if (PerlIO_apply_layers(aTHX_ IoOFP(io),mode,type) != 0) {
562                     PerlIO_close(IoOFP(io));
563                     PerlIO_close(fp);
564                     IoIFP(io) = Nullfp;
565                     IoOFP(io) = Nullfp;
566                     goto say_false;
567                 }
568             }
569         }
570         else
571             IoOFP(io) = fp;
572     }
573     return TRUE;
574
575 say_false:
576     IoIFP(io) = saveifp;
577     IoOFP(io) = saveofp;
578     IoTYPE(io) = savetype;
579     return FALSE;
580 }
581
582 PerlIO *
583 Perl_nextargv(pTHX_ register GV *gv)
584 {
585     register SV *sv;
586 #ifndef FLEXFILENAMES
587     int filedev;
588     int fileino;
589 #endif
590     Uid_t fileuid;
591     Gid_t filegid;
592     IO *io = GvIOp(gv);
593
594     if (!PL_argvoutgv)
595         PL_argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO);
596     if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
597         IoFLAGS(io) &= ~IOf_START;
598         if (PL_inplace) {
599             if (!PL_argvout_stack)
600                 PL_argvout_stack = newAV();
601             av_push(PL_argvout_stack, SvREFCNT_inc(PL_defoutgv));
602         }
603     }
604     if (PL_filemode & (S_ISUID|S_ISGID)) {
605         PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv)));  /* chmod must follow last write */
606 #ifdef HAS_FCHMOD
607         (void)fchmod(PL_lastfd,PL_filemode);
608 #else
609         (void)PerlLIO_chmod(PL_oldname,PL_filemode);
610 #endif
611     }
612     PL_filemode = 0;
613     while (av_len(GvAV(gv)) >= 0) {
614         STRLEN oldlen;
615         sv = av_shift(GvAV(gv));
616         SAVEFREESV(sv);
617         sv_setsv(GvSV(gv),sv);
618         SvSETMAGIC(GvSV(gv));
619         PL_oldname = SvPVx(GvSV(gv), oldlen);
620         if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,Nullfp)) {
621             if (PL_inplace) {
622                 TAINT_PROPER("inplace open");
623                 if (oldlen == 1 && *PL_oldname == '-') {
624                     setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
625                     return IoIFP(GvIOp(gv));
626                 }
627 #ifndef FLEXFILENAMES
628                 filedev = PL_statbuf.st_dev;
629                 fileino = PL_statbuf.st_ino;
630 #endif
631                 PL_filemode = PL_statbuf.st_mode;
632                 fileuid = PL_statbuf.st_uid;
633                 filegid = PL_statbuf.st_gid;
634                 if (!S_ISREG(PL_filemode)) {
635                     if (ckWARN_d(WARN_INPLACE)) 
636                         Perl_warner(aTHX_ WARN_INPLACE,
637                             "Can't do inplace edit: %s is not a regular file",
638                             PL_oldname );
639                     do_close(gv,FALSE);
640                     continue;
641                 }
642                 if (*PL_inplace) {
643                     char *star = strchr(PL_inplace, '*');
644                     if (star) {
645                         char *begin = PL_inplace;
646                         sv_setpvn(sv, "", 0);
647                         do {
648                             sv_catpvn(sv, begin, star - begin);
649                             sv_catpvn(sv, PL_oldname, oldlen);
650                             begin = ++star;
651                         } while ((star = strchr(begin, '*')));
652                         if (*begin)
653                             sv_catpv(sv,begin);
654                     }
655                     else {
656                         sv_catpv(sv,PL_inplace);
657                     }
658 #ifndef FLEXFILENAMES
659                     if (PerlLIO_stat(SvPVX(sv),&PL_statbuf) >= 0
660                       && PL_statbuf.st_dev == filedev
661                       && PL_statbuf.st_ino == fileino
662 #ifdef DJGPP
663                       || (_djstat_fail_bits & _STFAIL_TRUENAME)!=0
664 #endif
665                       )
666                     {
667                         if (ckWARN_d(WARN_INPLACE))     
668                             Perl_warner(aTHX_ WARN_INPLACE,
669                               "Can't do inplace edit: %s would not be unique",
670                               SvPVX(sv));
671                         do_close(gv,FALSE);
672                         continue;
673                     }
674 #endif
675 #ifdef HAS_RENAME
676 #if !defined(DOSISH) && !defined(__CYGWIN__)
677                     if (PerlLIO_rename(PL_oldname,SvPVX(sv)) < 0) {
678                         if (ckWARN_d(WARN_INPLACE))     
679                             Perl_warner(aTHX_ WARN_INPLACE,
680                               "Can't rename %s to %s: %s, skipping file",
681                               PL_oldname, SvPVX(sv), Strerror(errno) );
682                         do_close(gv,FALSE);
683                         continue;
684                     }
685 #else
686                     do_close(gv,FALSE);
687                     (void)PerlLIO_unlink(SvPVX(sv));
688                     (void)PerlLIO_rename(PL_oldname,SvPVX(sv));
689                     do_open(gv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,O_RDONLY,0,Nullfp);
690 #endif /* DOSISH */
691 #else
692                     (void)UNLINK(SvPVX(sv));
693                     if (link(PL_oldname,SvPVX(sv)) < 0) {
694                         if (ckWARN_d(WARN_INPLACE))     
695                             Perl_warner(aTHX_ WARN_INPLACE,
696                               "Can't rename %s to %s: %s, skipping file",
697                               PL_oldname, SvPVX(sv), Strerror(errno) );
698                         do_close(gv,FALSE);
699                         continue;
700                     }
701                     (void)UNLINK(PL_oldname);
702 #endif
703                 }
704                 else {
705 #if !defined(DOSISH) && !defined(AMIGAOS)
706 #  ifndef VMS  /* Don't delete; use automatic file versioning */
707                     if (UNLINK(PL_oldname) < 0) {
708                         if (ckWARN_d(WARN_INPLACE))     
709                             Perl_warner(aTHX_ WARN_INPLACE,
710                               "Can't remove %s: %s, skipping file",
711                               PL_oldname, Strerror(errno) );
712                         do_close(gv,FALSE);
713                         continue;
714                     }
715 #  endif
716 #else
717                     Perl_croak(aTHX_ "Can't do inplace edit without backup");
718 #endif
719                 }
720
721                 sv_setpvn(sv,">",!PL_inplace);
722                 sv_catpvn(sv,PL_oldname,oldlen);
723                 SETERRNO(0,0);          /* in case sprintf set errno */
724 #ifdef VMS
725                 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
726                  O_WRONLY|O_CREAT|O_TRUNC,0,Nullfp))
727 #else
728                 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
729                              O_WRONLY|O_CREAT|OPEN_EXCL,0666,Nullfp))
730 #endif
731                 {
732                     if (ckWARN_d(WARN_INPLACE)) 
733                         Perl_warner(aTHX_ WARN_INPLACE, "Can't do inplace edit on %s: %s",
734                           PL_oldname, Strerror(errno) );
735                     do_close(gv,FALSE);
736                     continue;
737                 }
738                 setdefout(PL_argvoutgv);
739                 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
740                 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
741 #ifdef HAS_FCHMOD
742                 (void)fchmod(PL_lastfd,PL_filemode);
743 #else
744 #  if !(defined(WIN32) && defined(__BORLANDC__))
745                 /* Borland runtime creates a readonly file! */
746                 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
747 #  endif
748 #endif
749                 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
750 #ifdef HAS_FCHOWN
751                     (void)fchown(PL_lastfd,fileuid,filegid);
752 #else
753 #ifdef HAS_CHOWN
754                     (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
755 #endif
756 #endif
757                 }
758             }
759             return IoIFP(GvIOp(gv));
760         }
761         else {
762             if (ckWARN_d(WARN_INPLACE)) {
763                 int eno = errno;
764                 if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0
765                     && !S_ISREG(PL_statbuf.st_mode))    
766                 {
767                     Perl_warner(aTHX_ WARN_INPLACE,
768                                 "Can't do inplace edit: %s is not a regular file",
769                                 PL_oldname);
770                 }
771                 else
772                     Perl_warner(aTHX_ WARN_INPLACE, "Can't open %s: %s",
773                                 PL_oldname, Strerror(eno));
774             }
775         }
776     }
777     if (io && (IoFLAGS(io) & IOf_ARGV))
778         IoFLAGS(io) |= IOf_START;
779     if (PL_inplace) {
780         (void)do_close(PL_argvoutgv,FALSE);
781         if (io && (IoFLAGS(io) & IOf_ARGV)
782             && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
783         {
784             GV *oldout = (GV*)av_pop(PL_argvout_stack);
785             setdefout(oldout);
786             SvREFCNT_dec(oldout);
787             return Nullfp;
788         }
789         setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
790     }
791     return Nullfp;
792 }
793
794 #ifdef HAS_PIPE
795 void
796 Perl_do_pipe(pTHX_ SV *sv, GV *rgv, GV *wgv)
797 {
798     register IO *rstio;
799     register IO *wstio;
800     int fd[2];
801
802     if (!rgv)
803         goto badexit;
804     if (!wgv)
805         goto badexit;
806
807     rstio = GvIOn(rgv);
808     wstio = GvIOn(wgv);
809
810     if (IoIFP(rstio))
811         do_close(rgv,FALSE);
812     if (IoIFP(wstio))
813         do_close(wgv,FALSE);
814
815     if (PerlProc_pipe(fd) < 0)
816         goto badexit;
817     IoIFP(rstio) = PerlIO_fdopen(fd[0], "r");
818     IoOFP(wstio) = PerlIO_fdopen(fd[1], "w");
819     IoIFP(wstio) = IoOFP(wstio);
820     IoTYPE(rstio) = IoTYPE_RDONLY;
821     IoTYPE(wstio) = IoTYPE_WRONLY;
822     if (!IoIFP(rstio) || !IoOFP(wstio)) {
823         if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
824         else PerlLIO_close(fd[0]);
825         if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
826         else PerlLIO_close(fd[1]);
827         goto badexit;
828     }
829
830     sv_setsv(sv,&PL_sv_yes);
831     return;
832
833 badexit:
834     sv_setsv(sv,&PL_sv_undef);
835     return;
836 }
837 #endif
838
839 /* explicit renamed to avoid C++ conflict    -- kja */
840 bool
841 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
842 {
843     bool retval;
844     IO *io;
845
846     if (!gv)
847         gv = PL_argvgv;
848     if (!gv || SvTYPE(gv) != SVt_PVGV) {
849         if (not_implicit)
850             SETERRNO(EBADF,SS$_IVCHAN);
851         return FALSE;
852     }
853     io = GvIO(gv);
854     if (!io) {          /* never opened */
855         if (not_implicit) {
856             if (ckWARN(WARN_UNOPENED)) /* no check for closed here */
857                 report_evil_fh(gv, io, PL_op->op_type);
858             SETERRNO(EBADF,SS$_IVCHAN);
859         }
860         return FALSE;
861     }
862     retval = io_close(io, not_implicit);
863     if (not_implicit) {
864         IoLINES(io) = 0;
865         IoPAGE(io) = 0;
866         IoLINES_LEFT(io) = IoPAGE_LEN(io);
867     }
868     IoTYPE(io) = IoTYPE_CLOSED;
869     return retval;
870 }
871
872 bool
873 Perl_io_close(pTHX_ IO *io, bool not_implicit)
874 {
875     bool retval = FALSE;
876     int status;
877
878     if (IoIFP(io)) {
879         if (IoTYPE(io) == IoTYPE_PIPE) {
880             status = PerlProc_pclose(IoIFP(io));
881             if (not_implicit) {
882                 STATUS_NATIVE_SET(status);
883                 retval = (STATUS_POSIX == 0);
884             }
885             else {
886                 retval = (status != -1);
887             }
888         }
889         else if (IoTYPE(io) == IoTYPE_STD)
890             retval = TRUE;
891         else {
892             if (IoOFP(io) && IoOFP(io) != IoIFP(io)) {          /* a socket */
893                 retval = (PerlIO_close(IoOFP(io)) != EOF);
894                 PerlIO_close(IoIFP(io));        /* clear stdio, fd already closed */
895             }
896             else
897                 retval = (PerlIO_close(IoIFP(io)) != EOF);
898         }
899         IoOFP(io) = IoIFP(io) = Nullfp;
900     }
901     else if (not_implicit) {
902         SETERRNO(EBADF,SS$_IVCHAN);
903     }
904
905     return retval;
906 }
907
908 bool
909 Perl_do_eof(pTHX_ GV *gv)
910 {
911     register IO *io;
912     int ch;
913
914     io = GvIO(gv);
915
916     if (!io)
917         return TRUE;
918     else if (ckWARN(WARN_IO)
919              && (IoTYPE(io) == IoTYPE_WRONLY || IoIFP(io) == PerlIO_stdout()
920                  || IoIFP(io) == PerlIO_stderr()))
921     {
922         /* integrate to report_evil_fh()? */
923         char *name = NULL;
924         if (isGV(gv)) {
925             SV* sv = sv_newmortal();
926             gv_efullname4(sv, gv, Nullch, FALSE);
927             name = SvPV_nolen(sv);
928         }
929         if (name && *name)
930             Perl_warner(aTHX_ WARN_IO,
931                         "Filehandle %s opened only for output", name);
932         else
933             Perl_warner(aTHX_ WARN_IO,
934                         "Filehandle opened only for output");
935     }
936
937     while (IoIFP(io)) {
938
939         if (PerlIO_has_cntptr(IoIFP(io))) {     /* (the code works without this) */
940             if (PerlIO_get_cnt(IoIFP(io)) > 0)  /* cheat a little, since */
941                 return FALSE;                   /* this is the most usual case */
942         }
943
944         ch = PerlIO_getc(IoIFP(io));
945         if (ch != EOF) {
946             (void)PerlIO_ungetc(IoIFP(io),ch);
947             return FALSE;
948         }
949
950         if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
951             if (PerlIO_get_cnt(IoIFP(io)) < -1)
952                 PerlIO_set_cnt(IoIFP(io),-1);
953         }
954         if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
955             if (!nextargv(PL_argvgv))   /* get another fp handy */
956                 return TRUE;
957         }
958         else
959             return TRUE;                /* normal fp, definitely end of file */
960     }
961     return TRUE;
962 }
963
964 Off_t
965 Perl_do_tell(pTHX_ GV *gv)
966 {
967     register IO *io;
968     register PerlIO *fp;
969
970     if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
971 #ifdef ULTRIX_STDIO_BOTCH
972         if (PerlIO_eof(fp))
973             (void)PerlIO_seek(fp, 0L, 2);       /* ultrix 1.2 workaround */
974 #endif
975         return PerlIO_tell(fp);
976     }
977     if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
978         report_evil_fh(gv, io, PL_op->op_type);
979     SETERRNO(EBADF,RMS$_IFI);
980     return (Off_t)-1;
981 }
982
983 bool
984 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
985 {
986     register IO *io;
987     register PerlIO *fp;
988
989     if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
990 #ifdef ULTRIX_STDIO_BOTCH
991         if (PerlIO_eof(fp))
992             (void)PerlIO_seek(fp, 0L, 2);       /* ultrix 1.2 workaround */
993 #endif
994         return PerlIO_seek(fp, pos, whence) >= 0;
995     }
996     if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
997         report_evil_fh(gv, io, PL_op->op_type);
998     SETERRNO(EBADF,RMS$_IFI);
999     return FALSE;
1000 }
1001
1002 Off_t
1003 Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
1004 {
1005     register IO *io;
1006     register PerlIO *fp;
1007
1008     if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
1009         return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
1010     if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1011         report_evil_fh(gv, io, PL_op->op_type);
1012     SETERRNO(EBADF,RMS$_IFI);
1013     return (Off_t)-1;
1014 }
1015
1016 int
1017 Perl_mode_from_discipline(pTHX_ SV *discp)
1018 {
1019     int mode = O_BINARY;
1020     if (discp) {
1021         STRLEN len;
1022         char *s = SvPV(discp,len);
1023         while (*s) {
1024             if (*s == ':') {
1025                 switch (s[1]) {
1026                 case 'r':
1027                     if (len > 3 && strnEQ(s+1, "raw", 3)
1028                         && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1029                     {
1030                         mode = O_BINARY;
1031                         s += 4;
1032                         len -= 4;
1033                         break;
1034                     }
1035                     /* FALL THROUGH */
1036                 case 'c':
1037                     if (len > 4 && strnEQ(s+1, "crlf", 4)
1038                         && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1039                     {
1040                         mode = O_TEXT;
1041                         s += 5;
1042                         len -= 5;
1043                         break;
1044                     }
1045                     /* FALL THROUGH */
1046                 default:
1047                     goto fail_discipline;
1048                 }
1049             }
1050             else if (isSPACE(*s)) {
1051                 ++s;
1052                 --len;
1053             }
1054             else {
1055                 char *end;
1056 fail_discipline:
1057                 end = strchr(s+1, ':');
1058                 if (!end)
1059                     end = s+len;
1060 #ifndef PERLIO_LAYERS
1061                 Perl_croak(aTHX_ "Unknown discipline '%.*s'", end-s, s);
1062 #else
1063                 s = end;
1064 #endif
1065             }
1066         }
1067     }
1068     return mode;
1069 }
1070
1071 int
1072 Perl_do_binmode(pTHX_ PerlIO *fp, int iotype, int mode)
1073 {
1074  /* The old body of this is now in non-LAYER part of perlio.c
1075   * This is a stub for any XS code which might have been calling it.
1076   */
1077  char *name = (O_BINARY != O_TEXT && !(mode & O_BINARY)) ? ":crlf" : ":raw";
1078  return PerlIO_binmode(aTHX_ fp, iotype, mode, name);
1079 }
1080
1081 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP)
1082         /* code courtesy of William Kucharski */
1083 #define HAS_CHSIZE
1084
1085 I32 my_chsize(fd, length)
1086 I32 fd;                 /* file descriptor */
1087 Off_t length;           /* length to set file to */
1088 {
1089     struct flock fl;
1090     struct stat filebuf;
1091
1092     if (PerlLIO_fstat(fd, &filebuf) < 0)
1093         return -1;
1094
1095     if (filebuf.st_size < length) {
1096
1097         /* extend file length */
1098
1099         if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
1100             return -1;
1101
1102         /* write a "0" byte */
1103
1104         if ((PerlLIO_write(fd, "", 1)) != 1)
1105             return -1;
1106     }
1107     else {
1108         /* truncate length */
1109
1110         fl.l_whence = 0;
1111         fl.l_len = 0;
1112         fl.l_start = length;
1113         fl.l_type = F_WRLCK;    /* write lock on file space */
1114
1115         /*
1116         * This relies on the UNDOCUMENTED F_FREESP argument to
1117         * fcntl(2), which truncates the file so that it ends at the
1118         * position indicated by fl.l_start.
1119         *
1120         * Will minor miracles never cease?
1121         */
1122
1123         if (fcntl(fd, F_FREESP, &fl) < 0)
1124             return -1;
1125
1126     }
1127
1128     return 0;
1129 }
1130 #endif /* F_FREESP */
1131
1132 bool
1133 Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
1134 {
1135     register char *tmps;
1136     STRLEN len;
1137
1138     /* assuming fp is checked earlier */
1139     if (!sv)
1140         return TRUE;
1141     if (PL_ofmt) {
1142         if (SvGMAGICAL(sv))
1143             mg_get(sv);
1144         if (SvIOK(sv) && SvIVX(sv) != 0) {
1145             PerlIO_printf(fp, PL_ofmt, (NV)SvIVX(sv));
1146             return !PerlIO_error(fp);
1147         }
1148         if (  (SvNOK(sv) && SvNVX(sv) != 0.0)
1149            || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) {
1150             PerlIO_printf(fp, PL_ofmt, SvNVX(sv));
1151             return !PerlIO_error(fp);
1152         }
1153     }
1154     switch (SvTYPE(sv)) {
1155     case SVt_NULL:
1156         if (ckWARN(WARN_UNINITIALIZED))
1157             report_uninit();
1158         return TRUE;
1159     case SVt_IV:
1160         if (SvIOK(sv)) {
1161             if (SvGMAGICAL(sv))
1162                 mg_get(sv);
1163             if (SvIsUV(sv))
1164                 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
1165             else
1166                 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
1167             return !PerlIO_error(fp);
1168         }
1169         /* FALL THROUGH */
1170     default:
1171         if (PerlIO_isutf8(fp)) {
1172             if (!SvUTF8(sv))
1173                 sv_utf8_upgrade(sv = sv_mortalcopy(sv));
1174         }
1175         else if (DO_UTF8(sv))
1176             sv_utf8_downgrade((sv = sv_mortalcopy(sv)), FALSE);
1177         tmps = SvPV(sv, len);
1178         break;
1179     }
1180     /* To detect whether the process is about to overstep its
1181      * filesize limit we would need getrlimit().  We could then
1182      * also transparently raise the limit with setrlimit() --
1183      * but only until the system hard limit/the filesystem limit,
1184      * at which we would get EPERM.  Note that when using buffered
1185      * io the write failure can be delayed until the flush/close. --jhi */
1186     if (len && (PerlIO_write(fp,tmps,len) == 0))
1187         return FALSE;
1188     return !PerlIO_error(fp);
1189 }
1190
1191 I32
1192 Perl_my_stat(pTHX)
1193 {
1194     djSP;
1195     IO *io;
1196     GV* gv;
1197
1198     if (PL_op->op_flags & OPf_REF) {
1199         EXTEND(SP,1);
1200         gv = cGVOP_gv;
1201       do_fstat:
1202         io = GvIO(gv);
1203         if (io && IoIFP(io)) {
1204             PL_statgv = gv;
1205             sv_setpv(PL_statname,"");
1206             PL_laststype = OP_STAT;
1207             return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
1208         }
1209         else {
1210             if (gv == PL_defgv)
1211                 return PL_laststatval;
1212             if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1213                 report_evil_fh(gv, io, PL_op->op_type);
1214             PL_statgv = Nullgv;
1215             sv_setpv(PL_statname,"");
1216             return (PL_laststatval = -1);
1217         }
1218     }
1219     else {
1220         SV* sv = POPs;
1221         char *s;
1222         STRLEN n_a;
1223         PUTBACK;
1224         if (SvTYPE(sv) == SVt_PVGV) {
1225             gv = (GV*)sv;
1226             goto do_fstat;
1227         }
1228         else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
1229             gv = (GV*)SvRV(sv);
1230             goto do_fstat;
1231         }
1232
1233         s = SvPV(sv, n_a);
1234         PL_statgv = Nullgv;
1235         sv_setpv(PL_statname, s);
1236         PL_laststype = OP_STAT;
1237         PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1238         if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
1239             Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "stat");
1240         return PL_laststatval;
1241     }
1242 }
1243
1244 I32
1245 Perl_my_lstat(pTHX)
1246 {
1247     djSP;
1248     SV *sv;
1249     STRLEN n_a;
1250     if (PL_op->op_flags & OPf_REF) {
1251         EXTEND(SP,1);
1252         if (cGVOP_gv == PL_defgv) {
1253             if (PL_laststype != OP_LSTAT)
1254                 Perl_croak(aTHX_ "The stat preceding -l _ wasn't an lstat");
1255             return PL_laststatval;
1256         }
1257         Perl_croak(aTHX_ "You can't use -l on a filehandle");
1258     }
1259
1260     PL_laststype = OP_LSTAT;
1261     PL_statgv = Nullgv;
1262     sv = POPs;
1263     PUTBACK;
1264     sv_setpv(PL_statname,SvPV(sv, n_a));
1265     PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache);
1266     if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
1267         Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "lstat");
1268     return PL_laststatval;
1269 }
1270
1271 bool
1272 Perl_do_aexec(pTHX_ SV *really, register SV **mark, register SV **sp)
1273 {
1274     return do_aexec5(really, mark, sp, 0, 0);
1275 }
1276
1277 bool
1278 Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1279                int fd, int do_report)
1280 {
1281 #ifdef MACOS_TRADITIONAL
1282     Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1283 #else
1284     register char **a;
1285     char *tmps;
1286     STRLEN n_a;
1287
1288     if (sp > mark) {
1289         New(401,PL_Argv, sp - mark + 1, char*);
1290         a = PL_Argv;
1291         while (++mark <= sp) {
1292             if (*mark)
1293                 *a++ = SvPVx(*mark, n_a);
1294             else
1295                 *a++ = "";
1296         }
1297         *a = Nullch;
1298         if (*PL_Argv[0] != '/') /* will execvp use PATH? */
1299             TAINT_ENV();                /* testing IFS here is overkill, probably */
1300         if (really && *(tmps = SvPV(really, n_a)))
1301             PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
1302         else
1303             PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1304         if (ckWARN(WARN_EXEC))
1305             Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1306                 PL_Argv[0], Strerror(errno));
1307         if (do_report) {
1308             int e = errno;
1309
1310             PerlLIO_write(fd, (void*)&e, sizeof(int));
1311             PerlLIO_close(fd);
1312         }
1313     }
1314     do_execfree();
1315 #endif
1316     return FALSE;
1317 }
1318
1319 void
1320 Perl_do_execfree(pTHX)
1321 {
1322     if (PL_Argv) {
1323         Safefree(PL_Argv);
1324         PL_Argv = Null(char **);
1325     }
1326     if (PL_Cmd) {
1327         Safefree(PL_Cmd);
1328         PL_Cmd = Nullch;
1329     }
1330 }
1331
1332 #if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC) && !defined(MACOS_TRADITIONAL)
1333
1334 bool
1335 Perl_do_exec(pTHX_ char *cmd)
1336 {
1337     return do_exec3(cmd,0,0);
1338 }
1339
1340 bool
1341 Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report)
1342 {
1343     register char **a;
1344     register char *s;
1345     char flags[10];
1346
1347     while (*cmd && isSPACE(*cmd))
1348         cmd++;
1349
1350     /* save an extra exec if possible */
1351
1352 #ifdef CSH
1353     if (strnEQ(cmd,PL_cshname,PL_cshlen) && strnEQ(cmd+PL_cshlen," -c",3)) {
1354         strcpy(flags,"-c");
1355         s = cmd+PL_cshlen+3;
1356         if (*s == 'f') {
1357             s++;
1358             strcat(flags,"f");
1359         }
1360         if (*s == ' ')
1361             s++;
1362         if (*s++ == '\'') {
1363             char *ncmd = s;
1364
1365             while (*s)
1366                 s++;
1367             if (s[-1] == '\n')
1368                 *--s = '\0';
1369             if (s[-1] == '\'') {
1370                 *--s = '\0';
1371                 PerlProc_execl(PL_cshname,"csh", flags,ncmd,(char*)0);
1372                 *s = '\'';
1373                 return FALSE;
1374             }
1375         }
1376     }
1377 #endif /* CSH */
1378
1379     /* see if there are shell metacharacters in it */
1380
1381     if (*cmd == '.' && isSPACE(cmd[1]))
1382         goto doshell;
1383
1384     if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1385         goto doshell;
1386
1387     for (s = cmd; *s && isALNUM(*s); s++) ;     /* catch VAR=val gizmo */
1388     if (*s == '=')
1389         goto doshell;
1390
1391     for (s = cmd; *s; s++) {
1392         if (*s != ' ' && !isALPHA(*s) && strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1393             if (*s == '\n' && !s[1]) {
1394                 *s = '\0';
1395                 break;
1396             }
1397             /* handle the 2>&1 construct at the end */
1398             if (*s == '>' && s[1] == '&' && s[2] == '1'
1399                 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1400                 && (!s[3] || isSPACE(s[3])))
1401             {
1402                 char *t = s + 3;
1403
1404                 while (*t && isSPACE(*t))
1405                     ++t;
1406                 if (!*t && (dup2(1,2) != -1)) {
1407                     s[-2] = '\0';
1408                     break;
1409                 }
1410             }
1411           doshell:
1412             PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
1413             return FALSE;
1414         }
1415     }
1416
1417     New(402,PL_Argv, (s - cmd) / 2 + 2, char*);
1418     PL_Cmd = savepvn(cmd, s-cmd);
1419     a = PL_Argv;
1420     for (s = PL_Cmd; *s;) {
1421         while (*s && isSPACE(*s)) s++;
1422         if (*s)
1423             *(a++) = s;
1424         while (*s && !isSPACE(*s)) s++;
1425         if (*s)
1426             *s++ = '\0';
1427     }
1428     *a = Nullch;
1429     if (PL_Argv[0]) {
1430         PerlProc_execvp(PL_Argv[0],PL_Argv);
1431         if (errno == ENOEXEC) {         /* for system V NIH syndrome */
1432             do_execfree();
1433             goto doshell;
1434         }
1435         {
1436             int e = errno;
1437
1438             if (ckWARN(WARN_EXEC))
1439                 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1440                     PL_Argv[0], Strerror(errno));
1441             if (do_report) {
1442                 PerlLIO_write(fd, (void*)&e, sizeof(int));
1443                 PerlLIO_close(fd);
1444             }
1445         }
1446     }
1447     do_execfree();
1448     return FALSE;
1449 }
1450
1451 #endif /* OS2 || WIN32 */
1452
1453 I32
1454 Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
1455 {
1456     register I32 val;
1457     register I32 val2;
1458     register I32 tot = 0;
1459     char *what;
1460     char *s;
1461     SV **oldmark = mark;
1462     STRLEN n_a;
1463
1464 #define APPLY_TAINT_PROPER() \
1465     STMT_START {                                                        \
1466         if (PL_tainted) { TAINT_PROPER(what); }                         \
1467     } STMT_END
1468
1469     /* This is a first heuristic; it doesn't catch tainting magic. */
1470     if (PL_tainting) {
1471         while (++mark <= sp) {
1472             if (SvTAINTED(*mark)) {
1473                 TAINT;
1474                 break;
1475             }
1476         }
1477         mark = oldmark;
1478     }
1479     switch (type) {
1480     case OP_CHMOD:
1481         what = "chmod";
1482         APPLY_TAINT_PROPER();
1483         if (++mark <= sp) {
1484             val = SvIVx(*mark);
1485             APPLY_TAINT_PROPER();
1486             tot = sp - mark;
1487             while (++mark <= sp) {
1488                 char *name = SvPVx(*mark, n_a);
1489                 APPLY_TAINT_PROPER();
1490                 if (PerlLIO_chmod(name, val))
1491                     tot--;
1492             }
1493         }
1494         break;
1495 #ifdef HAS_CHOWN
1496     case OP_CHOWN:
1497         what = "chown";
1498         APPLY_TAINT_PROPER();
1499         if (sp - mark > 2) {
1500             val = SvIVx(*++mark);
1501             val2 = SvIVx(*++mark);
1502             APPLY_TAINT_PROPER();
1503             tot = sp - mark;
1504             while (++mark <= sp) {
1505                 char *name = SvPVx(*mark, n_a);
1506                 APPLY_TAINT_PROPER();
1507                 if (PerlLIO_chown(name, val, val2))
1508                     tot--;
1509             }
1510         }
1511         break;
1512 #endif
1513 /*
1514 XXX Should we make lchown() directly available from perl?
1515 For now, we'll let Configure test for HAS_LCHOWN, but do
1516 nothing in the core.
1517     --AD  5/1998
1518 */
1519 #ifdef HAS_KILL
1520     case OP_KILL:
1521         what = "kill";
1522         APPLY_TAINT_PROPER();
1523         if (mark == sp)
1524             break;
1525         s = SvPVx(*++mark, n_a);
1526         if (isUPPER(*s)) {
1527             if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1528                 s += 3;
1529             if (!(val = whichsig(s)))
1530                 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
1531         }
1532         else
1533             val = SvIVx(*mark);
1534         APPLY_TAINT_PROPER();
1535         tot = sp - mark;
1536 #ifdef VMS
1537         /* kill() doesn't do process groups (job trees?) under VMS */
1538         if (val < 0) val = -val;
1539         if (val == SIGKILL) {
1540 #           include <starlet.h>
1541             /* Use native sys$delprc() to insure that target process is
1542              * deleted; supervisor-mode images don't pay attention to
1543              * CRTL's emulation of Unix-style signals and kill()
1544              */
1545             while (++mark <= sp) {
1546                 I32 proc = SvIVx(*mark);
1547                 register unsigned long int __vmssts;
1548                 APPLY_TAINT_PROPER();
1549                 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1550                     tot--;
1551                     switch (__vmssts) {
1552                         case SS$_NONEXPR:
1553                         case SS$_NOSUCHNODE:
1554                             SETERRNO(ESRCH,__vmssts);
1555                             break;
1556                         case SS$_NOPRIV:
1557                             SETERRNO(EPERM,__vmssts);
1558                             break;
1559                         default:
1560                             SETERRNO(EVMSERR,__vmssts);
1561                     }
1562                 }
1563             }
1564             break;
1565         }
1566 #endif
1567         if (val < 0) {
1568             val = -val;
1569             while (++mark <= sp) {
1570                 I32 proc = SvIVx(*mark);
1571                 APPLY_TAINT_PROPER();
1572 #ifdef HAS_KILLPG
1573                 if (PerlProc_killpg(proc,val))  /* BSD */
1574 #else
1575                 if (PerlProc_kill(-proc,val))   /* SYSV */
1576 #endif
1577                     tot--;
1578             }
1579         }
1580         else {
1581             while (++mark <= sp) {
1582                 I32 proc = SvIVx(*mark);
1583                 APPLY_TAINT_PROPER();
1584                 if (PerlProc_kill(proc, val))
1585                     tot--;
1586             }
1587         }
1588         break;
1589 #endif
1590     case OP_UNLINK:
1591         what = "unlink";
1592         APPLY_TAINT_PROPER();
1593         tot = sp - mark;
1594         while (++mark <= sp) {
1595             s = SvPVx(*mark, n_a);
1596             APPLY_TAINT_PROPER();
1597             if (PL_euid || PL_unsafe) {
1598                 if (UNLINK(s))
1599                     tot--;
1600             }
1601             else {      /* don't let root wipe out directories without -U */
1602                 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1603                     tot--;
1604                 else {
1605                     if (UNLINK(s))
1606                         tot--;
1607                 }
1608             }
1609         }
1610         break;
1611 #ifdef HAS_UTIME
1612     case OP_UTIME:
1613         what = "utime";
1614         APPLY_TAINT_PROPER();
1615         if (sp - mark > 2) {
1616 #if defined(I_UTIME) || defined(VMS)
1617             struct utimbuf utbuf;
1618 #else
1619             struct {
1620                 Time_t  actime;
1621                 Time_t  modtime;
1622             } utbuf;
1623 #endif
1624
1625             Zero(&utbuf, sizeof utbuf, char);
1626 #ifdef BIG_TIME
1627             utbuf.actime = (Time_t)SvNVx(*++mark);      /* time accessed */
1628             utbuf.modtime = (Time_t)SvNVx(*++mark);     /* time modified */
1629 #else
1630             utbuf.actime = (Time_t)SvIVx(*++mark);      /* time accessed */
1631             utbuf.modtime = (Time_t)SvIVx(*++mark);     /* time modified */
1632 #endif
1633             APPLY_TAINT_PROPER();
1634             tot = sp - mark;
1635             while (++mark <= sp) {
1636                 char *name = SvPVx(*mark, n_a);
1637                 APPLY_TAINT_PROPER();
1638                 if (PerlLIO_utime(name, &utbuf))
1639                     tot--;
1640             }
1641         }
1642         else
1643             tot = 0;
1644         break;
1645 #endif
1646     }
1647     return tot;
1648
1649 #undef APPLY_TAINT_PROPER
1650 }
1651
1652 /* Do the permissions allow some operation?  Assumes statcache already set. */
1653 #ifndef VMS /* VMS' cando is in vms.c */
1654 bool
1655 Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register Stat_t *statbufp)
1656 /* Note: we use `effective' both for uids and gids.
1657  * Here we are betting on Uid_t being equal or wider than Gid_t.  */
1658 {
1659 #ifdef DOSISH
1660     /* [Comments and code from Len Reed]
1661      * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1662      * to write-protected files.  The execute permission bit is set
1663      * by the Miscrosoft C library stat() function for the following:
1664      *          .exe files
1665      *          .com files
1666      *          .bat files
1667      *          directories
1668      * All files and directories are readable.
1669      * Directories and special files, e.g. "CON", cannot be
1670      * write-protected.
1671      * [Comment by Tom Dinger -- a directory can have the write-protect
1672      *          bit set in the file system, but DOS permits changes to
1673      *          the directory anyway.  In addition, all bets are off
1674      *          here for networked software, such as Novell and
1675      *          Sun's PC-NFS.]
1676      */
1677
1678      /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1679       * too so it will actually look into the files for magic numbers
1680       */
1681      return (mode & statbufp->st_mode) ? TRUE : FALSE;
1682
1683 #else /* ! DOSISH */
1684     if ((effective ? PL_euid : PL_uid) == 0) {  /* root is special */
1685         if (mode == S_IXUSR) {
1686             if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1687                 return TRUE;
1688         }
1689         else
1690             return TRUE;                /* root reads and writes anything */
1691         return FALSE;
1692     }
1693     if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1694         if (statbufp->st_mode & mode)
1695             return TRUE;        /* ok as "user" */
1696     }
1697     else if (ingroup(statbufp->st_gid,effective)) {
1698         if (statbufp->st_mode & mode >> 3)
1699             return TRUE;        /* ok as "group" */
1700     }
1701     else if (statbufp->st_mode & mode >> 6)
1702         return TRUE;    /* ok as "other" */
1703     return FALSE;
1704 #endif /* ! DOSISH */
1705 }
1706 #endif /* ! VMS */
1707
1708 bool
1709 Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective)
1710 {
1711 #ifdef MACOS_TRADITIONAL
1712     /* This is simply not correct for AppleShare, but fix it yerself. */
1713     return TRUE;
1714 #else
1715     if (testgid == (effective ? PL_egid : PL_gid))
1716         return TRUE;
1717 #ifdef HAS_GETGROUPS
1718 #ifndef NGROUPS
1719 #define NGROUPS 32
1720 #endif
1721     {
1722         Groups_t gary[NGROUPS];
1723         I32 anum;
1724
1725         anum = getgroups(NGROUPS,gary);
1726         while (--anum >= 0)
1727             if (gary[anum] == testgid)
1728                 return TRUE;
1729     }
1730 #endif
1731     return FALSE;
1732 #endif
1733 }
1734
1735 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1736
1737 I32
1738 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
1739 {
1740     key_t key;
1741     I32 n, flags;
1742
1743     key = (key_t)SvNVx(*++mark);
1744     n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1745     flags = SvIVx(*++mark);
1746     SETERRNO(0,0);
1747     switch (optype)
1748     {
1749 #ifdef HAS_MSG
1750     case OP_MSGGET:
1751         return msgget(key, flags);
1752 #endif
1753 #ifdef HAS_SEM
1754     case OP_SEMGET:
1755         return semget(key, n, flags);
1756 #endif
1757 #ifdef HAS_SHM
1758     case OP_SHMGET:
1759         return shmget(key, n, flags);
1760 #endif
1761 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1762     default:
1763         Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1764 #endif
1765     }
1766     return -1;                  /* should never happen */
1767 }
1768
1769 I32
1770 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
1771 {
1772     SV *astr;
1773     char *a;
1774     I32 id, n, cmd, infosize, getinfo;
1775     I32 ret = -1;
1776
1777     id = SvIVx(*++mark);
1778     n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1779     cmd = SvIVx(*++mark);
1780     astr = *++mark;
1781     infosize = 0;
1782     getinfo = (cmd == IPC_STAT);
1783
1784     switch (optype)
1785     {
1786 #ifdef HAS_MSG
1787     case OP_MSGCTL:
1788         if (cmd == IPC_STAT || cmd == IPC_SET)
1789             infosize = sizeof(struct msqid_ds);
1790         break;
1791 #endif
1792 #ifdef HAS_SHM
1793     case OP_SHMCTL:
1794         if (cmd == IPC_STAT || cmd == IPC_SET)
1795             infosize = sizeof(struct shmid_ds);
1796         break;
1797 #endif
1798 #ifdef HAS_SEM
1799     case OP_SEMCTL:
1800 #ifdef Semctl
1801         if (cmd == IPC_STAT || cmd == IPC_SET)
1802             infosize = sizeof(struct semid_ds);
1803         else if (cmd == GETALL || cmd == SETALL)
1804         {
1805             struct semid_ds semds;
1806             union semun semun;
1807 #ifdef EXTRA_F_IN_SEMUN_BUF
1808             semun.buff = &semds;
1809 #else
1810             semun.buf = &semds;
1811 #endif
1812             getinfo = (cmd == GETALL);
1813             if (Semctl(id, 0, IPC_STAT, semun) == -1)
1814                 return -1;
1815             infosize = semds.sem_nsems * sizeof(short);
1816                 /* "short" is technically wrong but much more portable
1817                    than guessing about u_?short(_t)? */
1818         }
1819 #else
1820         Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1821 #endif
1822         break;
1823 #endif
1824 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1825     default:
1826         Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1827 #endif
1828     }
1829
1830     if (infosize)
1831     {
1832         STRLEN len;
1833         if (getinfo)
1834         {
1835             SvPV_force(astr, len);
1836             a = SvGROW(astr, infosize+1);
1837         }
1838         else
1839         {
1840             a = SvPV(astr, len);
1841             if (len != infosize)
1842                 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
1843                       PL_op_desc[optype],
1844                       (unsigned long)len,
1845                       (long)infosize);
1846         }
1847     }
1848     else
1849     {
1850         IV i = SvIV(astr);
1851         a = INT2PTR(char *,i);          /* ouch */
1852     }
1853     SETERRNO(0,0);
1854     switch (optype)
1855     {
1856 #ifdef HAS_MSG
1857     case OP_MSGCTL:
1858         ret = msgctl(id, cmd, (struct msqid_ds *)a);
1859         break;
1860 #endif
1861 #ifdef HAS_SEM
1862     case OP_SEMCTL: {
1863 #ifdef Semctl
1864             union semun unsemds;
1865
1866 #ifdef EXTRA_F_IN_SEMUN_BUF
1867             unsemds.buff = (struct semid_ds *)a;
1868 #else
1869             unsemds.buf = (struct semid_ds *)a;
1870 #endif
1871             ret = Semctl(id, n, cmd, unsemds);
1872 #else
1873             Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1874 #endif
1875         }
1876         break;
1877 #endif
1878 #ifdef HAS_SHM
1879     case OP_SHMCTL:
1880         ret = shmctl(id, cmd, (struct shmid_ds *)a);
1881         break;
1882 #endif
1883     }
1884     if (getinfo && ret >= 0) {
1885         SvCUR_set(astr, infosize);
1886         *SvEND(astr) = '\0';
1887         SvSETMAGIC(astr);
1888     }
1889     return ret;
1890 }
1891
1892 I32
1893 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
1894 {
1895 #ifdef HAS_MSG
1896     SV *mstr;
1897     char *mbuf;
1898     I32 id, msize, flags;
1899     STRLEN len;
1900
1901     id = SvIVx(*++mark);
1902     mstr = *++mark;
1903     flags = SvIVx(*++mark);
1904     mbuf = SvPV(mstr, len);
1905     if ((msize = len - sizeof(long)) < 0)
1906         Perl_croak(aTHX_ "Arg too short for msgsnd");
1907     SETERRNO(0,0);
1908     return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
1909 #else
1910     Perl_croak(aTHX_ "msgsnd not implemented");
1911 #endif
1912 }
1913
1914 I32
1915 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
1916 {
1917 #ifdef HAS_MSG
1918     SV *mstr;
1919     char *mbuf;
1920     long mtype;
1921     I32 id, msize, flags, ret;
1922     STRLEN len;
1923
1924     id = SvIVx(*++mark);
1925     mstr = *++mark;
1926     /* suppress warning when reading into undef var --jhi */
1927     if (! SvOK(mstr))
1928         sv_setpvn(mstr, "", 0);
1929     msize = SvIVx(*++mark);
1930     mtype = (long)SvIVx(*++mark);
1931     flags = SvIVx(*++mark);
1932     SvPV_force(mstr, len);
1933     mbuf = SvGROW(mstr, sizeof(long)+msize+1);
1934
1935     SETERRNO(0,0);
1936     ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
1937     if (ret >= 0) {
1938         SvCUR_set(mstr, sizeof(long)+ret);
1939         *SvEND(mstr) = '\0';
1940 #ifndef INCOMPLETE_TAINTS
1941         /* who knows who has been playing with this message? */
1942         SvTAINTED_on(mstr);
1943 #endif
1944     }
1945     return ret;
1946 #else
1947     Perl_croak(aTHX_ "msgrcv not implemented");
1948 #endif
1949 }
1950
1951 I32
1952 Perl_do_semop(pTHX_ SV **mark, SV **sp)
1953 {
1954 #ifdef HAS_SEM
1955     SV *opstr;
1956     char *opbuf;
1957     I32 id;
1958     STRLEN opsize;
1959
1960     id = SvIVx(*++mark);
1961     opstr = *++mark;
1962     opbuf = SvPV(opstr, opsize);
1963     if (opsize < sizeof(struct sembuf)
1964         || (opsize % sizeof(struct sembuf)) != 0) {
1965         SETERRNO(EINVAL,LIB$_INVARG);
1966         return -1;
1967     }
1968     SETERRNO(0,0);
1969     return semop(id, (struct sembuf *)opbuf, opsize/sizeof(struct sembuf));
1970 #else
1971     Perl_croak(aTHX_ "semop not implemented");
1972 #endif
1973 }
1974
1975 I32
1976 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
1977 {
1978 #ifdef HAS_SHM
1979     SV *mstr;
1980     char *mbuf, *shm;
1981     I32 id, mpos, msize;
1982     STRLEN len;
1983     struct shmid_ds shmds;
1984
1985     id = SvIVx(*++mark);
1986     mstr = *++mark;
1987     mpos = SvIVx(*++mark);
1988     msize = SvIVx(*++mark);
1989     SETERRNO(0,0);
1990     if (shmctl(id, IPC_STAT, &shmds) == -1)
1991         return -1;
1992     if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
1993         SETERRNO(EFAULT,SS$_ACCVIO);            /* can't do as caller requested */
1994         return -1;
1995     }
1996     shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
1997     if (shm == (char *)-1)      /* I hate System V IPC, I really do */
1998         return -1;
1999     if (optype == OP_SHMREAD) {
2000         /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2001         if (! SvOK(mstr))
2002             sv_setpvn(mstr, "", 0);
2003         SvPV_force(mstr, len);
2004         mbuf = SvGROW(mstr, msize+1);
2005
2006         Copy(shm + mpos, mbuf, msize, char);
2007         SvCUR_set(mstr, msize);
2008         *SvEND(mstr) = '\0';
2009         SvSETMAGIC(mstr);
2010 #ifndef INCOMPLETE_TAINTS
2011         /* who knows who has been playing with this shared memory? */
2012         SvTAINTED_on(mstr);
2013 #endif
2014     }
2015     else {
2016         I32 n;
2017
2018         mbuf = SvPV(mstr, len);
2019         if ((n = len) > msize)
2020             n = msize;
2021         Copy(mbuf, shm + mpos, n, char);
2022         if (n < msize)
2023             memzero(shm + mpos + n, msize - n);
2024     }
2025     return shmdt(shm);
2026 #else
2027     Perl_croak(aTHX_ "shm I/O not implemented");
2028 #endif
2029 }
2030
2031 #endif /* SYSV IPC */
2032
2033 /*
2034 =for apidoc start_glob
2035
2036 Function called by C<do_readline> to spawn a glob (or do the glob inside
2037 perl on VMS). This code used to be inline, but now perl uses C<File::Glob>
2038 this glob starter is only used by miniperl during the build proccess.
2039 Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up.
2040
2041 =cut
2042 */
2043
2044 PerlIO *
2045 Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
2046 {
2047     SV *tmpcmd = NEWSV(55, 0);
2048     PerlIO *fp;
2049     ENTER;
2050     SAVEFREESV(tmpcmd);
2051 #ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
2052            /* since spawning off a process is a real performance hit */
2053     {
2054 #include <descrip.h>
2055 #include <lib$routines.h>
2056 #include <nam.h>
2057 #include <rmsdef.h>
2058         char rslt[NAM$C_MAXRSS+1+sizeof(unsigned short int)] = {'\0','\0'};
2059         char vmsspec[NAM$C_MAXRSS+1];
2060         char *rstr = rslt + sizeof(unsigned short int), *begin, *end, *cp;
2061         char tmpfnam[L_tmpnam] = "SYS$SCRATCH:";
2062         $DESCRIPTOR(dfltdsc,"SYS$DISK:[]*.*;");
2063         PerlIO *tmpfp;
2064         STRLEN i;
2065         struct dsc$descriptor_s wilddsc
2066             = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
2067         struct dsc$descriptor_vs rsdsc
2068             = {sizeof rslt, DSC$K_DTYPE_VT, DSC$K_CLASS_VS, rslt};
2069         unsigned long int cxt = 0, sts = 0, ok = 1, hasdir = 0, hasver = 0, isunix = 0;
2070
2071         /* We could find out if there's an explicit dev/dir or version
2072            by peeking into lib$find_file's internal context at
2073            ((struct NAM *)((struct FAB *)cxt)->fab$l_nam)->nam$l_fnb
2074            but that's unsupported, so I don't want to do it now and
2075            have it bite someone in the future. */
2076         strcat(tmpfnam,PerlLIO_tmpnam(NULL));
2077         cp = SvPV(tmpglob,i);
2078         for (; i; i--) {
2079             if (cp[i] == ';') hasver = 1;
2080             if (cp[i] == '.') {
2081                 if (sts) hasver = 1;
2082                 else sts = 1;
2083             }
2084             if (cp[i] == '/') {
2085                 hasdir = isunix = 1;
2086                 break;
2087             }
2088             if (cp[i] == ']' || cp[i] == '>' || cp[i] == ':') {
2089                 hasdir = 1;
2090                 break;
2091             }
2092         }
2093         if ((tmpfp = PerlIO_open(tmpfnam,"w+","fop=dlt")) != NULL) {
2094             Stat_t st;
2095             if (!PerlLIO_stat(SvPVX(tmpglob),&st) && S_ISDIR(st.st_mode))
2096                 ok = ((wilddsc.dsc$a_pointer = tovmspath(SvPVX(tmpglob),vmsspec)) != NULL);
2097             else ok = ((wilddsc.dsc$a_pointer = tovmsspec(SvPVX(tmpglob),vmsspec)) != NULL);
2098             if (ok) wilddsc.dsc$w_length = (unsigned short int) strlen(wilddsc.dsc$a_pointer);
2099             while (ok && ((sts = lib$find_file(&wilddsc,&rsdsc,&cxt,
2100                                                &dfltdsc,NULL,NULL,NULL))&1)) {
2101                 end = rstr + (unsigned long int) *rslt;
2102                 if (!hasver) while (*end != ';') end--;
2103                 *(end++) = '\n';  *end = '\0';
2104                 for (cp = rstr; *cp; cp++) *cp = _tolower(*cp);
2105                 if (hasdir) {
2106                     if (isunix) trim_unixpath(rstr,SvPVX(tmpglob),1);
2107                     begin = rstr;
2108                 }
2109                 else {
2110                     begin = end;
2111                     while (*(--begin) != ']' && *begin != '>') ;
2112                     ++begin;
2113                 }
2114                 ok = (PerlIO_puts(tmpfp,begin) != EOF);
2115             }
2116             if (cxt) (void)lib$find_file_end(&cxt);
2117             if (ok && sts != RMS$_NMF &&
2118                 sts != RMS$_DNF && sts != RMS$_FNF) ok = 0;
2119             if (!ok) {
2120                 if (!(sts & 1)) {
2121                     SETERRNO((sts == RMS$_SYN ? EINVAL : EVMSERR),sts);
2122                 }
2123                 PerlIO_close(tmpfp);
2124                 fp = NULL;
2125             }
2126             else {
2127                 PerlIO_rewind(tmpfp);
2128                 IoTYPE(io) = IoTYPE_RDONLY;
2129                 IoIFP(io) = fp = tmpfp;
2130                 IoFLAGS(io) &= ~IOf_UNTAINT;  /* maybe redundant */
2131             }
2132         }
2133     }
2134 #else /* !VMS */
2135 #ifdef MACOS_TRADITIONAL
2136     sv_setpv(tmpcmd, "glob ");
2137     sv_catsv(tmpcmd, tmpglob);
2138     sv_catpv(tmpcmd, " |");
2139 #else
2140 #ifdef DOSISH
2141 #ifdef OS2
2142     sv_setpv(tmpcmd, "for a in ");
2143     sv_catsv(tmpcmd, tmpglob);
2144     sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
2145 #else
2146 #ifdef DJGPP
2147     sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
2148     sv_catsv(tmpcmd, tmpglob);
2149 #else
2150     sv_setpv(tmpcmd, "perlglob ");
2151     sv_catsv(tmpcmd, tmpglob);
2152     sv_catpv(tmpcmd, " |");
2153 #endif /* !DJGPP */
2154 #endif /* !OS2 */
2155 #else /* !DOSISH */
2156 #if defined(CSH)
2157     sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
2158     sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
2159     sv_catsv(tmpcmd, tmpglob);
2160     sv_catpv(tmpcmd, "' 2>/dev/null |");
2161 #else
2162     sv_setpv(tmpcmd, "echo ");
2163     sv_catsv(tmpcmd, tmpglob);
2164 #if 'z' - 'a' == 25
2165     sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
2166 #else
2167     sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
2168 #endif
2169 #endif /* !CSH */
2170 #endif /* !DOSISH */
2171 #endif /* MACOS_TRADITIONAL */
2172     (void)do_open(PL_last_in_gv, SvPVX(tmpcmd), SvCUR(tmpcmd),
2173                   FALSE, O_RDONLY, 0, Nullfp);
2174     fp = IoIFP(io);
2175 #endif /* !VMS */
2176     LEAVE;
2177     return fp;
2178 }