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 S_openn_setup(pTHX_ GV *gv, char *mode, PerlIO **saveifp, PerlIO **saveofp,
65 int *savefd, char *savetype)
67 IO * const io = GvIOn(gv);
69 PERL_ARGS_ASSERT_OPENN_SETUP;
74 *savetype = IoTYPE_CLOSED;
76 Zero(mode,sizeof(mode),char);
77 PL_forkprocess = 1; /* assume true if no fork */
79 /* If currently open - close before we re-open */
81 if (IoTYPE(io) == IoTYPE_STD) {
82 /* This is a clone of one of STD* handles */
85 const int old_fd = PerlIO_fileno(IoIFP(io));
87 if (old_fd >= 0 && old_fd <= PL_maxsysfd) {
88 /* This is one of the original STD* handles */
91 *savetype = IoTYPE(io);
97 if (IoTYPE(io) == IoTYPE_PIPE)
98 result = PerlProc_pclose(IoIFP(io));
99 else if (IoIFP(io) != IoOFP(io)) {
101 result = PerlIO_close(IoOFP(io));
102 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
105 result = PerlIO_close(IoIFP(io));
108 result = PerlIO_close(IoIFP(io));
110 if (result == EOF && old_fd > PL_maxsysfd) {
111 /* Why is this not Perl_warn*() call ? */
112 PerlIO_printf(Perl_error_log,
113 "Warning: unable to close filehandle %"HEKf" properly.\n",
114 HEKfARG(GvENAME_HEK(gv))
119 IoOFP(io) = IoIFP(io) = NULL;
125 Perl_do_openn(pTHX_ GV *gv, const char *oname, I32 len, int as_raw,
126 int rawmode, int rawperm, PerlIO *supplied_fp, SV **svp,
129 PERL_ARGS_ASSERT_DO_OPENN;
132 /* sysopen style args, i.e. integer mode and permissions */
135 Perl_croak(aTHX_ "panic: sysopen with multiple args, num_svs=%ld",
138 return do_open_raw(gv, oname, len, rawmode, rawperm);
140 return do_open6(gv, oname, len, supplied_fp, svp, num_svs);
144 Perl_do_open_raw(pTHX_ GV *gv, const char *oname, STRLEN len,
145 int rawmode, int rawperm)
151 char mode[PERL_MODE_MAX]; /* file mode ("r\0", "rb\0", "ab\0" etc.) */
152 IO * const io = openn_setup(gv, mode, &saveifp, &saveofp, &savefd, &savetype);
156 PERL_ARGS_ASSERT_DO_OPEN_RAW;
158 /* For ease of blame back to 5.000, keep the existing indenting. */
160 /* sysopen style args, i.e. integer mode and permissions */
162 const int appendtrunc =
164 #ifdef O_APPEND /* Not fully portable. */
167 #ifdef O_TRUNC /* Not fully portable. */
171 const int modifyingmode = O_WRONLY|O_RDWR|O_CREAT|appendtrunc;
181 It might be (in OS/390 and Mac OS Classic it is)
187 This means that simple & with O_RDWR would look
188 like O_RDONLY is present. Therefore we have to
191 if ((ismodifying = (rawmode & modifyingmode))) {
192 if ((ismodifying & O_WRONLY) == O_WRONLY ||
193 (ismodifying & O_RDWR) == O_RDWR ||
194 (ismodifying & (O_CREAT|appendtrunc)))
195 TAINT_PROPER("sysopen");
197 mode[ix++] = IoTYPE_NUMERIC; /* Marker to openn to use numeric "sysopen" */
199 #if defined(USE_64_BIT_RAWIO) && defined(O_LARGEFILE)
200 rawmode |= O_LARGEFILE; /* Transparently largefiley. */
203 IoTYPE(io) = PerlIO_intmode2str(rawmode, &mode[ix], &writing);
205 namesv = newSVpvn_flags(oname, len, SVs_TEMP);
206 fp = PerlIO_openn(aTHX_ NULL, mode, -1, rawmode, rawperm, NULL, 1, &namesv);
208 return openn_cleanup(gv, io, fp, mode, oname, saveifp, saveofp, savefd,
209 savetype, writing, 0, NULL);
213 Perl_do_open6(pTHX_ GV *gv, const char *oname, STRLEN len,
214 PerlIO *supplied_fp, SV **svp, U32 num_svs)
220 char mode[PERL_MODE_MAX]; /* file mode ("r\0", "rb\0", "ab\0" etc.) */
221 IO * const io = openn_setup(gv, mode, &saveifp, &saveofp, &savefd, &savetype);
224 bool was_fdopen = FALSE;
227 PERL_ARGS_ASSERT_DO_OPEN6;
229 /* For ease of blame back to 5.000, keep the existing indenting. */
231 /* Regular (non-sys) open */
236 bool in_raw = 0, in_crlf = 0, out_raw = 0, out_crlf = 0;
238 /* Collect default raw/crlf info from the op */
239 if (PL_op && PL_op->op_type == OP_OPEN) {
240 /* set up IO layers */
241 const U8 flags = PL_op->op_private;
242 in_raw = (flags & OPpOPEN_IN_RAW);
243 in_crlf = (flags & OPpOPEN_IN_CRLF);
244 out_raw = (flags & OPpOPEN_OUT_RAW);
245 out_crlf = (flags & OPpOPEN_OUT_CRLF);
248 type = savepvn(oname, len);
252 /* Lose leading and trailing white space */
253 while (isSPACE(*type))
255 while (tend > type && isSPACE(tend[-1]))
261 /* New style explicit name, type is just mode and layer info */
263 if (SvROK(*svp) && !strchr(oname,'&')) {
265 Perl_warner(aTHX_ packWARN(WARN_IO),
266 "Can't open a reference");
267 SETERRNO(EINVAL, LIB_INVARG);
271 #endif /* USE_STDIO */
272 p = (SvOK(*svp) || SvGMAGICAL(*svp)) ? SvPV(*svp, nlen) : NULL;
274 if (p && !IS_SAFE_PATHNAME(p, nlen, "open")) {
279 name = p ? savepvn(p, nlen) : savepvs("");
288 if ((*type == IoTYPE_RDWR) && /* scary */
289 (*(type+1) == IoTYPE_RDONLY || *(type+1) == IoTYPE_WRONLY) &&
290 ((!num_svs || (tend > type+1 && tend[-1] != IoTYPE_PIPE)))) {
291 TAINT_PROPER("open");
296 if (*type == IoTYPE_PIPE) {
298 if (type[1] != IoTYPE_STD) {
300 Perl_croak(aTHX_ "Unknown open() mode '%.*s'", (int)olen, oname);
306 } while (isSPACE(*type));
312 /* command is missing 19990114 */
313 if (ckWARN(WARN_PIPE))
314 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open");
319 if (!(*name == '-' && name[1] == '\0') || num_svs)
321 TAINT_PROPER("piped open");
322 if (!num_svs && name[len-1] == '|') {
324 if (ckWARN(WARN_PIPE))
325 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Can't open bidirectional pipe");
334 fp = PerlProc_popen_list(mode, num_svs, svp);
337 fp = PerlProc_popen(name,mode);
341 if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) {
348 else if (*type == IoTYPE_WRONLY) {
349 TAINT_PROPER("open");
351 if (*type == IoTYPE_WRONLY) {
352 /* Two IoTYPE_WRONLYs in a row make for an IoTYPE_APPEND. */
353 mode[0] = IoTYPE(io) = IoTYPE_APPEND;
367 dodup = PERLIO_DUP_FD;
373 if (!num_svs && !*type && supplied_fp) {
374 /* "<+&" etc. is used by typemaps */
378 PerlIO *that_fp = NULL;
382 /* diag_listed_as: More than one argument to '%s' open */
383 Perl_croak(aTHX_ "More than one argument to '%c&' open",IoTYPE(io));
385 while (isSPACE(*type))
389 || (SvPOKp(*svp) && looks_like_number(*svp))
391 wanted_fd = SvUV(*svp);
394 else if (isDIGIT(*type)
395 && grok_atoUV(type, &uv, NULL)
403 thatio = sv_2io(*svp);
406 GV * const thatgv = gv_fetchpvn_flags(type, tend - type,
408 thatio = GvIO(thatgv);
412 SETERRNO(EINVAL,SS_IVCHAN);
417 if ((that_fp = IoIFP(thatio))) {
418 /* Flush stdio buffer before dup. --mjd
419 * Unfortunately SEEK_CURing 0 seems to
420 * be optimized away on most platforms;
421 * only Solaris and Linux seem to flush
423 /* On the other hand, do all platforms
424 * take gracefully to flushing a read-only
425 * filehandle? Perhaps we should do
426 * fsetpos(src)+fgetpos(dst)? --nik */
427 PerlIO_flush(that_fp);
428 wanted_fd = PerlIO_fileno(that_fp);
429 /* When dup()ing STDIN, STDOUT or STDERR
430 * explicitly set appropriate access mode */
431 if (that_fp == PerlIO_stdout()
432 || that_fp == PerlIO_stderr())
433 IoTYPE(io) = IoTYPE_WRONLY;
434 else if (that_fp == PerlIO_stdin())
435 IoTYPE(io) = IoTYPE_RDONLY;
436 /* When dup()ing a socket, say result is
438 else if (IoTYPE(thatio) == IoTYPE_SOCKET)
439 IoTYPE(io) = IoTYPE_SOCKET;
447 fp = PerlIO_fdupopen(aTHX_ that_fp, NULL, dodup);
451 wanted_fd = PerlLIO_dup(wanted_fd);
454 if (!(fp = PerlIO_openn(aTHX_ type,mode,wanted_fd,0,0,NULL,num_svs,svp))) {
455 if (dodup && wanted_fd >= 0)
456 PerlLIO_close(wanted_fd);
462 while (isSPACE(*type))
464 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
466 fp = PerlIO_stdout();
467 IoTYPE(io) = IoTYPE_STD;
469 /* diag_listed_as: More than one argument to '%s' open */
470 Perl_croak(aTHX_ "More than one argument to '>%c' open",IoTYPE_STD);
475 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
478 SV *namesv = newSVpvn_flags(type, tend - type, SVs_TEMP);
480 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,1,&namesv);
484 if (!fp && type && *type && *type != ':' && !isIDFIRST(*type))
485 goto unknown_open_mode;
486 } /* IoTYPE_WRONLY */
487 else if (*type == IoTYPE_RDONLY) {
490 } while (isSPACE(*type));
499 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
502 IoTYPE(io) = IoTYPE_STD;
504 /* diag_listed_as: More than one argument to '%s' open */
505 Perl_croak(aTHX_ "More than one argument to '<%c' open",IoTYPE_STD);
510 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
513 SV *namesv = newSVpvn_flags(type, tend - type, SVs_TEMP);
515 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,1,&namesv);
518 if (!fp && type && *type && *type != ':' && !isIDFIRST(*type))
519 goto unknown_open_mode;
520 } /* IoTYPE_RDONLY */
521 else if ((num_svs && /* '-|...' or '...|' */
522 type[0] == IoTYPE_STD && type[1] == IoTYPE_PIPE) ||
523 (!num_svs && tend > type+1 && tend[-1] == IoTYPE_PIPE)) {
525 type += 2; /* skip over '-|' */
529 while (tend > type && isSPACE(tend[-1]))
531 for (; isSPACE(*type); type++)
537 /* command is missing 19990114 */
538 if (ckWARN(WARN_PIPE))
539 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open");
544 if (!(*name == '-' && name[1] == '\0') || num_svs)
546 TAINT_PROPER("piped open");
555 fp = PerlProc_popen_list(mode,num_svs,svp);
558 fp = PerlProc_popen(name,mode);
560 IoTYPE(io) = IoTYPE_PIPE;
562 while (isSPACE(*type))
565 if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) {
572 else { /* layer(Args) */
574 goto unknown_open_mode;
576 IoTYPE(io) = IoTYPE_RDONLY;
577 for (; isSPACE(*name); name++)
586 if (*name == '-' && name[1] == '\0') {
588 IoTYPE(io) = IoTYPE_STD;
592 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
595 SV *namesv = newSVpvn_flags(type, tend - type, SVs_TEMP);
597 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,1,&namesv);
604 return openn_cleanup(gv, io, fp, mode, oname, saveifp, saveofp, savefd,
605 savetype, writing, was_fdopen, type);
608 /* Yes, this is ugly, but it's private, and I don't see a cleaner way to
609 simplify the two-headed public interface of do_openn. */
611 S_openn_cleanup(pTHX_ GV *gv, IO *io, PerlIO *fp, char *mode, const char *oname,
612 PerlIO *saveifp, PerlIO *saveofp, int savefd, char savetype,
613 int writing, bool was_fdopen, const char *type)
617 PERL_ARGS_ASSERT_OPENN_CLEANUP;
620 if (IoTYPE(io) == IoTYPE_RDONLY && ckWARN(WARN_NEWLINE)
621 && should_warn_nl(oname)
625 GCC_DIAG_IGNORE(-Wformat-nonliteral); /* PL_warn_nl is constant */
626 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
632 if (ckWARN(WARN_IO)) {
633 if ((IoTYPE(io) == IoTYPE_RDONLY) &&
634 (fp == PerlIO_stdout() || fp == PerlIO_stderr())) {
635 Perl_warner(aTHX_ packWARN(WARN_IO),
636 "Filehandle STD%s reopened as %"HEKf
638 ((fp == PerlIO_stdout()) ? "OUT" : "ERR"),
639 HEKfARG(GvENAME_HEK(gv)));
641 else if ((IoTYPE(io) == IoTYPE_WRONLY) && fp == PerlIO_stdin()) {
642 Perl_warner(aTHX_ packWARN(WARN_IO),
643 "Filehandle STDIN reopened as %"HEKf" only for output",
644 HEKfARG(GvENAME_HEK(gv))
649 fd = PerlIO_fileno(fp);
650 /* Do NOT do: "if (fd < 0) goto say_false;" here. If there is no
651 * fd assume it isn't a socket - this covers PerlIO::scalar -
652 * otherwise unless we "know" the type probe for socket-ness.
654 if (IoTYPE(io) && IoTYPE(io) != IoTYPE_PIPE && IoTYPE(io) != IoTYPE_STD && fd >= 0) {
655 if (PerlLIO_fstat(fd,&PL_statbuf) < 0) {
656 /* If PerlIO claims to have fd we had better be able to fstat() it. */
657 (void) PerlIO_close(fp);
661 if (S_ISSOCK(PL_statbuf.st_mode))
662 IoTYPE(io) = IoTYPE_SOCKET; /* in case a socket was passed in to us */
665 !(PL_statbuf.st_mode & S_IFMT)
666 && IoTYPE(io) != IoTYPE_WRONLY /* Dups of STD* filehandles already have */
667 && IoTYPE(io) != IoTYPE_RDONLY /* type so they aren't marked as sockets */
668 ) { /* on OS's that return 0 on fstat()ed pipe */
670 Sock_size_t buflen = sizeof tmpbuf;
671 if (PerlSock_getsockname(fd, (struct sockaddr *)tmpbuf, &buflen) >= 0
672 || errno != ENOTSOCK)
673 IoTYPE(io) = IoTYPE_SOCKET; /* some OS's return 0 on fstat()ed socket */
674 /* but some return 0 for streams too, sigh */
676 #endif /* HAS_SOCKET */
677 #endif /* !PERL_MICRO */
681 * If this is a standard handle we discard all the layer stuff
682 * and just dup the fd into whatever was on the handle before !
685 if (saveifp) { /* must use old fp? */
686 /* If fd is less that PL_maxsysfd i.e. STDIN..STDERR
687 then dup the new fileno down
690 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
691 if (saveofp != saveifp) { /* was a socket? */
692 PerlIO_close(saveofp);
696 /* Still a small can-of-worms here if (say) PerlIO::scalar
697 is assigned to (say) STDOUT - for now let dup2() fail
698 and provide the error
701 SETERRNO(EBADF,RMS_IFI);
703 } else if (PerlLIO_dup2(fd, savefd) < 0) {
704 (void)PerlIO_close(fp);
708 if (savefd != PerlIO_fileno(PerlIO_stdin())) {
709 char newname[FILENAME_MAX+1];
710 if (PerlIO_getname(fp, newname)) {
711 if (fd == PerlIO_fileno(PerlIO_stdout()))
712 vmssetuserlnm("SYS$OUTPUT", newname);
713 if (fd == PerlIO_fileno(PerlIO_stderr()))
714 vmssetuserlnm("SYS$ERROR", newname);
720 /* PL_fdpid isn't used on Windows, so avoid this useless work.
721 * XXX Probably the same for a lot of other places. */
726 sv = *av_fetch(PL_fdpid,fd,TRUE);
727 SvUPGRADE(sv, SVt_IV);
730 sv = *av_fetch(PL_fdpid,savefd,TRUE);
731 SvUPGRADE(sv, SVt_IV);
737 /* need to close fp without closing underlying fd */
738 int ofd = PerlIO_fileno(fp);
739 int dupfd = ofd >= 0 ? PerlLIO_dup(ofd) : -1;
740 #if defined(HAS_FCNTL) && defined(F_SETFD)
741 /* Assume if we have F_SETFD we have F_GETFD */
742 int coe = ofd >= 0 ? fcntl(ofd, F_GETFD) : -1;
745 PerlLIO_close(dupfd);
749 if (ofd < 0 || dupfd < 0) {
751 PerlLIO_close(dupfd);
755 PerlLIO_dup2(dupfd, ofd);
756 #if defined(HAS_FCNTL) && defined(F_SETFD)
757 /* The dup trick has lost close-on-exec on ofd */
758 fcntl(ofd,F_SETFD, coe);
760 PerlLIO_close(dupfd);
767 fd = PerlIO_fileno(fp);
769 #if defined(HAS_FCNTL) && defined(F_SETFD)
771 if (fcntl(fd, F_SETFD, fd > PL_maxsysfd) < 0) {
779 IoFLAGS(io) &= ~IOf_NOLINE;
781 if (IoTYPE(io) == IoTYPE_SOCKET
782 || (IoTYPE(io) == IoTYPE_WRONLY && fd >= 0 && S_ISCHR(PL_statbuf.st_mode)) ) {
784 if (*s == IoTYPE_IMPLICIT || *s == IoTYPE_NUMERIC)
787 if (!(IoOFP(io) = PerlIO_openn(aTHX_ type,s,fd,0,0,NULL,0,NULL))) {
800 IoTYPE(io) = savetype;
805 Perl_nextargv(pTHX_ GV *gv, bool nomagicopen)
807 IO * const io = GvIOp(gv);
809 PERL_ARGS_ASSERT_NEXTARGV;
812 PL_argvoutgv = gv_fetchpvs("ARGVOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
813 if (io && (IoFLAGS(io) & (IOf_ARGV|IOf_START)) == (IOf_ARGV|IOf_START)) {
814 IoFLAGS(io) &= ~IOf_START;
817 Perl_av_create_and_push(aTHX_ &PL_argvout_stack,
818 SvREFCNT_inc_simple_NN(PL_defoutgv));
821 if (PL_filemode & (S_ISUID|S_ISGID)) {
822 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
825 (void)fchmod(PL_lastfd,PL_filemode);
827 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
834 while (av_tindex(GvAV(gv)) >= 0) {
836 SV *const sv = av_shift(GvAV(gv));
838 SvTAINTED_off(GvSVn(gv)); /* previous tainting irrelevant */
839 sv_setsv(GvSVn(gv),sv);
840 SvSETMAGIC(GvSV(gv));
841 PL_oldname = SvPVx(GvSV(gv), oldlen);
842 if (LIKELY(!PL_inplace)) {
844 ? do_open6(gv, "<", 1, NULL, &GvSV(gv), 1)
845 : do_open6(gv, PL_oldname, oldlen, NULL, NULL, 0)
847 return IoIFP(GvIOp(gv));
851 /* This very long block ends with return IoIFP(GvIOp(gv));
852 Both this block and the block above fall through on open
853 failure to the warning code, and then the while loop above tries
855 if (do_open_raw(gv, PL_oldname, oldlen, O_RDONLY, 0)) {
856 #ifndef FLEXFILENAMES
863 TAINT_PROPER("inplace open");
864 if (oldlen == 1 && *PL_oldname == '-') {
865 setdefout(gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL,
867 return IoIFP(GvIOp(gv));
869 #ifndef FLEXFILENAMES
870 filedev = PL_statbuf.st_dev;
871 fileino = PL_statbuf.st_ino;
873 PL_filemode = PL_statbuf.st_mode;
874 fileuid = PL_statbuf.st_uid;
875 filegid = PL_statbuf.st_gid;
876 if (!S_ISREG(PL_filemode)) {
877 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
878 "Can't do inplace edit: %s is not a regular file",
883 if (*PL_inplace && strNE(PL_inplace, "*")) {
884 const char *star = strchr(PL_inplace, '*');
886 const char *begin = PL_inplace;
889 sv_catpvn(sv, begin, star - begin);
890 sv_catpvn(sv, PL_oldname, oldlen);
892 } while ((star = strchr(begin, '*')));
897 sv_catpv(sv,PL_inplace);
899 #ifndef FLEXFILENAMES
900 if ((PerlLIO_stat(SvPVX_const(sv),&PL_statbuf) >= 0
901 && PL_statbuf.st_dev == filedev
902 && PL_statbuf.st_ino == fileino)
904 || ((_djstat_fail_bits & _STFAIL_TRUENAME)!=0)
908 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
909 "Can't do inplace edit: %"SVf" would not be unique",
916 #if !defined(DOSISH) && !defined(__CYGWIN__)
917 if (PerlLIO_rename(PL_oldname,SvPVX_const(sv)) < 0) {
918 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
919 "Can't rename %s to %"SVf": %s, skipping file",
920 PL_oldname, SVfARG(sv), Strerror(errno));
926 (void)PerlLIO_unlink(SvPVX_const(sv));
927 (void)PerlLIO_rename(PL_oldname,SvPVX_const(sv));
928 do_open_raw(gv, SvPVX_const(sv), SvCUR(sv), O_RDONLY, 0);
931 (void)UNLINK(SvPVX_const(sv));
932 if (link(PL_oldname,SvPVX_const(sv)) < 0) {
933 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
934 "Can't rename %s to %"SVf": %s, skipping file",
935 PL_oldname, SVfARG(sv), Strerror(errno) );
939 (void)UNLINK(PL_oldname);
943 #if !defined(DOSISH) && !defined(AMIGAOS)
944 # ifndef VMS /* Don't delete; use automatic file versioning */
945 if (UNLINK(PL_oldname) < 0) {
946 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
947 "Can't remove %s: %s, skipping file",
948 PL_oldname, Strerror(errno) );
954 Perl_croak(aTHX_ "Can't do inplace edit without backup");
958 sv_setpvn(sv,PL_oldname,oldlen);
959 SETERRNO(0,0); /* in case sprintf set errno */
960 if (!Perl_do_open_raw(aTHX_ PL_argvoutgv, SvPVX_const(sv),
963 O_WRONLY|O_CREAT|O_TRUNC, 0
965 O_WRONLY|O_CREAT|OPEN_EXCL, 0600
968 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE), "Can't do inplace edit on %s: %s",
969 PL_oldname, Strerror(errno) );
973 setdefout(PL_argvoutgv);
974 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
975 if (PL_lastfd >= 0) {
976 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
978 (void)fchmod(PL_lastfd,PL_filemode);
980 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
982 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
983 /* XXX silently ignore failures */
985 PERL_UNUSED_RESULT(fchown(PL_lastfd,fileuid,filegid));
988 PERL_UNUSED_RESULT(PerlLIO_chown(PL_oldname,fileuid,filegid));
993 return IoIFP(GvIOp(gv));
995 } /* successful do_open_raw(), PL_inplace non-NULL */
997 if (ckWARN_d(WARN_INPLACE)) {
998 const int eno = errno;
999 if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0
1000 && !S_ISREG(PL_statbuf.st_mode)) {
1001 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
1002 "Can't do inplace edit: %s is not a regular file",
1006 Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't open %s: %s",
1007 PL_oldname, Strerror(eno));
1011 if (io && (IoFLAGS(io) & IOf_ARGV))
1012 IoFLAGS(io) |= IOf_START;
1014 (void)do_close(PL_argvoutgv,FALSE);
1015 if (io && (IoFLAGS(io) & IOf_ARGV)
1016 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
1018 GV * const oldout = MUTABLE_GV(av_pop(PL_argvout_stack));
1020 SvREFCNT_dec_NN(oldout);
1023 setdefout(gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO));
1028 /* explicit renamed to avoid C++ conflict -- kja */
1030 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
1037 if (!gv || !isGV_with_GP(gv)) {
1039 SETERRNO(EBADF,SS_IVCHAN);
1043 if (!io) { /* never opened */
1046 SETERRNO(EBADF,SS_IVCHAN);
1050 retval = io_close(io, NULL, not_implicit, FALSE);
1054 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1056 IoTYPE(io) = IoTYPE_CLOSED;
1061 Perl_io_close(pTHX_ IO *io, GV *gv, bool not_implicit, bool warn_on_fail)
1063 bool retval = FALSE;
1065 PERL_ARGS_ASSERT_IO_CLOSE;
1068 if (IoTYPE(io) == IoTYPE_PIPE) {
1069 const int status = PerlProc_pclose(IoIFP(io));
1071 STATUS_NATIVE_CHILD_SET(status);
1072 retval = (STATUS_UNIX == 0);
1075 retval = (status != -1);
1078 else if (IoTYPE(io) == IoTYPE_STD)
1081 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
1082 const bool prev_err = PerlIO_error(IoOFP(io));
1085 PerlIO_restore_errno(IoOFP(io));
1087 retval = (PerlIO_close(IoOFP(io)) != EOF && !prev_err);
1088 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
1091 const bool prev_err = PerlIO_error(IoIFP(io));
1094 PerlIO_restore_errno(IoIFP(io));
1096 retval = (PerlIO_close(IoIFP(io)) != EOF && !prev_err);
1099 IoOFP(io) = IoIFP(io) = NULL;
1101 if (warn_on_fail && !retval) {
1103 Perl_ck_warner_d(aTHX_ packWARN(WARN_IO),
1104 "Warning: unable to close filehandle %"
1105 HEKf" properly: %"SVf,
1106 GvNAME_HEK(gv), get_sv("!",GV_ADD));
1108 Perl_ck_warner_d(aTHX_ packWARN(WARN_IO),
1109 "Warning: unable to close filehandle "
1111 get_sv("!",GV_ADD));
1114 else if (not_implicit) {
1115 SETERRNO(EBADF,SS_IVCHAN);
1122 Perl_do_eof(pTHX_ GV *gv)
1124 IO * const io = GvIO(gv);
1126 PERL_ARGS_ASSERT_DO_EOF;
1130 else if (IoTYPE(io) == IoTYPE_WRONLY)
1131 report_wrongway_fh(gv, '>');
1134 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
1135 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
1136 return FALSE; /* this is the most usual case */
1140 /* getc and ungetc can stomp on errno */
1142 const int ch = PerlIO_getc(IoIFP(io));
1144 (void)PerlIO_ungetc(IoIFP(io),ch);
1151 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
1152 if (PerlIO_get_cnt(IoIFP(io)) < -1)
1153 PerlIO_set_cnt(IoIFP(io),-1);
1155 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
1156 if (gv != PL_argvgv || !nextargv(gv, FALSE)) /* get another fp handy */
1160 return TRUE; /* normal fp, definitely end of file */
1166 Perl_do_tell(pTHX_ GV *gv)
1168 IO *const io = GvIO(gv);
1171 PERL_ARGS_ASSERT_DO_TELL;
1173 if (io && (fp = IoIFP(io))) {
1174 return PerlIO_tell(fp);
1177 SETERRNO(EBADF,RMS_IFI);
1182 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
1184 IO *const io = GvIO(gv);
1187 if (io && (fp = IoIFP(io))) {
1188 return PerlIO_seek(fp, pos, whence) >= 0;
1191 SETERRNO(EBADF,RMS_IFI);
1196 Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
1198 IO *const io = GvIO(gv);
1201 PERL_ARGS_ASSERT_DO_SYSSEEK;
1203 if (io && (fp = IoIFP(io))) {
1204 int fd = PerlIO_fileno(fp);
1206 return PerlLIO_lseek(fd, pos, whence);
1210 SETERRNO(EBADF,RMS_IFI);
1215 Perl_mode_from_discipline(pTHX_ const char *s, STRLEN len)
1217 int mode = O_BINARY;
1218 PERL_UNUSED_CONTEXT;
1224 if (s[2] == 'a' && s[3] == 'w'
1225 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1234 if (s[2] == 'r' && s[3] == 'l' && s[4] == 'f'
1235 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1244 goto fail_discipline;
1247 else if (isSPACE(*s)) {
1254 end = strchr(s+1, ':');
1257 #ifndef PERLIO_LAYERS
1258 Perl_croak(aTHX_ "IO layers (like '%.*s') unavailable", end-s, s);
1269 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE)
1271 my_chsize(int fd, Off_t length)
1274 /* code courtesy of William Kucharski */
1279 if (PerlLIO_fstat(fd, &filebuf) < 0)
1282 if (filebuf.st_size < length) {
1284 /* extend file length */
1286 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
1289 /* write a "0" byte */
1291 if ((PerlLIO_write(fd, "", 1)) != 1)
1295 /* truncate length */
1299 fl.l_start = length;
1300 fl.l_type = F_WRLCK; /* write lock on file space */
1303 * This relies on the UNDOCUMENTED F_FREESP argument to
1304 * fcntl(2), which truncates the file so that it ends at the
1305 * position indicated by fl.l_start.
1307 * Will minor miracles never cease?
1310 if (fcntl(fd, F_FREESP, &fl) < 0)
1316 Perl_croak_nocontext("truncate not implemented");
1317 #endif /* F_FREESP */
1320 #endif /* !HAS_TRUNCATE && !HAS_CHSIZE */
1323 Perl_do_print(pTHX_ SV *sv, PerlIO *fp)
1325 PERL_ARGS_ASSERT_DO_PRINT;
1327 /* assuming fp is checked earlier */
1330 if (SvTYPE(sv) == SVt_IV && SvIOK(sv)) {
1331 assert(!SvGMAGICAL(sv));
1333 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
1335 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
1336 return !PerlIO_error(fp);
1340 /* Do this first to trigger any overloading. */
1341 const char *tmps = SvPV_const(sv, len);
1345 if (PerlIO_isutf8(fp)) { /* If the stream is utf8 ... */
1346 if (!SvUTF8(sv)) { /* Convert to utf8 if necessary */
1347 /* We don't modify the original scalar. */
1348 tmpbuf = bytes_to_utf8((const U8*) tmps, &len);
1349 tmps = (char *) tmpbuf;
1351 else if (ckWARN4_d(WARN_UTF8, WARN_SURROGATE, WARN_NON_UNICODE, WARN_NONCHAR)) {
1352 (void) check_utf8_print((const U8*) tmps, len);
1354 } /* else stream isn't utf8 */
1355 else if (DO_UTF8(sv)) { /* But if is utf8 internally, attempt to
1357 STRLEN tmplen = len;
1359 U8 * const result = bytes_from_utf8((const U8*) tmps, &tmplen, &utf8);
1362 /* Here, succeeded in downgrading from utf8. Set up to below
1363 * output the converted value */
1365 tmps = (char *) tmpbuf;
1368 else { /* Non-utf8 output stream, but string only representable in
1370 assert((char *)result == tmps);
1371 Perl_ck_warner_d(aTHX_ packWARN(WARN_UTF8),
1372 "Wide character in %s",
1373 PL_op ? OP_DESC(PL_op) : "print"
1375 /* Could also check that isn't one of the things to avoid
1376 * in utf8 by using check_utf8_print(), but not doing so,
1377 * since the stream isn't a UTF8 stream */
1380 /* To detect whether the process is about to overstep its
1381 * filesize limit we would need getrlimit(). We could then
1382 * also transparently raise the limit with setrlimit() --
1383 * but only until the system hard limit/the filesystem limit,
1384 * at which we would get EPERM. Note that when using buffered
1385 * io the write failure can be delayed until the flush/close. --jhi */
1386 if (len && (PerlIO_write(fp,tmps,len) == 0))
1389 return happy ? !PerlIO_error(fp) : FALSE;
1394 Perl_my_stat_flags(pTHX_ const U32 flags)
1400 if (PL_op->op_flags & OPf_REF) {
1404 return PL_laststatval;
1407 PL_laststype = OP_STAT;
1408 PL_statgv = gv ? gv : (GV *)io;
1409 sv_setpvs(PL_statname, "");
1412 int fd = PerlIO_fileno(IoIFP(io));
1414 /* E.g. PerlIO::scalar has no real fd. */
1415 return (PL_laststatval = -1);
1417 return (PL_laststatval = PerlLIO_fstat(fd, &PL_statcache));
1419 } else if (IoDIRP(io)) {
1420 return (PL_laststatval = PerlLIO_fstat(my_dirfd(IoDIRP(io)), &PL_statcache));
1423 PL_laststatval = -1;
1427 else if ((PL_op->op_private & (OPpFT_STACKED|OPpFT_AFTER_t))
1429 return PL_laststatval;
1431 SV* const sv = TOPs;
1434 if ((gv = MAYBE_DEREF_GV_flags(sv,flags))) {
1437 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
1438 io = MUTABLE_IO(SvRV(sv));
1440 goto do_fstat_have_io;
1443 s = SvPV_flags_const(sv, len, flags);
1445 sv_setpvn(PL_statname, s, len);
1446 s = SvPVX_const(PL_statname); /* s now NUL-terminated */
1447 PL_laststype = OP_STAT;
1448 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1449 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && should_warn_nl(s)) {
1450 GCC_DIAG_IGNORE(-Wformat-nonliteral); /* PL_warn_nl is constant */
1451 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
1454 return PL_laststatval;
1460 Perl_my_lstat_flags(pTHX_ const U32 flags)
1462 static const char* const no_prev_lstat = "The stat preceding -l _ wasn't an lstat";
1465 SV* const sv = TOPs;
1467 if (PL_op->op_flags & OPf_REF) {
1468 if (cGVOP_gv == PL_defgv) {
1469 if (PL_laststype != OP_LSTAT)
1470 Perl_croak(aTHX_ "%s", no_prev_lstat);
1471 return PL_laststatval;
1473 PL_laststatval = -1;
1474 if (ckWARN(WARN_IO)) {
1475 /* diag_listed_as: Use of -l on filehandle%s */
1476 Perl_warner(aTHX_ packWARN(WARN_IO),
1477 "Use of -l on filehandle %"HEKf,
1478 HEKfARG(GvENAME_HEK(cGVOP_gv)));
1482 if ((PL_op->op_private & (OPpFT_STACKED|OPpFT_AFTER_t))
1484 if (PL_laststype != OP_LSTAT)
1485 Perl_croak(aTHX_ "%s", no_prev_lstat);
1486 return PL_laststatval;
1489 PL_laststype = OP_LSTAT;
1491 if ( ( (SvROK(sv) && ( isGV_with_GP(SvRV(sv))
1492 || (isio = SvTYPE(SvRV(sv)) == SVt_PVIO) )
1496 && ckWARN(WARN_IO)) {
1498 /* diag_listed_as: Use of -l on filehandle%s */
1499 Perl_warner(aTHX_ packWARN(WARN_IO),
1500 "Use of -l on filehandle");
1502 /* diag_listed_as: Use of -l on filehandle%s */
1503 Perl_warner(aTHX_ packWARN(WARN_IO),
1504 "Use of -l on filehandle %"HEKf,
1505 HEKfARG(GvENAME_HEK((const GV *)
1506 (SvROK(sv) ? SvRV(sv) : sv))));
1508 file = SvPV_flags_const_nolen(sv, flags);
1509 sv_setpv(PL_statname,file);
1510 PL_laststatval = PerlLIO_lstat(file,&PL_statcache);
1511 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && should_warn_nl(file)) {
1512 GCC_DIAG_IGNORE(-Wformat-nonliteral); /* PL_warn_nl is constant */
1513 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "lstat");
1516 return PL_laststatval;
1520 S_exec_failed(pTHX_ const char *cmd, int fd, int do_report)
1522 const int e = errno;
1523 PERL_ARGS_ASSERT_EXEC_FAILED;
1524 if (ckWARN(WARN_EXEC))
1525 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
1528 /* XXX silently ignore failures */
1529 PERL_UNUSED_RESULT(PerlLIO_write(fd, (void*)&e, sizeof(int)));
1535 Perl_do_aexec5(pTHX_ SV *really, SV **mark, SV **sp,
1536 int fd, int do_report)
1539 PERL_ARGS_ASSERT_DO_AEXEC5;
1540 #if defined(__SYMBIAN32__) || defined(__LIBCATAMOUNT__)
1541 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1545 const char *tmps = NULL;
1546 Newx(PL_Argv, sp - mark + 1, const char*);
1549 while (++mark <= sp) {
1551 *a++ = SvPV_nolen_const(*mark);
1557 tmps = SvPV_nolen_const(really);
1558 if ((!really && *PL_Argv[0] != '/') ||
1559 (really && *tmps != '/')) /* will execvp use PATH? */
1560 TAINT_ENV(); /* testing IFS here is overkill, probably */
1562 if (really && *tmps)
1563 PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
1565 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1567 S_exec_failed(aTHX_ (really ? tmps : PL_Argv[0]), fd, do_report);
1575 Perl_do_execfree(pTHX)
1583 #ifdef PERL_DEFAULT_DO_EXEC3_IMPLEMENTATION
1586 Perl_do_exec3(pTHX_ const char *incmd, int fd, int do_report)
1593 /* Make a copy so we can change it */
1594 const Size_t cmdlen = strlen(incmd) + 1;
1596 PERL_ARGS_ASSERT_DO_EXEC3;
1598 Newx(buf, cmdlen, char);
1600 memcpy(cmd, incmd, cmdlen);
1602 while (*cmd && isSPACE(*cmd))
1605 /* save an extra exec if possible */
1609 char flags[PERL_FLAGS_MAX];
1610 if (strnEQ(cmd,PL_cshname,PL_cshlen) &&
1611 strnEQ(cmd+PL_cshlen," -c",3)) {
1612 my_strlcpy(flags, "-c", PERL_FLAGS_MAX);
1613 s = cmd+PL_cshlen+3;
1616 my_strlcat(flags, "f", PERL_FLAGS_MAX - 2);
1621 char * const ncmd = s;
1627 if (s[-1] == '\'') {
1630 PerlProc_execl(PL_cshname, "csh", flags, ncmd, (char*)NULL);
1633 S_exec_failed(aTHX_ PL_cshname, fd, do_report);
1642 /* see if there are shell metacharacters in it */
1644 if (*cmd == '.' && isSPACE(cmd[1]))
1647 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1651 while (isWORDCHAR(*s))
1652 s++; /* catch VAR=val gizmo */
1656 for (s = cmd; *s; s++) {
1657 if (*s != ' ' && !isALPHA(*s) &&
1658 strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1659 if (*s == '\n' && !s[1]) {
1663 /* handle the 2>&1 construct at the end */
1664 if (*s == '>' && s[1] == '&' && s[2] == '1'
1665 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1666 && (!s[3] || isSPACE(s[3])))
1668 const char *t = s + 3;
1670 while (*t && isSPACE(*t))
1672 if (!*t && (PerlLIO_dup2(1,2) != -1)) {
1679 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char *)NULL);
1681 S_exec_failed(aTHX_ PL_sh_path, fd, do_report);
1687 Newx(PL_Argv, (s - cmd) / 2 + 2, const char*);
1688 PL_Cmd = savepvn(cmd, s-cmd);
1690 for (s = PL_Cmd; *s;) {
1695 while (*s && !isSPACE(*s))
1703 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1705 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1709 S_exec_failed(aTHX_ PL_Argv[0], fd, do_report);
1716 #endif /* OS2 || WIN32 */
1719 #include <starlet.h> /* for sys$delprc */
1723 Perl_apply(pTHX_ I32 type, SV **mark, SV **sp)
1727 const char *const what = PL_op_name[type];
1730 SV ** const oldmark = mark;
1731 bool killgp = FALSE;
1733 PERL_ARGS_ASSERT_APPLY;
1735 PERL_UNUSED_VAR(what); /* may not be used depending on compile options */
1737 /* Doing this ahead of the switch statement preserves the old behaviour,
1738 where attempting to use kill as a taint test test would fail on
1739 platforms where kill was not defined. */
1741 if (type == OP_KILL)
1742 Perl_die(aTHX_ PL_no_func, what);
1745 if (type == OP_CHOWN)
1746 Perl_die(aTHX_ PL_no_func, what);
1750 #define APPLY_TAINT_PROPER() \
1752 if (TAINT_get) { TAINT_PROPER(what); } \
1755 /* This is a first heuristic; it doesn't catch tainting magic. */
1757 while (++mark <= sp) {
1758 if (SvTAINTED(*mark)) {
1767 APPLY_TAINT_PROPER();
1770 APPLY_TAINT_PROPER();
1772 while (++mark <= sp) {
1774 if ((gv = MAYBE_DEREF_GV(*mark))) {
1775 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1777 int fd = PerlIO_fileno(IoIFP(GvIOn(gv)));
1778 APPLY_TAINT_PROPER();
1780 SETERRNO(EBADF,RMS_IFI);
1782 } else if (fchmod(fd, val))
1785 Perl_die(aTHX_ PL_no_func, "fchmod");
1789 SETERRNO(EBADF,RMS_IFI);
1794 const char *name = SvPV_nomg_const(*mark, len);
1795 APPLY_TAINT_PROPER();
1796 if (!IS_SAFE_PATHNAME(name, len, "chmod") ||
1797 PerlLIO_chmod(name, val)) {
1806 APPLY_TAINT_PROPER();
1807 if (sp - mark > 2) {
1809 val = SvIVx(*++mark);
1810 val2 = SvIVx(*++mark);
1811 APPLY_TAINT_PROPER();
1813 while (++mark <= sp) {
1815 if ((gv = MAYBE_DEREF_GV(*mark))) {
1816 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1818 int fd = PerlIO_fileno(IoIFP(GvIOn(gv)));
1819 APPLY_TAINT_PROPER();
1821 SETERRNO(EBADF,RMS_IFI);
1823 } else if (fchown(fd, val, val2))
1826 Perl_die(aTHX_ PL_no_func, "fchown");
1830 SETERRNO(EBADF,RMS_IFI);
1835 const char *name = SvPV_nomg_const(*mark, len);
1836 APPLY_TAINT_PROPER();
1837 if (!IS_SAFE_PATHNAME(name, len, "chown") ||
1838 PerlLIO_chown(name, val, val2)) {
1847 XXX Should we make lchown() directly available from perl?
1848 For now, we'll let Configure test for HAS_LCHOWN, but do
1849 nothing in the core.
1854 APPLY_TAINT_PROPER();
1857 s = SvPVx_const(*++mark, len);
1858 if (*s == '-' && isALPHA(s[1]))
1865 if (*s == 'S' && s[1] == 'I' && s[2] == 'G') {
1869 if ((val = whichsig_pvn(s, len)) < 0)
1870 Perl_croak(aTHX_ "Unrecognized signal name \"%"SVf"\"", SVfARG(*mark));
1881 APPLY_TAINT_PROPER();
1884 /* kill() doesn't do process groups (job trees?) under VMS */
1885 if (val == SIGKILL) {
1886 /* Use native sys$delprc() to insure that target process is
1887 * deleted; supervisor-mode images don't pay attention to
1888 * CRTL's emulation of Unix-style signals and kill()
1890 while (++mark <= sp) {
1892 unsigned long int __vmssts;
1894 if (!(SvIOK(*mark) || SvNOK(*mark) || looks_like_number(*mark)))
1895 Perl_croak(aTHX_ "Can't kill a non-numeric process ID");
1896 proc = SvIV_nomg(*mark);
1897 APPLY_TAINT_PROPER();
1898 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1902 case SS$_NOSUCHNODE:
1903 SETERRNO(ESRCH,__vmssts);
1906 SETERRNO(EPERM,__vmssts);
1909 SETERRNO(EVMSERR,__vmssts);
1917 while (++mark <= sp) {
1920 if (!(SvNIOK(*mark) || looks_like_number(*mark)))
1921 Perl_croak(aTHX_ "Can't kill a non-numeric process ID");
1922 proc = SvIV_nomg(*mark);
1923 APPLY_TAINT_PROPER();
1925 /* use killpg in preference, as the killpg() wrapper for Win32
1926 * understands process groups, but the kill() wrapper doesn't */
1927 if (killgp ? PerlProc_killpg(proc, val)
1928 : PerlProc_kill(proc, val))
1930 if (PerlProc_kill(killgp ? -proc: proc, val))
1938 APPLY_TAINT_PROPER();
1940 while (++mark <= sp) {
1941 s = SvPV_const(*mark, len);
1942 APPLY_TAINT_PROPER();
1943 if (!IS_SAFE_PATHNAME(s, len, "unlink")) {
1946 else if (PL_unsafe) {
1950 else { /* don't let root wipe out directories without -U */
1951 if (PerlLIO_lstat(s,&PL_statbuf) < 0)
1953 else if (S_ISDIR(PL_statbuf.st_mode)) {
1955 SETERRNO(EISDIR, SS_NOPRIV);
1964 #if defined(HAS_UTIME) || defined(HAS_FUTIMES)
1966 APPLY_TAINT_PROPER();
1967 if (sp - mark > 2) {
1968 #if defined(HAS_FUTIMES)
1969 struct timeval utbuf[2];
1970 void *utbufp = utbuf;
1971 #elif defined(I_UTIME) || defined(VMS)
1972 struct utimbuf utbuf;
1973 struct utimbuf *utbufp = &utbuf;
1979 void *utbufp = &utbuf;
1982 SV* const accessed = *++mark;
1983 SV* const modified = *++mark;
1985 /* Be like C, and if both times are undefined, let the C
1986 * library figure out what to do. This usually means
1987 * "current time". */
1989 if ( accessed == &PL_sv_undef && modified == &PL_sv_undef )
1992 Zero(&utbuf, sizeof utbuf, char);
1994 utbuf[0].tv_sec = (long)SvIV(accessed); /* time accessed */
1995 utbuf[0].tv_usec = 0;
1996 utbuf[1].tv_sec = (long)SvIV(modified); /* time modified */
1997 utbuf[1].tv_usec = 0;
1998 #elif defined(BIG_TIME)
1999 utbuf.actime = (Time_t)SvNV(accessed); /* time accessed */
2000 utbuf.modtime = (Time_t)SvNV(modified); /* time modified */
2002 utbuf.actime = (Time_t)SvIV(accessed); /* time accessed */
2003 utbuf.modtime = (Time_t)SvIV(modified); /* time modified */
2006 APPLY_TAINT_PROPER();
2008 while (++mark <= sp) {
2010 if ((gv = MAYBE_DEREF_GV(*mark))) {
2011 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
2013 int fd = PerlIO_fileno(IoIFP(GvIOn(gv)));
2014 APPLY_TAINT_PROPER();
2016 SETERRNO(EBADF,RMS_IFI);
2018 } else if (futimes(fd, (struct timeval *) utbufp))
2021 Perl_die(aTHX_ PL_no_func, "futimes");
2029 const char * const name = SvPV_nomg_const(*mark, len);
2030 APPLY_TAINT_PROPER();
2031 if (!IS_SAFE_PATHNAME(name, len, "utime")) {
2036 if (utimes(name, (struct timeval *)utbufp))
2038 if (PerlLIO_utime(name, utbufp))
2052 #undef APPLY_TAINT_PROPER
2055 /* Do the permissions allow some operation? Assumes statcache already set. */
2056 #ifndef VMS /* VMS' cando is in vms.c */
2058 Perl_cando(pTHX_ Mode_t mode, bool effective, const Stat_t *statbufp)
2059 /* effective is a flag, true for EUID, or for checking if the effective gid
2060 * is in the list of groups returned from getgroups().
2063 PERL_ARGS_ASSERT_CANDO;
2064 PERL_UNUSED_CONTEXT;
2067 /* [Comments and code from Len Reed]
2068 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
2069 * to write-protected files. The execute permission bit is set
2070 * by the Microsoft C library stat() function for the following:
2075 * All files and directories are readable.
2076 * Directories and special files, e.g. "CON", cannot be
2078 * [Comment by Tom Dinger -- a directory can have the write-protect
2079 * bit set in the file system, but DOS permits changes to
2080 * the directory anyway. In addition, all bets are off
2081 * here for networked software, such as Novell and
2085 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
2086 * too so it will actually look into the files for magic numbers
2088 return (mode & statbufp->st_mode) ? TRUE : FALSE;
2090 #else /* ! DOSISH */
2092 if (ingroup(544,effective)) { /* member of Administrators */
2094 if ((effective ? PerlProc_geteuid() : PerlProc_getuid()) == 0) { /* root is special */
2096 if (mode == S_IXUSR) {
2097 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
2101 return TRUE; /* root reads and writes anything */
2104 if (statbufp->st_uid == (effective ? PerlProc_geteuid() : PerlProc_getuid()) ) {
2105 if (statbufp->st_mode & mode)
2106 return TRUE; /* ok as "user" */
2108 else if (ingroup(statbufp->st_gid,effective)) {
2109 if (statbufp->st_mode & mode >> 3)
2110 return TRUE; /* ok as "group" */
2112 else if (statbufp->st_mode & mode >> 6)
2113 return TRUE; /* ok as "other" */
2115 #endif /* ! DOSISH */
2120 S_ingroup(pTHX_ Gid_t testgid, bool effective)
2122 #ifndef PERL_IMPLICIT_SYS
2123 /* PERL_IMPLICIT_SYS like Win32: getegid() etc. require the context. */
2124 PERL_UNUSED_CONTEXT;
2126 if (testgid == (effective ? PerlProc_getegid() : PerlProc_getgid()))
2128 #ifdef HAS_GETGROUPS
2130 Groups_t *gary = NULL;
2134 anum = getgroups(0, gary);
2136 Newx(gary, anum, Groups_t);
2137 anum = getgroups(anum, gary);
2139 if (gary[anum] == testgid) {
2153 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
2156 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
2158 const key_t key = (key_t)SvNVx(*++mark);
2159 SV *nsv = optype == OP_MSGGET ? NULL : *++mark;
2160 const I32 flags = SvIVx(*++mark);
2162 PERL_ARGS_ASSERT_DO_IPCGET;
2163 PERL_UNUSED_ARG(sp);
2170 return msgget(key, flags);
2174 return semget(key, (int) SvIV(nsv), flags);
2178 return shmget(key, (size_t) SvUV(nsv), flags);
2180 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
2182 /* diag_listed_as: msg%s not implemented */
2183 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2186 return -1; /* should never happen */
2190 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
2194 const I32 id = SvIVx(*++mark);
2196 const I32 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
2198 const I32 cmd = SvIVx(*++mark);
2199 SV * const astr = *++mark;
2200 STRLEN infosize = 0;
2201 I32 getinfo = (cmd == IPC_STAT);
2203 PERL_ARGS_ASSERT_DO_IPCCTL;
2204 PERL_UNUSED_ARG(sp);
2210 if (cmd == IPC_STAT || cmd == IPC_SET)
2211 infosize = sizeof(struct msqid_ds);
2216 if (cmd == IPC_STAT || cmd == IPC_SET)
2217 infosize = sizeof(struct shmid_ds);
2223 if (cmd == IPC_STAT || cmd == IPC_SET)
2224 infosize = sizeof(struct semid_ds);
2225 else if (cmd == GETALL || cmd == SETALL)
2227 struct semid_ds semds;
2229 #ifdef EXTRA_F_IN_SEMUN_BUF
2230 semun.buff = &semds;
2234 getinfo = (cmd == GETALL);
2235 if (Semctl(id, 0, IPC_STAT, semun) == -1)
2237 infosize = semds.sem_nsems * sizeof(short);
2238 /* "short" is technically wrong but much more portable
2239 than guessing about u_?short(_t)? */
2242 /* diag_listed_as: sem%s not implemented */
2243 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2247 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
2249 /* diag_listed_as: shm%s not implemented */
2250 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2258 SvPV_force_nolen(astr);
2259 a = SvGROW(astr, infosize+1);
2264 a = SvPV(astr, len);
2265 if (len != infosize)
2266 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
2274 const IV i = SvIV(astr);
2275 a = INT2PTR(char *,i); /* ouch */
2282 ret = msgctl(id, cmd, (struct msqid_ds *)a);
2288 union semun unsemds;
2291 unsemds.val = PTR2nat(a);
2294 #ifdef EXTRA_F_IN_SEMUN_BUF
2295 unsemds.buff = (struct semid_ds *)a;
2297 unsemds.buf = (struct semid_ds *)a;
2300 ret = Semctl(id, n, cmd, unsemds);
2302 /* diag_listed_as: sem%s not implemented */
2303 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2310 ret = shmctl(id, cmd, (struct shmid_ds *)a);
2314 if (getinfo && ret >= 0) {
2315 SvCUR_set(astr, infosize);
2316 *SvEND(astr) = '\0';
2323 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
2327 const I32 id = SvIVx(*++mark);
2328 SV * const mstr = *++mark;
2329 const I32 flags = SvIVx(*++mark);
2330 const char * const mbuf = SvPV_const(mstr, len);
2331 const I32 msize = len - sizeof(long);
2333 PERL_ARGS_ASSERT_DO_MSGSND;
2334 PERL_UNUSED_ARG(sp);
2337 Perl_croak(aTHX_ "Arg too short for msgsnd");
2339 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
2341 PERL_UNUSED_ARG(sp);
2342 PERL_UNUSED_ARG(mark);
2343 /* diag_listed_as: msg%s not implemented */
2344 Perl_croak(aTHX_ "msgsnd not implemented");
2350 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
2355 I32 msize, flags, ret;
2356 const I32 id = SvIVx(*++mark);
2357 SV * const mstr = *++mark;
2359 PERL_ARGS_ASSERT_DO_MSGRCV;
2360 PERL_UNUSED_ARG(sp);
2362 /* suppress warning when reading into undef var --jhi */
2364 sv_setpvs(mstr, "");
2365 msize = SvIVx(*++mark);
2366 mtype = (long)SvIVx(*++mark);
2367 flags = SvIVx(*++mark);
2368 SvPV_force_nolen(mstr);
2369 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
2372 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
2374 SvCUR_set(mstr, sizeof(long)+ret);
2375 *SvEND(mstr) = '\0';
2376 /* who knows who has been playing with this message? */
2381 PERL_UNUSED_ARG(sp);
2382 PERL_UNUSED_ARG(mark);
2383 /* diag_listed_as: msg%s not implemented */
2384 Perl_croak(aTHX_ "msgrcv not implemented");
2390 Perl_do_semop(pTHX_ SV **mark, SV **sp)
2394 const I32 id = SvIVx(*++mark);
2395 SV * const opstr = *++mark;
2396 const char * const opbuf = SvPV_const(opstr, opsize);
2398 PERL_ARGS_ASSERT_DO_SEMOP;
2399 PERL_UNUSED_ARG(sp);
2401 if (opsize < 3 * SHORTSIZE
2402 || (opsize % (3 * SHORTSIZE))) {
2403 SETERRNO(EINVAL,LIB_INVARG);
2407 /* We can't assume that sizeof(struct sembuf) == 3 * sizeof(short). */
2409 const int nsops = opsize / (3 * sizeof (short));
2411 short * const ops = (short *) opbuf;
2413 struct sembuf *temps, *t;
2416 Newx (temps, nsops, struct sembuf);
2424 result = semop(id, temps, nsops);
2429 /* diag_listed_as: sem%s not implemented */
2430 Perl_croak(aTHX_ "semop not implemented");
2435 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
2439 struct shmid_ds shmds;
2440 const I32 id = SvIVx(*++mark);
2441 SV * const mstr = *++mark;
2442 const I32 mpos = SvIVx(*++mark);
2443 const I32 msize = SvIVx(*++mark);
2445 PERL_ARGS_ASSERT_DO_SHMIO;
2446 PERL_UNUSED_ARG(sp);
2449 if (shmctl(id, IPC_STAT, &shmds) == -1)
2451 if (mpos < 0 || msize < 0
2452 || (size_t)mpos + msize > (size_t)shmds.shm_segsz) {
2453 SETERRNO(EFAULT,SS_ACCVIO); /* can't do as caller requested */
2456 shm = (char *)shmat(id, NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
2457 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2459 if (optype == OP_SHMREAD) {
2461 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2463 SvUPGRADE(mstr, SVt_PV);
2465 sv_setpvs(mstr, "");
2467 mbuf = SvGROW(mstr, (STRLEN)msize+1);
2469 Copy(shm + mpos, mbuf, msize, char);
2470 SvCUR_set(mstr, msize);
2471 *SvEND(mstr) = '\0';
2473 /* who knows who has been playing with this shared memory? */
2479 const char *mbuf = SvPV_const(mstr, len);
2480 const I32 n = ((I32)len > msize) ? msize : (I32)len;
2481 Copy(mbuf, shm + mpos, n, char);
2483 memzero(shm + mpos + n, msize - n);
2487 /* diag_listed_as: shm%s not implemented */
2488 Perl_croak(aTHX_ "shm I/O not implemented");
2493 #endif /* SYSV IPC */
2498 =for apidoc start_glob
2500 Function called by C<do_readline> to spawn a glob (or do the glob inside
2501 perl on VMS). This code used to be inline, but now perl uses C<File::Glob>
2502 this glob starter is only used by miniperl during the build process.
2503 Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up.
2509 Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
2511 SV * const tmpcmd = newSV(0);
2514 const char *s = SvPV(tmpglob, len);
2516 PERL_ARGS_ASSERT_START_GLOB;
2518 if (!IS_SAFE_SYSCALL(s, len, "pattern", "glob"))
2523 #ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
2524 /* since spawning off a process is a real performance hit */
2531 fp = Perl_vms_start_glob(aTHX_ tmpglob, io);
2536 sv_setpv(tmpcmd, "for a in ");
2537 sv_catsv(tmpcmd, tmpglob);
2538 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
2541 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
2542 sv_catsv(tmpcmd, tmpglob);
2544 sv_setpv(tmpcmd, "perlglob ");
2545 sv_catsv(tmpcmd, tmpglob);
2546 sv_catpv(tmpcmd, " |");
2551 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
2552 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
2553 sv_catsv(tmpcmd, tmpglob);
2554 sv_catpv(tmpcmd, "' 2>/dev/null |");
2556 sv_setpv(tmpcmd, "echo ");
2557 sv_catsv(tmpcmd, tmpglob);
2558 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
2560 #endif /* !DOSISH */
2562 GV * const envgv = gv_fetchpvs("ENV", 0, SVt_PVHV);
2563 SV ** const home = hv_fetchs(GvHV(envgv), "HOME", 0);
2564 SV ** const path = hv_fetchs(GvHV(envgv), "PATH", 0);
2565 if (home && *home) SvGETMAGIC(*home);
2566 if (path && *path) SvGETMAGIC(*path);
2567 save_hash(gv_fetchpvs("ENV", 0, SVt_PVHV));
2568 if (home && *home) SvSETMAGIC(*home);
2569 if (path && *path) SvSETMAGIC(*path);
2571 (void)do_open6(PL_last_in_gv, SvPVX_const(tmpcmd), SvCUR(tmpcmd),
2577 if (!fp && ckWARN(WARN_GLOB)) {
2578 Perl_warner(aTHX_ packWARN(WARN_GLOB), "glob failed (can't start child: %s)",
2586 * ex: set ts=8 sts=4 sw=4 et: