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, NULL);
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, Stat_t *statbufp)
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, statbufp);
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, NULL);
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, Stat_t *statbufp)
622 PERL_ARGS_ASSERT_OPENN_CLEANUP;
625 if (IoTYPE(io) == IoTYPE_RDONLY && ckWARN(WARN_NEWLINE)
626 && should_warn_nl(oname)
630 GCC_DIAG_IGNORE(-Wformat-nonliteral); /* PL_warn_nl is constant */
631 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
637 if (ckWARN(WARN_IO)) {
638 if ((IoTYPE(io) == IoTYPE_RDONLY) &&
639 (fp == PerlIO_stdout() || fp == PerlIO_stderr())) {
640 Perl_warner(aTHX_ packWARN(WARN_IO),
641 "Filehandle STD%s reopened as %" HEKf
643 ((fp == PerlIO_stdout()) ? "OUT" : "ERR"),
644 HEKfARG(GvENAME_HEK(gv)));
646 else if ((IoTYPE(io) == IoTYPE_WRONLY) && fp == PerlIO_stdin()) {
647 Perl_warner(aTHX_ packWARN(WARN_IO),
648 "Filehandle STDIN reopened as %" HEKf " only for output",
649 HEKfARG(GvENAME_HEK(gv))
654 fd = PerlIO_fileno(fp);
655 /* Do NOT do: "if (fd < 0) goto say_false;" here. If there is no
656 * fd assume it isn't a socket - this covers PerlIO::scalar -
657 * otherwise unless we "know" the type probe for socket-ness.
659 if (IoTYPE(io) && IoTYPE(io) != IoTYPE_PIPE && IoTYPE(io) != IoTYPE_STD && fd >= 0) {
660 if (PerlLIO_fstat(fd,&statbuf) < 0) {
661 /* If PerlIO claims to have fd we had better be able to fstat() it. */
662 (void) PerlIO_close(fp);
666 if (S_ISSOCK(statbuf.st_mode))
667 IoTYPE(io) = IoTYPE_SOCKET; /* in case a socket was passed in to us */
670 !(statbuf.st_mode & S_IFMT)
671 && IoTYPE(io) != IoTYPE_WRONLY /* Dups of STD* filehandles already have */
672 && IoTYPE(io) != IoTYPE_RDONLY /* type so they aren't marked as sockets */
673 ) { /* on OS's that return 0 on fstat()ed pipe */
675 Sock_size_t buflen = sizeof tmpbuf;
676 if (PerlSock_getsockname(fd, (struct sockaddr *)tmpbuf, &buflen) >= 0
677 || errno != ENOTSOCK)
678 IoTYPE(io) = IoTYPE_SOCKET; /* some OS's return 0 on fstat()ed socket */
679 /* but some return 0 for streams too, sigh */
681 #endif /* HAS_SOCKET */
682 #endif /* !PERL_MICRO */
686 * If this is a standard handle we discard all the layer stuff
687 * and just dup the fd into whatever was on the handle before !
690 if (saveifp) { /* must use old fp? */
691 /* If fd is less that PL_maxsysfd i.e. STDIN..STDERR
692 then dup the new fileno down
695 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
696 if (saveofp != saveifp) { /* was a socket? */
697 PerlIO_close(saveofp);
701 /* Still a small can-of-worms here if (say) PerlIO::scalar
702 is assigned to (say) STDOUT - for now let dup2() fail
703 and provide the error
706 SETERRNO(EBADF,RMS_IFI);
708 } else if (PerlLIO_dup2(fd, savefd) < 0) {
709 (void)PerlIO_close(fp);
713 if (savefd != PerlIO_fileno(PerlIO_stdin())) {
714 char newname[FILENAME_MAX+1];
715 if (PerlIO_getname(fp, newname)) {
716 if (fd == PerlIO_fileno(PerlIO_stdout()))
717 vmssetuserlnm("SYS$OUTPUT", newname);
718 if (fd == PerlIO_fileno(PerlIO_stderr()))
719 vmssetuserlnm("SYS$ERROR", newname);
725 /* PL_fdpid isn't used on Windows, so avoid this useless work.
726 * XXX Probably the same for a lot of other places. */
731 sv = *av_fetch(PL_fdpid,fd,TRUE);
732 SvUPGRADE(sv, SVt_IV);
735 sv = *av_fetch(PL_fdpid,savefd,TRUE);
736 SvUPGRADE(sv, SVt_IV);
742 /* need to close fp without closing underlying fd */
743 int ofd = PerlIO_fileno(fp);
744 int dupfd = ofd >= 0 ? PerlLIO_dup(ofd) : -1;
745 #if defined(HAS_FCNTL) && defined(F_SETFD)
746 /* Assume if we have F_SETFD we have F_GETFD. */
747 /* Get a copy of all the fd flags. */
748 int fd_flags = ofd >= 0 ? fcntl(ofd, F_GETFD) : -1;
751 PerlLIO_close(dupfd);
755 if (ofd < 0 || dupfd < 0) {
757 PerlLIO_close(dupfd);
761 PerlLIO_dup2(dupfd, ofd);
762 #if defined(HAS_FCNTL) && defined(F_SETFD)
763 /* The dup trick has lost close-on-exec on ofd,
764 * and possibly any other flags, so restore them. */
765 if (fcntl(ofd,F_SETFD, fd_flags) < 0) {
767 PerlLIO_close(dupfd);
771 PerlLIO_close(dupfd);
778 fd = PerlIO_fileno(fp);
780 #if defined(HAS_FCNTL) && defined(F_SETFD) && defined(FD_CLOEXEC)
781 if (fd >= 0 && fd > PL_maxsysfd && fcntl(fd, F_SETFD, FD_CLOEXEC) < 0) {
788 IoFLAGS(io) &= ~IOf_NOLINE;
790 if (IoTYPE(io) == IoTYPE_SOCKET
791 || (IoTYPE(io) == IoTYPE_WRONLY && fd >= 0 && S_ISCHR(statbuf.st_mode)) ) {
793 if (*s == IoTYPE_IMPLICIT || *s == IoTYPE_NUMERIC)
796 if (!(IoOFP(io) = PerlIO_openn(aTHX_ type,s,fd,0,0,NULL,0,NULL))) {
812 IoTYPE(io) = savetype;
816 /* Open a temp file in the same directory as an original name.
820 S_openindirtemp(pTHX_ GV *gv, SV *orig_name, SV *temp_out_name) {
823 const char *p = SvPV_nolen(orig_name);
826 /* look for the last directory separator */
827 sep = strrchr(p, '/');
832 if ((sep2 = strrchr(sep ? sep : p, '\\')))
838 const char *openp = strchr(p, '[');
840 sep = strchr(openp, ']');
842 sep = strchr(p, ':');
847 sv_setpvn(temp_out_name, p, sep - p + 1);
848 sv_catpvs(temp_out_name, "XXXXXXXX");
851 sv_setpvs(temp_out_name, "XXXXXXXX");
853 fd = Perl_my_mkstemp(SvPVX(temp_out_name));
858 fp = PerlIO_fdopen(fd, "w+");
862 return do_openn(gv, "+>&", 3, 0, 0, 0, fp, NULL, 0);
865 #if defined(HAS_UNLINKAT) && defined(HAS_RENAMEAT) && defined(HAS_FCHMODAT) && \
866 (defined(HAS_DIRFD) || defined(HAS_DIR_DD_FD)) && !defined(NO_USE_ATFUNCTIONS) && \
868 # define ARGV_USE_ATFUNCTIONS
871 /* Win32 doesn't necessarily return useful information
874 #ifndef ARGV_USE_ATFUNCTIONS
876 # define ARGV_USE_STAT_INO
880 #define ARGVMG_BACKUP_NAME 0
881 #define ARGVMG_TEMP_NAME 1
882 #define ARGVMG_ORIG_NAME 2
883 #define ARGVMG_ORIG_MODE 3
884 #define ARGVMG_ORIG_PID 4
886 #if defined(ARGV_USE_ATFUNCTIONS)
887 #define ARGVMG_ORIG_DIRP 5
888 #elif defined(ARGV_USE_STAT_INO)
889 /* we store the entire stat_t since the ino_t and dev_t values might
890 not fit in an IV. I could have created a new structure and
891 transferred them across, but this seemed too much effort for very
894 #define ARGVMG_ORIG_CWD_STAT 5
898 S_argvout_free(pTHX_ SV *io, MAGIC *mg) {
903 /* note this can be entered once the file has been
904 successfully deleted too */
905 assert(IoTYPE(io) != IoTYPE_PIPE);
907 /* mg_obj can be NULL if a thread is created with the handle open, in which
908 case we leave any clean up to the parent thread */
909 if (mg->mg_obj && IoIFP(io)) {
911 #ifdef ARGV_USE_ATFUNCTIONS
915 PerlIO *iop = IoIFP(io);
917 assert(SvTYPE(mg->mg_obj) == SVt_PVAV);
919 pid_psv = av_fetch((AV*)mg->mg_obj, ARGVMG_ORIG_PID, FALSE);
921 assert(pid_psv && *pid_psv);
923 if (SvIV(*pid_psv) == (IV)PerlProc_getpid()) {
924 /* if we get here the file hasn't been closed explicitly by the
925 user and hadn't been closed implicitly by nextargv(), so
927 (void)PerlIO_close(iop);
928 IoIFP(io) = IoOFP(io) = NULL;
929 temp_psv = av_fetch((AV*)mg->mg_obj, ARGVMG_TEMP_NAME, FALSE);
930 assert(temp_psv && *temp_psv && SvPOK(*temp_psv));
931 #ifdef ARGV_USE_ATFUNCTIONS
932 dir_psv = av_fetch((AV*)mg->mg_obj, ARGVMG_ORIG_DIRP, FALSE);
933 assert(dir_psv && *dir_psv && SvIOK(*dir_psv));
934 dir = INT2PTR(DIR *, SvIV(*dir_psv));
936 (void)unlinkat(my_dirfd(dir), SvPVX(*temp_psv), 0);
940 (void)UNLINK(SvPVX(*temp_psv));
949 S_argvout_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param) {
950 PERL_UNUSED_ARG(param);
952 /* ideally we could just remove the magic from the SV but we don't get the SV here */
953 SvREFCNT_dec(mg->mg_obj);
959 /* Magic of this type has an AV containing the following:
960 0: name of the backup file (if any)
961 1: name of the temp output file
962 2: name of the original file
963 3: file mode of the original file
964 4: pid of the process we opened at, to prevent doing the renaming
965 etc in both the child and the parent after a fork
967 If we have unlinkat(), renameat(), fchmodat(), dirfd() we also keep:
968 5: the DIR * for the current directory when we open the file, stored as an IV
971 static const MGVTBL argvout_vtbl =
976 NULL, /* svt_clear */
977 S_argvout_free, /* svt_free */
979 S_argvout_dup, /* svt_dup */
984 Perl_nextargv(pTHX_ GV *gv, bool nomagicopen)
986 IO * const io = GvIOp(gv);
987 SV *const old_out_name = PL_inplace ? newSVsv(GvSV(gv)) : NULL;
989 PERL_ARGS_ASSERT_NEXTARGV;
992 SAVEFREESV(old_out_name);
995 PL_argvoutgv = gv_fetchpvs("ARGVOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
996 if (io && (IoFLAGS(io) & (IOf_ARGV|IOf_START)) == (IOf_ARGV|IOf_START)) {
997 IoFLAGS(io) &= ~IOf_START;
1000 Perl_av_create_and_push(aTHX_ &PL_argvout_stack,
1001 SvREFCNT_inc_simple_NN(PL_defoutgv));
1006 IO * const io = GvIOp(PL_argvoutgv);
1007 if (io && IoIFP(io) && old_out_name) {
1008 do_close(PL_argvoutgv, FALSE);
1016 while (av_tindex(GvAV(gv)) >= 0) {
1018 SV *const sv = av_shift(GvAV(gv));
1020 SvTAINTED_off(GvSVn(gv)); /* previous tainting irrelevant */
1021 sv_setsv(GvSVn(gv),sv);
1022 SvSETMAGIC(GvSV(gv));
1023 PL_oldname = SvPVx(GvSV(gv), oldlen);
1024 if (LIKELY(!PL_inplace)) {
1026 ? do_open6(gv, "<", 1, NULL, &GvSV(gv), 1)
1027 : do_open6(gv, PL_oldname, oldlen, NULL, NULL, 0)
1029 return IoIFP(GvIOp(gv));
1034 /* This very long block ends with return IoIFP(GvIOp(gv));
1035 Both this block and the block above fall through on open
1036 failure to the warning code, and then the while loop above tries
1038 if (do_open_raw(gv, PL_oldname, oldlen, O_RDONLY, 0, &statbuf)) {
1039 #ifndef FLEXFILENAMES
1043 #ifdef ARGV_USE_ATFUNCTIONS
1048 AV *magic_av = NULL;
1049 SV *temp_name_sv = NULL;
1052 TAINT_PROPER("inplace open");
1053 if (oldlen == 1 && *PL_oldname == '-') {
1054 setdefout(gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL,
1056 return IoIFP(GvIOp(gv));
1058 #ifndef FLEXFILENAMES
1059 filedev = statbuf.st_dev;
1060 fileino = statbuf.st_ino;
1062 PL_filemode = statbuf.st_mode;
1063 fileuid = statbuf.st_uid;
1064 filegid = statbuf.st_gid;
1065 if (!S_ISREG(PL_filemode)) {
1066 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
1067 "Can't do inplace edit: %s is not a regular file",
1073 if (*PL_inplace && strNE(PL_inplace, "*")) {
1074 const char *star = strchr(PL_inplace, '*');
1076 const char *begin = PL_inplace;
1079 sv_catpvn(sv, begin, star - begin);
1080 sv_catpvn(sv, PL_oldname, oldlen);
1082 } while ((star = strchr(begin, '*')));
1087 sv_catpv(sv,PL_inplace);
1089 #ifndef FLEXFILENAMES
1090 if ((PerlLIO_stat(SvPVX_const(sv),&statbuf) >= 0
1091 && statbuf.st_dev == filedev
1092 && statbuf.st_ino == fileino)
1094 || ((_djstat_fail_bits & _STFAIL_TRUENAME)!=0)
1098 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
1099 "Can't do inplace edit: %"
1100 SVf " would not be unique",
1105 av_store(magic_av, ARGVMG_BACKUP_NAME, newSVsv(sv));
1108 sv_setpvn(sv,PL_oldname,oldlen);
1109 SETERRNO(0,0); /* in case sprintf set errno */
1110 temp_name_sv = newSV(0);
1111 if (!S_openindirtemp(aTHX_ PL_argvoutgv, GvSV(gv), temp_name_sv)) {
1112 SvREFCNT_dec(temp_name_sv);
1113 /* diag_listed_as: Can't do inplace edit on %s: %s */
1114 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE), "Can't do inplace edit on %s: Cannot make temp name: %s",
1115 PL_oldname, Strerror(errno) );
1116 #ifndef FLEXFILENAMES
1120 SvREFCNT_dec(magic_av);
1123 av_store(magic_av, ARGVMG_TEMP_NAME, temp_name_sv);
1124 av_store(magic_av, ARGVMG_ORIG_NAME, newSVsv(sv));
1125 av_store(magic_av, ARGVMG_ORIG_MODE, newSVuv(PL_filemode));
1126 av_store(magic_av, ARGVMG_ORIG_PID, newSViv((IV)PerlProc_getpid()));
1127 #if defined(ARGV_USE_ATFUNCTIONS)
1128 curdir = opendir(".");
1129 av_store(magic_av, ARGVMG_ORIG_DIRP, newSViv(PTR2IV(curdir)));
1130 #elif defined(ARGV_USE_STAT_INO)
1131 if (PerlLIO_stat(".", &statbuf) >= 0) {
1132 av_store(magic_av, ARGVMG_ORIG_CWD_STAT,
1133 newSVpvn((char *)&statbuf, sizeof(statbuf)));
1136 setdefout(PL_argvoutgv);
1137 sv_setsv(GvSVn(PL_argvoutgv), temp_name_sv);
1138 mg = sv_magicext((SV*)GvIOp(PL_argvoutgv), (SV*)magic_av, PERL_MAGIC_uvar, &argvout_vtbl, NULL, 0);
1139 mg->mg_flags |= MGf_DUP;
1140 SvREFCNT_dec(magic_av);
1141 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
1142 if (PL_lastfd >= 0) {
1143 (void)PerlLIO_fstat(PL_lastfd,&statbuf);
1145 (void)fchmod(PL_lastfd,PL_filemode);
1147 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
1149 if (fileuid != statbuf.st_uid || filegid != statbuf.st_gid) {
1150 /* XXX silently ignore failures */
1152 PERL_UNUSED_RESULT(fchown(PL_lastfd,fileuid,filegid));
1153 #elif defined(HAS_CHOWN)
1154 PERL_UNUSED_RESULT(PerlLIO_chown(PL_oldname,fileuid,filegid));
1158 return IoIFP(GvIOp(gv));
1160 } /* successful do_open_raw(), PL_inplace non-NULL */
1162 if (ckWARN_d(WARN_INPLACE)) {
1163 const int eno = errno;
1165 if (PerlLIO_stat(PL_oldname, &statbuf) >= 0
1166 && !S_ISREG(statbuf.st_mode)) {
1167 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
1168 "Can't do inplace edit: %s is not a regular file",
1172 Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't open %s: %s",
1173 PL_oldname, Strerror(eno));
1177 if (io && (IoFLAGS(io) & IOf_ARGV))
1178 IoFLAGS(io) |= IOf_START;
1180 if (io && (IoFLAGS(io) & IOf_ARGV)
1181 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
1183 GV * const oldout = MUTABLE_GV(av_pop(PL_argvout_stack));
1185 SvREFCNT_dec_NN(oldout);
1188 setdefout(gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO));
1193 #ifdef ARGV_USE_ATFUNCTIONS
1194 # if defined(__FreeBSD__)
1196 /* FreeBSD 11 renameat() mis-behaves strangely with absolute paths in cases where the
1197 * equivalent rename() succeeds
1200 S_my_renameat(int olddfd, const char *oldpath, int newdfd, const char *newpath) {
1201 /* this is intended only for use in Perl_do_close() */
1202 assert(olddfd == newdfd);
1203 assert(PERL_FILE_IS_ABSOLUTE(oldpath) == PERL_FILE_IS_ABSOLUTE(newpath));
1204 if (PERL_FILE_IS_ABSOLUTE(oldpath)) {
1205 return PerlLIO_rename(oldpath, newpath);
1208 return renameat(olddfd, oldpath, newdfd, newpath);
1213 # define S_my_renameat(dh1, pv1, dh2, pv2) renameat((dh1), (pv1), (dh2), (pv2))
1214 # endif /* if defined(__FreeBSD__) */
1217 /* explicit renamed to avoid C++ conflict -- kja */
1219 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
1227 if (!gv || !isGV_with_GP(gv)) {
1229 SETERRNO(EBADF,SS_IVCHAN);
1233 if (!io) { /* never opened */
1236 SETERRNO(EBADF,SS_IVCHAN);
1240 if ((mg = mg_findext((SV*)io, PERL_MAGIC_uvar, &argvout_vtbl))
1242 /* handle to an in-place edit work file */
1243 SV **back_psv = av_fetch((AV*)mg->mg_obj, ARGVMG_BACKUP_NAME, FALSE);
1244 SV **temp_psv = av_fetch((AV*)mg->mg_obj, ARGVMG_TEMP_NAME, FALSE);
1245 /* PL_oldname may have been modified by a nested ARGV use at this point */
1246 SV **orig_psv = av_fetch((AV*)mg->mg_obj, ARGVMG_ORIG_NAME, FALSE);
1247 SV **mode_psv = av_fetch((AV*)mg->mg_obj, ARGVMG_ORIG_MODE, FALSE);
1248 SV **pid_psv = av_fetch((AV*)mg->mg_obj, ARGVMG_ORIG_PID, FALSE);
1249 #if defined(ARGV_USE_ATFUNCTIONS)
1250 SV **dir_psv = av_fetch((AV*)mg->mg_obj, ARGVMG_ORIG_DIRP, FALSE);
1253 #elif defined(ARGV_USE_STAT_INO)
1254 SV **stat_psv = av_fetch((AV*)mg->mg_obj, ARGVMG_ORIG_CWD_STAT, FALSE);
1255 Stat_t *orig_cwd_stat = stat_psv && *stat_psv ? (Stat_t *)SvPVX(*stat_psv) : NULL;
1257 #ifndef ARGV_USE_ATFUNCTIONS
1263 const char *orig_pv;
1265 assert(temp_psv && *temp_psv);
1266 assert(orig_psv && *orig_psv);
1267 assert(mode_psv && *mode_psv);
1268 assert(pid_psv && *pid_psv);
1269 #ifdef ARGV_USE_ATFUNCTIONS
1270 assert(dir_psv && *dir_psv);
1271 dir = INT2PTR(DIR *, SvIVX(*dir_psv));
1272 dfd = my_dirfd(dir);
1275 orig_pv = SvPVX(*orig_psv);
1276 mode = SvUV(*mode_psv);
1278 if ((mode & (S_ISUID|S_ISGID)) != 0
1279 && (fd = PerlIO_fileno(IoIFP(io))) >= 0) {
1280 (void)PerlIO_flush(IoIFP(io));
1282 (void)fchmod(fd, mode);
1284 (void)PerlLIO_chmod(orig_pv, mode);
1288 retval = io_close(io, NULL, not_implicit, FALSE);
1290 if (SvIV(*pid_psv) != (IV)PerlProc_getpid()) {
1291 /* this is a child process, don't duplicate our rename() etc
1297 #ifdef ARGV_USE_STAT_INO
1298 /* if the path is absolute the possible moving of cwd (which the file
1299 might be in) isn't our problem.
1300 This code tries to be reasonably balanced about detecting a changed
1301 CWD, if we have the information needed to check that curdir has changed, we
1304 if (!PERL_FILE_IS_ABSOLUTE(SvPVX(*orig_psv))
1306 && PerlLIO_stat(".", &statbuf) >= 0
1307 && ( statbuf.st_dev != orig_cwd_stat->st_dev
1308 || statbuf.st_ino != orig_cwd_stat->st_ino)) {
1309 Perl_croak(aTHX_ "Cannot complete in-place edit of %" SVf ": %s",
1310 *orig_psv, "Current directory has changed");
1313 #if !defined(ARGV_USE_ATFUNCTIONS) && !defined(ARGV_USE_STAT_INO)
1314 /* Some platforms don't have useful st_ino etc, so just
1315 check we can see the work file.
1317 if (!PERL_FILE_IS_ABSOLUTE(SvPVX(*orig_psv))
1318 && PerlLIO_stat(SvPVX(*temp_psv), &statbuf) < 0) {
1319 Perl_croak(aTHX_ "Cannot complete in-place edit of %" SVf ": %s",
1321 "Work file is missing - did you change directory?");
1325 #if defined(DOSISH) || defined(__CYGWIN__)
1326 if (PL_argvgv && GvIOp(PL_argvgv)
1327 && IoIFP(GvIOp(PL_argvgv))
1328 && (IoFLAGS(GvIOp(PL_argvgv)) & (IOf_ARGV|IOf_START)) == IOf_ARGV) {
1329 do_close(PL_argvgv, FALSE);
1332 if (back_psv && *back_psv) {
1333 #if defined(HAS_LINK) && !defined(DOSISH) && !defined(__CYGWIN__) && defined(HAS_RENAME)
1335 # ifdef ARGV_USE_ATFUNCTIONS
1336 linkat(dfd, orig_pv, dfd, SvPVX(*back_psv), 0) < 0
1338 link(orig_pv, SvPVX(*back_psv)) < 0
1345 # ifdef ARGV_USE_ATFUNCTIONS
1346 S_my_renameat(dfd, orig_pv, dfd, SvPVX(*back_psv)) < 0
1348 PerlLIO_rename(orig_pv, SvPVX(*back_psv)) < 0
1351 if (!not_implicit) {
1352 # ifdef ARGV_USE_ATFUNCTIONS
1353 (void)unlinkat(dfd, SvPVX_const(*temp_psv), 0);
1355 UNLINK(SvPVX(*temp_psv));
1357 Perl_croak(aTHX_ "Can't rename %s to %s: %s, skipping file",
1358 SvPVX(*orig_psv), SvPVX(*back_psv), Strerror(errno));
1360 /* should we warn here? */
1364 (void)UNLINK(SvPVX(*back_psv));
1365 if (link(orig_pv, SvPVX(*back_psv))) {
1366 if (!not_implicit) {
1367 Perl_croak(aTHX_ "Can't rename %s to %s: %s, skipping file",
1368 SvPVX(*orig_psv), SvPVX(*back_psv), Strerror(errno));
1372 /* we need to use link() to get the temp into place too, and linK()
1373 fails if the new link name exists */
1374 (void)UNLINK(orig_pv);
1378 #if defined(DOSISH) || defined(__CYGWIN__) || !defined(HAS_RENAME)
1384 #if !defined(HAS_RENAME)
1385 link(SvPVX(*temp_psv), orig_pv) < 0
1386 #elif defined(ARGV_USE_ATFUNCTIONS)
1387 S_my_renameat(dfd, SvPVX(*temp_psv), dfd, orig_pv) < 0
1389 PerlLIO_rename(SvPVX(*temp_psv), orig_pv) < 0
1392 if (!not_implicit) {
1393 #ifdef ARGV_USE_ATFUNCTIONS
1394 (void)unlinkat(dfd, SvPVX_const(*temp_psv), 0);
1396 UNLINK(SvPVX(*temp_psv));
1398 Perl_croak(aTHX_ "Can't rename in-place work file '%s' to '%s': %s\n",
1399 SvPVX(*temp_psv), SvPVX(*orig_psv), Strerror(errno));
1402 UNLINK(SvPVX_const(*temp_psv));
1406 UNLINK(SvPVX(*temp_psv));
1410 #ifdef ARGV_USE_ATFUNCTIONS
1411 unlinkat(dfd, SvPVX_const(*temp_psv), 0);
1413 UNLINK(SvPVX_const(*temp_psv));
1415 if (!not_implicit) {
1416 Perl_croak(aTHX_ "Failed to close in-place work file %s: %s",
1417 SvPVX(*temp_psv), Strerror(errno));
1421 mg_freeext((SV*)io, PERL_MAGIC_uvar, &argvout_vtbl);
1424 retval = io_close(io, NULL, not_implicit, FALSE);
1429 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1431 IoTYPE(io) = IoTYPE_CLOSED;
1436 Perl_io_close(pTHX_ IO *io, GV *gv, bool not_implicit, bool warn_on_fail)
1438 bool retval = FALSE;
1440 PERL_ARGS_ASSERT_IO_CLOSE;
1443 if (IoTYPE(io) == IoTYPE_PIPE) {
1444 const int status = PerlProc_pclose(IoIFP(io));
1446 STATUS_NATIVE_CHILD_SET(status);
1447 retval = (STATUS_UNIX == 0);
1450 retval = (status != -1);
1453 else if (IoTYPE(io) == IoTYPE_STD)
1456 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
1457 const bool prev_err = PerlIO_error(IoOFP(io));
1460 PerlIO_restore_errno(IoOFP(io));
1462 retval = (PerlIO_close(IoOFP(io)) != EOF && !prev_err);
1463 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
1466 const bool prev_err = PerlIO_error(IoIFP(io));
1469 PerlIO_restore_errno(IoIFP(io));
1471 retval = (PerlIO_close(IoIFP(io)) != EOF && !prev_err);
1474 IoOFP(io) = IoIFP(io) = NULL;
1476 if (warn_on_fail && !retval) {
1478 Perl_ck_warner_d(aTHX_ packWARN(WARN_IO),
1479 "Warning: unable to close filehandle %"
1480 HEKf " properly: %" SVf,
1481 HEKfARG(GvNAME_HEK(gv)),
1482 SVfARG(get_sv("!",GV_ADD)));
1484 Perl_ck_warner_d(aTHX_ packWARN(WARN_IO),
1485 "Warning: unable to close filehandle "
1487 SVfARG(get_sv("!",GV_ADD)));
1490 else if (not_implicit) {
1491 SETERRNO(EBADF,SS_IVCHAN);
1498 Perl_do_eof(pTHX_ GV *gv)
1500 IO * const io = GvIO(gv);
1502 PERL_ARGS_ASSERT_DO_EOF;
1506 else if (IoTYPE(io) == IoTYPE_WRONLY)
1507 report_wrongway_fh(gv, '>');
1510 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
1511 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
1512 return FALSE; /* this is the most usual case */
1516 /* getc and ungetc can stomp on errno */
1518 const int ch = PerlIO_getc(IoIFP(io));
1520 (void)PerlIO_ungetc(IoIFP(io),ch);
1527 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
1528 if (PerlIO_get_cnt(IoIFP(io)) < -1)
1529 PerlIO_set_cnt(IoIFP(io),-1);
1531 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
1532 if (gv != PL_argvgv || !nextargv(gv, FALSE)) /* get another fp handy */
1536 return TRUE; /* normal fp, definitely end of file */
1542 Perl_do_tell(pTHX_ GV *gv)
1544 IO *const io = GvIO(gv);
1547 PERL_ARGS_ASSERT_DO_TELL;
1549 if (io && (fp = IoIFP(io))) {
1550 return PerlIO_tell(fp);
1553 SETERRNO(EBADF,RMS_IFI);
1558 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
1560 IO *const io = GvIO(gv);
1563 if (io && (fp = IoIFP(io))) {
1564 return PerlIO_seek(fp, pos, whence) >= 0;
1567 SETERRNO(EBADF,RMS_IFI);
1572 Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
1574 IO *const io = GvIO(gv);
1577 PERL_ARGS_ASSERT_DO_SYSSEEK;
1579 if (io && (fp = IoIFP(io))) {
1580 int fd = PerlIO_fileno(fp);
1581 if (fd < 0 || (whence == SEEK_SET && pos < 0)) {
1582 SETERRNO(EINVAL,LIB_INVARG);
1585 return PerlLIO_lseek(fd, pos, whence);
1589 SETERRNO(EBADF,RMS_IFI);
1594 Perl_mode_from_discipline(pTHX_ const char *s, STRLEN len)
1596 int mode = O_BINARY;
1597 PERL_UNUSED_CONTEXT;
1603 if (s[2] == 'a' && s[3] == 'w'
1604 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1613 if (s[2] == 'r' && s[3] == 'l' && s[4] == 'f'
1614 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1623 goto fail_discipline;
1626 else if (isSPACE(*s)) {
1633 end = strchr(s+1, ':');
1636 #ifndef PERLIO_LAYERS
1637 Perl_croak(aTHX_ "IO layers (like '%.*s') unavailable", end-s, s);
1648 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE)
1650 my_chsize(int fd, Off_t length)
1653 /* code courtesy of William Kucharski */
1658 if (PerlLIO_fstat(fd, &filebuf) < 0)
1661 if (filebuf.st_size < length) {
1663 /* extend file length */
1665 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
1668 /* write a "0" byte */
1670 if ((PerlLIO_write(fd, "", 1)) != 1)
1674 /* truncate length */
1678 fl.l_start = length;
1679 fl.l_type = F_WRLCK; /* write lock on file space */
1682 * This relies on the UNDOCUMENTED F_FREESP argument to
1683 * fcntl(2), which truncates the file so that it ends at the
1684 * position indicated by fl.l_start.
1686 * Will minor miracles never cease?
1689 if (fcntl(fd, F_FREESP, &fl) < 0)
1695 Perl_croak_nocontext("truncate not implemented");
1696 #endif /* F_FREESP */
1699 #endif /* !HAS_TRUNCATE && !HAS_CHSIZE */
1702 Perl_do_print(pTHX_ SV *sv, PerlIO *fp)
1704 PERL_ARGS_ASSERT_DO_PRINT;
1706 /* assuming fp is checked earlier */
1709 if (SvTYPE(sv) == SVt_IV && SvIOK(sv)) {
1710 assert(!SvGMAGICAL(sv));
1712 PerlIO_printf(fp, "%" UVuf, (UV)SvUVX(sv));
1714 PerlIO_printf(fp, "%" IVdf, (IV)SvIVX(sv));
1715 return !PerlIO_error(fp);
1719 /* Do this first to trigger any overloading. */
1720 const char *tmps = SvPV_const(sv, len);
1724 if (PerlIO_isutf8(fp)) { /* If the stream is utf8 ... */
1725 if (!SvUTF8(sv)) { /* Convert to utf8 if necessary */
1726 /* We don't modify the original scalar. */
1727 tmpbuf = bytes_to_utf8((const U8*) tmps, &len);
1728 tmps = (char *) tmpbuf;
1730 else if (ckWARN4_d(WARN_UTF8, WARN_SURROGATE, WARN_NON_UNICODE, WARN_NONCHAR)) {
1731 (void) check_utf8_print((const U8*) tmps, len);
1733 } /* else stream isn't utf8 */
1734 else if (DO_UTF8(sv)) { /* But if is utf8 internally, attempt to
1736 STRLEN tmplen = len;
1738 U8 * const result = bytes_from_utf8((const U8*) tmps, &tmplen, &utf8);
1741 /* Here, succeeded in downgrading from utf8. Set up to below
1742 * output the converted value */
1744 tmps = (char *) tmpbuf;
1747 else { /* Non-utf8 output stream, but string only representable in
1749 assert((char *)result == tmps);
1750 Perl_ck_warner_d(aTHX_ packWARN(WARN_UTF8),
1751 "Wide character in %s",
1752 PL_op ? OP_DESC(PL_op) : "print"
1754 /* Could also check that isn't one of the things to avoid
1755 * in utf8 by using check_utf8_print(), but not doing so,
1756 * since the stream isn't a UTF8 stream */
1759 /* To detect whether the process is about to overstep its
1760 * filesize limit we would need getrlimit(). We could then
1761 * also transparently raise the limit with setrlimit() --
1762 * but only until the system hard limit/the filesystem limit,
1763 * at which we would get EPERM. Note that when using buffered
1764 * io the write failure can be delayed until the flush/close. --jhi */
1765 if (len && (PerlIO_write(fp,tmps,len) == 0))
1768 return happy ? !PerlIO_error(fp) : FALSE;
1773 Perl_my_stat_flags(pTHX_ const U32 flags)
1779 if (PL_op->op_flags & OPf_REF) {
1783 return PL_laststatval;
1786 PL_laststype = OP_STAT;
1787 PL_statgv = gv ? gv : (GV *)io;
1788 SvPVCLEAR(PL_statname);
1791 int fd = PerlIO_fileno(IoIFP(io));
1793 /* E.g. PerlIO::scalar has no real fd. */
1794 return (PL_laststatval = -1);
1796 return (PL_laststatval = PerlLIO_fstat(fd, &PL_statcache));
1798 } else if (IoDIRP(io)) {
1799 return (PL_laststatval = PerlLIO_fstat(my_dirfd(IoDIRP(io)), &PL_statcache));
1802 PL_laststatval = -1;
1806 else if ((PL_op->op_private & (OPpFT_STACKED|OPpFT_AFTER_t))
1808 return PL_laststatval;
1810 SV* const sv = TOPs;
1813 if ((gv = MAYBE_DEREF_GV_flags(sv,flags))) {
1816 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
1817 io = MUTABLE_IO(SvRV(sv));
1819 goto do_fstat_have_io;
1822 s = SvPV_flags_const(sv, len, flags);
1824 sv_setpvn(PL_statname, s, len);
1825 s = SvPVX_const(PL_statname); /* s now NUL-terminated */
1826 PL_laststype = OP_STAT;
1827 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1828 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && should_warn_nl(s)) {
1829 GCC_DIAG_IGNORE(-Wformat-nonliteral); /* PL_warn_nl is constant */
1830 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
1833 return PL_laststatval;
1839 Perl_my_lstat_flags(pTHX_ const U32 flags)
1841 static const char* const no_prev_lstat = "The stat preceding -l _ wasn't an lstat";
1844 SV* const sv = TOPs;
1846 if (PL_op->op_flags & OPf_REF) {
1847 if (cGVOP_gv == PL_defgv) {
1848 if (PL_laststype != OP_LSTAT)
1849 Perl_croak(aTHX_ "%s", no_prev_lstat);
1850 return PL_laststatval;
1852 PL_laststatval = -1;
1853 if (ckWARN(WARN_IO)) {
1854 /* diag_listed_as: Use of -l on filehandle%s */
1855 Perl_warner(aTHX_ packWARN(WARN_IO),
1856 "Use of -l on filehandle %" HEKf,
1857 HEKfARG(GvENAME_HEK(cGVOP_gv)));
1861 if ((PL_op->op_private & (OPpFT_STACKED|OPpFT_AFTER_t))
1863 if (PL_laststype != OP_LSTAT)
1864 Perl_croak(aTHX_ "%s", no_prev_lstat);
1865 return PL_laststatval;
1868 PL_laststype = OP_LSTAT;
1870 if ( ( (SvROK(sv) && ( isGV_with_GP(SvRV(sv))
1871 || (isio = SvTYPE(SvRV(sv)) == SVt_PVIO) )
1875 && ckWARN(WARN_IO)) {
1877 /* diag_listed_as: Use of -l on filehandle%s */
1878 Perl_warner(aTHX_ packWARN(WARN_IO),
1879 "Use of -l on filehandle");
1881 /* diag_listed_as: Use of -l on filehandle%s */
1882 Perl_warner(aTHX_ packWARN(WARN_IO),
1883 "Use of -l on filehandle %" HEKf,
1884 HEKfARG(GvENAME_HEK((const GV *)
1885 (SvROK(sv) ? SvRV(sv) : sv))));
1887 file = SvPV_flags_const_nolen(sv, flags);
1888 sv_setpv(PL_statname,file);
1889 PL_laststatval = PerlLIO_lstat(file,&PL_statcache);
1890 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && should_warn_nl(file)) {
1891 GCC_DIAG_IGNORE(-Wformat-nonliteral); /* PL_warn_nl is constant */
1892 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "lstat");
1895 return PL_laststatval;
1899 S_exec_failed(pTHX_ const char *cmd, int fd, int do_report)
1901 const int e = errno;
1902 PERL_ARGS_ASSERT_EXEC_FAILED;
1904 if (ckWARN(WARN_EXEC))
1905 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
1908 /* XXX silently ignore failures */
1909 PERL_UNUSED_RESULT(PerlLIO_write(fd, (void*)&e, sizeof(int)));
1915 Perl_do_aexec5(pTHX_ SV *really, SV **mark, SV **sp,
1916 int fd, int do_report)
1919 PERL_ARGS_ASSERT_DO_AEXEC5;
1920 #if defined(__SYMBIAN32__) || defined(__LIBCATAMOUNT__)
1921 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1926 const char *tmps = NULL;
1927 Newx(PL_Argv, sp - mark + 1, const char*);
1930 while (++mark <= sp) {
1932 *a++ = SvPV_nolen_const(*mark);
1938 tmps = SvPV_nolen_const(really);
1939 if ((!really && PL_Argv[0] && *PL_Argv[0] != '/') ||
1940 (really && *tmps != '/')) /* will execvp use PATH? */
1941 TAINT_ENV(); /* testing IFS here is overkill, probably */
1943 if (really && *tmps) {
1944 PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
1945 } else if (PL_Argv[0]) {
1946 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1948 SETERRNO(ENOENT,RMS_FNF);
1951 S_exec_failed(aTHX_ (really ? tmps : PL_Argv[0] ? PL_Argv[0] : ""), fd, do_report);
1959 Perl_do_execfree(pTHX)
1967 #ifdef PERL_DEFAULT_DO_EXEC3_IMPLEMENTATION
1970 Perl_do_exec3(pTHX_ const char *incmd, int fd, int do_report)
1977 /* Make a copy so we can change it */
1978 const Size_t cmdlen = strlen(incmd) + 1;
1980 PERL_ARGS_ASSERT_DO_EXEC3;
1982 Newx(buf, cmdlen, char);
1984 memcpy(cmd, incmd, cmdlen);
1986 while (*cmd && isSPACE(*cmd))
1989 /* save an extra exec if possible */
1993 char flags[PERL_FLAGS_MAX];
1994 if (strnEQ(cmd,PL_cshname,PL_cshlen) &&
1995 strEQs(cmd+PL_cshlen," -c")) {
1996 my_strlcpy(flags, "-c", PERL_FLAGS_MAX);
1997 s = cmd+PL_cshlen+3;
2000 my_strlcat(flags, "f", PERL_FLAGS_MAX - 2);
2005 char * const ncmd = s;
2011 if (s[-1] == '\'') {
2014 PerlProc_execl(PL_cshname, "csh", flags, ncmd, (char*)NULL);
2017 S_exec_failed(aTHX_ PL_cshname, fd, do_report);
2026 /* see if there are shell metacharacters in it */
2028 if (*cmd == '.' && isSPACE(cmd[1]))
2031 if (strEQs(cmd,"exec") && isSPACE(cmd[4]))
2035 while (isWORDCHAR(*s))
2036 s++; /* catch VAR=val gizmo */
2040 for (s = cmd; *s; s++) {
2041 if (*s != ' ' && !isALPHA(*s) &&
2042 strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
2043 if (*s == '\n' && !s[1]) {
2047 /* handle the 2>&1 construct at the end */
2048 if (*s == '>' && s[1] == '&' && s[2] == '1'
2049 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
2050 && (!s[3] || isSPACE(s[3])))
2052 const char *t = s + 3;
2054 while (*t && isSPACE(*t))
2056 if (!*t && (PerlLIO_dup2(1,2) != -1)) {
2063 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char *)NULL);
2065 S_exec_failed(aTHX_ PL_sh_path, fd, do_report);
2071 Newx(PL_Argv, (s - cmd) / 2 + 2, const char*);
2072 PL_Cmd = savepvn(cmd, s-cmd);
2074 for (s = PL_Cmd; *s;) {
2079 while (*s && !isSPACE(*s))
2087 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
2089 if (errno == ENOEXEC) { /* for system V NIH syndrome */
2093 S_exec_failed(aTHX_ PL_Argv[0], fd, do_report);
2100 #endif /* OS2 || WIN32 */
2103 Perl_apply(pTHX_ I32 type, SV **mark, SV **sp)
2107 const char *const what = PL_op_name[type];
2110 SV ** const oldmark = mark;
2111 bool killgp = FALSE;
2113 PERL_ARGS_ASSERT_APPLY;
2115 PERL_UNUSED_VAR(what); /* may not be used depending on compile options */
2117 /* Doing this ahead of the switch statement preserves the old behaviour,
2118 where attempting to use kill as a taint test test would fail on
2119 platforms where kill was not defined. */
2121 if (type == OP_KILL)
2122 Perl_die(aTHX_ PL_no_func, what);
2125 if (type == OP_CHOWN)
2126 Perl_die(aTHX_ PL_no_func, what);
2130 #define APPLY_TAINT_PROPER() \
2132 if (TAINT_get) { TAINT_PROPER(what); } \
2135 /* This is a first heuristic; it doesn't catch tainting magic. */
2137 while (++mark <= sp) {
2138 if (SvTAINTED(*mark)) {
2147 APPLY_TAINT_PROPER();
2150 APPLY_TAINT_PROPER();
2152 while (++mark <= sp) {
2154 if ((gv = MAYBE_DEREF_GV(*mark))) {
2155 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
2157 int fd = PerlIO_fileno(IoIFP(GvIOn(gv)));
2158 APPLY_TAINT_PROPER();
2160 SETERRNO(EBADF,RMS_IFI);
2162 } else if (fchmod(fd, val))
2165 Perl_die(aTHX_ PL_no_func, "fchmod");
2169 SETERRNO(EBADF,RMS_IFI);
2174 const char *name = SvPV_nomg_const(*mark, len);
2175 APPLY_TAINT_PROPER();
2176 if (!IS_SAFE_PATHNAME(name, len, "chmod") ||
2177 PerlLIO_chmod(name, val)) {
2186 APPLY_TAINT_PROPER();
2187 if (sp - mark > 2) {
2189 val = SvIVx(*++mark);
2190 val2 = SvIVx(*++mark);
2191 APPLY_TAINT_PROPER();
2193 while (++mark <= sp) {
2195 if ((gv = MAYBE_DEREF_GV(*mark))) {
2196 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
2198 int fd = PerlIO_fileno(IoIFP(GvIOn(gv)));
2199 APPLY_TAINT_PROPER();
2201 SETERRNO(EBADF,RMS_IFI);
2203 } else if (fchown(fd, val, val2))
2206 Perl_die(aTHX_ PL_no_func, "fchown");
2210 SETERRNO(EBADF,RMS_IFI);
2215 const char *name = SvPV_nomg_const(*mark, len);
2216 APPLY_TAINT_PROPER();
2217 if (!IS_SAFE_PATHNAME(name, len, "chown") ||
2218 PerlLIO_chown(name, val, val2)) {
2227 XXX Should we make lchown() directly available from perl?
2228 For now, we'll let Configure test for HAS_LCHOWN, but do
2229 nothing in the core.
2234 APPLY_TAINT_PROPER();
2237 s = SvPVx_const(*++mark, len);
2238 if (*s == '-' && isALPHA(s[1]))
2245 if (*s == 'S' && s[1] == 'I' && s[2] == 'G') {
2249 if ((val = whichsig_pvn(s, len)) < 0)
2250 Perl_croak(aTHX_ "Unrecognized signal name \"%" SVf "\"",
2262 APPLY_TAINT_PROPER();
2265 while (++mark <= sp) {
2268 if (!(SvNIOK(*mark) || looks_like_number(*mark)))
2269 Perl_croak(aTHX_ "Can't kill a non-numeric process ID");
2270 proc = SvIV_nomg(*mark);
2271 APPLY_TAINT_PROPER();
2273 /* use killpg in preference, as the killpg() wrapper for Win32
2274 * understands process groups, but the kill() wrapper doesn't */
2275 if (killgp ? PerlProc_killpg(proc, val)
2276 : PerlProc_kill(proc, val))
2278 if (PerlProc_kill(killgp ? -proc: proc, val))
2286 APPLY_TAINT_PROPER();
2288 while (++mark <= sp) {
2289 s = SvPV_const(*mark, len);
2290 APPLY_TAINT_PROPER();
2291 if (!IS_SAFE_PATHNAME(s, len, "unlink")) {
2294 else if (PL_unsafe) {
2299 #if defined(__amigaos4__) && defined(NEWLIB)
2302 /* Under AmigaOS4 unlink only 'fails' if the
2303 * filename is invalid. It may not remove the file
2304 * if it's locked, so check if it's still around. */
2305 if ((access(s,F_OK) != -1))
2312 else { /* don't let root wipe out directories without -U */
2314 if (PerlLIO_lstat(s, &statbuf) < 0)
2316 else if (S_ISDIR(statbuf.st_mode)) {
2317 SETERRNO(EISDIR, SS_NOPRIV);
2325 #if defined(__amigaos4__) && defined(NEWLIB)
2328 /* Under AmigaOS4 unlink only 'fails' if the filename is invalid */
2329 /* It may not remove the file if it's Locked, so check if it's still */
2331 if((access(s,F_OK) != -1))
2341 #if defined(HAS_UTIME) || defined(HAS_FUTIMES)
2343 APPLY_TAINT_PROPER();
2344 if (sp - mark > 2) {
2345 #if defined(HAS_FUTIMES)
2346 struct timeval utbuf[2];
2347 void *utbufp = utbuf;
2348 #elif defined(I_UTIME) || defined(VMS)
2349 struct utimbuf utbuf;
2350 struct utimbuf *utbufp = &utbuf;
2356 void *utbufp = &utbuf;
2359 SV* const accessed = *++mark;
2360 SV* const modified = *++mark;
2362 /* Be like C, and if both times are undefined, let the C
2363 * library figure out what to do. This usually means
2364 * "current time". */
2366 if ( accessed == &PL_sv_undef && modified == &PL_sv_undef )
2369 Zero(&utbuf, sizeof utbuf, char);
2371 utbuf[0].tv_sec = (long)SvIV(accessed); /* time accessed */
2372 utbuf[0].tv_usec = 0;
2373 utbuf[1].tv_sec = (long)SvIV(modified); /* time modified */
2374 utbuf[1].tv_usec = 0;
2375 #elif defined(BIG_TIME)
2376 utbuf.actime = (Time_t)SvNV(accessed); /* time accessed */
2377 utbuf.modtime = (Time_t)SvNV(modified); /* time modified */
2379 utbuf.actime = (Time_t)SvIV(accessed); /* time accessed */
2380 utbuf.modtime = (Time_t)SvIV(modified); /* time modified */
2383 APPLY_TAINT_PROPER();
2385 while (++mark <= sp) {
2387 if ((gv = MAYBE_DEREF_GV(*mark))) {
2388 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
2390 int fd = PerlIO_fileno(IoIFP(GvIOn(gv)));
2391 APPLY_TAINT_PROPER();
2393 SETERRNO(EBADF,RMS_IFI);
2395 } else if (futimes(fd, (struct timeval *) utbufp))
2398 Perl_die(aTHX_ PL_no_func, "futimes");
2406 const char * const name = SvPV_nomg_const(*mark, len);
2407 APPLY_TAINT_PROPER();
2408 if (!IS_SAFE_PATHNAME(name, len, "utime")) {
2413 if (utimes(name, (struct timeval *)utbufp))
2415 if (PerlLIO_utime(name, utbufp))
2429 #undef APPLY_TAINT_PROPER
2432 /* Do the permissions in *statbufp allow some operation? */
2433 #ifndef VMS /* VMS' cando is in vms.c */
2435 Perl_cando(pTHX_ Mode_t mode, bool effective, const Stat_t *statbufp)
2436 /* effective is a flag, true for EUID, or for checking if the effective gid
2437 * is in the list of groups returned from getgroups().
2440 PERL_ARGS_ASSERT_CANDO;
2441 PERL_UNUSED_CONTEXT;
2444 /* [Comments and code from Len Reed]
2445 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
2446 * to write-protected files. The execute permission bit is set
2447 * by the Microsoft C library stat() function for the following:
2452 * All files and directories are readable.
2453 * Directories and special files, e.g. "CON", cannot be
2455 * [Comment by Tom Dinger -- a directory can have the write-protect
2456 * bit set in the file system, but DOS permits changes to
2457 * the directory anyway. In addition, all bets are off
2458 * here for networked software, such as Novell and
2462 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
2463 * too so it will actually look into the files for magic numbers
2465 return cBOOL(mode & statbufp->st_mode);
2467 #else /* ! DOSISH */
2469 if (ingroup(544,effective)) { /* member of Administrators */
2471 if ((effective ? PerlProc_geteuid() : PerlProc_getuid()) == 0) { /* root is special */
2473 if (mode == S_IXUSR) {
2474 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
2478 return TRUE; /* root reads and writes anything */
2481 if (statbufp->st_uid == (effective ? PerlProc_geteuid() : PerlProc_getuid()) ) {
2482 if (statbufp->st_mode & mode)
2483 return TRUE; /* ok as "user" */
2485 else if (ingroup(statbufp->st_gid,effective)) {
2486 if (statbufp->st_mode & mode >> 3)
2487 return TRUE; /* ok as "group" */
2489 else if (statbufp->st_mode & mode >> 6)
2490 return TRUE; /* ok as "other" */
2492 #endif /* ! DOSISH */
2497 S_ingroup(pTHX_ Gid_t testgid, bool effective)
2499 #ifndef PERL_IMPLICIT_SYS
2500 /* PERL_IMPLICIT_SYS like Win32: getegid() etc. require the context. */
2501 PERL_UNUSED_CONTEXT;
2503 if (testgid == (effective ? PerlProc_getegid() : PerlProc_getgid()))
2505 #ifdef HAS_GETGROUPS
2507 Groups_t *gary = NULL;
2511 anum = getgroups(0, gary);
2513 Newx(gary, anum, Groups_t);
2514 anum = getgroups(anum, gary);
2516 if (gary[anum] == testgid) {
2530 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
2533 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
2535 const key_t key = (key_t)SvNVx(*++mark);
2536 SV *nsv = optype == OP_MSGGET ? NULL : *++mark;
2537 const I32 flags = SvIVx(*++mark);
2539 PERL_ARGS_ASSERT_DO_IPCGET;
2540 PERL_UNUSED_ARG(sp);
2547 return msgget(key, flags);
2551 return semget(key, (int) SvIV(nsv), flags);
2555 return shmget(key, (size_t) SvUV(nsv), flags);
2557 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
2559 /* diag_listed_as: msg%s not implemented */
2560 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2563 return -1; /* should never happen */
2567 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
2571 const I32 id = SvIVx(*++mark);
2573 const I32 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
2575 const I32 cmd = SvIVx(*++mark);
2576 SV * const astr = *++mark;
2577 STRLEN infosize = 0;
2578 I32 getinfo = (cmd == IPC_STAT);
2580 PERL_ARGS_ASSERT_DO_IPCCTL;
2581 PERL_UNUSED_ARG(sp);
2587 if (cmd == IPC_STAT || cmd == IPC_SET)
2588 infosize = sizeof(struct msqid_ds);
2593 if (cmd == IPC_STAT || cmd == IPC_SET)
2594 infosize = sizeof(struct shmid_ds);
2600 if (cmd == IPC_STAT || cmd == IPC_SET)
2601 infosize = sizeof(struct semid_ds);
2602 else if (cmd == GETALL || cmd == SETALL)
2604 struct semid_ds semds;
2606 #ifdef EXTRA_F_IN_SEMUN_BUF
2607 semun.buff = &semds;
2611 getinfo = (cmd == GETALL);
2612 if (Semctl(id, 0, IPC_STAT, semun) == -1)
2614 infosize = semds.sem_nsems * sizeof(short);
2615 /* "short" is technically wrong but much more portable
2616 than guessing about u_?short(_t)? */
2619 /* diag_listed_as: sem%s not implemented */
2620 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2624 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
2626 /* diag_listed_as: shm%s not implemented */
2627 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2635 SvPV_force_nolen(astr);
2636 a = SvGROW(astr, infosize+1);
2641 a = SvPV(astr, len);
2642 if (len != infosize)
2643 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
2651 const IV i = SvIV(astr);
2652 a = INT2PTR(char *,i); /* ouch */
2659 ret = msgctl(id, cmd, (struct msqid_ds *)a);
2665 union semun unsemds;
2668 unsemds.val = PTR2nat(a);
2671 #ifdef EXTRA_F_IN_SEMUN_BUF
2672 unsemds.buff = (struct semid_ds *)a;
2674 unsemds.buf = (struct semid_ds *)a;
2677 ret = Semctl(id, n, cmd, unsemds);
2679 /* diag_listed_as: sem%s not implemented */
2680 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2687 ret = shmctl(id, cmd, (struct shmid_ds *)a);
2691 if (getinfo && ret >= 0) {
2692 SvCUR_set(astr, infosize);
2693 *SvEND(astr) = '\0';
2700 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
2704 const I32 id = SvIVx(*++mark);
2705 SV * const mstr = *++mark;
2706 const I32 flags = SvIVx(*++mark);
2707 const char * const mbuf = SvPV_const(mstr, len);
2708 const I32 msize = len - sizeof(long);
2710 PERL_ARGS_ASSERT_DO_MSGSND;
2711 PERL_UNUSED_ARG(sp);
2714 Perl_croak(aTHX_ "Arg too short for msgsnd");
2716 if (id >= 0 && flags >= 0) {
2717 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
2719 SETERRNO(EINVAL,LIB_INVARG);
2723 PERL_UNUSED_ARG(sp);
2724 PERL_UNUSED_ARG(mark);
2725 /* diag_listed_as: msg%s not implemented */
2726 Perl_croak(aTHX_ "msgsnd not implemented");
2732 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
2737 I32 msize, flags, ret;
2738 const I32 id = SvIVx(*++mark);
2739 SV * const mstr = *++mark;
2741 PERL_ARGS_ASSERT_DO_MSGRCV;
2742 PERL_UNUSED_ARG(sp);
2744 /* suppress warning when reading into undef var --jhi */
2747 msize = SvIVx(*++mark);
2748 mtype = (long)SvIVx(*++mark);
2749 flags = SvIVx(*++mark);
2750 SvPV_force_nolen(mstr);
2751 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
2754 if (id >= 0 && msize >= 0 && flags >= 0) {
2755 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
2757 SETERRNO(EINVAL,LIB_INVARG);
2761 SvCUR_set(mstr, sizeof(long)+ret);
2762 *SvEND(mstr) = '\0';
2763 /* who knows who has been playing with this message? */
2768 PERL_UNUSED_ARG(sp);
2769 PERL_UNUSED_ARG(mark);
2770 /* diag_listed_as: msg%s not implemented */
2771 Perl_croak(aTHX_ "msgrcv not implemented");
2777 Perl_do_semop(pTHX_ SV **mark, SV **sp)
2781 const I32 id = SvIVx(*++mark);
2782 SV * const opstr = *++mark;
2783 const char * const opbuf = SvPV_const(opstr, opsize);
2785 PERL_ARGS_ASSERT_DO_SEMOP;
2786 PERL_UNUSED_ARG(sp);
2788 if (opsize < 3 * SHORTSIZE
2789 || (opsize % (3 * SHORTSIZE))) {
2790 SETERRNO(EINVAL,LIB_INVARG);
2794 /* We can't assume that sizeof(struct sembuf) == 3 * sizeof(short). */
2796 const int nsops = opsize / (3 * sizeof (short));
2798 short * const ops = (short *) opbuf;
2800 struct sembuf *temps, *t;
2803 Newx (temps, nsops, struct sembuf);
2811 result = semop(id, temps, nsops);
2816 /* diag_listed_as: sem%s not implemented */
2817 Perl_croak(aTHX_ "semop not implemented");
2822 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
2826 struct shmid_ds shmds;
2827 const I32 id = SvIVx(*++mark);
2828 SV * const mstr = *++mark;
2829 const I32 mpos = SvIVx(*++mark);
2830 const I32 msize = SvIVx(*++mark);
2832 PERL_ARGS_ASSERT_DO_SHMIO;
2833 PERL_UNUSED_ARG(sp);
2836 if (shmctl(id, IPC_STAT, &shmds) == -1)
2838 if (mpos < 0 || msize < 0
2839 || (size_t)mpos + msize > (size_t)shmds.shm_segsz) {
2840 SETERRNO(EFAULT,SS_ACCVIO); /* can't do as caller requested */
2844 shm = (char *)shmat(id, NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
2846 SETERRNO(EINVAL,LIB_INVARG);
2849 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2851 if (optype == OP_SHMREAD) {
2853 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2855 SvUPGRADE(mstr, SVt_PV);
2859 mbuf = SvGROW(mstr, (STRLEN)msize+1);
2861 Copy(shm + mpos, mbuf, msize, char);
2862 SvCUR_set(mstr, msize);
2863 *SvEND(mstr) = '\0';
2865 /* who knows who has been playing with this shared memory? */
2871 const char *mbuf = SvPV_const(mstr, len);
2872 const I32 n = ((I32)len > msize) ? msize : (I32)len;
2873 Copy(mbuf, shm + mpos, n, char);
2875 memzero(shm + mpos + n, msize - n);
2879 /* diag_listed_as: shm%s not implemented */
2880 Perl_croak(aTHX_ "shm I/O not implemented");
2885 #endif /* SYSV IPC */
2890 =for apidoc start_glob
2892 Function called by C<do_readline> to spawn a glob (or do the glob inside
2893 perl on VMS). This code used to be inline, but now perl uses C<File::Glob>
2894 this glob starter is only used by miniperl during the build process,
2895 or when PERL_EXTERNAL_GLOB is defined.
2896 Moving it away shrinks F<pp_hot.c>; shrinking F<pp_hot.c> helps speed perl up.
2902 Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
2904 SV * const tmpcmd = newSV(0);
2907 const char *s = SvPV(tmpglob, len);
2909 PERL_ARGS_ASSERT_START_GLOB;
2911 if (!IS_SAFE_SYSCALL(s, len, "pattern", "glob"))
2916 #ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
2917 /* since spawning off a process is a real performance hit */
2919 fp = Perl_vms_start_glob(aTHX_ tmpglob, io);
2924 sv_setpv(tmpcmd, "for a in ");
2925 sv_catsv(tmpcmd, tmpglob);
2926 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
2927 # elif defined(DJGPP)
2928 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
2929 sv_catsv(tmpcmd, tmpglob);
2931 sv_setpv(tmpcmd, "perlglob ");
2932 sv_catsv(tmpcmd, tmpglob);
2933 sv_catpv(tmpcmd, " |");
2936 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
2937 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
2938 sv_catsv(tmpcmd, tmpglob);
2939 sv_catpv(tmpcmd, "' 2>/dev/null |");
2941 sv_setpv(tmpcmd, "echo ");
2942 sv_catsv(tmpcmd, tmpglob);
2943 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
2944 # endif /* !DOSISH && !CSH */
2946 SV ** const svp = hv_fetchs(GvHVn(PL_envgv), "LS_COLORS", 0);
2948 save_helem_flags(GvHV(PL_envgv),
2949 newSVpvs_flags("LS_COLORS", SVs_TEMP), svp,
2952 (void)do_open6(PL_last_in_gv, SvPVX_const(tmpcmd), SvCUR(tmpcmd),
2958 if (!fp && ckWARN(WARN_GLOB)) {
2959 Perl_warner(aTHX_ packWARN(WARN_GLOB), "glob failed (can't start child: %s)",
2967 * ex: set ts=8 sts=4 sw=4 et: