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 Perl_do_openn(pTHX_ GV *gv, register const char *oname, I32 len, int as_raw,
65 int rawmode, int rawperm, PerlIO *supplied_fp, SV **svp,
69 register IO * const io = GvIOn(gv);
70 PerlIO *saveifp = NULL;
71 PerlIO *saveofp = NULL;
73 char savetype = IoTYPE_CLOSED;
78 bool was_fdopen = FALSE;
79 bool in_raw = 0, in_crlf = 0, out_raw = 0, out_crlf = 0;
81 char mode[PERL_MODE_MAX]; /* file mode ("r\0", "rb\0", "ab\0" etc.) */
84 PERL_ARGS_ASSERT_DO_OPENN;
86 Zero(mode,sizeof(mode),char);
87 PL_forkprocess = 1; /* assume true if no fork */
89 /* Collect default raw/crlf info from the op */
90 if (PL_op && PL_op->op_type == OP_OPEN) {
91 /* set up IO layers */
92 const U8 flags = PL_op->op_private;
93 in_raw = (flags & OPpOPEN_IN_RAW);
94 in_crlf = (flags & OPpOPEN_IN_CRLF);
95 out_raw = (flags & OPpOPEN_OUT_RAW);
96 out_crlf = (flags & OPpOPEN_OUT_CRLF);
99 /* If currently open - close before we re-open */
101 fd = PerlIO_fileno(IoIFP(io));
102 if (IoTYPE(io) == IoTYPE_STD) {
103 /* This is a clone of one of STD* handles */
106 else if (fd >= 0 && fd <= PL_maxsysfd) {
107 /* This is one of the original STD* handles */
110 savetype = IoTYPE(io);
114 else if (IoTYPE(io) == IoTYPE_PIPE)
115 result = PerlProc_pclose(IoIFP(io));
116 else if (IoIFP(io) != IoOFP(io)) {
118 result = PerlIO_close(IoOFP(io));
119 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
122 result = PerlIO_close(IoIFP(io));
125 result = PerlIO_close(IoIFP(io));
126 if (result == EOF && fd > PL_maxsysfd) {
127 /* Why is this not Perl_warn*() call ? */
128 PerlIO_printf(Perl_error_log,
129 "Warning: unable to close filehandle %s properly.\n",
132 IoOFP(io) = IoIFP(io) = NULL;
136 /* sysopen style args, i.e. integer mode and permissions */
138 const int appendtrunc =
140 #ifdef O_APPEND /* Not fully portable. */
143 #ifdef O_TRUNC /* Not fully portable. */
147 const int modifyingmode = O_WRONLY|O_RDWR|O_CREAT|appendtrunc;
151 Perl_croak(aTHX_ "panic: sysopen with multiple args");
159 It might be (in OS/390 and Mac OS Classic it is)
165 This means that simple & with O_RDWR would look
166 like O_RDONLY is present. Therefore we have to
169 if ((ismodifying = (rawmode & modifyingmode))) {
170 if ((ismodifying & O_WRONLY) == O_WRONLY ||
171 (ismodifying & O_RDWR) == O_RDWR ||
172 (ismodifying & (O_CREAT|appendtrunc)))
173 TAINT_PROPER("sysopen");
175 mode[ix++] = IoTYPE_NUMERIC; /* Marker to openn to use numeric "sysopen" */
177 #if defined(USE_64_BIT_RAWIO) && defined(O_LARGEFILE)
178 rawmode |= O_LARGEFILE; /* Transparently largefiley. */
181 IoTYPE(io) = PerlIO_intmode2str(rawmode, &mode[ix], &writing);
183 namesv = newSVpvn_flags(oname, len, SVs_TEMP);
187 fp = PerlIO_openn(aTHX_ type, mode, -1, rawmode, rawperm, NULL, num_svs, svp);
190 /* Regular (non-sys) open */
196 type = savepvn(oname, len);
200 /* Lose leading and trailing white space */
201 while (isSPACE(*type))
203 while (tend > type && isSPACE(tend[-1]))
207 /* New style explicit name, type is just mode and layer info */
209 if (SvROK(*svp) && !strchr(oname,'&')) {
211 Perl_warner(aTHX_ packWARN(WARN_IO),
212 "Can't open a reference");
213 SETERRNO(EINVAL, LIB_INVARG);
216 #endif /* USE_STDIO */
217 name = SvOK(*svp) ? savesvpv (*svp) : savepvs ("");
225 if ((*type == IoTYPE_RDWR) && /* scary */
226 (*(type+1) == IoTYPE_RDONLY || *(type+1) == IoTYPE_WRONLY) &&
227 ((!num_svs || (tend > type+1 && tend[-1] != IoTYPE_PIPE)))) {
228 TAINT_PROPER("open");
233 if (*type == IoTYPE_PIPE) {
235 if (type[1] != IoTYPE_STD) {
237 Perl_croak(aTHX_ "Unknown open() mode '%.*s'", (int)olen, oname);
243 } while (isSPACE(*type));
249 /* command is missing 19990114 */
250 if (ckWARN(WARN_PIPE))
251 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open");
255 if (!(*name == '-' && name[1] == '\0') || num_svs)
257 TAINT_PROPER("piped open");
258 if (!num_svs && name[len-1] == '|') {
260 if (ckWARN(WARN_PIPE))
261 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Can't open bidirectional pipe");
270 fp = PerlProc_popen_list(mode, num_svs, svp);
273 fp = PerlProc_popen(name,mode);
277 if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) {
283 else if (*type == IoTYPE_WRONLY) {
284 TAINT_PROPER("open");
286 if (*type == IoTYPE_WRONLY) {
287 /* Two IoTYPE_WRONLYs in a row make for an IoTYPE_APPEND. */
288 mode[0] = IoTYPE(io) = IoTYPE_APPEND;
302 dodup = PERLIO_DUP_FD;
308 if (!num_svs && !*type && supplied_fp) {
309 /* "<+&" etc. is used by typemaps */
313 PerlIO *that_fp = NULL;
315 Perl_croak(aTHX_ "More than one argument to '%c&' open",IoTYPE(io));
317 while (isSPACE(*type))
319 if (num_svs && (SvIOK(*svp) || (SvPOK(*svp) && looks_like_number(*svp)))) {
323 else if (isDIGIT(*type)) {
329 thatio = sv_2io(*svp);
332 GV * const thatgv = gv_fetchpvn_flags(type, tend - type,
334 thatio = GvIO(thatgv);
338 SETERRNO(EINVAL,SS_IVCHAN);
342 if ((that_fp = IoIFP(thatio))) {
343 /* Flush stdio buffer before dup. --mjd
344 * Unfortunately SEEK_CURing 0 seems to
345 * be optimized away on most platforms;
346 * only Solaris and Linux seem to flush
349 /* sfio fails to clear error on next
350 sfwrite, contrary to documentation.
352 if (PerlIO_seek(that_fp, 0, SEEK_CUR) == -1)
353 PerlIO_clearerr(that_fp);
355 /* On the other hand, do all platforms
356 * take gracefully to flushing a read-only
357 * filehandle? Perhaps we should do
358 * fsetpos(src)+fgetpos(dst)? --nik */
359 PerlIO_flush(that_fp);
360 fd = PerlIO_fileno(that_fp);
361 /* When dup()ing STDIN, STDOUT or STDERR
362 * explicitly set appropriate access mode */
363 if (that_fp == PerlIO_stdout()
364 || that_fp == PerlIO_stderr())
365 IoTYPE(io) = IoTYPE_WRONLY;
366 else if (that_fp == PerlIO_stdin())
367 IoTYPE(io) = IoTYPE_RDONLY;
368 /* When dup()ing a socket, say result is
370 else if (IoTYPE(thatio) == IoTYPE_SOCKET)
371 IoTYPE(io) = IoTYPE_SOCKET;
379 fp = PerlIO_fdupopen(aTHX_ that_fp, NULL, dodup);
383 fd = PerlLIO_dup(fd);
386 if (!(fp = PerlIO_openn(aTHX_ type,mode,fd,0,0,NULL,num_svs,svp))) {
387 if (dodup && fd >= 0)
394 while (isSPACE(*type))
396 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
398 fp = PerlIO_stdout();
399 IoTYPE(io) = IoTYPE_STD;
401 Perl_croak(aTHX_ "More than one argument to '>%c' open",IoTYPE_STD);
406 namesv = newSVpvn_flags(type, tend - type, SVs_TEMP);
411 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
414 if (!fp && type && *type && *type != ':' && !isIDFIRST(*type))
415 goto unknown_open_mode;
416 } /* IoTYPE_WRONLY */
417 else if (*type == IoTYPE_RDONLY) {
420 } while (isSPACE(*type));
429 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
432 IoTYPE(io) = IoTYPE_STD;
434 Perl_croak(aTHX_ "More than one argument to '<%c' open",IoTYPE_STD);
439 namesv = newSVpvn_flags(type, tend - type, SVs_TEMP);
444 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
446 if (!fp && type && *type && *type != ':' && !isIDFIRST(*type))
447 goto unknown_open_mode;
448 } /* IoTYPE_RDONLY */
449 else if ((num_svs && /* '-|...' or '...|' */
450 type[0] == IoTYPE_STD && type[1] == IoTYPE_PIPE) ||
451 (!num_svs && tend > type+1 && tend[-1] == IoTYPE_PIPE)) {
453 type += 2; /* skip over '-|' */
457 while (tend > type && isSPACE(tend[-1]))
459 for (; isSPACE(*type); type++)
465 /* command is missing 19990114 */
466 if (ckWARN(WARN_PIPE))
467 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open");
471 if (!(*name == '-' && name[1] == '\0') || num_svs)
473 TAINT_PROPER("piped open");
482 fp = PerlProc_popen_list(mode,num_svs,svp);
485 fp = PerlProc_popen(name,mode);
487 IoTYPE(io) = IoTYPE_PIPE;
489 while (isSPACE(*type))
492 if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) {
498 else { /* layer(Args) */
500 goto unknown_open_mode;
502 IoTYPE(io) = IoTYPE_RDONLY;
503 for (; isSPACE(*name); name++)
512 if (*name == '-' && name[1] == '\0') {
514 IoTYPE(io) = IoTYPE_STD;
518 namesv = newSVpvn_flags(type, tend - type, SVs_TEMP);
523 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
528 if (IoTYPE(io) == IoTYPE_RDONLY && ckWARN(WARN_NEWLINE)
529 && strchr(oname, '\n')
532 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
536 if (ckWARN(WARN_IO)) {
537 if ((IoTYPE(io) == IoTYPE_RDONLY) &&
538 (fp == PerlIO_stdout() || fp == PerlIO_stderr())) {
539 Perl_warner(aTHX_ packWARN(WARN_IO),
540 "Filehandle STD%s reopened as %s only for input",
541 ((fp == PerlIO_stdout()) ? "OUT" : "ERR"),
544 else if ((IoTYPE(io) == IoTYPE_WRONLY) && fp == PerlIO_stdin()) {
545 Perl_warner(aTHX_ packWARN(WARN_IO),
546 "Filehandle STDIN reopened as %s only for output",
551 fd = PerlIO_fileno(fp);
552 /* If there is no fd (e.g. PerlIO::scalar) assume it isn't a
553 * socket - this covers PerlIO::scalar - otherwise unless we "know" the
554 * type probe for socket-ness.
556 if (IoTYPE(io) && IoTYPE(io) != IoTYPE_PIPE && IoTYPE(io) != IoTYPE_STD && fd >= 0) {
557 if (PerlLIO_fstat(fd,&PL_statbuf) < 0) {
558 /* If PerlIO claims to have fd we had better be able to fstat() it. */
559 (void) PerlIO_close(fp);
563 if (S_ISSOCK(PL_statbuf.st_mode))
564 IoTYPE(io) = IoTYPE_SOCKET; /* in case a socket was passed in to us */
568 !(PL_statbuf.st_mode & S_IFMT)
572 && IoTYPE(io) != IoTYPE_WRONLY /* Dups of STD* filehandles already have */
573 && IoTYPE(io) != IoTYPE_RDONLY /* type so they aren't marked as sockets */
574 ) { /* on OS's that return 0 on fstat()ed pipe */
576 Sock_size_t buflen = sizeof tmpbuf;
577 if (PerlSock_getsockname(fd, (struct sockaddr *)tmpbuf, &buflen) >= 0
578 || errno != ENOTSOCK)
579 IoTYPE(io) = IoTYPE_SOCKET; /* some OS's return 0 on fstat()ed socket */
580 /* but some return 0 for streams too, sigh */
582 #endif /* HAS_SOCKET */
583 #endif /* !PERL_MICRO */
587 * If this is a standard handle we discard all the layer stuff
588 * and just dup the fd into whatever was on the handle before !
591 if (saveifp) { /* must use old fp? */
592 /* If fd is less that PL_maxsysfd i.e. STDIN..STDERR
593 then dup the new fileno down
596 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
597 if (saveofp != saveifp) { /* was a socket? */
598 PerlIO_close(saveofp);
602 /* Still a small can-of-worms here if (say) PerlIO::scalar
603 is assigned to (say) STDOUT - for now let dup2() fail
604 and provide the error
606 if (PerlLIO_dup2(fd, savefd) < 0) {
607 (void)PerlIO_close(fp);
611 if (savefd != PerlIO_fileno(PerlIO_stdin())) {
612 char newname[FILENAME_MAX+1];
613 if (PerlIO_getname(fp, newname)) {
614 if (fd == PerlIO_fileno(PerlIO_stdout()))
615 Perl_vmssetuserlnm(aTHX_ "SYS$OUTPUT", newname);
616 if (fd == PerlIO_fileno(PerlIO_stderr()))
617 Perl_vmssetuserlnm(aTHX_ "SYS$ERROR", newname);
623 /* PL_fdpid isn't used on Windows, so avoid this useless work.
624 * XXX Probably the same for a lot of other places. */
630 sv = *av_fetch(PL_fdpid,fd,TRUE);
631 SvUPGRADE(sv, SVt_IV);
634 sv = *av_fetch(PL_fdpid,savefd,TRUE);
635 SvUPGRADE(sv, SVt_IV);
642 /* need to close fp without closing underlying fd */
643 int ofd = PerlIO_fileno(fp);
644 int dupfd = PerlLIO_dup(ofd);
645 #if defined(HAS_FCNTL) && defined(F_SETFD)
646 /* Assume if we have F_SETFD we have F_GETFD */
647 int coe = fcntl(ofd,F_GETFD);
650 PerlLIO_dup2(dupfd,ofd);
651 #if defined(HAS_FCNTL) && defined(F_SETFD)
652 /* The dup trick has lost close-on-exec on ofd */
653 fcntl(ofd,F_SETFD, coe);
655 PerlLIO_close(dupfd);
662 fd = PerlIO_fileno(fp);
664 #if defined(HAS_FCNTL) && defined(F_SETFD)
667 fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
673 IoFLAGS(io) &= ~IOf_NOLINE;
675 if (IoTYPE(io) == IoTYPE_SOCKET
676 || (IoTYPE(io) == IoTYPE_WRONLY && fd >= 0 && S_ISCHR(PL_statbuf.st_mode)) ) {
678 if (*s == IoTYPE_IMPLICIT || *s == IoTYPE_NUMERIC)
681 if (!(IoOFP(io) = PerlIO_openn(aTHX_ type,s,fd,0,0,NULL,0,svp))) {
695 IoTYPE(io) = savetype;
700 Perl_nextargv(pTHX_ register GV *gv)
704 #ifndef FLEXFILENAMES
710 IO * const io = GvIOp(gv);
712 PERL_ARGS_ASSERT_NEXTARGV;
715 PL_argvoutgv = gv_fetchpvs("ARGVOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
716 if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
717 IoFLAGS(io) &= ~IOf_START;
720 Perl_av_create_and_push(aTHX_ &PL_argvout_stack,
721 SvREFCNT_inc_simple_NN(PL_defoutgv));
724 if (PL_filemode & (S_ISUID|S_ISGID)) {
725 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
728 (void)fchmod(PL_lastfd,PL_filemode);
730 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
737 while (av_len(GvAV(gv)) >= 0) {
739 sv = av_shift(GvAV(gv));
741 sv_setsv(GvSVn(gv),sv);
742 SvSETMAGIC(GvSV(gv));
743 PL_oldname = SvPVx(GvSV(gv), oldlen);
744 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,NULL)) {
746 TAINT_PROPER("inplace open");
747 if (oldlen == 1 && *PL_oldname == '-') {
748 setdefout(gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL,
750 return IoIFP(GvIOp(gv));
752 #ifndef FLEXFILENAMES
753 filedev = PL_statbuf.st_dev;
754 fileino = PL_statbuf.st_ino;
756 PL_filemode = PL_statbuf.st_mode;
757 fileuid = PL_statbuf.st_uid;
758 filegid = PL_statbuf.st_gid;
759 if (!S_ISREG(PL_filemode)) {
760 if (ckWARN_d(WARN_INPLACE))
761 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
762 "Can't do inplace edit: %s is not a regular file",
768 const char *star = strchr(PL_inplace, '*');
770 const char *begin = PL_inplace;
773 sv_catpvn(sv, begin, star - begin);
774 sv_catpvn(sv, PL_oldname, oldlen);
776 } while ((star = strchr(begin, '*')));
781 sv_catpv(sv,PL_inplace);
783 #ifndef FLEXFILENAMES
784 if ((PerlLIO_stat(SvPVX_const(sv),&PL_statbuf) >= 0
785 && PL_statbuf.st_dev == filedev
786 && PL_statbuf.st_ino == fileino)
788 || ((_djstat_fail_bits & _STFAIL_TRUENAME)!=0)
792 if (ckWARN_d(WARN_INPLACE))
793 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
794 "Can't do inplace edit: %"SVf" would not be unique",
801 #if !defined(DOSISH) && !defined(__CYGWIN__) && !defined(EPOC)
802 if (PerlLIO_rename(PL_oldname,SvPVX_const(sv)) < 0) {
803 if (ckWARN_d(WARN_INPLACE))
804 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
805 "Can't rename %s to %"SVf": %s, skipping file",
806 PL_oldname, SVfARG(sv), Strerror(errno));
812 (void)PerlLIO_unlink(SvPVX_const(sv));
813 (void)PerlLIO_rename(PL_oldname,SvPVX_const(sv));
814 do_open(gv,(char*)SvPVX_const(sv),SvCUR(sv),TRUE,O_RDONLY,0,NULL);
817 (void)UNLINK(SvPVX_const(sv));
818 if (link(PL_oldname,SvPVX_const(sv)) < 0) {
819 if (ckWARN_d(WARN_INPLACE))
820 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
821 "Can't rename %s to %"SVf": %s, skipping file",
822 PL_oldname, SVfARG(sv), Strerror(errno) );
826 (void)UNLINK(PL_oldname);
830 #if !defined(DOSISH) && !defined(AMIGAOS)
831 # ifndef VMS /* Don't delete; use automatic file versioning */
832 if (UNLINK(PL_oldname) < 0) {
833 if (ckWARN_d(WARN_INPLACE))
834 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
835 "Can't remove %s: %s, skipping file",
836 PL_oldname, Strerror(errno) );
842 Perl_croak(aTHX_ "Can't do inplace edit without backup");
846 sv_setpvn(sv,PL_oldname,oldlen);
847 SETERRNO(0,0); /* in case sprintf set errno */
848 if (!Perl_do_openn(aTHX_ PL_argvoutgv, (char*)SvPVX_const(sv),
851 O_WRONLY|O_CREAT|O_TRUNC,0,
853 O_WRONLY|O_CREAT|OPEN_EXCL,0600,
856 if (ckWARN_d(WARN_INPLACE))
857 Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't do inplace edit on %s: %s",
858 PL_oldname, Strerror(errno) );
862 setdefout(PL_argvoutgv);
863 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
864 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
866 (void)fchmod(PL_lastfd,PL_filemode);
868 # if !(defined(WIN32) && defined(__BORLANDC__))
869 /* Borland runtime creates a readonly file! */
870 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
873 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
875 (void)fchown(PL_lastfd,fileuid,filegid);
878 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
883 return IoIFP(GvIOp(gv));
886 if (ckWARN_d(WARN_INPLACE)) {
887 const int eno = errno;
888 if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0
889 && !S_ISREG(PL_statbuf.st_mode))
891 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
892 "Can't do inplace edit: %s is not a regular file",
896 Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't open %s: %s",
897 PL_oldname, Strerror(eno));
901 if (io && (IoFLAGS(io) & IOf_ARGV))
902 IoFLAGS(io) |= IOf_START;
904 (void)do_close(PL_argvoutgv,FALSE);
905 if (io && (IoFLAGS(io) & IOf_ARGV)
906 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
908 GV * const oldout = MUTABLE_GV(av_pop(PL_argvout_stack));
910 SvREFCNT_dec(oldout);
913 setdefout(gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO));
918 /* explicit renamed to avoid C++ conflict -- kja */
920 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
928 if (!gv || !isGV_with_GP(gv)) {
930 SETERRNO(EBADF,SS_IVCHAN);
934 if (!io) { /* never opened */
936 if (ckWARN(WARN_UNOPENED)) /* no check for closed here */
937 report_evil_fh(gv, io, PL_op->op_type);
938 SETERRNO(EBADF,SS_IVCHAN);
942 retval = io_close(io, not_implicit);
946 IoLINES_LEFT(io) = IoPAGE_LEN(io);
948 IoTYPE(io) = IoTYPE_CLOSED;
953 Perl_io_close(pTHX_ IO *io, bool not_implicit)
958 PERL_ARGS_ASSERT_IO_CLOSE;
961 if (IoTYPE(io) == IoTYPE_PIPE) {
962 const int status = PerlProc_pclose(IoIFP(io));
964 STATUS_NATIVE_CHILD_SET(status);
965 retval = (STATUS_UNIX == 0);
968 retval = (status != -1);
971 else if (IoTYPE(io) == IoTYPE_STD)
974 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
975 const bool prev_err = PerlIO_error(IoOFP(io));
976 retval = (PerlIO_close(IoOFP(io)) != EOF && !prev_err);
977 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
980 const bool prev_err = PerlIO_error(IoIFP(io));
981 retval = (PerlIO_close(IoIFP(io)) != EOF && !prev_err);
984 IoOFP(io) = IoIFP(io) = NULL;
986 else if (not_implicit) {
987 SETERRNO(EBADF,SS_IVCHAN);
994 Perl_do_eof(pTHX_ GV *gv)
997 register IO * const io = GvIO(gv);
999 PERL_ARGS_ASSERT_DO_EOF;
1003 else if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
1004 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1007 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
1008 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
1009 return FALSE; /* this is the most usual case */
1013 /* getc and ungetc can stomp on errno */
1015 const int ch = PerlIO_getc(IoIFP(io));
1017 (void)PerlIO_ungetc(IoIFP(io),ch);
1024 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
1025 if (PerlIO_get_cnt(IoIFP(io)) < -1)
1026 PerlIO_set_cnt(IoIFP(io),-1);
1028 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
1029 if (gv != PL_argvgv || !nextargv(gv)) /* get another fp handy */
1033 return TRUE; /* normal fp, definitely end of file */
1039 Perl_do_tell(pTHX_ GV *gv)
1042 register IO *io = NULL;
1043 register PerlIO *fp;
1045 PERL_ARGS_ASSERT_DO_TELL;
1047 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1048 #ifdef ULTRIX_STDIO_BOTCH
1050 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1052 return PerlIO_tell(fp);
1054 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1055 report_evil_fh(gv, io, PL_op->op_type);
1056 SETERRNO(EBADF,RMS_IFI);
1061 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
1064 register IO *io = NULL;
1065 register PerlIO *fp;
1067 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1068 #ifdef ULTRIX_STDIO_BOTCH
1070 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1072 return PerlIO_seek(fp, pos, whence) >= 0;
1074 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1075 report_evil_fh(gv, io, PL_op->op_type);
1076 SETERRNO(EBADF,RMS_IFI);
1081 Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
1084 register IO *io = NULL;
1085 register PerlIO *fp;
1087 PERL_ARGS_ASSERT_DO_SYSSEEK;
1089 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
1090 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
1091 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1092 report_evil_fh(gv, io, PL_op->op_type);
1093 SETERRNO(EBADF,RMS_IFI);
1098 Perl_mode_from_discipline(pTHX_ const char *s, STRLEN len)
1100 int mode = O_BINARY;
1106 if (s[2] == 'a' && s[3] == 'w'
1107 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1116 if (s[2] == 'r' && s[3] == 'l' && s[4] == 'f'
1117 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1126 goto fail_discipline;
1129 else if (isSPACE(*s)) {
1136 end = strchr(s+1, ':');
1139 #ifndef PERLIO_LAYERS
1140 Perl_croak(aTHX_ "IO layers (like '%.*s') unavailable", end-s, s);
1151 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE)
1153 my_chsize(int fd, Off_t length)
1156 /* code courtesy of William Kucharski */
1161 if (PerlLIO_fstat(fd, &filebuf) < 0)
1164 if (filebuf.st_size < length) {
1166 /* extend file length */
1168 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
1171 /* write a "0" byte */
1173 if ((PerlLIO_write(fd, "", 1)) != 1)
1177 /* truncate length */
1181 fl.l_start = length;
1182 fl.l_type = F_WRLCK; /* write lock on file space */
1185 * This relies on the UNDOCUMENTED F_FREESP argument to
1186 * fcntl(2), which truncates the file so that it ends at the
1187 * position indicated by fl.l_start.
1189 * Will minor miracles never cease?
1192 if (fcntl(fd, F_FREESP, &fl) < 0)
1198 Perl_croak_nocontext("truncate not implemented");
1199 #endif /* F_FREESP */
1202 #endif /* !HAS_TRUNCATE && !HAS_CHSIZE */
1205 Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
1209 PERL_ARGS_ASSERT_DO_PRINT;
1211 /* assuming fp is checked earlier */
1214 if (SvTYPE(sv) == SVt_IV && SvIOK(sv)) {
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 const char *tmps = SvPV_const(sv, len);
1229 if (PerlIO_isutf8(fp)) {
1231 /* We don't modify the original scalar. */
1232 tmpbuf = bytes_to_utf8((const U8*) tmps, &len);
1233 tmps = (char *) tmpbuf;
1236 else if (DO_UTF8(sv)) {
1237 STRLEN tmplen = len;
1239 U8 * const result = bytes_from_utf8((const U8*) tmps, &tmplen, &utf8);
1242 tmps = (char *) tmpbuf;
1246 assert((char *)result == tmps);
1247 if (ckWARN_d(WARN_UTF8)) {
1248 Perl_warner(aTHX_ packWARN(WARN_UTF8),
1249 "Wide character in print");
1253 /* To detect whether the process is about to overstep its
1254 * filesize limit we would need getrlimit(). We could then
1255 * also transparently raise the limit with setrlimit() --
1256 * but only until the system hard limit/the filesystem limit,
1257 * at which we would get EPERM. Note that when using buffered
1258 * io the write failure can be delayed until the flush/close. --jhi */
1259 if (len && (PerlIO_write(fp,tmps,len) == 0))
1262 return happy ? !PerlIO_error(fp) : FALSE;
1274 if (PL_op->op_flags & OPf_REF) {
1279 return PL_laststatval;
1282 PL_laststype = OP_STAT;
1284 sv_setpvs(PL_statname, "");
1287 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
1288 } else if (IoDIRP(io)) {
1289 return (PL_laststatval = PerlLIO_fstat(my_dirfd(IoDIRP(io)), &PL_statcache));
1291 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1292 report_evil_fh(gv, io, PL_op->op_type);
1293 return (PL_laststatval = -1);
1296 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1297 report_evil_fh(gv, io, PL_op->op_type);
1298 return (PL_laststatval = -1);
1301 else if (PL_op->op_private & OPpFT_STACKED) {
1302 return PL_laststatval;
1305 SV* const sv = POPs;
1309 if (isGV_with_GP(sv)) {
1310 gv = MUTABLE_GV(sv);
1313 else if (SvROK(sv) && isGV_with_GP(SvRV(sv))) {
1314 gv = MUTABLE_GV(SvRV(sv));
1317 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
1318 io = MUTABLE_IO(SvRV(sv));
1320 goto do_fstat_have_io;
1323 s = SvPV_const(sv, len);
1325 sv_setpvn(PL_statname, s, len);
1326 s = SvPVX_const(PL_statname); /* s now NUL-terminated */
1327 PL_laststype = OP_STAT;
1328 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1329 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
1330 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
1331 return PL_laststatval;
1340 static const char no_prev_lstat[] = "The stat preceding -l _ wasn't an lstat";
1344 if (PL_op->op_flags & OPf_REF) {
1346 if (cGVOP_gv == PL_defgv) {
1347 if (PL_laststype != OP_LSTAT)
1348 Perl_croak(aTHX_ no_prev_lstat);
1349 return PL_laststatval;
1351 if (ckWARN(WARN_IO)) {
1352 Perl_warner(aTHX_ packWARN(WARN_IO), "Use of -l on filehandle %s",
1354 return (PL_laststatval = -1);
1357 else if (PL_laststype != OP_LSTAT
1358 && (PL_op->op_private & OPpFT_STACKED) && ckWARN(WARN_IO))
1359 Perl_croak(aTHX_ no_prev_lstat);
1361 PL_laststype = OP_LSTAT;
1365 if (SvROK(sv) && isGV_with_GP(SvRV(sv)) && ckWARN(WARN_IO)) {
1366 Perl_warner(aTHX_ packWARN(WARN_IO), "Use of -l on filehandle %s",
1367 GvENAME((const GV *)SvRV(sv)));
1368 return (PL_laststatval = -1);
1370 file = SvPV_nolen_const(sv);
1371 sv_setpv(PL_statname,file);
1372 PL_laststatval = PerlLIO_lstat(file,&PL_statcache);
1373 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(file, '\n'))
1374 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "lstat");
1375 return PL_laststatval;
1379 S_exec_failed(pTHX_ const char *cmd, int fd, int do_report)
1381 const int e = errno;
1382 PERL_ARGS_ASSERT_EXEC_FAILED;
1383 if (ckWARN(WARN_EXEC))
1384 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
1387 PerlLIO_write(fd, (void*)&e, sizeof(int));
1393 Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1394 int fd, int do_report)
1397 PERL_ARGS_ASSERT_DO_AEXEC5;
1398 #if defined(MACOS_TRADITIONAL) || defined(__SYMBIAN32__) || defined(__LIBCATAMOUNT__)
1399 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1403 const char *tmps = NULL;
1404 Newx(PL_Argv, sp - mark + 1, const char*);
1407 while (++mark <= sp) {
1409 *a++ = SvPV_nolen_const(*mark);
1415 tmps = SvPV_nolen_const(really);
1416 if ((!really && *PL_Argv[0] != '/') ||
1417 (really && *tmps != '/')) /* will execvp use PATH? */
1418 TAINT_ENV(); /* testing IFS here is overkill, probably */
1420 if (really && *tmps)
1421 PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
1423 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1425 S_exec_failed(aTHX_ (really ? tmps : PL_Argv[0]), fd, do_report);
1433 Perl_do_execfree(pTHX)
1442 #ifdef PERL_DEFAULT_DO_EXEC3_IMPLEMENTATION
1445 Perl_do_exec3(pTHX_ const char *incmd, int fd, int do_report)
1448 register const char **a;
1452 /* Make a copy so we can change it */
1453 const Size_t cmdlen = strlen(incmd) + 1;
1455 PERL_ARGS_ASSERT_DO_EXEC3;
1457 Newx(buf, cmdlen, char);
1459 memcpy(cmd, incmd, cmdlen);
1461 while (*cmd && isSPACE(*cmd))
1464 /* save an extra exec if possible */
1468 char flags[PERL_FLAGS_MAX];
1469 if (strnEQ(cmd,PL_cshname,PL_cshlen) &&
1470 strnEQ(cmd+PL_cshlen," -c",3)) {
1471 my_strlcpy(flags, "-c", PERL_FLAGS_MAX);
1472 s = cmd+PL_cshlen+3;
1475 my_strlcat(flags, "f", PERL_FLAGS_MAX - 2);
1480 char * const ncmd = s;
1486 if (s[-1] == '\'') {
1489 PerlProc_execl(PL_cshname, "csh", flags, ncmd, (char*)NULL);
1492 S_exec_failed(aTHX_ PL_cshname, fd, do_report);
1501 /* see if there are shell metacharacters in it */
1503 if (*cmd == '.' && isSPACE(cmd[1]))
1506 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1511 s++; /* catch VAR=val gizmo */
1515 for (s = cmd; *s; s++) {
1516 if (*s != ' ' && !isALPHA(*s) &&
1517 strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1518 if (*s == '\n' && !s[1]) {
1522 /* handle the 2>&1 construct at the end */
1523 if (*s == '>' && s[1] == '&' && s[2] == '1'
1524 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1525 && (!s[3] || isSPACE(s[3])))
1527 const char *t = s + 3;
1529 while (*t && isSPACE(*t))
1531 if (!*t && (PerlLIO_dup2(1,2) != -1)) {
1538 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char *)NULL);
1540 S_exec_failed(aTHX_ PL_sh_path, fd, do_report);
1546 Newx(PL_Argv, (s - cmd) / 2 + 2, const char*);
1547 PL_Cmd = savepvn(cmd, s-cmd);
1549 for (s = PL_Cmd; *s;) {
1554 while (*s && !isSPACE(*s))
1562 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1564 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1568 S_exec_failed(aTHX_ PL_Argv[0], fd, do_report);
1575 #endif /* OS2 || WIN32 */
1578 Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
1582 register I32 tot = 0;
1583 const char *const what = PL_op_name[type];
1585 SV ** const oldmark = mark;
1587 PERL_ARGS_ASSERT_APPLY;
1589 /* Doing this ahead of the switch statement preserves the old behaviour,
1590 where attempting to use kill as a taint test test would fail on
1591 platforms where kill was not defined. */
1593 if (type == OP_KILL)
1594 Perl_die(aTHX_ PL_no_func, what);
1597 if (type == OP_CHOWN)
1598 Perl_die(aTHX_ PL_no_func, what);
1602 #define APPLY_TAINT_PROPER() \
1604 if (PL_tainted) { TAINT_PROPER(what); } \
1607 /* This is a first heuristic; it doesn't catch tainting magic. */
1609 while (++mark <= sp) {
1610 if (SvTAINTED(*mark)) {
1619 APPLY_TAINT_PROPER();
1622 APPLY_TAINT_PROPER();
1624 while (++mark <= sp) {
1626 if (isGV_with_GP(*mark)) {
1627 gv = MUTABLE_GV(*mark);
1629 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1631 APPLY_TAINT_PROPER();
1632 if (fchmod(PerlIO_fileno(IoIFP(GvIOn(gv))), val))
1635 Perl_die(aTHX_ PL_no_func, "fchmod");
1642 else if (SvROK(*mark) && isGV_with_GP(SvRV(*mark))) {
1643 gv = MUTABLE_GV(SvRV(*mark));
1647 const char *name = SvPV_nolen_const(*mark);
1648 APPLY_TAINT_PROPER();
1649 if (PerlLIO_chmod(name, val))
1657 APPLY_TAINT_PROPER();
1658 if (sp - mark > 2) {
1660 val = SvIVx(*++mark);
1661 val2 = SvIVx(*++mark);
1662 APPLY_TAINT_PROPER();
1664 while (++mark <= sp) {
1666 if (isGV_with_GP(*mark)) {
1667 gv = MUTABLE_GV(*mark);
1669 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1671 APPLY_TAINT_PROPER();
1672 if (fchown(PerlIO_fileno(IoIFP(GvIOn(gv))), val, val2))
1675 Perl_die(aTHX_ PL_no_func, "fchown");
1682 else if (SvROK(*mark) && isGV_with_GP(SvRV(*mark))) {
1683 gv = MUTABLE_GV(SvRV(*mark));
1687 const char *name = SvPV_nolen_const(*mark);
1688 APPLY_TAINT_PROPER();
1689 if (PerlLIO_chown(name, val, val2))
1697 XXX Should we make lchown() directly available from perl?
1698 For now, we'll let Configure test for HAS_LCHOWN, but do
1699 nothing in the core.
1704 APPLY_TAINT_PROPER();
1707 s = SvPVx_nolen_const(*++mark);
1709 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1711 if ((val = whichsig(s)) < 0)
1712 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
1716 APPLY_TAINT_PROPER();
1719 /* kill() doesn't do process groups (job trees?) under VMS */
1720 if (val < 0) val = -val;
1721 if (val == SIGKILL) {
1722 # include <starlet.h>
1723 /* Use native sys$delprc() to insure that target process is
1724 * deleted; supervisor-mode images don't pay attention to
1725 * CRTL's emulation of Unix-style signals and kill()
1727 while (++mark <= sp) {
1728 I32 proc = SvIV(*mark);
1729 register unsigned long int __vmssts;
1730 APPLY_TAINT_PROPER();
1731 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1735 case SS$_NOSUCHNODE:
1736 SETERRNO(ESRCH,__vmssts);
1739 SETERRNO(EPERM,__vmssts);
1742 SETERRNO(EVMSERR,__vmssts);
1751 while (++mark <= sp) {
1752 const I32 proc = SvIV(*mark);
1753 APPLY_TAINT_PROPER();
1755 if (PerlProc_killpg(proc,val)) /* BSD */
1757 if (PerlProc_kill(-proc,val)) /* SYSV */
1763 while (++mark <= sp) {
1764 const I32 proc = SvIV(*mark);
1765 APPLY_TAINT_PROPER();
1766 if (PerlProc_kill(proc, val))
1773 APPLY_TAINT_PROPER();
1775 while (++mark <= sp) {
1776 s = SvPV_nolen_const(*mark);
1777 APPLY_TAINT_PROPER();
1778 if (PL_euid || PL_unsafe) {
1782 else { /* don't let root wipe out directories without -U */
1783 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1792 #if defined(HAS_UTIME) || defined(HAS_FUTIMES)
1794 APPLY_TAINT_PROPER();
1795 if (sp - mark > 2) {
1796 #if defined(HAS_FUTIMES)
1797 struct timeval utbuf[2];
1798 void *utbufp = utbuf;
1799 #elif defined(I_UTIME) || defined(VMS)
1800 struct utimbuf utbuf;
1801 struct utimbuf *utbufp = &utbuf;
1807 void *utbufp = &utbuf;
1810 SV* const accessed = *++mark;
1811 SV* const modified = *++mark;
1813 /* Be like C, and if both times are undefined, let the C
1814 * library figure out what to do. This usually means
1815 * "current time". */
1817 if ( accessed == &PL_sv_undef && modified == &PL_sv_undef )
1820 Zero(&utbuf, sizeof utbuf, char);
1822 utbuf[0].tv_sec = (long)SvIV(accessed); /* time accessed */
1823 utbuf[0].tv_usec = 0;
1824 utbuf[1].tv_sec = (long)SvIV(modified); /* time modified */
1825 utbuf[1].tv_usec = 0;
1826 #elif defined(BIG_TIME)
1827 utbuf.actime = (Time_t)SvNV(accessed); /* time accessed */
1828 utbuf.modtime = (Time_t)SvNV(modified); /* time modified */
1830 utbuf.actime = (Time_t)SvIV(accessed); /* time accessed */
1831 utbuf.modtime = (Time_t)SvIV(modified); /* time modified */
1834 APPLY_TAINT_PROPER();
1836 while (++mark <= sp) {
1838 if (isGV_with_GP(*mark)) {
1839 gv = MUTABLE_GV(*mark);
1841 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1843 APPLY_TAINT_PROPER();
1844 if (futimes(PerlIO_fileno(IoIFP(GvIOn(gv))),
1845 (struct timeval *) utbufp))
1848 Perl_die(aTHX_ PL_no_func, "futimes");
1855 else if (SvROK(*mark) && isGV_with_GP(SvRV(*mark))) {
1856 gv = MUTABLE_GV(SvRV(*mark));
1860 const char * const name = SvPV_nolen_const(*mark);
1861 APPLY_TAINT_PROPER();
1863 if (utimes(name, (struct timeval *)utbufp))
1865 if (PerlLIO_utime(name, utbufp))
1879 #undef APPLY_TAINT_PROPER
1882 /* Do the permissions allow some operation? Assumes statcache already set. */
1883 #ifndef VMS /* VMS' cando is in vms.c */
1885 Perl_cando(pTHX_ Mode_t mode, bool effective, register const Stat_t *statbufp)
1886 /* effective is a flag, true for EUID, or for checking if the effective gid
1887 * is in the list of groups returned from getgroups().
1892 PERL_ARGS_ASSERT_CANDO;
1895 /* [Comments and code from Len Reed]
1896 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1897 * to write-protected files. The execute permission bit is set
1898 * by the Miscrosoft C library stat() function for the following:
1903 * All files and directories are readable.
1904 * Directories and special files, e.g. "CON", cannot be
1906 * [Comment by Tom Dinger -- a directory can have the write-protect
1907 * bit set in the file system, but DOS permits changes to
1908 * the directory anyway. In addition, all bets are off
1909 * here for networked software, such as Novell and
1913 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1914 * too so it will actually look into the files for magic numbers
1916 return (mode & statbufp->st_mode) ? TRUE : FALSE;
1918 #else /* ! DOSISH */
1919 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
1920 if (mode == S_IXUSR) {
1921 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1925 return TRUE; /* root reads and writes anything */
1928 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1929 if (statbufp->st_mode & mode)
1930 return TRUE; /* ok as "user" */
1932 else if (ingroup(statbufp->st_gid,effective)) {
1933 if (statbufp->st_mode & mode >> 3)
1934 return TRUE; /* ok as "group" */
1936 else if (statbufp->st_mode & mode >> 6)
1937 return TRUE; /* ok as "other" */
1939 #endif /* ! DOSISH */
1944 S_ingroup(pTHX_ Gid_t testgid, bool effective)
1946 #ifdef MACOS_TRADITIONAL
1947 /* This is simply not correct for AppleShare, but fix it yerself. */
1951 if (testgid == (effective ? PL_egid : PL_gid))
1953 #ifdef HAS_GETGROUPS
1955 Groups_t *gary = NULL;
1959 anum = getgroups(0, gary);
1960 Newx(gary, anum, Groups_t);
1961 anum = getgroups(anum, gary);
1963 if (gary[anum] == testgid) {
1977 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1980 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
1983 const key_t key = (key_t)SvNVx(*++mark);
1984 const I32 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1985 const I32 flags = SvIVx(*++mark);
1987 PERL_ARGS_ASSERT_DO_IPCGET;
1988 PERL_UNUSED_ARG(sp);
1995 return msgget(key, flags);
1999 return semget(key, n, flags);
2003 return shmget(key, n, flags);
2005 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
2007 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2010 return -1; /* should never happen */
2014 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
2019 const I32 id = SvIVx(*++mark);
2021 const I32 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
2023 const I32 cmd = SvIVx(*++mark);
2024 SV * const astr = *++mark;
2025 STRLEN infosize = 0;
2026 I32 getinfo = (cmd == IPC_STAT);
2028 PERL_ARGS_ASSERT_DO_IPCCTL;
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_ARGS_ASSERT_DO_MSGSND;
2152 PERL_UNUSED_ARG(sp);
2155 Perl_croak(aTHX_ "Arg too short for msgsnd");
2157 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
2159 PERL_UNUSED_ARG(sp);
2160 PERL_UNUSED_ARG(mark);
2161 Perl_croak(aTHX_ "msgsnd not implemented");
2166 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
2172 I32 msize, flags, ret;
2173 const I32 id = SvIVx(*++mark);
2174 SV * const mstr = *++mark;
2176 PERL_ARGS_ASSERT_DO_MSGRCV;
2177 PERL_UNUSED_ARG(sp);
2179 /* suppress warning when reading into undef var --jhi */
2181 sv_setpvs(mstr, "");
2182 msize = SvIVx(*++mark);
2183 mtype = (long)SvIVx(*++mark);
2184 flags = SvIVx(*++mark);
2185 SvPV_force_nolen(mstr);
2186 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
2189 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
2191 SvCUR_set(mstr, sizeof(long)+ret);
2192 *SvEND(mstr) = '\0';
2193 #ifndef INCOMPLETE_TAINTS
2194 /* who knows who has been playing with this message? */
2200 PERL_UNUSED_ARG(sp);
2201 PERL_UNUSED_ARG(mark);
2202 Perl_croak(aTHX_ "msgrcv not implemented");
2207 Perl_do_semop(pTHX_ SV **mark, SV **sp)
2212 const I32 id = SvIVx(*++mark);
2213 SV * const opstr = *++mark;
2214 const char * const opbuf = SvPV_const(opstr, opsize);
2216 PERL_ARGS_ASSERT_DO_SEMOP;
2217 PERL_UNUSED_ARG(sp);
2219 if (opsize < 3 * SHORTSIZE
2220 || (opsize % (3 * SHORTSIZE))) {
2221 SETERRNO(EINVAL,LIB_INVARG);
2225 /* We can't assume that sizeof(struct sembuf) == 3 * sizeof(short). */
2227 const int nsops = opsize / (3 * sizeof (short));
2229 short * const ops = (short *) opbuf;
2231 struct sembuf *temps, *t;
2234 Newx (temps, nsops, struct sembuf);
2242 result = semop(id, temps, nsops);
2256 Perl_croak(aTHX_ "semop not implemented");
2261 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
2266 struct shmid_ds shmds;
2267 const I32 id = SvIVx(*++mark);
2268 SV * const mstr = *++mark;
2269 const I32 mpos = SvIVx(*++mark);
2270 const I32 msize = SvIVx(*++mark);
2272 PERL_ARGS_ASSERT_DO_SHMIO;
2273 PERL_UNUSED_ARG(sp);
2276 if (shmctl(id, IPC_STAT, &shmds) == -1)
2278 if (mpos < 0 || msize < 0
2279 || (size_t)mpos + msize > (size_t)shmds.shm_segsz) {
2280 SETERRNO(EFAULT,SS_ACCVIO); /* can't do as caller requested */
2283 shm = (char *)shmat(id, NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
2284 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2286 if (optype == OP_SHMREAD) {
2288 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2290 sv_setpvs(mstr, "");
2291 SvPV_force_nolen(mstr);
2292 mbuf = SvGROW(mstr, (STRLEN)msize+1);
2294 Copy(shm + mpos, mbuf, msize, char);
2295 SvCUR_set(mstr, msize);
2296 *SvEND(mstr) = '\0';
2298 #ifndef INCOMPLETE_TAINTS
2299 /* who knows who has been playing with this shared memory? */
2306 const char *mbuf = SvPV_const(mstr, len);
2307 const I32 n = ((I32)len > msize) ? msize : (I32)len;
2308 Copy(mbuf, shm + mpos, n, char);
2310 memzero(shm + mpos + n, msize - n);
2314 Perl_croak(aTHX_ "shm I/O not implemented");
2318 #endif /* SYSV IPC */
2323 =for apidoc start_glob
2325 Function called by C<do_readline> to spawn a glob (or do the glob inside
2326 perl on VMS). This code used to be inline, but now perl uses C<File::Glob>
2327 this glob starter is only used by miniperl during the build process.
2328 Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up.
2334 Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
2337 SV * const tmpcmd = newSV(0);
2340 PERL_ARGS_ASSERT_START_GLOB;
2344 #ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
2345 /* since spawning off a process is a real performance hit */
2352 fp = Perl_vms_start_glob(aTHX_ tmpglob, io);
2355 #ifdef MACOS_TRADITIONAL
2356 sv_setpv(tmpcmd, "glob ");
2357 sv_catsv(tmpcmd, tmpglob);
2358 sv_catpv(tmpcmd, " |");
2362 sv_setpv(tmpcmd, "for a in ");
2363 sv_catsv(tmpcmd, tmpglob);
2364 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
2367 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
2368 sv_catsv(tmpcmd, tmpglob);
2370 sv_setpv(tmpcmd, "perlglob ");
2371 sv_catsv(tmpcmd, tmpglob);
2372 sv_catpv(tmpcmd, " |");
2377 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
2378 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
2379 sv_catsv(tmpcmd, tmpglob);
2380 sv_catpv(tmpcmd, "' 2>/dev/null |");
2382 sv_setpv(tmpcmd, "echo ");
2383 sv_catsv(tmpcmd, tmpglob);
2385 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
2387 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
2390 #endif /* !DOSISH */
2391 #endif /* MACOS_TRADITIONAL */
2392 (void)do_open(PL_last_in_gv, (char*)SvPVX_const(tmpcmd), SvCUR(tmpcmd),
2393 FALSE, O_RDONLY, 0, NULL);
2402 * c-indentation-style: bsd
2404 * indent-tabs-mode: t
2407 * ex: set ts=8 sts=4 sw=4 noet: