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
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)
152 char mode[PERL_MODE_MAX]; /* file mode ("r\0", "rb\0", "ab\0" etc.) */
153 IO * const io = openn_setup(gv, mode, &saveifp, &saveofp, &savefd, &savetype);
157 PERL_ARGS_ASSERT_DO_OPEN_RAW;
159 /* For ease of blame back to 5.000, keep the existing indenting. */
161 /* sysopen style args, i.e. integer mode and permissions */
163 const int appendtrunc =
165 #ifdef O_APPEND /* Not fully portable. */
168 #ifdef O_TRUNC /* Not fully portable. */
172 const int modifyingmode = O_WRONLY|O_RDWR|O_CREAT|appendtrunc;
182 It might be (in OS/390 and Mac OS Classic it is)
188 This means that simple & with O_RDWR would look
189 like O_RDONLY is present. Therefore we have to
192 if ((ismodifying = (rawmode & modifyingmode))) {
193 if ((ismodifying & O_WRONLY) == O_WRONLY ||
194 (ismodifying & O_RDWR) == O_RDWR ||
195 (ismodifying & (O_CREAT|appendtrunc)))
196 TAINT_PROPER("sysopen");
198 mode[ix++] = IoTYPE_NUMERIC; /* Marker to openn to use numeric "sysopen" */
200 #if defined(USE_64_BIT_RAWIO) && defined(O_LARGEFILE)
201 rawmode |= O_LARGEFILE; /* Transparently largefiley. */
204 IoTYPE(io) = PerlIO_intmode2str(rawmode, &mode[ix], &writing);
206 namesv = newSVpvn_flags(oname, len, SVs_TEMP);
207 fp = PerlIO_openn(aTHX_ NULL, mode, -1, rawmode, rawperm, NULL, 1, &namesv);
209 return openn_cleanup(gv, io, fp, mode, oname, saveifp, saveofp, savefd,
210 savetype, writing, 0, NULL);
214 Perl_do_open6(pTHX_ GV *gv, const char *oname, STRLEN len,
215 PerlIO *supplied_fp, SV **svp, U32 num_svs)
221 char mode[PERL_MODE_MAX]; /* file mode ("r\0", "rb\0", "ab\0" etc.) */
222 IO * const io = openn_setup(gv, mode, &saveifp, &saveofp, &savefd, &savetype);
225 bool was_fdopen = FALSE;
228 PERL_ARGS_ASSERT_DO_OPEN6;
230 /* For ease of blame back to 5.000, keep the existing indenting. */
232 /* Regular (non-sys) open */
237 bool in_raw = 0, in_crlf = 0, out_raw = 0, out_crlf = 0;
239 /* Collect default raw/crlf info from the op */
240 if (PL_op && PL_op->op_type == OP_OPEN) {
241 /* set up IO layers */
242 const U8 flags = PL_op->op_private;
243 in_raw = (flags & OPpOPEN_IN_RAW);
244 in_crlf = (flags & OPpOPEN_IN_CRLF);
245 out_raw = (flags & OPpOPEN_OUT_RAW);
246 out_crlf = (flags & OPpOPEN_OUT_CRLF);
249 type = savepvn(oname, len);
253 /* Lose leading and trailing white space */
254 while (isSPACE(*type))
256 while (tend > type && isSPACE(tend[-1]))
262 /* New style explicit name, type is just mode and layer info */
264 if (SvROK(*svp) && !strchr(oname,'&')) {
266 Perl_warner(aTHX_ packWARN(WARN_IO),
267 "Can't open a reference");
268 SETERRNO(EINVAL, LIB_INVARG);
272 #endif /* USE_STDIO */
273 p = (SvOK(*svp) || SvGMAGICAL(*svp)) ? SvPV(*svp, nlen) : NULL;
275 if (p && !IS_SAFE_PATHNAME(p, nlen, "open")) {
280 name = p ? savepvn(p, nlen) : savepvs("");
289 if ((*type == IoTYPE_RDWR) && /* scary */
290 (*(type+1) == IoTYPE_RDONLY || *(type+1) == IoTYPE_WRONLY) &&
291 ((!num_svs || (tend > type+1 && tend[-1] != IoTYPE_PIPE)))) {
292 TAINT_PROPER("open");
297 if (*type == IoTYPE_PIPE) {
299 if (type[1] != IoTYPE_STD) {
301 Perl_croak(aTHX_ "Unknown open() mode '%.*s'", (int)olen, oname);
307 } while (isSPACE(*type));
313 /* command is missing 19990114 */
314 if (ckWARN(WARN_PIPE))
315 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open");
320 if (!(*name == '-' && name[1] == '\0') || num_svs)
322 TAINT_PROPER("piped open");
323 if (!num_svs && name[len-1] == '|') {
325 if (ckWARN(WARN_PIPE))
326 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Can't open bidirectional pipe");
335 fp = PerlProc_popen_list(mode, num_svs, svp);
338 fp = PerlProc_popen(name,mode);
342 if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) {
349 else if (*type == IoTYPE_WRONLY) {
350 TAINT_PROPER("open");
352 if (*type == IoTYPE_WRONLY) {
353 /* Two IoTYPE_WRONLYs in a row make for an IoTYPE_APPEND. */
354 mode[0] = IoTYPE(io) = IoTYPE_APPEND;
368 dodup = PERLIO_DUP_FD;
374 if (!num_svs && !*type && supplied_fp) {
375 /* "<+&" etc. is used by typemaps */
379 PerlIO *that_fp = NULL;
383 /* diag_listed_as: More than one argument to '%s' open */
384 Perl_croak(aTHX_ "More than one argument to '%c&' open",IoTYPE(io));
386 while (isSPACE(*type))
390 || (SvPOKp(*svp) && looks_like_number(*svp))
392 wanted_fd = SvUV(*svp);
395 else if (isDIGIT(*type)
396 && grok_atoUV(type, &uv, NULL)
404 thatio = sv_2io(*svp);
407 GV * const thatgv = gv_fetchpvn_flags(type, tend - type,
409 thatio = GvIO(thatgv);
413 SETERRNO(EINVAL,SS_IVCHAN);
418 if ((that_fp = IoIFP(thatio))) {
419 /* Flush stdio buffer before dup. --mjd
420 * Unfortunately SEEK_CURing 0 seems to
421 * be optimized away on most platforms;
422 * only Solaris and Linux seem to flush
424 /* On the other hand, do all platforms
425 * take gracefully to flushing a read-only
426 * filehandle? Perhaps we should do
427 * fsetpos(src)+fgetpos(dst)? --nik */
428 PerlIO_flush(that_fp);
429 wanted_fd = PerlIO_fileno(that_fp);
430 /* When dup()ing STDIN, STDOUT or STDERR
431 * explicitly set appropriate access mode */
432 if (that_fp == PerlIO_stdout()
433 || that_fp == PerlIO_stderr())
434 IoTYPE(io) = IoTYPE_WRONLY;
435 else if (that_fp == PerlIO_stdin())
436 IoTYPE(io) = IoTYPE_RDONLY;
437 /* When dup()ing a socket, say result is
439 else if (IoTYPE(thatio) == IoTYPE_SOCKET)
440 IoTYPE(io) = IoTYPE_SOCKET;
443 SETERRNO(EBADF, RMS_IFI);
451 fp = PerlIO_fdupopen(aTHX_ that_fp, NULL, dodup);
455 wanted_fd = PerlLIO_dup(wanted_fd);
458 if (!(fp = PerlIO_openn(aTHX_ type,mode,wanted_fd,0,0,NULL,num_svs,svp))) {
459 if (dodup && wanted_fd >= 0)
460 PerlLIO_close(wanted_fd);
466 while (isSPACE(*type))
468 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
470 fp = PerlIO_stdout();
471 IoTYPE(io) = IoTYPE_STD;
473 /* diag_listed_as: More than one argument to '%s' open */
474 Perl_croak(aTHX_ "More than one argument to '>%c' open",IoTYPE_STD);
479 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
482 SV *namesv = newSVpvn_flags(type, tend - type, SVs_TEMP);
484 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,1,&namesv);
488 if (!fp && type && *type && *type != ':' && !isIDFIRST(*type))
489 goto unknown_open_mode;
490 } /* IoTYPE_WRONLY */
491 else if (*type == IoTYPE_RDONLY) {
494 } while (isSPACE(*type));
503 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
506 IoTYPE(io) = IoTYPE_STD;
508 /* diag_listed_as: More than one argument to '%s' open */
509 Perl_croak(aTHX_ "More than one argument to '<%c' open",IoTYPE_STD);
514 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
517 SV *namesv = newSVpvn_flags(type, tend - type, SVs_TEMP);
519 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,1,&namesv);
522 if (!fp && type && *type && *type != ':' && !isIDFIRST(*type))
523 goto unknown_open_mode;
524 } /* IoTYPE_RDONLY */
525 else if ((num_svs && /* '-|...' or '...|' */
526 type[0] == IoTYPE_STD && type[1] == IoTYPE_PIPE) ||
527 (!num_svs && tend > type+1 && tend[-1] == IoTYPE_PIPE)) {
529 type += 2; /* skip over '-|' */
533 while (tend > type && isSPACE(tend[-1]))
535 for (; isSPACE(*type); type++)
541 /* command is missing 19990114 */
542 if (ckWARN(WARN_PIPE))
543 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open");
548 if (!(*name == '-' && name[1] == '\0') || num_svs)
550 TAINT_PROPER("piped open");
559 fp = PerlProc_popen_list(mode,num_svs,svp);
562 fp = PerlProc_popen(name,mode);
564 IoTYPE(io) = IoTYPE_PIPE;
566 while (isSPACE(*type))
569 if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) {
576 else { /* layer(Args) */
578 goto unknown_open_mode;
580 IoTYPE(io) = IoTYPE_RDONLY;
581 for (; isSPACE(*name); name++)
590 if (*name == '-' && name[1] == '\0') {
592 IoTYPE(io) = IoTYPE_STD;
596 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
599 SV *namesv = newSVpvn_flags(type, tend - type, SVs_TEMP);
601 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,1,&namesv);
608 return openn_cleanup(gv, io, fp, mode, oname, saveifp, saveofp, savefd,
609 savetype, writing, was_fdopen, type);
612 /* Yes, this is ugly, but it's private, and I don't see a cleaner way to
613 simplify the two-headed public interface of do_openn. */
615 S_openn_cleanup(pTHX_ GV *gv, IO *io, PerlIO *fp, char *mode, const char *oname,
616 PerlIO *saveifp, PerlIO *saveofp, int savefd, char savetype,
617 int writing, bool was_fdopen, const char *type)
621 PERL_ARGS_ASSERT_OPENN_CLEANUP;
624 if (IoTYPE(io) == IoTYPE_RDONLY && ckWARN(WARN_NEWLINE)
625 && should_warn_nl(oname)
629 GCC_DIAG_IGNORE(-Wformat-nonliteral); /* PL_warn_nl is constant */
630 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
636 if (ckWARN(WARN_IO)) {
637 if ((IoTYPE(io) == IoTYPE_RDONLY) &&
638 (fp == PerlIO_stdout() || fp == PerlIO_stderr())) {
639 Perl_warner(aTHX_ packWARN(WARN_IO),
640 "Filehandle STD%s reopened as %" HEKf
642 ((fp == PerlIO_stdout()) ? "OUT" : "ERR"),
643 HEKfARG(GvENAME_HEK(gv)));
645 else if ((IoTYPE(io) == IoTYPE_WRONLY) && fp == PerlIO_stdin()) {
646 Perl_warner(aTHX_ packWARN(WARN_IO),
647 "Filehandle STDIN reopened as %" HEKf " only for output",
648 HEKfARG(GvENAME_HEK(gv))
653 fd = PerlIO_fileno(fp);
654 /* Do NOT do: "if (fd < 0) goto say_false;" here. If there is no
655 * fd assume it isn't a socket - this covers PerlIO::scalar -
656 * otherwise unless we "know" the type probe for socket-ness.
658 if (IoTYPE(io) && IoTYPE(io) != IoTYPE_PIPE && IoTYPE(io) != IoTYPE_STD && fd >= 0) {
659 if (PerlLIO_fstat(fd,&PL_statbuf) < 0) {
660 /* If PerlIO claims to have fd we had better be able to fstat() it. */
661 (void) PerlIO_close(fp);
665 if (S_ISSOCK(PL_statbuf.st_mode))
666 IoTYPE(io) = IoTYPE_SOCKET; /* in case a socket was passed in to us */
669 !(PL_statbuf.st_mode & S_IFMT)
670 && IoTYPE(io) != IoTYPE_WRONLY /* Dups of STD* filehandles already have */
671 && IoTYPE(io) != IoTYPE_RDONLY /* type so they aren't marked as sockets */
672 ) { /* on OS's that return 0 on fstat()ed pipe */
674 Sock_size_t buflen = sizeof tmpbuf;
675 if (PerlSock_getsockname(fd, (struct sockaddr *)tmpbuf, &buflen) >= 0
676 || errno != ENOTSOCK)
677 IoTYPE(io) = IoTYPE_SOCKET; /* some OS's return 0 on fstat()ed socket */
678 /* but some return 0 for streams too, sigh */
680 #endif /* HAS_SOCKET */
681 #endif /* !PERL_MICRO */
685 * If this is a standard handle we discard all the layer stuff
686 * and just dup the fd into whatever was on the handle before !
689 if (saveifp) { /* must use old fp? */
690 /* If fd is less that PL_maxsysfd i.e. STDIN..STDERR
691 then dup the new fileno down
694 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
695 if (saveofp != saveifp) { /* was a socket? */
696 PerlIO_close(saveofp);
700 /* Still a small can-of-worms here if (say) PerlIO::scalar
701 is assigned to (say) STDOUT - for now let dup2() fail
702 and provide the error
705 SETERRNO(EBADF,RMS_IFI);
707 } else if (PerlLIO_dup2(fd, savefd) < 0) {
708 (void)PerlIO_close(fp);
712 if (savefd != PerlIO_fileno(PerlIO_stdin())) {
713 char newname[FILENAME_MAX+1];
714 if (PerlIO_getname(fp, newname)) {
715 if (fd == PerlIO_fileno(PerlIO_stdout()))
716 vmssetuserlnm("SYS$OUTPUT", newname);
717 if (fd == PerlIO_fileno(PerlIO_stderr()))
718 vmssetuserlnm("SYS$ERROR", newname);
724 /* PL_fdpid isn't used on Windows, so avoid this useless work.
725 * XXX Probably the same for a lot of other places. */
730 sv = *av_fetch(PL_fdpid,fd,TRUE);
731 SvUPGRADE(sv, SVt_IV);
734 sv = *av_fetch(PL_fdpid,savefd,TRUE);
735 SvUPGRADE(sv, SVt_IV);
741 /* need to close fp without closing underlying fd */
742 int ofd = PerlIO_fileno(fp);
743 int dupfd = ofd >= 0 ? PerlLIO_dup(ofd) : -1;
744 #if defined(HAS_FCNTL) && defined(F_SETFD)
745 /* Assume if we have F_SETFD we have F_GETFD. */
746 /* Get a copy of all the fd flags. */
747 int fd_flags = ofd >= 0 ? fcntl(ofd, F_GETFD) : -1;
750 PerlLIO_close(dupfd);
754 if (ofd < 0 || dupfd < 0) {
756 PerlLIO_close(dupfd);
760 PerlLIO_dup2(dupfd, ofd);
761 #if defined(HAS_FCNTL) && defined(F_SETFD)
762 /* The dup trick has lost close-on-exec on ofd,
763 * and possibly any other flags, so restore them. */
764 if (fcntl(ofd,F_SETFD, fd_flags) < 0) {
766 PerlLIO_close(dupfd);
770 PerlLIO_close(dupfd);
777 fd = PerlIO_fileno(fp);
779 #if defined(HAS_FCNTL) && defined(F_SETFD) && defined(FD_CLOEXEC)
780 if (fd >= 0 && fd > PL_maxsysfd && fcntl(fd, F_SETFD, FD_CLOEXEC) < 0) {
787 IoFLAGS(io) &= ~IOf_NOLINE;
789 if (IoTYPE(io) == IoTYPE_SOCKET
790 || (IoTYPE(io) == IoTYPE_WRONLY && fd >= 0 && S_ISCHR(PL_statbuf.st_mode)) ) {
792 if (*s == IoTYPE_IMPLICIT || *s == IoTYPE_NUMERIC)
795 if (!(IoOFP(io) = PerlIO_openn(aTHX_ type,s,fd,0,0,NULL,0,NULL))) {
808 IoTYPE(io) = savetype;
813 Perl_nextargv(pTHX_ GV *gv, bool nomagicopen)
815 IO * const io = GvIOp(gv);
816 SV *const old_out_name = PL_inplace ? newSVsv(GvSV(gv)) : NULL;
818 PERL_ARGS_ASSERT_NEXTARGV;
821 SAVEFREESV(old_out_name);
824 PL_argvoutgv = gv_fetchpvs("ARGVOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
825 if (io && (IoFLAGS(io) & (IOf_ARGV|IOf_START)) == (IOf_ARGV|IOf_START)) {
826 IoFLAGS(io) &= ~IOf_START;
829 Perl_av_create_and_push(aTHX_ &PL_argvout_stack,
830 SvREFCNT_inc_simple_NN(PL_defoutgv));
833 if (PL_filemode & (S_ISUID|S_ISGID)) {
834 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
837 (void)fchmod(PL_lastfd,PL_filemode);
839 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
846 while (av_tindex(GvAV(gv)) >= 0) {
849 SV *const sv = av_shift(GvAV(gv));
851 SvTAINTED_off(GvSVn(gv)); /* previous tainting irrelevant */
852 sv_setsv(GvSVn(gv),sv);
853 SvSETMAGIC(GvSV(gv));
854 PL_oldname = SvPVx(GvSV(gv), oldlen);
855 if (LIKELY(!PL_inplace)) {
857 ? do_open6(gv, "<", 1, NULL, &GvSV(gv), 1)
858 : do_open6(gv, PL_oldname, oldlen, NULL, NULL, 0)
860 return IoIFP(GvIOp(gv));
865 IO * const io = GvIOp(PL_argvoutgv);
866 if (io && IoIFP(io) && old_out_name && !io_close(io, PL_argvoutgv, FALSE, FALSE)) {
867 Perl_croak(aTHX_ "Failed to close in-place edit file %"
868 SVf ": %s\n", old_out_name, Strerror(errno));
871 /* This very long block ends with return IoIFP(GvIOp(gv));
872 Both this block and the block above fall through on open
873 failure to the warning code, and then the while loop above tries
875 if (do_open_raw(gv, PL_oldname, oldlen, O_RDONLY, 0)) {
876 #ifndef FLEXFILENAMES
883 TAINT_PROPER("inplace open");
884 if (oldlen == 1 && *PL_oldname == '-') {
885 setdefout(gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL,
887 return IoIFP(GvIOp(gv));
889 #ifndef FLEXFILENAMES
890 filedev = PL_statbuf.st_dev;
891 fileino = PL_statbuf.st_ino;
893 PL_filemode = PL_statbuf.st_mode;
894 fileuid = PL_statbuf.st_uid;
895 filegid = PL_statbuf.st_gid;
896 if (!S_ISREG(PL_filemode)) {
897 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
898 "Can't do inplace edit: %s is not a regular file",
903 if (*PL_inplace && strNE(PL_inplace, "*")) {
904 const char *star = strchr(PL_inplace, '*');
906 const char *begin = PL_inplace;
909 sv_catpvn(sv, begin, star - begin);
910 sv_catpvn(sv, PL_oldname, oldlen);
912 } while ((star = strchr(begin, '*')));
917 sv_catpv(sv,PL_inplace);
919 #ifndef FLEXFILENAMES
920 if ((PerlLIO_stat(SvPVX_const(sv),&PL_statbuf) >= 0
921 && PL_statbuf.st_dev == filedev
922 && PL_statbuf.st_ino == fileino)
924 || ((_djstat_fail_bits & _STFAIL_TRUENAME)!=0)
928 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
929 "Can't do inplace edit: %"
930 SVf " would not be unique",
937 #if !defined(DOSISH) && !defined(__CYGWIN__)
938 if (PerlLIO_rename(PL_oldname,SvPVX_const(sv)) < 0) {
939 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
940 "Can't rename %s to %" SVf
941 ": %s, skipping file",
942 PL_oldname, SVfARG(sv),
949 (void)PerlLIO_unlink(SvPVX_const(sv));
950 (void)PerlLIO_rename(PL_oldname,SvPVX_const(sv));
951 do_open_raw(gv, SvPVX_const(sv), SvCUR(sv), O_RDONLY, 0);
954 (void)UNLINK(SvPVX_const(sv));
955 if (link(PL_oldname,SvPVX_const(sv)) < 0) {
956 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
957 "Can't rename %s to %" SVf ": %s, skipping file",
958 PL_oldname, SVfARG(sv), Strerror(errno) );
962 (void)UNLINK(PL_oldname);
966 #if !defined(DOSISH) && !defined(__amigaos4__)
967 # ifndef VMS /* Don't delete; use automatic file versioning */
968 if (UNLINK(PL_oldname) < 0) {
969 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
970 "Can't remove %s: %s, skipping file",
971 PL_oldname, Strerror(errno) );
977 Perl_croak(aTHX_ "Can't do inplace edit without backup");
981 sv_setpvn(sv,PL_oldname,oldlen);
982 SETERRNO(0,0); /* in case sprintf set errno */
983 if (!Perl_do_open_raw(aTHX_ PL_argvoutgv, SvPVX_const(sv),
986 O_WRONLY|O_CREAT|O_TRUNC, 0
988 O_WRONLY|O_CREAT|OPEN_EXCL, 0600
991 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE), "Can't do inplace edit on %s: %s",
992 PL_oldname, Strerror(errno) );
996 setdefout(PL_argvoutgv);
997 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
998 if (PL_lastfd >= 0) {
999 (void)PerlLIO_fstat(PL_lastfd,&statbuf);
1001 (void)fchmod(PL_lastfd,PL_filemode);
1003 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
1005 if (fileuid != statbuf.st_uid || filegid != statbuf.st_gid) {
1006 /* XXX silently ignore failures */
1008 PERL_UNUSED_RESULT(fchown(PL_lastfd,fileuid,filegid));
1011 PERL_UNUSED_RESULT(PerlLIO_chown(PL_oldname,fileuid,filegid));
1016 return IoIFP(GvIOp(gv));
1018 } /* successful do_open_raw(), PL_inplace non-NULL */
1020 if (ckWARN_d(WARN_INPLACE)) {
1021 const int eno = errno;
1022 if (PerlLIO_stat(PL_oldname, &statbuf) >= 0
1023 && !S_ISREG(statbuf.st_mode)) {
1024 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
1025 "Can't do inplace edit: %s is not a regular file",
1029 Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't open %s: %s",
1030 PL_oldname, Strerror(eno));
1034 if (io && (IoFLAGS(io) & IOf_ARGV))
1035 IoFLAGS(io) |= IOf_START;
1038 IO * const io = GvIOp(PL_argvoutgv);
1039 if (io && IoIFP(io) && !io_close(io, PL_argvoutgv, FALSE, FALSE)) {
1040 Perl_croak(aTHX_ "Failed to close in-place edit file %" SVf ": %s\n",
1041 old_out_name, Strerror(errno));
1045 /* maybe this is no longer wanted */
1046 (void)do_close(PL_argvoutgv,FALSE);
1048 if (io && (IoFLAGS(io) & IOf_ARGV)
1049 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
1051 GV * const oldout = MUTABLE_GV(av_pop(PL_argvout_stack));
1053 SvREFCNT_dec_NN(oldout);
1056 setdefout(gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO));
1061 /* explicit renamed to avoid C++ conflict -- kja */
1063 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
1070 if (!gv || !isGV_with_GP(gv)) {
1072 SETERRNO(EBADF,SS_IVCHAN);
1076 if (!io) { /* never opened */
1079 SETERRNO(EBADF,SS_IVCHAN);
1083 retval = io_close(io, NULL, not_implicit, FALSE);
1087 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1089 IoTYPE(io) = IoTYPE_CLOSED;
1094 Perl_io_close(pTHX_ IO *io, GV *gv, bool not_implicit, bool warn_on_fail)
1096 bool retval = FALSE;
1098 PERL_ARGS_ASSERT_IO_CLOSE;
1101 if (IoTYPE(io) == IoTYPE_PIPE) {
1102 const int status = PerlProc_pclose(IoIFP(io));
1104 STATUS_NATIVE_CHILD_SET(status);
1105 retval = (STATUS_UNIX == 0);
1108 retval = (status != -1);
1111 else if (IoTYPE(io) == IoTYPE_STD)
1114 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
1115 const bool prev_err = PerlIO_error(IoOFP(io));
1118 PerlIO_restore_errno(IoOFP(io));
1120 retval = (PerlIO_close(IoOFP(io)) != EOF && !prev_err);
1121 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
1124 const bool prev_err = PerlIO_error(IoIFP(io));
1127 PerlIO_restore_errno(IoIFP(io));
1129 retval = (PerlIO_close(IoIFP(io)) != EOF && !prev_err);
1132 IoOFP(io) = IoIFP(io) = NULL;
1134 if (warn_on_fail && !retval) {
1136 Perl_ck_warner_d(aTHX_ packWARN(WARN_IO),
1137 "Warning: unable to close filehandle %"
1138 HEKf " properly: %" SVf,
1139 HEKfARG(GvNAME_HEK(gv)),
1140 SVfARG(get_sv("!",GV_ADD)));
1142 Perl_ck_warner_d(aTHX_ packWARN(WARN_IO),
1143 "Warning: unable to close filehandle "
1145 SVfARG(get_sv("!",GV_ADD)));
1148 else if (not_implicit) {
1149 SETERRNO(EBADF,SS_IVCHAN);
1156 Perl_do_eof(pTHX_ GV *gv)
1158 IO * const io = GvIO(gv);
1160 PERL_ARGS_ASSERT_DO_EOF;
1164 else if (IoTYPE(io) == IoTYPE_WRONLY)
1165 report_wrongway_fh(gv, '>');
1168 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
1169 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
1170 return FALSE; /* this is the most usual case */
1174 /* getc and ungetc can stomp on errno */
1176 const int ch = PerlIO_getc(IoIFP(io));
1178 (void)PerlIO_ungetc(IoIFP(io),ch);
1185 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
1186 if (PerlIO_get_cnt(IoIFP(io)) < -1)
1187 PerlIO_set_cnt(IoIFP(io),-1);
1189 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
1190 if (gv != PL_argvgv || !nextargv(gv, FALSE)) /* get another fp handy */
1194 return TRUE; /* normal fp, definitely end of file */
1200 Perl_do_tell(pTHX_ GV *gv)
1202 IO *const io = GvIO(gv);
1205 PERL_ARGS_ASSERT_DO_TELL;
1207 if (io && (fp = IoIFP(io))) {
1208 return PerlIO_tell(fp);
1211 SETERRNO(EBADF,RMS_IFI);
1216 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
1218 IO *const io = GvIO(gv);
1221 if (io && (fp = IoIFP(io))) {
1222 return PerlIO_seek(fp, pos, whence) >= 0;
1225 SETERRNO(EBADF,RMS_IFI);
1230 Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
1232 IO *const io = GvIO(gv);
1235 PERL_ARGS_ASSERT_DO_SYSSEEK;
1237 if (io && (fp = IoIFP(io))) {
1238 int fd = PerlIO_fileno(fp);
1239 if (fd < 0 || (whence == SEEK_SET && pos < 0)) {
1240 SETERRNO(EINVAL,LIB_INVARG);
1243 return PerlLIO_lseek(fd, pos, whence);
1247 SETERRNO(EBADF,RMS_IFI);
1252 Perl_mode_from_discipline(pTHX_ const char *s, STRLEN len)
1254 int mode = O_BINARY;
1255 PERL_UNUSED_CONTEXT;
1261 if (s[2] == 'a' && s[3] == 'w'
1262 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1271 if (s[2] == 'r' && s[3] == 'l' && s[4] == 'f'
1272 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1281 goto fail_discipline;
1284 else if (isSPACE(*s)) {
1291 end = strchr(s+1, ':');
1294 #ifndef PERLIO_LAYERS
1295 Perl_croak(aTHX_ "IO layers (like '%.*s') unavailable", end-s, s);
1306 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE)
1308 my_chsize(int fd, Off_t length)
1311 /* code courtesy of William Kucharski */
1316 if (PerlLIO_fstat(fd, &filebuf) < 0)
1319 if (filebuf.st_size < length) {
1321 /* extend file length */
1323 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
1326 /* write a "0" byte */
1328 if ((PerlLIO_write(fd, "", 1)) != 1)
1332 /* truncate length */
1336 fl.l_start = length;
1337 fl.l_type = F_WRLCK; /* write lock on file space */
1340 * This relies on the UNDOCUMENTED F_FREESP argument to
1341 * fcntl(2), which truncates the file so that it ends at the
1342 * position indicated by fl.l_start.
1344 * Will minor miracles never cease?
1347 if (fcntl(fd, F_FREESP, &fl) < 0)
1353 Perl_croak_nocontext("truncate not implemented");
1354 #endif /* F_FREESP */
1357 #endif /* !HAS_TRUNCATE && !HAS_CHSIZE */
1360 Perl_do_print(pTHX_ SV *sv, PerlIO *fp)
1362 PERL_ARGS_ASSERT_DO_PRINT;
1364 /* assuming fp is checked earlier */
1367 if (SvTYPE(sv) == SVt_IV && SvIOK(sv)) {
1368 assert(!SvGMAGICAL(sv));
1370 PerlIO_printf(fp, "%" UVuf, (UV)SvUVX(sv));
1372 PerlIO_printf(fp, "%" IVdf, (IV)SvIVX(sv));
1373 return !PerlIO_error(fp);
1377 /* Do this first to trigger any overloading. */
1378 const char *tmps = SvPV_const(sv, len);
1382 if (PerlIO_isutf8(fp)) { /* If the stream is utf8 ... */
1383 if (!SvUTF8(sv)) { /* Convert to utf8 if necessary */
1384 /* We don't modify the original scalar. */
1385 tmpbuf = bytes_to_utf8((const U8*) tmps, &len);
1386 tmps = (char *) tmpbuf;
1388 else if (ckWARN4_d(WARN_UTF8, WARN_SURROGATE, WARN_NON_UNICODE, WARN_NONCHAR)) {
1389 (void) check_utf8_print((const U8*) tmps, len);
1391 } /* else stream isn't utf8 */
1392 else if (DO_UTF8(sv)) { /* But if is utf8 internally, attempt to
1394 STRLEN tmplen = len;
1396 U8 * const result = bytes_from_utf8((const U8*) tmps, &tmplen, &utf8);
1399 /* Here, succeeded in downgrading from utf8. Set up to below
1400 * output the converted value */
1402 tmps = (char *) tmpbuf;
1405 else { /* Non-utf8 output stream, but string only representable in
1407 assert((char *)result == tmps);
1408 Perl_ck_warner_d(aTHX_ packWARN(WARN_UTF8),
1409 "Wide character in %s",
1410 PL_op ? OP_DESC(PL_op) : "print"
1412 /* Could also check that isn't one of the things to avoid
1413 * in utf8 by using check_utf8_print(), but not doing so,
1414 * since the stream isn't a UTF8 stream */
1417 /* To detect whether the process is about to overstep its
1418 * filesize limit we would need getrlimit(). We could then
1419 * also transparently raise the limit with setrlimit() --
1420 * but only until the system hard limit/the filesystem limit,
1421 * at which we would get EPERM. Note that when using buffered
1422 * io the write failure can be delayed until the flush/close. --jhi */
1423 if (len && (PerlIO_write(fp,tmps,len) == 0))
1426 return happy ? !PerlIO_error(fp) : FALSE;
1431 Perl_my_stat_flags(pTHX_ const U32 flags)
1437 if (PL_op->op_flags & OPf_REF) {
1441 return PL_laststatval;
1444 PL_laststype = OP_STAT;
1445 PL_statgv = gv ? gv : (GV *)io;
1446 SvPVCLEAR(PL_statname);
1449 int fd = PerlIO_fileno(IoIFP(io));
1451 /* E.g. PerlIO::scalar has no real fd. */
1452 return (PL_laststatval = -1);
1454 return (PL_laststatval = PerlLIO_fstat(fd, &PL_statcache));
1456 } else if (IoDIRP(io)) {
1457 return (PL_laststatval = PerlLIO_fstat(my_dirfd(IoDIRP(io)), &PL_statcache));
1460 PL_laststatval = -1;
1464 else if ((PL_op->op_private & (OPpFT_STACKED|OPpFT_AFTER_t))
1466 return PL_laststatval;
1468 SV* const sv = TOPs;
1471 if ((gv = MAYBE_DEREF_GV_flags(sv,flags))) {
1474 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
1475 io = MUTABLE_IO(SvRV(sv));
1477 goto do_fstat_have_io;
1480 s = SvPV_flags_const(sv, len, flags);
1482 sv_setpvn(PL_statname, s, len);
1483 s = SvPVX_const(PL_statname); /* s now NUL-terminated */
1484 PL_laststype = OP_STAT;
1485 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1486 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && should_warn_nl(s)) {
1487 GCC_DIAG_IGNORE(-Wformat-nonliteral); /* PL_warn_nl is constant */
1488 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
1491 return PL_laststatval;
1497 Perl_my_lstat_flags(pTHX_ const U32 flags)
1499 static const char* const no_prev_lstat = "The stat preceding -l _ wasn't an lstat";
1502 SV* const sv = TOPs;
1504 if (PL_op->op_flags & OPf_REF) {
1505 if (cGVOP_gv == PL_defgv) {
1506 if (PL_laststype != OP_LSTAT)
1507 Perl_croak(aTHX_ "%s", no_prev_lstat);
1508 return PL_laststatval;
1510 PL_laststatval = -1;
1511 if (ckWARN(WARN_IO)) {
1512 /* diag_listed_as: Use of -l on filehandle%s */
1513 Perl_warner(aTHX_ packWARN(WARN_IO),
1514 "Use of -l on filehandle %" HEKf,
1515 HEKfARG(GvENAME_HEK(cGVOP_gv)));
1519 if ((PL_op->op_private & (OPpFT_STACKED|OPpFT_AFTER_t))
1521 if (PL_laststype != OP_LSTAT)
1522 Perl_croak(aTHX_ "%s", no_prev_lstat);
1523 return PL_laststatval;
1526 PL_laststype = OP_LSTAT;
1528 if ( ( (SvROK(sv) && ( isGV_with_GP(SvRV(sv))
1529 || (isio = SvTYPE(SvRV(sv)) == SVt_PVIO) )
1533 && ckWARN(WARN_IO)) {
1535 /* diag_listed_as: Use of -l on filehandle%s */
1536 Perl_warner(aTHX_ packWARN(WARN_IO),
1537 "Use of -l on filehandle");
1539 /* diag_listed_as: Use of -l on filehandle%s */
1540 Perl_warner(aTHX_ packWARN(WARN_IO),
1541 "Use of -l on filehandle %" HEKf,
1542 HEKfARG(GvENAME_HEK((const GV *)
1543 (SvROK(sv) ? SvRV(sv) : sv))));
1545 file = SvPV_flags_const_nolen(sv, flags);
1546 sv_setpv(PL_statname,file);
1547 PL_laststatval = PerlLIO_lstat(file,&PL_statcache);
1548 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && should_warn_nl(file)) {
1549 GCC_DIAG_IGNORE(-Wformat-nonliteral); /* PL_warn_nl is constant */
1550 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "lstat");
1553 return PL_laststatval;
1557 S_exec_failed(pTHX_ const char *cmd, int fd, int do_report)
1559 const int e = errno;
1560 PERL_ARGS_ASSERT_EXEC_FAILED;
1562 if (ckWARN(WARN_EXEC))
1563 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
1566 /* XXX silently ignore failures */
1567 PERL_UNUSED_RESULT(PerlLIO_write(fd, (void*)&e, sizeof(int)));
1573 Perl_do_aexec5(pTHX_ SV *really, SV **mark, SV **sp,
1574 int fd, int do_report)
1577 PERL_ARGS_ASSERT_DO_AEXEC5;
1578 #if defined(__SYMBIAN32__) || defined(__LIBCATAMOUNT__)
1579 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1583 const char *tmps = NULL;
1584 Newx(PL_Argv, sp - mark + 1, const char*);
1587 while (++mark <= sp) {
1589 *a++ = SvPV_nolen_const(*mark);
1595 tmps = SvPV_nolen_const(really);
1596 if ((!really && *PL_Argv[0] != '/') ||
1597 (really && *tmps != '/')) /* will execvp use PATH? */
1598 TAINT_ENV(); /* testing IFS here is overkill, probably */
1600 if (really && *tmps) {
1601 PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
1603 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1606 S_exec_failed(aTHX_ (really ? tmps : PL_Argv[0]), fd, do_report);
1614 Perl_do_execfree(pTHX)
1622 #ifdef PERL_DEFAULT_DO_EXEC3_IMPLEMENTATION
1625 Perl_do_exec3(pTHX_ const char *incmd, int fd, int do_report)
1632 /* Make a copy so we can change it */
1633 const Size_t cmdlen = strlen(incmd) + 1;
1635 PERL_ARGS_ASSERT_DO_EXEC3;
1637 Newx(buf, cmdlen, char);
1639 memcpy(cmd, incmd, cmdlen);
1641 while (*cmd && isSPACE(*cmd))
1644 /* save an extra exec if possible */
1648 char flags[PERL_FLAGS_MAX];
1649 if (strnEQ(cmd,PL_cshname,PL_cshlen) &&
1650 strEQs(cmd+PL_cshlen," -c")) {
1651 my_strlcpy(flags, "-c", PERL_FLAGS_MAX);
1652 s = cmd+PL_cshlen+3;
1655 my_strlcat(flags, "f", PERL_FLAGS_MAX - 2);
1660 char * const ncmd = s;
1666 if (s[-1] == '\'') {
1669 PerlProc_execl(PL_cshname, "csh", flags, ncmd, (char*)NULL);
1672 S_exec_failed(aTHX_ PL_cshname, fd, do_report);
1681 /* see if there are shell metacharacters in it */
1683 if (*cmd == '.' && isSPACE(cmd[1]))
1686 if (strEQs(cmd,"exec") && isSPACE(cmd[4]))
1690 while (isWORDCHAR(*s))
1691 s++; /* catch VAR=val gizmo */
1695 for (s = cmd; *s; s++) {
1696 if (*s != ' ' && !isALPHA(*s) &&
1697 strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1698 if (*s == '\n' && !s[1]) {
1702 /* handle the 2>&1 construct at the end */
1703 if (*s == '>' && s[1] == '&' && s[2] == '1'
1704 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1705 && (!s[3] || isSPACE(s[3])))
1707 const char *t = s + 3;
1709 while (*t && isSPACE(*t))
1711 if (!*t && (PerlLIO_dup2(1,2) != -1)) {
1718 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char *)NULL);
1720 S_exec_failed(aTHX_ PL_sh_path, fd, do_report);
1726 Newx(PL_Argv, (s - cmd) / 2 + 2, const char*);
1727 PL_Cmd = savepvn(cmd, s-cmd);
1729 for (s = PL_Cmd; *s;) {
1734 while (*s && !isSPACE(*s))
1742 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1744 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1748 S_exec_failed(aTHX_ PL_Argv[0], fd, do_report);
1755 #endif /* OS2 || WIN32 */
1758 Perl_apply(pTHX_ I32 type, SV **mark, SV **sp)
1762 const char *const what = PL_op_name[type];
1765 SV ** const oldmark = mark;
1766 bool killgp = FALSE;
1768 PERL_ARGS_ASSERT_APPLY;
1770 PERL_UNUSED_VAR(what); /* may not be used depending on compile options */
1772 /* Doing this ahead of the switch statement preserves the old behaviour,
1773 where attempting to use kill as a taint test test would fail on
1774 platforms where kill was not defined. */
1776 if (type == OP_KILL)
1777 Perl_die(aTHX_ PL_no_func, what);
1780 if (type == OP_CHOWN)
1781 Perl_die(aTHX_ PL_no_func, what);
1785 #define APPLY_TAINT_PROPER() \
1787 if (TAINT_get) { TAINT_PROPER(what); } \
1790 /* This is a first heuristic; it doesn't catch tainting magic. */
1792 while (++mark <= sp) {
1793 if (SvTAINTED(*mark)) {
1802 APPLY_TAINT_PROPER();
1805 APPLY_TAINT_PROPER();
1807 while (++mark <= sp) {
1809 if ((gv = MAYBE_DEREF_GV(*mark))) {
1810 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1812 int fd = PerlIO_fileno(IoIFP(GvIOn(gv)));
1813 APPLY_TAINT_PROPER();
1815 SETERRNO(EBADF,RMS_IFI);
1817 } else if (fchmod(fd, val))
1820 Perl_die(aTHX_ PL_no_func, "fchmod");
1824 SETERRNO(EBADF,RMS_IFI);
1829 const char *name = SvPV_nomg_const(*mark, len);
1830 APPLY_TAINT_PROPER();
1831 if (!IS_SAFE_PATHNAME(name, len, "chmod") ||
1832 PerlLIO_chmod(name, val)) {
1841 APPLY_TAINT_PROPER();
1842 if (sp - mark > 2) {
1844 val = SvIVx(*++mark);
1845 val2 = SvIVx(*++mark);
1846 APPLY_TAINT_PROPER();
1848 while (++mark <= sp) {
1850 if ((gv = MAYBE_DEREF_GV(*mark))) {
1851 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1853 int fd = PerlIO_fileno(IoIFP(GvIOn(gv)));
1854 APPLY_TAINT_PROPER();
1856 SETERRNO(EBADF,RMS_IFI);
1858 } else if (fchown(fd, val, val2))
1861 Perl_die(aTHX_ PL_no_func, "fchown");
1865 SETERRNO(EBADF,RMS_IFI);
1870 const char *name = SvPV_nomg_const(*mark, len);
1871 APPLY_TAINT_PROPER();
1872 if (!IS_SAFE_PATHNAME(name, len, "chown") ||
1873 PerlLIO_chown(name, val, val2)) {
1882 XXX Should we make lchown() directly available from perl?
1883 For now, we'll let Configure test for HAS_LCHOWN, but do
1884 nothing in the core.
1889 APPLY_TAINT_PROPER();
1892 s = SvPVx_const(*++mark, len);
1893 if (*s == '-' && isALPHA(s[1]))
1900 if (*s == 'S' && s[1] == 'I' && s[2] == 'G') {
1904 if ((val = whichsig_pvn(s, len)) < 0)
1905 Perl_croak(aTHX_ "Unrecognized signal name \"%" SVf "\"",
1917 APPLY_TAINT_PROPER();
1920 while (++mark <= sp) {
1923 if (!(SvNIOK(*mark) || looks_like_number(*mark)))
1924 Perl_croak(aTHX_ "Can't kill a non-numeric process ID");
1925 proc = SvIV_nomg(*mark);
1926 APPLY_TAINT_PROPER();
1928 /* use killpg in preference, as the killpg() wrapper for Win32
1929 * understands process groups, but the kill() wrapper doesn't */
1930 if (killgp ? PerlProc_killpg(proc, val)
1931 : PerlProc_kill(proc, val))
1933 if (PerlProc_kill(killgp ? -proc: proc, val))
1941 APPLY_TAINT_PROPER();
1943 while (++mark <= sp) {
1944 s = SvPV_const(*mark, len);
1945 APPLY_TAINT_PROPER();
1946 if (!IS_SAFE_PATHNAME(s, len, "unlink")) {
1949 else if (PL_unsafe) {
1954 #if defined(__amigaos4__) && defined(NEWLIB)
1957 /* Under AmigaOS4 unlink only 'fails' if the
1958 * filename is invalid. It may not remove the file
1959 * if it's locked, so check if it's still around. */
1960 if ((access(s,F_OK) != -1))
1967 else { /* don't let root wipe out directories without -U */
1969 if (PerlLIO_lstat(s, &statbuf) < 0)
1971 else if (S_ISDIR(statbuf.st_mode)) {
1972 SETERRNO(EISDIR, SS_NOPRIV);
1980 #if defined(__amigaos4__) && defined(NEWLIB)
1983 /* Under AmigaOS4 unlink only 'fails' if the filename is invalid */
1984 /* It may not remove the file if it's Locked, so check if it's still */
1986 if((access(s,F_OK) != -1))
1996 #if defined(HAS_UTIME) || defined(HAS_FUTIMES)
1998 APPLY_TAINT_PROPER();
1999 if (sp - mark > 2) {
2000 #if defined(HAS_FUTIMES)
2001 struct timeval utbuf[2];
2002 void *utbufp = utbuf;
2003 #elif defined(I_UTIME) || defined(VMS)
2004 struct utimbuf utbuf;
2005 struct utimbuf *utbufp = &utbuf;
2011 void *utbufp = &utbuf;
2014 SV* const accessed = *++mark;
2015 SV* const modified = *++mark;
2017 /* Be like C, and if both times are undefined, let the C
2018 * library figure out what to do. This usually means
2019 * "current time". */
2021 if ( accessed == &PL_sv_undef && modified == &PL_sv_undef )
2024 Zero(&utbuf, sizeof utbuf, char);
2026 utbuf[0].tv_sec = (long)SvIV(accessed); /* time accessed */
2027 utbuf[0].tv_usec = 0;
2028 utbuf[1].tv_sec = (long)SvIV(modified); /* time modified */
2029 utbuf[1].tv_usec = 0;
2030 #elif defined(BIG_TIME)
2031 utbuf.actime = (Time_t)SvNV(accessed); /* time accessed */
2032 utbuf.modtime = (Time_t)SvNV(modified); /* time modified */
2034 utbuf.actime = (Time_t)SvIV(accessed); /* time accessed */
2035 utbuf.modtime = (Time_t)SvIV(modified); /* time modified */
2038 APPLY_TAINT_PROPER();
2040 while (++mark <= sp) {
2042 if ((gv = MAYBE_DEREF_GV(*mark))) {
2043 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
2045 int fd = PerlIO_fileno(IoIFP(GvIOn(gv)));
2046 APPLY_TAINT_PROPER();
2048 SETERRNO(EBADF,RMS_IFI);
2050 } else if (futimes(fd, (struct timeval *) utbufp))
2053 Perl_die(aTHX_ PL_no_func, "futimes");
2061 const char * const name = SvPV_nomg_const(*mark, len);
2062 APPLY_TAINT_PROPER();
2063 if (!IS_SAFE_PATHNAME(name, len, "utime")) {
2068 if (utimes(name, (struct timeval *)utbufp))
2070 if (PerlLIO_utime(name, utbufp))
2084 #undef APPLY_TAINT_PROPER
2087 /* Do the permissions in *statbufp allow some operation? */
2088 #ifndef VMS /* VMS' cando is in vms.c */
2090 Perl_cando(pTHX_ Mode_t mode, bool effective, const Stat_t *statbufp)
2091 /* effective is a flag, true for EUID, or for checking if the effective gid
2092 * is in the list of groups returned from getgroups().
2095 PERL_ARGS_ASSERT_CANDO;
2096 PERL_UNUSED_CONTEXT;
2099 /* [Comments and code from Len Reed]
2100 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
2101 * to write-protected files. The execute permission bit is set
2102 * by the Microsoft C library stat() function for the following:
2107 * All files and directories are readable.
2108 * Directories and special files, e.g. "CON", cannot be
2110 * [Comment by Tom Dinger -- a directory can have the write-protect
2111 * bit set in the file system, but DOS permits changes to
2112 * the directory anyway. In addition, all bets are off
2113 * here for networked software, such as Novell and
2117 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
2118 * too so it will actually look into the files for magic numbers
2120 return cBOOL(mode & statbufp->st_mode);
2122 #else /* ! DOSISH */
2124 if (ingroup(544,effective)) { /* member of Administrators */
2126 if ((effective ? PerlProc_geteuid() : PerlProc_getuid()) == 0) { /* root is special */
2128 if (mode == S_IXUSR) {
2129 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
2133 return TRUE; /* root reads and writes anything */
2136 if (statbufp->st_uid == (effective ? PerlProc_geteuid() : PerlProc_getuid()) ) {
2137 if (statbufp->st_mode & mode)
2138 return TRUE; /* ok as "user" */
2140 else if (ingroup(statbufp->st_gid,effective)) {
2141 if (statbufp->st_mode & mode >> 3)
2142 return TRUE; /* ok as "group" */
2144 else if (statbufp->st_mode & mode >> 6)
2145 return TRUE; /* ok as "other" */
2147 #endif /* ! DOSISH */
2152 S_ingroup(pTHX_ Gid_t testgid, bool effective)
2154 #ifndef PERL_IMPLICIT_SYS
2155 /* PERL_IMPLICIT_SYS like Win32: getegid() etc. require the context. */
2156 PERL_UNUSED_CONTEXT;
2158 if (testgid == (effective ? PerlProc_getegid() : PerlProc_getgid()))
2160 #ifdef HAS_GETGROUPS
2162 Groups_t *gary = NULL;
2166 anum = getgroups(0, gary);
2168 Newx(gary, anum, Groups_t);
2169 anum = getgroups(anum, gary);
2171 if (gary[anum] == testgid) {
2185 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
2188 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
2190 const key_t key = (key_t)SvNVx(*++mark);
2191 SV *nsv = optype == OP_MSGGET ? NULL : *++mark;
2192 const I32 flags = SvIVx(*++mark);
2194 PERL_ARGS_ASSERT_DO_IPCGET;
2195 PERL_UNUSED_ARG(sp);
2202 return msgget(key, flags);
2206 return semget(key, (int) SvIV(nsv), flags);
2210 return shmget(key, (size_t) SvUV(nsv), flags);
2212 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
2214 /* diag_listed_as: msg%s not implemented */
2215 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2218 return -1; /* should never happen */
2222 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
2226 const I32 id = SvIVx(*++mark);
2228 const I32 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
2230 const I32 cmd = SvIVx(*++mark);
2231 SV * const astr = *++mark;
2232 STRLEN infosize = 0;
2233 I32 getinfo = (cmd == IPC_STAT);
2235 PERL_ARGS_ASSERT_DO_IPCCTL;
2236 PERL_UNUSED_ARG(sp);
2242 if (cmd == IPC_STAT || cmd == IPC_SET)
2243 infosize = sizeof(struct msqid_ds);
2248 if (cmd == IPC_STAT || cmd == IPC_SET)
2249 infosize = sizeof(struct shmid_ds);
2255 if (cmd == IPC_STAT || cmd == IPC_SET)
2256 infosize = sizeof(struct semid_ds);
2257 else if (cmd == GETALL || cmd == SETALL)
2259 struct semid_ds semds;
2261 #ifdef EXTRA_F_IN_SEMUN_BUF
2262 semun.buff = &semds;
2266 getinfo = (cmd == GETALL);
2267 if (Semctl(id, 0, IPC_STAT, semun) == -1)
2269 infosize = semds.sem_nsems * sizeof(short);
2270 /* "short" is technically wrong but much more portable
2271 than guessing about u_?short(_t)? */
2274 /* diag_listed_as: sem%s not implemented */
2275 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2279 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
2281 /* diag_listed_as: shm%s not implemented */
2282 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2290 SvPV_force_nolen(astr);
2291 a = SvGROW(astr, infosize+1);
2296 a = SvPV(astr, len);
2297 if (len != infosize)
2298 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
2306 const IV i = SvIV(astr);
2307 a = INT2PTR(char *,i); /* ouch */
2314 ret = msgctl(id, cmd, (struct msqid_ds *)a);
2320 union semun unsemds;
2323 unsemds.val = PTR2nat(a);
2326 #ifdef EXTRA_F_IN_SEMUN_BUF
2327 unsemds.buff = (struct semid_ds *)a;
2329 unsemds.buf = (struct semid_ds *)a;
2332 ret = Semctl(id, n, cmd, unsemds);
2334 /* diag_listed_as: sem%s not implemented */
2335 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2342 ret = shmctl(id, cmd, (struct shmid_ds *)a);
2346 if (getinfo && ret >= 0) {
2347 SvCUR_set(astr, infosize);
2348 *SvEND(astr) = '\0';
2355 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
2359 const I32 id = SvIVx(*++mark);
2360 SV * const mstr = *++mark;
2361 const I32 flags = SvIVx(*++mark);
2362 const char * const mbuf = SvPV_const(mstr, len);
2363 const I32 msize = len - sizeof(long);
2365 PERL_ARGS_ASSERT_DO_MSGSND;
2366 PERL_UNUSED_ARG(sp);
2369 Perl_croak(aTHX_ "Arg too short for msgsnd");
2371 if (id >= 0 && flags >= 0) {
2372 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
2374 SETERRNO(EINVAL,LIB_INVARG);
2378 PERL_UNUSED_ARG(sp);
2379 PERL_UNUSED_ARG(mark);
2380 /* diag_listed_as: msg%s not implemented */
2381 Perl_croak(aTHX_ "msgsnd not implemented");
2387 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
2392 I32 msize, flags, ret;
2393 const I32 id = SvIVx(*++mark);
2394 SV * const mstr = *++mark;
2396 PERL_ARGS_ASSERT_DO_MSGRCV;
2397 PERL_UNUSED_ARG(sp);
2399 /* suppress warning when reading into undef var --jhi */
2402 msize = SvIVx(*++mark);
2403 mtype = (long)SvIVx(*++mark);
2404 flags = SvIVx(*++mark);
2405 SvPV_force_nolen(mstr);
2406 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
2409 if (id >= 0 && msize >= 0 && flags >= 0) {
2410 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
2412 SETERRNO(EINVAL,LIB_INVARG);
2416 SvCUR_set(mstr, sizeof(long)+ret);
2417 *SvEND(mstr) = '\0';
2418 /* who knows who has been playing with this message? */
2423 PERL_UNUSED_ARG(sp);
2424 PERL_UNUSED_ARG(mark);
2425 /* diag_listed_as: msg%s not implemented */
2426 Perl_croak(aTHX_ "msgrcv not implemented");
2432 Perl_do_semop(pTHX_ SV **mark, SV **sp)
2436 const I32 id = SvIVx(*++mark);
2437 SV * const opstr = *++mark;
2438 const char * const opbuf = SvPV_const(opstr, opsize);
2440 PERL_ARGS_ASSERT_DO_SEMOP;
2441 PERL_UNUSED_ARG(sp);
2443 if (opsize < 3 * SHORTSIZE
2444 || (opsize % (3 * SHORTSIZE))) {
2445 SETERRNO(EINVAL,LIB_INVARG);
2449 /* We can't assume that sizeof(struct sembuf) == 3 * sizeof(short). */
2451 const int nsops = opsize / (3 * sizeof (short));
2453 short * const ops = (short *) opbuf;
2455 struct sembuf *temps, *t;
2458 Newx (temps, nsops, struct sembuf);
2466 result = semop(id, temps, nsops);
2471 /* diag_listed_as: sem%s not implemented */
2472 Perl_croak(aTHX_ "semop not implemented");
2477 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
2481 struct shmid_ds shmds;
2482 const I32 id = SvIVx(*++mark);
2483 SV * const mstr = *++mark;
2484 const I32 mpos = SvIVx(*++mark);
2485 const I32 msize = SvIVx(*++mark);
2487 PERL_ARGS_ASSERT_DO_SHMIO;
2488 PERL_UNUSED_ARG(sp);
2491 if (shmctl(id, IPC_STAT, &shmds) == -1)
2493 if (mpos < 0 || msize < 0
2494 || (size_t)mpos + msize > (size_t)shmds.shm_segsz) {
2495 SETERRNO(EFAULT,SS_ACCVIO); /* can't do as caller requested */
2499 shm = (char *)shmat(id, NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
2501 SETERRNO(EINVAL,LIB_INVARG);
2504 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2506 if (optype == OP_SHMREAD) {
2508 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2510 SvUPGRADE(mstr, SVt_PV);
2514 mbuf = SvGROW(mstr, (STRLEN)msize+1);
2516 Copy(shm + mpos, mbuf, msize, char);
2517 SvCUR_set(mstr, msize);
2518 *SvEND(mstr) = '\0';
2520 /* who knows who has been playing with this shared memory? */
2526 const char *mbuf = SvPV_const(mstr, len);
2527 const I32 n = ((I32)len > msize) ? msize : (I32)len;
2528 Copy(mbuf, shm + mpos, n, char);
2530 memzero(shm + mpos + n, msize - n);
2534 /* diag_listed_as: shm%s not implemented */
2535 Perl_croak(aTHX_ "shm I/O not implemented");
2540 #endif /* SYSV IPC */
2545 =for apidoc start_glob
2547 Function called by C<do_readline> to spawn a glob (or do the glob inside
2548 perl on VMS). This code used to be inline, but now perl uses C<File::Glob>
2549 this glob starter is only used by miniperl during the build process,
2550 or when PERL_EXTERNAL_GLOB is defined.
2551 Moving it away shrinks F<pp_hot.c>; shrinking F<pp_hot.c> helps speed perl up.
2557 Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
2559 SV * const tmpcmd = newSV(0);
2562 const char *s = SvPV(tmpglob, len);
2564 PERL_ARGS_ASSERT_START_GLOB;
2566 if (!IS_SAFE_SYSCALL(s, len, "pattern", "glob"))
2571 #ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
2572 /* since spawning off a process is a real performance hit */
2579 fp = Perl_vms_start_glob(aTHX_ tmpglob, io);
2584 sv_setpv(tmpcmd, "for a in ");
2585 sv_catsv(tmpcmd, tmpglob);
2586 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
2589 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
2590 sv_catsv(tmpcmd, tmpglob);
2592 sv_setpv(tmpcmd, "perlglob ");
2593 sv_catsv(tmpcmd, tmpglob);
2594 sv_catpv(tmpcmd, " |");
2599 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
2600 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
2601 sv_catsv(tmpcmd, tmpglob);
2602 sv_catpv(tmpcmd, "' 2>/dev/null |");
2604 sv_setpv(tmpcmd, "echo ");
2605 sv_catsv(tmpcmd, tmpglob);
2606 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
2608 #endif /* !DOSISH */
2610 SV ** const svp = hv_fetchs(GvHVn(PL_envgv), "LS_COLORS", 0);
2612 save_helem_flags(GvHV(PL_envgv),
2613 newSVpvs_flags("LS_COLORS", SVs_TEMP), svp,
2616 (void)do_open6(PL_last_in_gv, SvPVX_const(tmpcmd), SvCUR(tmpcmd),
2622 if (!fp && ckWARN(WARN_GLOB)) {
2623 Perl_warner(aTHX_ packWARN(WARN_GLOB), "glob failed (can't start child: %s)",
2631 * ex: set ts=8 sts=4 sw=4 et: