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) || SvGMAGICAL(*svp)) ?
218 savesvpv (*svp) : savepvs ("");
226 if ((*type == IoTYPE_RDWR) && /* scary */
227 (*(type+1) == IoTYPE_RDONLY || *(type+1) == IoTYPE_WRONLY) &&
228 ((!num_svs || (tend > type+1 && tend[-1] != IoTYPE_PIPE)))) {
229 TAINT_PROPER("open");
234 if (*type == IoTYPE_PIPE) {
236 if (type[1] != IoTYPE_STD) {
238 Perl_croak(aTHX_ "Unknown open() mode '%.*s'", (int)olen, oname);
244 } while (isSPACE(*type));
250 /* command is missing 19990114 */
251 if (ckWARN(WARN_PIPE))
252 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open");
256 if (!(*name == '-' && name[1] == '\0') || num_svs)
258 TAINT_PROPER("piped open");
259 if (!num_svs && name[len-1] == '|') {
261 if (ckWARN(WARN_PIPE))
262 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Can't open bidirectional pipe");
271 fp = PerlProc_popen_list(mode, num_svs, svp);
274 fp = PerlProc_popen(name,mode);
278 if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) {
284 else if (*type == IoTYPE_WRONLY) {
285 TAINT_PROPER("open");
287 if (*type == IoTYPE_WRONLY) {
288 /* Two IoTYPE_WRONLYs in a row make for an IoTYPE_APPEND. */
289 mode[0] = IoTYPE(io) = IoTYPE_APPEND;
303 dodup = PERLIO_DUP_FD;
309 if (!num_svs && !*type && supplied_fp) {
310 /* "<+&" etc. is used by typemaps */
314 PerlIO *that_fp = NULL;
316 /* diag_listed_as: More than one argument to '%s' open */
317 Perl_croak(aTHX_ "More than one argument to '%c&' open",IoTYPE(io));
319 while (isSPACE(*type))
321 if (num_svs && (SvIOK(*svp) || (SvPOK(*svp) && looks_like_number(*svp)))) {
325 else if (isDIGIT(*type)) {
331 thatio = sv_2io(*svp);
334 GV * const thatgv = gv_fetchpvn_flags(type, tend - type,
336 thatio = GvIO(thatgv);
340 SETERRNO(EINVAL,SS_IVCHAN);
344 if ((that_fp = IoIFP(thatio))) {
345 /* Flush stdio buffer before dup. --mjd
346 * Unfortunately SEEK_CURing 0 seems to
347 * be optimized away on most platforms;
348 * only Solaris and Linux seem to flush
351 /* sfio fails to clear error on next
352 sfwrite, contrary to documentation.
354 if (PerlIO_seek(that_fp, 0, SEEK_CUR) == -1)
355 PerlIO_clearerr(that_fp);
357 /* On the other hand, do all platforms
358 * take gracefully to flushing a read-only
359 * filehandle? Perhaps we should do
360 * fsetpos(src)+fgetpos(dst)? --nik */
361 PerlIO_flush(that_fp);
362 fd = PerlIO_fileno(that_fp);
363 /* When dup()ing STDIN, STDOUT or STDERR
364 * explicitly set appropriate access mode */
365 if (that_fp == PerlIO_stdout()
366 || that_fp == PerlIO_stderr())
367 IoTYPE(io) = IoTYPE_WRONLY;
368 else if (that_fp == PerlIO_stdin())
369 IoTYPE(io) = IoTYPE_RDONLY;
370 /* When dup()ing a socket, say result is
372 else if (IoTYPE(thatio) == IoTYPE_SOCKET)
373 IoTYPE(io) = IoTYPE_SOCKET;
381 fp = PerlIO_fdupopen(aTHX_ that_fp, NULL, dodup);
385 fd = PerlLIO_dup(fd);
388 if (!(fp = PerlIO_openn(aTHX_ type,mode,fd,0,0,NULL,num_svs,svp))) {
389 if (dodup && fd >= 0)
396 while (isSPACE(*type))
398 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
400 fp = PerlIO_stdout();
401 IoTYPE(io) = IoTYPE_STD;
403 /* diag_listed_as: More than one argument to '%s' open */
404 Perl_croak(aTHX_ "More than one argument to '>%c' open",IoTYPE_STD);
409 namesv = newSVpvn_flags(type, tend - type, SVs_TEMP);
414 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
417 if (!fp && type && *type && *type != ':' && !isIDFIRST(*type))
418 goto unknown_open_mode;
419 } /* IoTYPE_WRONLY */
420 else if (*type == IoTYPE_RDONLY) {
423 } while (isSPACE(*type));
432 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
435 IoTYPE(io) = IoTYPE_STD;
437 /* diag_listed_as: More than one argument to '%s' open */
438 Perl_croak(aTHX_ "More than one argument to '<%c' open",IoTYPE_STD);
443 namesv = newSVpvn_flags(type, tend - type, SVs_TEMP);
448 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
450 if (!fp && type && *type && *type != ':' && !isIDFIRST(*type))
451 goto unknown_open_mode;
452 } /* IoTYPE_RDONLY */
453 else if ((num_svs && /* '-|...' or '...|' */
454 type[0] == IoTYPE_STD && type[1] == IoTYPE_PIPE) ||
455 (!num_svs && tend > type+1 && tend[-1] == IoTYPE_PIPE)) {
457 type += 2; /* skip over '-|' */
461 while (tend > type && isSPACE(tend[-1]))
463 for (; isSPACE(*type); type++)
469 /* command is missing 19990114 */
470 if (ckWARN(WARN_PIPE))
471 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open");
475 if (!(*name == '-' && name[1] == '\0') || num_svs)
477 TAINT_PROPER("piped open");
486 fp = PerlProc_popen_list(mode,num_svs,svp);
489 fp = PerlProc_popen(name,mode);
491 IoTYPE(io) = IoTYPE_PIPE;
493 while (isSPACE(*type))
496 if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) {
502 else { /* layer(Args) */
504 goto unknown_open_mode;
506 IoTYPE(io) = IoTYPE_RDONLY;
507 for (; isSPACE(*name); name++)
516 if (*name == '-' && name[1] == '\0') {
518 IoTYPE(io) = IoTYPE_STD;
522 namesv = newSVpvn_flags(type, tend - type, SVs_TEMP);
527 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
532 if (IoTYPE(io) == IoTYPE_RDONLY && ckWARN(WARN_NEWLINE)
533 && strchr(oname, '\n')
536 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
540 if (ckWARN(WARN_IO)) {
541 if ((IoTYPE(io) == IoTYPE_RDONLY) &&
542 (fp == PerlIO_stdout() || fp == PerlIO_stderr())) {
543 Perl_warner(aTHX_ packWARN(WARN_IO),
544 "Filehandle STD%s reopened as %s only for input",
545 ((fp == PerlIO_stdout()) ? "OUT" : "ERR"),
548 else if ((IoTYPE(io) == IoTYPE_WRONLY) && fp == PerlIO_stdin()) {
549 Perl_warner(aTHX_ packWARN(WARN_IO),
550 "Filehandle STDIN reopened as %s only for output",
555 fd = PerlIO_fileno(fp);
556 /* If there is no fd (e.g. PerlIO::scalar) assume it isn't a
557 * socket - this covers PerlIO::scalar - otherwise unless we "know" the
558 * type probe for socket-ness.
560 if (IoTYPE(io) && IoTYPE(io) != IoTYPE_PIPE && IoTYPE(io) != IoTYPE_STD && fd >= 0) {
561 if (PerlLIO_fstat(fd,&PL_statbuf) < 0) {
562 /* If PerlIO claims to have fd we had better be able to fstat() it. */
563 (void) PerlIO_close(fp);
567 if (S_ISSOCK(PL_statbuf.st_mode))
568 IoTYPE(io) = IoTYPE_SOCKET; /* in case a socket was passed in to us */
572 !(PL_statbuf.st_mode & S_IFMT)
576 && IoTYPE(io) != IoTYPE_WRONLY /* Dups of STD* filehandles already have */
577 && IoTYPE(io) != IoTYPE_RDONLY /* type so they aren't marked as sockets */
578 ) { /* on OS's that return 0 on fstat()ed pipe */
580 Sock_size_t buflen = sizeof tmpbuf;
581 if (PerlSock_getsockname(fd, (struct sockaddr *)tmpbuf, &buflen) >= 0
582 || errno != ENOTSOCK)
583 IoTYPE(io) = IoTYPE_SOCKET; /* some OS's return 0 on fstat()ed socket */
584 /* but some return 0 for streams too, sigh */
586 #endif /* HAS_SOCKET */
587 #endif /* !PERL_MICRO */
591 * If this is a standard handle we discard all the layer stuff
592 * and just dup the fd into whatever was on the handle before !
595 if (saveifp) { /* must use old fp? */
596 /* If fd is less that PL_maxsysfd i.e. STDIN..STDERR
597 then dup the new fileno down
600 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
601 if (saveofp != saveifp) { /* was a socket? */
602 PerlIO_close(saveofp);
606 /* Still a small can-of-worms here if (say) PerlIO::scalar
607 is assigned to (say) STDOUT - for now let dup2() fail
608 and provide the error
610 if (PerlLIO_dup2(fd, savefd) < 0) {
611 (void)PerlIO_close(fp);
615 if (savefd != PerlIO_fileno(PerlIO_stdin())) {
616 char newname[FILENAME_MAX+1];
617 if (PerlIO_getname(fp, newname)) {
618 if (fd == PerlIO_fileno(PerlIO_stdout()))
619 Perl_vmssetuserlnm(aTHX_ "SYS$OUTPUT", newname);
620 if (fd == PerlIO_fileno(PerlIO_stderr()))
621 Perl_vmssetuserlnm(aTHX_ "SYS$ERROR", newname);
627 /* PL_fdpid isn't used on Windows, so avoid this useless work.
628 * XXX Probably the same for a lot of other places. */
633 sv = *av_fetch(PL_fdpid,fd,TRUE);
634 SvUPGRADE(sv, SVt_IV);
637 sv = *av_fetch(PL_fdpid,savefd,TRUE);
638 SvUPGRADE(sv, SVt_IV);
644 /* need to close fp without closing underlying fd */
645 int ofd = PerlIO_fileno(fp);
646 int dupfd = PerlLIO_dup(ofd);
647 #if defined(HAS_FCNTL) && defined(F_SETFD)
648 /* Assume if we have F_SETFD we have F_GETFD */
649 int coe = fcntl(ofd,F_GETFD);
652 PerlLIO_dup2(dupfd,ofd);
653 #if defined(HAS_FCNTL) && defined(F_SETFD)
654 /* The dup trick has lost close-on-exec on ofd */
655 fcntl(ofd,F_SETFD, coe);
657 PerlLIO_close(dupfd);
664 fd = PerlIO_fileno(fp);
666 #if defined(HAS_FCNTL) && defined(F_SETFD)
669 fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
675 IoFLAGS(io) &= ~IOf_NOLINE;
677 if (IoTYPE(io) == IoTYPE_SOCKET
678 || (IoTYPE(io) == IoTYPE_WRONLY && fd >= 0 && S_ISCHR(PL_statbuf.st_mode)) ) {
680 if (*s == IoTYPE_IMPLICIT || *s == IoTYPE_NUMERIC)
683 if (!(IoOFP(io) = PerlIO_openn(aTHX_ type,s,fd,0,0,NULL,0,svp))) {
697 IoTYPE(io) = savetype;
702 Perl_nextargv(pTHX_ register GV *gv)
706 #ifndef FLEXFILENAMES
712 IO * const io = GvIOp(gv);
714 PERL_ARGS_ASSERT_NEXTARGV;
717 PL_argvoutgv = gv_fetchpvs("ARGVOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
718 if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
719 IoFLAGS(io) &= ~IOf_START;
722 Perl_av_create_and_push(aTHX_ &PL_argvout_stack,
723 SvREFCNT_inc_simple_NN(PL_defoutgv));
726 if (PL_filemode & (S_ISUID|S_ISGID)) {
727 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
730 (void)fchmod(PL_lastfd,PL_filemode);
732 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
739 while (av_len(GvAV(gv)) >= 0) {
741 sv = av_shift(GvAV(gv));
743 sv_setsv(GvSVn(gv),sv);
744 SvSETMAGIC(GvSV(gv));
745 PL_oldname = SvPVx(GvSV(gv), oldlen);
746 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,NULL)) {
748 TAINT_PROPER("inplace open");
749 if (oldlen == 1 && *PL_oldname == '-') {
750 setdefout(gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL,
752 return IoIFP(GvIOp(gv));
754 #ifndef FLEXFILENAMES
755 filedev = PL_statbuf.st_dev;
756 fileino = PL_statbuf.st_ino;
758 PL_filemode = PL_statbuf.st_mode;
759 fileuid = PL_statbuf.st_uid;
760 filegid = PL_statbuf.st_gid;
761 if (!S_ISREG(PL_filemode)) {
762 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
763 "Can't do inplace edit: %s is not a regular file",
768 if (*PL_inplace && strNE(PL_inplace, "*")) {
769 const char *star = strchr(PL_inplace, '*');
771 const char *begin = PL_inplace;
774 sv_catpvn(sv, begin, star - begin);
775 sv_catpvn(sv, PL_oldname, oldlen);
777 } while ((star = strchr(begin, '*')));
782 sv_catpv(sv,PL_inplace);
784 #ifndef FLEXFILENAMES
785 if ((PerlLIO_stat(SvPVX_const(sv),&PL_statbuf) >= 0
786 && PL_statbuf.st_dev == filedev
787 && PL_statbuf.st_ino == fileino)
789 || ((_djstat_fail_bits & _STFAIL_TRUENAME)!=0)
793 Perl_ck_warner_d(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 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
804 "Can't rename %s to %"SVf": %s, skipping file",
805 PL_oldname, SVfARG(sv), Strerror(errno));
811 (void)PerlLIO_unlink(SvPVX_const(sv));
812 (void)PerlLIO_rename(PL_oldname,SvPVX_const(sv));
813 do_open(gv,(char*)SvPVX_const(sv),SvCUR(sv),TRUE,O_RDONLY,0,NULL);
816 (void)UNLINK(SvPVX_const(sv));
817 if (link(PL_oldname,SvPVX_const(sv)) < 0) {
818 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
819 "Can't rename %s to %"SVf": %s, skipping file",
820 PL_oldname, SVfARG(sv), Strerror(errno) );
824 (void)UNLINK(PL_oldname);
828 #if !defined(DOSISH) && !defined(AMIGAOS)
829 # ifndef VMS /* Don't delete; use automatic file versioning */
830 if (UNLINK(PL_oldname) < 0) {
831 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
832 "Can't remove %s: %s, skipping file",
833 PL_oldname, Strerror(errno) );
839 Perl_croak(aTHX_ "Can't do inplace edit without backup");
843 sv_setpvn(sv,PL_oldname,oldlen);
844 SETERRNO(0,0); /* in case sprintf set errno */
845 if (!Perl_do_openn(aTHX_ PL_argvoutgv, (char*)SvPVX_const(sv),
848 O_WRONLY|O_CREAT|O_TRUNC,0,
850 O_WRONLY|O_CREAT|OPEN_EXCL,0600,
853 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE), "Can't do inplace edit on %s: %s",
854 PL_oldname, Strerror(errno) );
858 setdefout(PL_argvoutgv);
859 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
860 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
862 (void)fchmod(PL_lastfd,PL_filemode);
864 # if !(defined(WIN32) && defined(__BORLANDC__))
865 /* Borland runtime creates a readonly file! */
866 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
869 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
871 (void)fchown(PL_lastfd,fileuid,filegid);
874 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
879 return IoIFP(GvIOp(gv));
882 if (ckWARN_d(WARN_INPLACE)) {
883 const int eno = errno;
884 if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0
885 && !S_ISREG(PL_statbuf.st_mode))
887 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
888 "Can't do inplace edit: %s is not a regular file",
892 Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't open %s: %s",
893 PL_oldname, Strerror(eno));
897 if (io && (IoFLAGS(io) & IOf_ARGV))
898 IoFLAGS(io) |= IOf_START;
900 (void)do_close(PL_argvoutgv,FALSE);
901 if (io && (IoFLAGS(io) & IOf_ARGV)
902 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
904 GV * const oldout = MUTABLE_GV(av_pop(PL_argvout_stack));
906 SvREFCNT_dec(oldout);
909 setdefout(gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO));
914 /* explicit renamed to avoid C++ conflict -- kja */
916 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
924 if (!gv || !isGV_with_GP(gv)) {
926 SETERRNO(EBADF,SS_IVCHAN);
930 if (!io) { /* never opened */
932 if (ckWARN(WARN_UNOPENED)) /* no check for closed here */
933 report_evil_fh(gv, io, PL_op->op_type);
934 SETERRNO(EBADF,SS_IVCHAN);
938 retval = io_close(io, not_implicit);
942 IoLINES_LEFT(io) = IoPAGE_LEN(io);
944 IoTYPE(io) = IoTYPE_CLOSED;
949 Perl_io_close(pTHX_ IO *io, bool not_implicit)
954 PERL_ARGS_ASSERT_IO_CLOSE;
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);
995 PERL_ARGS_ASSERT_DO_EOF;
999 else if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
1000 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1003 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
1004 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
1005 return FALSE; /* this is the most usual case */
1009 /* getc and ungetc can stomp on errno */
1011 const int ch = PerlIO_getc(IoIFP(io));
1013 (void)PerlIO_ungetc(IoIFP(io),ch);
1020 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
1021 if (PerlIO_get_cnt(IoIFP(io)) < -1)
1022 PerlIO_set_cnt(IoIFP(io),-1);
1024 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
1025 if (gv != PL_argvgv || !nextargv(gv)) /* get another fp handy */
1029 return TRUE; /* normal fp, definitely end of file */
1035 Perl_do_tell(pTHX_ GV *gv)
1038 register IO *io = NULL;
1039 register PerlIO *fp;
1041 PERL_ARGS_ASSERT_DO_TELL;
1043 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1044 #ifdef ULTRIX_STDIO_BOTCH
1046 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1048 return PerlIO_tell(fp);
1050 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1051 report_evil_fh(gv, io, PL_op->op_type);
1052 SETERRNO(EBADF,RMS_IFI);
1057 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
1060 register IO *io = NULL;
1061 register PerlIO *fp;
1063 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1064 #ifdef ULTRIX_STDIO_BOTCH
1066 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1068 return PerlIO_seek(fp, pos, whence) >= 0;
1070 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1071 report_evil_fh(gv, io, PL_op->op_type);
1072 SETERRNO(EBADF,RMS_IFI);
1077 Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
1080 register IO *io = NULL;
1081 register PerlIO *fp;
1083 PERL_ARGS_ASSERT_DO_SYSSEEK;
1085 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
1086 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
1087 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1088 report_evil_fh(gv, io, PL_op->op_type);
1089 SETERRNO(EBADF,RMS_IFI);
1094 Perl_mode_from_discipline(pTHX_ const char *s, STRLEN len)
1096 int mode = O_BINARY;
1102 if (s[2] == 'a' && s[3] == 'w'
1103 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1112 if (s[2] == 'r' && s[3] == 'l' && s[4] == 'f'
1113 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1122 goto fail_discipline;
1125 else if (isSPACE(*s)) {
1132 end = strchr(s+1, ':');
1135 #ifndef PERLIO_LAYERS
1136 Perl_croak(aTHX_ "IO layers (like '%.*s') unavailable", end-s, s);
1147 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE)
1149 my_chsize(int fd, Off_t length)
1152 /* code courtesy of William Kucharski */
1157 if (PerlLIO_fstat(fd, &filebuf) < 0)
1160 if (filebuf.st_size < length) {
1162 /* extend file length */
1164 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
1167 /* write a "0" byte */
1169 if ((PerlLIO_write(fd, "", 1)) != 1)
1173 /* truncate length */
1177 fl.l_start = length;
1178 fl.l_type = F_WRLCK; /* write lock on file space */
1181 * This relies on the UNDOCUMENTED F_FREESP argument to
1182 * fcntl(2), which truncates the file so that it ends at the
1183 * position indicated by fl.l_start.
1185 * Will minor miracles never cease?
1188 if (fcntl(fd, F_FREESP, &fl) < 0)
1194 Perl_croak_nocontext("truncate not implemented");
1195 #endif /* F_FREESP */
1198 #endif /* !HAS_TRUNCATE && !HAS_CHSIZE */
1201 Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
1205 PERL_ARGS_ASSERT_DO_PRINT;
1207 /* assuming fp is checked earlier */
1210 if (SvTYPE(sv) == SVt_IV && SvIOK(sv)) {
1211 assert(!SvGMAGICAL(sv));
1213 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
1215 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
1216 return !PerlIO_error(fp);
1220 /* Do this first to trigger any overloading. */
1221 const char *tmps = SvPV_const(sv, len);
1225 if (PerlIO_isutf8(fp)) {
1227 /* We don't modify the original scalar. */
1228 tmpbuf = bytes_to_utf8((const U8*) tmps, &len);
1229 tmps = (char *) tmpbuf;
1232 else if (DO_UTF8(sv)) {
1233 STRLEN tmplen = len;
1235 U8 * const result = bytes_from_utf8((const U8*) tmps, &tmplen, &utf8);
1238 tmps = (char *) tmpbuf;
1242 assert((char *)result == tmps);
1243 Perl_ck_warner_d(aTHX_ packWARN(WARN_UTF8),
1244 "Wide character in print");
1247 /* To detect whether the process is about to overstep its
1248 * filesize limit we would need getrlimit(). We could then
1249 * also transparently raise the limit with setrlimit() --
1250 * but only until the system hard limit/the filesystem limit,
1251 * at which we would get EPERM. Note that when using buffered
1252 * io the write failure can be delayed until the flush/close. --jhi */
1253 if (len && (PerlIO_write(fp,tmps,len) == 0))
1256 return happy ? !PerlIO_error(fp) : FALSE;
1268 if (PL_op->op_flags & OPf_REF) {
1273 return PL_laststatval;
1276 PL_laststype = OP_STAT;
1278 sv_setpvs(PL_statname, "");
1281 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
1282 } else if (IoDIRP(io)) {
1283 return (PL_laststatval = PerlLIO_fstat(my_dirfd(IoDIRP(io)), &PL_statcache));
1285 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1286 report_evil_fh(gv, io, PL_op->op_type);
1287 return (PL_laststatval = -1);
1290 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1291 report_evil_fh(gv, io, PL_op->op_type);
1292 return (PL_laststatval = -1);
1295 else if (PL_op->op_private & OPpFT_STACKED) {
1296 return PL_laststatval;
1299 SV* const sv = POPs;
1303 if (isGV_with_GP(sv)) {
1304 gv = MUTABLE_GV(sv);
1307 else if (SvROK(sv) && isGV_with_GP(SvRV(sv))) {
1308 gv = MUTABLE_GV(SvRV(sv));
1311 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
1312 io = MUTABLE_IO(SvRV(sv));
1314 goto do_fstat_have_io;
1317 s = SvPV_const(sv, len);
1319 sv_setpvn(PL_statname, s, len);
1320 s = SvPVX_const(PL_statname); /* s now NUL-terminated */
1321 PL_laststype = OP_STAT;
1322 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1323 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
1324 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
1325 return PL_laststatval;
1334 static const char no_prev_lstat[] = "The stat preceding -l _ wasn't an lstat";
1338 if (PL_op->op_flags & OPf_REF) {
1340 if (cGVOP_gv == PL_defgv) {
1341 if (PL_laststype != OP_LSTAT)
1342 Perl_croak(aTHX_ no_prev_lstat);
1343 return PL_laststatval;
1345 if (ckWARN(WARN_IO)) {
1346 Perl_warner(aTHX_ packWARN(WARN_IO), "Use of -l on filehandle %s",
1348 return (PL_laststatval = -1);
1351 else if (PL_laststype != OP_LSTAT
1352 && (PL_op->op_private & OPpFT_STACKED) && ckWARN(WARN_IO))
1353 Perl_croak(aTHX_ no_prev_lstat);
1355 PL_laststype = OP_LSTAT;
1359 if (SvROK(sv) && isGV_with_GP(SvRV(sv)) && ckWARN(WARN_IO)) {
1360 Perl_warner(aTHX_ packWARN(WARN_IO), "Use of -l on filehandle %s",
1361 GvENAME((const GV *)SvRV(sv)));
1362 return (PL_laststatval = -1);
1364 file = SvPV_nolen_const(sv);
1365 sv_setpv(PL_statname,file);
1366 PL_laststatval = PerlLIO_lstat(file,&PL_statcache);
1367 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(file, '\n'))
1368 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "lstat");
1369 return PL_laststatval;
1373 S_exec_failed(pTHX_ const char *cmd, int fd, int do_report)
1375 const int e = errno;
1376 PERL_ARGS_ASSERT_EXEC_FAILED;
1377 if (ckWARN(WARN_EXEC))
1378 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
1381 PerlLIO_write(fd, (void*)&e, sizeof(int));
1387 Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1388 int fd, int do_report)
1391 PERL_ARGS_ASSERT_DO_AEXEC5;
1392 #if defined(__SYMBIAN32__) || defined(__LIBCATAMOUNT__)
1393 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1397 const char *tmps = NULL;
1398 Newx(PL_Argv, sp - mark + 1, const char*);
1401 while (++mark <= sp) {
1403 *a++ = SvPV_nolen_const(*mark);
1409 tmps = SvPV_nolen_const(really);
1410 if ((!really && *PL_Argv[0] != '/') ||
1411 (really && *tmps != '/')) /* will execvp use PATH? */
1412 TAINT_ENV(); /* testing IFS here is overkill, probably */
1414 if (really && *tmps)
1415 PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
1417 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1419 S_exec_failed(aTHX_ (really ? tmps : PL_Argv[0]), fd, do_report);
1427 Perl_do_execfree(pTHX)
1436 #ifdef PERL_DEFAULT_DO_EXEC3_IMPLEMENTATION
1439 Perl_do_exec3(pTHX_ const char *incmd, int fd, int do_report)
1442 register const char **a;
1446 /* Make a copy so we can change it */
1447 const Size_t cmdlen = strlen(incmd) + 1;
1449 PERL_ARGS_ASSERT_DO_EXEC3;
1451 Newx(buf, cmdlen, char);
1453 memcpy(cmd, incmd, cmdlen);
1455 while (*cmd && isSPACE(*cmd))
1458 /* save an extra exec if possible */
1462 char flags[PERL_FLAGS_MAX];
1463 if (strnEQ(cmd,PL_cshname,PL_cshlen) &&
1464 strnEQ(cmd+PL_cshlen," -c",3)) {
1465 my_strlcpy(flags, "-c", PERL_FLAGS_MAX);
1466 s = cmd+PL_cshlen+3;
1469 my_strlcat(flags, "f", PERL_FLAGS_MAX - 2);
1474 char * const ncmd = s;
1480 if (s[-1] == '\'') {
1483 PerlProc_execl(PL_cshname, "csh", flags, ncmd, (char*)NULL);
1486 S_exec_failed(aTHX_ PL_cshname, fd, do_report);
1495 /* see if there are shell metacharacters in it */
1497 if (*cmd == '.' && isSPACE(cmd[1]))
1500 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1505 s++; /* catch VAR=val gizmo */
1509 for (s = cmd; *s; s++) {
1510 if (*s != ' ' && !isALPHA(*s) &&
1511 strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1512 if (*s == '\n' && !s[1]) {
1516 /* handle the 2>&1 construct at the end */
1517 if (*s == '>' && s[1] == '&' && s[2] == '1'
1518 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1519 && (!s[3] || isSPACE(s[3])))
1521 const char *t = s + 3;
1523 while (*t && isSPACE(*t))
1525 if (!*t && (PerlLIO_dup2(1,2) != -1)) {
1532 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char *)NULL);
1534 S_exec_failed(aTHX_ PL_sh_path, fd, do_report);
1540 Newx(PL_Argv, (s - cmd) / 2 + 2, const char*);
1541 PL_Cmd = savepvn(cmd, s-cmd);
1543 for (s = PL_Cmd; *s;) {
1548 while (*s && !isSPACE(*s))
1556 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1558 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1562 S_exec_failed(aTHX_ PL_Argv[0], fd, do_report);
1569 #endif /* OS2 || WIN32 */
1572 Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
1576 register I32 tot = 0;
1577 const char *const what = PL_op_name[type];
1579 SV ** const oldmark = mark;
1581 PERL_ARGS_ASSERT_APPLY;
1583 /* Doing this ahead of the switch statement preserves the old behaviour,
1584 where attempting to use kill as a taint test test would fail on
1585 platforms where kill was not defined. */
1587 if (type == OP_KILL)
1588 Perl_die(aTHX_ PL_no_func, what);
1591 if (type == OP_CHOWN)
1592 Perl_die(aTHX_ PL_no_func, what);
1596 #define APPLY_TAINT_PROPER() \
1598 if (PL_tainted) { TAINT_PROPER(what); } \
1601 /* This is a first heuristic; it doesn't catch tainting magic. */
1603 while (++mark <= sp) {
1604 if (SvTAINTED(*mark)) {
1613 APPLY_TAINT_PROPER();
1616 APPLY_TAINT_PROPER();
1618 while (++mark <= sp) {
1620 if (isGV_with_GP(*mark)) {
1621 gv = MUTABLE_GV(*mark);
1623 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1625 APPLY_TAINT_PROPER();
1626 if (fchmod(PerlIO_fileno(IoIFP(GvIOn(gv))), val))
1629 Perl_die(aTHX_ PL_no_func, "fchmod");
1636 else if (SvROK(*mark) && isGV_with_GP(SvRV(*mark))) {
1637 gv = MUTABLE_GV(SvRV(*mark));
1641 const char *name = SvPV_nolen_const(*mark);
1642 APPLY_TAINT_PROPER();
1643 if (PerlLIO_chmod(name, val))
1651 APPLY_TAINT_PROPER();
1652 if (sp - mark > 2) {
1654 val = SvIVx(*++mark);
1655 val2 = SvIVx(*++mark);
1656 APPLY_TAINT_PROPER();
1658 while (++mark <= sp) {
1660 if (isGV_with_GP(*mark)) {
1661 gv = MUTABLE_GV(*mark);
1663 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1665 APPLY_TAINT_PROPER();
1666 if (fchown(PerlIO_fileno(IoIFP(GvIOn(gv))), val, val2))
1669 Perl_die(aTHX_ PL_no_func, "fchown");
1676 else if (SvROK(*mark) && isGV_with_GP(SvRV(*mark))) {
1677 gv = MUTABLE_GV(SvRV(*mark));
1681 const char *name = SvPV_nolen_const(*mark);
1682 APPLY_TAINT_PROPER();
1683 if (PerlLIO_chown(name, val, val2))
1691 XXX Should we make lchown() directly available from perl?
1692 For now, we'll let Configure test for HAS_LCHOWN, but do
1693 nothing in the core.
1698 APPLY_TAINT_PROPER();
1701 s = SvPVx_nolen_const(*++mark);
1703 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1705 if ((val = whichsig(s)) < 0)
1706 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
1710 APPLY_TAINT_PROPER();
1713 /* kill() doesn't do process groups (job trees?) under VMS */
1714 if (val < 0) val = -val;
1715 if (val == SIGKILL) {
1716 # include <starlet.h>
1717 /* Use native sys$delprc() to insure that target process is
1718 * deleted; supervisor-mode images don't pay attention to
1719 * CRTL's emulation of Unix-style signals and kill()
1721 while (++mark <= sp) {
1723 register unsigned long int __vmssts;
1724 if (!(SvIOK(*mark) || SvNOK(*mark) || looks_like_number(*mark)))
1725 Perl_croak(aTHX_ "Can't kill a non-numeric process ID");
1727 APPLY_TAINT_PROPER();
1728 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1732 case SS$_NOSUCHNODE:
1733 SETERRNO(ESRCH,__vmssts);
1736 SETERRNO(EPERM,__vmssts);
1739 SETERRNO(EVMSERR,__vmssts);
1748 while (++mark <= sp) {
1750 if (!(SvIOK(*mark) || SvNOK(*mark) || looks_like_number(*mark)))
1751 Perl_croak(aTHX_ "Can't kill a non-numeric process ID");
1753 APPLY_TAINT_PROPER();
1755 if (PerlProc_killpg(proc,val)) /* BSD */
1757 if (PerlProc_kill(-proc,val)) /* SYSV */
1763 while (++mark <= sp) {
1765 if (!(SvIOK(*mark) || SvNOK(*mark) || looks_like_number(*mark)))
1766 Perl_croak(aTHX_ "Can't kill a non-numeric process ID");
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 */
1923 if (ingroup(544,effective)) { /* member of Administrators */
1925 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
1927 if (mode == S_IXUSR) {
1928 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1932 return TRUE; /* root reads and writes anything */
1935 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1936 if (statbufp->st_mode & mode)
1937 return TRUE; /* ok as "user" */
1939 else if (ingroup(statbufp->st_gid,effective)) {
1940 if (statbufp->st_mode & mode >> 3)
1941 return TRUE; /* ok as "group" */
1943 else if (statbufp->st_mode & mode >> 6)
1944 return TRUE; /* ok as "other" */
1946 #endif /* ! DOSISH */
1951 S_ingroup(pTHX_ Gid_t testgid, bool effective)
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) {
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 SV *nsv = optype == OP_MSGGET ? NULL : *++mark;
1987 const I32 flags = SvIVx(*++mark);
1989 PERL_ARGS_ASSERT_DO_IPCGET;
1990 PERL_UNUSED_ARG(sp);
1997 return msgget(key, flags);
2001 return semget(key, (int) SvIV(nsv), flags);
2005 return shmget(key, (size_t) SvUV(nsv), flags);
2007 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
2009 /* diag_listed_as: msg%s not implemented */
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 /* diag_listed_as: sem%s not implemented */
2071 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2075 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
2077 /* diag_listed_as: shm%s not implemented */
2078 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2086 SvPV_force_nolen(astr);
2087 a = SvGROW(astr, infosize+1);
2092 a = SvPV(astr, len);
2093 if (len != infosize)
2094 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
2102 const IV i = SvIV(astr);
2103 a = INT2PTR(char *,i); /* ouch */
2110 ret = msgctl(id, cmd, (struct msqid_ds *)a);
2116 union semun unsemds;
2118 #ifdef EXTRA_F_IN_SEMUN_BUF
2119 unsemds.buff = (struct semid_ds *)a;
2121 unsemds.buf = (struct semid_ds *)a;
2123 ret = Semctl(id, n, cmd, unsemds);
2125 /* diag_listed_as: sem%s not implemented */
2126 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2133 ret = shmctl(id, cmd, (struct shmid_ds *)a);
2137 if (getinfo && ret >= 0) {
2138 SvCUR_set(astr, infosize);
2139 *SvEND(astr) = '\0';
2146 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
2151 const I32 id = SvIVx(*++mark);
2152 SV * const mstr = *++mark;
2153 const I32 flags = SvIVx(*++mark);
2154 const char * const mbuf = SvPV_const(mstr, len);
2155 const I32 msize = len - sizeof(long);
2157 PERL_ARGS_ASSERT_DO_MSGSND;
2158 PERL_UNUSED_ARG(sp);
2161 Perl_croak(aTHX_ "Arg too short for msgsnd");
2163 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
2165 PERL_UNUSED_ARG(sp);
2166 PERL_UNUSED_ARG(mark);
2167 /* diag_listed_as: msg%s not implemented */
2168 Perl_croak(aTHX_ "msgsnd not implemented");
2173 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
2179 I32 msize, flags, ret;
2180 const I32 id = SvIVx(*++mark);
2181 SV * const mstr = *++mark;
2183 PERL_ARGS_ASSERT_DO_MSGRCV;
2184 PERL_UNUSED_ARG(sp);
2186 /* suppress warning when reading into undef var --jhi */
2188 sv_setpvs(mstr, "");
2189 msize = SvIVx(*++mark);
2190 mtype = (long)SvIVx(*++mark);
2191 flags = SvIVx(*++mark);
2192 SvPV_force_nolen(mstr);
2193 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
2196 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
2198 SvCUR_set(mstr, sizeof(long)+ret);
2199 *SvEND(mstr) = '\0';
2200 #ifndef INCOMPLETE_TAINTS
2201 /* who knows who has been playing with this message? */
2207 PERL_UNUSED_ARG(sp);
2208 PERL_UNUSED_ARG(mark);
2209 /* diag_listed_as: msg%s not implemented */
2210 Perl_croak(aTHX_ "msgrcv not implemented");
2215 Perl_do_semop(pTHX_ SV **mark, SV **sp)
2220 const I32 id = SvIVx(*++mark);
2221 SV * const opstr = *++mark;
2222 const char * const opbuf = SvPV_const(opstr, opsize);
2224 PERL_ARGS_ASSERT_DO_SEMOP;
2225 PERL_UNUSED_ARG(sp);
2227 if (opsize < 3 * SHORTSIZE
2228 || (opsize % (3 * SHORTSIZE))) {
2229 SETERRNO(EINVAL,LIB_INVARG);
2233 /* We can't assume that sizeof(struct sembuf) == 3 * sizeof(short). */
2235 const int nsops = opsize / (3 * sizeof (short));
2237 short * const ops = (short *) opbuf;
2239 struct sembuf *temps, *t;
2242 Newx (temps, nsops, struct sembuf);
2250 result = semop(id, temps, nsops);
2264 /* diag_listed_as: sem%s not implemented */
2265 Perl_croak(aTHX_ "semop not implemented");
2270 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
2275 struct shmid_ds shmds;
2276 const I32 id = SvIVx(*++mark);
2277 SV * const mstr = *++mark;
2278 const I32 mpos = SvIVx(*++mark);
2279 const I32 msize = SvIVx(*++mark);
2281 PERL_ARGS_ASSERT_DO_SHMIO;
2282 PERL_UNUSED_ARG(sp);
2285 if (shmctl(id, IPC_STAT, &shmds) == -1)
2287 if (mpos < 0 || msize < 0
2288 || (size_t)mpos + msize > (size_t)shmds.shm_segsz) {
2289 SETERRNO(EFAULT,SS_ACCVIO); /* can't do as caller requested */
2292 shm = (char *)shmat(id, NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
2293 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2295 if (optype == OP_SHMREAD) {
2297 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2299 sv_setpvs(mstr, "");
2300 SvPV_force_nolen(mstr);
2301 mbuf = SvGROW(mstr, (STRLEN)msize+1);
2303 Copy(shm + mpos, mbuf, msize, char);
2304 SvCUR_set(mstr, msize);
2305 *SvEND(mstr) = '\0';
2307 #ifndef INCOMPLETE_TAINTS
2308 /* who knows who has been playing with this shared memory? */
2315 const char *mbuf = SvPV_const(mstr, len);
2316 const I32 n = ((I32)len > msize) ? msize : (I32)len;
2317 Copy(mbuf, shm + mpos, n, char);
2319 memzero(shm + mpos + n, msize - n);
2323 /* diag_listed_as: shm%s not implemented */
2324 Perl_croak(aTHX_ "shm I/O not implemented");
2328 #endif /* SYSV IPC */
2333 =for apidoc start_glob
2335 Function called by C<do_readline> to spawn a glob (or do the glob inside
2336 perl on VMS). This code used to be inline, but now perl uses C<File::Glob>
2337 this glob starter is only used by miniperl during the build process.
2338 Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up.
2344 Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
2347 SV * const tmpcmd = newSV(0);
2350 PERL_ARGS_ASSERT_START_GLOB;
2354 #ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
2355 /* since spawning off a process is a real performance hit */
2362 fp = Perl_vms_start_glob(aTHX_ tmpglob, io);
2367 sv_setpv(tmpcmd, "for a in ");
2368 sv_catsv(tmpcmd, tmpglob);
2369 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
2372 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
2373 sv_catsv(tmpcmd, tmpglob);
2375 sv_setpv(tmpcmd, "perlglob ");
2376 sv_catsv(tmpcmd, tmpglob);
2377 sv_catpv(tmpcmd, " |");
2382 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
2383 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
2384 sv_catsv(tmpcmd, tmpglob);
2385 sv_catpv(tmpcmd, "' 2>/dev/null |");
2387 sv_setpv(tmpcmd, "echo ");
2388 sv_catsv(tmpcmd, tmpglob);
2390 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
2392 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
2395 #endif /* !DOSISH */
2396 (void)do_open(PL_last_in_gv, (char*)SvPVX_const(tmpcmd), SvCUR(tmpcmd),
2397 FALSE, O_RDONLY, 0, NULL);
2406 * c-indentation-style: bsd
2408 * indent-tabs-mode: t
2411 * ex: set ts=8 sts=4 sw=4 noet: