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_open9(gv, name, len, as_raw, rawmode, rawperm,
59 supplied_fp, Nullsv, 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 register IO *io = GvIOn(gv);
68 PerlIO *saveifp = Nullfp;
69 PerlIO *saveofp = Nullfp;
70 char savetype = IoTYPE_CLOSED;
75 bool was_fdopen = FALSE;
76 bool in_raw = 0, in_crlf = 0, out_raw = 0, out_crlf = 0;
79 char mode[4]; /* stdio file mode ("r\0", "rb\0", "r+b\0" etc.) */
81 Zero(mode,sizeof(mode),char);
82 PL_forkprocess = 1; /* assume true if no fork */
84 /* Collect default raw/crlf info from the op */
85 if (PL_op && PL_op->op_type == OP_OPEN) {
86 /* set up disciplines */
87 U8 flags = PL_op->op_private;
88 in_raw = (flags & OPpOPEN_IN_RAW);
89 in_crlf = (flags & OPpOPEN_IN_CRLF);
90 out_raw = (flags & OPpOPEN_OUT_RAW);
91 out_crlf = (flags & OPpOPEN_OUT_CRLF);
94 /* If currently open - close before we re-open */
96 fd = PerlIO_fileno(IoIFP(io));
97 if (IoTYPE(io) == IoTYPE_STD)
99 else if (fd <= PL_maxsysfd) {
102 savetype = IoTYPE(io);
105 else if (IoTYPE(io) == IoTYPE_PIPE)
106 result = PerlProc_pclose(IoIFP(io));
107 else if (IoIFP(io) != IoOFP(io)) {
109 result = PerlIO_close(IoOFP(io));
110 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
113 result = PerlIO_close(IoIFP(io));
116 result = PerlIO_close(IoIFP(io));
117 if (result == EOF && fd > PL_maxsysfd)
118 PerlIO_printf(Perl_error_log,
119 "Warning: unable to close filehandle %s properly.\n",
121 IoOFP(io) = IoIFP(io) = Nullfp;
125 /* sysopen style args, i.e. integer mode and permissions */
127 #if defined(USE_64_BIT_RAWIO) && defined(O_LARGEFILE)
128 rawmode |= O_LARGEFILE;
132 #define O_ACCMODE 3 /* Assume traditional implementation */
135 switch (result = rawmode & O_ACCMODE) {
137 IoTYPE(io) = IoTYPE_RDONLY;
140 IoTYPE(io) = IoTYPE_WRONLY;
144 IoTYPE(io) = IoTYPE_RDWR;
148 writing = (result > 0);
149 fd = PerlLIO_open3(name, rawmode, rawperm);
155 if (result == O_RDONLY) {
159 else if (rawmode & O_APPEND) {
161 if (result != O_WRONLY)
166 if (result == O_WRONLY)
173 if (rawmode & O_BINARY)
176 fp = PerlIO_fdopen(fd, mode);
182 /* Regular (non-sys) open */
188 type = savepvn(name, len);
191 /* Loose trailing white space */
192 while (tend > type && isSPACE(tend[-1]))
195 /* New style explict name, type is just mode and discipline/layer info */
197 name = SvPV(svs, l) ;
199 name = savepvn(name, len);
202 for (; isSPACE(*type); type++) ;
209 if (*type == IoTYPE_RDWR && (!num_svs || tend > type+1 && tend[-1] != IoTYPE_PIPE)) { /* scary */
214 if (*type == IoTYPE_PIPE) {
216 if (type[1] != IoTYPE_STD) {
218 Perl_croak(aTHX_ "Unknown open() mode '%.*s'", (int)olen, oname);
223 for (type++; isSPACE(*type); type++) ;
228 if (*name == '\0') { /* command is missing 19990114 */
229 if (ckWARN(WARN_PIPE))
230 Perl_warner(aTHX_ WARN_PIPE, "Missing command in piped open");
234 if (strNE(name,"-") || num_svs)
236 TAINT_PROPER("piped open");
237 if (!num_svs && name[len-1] == '|') {
239 if (ckWARN(WARN_PIPE))
240 Perl_warner(aTHX_ WARN_PIPE, "Can't open bidirectional pipe");
248 fp = PerlProc_popen(name,mode);
250 else if (*type == IoTYPE_WRONLY) {
251 TAINT_PROPER("open");
253 if (*type == IoTYPE_WRONLY) {
254 /* Two IoTYPE_WRONLYs in a row make for an IoTYPE_APPEND. */
255 mode[0] = IoTYPE(io) = IoTYPE_APPEND;
278 if (!*name && supplied_fp)
282 for (; isSPACE(*name); name++) ;
287 gv = gv_fetchpv(name,FALSE,SVt_PVIO);
291 SETERRNO(EINVAL,SS$_IVCHAN);
296 PerlIO *fp = IoIFP(thatio);
297 /* Flush stdio buffer before dup. --mjd
298 * Unfortunately SEEK_CURing 0 seems to
299 * be optimized away on most platforms;
300 * only Solaris and Linux seem to flush
303 /* sfio fails to clear error on next
304 sfwrite, contrary to documentation.
306 if (PerlIO_seek(fp, 0, SEEK_CUR) == -1)
309 /* On the other hand, do all platforms
310 * take gracefully to flushing a read-only
311 * filehandle? Perhaps we should do
312 * fsetpos(src)+fgetpos(dst)? --nik */
314 fd = PerlIO_fileno(fp);
315 /* When dup()ing STDIN, STDOUT or STDERR
316 * explicitly set appropriate access mode */
317 if (IoIFP(thatio) == PerlIO_stdout()
318 || IoIFP(thatio) == PerlIO_stderr())
319 IoTYPE(io) = IoTYPE_WRONLY;
320 else if (IoIFP(thatio) == PerlIO_stdin())
321 IoTYPE(io) = IoTYPE_RDONLY;
322 /* When dup()ing a socket, say result is
324 else if (IoTYPE(thatio) == IoTYPE_SOCKET)
325 IoTYPE(io) = IoTYPE_SOCKET;
331 fd = PerlLIO_dup(fd);
334 if (!(fp = PerlIO_fdopen(fd,mode))) {
342 for (; isSPACE(*type); type++) ;
343 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
346 fp = PerlIO_stdout();
347 IoTYPE(io) = IoTYPE_STD;
350 fp = PerlIO_open((num_svs ? name : type), mode);
354 else if (*type == IoTYPE_RDONLY) {
356 for (type++; isSPACE(*type); type++) ;
367 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
371 IoTYPE(io) = IoTYPE_STD;
374 fp = PerlIO_open((num_svs ? name : type), mode);
376 else if ((num_svs && type[0] == IoTYPE_STD && type[1] == IoTYPE_PIPE) ||
377 (!num_svs && tend > type+1 && tend[-1] == IoTYPE_PIPE)) {
379 type += 2; /* skip over '-|' */
383 while (tend > type && isSPACE(tend[-1]))
386 for (; isSPACE(*type); type++) ;
390 if (*name == '\0') { /* command is missing 19990114 */
391 if (ckWARN(WARN_PIPE))
392 Perl_warner(aTHX_ WARN_PIPE, "Missing command in piped open");
396 if (strNE(name,"-") || num_svs)
398 TAINT_PROPER("piped open");
404 fp = PerlProc_popen(name,mode);
405 IoTYPE(io) = IoTYPE_PIPE;
411 IoTYPE(io) = IoTYPE_RDONLY;
413 for (; isSPACE(*name); name++) ;
419 if (strEQ(name,"-")) {
421 IoTYPE(io) = IoTYPE_STD;
424 fp = PerlIO_open(name,mode);
429 if (ckWARN(WARN_NEWLINE) && IoTYPE(io) == IoTYPE_RDONLY && strchr(name, '\n'))
430 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "open");
433 if (IoTYPE(io) && IoTYPE(io) != IoTYPE_PIPE && IoTYPE(io) != IoTYPE_STD) {
434 if (PerlLIO_fstat(PerlIO_fileno(fp),&PL_statbuf) < 0) {
435 (void)PerlIO_close(fp);
438 if (S_ISSOCK(PL_statbuf.st_mode))
439 IoTYPE(io) = IoTYPE_SOCKET; /* in case a socket was passed in to us */
443 !(PL_statbuf.st_mode & S_IFMT)
447 && IoTYPE(io) != IoTYPE_WRONLY /* Dups of STD* filehandles already have */
448 && IoTYPE(io) != IoTYPE_RDONLY /* type so they aren't marked as sockets */
449 ) { /* on OS's that return 0 on fstat()ed pipe */
451 Sock_size_t buflen = sizeof tmpbuf;
452 if (PerlSock_getsockname(PerlIO_fileno(fp), (struct sockaddr *)tmpbuf,
454 || errno != ENOTSOCK)
455 IoTYPE(io) = IoTYPE_SOCKET; /* some OS's return 0 on fstat()ed socket */
456 /* but some return 0 for streams too, sigh */
460 if (saveifp) { /* must use old fp? */
461 /* If fd is less that PL_maxsysfd i.e. STDIN..STDERR
462 then dup the new fileno down
464 fd = PerlIO_fileno(saveifp);
466 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
467 if (saveofp != saveifp) { /* was a socket? */
468 PerlIO_close(saveofp);
469 /* This looks very suspect - NI-S 24 Nov 2000 */
471 Safefree(saveofp); /* ??? */
474 if (fd != PerlIO_fileno(fp)) {
478 PerlLIO_dup2(PerlIO_fileno(fp), fd);
480 if (fd != PerlIO_fileno(PerlIO_stdin())) {
481 char newname[FILENAME_MAX+1];
482 if (fgetname(fp, newname)) {
483 if (fd == PerlIO_fileno(PerlIO_stdout())) Perl_vmssetuserlnm("SYS$OUTPUT", newname);
484 if (fd == PerlIO_fileno(PerlIO_stderr())) Perl_vmssetuserlnm("SYS$ERROR", newname);
489 sv = *av_fetch(PL_fdpid,PerlIO_fileno(fp),TRUE);
490 (void)SvUPGRADE(sv, SVt_IV);
493 sv = *av_fetch(PL_fdpid,fd,TRUE);
495 (void)SvUPGRADE(sv, SVt_IV);
504 #if defined(HAS_FCNTL) && defined(F_SETFD)
506 int save_errno = errno;
507 fd = PerlIO_fileno(fp);
508 fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
514 /* Need to supply default type info from open.pm */
515 SV *layers = PL_curcop->cop_io;
519 type = SvPV(layers,len);
520 if (type && mode[0] != 'r') {
521 /* Skip to write part */
522 char *s = strchr(type,0);
523 if (s && (s-type) < len) {
530 while (isSPACE(*type)) type++;
532 if (PerlIO_apply_layers(aTHX_ IoIFP(io),mode,type) != 0) {
538 IoFLAGS(io) &= ~IOf_NOLINE;
540 if (IoTYPE(io) == IoTYPE_SOCKET
541 || (IoTYPE(io) == IoTYPE_WRONLY && S_ISCHR(PL_statbuf.st_mode)) )
544 if (!(IoOFP(io) = PerlIO_fdopen(PerlIO_fileno(fp),mode))) {
550 if (PerlIO_apply_layers(aTHX_ IoOFP(io),mode,type) != 0) {
551 PerlIO_close(IoOFP(io));
567 IoTYPE(io) = savetype;
572 Perl_nextargv(pTHX_ register GV *gv)
575 #ifndef FLEXFILENAMES
584 PL_argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO);
585 if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
586 IoFLAGS(io) &= ~IOf_START;
588 if (!PL_argvout_stack)
589 PL_argvout_stack = newAV();
590 av_push(PL_argvout_stack, SvREFCNT_inc(PL_defoutgv));
593 if (PL_filemode & (S_ISUID|S_ISGID)) {
594 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
596 (void)fchmod(PL_lastfd,PL_filemode);
598 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
602 while (av_len(GvAV(gv)) >= 0) {
604 sv = av_shift(GvAV(gv));
606 sv_setsv(GvSV(gv),sv);
607 SvSETMAGIC(GvSV(gv));
608 PL_oldname = SvPVx(GvSV(gv), oldlen);
609 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,Nullfp)) {
611 TAINT_PROPER("inplace open");
612 if (oldlen == 1 && *PL_oldname == '-') {
613 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
614 return IoIFP(GvIOp(gv));
616 #ifndef FLEXFILENAMES
617 filedev = PL_statbuf.st_dev;
618 fileino = PL_statbuf.st_ino;
620 PL_filemode = PL_statbuf.st_mode;
621 fileuid = PL_statbuf.st_uid;
622 filegid = PL_statbuf.st_gid;
623 if (!S_ISREG(PL_filemode)) {
624 if (ckWARN_d(WARN_INPLACE))
625 Perl_warner(aTHX_ WARN_INPLACE,
626 "Can't do inplace edit: %s is not a regular file",
632 char *star = strchr(PL_inplace, '*');
634 char *begin = PL_inplace;
635 sv_setpvn(sv, "", 0);
637 sv_catpvn(sv, begin, star - begin);
638 sv_catpvn(sv, PL_oldname, oldlen);
640 } while ((star = strchr(begin, '*')));
645 sv_catpv(sv,PL_inplace);
647 #ifndef FLEXFILENAMES
648 if (PerlLIO_stat(SvPVX(sv),&PL_statbuf) >= 0
649 && PL_statbuf.st_dev == filedev
650 && PL_statbuf.st_ino == fileino
652 || (_djstat_fail_bits & _STFAIL_TRUENAME)!=0
656 if (ckWARN_d(WARN_INPLACE))
657 Perl_warner(aTHX_ WARN_INPLACE,
658 "Can't do inplace edit: %s would not be unique",
665 #if !defined(DOSISH) && !defined(__CYGWIN__)
666 if (PerlLIO_rename(PL_oldname,SvPVX(sv)) < 0) {
667 if (ckWARN_d(WARN_INPLACE))
668 Perl_warner(aTHX_ WARN_INPLACE,
669 "Can't rename %s to %s: %s, skipping file",
670 PL_oldname, SvPVX(sv), Strerror(errno) );
676 (void)PerlLIO_unlink(SvPVX(sv));
677 (void)PerlLIO_rename(PL_oldname,SvPVX(sv));
678 do_open(gv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,O_RDONLY,0,Nullfp);
681 (void)UNLINK(SvPVX(sv));
682 if (link(PL_oldname,SvPVX(sv)) < 0) {
683 if (ckWARN_d(WARN_INPLACE))
684 Perl_warner(aTHX_ WARN_INPLACE,
685 "Can't rename %s to %s: %s, skipping file",
686 PL_oldname, SvPVX(sv), Strerror(errno) );
690 (void)UNLINK(PL_oldname);
694 #if !defined(DOSISH) && !defined(AMIGAOS)
695 # ifndef VMS /* Don't delete; use automatic file versioning */
696 if (UNLINK(PL_oldname) < 0) {
697 if (ckWARN_d(WARN_INPLACE))
698 Perl_warner(aTHX_ WARN_INPLACE,
699 "Can't remove %s: %s, skipping file",
700 PL_oldname, Strerror(errno) );
706 Perl_croak(aTHX_ "Can't do inplace edit without backup");
710 sv_setpvn(sv,">",!PL_inplace);
711 sv_catpvn(sv,PL_oldname,oldlen);
712 SETERRNO(0,0); /* in case sprintf set errno */
714 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
715 O_WRONLY|O_CREAT|O_TRUNC,0,Nullfp))
717 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
718 O_WRONLY|O_CREAT|OPEN_EXCL,0666,Nullfp))
721 if (ckWARN_d(WARN_INPLACE))
722 Perl_warner(aTHX_ WARN_INPLACE, "Can't do inplace edit on %s: %s",
723 PL_oldname, Strerror(errno) );
727 setdefout(PL_argvoutgv);
728 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
729 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
731 (void)fchmod(PL_lastfd,PL_filemode);
733 # if !(defined(WIN32) && defined(__BORLANDC__))
734 /* Borland runtime creates a readonly file! */
735 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
738 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
740 (void)fchown(PL_lastfd,fileuid,filegid);
743 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
748 return IoIFP(GvIOp(gv));
751 if (ckWARN_d(WARN_INPLACE)) {
753 if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0
754 && !S_ISREG(PL_statbuf.st_mode))
756 Perl_warner(aTHX_ WARN_INPLACE,
757 "Can't do inplace edit: %s is not a regular file",
761 Perl_warner(aTHX_ WARN_INPLACE, "Can't open %s: %s",
762 PL_oldname, Strerror(eno));
766 if (io && (IoFLAGS(io) & IOf_ARGV))
767 IoFLAGS(io) |= IOf_START;
769 (void)do_close(PL_argvoutgv,FALSE);
770 if (io && (IoFLAGS(io) & IOf_ARGV)
771 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
773 GV *oldout = (GV*)av_pop(PL_argvout_stack);
775 SvREFCNT_dec(oldout);
778 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
785 Perl_do_pipe(pTHX_ SV *sv, GV *rgv, GV *wgv)
804 if (PerlProc_pipe(fd) < 0)
806 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r");
807 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w");
808 IoIFP(wstio) = IoOFP(wstio);
809 IoTYPE(rstio) = IoTYPE_RDONLY;
810 IoTYPE(wstio) = IoTYPE_WRONLY;
811 if (!IoIFP(rstio) || !IoOFP(wstio)) {
812 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
813 else PerlLIO_close(fd[0]);
814 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
815 else PerlLIO_close(fd[1]);
819 sv_setsv(sv,&PL_sv_yes);
823 sv_setsv(sv,&PL_sv_undef);
828 /* explicit renamed to avoid C++ conflict -- kja */
830 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
837 if (!gv || SvTYPE(gv) != SVt_PVGV) {
839 SETERRNO(EBADF,SS$_IVCHAN);
843 if (!io) { /* never opened */
845 if (ckWARN(WARN_UNOPENED)) /* no check for closed here */
846 report_evil_fh(gv, io, PL_op->op_type);
847 SETERRNO(EBADF,SS$_IVCHAN);
851 retval = io_close(io, not_implicit);
855 IoLINES_LEFT(io) = IoPAGE_LEN(io);
857 IoTYPE(io) = IoTYPE_CLOSED;
862 Perl_io_close(pTHX_ IO *io, bool not_implicit)
868 if (IoTYPE(io) == IoTYPE_PIPE) {
869 status = PerlProc_pclose(IoIFP(io));
871 STATUS_NATIVE_SET(status);
872 retval = (STATUS_POSIX == 0);
875 retval = (status != -1);
878 else if (IoTYPE(io) == IoTYPE_STD)
881 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
882 retval = (PerlIO_close(IoOFP(io)) != EOF);
883 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
886 retval = (PerlIO_close(IoIFP(io)) != EOF);
888 IoOFP(io) = IoIFP(io) = Nullfp;
890 else if (not_implicit) {
891 SETERRNO(EBADF,SS$_IVCHAN);
898 Perl_do_eof(pTHX_ GV *gv)
907 else if (ckWARN(WARN_IO)
908 && (IoTYPE(io) == IoTYPE_WRONLY || IoIFP(io) == PerlIO_stdout()
909 || IoIFP(io) == PerlIO_stderr()))
911 /* integrate to report_evil_fh()? */
914 SV* sv = sv_newmortal();
915 gv_efullname4(sv, gv, Nullch, FALSE);
916 name = SvPV_nolen(sv);
919 Perl_warner(aTHX_ WARN_IO,
920 "Filehandle %s opened only for output", name);
922 Perl_warner(aTHX_ WARN_IO,
923 "Filehandle opened only for output");
928 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
929 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
930 return FALSE; /* this is the most usual case */
933 ch = PerlIO_getc(IoIFP(io));
935 (void)PerlIO_ungetc(IoIFP(io),ch);
939 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
940 if (PerlIO_get_cnt(IoIFP(io)) < -1)
941 PerlIO_set_cnt(IoIFP(io),-1);
943 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
944 if (!nextargv(PL_argvgv)) /* get another fp handy */
948 return TRUE; /* normal fp, definitely end of file */
954 Perl_do_tell(pTHX_ GV *gv)
959 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
960 #ifdef ULTRIX_STDIO_BOTCH
962 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
964 return PerlIO_tell(fp);
966 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
967 report_evil_fh(gv, io, PL_op->op_type);
968 SETERRNO(EBADF,RMS$_IFI);
973 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
978 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
979 #ifdef ULTRIX_STDIO_BOTCH
981 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
983 return PerlIO_seek(fp, pos, whence) >= 0;
985 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
986 report_evil_fh(gv, io, PL_op->op_type);
987 SETERRNO(EBADF,RMS$_IFI);
992 Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
997 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
998 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
999 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1000 report_evil_fh(gv, io, PL_op->op_type);
1001 SETERRNO(EBADF,RMS$_IFI);
1006 Perl_mode_from_discipline(pTHX_ SV *discp)
1008 int mode = O_BINARY;
1011 char *s = SvPV(discp,len);
1016 if (len > 3 && strnEQ(s+1, "raw", 3)
1017 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1026 if (len > 4 && strnEQ(s+1, "crlf", 4)
1027 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1036 goto fail_discipline;
1039 else if (isSPACE(*s)) {
1046 end = strchr(s+1, ':');
1049 #ifndef PERLIO_LAYERS
1050 Perl_croak(aTHX_ "Unknown discipline '%.*s'", end-s, s);
1061 Perl_do_binmode(pTHX_ PerlIO *fp, int iotype, int mode)
1063 /* The old body of this is now in non-LAYER part of perlio.c
1064 * This is a stub for any XS code which might have been calling it.
1066 char *name = (O_BINARY != O_TEXT && !(mode & O_BINARY)) ? ":crlf" : ":raw";
1067 return PerlIO_binmode(aTHX_ fp, iotype, mode, name);
1070 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP)
1071 /* code courtesy of William Kucharski */
1074 I32 my_chsize(fd, length)
1075 I32 fd; /* file descriptor */
1076 Off_t length; /* length to set file to */
1079 struct stat filebuf;
1081 if (PerlLIO_fstat(fd, &filebuf) < 0)
1084 if (filebuf.st_size < length) {
1086 /* extend file length */
1088 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
1091 /* write a "0" byte */
1093 if ((PerlLIO_write(fd, "", 1)) != 1)
1097 /* truncate length */
1101 fl.l_start = length;
1102 fl.l_type = F_WRLCK; /* write lock on file space */
1105 * This relies on the UNDOCUMENTED F_FREESP argument to
1106 * fcntl(2), which truncates the file so that it ends at the
1107 * position indicated by fl.l_start.
1109 * Will minor miracles never cease?
1112 if (fcntl(fd, F_FREESP, &fl) < 0)
1119 #endif /* F_FREESP */
1122 Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
1124 register char *tmps;
1127 /* assuming fp is checked earlier */
1133 if (SvIOK(sv) && SvIVX(sv) != 0) {
1134 PerlIO_printf(fp, PL_ofmt, (NV)SvIVX(sv));
1135 return !PerlIO_error(fp);
1137 if ( (SvNOK(sv) && SvNVX(sv) != 0.0)
1138 || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) {
1139 PerlIO_printf(fp, PL_ofmt, SvNVX(sv));
1140 return !PerlIO_error(fp);
1143 switch (SvTYPE(sv)) {
1145 if (ckWARN(WARN_UNINITIALIZED))
1153 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
1155 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
1156 return !PerlIO_error(fp);
1160 if (PerlIO_isutf8(fp)) {
1161 tmps = SvPVutf8(sv, len);
1165 sv_utf8_downgrade(sv, FALSE);
1166 tmps = SvPV(sv, len);
1170 /* To detect whether the process is about to overstep its
1171 * filesize limit we would need getrlimit(). We could then
1172 * also transparently raise the limit with setrlimit() --
1173 * but only until the system hard limit/the filesystem limit,
1174 * at which we would get EPERM. Note that when using buffered
1175 * io the write failure can be delayed until the flush/close. --jhi */
1176 if (len && (PerlIO_write(fp,tmps,len) == 0))
1178 return !PerlIO_error(fp);
1188 if (PL_op->op_flags & OPf_REF) {
1193 if (io && IoIFP(io)) {
1195 sv_setpv(PL_statname,"");
1196 PL_laststype = OP_STAT;
1197 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
1201 return PL_laststatval;
1202 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1203 report_evil_fh(gv, io, PL_op->op_type);
1205 sv_setpv(PL_statname,"");
1206 return (PL_laststatval = -1);
1214 if (SvTYPE(sv) == SVt_PVGV) {
1218 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
1225 sv_setpv(PL_statname, s);
1226 PL_laststype = OP_STAT;
1227 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1228 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
1229 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "stat");
1230 return PL_laststatval;
1240 if (PL_op->op_flags & OPf_REF) {
1242 if (cGVOP_gv == PL_defgv) {
1243 if (PL_laststype != OP_LSTAT)
1244 Perl_croak(aTHX_ "The stat preceding -l _ wasn't an lstat");
1245 return PL_laststatval;
1247 Perl_croak(aTHX_ "You can't use -l on a filehandle");
1250 PL_laststype = OP_LSTAT;
1254 sv_setpv(PL_statname,SvPV(sv, n_a));
1255 PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache);
1256 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
1257 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "lstat");
1258 return PL_laststatval;
1262 Perl_do_aexec(pTHX_ SV *really, register SV **mark, register SV **sp)
1264 return do_aexec5(really, mark, sp, 0, 0);
1268 Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1269 int fd, int do_report)
1271 #ifdef MACOS_TRADITIONAL
1272 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1279 New(401,PL_Argv, sp - mark + 1, char*);
1281 while (++mark <= sp) {
1283 *a++ = SvPVx(*mark, n_a);
1288 if (*PL_Argv[0] != '/') /* will execvp use PATH? */
1289 TAINT_ENV(); /* testing IFS here is overkill, probably */
1290 if (really && *(tmps = SvPV(really, n_a)))
1291 PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
1293 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1294 if (ckWARN(WARN_EXEC))
1295 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1296 PL_Argv[0], Strerror(errno));
1300 PerlLIO_write(fd, (void*)&e, sizeof(int));
1310 Perl_do_execfree(pTHX)
1314 PL_Argv = Null(char **);
1322 #if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC) && !defined(MACOS_TRADITIONAL)
1325 Perl_do_exec(pTHX_ char *cmd)
1327 return do_exec3(cmd,0,0);
1331 Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report)
1337 while (*cmd && isSPACE(*cmd))
1340 /* save an extra exec if possible */
1343 if (strnEQ(cmd,PL_cshname,PL_cshlen) && strnEQ(cmd+PL_cshlen," -c",3)) {
1345 s = cmd+PL_cshlen+3;
1359 if (s[-1] == '\'') {
1361 PerlProc_execl(PL_cshname,"csh", flags,ncmd,(char*)0);
1369 /* see if there are shell metacharacters in it */
1371 if (*cmd == '.' && isSPACE(cmd[1]))
1374 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1377 for (s = cmd; *s && isALNUM(*s); s++) ; /* catch VAR=val gizmo */
1381 for (s = cmd; *s; s++) {
1382 if (*s != ' ' && !isALPHA(*s) && strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1383 if (*s == '\n' && !s[1]) {
1387 /* handle the 2>&1 construct at the end */
1388 if (*s == '>' && s[1] == '&' && s[2] == '1'
1389 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1390 && (!s[3] || isSPACE(s[3])))
1394 while (*t && isSPACE(*t))
1396 if (!*t && (dup2(1,2) != -1)) {
1402 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
1407 New(402,PL_Argv, (s - cmd) / 2 + 2, char*);
1408 PL_Cmd = savepvn(cmd, s-cmd);
1410 for (s = PL_Cmd; *s;) {
1411 while (*s && isSPACE(*s)) s++;
1414 while (*s && !isSPACE(*s)) s++;
1420 PerlProc_execvp(PL_Argv[0],PL_Argv);
1421 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1428 if (ckWARN(WARN_EXEC))
1429 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1430 PL_Argv[0], Strerror(errno));
1432 PerlLIO_write(fd, (void*)&e, sizeof(int));
1441 #endif /* OS2 || WIN32 */
1444 Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
1448 register I32 tot = 0;
1451 SV **oldmark = mark;
1454 #define APPLY_TAINT_PROPER() \
1456 if (PL_tainted) { TAINT_PROPER(what); } \
1459 /* This is a first heuristic; it doesn't catch tainting magic. */
1461 while (++mark <= sp) {
1462 if (SvTAINTED(*mark)) {
1472 APPLY_TAINT_PROPER();
1475 APPLY_TAINT_PROPER();
1477 while (++mark <= sp) {
1478 char *name = SvPVx(*mark, n_a);
1479 APPLY_TAINT_PROPER();
1480 if (PerlLIO_chmod(name, val))
1488 APPLY_TAINT_PROPER();
1489 if (sp - mark > 2) {
1490 val = SvIVx(*++mark);
1491 val2 = SvIVx(*++mark);
1492 APPLY_TAINT_PROPER();
1494 while (++mark <= sp) {
1495 char *name = SvPVx(*mark, n_a);
1496 APPLY_TAINT_PROPER();
1497 if (PerlLIO_chown(name, val, val2))
1504 XXX Should we make lchown() directly available from perl?
1505 For now, we'll let Configure test for HAS_LCHOWN, but do
1506 nothing in the core.
1512 APPLY_TAINT_PROPER();
1515 s = SvPVx(*++mark, n_a);
1517 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1519 if (!(val = whichsig(s)))
1520 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
1524 APPLY_TAINT_PROPER();
1527 /* kill() doesn't do process groups (job trees?) under VMS */
1528 if (val < 0) val = -val;
1529 if (val == SIGKILL) {
1530 # include <starlet.h>
1531 /* Use native sys$delprc() to insure that target process is
1532 * deleted; supervisor-mode images don't pay attention to
1533 * CRTL's emulation of Unix-style signals and kill()
1535 while (++mark <= sp) {
1536 I32 proc = SvIVx(*mark);
1537 register unsigned long int __vmssts;
1538 APPLY_TAINT_PROPER();
1539 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1543 case SS$_NOSUCHNODE:
1544 SETERRNO(ESRCH,__vmssts);
1547 SETERRNO(EPERM,__vmssts);
1550 SETERRNO(EVMSERR,__vmssts);
1559 while (++mark <= sp) {
1560 I32 proc = SvIVx(*mark);
1561 APPLY_TAINT_PROPER();
1563 if (PerlProc_killpg(proc,val)) /* BSD */
1565 if (PerlProc_kill(-proc,val)) /* SYSV */
1571 while (++mark <= sp) {
1572 I32 proc = SvIVx(*mark);
1573 APPLY_TAINT_PROPER();
1574 if (PerlProc_kill(proc, val))
1582 APPLY_TAINT_PROPER();
1584 while (++mark <= sp) {
1585 s = SvPVx(*mark, n_a);
1586 APPLY_TAINT_PROPER();
1587 if (PL_euid || PL_unsafe) {
1591 else { /* don't let root wipe out directories without -U */
1592 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1604 APPLY_TAINT_PROPER();
1605 if (sp - mark > 2) {
1606 #if defined(I_UTIME) || defined(VMS)
1607 struct utimbuf utbuf;
1615 Zero(&utbuf, sizeof utbuf, char);
1617 utbuf.actime = (Time_t)SvNVx(*++mark); /* time accessed */
1618 utbuf.modtime = (Time_t)SvNVx(*++mark); /* time modified */
1620 utbuf.actime = (Time_t)SvIVx(*++mark); /* time accessed */
1621 utbuf.modtime = (Time_t)SvIVx(*++mark); /* time modified */
1623 APPLY_TAINT_PROPER();
1625 while (++mark <= sp) {
1626 char *name = SvPVx(*mark, n_a);
1627 APPLY_TAINT_PROPER();
1628 if (PerlLIO_utime(name, &utbuf))
1639 #undef APPLY_TAINT_PROPER
1642 /* Do the permissions allow some operation? Assumes statcache already set. */
1643 #ifndef VMS /* VMS' cando is in vms.c */
1645 Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register Stat_t *statbufp)
1646 /* Note: we use `effective' both for uids and gids.
1647 * Here we are betting on Uid_t being equal or wider than Gid_t. */
1650 /* [Comments and code from Len Reed]
1651 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1652 * to write-protected files. The execute permission bit is set
1653 * by the Miscrosoft C library stat() function for the following:
1658 * All files and directories are readable.
1659 * Directories and special files, e.g. "CON", cannot be
1661 * [Comment by Tom Dinger -- a directory can have the write-protect
1662 * bit set in the file system, but DOS permits changes to
1663 * the directory anyway. In addition, all bets are off
1664 * here for networked software, such as Novell and
1668 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1669 * too so it will actually look into the files for magic numbers
1671 return (mode & statbufp->st_mode) ? TRUE : FALSE;
1673 #else /* ! DOSISH */
1674 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
1675 if (mode == S_IXUSR) {
1676 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1680 return TRUE; /* root reads and writes anything */
1683 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1684 if (statbufp->st_mode & mode)
1685 return TRUE; /* ok as "user" */
1687 else if (ingroup(statbufp->st_gid,effective)) {
1688 if (statbufp->st_mode & mode >> 3)
1689 return TRUE; /* ok as "group" */
1691 else if (statbufp->st_mode & mode >> 6)
1692 return TRUE; /* ok as "other" */
1694 #endif /* ! DOSISH */
1699 Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective)
1701 #ifdef MACOS_TRADITIONAL
1702 /* This is simply not correct for AppleShare, but fix it yerself. */
1705 if (testgid == (effective ? PL_egid : PL_gid))
1707 #ifdef HAS_GETGROUPS
1712 Groups_t gary[NGROUPS];
1715 anum = getgroups(NGROUPS,gary);
1717 if (gary[anum] == testgid)
1725 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1728 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
1733 key = (key_t)SvNVx(*++mark);
1734 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1735 flags = SvIVx(*++mark);
1741 return msgget(key, flags);
1745 return semget(key, n, flags);
1749 return shmget(key, n, flags);
1751 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1753 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1756 return -1; /* should never happen */
1760 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
1764 I32 id, n, cmd, infosize, getinfo;
1767 id = SvIVx(*++mark);
1768 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1769 cmd = SvIVx(*++mark);
1772 getinfo = (cmd == IPC_STAT);
1778 if (cmd == IPC_STAT || cmd == IPC_SET)
1779 infosize = sizeof(struct msqid_ds);
1784 if (cmd == IPC_STAT || cmd == IPC_SET)
1785 infosize = sizeof(struct shmid_ds);
1791 if (cmd == IPC_STAT || cmd == IPC_SET)
1792 infosize = sizeof(struct semid_ds);
1793 else if (cmd == GETALL || cmd == SETALL)
1795 struct semid_ds semds;
1797 #ifdef EXTRA_F_IN_SEMUN_BUF
1798 semun.buff = &semds;
1802 getinfo = (cmd == GETALL);
1803 if (Semctl(id, 0, IPC_STAT, semun) == -1)
1805 infosize = semds.sem_nsems * sizeof(short);
1806 /* "short" is technically wrong but much more portable
1807 than guessing about u_?short(_t)? */
1810 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1814 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1816 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1825 SvPV_force(astr, len);
1826 a = SvGROW(astr, infosize+1);
1830 a = SvPV(astr, len);
1831 if (len != infosize)
1832 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
1841 a = INT2PTR(char *,i); /* ouch */
1848 ret = msgctl(id, cmd, (struct msqid_ds *)a);
1854 union semun unsemds;
1856 #ifdef EXTRA_F_IN_SEMUN_BUF
1857 unsemds.buff = (struct semid_ds *)a;
1859 unsemds.buf = (struct semid_ds *)a;
1861 ret = Semctl(id, n, cmd, unsemds);
1863 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1870 ret = shmctl(id, cmd, (struct shmid_ds *)a);
1874 if (getinfo && ret >= 0) {
1875 SvCUR_set(astr, infosize);
1876 *SvEND(astr) = '\0';
1883 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
1888 I32 id, msize, flags;
1891 id = SvIVx(*++mark);
1893 flags = SvIVx(*++mark);
1894 mbuf = SvPV(mstr, len);
1895 if ((msize = len - sizeof(long)) < 0)
1896 Perl_croak(aTHX_ "Arg too short for msgsnd");
1898 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
1900 Perl_croak(aTHX_ "msgsnd not implemented");
1905 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
1911 I32 id, msize, flags, ret;
1914 id = SvIVx(*++mark);
1916 /* suppress warning when reading into undef var --jhi */
1918 sv_setpvn(mstr, "", 0);
1919 msize = SvIVx(*++mark);
1920 mtype = (long)SvIVx(*++mark);
1921 flags = SvIVx(*++mark);
1922 SvPV_force(mstr, len);
1923 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
1926 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
1928 SvCUR_set(mstr, sizeof(long)+ret);
1929 *SvEND(mstr) = '\0';
1930 #ifndef INCOMPLETE_TAINTS
1931 /* who knows who has been playing with this message? */
1937 Perl_croak(aTHX_ "msgrcv not implemented");
1942 Perl_do_semop(pTHX_ SV **mark, SV **sp)
1950 id = SvIVx(*++mark);
1952 opbuf = SvPV(opstr, opsize);
1953 if (opsize < sizeof(struct sembuf)
1954 || (opsize % sizeof(struct sembuf)) != 0) {
1955 SETERRNO(EINVAL,LIB$_INVARG);
1959 return semop(id, (struct sembuf *)opbuf, opsize/sizeof(struct sembuf));
1961 Perl_croak(aTHX_ "semop not implemented");
1966 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
1971 I32 id, mpos, msize;
1973 struct shmid_ds shmds;
1975 id = SvIVx(*++mark);
1977 mpos = SvIVx(*++mark);
1978 msize = SvIVx(*++mark);
1980 if (shmctl(id, IPC_STAT, &shmds) == -1)
1982 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
1983 SETERRNO(EFAULT,SS$_ACCVIO); /* can't do as caller requested */
1986 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
1987 if (shm == (char *)-1) /* I hate System V IPC, I really do */
1989 if (optype == OP_SHMREAD) {
1990 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
1992 sv_setpvn(mstr, "", 0);
1993 SvPV_force(mstr, len);
1994 mbuf = SvGROW(mstr, msize+1);
1996 Copy(shm + mpos, mbuf, msize, char);
1997 SvCUR_set(mstr, msize);
1998 *SvEND(mstr) = '\0';
2000 #ifndef INCOMPLETE_TAINTS
2001 /* who knows who has been playing with this shared memory? */
2008 mbuf = SvPV(mstr, len);
2009 if ((n = len) > msize)
2011 Copy(mbuf, shm + mpos, n, char);
2013 memzero(shm + mpos + n, msize - n);
2017 Perl_croak(aTHX_ "shm I/O not implemented");
2021 #endif /* SYSV IPC */
2024 =for apidoc start_glob
2026 Function called by C<do_readline> to spawn a glob (or do the glob inside
2027 perl on VMS). This code used to be inline, but now perl uses C<File::Glob>
2028 this glob starter is only used by miniperl during the build proccess.
2029 Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up.
2035 Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
2037 SV *tmpcmd = NEWSV(55, 0);
2041 #ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
2042 /* since spawning off a process is a real performance hit */
2044 #include <descrip.h>
2045 #include <lib$routines.h>
2048 char rslt[NAM$C_MAXRSS+1+sizeof(unsigned short int)] = {'\0','\0'};
2049 char vmsspec[NAM$C_MAXRSS+1];
2050 char *rstr = rslt + sizeof(unsigned short int), *begin, *end, *cp;
2051 char tmpfnam[L_tmpnam] = "SYS$SCRATCH:";
2052 $DESCRIPTOR(dfltdsc,"SYS$DISK:[]*.*;");
2055 struct dsc$descriptor_s wilddsc
2056 = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
2057 struct dsc$descriptor_vs rsdsc
2058 = {sizeof rslt, DSC$K_DTYPE_VT, DSC$K_CLASS_VS, rslt};
2059 unsigned long int cxt = 0, sts = 0, ok = 1, hasdir = 0, hasver = 0, isunix = 0;
2061 /* We could find out if there's an explicit dev/dir or version
2062 by peeking into lib$find_file's internal context at
2063 ((struct NAM *)((struct FAB *)cxt)->fab$l_nam)->nam$l_fnb
2064 but that's unsupported, so I don't want to do it now and
2065 have it bite someone in the future. */
2066 strcat(tmpfnam,PerlLIO_tmpnam(NULL));
2067 cp = SvPV(tmpglob,i);
2069 if (cp[i] == ';') hasver = 1;
2071 if (sts) hasver = 1;
2075 hasdir = isunix = 1;
2078 if (cp[i] == ']' || cp[i] == '>' || cp[i] == ':') {
2083 if ((tmpfp = PerlIO_open(tmpfnam,"w+","fop=dlt")) != NULL) {
2085 if (!PerlLIO_stat(SvPVX(tmpglob),&st) && S_ISDIR(st.st_mode))
2086 ok = ((wilddsc.dsc$a_pointer = tovmspath(SvPVX(tmpglob),vmsspec)) != NULL);
2087 else ok = ((wilddsc.dsc$a_pointer = tovmsspec(SvPVX(tmpglob),vmsspec)) != NULL);
2088 if (ok) wilddsc.dsc$w_length = (unsigned short int) strlen(wilddsc.dsc$a_pointer);
2089 while (ok && ((sts = lib$find_file(&wilddsc,&rsdsc,&cxt,
2090 &dfltdsc,NULL,NULL,NULL))&1)) {
2091 end = rstr + (unsigned long int) *rslt;
2092 if (!hasver) while (*end != ';') end--;
2093 *(end++) = '\n'; *end = '\0';
2094 for (cp = rstr; *cp; cp++) *cp = _tolower(*cp);
2096 if (isunix) trim_unixpath(rstr,SvPVX(tmpglob),1);
2101 while (*(--begin) != ']' && *begin != '>') ;
2104 ok = (PerlIO_puts(tmpfp,begin) != EOF);
2106 if (cxt) (void)lib$find_file_end(&cxt);
2107 if (ok && sts != RMS$_NMF &&
2108 sts != RMS$_DNF && sts != RMS$_FNF) ok = 0;
2111 SETERRNO((sts == RMS$_SYN ? EINVAL : EVMSERR),sts);
2113 PerlIO_close(tmpfp);
2117 PerlIO_rewind(tmpfp);
2118 IoTYPE(io) = IoTYPE_RDONLY;
2119 IoIFP(io) = fp = tmpfp;
2120 IoFLAGS(io) &= ~IOf_UNTAINT; /* maybe redundant */
2125 #ifdef MACOS_TRADITIONAL
2126 sv_setpv(tmpcmd, "glob ");
2127 sv_catsv(tmpcmd, tmpglob);
2128 sv_catpv(tmpcmd, " |");
2132 sv_setpv(tmpcmd, "for a in ");
2133 sv_catsv(tmpcmd, tmpglob);
2134 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
2137 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
2138 sv_catsv(tmpcmd, tmpglob);
2140 sv_setpv(tmpcmd, "perlglob ");
2141 sv_catsv(tmpcmd, tmpglob);
2142 sv_catpv(tmpcmd, " |");
2147 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
2148 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
2149 sv_catsv(tmpcmd, tmpglob);
2150 sv_catpv(tmpcmd, "' 2>/dev/null |");
2152 sv_setpv(tmpcmd, "echo ");
2153 sv_catsv(tmpcmd, tmpglob);
2155 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
2157 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
2160 #endif /* !DOSISH */
2161 #endif /* MACOS_TRADITIONAL */
2162 (void)do_open(PL_last_in_gv, SvPVX(tmpcmd), SvCUR(tmpcmd),
2163 FALSE, O_RDONLY, 0, Nullfp);