3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, 2004, 2005, 2006, 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."
18 /* This file contains functions that do the actual I/O on behalf of ops.
19 * For example, pp_print() calls the do_print() function in this file for
20 * each argument needing printing.
24 #define PERL_IN_DOIO_C
27 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
36 # ifndef HAS_SHMAT_PROTOTYPE
37 extern Shmat_t shmat (int, char *, int);
43 # if defined(_MSC_VER) || defined(__MINGW32__)
44 # include <sys/utime.h>
51 # define OPEN_EXCL O_EXCL
56 #define PERL_MODE_MAX 8
57 #define PERL_FLAGS_MAX 10
62 Perl_do_openn(pTHX_ GV *gv, register const char *oname, I32 len, int as_raw,
63 int rawmode, int rawperm, PerlIO *supplied_fp, SV **svp,
67 register IO * const io = GvIOn(gv);
68 PerlIO *saveifp = NULL;
69 PerlIO *saveofp = NULL;
71 char savetype = IoTYPE_CLOSED;
76 bool was_fdopen = FALSE;
77 bool in_raw = 0, in_crlf = 0, out_raw = 0, out_crlf = 0;
79 char mode[PERL_MODE_MAX]; /* file mode ("r\0", "rb\0", "ab\0" etc.) */
82 Zero(mode,sizeof(mode),char);
83 PL_forkprocess = 1; /* assume true if no fork */
85 /* Collect default raw/crlf info from the op */
86 if (PL_op && PL_op->op_type == OP_OPEN) {
87 /* set up IO layers */
88 const U8 flags = PL_op->op_private;
89 in_raw = (flags & OPpOPEN_IN_RAW);
90 in_crlf = (flags & OPpOPEN_IN_CRLF);
91 out_raw = (flags & OPpOPEN_OUT_RAW);
92 out_crlf = (flags & OPpOPEN_OUT_CRLF);
95 /* If currently open - close before we re-open */
97 fd = PerlIO_fileno(IoIFP(io));
98 if (IoTYPE(io) == IoTYPE_STD) {
99 /* This is a clone of one of STD* handles */
102 else if (fd >= 0 && fd <= PL_maxsysfd) {
103 /* This is one of the original STD* handles */
106 savetype = IoTYPE(io);
110 else if (IoTYPE(io) == IoTYPE_PIPE)
111 result = PerlProc_pclose(IoIFP(io));
112 else if (IoIFP(io) != IoOFP(io)) {
114 result = PerlIO_close(IoOFP(io));
115 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
118 result = PerlIO_close(IoIFP(io));
121 result = PerlIO_close(IoIFP(io));
122 if (result == EOF && fd > PL_maxsysfd) {
123 /* Why is this not Perl_warn*() call ? */
124 PerlIO_printf(Perl_error_log,
125 "Warning: unable to close filehandle %s properly.\n",
128 IoOFP(io) = IoIFP(io) = NULL;
132 /* sysopen style args, i.e. integer mode and permissions */
134 const int appendtrunc =
136 #ifdef O_APPEND /* Not fully portable. */
139 #ifdef O_TRUNC /* Not fully portable. */
143 const int modifyingmode = O_WRONLY|O_RDWR|O_CREAT|appendtrunc;
147 Perl_croak(aTHX_ "panic: sysopen with multiple args");
155 It might be (in OS/390 and Mac OS Classic it is)
161 This means that simple & with O_RDWR would look
162 like O_RDONLY is present. Therefore we have to
165 if ((ismodifying = (rawmode & modifyingmode))) {
166 if ((ismodifying & O_WRONLY) == O_WRONLY ||
167 (ismodifying & O_RDWR) == O_RDWR ||
168 (ismodifying & (O_CREAT|appendtrunc)))
169 TAINT_PROPER("sysopen");
171 mode[ix++] = IoTYPE_NUMERIC; /* Marker to openn to use numeric "sysopen" */
173 #if defined(USE_64_BIT_RAWIO) && defined(O_LARGEFILE)
174 rawmode |= O_LARGEFILE; /* Transparently largefiley. */
177 IoTYPE(io) = PerlIO_intmode2str(rawmode, &mode[ix], &writing);
179 namesv = sv_2mortal(newSVpv(oname,0));
183 fp = PerlIO_openn(aTHX_ type, mode, -1, rawmode, rawperm, NULL, num_svs, svp);
186 /* Regular (non-sys) open */
192 type = savepvn(oname, len);
196 /* Lose leading and trailing white space */
197 while (isSPACE(*type))
199 while (tend > type && isSPACE(tend[-1]))
203 /* New style explicit name, type is just mode and layer info */
205 if (SvROK(*svp) && !strchr(oname,'&')) {
207 Perl_warner(aTHX_ packWARN(WARN_IO),
208 "Can't open a reference");
209 SETERRNO(EINVAL, LIB_INVARG);
212 #endif /* USE_STDIO */
213 name = SvOK(*svp) ? savesvpv (*svp) : savepvn ("", 0);
221 if ((*type == IoTYPE_RDWR) && /* scary */
222 (*(type+1) == IoTYPE_RDONLY || *(type+1) == IoTYPE_WRONLY) &&
223 ((!num_svs || (tend > type+1 && tend[-1] != IoTYPE_PIPE)))) {
224 TAINT_PROPER("open");
229 if (*type == IoTYPE_PIPE) {
231 if (type[1] != IoTYPE_STD) {
233 Perl_croak(aTHX_ "Unknown open() mode '%.*s'", (int)olen, oname);
239 } while (isSPACE(*type));
245 /* command is missing 19990114 */
246 if (ckWARN(WARN_PIPE))
247 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open");
251 if (!(*name == '-' && name[1] == '\0') || num_svs)
253 TAINT_PROPER("piped open");
254 if (!num_svs && name[len-1] == '|') {
256 if (ckWARN(WARN_PIPE))
257 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Can't open bidirectional pipe");
262 my_strlcat(mode, "b", PERL_MODE_MAX - 1);
264 my_strlcat(mode, "t", PERL_MODE_MAX - 1);
266 fp = PerlProc_popen_list(mode, num_svs, svp);
269 fp = PerlProc_popen(name,mode);
273 if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) {
279 else if (*type == IoTYPE_WRONLY) {
280 TAINT_PROPER("open");
282 if (*type == IoTYPE_WRONLY) {
283 /* Two IoTYPE_WRONLYs in a row make for an IoTYPE_APPEND. */
284 mode[0] = IoTYPE(io) = IoTYPE_APPEND;
293 my_strlcat(mode, "b", PERL_MODE_MAX - 1);
295 my_strlcat(mode, "t", PERL_MODE_MAX - 1);
298 dodup = PERLIO_DUP_FD;
304 if (!num_svs && !*type && supplied_fp) {
305 /* "<+&" etc. is used by typemaps */
309 PerlIO *that_fp = NULL;
311 Perl_croak(aTHX_ "More than one argument to '%c&' open",IoTYPE(io));
313 while (isSPACE(*type))
315 if (num_svs && (SvIOK(*svp) || (SvPOK(*svp) && looks_like_number(*svp)))) {
319 else if (isDIGIT(*type)) {
325 thatio = sv_2io(*svp);
328 GV * const thatgv = gv_fetchpvn_flags(type, tend - type,
330 thatio = GvIO(thatgv);
334 SETERRNO(EINVAL,SS_IVCHAN);
338 if ((that_fp = IoIFP(thatio))) {
339 /* Flush stdio buffer before dup. --mjd
340 * Unfortunately SEEK_CURing 0 seems to
341 * be optimized away on most platforms;
342 * only Solaris and Linux seem to flush
345 /* sfio fails to clear error on next
346 sfwrite, contrary to documentation.
348 if (PerlIO_seek(that_fp, 0, SEEK_CUR) == -1)
349 PerlIO_clearerr(that_fp);
351 /* On the other hand, do all platforms
352 * take gracefully to flushing a read-only
353 * filehandle? Perhaps we should do
354 * fsetpos(src)+fgetpos(dst)? --nik */
355 PerlIO_flush(that_fp);
356 fd = PerlIO_fileno(that_fp);
357 /* When dup()ing STDIN, STDOUT or STDERR
358 * explicitly set appropriate access mode */
359 if (that_fp == PerlIO_stdout()
360 || that_fp == PerlIO_stderr())
361 IoTYPE(io) = IoTYPE_WRONLY;
362 else if (that_fp == PerlIO_stdin())
363 IoTYPE(io) = IoTYPE_RDONLY;
364 /* When dup()ing a socket, say result is
366 else if (IoTYPE(thatio) == IoTYPE_SOCKET)
367 IoTYPE(io) = IoTYPE_SOCKET;
375 fp = PerlIO_fdupopen(aTHX_ that_fp, NULL, dodup);
379 fd = PerlLIO_dup(fd);
382 if (!(fp = PerlIO_openn(aTHX_ type,mode,fd,0,0,NULL,num_svs,svp))) {
383 if (dodup && fd >= 0)
390 while (isSPACE(*type))
392 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
394 fp = PerlIO_stdout();
395 IoTYPE(io) = IoTYPE_STD;
397 Perl_croak(aTHX_ "More than one argument to '>%c' open",IoTYPE_STD);
402 namesv = sv_2mortal(newSVpvn(type,tend - type));
407 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
410 if (!fp && type && *type && *type != ':' && !isIDFIRST(*type))
411 goto unknown_open_mode;
412 } /* IoTYPE_WRONLY */
413 else if (*type == IoTYPE_RDONLY) {
416 } while (isSPACE(*type));
419 my_strlcat(mode, "b", PERL_MODE_MAX - 1);
421 my_strlcat(mode, "t", PERL_MODE_MAX - 1);
425 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
428 IoTYPE(io) = IoTYPE_STD;
430 Perl_croak(aTHX_ "More than one argument to '<%c' open",IoTYPE_STD);
435 namesv = sv_2mortal(newSVpvn(type,tend - type));
440 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
442 if (!fp && type && *type && *type != ':' && !isIDFIRST(*type))
443 goto unknown_open_mode;
444 } /* IoTYPE_RDONLY */
445 else if ((num_svs && /* '-|...' or '...|' */
446 type[0] == IoTYPE_STD && type[1] == IoTYPE_PIPE) ||
447 (!num_svs && tend > type+1 && tend[-1] == IoTYPE_PIPE)) {
449 type += 2; /* skip over '-|' */
453 while (tend > type && isSPACE(tend[-1]))
455 for (; isSPACE(*type); type++)
461 /* command is missing 19990114 */
462 if (ckWARN(WARN_PIPE))
463 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open");
467 if (!(*name == '-' && name[1] == '\0') || num_svs)
469 TAINT_PROPER("piped open");
473 my_strlcat(mode, "b", PERL_MODE_MAX - 1);
475 my_strlcat(mode, "t", PERL_MODE_MAX - 1);
478 fp = PerlProc_popen_list(mode,num_svs,svp);
481 fp = PerlProc_popen(name,mode);
483 IoTYPE(io) = IoTYPE_PIPE;
485 while (isSPACE(*type))
488 if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) {
494 else { /* layer(Args) */
496 goto unknown_open_mode;
498 IoTYPE(io) = IoTYPE_RDONLY;
499 for (; isSPACE(*name); name++)
504 my_strlcat(mode, "b", PERL_MODE_MAX - 1);
506 my_strlcat(mode, "t", PERL_MODE_MAX - 1);
508 if (*name == '-' && name[1] == '\0') {
510 IoTYPE(io) = IoTYPE_STD;
514 namesv = sv_2mortal(newSVpvn(type,tend - type));
519 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
524 if (IoTYPE(io) == IoTYPE_RDONLY && ckWARN(WARN_NEWLINE)
525 && strchr(oname, '\n')
528 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
532 if (ckWARN(WARN_IO)) {
533 if ((IoTYPE(io) == IoTYPE_RDONLY) &&
534 (fp == PerlIO_stdout() || fp == PerlIO_stderr())) {
535 Perl_warner(aTHX_ packWARN(WARN_IO),
536 "Filehandle STD%s reopened as %s only for input",
537 ((fp == PerlIO_stdout()) ? "OUT" : "ERR"),
540 else if ((IoTYPE(io) == IoTYPE_WRONLY) && fp == PerlIO_stdin()) {
541 Perl_warner(aTHX_ packWARN(WARN_IO),
542 "Filehandle STDIN reopened as %s only for output",
547 fd = PerlIO_fileno(fp);
548 /* If there is no fd (e.g. PerlIO::scalar) assume it isn't a
549 * socket - this covers PerlIO::scalar - otherwise unless we "know" the
550 * type probe for socket-ness.
552 if (IoTYPE(io) && IoTYPE(io) != IoTYPE_PIPE && IoTYPE(io) != IoTYPE_STD && fd >= 0) {
553 if (PerlLIO_fstat(fd,&PL_statbuf) < 0) {
554 /* If PerlIO claims to have fd we had better be able to fstat() it. */
555 (void) PerlIO_close(fp);
559 if (S_ISSOCK(PL_statbuf.st_mode))
560 IoTYPE(io) = IoTYPE_SOCKET; /* in case a socket was passed in to us */
564 !(PL_statbuf.st_mode & S_IFMT)
568 && IoTYPE(io) != IoTYPE_WRONLY /* Dups of STD* filehandles already have */
569 && IoTYPE(io) != IoTYPE_RDONLY /* type so they aren't marked as sockets */
570 ) { /* on OS's that return 0 on fstat()ed pipe */
572 Sock_size_t buflen = sizeof tmpbuf;
573 if (PerlSock_getsockname(fd, (struct sockaddr *)tmpbuf, &buflen) >= 0
574 || errno != ENOTSOCK)
575 IoTYPE(io) = IoTYPE_SOCKET; /* some OS's return 0 on fstat()ed socket */
576 /* but some return 0 for streams too, sigh */
578 #endif /* HAS_SOCKET */
579 #endif /* !PERL_MICRO */
583 * If this is a standard handle we discard all the layer stuff
584 * and just dup the fd into whatever was on the handle before !
587 if (saveifp) { /* must use old fp? */
588 /* If fd is less that PL_maxsysfd i.e. STDIN..STDERR
589 then dup the new fileno down
592 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
593 if (saveofp != saveifp) { /* was a socket? */
594 PerlIO_close(saveofp);
598 /* Still a small can-of-worms here if (say) PerlIO::scalar
599 is assigned to (say) STDOUT - for now let dup2() fail
600 and provide the error
602 if (PerlLIO_dup2(fd, savefd) < 0) {
603 (void)PerlIO_close(fp);
607 if (savefd != PerlIO_fileno(PerlIO_stdin())) {
608 char newname[FILENAME_MAX+1];
609 if (PerlIO_getname(fp, newname)) {
610 if (fd == PerlIO_fileno(PerlIO_stdout()))
611 Perl_vmssetuserlnm(aTHX_ "SYS$OUTPUT", newname);
612 if (fd == PerlIO_fileno(PerlIO_stderr()))
613 Perl_vmssetuserlnm(aTHX_ "SYS$ERROR", newname);
619 /* PL_fdpid isn't used on Windows, so avoid this useless work.
620 * XXX Probably the same for a lot of other places. */
626 sv = *av_fetch(PL_fdpid,fd,TRUE);
627 SvUPGRADE(sv, SVt_IV);
630 sv = *av_fetch(PL_fdpid,savefd,TRUE);
631 SvUPGRADE(sv, SVt_IV);
638 /* need to close fp without closing underlying fd */
639 int ofd = PerlIO_fileno(fp);
640 int dupfd = PerlLIO_dup(ofd);
641 #if defined(HAS_FCNTL) && defined(F_SETFD)
642 /* Assume if we have F_SETFD we have F_GETFD */
643 int coe = fcntl(ofd,F_GETFD);
646 PerlLIO_dup2(dupfd,ofd);
647 #if defined(HAS_FCNTL) && defined(F_SETFD)
648 /* The dup trick has lost close-on-exec on ofd */
649 fcntl(ofd,F_SETFD, coe);
651 PerlLIO_close(dupfd);
658 fd = PerlIO_fileno(fp);
660 #if defined(HAS_FCNTL) && defined(F_SETFD)
662 const int save_errno = errno;
663 fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
669 IoFLAGS(io) &= ~IOf_NOLINE;
671 if (IoTYPE(io) == IoTYPE_SOCKET
672 || (IoTYPE(io) == IoTYPE_WRONLY && fd >= 0 && S_ISCHR(PL_statbuf.st_mode)) ) {
674 if (*s == IoTYPE_IMPLICIT || *s == IoTYPE_NUMERIC)
677 if (!(IoOFP(io) = PerlIO_openn(aTHX_ type,s,fd,0,0,NULL,0,svp))) {
691 IoTYPE(io) = savetype;
696 Perl_nextargv(pTHX_ register GV *gv)
700 #ifndef FLEXFILENAMES
706 IO * const io = GvIOp(gv);
709 PL_argvoutgv = gv_fetchpvs("ARGVOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
710 if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
711 IoFLAGS(io) &= ~IOf_START;
713 if (!PL_argvout_stack)
714 PL_argvout_stack = newAV();
716 av_push(PL_argvout_stack, SvREFCNT_inc_simple_NN(PL_defoutgv));
719 if (PL_filemode & (S_ISUID|S_ISGID)) {
720 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
723 (void)fchmod(PL_lastfd,PL_filemode);
725 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
732 while (av_len(GvAV(gv)) >= 0) {
734 sv = av_shift(GvAV(gv));
736 sv_setsv(GvSVn(gv),sv);
737 SvSETMAGIC(GvSV(gv));
738 PL_oldname = SvPVx(GvSV(gv), oldlen);
739 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,NULL)) {
741 TAINT_PROPER("inplace open");
742 if (oldlen == 1 && *PL_oldname == '-') {
743 setdefout(gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL,
745 return IoIFP(GvIOp(gv));
747 #ifndef FLEXFILENAMES
748 filedev = PL_statbuf.st_dev;
749 fileino = PL_statbuf.st_ino;
751 PL_filemode = PL_statbuf.st_mode;
752 fileuid = PL_statbuf.st_uid;
753 filegid = PL_statbuf.st_gid;
754 if (!S_ISREG(PL_filemode)) {
755 if (ckWARN_d(WARN_INPLACE))
756 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
757 "Can't do inplace edit: %s is not a regular file",
763 const char *star = strchr(PL_inplace, '*');
765 const char *begin = PL_inplace;
766 sv_setpvn(sv, "", 0);
768 sv_catpvn(sv, begin, star - begin);
769 sv_catpvn(sv, PL_oldname, oldlen);
771 } while ((star = strchr(begin, '*')));
776 sv_catpv(sv,PL_inplace);
778 #ifndef FLEXFILENAMES
779 if ((PerlLIO_stat(SvPVX_const(sv),&PL_statbuf) >= 0
780 && PL_statbuf.st_dev == filedev
781 && PL_statbuf.st_ino == fileino)
783 || ((_djstat_fail_bits & _STFAIL_TRUENAME)!=0)
787 if (ckWARN_d(WARN_INPLACE))
788 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
789 "Can't do inplace edit: %"SVf" would not be unique",
796 #if !defined(DOSISH) && !defined(__CYGWIN__) && !defined(EPOC)
797 if (PerlLIO_rename(PL_oldname,SvPVX_const(sv)) < 0) {
798 if (ckWARN_d(WARN_INPLACE))
799 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
800 "Can't rename %s to %"SVf": %s, skipping file",
801 PL_oldname, (void*)sv, Strerror(errno));
807 (void)PerlLIO_unlink(SvPVX_const(sv));
808 (void)PerlLIO_rename(PL_oldname,SvPVX_const(sv));
809 do_open(gv,(char*)SvPVX_const(sv),SvCUR(sv),PL_inplace!=0,
813 (void)UNLINK(SvPVX_const(sv));
814 if (link(PL_oldname,SvPVX_const(sv)) < 0) {
815 if (ckWARN_d(WARN_INPLACE))
816 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
817 "Can't rename %s to %"SVf": %s, skipping file",
818 PL_oldname, sv, Strerror(errno) );
822 (void)UNLINK(PL_oldname);
826 #if !defined(DOSISH) && !defined(AMIGAOS)
827 # ifndef VMS /* Don't delete; use automatic file versioning */
828 if (UNLINK(PL_oldname) < 0) {
829 if (ckWARN_d(WARN_INPLACE))
830 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
831 "Can't remove %s: %s, skipping file",
832 PL_oldname, Strerror(errno) );
838 Perl_croak(aTHX_ "Can't do inplace edit without backup");
842 sv_setpvn(sv,">",!PL_inplace);
843 sv_catpvn(sv,PL_oldname,oldlen);
844 SETERRNO(0,0); /* in case sprintf set errno */
846 if (!do_open(PL_argvoutgv,(char*)SvPVX_const(sv),SvCUR(sv),
847 PL_inplace!=0,O_WRONLY|O_CREAT|O_TRUNC,0,NULL))
849 if (!do_open(PL_argvoutgv,(char*)SvPVX_const(sv),SvCUR(sv),
850 PL_inplace!=0,O_WRONLY|O_CREAT|OPEN_EXCL,0666,
854 if (ckWARN_d(WARN_INPLACE))
855 Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't do inplace edit on %s: %s",
856 PL_oldname, Strerror(errno) );
860 setdefout(PL_argvoutgv);
861 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
862 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
864 (void)fchmod(PL_lastfd,PL_filemode);
866 # if !(defined(WIN32) && defined(__BORLANDC__))
867 /* Borland runtime creates a readonly file! */
868 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
871 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
873 (void)fchown(PL_lastfd,fileuid,filegid);
876 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
881 return IoIFP(GvIOp(gv));
884 if (ckWARN_d(WARN_INPLACE)) {
885 const int eno = errno;
886 if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0
887 && !S_ISREG(PL_statbuf.st_mode))
889 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
890 "Can't do inplace edit: %s is not a regular file",
894 Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't open %s: %s",
895 PL_oldname, Strerror(eno));
899 if (io && (IoFLAGS(io) & IOf_ARGV))
900 IoFLAGS(io) |= IOf_START;
902 (void)do_close(PL_argvoutgv,FALSE);
903 if (io && (IoFLAGS(io) & IOf_ARGV)
904 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
906 GV * const oldout = (GV*)av_pop(PL_argvout_stack);
908 SvREFCNT_dec(oldout);
911 setdefout(gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO));
916 /* explicit renamed to avoid C++ conflict -- kja */
918 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
926 if (!gv || SvTYPE(gv) != SVt_PVGV) {
928 SETERRNO(EBADF,SS_IVCHAN);
932 if (!io) { /* never opened */
934 if (ckWARN(WARN_UNOPENED)) /* no check for closed here */
935 report_evil_fh(gv, io, PL_op->op_type);
936 SETERRNO(EBADF,SS_IVCHAN);
940 retval = io_close(io, not_implicit);
944 IoLINES_LEFT(io) = IoPAGE_LEN(io);
946 IoTYPE(io) = IoTYPE_CLOSED;
951 Perl_io_close(pTHX_ IO *io, bool not_implicit)
957 if (IoTYPE(io) == IoTYPE_PIPE) {
958 const int status = PerlProc_pclose(IoIFP(io));
960 STATUS_NATIVE_CHILD_SET(status);
961 retval = (STATUS_UNIX == 0);
964 retval = (status != -1);
967 else if (IoTYPE(io) == IoTYPE_STD)
970 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
971 const bool prev_err = PerlIO_error(IoOFP(io));
972 retval = (PerlIO_close(IoOFP(io)) != EOF && !prev_err);
973 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
976 const bool prev_err = PerlIO_error(IoIFP(io));
977 retval = (PerlIO_close(IoIFP(io)) != EOF && !prev_err);
980 IoOFP(io) = IoIFP(io) = NULL;
982 else if (not_implicit) {
983 SETERRNO(EBADF,SS_IVCHAN);
990 Perl_do_eof(pTHX_ GV *gv)
993 register IO * const io = GvIO(gv);
997 else if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
998 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1001 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
1002 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
1003 return FALSE; /* this is the most usual case */
1007 /* getc and ungetc can stomp on errno */
1008 const int saverrno = errno;
1009 const int ch = PerlIO_getc(IoIFP(io));
1011 (void)PerlIO_ungetc(IoIFP(io),ch);
1018 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
1019 if (PerlIO_get_cnt(IoIFP(io)) < -1)
1020 PerlIO_set_cnt(IoIFP(io),-1);
1022 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
1023 if (gv != PL_argvgv || !nextargv(gv)) /* get another fp handy */
1027 return TRUE; /* normal fp, definitely end of file */
1033 Perl_do_tell(pTHX_ GV *gv)
1036 register IO *io = NULL;
1037 register PerlIO *fp;
1039 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1040 #ifdef ULTRIX_STDIO_BOTCH
1042 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1044 return PerlIO_tell(fp);
1046 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1047 report_evil_fh(gv, io, PL_op->op_type);
1048 SETERRNO(EBADF,RMS_IFI);
1053 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
1056 register IO *io = NULL;
1057 register PerlIO *fp;
1059 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1060 #ifdef ULTRIX_STDIO_BOTCH
1062 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1064 return PerlIO_seek(fp, pos, whence) >= 0;
1066 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1067 report_evil_fh(gv, io, PL_op->op_type);
1068 SETERRNO(EBADF,RMS_IFI);
1073 Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
1076 register IO *io = NULL;
1077 register PerlIO *fp;
1079 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
1080 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
1081 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1082 report_evil_fh(gv, io, PL_op->op_type);
1083 SETERRNO(EBADF,RMS_IFI);
1088 Perl_mode_from_discipline(pTHX_ SV *discp)
1090 int mode = O_BINARY;
1093 const char *s = SvPV_const(discp,len);
1098 if (s[2] == 'a' && s[3] == 'w'
1099 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1108 if (s[2] == 'r' && s[3] == 'l' && s[4] == 'f'
1109 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1118 goto fail_discipline;
1121 else if (isSPACE(*s)) {
1128 end = strchr(s+1, ':');
1131 #ifndef PERLIO_LAYERS
1132 Perl_croak(aTHX_ "IO layers (like '%.*s') unavailable", end-s, s);
1143 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE)
1145 my_chsize(int fd, Off_t length)
1148 /* code courtesy of William Kucharski */
1153 if (PerlLIO_fstat(fd, &filebuf) < 0)
1156 if (filebuf.st_size < length) {
1158 /* extend file length */
1160 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
1163 /* write a "0" byte */
1165 if ((PerlLIO_write(fd, "", 1)) != 1)
1169 /* truncate length */
1173 fl.l_start = length;
1174 fl.l_type = F_WRLCK; /* write lock on file space */
1177 * This relies on the UNDOCUMENTED F_FREESP argument to
1178 * fcntl(2), which truncates the file so that it ends at the
1179 * position indicated by fl.l_start.
1181 * Will minor miracles never cease?
1184 if (fcntl(fd, F_FREESP, &fl) < 0)
1190 Perl_croak_nocontext("truncate not implemented");
1191 #endif /* F_FREESP */
1194 #endif /* !HAS_TRUNCATE && !HAS_CHSIZE */
1197 Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
1200 register const char *tmps;
1205 /* assuming fp is checked earlier */
1208 switch (SvTYPE(sv)) {
1210 if (ckWARN(WARN_UNINITIALIZED))
1215 assert(!SvGMAGICAL(sv));
1217 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
1219 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
1220 return !PerlIO_error(fp);
1224 /* Do this first to trigger any overloading. */
1225 tmps = SvPV_const(sv, len);
1226 if (PerlIO_isutf8(fp)) {
1228 const STRLEN origlen = len;
1229 /* We don't modify the original scalar. */
1230 tmpbuf = bytes_to_utf8((const U8*) tmps, &len);
1231 tmps = (char *) tmpbuf;
1232 if (ckWARN(WARN_UTF8) && len != origlen) {
1233 Perl_warner(aTHX_ packWARN(WARN_UTF8),
1234 "Variable length character upgraded in print");
1238 else if (DO_UTF8(sv)) {
1239 STRLEN tmplen = len;
1241 U8 * const result = bytes_from_utf8((const U8*) tmps, &tmplen, &utf8);
1244 tmps = (char *) tmpbuf;
1248 assert((char *)result == tmps);
1249 if (ckWARN_d(WARN_UTF8)) {
1250 Perl_warner(aTHX_ packWARN(WARN_UTF8),
1251 "Wide character in print");
1257 /* To detect whether the process is about to overstep its
1258 * filesize limit we would need getrlimit(). We could then
1259 * also transparently raise the limit with setrlimit() --
1260 * but only until the system hard limit/the filesystem limit,
1261 * at which we would get EPERM. Note that when using buffered
1262 * io the write failure can be delayed until the flush/close. --jhi */
1263 if (len && (PerlIO_write(fp,tmps,len) == 0))
1266 return happy ? !PerlIO_error(fp) : FALSE;
1277 if (PL_op->op_flags & OPf_REF) {
1282 return PL_laststatval;
1285 PL_laststype = OP_STAT;
1287 sv_setpvn(PL_statname, "", 0);
1290 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
1291 } else if (IoDIRP(io)) {
1293 return (PL_laststatval = PerlLIO_fstat(dirfd(IoDIRP(io)), &PL_statcache));
1295 Perl_die(aTHX_ PL_no_func, "dirfd");
1298 /* Can't use NORETURN_FUNCTION_END because Perl_die is not
1299 * __attribute__noreturn__
1300 * Can't use DIE because that does not return an integer
1304 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1305 report_evil_fh(gv, io, PL_op->op_type);
1306 return (PL_laststatval = -1);
1309 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1310 report_evil_fh(gv, io, PL_op->op_type);
1311 return (PL_laststatval = -1);
1314 else if (PL_op->op_private & OPpFT_STACKED) {
1315 return PL_laststatval;
1318 SV* const sv = POPs;
1322 if (SvTYPE(sv) == SVt_PVGV) {
1326 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
1330 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
1333 goto do_fstat_have_io;
1336 s = SvPV_const(sv, len);
1338 sv_setpvn(PL_statname, s, len);
1339 s = SvPVX_const(PL_statname); /* s now NUL-terminated */
1340 PL_laststype = OP_STAT;
1341 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1342 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
1343 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
1344 return PL_laststatval;
1353 static const char no_prev_lstat[] = "The stat preceding -l _ wasn't an lstat";
1356 if (PL_op->op_flags & OPf_REF) {
1358 if (cGVOP_gv == PL_defgv) {
1359 if (PL_laststype != OP_LSTAT)
1360 Perl_croak(aTHX_ no_prev_lstat);
1361 return PL_laststatval;
1363 if (ckWARN(WARN_IO)) {
1364 Perl_warner(aTHX_ packWARN(WARN_IO), "Use of -l on filehandle %s",
1366 return (PL_laststatval = -1);
1369 else if (PL_laststype != OP_LSTAT
1370 && (PL_op->op_private & OPpFT_STACKED) && ckWARN(WARN_IO))
1371 Perl_croak(aTHX_ no_prev_lstat);
1373 PL_laststype = OP_LSTAT;
1377 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV && ckWARN(WARN_IO)) {
1378 Perl_warner(aTHX_ packWARN(WARN_IO), "Use of -l on filehandle %s",
1379 GvENAME((GV*) SvRV(sv)));
1380 return (PL_laststatval = -1);
1382 /* XXX Do really need to be calling SvPV() all these times? */
1383 sv_setpv(PL_statname,SvPV_nolen_const(sv));
1384 PL_laststatval = PerlLIO_lstat(SvPV_nolen_const(sv),&PL_statcache);
1385 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(sv), '\n'))
1386 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "lstat");
1387 return PL_laststatval;
1391 S_exec_failed(pTHX_ const char *cmd, int fd, int do_report)
1393 const int e = errno;
1394 if (ckWARN(WARN_EXEC))
1395 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
1398 PerlLIO_write(fd, (void*)&e, sizeof(int));
1404 Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1405 int fd, int do_report)
1408 #if defined(MACOS_TRADITIONAL) || defined(__SYMBIAN32__)
1409 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1413 const char *tmps = NULL;
1414 Newx(PL_Argv, sp - mark + 1, char*);
1417 while (++mark <= sp) {
1419 *a++ = (char*)SvPV_nolen_const(*mark);
1425 tmps = SvPV_nolen_const(really);
1426 if ((!really && *PL_Argv[0] != '/') ||
1427 (really && *tmps != '/')) /* will execvp use PATH? */
1428 TAINT_ENV(); /* testing IFS here is overkill, probably */
1430 if (really && *tmps)
1431 PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
1433 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1435 S_exec_failed(aTHX_ (really ? tmps : PL_Argv[0]), fd, do_report);
1443 Perl_do_execfree(pTHX)
1452 #ifdef PERL_DEFAULT_DO_EXEC3_IMPLEMENTATION
1455 Perl_do_exec3(pTHX_ const char *incmd, int fd, int do_report)
1463 /* Make a copy so we can change it */
1464 const Size_t cmdlen = strlen(incmd) + 1;
1465 Newx(buf, cmdlen, char);
1467 my_strlcpy(cmd, incmd, cmdlen);
1469 while (*cmd && isSPACE(*cmd))
1472 /* save an extra exec if possible */
1476 char flags[PERL_FLAGS_MAX];
1477 if (strnEQ(cmd,PL_cshname,PL_cshlen) &&
1478 strnEQ(cmd+PL_cshlen," -c",3)) {
1479 my_strlcpy(flags, "-c", PERL_FLAGS_MAX);
1480 s = cmd+PL_cshlen+3;
1483 my_strlcat(flags, "f", PERL_FLAGS_MAX - 2);
1488 char * const ncmd = s;
1494 if (s[-1] == '\'') {
1497 PerlProc_execl(PL_cshname, "csh", flags, ncmd, (char*)NULL);
1500 S_exec_failed(aTHX_ PL_cshname, fd, do_report);
1509 /* see if there are shell metacharacters in it */
1511 if (*cmd == '.' && isSPACE(cmd[1]))
1514 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1519 s++; /* catch VAR=val gizmo */
1523 for (s = cmd; *s; s++) {
1524 if (*s != ' ' && !isALPHA(*s) &&
1525 strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1526 if (*s == '\n' && !s[1]) {
1530 /* handle the 2>&1 construct at the end */
1531 if (*s == '>' && s[1] == '&' && s[2] == '1'
1532 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1533 && (!s[3] || isSPACE(s[3])))
1535 const char *t = s + 3;
1537 while (*t && isSPACE(*t))
1539 if (!*t && (PerlLIO_dup2(1,2) != -1)) {
1546 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char *)NULL);
1548 S_exec_failed(aTHX_ PL_sh_path, fd, do_report);
1554 Newx(PL_Argv, (s - cmd) / 2 + 2, char*);
1555 PL_Cmd = savepvn(cmd, s-cmd);
1557 for (s = PL_Cmd; *s;) {
1562 while (*s && !isSPACE(*s))
1570 PerlProc_execvp(PL_Argv[0],PL_Argv);
1572 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1576 S_exec_failed(aTHX_ PL_Argv[0], fd, do_report);
1583 #endif /* OS2 || WIN32 */
1586 Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
1590 register I32 tot = 0;
1591 const char *const what = PL_op_name[type];
1593 SV ** const oldmark = mark;
1595 /* Doing this ahead of the switch statement preserves the old behaviour,
1596 where attempting to use kill as a taint test test would fail on
1597 platforms where kill was not defined. */
1599 if (type == OP_KILL)
1600 Perl_die(aTHX_ PL_no_func, what);
1603 if (type == OP_CHOWN)
1604 Perl_die(aTHX_ PL_no_func, what);
1608 #define APPLY_TAINT_PROPER() \
1610 if (PL_tainted) { TAINT_PROPER(what); } \
1613 /* This is a first heuristic; it doesn't catch tainting magic. */
1615 while (++mark <= sp) {
1616 if (SvTAINTED(*mark)) {
1625 APPLY_TAINT_PROPER();
1628 APPLY_TAINT_PROPER();
1630 while (++mark <= sp) {
1632 if (SvTYPE(*mark) == SVt_PVGV) {
1635 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1637 APPLY_TAINT_PROPER();
1638 if (fchmod(PerlIO_fileno(IoIFP(GvIOn(gv))), val))
1641 Perl_die(aTHX_ PL_no_func, "fchmod");
1648 else if (SvROK(*mark) && SvTYPE(SvRV(*mark)) == SVt_PVGV) {
1649 gv = (GV*)SvRV(*mark);
1653 const char *name = SvPV_nolen_const(*mark);
1654 APPLY_TAINT_PROPER();
1655 if (PerlLIO_chmod(name, val))
1663 APPLY_TAINT_PROPER();
1664 if (sp - mark > 2) {
1666 val = SvIVx(*++mark);
1667 val2 = SvIVx(*++mark);
1668 APPLY_TAINT_PROPER();
1670 while (++mark <= sp) {
1672 if (SvTYPE(*mark) == SVt_PVGV) {
1675 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1677 APPLY_TAINT_PROPER();
1678 if (fchown(PerlIO_fileno(IoIFP(GvIOn(gv))), val, val2))
1681 Perl_die(aTHX_ PL_no_func, "fchown");
1688 else if (SvROK(*mark) && SvTYPE(SvRV(*mark)) == SVt_PVGV) {
1689 gv = (GV*)SvRV(*mark);
1693 const char *name = SvPV_nolen_const(*mark);
1694 APPLY_TAINT_PROPER();
1695 if (PerlLIO_chown(name, val, val2))
1703 XXX Should we make lchown() directly available from perl?
1704 For now, we'll let Configure test for HAS_LCHOWN, but do
1705 nothing in the core.
1710 APPLY_TAINT_PROPER();
1713 s = SvPVx_nolen_const(*++mark);
1715 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1717 if ((val = whichsig(s)) < 0)
1718 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
1722 APPLY_TAINT_PROPER();
1725 /* kill() doesn't do process groups (job trees?) under VMS */
1726 if (val < 0) val = -val;
1727 if (val == SIGKILL) {
1728 # include <starlet.h>
1729 /* Use native sys$delprc() to insure that target process is
1730 * deleted; supervisor-mode images don't pay attention to
1731 * CRTL's emulation of Unix-style signals and kill()
1733 while (++mark <= sp) {
1734 I32 proc = SvIVx(*mark);
1735 register unsigned long int __vmssts;
1736 APPLY_TAINT_PROPER();
1737 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1741 case SS$_NOSUCHNODE:
1742 SETERRNO(ESRCH,__vmssts);
1745 SETERRNO(EPERM,__vmssts);
1748 SETERRNO(EVMSERR,__vmssts);
1757 while (++mark <= sp) {
1758 const I32 proc = SvIVx(*mark);
1759 APPLY_TAINT_PROPER();
1761 if (PerlProc_killpg(proc,val)) /* BSD */
1763 if (PerlProc_kill(-proc,val)) /* SYSV */
1769 while (++mark <= sp) {
1770 const I32 proc = SvIVx(*mark);
1771 APPLY_TAINT_PROPER();
1772 if (PerlProc_kill(proc, val))
1779 APPLY_TAINT_PROPER();
1781 while (++mark <= sp) {
1782 s = SvPV_nolen_const(*mark);
1783 APPLY_TAINT_PROPER();
1784 if (PL_euid || PL_unsafe) {
1788 else { /* don't let root wipe out directories without -U */
1789 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1798 #if defined(HAS_UTIME) || defined(HAS_FUTIMES)
1800 APPLY_TAINT_PROPER();
1801 if (sp - mark > 2) {
1802 #if defined(HAS_FUTIMES)
1803 struct timeval utbuf[2];
1804 void *utbufp = utbuf;
1805 #elif defined(I_UTIME) || defined(VMS)
1806 struct utimbuf utbuf;
1807 struct utimbuf *utbufp = &utbuf;
1813 void *utbufp = &utbuf;
1816 SV* const accessed = *++mark;
1817 SV* const modified = *++mark;
1819 /* Be like C, and if both times are undefined, let the C
1820 * library figure out what to do. This usually means
1821 * "current time". */
1823 if ( accessed == &PL_sv_undef && modified == &PL_sv_undef )
1826 Zero(&utbuf, sizeof utbuf, char);
1828 utbuf[0].tv_sec = (long)SvIVx(accessed); /* time accessed */
1829 utbuf[0].tv_usec = 0;
1830 utbuf[1].tv_sec = (long)SvIVx(modified); /* time modified */
1831 utbuf[1].tv_usec = 0;
1832 #elif defined(BIG_TIME)
1833 utbuf.actime = (Time_t)SvNVx(accessed); /* time accessed */
1834 utbuf.modtime = (Time_t)SvNVx(modified); /* time modified */
1836 utbuf.actime = (Time_t)SvIVx(accessed); /* time accessed */
1837 utbuf.modtime = (Time_t)SvIVx(modified); /* time modified */
1840 APPLY_TAINT_PROPER();
1842 while (++mark <= sp) {
1844 if (SvTYPE(*mark) == SVt_PVGV) {
1847 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1849 APPLY_TAINT_PROPER();
1850 if (futimes(PerlIO_fileno(IoIFP(GvIOn(gv))), utbufp))
1853 Perl_die(aTHX_ PL_no_func, "futimes");
1860 else if (SvROK(*mark) && SvTYPE(SvRV(*mark)) == SVt_PVGV) {
1861 gv = (GV*)SvRV(*mark);
1865 const char * const name = SvPV_nolen_const(*mark);
1866 APPLY_TAINT_PROPER();
1868 if (utimes(name, utbufp))
1870 if (PerlLIO_utime(name, utbufp))
1884 #undef APPLY_TAINT_PROPER
1887 /* Do the permissions allow some operation? Assumes statcache already set. */
1888 #ifndef VMS /* VMS' cando is in vms.c */
1890 Perl_cando(pTHX_ Mode_t mode, bool effective, register const Stat_t *statbufp)
1891 /* effective is a flag, true for EUID, or for checking if the effective gid
1892 * is in the list of groups returned from getgroups().
1897 /* [Comments and code from Len Reed]
1898 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1899 * to write-protected files. The execute permission bit is set
1900 * by the Miscrosoft C library stat() function for the following:
1905 * All files and directories are readable.
1906 * Directories and special files, e.g. "CON", cannot be
1908 * [Comment by Tom Dinger -- a directory can have the write-protect
1909 * bit set in the file system, but DOS permits changes to
1910 * the directory anyway. In addition, all bets are off
1911 * here for networked software, such as Novell and
1915 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1916 * too so it will actually look into the files for magic numbers
1918 return (mode & statbufp->st_mode) ? TRUE : FALSE;
1920 #else /* ! DOSISH */
1921 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
1922 if (mode == S_IXUSR) {
1923 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1927 return TRUE; /* root reads and writes anything */
1930 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1931 if (statbufp->st_mode & mode)
1932 return TRUE; /* ok as "user" */
1934 else if (ingroup(statbufp->st_gid,effective)) {
1935 if (statbufp->st_mode & mode >> 3)
1936 return TRUE; /* ok as "group" */
1938 else if (statbufp->st_mode & mode >> 6)
1939 return TRUE; /* ok as "other" */
1941 #endif /* ! DOSISH */
1946 Perl_ingroup(pTHX_ Gid_t testgid, bool effective)
1948 #ifdef MACOS_TRADITIONAL
1949 /* This is simply not correct for AppleShare, but fix it yerself. */
1953 if (testgid == (effective ? PL_egid : PL_gid))
1955 #ifdef HAS_GETGROUPS
1957 Groups_t *gary = NULL;
1961 anum = getgroups(0, gary);
1962 Newx(gary, anum, Groups_t);
1963 anum = getgroups(anum, gary);
1965 if (gary[anum] == testgid) {
1979 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1982 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
1985 const key_t key = (key_t)SvNVx(*++mark);
1986 const I32 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1987 const I32 flags = SvIVx(*++mark);
1989 PERL_UNUSED_ARG(sp);
1996 return msgget(key, flags);
2000 return semget(key, n, flags);
2004 return shmget(key, n, flags);
2006 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
2008 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2011 return -1; /* should never happen */
2015 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
2020 const I32 id = SvIVx(*++mark);
2022 const I32 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
2024 const I32 cmd = SvIVx(*++mark);
2025 SV * const astr = *++mark;
2026 STRLEN infosize = 0;
2027 I32 getinfo = (cmd == IPC_STAT);
2029 PERL_UNUSED_ARG(sp);
2035 if (cmd == IPC_STAT || cmd == IPC_SET)
2036 infosize = sizeof(struct msqid_ds);
2041 if (cmd == IPC_STAT || cmd == IPC_SET)
2042 infosize = sizeof(struct shmid_ds);
2048 if (cmd == IPC_STAT || cmd == IPC_SET)
2049 infosize = sizeof(struct semid_ds);
2050 else if (cmd == GETALL || cmd == SETALL)
2052 struct semid_ds semds;
2054 #ifdef EXTRA_F_IN_SEMUN_BUF
2055 semun.buff = &semds;
2059 getinfo = (cmd == GETALL);
2060 if (Semctl(id, 0, IPC_STAT, semun) == -1)
2062 infosize = semds.sem_nsems * sizeof(short);
2063 /* "short" is technically wrong but much more portable
2064 than guessing about u_?short(_t)? */
2067 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2071 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
2073 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2081 SvPV_force_nolen(astr);
2082 a = SvGROW(astr, infosize+1);
2087 a = SvPV(astr, len);
2088 if (len != infosize)
2089 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
2097 const IV i = SvIV(astr);
2098 a = INT2PTR(char *,i); /* ouch */
2105 ret = msgctl(id, cmd, (struct msqid_ds *)a);
2111 union semun unsemds;
2113 #ifdef EXTRA_F_IN_SEMUN_BUF
2114 unsemds.buff = (struct semid_ds *)a;
2116 unsemds.buf = (struct semid_ds *)a;
2118 ret = Semctl(id, n, cmd, unsemds);
2120 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2127 ret = shmctl(id, cmd, (struct shmid_ds *)a);
2131 if (getinfo && ret >= 0) {
2132 SvCUR_set(astr, infosize);
2133 *SvEND(astr) = '\0';
2140 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
2145 const I32 id = SvIVx(*++mark);
2146 SV * const mstr = *++mark;
2147 const I32 flags = SvIVx(*++mark);
2148 const char * const mbuf = SvPV_const(mstr, len);
2149 const I32 msize = len - sizeof(long);
2151 PERL_UNUSED_ARG(sp);
2154 Perl_croak(aTHX_ "Arg too short for msgsnd");
2156 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
2158 Perl_croak(aTHX_ "msgsnd not implemented");
2163 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
2169 I32 msize, flags, ret;
2170 const I32 id = SvIVx(*++mark);
2171 SV * const mstr = *++mark;
2172 PERL_UNUSED_ARG(sp);
2174 /* suppress warning when reading into undef var --jhi */
2176 sv_setpvn(mstr, "", 0);
2177 msize = SvIVx(*++mark);
2178 mtype = (long)SvIVx(*++mark);
2179 flags = SvIVx(*++mark);
2180 SvPV_force_nolen(mstr);
2181 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
2184 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
2186 SvCUR_set(mstr, sizeof(long)+ret);
2187 *SvEND(mstr) = '\0';
2188 #ifndef INCOMPLETE_TAINTS
2189 /* who knows who has been playing with this message? */
2195 Perl_croak(aTHX_ "msgrcv not implemented");
2200 Perl_do_semop(pTHX_ SV **mark, SV **sp)
2205 const I32 id = SvIVx(*++mark);
2206 SV * const opstr = *++mark;
2207 const char * const opbuf = SvPV_const(opstr, opsize);
2208 PERL_UNUSED_ARG(sp);
2210 if (opsize < 3 * SHORTSIZE
2211 || (opsize % (3 * SHORTSIZE))) {
2212 SETERRNO(EINVAL,LIB_INVARG);
2216 /* We can't assume that sizeof(struct sembuf) == 3 * sizeof(short). */
2218 const int nsops = opsize / (3 * sizeof (short));
2220 short * const ops = (short *) opbuf;
2222 struct sembuf *temps, *t;
2225 Newx (temps, nsops, struct sembuf);
2233 result = semop(id, temps, nsops);
2247 Perl_croak(aTHX_ "semop not implemented");
2252 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
2257 struct shmid_ds shmds;
2258 const I32 id = SvIVx(*++mark);
2259 SV * const mstr = *++mark;
2260 const I32 mpos = SvIVx(*++mark);
2261 const I32 msize = SvIVx(*++mark);
2262 PERL_UNUSED_ARG(sp);
2265 if (shmctl(id, IPC_STAT, &shmds) == -1)
2267 if (mpos < 0 || msize < 0
2268 || (size_t)mpos + msize > (size_t)shmds.shm_segsz) {
2269 SETERRNO(EFAULT,SS_ACCVIO); /* can't do as caller requested */
2272 shm = (char *)shmat(id, NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
2273 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2275 if (optype == OP_SHMREAD) {
2277 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2279 sv_setpvn(mstr, "", 0);
2280 SvPV_force_nolen(mstr);
2281 mbuf = SvGROW(mstr, (STRLEN)msize+1);
2283 Copy(shm + mpos, mbuf, msize, char);
2284 SvCUR_set(mstr, msize);
2285 *SvEND(mstr) = '\0';
2287 #ifndef INCOMPLETE_TAINTS
2288 /* who knows who has been playing with this shared memory? */
2295 const char *mbuf = SvPV_const(mstr, len);
2296 const I32 n = ((I32)len > msize) ? msize : (I32)len;
2297 Copy(mbuf, shm + mpos, n, char);
2299 memzero(shm + mpos + n, msize - n);
2303 Perl_croak(aTHX_ "shm I/O not implemented");
2307 #endif /* SYSV IPC */
2312 =for apidoc start_glob
2314 Function called by C<do_readline> to spawn a glob (or do the glob inside
2315 perl on VMS). This code used to be inline, but now perl uses C<File::Glob>
2316 this glob starter is only used by miniperl during the build process.
2317 Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up.
2323 Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
2326 SV * const tmpcmd = newSV(0);
2330 #ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
2331 /* since spawning off a process is a real performance hit */
2338 fp = Perl_vms_start_glob(aTHX_ tmpglob, io);
2341 #ifdef MACOS_TRADITIONAL
2342 sv_setpv(tmpcmd, "glob ");
2343 sv_catsv(tmpcmd, tmpglob);
2344 sv_catpv(tmpcmd, " |");
2348 sv_setpv(tmpcmd, "for a in ");
2349 sv_catsv(tmpcmd, tmpglob);
2350 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
2353 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
2354 sv_catsv(tmpcmd, tmpglob);
2356 sv_setpv(tmpcmd, "perlglob ");
2357 sv_catsv(tmpcmd, tmpglob);
2358 sv_catpv(tmpcmd, " |");
2363 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
2364 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
2365 sv_catsv(tmpcmd, tmpglob);
2366 sv_catpv(tmpcmd, "' 2>/dev/null |");
2368 sv_setpv(tmpcmd, "echo ");
2369 sv_catsv(tmpcmd, tmpglob);
2371 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
2373 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
2376 #endif /* !DOSISH */
2377 #endif /* MACOS_TRADITIONAL */
2378 (void)do_open(PL_last_in_gv, (char*)SvPVX_const(tmpcmd), SvCUR(tmpcmd),
2379 FALSE, O_RDONLY, 0, NULL);
2388 * c-indentation-style: bsd
2390 * indent-tabs-mode: t
2393 * ex: set ts=8 sts=4 sw=4 noet: