3 * Copyright (c) 1991-2000, 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 sv = *av_fetch(PL_fdpid,PerlIO_fileno(fp),TRUE);
481 (void)SvUPGRADE(sv, SVt_IV);
484 sv = *av_fetch(PL_fdpid,fd,TRUE);
486 (void)SvUPGRADE(sv, SVt_IV);
495 #if defined(HAS_FCNTL) && defined(F_SETFD)
497 int save_errno = errno;
498 fd = PerlIO_fileno(fp);
499 fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
505 /* Need to supply default type info from open.pm */
506 SV *layers = PL_curcop->cop_io;
510 type = SvPV(layers,len);
511 if (type && mode[0] != 'r') {
512 /* Skip to write part */
513 char *s = strchr(type,0);
514 if (s && (s-type) < len) {
521 while (isSPACE(*type)) type++;
523 if (PerlIO_apply_layers(aTHX_ IoIFP(io),mode,type) != 0) {
529 IoFLAGS(io) &= ~IOf_NOLINE;
531 if (IoTYPE(io) == IoTYPE_SOCKET
532 || (IoTYPE(io) == IoTYPE_WRONLY && S_ISCHR(PL_statbuf.st_mode)) )
535 if (!(IoOFP(io) = PerlIO_fdopen(PerlIO_fileno(fp),mode))) {
541 if (PerlIO_apply_layers(aTHX_ IoOFP(io),mode,type) != 0) {
542 PerlIO_close(IoOFP(io));
558 IoTYPE(io) = savetype;
563 Perl_nextargv(pTHX_ register GV *gv)
566 #ifndef FLEXFILENAMES
575 PL_argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO);
576 if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
577 IoFLAGS(io) &= ~IOf_START;
579 if (!PL_argvout_stack)
580 PL_argvout_stack = newAV();
581 av_push(PL_argvout_stack, SvREFCNT_inc(PL_defoutgv));
584 if (PL_filemode & (S_ISUID|S_ISGID)) {
585 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
587 (void)fchmod(PL_lastfd,PL_filemode);
589 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
593 while (av_len(GvAV(gv)) >= 0) {
595 sv = av_shift(GvAV(gv));
597 sv_setsv(GvSV(gv),sv);
598 SvSETMAGIC(GvSV(gv));
599 PL_oldname = SvPVx(GvSV(gv), oldlen);
600 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,Nullfp)) {
602 TAINT_PROPER("inplace open");
603 if (oldlen == 1 && *PL_oldname == '-') {
604 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
605 return IoIFP(GvIOp(gv));
607 #ifndef FLEXFILENAMES
608 filedev = PL_statbuf.st_dev;
609 fileino = PL_statbuf.st_ino;
611 PL_filemode = PL_statbuf.st_mode;
612 fileuid = PL_statbuf.st_uid;
613 filegid = PL_statbuf.st_gid;
614 if (!S_ISREG(PL_filemode)) {
615 if (ckWARN_d(WARN_INPLACE))
616 Perl_warner(aTHX_ WARN_INPLACE,
617 "Can't do inplace edit: %s is not a regular file",
623 char *star = strchr(PL_inplace, '*');
625 char *begin = PL_inplace;
626 sv_setpvn(sv, "", 0);
628 sv_catpvn(sv, begin, star - begin);
629 sv_catpvn(sv, PL_oldname, oldlen);
631 } while ((star = strchr(begin, '*')));
636 sv_catpv(sv,PL_inplace);
638 #ifndef FLEXFILENAMES
639 if (PerlLIO_stat(SvPVX(sv),&PL_statbuf) >= 0
640 && PL_statbuf.st_dev == filedev
641 && PL_statbuf.st_ino == fileino
643 || (_djstat_fail_bits & _STFAIL_TRUENAME)!=0
647 if (ckWARN_d(WARN_INPLACE))
648 Perl_warner(aTHX_ WARN_INPLACE,
649 "Can't do inplace edit: %s would not be unique",
656 #if !defined(DOSISH) && !defined(__CYGWIN__)
657 if (PerlLIO_rename(PL_oldname,SvPVX(sv)) < 0) {
658 if (ckWARN_d(WARN_INPLACE))
659 Perl_warner(aTHX_ WARN_INPLACE,
660 "Can't rename %s to %s: %s, skipping file",
661 PL_oldname, SvPVX(sv), Strerror(errno) );
667 (void)PerlLIO_unlink(SvPVX(sv));
668 (void)PerlLIO_rename(PL_oldname,SvPVX(sv));
669 do_open(gv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,O_RDONLY,0,Nullfp);
672 (void)UNLINK(SvPVX(sv));
673 if (link(PL_oldname,SvPVX(sv)) < 0) {
674 if (ckWARN_d(WARN_INPLACE))
675 Perl_warner(aTHX_ WARN_INPLACE,
676 "Can't rename %s to %s: %s, skipping file",
677 PL_oldname, SvPVX(sv), Strerror(errno) );
681 (void)UNLINK(PL_oldname);
685 #if !defined(DOSISH) && !defined(AMIGAOS)
686 # ifndef VMS /* Don't delete; use automatic file versioning */
687 if (UNLINK(PL_oldname) < 0) {
688 if (ckWARN_d(WARN_INPLACE))
689 Perl_warner(aTHX_ WARN_INPLACE,
690 "Can't remove %s: %s, skipping file",
691 PL_oldname, Strerror(errno) );
697 Perl_croak(aTHX_ "Can't do inplace edit without backup");
701 sv_setpvn(sv,">",!PL_inplace);
702 sv_catpvn(sv,PL_oldname,oldlen);
703 SETERRNO(0,0); /* in case sprintf set errno */
705 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
706 O_WRONLY|O_CREAT|O_TRUNC,0,Nullfp))
708 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
709 O_WRONLY|O_CREAT|OPEN_EXCL,0666,Nullfp))
712 if (ckWARN_d(WARN_INPLACE))
713 Perl_warner(aTHX_ WARN_INPLACE, "Can't do inplace edit on %s: %s",
714 PL_oldname, Strerror(errno) );
718 setdefout(PL_argvoutgv);
719 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
720 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
722 (void)fchmod(PL_lastfd,PL_filemode);
724 # if !(defined(WIN32) && defined(__BORLANDC__))
725 /* Borland runtime creates a readonly file! */
726 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
729 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
731 (void)fchown(PL_lastfd,fileuid,filegid);
734 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
739 return IoIFP(GvIOp(gv));
742 if (ckWARN_d(WARN_INPLACE)) {
744 if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0
745 && !S_ISREG(PL_statbuf.st_mode))
747 Perl_warner(aTHX_ WARN_INPLACE,
748 "Can't do inplace edit: %s is not a regular file",
752 Perl_warner(aTHX_ WARN_INPLACE, "Can't open %s: %s",
753 PL_oldname, Strerror(eno));
757 if (io && (IoFLAGS(io) & IOf_ARGV))
758 IoFLAGS(io) |= IOf_START;
760 (void)do_close(PL_argvoutgv,FALSE);
761 if (io && (IoFLAGS(io) & IOf_ARGV)
762 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
764 GV *oldout = (GV*)av_pop(PL_argvout_stack);
766 SvREFCNT_dec(oldout);
769 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
776 Perl_do_pipe(pTHX_ SV *sv, GV *rgv, GV *wgv)
795 if (PerlProc_pipe(fd) < 0)
797 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r");
798 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w");
799 IoIFP(wstio) = IoOFP(wstio);
800 IoTYPE(rstio) = IoTYPE_RDONLY;
801 IoTYPE(wstio) = IoTYPE_WRONLY;
802 if (!IoIFP(rstio) || !IoOFP(wstio)) {
803 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
804 else PerlLIO_close(fd[0]);
805 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
806 else PerlLIO_close(fd[1]);
810 sv_setsv(sv,&PL_sv_yes);
814 sv_setsv(sv,&PL_sv_undef);
819 /* explicit renamed to avoid C++ conflict -- kja */
821 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
828 if (!gv || SvTYPE(gv) != SVt_PVGV) {
830 SETERRNO(EBADF,SS$_IVCHAN);
834 if (!io) { /* never opened */
836 if (ckWARN(WARN_UNOPENED)) /* no check for closed here */
837 report_evil_fh(gv, io, PL_op->op_type);
838 SETERRNO(EBADF,SS$_IVCHAN);
842 retval = io_close(io, not_implicit);
846 IoLINES_LEFT(io) = IoPAGE_LEN(io);
848 IoTYPE(io) = IoTYPE_CLOSED;
853 Perl_io_close(pTHX_ IO *io, bool not_implicit)
859 if (IoTYPE(io) == IoTYPE_PIPE) {
860 status = PerlProc_pclose(IoIFP(io));
862 STATUS_NATIVE_SET(status);
863 retval = (STATUS_POSIX == 0);
866 retval = (status != -1);
869 else if (IoTYPE(io) == IoTYPE_STD)
872 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
873 retval = (PerlIO_close(IoOFP(io)) != EOF);
874 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
877 retval = (PerlIO_close(IoIFP(io)) != EOF);
879 IoOFP(io) = IoIFP(io) = Nullfp;
881 else if (not_implicit) {
882 SETERRNO(EBADF,SS$_IVCHAN);
889 Perl_do_eof(pTHX_ GV *gv)
898 else if (ckWARN(WARN_IO)
899 && (IoTYPE(io) == IoTYPE_WRONLY || IoIFP(io) == PerlIO_stdout()
900 || IoIFP(io) == PerlIO_stderr()))
902 /* integrate to report_evil_fh()? */
905 SV* sv = sv_newmortal();
906 gv_efullname4(sv, gv, Nullch, FALSE);
907 name = SvPV_nolen(sv);
910 Perl_warner(aTHX_ WARN_IO,
911 "Filehandle %s opened only for output", name);
913 Perl_warner(aTHX_ WARN_IO,
914 "Filehandle opened only for output");
919 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
920 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
921 return FALSE; /* this is the most usual case */
924 ch = PerlIO_getc(IoIFP(io));
926 (void)PerlIO_ungetc(IoIFP(io),ch);
930 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
931 if (PerlIO_get_cnt(IoIFP(io)) < -1)
932 PerlIO_set_cnt(IoIFP(io),-1);
934 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
935 if (!nextargv(PL_argvgv)) /* get another fp handy */
939 return TRUE; /* normal fp, definitely end of file */
945 Perl_do_tell(pTHX_ GV *gv)
950 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
951 #ifdef ULTRIX_STDIO_BOTCH
953 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
955 return PerlIO_tell(fp);
957 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
958 report_evil_fh(gv, io, PL_op->op_type);
959 SETERRNO(EBADF,RMS$_IFI);
964 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
969 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
970 #ifdef ULTRIX_STDIO_BOTCH
972 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
974 return PerlIO_seek(fp, pos, whence) >= 0;
976 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
977 report_evil_fh(gv, io, PL_op->op_type);
978 SETERRNO(EBADF,RMS$_IFI);
983 Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
988 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
989 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
990 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
991 report_evil_fh(gv, io, PL_op->op_type);
992 SETERRNO(EBADF,RMS$_IFI);
997 Perl_mode_from_discipline(pTHX_ SV *discp)
1002 char *s = SvPV(discp,len);
1007 if (len > 3 && strnEQ(s+1, "raw", 3)
1008 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1017 if (len > 4 && strnEQ(s+1, "crlf", 4)
1018 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1027 goto fail_discipline;
1030 else if (isSPACE(*s)) {
1037 end = strchr(s+1, ':');
1040 #ifndef PERLIO_LAYERS
1041 Perl_croak(aTHX_ "Unknown discipline '%.*s'", end-s, s);
1052 Perl_do_binmode(pTHX_ PerlIO *fp, int iotype, int mode)
1054 /* The old body of this is now in non-LAYER part of perlio.c
1055 * This is a stub for any XS code which might have been calling it.
1057 char *name = (O_BINARY != O_TEXT && !(mode & O_BINARY)) ? ":crlf" : ":raw";
1058 return PerlIO_binmode(aTHX_ fp, iotype, mode, name);
1061 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP)
1062 /* code courtesy of William Kucharski */
1065 I32 my_chsize(fd, length)
1066 I32 fd; /* file descriptor */
1067 Off_t length; /* length to set file to */
1070 struct stat filebuf;
1072 if (PerlLIO_fstat(fd, &filebuf) < 0)
1075 if (filebuf.st_size < length) {
1077 /* extend file length */
1079 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
1082 /* write a "0" byte */
1084 if ((PerlLIO_write(fd, "", 1)) != 1)
1088 /* truncate length */
1092 fl.l_start = length;
1093 fl.l_type = F_WRLCK; /* write lock on file space */
1096 * This relies on the UNDOCUMENTED F_FREESP argument to
1097 * fcntl(2), which truncates the file so that it ends at the
1098 * position indicated by fl.l_start.
1100 * Will minor miracles never cease?
1103 if (fcntl(fd, F_FREESP, &fl) < 0)
1110 #endif /* F_FREESP */
1113 Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
1115 register char *tmps;
1118 /* assuming fp is checked earlier */
1124 if (SvIOK(sv) && SvIVX(sv) != 0) {
1125 PerlIO_printf(fp, PL_ofmt, (NV)SvIVX(sv));
1126 return !PerlIO_error(fp);
1128 if ( (SvNOK(sv) && SvNVX(sv) != 0.0)
1129 || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) {
1130 PerlIO_printf(fp, PL_ofmt, SvNVX(sv));
1131 return !PerlIO_error(fp);
1134 switch (SvTYPE(sv)) {
1136 if (ckWARN(WARN_UNINITIALIZED))
1144 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
1146 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
1147 return !PerlIO_error(fp);
1151 if (PerlIO_isutf8(fp)) {
1152 tmps = SvPVutf8(sv, len);
1156 sv_utf8_downgrade(sv, FALSE);
1157 tmps = SvPV(sv, len);
1161 /* To detect whether the process is about to overstep its
1162 * filesize limit we would need getrlimit(). We could then
1163 * also transparently raise the limit with setrlimit() --
1164 * but only until the system hard limit/the filesystem limit,
1165 * at which we would get EPERM. Note that when using buffered
1166 * io the write failure can be delayed until the flush/close. --jhi */
1167 if (len && (PerlIO_write(fp,tmps,len) == 0))
1169 return !PerlIO_error(fp);
1179 if (PL_op->op_flags & OPf_REF) {
1184 if (io && IoIFP(io)) {
1186 sv_setpv(PL_statname,"");
1187 PL_laststype = OP_STAT;
1188 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
1192 return PL_laststatval;
1193 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1194 report_evil_fh(gv, io, PL_op->op_type);
1196 sv_setpv(PL_statname,"");
1197 return (PL_laststatval = -1);
1205 if (SvTYPE(sv) == SVt_PVGV) {
1209 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
1216 sv_setpv(PL_statname, s);
1217 PL_laststype = OP_STAT;
1218 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1219 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
1220 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "stat");
1221 return PL_laststatval;
1231 if (PL_op->op_flags & OPf_REF) {
1233 if (cGVOP_gv == PL_defgv) {
1234 if (PL_laststype != OP_LSTAT)
1235 Perl_croak(aTHX_ "The stat preceding -l _ wasn't an lstat");
1236 return PL_laststatval;
1238 Perl_croak(aTHX_ "You can't use -l on a filehandle");
1241 PL_laststype = OP_LSTAT;
1245 sv_setpv(PL_statname,SvPV(sv, n_a));
1246 PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache);
1247 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
1248 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "lstat");
1249 return PL_laststatval;
1253 Perl_do_aexec(pTHX_ SV *really, register SV **mark, register SV **sp)
1255 return do_aexec5(really, mark, sp, 0, 0);
1259 Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1260 int fd, int do_report)
1262 #ifdef MACOS_TRADITIONAL
1263 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1270 New(401,PL_Argv, sp - mark + 1, char*);
1272 while (++mark <= sp) {
1274 *a++ = SvPVx(*mark, n_a);
1279 if (*PL_Argv[0] != '/') /* will execvp use PATH? */
1280 TAINT_ENV(); /* testing IFS here is overkill, probably */
1281 if (really && *(tmps = SvPV(really, n_a)))
1282 PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
1284 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1285 if (ckWARN(WARN_EXEC))
1286 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1287 PL_Argv[0], Strerror(errno));
1291 PerlLIO_write(fd, (void*)&e, sizeof(int));
1301 Perl_do_execfree(pTHX)
1305 PL_Argv = Null(char **);
1313 #if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC) && !defined(MACOS_TRADITIONAL)
1316 Perl_do_exec(pTHX_ char *cmd)
1318 return do_exec3(cmd,0,0);
1322 Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report)
1328 while (*cmd && isSPACE(*cmd))
1331 /* save an extra exec if possible */
1334 if (strnEQ(cmd,PL_cshname,PL_cshlen) && strnEQ(cmd+PL_cshlen," -c",3)) {
1336 s = cmd+PL_cshlen+3;
1350 if (s[-1] == '\'') {
1352 PerlProc_execl(PL_cshname,"csh", flags,ncmd,(char*)0);
1360 /* see if there are shell metacharacters in it */
1362 if (*cmd == '.' && isSPACE(cmd[1]))
1365 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1368 for (s = cmd; *s && isALNUM(*s); s++) ; /* catch VAR=val gizmo */
1372 for (s = cmd; *s; s++) {
1373 if (*s != ' ' && !isALPHA(*s) && strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1374 if (*s == '\n' && !s[1]) {
1378 /* handle the 2>&1 construct at the end */
1379 if (*s == '>' && s[1] == '&' && s[2] == '1'
1380 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1381 && (!s[3] || isSPACE(s[3])))
1385 while (*t && isSPACE(*t))
1387 if (!*t && (dup2(1,2) != -1)) {
1393 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
1398 New(402,PL_Argv, (s - cmd) / 2 + 2, char*);
1399 PL_Cmd = savepvn(cmd, s-cmd);
1401 for (s = PL_Cmd; *s;) {
1402 while (*s && isSPACE(*s)) s++;
1405 while (*s && !isSPACE(*s)) s++;
1411 PerlProc_execvp(PL_Argv[0],PL_Argv);
1412 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1419 if (ckWARN(WARN_EXEC))
1420 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1421 PL_Argv[0], Strerror(errno));
1423 PerlLIO_write(fd, (void*)&e, sizeof(int));
1432 #endif /* OS2 || WIN32 */
1435 Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
1439 register I32 tot = 0;
1442 SV **oldmark = mark;
1445 #define APPLY_TAINT_PROPER() \
1447 if (PL_tainted) { TAINT_PROPER(what); } \
1450 /* This is a first heuristic; it doesn't catch tainting magic. */
1452 while (++mark <= sp) {
1453 if (SvTAINTED(*mark)) {
1463 APPLY_TAINT_PROPER();
1466 APPLY_TAINT_PROPER();
1468 while (++mark <= sp) {
1469 char *name = SvPVx(*mark, n_a);
1470 APPLY_TAINT_PROPER();
1471 if (PerlLIO_chmod(name, val))
1479 APPLY_TAINT_PROPER();
1480 if (sp - mark > 2) {
1481 val = SvIVx(*++mark);
1482 val2 = SvIVx(*++mark);
1483 APPLY_TAINT_PROPER();
1485 while (++mark <= sp) {
1486 char *name = SvPVx(*mark, n_a);
1487 APPLY_TAINT_PROPER();
1488 if (PerlLIO_chown(name, val, val2))
1495 XXX Should we make lchown() directly available from perl?
1496 For now, we'll let Configure test for HAS_LCHOWN, but do
1497 nothing in the core.
1503 APPLY_TAINT_PROPER();
1506 s = SvPVx(*++mark, n_a);
1508 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1510 if (!(val = whichsig(s)))
1511 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
1515 APPLY_TAINT_PROPER();
1518 /* kill() doesn't do process groups (job trees?) under VMS */
1519 if (val < 0) val = -val;
1520 if (val == SIGKILL) {
1521 # include <starlet.h>
1522 /* Use native sys$delprc() to insure that target process is
1523 * deleted; supervisor-mode images don't pay attention to
1524 * CRTL's emulation of Unix-style signals and kill()
1526 while (++mark <= sp) {
1527 I32 proc = SvIVx(*mark);
1528 register unsigned long int __vmssts;
1529 APPLY_TAINT_PROPER();
1530 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1534 case SS$_NOSUCHNODE:
1535 SETERRNO(ESRCH,__vmssts);
1538 SETERRNO(EPERM,__vmssts);
1541 SETERRNO(EVMSERR,__vmssts);
1550 while (++mark <= sp) {
1551 I32 proc = SvIVx(*mark);
1552 APPLY_TAINT_PROPER();
1554 if (PerlProc_killpg(proc,val)) /* BSD */
1556 if (PerlProc_kill(-proc,val)) /* SYSV */
1562 while (++mark <= sp) {
1563 I32 proc = SvIVx(*mark);
1564 APPLY_TAINT_PROPER();
1565 if (PerlProc_kill(proc, val))
1573 APPLY_TAINT_PROPER();
1575 while (++mark <= sp) {
1576 s = SvPVx(*mark, n_a);
1577 APPLY_TAINT_PROPER();
1578 if (PL_euid || PL_unsafe) {
1582 else { /* don't let root wipe out directories without -U */
1583 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1595 APPLY_TAINT_PROPER();
1596 if (sp - mark > 2) {
1597 #if defined(I_UTIME) || defined(VMS)
1598 struct utimbuf utbuf;
1606 Zero(&utbuf, sizeof utbuf, char);
1608 utbuf.actime = (Time_t)SvNVx(*++mark); /* time accessed */
1609 utbuf.modtime = (Time_t)SvNVx(*++mark); /* time modified */
1611 utbuf.actime = (Time_t)SvIVx(*++mark); /* time accessed */
1612 utbuf.modtime = (Time_t)SvIVx(*++mark); /* time modified */
1614 APPLY_TAINT_PROPER();
1616 while (++mark <= sp) {
1617 char *name = SvPVx(*mark, n_a);
1618 APPLY_TAINT_PROPER();
1619 if (PerlLIO_utime(name, &utbuf))
1630 #undef APPLY_TAINT_PROPER
1633 /* Do the permissions allow some operation? Assumes statcache already set. */
1634 #ifndef VMS /* VMS' cando is in vms.c */
1636 Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register Stat_t *statbufp)
1637 /* Note: we use `effective' both for uids and gids.
1638 * Here we are betting on Uid_t being equal or wider than Gid_t. */
1641 /* [Comments and code from Len Reed]
1642 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1643 * to write-protected files. The execute permission bit is set
1644 * by the Miscrosoft C library stat() function for the following:
1649 * All files and directories are readable.
1650 * Directories and special files, e.g. "CON", cannot be
1652 * [Comment by Tom Dinger -- a directory can have the write-protect
1653 * bit set in the file system, but DOS permits changes to
1654 * the directory anyway. In addition, all bets are off
1655 * here for networked software, such as Novell and
1659 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1660 * too so it will actually look into the files for magic numbers
1662 return (mode & statbufp->st_mode) ? TRUE : FALSE;
1664 #else /* ! DOSISH */
1665 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
1666 if (mode == S_IXUSR) {
1667 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1671 return TRUE; /* root reads and writes anything */
1674 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1675 if (statbufp->st_mode & mode)
1676 return TRUE; /* ok as "user" */
1678 else if (ingroup(statbufp->st_gid,effective)) {
1679 if (statbufp->st_mode & mode >> 3)
1680 return TRUE; /* ok as "group" */
1682 else if (statbufp->st_mode & mode >> 6)
1683 return TRUE; /* ok as "other" */
1685 #endif /* ! DOSISH */
1690 Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective)
1692 #ifdef MACOS_TRADITIONAL
1693 /* This is simply not correct for AppleShare, but fix it yerself. */
1696 if (testgid == (effective ? PL_egid : PL_gid))
1698 #ifdef HAS_GETGROUPS
1703 Groups_t gary[NGROUPS];
1706 anum = getgroups(NGROUPS,gary);
1708 if (gary[anum] == testgid)
1716 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1719 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
1724 key = (key_t)SvNVx(*++mark);
1725 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1726 flags = SvIVx(*++mark);
1732 return msgget(key, flags);
1736 return semget(key, n, flags);
1740 return shmget(key, n, flags);
1742 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1744 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1747 return -1; /* should never happen */
1751 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
1755 I32 id, n, cmd, infosize, getinfo;
1758 id = SvIVx(*++mark);
1759 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1760 cmd = SvIVx(*++mark);
1763 getinfo = (cmd == IPC_STAT);
1769 if (cmd == IPC_STAT || cmd == IPC_SET)
1770 infosize = sizeof(struct msqid_ds);
1775 if (cmd == IPC_STAT || cmd == IPC_SET)
1776 infosize = sizeof(struct shmid_ds);
1782 if (cmd == IPC_STAT || cmd == IPC_SET)
1783 infosize = sizeof(struct semid_ds);
1784 else if (cmd == GETALL || cmd == SETALL)
1786 struct semid_ds semds;
1788 #ifdef EXTRA_F_IN_SEMUN_BUF
1789 semun.buff = &semds;
1793 getinfo = (cmd == GETALL);
1794 if (Semctl(id, 0, IPC_STAT, semun) == -1)
1796 infosize = semds.sem_nsems * sizeof(short);
1797 /* "short" is technically wrong but much more portable
1798 than guessing about u_?short(_t)? */
1801 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1805 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1807 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1816 SvPV_force(astr, len);
1817 a = SvGROW(astr, infosize+1);
1821 a = SvPV(astr, len);
1822 if (len != infosize)
1823 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
1832 a = INT2PTR(char *,i); /* ouch */
1839 ret = msgctl(id, cmd, (struct msqid_ds *)a);
1845 union semun unsemds;
1847 #ifdef EXTRA_F_IN_SEMUN_BUF
1848 unsemds.buff = (struct semid_ds *)a;
1850 unsemds.buf = (struct semid_ds *)a;
1852 ret = Semctl(id, n, cmd, unsemds);
1854 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1861 ret = shmctl(id, cmd, (struct shmid_ds *)a);
1865 if (getinfo && ret >= 0) {
1866 SvCUR_set(astr, infosize);
1867 *SvEND(astr) = '\0';
1874 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
1879 I32 id, msize, flags;
1882 id = SvIVx(*++mark);
1884 flags = SvIVx(*++mark);
1885 mbuf = SvPV(mstr, len);
1886 if ((msize = len - sizeof(long)) < 0)
1887 Perl_croak(aTHX_ "Arg too short for msgsnd");
1889 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
1891 Perl_croak(aTHX_ "msgsnd not implemented");
1896 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
1902 I32 id, msize, flags, ret;
1905 id = SvIVx(*++mark);
1907 /* suppress warning when reading into undef var --jhi */
1909 sv_setpvn(mstr, "", 0);
1910 msize = SvIVx(*++mark);
1911 mtype = (long)SvIVx(*++mark);
1912 flags = SvIVx(*++mark);
1913 SvPV_force(mstr, len);
1914 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
1917 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
1919 SvCUR_set(mstr, sizeof(long)+ret);
1920 *SvEND(mstr) = '\0';
1921 #ifndef INCOMPLETE_TAINTS
1922 /* who knows who has been playing with this message? */
1928 Perl_croak(aTHX_ "msgrcv not implemented");
1933 Perl_do_semop(pTHX_ SV **mark, SV **sp)
1941 id = SvIVx(*++mark);
1943 opbuf = SvPV(opstr, opsize);
1944 if (opsize < sizeof(struct sembuf)
1945 || (opsize % sizeof(struct sembuf)) != 0) {
1946 SETERRNO(EINVAL,LIB$_INVARG);
1950 return semop(id, (struct sembuf *)opbuf, opsize/sizeof(struct sembuf));
1952 Perl_croak(aTHX_ "semop not implemented");
1957 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
1962 I32 id, mpos, msize;
1964 struct shmid_ds shmds;
1966 id = SvIVx(*++mark);
1968 mpos = SvIVx(*++mark);
1969 msize = SvIVx(*++mark);
1971 if (shmctl(id, IPC_STAT, &shmds) == -1)
1973 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
1974 SETERRNO(EFAULT,SS$_ACCVIO); /* can't do as caller requested */
1977 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
1978 if (shm == (char *)-1) /* I hate System V IPC, I really do */
1980 if (optype == OP_SHMREAD) {
1981 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
1983 sv_setpvn(mstr, "", 0);
1984 SvPV_force(mstr, len);
1985 mbuf = SvGROW(mstr, msize+1);
1987 Copy(shm + mpos, mbuf, msize, char);
1988 SvCUR_set(mstr, msize);
1989 *SvEND(mstr) = '\0';
1991 #ifndef INCOMPLETE_TAINTS
1992 /* who knows who has been playing with this shared memory? */
1999 mbuf = SvPV(mstr, len);
2000 if ((n = len) > msize)
2002 Copy(mbuf, shm + mpos, n, char);
2004 memzero(shm + mpos + n, msize - n);
2008 Perl_croak(aTHX_ "shm I/O not implemented");
2012 #endif /* SYSV IPC */
2015 =for apidoc start_glob
2017 Function called by C<do_readline> to spawn a glob (or do the glob inside
2018 perl on VMS). This code used to be inline, but now perl uses C<File::Glob>
2019 this glob starter is only used by miniperl during the build proccess.
2020 Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up.
2026 Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
2028 SV *tmpcmd = NEWSV(55, 0);
2032 #ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
2033 /* since spawning off a process is a real performance hit */
2035 #include <descrip.h>
2036 #include <lib$routines.h>
2039 char rslt[NAM$C_MAXRSS+1+sizeof(unsigned short int)] = {'\0','\0'};
2040 char vmsspec[NAM$C_MAXRSS+1];
2041 char *rstr = rslt + sizeof(unsigned short int), *begin, *end, *cp;
2042 char tmpfnam[L_tmpnam] = "SYS$SCRATCH:";
2043 $DESCRIPTOR(dfltdsc,"SYS$DISK:[]*.*;");
2046 struct dsc$descriptor_s wilddsc
2047 = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
2048 struct dsc$descriptor_vs rsdsc
2049 = {sizeof rslt, DSC$K_DTYPE_VT, DSC$K_CLASS_VS, rslt};
2050 unsigned long int cxt = 0, sts = 0, ok = 1, hasdir = 0, hasver = 0, isunix = 0;
2052 /* We could find out if there's an explicit dev/dir or version
2053 by peeking into lib$find_file's internal context at
2054 ((struct NAM *)((struct FAB *)cxt)->fab$l_nam)->nam$l_fnb
2055 but that's unsupported, so I don't want to do it now and
2056 have it bite someone in the future. */
2057 strcat(tmpfnam,PerlLIO_tmpnam(NULL));
2058 cp = SvPV(tmpglob,i);
2060 if (cp[i] == ';') hasver = 1;
2062 if (sts) hasver = 1;
2066 hasdir = isunix = 1;
2069 if (cp[i] == ']' || cp[i] == '>' || cp[i] == ':') {
2074 if ((tmpfp = PerlIO_open(tmpfnam,"w+","fop=dlt")) != NULL) {
2076 if (!PerlLIO_stat(SvPVX(tmpglob),&st) && S_ISDIR(st.st_mode))
2077 ok = ((wilddsc.dsc$a_pointer = tovmspath(SvPVX(tmpglob),vmsspec)) != NULL);
2078 else ok = ((wilddsc.dsc$a_pointer = tovmsspec(SvPVX(tmpglob),vmsspec)) != NULL);
2079 if (ok) wilddsc.dsc$w_length = (unsigned short int) strlen(wilddsc.dsc$a_pointer);
2080 while (ok && ((sts = lib$find_file(&wilddsc,&rsdsc,&cxt,
2081 &dfltdsc,NULL,NULL,NULL))&1)) {
2082 end = rstr + (unsigned long int) *rslt;
2083 if (!hasver) while (*end != ';') end--;
2084 *(end++) = '\n'; *end = '\0';
2085 for (cp = rstr; *cp; cp++) *cp = _tolower(*cp);
2087 if (isunix) trim_unixpath(rstr,SvPVX(tmpglob),1);
2092 while (*(--begin) != ']' && *begin != '>') ;
2095 ok = (PerlIO_puts(tmpfp,begin) != EOF);
2097 if (cxt) (void)lib$find_file_end(&cxt);
2098 if (ok && sts != RMS$_NMF &&
2099 sts != RMS$_DNF && sts != RMS$_FNF) ok = 0;
2102 SETERRNO((sts == RMS$_SYN ? EINVAL : EVMSERR),sts);
2104 PerlIO_close(tmpfp);
2108 PerlIO_rewind(tmpfp);
2109 IoTYPE(io) = IoTYPE_RDONLY;
2110 IoIFP(io) = fp = tmpfp;
2111 IoFLAGS(io) &= ~IOf_UNTAINT; /* maybe redundant */
2116 #ifdef MACOS_TRADITIONAL
2117 sv_setpv(tmpcmd, "glob ");
2118 sv_catsv(tmpcmd, tmpglob);
2119 sv_catpv(tmpcmd, " |");
2123 sv_setpv(tmpcmd, "for a in ");
2124 sv_catsv(tmpcmd, tmpglob);
2125 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
2128 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
2129 sv_catsv(tmpcmd, tmpglob);
2131 sv_setpv(tmpcmd, "perlglob ");
2132 sv_catsv(tmpcmd, tmpglob);
2133 sv_catpv(tmpcmd, " |");
2138 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
2139 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
2140 sv_catsv(tmpcmd, tmpglob);
2141 sv_catpv(tmpcmd, "' 2>/dev/null |");
2143 sv_setpv(tmpcmd, "echo ");
2144 sv_catsv(tmpcmd, tmpglob);
2146 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
2148 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
2151 #endif /* !DOSISH */
2152 #endif /* MACOS_TRADITIONAL */
2153 (void)do_open(PL_last_in_gv, SvPVX(tmpcmd), SvCUR(tmpcmd),
2154 FALSE, O_RDONLY, 0, Nullfp);