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, register const char *oname, I32 len, int as_raw,
65 int rawmode, int rawperm, PerlIO *supplied_fp, SV **svp,
69 register 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 Perl_vmssetuserlnm(aTHX_ "SYS$OUTPUT", newname);
627 if (fd == PerlIO_fileno(PerlIO_stderr()))
628 Perl_vmssetuserlnm(aTHX_ "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_ register 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 sv_setsv(GvSVn(gv),sv);
751 SvSETMAGIC(GvSV(gv));
752 PL_oldname = SvPVx(GvSV(gv), oldlen);
753 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,NULL)) {
755 TAINT_PROPER("inplace open");
756 if (oldlen == 1 && *PL_oldname == '-') {
757 setdefout(gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL,
759 return IoIFP(GvIOp(gv));
761 #ifndef FLEXFILENAMES
762 filedev = PL_statbuf.st_dev;
763 fileino = PL_statbuf.st_ino;
765 PL_filemode = PL_statbuf.st_mode;
766 fileuid = PL_statbuf.st_uid;
767 filegid = PL_statbuf.st_gid;
768 if (!S_ISREG(PL_filemode)) {
769 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
770 "Can't do inplace edit: %s is not a regular file",
775 if (*PL_inplace && strNE(PL_inplace, "*")) {
776 const char *star = strchr(PL_inplace, '*');
778 const char *begin = PL_inplace;
781 sv_catpvn(sv, begin, star - begin);
782 sv_catpvn(sv, PL_oldname, oldlen);
784 } while ((star = strchr(begin, '*')));
789 sv_catpv(sv,PL_inplace);
791 #ifndef FLEXFILENAMES
792 if ((PerlLIO_stat(SvPVX_const(sv),&PL_statbuf) >= 0
793 && PL_statbuf.st_dev == filedev
794 && PL_statbuf.st_ino == fileino)
796 || ((_djstat_fail_bits & _STFAIL_TRUENAME)!=0)
800 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
801 "Can't do inplace edit: %"SVf" would not be unique",
808 #if !defined(DOSISH) && !defined(__CYGWIN__) && !defined(EPOC)
809 if (PerlLIO_rename(PL_oldname,SvPVX_const(sv)) < 0) {
810 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
811 "Can't rename %s to %"SVf": %s, skipping file",
812 PL_oldname, SVfARG(sv), Strerror(errno));
818 (void)PerlLIO_unlink(SvPVX_const(sv));
819 (void)PerlLIO_rename(PL_oldname,SvPVX_const(sv));
820 do_open(gv,(char*)SvPVX_const(sv),SvCUR(sv),TRUE,O_RDONLY,0,NULL);
823 (void)UNLINK(SvPVX_const(sv));
824 if (link(PL_oldname,SvPVX_const(sv)) < 0) {
825 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
826 "Can't rename %s to %"SVf": %s, skipping file",
827 PL_oldname, SVfARG(sv), Strerror(errno) );
831 (void)UNLINK(PL_oldname);
835 #if !defined(DOSISH) && !defined(AMIGAOS)
836 # ifndef VMS /* Don't delete; use automatic file versioning */
837 if (UNLINK(PL_oldname) < 0) {
838 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
839 "Can't remove %s: %s, skipping file",
840 PL_oldname, Strerror(errno) );
846 Perl_croak(aTHX_ "Can't do inplace edit without backup");
850 sv_setpvn(sv,PL_oldname,oldlen);
851 SETERRNO(0,0); /* in case sprintf set errno */
852 if (!Perl_do_openn(aTHX_ PL_argvoutgv, (char*)SvPVX_const(sv),
855 O_WRONLY|O_CREAT|O_TRUNC,0,
857 O_WRONLY|O_CREAT|OPEN_EXCL,0600,
860 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE), "Can't do inplace edit on %s: %s",
861 PL_oldname, Strerror(errno) );
865 setdefout(PL_argvoutgv);
866 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
867 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
869 (void)fchmod(PL_lastfd,PL_filemode);
871 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
873 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
875 (void)fchown(PL_lastfd,fileuid,filegid);
878 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
883 return IoIFP(GvIOp(gv));
886 if (ckWARN_d(WARN_INPLACE)) {
887 const int eno = errno;
888 if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0
889 && !S_ISREG(PL_statbuf.st_mode))
891 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
892 "Can't do inplace edit: %s is not a regular file",
896 Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't open %s: %s",
897 PL_oldname, Strerror(eno));
901 if (io && (IoFLAGS(io) & IOf_ARGV))
902 IoFLAGS(io) |= IOf_START;
904 (void)do_close(PL_argvoutgv,FALSE);
905 if (io && (IoFLAGS(io) & IOf_ARGV)
906 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
908 GV * const oldout = MUTABLE_GV(av_pop(PL_argvout_stack));
910 SvREFCNT_dec(oldout);
913 setdefout(gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO));
918 /* explicit renamed to avoid C++ conflict -- kja */
920 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
928 if (!gv || !isGV_with_GP(gv)) {
930 SETERRNO(EBADF,SS_IVCHAN);
934 if (!io) { /* never opened */
937 SETERRNO(EBADF,SS_IVCHAN);
941 retval = io_close(io, not_implicit);
945 IoLINES_LEFT(io) = IoPAGE_LEN(io);
947 IoTYPE(io) = IoTYPE_CLOSED;
952 Perl_io_close(pTHX_ IO *io, bool not_implicit)
957 PERL_ARGS_ASSERT_IO_CLOSE;
960 if (IoTYPE(io) == IoTYPE_PIPE) {
961 const int status = PerlProc_pclose(IoIFP(io));
963 STATUS_NATIVE_CHILD_SET(status);
964 retval = (STATUS_UNIX == 0);
967 retval = (status != -1);
970 else if (IoTYPE(io) == IoTYPE_STD)
973 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
974 const bool prev_err = PerlIO_error(IoOFP(io));
975 retval = (PerlIO_close(IoOFP(io)) != EOF && !prev_err);
976 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
979 const bool prev_err = PerlIO_error(IoIFP(io));
980 retval = (PerlIO_close(IoIFP(io)) != EOF && !prev_err);
983 IoOFP(io) = IoIFP(io) = NULL;
985 else if (not_implicit) {
986 SETERRNO(EBADF,SS_IVCHAN);
993 Perl_do_eof(pTHX_ GV *gv)
996 register IO * const io = GvIO(gv);
998 PERL_ARGS_ASSERT_DO_EOF;
1002 else if (IoTYPE(io) == IoTYPE_WRONLY)
1003 report_wrongway_fh(gv, '>');
1006 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
1007 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
1008 return FALSE; /* this is the most usual case */
1012 /* getc and ungetc can stomp on errno */
1014 const int ch = PerlIO_getc(IoIFP(io));
1016 (void)PerlIO_ungetc(IoIFP(io),ch);
1023 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
1024 if (PerlIO_get_cnt(IoIFP(io)) < -1)
1025 PerlIO_set_cnt(IoIFP(io),-1);
1027 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
1028 if (gv != PL_argvgv || !nextargv(gv)) /* get another fp handy */
1032 return TRUE; /* normal fp, definitely end of file */
1038 Perl_do_tell(pTHX_ GV *gv)
1041 IO *const io = GvIO(gv);
1042 register PerlIO *fp;
1044 PERL_ARGS_ASSERT_DO_TELL;
1046 if (io && (fp = IoIFP(io))) {
1047 #ifdef ULTRIX_STDIO_BOTCH
1049 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1051 return PerlIO_tell(fp);
1054 SETERRNO(EBADF,RMS_IFI);
1059 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
1062 IO *const io = GvIO(gv);
1063 register PerlIO *fp;
1065 if (io && (fp = IoIFP(io))) {
1066 #ifdef ULTRIX_STDIO_BOTCH
1068 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1070 return PerlIO_seek(fp, pos, whence) >= 0;
1073 SETERRNO(EBADF,RMS_IFI);
1078 Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
1081 IO *const io = GvIO(gv);
1082 register PerlIO *fp;
1084 PERL_ARGS_ASSERT_DO_SYSSEEK;
1086 if (io && (fp = IoIFP(io)))
1087 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
1089 SETERRNO(EBADF,RMS_IFI);
1094 Perl_mode_from_discipline(pTHX_ const char *s, STRLEN len)
1096 int mode = O_BINARY;
1102 if (s[2] == 'a' && s[3] == 'w'
1103 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1112 if (s[2] == 'r' && s[3] == 'l' && s[4] == 'f'
1113 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1122 goto fail_discipline;
1125 else if (isSPACE(*s)) {
1132 end = strchr(s+1, ':');
1135 #ifndef PERLIO_LAYERS
1136 Perl_croak(aTHX_ "IO layers (like '%.*s') unavailable", end-s, s);
1147 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE)
1149 my_chsize(int fd, Off_t length)
1152 /* code courtesy of William Kucharski */
1157 if (PerlLIO_fstat(fd, &filebuf) < 0)
1160 if (filebuf.st_size < length) {
1162 /* extend file length */
1164 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
1167 /* write a "0" byte */
1169 if ((PerlLIO_write(fd, "", 1)) != 1)
1173 /* truncate length */
1177 fl.l_start = length;
1178 fl.l_type = F_WRLCK; /* write lock on file space */
1181 * This relies on the UNDOCUMENTED F_FREESP argument to
1182 * fcntl(2), which truncates the file so that it ends at the
1183 * position indicated by fl.l_start.
1185 * Will minor miracles never cease?
1188 if (fcntl(fd, F_FREESP, &fl) < 0)
1194 Perl_croak_nocontext("truncate not implemented");
1195 #endif /* F_FREESP */
1198 #endif /* !HAS_TRUNCATE && !HAS_CHSIZE */
1201 Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
1205 PERL_ARGS_ASSERT_DO_PRINT;
1207 /* assuming fp is checked earlier */
1210 if (SvTYPE(sv) == SVt_IV && SvIOK(sv)) {
1211 assert(!SvGMAGICAL(sv));
1213 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
1215 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
1216 return !PerlIO_error(fp);
1220 /* Do this first to trigger any overloading. */
1221 const char *tmps = SvPV_const(sv, len);
1225 if (PerlIO_isutf8(fp)) { /* If the stream is utf8 ... */
1226 if (!SvUTF8(sv)) { /* Convert to utf8 if necessary */
1227 /* We don't modify the original scalar. */
1228 tmpbuf = bytes_to_utf8((const U8*) tmps, &len);
1229 tmps = (char *) tmpbuf;
1231 else if (ckWARN4_d(WARN_UTF8, WARN_SURROGATE, WARN_NON_UNICODE, WARN_NONCHAR)) {
1232 (void) check_utf8_print((const U8*) tmps, len);
1234 } /* else stream isn't utf8 */
1235 else if (DO_UTF8(sv)) { /* But if is utf8 internally, attempt to
1237 STRLEN tmplen = len;
1239 U8 * const result = bytes_from_utf8((const U8*) tmps, &tmplen, &utf8);
1242 /* Here, succeeded in downgrading from utf8. Set up to below
1243 * output the converted value */
1245 tmps = (char *) tmpbuf;
1248 else { /* Non-utf8 output stream, but string only representable in
1250 assert((char *)result == tmps);
1251 Perl_ck_warner_d(aTHX_ packWARN(WARN_UTF8),
1252 "Wide character in %s",
1253 PL_op ? OP_DESC(PL_op) : "print"
1255 /* Could also check that isn't one of the things to avoid
1256 * in utf8 by using check_utf8_print(), but not doing so,
1257 * since the stream isn't a UTF8 stream */
1260 /* To detect whether the process is about to overstep its
1261 * filesize limit we would need getrlimit(). We could then
1262 * also transparently raise the limit with setrlimit() --
1263 * but only until the system hard limit/the filesystem limit,
1264 * at which we would get EPERM. Note that when using buffered
1265 * io the write failure can be delayed until the flush/close. --jhi */
1266 if (len && (PerlIO_write(fp,tmps,len) == 0))
1269 return happy ? !PerlIO_error(fp) : FALSE;
1274 Perl_my_stat_flags(pTHX_ const U32 flags)
1281 if (PL_op->op_flags & OPf_REF) {
1285 return PL_laststatval;
1288 PL_laststype = OP_STAT;
1289 PL_statgv = gv ? gv : (GV *)io;
1290 sv_setpvs(PL_statname, "");
1293 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
1294 } else if (IoDIRP(io)) {
1295 return (PL_laststatval = PerlLIO_fstat(my_dirfd(IoDIRP(io)), &PL_statcache));
1298 PL_laststatval = -1;
1302 else if ((PL_op->op_private & (OPpFT_STACKED|OPpFT_AFTER_t))
1304 return PL_laststatval;
1306 SV* const sv = TOPs;
1309 if ((gv = MAYBE_DEREF_GV_flags(sv,flags))) {
1312 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
1313 io = MUTABLE_IO(SvRV(sv));
1315 goto do_fstat_have_io;
1318 s = SvPV_flags_const(sv, len, flags);
1320 sv_setpvn(PL_statname, s, len);
1321 s = SvPVX_const(PL_statname); /* s now NUL-terminated */
1322 PL_laststype = OP_STAT;
1323 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1324 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
1325 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
1326 return PL_laststatval;
1332 Perl_my_lstat_flags(pTHX_ const U32 flags)
1335 static const char no_prev_lstat[] = "The stat preceding -l _ wasn't an lstat";
1338 if (PL_op->op_flags & OPf_REF) {
1339 if (cGVOP_gv == PL_defgv) {
1340 if (PL_laststype != OP_LSTAT)
1341 Perl_croak(aTHX_ no_prev_lstat);
1342 return PL_laststatval;
1344 PL_laststatval = -1;
1345 if (ckWARN(WARN_IO)) {
1346 Perl_warner(aTHX_ packWARN(WARN_IO),
1347 "Use of -l on filehandle %"HEKf,
1348 HEKfARG(GvENAME_HEK(cGVOP_gv)));
1352 if ((PL_op->op_private & (OPpFT_STACKED|OPpFT_AFTER_t))
1354 if (PL_laststype != OP_LSTAT)
1355 Perl_croak(aTHX_ no_prev_lstat);
1356 return PL_laststatval;
1359 PL_laststype = OP_LSTAT;
1361 file = SvPV_flags_const_nolen(TOPs, flags);
1362 sv_setpv(PL_statname,file);
1363 PL_laststatval = PerlLIO_lstat(file,&PL_statcache);
1364 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(file, '\n'))
1365 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "lstat");
1366 return PL_laststatval;
1370 S_exec_failed(pTHX_ const char *cmd, int fd, int do_report)
1372 const int e = errno;
1373 PERL_ARGS_ASSERT_EXEC_FAILED;
1374 if (ckWARN(WARN_EXEC))
1375 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
1378 PerlLIO_write(fd, (void*)&e, sizeof(int));
1384 Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1385 int fd, int do_report)
1388 PERL_ARGS_ASSERT_DO_AEXEC5;
1389 #if defined(__SYMBIAN32__) || defined(__LIBCATAMOUNT__)
1390 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1394 const char *tmps = NULL;
1395 Newx(PL_Argv, sp - mark + 1, const char*);
1398 while (++mark <= sp) {
1400 *a++ = SvPV_nolen_const(*mark);
1406 tmps = SvPV_nolen_const(really);
1407 if ((!really && *PL_Argv[0] != '/') ||
1408 (really && *tmps != '/')) /* will execvp use PATH? */
1409 TAINT_ENV(); /* testing IFS here is overkill, probably */
1411 if (really && *tmps)
1412 PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
1414 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1416 S_exec_failed(aTHX_ (really ? tmps : PL_Argv[0]), fd, do_report);
1424 Perl_do_execfree(pTHX)
1433 #ifdef PERL_DEFAULT_DO_EXEC3_IMPLEMENTATION
1436 Perl_do_exec3(pTHX_ const char *incmd, int fd, int do_report)
1439 register const char **a;
1443 /* Make a copy so we can change it */
1444 const Size_t cmdlen = strlen(incmd) + 1;
1446 PERL_ARGS_ASSERT_DO_EXEC3;
1448 Newx(buf, cmdlen, char);
1450 memcpy(cmd, incmd, cmdlen);
1452 while (*cmd && isSPACE(*cmd))
1455 /* save an extra exec if possible */
1459 char flags[PERL_FLAGS_MAX];
1460 if (strnEQ(cmd,PL_cshname,PL_cshlen) &&
1461 strnEQ(cmd+PL_cshlen," -c",3)) {
1462 my_strlcpy(flags, "-c", PERL_FLAGS_MAX);
1463 s = cmd+PL_cshlen+3;
1466 my_strlcat(flags, "f", PERL_FLAGS_MAX - 2);
1471 char * const ncmd = s;
1477 if (s[-1] == '\'') {
1480 PerlProc_execl(PL_cshname, "csh", flags, ncmd, (char*)NULL);
1483 S_exec_failed(aTHX_ PL_cshname, fd, do_report);
1492 /* see if there are shell metacharacters in it */
1494 if (*cmd == '.' && isSPACE(cmd[1]))
1497 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1502 s++; /* catch VAR=val gizmo */
1506 for (s = cmd; *s; s++) {
1507 if (*s != ' ' && !isALPHA(*s) &&
1508 strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1509 if (*s == '\n' && !s[1]) {
1513 /* handle the 2>&1 construct at the end */
1514 if (*s == '>' && s[1] == '&' && s[2] == '1'
1515 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1516 && (!s[3] || isSPACE(s[3])))
1518 const char *t = s + 3;
1520 while (*t && isSPACE(*t))
1522 if (!*t && (PerlLIO_dup2(1,2) != -1)) {
1529 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char *)NULL);
1531 S_exec_failed(aTHX_ PL_sh_path, fd, do_report);
1537 Newx(PL_Argv, (s - cmd) / 2 + 2, const char*);
1538 PL_Cmd = savepvn(cmd, s-cmd);
1540 for (s = PL_Cmd; *s;) {
1545 while (*s && !isSPACE(*s))
1553 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1555 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1559 S_exec_failed(aTHX_ PL_Argv[0], fd, do_report);
1566 #endif /* OS2 || WIN32 */
1569 #include <starlet.h> /* for sys$delprc */
1573 Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
1577 register I32 tot = 0;
1578 const char *const what = PL_op_name[type];
1581 SV ** const oldmark = mark;
1582 bool killgp = FALSE;
1584 PERL_ARGS_ASSERT_APPLY;
1586 /* Doing this ahead of the switch statement preserves the old behaviour,
1587 where attempting to use kill as a taint test test would fail on
1588 platforms where kill was not defined. */
1590 if (type == OP_KILL)
1591 Perl_die(aTHX_ PL_no_func, what);
1594 if (type == OP_CHOWN)
1595 Perl_die(aTHX_ PL_no_func, what);
1599 #define APPLY_TAINT_PROPER() \
1601 if (PL_tainted) { TAINT_PROPER(what); } \
1604 /* This is a first heuristic; it doesn't catch tainting magic. */
1606 while (++mark <= sp) {
1607 if (SvTAINTED(*mark)) {
1616 APPLY_TAINT_PROPER();
1619 APPLY_TAINT_PROPER();
1621 while (++mark <= sp) {
1623 if ((gv = MAYBE_DEREF_GV(*mark))) {
1624 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1626 APPLY_TAINT_PROPER();
1627 if (fchmod(PerlIO_fileno(IoIFP(GvIOn(gv))), val))
1630 Perl_die(aTHX_ PL_no_func, "fchmod");
1638 const char *name = SvPV_nomg_const_nolen(*mark);
1639 APPLY_TAINT_PROPER();
1640 if (PerlLIO_chmod(name, val))
1648 APPLY_TAINT_PROPER();
1649 if (sp - mark > 2) {
1651 val = SvIVx(*++mark);
1652 val2 = SvIVx(*++mark);
1653 APPLY_TAINT_PROPER();
1655 while (++mark <= sp) {
1657 if ((gv = MAYBE_DEREF_GV(*mark))) {
1658 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1660 APPLY_TAINT_PROPER();
1661 if (fchown(PerlIO_fileno(IoIFP(GvIOn(gv))), val, val2))
1664 Perl_die(aTHX_ PL_no_func, "fchown");
1672 const char *name = SvPV_nomg_const_nolen(*mark);
1673 APPLY_TAINT_PROPER();
1674 if (PerlLIO_chown(name, val, val2))
1682 XXX Should we make lchown() directly available from perl?
1683 For now, we'll let Configure test for HAS_LCHOWN, but do
1684 nothing in the core.
1689 APPLY_TAINT_PROPER();
1692 s = SvPVx_const(*++mark, len);
1693 if (*s == '-' && isALPHA(s[1]))
1700 if (*s == 'S' && s[1] == 'I' && s[2] == 'G') {
1704 if ((val = whichsig_pvn(s, len)) < 0)
1705 Perl_croak(aTHX_ "Unrecognized signal name \"%"SVf"\"", SVfARG(*mark));
1716 APPLY_TAINT_PROPER();
1719 /* kill() doesn't do process groups (job trees?) under VMS */
1720 if (val == SIGKILL) {
1721 /* Use native sys$delprc() to insure that target process is
1722 * deleted; supervisor-mode images don't pay attention to
1723 * CRTL's emulation of Unix-style signals and kill()
1725 while (++mark <= sp) {
1727 register unsigned long int __vmssts;
1729 if (!(SvIOK(*mark) || SvNOK(*mark) || looks_like_number(*mark)))
1730 Perl_croak(aTHX_ "Can't kill a non-numeric process ID");
1731 proc = SvIV_nomg(*mark);
1732 APPLY_TAINT_PROPER();
1733 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1737 case SS$_NOSUCHNODE:
1738 SETERRNO(ESRCH,__vmssts);
1741 SETERRNO(EPERM,__vmssts);
1744 SETERRNO(EVMSERR,__vmssts);
1752 while (++mark <= sp) {
1755 if (!(SvIOK(*mark) || SvNOK(*mark) || looks_like_number(*mark)))
1756 Perl_croak(aTHX_ "Can't kill a non-numeric process ID");
1757 proc = SvIV_nomg(*mark);
1762 APPLY_TAINT_PROPER();
1763 if (PerlProc_kill(proc, val))
1770 APPLY_TAINT_PROPER();
1772 while (++mark <= sp) {
1773 s = SvPV_nolen_const(*mark);
1774 APPLY_TAINT_PROPER();
1775 if (PerlProc_geteuid() || PL_unsafe) {
1779 else { /* don't let root wipe out directories without -U */
1780 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1789 #if defined(HAS_UTIME) || defined(HAS_FUTIMES)
1791 APPLY_TAINT_PROPER();
1792 if (sp - mark > 2) {
1793 #if defined(HAS_FUTIMES)
1794 struct timeval utbuf[2];
1795 void *utbufp = utbuf;
1796 #elif defined(I_UTIME) || defined(VMS)
1797 struct utimbuf utbuf;
1798 struct utimbuf *utbufp = &utbuf;
1804 void *utbufp = &utbuf;
1807 SV* const accessed = *++mark;
1808 SV* const modified = *++mark;
1810 /* Be like C, and if both times are undefined, let the C
1811 * library figure out what to do. This usually means
1812 * "current time". */
1814 if ( accessed == &PL_sv_undef && modified == &PL_sv_undef )
1817 Zero(&utbuf, sizeof utbuf, char);
1819 utbuf[0].tv_sec = (long)SvIV(accessed); /* time accessed */
1820 utbuf[0].tv_usec = 0;
1821 utbuf[1].tv_sec = (long)SvIV(modified); /* time modified */
1822 utbuf[1].tv_usec = 0;
1823 #elif defined(BIG_TIME)
1824 utbuf.actime = (Time_t)SvNV(accessed); /* time accessed */
1825 utbuf.modtime = (Time_t)SvNV(modified); /* time modified */
1827 utbuf.actime = (Time_t)SvIV(accessed); /* time accessed */
1828 utbuf.modtime = (Time_t)SvIV(modified); /* time modified */
1831 APPLY_TAINT_PROPER();
1833 while (++mark <= sp) {
1835 if ((gv = MAYBE_DEREF_GV(*mark))) {
1836 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1838 APPLY_TAINT_PROPER();
1839 if (futimes(PerlIO_fileno(IoIFP(GvIOn(gv))),
1840 (struct timeval *) utbufp))
1843 Perl_die(aTHX_ PL_no_func, "futimes");
1851 const char * const name = SvPV_nomg_const_nolen(*mark);
1852 APPLY_TAINT_PROPER();
1854 if (utimes(name, (struct timeval *)utbufp))
1856 if (PerlLIO_utime(name, utbufp))
1870 #undef APPLY_TAINT_PROPER
1873 /* Do the permissions allow some operation? Assumes statcache already set. */
1874 #ifndef VMS /* VMS' cando is in vms.c */
1876 Perl_cando(pTHX_ Mode_t mode, bool effective, register const Stat_t *statbufp)
1877 /* effective is a flag, true for EUID, or for checking if the effective gid
1878 * is in the list of groups returned from getgroups().
1883 PERL_ARGS_ASSERT_CANDO;
1886 /* [Comments and code from Len Reed]
1887 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1888 * to write-protected files. The execute permission bit is set
1889 * by the Microsoft C library stat() function for the following:
1894 * All files and directories are readable.
1895 * Directories and special files, e.g. "CON", cannot be
1897 * [Comment by Tom Dinger -- a directory can have the write-protect
1898 * bit set in the file system, but DOS permits changes to
1899 * the directory anyway. In addition, all bets are off
1900 * here for networked software, such as Novell and
1904 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1905 * too so it will actually look into the files for magic numbers
1907 return (mode & statbufp->st_mode) ? TRUE : FALSE;
1909 #else /* ! DOSISH */
1911 if (ingroup(544,effective)) { /* member of Administrators */
1913 if ((effective ? PerlProc_geteuid() : PerlProc_getuid()) == 0) { /* root is special */
1915 if (mode == S_IXUSR) {
1916 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1920 return TRUE; /* root reads and writes anything */
1923 if (statbufp->st_uid == (effective ? PerlProc_geteuid() : PerlProc_getuid()) ) {
1924 if (statbufp->st_mode & mode)
1925 return TRUE; /* ok as "user" */
1927 else if (ingroup(statbufp->st_gid,effective)) {
1928 if (statbufp->st_mode & mode >> 3)
1929 return TRUE; /* ok as "group" */
1931 else if (statbufp->st_mode & mode >> 6)
1932 return TRUE; /* ok as "other" */
1934 #endif /* ! DOSISH */
1939 S_ingroup(pTHX_ Gid_t testgid, bool effective)
1942 if (testgid == (effective ? PerlProc_getegid() : PerlProc_getgid()))
1944 #ifdef HAS_GETGROUPS
1946 Groups_t *gary = NULL;
1950 anum = getgroups(0, gary);
1951 Newx(gary, anum, Groups_t);
1952 anum = getgroups(anum, gary);
1954 if (gary[anum] == testgid) {
1967 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1970 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
1973 const key_t key = (key_t)SvNVx(*++mark);
1974 SV *nsv = optype == OP_MSGGET ? NULL : *++mark;
1975 const I32 flags = SvIVx(*++mark);
1977 PERL_ARGS_ASSERT_DO_IPCGET;
1978 PERL_UNUSED_ARG(sp);
1985 return msgget(key, flags);
1989 return semget(key, (int) SvIV(nsv), flags);
1993 return shmget(key, (size_t) SvUV(nsv), flags);
1995 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1997 /* diag_listed_as: msg%s not implemented */
1998 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2001 return -1; /* should never happen */
2005 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
2010 const I32 id = SvIVx(*++mark);
2012 const I32 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
2014 const I32 cmd = SvIVx(*++mark);
2015 SV * const astr = *++mark;
2016 STRLEN infosize = 0;
2017 I32 getinfo = (cmd == IPC_STAT);
2019 PERL_ARGS_ASSERT_DO_IPCCTL;
2020 PERL_UNUSED_ARG(sp);
2026 if (cmd == IPC_STAT || cmd == IPC_SET)
2027 infosize = sizeof(struct msqid_ds);
2032 if (cmd == IPC_STAT || cmd == IPC_SET)
2033 infosize = sizeof(struct shmid_ds);
2039 if (cmd == IPC_STAT || cmd == IPC_SET)
2040 infosize = sizeof(struct semid_ds);
2041 else if (cmd == GETALL || cmd == SETALL)
2043 struct semid_ds semds;
2045 #ifdef EXTRA_F_IN_SEMUN_BUF
2046 semun.buff = &semds;
2050 getinfo = (cmd == GETALL);
2051 if (Semctl(id, 0, IPC_STAT, semun) == -1)
2053 infosize = semds.sem_nsems * sizeof(short);
2054 /* "short" is technically wrong but much more portable
2055 than guessing about u_?short(_t)? */
2058 /* diag_listed_as: sem%s not implemented */
2059 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2063 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
2065 /* diag_listed_as: shm%s not implemented */
2066 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2074 SvPV_force_nolen(astr);
2075 a = SvGROW(astr, infosize+1);
2080 a = SvPV(astr, len);
2081 if (len != infosize)
2082 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
2090 const IV i = SvIV(astr);
2091 a = INT2PTR(char *,i); /* ouch */
2098 ret = msgctl(id, cmd, (struct msqid_ds *)a);
2104 union semun unsemds;
2106 #ifdef EXTRA_F_IN_SEMUN_BUF
2107 unsemds.buff = (struct semid_ds *)a;
2109 unsemds.buf = (struct semid_ds *)a;
2111 ret = Semctl(id, n, cmd, unsemds);
2113 /* diag_listed_as: sem%s not implemented */
2114 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2121 ret = shmctl(id, cmd, (struct shmid_ds *)a);
2125 if (getinfo && ret >= 0) {
2126 SvCUR_set(astr, infosize);
2127 *SvEND(astr) = '\0';
2134 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
2139 const I32 id = SvIVx(*++mark);
2140 SV * const mstr = *++mark;
2141 const I32 flags = SvIVx(*++mark);
2142 const char * const mbuf = SvPV_const(mstr, len);
2143 const I32 msize = len - sizeof(long);
2145 PERL_ARGS_ASSERT_DO_MSGSND;
2146 PERL_UNUSED_ARG(sp);
2149 Perl_croak(aTHX_ "Arg too short for msgsnd");
2151 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
2153 PERL_UNUSED_ARG(sp);
2154 PERL_UNUSED_ARG(mark);
2155 /* diag_listed_as: msg%s not implemented */
2156 Perl_croak(aTHX_ "msgsnd not implemented");
2161 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
2167 I32 msize, flags, ret;
2168 const I32 id = SvIVx(*++mark);
2169 SV * const mstr = *++mark;
2171 PERL_ARGS_ASSERT_DO_MSGRCV;
2172 PERL_UNUSED_ARG(sp);
2174 /* suppress warning when reading into undef var --jhi */
2176 sv_setpvs(mstr, "");
2177 msize = SvIVx(*++mark);
2178 mtype = (long)SvIVx(*++mark);
2179 flags = SvIVx(*++mark);
2180 SvPV_force_nolen(mstr);
2181 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
2184 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
2186 SvCUR_set(mstr, sizeof(long)+ret);
2187 *SvEND(mstr) = '\0';
2188 #ifndef INCOMPLETE_TAINTS
2189 /* who knows who has been playing with this message? */
2195 PERL_UNUSED_ARG(sp);
2196 PERL_UNUSED_ARG(mark);
2197 /* diag_listed_as: msg%s not implemented */
2198 Perl_croak(aTHX_ "msgrcv not implemented");
2203 Perl_do_semop(pTHX_ SV **mark, SV **sp)
2208 const I32 id = SvIVx(*++mark);
2209 SV * const opstr = *++mark;
2210 const char * const opbuf = SvPV_const(opstr, opsize);
2212 PERL_ARGS_ASSERT_DO_SEMOP;
2213 PERL_UNUSED_ARG(sp);
2215 if (opsize < 3 * SHORTSIZE
2216 || (opsize % (3 * SHORTSIZE))) {
2217 SETERRNO(EINVAL,LIB_INVARG);
2221 /* We can't assume that sizeof(struct sembuf) == 3 * sizeof(short). */
2223 const int nsops = opsize / (3 * sizeof (short));
2225 short * const ops = (short *) opbuf;
2227 struct sembuf *temps, *t;
2230 Newx (temps, nsops, struct sembuf);
2238 result = semop(id, temps, nsops);
2252 /* diag_listed_as: sem%s not implemented */
2253 Perl_croak(aTHX_ "semop not implemented");
2258 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
2263 struct shmid_ds shmds;
2264 const I32 id = SvIVx(*++mark);
2265 SV * const mstr = *++mark;
2266 const I32 mpos = SvIVx(*++mark);
2267 const I32 msize = SvIVx(*++mark);
2269 PERL_ARGS_ASSERT_DO_SHMIO;
2270 PERL_UNUSED_ARG(sp);
2273 if (shmctl(id, IPC_STAT, &shmds) == -1)
2275 if (mpos < 0 || msize < 0
2276 || (size_t)mpos + msize > (size_t)shmds.shm_segsz) {
2277 SETERRNO(EFAULT,SS_ACCVIO); /* can't do as caller requested */
2280 shm = (char *)shmat(id, NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
2281 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2283 if (optype == OP_SHMREAD) {
2285 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2287 sv_setpvs(mstr, "");
2288 SvUPGRADE(mstr, SVt_PV);
2290 mbuf = SvGROW(mstr, (STRLEN)msize+1);
2292 Copy(shm + mpos, mbuf, msize, char);
2293 SvCUR_set(mstr, msize);
2294 *SvEND(mstr) = '\0';
2296 #ifndef INCOMPLETE_TAINTS
2297 /* who knows who has been playing with this shared memory? */
2304 const char *mbuf = SvPV_const(mstr, len);
2305 const I32 n = ((I32)len > msize) ? msize : (I32)len;
2306 Copy(mbuf, shm + mpos, n, char);
2308 memzero(shm + mpos + n, msize - n);
2312 /* diag_listed_as: shm%s not implemented */
2313 Perl_croak(aTHX_ "shm I/O not implemented");
2317 #endif /* SYSV IPC */
2322 =for apidoc start_glob
2324 Function called by C<do_readline> to spawn a glob (or do the glob inside
2325 perl on VMS). This code used to be inline, but now perl uses C<File::Glob>
2326 this glob starter is only used by miniperl during the build process.
2327 Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up.
2333 Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
2336 SV * const tmpcmd = newSV(0);
2339 PERL_ARGS_ASSERT_START_GLOB;
2343 #ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
2344 /* since spawning off a process is a real performance hit */
2351 fp = Perl_vms_start_glob(aTHX_ tmpglob, io);
2356 sv_setpv(tmpcmd, "for a in ");
2357 sv_catsv(tmpcmd, tmpglob);
2358 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
2361 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
2362 sv_catsv(tmpcmd, tmpglob);
2364 sv_setpv(tmpcmd, "perlglob ");
2365 sv_catsv(tmpcmd, tmpglob);
2366 sv_catpv(tmpcmd, " |");
2371 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
2372 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
2373 sv_catsv(tmpcmd, tmpglob);
2374 sv_catpv(tmpcmd, "' 2>/dev/null |");
2376 sv_setpv(tmpcmd, "echo ");
2377 sv_catsv(tmpcmd, tmpglob);
2379 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
2381 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
2384 #endif /* !DOSISH */
2386 GV * const envgv = gv_fetchpvs("ENV", 0, SVt_PVHV);
2387 SV ** const home = hv_fetchs(GvHV(envgv), "HOME", 0);
2388 SV ** const path = hv_fetchs(GvHV(envgv), "PATH", 0);
2389 if (home && *home) SvGETMAGIC(*home);
2390 if (path && *path) SvGETMAGIC(*path);
2391 save_hash(gv_fetchpvs("ENV", 0, SVt_PVHV));
2392 if (home && *home) SvSETMAGIC(*home);
2393 if (path && *path) SvSETMAGIC(*path);
2395 (void)do_open(PL_last_in_gv, (char*)SvPVX_const(tmpcmd), SvCUR(tmpcmd),
2396 FALSE, O_RDONLY, 0, NULL);
2405 * c-indentation-style: bsd
2407 * indent-tabs-mode: nil
2410 * ex: set ts=8 sts=4 sw=4 et: