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)
666 const int save_errno = errno;
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),PL_inplace!=0,
818 (void)UNLINK(SvPVX_const(sv));
819 if (link(PL_oldname,SvPVX_const(sv)) < 0) {
820 if (ckWARN_d(WARN_INPLACE))
821 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
822 "Can't rename %s to %"SVf": %s, skipping file",
823 PL_oldname, SVfARG(sv), Strerror(errno) );
827 (void)UNLINK(PL_oldname);
831 #if !defined(DOSISH) && !defined(AMIGAOS)
832 # ifndef VMS /* Don't delete; use automatic file versioning */
833 if (UNLINK(PL_oldname) < 0) {
834 if (ckWARN_d(WARN_INPLACE))
835 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
836 "Can't remove %s: %s, skipping file",
837 PL_oldname, Strerror(errno) );
843 Perl_croak(aTHX_ "Can't do inplace edit without backup");
847 sv_setpvn(sv,">",!PL_inplace);
848 sv_catpvn(sv,PL_oldname,oldlen);
849 SETERRNO(0,0); /* in case sprintf set errno */
851 if (!do_open(PL_argvoutgv,(char*)SvPVX_const(sv),SvCUR(sv),
852 PL_inplace!=0,O_WRONLY|O_CREAT|O_TRUNC,0,NULL))
854 if (!do_open(PL_argvoutgv,(char*)SvPVX_const(sv),SvCUR(sv),
855 PL_inplace!=0,O_WRONLY|O_CREAT|OPEN_EXCL,0666,
859 if (ckWARN_d(WARN_INPLACE))
860 Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't do inplace edit on %s: %s",
861 PL_oldname, Strerror(errno) );
865 setdefout(PL_argvoutgv);
866 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
867 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
869 (void)fchmod(PL_lastfd,PL_filemode);
871 # if !(defined(WIN32) && defined(__BORLANDC__))
872 /* Borland runtime creates a readonly file! */
873 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
876 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
878 (void)fchown(PL_lastfd,fileuid,filegid);
881 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
886 return IoIFP(GvIOp(gv));
889 if (ckWARN_d(WARN_INPLACE)) {
890 const int eno = errno;
891 if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0
892 && !S_ISREG(PL_statbuf.st_mode))
894 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
895 "Can't do inplace edit: %s is not a regular file",
899 Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't open %s: %s",
900 PL_oldname, Strerror(eno));
904 if (io && (IoFLAGS(io) & IOf_ARGV))
905 IoFLAGS(io) |= IOf_START;
907 (void)do_close(PL_argvoutgv,FALSE);
908 if (io && (IoFLAGS(io) & IOf_ARGV)
909 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
911 GV * const oldout = MUTABLE_GV(av_pop(PL_argvout_stack));
913 SvREFCNT_dec(oldout);
916 setdefout(gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO));
921 /* explicit renamed to avoid C++ conflict -- kja */
923 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
931 if (!gv || !isGV_with_GP(gv)) {
933 SETERRNO(EBADF,SS_IVCHAN);
937 if (!io) { /* never opened */
939 if (ckWARN(WARN_UNOPENED)) /* no check for closed here */
940 report_evil_fh(gv, io, PL_op->op_type);
941 SETERRNO(EBADF,SS_IVCHAN);
945 retval = io_close(io, not_implicit);
949 IoLINES_LEFT(io) = IoPAGE_LEN(io);
951 IoTYPE(io) = IoTYPE_CLOSED;
956 Perl_io_close(pTHX_ IO *io, bool not_implicit)
961 PERL_ARGS_ASSERT_IO_CLOSE;
964 if (IoTYPE(io) == IoTYPE_PIPE) {
965 const int status = PerlProc_pclose(IoIFP(io));
967 STATUS_NATIVE_CHILD_SET(status);
968 retval = (STATUS_UNIX == 0);
971 retval = (status != -1);
974 else if (IoTYPE(io) == IoTYPE_STD)
977 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
978 const bool prev_err = PerlIO_error(IoOFP(io));
979 retval = (PerlIO_close(IoOFP(io)) != EOF && !prev_err);
980 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
983 const bool prev_err = PerlIO_error(IoIFP(io));
984 retval = (PerlIO_close(IoIFP(io)) != EOF && !prev_err);
987 IoOFP(io) = IoIFP(io) = NULL;
989 else if (not_implicit) {
990 SETERRNO(EBADF,SS_IVCHAN);
997 Perl_do_eof(pTHX_ GV *gv)
1000 register IO * const io = GvIO(gv);
1002 PERL_ARGS_ASSERT_DO_EOF;
1006 else if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
1007 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1010 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
1011 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
1012 return FALSE; /* this is the most usual case */
1016 /* getc and ungetc can stomp on errno */
1017 const int saverrno = errno;
1018 const int ch = PerlIO_getc(IoIFP(io));
1020 (void)PerlIO_ungetc(IoIFP(io),ch);
1027 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
1028 if (PerlIO_get_cnt(IoIFP(io)) < -1)
1029 PerlIO_set_cnt(IoIFP(io),-1);
1031 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
1032 if (gv != PL_argvgv || !nextargv(gv)) /* get another fp handy */
1036 return TRUE; /* normal fp, definitely end of file */
1042 Perl_do_tell(pTHX_ GV *gv)
1045 register IO *io = NULL;
1046 register PerlIO *fp;
1048 PERL_ARGS_ASSERT_DO_TELL;
1050 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1051 #ifdef ULTRIX_STDIO_BOTCH
1053 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1055 return PerlIO_tell(fp);
1057 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1058 report_evil_fh(gv, io, PL_op->op_type);
1059 SETERRNO(EBADF,RMS_IFI);
1064 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
1067 register IO *io = NULL;
1068 register PerlIO *fp;
1070 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1071 #ifdef ULTRIX_STDIO_BOTCH
1073 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1075 return PerlIO_seek(fp, pos, whence) >= 0;
1077 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1078 report_evil_fh(gv, io, PL_op->op_type);
1079 SETERRNO(EBADF,RMS_IFI);
1084 Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
1087 register IO *io = NULL;
1088 register PerlIO *fp;
1090 PERL_ARGS_ASSERT_DO_SYSSEEK;
1092 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
1093 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
1094 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1095 report_evil_fh(gv, io, PL_op->op_type);
1096 SETERRNO(EBADF,RMS_IFI);
1101 Perl_mode_from_discipline(pTHX_ const char *s, STRLEN len)
1103 int mode = O_BINARY;
1109 if (s[2] == 'a' && s[3] == 'w'
1110 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1119 if (s[2] == 'r' && s[3] == 'l' && s[4] == 'f'
1120 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1129 goto fail_discipline;
1132 else if (isSPACE(*s)) {
1139 end = strchr(s+1, ':');
1142 #ifndef PERLIO_LAYERS
1143 Perl_croak(aTHX_ "IO layers (like '%.*s') unavailable", end-s, s);
1154 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE)
1156 my_chsize(int fd, Off_t length)
1159 /* code courtesy of William Kucharski */
1164 if (PerlLIO_fstat(fd, &filebuf) < 0)
1167 if (filebuf.st_size < length) {
1169 /* extend file length */
1171 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
1174 /* write a "0" byte */
1176 if ((PerlLIO_write(fd, "", 1)) != 1)
1180 /* truncate length */
1184 fl.l_start = length;
1185 fl.l_type = F_WRLCK; /* write lock on file space */
1188 * This relies on the UNDOCUMENTED F_FREESP argument to
1189 * fcntl(2), which truncates the file so that it ends at the
1190 * position indicated by fl.l_start.
1192 * Will minor miracles never cease?
1195 if (fcntl(fd, F_FREESP, &fl) < 0)
1201 Perl_croak_nocontext("truncate not implemented");
1202 #endif /* F_FREESP */
1205 #endif /* !HAS_TRUNCATE && !HAS_CHSIZE */
1208 Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
1212 PERL_ARGS_ASSERT_DO_PRINT;
1214 /* assuming fp is checked earlier */
1217 if (SvTYPE(sv) == SVt_IV && SvIOK(sv)) {
1218 assert(!SvGMAGICAL(sv));
1220 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
1222 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
1223 return !PerlIO_error(fp);
1227 /* Do this first to trigger any overloading. */
1228 const char *tmps = SvPV_const(sv, len);
1232 if (PerlIO_isutf8(fp)) {
1234 /* We don't modify the original scalar. */
1235 tmpbuf = bytes_to_utf8((const U8*) tmps, &len);
1236 tmps = (char *) tmpbuf;
1239 else if (DO_UTF8(sv)) {
1240 STRLEN tmplen = len;
1242 U8 * const result = bytes_from_utf8((const U8*) tmps, &tmplen, &utf8);
1245 tmps = (char *) tmpbuf;
1249 assert((char *)result == tmps);
1250 if (ckWARN_d(WARN_UTF8)) {
1251 Perl_warner(aTHX_ packWARN(WARN_UTF8),
1252 "Wide character in print");
1256 /* To detect whether the process is about to overstep its
1257 * filesize limit we would need getrlimit(). We could then
1258 * also transparently raise the limit with setrlimit() --
1259 * but only until the system hard limit/the filesystem limit,
1260 * at which we would get EPERM. Note that when using buffered
1261 * io the write failure can be delayed until the flush/close. --jhi */
1262 if (len && (PerlIO_write(fp,tmps,len) == 0))
1265 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_setpvs(PL_statname, "");
1290 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
1291 } else if (IoDIRP(io)) {
1292 return (PL_laststatval = PerlLIO_fstat(my_dirfd(IoDIRP(io)), &PL_statcache));
1294 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1295 report_evil_fh(gv, io, PL_op->op_type);
1296 return (PL_laststatval = -1);
1299 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1300 report_evil_fh(gv, io, PL_op->op_type);
1301 return (PL_laststatval = -1);
1304 else if (PL_op->op_private & OPpFT_STACKED) {
1305 return PL_laststatval;
1308 SV* const sv = POPs;
1312 if (isGV_with_GP(sv)) {
1313 gv = MUTABLE_GV(sv);
1316 else if (SvROK(sv) && isGV_with_GP(SvRV(sv))) {
1317 gv = MUTABLE_GV(SvRV(sv));
1320 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
1321 io = MUTABLE_IO(SvRV(sv));
1323 goto do_fstat_have_io;
1326 s = SvPV_const(sv, len);
1328 sv_setpvn(PL_statname, s, len);
1329 s = SvPVX_const(PL_statname); /* s now NUL-terminated */
1330 PL_laststype = OP_STAT;
1331 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1332 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
1333 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
1334 return PL_laststatval;
1343 static const char no_prev_lstat[] = "The stat preceding -l _ wasn't an lstat";
1347 if (PL_op->op_flags & OPf_REF) {
1349 if (cGVOP_gv == PL_defgv) {
1350 if (PL_laststype != OP_LSTAT)
1351 Perl_croak(aTHX_ no_prev_lstat);
1352 return PL_laststatval;
1354 if (ckWARN(WARN_IO)) {
1355 Perl_warner(aTHX_ packWARN(WARN_IO), "Use of -l on filehandle %s",
1357 return (PL_laststatval = -1);
1360 else if (PL_laststype != OP_LSTAT
1361 && (PL_op->op_private & OPpFT_STACKED) && ckWARN(WARN_IO))
1362 Perl_croak(aTHX_ no_prev_lstat);
1364 PL_laststype = OP_LSTAT;
1368 if (SvROK(sv) && isGV_with_GP(SvRV(sv)) && ckWARN(WARN_IO)) {
1369 Perl_warner(aTHX_ packWARN(WARN_IO), "Use of -l on filehandle %s",
1370 GvENAME((const GV *)SvRV(sv)));
1371 return (PL_laststatval = -1);
1373 file = SvPV_nolen_const(sv);
1374 sv_setpv(PL_statname,file);
1375 PL_laststatval = PerlLIO_lstat(file,&PL_statcache);
1376 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(file, '\n'))
1377 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "lstat");
1378 return PL_laststatval;
1382 S_exec_failed(pTHX_ const char *cmd, int fd, int do_report)
1384 const int e = errno;
1385 PERL_ARGS_ASSERT_EXEC_FAILED;
1386 if (ckWARN(WARN_EXEC))
1387 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
1390 PerlLIO_write(fd, (void*)&e, sizeof(int));
1396 Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1397 int fd, int do_report)
1400 PERL_ARGS_ASSERT_DO_AEXEC5;
1401 #if defined(MACOS_TRADITIONAL) || defined(__SYMBIAN32__) || defined(__LIBCATAMOUNT__)
1402 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1406 const char *tmps = NULL;
1407 Newx(PL_Argv, sp - mark + 1, const char*);
1410 while (++mark <= sp) {
1412 *a++ = SvPV_nolen_const(*mark);
1418 tmps = SvPV_nolen_const(really);
1419 if ((!really && *PL_Argv[0] != '/') ||
1420 (really && *tmps != '/')) /* will execvp use PATH? */
1421 TAINT_ENV(); /* testing IFS here is overkill, probably */
1423 if (really && *tmps)
1424 PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
1426 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1428 S_exec_failed(aTHX_ (really ? tmps : PL_Argv[0]), fd, do_report);
1436 Perl_do_execfree(pTHX)
1445 #ifdef PERL_DEFAULT_DO_EXEC3_IMPLEMENTATION
1448 Perl_do_exec3(pTHX_ const char *incmd, int fd, int do_report)
1451 register const char **a;
1455 /* Make a copy so we can change it */
1456 const Size_t cmdlen = strlen(incmd) + 1;
1458 PERL_ARGS_ASSERT_DO_EXEC3;
1460 Newx(buf, cmdlen, char);
1462 memcpy(cmd, incmd, cmdlen);
1464 while (*cmd && isSPACE(*cmd))
1467 /* save an extra exec if possible */
1471 char flags[PERL_FLAGS_MAX];
1472 if (strnEQ(cmd,PL_cshname,PL_cshlen) &&
1473 strnEQ(cmd+PL_cshlen," -c",3)) {
1474 my_strlcpy(flags, "-c", PERL_FLAGS_MAX);
1475 s = cmd+PL_cshlen+3;
1478 my_strlcat(flags, "f", PERL_FLAGS_MAX - 2);
1483 char * const ncmd = s;
1489 if (s[-1] == '\'') {
1492 PerlProc_execl(PL_cshname, "csh", flags, ncmd, (char*)NULL);
1495 S_exec_failed(aTHX_ PL_cshname, fd, do_report);
1504 /* see if there are shell metacharacters in it */
1506 if (*cmd == '.' && isSPACE(cmd[1]))
1509 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1514 s++; /* catch VAR=val gizmo */
1518 for (s = cmd; *s; s++) {
1519 if (*s != ' ' && !isALPHA(*s) &&
1520 strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1521 if (*s == '\n' && !s[1]) {
1525 /* handle the 2>&1 construct at the end */
1526 if (*s == '>' && s[1] == '&' && s[2] == '1'
1527 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1528 && (!s[3] || isSPACE(s[3])))
1530 const char *t = s + 3;
1532 while (*t && isSPACE(*t))
1534 if (!*t && (PerlLIO_dup2(1,2) != -1)) {
1541 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char *)NULL);
1543 S_exec_failed(aTHX_ PL_sh_path, fd, do_report);
1549 Newx(PL_Argv, (s - cmd) / 2 + 2, const char*);
1550 PL_Cmd = savepvn(cmd, s-cmd);
1552 for (s = PL_Cmd; *s;) {
1557 while (*s && !isSPACE(*s))
1565 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1567 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1571 S_exec_failed(aTHX_ PL_Argv[0], fd, do_report);
1578 #endif /* OS2 || WIN32 */
1581 Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
1585 register I32 tot = 0;
1586 const char *const what = PL_op_name[type];
1588 SV ** const oldmark = mark;
1590 PERL_ARGS_ASSERT_APPLY;
1592 /* Doing this ahead of the switch statement preserves the old behaviour,
1593 where attempting to use kill as a taint test test would fail on
1594 platforms where kill was not defined. */
1596 if (type == OP_KILL)
1597 Perl_die(aTHX_ PL_no_func, what);
1600 if (type == OP_CHOWN)
1601 Perl_die(aTHX_ PL_no_func, what);
1605 #define APPLY_TAINT_PROPER() \
1607 if (PL_tainted) { TAINT_PROPER(what); } \
1610 /* This is a first heuristic; it doesn't catch tainting magic. */
1612 while (++mark <= sp) {
1613 if (SvTAINTED(*mark)) {
1622 APPLY_TAINT_PROPER();
1625 APPLY_TAINT_PROPER();
1627 while (++mark <= sp) {
1629 if (isGV_with_GP(*mark)) {
1630 gv = MUTABLE_GV(*mark);
1632 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1634 APPLY_TAINT_PROPER();
1635 if (fchmod(PerlIO_fileno(IoIFP(GvIOn(gv))), val))
1638 Perl_die(aTHX_ PL_no_func, "fchmod");
1645 else if (SvROK(*mark) && isGV_with_GP(SvRV(*mark))) {
1646 gv = MUTABLE_GV(SvRV(*mark));
1650 const char *name = SvPV_nolen_const(*mark);
1651 APPLY_TAINT_PROPER();
1652 if (PerlLIO_chmod(name, val))
1660 APPLY_TAINT_PROPER();
1661 if (sp - mark > 2) {
1663 val = SvIVx(*++mark);
1664 val2 = SvIVx(*++mark);
1665 APPLY_TAINT_PROPER();
1667 while (++mark <= sp) {
1669 if (isGV_with_GP(*mark)) {
1670 gv = MUTABLE_GV(*mark);
1672 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1674 APPLY_TAINT_PROPER();
1675 if (fchown(PerlIO_fileno(IoIFP(GvIOn(gv))), val, val2))
1678 Perl_die(aTHX_ PL_no_func, "fchown");
1685 else if (SvROK(*mark) && isGV_with_GP(SvRV(*mark))) {
1686 gv = MUTABLE_GV(SvRV(*mark));
1690 const char *name = SvPV_nolen_const(*mark);
1691 APPLY_TAINT_PROPER();
1692 if (PerlLIO_chown(name, val, val2))
1700 XXX Should we make lchown() directly available from perl?
1701 For now, we'll let Configure test for HAS_LCHOWN, but do
1702 nothing in the core.
1707 APPLY_TAINT_PROPER();
1710 s = SvPVx_nolen_const(*++mark);
1712 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1714 if ((val = whichsig(s)) < 0)
1715 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
1719 APPLY_TAINT_PROPER();
1722 /* kill() doesn't do process groups (job trees?) under VMS */
1723 if (val < 0) val = -val;
1724 if (val == SIGKILL) {
1725 # include <starlet.h>
1726 /* Use native sys$delprc() to insure that target process is
1727 * deleted; supervisor-mode images don't pay attention to
1728 * CRTL's emulation of Unix-style signals and kill()
1730 while (++mark <= sp) {
1731 I32 proc = SvIV(*mark);
1732 register unsigned long int __vmssts;
1733 APPLY_TAINT_PROPER();
1734 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1738 case SS$_NOSUCHNODE:
1739 SETERRNO(ESRCH,__vmssts);
1742 SETERRNO(EPERM,__vmssts);
1745 SETERRNO(EVMSERR,__vmssts);
1754 while (++mark <= sp) {
1755 const I32 proc = SvIV(*mark);
1756 APPLY_TAINT_PROPER();
1758 if (PerlProc_killpg(proc,val)) /* BSD */
1760 if (PerlProc_kill(-proc,val)) /* SYSV */
1766 while (++mark <= sp) {
1767 const I32 proc = SvIV(*mark);
1768 APPLY_TAINT_PROPER();
1769 if (PerlProc_kill(proc, val))
1776 APPLY_TAINT_PROPER();
1778 while (++mark <= sp) {
1779 s = SvPV_nolen_const(*mark);
1780 APPLY_TAINT_PROPER();
1781 if (PL_euid || PL_unsafe) {
1785 else { /* don't let root wipe out directories without -U */
1786 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1795 #if defined(HAS_UTIME) || defined(HAS_FUTIMES)
1797 APPLY_TAINT_PROPER();
1798 if (sp - mark > 2) {
1799 #if defined(HAS_FUTIMES)
1800 struct timeval utbuf[2];
1801 void *utbufp = utbuf;
1802 #elif defined(I_UTIME) || defined(VMS)
1803 struct utimbuf utbuf;
1804 struct utimbuf *utbufp = &utbuf;
1810 void *utbufp = &utbuf;
1813 SV* const accessed = *++mark;
1814 SV* const modified = *++mark;
1816 /* Be like C, and if both times are undefined, let the C
1817 * library figure out what to do. This usually means
1818 * "current time". */
1820 if ( accessed == &PL_sv_undef && modified == &PL_sv_undef )
1823 Zero(&utbuf, sizeof utbuf, char);
1825 utbuf[0].tv_sec = (long)SvIV(accessed); /* time accessed */
1826 utbuf[0].tv_usec = 0;
1827 utbuf[1].tv_sec = (long)SvIV(modified); /* time modified */
1828 utbuf[1].tv_usec = 0;
1829 #elif defined(BIG_TIME)
1830 utbuf.actime = (Time_t)SvNV(accessed); /* time accessed */
1831 utbuf.modtime = (Time_t)SvNV(modified); /* time modified */
1833 utbuf.actime = (Time_t)SvIV(accessed); /* time accessed */
1834 utbuf.modtime = (Time_t)SvIV(modified); /* time modified */
1837 APPLY_TAINT_PROPER();
1839 while (++mark <= sp) {
1841 if (isGV_with_GP(*mark)) {
1842 gv = MUTABLE_GV(*mark);
1844 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1846 APPLY_TAINT_PROPER();
1847 if (futimes(PerlIO_fileno(IoIFP(GvIOn(gv))),
1848 (struct timeval *) utbufp))
1851 Perl_die(aTHX_ PL_no_func, "futimes");
1858 else if (SvROK(*mark) && isGV_with_GP(SvRV(*mark))) {
1859 gv = MUTABLE_GV(SvRV(*mark));
1863 const char * const name = SvPV_nolen_const(*mark);
1864 APPLY_TAINT_PROPER();
1866 if (utimes(name, (struct timeval *)utbufp))
1868 if (PerlLIO_utime(name, utbufp))
1882 #undef APPLY_TAINT_PROPER
1885 /* Do the permissions allow some operation? Assumes statcache already set. */
1886 #ifndef VMS /* VMS' cando is in vms.c */
1888 Perl_cando(pTHX_ Mode_t mode, bool effective, register const Stat_t *statbufp)
1889 /* effective is a flag, true for EUID, or for checking if the effective gid
1890 * is in the list of groups returned from getgroups().
1895 PERL_ARGS_ASSERT_CANDO;
1898 /* [Comments and code from Len Reed]
1899 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1900 * to write-protected files. The execute permission bit is set
1901 * by the Miscrosoft C library stat() function for the following:
1906 * All files and directories are readable.
1907 * Directories and special files, e.g. "CON", cannot be
1909 * [Comment by Tom Dinger -- a directory can have the write-protect
1910 * bit set in the file system, but DOS permits changes to
1911 * the directory anyway. In addition, all bets are off
1912 * here for networked software, such as Novell and
1916 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1917 * too so it will actually look into the files for magic numbers
1919 return (mode & statbufp->st_mode) ? TRUE : FALSE;
1921 #else /* ! DOSISH */
1922 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
1923 if (mode == S_IXUSR) {
1924 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1928 return TRUE; /* root reads and writes anything */
1931 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1932 if (statbufp->st_mode & mode)
1933 return TRUE; /* ok as "user" */
1935 else if (ingroup(statbufp->st_gid,effective)) {
1936 if (statbufp->st_mode & mode >> 3)
1937 return TRUE; /* ok as "group" */
1939 else if (statbufp->st_mode & mode >> 6)
1940 return TRUE; /* ok as "other" */
1942 #endif /* ! DOSISH */
1947 Perl_ingroup(pTHX_ Gid_t testgid, bool effective)
1949 #ifdef MACOS_TRADITIONAL
1950 /* This is simply not correct for AppleShare, but fix it yerself. */
1954 if (testgid == (effective ? PL_egid : PL_gid))
1956 #ifdef HAS_GETGROUPS
1958 Groups_t *gary = NULL;
1962 anum = getgroups(0, gary);
1963 Newx(gary, anum, Groups_t);
1964 anum = getgroups(anum, gary);
1966 if (gary[anum] == testgid) {
1980 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1983 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
1986 const key_t key = (key_t)SvNVx(*++mark);
1987 const I32 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1988 const I32 flags = SvIVx(*++mark);
1990 PERL_ARGS_ASSERT_DO_IPCGET;
1991 PERL_UNUSED_ARG(sp);
1998 return msgget(key, flags);
2002 return semget(key, n, flags);
2006 return shmget(key, n, flags);
2008 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
2010 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2013 return -1; /* should never happen */
2017 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
2022 const I32 id = SvIVx(*++mark);
2024 const I32 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
2026 const I32 cmd = SvIVx(*++mark);
2027 SV * const astr = *++mark;
2028 STRLEN infosize = 0;
2029 I32 getinfo = (cmd == IPC_STAT);
2031 PERL_ARGS_ASSERT_DO_IPCCTL;
2032 PERL_UNUSED_ARG(sp);
2038 if (cmd == IPC_STAT || cmd == IPC_SET)
2039 infosize = sizeof(struct msqid_ds);
2044 if (cmd == IPC_STAT || cmd == IPC_SET)
2045 infosize = sizeof(struct shmid_ds);
2051 if (cmd == IPC_STAT || cmd == IPC_SET)
2052 infosize = sizeof(struct semid_ds);
2053 else if (cmd == GETALL || cmd == SETALL)
2055 struct semid_ds semds;
2057 #ifdef EXTRA_F_IN_SEMUN_BUF
2058 semun.buff = &semds;
2062 getinfo = (cmd == GETALL);
2063 if (Semctl(id, 0, IPC_STAT, semun) == -1)
2065 infosize = semds.sem_nsems * sizeof(short);
2066 /* "short" is technically wrong but much more portable
2067 than guessing about u_?short(_t)? */
2070 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2074 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
2076 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2084 SvPV_force_nolen(astr);
2085 a = SvGROW(astr, infosize+1);
2090 a = SvPV(astr, len);
2091 if (len != infosize)
2092 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
2100 const IV i = SvIV(astr);
2101 a = INT2PTR(char *,i); /* ouch */
2108 ret = msgctl(id, cmd, (struct msqid_ds *)a);
2114 union semun unsemds;
2116 #ifdef EXTRA_F_IN_SEMUN_BUF
2117 unsemds.buff = (struct semid_ds *)a;
2119 unsemds.buf = (struct semid_ds *)a;
2121 ret = Semctl(id, n, cmd, unsemds);
2123 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2130 ret = shmctl(id, cmd, (struct shmid_ds *)a);
2134 if (getinfo && ret >= 0) {
2135 SvCUR_set(astr, infosize);
2136 *SvEND(astr) = '\0';
2143 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
2148 const I32 id = SvIVx(*++mark);
2149 SV * const mstr = *++mark;
2150 const I32 flags = SvIVx(*++mark);
2151 const char * const mbuf = SvPV_const(mstr, len);
2152 const I32 msize = len - sizeof(long);
2154 PERL_ARGS_ASSERT_DO_MSGSND;
2155 PERL_UNUSED_ARG(sp);
2158 Perl_croak(aTHX_ "Arg too short for msgsnd");
2160 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
2162 PERL_UNUSED_ARG(sp);
2163 PERL_UNUSED_ARG(mark);
2164 Perl_croak(aTHX_ "msgsnd not implemented");
2169 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
2175 I32 msize, flags, ret;
2176 const I32 id = SvIVx(*++mark);
2177 SV * const mstr = *++mark;
2179 PERL_ARGS_ASSERT_DO_MSGRCV;
2180 PERL_UNUSED_ARG(sp);
2182 /* suppress warning when reading into undef var --jhi */
2184 sv_setpvs(mstr, "");
2185 msize = SvIVx(*++mark);
2186 mtype = (long)SvIVx(*++mark);
2187 flags = SvIVx(*++mark);
2188 SvPV_force_nolen(mstr);
2189 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
2192 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
2194 SvCUR_set(mstr, sizeof(long)+ret);
2195 *SvEND(mstr) = '\0';
2196 #ifndef INCOMPLETE_TAINTS
2197 /* who knows who has been playing with this message? */
2203 PERL_UNUSED_ARG(sp);
2204 PERL_UNUSED_ARG(mark);
2205 Perl_croak(aTHX_ "msgrcv not implemented");
2210 Perl_do_semop(pTHX_ SV **mark, SV **sp)
2215 const I32 id = SvIVx(*++mark);
2216 SV * const opstr = *++mark;
2217 const char * const opbuf = SvPV_const(opstr, opsize);
2219 PERL_ARGS_ASSERT_DO_SEMOP;
2220 PERL_UNUSED_ARG(sp);
2222 if (opsize < 3 * SHORTSIZE
2223 || (opsize % (3 * SHORTSIZE))) {
2224 SETERRNO(EINVAL,LIB_INVARG);
2228 /* We can't assume that sizeof(struct sembuf) == 3 * sizeof(short). */
2230 const int nsops = opsize / (3 * sizeof (short));
2232 short * const ops = (short *) opbuf;
2234 struct sembuf *temps, *t;
2237 Newx (temps, nsops, struct sembuf);
2245 result = semop(id, temps, nsops);
2259 Perl_croak(aTHX_ "semop not implemented");
2264 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
2269 struct shmid_ds shmds;
2270 const I32 id = SvIVx(*++mark);
2271 SV * const mstr = *++mark;
2272 const I32 mpos = SvIVx(*++mark);
2273 const I32 msize = SvIVx(*++mark);
2275 PERL_ARGS_ASSERT_DO_SHMIO;
2276 PERL_UNUSED_ARG(sp);
2279 if (shmctl(id, IPC_STAT, &shmds) == -1)
2281 if (mpos < 0 || msize < 0
2282 || (size_t)mpos + msize > (size_t)shmds.shm_segsz) {
2283 SETERRNO(EFAULT,SS_ACCVIO); /* can't do as caller requested */
2286 shm = (char *)shmat(id, NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
2287 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2289 if (optype == OP_SHMREAD) {
2291 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2293 sv_setpvs(mstr, "");
2294 SvPV_force_nolen(mstr);
2295 mbuf = SvGROW(mstr, (STRLEN)msize+1);
2297 Copy(shm + mpos, mbuf, msize, char);
2298 SvCUR_set(mstr, msize);
2299 *SvEND(mstr) = '\0';
2301 #ifndef INCOMPLETE_TAINTS
2302 /* who knows who has been playing with this shared memory? */
2309 const char *mbuf = SvPV_const(mstr, len);
2310 const I32 n = ((I32)len > msize) ? msize : (I32)len;
2311 Copy(mbuf, shm + mpos, n, char);
2313 memzero(shm + mpos + n, msize - n);
2317 Perl_croak(aTHX_ "shm I/O not implemented");
2321 #endif /* SYSV IPC */
2326 =for apidoc start_glob
2328 Function called by C<do_readline> to spawn a glob (or do the glob inside
2329 perl on VMS). This code used to be inline, but now perl uses C<File::Glob>
2330 this glob starter is only used by miniperl during the build process.
2331 Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up.
2337 Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
2340 SV * const tmpcmd = newSV(0);
2343 PERL_ARGS_ASSERT_START_GLOB;
2347 #ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
2348 /* since spawning off a process is a real performance hit */
2355 fp = Perl_vms_start_glob(aTHX_ tmpglob, io);
2358 #ifdef MACOS_TRADITIONAL
2359 sv_setpv(tmpcmd, "glob ");
2360 sv_catsv(tmpcmd, tmpglob);
2361 sv_catpv(tmpcmd, " |");
2365 sv_setpv(tmpcmd, "for a in ");
2366 sv_catsv(tmpcmd, tmpglob);
2367 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
2370 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
2371 sv_catsv(tmpcmd, tmpglob);
2373 sv_setpv(tmpcmd, "perlglob ");
2374 sv_catsv(tmpcmd, tmpglob);
2375 sv_catpv(tmpcmd, " |");
2380 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
2381 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
2382 sv_catsv(tmpcmd, tmpglob);
2383 sv_catpv(tmpcmd, "' 2>/dev/null |");
2385 sv_setpv(tmpcmd, "echo ");
2386 sv_catsv(tmpcmd, tmpglob);
2388 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
2390 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
2393 #endif /* !DOSISH */
2394 #endif /* MACOS_TRADITIONAL */
2395 (void)do_open(PL_last_in_gv, (char*)SvPVX_const(tmpcmd), SvCUR(tmpcmd),
2396 FALSE, O_RDONLY, 0, NULL);
2405 * c-indentation-style: bsd
2407 * indent-tabs-mode: t
2410 * ex: set ts=8 sts=4 sw=4 noet: