SYN SYN
[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             tmps = SvPVutf8(sv, len);
1173         }
1174         else {
1175             if (DO_UTF8(sv))
1176                 sv_utf8_downgrade(sv, FALSE);
1177             tmps = SvPV(sv, len);
1178         }
1179         break;
1180     }
1181     /* To detect whether the process is about to overstep its
1182      * filesize limit we would need getrlimit().  We could then
1183      * also transparently raise the limit with setrlimit() --
1184      * but only until the system hard limit/the filesystem limit,
1185      * at which we would get EPERM.  Note that when using buffered
1186      * io the write failure can be delayed until the flush/close. --jhi */
1187     if (len && (PerlIO_write(fp,tmps,len) == 0))
1188         return FALSE;
1189     return !PerlIO_error(fp);
1190 }
1191
1192 I32
1193 Perl_my_stat(pTHX)
1194 {
1195     djSP;
1196     IO *io;
1197     GV* gv;
1198
1199     if (PL_op->op_flags & OPf_REF) {
1200         EXTEND(SP,1);
1201         gv = cGVOP_gv;
1202       do_fstat:
1203         io = GvIO(gv);
1204         if (io && IoIFP(io)) {
1205             PL_statgv = gv;
1206             sv_setpv(PL_statname,"");
1207             PL_laststype = OP_STAT;
1208             return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
1209         }
1210         else {
1211             if (gv == PL_defgv)
1212                 return PL_laststatval;
1213             if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1214                 report_evil_fh(gv, io, PL_op->op_type);
1215             PL_statgv = Nullgv;
1216             sv_setpv(PL_statname,"");
1217             return (PL_laststatval = -1);
1218         }
1219     }
1220     else {
1221         SV* sv = POPs;
1222         char *s;
1223         STRLEN n_a;
1224         PUTBACK;
1225         if (SvTYPE(sv) == SVt_PVGV) {
1226             gv = (GV*)sv;
1227             goto do_fstat;
1228         }
1229         else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
1230             gv = (GV*)SvRV(sv);
1231             goto do_fstat;
1232         }
1233
1234         s = SvPV(sv, n_a);
1235         PL_statgv = Nullgv;
1236         sv_setpv(PL_statname, s);
1237         PL_laststype = OP_STAT;
1238         PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1239         if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
1240             Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "stat");
1241         return PL_laststatval;
1242     }
1243 }
1244
1245 I32
1246 Perl_my_lstat(pTHX)
1247 {
1248     djSP;
1249     SV *sv;
1250     STRLEN n_a;
1251     if (PL_op->op_flags & OPf_REF) {
1252         EXTEND(SP,1);
1253         if (cGVOP_gv == PL_defgv) {
1254             if (PL_laststype != OP_LSTAT)
1255                 Perl_croak(aTHX_ "The stat preceding -l _ wasn't an lstat");
1256             return PL_laststatval;
1257         }
1258         Perl_croak(aTHX_ "You can't use -l on a filehandle");
1259     }
1260
1261     PL_laststype = OP_LSTAT;
1262     PL_statgv = Nullgv;
1263     sv = POPs;
1264     PUTBACK;
1265     sv_setpv(PL_statname,SvPV(sv, n_a));
1266     PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache);
1267     if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
1268         Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "lstat");
1269     return PL_laststatval;
1270 }
1271
1272 bool
1273 Perl_do_aexec(pTHX_ SV *really, register SV **mark, register SV **sp)
1274 {
1275     return do_aexec5(really, mark, sp, 0, 0);
1276 }
1277
1278 bool
1279 Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1280                int fd, int do_report)
1281 {
1282 #ifdef MACOS_TRADITIONAL
1283     Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1284 #else
1285     register char **a;
1286     char *tmps;
1287     STRLEN n_a;
1288
1289     if (sp > mark) {
1290         New(401,PL_Argv, sp - mark + 1, char*);
1291         a = PL_Argv;
1292         while (++mark <= sp) {
1293             if (*mark)
1294                 *a++ = SvPVx(*mark, n_a);
1295             else
1296                 *a++ = "";
1297         }
1298         *a = Nullch;
1299         if (*PL_Argv[0] != '/') /* will execvp use PATH? */
1300             TAINT_ENV();                /* testing IFS here is overkill, probably */
1301         if (really && *(tmps = SvPV(really, n_a)))
1302             PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
1303         else
1304             PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1305         if (ckWARN(WARN_EXEC))
1306             Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1307                 PL_Argv[0], Strerror(errno));
1308         if (do_report) {
1309             int e = errno;
1310
1311             PerlLIO_write(fd, (void*)&e, sizeof(int));
1312             PerlLIO_close(fd);
1313         }
1314     }
1315     do_execfree();
1316 #endif
1317     return FALSE;
1318 }
1319
1320 void
1321 Perl_do_execfree(pTHX)
1322 {
1323     if (PL_Argv) {
1324         Safefree(PL_Argv);
1325         PL_Argv = Null(char **);
1326     }
1327     if (PL_Cmd) {
1328         Safefree(PL_Cmd);
1329         PL_Cmd = Nullch;
1330     }
1331 }
1332
1333 #if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC) && !defined(MACOS_TRADITIONAL)
1334
1335 bool
1336 Perl_do_exec(pTHX_ char *cmd)
1337 {
1338     return do_exec3(cmd,0,0);
1339 }
1340
1341 bool
1342 Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report)
1343 {
1344     register char **a;
1345     register char *s;
1346     char flags[10];
1347
1348     while (*cmd && isSPACE(*cmd))
1349         cmd++;
1350
1351     /* save an extra exec if possible */
1352
1353 #ifdef CSH
1354     if (strnEQ(cmd,PL_cshname,PL_cshlen) && strnEQ(cmd+PL_cshlen," -c",3)) {
1355         strcpy(flags,"-c");
1356         s = cmd+PL_cshlen+3;
1357         if (*s == 'f') {
1358             s++;
1359             strcat(flags,"f");
1360         }
1361         if (*s == ' ')
1362             s++;
1363         if (*s++ == '\'') {
1364             char *ncmd = s;
1365
1366             while (*s)
1367                 s++;
1368             if (s[-1] == '\n')
1369                 *--s = '\0';
1370             if (s[-1] == '\'') {
1371                 *--s = '\0';
1372                 PerlProc_execl(PL_cshname,"csh", flags,ncmd,(char*)0);
1373                 *s = '\'';
1374                 return FALSE;
1375             }
1376         }
1377     }
1378 #endif /* CSH */
1379
1380     /* see if there are shell metacharacters in it */
1381
1382     if (*cmd == '.' && isSPACE(cmd[1]))
1383         goto doshell;
1384
1385     if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1386         goto doshell;
1387
1388     for (s = cmd; *s && isALNUM(*s); s++) ;     /* catch VAR=val gizmo */
1389     if (*s == '=')
1390         goto doshell;
1391
1392     for (s = cmd; *s; s++) {
1393         if (*s != ' ' && !isALPHA(*s) && strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1394             if (*s == '\n' && !s[1]) {
1395                 *s = '\0';
1396                 break;
1397             }
1398             /* handle the 2>&1 construct at the end */
1399             if (*s == '>' && s[1] == '&' && s[2] == '1'
1400                 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1401                 && (!s[3] || isSPACE(s[3])))
1402             {
1403                 char *t = s + 3;
1404
1405                 while (*t && isSPACE(*t))
1406                     ++t;
1407                 if (!*t && (dup2(1,2) != -1)) {
1408                     s[-2] = '\0';
1409                     break;
1410                 }
1411             }
1412           doshell:
1413             PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
1414             return FALSE;
1415         }
1416     }
1417
1418     New(402,PL_Argv, (s - cmd) / 2 + 2, char*);
1419     PL_Cmd = savepvn(cmd, s-cmd);
1420     a = PL_Argv;
1421     for (s = PL_Cmd; *s;) {
1422         while (*s && isSPACE(*s)) s++;
1423         if (*s)
1424             *(a++) = s;
1425         while (*s && !isSPACE(*s)) s++;
1426         if (*s)
1427             *s++ = '\0';
1428     }
1429     *a = Nullch;
1430     if (PL_Argv[0]) {
1431         PerlProc_execvp(PL_Argv[0],PL_Argv);
1432         if (errno == ENOEXEC) {         /* for system V NIH syndrome */
1433             do_execfree();
1434             goto doshell;
1435         }
1436         {
1437             int e = errno;
1438
1439             if (ckWARN(WARN_EXEC))
1440                 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1441                     PL_Argv[0], Strerror(errno));
1442             if (do_report) {
1443                 PerlLIO_write(fd, (void*)&e, sizeof(int));
1444                 PerlLIO_close(fd);
1445             }
1446         }
1447     }
1448     do_execfree();
1449     return FALSE;
1450 }
1451
1452 #endif /* OS2 || WIN32 */
1453
1454 I32
1455 Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
1456 {
1457     register I32 val;
1458     register I32 val2;
1459     register I32 tot = 0;
1460     char *what;
1461     char *s;
1462     SV **oldmark = mark;
1463     STRLEN n_a;
1464
1465 #define APPLY_TAINT_PROPER() \
1466     STMT_START {                                                        \
1467         if (PL_tainted) { TAINT_PROPER(what); }                         \
1468     } STMT_END
1469
1470     /* This is a first heuristic; it doesn't catch tainting magic. */
1471     if (PL_tainting) {
1472         while (++mark <= sp) {
1473             if (SvTAINTED(*mark)) {
1474                 TAINT;
1475                 break;
1476             }
1477         }
1478         mark = oldmark;
1479     }
1480     switch (type) {
1481     case OP_CHMOD:
1482         what = "chmod";
1483         APPLY_TAINT_PROPER();
1484         if (++mark <= sp) {
1485             val = SvIVx(*mark);
1486             APPLY_TAINT_PROPER();
1487             tot = sp - mark;
1488             while (++mark <= sp) {
1489                 char *name = SvPVx(*mark, n_a);
1490                 APPLY_TAINT_PROPER();
1491                 if (PerlLIO_chmod(name, val))
1492                     tot--;
1493             }
1494         }
1495         break;
1496 #ifdef HAS_CHOWN
1497     case OP_CHOWN:
1498         what = "chown";
1499         APPLY_TAINT_PROPER();
1500         if (sp - mark > 2) {
1501             val = SvIVx(*++mark);
1502             val2 = SvIVx(*++mark);
1503             APPLY_TAINT_PROPER();
1504             tot = sp - mark;
1505             while (++mark <= sp) {
1506                 char *name = SvPVx(*mark, n_a);
1507                 APPLY_TAINT_PROPER();
1508                 if (PerlLIO_chown(name, val, val2))
1509                     tot--;
1510             }
1511         }
1512         break;
1513 #endif
1514 /*
1515 XXX Should we make lchown() directly available from perl?
1516 For now, we'll let Configure test for HAS_LCHOWN, but do
1517 nothing in the core.
1518     --AD  5/1998
1519 */
1520 #ifdef HAS_KILL
1521     case OP_KILL:
1522         what = "kill";
1523         APPLY_TAINT_PROPER();
1524         if (mark == sp)
1525             break;
1526         s = SvPVx(*++mark, n_a);
1527         if (isUPPER(*s)) {
1528             if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1529                 s += 3;
1530             if (!(val = whichsig(s)))
1531                 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
1532         }
1533         else
1534             val = SvIVx(*mark);
1535         APPLY_TAINT_PROPER();
1536         tot = sp - mark;
1537 #ifdef VMS
1538         /* kill() doesn't do process groups (job trees?) under VMS */
1539         if (val < 0) val = -val;
1540         if (val == SIGKILL) {
1541 #           include <starlet.h>
1542             /* Use native sys$delprc() to insure that target process is
1543              * deleted; supervisor-mode images don't pay attention to
1544              * CRTL's emulation of Unix-style signals and kill()
1545              */
1546             while (++mark <= sp) {
1547                 I32 proc = SvIVx(*mark);
1548                 register unsigned long int __vmssts;
1549                 APPLY_TAINT_PROPER();
1550                 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1551                     tot--;
1552                     switch (__vmssts) {
1553                         case SS$_NONEXPR:
1554                         case SS$_NOSUCHNODE:
1555                             SETERRNO(ESRCH,__vmssts);
1556                             break;
1557                         case SS$_NOPRIV:
1558                             SETERRNO(EPERM,__vmssts);
1559                             break;
1560                         default:
1561                             SETERRNO(EVMSERR,__vmssts);
1562                     }
1563                 }
1564             }
1565             break;
1566         }
1567 #endif
1568         if (val < 0) {
1569             val = -val;
1570             while (++mark <= sp) {
1571                 I32 proc = SvIVx(*mark);
1572                 APPLY_TAINT_PROPER();
1573 #ifdef HAS_KILLPG
1574                 if (PerlProc_killpg(proc,val))  /* BSD */
1575 #else
1576                 if (PerlProc_kill(-proc,val))   /* SYSV */
1577 #endif
1578                     tot--;
1579             }
1580         }
1581         else {
1582             while (++mark <= sp) {
1583                 I32 proc = SvIVx(*mark);
1584                 APPLY_TAINT_PROPER();
1585                 if (PerlProc_kill(proc, val))
1586                     tot--;
1587             }
1588         }
1589         break;
1590 #endif
1591     case OP_UNLINK:
1592         what = "unlink";
1593         APPLY_TAINT_PROPER();
1594         tot = sp - mark;
1595         while (++mark <= sp) {
1596             s = SvPVx(*mark, n_a);
1597             APPLY_TAINT_PROPER();
1598             if (PL_euid || PL_unsafe) {
1599                 if (UNLINK(s))
1600                     tot--;
1601             }
1602             else {      /* don't let root wipe out directories without -U */
1603                 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1604                     tot--;
1605                 else {
1606                     if (UNLINK(s))
1607                         tot--;
1608                 }
1609             }
1610         }
1611         break;
1612 #ifdef HAS_UTIME
1613     case OP_UTIME:
1614         what = "utime";
1615         APPLY_TAINT_PROPER();
1616         if (sp - mark > 2) {
1617 #if defined(I_UTIME) || defined(VMS)
1618             struct utimbuf utbuf;
1619 #else
1620             struct {
1621                 Time_t  actime;
1622                 Time_t  modtime;
1623             } utbuf;
1624 #endif
1625
1626             Zero(&utbuf, sizeof utbuf, char);
1627 #ifdef BIG_TIME
1628             utbuf.actime = (Time_t)SvNVx(*++mark);      /* time accessed */
1629             utbuf.modtime = (Time_t)SvNVx(*++mark);     /* time modified */
1630 #else
1631             utbuf.actime = (Time_t)SvIVx(*++mark);      /* time accessed */
1632             utbuf.modtime = (Time_t)SvIVx(*++mark);     /* time modified */
1633 #endif
1634             APPLY_TAINT_PROPER();
1635             tot = sp - mark;
1636             while (++mark <= sp) {
1637                 char *name = SvPVx(*mark, n_a);
1638                 APPLY_TAINT_PROPER();
1639                 if (PerlLIO_utime(name, &utbuf))
1640                     tot--;
1641             }
1642         }
1643         else
1644             tot = 0;
1645         break;
1646 #endif
1647     }
1648     return tot;
1649
1650 #undef APPLY_TAINT_PROPER
1651 }
1652
1653 /* Do the permissions allow some operation?  Assumes statcache already set. */
1654 #ifndef VMS /* VMS' cando is in vms.c */
1655 bool
1656 Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register Stat_t *statbufp)
1657 /* Note: we use `effective' both for uids and gids.
1658  * Here we are betting on Uid_t being equal or wider than Gid_t.  */
1659 {
1660 #ifdef DOSISH
1661     /* [Comments and code from Len Reed]
1662      * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1663      * to write-protected files.  The execute permission bit is set
1664      * by the Miscrosoft C library stat() function for the following:
1665      *          .exe files
1666      *          .com files
1667      *          .bat files
1668      *          directories
1669      * All files and directories are readable.
1670      * Directories and special files, e.g. "CON", cannot be
1671      * write-protected.
1672      * [Comment by Tom Dinger -- a directory can have the write-protect
1673      *          bit set in the file system, but DOS permits changes to
1674      *          the directory anyway.  In addition, all bets are off
1675      *          here for networked software, such as Novell and
1676      *          Sun's PC-NFS.]
1677      */
1678
1679      /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1680       * too so it will actually look into the files for magic numbers
1681       */
1682      return (mode & statbufp->st_mode) ? TRUE : FALSE;
1683
1684 #else /* ! DOSISH */
1685     if ((effective ? PL_euid : PL_uid) == 0) {  /* root is special */
1686         if (mode == S_IXUSR) {
1687             if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1688                 return TRUE;
1689         }
1690         else
1691             return TRUE;                /* root reads and writes anything */
1692         return FALSE;
1693     }
1694     if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1695         if (statbufp->st_mode & mode)
1696             return TRUE;        /* ok as "user" */
1697     }
1698     else if (ingroup(statbufp->st_gid,effective)) {
1699         if (statbufp->st_mode & mode >> 3)
1700             return TRUE;        /* ok as "group" */
1701     }
1702     else if (statbufp->st_mode & mode >> 6)
1703         return TRUE;    /* ok as "other" */
1704     return FALSE;
1705 #endif /* ! DOSISH */
1706 }
1707 #endif /* ! VMS */
1708
1709 bool
1710 Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective)
1711 {
1712 #ifdef MACOS_TRADITIONAL
1713     /* This is simply not correct for AppleShare, but fix it yerself. */
1714     return TRUE;
1715 #else
1716     if (testgid == (effective ? PL_egid : PL_gid))
1717         return TRUE;
1718 #ifdef HAS_GETGROUPS
1719 #ifndef NGROUPS
1720 #define NGROUPS 32
1721 #endif
1722     {
1723         Groups_t gary[NGROUPS];
1724         I32 anum;
1725
1726         anum = getgroups(NGROUPS,gary);
1727         while (--anum >= 0)
1728             if (gary[anum] == testgid)
1729                 return TRUE;
1730     }
1731 #endif
1732     return FALSE;
1733 #endif
1734 }
1735
1736 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1737
1738 I32
1739 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
1740 {
1741     key_t key;
1742     I32 n, flags;
1743
1744     key = (key_t)SvNVx(*++mark);
1745     n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1746     flags = SvIVx(*++mark);
1747     SETERRNO(0,0);
1748     switch (optype)
1749     {
1750 #ifdef HAS_MSG
1751     case OP_MSGGET:
1752         return msgget(key, flags);
1753 #endif
1754 #ifdef HAS_SEM
1755     case OP_SEMGET:
1756         return semget(key, n, flags);
1757 #endif
1758 #ifdef HAS_SHM
1759     case OP_SHMGET:
1760         return shmget(key, n, flags);
1761 #endif
1762 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1763     default:
1764         Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1765 #endif
1766     }
1767     return -1;                  /* should never happen */
1768 }
1769
1770 I32
1771 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
1772 {
1773     SV *astr;
1774     char *a;
1775     I32 id, n, cmd, infosize, getinfo;
1776     I32 ret = -1;
1777
1778     id = SvIVx(*++mark);
1779     n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1780     cmd = SvIVx(*++mark);
1781     astr = *++mark;
1782     infosize = 0;
1783     getinfo = (cmd == IPC_STAT);
1784
1785     switch (optype)
1786     {
1787 #ifdef HAS_MSG
1788     case OP_MSGCTL:
1789         if (cmd == IPC_STAT || cmd == IPC_SET)
1790             infosize = sizeof(struct msqid_ds);
1791         break;
1792 #endif
1793 #ifdef HAS_SHM
1794     case OP_SHMCTL:
1795         if (cmd == IPC_STAT || cmd == IPC_SET)
1796             infosize = sizeof(struct shmid_ds);
1797         break;
1798 #endif
1799 #ifdef HAS_SEM
1800     case OP_SEMCTL:
1801 #ifdef Semctl
1802         if (cmd == IPC_STAT || cmd == IPC_SET)
1803             infosize = sizeof(struct semid_ds);
1804         else if (cmd == GETALL || cmd == SETALL)
1805         {
1806             struct semid_ds semds;
1807             union semun semun;
1808 #ifdef EXTRA_F_IN_SEMUN_BUF
1809             semun.buff = &semds;
1810 #else
1811             semun.buf = &semds;
1812 #endif
1813             getinfo = (cmd == GETALL);
1814             if (Semctl(id, 0, IPC_STAT, semun) == -1)
1815                 return -1;
1816             infosize = semds.sem_nsems * sizeof(short);
1817                 /* "short" is technically wrong but much more portable
1818                    than guessing about u_?short(_t)? */
1819         }
1820 #else
1821         Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1822 #endif
1823         break;
1824 #endif
1825 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1826     default:
1827         Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1828 #endif
1829     }
1830
1831     if (infosize)
1832     {
1833         STRLEN len;
1834         if (getinfo)
1835         {
1836             SvPV_force(astr, len);
1837             a = SvGROW(astr, infosize+1);
1838         }
1839         else
1840         {
1841             a = SvPV(astr, len);
1842             if (len != infosize)
1843                 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
1844                       PL_op_desc[optype],
1845                       (unsigned long)len,
1846                       (long)infosize);
1847         }
1848     }
1849     else
1850     {
1851         IV i = SvIV(astr);
1852         a = INT2PTR(char *,i);          /* ouch */
1853     }
1854     SETERRNO(0,0);
1855     switch (optype)
1856     {
1857 #ifdef HAS_MSG
1858     case OP_MSGCTL:
1859         ret = msgctl(id, cmd, (struct msqid_ds *)a);
1860         break;
1861 #endif
1862 #ifdef HAS_SEM
1863     case OP_SEMCTL: {
1864 #ifdef Semctl
1865             union semun unsemds;
1866
1867 #ifdef EXTRA_F_IN_SEMUN_BUF
1868             unsemds.buff = (struct semid_ds *)a;
1869 #else
1870             unsemds.buf = (struct semid_ds *)a;
1871 #endif
1872             ret = Semctl(id, n, cmd, unsemds);
1873 #else
1874             Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1875 #endif
1876         }
1877         break;
1878 #endif
1879 #ifdef HAS_SHM
1880     case OP_SHMCTL:
1881         ret = shmctl(id, cmd, (struct shmid_ds *)a);
1882         break;
1883 #endif
1884     }
1885     if (getinfo && ret >= 0) {
1886         SvCUR_set(astr, infosize);
1887         *SvEND(astr) = '\0';
1888         SvSETMAGIC(astr);
1889     }
1890     return ret;
1891 }
1892
1893 I32
1894 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
1895 {
1896 #ifdef HAS_MSG
1897     SV *mstr;
1898     char *mbuf;
1899     I32 id, msize, flags;
1900     STRLEN len;
1901
1902     id = SvIVx(*++mark);
1903     mstr = *++mark;
1904     flags = SvIVx(*++mark);
1905     mbuf = SvPV(mstr, len);
1906     if ((msize = len - sizeof(long)) < 0)
1907         Perl_croak(aTHX_ "Arg too short for msgsnd");
1908     SETERRNO(0,0);
1909     return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
1910 #else
1911     Perl_croak(aTHX_ "msgsnd not implemented");
1912 #endif
1913 }
1914
1915 I32
1916 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
1917 {
1918 #ifdef HAS_MSG
1919     SV *mstr;
1920     char *mbuf;
1921     long mtype;
1922     I32 id, msize, flags, ret;
1923     STRLEN len;
1924
1925     id = SvIVx(*++mark);
1926     mstr = *++mark;
1927     /* suppress warning when reading into undef var --jhi */
1928     if (! SvOK(mstr))
1929         sv_setpvn(mstr, "", 0);
1930     msize = SvIVx(*++mark);
1931     mtype = (long)SvIVx(*++mark);
1932     flags = SvIVx(*++mark);
1933     SvPV_force(mstr, len);
1934     mbuf = SvGROW(mstr, sizeof(long)+msize+1);
1935
1936     SETERRNO(0,0);
1937     ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
1938     if (ret >= 0) {
1939         SvCUR_set(mstr, sizeof(long)+ret);
1940         *SvEND(mstr) = '\0';
1941 #ifndef INCOMPLETE_TAINTS
1942         /* who knows who has been playing with this message? */
1943         SvTAINTED_on(mstr);
1944 #endif
1945     }
1946     return ret;
1947 #else
1948     Perl_croak(aTHX_ "msgrcv not implemented");
1949 #endif
1950 }
1951
1952 I32
1953 Perl_do_semop(pTHX_ SV **mark, SV **sp)
1954 {
1955 #ifdef HAS_SEM
1956     SV *opstr;
1957     char *opbuf;
1958     I32 id;
1959     STRLEN opsize;
1960
1961     id = SvIVx(*++mark);
1962     opstr = *++mark;
1963     opbuf = SvPV(opstr, opsize);
1964     if (opsize < sizeof(struct sembuf)
1965         || (opsize % sizeof(struct sembuf)) != 0) {
1966         SETERRNO(EINVAL,LIB$_INVARG);
1967         return -1;
1968     }
1969     SETERRNO(0,0);
1970     return semop(id, (struct sembuf *)opbuf, opsize/sizeof(struct sembuf));
1971 #else
1972     Perl_croak(aTHX_ "semop not implemented");
1973 #endif
1974 }
1975
1976 I32
1977 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
1978 {
1979 #ifdef HAS_SHM
1980     SV *mstr;
1981     char *mbuf, *shm;
1982     I32 id, mpos, msize;
1983     STRLEN len;
1984     struct shmid_ds shmds;
1985
1986     id = SvIVx(*++mark);
1987     mstr = *++mark;
1988     mpos = SvIVx(*++mark);
1989     msize = SvIVx(*++mark);
1990     SETERRNO(0,0);
1991     if (shmctl(id, IPC_STAT, &shmds) == -1)
1992         return -1;
1993     if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
1994         SETERRNO(EFAULT,SS$_ACCVIO);            /* can't do as caller requested */
1995         return -1;
1996     }
1997     shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
1998     if (shm == (char *)-1)      /* I hate System V IPC, I really do */
1999         return -1;
2000     if (optype == OP_SHMREAD) {
2001         /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2002         if (! SvOK(mstr))
2003             sv_setpvn(mstr, "", 0);
2004         SvPV_force(mstr, len);
2005         mbuf = SvGROW(mstr, msize+1);
2006
2007         Copy(shm + mpos, mbuf, msize, char);
2008         SvCUR_set(mstr, msize);
2009         *SvEND(mstr) = '\0';
2010         SvSETMAGIC(mstr);
2011 #ifndef INCOMPLETE_TAINTS
2012         /* who knows who has been playing with this shared memory? */
2013         SvTAINTED_on(mstr);
2014 #endif
2015     }
2016     else {
2017         I32 n;
2018
2019         mbuf = SvPV(mstr, len);
2020         if ((n = len) > msize)
2021             n = msize;
2022         Copy(mbuf, shm + mpos, n, char);
2023         if (n < msize)
2024             memzero(shm + mpos + n, msize - n);
2025     }
2026     return shmdt(shm);
2027 #else
2028     Perl_croak(aTHX_ "shm I/O not implemented");
2029 #endif
2030 }
2031
2032 #endif /* SYSV IPC */
2033
2034 /*
2035 =for apidoc start_glob
2036
2037 Function called by C<do_readline> to spawn a glob (or do the glob inside
2038 perl on VMS). This code used to be inline, but now perl uses C<File::Glob>
2039 this glob starter is only used by miniperl during the build proccess.
2040 Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up.
2041
2042 =cut
2043 */
2044
2045 PerlIO *
2046 Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
2047 {
2048     SV *tmpcmd = NEWSV(55, 0);
2049     PerlIO *fp;
2050     ENTER;
2051     SAVEFREESV(tmpcmd);
2052 #ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
2053            /* since spawning off a process is a real performance hit */
2054     {
2055 #include <descrip.h>
2056 #include <lib$routines.h>
2057 #include <nam.h>
2058 #include <rmsdef.h>
2059         char rslt[NAM$C_MAXRSS+1+sizeof(unsigned short int)] = {'\0','\0'};
2060         char vmsspec[NAM$C_MAXRSS+1];
2061         char *rstr = rslt + sizeof(unsigned short int), *begin, *end, *cp;
2062         char tmpfnam[L_tmpnam] = "SYS$SCRATCH:";
2063         $DESCRIPTOR(dfltdsc,"SYS$DISK:[]*.*;");
2064         PerlIO *tmpfp;
2065         STRLEN i;
2066         struct dsc$descriptor_s wilddsc
2067             = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
2068         struct dsc$descriptor_vs rsdsc
2069             = {sizeof rslt, DSC$K_DTYPE_VT, DSC$K_CLASS_VS, rslt};
2070         unsigned long int cxt = 0, sts = 0, ok = 1, hasdir = 0, hasver = 0, isunix = 0;
2071
2072         /* We could find out if there's an explicit dev/dir or version
2073            by peeking into lib$find_file's internal context at
2074            ((struct NAM *)((struct FAB *)cxt)->fab$l_nam)->nam$l_fnb
2075            but that's unsupported, so I don't want to do it now and
2076            have it bite someone in the future. */
2077         strcat(tmpfnam,PerlLIO_tmpnam(NULL));
2078         cp = SvPV(tmpglob,i);
2079         for (; i; i--) {
2080             if (cp[i] == ';') hasver = 1;
2081             if (cp[i] == '.') {
2082                 if (sts) hasver = 1;
2083                 else sts = 1;
2084             }
2085             if (cp[i] == '/') {
2086                 hasdir = isunix = 1;
2087                 break;
2088             }
2089             if (cp[i] == ']' || cp[i] == '>' || cp[i] == ':') {
2090                 hasdir = 1;
2091                 break;
2092             }
2093         }
2094         if ((tmpfp = PerlIO_open(tmpfnam,"w+","fop=dlt")) != NULL) {
2095             Stat_t st;
2096             if (!PerlLIO_stat(SvPVX(tmpglob),&st) && S_ISDIR(st.st_mode))
2097                 ok = ((wilddsc.dsc$a_pointer = tovmspath(SvPVX(tmpglob),vmsspec)) != NULL);
2098             else ok = ((wilddsc.dsc$a_pointer = tovmsspec(SvPVX(tmpglob),vmsspec)) != NULL);
2099             if (ok) wilddsc.dsc$w_length = (unsigned short int) strlen(wilddsc.dsc$a_pointer);
2100             while (ok && ((sts = lib$find_file(&wilddsc,&rsdsc,&cxt,
2101                                                &dfltdsc,NULL,NULL,NULL))&1)) {
2102                 end = rstr + (unsigned long int) *rslt;
2103                 if (!hasver) while (*end != ';') end--;
2104                 *(end++) = '\n';  *end = '\0';
2105                 for (cp = rstr; *cp; cp++) *cp = _tolower(*cp);
2106                 if (hasdir) {
2107                     if (isunix) trim_unixpath(rstr,SvPVX(tmpglob),1);
2108                     begin = rstr;
2109                 }
2110                 else {
2111                     begin = end;
2112                     while (*(--begin) != ']' && *begin != '>') ;
2113                     ++begin;
2114                 }
2115                 ok = (PerlIO_puts(tmpfp,begin) != EOF);
2116             }
2117             if (cxt) (void)lib$find_file_end(&cxt);
2118             if (ok && sts != RMS$_NMF &&
2119                 sts != RMS$_DNF && sts != RMS$_FNF) ok = 0;
2120             if (!ok) {
2121                 if (!(sts & 1)) {
2122                     SETERRNO((sts == RMS$_SYN ? EINVAL : EVMSERR),sts);
2123                 }
2124                 PerlIO_close(tmpfp);
2125                 fp = NULL;
2126             }
2127             else {
2128                 PerlIO_rewind(tmpfp);
2129                 IoTYPE(io) = IoTYPE_RDONLY;
2130                 IoIFP(io) = fp = tmpfp;
2131                 IoFLAGS(io) &= ~IOf_UNTAINT;  /* maybe redundant */
2132             }
2133         }
2134     }
2135 #else /* !VMS */
2136 #ifdef MACOS_TRADITIONAL
2137     sv_setpv(tmpcmd, "glob ");
2138     sv_catsv(tmpcmd, tmpglob);
2139     sv_catpv(tmpcmd, " |");
2140 #else
2141 #ifdef DOSISH
2142 #ifdef OS2
2143     sv_setpv(tmpcmd, "for a in ");
2144     sv_catsv(tmpcmd, tmpglob);
2145     sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
2146 #else
2147 #ifdef DJGPP
2148     sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
2149     sv_catsv(tmpcmd, tmpglob);
2150 #else
2151     sv_setpv(tmpcmd, "perlglob ");
2152     sv_catsv(tmpcmd, tmpglob);
2153     sv_catpv(tmpcmd, " |");
2154 #endif /* !DJGPP */
2155 #endif /* !OS2 */
2156 #else /* !DOSISH */
2157 #if defined(CSH)
2158     sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
2159     sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
2160     sv_catsv(tmpcmd, tmpglob);
2161     sv_catpv(tmpcmd, "' 2>/dev/null |");
2162 #else
2163     sv_setpv(tmpcmd, "echo ");
2164     sv_catsv(tmpcmd, tmpglob);
2165 #if 'z' - 'a' == 25
2166     sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
2167 #else
2168     sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
2169 #endif
2170 #endif /* !CSH */
2171 #endif /* !DOSISH */
2172 #endif /* MACOS_TRADITIONAL */
2173     (void)do_open(PL_last_in_gv, SvPVX(tmpcmd), SvCUR(tmpcmd),
2174                   FALSE, O_RDONLY, 0, Nullfp);
2175     fp = IoIFP(io);
2176 #endif /* !VMS */
2177     LEAVE;
2178     return fp;
2179 }