3 * Copyright (c) 1991-2001, Larry Wall
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.
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."
18 #define PERL_IN_DOIO_C
21 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
30 # ifndef HAS_SHMAT_PROTOTYPE
31 extern Shmat_t shmat (int, char *, int);
37 # if defined(_MSC_VER) || defined(__MINGW32__)
38 # include <sys/utime.h>
45 # define OPEN_EXCL O_EXCL
50 #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
55 Perl_do_open(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
56 int rawmode, int rawperm, PerlIO *supplied_fp)
58 return do_openn(gv, name, len, as_raw, rawmode, rawperm,
59 supplied_fp, (SV **) NULL, 0);
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,
67 return do_openn(gv, name, len, as_raw, rawmode, rawperm,
68 supplied_fp, &svs, 1);
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,
76 register IO *io = GvIOn(gv);
77 PerlIO *saveifp = Nullfp;
78 PerlIO *saveofp = Nullfp;
79 char savetype = IoTYPE_CLOSED;
84 bool was_fdopen = FALSE;
85 bool in_raw = 0, in_crlf = 0, out_raw = 0, out_crlf = 0;
87 char mode[4]; /* stdio file mode ("r\0", "rb\0", "r+b\0" etc.) */
88 SV *svs = (num_svs) ? *svp : Nullsv;
90 Zero(mode,sizeof(mode),char);
91 PL_forkprocess = 1; /* assume true if no fork */
93 /* Collect default raw/crlf info from the op */
94 if (PL_op && PL_op->op_type == OP_OPEN) {
95 /* set up disciplines */
96 U8 flags = PL_op->op_private;
97 in_raw = (flags & OPpOPEN_IN_RAW);
98 in_crlf = (flags & OPpOPEN_IN_CRLF);
99 out_raw = (flags & OPpOPEN_OUT_RAW);
100 out_crlf = (flags & OPpOPEN_OUT_CRLF);
103 /* If currently open - close before we re-open */
105 fd = PerlIO_fileno(IoIFP(io));
106 if (IoTYPE(io) == IoTYPE_STD)
108 else if (fd <= PL_maxsysfd) {
111 savetype = IoTYPE(io);
114 else if (IoTYPE(io) == IoTYPE_PIPE)
115 result = PerlProc_pclose(IoIFP(io));
116 else if (IoIFP(io) != IoOFP(io)) {
118 result = PerlIO_close(IoOFP(io));
119 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
122 result = PerlIO_close(IoIFP(io));
125 result = PerlIO_close(IoIFP(io));
126 if (result == EOF && fd > PL_maxsysfd)
127 PerlIO_printf(Perl_error_log,
128 "Warning: unable to close filehandle %s properly.\n",
130 IoOFP(io) = IoIFP(io) = Nullfp;
134 /* sysopen style args, i.e. integer mode and permissions */
136 Perl_croak(aTHX_ "panic:sysopen with multiple args");
139 #if defined(USE_64_BIT_RAWIO) && defined(O_LARGEFILE)
140 rawmode |= O_LARGEFILE;
144 #define O_ACCMODE 3 /* Assume traditional implementation */
147 switch (result = rawmode & O_ACCMODE) {
149 IoTYPE(io) = IoTYPE_RDONLY;
152 IoTYPE(io) = IoTYPE_WRONLY;
156 IoTYPE(io) = IoTYPE_RDWR;
160 writing = (result > 0);
161 fd = PerlLIO_open3(name, rawmode, rawperm);
167 if (result == O_RDONLY) {
171 else if (rawmode & O_APPEND) {
173 if (result != O_WRONLY)
178 if (result == O_WRONLY)
185 if (rawmode & O_BINARY)
188 fp = PerlIO_fdopen(fd, mode);
194 /* Regular (non-sys) open */
200 type = savepvn(name, len);
203 /* Loose trailing white space */
204 while (tend > type && isSPACE(tend[-1]))
207 /* New style explict name, type is just mode and discipline/layer info */
209 name = SvPV(svs, l) ;
211 name = savepvn(name, len);
214 for (; isSPACE(*type); type++) ;
221 if ((*type == IoTYPE_RDWR) && ((!num_svs || tend > type+1 && tend[-1] != IoTYPE_PIPE))) { /* scary */
226 if (*type == IoTYPE_PIPE) {
228 if (type[1] != IoTYPE_STD) {
230 Perl_croak(aTHX_ "Unknown open() mode '%.*s'", (int)olen, oname);
235 for (type++; isSPACE(*type); type++) ;
241 /* command is missing 19990114 */
242 if (ckWARN(WARN_PIPE))
243 Perl_warner(aTHX_ WARN_PIPE, "Missing command in piped open");
247 if (strNE(name,"-") || num_svs)
249 TAINT_PROPER("piped open");
250 if (!num_svs && name[len-1] == '|') {
252 if (ckWARN(WARN_PIPE))
253 Perl_warner(aTHX_ WARN_PIPE, "Can't open bidirectional pipe");
262 fp = PerlProc_popen_list(mode, num_svs, svp);
265 fp = PerlProc_popen(name,mode);
268 else if (*type == IoTYPE_WRONLY) {
269 TAINT_PROPER("open");
271 if (*type == IoTYPE_WRONLY) {
272 /* Two IoTYPE_WRONLYs in a row make for an IoTYPE_APPEND. */
273 mode[0] = IoTYPE(io) = IoTYPE_APPEND;
293 if (!num_svs && !*type && supplied_fp)
294 /* "<+&" etc. is used by typemaps */
298 Perl_croak(aTHX_ "More than one argument to '%c&' open",IoTYPE(io));
300 if (num_svs && SvIOK(*svp))
302 else if (isDIGIT(*type)) {
304 for (; isSPACE(*type); type++) ;
310 thatio = sv_2io(*svp);
315 for (; isSPACE(*type); type++) ;
316 thatgv = gv_fetchpv(type,FALSE,SVt_PVIO);
317 thatio = GvIO(thatgv);
321 SETERRNO(EINVAL,SS$_IVCHAN);
326 PerlIO *fp = IoIFP(thatio);
327 /* Flush stdio buffer before dup. --mjd
328 * Unfortunately SEEK_CURing 0 seems to
329 * be optimized away on most platforms;
330 * only Solaris and Linux seem to flush
333 /* sfio fails to clear error on next
334 sfwrite, contrary to documentation.
336 if (PerlIO_seek(fp, 0, SEEK_CUR) == -1)
339 /* On the other hand, do all platforms
340 * take gracefully to flushing a read-only
341 * filehandle? Perhaps we should do
342 * fsetpos(src)+fgetpos(dst)? --nik */
344 fd = PerlIO_fileno(fp);
345 /* When dup()ing STDIN, STDOUT or STDERR
346 * explicitly set appropriate access mode */
347 if (IoIFP(thatio) == PerlIO_stdout()
348 || IoIFP(thatio) == PerlIO_stderr())
349 IoTYPE(io) = IoTYPE_WRONLY;
350 else if (IoIFP(thatio) == PerlIO_stdin())
351 IoTYPE(io) = IoTYPE_RDONLY;
352 /* When dup()ing a socket, say result is
354 else if (IoTYPE(thatio) == IoTYPE_SOCKET)
355 IoTYPE(io) = IoTYPE_SOCKET;
361 fd = PerlLIO_dup(fd);
364 if (!(fp = PerlIO_fdopen(fd,mode))) {
372 Perl_croak(aTHX_ "More than one argument to '>' open");
375 for (; isSPACE(*type); type++) ;
376 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
379 fp = PerlIO_stdout();
380 IoTYPE(io) = IoTYPE_STD;
383 fp = PerlIO_open((num_svs ? name : type), mode);
387 else if (*type == IoTYPE_RDONLY) {
389 Perl_croak(aTHX_ "More than one argument to '<' open");
392 for (type++; isSPACE(*type); type++) ;
402 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
406 IoTYPE(io) = IoTYPE_STD;
409 fp = PerlIO_open((num_svs ? name : type), mode);
411 else if ((num_svs && type[0] == IoTYPE_STD && type[1] == IoTYPE_PIPE) ||
412 (!num_svs && tend > type+1 && tend[-1] == IoTYPE_PIPE)) {
414 type += 2; /* skip over '-|' */
418 while (tend > type && isSPACE(tend[-1]))
421 for (; isSPACE(*type); type++) ;
426 /* command is missing 19990114 */
427 if (ckWARN(WARN_PIPE))
428 Perl_warner(aTHX_ WARN_PIPE, "Missing command in piped open");
432 if (strNE(name,"-") || num_svs)
434 TAINT_PROPER("piped open");
441 fp = PerlProc_popen_list(mode,num_svs,svp);
444 fp = PerlProc_popen(name,mode);
446 IoTYPE(io) = IoTYPE_PIPE;
452 IoTYPE(io) = IoTYPE_RDONLY;
454 for (; isSPACE(*name); name++) ;
460 if (strEQ(name,"-")) {
462 IoTYPE(io) = IoTYPE_STD;
465 fp = PerlIO_open(name,mode);
470 if (ckWARN(WARN_NEWLINE) && IoTYPE(io) == IoTYPE_RDONLY && strchr(name, '\n'))
471 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "open");
475 if (ckWARN(WARN_IO)) {
476 if ((IoTYPE(io) == IoTYPE_RDONLY) &&
477 (fp == PerlIO_stdout() || fp == PerlIO_stderr())) {
478 Perl_warner(aTHX_ WARN_IO, "'std%s' opened only for input",
479 (fp == PerlIO_stdout()) ? "out" : "err");
481 else if ((IoTYPE(io) == IoTYPE_WRONLY) && fp == PerlIO_stdout()) {
482 Perl_warner(aTHX_ WARN_IO, "'stdin' opened only for output");
486 if (IoTYPE(io) && IoTYPE(io) != IoTYPE_PIPE && IoTYPE(io) != IoTYPE_STD) {
487 if (PerlLIO_fstat(PerlIO_fileno(fp),&PL_statbuf) < 0) {
488 (void)PerlIO_close(fp);
491 if (S_ISSOCK(PL_statbuf.st_mode))
492 IoTYPE(io) = IoTYPE_SOCKET; /* in case a socket was passed in to us */
496 !(PL_statbuf.st_mode & S_IFMT)
500 && IoTYPE(io) != IoTYPE_WRONLY /* Dups of STD* filehandles already have */
501 && IoTYPE(io) != IoTYPE_RDONLY /* type so they aren't marked as sockets */
502 ) { /* on OS's that return 0 on fstat()ed pipe */
504 Sock_size_t buflen = sizeof tmpbuf;
505 if (PerlSock_getsockname(PerlIO_fileno(fp), (struct sockaddr *)tmpbuf,
507 || errno != ENOTSOCK)
508 IoTYPE(io) = IoTYPE_SOCKET; /* some OS's return 0 on fstat()ed socket */
509 /* but some return 0 for streams too, sigh */
513 if (saveifp) { /* must use old fp? */
514 /* If fd is less that PL_maxsysfd i.e. STDIN..STDERR
515 then dup the new fileno down
517 fd = PerlIO_fileno(saveifp);
519 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
520 if (saveofp != saveifp) { /* was a socket? */
521 PerlIO_close(saveofp);
522 /* This looks very suspect - NI-S 24 Nov 2000 */
524 Safefree(saveofp); /* ??? */
527 if (fd != PerlIO_fileno(fp)) {
531 PerlLIO_dup2(PerlIO_fileno(fp), fd);
533 if (fd != PerlIO_fileno(PerlIO_stdin())) {
534 char newname[FILENAME_MAX+1];
535 if (fgetname(fp, newname)) {
536 if (fd == PerlIO_fileno(PerlIO_stdout())) Perl_vmssetuserlnm("SYS$OUTPUT", newname);
537 if (fd == PerlIO_fileno(PerlIO_stderr())) Perl_vmssetuserlnm("SYS$ERROR", newname);
542 sv = *av_fetch(PL_fdpid,PerlIO_fileno(fp),TRUE);
543 (void)SvUPGRADE(sv, SVt_IV);
546 sv = *av_fetch(PL_fdpid,fd,TRUE);
548 (void)SvUPGRADE(sv, SVt_IV);
557 #if defined(HAS_FCNTL) && defined(F_SETFD)
559 int save_errno = errno;
560 fd = PerlIO_fileno(fp);
561 fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
567 /* Need to supply default type info from open.pm */
568 SV *layers = PL_curcop->cop_io;
572 type = SvPV(layers,len);
573 if (type && mode[0] != 'r') {
574 /* Skip to write part */
575 char *s = strchr(type,0);
576 if (s && (s-type) < len) {
583 while (isSPACE(*type)) type++;
586 if (PerlIO_apply_layers(aTHX_ IoIFP(io),mode,type) != 0) {
592 IoFLAGS(io) &= ~IOf_NOLINE;
594 if (IoTYPE(io) == IoTYPE_SOCKET
595 || (IoTYPE(io) == IoTYPE_WRONLY && S_ISCHR(PL_statbuf.st_mode)) )
598 if (!(IoOFP(io) = PerlIO_fdopen(PerlIO_fileno(fp),mode))) {
604 if (PerlIO_apply_layers(aTHX_ IoOFP(io),mode,type) != 0) {
605 PerlIO_close(IoOFP(io));
621 IoTYPE(io) = savetype;
626 Perl_nextargv(pTHX_ register GV *gv)
629 #ifndef FLEXFILENAMES
638 PL_argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO);
639 if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
640 IoFLAGS(io) &= ~IOf_START;
642 if (!PL_argvout_stack)
643 PL_argvout_stack = newAV();
644 av_push(PL_argvout_stack, SvREFCNT_inc(PL_defoutgv));
647 if (PL_filemode & (S_ISUID|S_ISGID)) {
648 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
650 (void)fchmod(PL_lastfd,PL_filemode);
652 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
656 while (av_len(GvAV(gv)) >= 0) {
658 sv = av_shift(GvAV(gv));
660 sv_setsv(GvSV(gv),sv);
661 SvSETMAGIC(GvSV(gv));
662 PL_oldname = SvPVx(GvSV(gv), oldlen);
663 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,Nullfp)) {
665 TAINT_PROPER("inplace open");
666 if (oldlen == 1 && *PL_oldname == '-') {
667 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
668 return IoIFP(GvIOp(gv));
670 #ifndef FLEXFILENAMES
671 filedev = PL_statbuf.st_dev;
672 fileino = PL_statbuf.st_ino;
674 PL_filemode = PL_statbuf.st_mode;
675 fileuid = PL_statbuf.st_uid;
676 filegid = PL_statbuf.st_gid;
677 if (!S_ISREG(PL_filemode)) {
678 if (ckWARN_d(WARN_INPLACE))
679 Perl_warner(aTHX_ WARN_INPLACE,
680 "Can't do inplace edit: %s is not a regular file",
686 char *star = strchr(PL_inplace, '*');
688 char *begin = PL_inplace;
689 sv_setpvn(sv, "", 0);
691 sv_catpvn(sv, begin, star - begin);
692 sv_catpvn(sv, PL_oldname, oldlen);
694 } while ((star = strchr(begin, '*')));
699 sv_catpv(sv,PL_inplace);
701 #ifndef FLEXFILENAMES
702 if (PerlLIO_stat(SvPVX(sv),&PL_statbuf) >= 0
703 && PL_statbuf.st_dev == filedev
704 && PL_statbuf.st_ino == fileino
706 || (_djstat_fail_bits & _STFAIL_TRUENAME)!=0
710 if (ckWARN_d(WARN_INPLACE))
711 Perl_warner(aTHX_ WARN_INPLACE,
712 "Can't do inplace edit: %s would not be unique",
719 #if !defined(DOSISH) && !defined(__CYGWIN__)
720 if (PerlLIO_rename(PL_oldname,SvPVX(sv)) < 0) {
721 if (ckWARN_d(WARN_INPLACE))
722 Perl_warner(aTHX_ WARN_INPLACE,
723 "Can't rename %s to %s: %s, skipping file",
724 PL_oldname, SvPVX(sv), Strerror(errno) );
730 (void)PerlLIO_unlink(SvPVX(sv));
731 (void)PerlLIO_rename(PL_oldname,SvPVX(sv));
732 do_open(gv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,O_RDONLY,0,Nullfp);
735 (void)UNLINK(SvPVX(sv));
736 if (link(PL_oldname,SvPVX(sv)) < 0) {
737 if (ckWARN_d(WARN_INPLACE))
738 Perl_warner(aTHX_ WARN_INPLACE,
739 "Can't rename %s to %s: %s, skipping file",
740 PL_oldname, SvPVX(sv), Strerror(errno) );
744 (void)UNLINK(PL_oldname);
748 #if !defined(DOSISH) && !defined(AMIGAOS)
749 # ifndef VMS /* Don't delete; use automatic file versioning */
750 if (UNLINK(PL_oldname) < 0) {
751 if (ckWARN_d(WARN_INPLACE))
752 Perl_warner(aTHX_ WARN_INPLACE,
753 "Can't remove %s: %s, skipping file",
754 PL_oldname, Strerror(errno) );
760 Perl_croak(aTHX_ "Can't do inplace edit without backup");
764 sv_setpvn(sv,">",!PL_inplace);
765 sv_catpvn(sv,PL_oldname,oldlen);
766 SETERRNO(0,0); /* in case sprintf set errno */
768 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
769 O_WRONLY|O_CREAT|O_TRUNC,0,Nullfp))
771 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
772 O_WRONLY|O_CREAT|OPEN_EXCL,0666,Nullfp))
775 if (ckWARN_d(WARN_INPLACE))
776 Perl_warner(aTHX_ WARN_INPLACE, "Can't do inplace edit on %s: %s",
777 PL_oldname, Strerror(errno) );
781 setdefout(PL_argvoutgv);
782 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
783 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
785 (void)fchmod(PL_lastfd,PL_filemode);
787 # if !(defined(WIN32) && defined(__BORLANDC__))
788 /* Borland runtime creates a readonly file! */
789 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
792 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
794 (void)fchown(PL_lastfd,fileuid,filegid);
797 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
802 return IoIFP(GvIOp(gv));
805 if (ckWARN_d(WARN_INPLACE)) {
807 if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0
808 && !S_ISREG(PL_statbuf.st_mode))
810 Perl_warner(aTHX_ WARN_INPLACE,
811 "Can't do inplace edit: %s is not a regular file",
815 Perl_warner(aTHX_ WARN_INPLACE, "Can't open %s: %s",
816 PL_oldname, Strerror(eno));
820 if (io && (IoFLAGS(io) & IOf_ARGV))
821 IoFLAGS(io) |= IOf_START;
823 (void)do_close(PL_argvoutgv,FALSE);
824 if (io && (IoFLAGS(io) & IOf_ARGV)
825 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
827 GV *oldout = (GV*)av_pop(PL_argvout_stack);
829 SvREFCNT_dec(oldout);
832 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
839 Perl_do_pipe(pTHX_ SV *sv, GV *rgv, GV *wgv)
858 if (PerlProc_pipe(fd) < 0)
860 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r");
861 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w");
862 IoIFP(wstio) = IoOFP(wstio);
863 IoTYPE(rstio) = IoTYPE_RDONLY;
864 IoTYPE(wstio) = IoTYPE_WRONLY;
865 if (!IoIFP(rstio) || !IoOFP(wstio)) {
866 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
867 else PerlLIO_close(fd[0]);
868 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
869 else PerlLIO_close(fd[1]);
873 sv_setsv(sv,&PL_sv_yes);
877 sv_setsv(sv,&PL_sv_undef);
882 /* explicit renamed to avoid C++ conflict -- kja */
884 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
891 if (!gv || SvTYPE(gv) != SVt_PVGV) {
893 SETERRNO(EBADF,SS$_IVCHAN);
897 if (!io) { /* never opened */
899 if (ckWARN(WARN_UNOPENED)) /* no check for closed here */
900 report_evil_fh(gv, io, PL_op->op_type);
901 SETERRNO(EBADF,SS$_IVCHAN);
905 retval = io_close(io, not_implicit);
909 IoLINES_LEFT(io) = IoPAGE_LEN(io);
911 IoTYPE(io) = IoTYPE_CLOSED;
916 Perl_io_close(pTHX_ IO *io, bool not_implicit)
922 if (IoTYPE(io) == IoTYPE_PIPE) {
923 status = PerlProc_pclose(IoIFP(io));
925 STATUS_NATIVE_SET(status);
926 retval = (STATUS_POSIX == 0);
929 retval = (status != -1);
932 else if (IoTYPE(io) == IoTYPE_STD)
935 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
936 retval = (PerlIO_close(IoOFP(io)) != EOF);
937 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
940 retval = (PerlIO_close(IoIFP(io)) != EOF);
942 IoOFP(io) = IoIFP(io) = Nullfp;
944 else if (not_implicit) {
945 SETERRNO(EBADF,SS$_IVCHAN);
952 Perl_do_eof(pTHX_ GV *gv)
961 else if (ckWARN(WARN_IO) && (IoTYPE(io) == IoTYPE_WRONLY))
963 /* integrate to report_evil_fh()? */
966 SV* sv = sv_newmortal();
967 gv_efullname4(sv, gv, Nullch, FALSE);
968 name = SvPV_nolen(sv);
971 Perl_warner(aTHX_ WARN_IO,
972 "Filehandle %s opened only for output", name);
974 Perl_warner(aTHX_ WARN_IO,
975 "Filehandle opened only for output");
980 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
981 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
982 return FALSE; /* this is the most usual case */
985 ch = PerlIO_getc(IoIFP(io));
987 (void)PerlIO_ungetc(IoIFP(io),ch);
991 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
992 if (PerlIO_get_cnt(IoIFP(io)) < -1)
993 PerlIO_set_cnt(IoIFP(io),-1);
995 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
996 if (!nextargv(PL_argvgv)) /* get another fp handy */
1000 return TRUE; /* normal fp, definitely end of file */
1006 Perl_do_tell(pTHX_ GV *gv)
1009 register PerlIO *fp;
1011 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1012 #ifdef ULTRIX_STDIO_BOTCH
1014 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1016 return PerlIO_tell(fp);
1018 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1019 report_evil_fh(gv, io, PL_op->op_type);
1020 SETERRNO(EBADF,RMS$_IFI);
1025 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
1028 register PerlIO *fp;
1030 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1031 #ifdef ULTRIX_STDIO_BOTCH
1033 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1035 return PerlIO_seek(fp, pos, whence) >= 0;
1037 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1038 report_evil_fh(gv, io, PL_op->op_type);
1039 SETERRNO(EBADF,RMS$_IFI);
1044 Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
1047 register PerlIO *fp;
1049 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
1050 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
1051 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1052 report_evil_fh(gv, io, PL_op->op_type);
1053 SETERRNO(EBADF,RMS$_IFI);
1058 Perl_mode_from_discipline(pTHX_ SV *discp)
1060 int mode = O_BINARY;
1063 char *s = SvPV(discp,len);
1068 if (len > 3 && strnEQ(s+1, "raw", 3)
1069 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1078 if (len > 4 && strnEQ(s+1, "crlf", 4)
1079 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1088 goto fail_discipline;
1091 else if (isSPACE(*s)) {
1098 end = strchr(s+1, ':');
1101 #ifndef PERLIO_LAYERS
1102 Perl_croak(aTHX_ "Unknown discipline '%.*s'", end-s, s);
1113 Perl_do_binmode(pTHX_ PerlIO *fp, int iotype, int mode)
1115 /* The old body of this is now in non-LAYER part of perlio.c
1116 * This is a stub for any XS code which might have been calling it.
1118 char *name = (O_BINARY != O_TEXT && !(mode & O_BINARY)) ? ":crlf" : ":raw";
1119 return PerlIO_binmode(aTHX_ fp, iotype, mode, name);
1122 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP)
1123 /* code courtesy of William Kucharski */
1126 I32 my_chsize(fd, length)
1127 I32 fd; /* file descriptor */
1128 Off_t length; /* length to set file to */
1131 struct stat filebuf;
1133 if (PerlLIO_fstat(fd, &filebuf) < 0)
1136 if (filebuf.st_size < length) {
1138 /* extend file length */
1140 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
1143 /* write a "0" byte */
1145 if ((PerlLIO_write(fd, "", 1)) != 1)
1149 /* truncate length */
1153 fl.l_start = length;
1154 fl.l_type = F_WRLCK; /* write lock on file space */
1157 * This relies on the UNDOCUMENTED F_FREESP argument to
1158 * fcntl(2), which truncates the file so that it ends at the
1159 * position indicated by fl.l_start.
1161 * Will minor miracles never cease?
1164 if (fcntl(fd, F_FREESP, &fl) < 0)
1171 #endif /* F_FREESP */
1174 Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
1176 register char *tmps;
1179 /* assuming fp is checked earlier */
1185 if (SvIOK(sv) && SvIVX(sv) != 0) {
1186 PerlIO_printf(fp, PL_ofmt, (NV)SvIVX(sv));
1187 return !PerlIO_error(fp);
1189 if ( (SvNOK(sv) && SvNVX(sv) != 0.0)
1190 || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) {
1191 PerlIO_printf(fp, PL_ofmt, SvNVX(sv));
1192 return !PerlIO_error(fp);
1195 switch (SvTYPE(sv)) {
1197 if (ckWARN(WARN_UNINITIALIZED))
1205 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
1207 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
1208 return !PerlIO_error(fp);
1212 if (PerlIO_isutf8(fp)) {
1214 sv_utf8_upgrade(sv = sv_mortalcopy(sv));
1216 else if (DO_UTF8(sv))
1217 sv_utf8_downgrade((sv = sv_mortalcopy(sv)), FALSE);
1218 tmps = SvPV(sv, len);
1221 /* To detect whether the process is about to overstep its
1222 * filesize limit we would need getrlimit(). We could then
1223 * also transparently raise the limit with setrlimit() --
1224 * but only until the system hard limit/the filesystem limit,
1225 * at which we would get EPERM. Note that when using buffered
1226 * io the write failure can be delayed until the flush/close. --jhi */
1227 if (len && (PerlIO_write(fp,tmps,len) == 0))
1229 return !PerlIO_error(fp);
1239 if (PL_op->op_flags & OPf_REF) {
1244 if (io && IoIFP(io)) {
1246 sv_setpv(PL_statname,"");
1247 PL_laststype = OP_STAT;
1248 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
1252 return PL_laststatval;
1253 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1254 report_evil_fh(gv, io, PL_op->op_type);
1256 sv_setpv(PL_statname,"");
1257 return (PL_laststatval = -1);
1265 if (SvTYPE(sv) == SVt_PVGV) {
1269 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
1276 sv_setpv(PL_statname, s);
1277 PL_laststype = OP_STAT;
1278 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1279 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
1280 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "stat");
1281 return PL_laststatval;
1291 if (PL_op->op_flags & OPf_REF) {
1293 if (cGVOP_gv == PL_defgv) {
1294 if (PL_laststype != OP_LSTAT)
1295 Perl_croak(aTHX_ "The stat preceding -l _ wasn't an lstat");
1296 return PL_laststatval;
1298 Perl_croak(aTHX_ "You can't use -l on a filehandle");
1301 PL_laststype = OP_LSTAT;
1305 sv_setpv(PL_statname,SvPV(sv, n_a));
1306 PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache);
1307 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
1308 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "lstat");
1309 return PL_laststatval;
1313 Perl_do_aexec(pTHX_ SV *really, register SV **mark, register SV **sp)
1315 return do_aexec5(really, mark, sp, 0, 0);
1319 Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1320 int fd, int do_report)
1322 #ifdef MACOS_TRADITIONAL
1323 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1330 New(401,PL_Argv, sp - mark + 1, char*);
1332 while (++mark <= sp) {
1334 *a++ = SvPVx(*mark, n_a);
1340 tmps = SvPV(really, n_a);
1341 if ((!really && *PL_Argv[0] != '/') ||
1342 (really && *tmps != '/')) /* will execvp use PATH? */
1343 TAINT_ENV(); /* testing IFS here is overkill, probably */
1344 if (really && *tmps)
1345 PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
1347 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1348 if (ckWARN(WARN_EXEC))
1349 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1350 (really ? tmps : PL_Argv[0]), Strerror(errno));
1354 PerlLIO_write(fd, (void*)&e, sizeof(int));
1364 Perl_do_execfree(pTHX)
1368 PL_Argv = Null(char **);
1376 #if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC) && !defined(MACOS_TRADITIONAL)
1379 Perl_do_exec(pTHX_ char *cmd)
1381 return do_exec3(cmd,0,0);
1385 Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report)
1391 while (*cmd && isSPACE(*cmd))
1394 /* save an extra exec if possible */
1397 if (strnEQ(cmd,PL_cshname,PL_cshlen) && strnEQ(cmd+PL_cshlen," -c",3)) {
1399 s = cmd+PL_cshlen+3;
1413 if (s[-1] == '\'') {
1415 PerlProc_execl(PL_cshname,"csh", flags,ncmd,(char*)0);
1423 /* see if there are shell metacharacters in it */
1425 if (*cmd == '.' && isSPACE(cmd[1]))
1428 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1431 for (s = cmd; *s && isALNUM(*s); s++) ; /* catch VAR=val gizmo */
1435 for (s = cmd; *s; s++) {
1436 if (*s != ' ' && !isALPHA(*s) && strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1437 if (*s == '\n' && !s[1]) {
1441 /* handle the 2>&1 construct at the end */
1442 if (*s == '>' && s[1] == '&' && s[2] == '1'
1443 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1444 && (!s[3] || isSPACE(s[3])))
1448 while (*t && isSPACE(*t))
1450 if (!*t && (dup2(1,2) != -1)) {
1456 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
1461 New(402,PL_Argv, (s - cmd) / 2 + 2, char*);
1462 PL_Cmd = savepvn(cmd, s-cmd);
1464 for (s = PL_Cmd; *s;) {
1465 while (*s && isSPACE(*s)) s++;
1468 while (*s && !isSPACE(*s)) s++;
1474 PerlProc_execvp(PL_Argv[0],PL_Argv);
1475 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1482 if (ckWARN(WARN_EXEC))
1483 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1484 PL_Argv[0], Strerror(errno));
1486 PerlLIO_write(fd, (void*)&e, sizeof(int));
1495 #endif /* OS2 || WIN32 */
1498 Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
1502 register I32 tot = 0;
1505 SV **oldmark = mark;
1508 #define APPLY_TAINT_PROPER() \
1510 if (PL_tainted) { TAINT_PROPER(what); } \
1513 /* This is a first heuristic; it doesn't catch tainting magic. */
1515 while (++mark <= sp) {
1516 if (SvTAINTED(*mark)) {
1526 APPLY_TAINT_PROPER();
1529 APPLY_TAINT_PROPER();
1531 while (++mark <= sp) {
1532 char *name = SvPVx(*mark, n_a);
1533 APPLY_TAINT_PROPER();
1534 if (PerlLIO_chmod(name, val))
1542 APPLY_TAINT_PROPER();
1543 if (sp - mark > 2) {
1544 val = SvIVx(*++mark);
1545 val2 = SvIVx(*++mark);
1546 APPLY_TAINT_PROPER();
1548 while (++mark <= sp) {
1549 char *name = SvPVx(*mark, n_a);
1550 APPLY_TAINT_PROPER();
1551 if (PerlLIO_chown(name, val, val2))
1558 XXX Should we make lchown() directly available from perl?
1559 For now, we'll let Configure test for HAS_LCHOWN, but do
1560 nothing in the core.
1566 APPLY_TAINT_PROPER();
1569 s = SvPVx(*++mark, n_a);
1571 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1573 if (!(val = whichsig(s)))
1574 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
1578 APPLY_TAINT_PROPER();
1581 /* kill() doesn't do process groups (job trees?) under VMS */
1582 if (val < 0) val = -val;
1583 if (val == SIGKILL) {
1584 # include <starlet.h>
1585 /* Use native sys$delprc() to insure that target process is
1586 * deleted; supervisor-mode images don't pay attention to
1587 * CRTL's emulation of Unix-style signals and kill()
1589 while (++mark <= sp) {
1590 I32 proc = SvIVx(*mark);
1591 register unsigned long int __vmssts;
1592 APPLY_TAINT_PROPER();
1593 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1597 case SS$_NOSUCHNODE:
1598 SETERRNO(ESRCH,__vmssts);
1601 SETERRNO(EPERM,__vmssts);
1604 SETERRNO(EVMSERR,__vmssts);
1613 while (++mark <= sp) {
1614 I32 proc = SvIVx(*mark);
1615 APPLY_TAINT_PROPER();
1617 if (PerlProc_killpg(proc,val)) /* BSD */
1619 if (PerlProc_kill(-proc,val)) /* SYSV */
1625 while (++mark <= sp) {
1626 I32 proc = SvIVx(*mark);
1627 APPLY_TAINT_PROPER();
1628 if (PerlProc_kill(proc, val))
1636 APPLY_TAINT_PROPER();
1638 while (++mark <= sp) {
1639 s = SvPVx(*mark, n_a);
1640 APPLY_TAINT_PROPER();
1641 if (PL_euid || PL_unsafe) {
1645 else { /* don't let root wipe out directories without -U */
1646 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1658 APPLY_TAINT_PROPER();
1659 if (sp - mark > 2) {
1660 #if defined(I_UTIME) || defined(VMS)
1661 struct utimbuf utbuf;
1669 Zero(&utbuf, sizeof utbuf, char);
1671 utbuf.actime = (Time_t)SvNVx(*++mark); /* time accessed */
1672 utbuf.modtime = (Time_t)SvNVx(*++mark); /* time modified */
1674 utbuf.actime = (Time_t)SvIVx(*++mark); /* time accessed */
1675 utbuf.modtime = (Time_t)SvIVx(*++mark); /* time modified */
1677 APPLY_TAINT_PROPER();
1679 while (++mark <= sp) {
1680 char *name = SvPVx(*mark, n_a);
1681 APPLY_TAINT_PROPER();
1682 if (PerlLIO_utime(name, &utbuf))
1693 #undef APPLY_TAINT_PROPER
1696 /* Do the permissions allow some operation? Assumes statcache already set. */
1697 #ifndef VMS /* VMS' cando is in vms.c */
1699 Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register Stat_t *statbufp)
1700 /* Note: we use `effective' both for uids and gids.
1701 * Here we are betting on Uid_t being equal or wider than Gid_t. */
1704 /* [Comments and code from Len Reed]
1705 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1706 * to write-protected files. The execute permission bit is set
1707 * by the Miscrosoft C library stat() function for the following:
1712 * All files and directories are readable.
1713 * Directories and special files, e.g. "CON", cannot be
1715 * [Comment by Tom Dinger -- a directory can have the write-protect
1716 * bit set in the file system, but DOS permits changes to
1717 * the directory anyway. In addition, all bets are off
1718 * here for networked software, such as Novell and
1722 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1723 * too so it will actually look into the files for magic numbers
1725 return (mode & statbufp->st_mode) ? TRUE : FALSE;
1727 #else /* ! DOSISH */
1728 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
1729 if (mode == S_IXUSR) {
1730 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1734 return TRUE; /* root reads and writes anything */
1737 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1738 if (statbufp->st_mode & mode)
1739 return TRUE; /* ok as "user" */
1741 else if (ingroup(statbufp->st_gid,effective)) {
1742 if (statbufp->st_mode & mode >> 3)
1743 return TRUE; /* ok as "group" */
1745 else if (statbufp->st_mode & mode >> 6)
1746 return TRUE; /* ok as "other" */
1748 #endif /* ! DOSISH */
1753 Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective)
1755 #ifdef MACOS_TRADITIONAL
1756 /* This is simply not correct for AppleShare, but fix it yerself. */
1759 if (testgid == (effective ? PL_egid : PL_gid))
1761 #ifdef HAS_GETGROUPS
1766 Groups_t gary[NGROUPS];
1769 anum = getgroups(NGROUPS,gary);
1771 if (gary[anum] == testgid)
1779 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1782 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
1787 key = (key_t)SvNVx(*++mark);
1788 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1789 flags = SvIVx(*++mark);
1795 return msgget(key, flags);
1799 return semget(key, n, flags);
1803 return shmget(key, n, flags);
1805 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1807 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1810 return -1; /* should never happen */
1814 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
1818 I32 id, n, cmd, infosize, getinfo;
1821 id = SvIVx(*++mark);
1822 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1823 cmd = SvIVx(*++mark);
1826 getinfo = (cmd == IPC_STAT);
1832 if (cmd == IPC_STAT || cmd == IPC_SET)
1833 infosize = sizeof(struct msqid_ds);
1838 if (cmd == IPC_STAT || cmd == IPC_SET)
1839 infosize = sizeof(struct shmid_ds);
1845 if (cmd == IPC_STAT || cmd == IPC_SET)
1846 infosize = sizeof(struct semid_ds);
1847 else if (cmd == GETALL || cmd == SETALL)
1849 struct semid_ds semds;
1851 #ifdef EXTRA_F_IN_SEMUN_BUF
1852 semun.buff = &semds;
1856 getinfo = (cmd == GETALL);
1857 if (Semctl(id, 0, IPC_STAT, semun) == -1)
1859 infosize = semds.sem_nsems * sizeof(short);
1860 /* "short" is technically wrong but much more portable
1861 than guessing about u_?short(_t)? */
1864 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1868 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1870 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1879 SvPV_force(astr, len);
1880 a = SvGROW(astr, infosize+1);
1884 a = SvPV(astr, len);
1885 if (len != infosize)
1886 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
1895 a = INT2PTR(char *,i); /* ouch */
1902 ret = msgctl(id, cmd, (struct msqid_ds *)a);
1908 union semun unsemds;
1910 #ifdef EXTRA_F_IN_SEMUN_BUF
1911 unsemds.buff = (struct semid_ds *)a;
1913 unsemds.buf = (struct semid_ds *)a;
1915 ret = Semctl(id, n, cmd, unsemds);
1917 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1924 ret = shmctl(id, cmd, (struct shmid_ds *)a);
1928 if (getinfo && ret >= 0) {
1929 SvCUR_set(astr, infosize);
1930 *SvEND(astr) = '\0';
1937 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
1942 I32 id, msize, flags;
1945 id = SvIVx(*++mark);
1947 flags = SvIVx(*++mark);
1948 mbuf = SvPV(mstr, len);
1949 if ((msize = len - sizeof(long)) < 0)
1950 Perl_croak(aTHX_ "Arg too short for msgsnd");
1952 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
1954 Perl_croak(aTHX_ "msgsnd not implemented");
1959 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
1965 I32 id, msize, flags, ret;
1968 id = SvIVx(*++mark);
1970 /* suppress warning when reading into undef var --jhi */
1972 sv_setpvn(mstr, "", 0);
1973 msize = SvIVx(*++mark);
1974 mtype = (long)SvIVx(*++mark);
1975 flags = SvIVx(*++mark);
1976 SvPV_force(mstr, len);
1977 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
1980 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
1982 SvCUR_set(mstr, sizeof(long)+ret);
1983 *SvEND(mstr) = '\0';
1984 #ifndef INCOMPLETE_TAINTS
1985 /* who knows who has been playing with this message? */
1991 Perl_croak(aTHX_ "msgrcv not implemented");
1996 Perl_do_semop(pTHX_ SV **mark, SV **sp)
2004 id = SvIVx(*++mark);
2006 opbuf = SvPV(opstr, opsize);
2007 if (opsize < sizeof(struct sembuf)
2008 || (opsize % sizeof(struct sembuf)) != 0) {
2009 SETERRNO(EINVAL,LIB$_INVARG);
2013 return semop(id, (struct sembuf *)opbuf, opsize/sizeof(struct sembuf));
2015 Perl_croak(aTHX_ "semop not implemented");
2020 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
2025 I32 id, mpos, msize;
2027 struct shmid_ds shmds;
2029 id = SvIVx(*++mark);
2031 mpos = SvIVx(*++mark);
2032 msize = SvIVx(*++mark);
2034 if (shmctl(id, IPC_STAT, &shmds) == -1)
2036 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
2037 SETERRNO(EFAULT,SS$_ACCVIO); /* can't do as caller requested */
2040 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
2041 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2043 if (optype == OP_SHMREAD) {
2044 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2046 sv_setpvn(mstr, "", 0);
2047 SvPV_force(mstr, len);
2048 mbuf = SvGROW(mstr, msize+1);
2050 Copy(shm + mpos, mbuf, msize, char);
2051 SvCUR_set(mstr, msize);
2052 *SvEND(mstr) = '\0';
2054 #ifndef INCOMPLETE_TAINTS
2055 /* who knows who has been playing with this shared memory? */
2062 mbuf = SvPV(mstr, len);
2063 if ((n = len) > msize)
2065 Copy(mbuf, shm + mpos, n, char);
2067 memzero(shm + mpos + n, msize - n);
2071 Perl_croak(aTHX_ "shm I/O not implemented");
2075 #endif /* SYSV IPC */
2078 =for apidoc start_glob
2080 Function called by C<do_readline> to spawn a glob (or do the glob inside
2081 perl on VMS). This code used to be inline, but now perl uses C<File::Glob>
2082 this glob starter is only used by miniperl during the build proccess.
2083 Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up.
2089 Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
2091 SV *tmpcmd = NEWSV(55, 0);
2095 #ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
2096 /* since spawning off a process is a real performance hit */
2098 #include <descrip.h>
2099 #include <lib$routines.h>
2102 char rslt[NAM$C_MAXRSS+1+sizeof(unsigned short int)] = {'\0','\0'};
2103 char vmsspec[NAM$C_MAXRSS+1];
2104 char *rstr = rslt + sizeof(unsigned short int), *begin, *end, *cp;
2105 char tmpfnam[L_tmpnam] = "SYS$SCRATCH:";
2106 $DESCRIPTOR(dfltdsc,"SYS$DISK:[]*.*;");
2109 struct dsc$descriptor_s wilddsc
2110 = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
2111 struct dsc$descriptor_vs rsdsc
2112 = {sizeof rslt, DSC$K_DTYPE_VT, DSC$K_CLASS_VS, rslt};
2113 unsigned long int cxt = 0, sts = 0, ok = 1, hasdir = 0, hasver = 0, isunix = 0;
2115 /* We could find out if there's an explicit dev/dir or version
2116 by peeking into lib$find_file's internal context at
2117 ((struct NAM *)((struct FAB *)cxt)->fab$l_nam)->nam$l_fnb
2118 but that's unsupported, so I don't want to do it now and
2119 have it bite someone in the future. */
2120 strcat(tmpfnam,PerlLIO_tmpnam(NULL));
2121 cp = SvPV(tmpglob,i);
2123 if (cp[i] == ';') hasver = 1;
2125 if (sts) hasver = 1;
2129 hasdir = isunix = 1;
2132 if (cp[i] == ']' || cp[i] == '>' || cp[i] == ':') {
2137 if ((tmpfp = PerlIO_open(tmpfnam,"w+","fop=dlt")) != NULL) {
2139 if (!PerlLIO_stat(SvPVX(tmpglob),&st) && S_ISDIR(st.st_mode))
2140 ok = ((wilddsc.dsc$a_pointer = tovmspath(SvPVX(tmpglob),vmsspec)) != NULL);
2141 else ok = ((wilddsc.dsc$a_pointer = tovmsspec(SvPVX(tmpglob),vmsspec)) != NULL);
2142 if (ok) wilddsc.dsc$w_length = (unsigned short int) strlen(wilddsc.dsc$a_pointer);
2143 while (ok && ((sts = lib$find_file(&wilddsc,&rsdsc,&cxt,
2144 &dfltdsc,NULL,NULL,NULL))&1)) {
2145 end = rstr + (unsigned long int) *rslt;
2146 if (!hasver) while (*end != ';') end--;
2147 *(end++) = '\n'; *end = '\0';
2148 for (cp = rstr; *cp; cp++) *cp = _tolower(*cp);
2150 if (isunix) trim_unixpath(rstr,SvPVX(tmpglob),1);
2155 while (*(--begin) != ']' && *begin != '>') ;
2158 ok = (PerlIO_puts(tmpfp,begin) != EOF);
2160 if (cxt) (void)lib$find_file_end(&cxt);
2161 if (ok && sts != RMS$_NMF &&
2162 sts != RMS$_DNF && sts != RMS$_FNF) ok = 0;
2165 SETERRNO((sts == RMS$_SYN ? EINVAL : EVMSERR),sts);
2167 PerlIO_close(tmpfp);
2171 PerlIO_rewind(tmpfp);
2172 IoTYPE(io) = IoTYPE_RDONLY;
2173 IoIFP(io) = fp = tmpfp;
2174 IoFLAGS(io) &= ~IOf_UNTAINT; /* maybe redundant */
2179 #ifdef MACOS_TRADITIONAL
2180 sv_setpv(tmpcmd, "glob ");
2181 sv_catsv(tmpcmd, tmpglob);
2182 sv_catpv(tmpcmd, " |");
2186 sv_setpv(tmpcmd, "for a in ");
2187 sv_catsv(tmpcmd, tmpglob);
2188 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
2191 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
2192 sv_catsv(tmpcmd, tmpglob);
2194 sv_setpv(tmpcmd, "perlglob ");
2195 sv_catsv(tmpcmd, tmpglob);
2196 sv_catpv(tmpcmd, " |");
2201 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
2202 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
2203 sv_catsv(tmpcmd, tmpglob);
2204 sv_catpv(tmpcmd, "' 2>/dev/null |");
2206 sv_setpv(tmpcmd, "echo ");
2207 sv_catsv(tmpcmd, tmpglob);
2209 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
2211 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
2214 #endif /* !DOSISH */
2215 #endif /* MACOS_TRADITIONAL */
2216 (void)do_open(PL_last_in_gv, SvPVX(tmpcmd), SvCUR(tmpcmd),
2217 FALSE, O_RDONLY, 0, Nullfp);