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 if (!IS_SAFE_PATHNAME(*svp, "open"))
222 name = (SvOK(*svp) || SvGMAGICAL(*svp)) ?
223 savesvpv (*svp) : savepvs ("");
231 if ((*type == IoTYPE_RDWR) && /* scary */
232 (*(type+1) == IoTYPE_RDONLY || *(type+1) == IoTYPE_WRONLY) &&
233 ((!num_svs || (tend > type+1 && tend[-1] != IoTYPE_PIPE)))) {
234 TAINT_PROPER("open");
239 if (*type == IoTYPE_PIPE) {
241 if (type[1] != IoTYPE_STD) {
243 Perl_croak(aTHX_ "Unknown open() mode '%.*s'", (int)olen, oname);
249 } while (isSPACE(*type));
255 /* command is missing 19990114 */
256 if (ckWARN(WARN_PIPE))
257 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open");
261 if (!(*name == '-' && name[1] == '\0') || num_svs)
263 TAINT_PROPER("piped open");
264 if (!num_svs && name[len-1] == '|') {
266 if (ckWARN(WARN_PIPE))
267 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Can't open bidirectional pipe");
276 fp = PerlProc_popen_list(mode, num_svs, svp);
279 fp = PerlProc_popen(name,mode);
283 if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) {
289 else if (*type == IoTYPE_WRONLY) {
290 TAINT_PROPER("open");
292 if (*type == IoTYPE_WRONLY) {
293 /* Two IoTYPE_WRONLYs in a row make for an IoTYPE_APPEND. */
294 mode[0] = IoTYPE(io) = IoTYPE_APPEND;
308 dodup = PERLIO_DUP_FD;
314 if (!num_svs && !*type && supplied_fp) {
315 /* "<+&" etc. is used by typemaps */
319 PerlIO *that_fp = NULL;
321 /* diag_listed_as: More than one argument to '%s' open */
322 Perl_croak(aTHX_ "More than one argument to '%c&' open",IoTYPE(io));
324 while (isSPACE(*type))
328 || (SvPOKp(*svp) && looks_like_number(*svp))
333 else if (isDIGIT(*type)) {
339 thatio = sv_2io(*svp);
342 GV * const thatgv = gv_fetchpvn_flags(type, tend - type,
344 thatio = GvIO(thatgv);
348 SETERRNO(EINVAL,SS_IVCHAN);
352 if ((that_fp = IoIFP(thatio))) {
353 /* Flush stdio buffer before dup. --mjd
354 * Unfortunately SEEK_CURing 0 seems to
355 * be optimized away on most platforms;
356 * only Solaris and Linux seem to flush
359 /* sfio fails to clear error on next
360 sfwrite, contrary to documentation.
362 if (PerlIO_seek(that_fp, 0, SEEK_CUR) == -1)
363 PerlIO_clearerr(that_fp);
365 /* On the other hand, do all platforms
366 * take gracefully to flushing a read-only
367 * filehandle? Perhaps we should do
368 * fsetpos(src)+fgetpos(dst)? --nik */
369 PerlIO_flush(that_fp);
370 fd = PerlIO_fileno(that_fp);
371 /* When dup()ing STDIN, STDOUT or STDERR
372 * explicitly set appropriate access mode */
373 if (that_fp == PerlIO_stdout()
374 || that_fp == PerlIO_stderr())
375 IoTYPE(io) = IoTYPE_WRONLY;
376 else if (that_fp == PerlIO_stdin())
377 IoTYPE(io) = IoTYPE_RDONLY;
378 /* When dup()ing a socket, say result is
380 else if (IoTYPE(thatio) == IoTYPE_SOCKET)
381 IoTYPE(io) = IoTYPE_SOCKET;
389 fp = PerlIO_fdupopen(aTHX_ that_fp, NULL, dodup);
393 fd = PerlLIO_dup(fd);
396 if (!(fp = PerlIO_openn(aTHX_ type,mode,fd,0,0,NULL,num_svs,svp))) {
397 if (dodup && fd >= 0)
404 while (isSPACE(*type))
406 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
408 fp = PerlIO_stdout();
409 IoTYPE(io) = IoTYPE_STD;
411 /* diag_listed_as: More than one argument to '%s' open */
412 Perl_croak(aTHX_ "More than one argument to '>%c' open",IoTYPE_STD);
417 namesv = newSVpvn_flags(type, tend - type, SVs_TEMP);
422 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
425 if (!fp && type && *type && *type != ':' && !isIDFIRST(*type))
426 goto unknown_open_mode;
427 } /* IoTYPE_WRONLY */
428 else if (*type == IoTYPE_RDONLY) {
431 } while (isSPACE(*type));
440 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
443 IoTYPE(io) = IoTYPE_STD;
445 /* diag_listed_as: More than one argument to '%s' open */
446 Perl_croak(aTHX_ "More than one argument to '<%c' open",IoTYPE_STD);
451 namesv = newSVpvn_flags(type, tend - type, SVs_TEMP);
456 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
458 if (!fp && type && *type && *type != ':' && !isIDFIRST(*type))
459 goto unknown_open_mode;
460 } /* IoTYPE_RDONLY */
461 else if ((num_svs && /* '-|...' or '...|' */
462 type[0] == IoTYPE_STD && type[1] == IoTYPE_PIPE) ||
463 (!num_svs && tend > type+1 && tend[-1] == IoTYPE_PIPE)) {
465 type += 2; /* skip over '-|' */
469 while (tend > type && isSPACE(tend[-1]))
471 for (; isSPACE(*type); type++)
477 /* command is missing 19990114 */
478 if (ckWARN(WARN_PIPE))
479 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open");
483 if (!(*name == '-' && name[1] == '\0') || num_svs)
485 TAINT_PROPER("piped open");
494 fp = PerlProc_popen_list(mode,num_svs,svp);
497 fp = PerlProc_popen(name,mode);
499 IoTYPE(io) = IoTYPE_PIPE;
501 while (isSPACE(*type))
504 if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) {
510 else { /* layer(Args) */
512 goto unknown_open_mode;
514 IoTYPE(io) = IoTYPE_RDONLY;
515 for (; isSPACE(*name); name++)
524 if (*name == '-' && name[1] == '\0') {
526 IoTYPE(io) = IoTYPE_STD;
530 namesv = newSVpvn_flags(type, tend - type, SVs_TEMP);
535 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
540 if (IoTYPE(io) == IoTYPE_RDONLY && ckWARN(WARN_NEWLINE)
541 && strchr(oname, '\n')
544 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
548 if (ckWARN(WARN_IO)) {
549 if ((IoTYPE(io) == IoTYPE_RDONLY) &&
550 (fp == PerlIO_stdout() || fp == PerlIO_stderr())) {
551 Perl_warner(aTHX_ packWARN(WARN_IO),
552 "Filehandle STD%s reopened as %"HEKf
554 ((fp == PerlIO_stdout()) ? "OUT" : "ERR"),
555 HEKfARG(GvENAME_HEK(gv)));
557 else if ((IoTYPE(io) == IoTYPE_WRONLY) && fp == PerlIO_stdin()) {
558 Perl_warner(aTHX_ packWARN(WARN_IO),
559 "Filehandle STDIN reopened as %"HEKf" only for output",
560 HEKfARG(GvENAME_HEK(gv))
565 fd = PerlIO_fileno(fp);
566 /* If there is no fd (e.g. PerlIO::scalar) assume it isn't a
567 * socket - this covers PerlIO::scalar - otherwise unless we "know" the
568 * type probe for socket-ness.
570 if (IoTYPE(io) && IoTYPE(io) != IoTYPE_PIPE && IoTYPE(io) != IoTYPE_STD && fd >= 0) {
571 if (PerlLIO_fstat(fd,&PL_statbuf) < 0) {
572 /* If PerlIO claims to have fd we had better be able to fstat() it. */
573 (void) PerlIO_close(fp);
577 if (S_ISSOCK(PL_statbuf.st_mode))
578 IoTYPE(io) = IoTYPE_SOCKET; /* in case a socket was passed in to us */
582 !(PL_statbuf.st_mode & S_IFMT)
586 && IoTYPE(io) != IoTYPE_WRONLY /* Dups of STD* filehandles already have */
587 && IoTYPE(io) != IoTYPE_RDONLY /* type so they aren't marked as sockets */
588 ) { /* on OS's that return 0 on fstat()ed pipe */
590 Sock_size_t buflen = sizeof tmpbuf;
591 if (PerlSock_getsockname(fd, (struct sockaddr *)tmpbuf, &buflen) >= 0
592 || errno != ENOTSOCK)
593 IoTYPE(io) = IoTYPE_SOCKET; /* some OS's return 0 on fstat()ed socket */
594 /* but some return 0 for streams too, sigh */
596 #endif /* HAS_SOCKET */
597 #endif /* !PERL_MICRO */
601 * If this is a standard handle we discard all the layer stuff
602 * and just dup the fd into whatever was on the handle before !
605 if (saveifp) { /* must use old fp? */
606 /* If fd is less that PL_maxsysfd i.e. STDIN..STDERR
607 then dup the new fileno down
610 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
611 if (saveofp != saveifp) { /* was a socket? */
612 PerlIO_close(saveofp);
616 /* Still a small can-of-worms here if (say) PerlIO::scalar
617 is assigned to (say) STDOUT - for now let dup2() fail
618 and provide the error
620 if (PerlLIO_dup2(fd, savefd) < 0) {
621 (void)PerlIO_close(fp);
625 if (savefd != PerlIO_fileno(PerlIO_stdin())) {
626 char newname[FILENAME_MAX+1];
627 if (PerlIO_getname(fp, newname)) {
628 if (fd == PerlIO_fileno(PerlIO_stdout()))
629 vmssetuserlnm("SYS$OUTPUT", newname);
630 if (fd == PerlIO_fileno(PerlIO_stderr()))
631 vmssetuserlnm("SYS$ERROR", newname);
637 /* PL_fdpid isn't used on Windows, so avoid this useless work.
638 * XXX Probably the same for a lot of other places. */
643 sv = *av_fetch(PL_fdpid,fd,TRUE);
644 SvUPGRADE(sv, SVt_IV);
647 sv = *av_fetch(PL_fdpid,savefd,TRUE);
648 SvUPGRADE(sv, SVt_IV);
654 /* need to close fp without closing underlying fd */
655 int ofd = PerlIO_fileno(fp);
656 int dupfd = PerlLIO_dup(ofd);
657 #if defined(HAS_FCNTL) && defined(F_SETFD)
658 /* Assume if we have F_SETFD we have F_GETFD */
659 int coe = fcntl(ofd,F_GETFD);
662 PerlLIO_dup2(dupfd,ofd);
663 #if defined(HAS_FCNTL) && defined(F_SETFD)
664 /* The dup trick has lost close-on-exec on ofd */
665 fcntl(ofd,F_SETFD, coe);
667 PerlLIO_close(dupfd);
674 fd = PerlIO_fileno(fp);
676 #if defined(HAS_FCNTL) && defined(F_SETFD)
679 fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
685 IoFLAGS(io) &= ~IOf_NOLINE;
687 if (IoTYPE(io) == IoTYPE_SOCKET
688 || (IoTYPE(io) == IoTYPE_WRONLY && fd >= 0 && S_ISCHR(PL_statbuf.st_mode)) ) {
690 if (*s == IoTYPE_IMPLICIT || *s == IoTYPE_NUMERIC)
693 if (!(IoOFP(io) = PerlIO_openn(aTHX_ type,s,fd,0,0,NULL,0,svp))) {
707 IoTYPE(io) = savetype;
712 Perl_nextargv(pTHX_ GV *gv)
716 #ifndef FLEXFILENAMES
722 IO * const io = GvIOp(gv);
724 PERL_ARGS_ASSERT_NEXTARGV;
727 PL_argvoutgv = gv_fetchpvs("ARGVOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
728 if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
729 IoFLAGS(io) &= ~IOf_START;
732 Perl_av_create_and_push(aTHX_ &PL_argvout_stack,
733 SvREFCNT_inc_simple_NN(PL_defoutgv));
736 if (PL_filemode & (S_ISUID|S_ISGID)) {
737 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
740 (void)fchmod(PL_lastfd,PL_filemode);
742 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
749 while (av_len(GvAV(gv)) >= 0) {
751 sv = av_shift(GvAV(gv));
753 SvTAINTED_off(GvSVn(gv)); /* previous tainting irrelevant */
754 sv_setsv(GvSVn(gv),sv);
755 SvSETMAGIC(GvSV(gv));
756 PL_oldname = SvPVx(GvSV(gv), oldlen);
757 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,NULL)) {
759 TAINT_PROPER("inplace open");
760 if (oldlen == 1 && *PL_oldname == '-') {
761 setdefout(gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL,
763 return IoIFP(GvIOp(gv));
765 #ifndef FLEXFILENAMES
766 filedev = PL_statbuf.st_dev;
767 fileino = PL_statbuf.st_ino;
769 PL_filemode = PL_statbuf.st_mode;
770 fileuid = PL_statbuf.st_uid;
771 filegid = PL_statbuf.st_gid;
772 if (!S_ISREG(PL_filemode)) {
773 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
774 "Can't do inplace edit: %s is not a regular file",
779 if (*PL_inplace && strNE(PL_inplace, "*")) {
780 const char *star = strchr(PL_inplace, '*');
782 const char *begin = PL_inplace;
785 sv_catpvn(sv, begin, star - begin);
786 sv_catpvn(sv, PL_oldname, oldlen);
788 } while ((star = strchr(begin, '*')));
793 sv_catpv(sv,PL_inplace);
795 #ifndef FLEXFILENAMES
796 if ((PerlLIO_stat(SvPVX_const(sv),&PL_statbuf) >= 0
797 && PL_statbuf.st_dev == filedev
798 && PL_statbuf.st_ino == fileino)
800 || ((_djstat_fail_bits & _STFAIL_TRUENAME)!=0)
804 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
805 "Can't do inplace edit: %"SVf" would not be unique",
812 #if !defined(DOSISH) && !defined(__CYGWIN__)
813 if (PerlLIO_rename(PL_oldname,SvPVX_const(sv)) < 0) {
814 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
815 "Can't rename %s to %"SVf": %s, skipping file",
816 PL_oldname, SVfARG(sv), Strerror(errno));
822 (void)PerlLIO_unlink(SvPVX_const(sv));
823 (void)PerlLIO_rename(PL_oldname,SvPVX_const(sv));
824 do_open(gv,(char*)SvPVX_const(sv),SvCUR(sv),TRUE,O_RDONLY,0,NULL);
827 (void)UNLINK(SvPVX_const(sv));
828 if (link(PL_oldname,SvPVX_const(sv)) < 0) {
829 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
830 "Can't rename %s to %"SVf": %s, skipping file",
831 PL_oldname, SVfARG(sv), Strerror(errno) );
835 (void)UNLINK(PL_oldname);
839 #if !defined(DOSISH) && !defined(AMIGAOS)
840 # ifndef VMS /* Don't delete; use automatic file versioning */
841 if (UNLINK(PL_oldname) < 0) {
842 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
843 "Can't remove %s: %s, skipping file",
844 PL_oldname, Strerror(errno) );
850 Perl_croak(aTHX_ "Can't do inplace edit without backup");
854 sv_setpvn(sv,PL_oldname,oldlen);
855 SETERRNO(0,0); /* in case sprintf set errno */
856 if (!Perl_do_openn(aTHX_ PL_argvoutgv, (char*)SvPVX_const(sv),
859 O_WRONLY|O_CREAT|O_TRUNC,0,
861 O_WRONLY|O_CREAT|OPEN_EXCL,0600,
864 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE), "Can't do inplace edit on %s: %s",
865 PL_oldname, Strerror(errno) );
869 setdefout(PL_argvoutgv);
870 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
871 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
873 (void)fchmod(PL_lastfd,PL_filemode);
875 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
877 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
879 (void)fchown(PL_lastfd,fileuid,filegid);
882 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
887 return IoIFP(GvIOp(gv));
890 if (ckWARN_d(WARN_INPLACE)) {
891 const int eno = errno;
892 if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0
893 && !S_ISREG(PL_statbuf.st_mode))
895 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
896 "Can't do inplace edit: %s is not a regular file",
900 Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't open %s: %s",
901 PL_oldname, Strerror(eno));
905 if (io && (IoFLAGS(io) & IOf_ARGV))
906 IoFLAGS(io) |= IOf_START;
908 (void)do_close(PL_argvoutgv,FALSE);
909 if (io && (IoFLAGS(io) & IOf_ARGV)
910 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
912 GV * const oldout = MUTABLE_GV(av_pop(PL_argvout_stack));
914 SvREFCNT_dec_NN(oldout);
917 setdefout(gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO));
922 /* explicit renamed to avoid C++ conflict -- kja */
924 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
932 if (!gv || !isGV_with_GP(gv)) {
934 SETERRNO(EBADF,SS_IVCHAN);
938 if (!io) { /* never opened */
941 SETERRNO(EBADF,SS_IVCHAN);
945 retval = io_close(io, not_implicit);
949 IoLINES_LEFT(io) = IoPAGE_LEN(io);
951 IoTYPE(io) = IoTYPE_CLOSED;
956 Perl_io_close(pTHX_ IO *io, bool not_implicit)
961 PERL_ARGS_ASSERT_IO_CLOSE;
964 if (IoTYPE(io) == IoTYPE_PIPE) {
965 const int status = PerlProc_pclose(IoIFP(io));
967 STATUS_NATIVE_CHILD_SET(status);
968 retval = (STATUS_UNIX == 0);
971 retval = (status != -1);
974 else if (IoTYPE(io) == IoTYPE_STD)
977 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
978 const bool prev_err = PerlIO_error(IoOFP(io));
979 retval = (PerlIO_close(IoOFP(io)) != EOF && !prev_err);
980 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
983 const bool prev_err = PerlIO_error(IoIFP(io));
984 retval = (PerlIO_close(IoIFP(io)) != EOF && !prev_err);
987 IoOFP(io) = IoIFP(io) = NULL;
989 else if (not_implicit) {
990 SETERRNO(EBADF,SS_IVCHAN);
997 Perl_do_eof(pTHX_ GV *gv)
1000 IO * const io = GvIO(gv);
1002 PERL_ARGS_ASSERT_DO_EOF;
1006 else if (IoTYPE(io) == IoTYPE_WRONLY)
1007 report_wrongway_fh(gv, '>');
1010 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
1011 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
1012 return FALSE; /* this is the most usual case */
1016 /* getc and ungetc can stomp on errno */
1018 const int ch = PerlIO_getc(IoIFP(io));
1020 (void)PerlIO_ungetc(IoIFP(io),ch);
1027 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
1028 if (PerlIO_get_cnt(IoIFP(io)) < -1)
1029 PerlIO_set_cnt(IoIFP(io),-1);
1031 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
1032 if (gv != PL_argvgv || !nextargv(gv)) /* get another fp handy */
1036 return TRUE; /* normal fp, definitely end of file */
1042 Perl_do_tell(pTHX_ GV *gv)
1045 IO *const io = GvIO(gv);
1048 PERL_ARGS_ASSERT_DO_TELL;
1050 if (io && (fp = IoIFP(io))) {
1051 #ifdef ULTRIX_STDIO_BOTCH
1053 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
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 #ifdef ULTRIX_STDIO_BOTCH
1072 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1074 return PerlIO_seek(fp, pos, whence) >= 0;
1077 SETERRNO(EBADF,RMS_IFI);
1082 Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
1085 IO *const io = GvIO(gv);
1088 PERL_ARGS_ASSERT_DO_SYSSEEK;
1090 if (io && (fp = IoIFP(io)))
1091 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
1093 SETERRNO(EBADF,RMS_IFI);
1098 Perl_mode_from_discipline(pTHX_ const char *s, STRLEN len)
1100 int mode = O_BINARY;
1106 if (s[2] == 'a' && s[3] == 'w'
1107 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1116 if (s[2] == 'r' && s[3] == 'l' && s[4] == 'f'
1117 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1126 goto fail_discipline;
1129 else if (isSPACE(*s)) {
1136 end = strchr(s+1, ':');
1139 #ifndef PERLIO_LAYERS
1140 Perl_croak(aTHX_ "IO layers (like '%.*s') unavailable", end-s, s);
1151 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE)
1153 my_chsize(int fd, Off_t length)
1156 /* code courtesy of William Kucharski */
1161 if (PerlLIO_fstat(fd, &filebuf) < 0)
1164 if (filebuf.st_size < length) {
1166 /* extend file length */
1168 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
1171 /* write a "0" byte */
1173 if ((PerlLIO_write(fd, "", 1)) != 1)
1177 /* truncate length */
1181 fl.l_start = length;
1182 fl.l_type = F_WRLCK; /* write lock on file space */
1185 * This relies on the UNDOCUMENTED F_FREESP argument to
1186 * fcntl(2), which truncates the file so that it ends at the
1187 * position indicated by fl.l_start.
1189 * Will minor miracles never cease?
1192 if (fcntl(fd, F_FREESP, &fl) < 0)
1198 Perl_croak_nocontext("truncate not implemented");
1199 #endif /* F_FREESP */
1202 #endif /* !HAS_TRUNCATE && !HAS_CHSIZE */
1205 Perl_do_print(pTHX_ SV *sv, PerlIO *fp)
1209 PERL_ARGS_ASSERT_DO_PRINT;
1211 /* assuming fp is checked earlier */
1214 if (SvTYPE(sv) == SVt_IV && SvIOK(sv)) {
1215 assert(!SvGMAGICAL(sv));
1217 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
1219 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
1220 return !PerlIO_error(fp);
1224 /* Do this first to trigger any overloading. */
1225 const char *tmps = SvPV_const(sv, len);
1229 if (PerlIO_isutf8(fp)) { /* If the stream is utf8 ... */
1230 if (!SvUTF8(sv)) { /* Convert to utf8 if necessary */
1231 /* We don't modify the original scalar. */
1232 tmpbuf = bytes_to_utf8((const U8*) tmps, &len);
1233 tmps = (char *) tmpbuf;
1235 else if (ckWARN4_d(WARN_UTF8, WARN_SURROGATE, WARN_NON_UNICODE, WARN_NONCHAR)) {
1236 (void) check_utf8_print((const U8*) tmps, len);
1238 } /* else stream isn't utf8 */
1239 else if (DO_UTF8(sv)) { /* But if is utf8 internally, attempt to
1241 STRLEN tmplen = len;
1243 U8 * const result = bytes_from_utf8((const U8*) tmps, &tmplen, &utf8);
1246 /* Here, succeeded in downgrading from utf8. Set up to below
1247 * output the converted value */
1249 tmps = (char *) tmpbuf;
1252 else { /* Non-utf8 output stream, but string only representable in
1254 assert((char *)result == tmps);
1255 Perl_ck_warner_d(aTHX_ packWARN(WARN_UTF8),
1256 "Wide character in %s",
1257 PL_op ? OP_DESC(PL_op) : "print"
1259 /* Could also check that isn't one of the things to avoid
1260 * in utf8 by using check_utf8_print(), but not doing so,
1261 * since the stream isn't a UTF8 stream */
1264 /* To detect whether the process is about to overstep its
1265 * filesize limit we would need getrlimit(). We could then
1266 * also transparently raise the limit with setrlimit() --
1267 * but only until the system hard limit/the filesystem limit,
1268 * at which we would get EPERM. Note that when using buffered
1269 * io the write failure can be delayed until the flush/close. --jhi */
1270 if (len && (PerlIO_write(fp,tmps,len) == 0))
1273 return happy ? !PerlIO_error(fp) : FALSE;
1278 Perl_my_stat_flags(pTHX_ const U32 flags)
1285 if (PL_op->op_flags & OPf_REF) {
1289 return PL_laststatval;
1292 PL_laststype = OP_STAT;
1293 PL_statgv = gv ? gv : (GV *)io;
1294 sv_setpvs(PL_statname, "");
1297 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
1298 } else if (IoDIRP(io)) {
1299 return (PL_laststatval = PerlLIO_fstat(my_dirfd(IoDIRP(io)), &PL_statcache));
1302 PL_laststatval = -1;
1306 else if ((PL_op->op_private & (OPpFT_STACKED|OPpFT_AFTER_t))
1308 return PL_laststatval;
1310 SV* const sv = TOPs;
1313 if ((gv = MAYBE_DEREF_GV_flags(sv,flags))) {
1316 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
1317 io = MUTABLE_IO(SvRV(sv));
1319 goto do_fstat_have_io;
1322 s = SvPV_flags_const(sv, len, flags);
1324 sv_setpvn(PL_statname, s, len);
1325 s = SvPVX_const(PL_statname); /* s now NUL-terminated */
1326 PL_laststype = OP_STAT;
1327 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1328 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
1329 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
1330 return PL_laststatval;
1336 Perl_my_lstat_flags(pTHX_ const U32 flags)
1339 static const char* const no_prev_lstat = "The stat preceding -l _ wasn't an lstat";
1342 SV* const sv = TOPs;
1344 if (PL_op->op_flags & OPf_REF) {
1345 if (cGVOP_gv == PL_defgv) {
1346 if (PL_laststype != OP_LSTAT)
1347 Perl_croak(aTHX_ "%s", no_prev_lstat);
1348 return PL_laststatval;
1350 PL_laststatval = -1;
1351 if (ckWARN(WARN_IO)) {
1352 /* diag_listed_as: Use of -l on filehandle%s */
1353 Perl_warner(aTHX_ packWARN(WARN_IO),
1354 "Use of -l on filehandle %"HEKf,
1355 HEKfARG(GvENAME_HEK(cGVOP_gv)));
1359 if ((PL_op->op_private & (OPpFT_STACKED|OPpFT_AFTER_t))
1361 if (PL_laststype != OP_LSTAT)
1362 Perl_croak(aTHX_ "%s", no_prev_lstat);
1363 return PL_laststatval;
1366 PL_laststype = OP_LSTAT;
1368 if ( ( (SvROK(sv) && ( isGV_with_GP(SvRV(sv))
1369 || (isio = SvTYPE(SvRV(sv)) == SVt_PVIO) )
1373 && ckWARN(WARN_IO)) {
1375 /* diag_listed_as: Use of -l on filehandle%s */
1376 Perl_warner(aTHX_ packWARN(WARN_IO),
1377 "Use of -l on filehandle");
1379 /* diag_listed_as: Use of -l on filehandle%s */
1380 Perl_warner(aTHX_ packWARN(WARN_IO),
1381 "Use of -l on filehandle %"HEKf,
1382 GvENAME_HEK((const GV *)
1383 (SvROK(sv) ? SvRV(sv) : sv)));
1385 file = SvPV_flags_const_nolen(sv, flags);
1386 sv_setpv(PL_statname,file);
1387 PL_laststatval = PerlLIO_lstat(file,&PL_statcache);
1388 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(file, '\n'))
1389 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "lstat");
1390 return PL_laststatval;
1394 S_exec_failed(pTHX_ const char *cmd, int fd, int do_report)
1396 const int e = errno;
1397 PERL_ARGS_ASSERT_EXEC_FAILED;
1398 if (ckWARN(WARN_EXEC))
1399 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
1402 PerlLIO_write(fd, (void*)&e, sizeof(int));
1408 Perl_do_aexec5(pTHX_ SV *really, SV **mark, SV **sp,
1409 int fd, int do_report)
1412 PERL_ARGS_ASSERT_DO_AEXEC5;
1413 #if defined(__SYMBIAN32__) || defined(__LIBCATAMOUNT__)
1414 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1418 const char *tmps = NULL;
1419 Newx(PL_Argv, sp - mark + 1, const char*);
1422 while (++mark <= sp) {
1424 *a++ = SvPV_nolen_const(*mark);
1430 tmps = SvPV_nolen_const(really);
1431 if ((!really && *PL_Argv[0] != '/') ||
1432 (really && *tmps != '/')) /* will execvp use PATH? */
1433 TAINT_ENV(); /* testing IFS here is overkill, probably */
1435 if (really && *tmps)
1436 PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
1438 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1440 S_exec_failed(aTHX_ (really ? tmps : PL_Argv[0]), fd, do_report);
1448 Perl_do_execfree(pTHX)
1457 #ifdef PERL_DEFAULT_DO_EXEC3_IMPLEMENTATION
1460 Perl_do_exec3(pTHX_ const char *incmd, int fd, int do_report)
1467 /* Make a copy so we can change it */
1468 const Size_t cmdlen = strlen(incmd) + 1;
1470 PERL_ARGS_ASSERT_DO_EXEC3;
1472 Newx(buf, cmdlen, char);
1474 memcpy(cmd, incmd, cmdlen);
1476 while (*cmd && isSPACE(*cmd))
1479 /* save an extra exec if possible */
1483 char flags[PERL_FLAGS_MAX];
1484 if (strnEQ(cmd,PL_cshname,PL_cshlen) &&
1485 strnEQ(cmd+PL_cshlen," -c",3)) {
1486 my_strlcpy(flags, "-c", PERL_FLAGS_MAX);
1487 s = cmd+PL_cshlen+3;
1490 my_strlcat(flags, "f", PERL_FLAGS_MAX - 2);
1495 char * const ncmd = s;
1501 if (s[-1] == '\'') {
1504 PerlProc_execl(PL_cshname, "csh", flags, ncmd, (char*)NULL);
1507 S_exec_failed(aTHX_ PL_cshname, fd, do_report);
1516 /* see if there are shell metacharacters in it */
1518 if (*cmd == '.' && isSPACE(cmd[1]))
1521 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1525 while (isWORDCHAR(*s))
1526 s++; /* catch VAR=val gizmo */
1530 for (s = cmd; *s; s++) {
1531 if (*s != ' ' && !isALPHA(*s) &&
1532 strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1533 if (*s == '\n' && !s[1]) {
1537 /* handle the 2>&1 construct at the end */
1538 if (*s == '>' && s[1] == '&' && s[2] == '1'
1539 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1540 && (!s[3] || isSPACE(s[3])))
1542 const char *t = s + 3;
1544 while (*t && isSPACE(*t))
1546 if (!*t && (PerlLIO_dup2(1,2) != -1)) {
1553 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char *)NULL);
1555 S_exec_failed(aTHX_ PL_sh_path, fd, do_report);
1561 Newx(PL_Argv, (s - cmd) / 2 + 2, const char*);
1562 PL_Cmd = savepvn(cmd, s-cmd);
1564 for (s = PL_Cmd; *s;) {
1569 while (*s && !isSPACE(*s))
1577 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1579 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1583 S_exec_failed(aTHX_ PL_Argv[0], fd, do_report);
1590 #endif /* OS2 || WIN32 */
1593 #include <starlet.h> /* for sys$delprc */
1597 Perl_apply(pTHX_ I32 type, SV **mark, SV **sp)
1602 const char *const what = PL_op_name[type];
1605 SV ** const oldmark = mark;
1606 bool killgp = FALSE;
1608 PERL_ARGS_ASSERT_APPLY;
1610 PERL_UNUSED_VAR(what); /* may not be used depending on compile options */
1612 /* Doing this ahead of the switch statement preserves the old behaviour,
1613 where attempting to use kill as a taint test test would fail on
1614 platforms where kill was not defined. */
1616 if (type == OP_KILL)
1617 Perl_die(aTHX_ PL_no_func, what);
1620 if (type == OP_CHOWN)
1621 Perl_die(aTHX_ PL_no_func, what);
1625 #define APPLY_TAINT_PROPER() \
1627 if (TAINT_get) { TAINT_PROPER(what); } \
1630 /* This is a first heuristic; it doesn't catch tainting magic. */
1632 while (++mark <= sp) {
1633 if (SvTAINTED(*mark)) {
1642 APPLY_TAINT_PROPER();
1645 APPLY_TAINT_PROPER();
1647 while (++mark <= sp) {
1649 if ((gv = MAYBE_DEREF_GV(*mark))) {
1650 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1652 APPLY_TAINT_PROPER();
1653 if (fchmod(PerlIO_fileno(IoIFP(GvIOn(gv))), val))
1656 Perl_die(aTHX_ PL_no_func, "fchmod");
1664 const char *name = SvPV_nomg_const_nolen(*mark);
1665 APPLY_TAINT_PROPER();
1666 if (!IS_SAFE_PATHNAME(*mark, "chmod") ||
1667 PerlLIO_chmod(name, val)) {
1676 APPLY_TAINT_PROPER();
1677 if (sp - mark > 2) {
1679 val = SvIVx(*++mark);
1680 val2 = SvIVx(*++mark);
1681 APPLY_TAINT_PROPER();
1683 while (++mark <= sp) {
1685 if ((gv = MAYBE_DEREF_GV(*mark))) {
1686 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1688 APPLY_TAINT_PROPER();
1689 if (fchown(PerlIO_fileno(IoIFP(GvIOn(gv))), val, val2))
1692 Perl_die(aTHX_ PL_no_func, "fchown");
1700 const char *name = SvPV_nomg_const_nolen(*mark);
1701 APPLY_TAINT_PROPER();
1702 if (!IS_SAFE_PATHNAME(*mark, "chown") ||
1703 PerlLIO_chown(name, val, val2)) {
1712 XXX Should we make lchown() directly available from perl?
1713 For now, we'll let Configure test for HAS_LCHOWN, but do
1714 nothing in the core.
1719 APPLY_TAINT_PROPER();
1722 s = SvPVx_const(*++mark, len);
1723 if (*s == '-' && isALPHA(s[1]))
1730 if (*s == 'S' && s[1] == 'I' && s[2] == 'G') {
1734 if ((val = whichsig_pvn(s, len)) < 0)
1735 Perl_croak(aTHX_ "Unrecognized signal name \"%"SVf"\"", SVfARG(*mark));
1746 APPLY_TAINT_PROPER();
1749 /* kill() doesn't do process groups (job trees?) under VMS */
1750 if (val == SIGKILL) {
1751 /* Use native sys$delprc() to insure that target process is
1752 * deleted; supervisor-mode images don't pay attention to
1753 * CRTL's emulation of Unix-style signals and kill()
1755 while (++mark <= sp) {
1757 unsigned long int __vmssts;
1759 if (!(SvIOK(*mark) || SvNOK(*mark) || looks_like_number(*mark)))
1760 Perl_croak(aTHX_ "Can't kill a non-numeric process ID");
1761 proc = SvIV_nomg(*mark);
1762 APPLY_TAINT_PROPER();
1763 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1767 case SS$_NOSUCHNODE:
1768 SETERRNO(ESRCH,__vmssts);
1771 SETERRNO(EPERM,__vmssts);
1774 SETERRNO(EVMSERR,__vmssts);
1782 while (++mark <= sp) {
1785 if (!(SvNIOK(*mark) || looks_like_number(*mark)))
1786 Perl_croak(aTHX_ "Can't kill a non-numeric process ID");
1787 proc = SvIV_nomg(*mark);
1792 APPLY_TAINT_PROPER();
1793 if (PerlProc_kill(proc, val))
1800 APPLY_TAINT_PROPER();
1802 while (++mark <= sp) {
1803 s = SvPV_nolen_const(*mark);
1804 APPLY_TAINT_PROPER();
1805 if (!IS_SAFE_PATHNAME(*mark, "unlink")) {
1808 else if (PerlProc_geteuid() || PL_unsafe) {
1812 else { /* don't let root wipe out directories without -U */
1813 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1822 #if defined(HAS_UTIME) || defined(HAS_FUTIMES)
1824 APPLY_TAINT_PROPER();
1825 if (sp - mark > 2) {
1826 #if defined(HAS_FUTIMES)
1827 struct timeval utbuf[2];
1828 void *utbufp = utbuf;
1829 #elif defined(I_UTIME) || defined(VMS)
1830 struct utimbuf utbuf;
1831 struct utimbuf *utbufp = &utbuf;
1837 void *utbufp = &utbuf;
1840 SV* const accessed = *++mark;
1841 SV* const modified = *++mark;
1843 /* Be like C, and if both times are undefined, let the C
1844 * library figure out what to do. This usually means
1845 * "current time". */
1847 if ( accessed == &PL_sv_undef && modified == &PL_sv_undef )
1850 Zero(&utbuf, sizeof utbuf, char);
1852 utbuf[0].tv_sec = (long)SvIV(accessed); /* time accessed */
1853 utbuf[0].tv_usec = 0;
1854 utbuf[1].tv_sec = (long)SvIV(modified); /* time modified */
1855 utbuf[1].tv_usec = 0;
1856 #elif defined(BIG_TIME)
1857 utbuf.actime = (Time_t)SvNV(accessed); /* time accessed */
1858 utbuf.modtime = (Time_t)SvNV(modified); /* time modified */
1860 utbuf.actime = (Time_t)SvIV(accessed); /* time accessed */
1861 utbuf.modtime = (Time_t)SvIV(modified); /* time modified */
1864 APPLY_TAINT_PROPER();
1866 while (++mark <= sp) {
1868 if ((gv = MAYBE_DEREF_GV(*mark))) {
1869 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1871 APPLY_TAINT_PROPER();
1872 if (futimes(PerlIO_fileno(IoIFP(GvIOn(gv))),
1873 (struct timeval *) utbufp))
1876 Perl_die(aTHX_ PL_no_func, "futimes");
1884 const char * const name = SvPV_nomg_const_nolen(*mark);
1885 APPLY_TAINT_PROPER();
1886 if (!IS_SAFE_PATHNAME(*mark, "utime")) {
1891 if (utimes(name, (struct timeval *)utbufp))
1893 if (PerlLIO_utime(name, utbufp))
1907 #undef APPLY_TAINT_PROPER
1910 /* Do the permissions allow some operation? Assumes statcache already set. */
1911 #ifndef VMS /* VMS' cando is in vms.c */
1913 Perl_cando(pTHX_ Mode_t mode, bool effective, const Stat_t *statbufp)
1914 /* effective is a flag, true for EUID, or for checking if the effective gid
1915 * is in the list of groups returned from getgroups().
1920 PERL_ARGS_ASSERT_CANDO;
1923 /* [Comments and code from Len Reed]
1924 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1925 * to write-protected files. The execute permission bit is set
1926 * by the Microsoft C library stat() function for the following:
1931 * All files and directories are readable.
1932 * Directories and special files, e.g. "CON", cannot be
1934 * [Comment by Tom Dinger -- a directory can have the write-protect
1935 * bit set in the file system, but DOS permits changes to
1936 * the directory anyway. In addition, all bets are off
1937 * here for networked software, such as Novell and
1941 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1942 * too so it will actually look into the files for magic numbers
1944 return (mode & statbufp->st_mode) ? TRUE : FALSE;
1946 #else /* ! DOSISH */
1948 if (ingroup(544,effective)) { /* member of Administrators */
1950 if ((effective ? PerlProc_geteuid() : PerlProc_getuid()) == 0) { /* root is special */
1952 if (mode == S_IXUSR) {
1953 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1957 return TRUE; /* root reads and writes anything */
1960 if (statbufp->st_uid == (effective ? PerlProc_geteuid() : PerlProc_getuid()) ) {
1961 if (statbufp->st_mode & mode)
1962 return TRUE; /* ok as "user" */
1964 else if (ingroup(statbufp->st_gid,effective)) {
1965 if (statbufp->st_mode & mode >> 3)
1966 return TRUE; /* ok as "group" */
1968 else if (statbufp->st_mode & mode >> 6)
1969 return TRUE; /* ok as "other" */
1971 #endif /* ! DOSISH */
1976 S_ingroup(pTHX_ Gid_t testgid, bool effective)
1979 if (testgid == (effective ? PerlProc_getegid() : PerlProc_getgid()))
1981 #ifdef HAS_GETGROUPS
1983 Groups_t *gary = NULL;
1987 anum = getgroups(0, gary);
1988 Newx(gary, anum, Groups_t);
1989 anum = getgroups(anum, gary);
1991 if (gary[anum] == testgid) {
2004 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
2007 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
2010 const key_t key = (key_t)SvNVx(*++mark);
2011 SV *nsv = optype == OP_MSGGET ? NULL : *++mark;
2012 const I32 flags = SvIVx(*++mark);
2014 PERL_ARGS_ASSERT_DO_IPCGET;
2015 PERL_UNUSED_ARG(sp);
2022 return msgget(key, flags);
2026 return semget(key, (int) SvIV(nsv), flags);
2030 return shmget(key, (size_t) SvUV(nsv), flags);
2032 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
2034 /* diag_listed_as: msg%s not implemented */
2035 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2038 return -1; /* should never happen */
2042 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
2047 const I32 id = SvIVx(*++mark);
2049 const I32 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
2051 const I32 cmd = SvIVx(*++mark);
2052 SV * const astr = *++mark;
2053 STRLEN infosize = 0;
2054 I32 getinfo = (cmd == IPC_STAT);
2056 PERL_ARGS_ASSERT_DO_IPCCTL;
2057 PERL_UNUSED_ARG(sp);
2063 if (cmd == IPC_STAT || cmd == IPC_SET)
2064 infosize = sizeof(struct msqid_ds);
2069 if (cmd == IPC_STAT || cmd == IPC_SET)
2070 infosize = sizeof(struct shmid_ds);
2076 if (cmd == IPC_STAT || cmd == IPC_SET)
2077 infosize = sizeof(struct semid_ds);
2078 else if (cmd == GETALL || cmd == SETALL)
2080 struct semid_ds semds;
2082 #ifdef EXTRA_F_IN_SEMUN_BUF
2083 semun.buff = &semds;
2087 getinfo = (cmd == GETALL);
2088 if (Semctl(id, 0, IPC_STAT, semun) == -1)
2090 infosize = semds.sem_nsems * sizeof(short);
2091 /* "short" is technically wrong but much more portable
2092 than guessing about u_?short(_t)? */
2095 /* diag_listed_as: sem%s not implemented */
2096 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2100 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
2102 /* diag_listed_as: shm%s not implemented */
2103 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2111 SvPV_force_nolen(astr);
2112 a = SvGROW(astr, infosize+1);
2117 a = SvPV(astr, len);
2118 if (len != infosize)
2119 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
2127 const IV i = SvIV(astr);
2128 a = INT2PTR(char *,i); /* ouch */
2135 ret = msgctl(id, cmd, (struct msqid_ds *)a);
2141 union semun unsemds;
2143 #ifdef EXTRA_F_IN_SEMUN_BUF
2144 unsemds.buff = (struct semid_ds *)a;
2146 unsemds.buf = (struct semid_ds *)a;
2148 ret = Semctl(id, n, cmd, unsemds);
2150 /* diag_listed_as: sem%s not implemented */
2151 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2158 ret = shmctl(id, cmd, (struct shmid_ds *)a);
2162 if (getinfo && ret >= 0) {
2163 SvCUR_set(astr, infosize);
2164 *SvEND(astr) = '\0';
2171 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
2176 const I32 id = SvIVx(*++mark);
2177 SV * const mstr = *++mark;
2178 const I32 flags = SvIVx(*++mark);
2179 const char * const mbuf = SvPV_const(mstr, len);
2180 const I32 msize = len - sizeof(long);
2182 PERL_ARGS_ASSERT_DO_MSGSND;
2183 PERL_UNUSED_ARG(sp);
2186 Perl_croak(aTHX_ "Arg too short for msgsnd");
2188 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
2190 PERL_UNUSED_ARG(sp);
2191 PERL_UNUSED_ARG(mark);
2192 /* diag_listed_as: msg%s not implemented */
2193 Perl_croak(aTHX_ "msgsnd not implemented");
2199 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
2205 I32 msize, flags, ret;
2206 const I32 id = SvIVx(*++mark);
2207 SV * const mstr = *++mark;
2209 PERL_ARGS_ASSERT_DO_MSGRCV;
2210 PERL_UNUSED_ARG(sp);
2212 /* suppress warning when reading into undef var --jhi */
2214 sv_setpvs(mstr, "");
2215 msize = SvIVx(*++mark);
2216 mtype = (long)SvIVx(*++mark);
2217 flags = SvIVx(*++mark);
2218 SvPV_force_nolen(mstr);
2219 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
2222 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
2224 SvCUR_set(mstr, sizeof(long)+ret);
2225 *SvEND(mstr) = '\0';
2226 #ifndef INCOMPLETE_TAINTS
2227 /* who knows who has been playing with this message? */
2233 PERL_UNUSED_ARG(sp);
2234 PERL_UNUSED_ARG(mark);
2235 /* diag_listed_as: msg%s not implemented */
2236 Perl_croak(aTHX_ "msgrcv not implemented");
2242 Perl_do_semop(pTHX_ SV **mark, SV **sp)
2247 const I32 id = SvIVx(*++mark);
2248 SV * const opstr = *++mark;
2249 const char * const opbuf = SvPV_const(opstr, opsize);
2251 PERL_ARGS_ASSERT_DO_SEMOP;
2252 PERL_UNUSED_ARG(sp);
2254 if (opsize < 3 * SHORTSIZE
2255 || (opsize % (3 * SHORTSIZE))) {
2256 SETERRNO(EINVAL,LIB_INVARG);
2260 /* We can't assume that sizeof(struct sembuf) == 3 * sizeof(short). */
2262 const int nsops = opsize / (3 * sizeof (short));
2264 short * const ops = (short *) opbuf;
2266 struct sembuf *temps, *t;
2269 Newx (temps, nsops, struct sembuf);
2277 result = semop(id, temps, nsops);
2291 /* diag_listed_as: sem%s not implemented */
2292 Perl_croak(aTHX_ "semop not implemented");
2297 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
2302 struct shmid_ds shmds;
2303 const I32 id = SvIVx(*++mark);
2304 SV * const mstr = *++mark;
2305 const I32 mpos = SvIVx(*++mark);
2306 const I32 msize = SvIVx(*++mark);
2308 PERL_ARGS_ASSERT_DO_SHMIO;
2309 PERL_UNUSED_ARG(sp);
2312 if (shmctl(id, IPC_STAT, &shmds) == -1)
2314 if (mpos < 0 || msize < 0
2315 || (size_t)mpos + msize > (size_t)shmds.shm_segsz) {
2316 SETERRNO(EFAULT,SS_ACCVIO); /* can't do as caller requested */
2319 shm = (char *)shmat(id, NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
2320 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2322 if (optype == OP_SHMREAD) {
2324 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2326 SvUPGRADE(mstr, SVt_PV);
2328 sv_setpvs(mstr, "");
2330 mbuf = SvGROW(mstr, (STRLEN)msize+1);
2332 Copy(shm + mpos, mbuf, msize, char);
2333 SvCUR_set(mstr, msize);
2334 *SvEND(mstr) = '\0';
2336 #ifndef INCOMPLETE_TAINTS
2337 /* who knows who has been playing with this shared memory? */
2344 const char *mbuf = SvPV_const(mstr, len);
2345 const I32 n = ((I32)len > msize) ? msize : (I32)len;
2346 Copy(mbuf, shm + mpos, n, char);
2348 memzero(shm + mpos + n, msize - n);
2352 /* diag_listed_as: shm%s not implemented */
2353 Perl_croak(aTHX_ "shm I/O not implemented");
2358 #endif /* SYSV IPC */
2363 =for apidoc start_glob
2365 Function called by C<do_readline> to spawn a glob (or do the glob inside
2366 perl on VMS). This code used to be inline, but now perl uses C<File::Glob>
2367 this glob starter is only used by miniperl during the build process.
2368 Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up.
2374 Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
2377 SV * const tmpcmd = newSV(0);
2380 PERL_ARGS_ASSERT_START_GLOB;
2382 if (!IS_SAFE_SYSCALL(tmpglob, "pattern", "glob"))
2387 #ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
2388 /* since spawning off a process is a real performance hit */
2395 fp = Perl_vms_start_glob(aTHX_ tmpglob, io);
2400 sv_setpv(tmpcmd, "for a in ");
2401 sv_catsv(tmpcmd, tmpglob);
2402 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
2405 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
2406 sv_catsv(tmpcmd, tmpglob);
2408 sv_setpv(tmpcmd, "perlglob ");
2409 sv_catsv(tmpcmd, tmpglob);
2410 sv_catpv(tmpcmd, " |");
2415 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
2416 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
2417 sv_catsv(tmpcmd, tmpglob);
2418 sv_catpv(tmpcmd, "' 2>/dev/null |");
2420 sv_setpv(tmpcmd, "echo ");
2421 sv_catsv(tmpcmd, tmpglob);
2423 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
2425 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
2428 #endif /* !DOSISH */
2430 GV * const envgv = gv_fetchpvs("ENV", 0, SVt_PVHV);
2431 SV ** const home = hv_fetchs(GvHV(envgv), "HOME", 0);
2432 SV ** const path = hv_fetchs(GvHV(envgv), "PATH", 0);
2433 if (home && *home) SvGETMAGIC(*home);
2434 if (path && *path) SvGETMAGIC(*path);
2435 save_hash(gv_fetchpvs("ENV", 0, SVt_PVHV));
2436 if (home && *home) SvSETMAGIC(*home);
2437 if (path && *path) SvSETMAGIC(*path);
2439 (void)do_open(PL_last_in_gv, (char*)SvPVX_const(tmpcmd), SvCUR(tmpcmd),
2440 FALSE, O_RDONLY, 0, NULL);
2449 * c-indentation-style: bsd
2451 * indent-tabs-mode: nil
2454 * ex: set ts=8 sts=4 sw=4 et: