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;
297 if (!*name && supplied_fp)
298 /* "<+&" etc. is used by typemaps */
302 for (; isSPACE(*name); name++) ;
307 gv = gv_fetchpv(name,FALSE,SVt_PVIO);
311 SETERRNO(EINVAL,SS$_IVCHAN);
316 PerlIO *fp = IoIFP(thatio);
317 /* Flush stdio buffer before dup. --mjd
318 * Unfortunately SEEK_CURing 0 seems to
319 * be optimized away on most platforms;
320 * only Solaris and Linux seem to flush
323 /* sfio fails to clear error on next
324 sfwrite, contrary to documentation.
326 if (PerlIO_seek(fp, 0, SEEK_CUR) == -1)
329 /* On the other hand, do all platforms
330 * take gracefully to flushing a read-only
331 * filehandle? Perhaps we should do
332 * fsetpos(src)+fgetpos(dst)? --nik */
334 fd = PerlIO_fileno(fp);
335 /* When dup()ing STDIN, STDOUT or STDERR
336 * explicitly set appropriate access mode */
337 if (IoIFP(thatio) == PerlIO_stdout()
338 || IoIFP(thatio) == PerlIO_stderr())
339 IoTYPE(io) = IoTYPE_WRONLY;
340 else if (IoIFP(thatio) == PerlIO_stdin())
341 IoTYPE(io) = IoTYPE_RDONLY;
342 /* When dup()ing a socket, say result is
344 else if (IoTYPE(thatio) == IoTYPE_SOCKET)
345 IoTYPE(io) = IoTYPE_SOCKET;
351 fd = PerlLIO_dup(fd);
354 if (!(fp = PerlIO_fdopen(fd,mode))) {
362 Perl_croak(aTHX_ "More than one argument to '>' open");
365 for (; isSPACE(*type); type++) ;
366 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
369 fp = PerlIO_stdout();
370 IoTYPE(io) = IoTYPE_STD;
373 fp = PerlIO_open((num_svs ? name : type), mode);
377 else if (*type == IoTYPE_RDONLY) {
379 Perl_croak(aTHX_ "More than one argument to '<' open");
382 for (type++; isSPACE(*type); type++) ;
393 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
397 IoTYPE(io) = IoTYPE_STD;
400 fp = PerlIO_open((num_svs ? name : type), mode);
402 else if ((num_svs && type[0] == IoTYPE_STD && type[1] == IoTYPE_PIPE) ||
403 (!num_svs && tend > type+1 && tend[-1] == IoTYPE_PIPE)) {
405 type += 2; /* skip over '-|' */
409 while (tend > type && isSPACE(tend[-1]))
412 for (; isSPACE(*type); type++) ;
417 /* command is missing 19990114 */
418 if (ckWARN(WARN_PIPE))
419 Perl_warner(aTHX_ WARN_PIPE, "Missing command in piped open");
423 if (strNE(name,"-") || num_svs)
425 TAINT_PROPER("piped open");
432 fp = PerlProc_popen_list(mode,num_svs,svp);
436 fp = PerlProc_popen(name,mode);
438 IoTYPE(io) = IoTYPE_PIPE;
444 IoTYPE(io) = IoTYPE_RDONLY;
446 for (; isSPACE(*name); name++) ;
452 if (strEQ(name,"-")) {
454 IoTYPE(io) = IoTYPE_STD;
457 fp = PerlIO_open(name,mode);
462 if (ckWARN(WARN_NEWLINE) && IoTYPE(io) == IoTYPE_RDONLY && strchr(name, '\n'))
463 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "open");
466 if (IoTYPE(io) && IoTYPE(io) != IoTYPE_PIPE && IoTYPE(io) != IoTYPE_STD) {
467 if (PerlLIO_fstat(PerlIO_fileno(fp),&PL_statbuf) < 0) {
468 (void)PerlIO_close(fp);
471 if (S_ISSOCK(PL_statbuf.st_mode))
472 IoTYPE(io) = IoTYPE_SOCKET; /* in case a socket was passed in to us */
476 !(PL_statbuf.st_mode & S_IFMT)
480 && IoTYPE(io) != IoTYPE_WRONLY /* Dups of STD* filehandles already have */
481 && IoTYPE(io) != IoTYPE_RDONLY /* type so they aren't marked as sockets */
482 ) { /* on OS's that return 0 on fstat()ed pipe */
484 Sock_size_t buflen = sizeof tmpbuf;
485 if (PerlSock_getsockname(PerlIO_fileno(fp), (struct sockaddr *)tmpbuf,
487 || errno != ENOTSOCK)
488 IoTYPE(io) = IoTYPE_SOCKET; /* some OS's return 0 on fstat()ed socket */
489 /* but some return 0 for streams too, sigh */
493 if (saveifp) { /* must use old fp? */
494 /* If fd is less that PL_maxsysfd i.e. STDIN..STDERR
495 then dup the new fileno down
497 fd = PerlIO_fileno(saveifp);
499 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
500 if (saveofp != saveifp) { /* was a socket? */
501 PerlIO_close(saveofp);
502 /* This looks very suspect - NI-S 24 Nov 2000 */
504 Safefree(saveofp); /* ??? */
507 if (fd != PerlIO_fileno(fp)) {
511 PerlLIO_dup2(PerlIO_fileno(fp), fd);
513 if (fd != PerlIO_fileno(PerlIO_stdin())) {
514 char newname[FILENAME_MAX+1];
515 if (fgetname(fp, newname)) {
516 if (fd == PerlIO_fileno(PerlIO_stdout())) Perl_vmssetuserlnm("SYS$OUTPUT", newname);
517 if (fd == PerlIO_fileno(PerlIO_stderr())) Perl_vmssetuserlnm("SYS$ERROR", newname);
522 sv = *av_fetch(PL_fdpid,PerlIO_fileno(fp),TRUE);
523 (void)SvUPGRADE(sv, SVt_IV);
526 sv = *av_fetch(PL_fdpid,fd,TRUE);
528 (void)SvUPGRADE(sv, SVt_IV);
537 #if defined(HAS_FCNTL) && defined(F_SETFD)
539 int save_errno = errno;
540 fd = PerlIO_fileno(fp);
541 fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
547 /* Need to supply default type info from open.pm */
548 SV *layers = PL_curcop->cop_io;
552 type = SvPV(layers,len);
553 if (type && mode[0] != 'r') {
554 /* Skip to write part */
555 char *s = strchr(type,0);
556 if (s && (s-type) < len) {
563 while (isSPACE(*type)) type++;
566 if (PerlIO_apply_layers(aTHX_ IoIFP(io),mode,type) != 0) {
572 IoFLAGS(io) &= ~IOf_NOLINE;
574 if (IoTYPE(io) == IoTYPE_SOCKET
575 || (IoTYPE(io) == IoTYPE_WRONLY && S_ISCHR(PL_statbuf.st_mode)) )
578 if (!(IoOFP(io) = PerlIO_fdopen(PerlIO_fileno(fp),mode))) {
584 if (PerlIO_apply_layers(aTHX_ IoOFP(io),mode,type) != 0) {
585 PerlIO_close(IoOFP(io));
601 IoTYPE(io) = savetype;
606 Perl_nextargv(pTHX_ register GV *gv)
609 #ifndef FLEXFILENAMES
618 PL_argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO);
619 if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
620 IoFLAGS(io) &= ~IOf_START;
622 if (!PL_argvout_stack)
623 PL_argvout_stack = newAV();
624 av_push(PL_argvout_stack, SvREFCNT_inc(PL_defoutgv));
627 if (PL_filemode & (S_ISUID|S_ISGID)) {
628 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
630 (void)fchmod(PL_lastfd,PL_filemode);
632 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
636 while (av_len(GvAV(gv)) >= 0) {
638 sv = av_shift(GvAV(gv));
640 sv_setsv(GvSV(gv),sv);
641 SvSETMAGIC(GvSV(gv));
642 PL_oldname = SvPVx(GvSV(gv), oldlen);
643 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,Nullfp)) {
645 TAINT_PROPER("inplace open");
646 if (oldlen == 1 && *PL_oldname == '-') {
647 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
648 return IoIFP(GvIOp(gv));
650 #ifndef FLEXFILENAMES
651 filedev = PL_statbuf.st_dev;
652 fileino = PL_statbuf.st_ino;
654 PL_filemode = PL_statbuf.st_mode;
655 fileuid = PL_statbuf.st_uid;
656 filegid = PL_statbuf.st_gid;
657 if (!S_ISREG(PL_filemode)) {
658 if (ckWARN_d(WARN_INPLACE))
659 Perl_warner(aTHX_ WARN_INPLACE,
660 "Can't do inplace edit: %s is not a regular file",
666 char *star = strchr(PL_inplace, '*');
668 char *begin = PL_inplace;
669 sv_setpvn(sv, "", 0);
671 sv_catpvn(sv, begin, star - begin);
672 sv_catpvn(sv, PL_oldname, oldlen);
674 } while ((star = strchr(begin, '*')));
679 sv_catpv(sv,PL_inplace);
681 #ifndef FLEXFILENAMES
682 if (PerlLIO_stat(SvPVX(sv),&PL_statbuf) >= 0
683 && PL_statbuf.st_dev == filedev
684 && PL_statbuf.st_ino == fileino
686 || (_djstat_fail_bits & _STFAIL_TRUENAME)!=0
690 if (ckWARN_d(WARN_INPLACE))
691 Perl_warner(aTHX_ WARN_INPLACE,
692 "Can't do inplace edit: %s would not be unique",
699 #if !defined(DOSISH) && !defined(__CYGWIN__)
700 if (PerlLIO_rename(PL_oldname,SvPVX(sv)) < 0) {
701 if (ckWARN_d(WARN_INPLACE))
702 Perl_warner(aTHX_ WARN_INPLACE,
703 "Can't rename %s to %s: %s, skipping file",
704 PL_oldname, SvPVX(sv), Strerror(errno) );
710 (void)PerlLIO_unlink(SvPVX(sv));
711 (void)PerlLIO_rename(PL_oldname,SvPVX(sv));
712 do_open(gv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,O_RDONLY,0,Nullfp);
715 (void)UNLINK(SvPVX(sv));
716 if (link(PL_oldname,SvPVX(sv)) < 0) {
717 if (ckWARN_d(WARN_INPLACE))
718 Perl_warner(aTHX_ WARN_INPLACE,
719 "Can't rename %s to %s: %s, skipping file",
720 PL_oldname, SvPVX(sv), Strerror(errno) );
724 (void)UNLINK(PL_oldname);
728 #if !defined(DOSISH) && !defined(AMIGAOS)
729 # ifndef VMS /* Don't delete; use automatic file versioning */
730 if (UNLINK(PL_oldname) < 0) {
731 if (ckWARN_d(WARN_INPLACE))
732 Perl_warner(aTHX_ WARN_INPLACE,
733 "Can't remove %s: %s, skipping file",
734 PL_oldname, Strerror(errno) );
740 Perl_croak(aTHX_ "Can't do inplace edit without backup");
744 sv_setpvn(sv,">",!PL_inplace);
745 sv_catpvn(sv,PL_oldname,oldlen);
746 SETERRNO(0,0); /* in case sprintf set errno */
748 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
749 O_WRONLY|O_CREAT|O_TRUNC,0,Nullfp))
751 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
752 O_WRONLY|O_CREAT|OPEN_EXCL,0666,Nullfp))
755 if (ckWARN_d(WARN_INPLACE))
756 Perl_warner(aTHX_ WARN_INPLACE, "Can't do inplace edit on %s: %s",
757 PL_oldname, Strerror(errno) );
761 setdefout(PL_argvoutgv);
762 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
763 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
765 (void)fchmod(PL_lastfd,PL_filemode);
767 # if !(defined(WIN32) && defined(__BORLANDC__))
768 /* Borland runtime creates a readonly file! */
769 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
772 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
774 (void)fchown(PL_lastfd,fileuid,filegid);
777 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
782 return IoIFP(GvIOp(gv));
785 if (ckWARN_d(WARN_INPLACE)) {
787 if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0
788 && !S_ISREG(PL_statbuf.st_mode))
790 Perl_warner(aTHX_ WARN_INPLACE,
791 "Can't do inplace edit: %s is not a regular file",
795 Perl_warner(aTHX_ WARN_INPLACE, "Can't open %s: %s",
796 PL_oldname, Strerror(eno));
800 if (io && (IoFLAGS(io) & IOf_ARGV))
801 IoFLAGS(io) |= IOf_START;
803 (void)do_close(PL_argvoutgv,FALSE);
804 if (io && (IoFLAGS(io) & IOf_ARGV)
805 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
807 GV *oldout = (GV*)av_pop(PL_argvout_stack);
809 SvREFCNT_dec(oldout);
812 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
819 Perl_do_pipe(pTHX_ SV *sv, GV *rgv, GV *wgv)
838 if (PerlProc_pipe(fd) < 0)
840 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r");
841 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w");
842 IoIFP(wstio) = IoOFP(wstio);
843 IoTYPE(rstio) = IoTYPE_RDONLY;
844 IoTYPE(wstio) = IoTYPE_WRONLY;
845 if (!IoIFP(rstio) || !IoOFP(wstio)) {
846 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
847 else PerlLIO_close(fd[0]);
848 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
849 else PerlLIO_close(fd[1]);
853 sv_setsv(sv,&PL_sv_yes);
857 sv_setsv(sv,&PL_sv_undef);
862 /* explicit renamed to avoid C++ conflict -- kja */
864 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
871 if (!gv || SvTYPE(gv) != SVt_PVGV) {
873 SETERRNO(EBADF,SS$_IVCHAN);
877 if (!io) { /* never opened */
879 if (ckWARN(WARN_UNOPENED)) /* no check for closed here */
880 report_evil_fh(gv, io, PL_op->op_type);
881 SETERRNO(EBADF,SS$_IVCHAN);
885 retval = io_close(io, not_implicit);
889 IoLINES_LEFT(io) = IoPAGE_LEN(io);
891 IoTYPE(io) = IoTYPE_CLOSED;
896 Perl_io_close(pTHX_ IO *io, bool not_implicit)
902 if (IoTYPE(io) == IoTYPE_PIPE) {
903 status = PerlProc_pclose(IoIFP(io));
905 STATUS_NATIVE_SET(status);
906 retval = (STATUS_POSIX == 0);
909 retval = (status != -1);
912 else if (IoTYPE(io) == IoTYPE_STD)
915 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
916 retval = (PerlIO_close(IoOFP(io)) != EOF);
917 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
920 retval = (PerlIO_close(IoIFP(io)) != EOF);
922 IoOFP(io) = IoIFP(io) = Nullfp;
924 else if (not_implicit) {
925 SETERRNO(EBADF,SS$_IVCHAN);
932 Perl_do_eof(pTHX_ GV *gv)
941 else if (ckWARN(WARN_IO)
942 && (IoTYPE(io) == IoTYPE_WRONLY || IoIFP(io) == PerlIO_stdout()
943 || IoIFP(io) == PerlIO_stderr()))
945 /* integrate to report_evil_fh()? */
948 SV* sv = sv_newmortal();
949 gv_efullname4(sv, gv, Nullch, FALSE);
950 name = SvPV_nolen(sv);
953 Perl_warner(aTHX_ WARN_IO,
954 "Filehandle %s opened only for output", name);
956 Perl_warner(aTHX_ WARN_IO,
957 "Filehandle opened only for output");
962 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
963 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
964 return FALSE; /* this is the most usual case */
967 ch = PerlIO_getc(IoIFP(io));
969 (void)PerlIO_ungetc(IoIFP(io),ch);
973 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
974 if (PerlIO_get_cnt(IoIFP(io)) < -1)
975 PerlIO_set_cnt(IoIFP(io),-1);
977 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
978 if (!nextargv(PL_argvgv)) /* get another fp handy */
982 return TRUE; /* normal fp, definitely end of file */
988 Perl_do_tell(pTHX_ GV *gv)
993 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
994 #ifdef ULTRIX_STDIO_BOTCH
996 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
998 return PerlIO_tell(fp);
1000 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1001 report_evil_fh(gv, io, PL_op->op_type);
1002 SETERRNO(EBADF,RMS$_IFI);
1007 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
1010 register PerlIO *fp;
1012 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1013 #ifdef ULTRIX_STDIO_BOTCH
1015 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1017 return PerlIO_seek(fp, pos, whence) >= 0;
1019 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1020 report_evil_fh(gv, io, PL_op->op_type);
1021 SETERRNO(EBADF,RMS$_IFI);
1026 Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
1029 register PerlIO *fp;
1031 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
1032 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
1033 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1034 report_evil_fh(gv, io, PL_op->op_type);
1035 SETERRNO(EBADF,RMS$_IFI);
1040 Perl_mode_from_discipline(pTHX_ SV *discp)
1042 int mode = O_BINARY;
1045 char *s = SvPV(discp,len);
1050 if (len > 3 && strnEQ(s+1, "raw", 3)
1051 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1060 if (len > 4 && strnEQ(s+1, "crlf", 4)
1061 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1070 goto fail_discipline;
1073 else if (isSPACE(*s)) {
1080 end = strchr(s+1, ':');
1083 #ifndef PERLIO_LAYERS
1084 Perl_croak(aTHX_ "Unknown discipline '%.*s'", end-s, s);
1095 Perl_do_binmode(pTHX_ PerlIO *fp, int iotype, int mode)
1097 /* The old body of this is now in non-LAYER part of perlio.c
1098 * This is a stub for any XS code which might have been calling it.
1100 char *name = (O_BINARY != O_TEXT && !(mode & O_BINARY)) ? ":crlf" : ":raw";
1101 return PerlIO_binmode(aTHX_ fp, iotype, mode, name);
1104 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP)
1105 /* code courtesy of William Kucharski */
1108 I32 my_chsize(fd, length)
1109 I32 fd; /* file descriptor */
1110 Off_t length; /* length to set file to */
1113 struct stat filebuf;
1115 if (PerlLIO_fstat(fd, &filebuf) < 0)
1118 if (filebuf.st_size < length) {
1120 /* extend file length */
1122 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
1125 /* write a "0" byte */
1127 if ((PerlLIO_write(fd, "", 1)) != 1)
1131 /* truncate length */
1135 fl.l_start = length;
1136 fl.l_type = F_WRLCK; /* write lock on file space */
1139 * This relies on the UNDOCUMENTED F_FREESP argument to
1140 * fcntl(2), which truncates the file so that it ends at the
1141 * position indicated by fl.l_start.
1143 * Will minor miracles never cease?
1146 if (fcntl(fd, F_FREESP, &fl) < 0)
1153 #endif /* F_FREESP */
1156 Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
1158 register char *tmps;
1161 /* assuming fp is checked earlier */
1167 if (SvIOK(sv) && SvIVX(sv) != 0) {
1168 PerlIO_printf(fp, PL_ofmt, (NV)SvIVX(sv));
1169 return !PerlIO_error(fp);
1171 if ( (SvNOK(sv) && SvNVX(sv) != 0.0)
1172 || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) {
1173 PerlIO_printf(fp, PL_ofmt, SvNVX(sv));
1174 return !PerlIO_error(fp);
1177 switch (SvTYPE(sv)) {
1179 if (ckWARN(WARN_UNINITIALIZED))
1187 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
1189 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
1190 return !PerlIO_error(fp);
1194 if (PerlIO_isutf8(fp)) {
1196 sv_utf8_upgrade(sv = sv_mortalcopy(sv));
1198 else if (DO_UTF8(sv))
1199 sv_utf8_downgrade((sv = sv_mortalcopy(sv)), FALSE);
1200 tmps = SvPV(sv, len);
1203 /* To detect whether the process is about to overstep its
1204 * filesize limit we would need getrlimit(). We could then
1205 * also transparently raise the limit with setrlimit() --
1206 * but only until the system hard limit/the filesystem limit,
1207 * at which we would get EPERM. Note that when using buffered
1208 * io the write failure can be delayed until the flush/close. --jhi */
1209 if (len && (PerlIO_write(fp,tmps,len) == 0))
1211 return !PerlIO_error(fp);
1221 if (PL_op->op_flags & OPf_REF) {
1226 if (io && IoIFP(io)) {
1228 sv_setpv(PL_statname,"");
1229 PL_laststype = OP_STAT;
1230 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
1234 return PL_laststatval;
1235 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1236 report_evil_fh(gv, io, PL_op->op_type);
1238 sv_setpv(PL_statname,"");
1239 return (PL_laststatval = -1);
1247 if (SvTYPE(sv) == SVt_PVGV) {
1251 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
1258 sv_setpv(PL_statname, s);
1259 PL_laststype = OP_STAT;
1260 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1261 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
1262 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "stat");
1263 return PL_laststatval;
1273 if (PL_op->op_flags & OPf_REF) {
1275 if (cGVOP_gv == PL_defgv) {
1276 if (PL_laststype != OP_LSTAT)
1277 Perl_croak(aTHX_ "The stat preceding -l _ wasn't an lstat");
1278 return PL_laststatval;
1280 Perl_croak(aTHX_ "You can't use -l on a filehandle");
1283 PL_laststype = OP_LSTAT;
1287 sv_setpv(PL_statname,SvPV(sv, n_a));
1288 PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache);
1289 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
1290 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "lstat");
1291 return PL_laststatval;
1295 Perl_do_aexec(pTHX_ SV *really, register SV **mark, register SV **sp)
1297 return do_aexec5(really, mark, sp, 0, 0);
1301 Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1302 int fd, int do_report)
1304 #ifdef MACOS_TRADITIONAL
1305 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1312 New(401,PL_Argv, sp - mark + 1, char*);
1314 while (++mark <= sp) {
1316 *a++ = SvPVx(*mark, n_a);
1322 tmps = SvPV(really, n_a);
1323 if ((!really && *PL_Argv[0] != '/') ||
1324 (really && *tmps != '/')) /* will execvp use PATH? */
1325 TAINT_ENV(); /* testing IFS here is overkill, probably */
1326 if (really && *tmps)
1327 PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
1329 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1330 if (ckWARN(WARN_EXEC))
1331 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1332 (really ? tmps : PL_Argv[0]), Strerror(errno));
1336 PerlLIO_write(fd, (void*)&e, sizeof(int));
1346 Perl_do_execfree(pTHX)
1350 PL_Argv = Null(char **);
1358 #if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC) && !defined(MACOS_TRADITIONAL)
1361 Perl_do_exec(pTHX_ char *cmd)
1363 return do_exec3(cmd,0,0);
1367 Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report)
1373 while (*cmd && isSPACE(*cmd))
1376 /* save an extra exec if possible */
1379 if (strnEQ(cmd,PL_cshname,PL_cshlen) && strnEQ(cmd+PL_cshlen," -c",3)) {
1381 s = cmd+PL_cshlen+3;
1395 if (s[-1] == '\'') {
1397 PerlProc_execl(PL_cshname,"csh", flags,ncmd,(char*)0);
1405 /* see if there are shell metacharacters in it */
1407 if (*cmd == '.' && isSPACE(cmd[1]))
1410 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1413 for (s = cmd; *s && isALNUM(*s); s++) ; /* catch VAR=val gizmo */
1417 for (s = cmd; *s; s++) {
1418 if (*s != ' ' && !isALPHA(*s) && strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1419 if (*s == '\n' && !s[1]) {
1423 /* handle the 2>&1 construct at the end */
1424 if (*s == '>' && s[1] == '&' && s[2] == '1'
1425 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1426 && (!s[3] || isSPACE(s[3])))
1430 while (*t && isSPACE(*t))
1432 if (!*t && (dup2(1,2) != -1)) {
1438 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
1443 New(402,PL_Argv, (s - cmd) / 2 + 2, char*);
1444 PL_Cmd = savepvn(cmd, s-cmd);
1446 for (s = PL_Cmd; *s;) {
1447 while (*s && isSPACE(*s)) s++;
1450 while (*s && !isSPACE(*s)) s++;
1456 PerlProc_execvp(PL_Argv[0],PL_Argv);
1457 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1464 if (ckWARN(WARN_EXEC))
1465 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1466 PL_Argv[0], Strerror(errno));
1468 PerlLIO_write(fd, (void*)&e, sizeof(int));
1477 #endif /* OS2 || WIN32 */
1480 Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
1484 register I32 tot = 0;
1487 SV **oldmark = mark;
1490 #define APPLY_TAINT_PROPER() \
1492 if (PL_tainted) { TAINT_PROPER(what); } \
1495 /* This is a first heuristic; it doesn't catch tainting magic. */
1497 while (++mark <= sp) {
1498 if (SvTAINTED(*mark)) {
1508 APPLY_TAINT_PROPER();
1511 APPLY_TAINT_PROPER();
1513 while (++mark <= sp) {
1514 char *name = SvPVx(*mark, n_a);
1515 APPLY_TAINT_PROPER();
1516 if (PerlLIO_chmod(name, val))
1524 APPLY_TAINT_PROPER();
1525 if (sp - mark > 2) {
1526 val = SvIVx(*++mark);
1527 val2 = SvIVx(*++mark);
1528 APPLY_TAINT_PROPER();
1530 while (++mark <= sp) {
1531 char *name = SvPVx(*mark, n_a);
1532 APPLY_TAINT_PROPER();
1533 if (PerlLIO_chown(name, val, val2))
1540 XXX Should we make lchown() directly available from perl?
1541 For now, we'll let Configure test for HAS_LCHOWN, but do
1542 nothing in the core.
1548 APPLY_TAINT_PROPER();
1551 s = SvPVx(*++mark, n_a);
1553 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1555 if (!(val = whichsig(s)))
1556 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
1560 APPLY_TAINT_PROPER();
1563 /* kill() doesn't do process groups (job trees?) under VMS */
1564 if (val < 0) val = -val;
1565 if (val == SIGKILL) {
1566 # include <starlet.h>
1567 /* Use native sys$delprc() to insure that target process is
1568 * deleted; supervisor-mode images don't pay attention to
1569 * CRTL's emulation of Unix-style signals and kill()
1571 while (++mark <= sp) {
1572 I32 proc = SvIVx(*mark);
1573 register unsigned long int __vmssts;
1574 APPLY_TAINT_PROPER();
1575 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1579 case SS$_NOSUCHNODE:
1580 SETERRNO(ESRCH,__vmssts);
1583 SETERRNO(EPERM,__vmssts);
1586 SETERRNO(EVMSERR,__vmssts);
1595 while (++mark <= sp) {
1596 I32 proc = SvIVx(*mark);
1597 APPLY_TAINT_PROPER();
1599 if (PerlProc_killpg(proc,val)) /* BSD */
1601 if (PerlProc_kill(-proc,val)) /* SYSV */
1607 while (++mark <= sp) {
1608 I32 proc = SvIVx(*mark);
1609 APPLY_TAINT_PROPER();
1610 if (PerlProc_kill(proc, val))
1618 APPLY_TAINT_PROPER();
1620 while (++mark <= sp) {
1621 s = SvPVx(*mark, n_a);
1622 APPLY_TAINT_PROPER();
1623 if (PL_euid || PL_unsafe) {
1627 else { /* don't let root wipe out directories without -U */
1628 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1640 APPLY_TAINT_PROPER();
1641 if (sp - mark > 2) {
1642 #if defined(I_UTIME) || defined(VMS)
1643 struct utimbuf utbuf;
1651 Zero(&utbuf, sizeof utbuf, char);
1653 utbuf.actime = (Time_t)SvNVx(*++mark); /* time accessed */
1654 utbuf.modtime = (Time_t)SvNVx(*++mark); /* time modified */
1656 utbuf.actime = (Time_t)SvIVx(*++mark); /* time accessed */
1657 utbuf.modtime = (Time_t)SvIVx(*++mark); /* time modified */
1659 APPLY_TAINT_PROPER();
1661 while (++mark <= sp) {
1662 char *name = SvPVx(*mark, n_a);
1663 APPLY_TAINT_PROPER();
1664 if (PerlLIO_utime(name, &utbuf))
1675 #undef APPLY_TAINT_PROPER
1678 /* Do the permissions allow some operation? Assumes statcache already set. */
1679 #ifndef VMS /* VMS' cando is in vms.c */
1681 Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register Stat_t *statbufp)
1682 /* Note: we use `effective' both for uids and gids.
1683 * Here we are betting on Uid_t being equal or wider than Gid_t. */
1686 /* [Comments and code from Len Reed]
1687 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1688 * to write-protected files. The execute permission bit is set
1689 * by the Miscrosoft C library stat() function for the following:
1694 * All files and directories are readable.
1695 * Directories and special files, e.g. "CON", cannot be
1697 * [Comment by Tom Dinger -- a directory can have the write-protect
1698 * bit set in the file system, but DOS permits changes to
1699 * the directory anyway. In addition, all bets are off
1700 * here for networked software, such as Novell and
1704 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1705 * too so it will actually look into the files for magic numbers
1707 return (mode & statbufp->st_mode) ? TRUE : FALSE;
1709 #else /* ! DOSISH */
1710 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
1711 if (mode == S_IXUSR) {
1712 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1716 return TRUE; /* root reads and writes anything */
1719 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1720 if (statbufp->st_mode & mode)
1721 return TRUE; /* ok as "user" */
1723 else if (ingroup(statbufp->st_gid,effective)) {
1724 if (statbufp->st_mode & mode >> 3)
1725 return TRUE; /* ok as "group" */
1727 else if (statbufp->st_mode & mode >> 6)
1728 return TRUE; /* ok as "other" */
1730 #endif /* ! DOSISH */
1735 Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective)
1737 #ifdef MACOS_TRADITIONAL
1738 /* This is simply not correct for AppleShare, but fix it yerself. */
1741 if (testgid == (effective ? PL_egid : PL_gid))
1743 #ifdef HAS_GETGROUPS
1748 Groups_t gary[NGROUPS];
1751 anum = getgroups(NGROUPS,gary);
1753 if (gary[anum] == testgid)
1761 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1764 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
1769 key = (key_t)SvNVx(*++mark);
1770 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1771 flags = SvIVx(*++mark);
1777 return msgget(key, flags);
1781 return semget(key, n, flags);
1785 return shmget(key, n, flags);
1787 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1789 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1792 return -1; /* should never happen */
1796 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
1800 I32 id, n, cmd, infosize, getinfo;
1803 id = SvIVx(*++mark);
1804 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1805 cmd = SvIVx(*++mark);
1808 getinfo = (cmd == IPC_STAT);
1814 if (cmd == IPC_STAT || cmd == IPC_SET)
1815 infosize = sizeof(struct msqid_ds);
1820 if (cmd == IPC_STAT || cmd == IPC_SET)
1821 infosize = sizeof(struct shmid_ds);
1827 if (cmd == IPC_STAT || cmd == IPC_SET)
1828 infosize = sizeof(struct semid_ds);
1829 else if (cmd == GETALL || cmd == SETALL)
1831 struct semid_ds semds;
1833 #ifdef EXTRA_F_IN_SEMUN_BUF
1834 semun.buff = &semds;
1838 getinfo = (cmd == GETALL);
1839 if (Semctl(id, 0, IPC_STAT, semun) == -1)
1841 infosize = semds.sem_nsems * sizeof(short);
1842 /* "short" is technically wrong but much more portable
1843 than guessing about u_?short(_t)? */
1846 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1850 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1852 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1861 SvPV_force(astr, len);
1862 a = SvGROW(astr, infosize+1);
1866 a = SvPV(astr, len);
1867 if (len != infosize)
1868 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
1877 a = INT2PTR(char *,i); /* ouch */
1884 ret = msgctl(id, cmd, (struct msqid_ds *)a);
1890 union semun unsemds;
1892 #ifdef EXTRA_F_IN_SEMUN_BUF
1893 unsemds.buff = (struct semid_ds *)a;
1895 unsemds.buf = (struct semid_ds *)a;
1897 ret = Semctl(id, n, cmd, unsemds);
1899 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1906 ret = shmctl(id, cmd, (struct shmid_ds *)a);
1910 if (getinfo && ret >= 0) {
1911 SvCUR_set(astr, infosize);
1912 *SvEND(astr) = '\0';
1919 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
1924 I32 id, msize, flags;
1927 id = SvIVx(*++mark);
1929 flags = SvIVx(*++mark);
1930 mbuf = SvPV(mstr, len);
1931 if ((msize = len - sizeof(long)) < 0)
1932 Perl_croak(aTHX_ "Arg too short for msgsnd");
1934 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
1936 Perl_croak(aTHX_ "msgsnd not implemented");
1941 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
1947 I32 id, msize, flags, ret;
1950 id = SvIVx(*++mark);
1952 /* suppress warning when reading into undef var --jhi */
1954 sv_setpvn(mstr, "", 0);
1955 msize = SvIVx(*++mark);
1956 mtype = (long)SvIVx(*++mark);
1957 flags = SvIVx(*++mark);
1958 SvPV_force(mstr, len);
1959 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
1962 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
1964 SvCUR_set(mstr, sizeof(long)+ret);
1965 *SvEND(mstr) = '\0';
1966 #ifndef INCOMPLETE_TAINTS
1967 /* who knows who has been playing with this message? */
1973 Perl_croak(aTHX_ "msgrcv not implemented");
1978 Perl_do_semop(pTHX_ SV **mark, SV **sp)
1986 id = SvIVx(*++mark);
1988 opbuf = SvPV(opstr, opsize);
1989 if (opsize < sizeof(struct sembuf)
1990 || (opsize % sizeof(struct sembuf)) != 0) {
1991 SETERRNO(EINVAL,LIB$_INVARG);
1995 return semop(id, (struct sembuf *)opbuf, opsize/sizeof(struct sembuf));
1997 Perl_croak(aTHX_ "semop not implemented");
2002 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
2007 I32 id, mpos, msize;
2009 struct shmid_ds shmds;
2011 id = SvIVx(*++mark);
2013 mpos = SvIVx(*++mark);
2014 msize = SvIVx(*++mark);
2016 if (shmctl(id, IPC_STAT, &shmds) == -1)
2018 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
2019 SETERRNO(EFAULT,SS$_ACCVIO); /* can't do as caller requested */
2022 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
2023 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2025 if (optype == OP_SHMREAD) {
2026 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2028 sv_setpvn(mstr, "", 0);
2029 SvPV_force(mstr, len);
2030 mbuf = SvGROW(mstr, msize+1);
2032 Copy(shm + mpos, mbuf, msize, char);
2033 SvCUR_set(mstr, msize);
2034 *SvEND(mstr) = '\0';
2036 #ifndef INCOMPLETE_TAINTS
2037 /* who knows who has been playing with this shared memory? */
2044 mbuf = SvPV(mstr, len);
2045 if ((n = len) > msize)
2047 Copy(mbuf, shm + mpos, n, char);
2049 memzero(shm + mpos + n, msize - n);
2053 Perl_croak(aTHX_ "shm I/O not implemented");
2057 #endif /* SYSV IPC */
2060 =for apidoc start_glob
2062 Function called by C<do_readline> to spawn a glob (or do the glob inside
2063 perl on VMS). This code used to be inline, but now perl uses C<File::Glob>
2064 this glob starter is only used by miniperl during the build proccess.
2065 Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up.
2071 Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
2073 SV *tmpcmd = NEWSV(55, 0);
2077 #ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
2078 /* since spawning off a process is a real performance hit */
2080 #include <descrip.h>
2081 #include <lib$routines.h>
2084 char rslt[NAM$C_MAXRSS+1+sizeof(unsigned short int)] = {'\0','\0'};
2085 char vmsspec[NAM$C_MAXRSS+1];
2086 char *rstr = rslt + sizeof(unsigned short int), *begin, *end, *cp;
2087 char tmpfnam[L_tmpnam] = "SYS$SCRATCH:";
2088 $DESCRIPTOR(dfltdsc,"SYS$DISK:[]*.*;");
2091 struct dsc$descriptor_s wilddsc
2092 = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
2093 struct dsc$descriptor_vs rsdsc
2094 = {sizeof rslt, DSC$K_DTYPE_VT, DSC$K_CLASS_VS, rslt};
2095 unsigned long int cxt = 0, sts = 0, ok = 1, hasdir = 0, hasver = 0, isunix = 0;
2097 /* We could find out if there's an explicit dev/dir or version
2098 by peeking into lib$find_file's internal context at
2099 ((struct NAM *)((struct FAB *)cxt)->fab$l_nam)->nam$l_fnb
2100 but that's unsupported, so I don't want to do it now and
2101 have it bite someone in the future. */
2102 strcat(tmpfnam,PerlLIO_tmpnam(NULL));
2103 cp = SvPV(tmpglob,i);
2105 if (cp[i] == ';') hasver = 1;
2107 if (sts) hasver = 1;
2111 hasdir = isunix = 1;
2114 if (cp[i] == ']' || cp[i] == '>' || cp[i] == ':') {
2119 if ((tmpfp = PerlIO_open(tmpfnam,"w+","fop=dlt")) != NULL) {
2121 if (!PerlLIO_stat(SvPVX(tmpglob),&st) && S_ISDIR(st.st_mode))
2122 ok = ((wilddsc.dsc$a_pointer = tovmspath(SvPVX(tmpglob),vmsspec)) != NULL);
2123 else ok = ((wilddsc.dsc$a_pointer = tovmsspec(SvPVX(tmpglob),vmsspec)) != NULL);
2124 if (ok) wilddsc.dsc$w_length = (unsigned short int) strlen(wilddsc.dsc$a_pointer);
2125 while (ok && ((sts = lib$find_file(&wilddsc,&rsdsc,&cxt,
2126 &dfltdsc,NULL,NULL,NULL))&1)) {
2127 end = rstr + (unsigned long int) *rslt;
2128 if (!hasver) while (*end != ';') end--;
2129 *(end++) = '\n'; *end = '\0';
2130 for (cp = rstr; *cp; cp++) *cp = _tolower(*cp);
2132 if (isunix) trim_unixpath(rstr,SvPVX(tmpglob),1);
2137 while (*(--begin) != ']' && *begin != '>') ;
2140 ok = (PerlIO_puts(tmpfp,begin) != EOF);
2142 if (cxt) (void)lib$find_file_end(&cxt);
2143 if (ok && sts != RMS$_NMF &&
2144 sts != RMS$_DNF && sts != RMS$_FNF) ok = 0;
2147 SETERRNO((sts == RMS$_SYN ? EINVAL : EVMSERR),sts);
2149 PerlIO_close(tmpfp);
2153 PerlIO_rewind(tmpfp);
2154 IoTYPE(io) = IoTYPE_RDONLY;
2155 IoIFP(io) = fp = tmpfp;
2156 IoFLAGS(io) &= ~IOf_UNTAINT; /* maybe redundant */
2161 #ifdef MACOS_TRADITIONAL
2162 sv_setpv(tmpcmd, "glob ");
2163 sv_catsv(tmpcmd, tmpglob);
2164 sv_catpv(tmpcmd, " |");
2168 sv_setpv(tmpcmd, "for a in ");
2169 sv_catsv(tmpcmd, tmpglob);
2170 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
2173 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
2174 sv_catsv(tmpcmd, tmpglob);
2176 sv_setpv(tmpcmd, "perlglob ");
2177 sv_catsv(tmpcmd, tmpglob);
2178 sv_catpv(tmpcmd, " |");
2183 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
2184 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
2185 sv_catsv(tmpcmd, tmpglob);
2186 sv_catpv(tmpcmd, "' 2>/dev/null |");
2188 sv_setpv(tmpcmd, "echo ");
2189 sv_catsv(tmpcmd, tmpglob);
2191 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
2193 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
2196 #endif /* !DOSISH */
2197 #endif /* MACOS_TRADITIONAL */
2198 (void)do_open(PL_last_in_gv, SvPVX(tmpcmd), SvCUR(tmpcmd),
2199 FALSE, O_RDONLY, 0, Nullfp);