3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4 * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
12 * Far below them they saw the white waters pour into a foaming bowl, and
13 * then swirl darkly about a deep oval basin in the rocks, until they found
14 * their way out again through a narrow gate, and flowed away, fuming and
15 * chattering, into calmer and more level reaches.
17 * [p.684 of _The Lord of the Rings_, IV/vi: "The Forbidden Pool"]
20 /* This file contains functions that do the actual I/O on behalf of ops.
21 * For example, pp_print() calls the do_print() function in this file for
22 * each argument needing printing.
26 #define PERL_IN_DOIO_C
29 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
38 # ifndef HAS_SHMAT_PROTOTYPE
39 extern Shmat_t shmat (int, char *, int);
45 # if defined(_MSC_VER) || defined(__MINGW32__)
46 # include <sys/utime.h>
53 # define OPEN_EXCL O_EXCL
58 #define PERL_MODE_MAX 8
59 #define PERL_FLAGS_MAX 10
64 Perl_do_openn(pTHX_ GV *gv, const char *oname, I32 len, int as_raw,
65 int rawmode, int rawperm, PerlIO *supplied_fp, SV **svp,
69 IO * const io = GvIOn(gv);
70 PerlIO *saveifp = NULL;
71 PerlIO *saveofp = NULL;
73 char savetype = IoTYPE_CLOSED;
78 bool was_fdopen = FALSE;
79 bool in_raw = 0, in_crlf = 0, out_raw = 0, out_crlf = 0;
81 char mode[PERL_MODE_MAX]; /* file mode ("r\0", "rb\0", "ab\0" etc.) */
84 PERL_ARGS_ASSERT_DO_OPENN;
86 Zero(mode,sizeof(mode),char);
87 PL_forkprocess = 1; /* assume true if no fork */
89 /* Collect default raw/crlf info from the op */
90 if (PL_op && PL_op->op_type == OP_OPEN) {
91 /* set up IO layers */
92 const U8 flags = PL_op->op_private;
93 in_raw = (flags & OPpOPEN_IN_RAW);
94 in_crlf = (flags & OPpOPEN_IN_CRLF);
95 out_raw = (flags & OPpOPEN_OUT_RAW);
96 out_crlf = (flags & OPpOPEN_OUT_CRLF);
99 /* If currently open - close before we re-open */
101 fd = PerlIO_fileno(IoIFP(io));
102 if (IoTYPE(io) == IoTYPE_STD) {
103 /* This is a clone of one of STD* handles */
106 else if (fd >= 0 && fd <= PL_maxsysfd) {
107 /* This is one of the original STD* handles */
110 savetype = IoTYPE(io);
114 else if (IoTYPE(io) == IoTYPE_PIPE)
115 result = PerlProc_pclose(IoIFP(io));
116 else if (IoIFP(io) != IoOFP(io)) {
118 result = PerlIO_close(IoOFP(io));
119 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
122 result = PerlIO_close(IoIFP(io));
125 result = PerlIO_close(IoIFP(io));
126 if (result == EOF && fd > PL_maxsysfd) {
127 /* Why is this not Perl_warn*() call ? */
128 PerlIO_printf(Perl_error_log,
129 "Warning: unable to close filehandle %"HEKf" properly.\n",
130 HEKfARG(GvENAME_HEK(gv))
133 IoOFP(io) = IoIFP(io) = NULL;
137 /* sysopen style args, i.e. integer mode and permissions */
139 const int appendtrunc =
141 #ifdef O_APPEND /* Not fully portable. */
144 #ifdef O_TRUNC /* Not fully portable. */
148 const int modifyingmode = O_WRONLY|O_RDWR|O_CREAT|appendtrunc;
152 Perl_croak(aTHX_ "panic: sysopen with multiple args, num_svs=%ld",
161 It might be (in OS/390 and Mac OS Classic it is)
167 This means that simple & with O_RDWR would look
168 like O_RDONLY is present. Therefore we have to
171 if ((ismodifying = (rawmode & modifyingmode))) {
172 if ((ismodifying & O_WRONLY) == O_WRONLY ||
173 (ismodifying & O_RDWR) == O_RDWR ||
174 (ismodifying & (O_CREAT|appendtrunc)))
175 TAINT_PROPER("sysopen");
177 mode[ix++] = IoTYPE_NUMERIC; /* Marker to openn to use numeric "sysopen" */
179 #if defined(USE_64_BIT_RAWIO) && defined(O_LARGEFILE)
180 rawmode |= O_LARGEFILE; /* Transparently largefiley. */
183 IoTYPE(io) = PerlIO_intmode2str(rawmode, &mode[ix], &writing);
185 namesv = newSVpvn_flags(oname, len, SVs_TEMP);
189 fp = PerlIO_openn(aTHX_ type, mode, -1, rawmode, rawperm, NULL, num_svs, svp);
192 /* Regular (non-sys) open */
198 type = savepvn(oname, len);
202 /* Lose leading and trailing white space */
203 while (isSPACE(*type))
205 while (tend > type && isSPACE(tend[-1]))
209 /* New style explicit name, type is just mode and layer info */
211 if (SvROK(*svp) && !strchr(oname,'&')) {
213 Perl_warner(aTHX_ packWARN(WARN_IO),
214 "Can't open a reference");
215 SETERRNO(EINVAL, LIB_INVARG);
218 #endif /* USE_STDIO */
219 name = (SvOK(*svp) || SvGMAGICAL(*svp)) ?
220 savesvpv (*svp) : savepvs ("");
228 if ((*type == IoTYPE_RDWR) && /* scary */
229 (*(type+1) == IoTYPE_RDONLY || *(type+1) == IoTYPE_WRONLY) &&
230 ((!num_svs || (tend > type+1 && tend[-1] != IoTYPE_PIPE)))) {
231 TAINT_PROPER("open");
236 if (*type == IoTYPE_PIPE) {
238 if (type[1] != IoTYPE_STD) {
240 Perl_croak(aTHX_ "Unknown open() mode '%.*s'", (int)olen, oname);
246 } while (isSPACE(*type));
252 /* command is missing 19990114 */
253 if (ckWARN(WARN_PIPE))
254 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open");
258 if (!(*name == '-' && name[1] == '\0') || num_svs)
260 TAINT_PROPER("piped open");
261 if (!num_svs && name[len-1] == '|') {
263 if (ckWARN(WARN_PIPE))
264 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Can't open bidirectional pipe");
273 fp = PerlProc_popen_list(mode, num_svs, svp);
276 fp = PerlProc_popen(name,mode);
280 if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) {
286 else if (*type == IoTYPE_WRONLY) {
287 TAINT_PROPER("open");
289 if (*type == IoTYPE_WRONLY) {
290 /* Two IoTYPE_WRONLYs in a row make for an IoTYPE_APPEND. */
291 mode[0] = IoTYPE(io) = IoTYPE_APPEND;
305 dodup = PERLIO_DUP_FD;
311 if (!num_svs && !*type && supplied_fp) {
312 /* "<+&" etc. is used by typemaps */
316 PerlIO *that_fp = NULL;
318 /* diag_listed_as: More than one argument to '%s' open */
319 Perl_croak(aTHX_ "More than one argument to '%c&' open",IoTYPE(io));
321 while (isSPACE(*type))
325 || (SvPOKp(*svp) && looks_like_number(*svp))
330 else if (isDIGIT(*type)) {
336 thatio = sv_2io(*svp);
339 GV * const thatgv = gv_fetchpvn_flags(type, tend - type,
341 thatio = GvIO(thatgv);
345 SETERRNO(EINVAL,SS_IVCHAN);
349 if ((that_fp = IoIFP(thatio))) {
350 /* Flush stdio buffer before dup. --mjd
351 * Unfortunately SEEK_CURing 0 seems to
352 * be optimized away on most platforms;
353 * only Solaris and Linux seem to flush
356 /* sfio fails to clear error on next
357 sfwrite, contrary to documentation.
359 if (PerlIO_seek(that_fp, 0, SEEK_CUR) == -1)
360 PerlIO_clearerr(that_fp);
362 /* On the other hand, do all platforms
363 * take gracefully to flushing a read-only
364 * filehandle? Perhaps we should do
365 * fsetpos(src)+fgetpos(dst)? --nik */
366 PerlIO_flush(that_fp);
367 fd = PerlIO_fileno(that_fp);
368 /* When dup()ing STDIN, STDOUT or STDERR
369 * explicitly set appropriate access mode */
370 if (that_fp == PerlIO_stdout()
371 || that_fp == PerlIO_stderr())
372 IoTYPE(io) = IoTYPE_WRONLY;
373 else if (that_fp == PerlIO_stdin())
374 IoTYPE(io) = IoTYPE_RDONLY;
375 /* When dup()ing a socket, say result is
377 else if (IoTYPE(thatio) == IoTYPE_SOCKET)
378 IoTYPE(io) = IoTYPE_SOCKET;
386 fp = PerlIO_fdupopen(aTHX_ that_fp, NULL, dodup);
390 fd = PerlLIO_dup(fd);
393 if (!(fp = PerlIO_openn(aTHX_ type,mode,fd,0,0,NULL,num_svs,svp))) {
394 if (dodup && fd >= 0)
401 while (isSPACE(*type))
403 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
405 fp = PerlIO_stdout();
406 IoTYPE(io) = IoTYPE_STD;
408 /* diag_listed_as: More than one argument to '%s' open */
409 Perl_croak(aTHX_ "More than one argument to '>%c' open",IoTYPE_STD);
414 namesv = newSVpvn_flags(type, tend - type, SVs_TEMP);
419 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
422 if (!fp && type && *type && *type != ':' && !isIDFIRST(*type))
423 goto unknown_open_mode;
424 } /* IoTYPE_WRONLY */
425 else if (*type == IoTYPE_RDONLY) {
428 } while (isSPACE(*type));
437 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
440 IoTYPE(io) = IoTYPE_STD;
442 /* diag_listed_as: More than one argument to '%s' open */
443 Perl_croak(aTHX_ "More than one argument to '<%c' open",IoTYPE_STD);
448 namesv = newSVpvn_flags(type, tend - type, SVs_TEMP);
453 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
455 if (!fp && type && *type && *type != ':' && !isIDFIRST(*type))
456 goto unknown_open_mode;
457 } /* IoTYPE_RDONLY */
458 else if ((num_svs && /* '-|...' or '...|' */
459 type[0] == IoTYPE_STD && type[1] == IoTYPE_PIPE) ||
460 (!num_svs && tend > type+1 && tend[-1] == IoTYPE_PIPE)) {
462 type += 2; /* skip over '-|' */
466 while (tend > type && isSPACE(tend[-1]))
468 for (; isSPACE(*type); type++)
474 /* command is missing 19990114 */
475 if (ckWARN(WARN_PIPE))
476 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open");
480 if (!(*name == '-' && name[1] == '\0') || num_svs)
482 TAINT_PROPER("piped open");
491 fp = PerlProc_popen_list(mode,num_svs,svp);
494 fp = PerlProc_popen(name,mode);
496 IoTYPE(io) = IoTYPE_PIPE;
498 while (isSPACE(*type))
501 if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) {
507 else { /* layer(Args) */
509 goto unknown_open_mode;
511 IoTYPE(io) = IoTYPE_RDONLY;
512 for (; isSPACE(*name); name++)
521 if (*name == '-' && name[1] == '\0') {
523 IoTYPE(io) = IoTYPE_STD;
527 namesv = newSVpvn_flags(type, tend - type, SVs_TEMP);
532 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
537 if (IoTYPE(io) == IoTYPE_RDONLY && ckWARN(WARN_NEWLINE)
538 && strchr(oname, '\n')
541 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
545 if (ckWARN(WARN_IO)) {
546 if ((IoTYPE(io) == IoTYPE_RDONLY) &&
547 (fp == PerlIO_stdout() || fp == PerlIO_stderr())) {
548 Perl_warner(aTHX_ packWARN(WARN_IO),
549 "Filehandle STD%s reopened as %"HEKf
551 ((fp == PerlIO_stdout()) ? "OUT" : "ERR"),
552 HEKfARG(GvENAME_HEK(gv)));
554 else if ((IoTYPE(io) == IoTYPE_WRONLY) && fp == PerlIO_stdin()) {
555 Perl_warner(aTHX_ packWARN(WARN_IO),
556 "Filehandle STDIN reopened as %"HEKf" only for output",
557 HEKfARG(GvENAME_HEK(gv))
562 fd = PerlIO_fileno(fp);
563 /* If there is no fd (e.g. PerlIO::scalar) assume it isn't a
564 * socket - this covers PerlIO::scalar - otherwise unless we "know" the
565 * type probe for socket-ness.
567 if (IoTYPE(io) && IoTYPE(io) != IoTYPE_PIPE && IoTYPE(io) != IoTYPE_STD && fd >= 0) {
568 if (PerlLIO_fstat(fd,&PL_statbuf) < 0) {
569 /* If PerlIO claims to have fd we had better be able to fstat() it. */
570 (void) PerlIO_close(fp);
574 if (S_ISSOCK(PL_statbuf.st_mode))
575 IoTYPE(io) = IoTYPE_SOCKET; /* in case a socket was passed in to us */
579 !(PL_statbuf.st_mode & S_IFMT)
583 && IoTYPE(io) != IoTYPE_WRONLY /* Dups of STD* filehandles already have */
584 && IoTYPE(io) != IoTYPE_RDONLY /* type so they aren't marked as sockets */
585 ) { /* on OS's that return 0 on fstat()ed pipe */
587 Sock_size_t buflen = sizeof tmpbuf;
588 if (PerlSock_getsockname(fd, (struct sockaddr *)tmpbuf, &buflen) >= 0
589 || errno != ENOTSOCK)
590 IoTYPE(io) = IoTYPE_SOCKET; /* some OS's return 0 on fstat()ed socket */
591 /* but some return 0 for streams too, sigh */
593 #endif /* HAS_SOCKET */
594 #endif /* !PERL_MICRO */
598 * If this is a standard handle we discard all the layer stuff
599 * and just dup the fd into whatever was on the handle before !
602 if (saveifp) { /* must use old fp? */
603 /* If fd is less that PL_maxsysfd i.e. STDIN..STDERR
604 then dup the new fileno down
607 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
608 if (saveofp != saveifp) { /* was a socket? */
609 PerlIO_close(saveofp);
613 /* Still a small can-of-worms here if (say) PerlIO::scalar
614 is assigned to (say) STDOUT - for now let dup2() fail
615 and provide the error
617 if (PerlLIO_dup2(fd, savefd) < 0) {
618 (void)PerlIO_close(fp);
622 if (savefd != PerlIO_fileno(PerlIO_stdin())) {
623 char newname[FILENAME_MAX+1];
624 if (PerlIO_getname(fp, newname)) {
625 if (fd == PerlIO_fileno(PerlIO_stdout()))
626 vmssetuserlnm("SYS$OUTPUT", newname);
627 if (fd == PerlIO_fileno(PerlIO_stderr()))
628 vmssetuserlnm("SYS$ERROR", newname);
634 /* PL_fdpid isn't used on Windows, so avoid this useless work.
635 * XXX Probably the same for a lot of other places. */
640 sv = *av_fetch(PL_fdpid,fd,TRUE);
641 SvUPGRADE(sv, SVt_IV);
644 sv = *av_fetch(PL_fdpid,savefd,TRUE);
645 SvUPGRADE(sv, SVt_IV);
651 /* need to close fp without closing underlying fd */
652 int ofd = PerlIO_fileno(fp);
653 int dupfd = PerlLIO_dup(ofd);
654 #if defined(HAS_FCNTL) && defined(F_SETFD)
655 /* Assume if we have F_SETFD we have F_GETFD */
656 int coe = fcntl(ofd,F_GETFD);
659 PerlLIO_dup2(dupfd,ofd);
660 #if defined(HAS_FCNTL) && defined(F_SETFD)
661 /* The dup trick has lost close-on-exec on ofd */
662 fcntl(ofd,F_SETFD, coe);
664 PerlLIO_close(dupfd);
671 fd = PerlIO_fileno(fp);
673 #if defined(HAS_FCNTL) && defined(F_SETFD)
676 fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
682 IoFLAGS(io) &= ~IOf_NOLINE;
684 if (IoTYPE(io) == IoTYPE_SOCKET
685 || (IoTYPE(io) == IoTYPE_WRONLY && fd >= 0 && S_ISCHR(PL_statbuf.st_mode)) ) {
687 if (*s == IoTYPE_IMPLICIT || *s == IoTYPE_NUMERIC)
690 if (!(IoOFP(io) = PerlIO_openn(aTHX_ type,s,fd,0,0,NULL,0,svp))) {
704 IoTYPE(io) = savetype;
709 Perl_nextargv(pTHX_ GV *gv)
713 #ifndef FLEXFILENAMES
719 IO * const io = GvIOp(gv);
721 PERL_ARGS_ASSERT_NEXTARGV;
724 PL_argvoutgv = gv_fetchpvs("ARGVOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
725 if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
726 IoFLAGS(io) &= ~IOf_START;
729 Perl_av_create_and_push(aTHX_ &PL_argvout_stack,
730 SvREFCNT_inc_simple_NN(PL_defoutgv));
733 if (PL_filemode & (S_ISUID|S_ISGID)) {
734 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
737 (void)fchmod(PL_lastfd,PL_filemode);
739 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
746 while (av_len(GvAV(gv)) >= 0) {
748 sv = av_shift(GvAV(gv));
750 SvTAINTED_off(GvSVn(gv)); /* previous tainting irrelevant */
751 sv_setsv(GvSVn(gv),sv);
752 SvSETMAGIC(GvSV(gv));
753 PL_oldname = SvPVx(GvSV(gv), oldlen);
754 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,NULL)) {
756 TAINT_PROPER("inplace open");
757 if (oldlen == 1 && *PL_oldname == '-') {
758 setdefout(gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL,
760 return IoIFP(GvIOp(gv));
762 #ifndef FLEXFILENAMES
763 filedev = PL_statbuf.st_dev;
764 fileino = PL_statbuf.st_ino;
766 PL_filemode = PL_statbuf.st_mode;
767 fileuid = PL_statbuf.st_uid;
768 filegid = PL_statbuf.st_gid;
769 if (!S_ISREG(PL_filemode)) {
770 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
771 "Can't do inplace edit: %s is not a regular file",
776 if (*PL_inplace && strNE(PL_inplace, "*")) {
777 const char *star = strchr(PL_inplace, '*');
779 const char *begin = PL_inplace;
782 sv_catpvn(sv, begin, star - begin);
783 sv_catpvn(sv, PL_oldname, oldlen);
785 } while ((star = strchr(begin, '*')));
790 sv_catpv(sv,PL_inplace);
792 #ifndef FLEXFILENAMES
793 if ((PerlLIO_stat(SvPVX_const(sv),&PL_statbuf) >= 0
794 && PL_statbuf.st_dev == filedev
795 && PL_statbuf.st_ino == fileino)
797 || ((_djstat_fail_bits & _STFAIL_TRUENAME)!=0)
801 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
802 "Can't do inplace edit: %"SVf" would not be unique",
809 #if !defined(DOSISH) && !defined(__CYGWIN__)
810 if (PerlLIO_rename(PL_oldname,SvPVX_const(sv)) < 0) {
811 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
812 "Can't rename %s to %"SVf": %s, skipping file",
813 PL_oldname, SVfARG(sv), Strerror(errno));
819 (void)PerlLIO_unlink(SvPVX_const(sv));
820 (void)PerlLIO_rename(PL_oldname,SvPVX_const(sv));
821 do_open(gv,(char*)SvPVX_const(sv),SvCUR(sv),TRUE,O_RDONLY,0,NULL);
824 (void)UNLINK(SvPVX_const(sv));
825 if (link(PL_oldname,SvPVX_const(sv)) < 0) {
826 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
827 "Can't rename %s to %"SVf": %s, skipping file",
828 PL_oldname, SVfARG(sv), Strerror(errno) );
832 (void)UNLINK(PL_oldname);
836 #if !defined(DOSISH) && !defined(AMIGAOS)
837 # ifndef VMS /* Don't delete; use automatic file versioning */
838 if (UNLINK(PL_oldname) < 0) {
839 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
840 "Can't remove %s: %s, skipping file",
841 PL_oldname, Strerror(errno) );
847 Perl_croak(aTHX_ "Can't do inplace edit without backup");
851 sv_setpvn(sv,PL_oldname,oldlen);
852 SETERRNO(0,0); /* in case sprintf set errno */
853 if (!Perl_do_openn(aTHX_ PL_argvoutgv, (char*)SvPVX_const(sv),
856 O_WRONLY|O_CREAT|O_TRUNC,0,
858 O_WRONLY|O_CREAT|OPEN_EXCL,0600,
861 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE), "Can't do inplace edit on %s: %s",
862 PL_oldname, Strerror(errno) );
866 setdefout(PL_argvoutgv);
867 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
868 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
870 (void)fchmod(PL_lastfd,PL_filemode);
872 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
874 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
876 (void)fchown(PL_lastfd,fileuid,filegid);
879 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
884 return IoIFP(GvIOp(gv));
887 if (ckWARN_d(WARN_INPLACE)) {
888 const int eno = errno;
889 if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0
890 && !S_ISREG(PL_statbuf.st_mode))
892 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
893 "Can't do inplace edit: %s is not a regular file",
897 Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't open %s: %s",
898 PL_oldname, Strerror(eno));
902 if (io && (IoFLAGS(io) & IOf_ARGV))
903 IoFLAGS(io) |= IOf_START;
905 (void)do_close(PL_argvoutgv,FALSE);
906 if (io && (IoFLAGS(io) & IOf_ARGV)
907 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
909 GV * const oldout = MUTABLE_GV(av_pop(PL_argvout_stack));
911 SvREFCNT_dec_NN(oldout);
914 setdefout(gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO));
919 /* explicit renamed to avoid C++ conflict -- kja */
921 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
929 if (!gv || !isGV_with_GP(gv)) {
931 SETERRNO(EBADF,SS_IVCHAN);
935 if (!io) { /* never opened */
938 SETERRNO(EBADF,SS_IVCHAN);
942 retval = io_close(io, not_implicit);
946 IoLINES_LEFT(io) = IoPAGE_LEN(io);
948 IoTYPE(io) = IoTYPE_CLOSED;
953 Perl_io_close(pTHX_ IO *io, bool not_implicit)
958 PERL_ARGS_ASSERT_IO_CLOSE;
961 if (IoTYPE(io) == IoTYPE_PIPE) {
962 const int status = PerlProc_pclose(IoIFP(io));
964 STATUS_NATIVE_CHILD_SET(status);
965 retval = (STATUS_UNIX == 0);
968 retval = (status != -1);
971 else if (IoTYPE(io) == IoTYPE_STD)
974 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
975 const bool prev_err = PerlIO_error(IoOFP(io));
976 retval = (PerlIO_close(IoOFP(io)) != EOF && !prev_err);
977 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
980 const bool prev_err = PerlIO_error(IoIFP(io));
981 retval = (PerlIO_close(IoIFP(io)) != EOF && !prev_err);
984 IoOFP(io) = IoIFP(io) = NULL;
986 else if (not_implicit) {
987 SETERRNO(EBADF,SS_IVCHAN);
994 Perl_do_eof(pTHX_ GV *gv)
997 IO * const io = GvIO(gv);
999 PERL_ARGS_ASSERT_DO_EOF;
1003 else if (IoTYPE(io) == IoTYPE_WRONLY)
1004 report_wrongway_fh(gv, '>');
1007 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
1008 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
1009 return FALSE; /* this is the most usual case */
1013 /* getc and ungetc can stomp on errno */
1015 const int ch = PerlIO_getc(IoIFP(io));
1017 (void)PerlIO_ungetc(IoIFP(io),ch);
1024 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
1025 if (PerlIO_get_cnt(IoIFP(io)) < -1)
1026 PerlIO_set_cnt(IoIFP(io),-1);
1028 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
1029 if (gv != PL_argvgv || !nextargv(gv)) /* get another fp handy */
1033 return TRUE; /* normal fp, definitely end of file */
1039 Perl_do_tell(pTHX_ GV *gv)
1042 IO *const io = GvIO(gv);
1045 PERL_ARGS_ASSERT_DO_TELL;
1047 if (io && (fp = IoIFP(io))) {
1048 #ifdef ULTRIX_STDIO_BOTCH
1050 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1052 return PerlIO_tell(fp);
1055 SETERRNO(EBADF,RMS_IFI);
1060 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
1063 IO *const io = GvIO(gv);
1066 if (io && (fp = IoIFP(io))) {
1067 #ifdef ULTRIX_STDIO_BOTCH
1069 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1071 return PerlIO_seek(fp, pos, whence) >= 0;
1074 SETERRNO(EBADF,RMS_IFI);
1079 Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
1082 IO *const io = GvIO(gv);
1085 PERL_ARGS_ASSERT_DO_SYSSEEK;
1087 if (io && (fp = IoIFP(io)))
1088 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
1090 SETERRNO(EBADF,RMS_IFI);
1095 Perl_mode_from_discipline(pTHX_ const char *s, STRLEN len)
1097 int mode = O_BINARY;
1103 if (s[2] == 'a' && s[3] == 'w'
1104 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1113 if (s[2] == 'r' && s[3] == 'l' && s[4] == 'f'
1114 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1123 goto fail_discipline;
1126 else if (isSPACE(*s)) {
1133 end = strchr(s+1, ':');
1136 #ifndef PERLIO_LAYERS
1137 Perl_croak(aTHX_ "IO layers (like '%.*s') unavailable", end-s, s);
1148 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE)
1150 my_chsize(int fd, Off_t length)
1153 /* code courtesy of William Kucharski */
1158 if (PerlLIO_fstat(fd, &filebuf) < 0)
1161 if (filebuf.st_size < length) {
1163 /* extend file length */
1165 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
1168 /* write a "0" byte */
1170 if ((PerlLIO_write(fd, "", 1)) != 1)
1174 /* truncate length */
1178 fl.l_start = length;
1179 fl.l_type = F_WRLCK; /* write lock on file space */
1182 * This relies on the UNDOCUMENTED F_FREESP argument to
1183 * fcntl(2), which truncates the file so that it ends at the
1184 * position indicated by fl.l_start.
1186 * Will minor miracles never cease?
1189 if (fcntl(fd, F_FREESP, &fl) < 0)
1195 Perl_croak_nocontext("truncate not implemented");
1196 #endif /* F_FREESP */
1199 #endif /* !HAS_TRUNCATE && !HAS_CHSIZE */
1202 Perl_do_print(pTHX_ SV *sv, PerlIO *fp)
1206 PERL_ARGS_ASSERT_DO_PRINT;
1208 /* assuming fp is checked earlier */
1211 if (SvTYPE(sv) == SVt_IV && SvIOK(sv)) {
1212 assert(!SvGMAGICAL(sv));
1214 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
1216 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
1217 return !PerlIO_error(fp);
1221 /* Do this first to trigger any overloading. */
1222 const char *tmps = SvPV_const(sv, len);
1226 if (PerlIO_isutf8(fp)) { /* If the stream is utf8 ... */
1227 if (!SvUTF8(sv)) { /* Convert to utf8 if necessary */
1228 /* We don't modify the original scalar. */
1229 tmpbuf = bytes_to_utf8((const U8*) tmps, &len);
1230 tmps = (char *) tmpbuf;
1232 else if (ckWARN4_d(WARN_UTF8, WARN_SURROGATE, WARN_NON_UNICODE, WARN_NONCHAR)) {
1233 (void) check_utf8_print((const U8*) tmps, len);
1235 } /* else stream isn't utf8 */
1236 else if (DO_UTF8(sv)) { /* But if is utf8 internally, attempt to
1238 STRLEN tmplen = len;
1240 U8 * const result = bytes_from_utf8((const U8*) tmps, &tmplen, &utf8);
1243 /* Here, succeeded in downgrading from utf8. Set up to below
1244 * output the converted value */
1246 tmps = (char *) tmpbuf;
1249 else { /* Non-utf8 output stream, but string only representable in
1251 assert((char *)result == tmps);
1252 Perl_ck_warner_d(aTHX_ packWARN(WARN_UTF8),
1253 "Wide character in %s",
1254 PL_op ? OP_DESC(PL_op) : "print"
1256 /* Could also check that isn't one of the things to avoid
1257 * in utf8 by using check_utf8_print(), but not doing so,
1258 * since the stream isn't a UTF8 stream */
1261 /* To detect whether the process is about to overstep its
1262 * filesize limit we would need getrlimit(). We could then
1263 * also transparently raise the limit with setrlimit() --
1264 * but only until the system hard limit/the filesystem limit,
1265 * at which we would get EPERM. Note that when using buffered
1266 * io the write failure can be delayed until the flush/close. --jhi */
1267 if (len && (PerlIO_write(fp,tmps,len) == 0))
1270 return happy ? !PerlIO_error(fp) : FALSE;
1275 Perl_my_stat_flags(pTHX_ const U32 flags)
1282 if (PL_op->op_flags & OPf_REF) {
1286 return PL_laststatval;
1289 PL_laststype = OP_STAT;
1290 PL_statgv = gv ? gv : (GV *)io;
1291 sv_setpvs(PL_statname, "");
1294 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
1295 } else if (IoDIRP(io)) {
1296 return (PL_laststatval = PerlLIO_fstat(my_dirfd(IoDIRP(io)), &PL_statcache));
1299 PL_laststatval = -1;
1303 else if ((PL_op->op_private & (OPpFT_STACKED|OPpFT_AFTER_t))
1305 return PL_laststatval;
1307 SV* const sv = TOPs;
1310 if ((gv = MAYBE_DEREF_GV_flags(sv,flags))) {
1313 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
1314 io = MUTABLE_IO(SvRV(sv));
1316 goto do_fstat_have_io;
1319 s = SvPV_flags_const(sv, len, flags);
1321 sv_setpvn(PL_statname, s, len);
1322 s = SvPVX_const(PL_statname); /* s now NUL-terminated */
1323 PL_laststype = OP_STAT;
1324 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1325 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
1326 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
1327 return PL_laststatval;
1333 Perl_my_lstat_flags(pTHX_ const U32 flags)
1336 static const char* const no_prev_lstat = "The stat preceding -l _ wasn't an lstat";
1339 SV* const sv = TOPs;
1341 if (PL_op->op_flags & OPf_REF) {
1342 if (cGVOP_gv == PL_defgv) {
1343 if (PL_laststype != OP_LSTAT)
1344 Perl_croak(aTHX_ "%s", no_prev_lstat);
1345 return PL_laststatval;
1347 PL_laststatval = -1;
1348 if (ckWARN(WARN_IO)) {
1349 /* diag_listed_as: Use of -l on filehandle%s */
1350 Perl_warner(aTHX_ packWARN(WARN_IO),
1351 "Use of -l on filehandle %"HEKf,
1352 HEKfARG(GvENAME_HEK(cGVOP_gv)));
1356 if ((PL_op->op_private & (OPpFT_STACKED|OPpFT_AFTER_t))
1358 if (PL_laststype != OP_LSTAT)
1359 Perl_croak(aTHX_ "%s", no_prev_lstat);
1360 return PL_laststatval;
1363 PL_laststype = OP_LSTAT;
1365 if ( ( (SvROK(sv) && ( isGV_with_GP(SvRV(sv))
1366 || (isio = SvTYPE(SvRV(sv)) == SVt_PVIO) )
1370 && ckWARN(WARN_IO)) {
1372 /* diag_listed_as: Use of -l on filehandle%s */
1373 Perl_warner(aTHX_ packWARN(WARN_IO),
1374 "Use of -l on filehandle");
1376 /* diag_listed_as: Use of -l on filehandle%s */
1377 Perl_warner(aTHX_ packWARN(WARN_IO),
1378 "Use of -l on filehandle %"HEKf,
1379 GvENAME_HEK((const GV *)
1380 (SvROK(sv) ? SvRV(sv) : sv)));
1382 file = SvPV_flags_const_nolen(sv, flags);
1383 sv_setpv(PL_statname,file);
1384 PL_laststatval = PerlLIO_lstat(file,&PL_statcache);
1385 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(file, '\n'))
1386 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "lstat");
1387 return PL_laststatval;
1391 S_exec_failed(pTHX_ const char *cmd, int fd, int do_report)
1393 const int e = errno;
1394 PERL_ARGS_ASSERT_EXEC_FAILED;
1395 if (ckWARN(WARN_EXEC))
1396 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
1399 PerlLIO_write(fd, (void*)&e, sizeof(int));
1405 Perl_do_aexec5(pTHX_ SV *really, SV **mark, SV **sp,
1406 int fd, int do_report)
1409 PERL_ARGS_ASSERT_DO_AEXEC5;
1410 #if defined(__SYMBIAN32__) || defined(__LIBCATAMOUNT__)
1411 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1415 const char *tmps = NULL;
1416 Newx(PL_Argv, sp - mark + 1, const char*);
1419 while (++mark <= sp) {
1421 *a++ = SvPV_nolen_const(*mark);
1427 tmps = SvPV_nolen_const(really);
1428 if ((!really && *PL_Argv[0] != '/') ||
1429 (really && *tmps != '/')) /* will execvp use PATH? */
1430 TAINT_ENV(); /* testing IFS here is overkill, probably */
1432 if (really && *tmps)
1433 PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
1435 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1437 S_exec_failed(aTHX_ (really ? tmps : PL_Argv[0]), fd, do_report);
1445 Perl_do_execfree(pTHX)
1454 #ifdef PERL_DEFAULT_DO_EXEC3_IMPLEMENTATION
1457 Perl_do_exec3(pTHX_ const char *incmd, int fd, int do_report)
1464 /* Make a copy so we can change it */
1465 const Size_t cmdlen = strlen(incmd) + 1;
1467 PERL_ARGS_ASSERT_DO_EXEC3;
1469 Newx(buf, cmdlen, char);
1471 memcpy(cmd, incmd, cmdlen);
1473 while (*cmd && isSPACE(*cmd))
1476 /* save an extra exec if possible */
1480 char flags[PERL_FLAGS_MAX];
1481 if (strnEQ(cmd,PL_cshname,PL_cshlen) &&
1482 strnEQ(cmd+PL_cshlen," -c",3)) {
1483 my_strlcpy(flags, "-c", PERL_FLAGS_MAX);
1484 s = cmd+PL_cshlen+3;
1487 my_strlcat(flags, "f", PERL_FLAGS_MAX - 2);
1492 char * const ncmd = s;
1498 if (s[-1] == '\'') {
1501 PerlProc_execl(PL_cshname, "csh", flags, ncmd, (char*)NULL);
1504 S_exec_failed(aTHX_ PL_cshname, fd, do_report);
1513 /* see if there are shell metacharacters in it */
1515 if (*cmd == '.' && isSPACE(cmd[1]))
1518 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1522 while (isWORDCHAR(*s))
1523 s++; /* catch VAR=val gizmo */
1527 for (s = cmd; *s; s++) {
1528 if (*s != ' ' && !isALPHA(*s) &&
1529 strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1530 if (*s == '\n' && !s[1]) {
1534 /* handle the 2>&1 construct at the end */
1535 if (*s == '>' && s[1] == '&' && s[2] == '1'
1536 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1537 && (!s[3] || isSPACE(s[3])))
1539 const char *t = s + 3;
1541 while (*t && isSPACE(*t))
1543 if (!*t && (PerlLIO_dup2(1,2) != -1)) {
1550 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char *)NULL);
1552 S_exec_failed(aTHX_ PL_sh_path, fd, do_report);
1558 Newx(PL_Argv, (s - cmd) / 2 + 2, const char*);
1559 PL_Cmd = savepvn(cmd, s-cmd);
1561 for (s = PL_Cmd; *s;) {
1566 while (*s && !isSPACE(*s))
1574 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1576 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1580 S_exec_failed(aTHX_ PL_Argv[0], fd, do_report);
1587 #endif /* OS2 || WIN32 */
1590 #include <starlet.h> /* for sys$delprc */
1594 Perl_apply(pTHX_ I32 type, SV **mark, SV **sp)
1599 const char *const what = PL_op_name[type];
1602 SV ** const oldmark = mark;
1603 bool killgp = FALSE;
1605 PERL_ARGS_ASSERT_APPLY;
1607 PERL_UNUSED_VAR(what); /* may not be used depending on compile options */
1609 /* Doing this ahead of the switch statement preserves the old behaviour,
1610 where attempting to use kill as a taint test test would fail on
1611 platforms where kill was not defined. */
1613 if (type == OP_KILL)
1614 Perl_die(aTHX_ PL_no_func, what);
1617 if (type == OP_CHOWN)
1618 Perl_die(aTHX_ PL_no_func, what);
1622 #define APPLY_TAINT_PROPER() \
1624 if (TAINT_get) { TAINT_PROPER(what); } \
1627 /* This is a first heuristic; it doesn't catch tainting magic. */
1629 while (++mark <= sp) {
1630 if (SvTAINTED(*mark)) {
1639 APPLY_TAINT_PROPER();
1642 APPLY_TAINT_PROPER();
1644 while (++mark <= sp) {
1646 if ((gv = MAYBE_DEREF_GV(*mark))) {
1647 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1649 APPLY_TAINT_PROPER();
1650 if (fchmod(PerlIO_fileno(IoIFP(GvIOn(gv))), val))
1653 Perl_die(aTHX_ PL_no_func, "fchmod");
1661 const char *name = SvPV_nomg_const_nolen(*mark);
1662 APPLY_TAINT_PROPER();
1663 if (PerlLIO_chmod(name, val))
1671 APPLY_TAINT_PROPER();
1672 if (sp - mark > 2) {
1674 val = SvIVx(*++mark);
1675 val2 = SvIVx(*++mark);
1676 APPLY_TAINT_PROPER();
1678 while (++mark <= sp) {
1680 if ((gv = MAYBE_DEREF_GV(*mark))) {
1681 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1683 APPLY_TAINT_PROPER();
1684 if (fchown(PerlIO_fileno(IoIFP(GvIOn(gv))), val, val2))
1687 Perl_die(aTHX_ PL_no_func, "fchown");
1695 const char *name = SvPV_nomg_const_nolen(*mark);
1696 APPLY_TAINT_PROPER();
1697 if (PerlLIO_chown(name, val, val2))
1705 XXX Should we make lchown() directly available from perl?
1706 For now, we'll let Configure test for HAS_LCHOWN, but do
1707 nothing in the core.
1712 APPLY_TAINT_PROPER();
1715 s = SvPVx_const(*++mark, len);
1716 if (*s == '-' && isALPHA(s[1]))
1723 if (*s == 'S' && s[1] == 'I' && s[2] == 'G') {
1727 if ((val = whichsig_pvn(s, len)) < 0)
1728 Perl_croak(aTHX_ "Unrecognized signal name \"%"SVf"\"", SVfARG(*mark));
1739 APPLY_TAINT_PROPER();
1742 /* kill() doesn't do process groups (job trees?) under VMS */
1743 if (val == SIGKILL) {
1744 /* Use native sys$delprc() to insure that target process is
1745 * deleted; supervisor-mode images don't pay attention to
1746 * CRTL's emulation of Unix-style signals and kill()
1748 while (++mark <= sp) {
1750 unsigned long int __vmssts;
1752 if (!(SvIOK(*mark) || SvNOK(*mark) || looks_like_number(*mark)))
1753 Perl_croak(aTHX_ "Can't kill a non-numeric process ID");
1754 proc = SvIV_nomg(*mark);
1755 APPLY_TAINT_PROPER();
1756 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1760 case SS$_NOSUCHNODE:
1761 SETERRNO(ESRCH,__vmssts);
1764 SETERRNO(EPERM,__vmssts);
1767 SETERRNO(EVMSERR,__vmssts);
1775 while (++mark <= sp) {
1778 if (!(SvNIOK(*mark) || looks_like_number(*mark)))
1779 Perl_croak(aTHX_ "Can't kill a non-numeric process ID");
1780 proc = SvIV_nomg(*mark);
1785 APPLY_TAINT_PROPER();
1786 if (PerlProc_kill(proc, val))
1793 APPLY_TAINT_PROPER();
1795 while (++mark <= sp) {
1796 s = SvPV_nolen_const(*mark);
1797 APPLY_TAINT_PROPER();
1798 if (PerlProc_geteuid() || PL_unsafe) {
1802 else { /* don't let root wipe out directories without -U */
1803 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1812 #if defined(HAS_UTIME) || defined(HAS_FUTIMES)
1814 APPLY_TAINT_PROPER();
1815 if (sp - mark > 2) {
1816 #if defined(HAS_FUTIMES)
1817 struct timeval utbuf[2];
1818 void *utbufp = utbuf;
1819 #elif defined(I_UTIME) || defined(VMS)
1820 struct utimbuf utbuf;
1821 struct utimbuf *utbufp = &utbuf;
1827 void *utbufp = &utbuf;
1830 SV* const accessed = *++mark;
1831 SV* const modified = *++mark;
1833 /* Be like C, and if both times are undefined, let the C
1834 * library figure out what to do. This usually means
1835 * "current time". */
1837 if ( accessed == &PL_sv_undef && modified == &PL_sv_undef )
1840 Zero(&utbuf, sizeof utbuf, char);
1842 utbuf[0].tv_sec = (long)SvIV(accessed); /* time accessed */
1843 utbuf[0].tv_usec = 0;
1844 utbuf[1].tv_sec = (long)SvIV(modified); /* time modified */
1845 utbuf[1].tv_usec = 0;
1846 #elif defined(BIG_TIME)
1847 utbuf.actime = (Time_t)SvNV(accessed); /* time accessed */
1848 utbuf.modtime = (Time_t)SvNV(modified); /* time modified */
1850 utbuf.actime = (Time_t)SvIV(accessed); /* time accessed */
1851 utbuf.modtime = (Time_t)SvIV(modified); /* time modified */
1854 APPLY_TAINT_PROPER();
1856 while (++mark <= sp) {
1858 if ((gv = MAYBE_DEREF_GV(*mark))) {
1859 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1861 APPLY_TAINT_PROPER();
1862 if (futimes(PerlIO_fileno(IoIFP(GvIOn(gv))),
1863 (struct timeval *) utbufp))
1866 Perl_die(aTHX_ PL_no_func, "futimes");
1874 const char * const name = SvPV_nomg_const_nolen(*mark);
1875 APPLY_TAINT_PROPER();
1877 if (utimes(name, (struct timeval *)utbufp))
1879 if (PerlLIO_utime(name, utbufp))
1893 #undef APPLY_TAINT_PROPER
1896 /* Do the permissions allow some operation? Assumes statcache already set. */
1897 #ifndef VMS /* VMS' cando is in vms.c */
1899 Perl_cando(pTHX_ Mode_t mode, bool effective, const Stat_t *statbufp)
1900 /* effective is a flag, true for EUID, or for checking if the effective gid
1901 * is in the list of groups returned from getgroups().
1906 PERL_ARGS_ASSERT_CANDO;
1909 /* [Comments and code from Len Reed]
1910 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1911 * to write-protected files. The execute permission bit is set
1912 * by the Microsoft C library stat() function for the following:
1917 * All files and directories are readable.
1918 * Directories and special files, e.g. "CON", cannot be
1920 * [Comment by Tom Dinger -- a directory can have the write-protect
1921 * bit set in the file system, but DOS permits changes to
1922 * the directory anyway. In addition, all bets are off
1923 * here for networked software, such as Novell and
1927 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1928 * too so it will actually look into the files for magic numbers
1930 return (mode & statbufp->st_mode) ? TRUE : FALSE;
1932 #else /* ! DOSISH */
1934 if (ingroup(544,effective)) { /* member of Administrators */
1936 if ((effective ? PerlProc_geteuid() : PerlProc_getuid()) == 0) { /* root is special */
1938 if (mode == S_IXUSR) {
1939 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1943 return TRUE; /* root reads and writes anything */
1946 if (statbufp->st_uid == (effective ? PerlProc_geteuid() : PerlProc_getuid()) ) {
1947 if (statbufp->st_mode & mode)
1948 return TRUE; /* ok as "user" */
1950 else if (ingroup(statbufp->st_gid,effective)) {
1951 if (statbufp->st_mode & mode >> 3)
1952 return TRUE; /* ok as "group" */
1954 else if (statbufp->st_mode & mode >> 6)
1955 return TRUE; /* ok as "other" */
1957 #endif /* ! DOSISH */
1962 S_ingroup(pTHX_ Gid_t testgid, bool effective)
1965 if (testgid == (effective ? PerlProc_getegid() : PerlProc_getgid()))
1967 #ifdef HAS_GETGROUPS
1969 Groups_t *gary = NULL;
1973 anum = getgroups(0, gary);
1974 Newx(gary, anum, Groups_t);
1975 anum = getgroups(anum, gary);
1977 if (gary[anum] == testgid) {
1990 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1993 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
1996 const key_t key = (key_t)SvNVx(*++mark);
1997 SV *nsv = optype == OP_MSGGET ? NULL : *++mark;
1998 const I32 flags = SvIVx(*++mark);
2000 PERL_ARGS_ASSERT_DO_IPCGET;
2001 PERL_UNUSED_ARG(sp);
2008 return msgget(key, flags);
2012 return semget(key, (int) SvIV(nsv), flags);
2016 return shmget(key, (size_t) SvUV(nsv), flags);
2018 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
2020 /* diag_listed_as: msg%s not implemented */
2021 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2024 return -1; /* should never happen */
2028 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
2033 const I32 id = SvIVx(*++mark);
2035 const I32 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
2037 const I32 cmd = SvIVx(*++mark);
2038 SV * const astr = *++mark;
2039 STRLEN infosize = 0;
2040 I32 getinfo = (cmd == IPC_STAT);
2042 PERL_ARGS_ASSERT_DO_IPCCTL;
2043 PERL_UNUSED_ARG(sp);
2049 if (cmd == IPC_STAT || cmd == IPC_SET)
2050 infosize = sizeof(struct msqid_ds);
2055 if (cmd == IPC_STAT || cmd == IPC_SET)
2056 infosize = sizeof(struct shmid_ds);
2062 if (cmd == IPC_STAT || cmd == IPC_SET)
2063 infosize = sizeof(struct semid_ds);
2064 else if (cmd == GETALL || cmd == SETALL)
2066 struct semid_ds semds;
2068 #ifdef EXTRA_F_IN_SEMUN_BUF
2069 semun.buff = &semds;
2073 getinfo = (cmd == GETALL);
2074 if (Semctl(id, 0, IPC_STAT, semun) == -1)
2076 infosize = semds.sem_nsems * sizeof(short);
2077 /* "short" is technically wrong but much more portable
2078 than guessing about u_?short(_t)? */
2081 /* diag_listed_as: sem%s not implemented */
2082 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2086 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
2088 /* diag_listed_as: shm%s not implemented */
2089 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2097 SvPV_force_nolen(astr);
2098 a = SvGROW(astr, infosize+1);
2103 a = SvPV(astr, len);
2104 if (len != infosize)
2105 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
2113 const IV i = SvIV(astr);
2114 a = INT2PTR(char *,i); /* ouch */
2121 ret = msgctl(id, cmd, (struct msqid_ds *)a);
2127 union semun unsemds;
2129 #ifdef EXTRA_F_IN_SEMUN_BUF
2130 unsemds.buff = (struct semid_ds *)a;
2132 unsemds.buf = (struct semid_ds *)a;
2134 ret = Semctl(id, n, cmd, unsemds);
2136 /* diag_listed_as: sem%s not implemented */
2137 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2144 ret = shmctl(id, cmd, (struct shmid_ds *)a);
2148 if (getinfo && ret >= 0) {
2149 SvCUR_set(astr, infosize);
2150 *SvEND(astr) = '\0';
2157 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
2162 const I32 id = SvIVx(*++mark);
2163 SV * const mstr = *++mark;
2164 const I32 flags = SvIVx(*++mark);
2165 const char * const mbuf = SvPV_const(mstr, len);
2166 const I32 msize = len - sizeof(long);
2168 PERL_ARGS_ASSERT_DO_MSGSND;
2169 PERL_UNUSED_ARG(sp);
2172 Perl_croak(aTHX_ "Arg too short for msgsnd");
2174 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
2176 PERL_UNUSED_ARG(sp);
2177 PERL_UNUSED_ARG(mark);
2178 /* diag_listed_as: msg%s not implemented */
2179 Perl_croak(aTHX_ "msgsnd not implemented");
2185 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
2191 I32 msize, flags, ret;
2192 const I32 id = SvIVx(*++mark);
2193 SV * const mstr = *++mark;
2195 PERL_ARGS_ASSERT_DO_MSGRCV;
2196 PERL_UNUSED_ARG(sp);
2198 /* suppress warning when reading into undef var --jhi */
2200 sv_setpvs(mstr, "");
2201 msize = SvIVx(*++mark);
2202 mtype = (long)SvIVx(*++mark);
2203 flags = SvIVx(*++mark);
2204 SvPV_force_nolen(mstr);
2205 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
2208 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
2210 SvCUR_set(mstr, sizeof(long)+ret);
2211 *SvEND(mstr) = '\0';
2212 #ifndef INCOMPLETE_TAINTS
2213 /* who knows who has been playing with this message? */
2219 PERL_UNUSED_ARG(sp);
2220 PERL_UNUSED_ARG(mark);
2221 /* diag_listed_as: msg%s not implemented */
2222 Perl_croak(aTHX_ "msgrcv not implemented");
2228 Perl_do_semop(pTHX_ SV **mark, SV **sp)
2233 const I32 id = SvIVx(*++mark);
2234 SV * const opstr = *++mark;
2235 const char * const opbuf = SvPV_const(opstr, opsize);
2237 PERL_ARGS_ASSERT_DO_SEMOP;
2238 PERL_UNUSED_ARG(sp);
2240 if (opsize < 3 * SHORTSIZE
2241 || (opsize % (3 * SHORTSIZE))) {
2242 SETERRNO(EINVAL,LIB_INVARG);
2246 /* We can't assume that sizeof(struct sembuf) == 3 * sizeof(short). */
2248 const int nsops = opsize / (3 * sizeof (short));
2250 short * const ops = (short *) opbuf;
2252 struct sembuf *temps, *t;
2255 Newx (temps, nsops, struct sembuf);
2263 result = semop(id, temps, nsops);
2277 /* diag_listed_as: sem%s not implemented */
2278 Perl_croak(aTHX_ "semop not implemented");
2283 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
2288 struct shmid_ds shmds;
2289 const I32 id = SvIVx(*++mark);
2290 SV * const mstr = *++mark;
2291 const I32 mpos = SvIVx(*++mark);
2292 const I32 msize = SvIVx(*++mark);
2294 PERL_ARGS_ASSERT_DO_SHMIO;
2295 PERL_UNUSED_ARG(sp);
2298 if (shmctl(id, IPC_STAT, &shmds) == -1)
2300 if (mpos < 0 || msize < 0
2301 || (size_t)mpos + msize > (size_t)shmds.shm_segsz) {
2302 SETERRNO(EFAULT,SS_ACCVIO); /* can't do as caller requested */
2305 shm = (char *)shmat(id, NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
2306 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2308 if (optype == OP_SHMREAD) {
2310 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2312 SvUPGRADE(mstr, SVt_PV);
2314 sv_setpvs(mstr, "");
2316 mbuf = SvGROW(mstr, (STRLEN)msize+1);
2318 Copy(shm + mpos, mbuf, msize, char);
2319 SvCUR_set(mstr, msize);
2320 *SvEND(mstr) = '\0';
2322 #ifndef INCOMPLETE_TAINTS
2323 /* who knows who has been playing with this shared memory? */
2330 const char *mbuf = SvPV_const(mstr, len);
2331 const I32 n = ((I32)len > msize) ? msize : (I32)len;
2332 Copy(mbuf, shm + mpos, n, char);
2334 memzero(shm + mpos + n, msize - n);
2338 /* diag_listed_as: shm%s not implemented */
2339 Perl_croak(aTHX_ "shm I/O not implemented");
2344 #endif /* SYSV IPC */
2349 =for apidoc start_glob
2351 Function called by C<do_readline> to spawn a glob (or do the glob inside
2352 perl on VMS). This code used to be inline, but now perl uses C<File::Glob>
2353 this glob starter is only used by miniperl during the build process.
2354 Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up.
2360 Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
2363 SV * const tmpcmd = newSV(0);
2366 PERL_ARGS_ASSERT_START_GLOB;
2370 #ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
2371 /* since spawning off a process is a real performance hit */
2378 fp = Perl_vms_start_glob(aTHX_ tmpglob, io);
2383 sv_setpv(tmpcmd, "for a in ");
2384 sv_catsv(tmpcmd, tmpglob);
2385 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
2388 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
2389 sv_catsv(tmpcmd, tmpglob);
2391 sv_setpv(tmpcmd, "perlglob ");
2392 sv_catsv(tmpcmd, tmpglob);
2393 sv_catpv(tmpcmd, " |");
2398 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
2399 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
2400 sv_catsv(tmpcmd, tmpglob);
2401 sv_catpv(tmpcmd, "' 2>/dev/null |");
2403 sv_setpv(tmpcmd, "echo ");
2404 sv_catsv(tmpcmd, tmpglob);
2406 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
2408 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
2411 #endif /* !DOSISH */
2413 GV * const envgv = gv_fetchpvs("ENV", 0, SVt_PVHV);
2414 SV ** const home = hv_fetchs(GvHV(envgv), "HOME", 0);
2415 SV ** const path = hv_fetchs(GvHV(envgv), "PATH", 0);
2416 if (home && *home) SvGETMAGIC(*home);
2417 if (path && *path) SvGETMAGIC(*path);
2418 save_hash(gv_fetchpvs("ENV", 0, SVt_PVHV));
2419 if (home && *home) SvSETMAGIC(*home);
2420 if (path && *path) SvSETMAGIC(*path);
2422 (void)do_open(PL_last_in_gv, (char*)SvPVX_const(tmpcmd), SvCUR(tmpcmd),
2423 FALSE, O_RDONLY, 0, NULL);
2432 * c-indentation-style: bsd
2434 * indent-tabs-mode: nil
2437 * ex: set ts=8 sts=4 sw=4 et: