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;
88 char mode[4]; /* stdio file mode ("r\0", "rb\0", "r+b\0" etc.) */
89 SV *svs = (num_svs) ? *svp : Nullsv;
91 Zero(mode,sizeof(mode),char);
92 PL_forkprocess = 1; /* assume true if no fork */
94 /* Collect default raw/crlf info from the op */
95 if (PL_op && PL_op->op_type == OP_OPEN) {
96 /* set up disciplines */
97 U8 flags = PL_op->op_private;
98 in_raw = (flags & OPpOPEN_IN_RAW);
99 in_crlf = (flags & OPpOPEN_IN_CRLF);
100 out_raw = (flags & OPpOPEN_OUT_RAW);
101 out_crlf = (flags & OPpOPEN_OUT_CRLF);
104 /* If currently open - close before we re-open */
106 fd = PerlIO_fileno(IoIFP(io));
107 if (IoTYPE(io) == IoTYPE_STD)
109 else if (fd <= PL_maxsysfd) {
112 savetype = IoTYPE(io);
115 else if (IoTYPE(io) == IoTYPE_PIPE)
116 result = PerlProc_pclose(IoIFP(io));
117 else if (IoIFP(io) != IoOFP(io)) {
119 result = PerlIO_close(IoOFP(io));
120 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
123 result = PerlIO_close(IoIFP(io));
126 result = PerlIO_close(IoIFP(io));
127 if (result == EOF && fd > PL_maxsysfd)
128 PerlIO_printf(Perl_error_log,
129 "Warning: unable to close filehandle %s properly.\n",
131 IoOFP(io) = IoIFP(io) = Nullfp;
135 /* sysopen style args, i.e. integer mode and permissions */
137 #if defined(USE_64_BIT_RAWIO) && defined(O_LARGEFILE)
138 rawmode |= O_LARGEFILE;
142 #define O_ACCMODE 3 /* Assume traditional implementation */
145 switch (result = rawmode & O_ACCMODE) {
147 IoTYPE(io) = IoTYPE_RDONLY;
150 IoTYPE(io) = IoTYPE_WRONLY;
154 IoTYPE(io) = IoTYPE_RDWR;
158 writing = (result > 0);
159 fd = PerlLIO_open3(name, rawmode, rawperm);
165 if (result == O_RDONLY) {
169 else if (rawmode & O_APPEND) {
171 if (result != O_WRONLY)
176 if (result == O_WRONLY)
183 if (rawmode & O_BINARY)
186 fp = PerlIO_fdopen(fd, mode);
192 /* Regular (non-sys) open */
198 type = savepvn(name, len);
201 /* Loose trailing white space */
202 while (tend > type && isSPACE(tend[-1]))
205 /* New style explict name, type is just mode and discipline/layer info */
207 name = SvPV(svs, l) ;
209 name = savepvn(name, len);
212 for (; isSPACE(*type); type++) ;
219 if (*type == IoTYPE_RDWR && (!num_svs || tend > type+1 && tend[-1] != IoTYPE_PIPE)) { /* scary */
224 if (*type == IoTYPE_PIPE) {
226 if (type[1] != IoTYPE_STD) {
228 Perl_croak(aTHX_ "Unknown open() mode '%.*s'", (int)olen, oname);
233 for (type++; isSPACE(*type); type++) ;
238 if (*name == '\0') { /* command is missing 19990114 */
239 if (ckWARN(WARN_PIPE))
240 Perl_warner(aTHX_ WARN_PIPE, "Missing command in piped open");
244 if (strNE(name,"-") || num_svs)
246 TAINT_PROPER("piped open");
247 if (!num_svs && name[len-1] == '|') {
249 if (ckWARN(WARN_PIPE))
250 Perl_warner(aTHX_ WARN_PIPE, "Can't open bidirectional pipe");
258 fp = PerlProc_popen(name,mode);
260 else if (*type == IoTYPE_WRONLY) {
261 TAINT_PROPER("open");
263 if (*type == IoTYPE_WRONLY) {
264 /* Two IoTYPE_WRONLYs in a row make for an IoTYPE_APPEND. */
265 mode[0] = IoTYPE(io) = IoTYPE_APPEND;
288 if (!*name && supplied_fp)
292 for (; isSPACE(*name); name++) ;
297 gv = gv_fetchpv(name,FALSE,SVt_PVIO);
301 SETERRNO(EINVAL,SS$_IVCHAN);
306 PerlIO *fp = IoIFP(thatio);
307 /* Flush stdio buffer before dup. --mjd
308 * Unfortunately SEEK_CURing 0 seems to
309 * be optimized away on most platforms;
310 * only Solaris and Linux seem to flush
313 /* sfio fails to clear error on next
314 sfwrite, contrary to documentation.
316 if (PerlIO_seek(fp, 0, SEEK_CUR) == -1)
319 /* On the other hand, do all platforms
320 * take gracefully to flushing a read-only
321 * filehandle? Perhaps we should do
322 * fsetpos(src)+fgetpos(dst)? --nik */
324 fd = PerlIO_fileno(fp);
325 /* When dup()ing STDIN, STDOUT or STDERR
326 * explicitly set appropriate access mode */
327 if (IoIFP(thatio) == PerlIO_stdout()
328 || IoIFP(thatio) == PerlIO_stderr())
329 IoTYPE(io) = IoTYPE_WRONLY;
330 else if (IoIFP(thatio) == PerlIO_stdin())
331 IoTYPE(io) = IoTYPE_RDONLY;
332 /* When dup()ing a socket, say result is
334 else if (IoTYPE(thatio) == IoTYPE_SOCKET)
335 IoTYPE(io) = IoTYPE_SOCKET;
341 fd = PerlLIO_dup(fd);
344 if (!(fp = PerlIO_fdopen(fd,mode))) {
352 for (; isSPACE(*type); type++) ;
353 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
356 fp = PerlIO_stdout();
357 IoTYPE(io) = IoTYPE_STD;
360 fp = PerlIO_open((num_svs ? name : type), mode);
364 else if (*type == IoTYPE_RDONLY) {
366 for (type++; isSPACE(*type); type++) ;
377 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
381 IoTYPE(io) = IoTYPE_STD;
384 fp = PerlIO_open((num_svs ? name : type), mode);
386 else if ((num_svs && type[0] == IoTYPE_STD && type[1] == IoTYPE_PIPE) ||
387 (!num_svs && tend > type+1 && tend[-1] == IoTYPE_PIPE)) {
389 type += 2; /* skip over '-|' */
393 while (tend > type && isSPACE(tend[-1]))
396 for (; isSPACE(*type); type++) ;
400 if (*name == '\0') { /* command is missing 19990114 */
401 if (ckWARN(WARN_PIPE))
402 Perl_warner(aTHX_ WARN_PIPE, "Missing command in piped open");
406 if (strNE(name,"-") || num_svs)
408 TAINT_PROPER("piped open");
414 fp = PerlProc_popen(name,mode);
415 IoTYPE(io) = IoTYPE_PIPE;
421 IoTYPE(io) = IoTYPE_RDONLY;
423 for (; isSPACE(*name); name++) ;
429 if (strEQ(name,"-")) {
431 IoTYPE(io) = IoTYPE_STD;
434 fp = PerlIO_open(name,mode);
439 if (ckWARN(WARN_NEWLINE) && IoTYPE(io) == IoTYPE_RDONLY && strchr(name, '\n'))
440 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "open");
443 if (IoTYPE(io) && IoTYPE(io) != IoTYPE_PIPE && IoTYPE(io) != IoTYPE_STD) {
444 if (PerlLIO_fstat(PerlIO_fileno(fp),&PL_statbuf) < 0) {
445 (void)PerlIO_close(fp);
448 if (S_ISSOCK(PL_statbuf.st_mode))
449 IoTYPE(io) = IoTYPE_SOCKET; /* in case a socket was passed in to us */
453 !(PL_statbuf.st_mode & S_IFMT)
457 && IoTYPE(io) != IoTYPE_WRONLY /* Dups of STD* filehandles already have */
458 && IoTYPE(io) != IoTYPE_RDONLY /* type so they aren't marked as sockets */
459 ) { /* on OS's that return 0 on fstat()ed pipe */
461 Sock_size_t buflen = sizeof tmpbuf;
462 if (PerlSock_getsockname(PerlIO_fileno(fp), (struct sockaddr *)tmpbuf,
464 || errno != ENOTSOCK)
465 IoTYPE(io) = IoTYPE_SOCKET; /* some OS's return 0 on fstat()ed socket */
466 /* but some return 0 for streams too, sigh */
470 if (saveifp) { /* must use old fp? */
471 /* If fd is less that PL_maxsysfd i.e. STDIN..STDERR
472 then dup the new fileno down
474 fd = PerlIO_fileno(saveifp);
476 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
477 if (saveofp != saveifp) { /* was a socket? */
478 PerlIO_close(saveofp);
479 /* This looks very suspect - NI-S 24 Nov 2000 */
481 Safefree(saveofp); /* ??? */
484 if (fd != PerlIO_fileno(fp)) {
488 PerlLIO_dup2(PerlIO_fileno(fp), fd);
490 if (fd != PerlIO_fileno(PerlIO_stdin())) {
491 char newname[FILENAME_MAX+1];
492 if (fgetname(fp, newname)) {
493 if (fd == PerlIO_fileno(PerlIO_stdout())) Perl_vmssetuserlnm("SYS$OUTPUT", newname);
494 if (fd == PerlIO_fileno(PerlIO_stderr())) Perl_vmssetuserlnm("SYS$ERROR", newname);
499 sv = *av_fetch(PL_fdpid,PerlIO_fileno(fp),TRUE);
500 (void)SvUPGRADE(sv, SVt_IV);
503 sv = *av_fetch(PL_fdpid,fd,TRUE);
505 (void)SvUPGRADE(sv, SVt_IV);
514 #if defined(HAS_FCNTL) && defined(F_SETFD)
516 int save_errno = errno;
517 fd = PerlIO_fileno(fp);
518 fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
524 /* Need to supply default type info from open.pm */
525 SV *layers = PL_curcop->cop_io;
529 type = SvPV(layers,len);
530 if (type && mode[0] != 'r') {
531 /* Skip to write part */
532 char *s = strchr(type,0);
533 if (s && (s-type) < len) {
540 while (isSPACE(*type)) type++;
543 if (PerlIO_apply_layers(aTHX_ IoIFP(io),mode,type) != 0) {
549 IoFLAGS(io) &= ~IOf_NOLINE;
551 if (IoTYPE(io) == IoTYPE_SOCKET
552 || (IoTYPE(io) == IoTYPE_WRONLY && S_ISCHR(PL_statbuf.st_mode)) )
555 if (!(IoOFP(io) = PerlIO_fdopen(PerlIO_fileno(fp),mode))) {
561 if (PerlIO_apply_layers(aTHX_ IoOFP(io),mode,type) != 0) {
562 PerlIO_close(IoOFP(io));
578 IoTYPE(io) = savetype;
583 Perl_nextargv(pTHX_ register GV *gv)
586 #ifndef FLEXFILENAMES
595 PL_argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO);
596 if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
597 IoFLAGS(io) &= ~IOf_START;
599 if (!PL_argvout_stack)
600 PL_argvout_stack = newAV();
601 av_push(PL_argvout_stack, SvREFCNT_inc(PL_defoutgv));
604 if (PL_filemode & (S_ISUID|S_ISGID)) {
605 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
607 (void)fchmod(PL_lastfd,PL_filemode);
609 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
613 while (av_len(GvAV(gv)) >= 0) {
615 sv = av_shift(GvAV(gv));
617 sv_setsv(GvSV(gv),sv);
618 SvSETMAGIC(GvSV(gv));
619 PL_oldname = SvPVx(GvSV(gv), oldlen);
620 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,Nullfp)) {
622 TAINT_PROPER("inplace open");
623 if (oldlen == 1 && *PL_oldname == '-') {
624 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
625 return IoIFP(GvIOp(gv));
627 #ifndef FLEXFILENAMES
628 filedev = PL_statbuf.st_dev;
629 fileino = PL_statbuf.st_ino;
631 PL_filemode = PL_statbuf.st_mode;
632 fileuid = PL_statbuf.st_uid;
633 filegid = PL_statbuf.st_gid;
634 if (!S_ISREG(PL_filemode)) {
635 if (ckWARN_d(WARN_INPLACE))
636 Perl_warner(aTHX_ WARN_INPLACE,
637 "Can't do inplace edit: %s is not a regular file",
643 char *star = strchr(PL_inplace, '*');
645 char *begin = PL_inplace;
646 sv_setpvn(sv, "", 0);
648 sv_catpvn(sv, begin, star - begin);
649 sv_catpvn(sv, PL_oldname, oldlen);
651 } while ((star = strchr(begin, '*')));
656 sv_catpv(sv,PL_inplace);
658 #ifndef FLEXFILENAMES
659 if (PerlLIO_stat(SvPVX(sv),&PL_statbuf) >= 0
660 && PL_statbuf.st_dev == filedev
661 && PL_statbuf.st_ino == fileino
663 || (_djstat_fail_bits & _STFAIL_TRUENAME)!=0
667 if (ckWARN_d(WARN_INPLACE))
668 Perl_warner(aTHX_ WARN_INPLACE,
669 "Can't do inplace edit: %s would not be unique",
676 #if !defined(DOSISH) && !defined(__CYGWIN__)
677 if (PerlLIO_rename(PL_oldname,SvPVX(sv)) < 0) {
678 if (ckWARN_d(WARN_INPLACE))
679 Perl_warner(aTHX_ WARN_INPLACE,
680 "Can't rename %s to %s: %s, skipping file",
681 PL_oldname, SvPVX(sv), Strerror(errno) );
687 (void)PerlLIO_unlink(SvPVX(sv));
688 (void)PerlLIO_rename(PL_oldname,SvPVX(sv));
689 do_open(gv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,O_RDONLY,0,Nullfp);
692 (void)UNLINK(SvPVX(sv));
693 if (link(PL_oldname,SvPVX(sv)) < 0) {
694 if (ckWARN_d(WARN_INPLACE))
695 Perl_warner(aTHX_ WARN_INPLACE,
696 "Can't rename %s to %s: %s, skipping file",
697 PL_oldname, SvPVX(sv), Strerror(errno) );
701 (void)UNLINK(PL_oldname);
705 #if !defined(DOSISH) && !defined(AMIGAOS)
706 # ifndef VMS /* Don't delete; use automatic file versioning */
707 if (UNLINK(PL_oldname) < 0) {
708 if (ckWARN_d(WARN_INPLACE))
709 Perl_warner(aTHX_ WARN_INPLACE,
710 "Can't remove %s: %s, skipping file",
711 PL_oldname, Strerror(errno) );
717 Perl_croak(aTHX_ "Can't do inplace edit without backup");
721 sv_setpvn(sv,">",!PL_inplace);
722 sv_catpvn(sv,PL_oldname,oldlen);
723 SETERRNO(0,0); /* in case sprintf set errno */
725 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
726 O_WRONLY|O_CREAT|O_TRUNC,0,Nullfp))
728 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
729 O_WRONLY|O_CREAT|OPEN_EXCL,0666,Nullfp))
732 if (ckWARN_d(WARN_INPLACE))
733 Perl_warner(aTHX_ WARN_INPLACE, "Can't do inplace edit on %s: %s",
734 PL_oldname, Strerror(errno) );
738 setdefout(PL_argvoutgv);
739 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
740 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
742 (void)fchmod(PL_lastfd,PL_filemode);
744 # if !(defined(WIN32) && defined(__BORLANDC__))
745 /* Borland runtime creates a readonly file! */
746 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
749 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
751 (void)fchown(PL_lastfd,fileuid,filegid);
754 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
759 return IoIFP(GvIOp(gv));
762 if (ckWARN_d(WARN_INPLACE)) {
764 if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0
765 && !S_ISREG(PL_statbuf.st_mode))
767 Perl_warner(aTHX_ WARN_INPLACE,
768 "Can't do inplace edit: %s is not a regular file",
772 Perl_warner(aTHX_ WARN_INPLACE, "Can't open %s: %s",
773 PL_oldname, Strerror(eno));
777 if (io && (IoFLAGS(io) & IOf_ARGV))
778 IoFLAGS(io) |= IOf_START;
780 (void)do_close(PL_argvoutgv,FALSE);
781 if (io && (IoFLAGS(io) & IOf_ARGV)
782 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
784 GV *oldout = (GV*)av_pop(PL_argvout_stack);
786 SvREFCNT_dec(oldout);
789 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
796 Perl_do_pipe(pTHX_ SV *sv, GV *rgv, GV *wgv)
815 if (PerlProc_pipe(fd) < 0)
817 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r");
818 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w");
819 IoIFP(wstio) = IoOFP(wstio);
820 IoTYPE(rstio) = IoTYPE_RDONLY;
821 IoTYPE(wstio) = IoTYPE_WRONLY;
822 if (!IoIFP(rstio) || !IoOFP(wstio)) {
823 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
824 else PerlLIO_close(fd[0]);
825 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
826 else PerlLIO_close(fd[1]);
830 sv_setsv(sv,&PL_sv_yes);
834 sv_setsv(sv,&PL_sv_undef);
839 /* explicit renamed to avoid C++ conflict -- kja */
841 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
848 if (!gv || SvTYPE(gv) != SVt_PVGV) {
850 SETERRNO(EBADF,SS$_IVCHAN);
854 if (!io) { /* never opened */
856 if (ckWARN(WARN_UNOPENED)) /* no check for closed here */
857 report_evil_fh(gv, io, PL_op->op_type);
858 SETERRNO(EBADF,SS$_IVCHAN);
862 retval = io_close(io, not_implicit);
866 IoLINES_LEFT(io) = IoPAGE_LEN(io);
868 IoTYPE(io) = IoTYPE_CLOSED;
873 Perl_io_close(pTHX_ IO *io, bool not_implicit)
879 if (IoTYPE(io) == IoTYPE_PIPE) {
880 status = PerlProc_pclose(IoIFP(io));
882 STATUS_NATIVE_SET(status);
883 retval = (STATUS_POSIX == 0);
886 retval = (status != -1);
889 else if (IoTYPE(io) == IoTYPE_STD)
892 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
893 retval = (PerlIO_close(IoOFP(io)) != EOF);
894 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
897 retval = (PerlIO_close(IoIFP(io)) != EOF);
899 IoOFP(io) = IoIFP(io) = Nullfp;
901 else if (not_implicit) {
902 SETERRNO(EBADF,SS$_IVCHAN);
909 Perl_do_eof(pTHX_ GV *gv)
918 else if (ckWARN(WARN_IO)
919 && (IoTYPE(io) == IoTYPE_WRONLY || IoIFP(io) == PerlIO_stdout()
920 || IoIFP(io) == PerlIO_stderr()))
922 /* integrate to report_evil_fh()? */
925 SV* sv = sv_newmortal();
926 gv_efullname4(sv, gv, Nullch, FALSE);
927 name = SvPV_nolen(sv);
930 Perl_warner(aTHX_ WARN_IO,
931 "Filehandle %s opened only for output", name);
933 Perl_warner(aTHX_ WARN_IO,
934 "Filehandle opened only for output");
939 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
940 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
941 return FALSE; /* this is the most usual case */
944 ch = PerlIO_getc(IoIFP(io));
946 (void)PerlIO_ungetc(IoIFP(io),ch);
950 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
951 if (PerlIO_get_cnt(IoIFP(io)) < -1)
952 PerlIO_set_cnt(IoIFP(io),-1);
954 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
955 if (!nextargv(PL_argvgv)) /* get another fp handy */
959 return TRUE; /* normal fp, definitely end of file */
965 Perl_do_tell(pTHX_ GV *gv)
970 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
971 #ifdef ULTRIX_STDIO_BOTCH
973 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
975 return PerlIO_tell(fp);
977 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
978 report_evil_fh(gv, io, PL_op->op_type);
979 SETERRNO(EBADF,RMS$_IFI);
984 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
989 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
990 #ifdef ULTRIX_STDIO_BOTCH
992 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
994 return PerlIO_seek(fp, pos, whence) >= 0;
996 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
997 report_evil_fh(gv, io, PL_op->op_type);
998 SETERRNO(EBADF,RMS$_IFI);
1003 Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
1006 register PerlIO *fp;
1008 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
1009 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
1010 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1011 report_evil_fh(gv, io, PL_op->op_type);
1012 SETERRNO(EBADF,RMS$_IFI);
1017 Perl_mode_from_discipline(pTHX_ SV *discp)
1019 int mode = O_BINARY;
1022 char *s = SvPV(discp,len);
1027 if (len > 3 && strnEQ(s+1, "raw", 3)
1028 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1037 if (len > 4 && strnEQ(s+1, "crlf", 4)
1038 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1047 goto fail_discipline;
1050 else if (isSPACE(*s)) {
1057 end = strchr(s+1, ':');
1060 #ifndef PERLIO_LAYERS
1061 Perl_croak(aTHX_ "Unknown discipline '%.*s'", end-s, s);
1072 Perl_do_binmode(pTHX_ PerlIO *fp, int iotype, int mode)
1074 /* The old body of this is now in non-LAYER part of perlio.c
1075 * This is a stub for any XS code which might have been calling it.
1077 char *name = (O_BINARY != O_TEXT && !(mode & O_BINARY)) ? ":crlf" : ":raw";
1078 return PerlIO_binmode(aTHX_ fp, iotype, mode, name);
1081 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP)
1082 /* code courtesy of William Kucharski */
1085 I32 my_chsize(fd, length)
1086 I32 fd; /* file descriptor */
1087 Off_t length; /* length to set file to */
1090 struct stat filebuf;
1092 if (PerlLIO_fstat(fd, &filebuf) < 0)
1095 if (filebuf.st_size < length) {
1097 /* extend file length */
1099 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
1102 /* write a "0" byte */
1104 if ((PerlLIO_write(fd, "", 1)) != 1)
1108 /* truncate length */
1112 fl.l_start = length;
1113 fl.l_type = F_WRLCK; /* write lock on file space */
1116 * This relies on the UNDOCUMENTED F_FREESP argument to
1117 * fcntl(2), which truncates the file so that it ends at the
1118 * position indicated by fl.l_start.
1120 * Will minor miracles never cease?
1123 if (fcntl(fd, F_FREESP, &fl) < 0)
1130 #endif /* F_FREESP */
1133 Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
1135 register char *tmps;
1138 /* assuming fp is checked earlier */
1144 if (SvIOK(sv) && SvIVX(sv) != 0) {
1145 PerlIO_printf(fp, PL_ofmt, (NV)SvIVX(sv));
1146 return !PerlIO_error(fp);
1148 if ( (SvNOK(sv) && SvNVX(sv) != 0.0)
1149 || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) {
1150 PerlIO_printf(fp, PL_ofmt, SvNVX(sv));
1151 return !PerlIO_error(fp);
1154 switch (SvTYPE(sv)) {
1156 if (ckWARN(WARN_UNINITIALIZED))
1164 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
1166 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
1167 return !PerlIO_error(fp);
1171 if (PerlIO_isutf8(fp)) {
1173 sv_utf8_upgrade(sv = sv_mortalcopy(sv));
1175 else if (DO_UTF8(sv))
1176 sv_utf8_downgrade((sv = sv_mortalcopy(sv)), FALSE);
1177 tmps = SvPV(sv, len);
1180 /* To detect whether the process is about to overstep its
1181 * filesize limit we would need getrlimit(). We could then
1182 * also transparently raise the limit with setrlimit() --
1183 * but only until the system hard limit/the filesystem limit,
1184 * at which we would get EPERM. Note that when using buffered
1185 * io the write failure can be delayed until the flush/close. --jhi */
1186 if (len && (PerlIO_write(fp,tmps,len) == 0))
1188 return !PerlIO_error(fp);
1198 if (PL_op->op_flags & OPf_REF) {
1203 if (io && IoIFP(io)) {
1205 sv_setpv(PL_statname,"");
1206 PL_laststype = OP_STAT;
1207 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
1211 return PL_laststatval;
1212 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1213 report_evil_fh(gv, io, PL_op->op_type);
1215 sv_setpv(PL_statname,"");
1216 return (PL_laststatval = -1);
1224 if (SvTYPE(sv) == SVt_PVGV) {
1228 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
1235 sv_setpv(PL_statname, s);
1236 PL_laststype = OP_STAT;
1237 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1238 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
1239 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "stat");
1240 return PL_laststatval;
1250 if (PL_op->op_flags & OPf_REF) {
1252 if (cGVOP_gv == PL_defgv) {
1253 if (PL_laststype != OP_LSTAT)
1254 Perl_croak(aTHX_ "The stat preceding -l _ wasn't an lstat");
1255 return PL_laststatval;
1257 Perl_croak(aTHX_ "You can't use -l on a filehandle");
1260 PL_laststype = OP_LSTAT;
1264 sv_setpv(PL_statname,SvPV(sv, n_a));
1265 PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache);
1266 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
1267 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "lstat");
1268 return PL_laststatval;
1272 Perl_do_aexec(pTHX_ SV *really, register SV **mark, register SV **sp)
1274 return do_aexec5(really, mark, sp, 0, 0);
1278 Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1279 int fd, int do_report)
1281 #ifdef MACOS_TRADITIONAL
1282 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1289 New(401,PL_Argv, sp - mark + 1, char*);
1291 while (++mark <= sp) {
1293 *a++ = SvPVx(*mark, n_a);
1298 if (*PL_Argv[0] != '/') /* will execvp use PATH? */
1299 TAINT_ENV(); /* testing IFS here is overkill, probably */
1300 if (really && *(tmps = SvPV(really, n_a)))
1301 PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
1303 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1304 if (ckWARN(WARN_EXEC))
1305 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1306 PL_Argv[0], Strerror(errno));
1310 PerlLIO_write(fd, (void*)&e, sizeof(int));
1320 Perl_do_execfree(pTHX)
1324 PL_Argv = Null(char **);
1332 #if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC) && !defined(MACOS_TRADITIONAL)
1335 Perl_do_exec(pTHX_ char *cmd)
1337 return do_exec3(cmd,0,0);
1341 Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report)
1347 while (*cmd && isSPACE(*cmd))
1350 /* save an extra exec if possible */
1353 if (strnEQ(cmd,PL_cshname,PL_cshlen) && strnEQ(cmd+PL_cshlen," -c",3)) {
1355 s = cmd+PL_cshlen+3;
1369 if (s[-1] == '\'') {
1371 PerlProc_execl(PL_cshname,"csh", flags,ncmd,(char*)0);
1379 /* see if there are shell metacharacters in it */
1381 if (*cmd == '.' && isSPACE(cmd[1]))
1384 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1387 for (s = cmd; *s && isALNUM(*s); s++) ; /* catch VAR=val gizmo */
1391 for (s = cmd; *s; s++) {
1392 if (*s != ' ' && !isALPHA(*s) && strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1393 if (*s == '\n' && !s[1]) {
1397 /* handle the 2>&1 construct at the end */
1398 if (*s == '>' && s[1] == '&' && s[2] == '1'
1399 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1400 && (!s[3] || isSPACE(s[3])))
1404 while (*t && isSPACE(*t))
1406 if (!*t && (dup2(1,2) != -1)) {
1412 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
1417 New(402,PL_Argv, (s - cmd) / 2 + 2, char*);
1418 PL_Cmd = savepvn(cmd, s-cmd);
1420 for (s = PL_Cmd; *s;) {
1421 while (*s && isSPACE(*s)) s++;
1424 while (*s && !isSPACE(*s)) s++;
1430 PerlProc_execvp(PL_Argv[0],PL_Argv);
1431 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1438 if (ckWARN(WARN_EXEC))
1439 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1440 PL_Argv[0], Strerror(errno));
1442 PerlLIO_write(fd, (void*)&e, sizeof(int));
1451 #endif /* OS2 || WIN32 */
1454 Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
1458 register I32 tot = 0;
1461 SV **oldmark = mark;
1464 #define APPLY_TAINT_PROPER() \
1466 if (PL_tainted) { TAINT_PROPER(what); } \
1469 /* This is a first heuristic; it doesn't catch tainting magic. */
1471 while (++mark <= sp) {
1472 if (SvTAINTED(*mark)) {
1482 APPLY_TAINT_PROPER();
1485 APPLY_TAINT_PROPER();
1487 while (++mark <= sp) {
1488 char *name = SvPVx(*mark, n_a);
1489 APPLY_TAINT_PROPER();
1490 if (PerlLIO_chmod(name, val))
1498 APPLY_TAINT_PROPER();
1499 if (sp - mark > 2) {
1500 val = SvIVx(*++mark);
1501 val2 = SvIVx(*++mark);
1502 APPLY_TAINT_PROPER();
1504 while (++mark <= sp) {
1505 char *name = SvPVx(*mark, n_a);
1506 APPLY_TAINT_PROPER();
1507 if (PerlLIO_chown(name, val, val2))
1514 XXX Should we make lchown() directly available from perl?
1515 For now, we'll let Configure test for HAS_LCHOWN, but do
1516 nothing in the core.
1522 APPLY_TAINT_PROPER();
1525 s = SvPVx(*++mark, n_a);
1527 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1529 if (!(val = whichsig(s)))
1530 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
1534 APPLY_TAINT_PROPER();
1537 /* kill() doesn't do process groups (job trees?) under VMS */
1538 if (val < 0) val = -val;
1539 if (val == SIGKILL) {
1540 # include <starlet.h>
1541 /* Use native sys$delprc() to insure that target process is
1542 * deleted; supervisor-mode images don't pay attention to
1543 * CRTL's emulation of Unix-style signals and kill()
1545 while (++mark <= sp) {
1546 I32 proc = SvIVx(*mark);
1547 register unsigned long int __vmssts;
1548 APPLY_TAINT_PROPER();
1549 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1553 case SS$_NOSUCHNODE:
1554 SETERRNO(ESRCH,__vmssts);
1557 SETERRNO(EPERM,__vmssts);
1560 SETERRNO(EVMSERR,__vmssts);
1569 while (++mark <= sp) {
1570 I32 proc = SvIVx(*mark);
1571 APPLY_TAINT_PROPER();
1573 if (PerlProc_killpg(proc,val)) /* BSD */
1575 if (PerlProc_kill(-proc,val)) /* SYSV */
1581 while (++mark <= sp) {
1582 I32 proc = SvIVx(*mark);
1583 APPLY_TAINT_PROPER();
1584 if (PerlProc_kill(proc, val))
1592 APPLY_TAINT_PROPER();
1594 while (++mark <= sp) {
1595 s = SvPVx(*mark, n_a);
1596 APPLY_TAINT_PROPER();
1597 if (PL_euid || PL_unsafe) {
1601 else { /* don't let root wipe out directories without -U */
1602 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1614 APPLY_TAINT_PROPER();
1615 if (sp - mark > 2) {
1616 #if defined(I_UTIME) || defined(VMS)
1617 struct utimbuf utbuf;
1625 Zero(&utbuf, sizeof utbuf, char);
1627 utbuf.actime = (Time_t)SvNVx(*++mark); /* time accessed */
1628 utbuf.modtime = (Time_t)SvNVx(*++mark); /* time modified */
1630 utbuf.actime = (Time_t)SvIVx(*++mark); /* time accessed */
1631 utbuf.modtime = (Time_t)SvIVx(*++mark); /* time modified */
1633 APPLY_TAINT_PROPER();
1635 while (++mark <= sp) {
1636 char *name = SvPVx(*mark, n_a);
1637 APPLY_TAINT_PROPER();
1638 if (PerlLIO_utime(name, &utbuf))
1649 #undef APPLY_TAINT_PROPER
1652 /* Do the permissions allow some operation? Assumes statcache already set. */
1653 #ifndef VMS /* VMS' cando is in vms.c */
1655 Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register Stat_t *statbufp)
1656 /* Note: we use `effective' both for uids and gids.
1657 * Here we are betting on Uid_t being equal or wider than Gid_t. */
1660 /* [Comments and code from Len Reed]
1661 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1662 * to write-protected files. The execute permission bit is set
1663 * by the Miscrosoft C library stat() function for the following:
1668 * All files and directories are readable.
1669 * Directories and special files, e.g. "CON", cannot be
1671 * [Comment by Tom Dinger -- a directory can have the write-protect
1672 * bit set in the file system, but DOS permits changes to
1673 * the directory anyway. In addition, all bets are off
1674 * here for networked software, such as Novell and
1678 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1679 * too so it will actually look into the files for magic numbers
1681 return (mode & statbufp->st_mode) ? TRUE : FALSE;
1683 #else /* ! DOSISH */
1684 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
1685 if (mode == S_IXUSR) {
1686 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1690 return TRUE; /* root reads and writes anything */
1693 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1694 if (statbufp->st_mode & mode)
1695 return TRUE; /* ok as "user" */
1697 else if (ingroup(statbufp->st_gid,effective)) {
1698 if (statbufp->st_mode & mode >> 3)
1699 return TRUE; /* ok as "group" */
1701 else if (statbufp->st_mode & mode >> 6)
1702 return TRUE; /* ok as "other" */
1704 #endif /* ! DOSISH */
1709 Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective)
1711 #ifdef MACOS_TRADITIONAL
1712 /* This is simply not correct for AppleShare, but fix it yerself. */
1715 if (testgid == (effective ? PL_egid : PL_gid))
1717 #ifdef HAS_GETGROUPS
1722 Groups_t gary[NGROUPS];
1725 anum = getgroups(NGROUPS,gary);
1727 if (gary[anum] == testgid)
1735 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1738 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
1743 key = (key_t)SvNVx(*++mark);
1744 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1745 flags = SvIVx(*++mark);
1751 return msgget(key, flags);
1755 return semget(key, n, flags);
1759 return shmget(key, n, flags);
1761 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1763 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1766 return -1; /* should never happen */
1770 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
1774 I32 id, n, cmd, infosize, getinfo;
1777 id = SvIVx(*++mark);
1778 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1779 cmd = SvIVx(*++mark);
1782 getinfo = (cmd == IPC_STAT);
1788 if (cmd == IPC_STAT || cmd == IPC_SET)
1789 infosize = sizeof(struct msqid_ds);
1794 if (cmd == IPC_STAT || cmd == IPC_SET)
1795 infosize = sizeof(struct shmid_ds);
1801 if (cmd == IPC_STAT || cmd == IPC_SET)
1802 infosize = sizeof(struct semid_ds);
1803 else if (cmd == GETALL || cmd == SETALL)
1805 struct semid_ds semds;
1807 #ifdef EXTRA_F_IN_SEMUN_BUF
1808 semun.buff = &semds;
1812 getinfo = (cmd == GETALL);
1813 if (Semctl(id, 0, IPC_STAT, semun) == -1)
1815 infosize = semds.sem_nsems * sizeof(short);
1816 /* "short" is technically wrong but much more portable
1817 than guessing about u_?short(_t)? */
1820 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1824 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1826 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1835 SvPV_force(astr, len);
1836 a = SvGROW(astr, infosize+1);
1840 a = SvPV(astr, len);
1841 if (len != infosize)
1842 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
1851 a = INT2PTR(char *,i); /* ouch */
1858 ret = msgctl(id, cmd, (struct msqid_ds *)a);
1864 union semun unsemds;
1866 #ifdef EXTRA_F_IN_SEMUN_BUF
1867 unsemds.buff = (struct semid_ds *)a;
1869 unsemds.buf = (struct semid_ds *)a;
1871 ret = Semctl(id, n, cmd, unsemds);
1873 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1880 ret = shmctl(id, cmd, (struct shmid_ds *)a);
1884 if (getinfo && ret >= 0) {
1885 SvCUR_set(astr, infosize);
1886 *SvEND(astr) = '\0';
1893 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
1898 I32 id, msize, flags;
1901 id = SvIVx(*++mark);
1903 flags = SvIVx(*++mark);
1904 mbuf = SvPV(mstr, len);
1905 if ((msize = len - sizeof(long)) < 0)
1906 Perl_croak(aTHX_ "Arg too short for msgsnd");
1908 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
1910 Perl_croak(aTHX_ "msgsnd not implemented");
1915 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
1921 I32 id, msize, flags, ret;
1924 id = SvIVx(*++mark);
1926 /* suppress warning when reading into undef var --jhi */
1928 sv_setpvn(mstr, "", 0);
1929 msize = SvIVx(*++mark);
1930 mtype = (long)SvIVx(*++mark);
1931 flags = SvIVx(*++mark);
1932 SvPV_force(mstr, len);
1933 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
1936 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
1938 SvCUR_set(mstr, sizeof(long)+ret);
1939 *SvEND(mstr) = '\0';
1940 #ifndef INCOMPLETE_TAINTS
1941 /* who knows who has been playing with this message? */
1947 Perl_croak(aTHX_ "msgrcv not implemented");
1952 Perl_do_semop(pTHX_ SV **mark, SV **sp)
1960 id = SvIVx(*++mark);
1962 opbuf = SvPV(opstr, opsize);
1963 if (opsize < sizeof(struct sembuf)
1964 || (opsize % sizeof(struct sembuf)) != 0) {
1965 SETERRNO(EINVAL,LIB$_INVARG);
1969 return semop(id, (struct sembuf *)opbuf, opsize/sizeof(struct sembuf));
1971 Perl_croak(aTHX_ "semop not implemented");
1976 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
1981 I32 id, mpos, msize;
1983 struct shmid_ds shmds;
1985 id = SvIVx(*++mark);
1987 mpos = SvIVx(*++mark);
1988 msize = SvIVx(*++mark);
1990 if (shmctl(id, IPC_STAT, &shmds) == -1)
1992 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
1993 SETERRNO(EFAULT,SS$_ACCVIO); /* can't do as caller requested */
1996 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
1997 if (shm == (char *)-1) /* I hate System V IPC, I really do */
1999 if (optype == OP_SHMREAD) {
2000 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2002 sv_setpvn(mstr, "", 0);
2003 SvPV_force(mstr, len);
2004 mbuf = SvGROW(mstr, msize+1);
2006 Copy(shm + mpos, mbuf, msize, char);
2007 SvCUR_set(mstr, msize);
2008 *SvEND(mstr) = '\0';
2010 #ifndef INCOMPLETE_TAINTS
2011 /* who knows who has been playing with this shared memory? */
2018 mbuf = SvPV(mstr, len);
2019 if ((n = len) > msize)
2021 Copy(mbuf, shm + mpos, n, char);
2023 memzero(shm + mpos + n, msize - n);
2027 Perl_croak(aTHX_ "shm I/O not implemented");
2031 #endif /* SYSV IPC */
2034 =for apidoc start_glob
2036 Function called by C<do_readline> to spawn a glob (or do the glob inside
2037 perl on VMS). This code used to be inline, but now perl uses C<File::Glob>
2038 this glob starter is only used by miniperl during the build proccess.
2039 Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up.
2045 Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
2047 SV *tmpcmd = NEWSV(55, 0);
2051 #ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
2052 /* since spawning off a process is a real performance hit */
2054 #include <descrip.h>
2055 #include <lib$routines.h>
2058 char rslt[NAM$C_MAXRSS+1+sizeof(unsigned short int)] = {'\0','\0'};
2059 char vmsspec[NAM$C_MAXRSS+1];
2060 char *rstr = rslt + sizeof(unsigned short int), *begin, *end, *cp;
2061 char tmpfnam[L_tmpnam] = "SYS$SCRATCH:";
2062 $DESCRIPTOR(dfltdsc,"SYS$DISK:[]*.*;");
2065 struct dsc$descriptor_s wilddsc
2066 = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
2067 struct dsc$descriptor_vs rsdsc
2068 = {sizeof rslt, DSC$K_DTYPE_VT, DSC$K_CLASS_VS, rslt};
2069 unsigned long int cxt = 0, sts = 0, ok = 1, hasdir = 0, hasver = 0, isunix = 0;
2071 /* We could find out if there's an explicit dev/dir or version
2072 by peeking into lib$find_file's internal context at
2073 ((struct NAM *)((struct FAB *)cxt)->fab$l_nam)->nam$l_fnb
2074 but that's unsupported, so I don't want to do it now and
2075 have it bite someone in the future. */
2076 strcat(tmpfnam,PerlLIO_tmpnam(NULL));
2077 cp = SvPV(tmpglob,i);
2079 if (cp[i] == ';') hasver = 1;
2081 if (sts) hasver = 1;
2085 hasdir = isunix = 1;
2088 if (cp[i] == ']' || cp[i] == '>' || cp[i] == ':') {
2093 if ((tmpfp = PerlIO_open(tmpfnam,"w+","fop=dlt")) != NULL) {
2095 if (!PerlLIO_stat(SvPVX(tmpglob),&st) && S_ISDIR(st.st_mode))
2096 ok = ((wilddsc.dsc$a_pointer = tovmspath(SvPVX(tmpglob),vmsspec)) != NULL);
2097 else ok = ((wilddsc.dsc$a_pointer = tovmsspec(SvPVX(tmpglob),vmsspec)) != NULL);
2098 if (ok) wilddsc.dsc$w_length = (unsigned short int) strlen(wilddsc.dsc$a_pointer);
2099 while (ok && ((sts = lib$find_file(&wilddsc,&rsdsc,&cxt,
2100 &dfltdsc,NULL,NULL,NULL))&1)) {
2101 end = rstr + (unsigned long int) *rslt;
2102 if (!hasver) while (*end != ';') end--;
2103 *(end++) = '\n'; *end = '\0';
2104 for (cp = rstr; *cp; cp++) *cp = _tolower(*cp);
2106 if (isunix) trim_unixpath(rstr,SvPVX(tmpglob),1);
2111 while (*(--begin) != ']' && *begin != '>') ;
2114 ok = (PerlIO_puts(tmpfp,begin) != EOF);
2116 if (cxt) (void)lib$find_file_end(&cxt);
2117 if (ok && sts != RMS$_NMF &&
2118 sts != RMS$_DNF && sts != RMS$_FNF) ok = 0;
2121 SETERRNO((sts == RMS$_SYN ? EINVAL : EVMSERR),sts);
2123 PerlIO_close(tmpfp);
2127 PerlIO_rewind(tmpfp);
2128 IoTYPE(io) = IoTYPE_RDONLY;
2129 IoIFP(io) = fp = tmpfp;
2130 IoFLAGS(io) &= ~IOf_UNTAINT; /* maybe redundant */
2135 #ifdef MACOS_TRADITIONAL
2136 sv_setpv(tmpcmd, "glob ");
2137 sv_catsv(tmpcmd, tmpglob);
2138 sv_catpv(tmpcmd, " |");
2142 sv_setpv(tmpcmd, "for a in ");
2143 sv_catsv(tmpcmd, tmpglob);
2144 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
2147 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
2148 sv_catsv(tmpcmd, tmpglob);
2150 sv_setpv(tmpcmd, "perlglob ");
2151 sv_catsv(tmpcmd, tmpglob);
2152 sv_catpv(tmpcmd, " |");
2157 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
2158 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
2159 sv_catsv(tmpcmd, tmpglob);
2160 sv_catpv(tmpcmd, "' 2>/dev/null |");
2162 sv_setpv(tmpcmd, "echo ");
2163 sv_catsv(tmpcmd, tmpglob);
2165 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
2167 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
2170 #endif /* !DOSISH */
2171 #endif /* MACOS_TRADITIONAL */
2172 (void)do_open(PL_last_in_gv, SvPVX(tmpcmd), SvCUR(tmpcmd),
2173 FALSE, O_RDONLY, 0, Nullfp);