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)
68 IO * const io = GvIOn(gv);
70 PERL_ARGS_ASSERT_OPENN_SETUP;
75 *savetype = IoTYPE_CLOSED;
77 Zero(mode,sizeof(mode),char);
78 PL_forkprocess = 1; /* assume true if no fork */
80 /* If currently open - close before we re-open */
82 if (IoTYPE(io) == IoTYPE_STD) {
83 /* This is a clone of one of STD* handles */
86 const int old_fd = PerlIO_fileno(IoIFP(io));
88 if (old_fd >= 0 && old_fd <= PL_maxsysfd) {
89 /* This is one of the original STD* handles */
92 *savetype = IoTYPE(io);
98 if (IoTYPE(io) == IoTYPE_PIPE)
99 result = PerlProc_pclose(IoIFP(io));
100 else if (IoIFP(io) != IoOFP(io)) {
102 result = PerlIO_close(IoOFP(io));
103 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
106 result = PerlIO_close(IoIFP(io));
109 result = PerlIO_close(IoIFP(io));
111 if (result == EOF && old_fd > PL_maxsysfd) {
112 /* Why is this not Perl_warn*() call ? */
113 PerlIO_printf(Perl_error_log,
114 "Warning: unable to close filehandle %"HEKf" properly.\n",
115 HEKfARG(GvENAME_HEK(gv))
120 IoOFP(io) = IoIFP(io) = NULL;
126 Perl_do_openn(pTHX_ GV *gv, const char *oname, I32 len, int as_raw,
127 int rawmode, int rawperm, PerlIO *supplied_fp, SV **svp,
130 PERL_ARGS_ASSERT_DO_OPENN;
133 /* sysopen style args, i.e. integer mode and permissions */
136 Perl_croak(aTHX_ "panic: sysopen with multiple args, num_svs=%ld",
139 return do_open_raw(gv, oname, len, rawmode, rawperm);
141 return do_open6(gv, oname, len, supplied_fp, svp, num_svs);
145 Perl_do_open_raw(pTHX_ GV *gv, const char *oname, STRLEN len,
146 int rawmode, int rawperm)
153 char mode[PERL_MODE_MAX]; /* file mode ("r\0", "rb\0", "ab\0" etc.) */
154 IO * const io = openn_setup(gv, mode, &saveifp, &saveofp, &savefd, &savetype);
158 PERL_ARGS_ASSERT_DO_OPEN_RAW;
160 /* For ease of blame back to 5.000, keep the existing indenting. */
162 /* sysopen style args, i.e. integer mode and permissions */
164 const int appendtrunc =
166 #ifdef O_APPEND /* Not fully portable. */
169 #ifdef O_TRUNC /* Not fully portable. */
173 const int modifyingmode = O_WRONLY|O_RDWR|O_CREAT|appendtrunc;
183 It might be (in OS/390 and Mac OS Classic it is)
189 This means that simple & with O_RDWR would look
190 like O_RDONLY is present. Therefore we have to
193 if ((ismodifying = (rawmode & modifyingmode))) {
194 if ((ismodifying & O_WRONLY) == O_WRONLY ||
195 (ismodifying & O_RDWR) == O_RDWR ||
196 (ismodifying & (O_CREAT|appendtrunc)))
197 TAINT_PROPER("sysopen");
199 mode[ix++] = IoTYPE_NUMERIC; /* Marker to openn to use numeric "sysopen" */
201 #if defined(USE_64_BIT_RAWIO) && defined(O_LARGEFILE)
202 rawmode |= O_LARGEFILE; /* Transparently largefiley. */
205 IoTYPE(io) = PerlIO_intmode2str(rawmode, &mode[ix], &writing);
207 namesv = newSVpvn_flags(oname, len, SVs_TEMP);
208 fp = PerlIO_openn(aTHX_ NULL, mode, -1, rawmode, rawperm, NULL, 1, &namesv);
210 return openn_cleanup(gv, io, fp, mode, oname, saveifp, saveofp, savefd,
211 savetype, writing, 0, NULL);
215 Perl_do_open6(pTHX_ GV *gv, const char *oname, STRLEN len,
216 PerlIO *supplied_fp, SV **svp, U32 num_svs)
223 char mode[PERL_MODE_MAX]; /* file mode ("r\0", "rb\0", "ab\0" etc.) */
224 IO * const io = openn_setup(gv, mode, &saveifp, &saveofp, &savefd, &savetype);
227 bool was_fdopen = FALSE;
230 PERL_ARGS_ASSERT_DO_OPEN6;
232 /* For ease of blame back to 5.000, keep the existing indenting. */
234 /* Regular (non-sys) open */
239 bool in_raw = 0, in_crlf = 0, out_raw = 0, out_crlf = 0;
241 /* Collect default raw/crlf info from the op */
242 if (PL_op && PL_op->op_type == OP_OPEN) {
243 /* set up IO layers */
244 const U8 flags = PL_op->op_private;
245 in_raw = (flags & OPpOPEN_IN_RAW);
246 in_crlf = (flags & OPpOPEN_IN_CRLF);
247 out_raw = (flags & OPpOPEN_OUT_RAW);
248 out_crlf = (flags & OPpOPEN_OUT_CRLF);
251 type = savepvn(oname, len);
255 /* Lose leading and trailing white space */
256 while (isSPACE(*type))
258 while (tend > type && isSPACE(tend[-1]))
264 /* New style explicit name, type is just mode and layer info */
266 if (SvROK(*svp) && !strchr(oname,'&')) {
268 Perl_warner(aTHX_ packWARN(WARN_IO),
269 "Can't open a reference");
270 SETERRNO(EINVAL, LIB_INVARG);
274 #endif /* USE_STDIO */
275 p = (SvOK(*svp) || SvGMAGICAL(*svp)) ? SvPV(*svp, nlen) : NULL;
277 if (p && !IS_SAFE_PATHNAME(p, nlen, "open")) {
282 name = p ? savepvn(p, nlen) : savepvs("");
291 if ((*type == IoTYPE_RDWR) && /* scary */
292 (*(type+1) == IoTYPE_RDONLY || *(type+1) == IoTYPE_WRONLY) &&
293 ((!num_svs || (tend > type+1 && tend[-1] != IoTYPE_PIPE)))) {
294 TAINT_PROPER("open");
299 if (*type == IoTYPE_PIPE) {
301 if (type[1] != IoTYPE_STD) {
303 Perl_croak(aTHX_ "Unknown open() mode '%.*s'", (int)olen, oname);
309 } while (isSPACE(*type));
315 /* command is missing 19990114 */
316 if (ckWARN(WARN_PIPE))
317 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open");
322 if (!(*name == '-' && name[1] == '\0') || num_svs)
324 TAINT_PROPER("piped open");
325 if (!num_svs && name[len-1] == '|') {
327 if (ckWARN(WARN_PIPE))
328 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Can't open bidirectional pipe");
337 fp = PerlProc_popen_list(mode, num_svs, svp);
340 fp = PerlProc_popen(name,mode);
344 if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) {
351 else if (*type == IoTYPE_WRONLY) {
352 TAINT_PROPER("open");
354 if (*type == IoTYPE_WRONLY) {
355 /* Two IoTYPE_WRONLYs in a row make for an IoTYPE_APPEND. */
356 mode[0] = IoTYPE(io) = IoTYPE_APPEND;
370 dodup = PERLIO_DUP_FD;
376 if (!num_svs && !*type && supplied_fp) {
377 /* "<+&" etc. is used by typemaps */
381 PerlIO *that_fp = NULL;
384 /* diag_listed_as: More than one argument to '%s' open */
385 Perl_croak(aTHX_ "More than one argument to '%c&' open",IoTYPE(io));
387 while (isSPACE(*type))
391 || (SvPOKp(*svp) && looks_like_number(*svp))
393 wanted_fd = SvUV(*svp);
396 else if (isDIGIT(*type)) {
397 wanted_fd = atoi(type);
402 thatio = sv_2io(*svp);
405 GV * const thatgv = gv_fetchpvn_flags(type, tend - type,
407 thatio = GvIO(thatgv);
411 SETERRNO(EINVAL,SS_IVCHAN);
416 if ((that_fp = IoIFP(thatio))) {
417 /* Flush stdio buffer before dup. --mjd
418 * Unfortunately SEEK_CURing 0 seems to
419 * be optimized away on most platforms;
420 * only Solaris and Linux seem to flush
422 /* On the other hand, do all platforms
423 * take gracefully to flushing a read-only
424 * filehandle? Perhaps we should do
425 * fsetpos(src)+fgetpos(dst)? --nik */
426 PerlIO_flush(that_fp);
427 wanted_fd = PerlIO_fileno(that_fp);
428 /* When dup()ing STDIN, STDOUT or STDERR
429 * explicitly set appropriate access mode */
430 if (that_fp == PerlIO_stdout()
431 || that_fp == PerlIO_stderr())
432 IoTYPE(io) = IoTYPE_WRONLY;
433 else if (that_fp == PerlIO_stdin())
434 IoTYPE(io) = IoTYPE_RDONLY;
435 /* When dup()ing a socket, say result is
437 else if (IoTYPE(thatio) == IoTYPE_SOCKET)
438 IoTYPE(io) = IoTYPE_SOCKET;
446 fp = PerlIO_fdupopen(aTHX_ that_fp, NULL, dodup);
450 wanted_fd = PerlLIO_dup(wanted_fd);
453 if (!(fp = PerlIO_openn(aTHX_ type,mode,wanted_fd,0,0,NULL,num_svs,svp))) {
454 if (dodup && wanted_fd >= 0)
455 PerlLIO_close(wanted_fd);
461 while (isSPACE(*type))
463 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
465 fp = PerlIO_stdout();
466 IoTYPE(io) = IoTYPE_STD;
468 /* diag_listed_as: More than one argument to '%s' open */
469 Perl_croak(aTHX_ "More than one argument to '>%c' open",IoTYPE_STD);
474 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
477 SV *namesv = newSVpvn_flags(type, tend - type, SVs_TEMP);
479 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,1,&namesv);
483 if (!fp && type && *type && *type != ':' && !isIDFIRST(*type))
484 goto unknown_open_mode;
485 } /* IoTYPE_WRONLY */
486 else if (*type == IoTYPE_RDONLY) {
489 } while (isSPACE(*type));
498 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
501 IoTYPE(io) = IoTYPE_STD;
503 /* diag_listed_as: More than one argument to '%s' open */
504 Perl_croak(aTHX_ "More than one argument to '<%c' open",IoTYPE_STD);
509 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
512 SV *namesv = newSVpvn_flags(type, tend - type, SVs_TEMP);
514 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,1,&namesv);
517 if (!fp && type && *type && *type != ':' && !isIDFIRST(*type))
518 goto unknown_open_mode;
519 } /* IoTYPE_RDONLY */
520 else if ((num_svs && /* '-|...' or '...|' */
521 type[0] == IoTYPE_STD && type[1] == IoTYPE_PIPE) ||
522 (!num_svs && tend > type+1 && tend[-1] == IoTYPE_PIPE)) {
524 type += 2; /* skip over '-|' */
528 while (tend > type && isSPACE(tend[-1]))
530 for (; isSPACE(*type); type++)
536 /* command is missing 19990114 */
537 if (ckWARN(WARN_PIPE))
538 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open");
543 if (!(*name == '-' && name[1] == '\0') || num_svs)
545 TAINT_PROPER("piped open");
554 fp = PerlProc_popen_list(mode,num_svs,svp);
557 fp = PerlProc_popen(name,mode);
559 IoTYPE(io) = IoTYPE_PIPE;
561 while (isSPACE(*type))
564 if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) {
571 else { /* layer(Args) */
573 goto unknown_open_mode;
575 IoTYPE(io) = IoTYPE_RDONLY;
576 for (; isSPACE(*name); name++)
585 if (*name == '-' && name[1] == '\0') {
587 IoTYPE(io) = IoTYPE_STD;
591 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
594 SV *namesv = newSVpvn_flags(type, tend - type, SVs_TEMP);
596 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,1,&namesv);
603 return openn_cleanup(gv, io, fp, mode, oname, saveifp, saveofp, savefd,
604 savetype, writing, was_fdopen, type);
607 /* Yes, this is ugly, but it's private, and I don't see a cleaner way to
608 simplify the two-headed public interface of do_openn. */
610 S_openn_cleanup(pTHX_ GV *gv, IO *io, PerlIO *fp, char *mode, const char *oname,
611 PerlIO *saveifp, PerlIO *saveofp, int savefd, char savetype,
612 int writing, bool was_fdopen, const char *type)
616 PERL_ARGS_ASSERT_OPENN_CLEANUP;
619 if (IoTYPE(io) == IoTYPE_RDONLY && ckWARN(WARN_NEWLINE)
620 && should_warn_nl(oname)
624 GCC_DIAG_IGNORE(-Wformat-nonliteral); /* PL_warn_nl is constant */
625 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
631 if (ckWARN(WARN_IO)) {
632 if ((IoTYPE(io) == IoTYPE_RDONLY) &&
633 (fp == PerlIO_stdout() || fp == PerlIO_stderr())) {
634 Perl_warner(aTHX_ packWARN(WARN_IO),
635 "Filehandle STD%s reopened as %"HEKf
637 ((fp == PerlIO_stdout()) ? "OUT" : "ERR"),
638 HEKfARG(GvENAME_HEK(gv)));
640 else if ((IoTYPE(io) == IoTYPE_WRONLY) && fp == PerlIO_stdin()) {
641 Perl_warner(aTHX_ packWARN(WARN_IO),
642 "Filehandle STDIN reopened as %"HEKf" only for output",
643 HEKfARG(GvENAME_HEK(gv))
648 fd = PerlIO_fileno(fp);
649 /* If there is no fd (e.g. PerlIO::scalar) assume it isn't a
650 * socket - this covers PerlIO::scalar - otherwise unless we "know" the
651 * type probe for socket-ness.
653 if (IoTYPE(io) && IoTYPE(io) != IoTYPE_PIPE && IoTYPE(io) != IoTYPE_STD && fd >= 0) {
654 if (PerlLIO_fstat(fd,&PL_statbuf) < 0) {
655 /* If PerlIO claims to have fd we had better be able to fstat() it. */
656 (void) PerlIO_close(fp);
660 if (S_ISSOCK(PL_statbuf.st_mode))
661 IoTYPE(io) = IoTYPE_SOCKET; /* in case a socket was passed in to us */
664 !(PL_statbuf.st_mode & S_IFMT)
665 && IoTYPE(io) != IoTYPE_WRONLY /* Dups of STD* filehandles already have */
666 && IoTYPE(io) != IoTYPE_RDONLY /* type so they aren't marked as sockets */
667 ) { /* on OS's that return 0 on fstat()ed pipe */
669 Sock_size_t buflen = sizeof tmpbuf;
670 if (PerlSock_getsockname(fd, (struct sockaddr *)tmpbuf, &buflen) >= 0
671 || errno != ENOTSOCK)
672 IoTYPE(io) = IoTYPE_SOCKET; /* some OS's return 0 on fstat()ed socket */
673 /* but some return 0 for streams too, sigh */
675 #endif /* HAS_SOCKET */
676 #endif /* !PERL_MICRO */
680 * If this is a standard handle we discard all the layer stuff
681 * and just dup the fd into whatever was on the handle before !
684 if (saveifp) { /* must use old fp? */
685 /* If fd is less that PL_maxsysfd i.e. STDIN..STDERR
686 then dup the new fileno down
689 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
690 if (saveofp != saveifp) { /* was a socket? */
691 PerlIO_close(saveofp);
695 /* Still a small can-of-worms here if (say) PerlIO::scalar
696 is assigned to (say) STDOUT - for now let dup2() fail
697 and provide the error
699 if (PerlLIO_dup2(fd, savefd) < 0) {
700 (void)PerlIO_close(fp);
704 if (savefd != PerlIO_fileno(PerlIO_stdin())) {
705 char newname[FILENAME_MAX+1];
706 if (PerlIO_getname(fp, newname)) {
707 if (fd == PerlIO_fileno(PerlIO_stdout()))
708 vmssetuserlnm("SYS$OUTPUT", newname);
709 if (fd == PerlIO_fileno(PerlIO_stderr()))
710 vmssetuserlnm("SYS$ERROR", newname);
716 /* PL_fdpid isn't used on Windows, so avoid this useless work.
717 * XXX Probably the same for a lot of other places. */
722 sv = *av_fetch(PL_fdpid,fd,TRUE);
723 SvUPGRADE(sv, SVt_IV);
726 sv = *av_fetch(PL_fdpid,savefd,TRUE);
727 SvUPGRADE(sv, SVt_IV);
733 /* need to close fp without closing underlying fd */
734 int ofd = PerlIO_fileno(fp);
735 int dupfd = PerlLIO_dup(ofd);
736 #if defined(HAS_FCNTL) && defined(F_SETFD)
737 /* Assume if we have F_SETFD we have F_GETFD */
738 int coe = fcntl(ofd,F_GETFD);
741 PerlLIO_dup2(dupfd,ofd);
742 #if defined(HAS_FCNTL) && defined(F_SETFD)
743 /* The dup trick has lost close-on-exec on ofd */
744 fcntl(ofd,F_SETFD, coe);
746 PerlLIO_close(dupfd);
753 fd = PerlIO_fileno(fp);
755 #if defined(HAS_FCNTL) && defined(F_SETFD)
758 fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
764 IoFLAGS(io) &= ~IOf_NOLINE;
766 if (IoTYPE(io) == IoTYPE_SOCKET
767 || (IoTYPE(io) == IoTYPE_WRONLY && fd >= 0 && S_ISCHR(PL_statbuf.st_mode)) ) {
769 if (*s == IoTYPE_IMPLICIT || *s == IoTYPE_NUMERIC)
772 if (!(IoOFP(io) = PerlIO_openn(aTHX_ type,s,fd,0,0,NULL,0,NULL))) {
786 IoTYPE(io) = savetype;
791 Perl_nextargv(pTHX_ GV *gv)
794 IO * const io = GvIOp(gv);
796 PERL_ARGS_ASSERT_NEXTARGV;
799 PL_argvoutgv = gv_fetchpvs("ARGVOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
800 if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
801 IoFLAGS(io) &= ~IOf_START;
804 Perl_av_create_and_push(aTHX_ &PL_argvout_stack,
805 SvREFCNT_inc_simple_NN(PL_defoutgv));
808 if (PL_filemode & (S_ISUID|S_ISGID)) {
809 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
812 (void)fchmod(PL_lastfd,PL_filemode);
814 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
821 while (av_tindex(GvAV(gv)) >= 0) {
823 SV *const sv = av_shift(GvAV(gv));
825 SvTAINTED_off(GvSVn(gv)); /* previous tainting irrelevant */
826 sv_setsv(GvSVn(gv),sv);
827 SvSETMAGIC(GvSV(gv));
828 PL_oldname = SvPVx(GvSV(gv), oldlen);
829 if (LIKELY(!PL_inplace)) {
830 if (do_open6(gv, PL_oldname, oldlen, NULL, NULL, 0)) {
831 return IoIFP(GvIOp(gv));
835 /* This very long block ends with return IoIFP(GvIOp(gv));
836 Both this block and the block above fall through on open
837 failure to the warning code, and then the while loop above tries
839 if (do_open_raw(gv, PL_oldname, oldlen, O_RDONLY, 0)) {
840 #ifndef FLEXFILENAMES
847 TAINT_PROPER("inplace open");
848 if (oldlen == 1 && *PL_oldname == '-') {
849 setdefout(gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL,
851 return IoIFP(GvIOp(gv));
853 #ifndef FLEXFILENAMES
854 filedev = PL_statbuf.st_dev;
855 fileino = PL_statbuf.st_ino;
857 PL_filemode = PL_statbuf.st_mode;
858 fileuid = PL_statbuf.st_uid;
859 filegid = PL_statbuf.st_gid;
860 if (!S_ISREG(PL_filemode)) {
861 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
862 "Can't do inplace edit: %s is not a regular file",
867 if (*PL_inplace && strNE(PL_inplace, "*")) {
868 const char *star = strchr(PL_inplace, '*');
870 const char *begin = PL_inplace;
873 sv_catpvn(sv, begin, star - begin);
874 sv_catpvn(sv, PL_oldname, oldlen);
876 } while ((star = strchr(begin, '*')));
881 sv_catpv(sv,PL_inplace);
883 #ifndef FLEXFILENAMES
884 if ((PerlLIO_stat(SvPVX_const(sv),&PL_statbuf) >= 0
885 && PL_statbuf.st_dev == filedev
886 && PL_statbuf.st_ino == fileino)
888 || ((_djstat_fail_bits & _STFAIL_TRUENAME)!=0)
892 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
893 "Can't do inplace edit: %"SVf" would not be unique",
900 #if !defined(DOSISH) && !defined(__CYGWIN__)
901 if (PerlLIO_rename(PL_oldname,SvPVX_const(sv)) < 0) {
902 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
903 "Can't rename %s to %"SVf": %s, skipping file",
904 PL_oldname, SVfARG(sv), Strerror(errno));
910 (void)PerlLIO_unlink(SvPVX_const(sv));
911 (void)PerlLIO_rename(PL_oldname,SvPVX_const(sv));
912 do_open_raw(gv, SvPVX_const(sv), SvCUR(sv), O_RDONLY, 0);
915 (void)UNLINK(SvPVX_const(sv));
916 if (link(PL_oldname,SvPVX_const(sv)) < 0) {
917 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
918 "Can't rename %s to %"SVf": %s, skipping file",
919 PL_oldname, SVfARG(sv), Strerror(errno) );
923 (void)UNLINK(PL_oldname);
927 #if !defined(DOSISH) && !defined(AMIGAOS)
928 # ifndef VMS /* Don't delete; use automatic file versioning */
929 if (UNLINK(PL_oldname) < 0) {
930 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
931 "Can't remove %s: %s, skipping file",
932 PL_oldname, Strerror(errno) );
938 Perl_croak(aTHX_ "Can't do inplace edit without backup");
942 sv_setpvn(sv,PL_oldname,oldlen);
943 SETERRNO(0,0); /* in case sprintf set errno */
944 if (!Perl_do_open_raw(aTHX_ PL_argvoutgv, SvPVX_const(sv),
947 O_WRONLY|O_CREAT|O_TRUNC, 0
949 O_WRONLY|O_CREAT|OPEN_EXCL, 0600
952 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE), "Can't do inplace edit on %s: %s",
953 PL_oldname, Strerror(errno) );
957 setdefout(PL_argvoutgv);
958 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
959 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
961 (void)fchmod(PL_lastfd,PL_filemode);
963 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
965 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
968 rc = fchown(PL_lastfd,fileuid,filegid);
971 rc = PerlLIO_chown(PL_oldname,fileuid,filegid);
974 /* XXX silently ignore failures */
977 return IoIFP(GvIOp(gv));
979 } /* successful do_open_raw(), PL_inplace non-NULL */
981 if (ckWARN_d(WARN_INPLACE)) {
982 const int eno = errno;
983 if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0
984 && !S_ISREG(PL_statbuf.st_mode)) {
985 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
986 "Can't do inplace edit: %s is not a regular file",
990 Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't open %s: %s",
991 PL_oldname, Strerror(eno));
995 if (io && (IoFLAGS(io) & IOf_ARGV))
996 IoFLAGS(io) |= IOf_START;
998 (void)do_close(PL_argvoutgv,FALSE);
999 if (io && (IoFLAGS(io) & IOf_ARGV)
1000 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
1002 GV * const oldout = MUTABLE_GV(av_pop(PL_argvout_stack));
1004 SvREFCNT_dec_NN(oldout);
1007 setdefout(gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO));
1012 /* explicit renamed to avoid C++ conflict -- kja */
1014 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
1022 if (!gv || !isGV_with_GP(gv)) {
1024 SETERRNO(EBADF,SS_IVCHAN);
1028 if (!io) { /* never opened */
1031 SETERRNO(EBADF,SS_IVCHAN);
1035 retval = io_close(io, not_implicit);
1039 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1041 IoTYPE(io) = IoTYPE_CLOSED;
1046 Perl_io_close(pTHX_ IO *io, bool not_implicit)
1049 bool retval = FALSE;
1051 PERL_ARGS_ASSERT_IO_CLOSE;
1054 if (IoTYPE(io) == IoTYPE_PIPE) {
1055 const int status = PerlProc_pclose(IoIFP(io));
1057 STATUS_NATIVE_CHILD_SET(status);
1058 retval = (STATUS_UNIX == 0);
1061 retval = (status != -1);
1064 else if (IoTYPE(io) == IoTYPE_STD)
1067 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
1068 const bool prev_err = PerlIO_error(IoOFP(io));
1069 retval = (PerlIO_close(IoOFP(io)) != EOF && !prev_err);
1070 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
1073 const bool prev_err = PerlIO_error(IoIFP(io));
1074 retval = (PerlIO_close(IoIFP(io)) != EOF && !prev_err);
1077 IoOFP(io) = IoIFP(io) = NULL;
1079 else if (not_implicit) {
1080 SETERRNO(EBADF,SS_IVCHAN);
1087 Perl_do_eof(pTHX_ GV *gv)
1090 IO * const io = GvIO(gv);
1092 PERL_ARGS_ASSERT_DO_EOF;
1096 else if (IoTYPE(io) == IoTYPE_WRONLY)
1097 report_wrongway_fh(gv, '>');
1100 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
1101 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
1102 return FALSE; /* this is the most usual case */
1106 /* getc and ungetc can stomp on errno */
1108 const int ch = PerlIO_getc(IoIFP(io));
1110 (void)PerlIO_ungetc(IoIFP(io),ch);
1117 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
1118 if (PerlIO_get_cnt(IoIFP(io)) < -1)
1119 PerlIO_set_cnt(IoIFP(io),-1);
1121 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
1122 if (gv != PL_argvgv || !nextargv(gv)) /* get another fp handy */
1126 return TRUE; /* normal fp, definitely end of file */
1132 Perl_do_tell(pTHX_ GV *gv)
1135 IO *const io = GvIO(gv);
1138 PERL_ARGS_ASSERT_DO_TELL;
1140 if (io && (fp = IoIFP(io))) {
1141 return PerlIO_tell(fp);
1144 SETERRNO(EBADF,RMS_IFI);
1149 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
1152 IO *const io = GvIO(gv);
1155 if (io && (fp = IoIFP(io))) {
1156 return PerlIO_seek(fp, pos, whence) >= 0;
1159 SETERRNO(EBADF,RMS_IFI);
1164 Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
1167 IO *const io = GvIO(gv);
1170 PERL_ARGS_ASSERT_DO_SYSSEEK;
1172 if (io && (fp = IoIFP(io)))
1173 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
1175 SETERRNO(EBADF,RMS_IFI);
1180 Perl_mode_from_discipline(pTHX_ const char *s, STRLEN len)
1182 int mode = O_BINARY;
1188 if (s[2] == 'a' && s[3] == 'w'
1189 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1198 if (s[2] == 'r' && s[3] == 'l' && s[4] == 'f'
1199 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1208 goto fail_discipline;
1211 else if (isSPACE(*s)) {
1218 end = strchr(s+1, ':');
1221 #ifndef PERLIO_LAYERS
1222 Perl_croak(aTHX_ "IO layers (like '%.*s') unavailable", end-s, s);
1233 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE)
1235 my_chsize(int fd, Off_t length)
1238 /* code courtesy of William Kucharski */
1243 if (PerlLIO_fstat(fd, &filebuf) < 0)
1246 if (filebuf.st_size < length) {
1248 /* extend file length */
1250 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
1253 /* write a "0" byte */
1255 if ((PerlLIO_write(fd, "", 1)) != 1)
1259 /* truncate length */
1263 fl.l_start = length;
1264 fl.l_type = F_WRLCK; /* write lock on file space */
1267 * This relies on the UNDOCUMENTED F_FREESP argument to
1268 * fcntl(2), which truncates the file so that it ends at the
1269 * position indicated by fl.l_start.
1271 * Will minor miracles never cease?
1274 if (fcntl(fd, F_FREESP, &fl) < 0)
1280 Perl_croak_nocontext("truncate not implemented");
1281 #endif /* F_FREESP */
1284 #endif /* !HAS_TRUNCATE && !HAS_CHSIZE */
1287 Perl_do_print(pTHX_ SV *sv, PerlIO *fp)
1291 PERL_ARGS_ASSERT_DO_PRINT;
1293 /* assuming fp is checked earlier */
1296 if (SvTYPE(sv) == SVt_IV && SvIOK(sv)) {
1297 assert(!SvGMAGICAL(sv));
1299 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
1301 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
1302 return !PerlIO_error(fp);
1306 /* Do this first to trigger any overloading. */
1307 const char *tmps = SvPV_const(sv, len);
1311 if (PerlIO_isutf8(fp)) { /* If the stream is utf8 ... */
1312 if (!SvUTF8(sv)) { /* Convert to utf8 if necessary */
1313 /* We don't modify the original scalar. */
1314 tmpbuf = bytes_to_utf8((const U8*) tmps, &len);
1315 tmps = (char *) tmpbuf;
1317 else if (ckWARN4_d(WARN_UTF8, WARN_SURROGATE, WARN_NON_UNICODE, WARN_NONCHAR)) {
1318 (void) check_utf8_print((const U8*) tmps, len);
1320 } /* else stream isn't utf8 */
1321 else if (DO_UTF8(sv)) { /* But if is utf8 internally, attempt to
1323 STRLEN tmplen = len;
1325 U8 * const result = bytes_from_utf8((const U8*) tmps, &tmplen, &utf8);
1328 /* Here, succeeded in downgrading from utf8. Set up to below
1329 * output the converted value */
1331 tmps = (char *) tmpbuf;
1334 else { /* Non-utf8 output stream, but string only representable in
1336 assert((char *)result == tmps);
1337 Perl_ck_warner_d(aTHX_ packWARN(WARN_UTF8),
1338 "Wide character in %s",
1339 PL_op ? OP_DESC(PL_op) : "print"
1341 /* Could also check that isn't one of the things to avoid
1342 * in utf8 by using check_utf8_print(), but not doing so,
1343 * since the stream isn't a UTF8 stream */
1346 /* To detect whether the process is about to overstep its
1347 * filesize limit we would need getrlimit(). We could then
1348 * also transparently raise the limit with setrlimit() --
1349 * but only until the system hard limit/the filesystem limit,
1350 * at which we would get EPERM. Note that when using buffered
1351 * io the write failure can be delayed until the flush/close. --jhi */
1352 if (len && (PerlIO_write(fp,tmps,len) == 0))
1355 return happy ? !PerlIO_error(fp) : FALSE;
1360 Perl_my_stat_flags(pTHX_ const U32 flags)
1367 if (PL_op->op_flags & OPf_REF) {
1371 return PL_laststatval;
1374 PL_laststype = OP_STAT;
1375 PL_statgv = gv ? gv : (GV *)io;
1376 sv_setpvs(PL_statname, "");
1379 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
1380 } else if (IoDIRP(io)) {
1381 return (PL_laststatval = PerlLIO_fstat(my_dirfd(IoDIRP(io)), &PL_statcache));
1384 PL_laststatval = -1;
1388 else if ((PL_op->op_private & (OPpFT_STACKED|OPpFT_AFTER_t))
1390 return PL_laststatval;
1392 SV* const sv = TOPs;
1395 if ((gv = MAYBE_DEREF_GV_flags(sv,flags))) {
1398 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
1399 io = MUTABLE_IO(SvRV(sv));
1401 goto do_fstat_have_io;
1404 s = SvPV_flags_const(sv, len, flags);
1406 sv_setpvn(PL_statname, s, len);
1407 s = SvPVX_const(PL_statname); /* s now NUL-terminated */
1408 PL_laststype = OP_STAT;
1409 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1410 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && should_warn_nl(s)) {
1411 GCC_DIAG_IGNORE(-Wformat-nonliteral); /* PL_warn_nl is constant */
1412 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
1415 return PL_laststatval;
1421 Perl_my_lstat_flags(pTHX_ const U32 flags)
1424 static const char* const no_prev_lstat = "The stat preceding -l _ wasn't an lstat";
1427 SV* const sv = TOPs;
1429 if (PL_op->op_flags & OPf_REF) {
1430 if (cGVOP_gv == PL_defgv) {
1431 if (PL_laststype != OP_LSTAT)
1432 Perl_croak(aTHX_ "%s", no_prev_lstat);
1433 return PL_laststatval;
1435 PL_laststatval = -1;
1436 if (ckWARN(WARN_IO)) {
1437 /* diag_listed_as: Use of -l on filehandle%s */
1438 Perl_warner(aTHX_ packWARN(WARN_IO),
1439 "Use of -l on filehandle %"HEKf,
1440 HEKfARG(GvENAME_HEK(cGVOP_gv)));
1444 if ((PL_op->op_private & (OPpFT_STACKED|OPpFT_AFTER_t))
1446 if (PL_laststype != OP_LSTAT)
1447 Perl_croak(aTHX_ "%s", no_prev_lstat);
1448 return PL_laststatval;
1451 PL_laststype = OP_LSTAT;
1453 if ( ( (SvROK(sv) && ( isGV_with_GP(SvRV(sv))
1454 || (isio = SvTYPE(SvRV(sv)) == SVt_PVIO) )
1458 && ckWARN(WARN_IO)) {
1460 /* diag_listed_as: Use of -l on filehandle%s */
1461 Perl_warner(aTHX_ packWARN(WARN_IO),
1462 "Use of -l on filehandle");
1464 /* diag_listed_as: Use of -l on filehandle%s */
1465 Perl_warner(aTHX_ packWARN(WARN_IO),
1466 "Use of -l on filehandle %"HEKf,
1467 GvENAME_HEK((const GV *)
1468 (SvROK(sv) ? SvRV(sv) : sv)));
1470 file = SvPV_flags_const_nolen(sv, flags);
1471 sv_setpv(PL_statname,file);
1472 PL_laststatval = PerlLIO_lstat(file,&PL_statcache);
1473 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && should_warn_nl(file)) {
1474 GCC_DIAG_IGNORE(-Wformat-nonliteral); /* PL_warn_nl is constant */
1475 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "lstat");
1478 return PL_laststatval;
1482 S_exec_failed(pTHX_ const char *cmd, int fd, int do_report)
1484 const int e = errno;
1485 PERL_ARGS_ASSERT_EXEC_FAILED;
1486 if (ckWARN(WARN_EXEC))
1487 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
1490 int rc = PerlLIO_write(fd, (void*)&e, sizeof(int));
1491 /* silently ignore failures */
1492 PERL_UNUSED_VAR(rc);
1498 Perl_do_aexec5(pTHX_ SV *really, SV **mark, SV **sp,
1499 int fd, int do_report)
1502 PERL_ARGS_ASSERT_DO_AEXEC5;
1503 #if defined(__SYMBIAN32__) || defined(__LIBCATAMOUNT__)
1504 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1508 const char *tmps = NULL;
1509 Newx(PL_Argv, sp - mark + 1, const char*);
1512 while (++mark <= sp) {
1514 *a++ = SvPV_nolen_const(*mark);
1520 tmps = SvPV_nolen_const(really);
1521 if ((!really && *PL_Argv[0] != '/') ||
1522 (really && *tmps != '/')) /* will execvp use PATH? */
1523 TAINT_ENV(); /* testing IFS here is overkill, probably */
1525 if (really && *tmps)
1526 PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
1528 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1530 S_exec_failed(aTHX_ (really ? tmps : PL_Argv[0]), fd, do_report);
1538 Perl_do_execfree(pTHX)
1547 #ifdef PERL_DEFAULT_DO_EXEC3_IMPLEMENTATION
1550 Perl_do_exec3(pTHX_ const char *incmd, int fd, int do_report)
1557 /* Make a copy so we can change it */
1558 const Size_t cmdlen = strlen(incmd) + 1;
1560 PERL_ARGS_ASSERT_DO_EXEC3;
1562 Newx(buf, cmdlen, char);
1564 memcpy(cmd, incmd, cmdlen);
1566 while (*cmd && isSPACE(*cmd))
1569 /* save an extra exec if possible */
1573 char flags[PERL_FLAGS_MAX];
1574 if (strnEQ(cmd,PL_cshname,PL_cshlen) &&
1575 strnEQ(cmd+PL_cshlen," -c",3)) {
1576 my_strlcpy(flags, "-c", PERL_FLAGS_MAX);
1577 s = cmd+PL_cshlen+3;
1580 my_strlcat(flags, "f", PERL_FLAGS_MAX - 2);
1585 char * const ncmd = s;
1591 if (s[-1] == '\'') {
1594 PerlProc_execl(PL_cshname, "csh", flags, ncmd, (char*)NULL);
1597 S_exec_failed(aTHX_ PL_cshname, fd, do_report);
1606 /* see if there are shell metacharacters in it */
1608 if (*cmd == '.' && isSPACE(cmd[1]))
1611 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1615 while (isWORDCHAR(*s))
1616 s++; /* catch VAR=val gizmo */
1620 for (s = cmd; *s; s++) {
1621 if (*s != ' ' && !isALPHA(*s) &&
1622 strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1623 if (*s == '\n' && !s[1]) {
1627 /* handle the 2>&1 construct at the end */
1628 if (*s == '>' && s[1] == '&' && s[2] == '1'
1629 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1630 && (!s[3] || isSPACE(s[3])))
1632 const char *t = s + 3;
1634 while (*t && isSPACE(*t))
1636 if (!*t && (PerlLIO_dup2(1,2) != -1)) {
1643 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char *)NULL);
1645 S_exec_failed(aTHX_ PL_sh_path, fd, do_report);
1651 Newx(PL_Argv, (s - cmd) / 2 + 2, const char*);
1652 PL_Cmd = savepvn(cmd, s-cmd);
1654 for (s = PL_Cmd; *s;) {
1659 while (*s && !isSPACE(*s))
1667 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1669 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1673 S_exec_failed(aTHX_ PL_Argv[0], fd, do_report);
1680 #endif /* OS2 || WIN32 */
1683 #include <starlet.h> /* for sys$delprc */
1687 Perl_apply(pTHX_ I32 type, SV **mark, SV **sp)
1692 const char *const what = PL_op_name[type];
1695 SV ** const oldmark = mark;
1696 bool killgp = FALSE;
1698 PERL_ARGS_ASSERT_APPLY;
1700 PERL_UNUSED_VAR(what); /* may not be used depending on compile options */
1702 /* Doing this ahead of the switch statement preserves the old behaviour,
1703 where attempting to use kill as a taint test test would fail on
1704 platforms where kill was not defined. */
1706 if (type == OP_KILL)
1707 Perl_die(aTHX_ PL_no_func, what);
1710 if (type == OP_CHOWN)
1711 Perl_die(aTHX_ PL_no_func, what);
1715 #define APPLY_TAINT_PROPER() \
1717 if (TAINT_get) { TAINT_PROPER(what); } \
1720 /* This is a first heuristic; it doesn't catch tainting magic. */
1722 while (++mark <= sp) {
1723 if (SvTAINTED(*mark)) {
1732 APPLY_TAINT_PROPER();
1735 APPLY_TAINT_PROPER();
1737 while (++mark <= sp) {
1739 if ((gv = MAYBE_DEREF_GV(*mark))) {
1740 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1742 APPLY_TAINT_PROPER();
1743 if (fchmod(PerlIO_fileno(IoIFP(GvIOn(gv))), val))
1746 Perl_die(aTHX_ PL_no_func, "fchmod");
1754 const char *name = SvPV_nomg_const(*mark, len);
1755 APPLY_TAINT_PROPER();
1756 if (!IS_SAFE_PATHNAME(name, len, "chmod") ||
1757 PerlLIO_chmod(name, val)) {
1766 APPLY_TAINT_PROPER();
1767 if (sp - mark > 2) {
1769 val = SvIVx(*++mark);
1770 val2 = SvIVx(*++mark);
1771 APPLY_TAINT_PROPER();
1773 while (++mark <= sp) {
1775 if ((gv = MAYBE_DEREF_GV(*mark))) {
1776 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1778 APPLY_TAINT_PROPER();
1779 if (fchown(PerlIO_fileno(IoIFP(GvIOn(gv))), val, val2))
1782 Perl_die(aTHX_ PL_no_func, "fchown");
1790 const char *name = SvPV_nomg_const(*mark, len);
1791 APPLY_TAINT_PROPER();
1792 if (!IS_SAFE_PATHNAME(name, len, "chown") ||
1793 PerlLIO_chown(name, val, val2)) {
1802 XXX Should we make lchown() directly available from perl?
1803 For now, we'll let Configure test for HAS_LCHOWN, but do
1804 nothing in the core.
1809 APPLY_TAINT_PROPER();
1812 s = SvPVx_const(*++mark, len);
1813 if (*s == '-' && isALPHA(s[1]))
1820 if (*s == 'S' && s[1] == 'I' && s[2] == 'G') {
1824 if ((val = whichsig_pvn(s, len)) < 0)
1825 Perl_croak(aTHX_ "Unrecognized signal name \"%"SVf"\"", SVfARG(*mark));
1836 APPLY_TAINT_PROPER();
1839 /* kill() doesn't do process groups (job trees?) under VMS */
1840 if (val == SIGKILL) {
1841 /* Use native sys$delprc() to insure that target process is
1842 * deleted; supervisor-mode images don't pay attention to
1843 * CRTL's emulation of Unix-style signals and kill()
1845 while (++mark <= sp) {
1847 unsigned long int __vmssts;
1849 if (!(SvIOK(*mark) || SvNOK(*mark) || looks_like_number(*mark)))
1850 Perl_croak(aTHX_ "Can't kill a non-numeric process ID");
1851 proc = SvIV_nomg(*mark);
1852 APPLY_TAINT_PROPER();
1853 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1857 case SS$_NOSUCHNODE:
1858 SETERRNO(ESRCH,__vmssts);
1861 SETERRNO(EPERM,__vmssts);
1864 SETERRNO(EVMSERR,__vmssts);
1872 while (++mark <= sp) {
1875 if (!(SvNIOK(*mark) || looks_like_number(*mark)))
1876 Perl_croak(aTHX_ "Can't kill a non-numeric process ID");
1877 proc = SvIV_nomg(*mark);
1878 APPLY_TAINT_PROPER();
1880 /* use killpg in preference, as the killpg() wrapper for Win32
1881 * understands process groups, but the kill() wrapper doesn't */
1882 if (killgp ? PerlProc_killpg(proc, val)
1883 : PerlProc_kill(proc, val))
1885 if (PerlProc_kill(killgp ? -proc: proc, val))
1893 APPLY_TAINT_PROPER();
1895 while (++mark <= sp) {
1896 s = SvPV_const(*mark, len);
1897 APPLY_TAINT_PROPER();
1898 if (!IS_SAFE_PATHNAME(s, len, "unlink")) {
1901 else if (PL_unsafe) {
1905 else { /* don't let root wipe out directories without -U */
1906 if (PerlLIO_lstat(s,&PL_statbuf) < 0)
1908 else if (S_ISDIR(PL_statbuf.st_mode)) {
1910 SETERRNO(EISDIR, SS_NOPRIV);
1919 #if defined(HAS_UTIME) || defined(HAS_FUTIMES)
1921 APPLY_TAINT_PROPER();
1922 if (sp - mark > 2) {
1923 #if defined(HAS_FUTIMES)
1924 struct timeval utbuf[2];
1925 void *utbufp = utbuf;
1926 #elif defined(I_UTIME) || defined(VMS)
1927 struct utimbuf utbuf;
1928 struct utimbuf *utbufp = &utbuf;
1934 void *utbufp = &utbuf;
1937 SV* const accessed = *++mark;
1938 SV* const modified = *++mark;
1940 /* Be like C, and if both times are undefined, let the C
1941 * library figure out what to do. This usually means
1942 * "current time". */
1944 if ( accessed == &PL_sv_undef && modified == &PL_sv_undef )
1947 Zero(&utbuf, sizeof utbuf, char);
1949 utbuf[0].tv_sec = (long)SvIV(accessed); /* time accessed */
1950 utbuf[0].tv_usec = 0;
1951 utbuf[1].tv_sec = (long)SvIV(modified); /* time modified */
1952 utbuf[1].tv_usec = 0;
1953 #elif defined(BIG_TIME)
1954 utbuf.actime = (Time_t)SvNV(accessed); /* time accessed */
1955 utbuf.modtime = (Time_t)SvNV(modified); /* time modified */
1957 utbuf.actime = (Time_t)SvIV(accessed); /* time accessed */
1958 utbuf.modtime = (Time_t)SvIV(modified); /* time modified */
1961 APPLY_TAINT_PROPER();
1963 while (++mark <= sp) {
1965 if ((gv = MAYBE_DEREF_GV(*mark))) {
1966 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1968 APPLY_TAINT_PROPER();
1969 if (futimes(PerlIO_fileno(IoIFP(GvIOn(gv))),
1970 (struct timeval *) utbufp))
1973 Perl_die(aTHX_ PL_no_func, "futimes");
1981 const char * const name = SvPV_nomg_const(*mark, len);
1982 APPLY_TAINT_PROPER();
1983 if (!IS_SAFE_PATHNAME(name, len, "utime")) {
1988 if (utimes(name, (struct timeval *)utbufp))
1990 if (PerlLIO_utime(name, utbufp))
2004 #undef APPLY_TAINT_PROPER
2007 /* Do the permissions allow some operation? Assumes statcache already set. */
2008 #ifndef VMS /* VMS' cando is in vms.c */
2010 Perl_cando(pTHX_ Mode_t mode, bool effective, const Stat_t *statbufp)
2011 /* effective is a flag, true for EUID, or for checking if the effective gid
2012 * is in the list of groups returned from getgroups().
2017 PERL_ARGS_ASSERT_CANDO;
2020 /* [Comments and code from Len Reed]
2021 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
2022 * to write-protected files. The execute permission bit is set
2023 * by the Microsoft C library stat() function for the following:
2028 * All files and directories are readable.
2029 * Directories and special files, e.g. "CON", cannot be
2031 * [Comment by Tom Dinger -- a directory can have the write-protect
2032 * bit set in the file system, but DOS permits changes to
2033 * the directory anyway. In addition, all bets are off
2034 * here for networked software, such as Novell and
2038 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
2039 * too so it will actually look into the files for magic numbers
2041 return (mode & statbufp->st_mode) ? TRUE : FALSE;
2043 #else /* ! DOSISH */
2045 if (ingroup(544,effective)) { /* member of Administrators */
2047 if ((effective ? PerlProc_geteuid() : PerlProc_getuid()) == 0) { /* root is special */
2049 if (mode == S_IXUSR) {
2050 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
2054 return TRUE; /* root reads and writes anything */
2057 if (statbufp->st_uid == (effective ? PerlProc_geteuid() : PerlProc_getuid()) ) {
2058 if (statbufp->st_mode & mode)
2059 return TRUE; /* ok as "user" */
2061 else if (ingroup(statbufp->st_gid,effective)) {
2062 if (statbufp->st_mode & mode >> 3)
2063 return TRUE; /* ok as "group" */
2065 else if (statbufp->st_mode & mode >> 6)
2066 return TRUE; /* ok as "other" */
2068 #endif /* ! DOSISH */
2073 S_ingroup(pTHX_ Gid_t testgid, bool effective)
2076 if (testgid == (effective ? PerlProc_getegid() : PerlProc_getgid()))
2078 #ifdef HAS_GETGROUPS
2080 Groups_t *gary = NULL;
2084 anum = getgroups(0, gary);
2085 Newx(gary, anum, Groups_t);
2086 anum = getgroups(anum, gary);
2088 if (gary[anum] == testgid) {
2101 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
2104 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
2107 const key_t key = (key_t)SvNVx(*++mark);
2108 SV *nsv = optype == OP_MSGGET ? NULL : *++mark;
2109 const I32 flags = SvIVx(*++mark);
2111 PERL_ARGS_ASSERT_DO_IPCGET;
2112 PERL_UNUSED_ARG(sp);
2119 return msgget(key, flags);
2123 return semget(key, (int) SvIV(nsv), flags);
2127 return shmget(key, (size_t) SvUV(nsv), flags);
2129 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
2131 /* diag_listed_as: msg%s not implemented */
2132 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2135 return -1; /* should never happen */
2139 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
2144 const I32 id = SvIVx(*++mark);
2146 const I32 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
2148 const I32 cmd = SvIVx(*++mark);
2149 SV * const astr = *++mark;
2150 STRLEN infosize = 0;
2151 I32 getinfo = (cmd == IPC_STAT);
2153 PERL_ARGS_ASSERT_DO_IPCCTL;
2154 PERL_UNUSED_ARG(sp);
2160 if (cmd == IPC_STAT || cmd == IPC_SET)
2161 infosize = sizeof(struct msqid_ds);
2166 if (cmd == IPC_STAT || cmd == IPC_SET)
2167 infosize = sizeof(struct shmid_ds);
2173 if (cmd == IPC_STAT || cmd == IPC_SET)
2174 infosize = sizeof(struct semid_ds);
2175 else if (cmd == GETALL || cmd == SETALL)
2177 struct semid_ds semds;
2179 #ifdef EXTRA_F_IN_SEMUN_BUF
2180 semun.buff = &semds;
2184 getinfo = (cmd == GETALL);
2185 if (Semctl(id, 0, IPC_STAT, semun) == -1)
2187 infosize = semds.sem_nsems * sizeof(short);
2188 /* "short" is technically wrong but much more portable
2189 than guessing about u_?short(_t)? */
2192 /* diag_listed_as: sem%s not implemented */
2193 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2197 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
2199 /* diag_listed_as: shm%s not implemented */
2200 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2208 SvPV_force_nolen(astr);
2209 a = SvGROW(astr, infosize+1);
2214 a = SvPV(astr, len);
2215 if (len != infosize)
2216 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
2224 const IV i = SvIV(astr);
2225 a = INT2PTR(char *,i); /* ouch */
2232 ret = msgctl(id, cmd, (struct msqid_ds *)a);
2238 union semun unsemds;
2241 unsemds.val = PTR2nat(a);
2244 #ifdef EXTRA_F_IN_SEMUN_BUF
2245 unsemds.buff = (struct semid_ds *)a;
2247 unsemds.buf = (struct semid_ds *)a;
2250 ret = Semctl(id, n, cmd, unsemds);
2252 /* diag_listed_as: sem%s not implemented */
2253 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2260 ret = shmctl(id, cmd, (struct shmid_ds *)a);
2264 if (getinfo && ret >= 0) {
2265 SvCUR_set(astr, infosize);
2266 *SvEND(astr) = '\0';
2273 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
2278 const I32 id = SvIVx(*++mark);
2279 SV * const mstr = *++mark;
2280 const I32 flags = SvIVx(*++mark);
2281 const char * const mbuf = SvPV_const(mstr, len);
2282 const I32 msize = len - sizeof(long);
2284 PERL_ARGS_ASSERT_DO_MSGSND;
2285 PERL_UNUSED_ARG(sp);
2288 Perl_croak(aTHX_ "Arg too short for msgsnd");
2290 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
2292 PERL_UNUSED_ARG(sp);
2293 PERL_UNUSED_ARG(mark);
2294 /* diag_listed_as: msg%s not implemented */
2295 Perl_croak(aTHX_ "msgsnd not implemented");
2301 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
2307 I32 msize, flags, ret;
2308 const I32 id = SvIVx(*++mark);
2309 SV * const mstr = *++mark;
2311 PERL_ARGS_ASSERT_DO_MSGRCV;
2312 PERL_UNUSED_ARG(sp);
2314 /* suppress warning when reading into undef var --jhi */
2316 sv_setpvs(mstr, "");
2317 msize = SvIVx(*++mark);
2318 mtype = (long)SvIVx(*++mark);
2319 flags = SvIVx(*++mark);
2320 SvPV_force_nolen(mstr);
2321 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
2324 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
2326 SvCUR_set(mstr, sizeof(long)+ret);
2327 *SvEND(mstr) = '\0';
2328 /* who knows who has been playing with this message? */
2333 PERL_UNUSED_ARG(sp);
2334 PERL_UNUSED_ARG(mark);
2335 /* diag_listed_as: msg%s not implemented */
2336 Perl_croak(aTHX_ "msgrcv not implemented");
2342 Perl_do_semop(pTHX_ SV **mark, SV **sp)
2347 const I32 id = SvIVx(*++mark);
2348 SV * const opstr = *++mark;
2349 const char * const opbuf = SvPV_const(opstr, opsize);
2351 PERL_ARGS_ASSERT_DO_SEMOP;
2352 PERL_UNUSED_ARG(sp);
2354 if (opsize < 3 * SHORTSIZE
2355 || (opsize % (3 * SHORTSIZE))) {
2356 SETERRNO(EINVAL,LIB_INVARG);
2360 /* We can't assume that sizeof(struct sembuf) == 3 * sizeof(short). */
2362 const int nsops = opsize / (3 * sizeof (short));
2364 short * const ops = (short *) opbuf;
2366 struct sembuf *temps, *t;
2369 Newx (temps, nsops, struct sembuf);
2377 result = semop(id, temps, nsops);
2382 /* diag_listed_as: sem%s not implemented */
2383 Perl_croak(aTHX_ "semop not implemented");
2388 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
2393 struct shmid_ds shmds;
2394 const I32 id = SvIVx(*++mark);
2395 SV * const mstr = *++mark;
2396 const I32 mpos = SvIVx(*++mark);
2397 const I32 msize = SvIVx(*++mark);
2399 PERL_ARGS_ASSERT_DO_SHMIO;
2400 PERL_UNUSED_ARG(sp);
2403 if (shmctl(id, IPC_STAT, &shmds) == -1)
2405 if (mpos < 0 || msize < 0
2406 || (size_t)mpos + msize > (size_t)shmds.shm_segsz) {
2407 SETERRNO(EFAULT,SS_ACCVIO); /* can't do as caller requested */
2410 shm = (char *)shmat(id, NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
2411 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2413 if (optype == OP_SHMREAD) {
2415 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2417 SvUPGRADE(mstr, SVt_PV);
2419 sv_setpvs(mstr, "");
2421 mbuf = SvGROW(mstr, (STRLEN)msize+1);
2423 Copy(shm + mpos, mbuf, msize, char);
2424 SvCUR_set(mstr, msize);
2425 *SvEND(mstr) = '\0';
2427 /* who knows who has been playing with this shared memory? */
2433 const char *mbuf = SvPV_const(mstr, len);
2434 const I32 n = ((I32)len > msize) ? msize : (I32)len;
2435 Copy(mbuf, shm + mpos, n, char);
2437 memzero(shm + mpos + n, msize - n);
2441 /* diag_listed_as: shm%s not implemented */
2442 Perl_croak(aTHX_ "shm I/O not implemented");
2447 #endif /* SYSV IPC */
2452 =for apidoc start_glob
2454 Function called by C<do_readline> to spawn a glob (or do the glob inside
2455 perl on VMS). This code used to be inline, but now perl uses C<File::Glob>
2456 this glob starter is only used by miniperl during the build process.
2457 Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up.
2463 Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
2466 SV * const tmpcmd = newSV(0);
2469 const char *s = SvPV(tmpglob, len);
2471 PERL_ARGS_ASSERT_START_GLOB;
2473 if (!IS_SAFE_SYSCALL(s, len, "pattern", "glob"))
2478 #ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
2479 /* since spawning off a process is a real performance hit */
2486 fp = Perl_vms_start_glob(aTHX_ tmpglob, io);
2491 sv_setpv(tmpcmd, "for a in ");
2492 sv_catsv(tmpcmd, tmpglob);
2493 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
2496 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
2497 sv_catsv(tmpcmd, tmpglob);
2499 sv_setpv(tmpcmd, "perlglob ");
2500 sv_catsv(tmpcmd, tmpglob);
2501 sv_catpv(tmpcmd, " |");
2506 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
2507 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
2508 sv_catsv(tmpcmd, tmpglob);
2509 sv_catpv(tmpcmd, "' 2>/dev/null |");
2511 sv_setpv(tmpcmd, "echo ");
2512 sv_catsv(tmpcmd, tmpglob);
2513 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
2515 #endif /* !DOSISH */
2517 GV * const envgv = gv_fetchpvs("ENV", 0, SVt_PVHV);
2518 SV ** const home = hv_fetchs(GvHV(envgv), "HOME", 0);
2519 SV ** const path = hv_fetchs(GvHV(envgv), "PATH", 0);
2520 if (home && *home) SvGETMAGIC(*home);
2521 if (path && *path) SvGETMAGIC(*path);
2522 save_hash(gv_fetchpvs("ENV", 0, SVt_PVHV));
2523 if (home && *home) SvSETMAGIC(*home);
2524 if (path && *path) SvSETMAGIC(*path);
2526 (void)do_open6(PL_last_in_gv, SvPVX_const(tmpcmd), SvCUR(tmpcmd),
2532 if (!fp && ckWARN(WARN_GLOB)) {
2533 Perl_warner(aTHX_ packWARN(WARN_GLOB), "glob failed (can't start child: %s)",
2542 * c-indentation-style: bsd
2544 * indent-tabs-mode: nil
2547 * ex: set ts=8 sts=4 sw=4 et: