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]))
211 /* New style explicit name, type is just mode and layer info */
213 if (SvROK(*svp) && !strchr(oname,'&')) {
215 Perl_warner(aTHX_ packWARN(WARN_IO),
216 "Can't open a reference");
217 SETERRNO(EINVAL, LIB_INVARG);
220 #endif /* USE_STDIO */
221 p = (SvOK(*svp) || SvGMAGICAL(*svp)) ? SvPV(*svp, nlen) : NULL;
223 if (p && !IS_SAFE_PATHNAME(p, nlen, "open"))
226 name = p ? savepvn(p, nlen) : savepvs("");
235 if ((*type == IoTYPE_RDWR) && /* scary */
236 (*(type+1) == IoTYPE_RDONLY || *(type+1) == IoTYPE_WRONLY) &&
237 ((!num_svs || (tend > type+1 && tend[-1] != IoTYPE_PIPE)))) {
238 TAINT_PROPER("open");
243 if (*type == IoTYPE_PIPE) {
245 if (type[1] != IoTYPE_STD) {
247 Perl_croak(aTHX_ "Unknown open() mode '%.*s'", (int)olen, oname);
253 } while (isSPACE(*type));
259 /* command is missing 19990114 */
260 if (ckWARN(WARN_PIPE))
261 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open");
265 if (!(*name == '-' && name[1] == '\0') || num_svs)
267 TAINT_PROPER("piped open");
268 if (!num_svs && name[len-1] == '|') {
270 if (ckWARN(WARN_PIPE))
271 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Can't open bidirectional pipe");
280 fp = PerlProc_popen_list(mode, num_svs, svp);
283 fp = PerlProc_popen(name,mode);
287 if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) {
293 else if (*type == IoTYPE_WRONLY) {
294 TAINT_PROPER("open");
296 if (*type == IoTYPE_WRONLY) {
297 /* Two IoTYPE_WRONLYs in a row make for an IoTYPE_APPEND. */
298 mode[0] = IoTYPE(io) = IoTYPE_APPEND;
312 dodup = PERLIO_DUP_FD;
318 if (!num_svs && !*type && supplied_fp) {
319 /* "<+&" etc. is used by typemaps */
323 PerlIO *that_fp = NULL;
325 /* diag_listed_as: More than one argument to '%s' open */
326 Perl_croak(aTHX_ "More than one argument to '%c&' open",IoTYPE(io));
328 while (isSPACE(*type))
332 || (SvPOKp(*svp) && looks_like_number(*svp))
337 else if (isDIGIT(*type)) {
343 thatio = sv_2io(*svp);
346 GV * const thatgv = gv_fetchpvn_flags(type, tend - type,
348 thatio = GvIO(thatgv);
352 SETERRNO(EINVAL,SS_IVCHAN);
356 if ((that_fp = IoIFP(thatio))) {
357 /* Flush stdio buffer before dup. --mjd
358 * Unfortunately SEEK_CURing 0 seems to
359 * be optimized away on most platforms;
360 * only Solaris and Linux seem to flush
363 /* sfio fails to clear error on next
364 sfwrite, contrary to documentation.
366 if (PerlIO_seek(that_fp, 0, SEEK_CUR) == -1)
367 PerlIO_clearerr(that_fp);
369 /* On the other hand, do all platforms
370 * take gracefully to flushing a read-only
371 * filehandle? Perhaps we should do
372 * fsetpos(src)+fgetpos(dst)? --nik */
373 PerlIO_flush(that_fp);
374 fd = PerlIO_fileno(that_fp);
375 /* When dup()ing STDIN, STDOUT or STDERR
376 * explicitly set appropriate access mode */
377 if (that_fp == PerlIO_stdout()
378 || that_fp == PerlIO_stderr())
379 IoTYPE(io) = IoTYPE_WRONLY;
380 else if (that_fp == PerlIO_stdin())
381 IoTYPE(io) = IoTYPE_RDONLY;
382 /* When dup()ing a socket, say result is
384 else if (IoTYPE(thatio) == IoTYPE_SOCKET)
385 IoTYPE(io) = IoTYPE_SOCKET;
393 fp = PerlIO_fdupopen(aTHX_ that_fp, NULL, dodup);
397 fd = PerlLIO_dup(fd);
400 if (!(fp = PerlIO_openn(aTHX_ type,mode,fd,0,0,NULL,num_svs,svp))) {
401 if (dodup && fd >= 0)
408 while (isSPACE(*type))
410 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
412 fp = PerlIO_stdout();
413 IoTYPE(io) = IoTYPE_STD;
415 /* diag_listed_as: More than one argument to '%s' open */
416 Perl_croak(aTHX_ "More than one argument to '>%c' open",IoTYPE_STD);
421 namesv = newSVpvn_flags(type, tend - type, SVs_TEMP);
426 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
429 if (!fp && type && *type && *type != ':' && !isIDFIRST(*type))
430 goto unknown_open_mode;
431 } /* IoTYPE_WRONLY */
432 else if (*type == IoTYPE_RDONLY) {
435 } while (isSPACE(*type));
444 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
447 IoTYPE(io) = IoTYPE_STD;
449 /* diag_listed_as: More than one argument to '%s' open */
450 Perl_croak(aTHX_ "More than one argument to '<%c' open",IoTYPE_STD);
455 namesv = newSVpvn_flags(type, tend - type, SVs_TEMP);
460 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
462 if (!fp && type && *type && *type != ':' && !isIDFIRST(*type))
463 goto unknown_open_mode;
464 } /* IoTYPE_RDONLY */
465 else if ((num_svs && /* '-|...' or '...|' */
466 type[0] == IoTYPE_STD && type[1] == IoTYPE_PIPE) ||
467 (!num_svs && tend > type+1 && tend[-1] == IoTYPE_PIPE)) {
469 type += 2; /* skip over '-|' */
473 while (tend > type && isSPACE(tend[-1]))
475 for (; isSPACE(*type); type++)
481 /* command is missing 19990114 */
482 if (ckWARN(WARN_PIPE))
483 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open");
487 if (!(*name == '-' && name[1] == '\0') || num_svs)
489 TAINT_PROPER("piped open");
498 fp = PerlProc_popen_list(mode,num_svs,svp);
501 fp = PerlProc_popen(name,mode);
503 IoTYPE(io) = IoTYPE_PIPE;
505 while (isSPACE(*type))
508 if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) {
514 else { /* layer(Args) */
516 goto unknown_open_mode;
518 IoTYPE(io) = IoTYPE_RDONLY;
519 for (; isSPACE(*name); name++)
528 if (*name == '-' && name[1] == '\0') {
530 IoTYPE(io) = IoTYPE_STD;
534 namesv = newSVpvn_flags(type, tend - type, SVs_TEMP);
539 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
544 if (IoTYPE(io) == IoTYPE_RDONLY && ckWARN(WARN_NEWLINE)
545 && strchr(oname, '\n')
548 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
552 if (ckWARN(WARN_IO)) {
553 if ((IoTYPE(io) == IoTYPE_RDONLY) &&
554 (fp == PerlIO_stdout() || fp == PerlIO_stderr())) {
555 Perl_warner(aTHX_ packWARN(WARN_IO),
556 "Filehandle STD%s reopened as %"HEKf
558 ((fp == PerlIO_stdout()) ? "OUT" : "ERR"),
559 HEKfARG(GvENAME_HEK(gv)));
561 else if ((IoTYPE(io) == IoTYPE_WRONLY) && fp == PerlIO_stdin()) {
562 Perl_warner(aTHX_ packWARN(WARN_IO),
563 "Filehandle STDIN reopened as %"HEKf" only for output",
564 HEKfARG(GvENAME_HEK(gv))
569 fd = PerlIO_fileno(fp);
570 /* If there is no fd (e.g. PerlIO::scalar) assume it isn't a
571 * socket - this covers PerlIO::scalar - otherwise unless we "know" the
572 * type probe for socket-ness.
574 if (IoTYPE(io) && IoTYPE(io) != IoTYPE_PIPE && IoTYPE(io) != IoTYPE_STD && fd >= 0) {
575 if (PerlLIO_fstat(fd,&PL_statbuf) < 0) {
576 /* If PerlIO claims to have fd we had better be able to fstat() it. */
577 (void) PerlIO_close(fp);
581 if (S_ISSOCK(PL_statbuf.st_mode))
582 IoTYPE(io) = IoTYPE_SOCKET; /* in case a socket was passed in to us */
586 !(PL_statbuf.st_mode & S_IFMT)
590 && IoTYPE(io) != IoTYPE_WRONLY /* Dups of STD* filehandles already have */
591 && IoTYPE(io) != IoTYPE_RDONLY /* type so they aren't marked as sockets */
592 ) { /* on OS's that return 0 on fstat()ed pipe */
594 Sock_size_t buflen = sizeof tmpbuf;
595 if (PerlSock_getsockname(fd, (struct sockaddr *)tmpbuf, &buflen) >= 0
596 || errno != ENOTSOCK)
597 IoTYPE(io) = IoTYPE_SOCKET; /* some OS's return 0 on fstat()ed socket */
598 /* but some return 0 for streams too, sigh */
600 #endif /* HAS_SOCKET */
601 #endif /* !PERL_MICRO */
605 * If this is a standard handle we discard all the layer stuff
606 * and just dup the fd into whatever was on the handle before !
609 if (saveifp) { /* must use old fp? */
610 /* If fd is less that PL_maxsysfd i.e. STDIN..STDERR
611 then dup the new fileno down
614 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
615 if (saveofp != saveifp) { /* was a socket? */
616 PerlIO_close(saveofp);
620 /* Still a small can-of-worms here if (say) PerlIO::scalar
621 is assigned to (say) STDOUT - for now let dup2() fail
622 and provide the error
624 if (PerlLIO_dup2(fd, savefd) < 0) {
625 (void)PerlIO_close(fp);
629 if (savefd != PerlIO_fileno(PerlIO_stdin())) {
630 char newname[FILENAME_MAX+1];
631 if (PerlIO_getname(fp, newname)) {
632 if (fd == PerlIO_fileno(PerlIO_stdout()))
633 vmssetuserlnm("SYS$OUTPUT", newname);
634 if (fd == PerlIO_fileno(PerlIO_stderr()))
635 vmssetuserlnm("SYS$ERROR", newname);
641 /* PL_fdpid isn't used on Windows, so avoid this useless work.
642 * XXX Probably the same for a lot of other places. */
647 sv = *av_fetch(PL_fdpid,fd,TRUE);
648 SvUPGRADE(sv, SVt_IV);
651 sv = *av_fetch(PL_fdpid,savefd,TRUE);
652 SvUPGRADE(sv, SVt_IV);
658 /* need to close fp without closing underlying fd */
659 int ofd = PerlIO_fileno(fp);
660 int dupfd = PerlLIO_dup(ofd);
661 #if defined(HAS_FCNTL) && defined(F_SETFD)
662 /* Assume if we have F_SETFD we have F_GETFD */
663 int coe = fcntl(ofd,F_GETFD);
666 PerlLIO_dup2(dupfd,ofd);
667 #if defined(HAS_FCNTL) && defined(F_SETFD)
668 /* The dup trick has lost close-on-exec on ofd */
669 fcntl(ofd,F_SETFD, coe);
671 PerlLIO_close(dupfd);
678 fd = PerlIO_fileno(fp);
680 #if defined(HAS_FCNTL) && defined(F_SETFD)
683 fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
689 IoFLAGS(io) &= ~IOf_NOLINE;
691 if (IoTYPE(io) == IoTYPE_SOCKET
692 || (IoTYPE(io) == IoTYPE_WRONLY && fd >= 0 && S_ISCHR(PL_statbuf.st_mode)) ) {
694 if (*s == IoTYPE_IMPLICIT || *s == IoTYPE_NUMERIC)
697 if (!(IoOFP(io) = PerlIO_openn(aTHX_ type,s,fd,0,0,NULL,0,svp))) {
711 IoTYPE(io) = savetype;
716 Perl_nextargv(pTHX_ GV *gv)
720 #ifndef FLEXFILENAMES
726 IO * const io = GvIOp(gv);
728 PERL_ARGS_ASSERT_NEXTARGV;
731 PL_argvoutgv = gv_fetchpvs("ARGVOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
732 if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
733 IoFLAGS(io) &= ~IOf_START;
736 Perl_av_create_and_push(aTHX_ &PL_argvout_stack,
737 SvREFCNT_inc_simple_NN(PL_defoutgv));
740 if (PL_filemode & (S_ISUID|S_ISGID)) {
741 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
744 (void)fchmod(PL_lastfd,PL_filemode);
746 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
753 while (av_len(GvAV(gv)) >= 0) {
755 sv = av_shift(GvAV(gv));
757 SvTAINTED_off(GvSVn(gv)); /* previous tainting irrelevant */
758 sv_setsv(GvSVn(gv),sv);
759 SvSETMAGIC(GvSV(gv));
760 PL_oldname = SvPVx(GvSV(gv), oldlen);
761 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,NULL)) {
763 TAINT_PROPER("inplace open");
764 if (oldlen == 1 && *PL_oldname == '-') {
765 setdefout(gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL,
767 return IoIFP(GvIOp(gv));
769 #ifndef FLEXFILENAMES
770 filedev = PL_statbuf.st_dev;
771 fileino = PL_statbuf.st_ino;
773 PL_filemode = PL_statbuf.st_mode;
774 fileuid = PL_statbuf.st_uid;
775 filegid = PL_statbuf.st_gid;
776 if (!S_ISREG(PL_filemode)) {
777 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
778 "Can't do inplace edit: %s is not a regular file",
783 if (*PL_inplace && strNE(PL_inplace, "*")) {
784 const char *star = strchr(PL_inplace, '*');
786 const char *begin = PL_inplace;
789 sv_catpvn(sv, begin, star - begin);
790 sv_catpvn(sv, PL_oldname, oldlen);
792 } while ((star = strchr(begin, '*')));
797 sv_catpv(sv,PL_inplace);
799 #ifndef FLEXFILENAMES
800 if ((PerlLIO_stat(SvPVX_const(sv),&PL_statbuf) >= 0
801 && PL_statbuf.st_dev == filedev
802 && PL_statbuf.st_ino == fileino)
804 || ((_djstat_fail_bits & _STFAIL_TRUENAME)!=0)
808 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
809 "Can't do inplace edit: %"SVf" would not be unique",
816 #if !defined(DOSISH) && !defined(__CYGWIN__)
817 if (PerlLIO_rename(PL_oldname,SvPVX_const(sv)) < 0) {
818 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
819 "Can't rename %s to %"SVf": %s, skipping file",
820 PL_oldname, SVfARG(sv), Strerror(errno));
826 (void)PerlLIO_unlink(SvPVX_const(sv));
827 (void)PerlLIO_rename(PL_oldname,SvPVX_const(sv));
828 do_open(gv,(char*)SvPVX_const(sv),SvCUR(sv),TRUE,O_RDONLY,0,NULL);
831 (void)UNLINK(SvPVX_const(sv));
832 if (link(PL_oldname,SvPVX_const(sv)) < 0) {
833 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
834 "Can't rename %s to %"SVf": %s, skipping file",
835 PL_oldname, SVfARG(sv), Strerror(errno) );
839 (void)UNLINK(PL_oldname);
843 #if !defined(DOSISH) && !defined(AMIGAOS)
844 # ifndef VMS /* Don't delete; use automatic file versioning */
845 if (UNLINK(PL_oldname) < 0) {
846 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
847 "Can't remove %s: %s, skipping file",
848 PL_oldname, Strerror(errno) );
854 Perl_croak(aTHX_ "Can't do inplace edit without backup");
858 sv_setpvn(sv,PL_oldname,oldlen);
859 SETERRNO(0,0); /* in case sprintf set errno */
860 if (!Perl_do_openn(aTHX_ PL_argvoutgv, (char*)SvPVX_const(sv),
863 O_WRONLY|O_CREAT|O_TRUNC,0,
865 O_WRONLY|O_CREAT|OPEN_EXCL,0600,
868 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE), "Can't do inplace edit on %s: %s",
869 PL_oldname, Strerror(errno) );
873 setdefout(PL_argvoutgv);
874 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
875 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
877 (void)fchmod(PL_lastfd,PL_filemode);
879 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
881 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
883 (void)fchown(PL_lastfd,fileuid,filegid);
886 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
891 return IoIFP(GvIOp(gv));
894 if (ckWARN_d(WARN_INPLACE)) {
895 const int eno = errno;
896 if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0
897 && !S_ISREG(PL_statbuf.st_mode))
899 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
900 "Can't do inplace edit: %s is not a regular file",
904 Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't open %s: %s",
905 PL_oldname, Strerror(eno));
909 if (io && (IoFLAGS(io) & IOf_ARGV))
910 IoFLAGS(io) |= IOf_START;
912 (void)do_close(PL_argvoutgv,FALSE);
913 if (io && (IoFLAGS(io) & IOf_ARGV)
914 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
916 GV * const oldout = MUTABLE_GV(av_pop(PL_argvout_stack));
918 SvREFCNT_dec_NN(oldout);
921 setdefout(gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO));
926 /* explicit renamed to avoid C++ conflict -- kja */
928 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
936 if (!gv || !isGV_with_GP(gv)) {
938 SETERRNO(EBADF,SS_IVCHAN);
942 if (!io) { /* never opened */
945 SETERRNO(EBADF,SS_IVCHAN);
949 retval = io_close(io, not_implicit);
953 IoLINES_LEFT(io) = IoPAGE_LEN(io);
955 IoTYPE(io) = IoTYPE_CLOSED;
960 Perl_io_close(pTHX_ IO *io, bool not_implicit)
965 PERL_ARGS_ASSERT_IO_CLOSE;
968 if (IoTYPE(io) == IoTYPE_PIPE) {
969 const int status = PerlProc_pclose(IoIFP(io));
971 STATUS_NATIVE_CHILD_SET(status);
972 retval = (STATUS_UNIX == 0);
975 retval = (status != -1);
978 else if (IoTYPE(io) == IoTYPE_STD)
981 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
982 const bool prev_err = PerlIO_error(IoOFP(io));
983 retval = (PerlIO_close(IoOFP(io)) != EOF && !prev_err);
984 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
987 const bool prev_err = PerlIO_error(IoIFP(io));
988 retval = (PerlIO_close(IoIFP(io)) != EOF && !prev_err);
991 IoOFP(io) = IoIFP(io) = NULL;
993 else if (not_implicit) {
994 SETERRNO(EBADF,SS_IVCHAN);
1001 Perl_do_eof(pTHX_ GV *gv)
1004 IO * const io = GvIO(gv);
1006 PERL_ARGS_ASSERT_DO_EOF;
1010 else if (IoTYPE(io) == IoTYPE_WRONLY)
1011 report_wrongway_fh(gv, '>');
1014 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
1015 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
1016 return FALSE; /* this is the most usual case */
1020 /* getc and ungetc can stomp on errno */
1022 const int ch = PerlIO_getc(IoIFP(io));
1024 (void)PerlIO_ungetc(IoIFP(io),ch);
1031 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
1032 if (PerlIO_get_cnt(IoIFP(io)) < -1)
1033 PerlIO_set_cnt(IoIFP(io),-1);
1035 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
1036 if (gv != PL_argvgv || !nextargv(gv)) /* get another fp handy */
1040 return TRUE; /* normal fp, definitely end of file */
1046 Perl_do_tell(pTHX_ GV *gv)
1049 IO *const io = GvIO(gv);
1052 PERL_ARGS_ASSERT_DO_TELL;
1054 if (io && (fp = IoIFP(io))) {
1055 return PerlIO_tell(fp);
1058 SETERRNO(EBADF,RMS_IFI);
1063 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
1066 IO *const io = GvIO(gv);
1069 if (io && (fp = IoIFP(io))) {
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);
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_ 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* const no_prev_lstat = "The stat preceding -l _ wasn't an lstat";
1338 SV* const sv = TOPs;
1340 if (PL_op->op_flags & OPf_REF) {
1341 if (cGVOP_gv == PL_defgv) {
1342 if (PL_laststype != OP_LSTAT)
1343 Perl_croak(aTHX_ "%s", no_prev_lstat);
1344 return PL_laststatval;
1346 PL_laststatval = -1;
1347 if (ckWARN(WARN_IO)) {
1348 /* diag_listed_as: Use of -l on filehandle%s */
1349 Perl_warner(aTHX_ packWARN(WARN_IO),
1350 "Use of -l on filehandle %"HEKf,
1351 HEKfARG(GvENAME_HEK(cGVOP_gv)));
1355 if ((PL_op->op_private & (OPpFT_STACKED|OPpFT_AFTER_t))
1357 if (PL_laststype != OP_LSTAT)
1358 Perl_croak(aTHX_ "%s", no_prev_lstat);
1359 return PL_laststatval;
1362 PL_laststype = OP_LSTAT;
1364 if ( ( (SvROK(sv) && ( isGV_with_GP(SvRV(sv))
1365 || (isio = SvTYPE(SvRV(sv)) == SVt_PVIO) )
1369 && ckWARN(WARN_IO)) {
1371 /* diag_listed_as: Use of -l on filehandle%s */
1372 Perl_warner(aTHX_ packWARN(WARN_IO),
1373 "Use of -l on filehandle");
1375 /* diag_listed_as: Use of -l on filehandle%s */
1376 Perl_warner(aTHX_ packWARN(WARN_IO),
1377 "Use of -l on filehandle %"HEKf,
1378 GvENAME_HEK((const GV *)
1379 (SvROK(sv) ? SvRV(sv) : sv)));
1381 file = SvPV_flags_const_nolen(sv, flags);
1382 sv_setpv(PL_statname,file);
1383 PL_laststatval = PerlLIO_lstat(file,&PL_statcache);
1384 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(file, '\n'))
1385 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "lstat");
1386 return PL_laststatval;
1390 S_exec_failed(pTHX_ const char *cmd, int fd, int do_report)
1392 const int e = errno;
1393 PERL_ARGS_ASSERT_EXEC_FAILED;
1394 if (ckWARN(WARN_EXEC))
1395 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
1398 (void)PerlLIO_write(fd, (void*)&e, sizeof(int));
1404 Perl_do_aexec5(pTHX_ SV *really, SV **mark, SV **sp,
1405 int fd, int do_report)
1408 PERL_ARGS_ASSERT_DO_AEXEC5;
1409 #if defined(__SYMBIAN32__) || defined(__LIBCATAMOUNT__)
1410 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1414 const char *tmps = NULL;
1415 Newx(PL_Argv, sp - mark + 1, const char*);
1418 while (++mark <= sp) {
1420 *a++ = SvPV_nolen_const(*mark);
1426 tmps = SvPV_nolen_const(really);
1427 if ((!really && *PL_Argv[0] != '/') ||
1428 (really && *tmps != '/')) /* will execvp use PATH? */
1429 TAINT_ENV(); /* testing IFS here is overkill, probably */
1431 if (really && *tmps)
1432 PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
1434 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1436 S_exec_failed(aTHX_ (really ? tmps : PL_Argv[0]), fd, do_report);
1444 Perl_do_execfree(pTHX)
1453 #ifdef PERL_DEFAULT_DO_EXEC3_IMPLEMENTATION
1456 Perl_do_exec3(pTHX_ const char *incmd, int fd, int do_report)
1463 /* Make a copy so we can change it */
1464 const Size_t cmdlen = strlen(incmd) + 1;
1466 PERL_ARGS_ASSERT_DO_EXEC3;
1468 Newx(buf, cmdlen, char);
1470 memcpy(cmd, incmd, cmdlen);
1472 while (*cmd && isSPACE(*cmd))
1475 /* save an extra exec if possible */
1479 char flags[PERL_FLAGS_MAX];
1480 if (strnEQ(cmd,PL_cshname,PL_cshlen) &&
1481 strnEQ(cmd+PL_cshlen," -c",3)) {
1482 my_strlcpy(flags, "-c", PERL_FLAGS_MAX);
1483 s = cmd+PL_cshlen+3;
1486 my_strlcat(flags, "f", PERL_FLAGS_MAX - 2);
1491 char * const ncmd = s;
1497 if (s[-1] == '\'') {
1500 PerlProc_execl(PL_cshname, "csh", flags, ncmd, (char*)NULL);
1503 S_exec_failed(aTHX_ PL_cshname, fd, do_report);
1512 /* see if there are shell metacharacters in it */
1514 if (*cmd == '.' && isSPACE(cmd[1]))
1517 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1521 while (isWORDCHAR(*s))
1522 s++; /* catch VAR=val gizmo */
1526 for (s = cmd; *s; s++) {
1527 if (*s != ' ' && !isALPHA(*s) &&
1528 strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1529 if (*s == '\n' && !s[1]) {
1533 /* handle the 2>&1 construct at the end */
1534 if (*s == '>' && s[1] == '&' && s[2] == '1'
1535 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1536 && (!s[3] || isSPACE(s[3])))
1538 const char *t = s + 3;
1540 while (*t && isSPACE(*t))
1542 if (!*t && (PerlLIO_dup2(1,2) != -1)) {
1549 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char *)NULL);
1551 S_exec_failed(aTHX_ PL_sh_path, fd, do_report);
1557 Newx(PL_Argv, (s - cmd) / 2 + 2, const char*);
1558 PL_Cmd = savepvn(cmd, s-cmd);
1560 for (s = PL_Cmd; *s;) {
1565 while (*s && !isSPACE(*s))
1573 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1575 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1579 S_exec_failed(aTHX_ PL_Argv[0], fd, do_report);
1586 #endif /* OS2 || WIN32 */
1589 #include <starlet.h> /* for sys$delprc */
1593 Perl_apply(pTHX_ I32 type, SV **mark, SV **sp)
1598 const char *const what = PL_op_name[type];
1601 SV ** const oldmark = mark;
1602 bool killgp = FALSE;
1604 PERL_ARGS_ASSERT_APPLY;
1606 PERL_UNUSED_VAR(what); /* may not be used depending on compile options */
1608 /* Doing this ahead of the switch statement preserves the old behaviour,
1609 where attempting to use kill as a taint test test would fail on
1610 platforms where kill was not defined. */
1612 if (type == OP_KILL)
1613 Perl_die(aTHX_ PL_no_func, what);
1616 if (type == OP_CHOWN)
1617 Perl_die(aTHX_ PL_no_func, what);
1621 #define APPLY_TAINT_PROPER() \
1623 if (TAINT_get) { TAINT_PROPER(what); } \
1626 /* This is a first heuristic; it doesn't catch tainting magic. */
1628 while (++mark <= sp) {
1629 if (SvTAINTED(*mark)) {
1638 APPLY_TAINT_PROPER();
1641 APPLY_TAINT_PROPER();
1643 while (++mark <= sp) {
1645 if ((gv = MAYBE_DEREF_GV(*mark))) {
1646 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1648 APPLY_TAINT_PROPER();
1649 if (fchmod(PerlIO_fileno(IoIFP(GvIOn(gv))), val))
1652 Perl_die(aTHX_ PL_no_func, "fchmod");
1660 const char *name = SvPV_nomg_const(*mark, len);
1661 APPLY_TAINT_PROPER();
1662 if (!IS_SAFE_PATHNAME(name, len, "chmod") ||
1663 PerlLIO_chmod(name, val)) {
1672 APPLY_TAINT_PROPER();
1673 if (sp - mark > 2) {
1675 val = SvIVx(*++mark);
1676 val2 = SvIVx(*++mark);
1677 APPLY_TAINT_PROPER();
1679 while (++mark <= sp) {
1681 if ((gv = MAYBE_DEREF_GV(*mark))) {
1682 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1684 APPLY_TAINT_PROPER();
1685 if (fchown(PerlIO_fileno(IoIFP(GvIOn(gv))), val, val2))
1688 Perl_die(aTHX_ PL_no_func, "fchown");
1696 const char *name = SvPV_nomg_const(*mark, len);
1697 APPLY_TAINT_PROPER();
1698 if (!IS_SAFE_PATHNAME(name, len, "chown") ||
1699 PerlLIO_chown(name, val, val2)) {
1708 XXX Should we make lchown() directly available from perl?
1709 For now, we'll let Configure test for HAS_LCHOWN, but do
1710 nothing in the core.
1715 APPLY_TAINT_PROPER();
1718 s = SvPVx_const(*++mark, len);
1719 if (*s == '-' && isALPHA(s[1]))
1726 if (*s == 'S' && s[1] == 'I' && s[2] == 'G') {
1730 if ((val = whichsig_pvn(s, len)) < 0)
1731 Perl_croak(aTHX_ "Unrecognized signal name \"%"SVf"\"", SVfARG(*mark));
1742 APPLY_TAINT_PROPER();
1745 /* kill() doesn't do process groups (job trees?) under VMS */
1746 if (val == SIGKILL) {
1747 /* Use native sys$delprc() to insure that target process is
1748 * deleted; supervisor-mode images don't pay attention to
1749 * CRTL's emulation of Unix-style signals and kill()
1751 while (++mark <= sp) {
1753 unsigned long int __vmssts;
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);
1758 APPLY_TAINT_PROPER();
1759 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1763 case SS$_NOSUCHNODE:
1764 SETERRNO(ESRCH,__vmssts);
1767 SETERRNO(EPERM,__vmssts);
1770 SETERRNO(EVMSERR,__vmssts);
1778 while (++mark <= sp) {
1781 if (!(SvNIOK(*mark) || looks_like_number(*mark)))
1782 Perl_croak(aTHX_ "Can't kill a non-numeric process ID");
1783 proc = SvIV_nomg(*mark);
1788 APPLY_TAINT_PROPER();
1789 if (PerlProc_kill(proc, val))
1796 APPLY_TAINT_PROPER();
1798 while (++mark <= sp) {
1799 s = SvPV_const(*mark, len);
1800 APPLY_TAINT_PROPER();
1801 if (!IS_SAFE_PATHNAME(s, len, "unlink")) {
1804 else if (PerlProc_geteuid() || PL_unsafe) {
1808 else { /* don't let root wipe out directories without -U */
1809 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1818 #if defined(HAS_UTIME) || defined(HAS_FUTIMES)
1820 APPLY_TAINT_PROPER();
1821 if (sp - mark > 2) {
1822 #if defined(HAS_FUTIMES)
1823 struct timeval utbuf[2];
1824 void *utbufp = utbuf;
1825 #elif defined(I_UTIME) || defined(VMS)
1826 struct utimbuf utbuf;
1827 struct utimbuf *utbufp = &utbuf;
1833 void *utbufp = &utbuf;
1836 SV* const accessed = *++mark;
1837 SV* const modified = *++mark;
1839 /* Be like C, and if both times are undefined, let the C
1840 * library figure out what to do. This usually means
1841 * "current time". */
1843 if ( accessed == &PL_sv_undef && modified == &PL_sv_undef )
1846 Zero(&utbuf, sizeof utbuf, char);
1848 utbuf[0].tv_sec = (long)SvIV(accessed); /* time accessed */
1849 utbuf[0].tv_usec = 0;
1850 utbuf[1].tv_sec = (long)SvIV(modified); /* time modified */
1851 utbuf[1].tv_usec = 0;
1852 #elif defined(BIG_TIME)
1853 utbuf.actime = (Time_t)SvNV(accessed); /* time accessed */
1854 utbuf.modtime = (Time_t)SvNV(modified); /* time modified */
1856 utbuf.actime = (Time_t)SvIV(accessed); /* time accessed */
1857 utbuf.modtime = (Time_t)SvIV(modified); /* time modified */
1860 APPLY_TAINT_PROPER();
1862 while (++mark <= sp) {
1864 if ((gv = MAYBE_DEREF_GV(*mark))) {
1865 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1867 APPLY_TAINT_PROPER();
1868 if (futimes(PerlIO_fileno(IoIFP(GvIOn(gv))),
1869 (struct timeval *) utbufp))
1872 Perl_die(aTHX_ PL_no_func, "futimes");
1880 const char * const name = SvPV_nomg_const(*mark, len);
1881 APPLY_TAINT_PROPER();
1882 if (!IS_SAFE_PATHNAME(name, len, "utime")) {
1887 if (utimes(name, (struct timeval *)utbufp))
1889 if (PerlLIO_utime(name, utbufp))
1903 #undef APPLY_TAINT_PROPER
1906 /* Do the permissions allow some operation? Assumes statcache already set. */
1907 #ifndef VMS /* VMS' cando is in vms.c */
1909 Perl_cando(pTHX_ Mode_t mode, bool effective, const Stat_t *statbufp)
1910 /* effective is a flag, true for EUID, or for checking if the effective gid
1911 * is in the list of groups returned from getgroups().
1916 PERL_ARGS_ASSERT_CANDO;
1919 /* [Comments and code from Len Reed]
1920 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1921 * to write-protected files. The execute permission bit is set
1922 * by the Microsoft C library stat() function for the following:
1927 * All files and directories are readable.
1928 * Directories and special files, e.g. "CON", cannot be
1930 * [Comment by Tom Dinger -- a directory can have the write-protect
1931 * bit set in the file system, but DOS permits changes to
1932 * the directory anyway. In addition, all bets are off
1933 * here for networked software, such as Novell and
1937 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1938 * too so it will actually look into the files for magic numbers
1940 return (mode & statbufp->st_mode) ? TRUE : FALSE;
1942 #else /* ! DOSISH */
1944 if (ingroup(544,effective)) { /* member of Administrators */
1946 if ((effective ? PerlProc_geteuid() : PerlProc_getuid()) == 0) { /* root is special */
1948 if (mode == S_IXUSR) {
1949 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1953 return TRUE; /* root reads and writes anything */
1956 if (statbufp->st_uid == (effective ? PerlProc_geteuid() : PerlProc_getuid()) ) {
1957 if (statbufp->st_mode & mode)
1958 return TRUE; /* ok as "user" */
1960 else if (ingroup(statbufp->st_gid,effective)) {
1961 if (statbufp->st_mode & mode >> 3)
1962 return TRUE; /* ok as "group" */
1964 else if (statbufp->st_mode & mode >> 6)
1965 return TRUE; /* ok as "other" */
1967 #endif /* ! DOSISH */
1972 S_ingroup(pTHX_ Gid_t testgid, bool effective)
1975 if (testgid == (effective ? PerlProc_getegid() : PerlProc_getgid()))
1977 #ifdef HAS_GETGROUPS
1979 Groups_t *gary = NULL;
1983 anum = getgroups(0, gary);
1984 Newx(gary, anum, Groups_t);
1985 anum = getgroups(anum, gary);
1987 if (gary[anum] == testgid) {
2000 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
2003 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
2006 const key_t key = (key_t)SvNVx(*++mark);
2007 SV *nsv = optype == OP_MSGGET ? NULL : *++mark;
2008 const I32 flags = SvIVx(*++mark);
2010 PERL_ARGS_ASSERT_DO_IPCGET;
2011 PERL_UNUSED_ARG(sp);
2018 return msgget(key, flags);
2022 return semget(key, (int) SvIV(nsv), flags);
2026 return shmget(key, (size_t) SvUV(nsv), flags);
2028 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
2030 /* diag_listed_as: msg%s not implemented */
2031 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2034 return -1; /* should never happen */
2038 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
2043 const I32 id = SvIVx(*++mark);
2045 const I32 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
2047 const I32 cmd = SvIVx(*++mark);
2048 SV * const astr = *++mark;
2049 STRLEN infosize = 0;
2050 I32 getinfo = (cmd == IPC_STAT);
2052 PERL_ARGS_ASSERT_DO_IPCCTL;
2053 PERL_UNUSED_ARG(sp);
2059 if (cmd == IPC_STAT || cmd == IPC_SET)
2060 infosize = sizeof(struct msqid_ds);
2065 if (cmd == IPC_STAT || cmd == IPC_SET)
2066 infosize = sizeof(struct shmid_ds);
2072 if (cmd == IPC_STAT || cmd == IPC_SET)
2073 infosize = sizeof(struct semid_ds);
2074 else if (cmd == GETALL || cmd == SETALL)
2076 struct semid_ds semds;
2078 #ifdef EXTRA_F_IN_SEMUN_BUF
2079 semun.buff = &semds;
2083 getinfo = (cmd == GETALL);
2084 if (Semctl(id, 0, IPC_STAT, semun) == -1)
2086 infosize = semds.sem_nsems * sizeof(short);
2087 /* "short" is technically wrong but much more portable
2088 than guessing about u_?short(_t)? */
2091 /* diag_listed_as: sem%s not implemented */
2092 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2096 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
2098 /* diag_listed_as: shm%s not implemented */
2099 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2107 SvPV_force_nolen(astr);
2108 a = SvGROW(astr, infosize+1);
2113 a = SvPV(astr, len);
2114 if (len != infosize)
2115 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
2123 const IV i = SvIV(astr);
2124 a = INT2PTR(char *,i); /* ouch */
2131 ret = msgctl(id, cmd, (struct msqid_ds *)a);
2137 union semun unsemds;
2139 #ifdef EXTRA_F_IN_SEMUN_BUF
2140 unsemds.buff = (struct semid_ds *)a;
2142 unsemds.buf = (struct semid_ds *)a;
2144 ret = Semctl(id, n, cmd, unsemds);
2146 /* diag_listed_as: sem%s not implemented */
2147 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2154 ret = shmctl(id, cmd, (struct shmid_ds *)a);
2158 if (getinfo && ret >= 0) {
2159 SvCUR_set(astr, infosize);
2160 *SvEND(astr) = '\0';
2167 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
2172 const I32 id = SvIVx(*++mark);
2173 SV * const mstr = *++mark;
2174 const I32 flags = SvIVx(*++mark);
2175 const char * const mbuf = SvPV_const(mstr, len);
2176 const I32 msize = len - sizeof(long);
2178 PERL_ARGS_ASSERT_DO_MSGSND;
2179 PERL_UNUSED_ARG(sp);
2182 Perl_croak(aTHX_ "Arg too short for msgsnd");
2184 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
2186 PERL_UNUSED_ARG(sp);
2187 PERL_UNUSED_ARG(mark);
2188 /* diag_listed_as: msg%s not implemented */
2189 Perl_croak(aTHX_ "msgsnd not implemented");
2195 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
2201 I32 msize, flags, ret;
2202 const I32 id = SvIVx(*++mark);
2203 SV * const mstr = *++mark;
2205 PERL_ARGS_ASSERT_DO_MSGRCV;
2206 PERL_UNUSED_ARG(sp);
2208 /* suppress warning when reading into undef var --jhi */
2210 sv_setpvs(mstr, "");
2211 msize = SvIVx(*++mark);
2212 mtype = (long)SvIVx(*++mark);
2213 flags = SvIVx(*++mark);
2214 SvPV_force_nolen(mstr);
2215 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
2218 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
2220 SvCUR_set(mstr, sizeof(long)+ret);
2221 *SvEND(mstr) = '\0';
2222 /* who knows who has been playing with this message? */
2227 PERL_UNUSED_ARG(sp);
2228 PERL_UNUSED_ARG(mark);
2229 /* diag_listed_as: msg%s not implemented */
2230 Perl_croak(aTHX_ "msgrcv not implemented");
2236 Perl_do_semop(pTHX_ SV **mark, SV **sp)
2241 const I32 id = SvIVx(*++mark);
2242 SV * const opstr = *++mark;
2243 const char * const opbuf = SvPV_const(opstr, opsize);
2245 PERL_ARGS_ASSERT_DO_SEMOP;
2246 PERL_UNUSED_ARG(sp);
2248 if (opsize < 3 * SHORTSIZE
2249 || (opsize % (3 * SHORTSIZE))) {
2250 SETERRNO(EINVAL,LIB_INVARG);
2254 /* We can't assume that sizeof(struct sembuf) == 3 * sizeof(short). */
2256 const int nsops = opsize / (3 * sizeof (short));
2258 short * const ops = (short *) opbuf;
2260 struct sembuf *temps, *t;
2263 Newx (temps, nsops, struct sembuf);
2271 result = semop(id, temps, nsops);
2285 /* diag_listed_as: sem%s not implemented */
2286 Perl_croak(aTHX_ "semop not implemented");
2291 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
2296 struct shmid_ds shmds;
2297 const I32 id = SvIVx(*++mark);
2298 SV * const mstr = *++mark;
2299 const I32 mpos = SvIVx(*++mark);
2300 const I32 msize = SvIVx(*++mark);
2302 PERL_ARGS_ASSERT_DO_SHMIO;
2303 PERL_UNUSED_ARG(sp);
2306 if (shmctl(id, IPC_STAT, &shmds) == -1)
2308 if (mpos < 0 || msize < 0
2309 || (size_t)mpos + msize > (size_t)shmds.shm_segsz) {
2310 SETERRNO(EFAULT,SS_ACCVIO); /* can't do as caller requested */
2313 shm = (char *)shmat(id, NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
2314 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2316 if (optype == OP_SHMREAD) {
2318 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2320 SvUPGRADE(mstr, SVt_PV);
2322 sv_setpvs(mstr, "");
2324 mbuf = SvGROW(mstr, (STRLEN)msize+1);
2326 Copy(shm + mpos, mbuf, msize, char);
2327 SvCUR_set(mstr, msize);
2328 *SvEND(mstr) = '\0';
2330 /* who knows who has been playing with this shared memory? */
2336 const char *mbuf = SvPV_const(mstr, len);
2337 const I32 n = ((I32)len > msize) ? msize : (I32)len;
2338 Copy(mbuf, shm + mpos, n, char);
2340 memzero(shm + mpos + n, msize - n);
2344 /* diag_listed_as: shm%s not implemented */
2345 Perl_croak(aTHX_ "shm I/O not implemented");
2350 #endif /* SYSV IPC */
2355 =for apidoc start_glob
2357 Function called by C<do_readline> to spawn a glob (or do the glob inside
2358 perl on VMS). This code used to be inline, but now perl uses C<File::Glob>
2359 this glob starter is only used by miniperl during the build process.
2360 Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up.
2366 Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
2369 SV * const tmpcmd = newSV(0);
2372 const char *s = SvPV(tmpglob, len);
2374 PERL_ARGS_ASSERT_START_GLOB;
2376 if (!IS_SAFE_SYSCALL(s, len, "pattern", "glob"))
2381 #ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
2382 /* since spawning off a process is a real performance hit */
2389 fp = Perl_vms_start_glob(aTHX_ tmpglob, io);
2394 sv_setpv(tmpcmd, "for a in ");
2395 sv_catsv(tmpcmd, tmpglob);
2396 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
2399 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
2400 sv_catsv(tmpcmd, tmpglob);
2402 sv_setpv(tmpcmd, "perlglob ");
2403 sv_catsv(tmpcmd, tmpglob);
2404 sv_catpv(tmpcmd, " |");
2409 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
2410 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
2411 sv_catsv(tmpcmd, tmpglob);
2412 sv_catpv(tmpcmd, "' 2>/dev/null |");
2414 sv_setpv(tmpcmd, "echo ");
2415 sv_catsv(tmpcmd, tmpglob);
2417 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
2419 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
2422 #endif /* !DOSISH */
2424 GV * const envgv = gv_fetchpvs("ENV", 0, SVt_PVHV);
2425 SV ** const home = hv_fetchs(GvHV(envgv), "HOME", 0);
2426 SV ** const path = hv_fetchs(GvHV(envgv), "PATH", 0);
2427 if (home && *home) SvGETMAGIC(*home);
2428 if (path && *path) SvGETMAGIC(*path);
2429 save_hash(gv_fetchpvs("ENV", 0, SVt_PVHV));
2430 if (home && *home) SvSETMAGIC(*home);
2431 if (path && *path) SvSETMAGIC(*path);
2433 (void)do_open(PL_last_in_gv, (char*)SvPVX_const(tmpcmd), SvCUR(tmpcmd),
2434 FALSE, O_RDONLY, 0, NULL);
2439 if (!fp && ckWARN(WARN_GLOB)) {
2440 Perl_warner(aTHX_ packWARN(WARN_GLOB), "glob failed (can't start child: %s)",
2449 * c-indentation-style: bsd
2451 * indent-tabs-mode: nil
2454 * ex: set ts=8 sts=4 sw=4 et: