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. */
629 sv = *av_fetch(PL_fdpid,fd,TRUE);
630 SvUPGRADE(sv, SVt_IV);
633 sv = *av_fetch(PL_fdpid,savefd,TRUE);
634 SvUPGRADE(sv, SVt_IV);
640 /* need to close fp without closing underlying fd */
641 int ofd = PerlIO_fileno(fp);
642 int dupfd = PerlLIO_dup(ofd);
643 #if defined(HAS_FCNTL) && defined(F_SETFD)
644 /* Assume if we have F_SETFD we have F_GETFD */
645 int coe = fcntl(ofd,F_GETFD);
648 PerlLIO_dup2(dupfd,ofd);
649 #if defined(HAS_FCNTL) && defined(F_SETFD)
650 /* The dup trick has lost close-on-exec on ofd */
651 fcntl(ofd,F_SETFD, coe);
653 PerlLIO_close(dupfd);
660 fd = PerlIO_fileno(fp);
662 #if defined(HAS_FCNTL) && defined(F_SETFD)
665 fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
671 IoFLAGS(io) &= ~IOf_NOLINE;
673 if (IoTYPE(io) == IoTYPE_SOCKET
674 || (IoTYPE(io) == IoTYPE_WRONLY && fd >= 0 && S_ISCHR(PL_statbuf.st_mode)) ) {
676 if (*s == IoTYPE_IMPLICIT || *s == IoTYPE_NUMERIC)
679 if (!(IoOFP(io) = PerlIO_openn(aTHX_ type,s,fd,0,0,NULL,0,svp))) {
693 IoTYPE(io) = savetype;
698 Perl_nextargv(pTHX_ register GV *gv)
702 #ifndef FLEXFILENAMES
708 IO * const io = GvIOp(gv);
710 PERL_ARGS_ASSERT_NEXTARGV;
713 PL_argvoutgv = gv_fetchpvs("ARGVOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
714 if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
715 IoFLAGS(io) &= ~IOf_START;
718 Perl_av_create_and_push(aTHX_ &PL_argvout_stack,
719 SvREFCNT_inc_simple_NN(PL_defoutgv));
722 if (PL_filemode & (S_ISUID|S_ISGID)) {
723 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
726 (void)fchmod(PL_lastfd,PL_filemode);
728 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
735 while (av_len(GvAV(gv)) >= 0) {
737 sv = av_shift(GvAV(gv));
739 sv_setsv(GvSVn(gv),sv);
740 SvSETMAGIC(GvSV(gv));
741 PL_oldname = SvPVx(GvSV(gv), oldlen);
742 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,NULL)) {
744 TAINT_PROPER("inplace open");
745 if (oldlen == 1 && *PL_oldname == '-') {
746 setdefout(gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL,
748 return IoIFP(GvIOp(gv));
750 #ifndef FLEXFILENAMES
751 filedev = PL_statbuf.st_dev;
752 fileino = PL_statbuf.st_ino;
754 PL_filemode = PL_statbuf.st_mode;
755 fileuid = PL_statbuf.st_uid;
756 filegid = PL_statbuf.st_gid;
757 if (!S_ISREG(PL_filemode)) {
758 if (ckWARN_d(WARN_INPLACE))
759 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
760 "Can't do inplace edit: %s is not a regular file",
766 const char *star = strchr(PL_inplace, '*');
768 const char *begin = PL_inplace;
771 sv_catpvn(sv, begin, star - begin);
772 sv_catpvn(sv, PL_oldname, oldlen);
774 } while ((star = strchr(begin, '*')));
779 sv_catpv(sv,PL_inplace);
781 #ifndef FLEXFILENAMES
782 if ((PerlLIO_stat(SvPVX_const(sv),&PL_statbuf) >= 0
783 && PL_statbuf.st_dev == filedev
784 && PL_statbuf.st_ino == fileino)
786 || ((_djstat_fail_bits & _STFAIL_TRUENAME)!=0)
790 if (ckWARN_d(WARN_INPLACE))
791 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
792 "Can't do inplace edit: %"SVf" would not be unique",
799 #if !defined(DOSISH) && !defined(__CYGWIN__) && !defined(EPOC)
800 if (PerlLIO_rename(PL_oldname,SvPVX_const(sv)) < 0) {
801 if (ckWARN_d(WARN_INPLACE))
802 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
803 "Can't rename %s to %"SVf": %s, skipping file",
804 PL_oldname, SVfARG(sv), Strerror(errno));
810 (void)PerlLIO_unlink(SvPVX_const(sv));
811 (void)PerlLIO_rename(PL_oldname,SvPVX_const(sv));
812 do_open(gv,(char*)SvPVX_const(sv),SvCUR(sv),TRUE,O_RDONLY,0,NULL);
815 (void)UNLINK(SvPVX_const(sv));
816 if (link(PL_oldname,SvPVX_const(sv)) < 0) {
817 if (ckWARN_d(WARN_INPLACE))
818 Perl_warner(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 if (ckWARN_d(WARN_INPLACE))
832 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
833 "Can't remove %s: %s, skipping file",
834 PL_oldname, Strerror(errno) );
840 Perl_croak(aTHX_ "Can't do inplace edit without backup");
844 sv_setpvn(sv,PL_oldname,oldlen);
845 SETERRNO(0,0); /* in case sprintf set errno */
846 if (!Perl_do_openn(aTHX_ PL_argvoutgv, (char*)SvPVX_const(sv),
849 O_WRONLY|O_CREAT|O_TRUNC,0,
851 O_WRONLY|O_CREAT|OPEN_EXCL,0600,
854 if (ckWARN_d(WARN_INPLACE))
855 Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't do inplace edit on %s: %s",
856 PL_oldname, Strerror(errno) );
860 setdefout(PL_argvoutgv);
861 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
862 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
864 (void)fchmod(PL_lastfd,PL_filemode);
866 # if !(defined(WIN32) && defined(__BORLANDC__))
867 /* Borland runtime creates a readonly file! */
868 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
871 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
873 (void)fchown(PL_lastfd,fileuid,filegid);
876 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
881 return IoIFP(GvIOp(gv));
884 if (ckWARN_d(WARN_INPLACE)) {
885 const int eno = errno;
886 if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0
887 && !S_ISREG(PL_statbuf.st_mode))
889 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
890 "Can't do inplace edit: %s is not a regular file",
894 Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't open %s: %s",
895 PL_oldname, Strerror(eno));
899 if (io && (IoFLAGS(io) & IOf_ARGV))
900 IoFLAGS(io) |= IOf_START;
902 (void)do_close(PL_argvoutgv,FALSE);
903 if (io && (IoFLAGS(io) & IOf_ARGV)
904 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
906 GV * const oldout = MUTABLE_GV(av_pop(PL_argvout_stack));
908 SvREFCNT_dec(oldout);
911 setdefout(gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO));
916 /* explicit renamed to avoid C++ conflict -- kja */
918 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
926 if (!gv || !isGV_with_GP(gv)) {
928 SETERRNO(EBADF,SS_IVCHAN);
932 if (!io) { /* never opened */
934 if (ckWARN(WARN_UNOPENED)) /* no check for closed here */
935 report_evil_fh(gv, io, PL_op->op_type);
936 SETERRNO(EBADF,SS_IVCHAN);
940 retval = io_close(io, not_implicit);
944 IoLINES_LEFT(io) = IoPAGE_LEN(io);
946 IoTYPE(io) = IoTYPE_CLOSED;
951 Perl_io_close(pTHX_ IO *io, bool not_implicit)
956 PERL_ARGS_ASSERT_IO_CLOSE;
959 if (IoTYPE(io) == IoTYPE_PIPE) {
960 const int status = PerlProc_pclose(IoIFP(io));
962 STATUS_NATIVE_CHILD_SET(status);
963 retval = (STATUS_UNIX == 0);
966 retval = (status != -1);
969 else if (IoTYPE(io) == IoTYPE_STD)
972 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
973 const bool prev_err = PerlIO_error(IoOFP(io));
974 retval = (PerlIO_close(IoOFP(io)) != EOF && !prev_err);
975 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
978 const bool prev_err = PerlIO_error(IoIFP(io));
979 retval = (PerlIO_close(IoIFP(io)) != EOF && !prev_err);
982 IoOFP(io) = IoIFP(io) = NULL;
984 else if (not_implicit) {
985 SETERRNO(EBADF,SS_IVCHAN);
992 Perl_do_eof(pTHX_ GV *gv)
995 register IO * const io = GvIO(gv);
997 PERL_ARGS_ASSERT_DO_EOF;
1001 else if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
1002 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1005 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
1006 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
1007 return FALSE; /* this is the most usual case */
1011 /* getc and ungetc can stomp on errno */
1013 const int ch = PerlIO_getc(IoIFP(io));
1015 (void)PerlIO_ungetc(IoIFP(io),ch);
1022 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
1023 if (PerlIO_get_cnt(IoIFP(io)) < -1)
1024 PerlIO_set_cnt(IoIFP(io),-1);
1026 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
1027 if (gv != PL_argvgv || !nextargv(gv)) /* get another fp handy */
1031 return TRUE; /* normal fp, definitely end of file */
1037 Perl_do_tell(pTHX_ GV *gv)
1040 register IO *io = NULL;
1041 register PerlIO *fp;
1043 PERL_ARGS_ASSERT_DO_TELL;
1045 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1046 #ifdef ULTRIX_STDIO_BOTCH
1048 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1050 return PerlIO_tell(fp);
1052 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1053 report_evil_fh(gv, io, PL_op->op_type);
1054 SETERRNO(EBADF,RMS_IFI);
1059 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
1062 register IO *io = NULL;
1063 register PerlIO *fp;
1065 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1066 #ifdef ULTRIX_STDIO_BOTCH
1068 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1070 return PerlIO_seek(fp, pos, whence) >= 0;
1072 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1073 report_evil_fh(gv, io, PL_op->op_type);
1074 SETERRNO(EBADF,RMS_IFI);
1079 Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
1082 register IO *io = NULL;
1083 register PerlIO *fp;
1085 PERL_ARGS_ASSERT_DO_SYSSEEK;
1087 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
1088 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
1089 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1090 report_evil_fh(gv, io, PL_op->op_type);
1091 SETERRNO(EBADF,RMS_IFI);
1096 Perl_mode_from_discipline(pTHX_ const char *s, STRLEN len)
1098 int mode = O_BINARY;
1104 if (s[2] == 'a' && s[3] == 'w'
1105 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1114 if (s[2] == 'r' && s[3] == 'l' && s[4] == 'f'
1115 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1124 goto fail_discipline;
1127 else if (isSPACE(*s)) {
1134 end = strchr(s+1, ':');
1137 #ifndef PERLIO_LAYERS
1138 Perl_croak(aTHX_ "IO layers (like '%.*s') unavailable", end-s, s);
1149 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE)
1151 my_chsize(int fd, Off_t length)
1154 /* code courtesy of William Kucharski */
1159 if (PerlLIO_fstat(fd, &filebuf) < 0)
1162 if (filebuf.st_size < length) {
1164 /* extend file length */
1166 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
1169 /* write a "0" byte */
1171 if ((PerlLIO_write(fd, "", 1)) != 1)
1175 /* truncate length */
1179 fl.l_start = length;
1180 fl.l_type = F_WRLCK; /* write lock on file space */
1183 * This relies on the UNDOCUMENTED F_FREESP argument to
1184 * fcntl(2), which truncates the file so that it ends at the
1185 * position indicated by fl.l_start.
1187 * Will minor miracles never cease?
1190 if (fcntl(fd, F_FREESP, &fl) < 0)
1196 Perl_croak_nocontext("truncate not implemented");
1197 #endif /* F_FREESP */
1200 #endif /* !HAS_TRUNCATE && !HAS_CHSIZE */
1203 Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
1207 PERL_ARGS_ASSERT_DO_PRINT;
1209 /* assuming fp is checked earlier */
1212 if (SvTYPE(sv) == SVt_IV && SvIOK(sv)) {
1213 assert(!SvGMAGICAL(sv));
1215 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
1217 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
1218 return !PerlIO_error(fp);
1222 /* Do this first to trigger any overloading. */
1223 const char *tmps = SvPV_const(sv, len);
1227 if (PerlIO_isutf8(fp)) {
1229 /* We don't modify the original scalar. */
1230 tmpbuf = bytes_to_utf8((const U8*) tmps, &len);
1231 tmps = (char *) tmpbuf;
1234 else if (DO_UTF8(sv)) {
1235 STRLEN tmplen = len;
1237 U8 * const result = bytes_from_utf8((const U8*) tmps, &tmplen, &utf8);
1240 tmps = (char *) tmpbuf;
1244 assert((char *)result == tmps);
1245 if (ckWARN_d(WARN_UTF8)) {
1246 Perl_warner(aTHX_ packWARN(WARN_UTF8),
1247 "Wide character in print");
1251 /* To detect whether the process is about to overstep its
1252 * filesize limit we would need getrlimit(). We could then
1253 * also transparently raise the limit with setrlimit() --
1254 * but only until the system hard limit/the filesystem limit,
1255 * at which we would get EPERM. Note that when using buffered
1256 * io the write failure can be delayed until the flush/close. --jhi */
1257 if (len && (PerlIO_write(fp,tmps,len) == 0))
1260 return happy ? !PerlIO_error(fp) : FALSE;
1272 if (PL_op->op_flags & OPf_REF) {
1277 return PL_laststatval;
1280 PL_laststype = OP_STAT;
1282 sv_setpvs(PL_statname, "");
1285 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
1286 } else if (IoDIRP(io)) {
1287 return (PL_laststatval = PerlLIO_fstat(my_dirfd(IoDIRP(io)), &PL_statcache));
1289 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1290 report_evil_fh(gv, io, PL_op->op_type);
1291 return (PL_laststatval = -1);
1294 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1295 report_evil_fh(gv, io, PL_op->op_type);
1296 return (PL_laststatval = -1);
1299 else if (PL_op->op_private & OPpFT_STACKED) {
1300 return PL_laststatval;
1303 SV* const sv = POPs;
1307 if (isGV_with_GP(sv)) {
1308 gv = MUTABLE_GV(sv);
1311 else if (SvROK(sv) && isGV_with_GP(SvRV(sv))) {
1312 gv = MUTABLE_GV(SvRV(sv));
1315 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
1316 io = MUTABLE_IO(SvRV(sv));
1318 goto do_fstat_have_io;
1321 s = SvPV_const(sv, len);
1323 sv_setpvn(PL_statname, s, len);
1324 s = SvPVX_const(PL_statname); /* s now NUL-terminated */
1325 PL_laststype = OP_STAT;
1326 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1327 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
1328 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
1329 return PL_laststatval;
1338 static const char no_prev_lstat[] = "The stat preceding -l _ wasn't an lstat";
1342 if (PL_op->op_flags & OPf_REF) {
1344 if (cGVOP_gv == PL_defgv) {
1345 if (PL_laststype != OP_LSTAT)
1346 Perl_croak(aTHX_ no_prev_lstat);
1347 return PL_laststatval;
1349 if (ckWARN(WARN_IO)) {
1350 Perl_warner(aTHX_ packWARN(WARN_IO), "Use of -l on filehandle %s",
1352 return (PL_laststatval = -1);
1355 else if (PL_laststype != OP_LSTAT
1356 && (PL_op->op_private & OPpFT_STACKED) && ckWARN(WARN_IO))
1357 Perl_croak(aTHX_ no_prev_lstat);
1359 PL_laststype = OP_LSTAT;
1363 if (SvROK(sv) && isGV_with_GP(SvRV(sv)) && ckWARN(WARN_IO)) {
1364 Perl_warner(aTHX_ packWARN(WARN_IO), "Use of -l on filehandle %s",
1365 GvENAME((const GV *)SvRV(sv)));
1366 return (PL_laststatval = -1);
1368 file = SvPV_nolen_const(sv);
1369 sv_setpv(PL_statname,file);
1370 PL_laststatval = PerlLIO_lstat(file,&PL_statcache);
1371 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(file, '\n'))
1372 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "lstat");
1373 return PL_laststatval;
1377 S_exec_failed(pTHX_ const char *cmd, int fd, int do_report)
1379 const int e = errno;
1380 PERL_ARGS_ASSERT_EXEC_FAILED;
1381 if (ckWARN(WARN_EXEC))
1382 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
1385 PerlLIO_write(fd, (void*)&e, sizeof(int));
1391 Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1392 int fd, int do_report)
1395 PERL_ARGS_ASSERT_DO_AEXEC5;
1396 #if defined(__SYMBIAN32__) || defined(__LIBCATAMOUNT__)
1397 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1401 const char *tmps = NULL;
1402 Newx(PL_Argv, sp - mark + 1, const char*);
1405 while (++mark <= sp) {
1407 *a++ = SvPV_nolen_const(*mark);
1413 tmps = SvPV_nolen_const(really);
1414 if ((!really && *PL_Argv[0] != '/') ||
1415 (really && *tmps != '/')) /* will execvp use PATH? */
1416 TAINT_ENV(); /* testing IFS here is overkill, probably */
1418 if (really && *tmps)
1419 PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
1421 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1423 S_exec_failed(aTHX_ (really ? tmps : PL_Argv[0]), fd, do_report);
1431 Perl_do_execfree(pTHX)
1440 #ifdef PERL_DEFAULT_DO_EXEC3_IMPLEMENTATION
1443 Perl_do_exec3(pTHX_ const char *incmd, int fd, int do_report)
1446 register const char **a;
1450 /* Make a copy so we can change it */
1451 const Size_t cmdlen = strlen(incmd) + 1;
1453 PERL_ARGS_ASSERT_DO_EXEC3;
1455 Newx(buf, cmdlen, char);
1457 memcpy(cmd, incmd, cmdlen);
1459 while (*cmd && isSPACE(*cmd))
1462 /* save an extra exec if possible */
1466 char flags[PERL_FLAGS_MAX];
1467 if (strnEQ(cmd,PL_cshname,PL_cshlen) &&
1468 strnEQ(cmd+PL_cshlen," -c",3)) {
1469 my_strlcpy(flags, "-c", PERL_FLAGS_MAX);
1470 s = cmd+PL_cshlen+3;
1473 my_strlcat(flags, "f", PERL_FLAGS_MAX - 2);
1478 char * const ncmd = s;
1484 if (s[-1] == '\'') {
1487 PerlProc_execl(PL_cshname, "csh", flags, ncmd, (char*)NULL);
1490 S_exec_failed(aTHX_ PL_cshname, fd, do_report);
1499 /* see if there are shell metacharacters in it */
1501 if (*cmd == '.' && isSPACE(cmd[1]))
1504 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1509 s++; /* catch VAR=val gizmo */
1513 for (s = cmd; *s; s++) {
1514 if (*s != ' ' && !isALPHA(*s) &&
1515 strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1516 if (*s == '\n' && !s[1]) {
1520 /* handle the 2>&1 construct at the end */
1521 if (*s == '>' && s[1] == '&' && s[2] == '1'
1522 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1523 && (!s[3] || isSPACE(s[3])))
1525 const char *t = s + 3;
1527 while (*t && isSPACE(*t))
1529 if (!*t && (PerlLIO_dup2(1,2) != -1)) {
1536 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char *)NULL);
1538 S_exec_failed(aTHX_ PL_sh_path, fd, do_report);
1544 Newx(PL_Argv, (s - cmd) / 2 + 2, const char*);
1545 PL_Cmd = savepvn(cmd, s-cmd);
1547 for (s = PL_Cmd; *s;) {
1552 while (*s && !isSPACE(*s))
1560 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1562 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1566 S_exec_failed(aTHX_ PL_Argv[0], fd, do_report);
1573 #endif /* OS2 || WIN32 */
1576 Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
1580 register I32 tot = 0;
1581 const char *const what = PL_op_name[type];
1583 SV ** const oldmark = mark;
1585 PERL_ARGS_ASSERT_APPLY;
1587 /* Doing this ahead of the switch statement preserves the old behaviour,
1588 where attempting to use kill as a taint test test would fail on
1589 platforms where kill was not defined. */
1591 if (type == OP_KILL)
1592 Perl_die(aTHX_ PL_no_func, what);
1595 if (type == OP_CHOWN)
1596 Perl_die(aTHX_ PL_no_func, what);
1600 #define APPLY_TAINT_PROPER() \
1602 if (PL_tainted) { TAINT_PROPER(what); } \
1605 /* This is a first heuristic; it doesn't catch tainting magic. */
1607 while (++mark <= sp) {
1608 if (SvTAINTED(*mark)) {
1617 APPLY_TAINT_PROPER();
1620 APPLY_TAINT_PROPER();
1622 while (++mark <= sp) {
1624 if (isGV_with_GP(*mark)) {
1625 gv = MUTABLE_GV(*mark);
1627 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1629 APPLY_TAINT_PROPER();
1630 if (fchmod(PerlIO_fileno(IoIFP(GvIOn(gv))), val))
1633 Perl_die(aTHX_ PL_no_func, "fchmod");
1640 else if (SvROK(*mark) && isGV_with_GP(SvRV(*mark))) {
1641 gv = MUTABLE_GV(SvRV(*mark));
1645 const char *name = SvPV_nolen_const(*mark);
1646 APPLY_TAINT_PROPER();
1647 if (PerlLIO_chmod(name, val))
1655 APPLY_TAINT_PROPER();
1656 if (sp - mark > 2) {
1658 val = SvIVx(*++mark);
1659 val2 = SvIVx(*++mark);
1660 APPLY_TAINT_PROPER();
1662 while (++mark <= sp) {
1664 if (isGV_with_GP(*mark)) {
1665 gv = MUTABLE_GV(*mark);
1667 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1669 APPLY_TAINT_PROPER();
1670 if (fchown(PerlIO_fileno(IoIFP(GvIOn(gv))), val, val2))
1673 Perl_die(aTHX_ PL_no_func, "fchown");
1680 else if (SvROK(*mark) && isGV_with_GP(SvRV(*mark))) {
1681 gv = MUTABLE_GV(SvRV(*mark));
1685 const char *name = SvPV_nolen_const(*mark);
1686 APPLY_TAINT_PROPER();
1687 if (PerlLIO_chown(name, val, val2))
1695 XXX Should we make lchown() directly available from perl?
1696 For now, we'll let Configure test for HAS_LCHOWN, but do
1697 nothing in the core.
1702 APPLY_TAINT_PROPER();
1705 s = SvPVx_nolen_const(*++mark);
1707 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1709 if ((val = whichsig(s)) < 0)
1710 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
1714 APPLY_TAINT_PROPER();
1717 /* kill() doesn't do process groups (job trees?) under VMS */
1718 if (val < 0) val = -val;
1719 if (val == SIGKILL) {
1720 # include <starlet.h>
1721 /* Use native sys$delprc() to insure that target process is
1722 * deleted; supervisor-mode images don't pay attention to
1723 * CRTL's emulation of Unix-style signals and kill()
1725 while (++mark <= sp) {
1726 I32 proc = SvIV(*mark);
1727 register unsigned long int __vmssts;
1728 APPLY_TAINT_PROPER();
1729 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1733 case SS$_NOSUCHNODE:
1734 SETERRNO(ESRCH,__vmssts);
1737 SETERRNO(EPERM,__vmssts);
1740 SETERRNO(EVMSERR,__vmssts);
1749 while (++mark <= sp) {
1750 const I32 proc = SvIV(*mark);
1751 APPLY_TAINT_PROPER();
1753 if (PerlProc_killpg(proc,val)) /* BSD */
1755 if (PerlProc_kill(-proc,val)) /* SYSV */
1761 while (++mark <= sp) {
1762 const I32 proc = SvIV(*mark);
1763 APPLY_TAINT_PROPER();
1764 if (PerlProc_kill(proc, val))
1771 APPLY_TAINT_PROPER();
1773 while (++mark <= sp) {
1774 s = SvPV_nolen_const(*mark);
1775 APPLY_TAINT_PROPER();
1776 if (PL_euid || PL_unsafe) {
1780 else { /* don't let root wipe out directories without -U */
1781 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1790 #if defined(HAS_UTIME) || defined(HAS_FUTIMES)
1792 APPLY_TAINT_PROPER();
1793 if (sp - mark > 2) {
1794 #if defined(HAS_FUTIMES)
1795 struct timeval utbuf[2];
1796 void *utbufp = utbuf;
1797 #elif defined(I_UTIME) || defined(VMS)
1798 struct utimbuf utbuf;
1799 struct utimbuf *utbufp = &utbuf;
1805 void *utbufp = &utbuf;
1808 SV* const accessed = *++mark;
1809 SV* const modified = *++mark;
1811 /* Be like C, and if both times are undefined, let the C
1812 * library figure out what to do. This usually means
1813 * "current time". */
1815 if ( accessed == &PL_sv_undef && modified == &PL_sv_undef )
1818 Zero(&utbuf, sizeof utbuf, char);
1820 utbuf[0].tv_sec = (long)SvIV(accessed); /* time accessed */
1821 utbuf[0].tv_usec = 0;
1822 utbuf[1].tv_sec = (long)SvIV(modified); /* time modified */
1823 utbuf[1].tv_usec = 0;
1824 #elif defined(BIG_TIME)
1825 utbuf.actime = (Time_t)SvNV(accessed); /* time accessed */
1826 utbuf.modtime = (Time_t)SvNV(modified); /* time modified */
1828 utbuf.actime = (Time_t)SvIV(accessed); /* time accessed */
1829 utbuf.modtime = (Time_t)SvIV(modified); /* time modified */
1832 APPLY_TAINT_PROPER();
1834 while (++mark <= sp) {
1836 if (isGV_with_GP(*mark)) {
1837 gv = MUTABLE_GV(*mark);
1839 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1841 APPLY_TAINT_PROPER();
1842 if (futimes(PerlIO_fileno(IoIFP(GvIOn(gv))),
1843 (struct timeval *) utbufp))
1846 Perl_die(aTHX_ PL_no_func, "futimes");
1853 else if (SvROK(*mark) && isGV_with_GP(SvRV(*mark))) {
1854 gv = MUTABLE_GV(SvRV(*mark));
1858 const char * const name = SvPV_nolen_const(*mark);
1859 APPLY_TAINT_PROPER();
1861 if (utimes(name, (struct timeval *)utbufp))
1863 if (PerlLIO_utime(name, utbufp))
1877 #undef APPLY_TAINT_PROPER
1880 /* Do the permissions allow some operation? Assumes statcache already set. */
1881 #ifndef VMS /* VMS' cando is in vms.c */
1883 Perl_cando(pTHX_ Mode_t mode, bool effective, register const Stat_t *statbufp)
1884 /* effective is a flag, true for EUID, or for checking if the effective gid
1885 * is in the list of groups returned from getgroups().
1890 PERL_ARGS_ASSERT_CANDO;
1893 /* [Comments and code from Len Reed]
1894 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1895 * to write-protected files. The execute permission bit is set
1896 * by the Miscrosoft C library stat() function for the following:
1901 * All files and directories are readable.
1902 * Directories and special files, e.g. "CON", cannot be
1904 * [Comment by Tom Dinger -- a directory can have the write-protect
1905 * bit set in the file system, but DOS permits changes to
1906 * the directory anyway. In addition, all bets are off
1907 * here for networked software, such as Novell and
1911 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1912 * too so it will actually look into the files for magic numbers
1914 return (mode & statbufp->st_mode) ? TRUE : FALSE;
1916 #else /* ! DOSISH */
1917 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
1918 if (mode == S_IXUSR) {
1919 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1923 return TRUE; /* root reads and writes anything */
1926 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1927 if (statbufp->st_mode & mode)
1928 return TRUE; /* ok as "user" */
1930 else if (ingroup(statbufp->st_gid,effective)) {
1931 if (statbufp->st_mode & mode >> 3)
1932 return TRUE; /* ok as "group" */
1934 else if (statbufp->st_mode & mode >> 6)
1935 return TRUE; /* ok as "other" */
1937 #endif /* ! DOSISH */
1942 S_ingroup(pTHX_ Gid_t testgid, bool effective)
1945 if (testgid == (effective ? PL_egid : PL_gid))
1947 #ifdef HAS_GETGROUPS
1949 Groups_t *gary = NULL;
1953 anum = getgroups(0, gary);
1954 Newx(gary, anum, Groups_t);
1955 anum = getgroups(anum, gary);
1957 if (gary[anum] == testgid) {
1970 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1973 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
1976 const key_t key = (key_t)SvNVx(*++mark);
1977 SV *nsv = optype == OP_MSGGET ? NULL : *++mark;
1978 const I32 flags = SvIVx(*++mark);
1980 PERL_ARGS_ASSERT_DO_IPCGET;
1981 PERL_UNUSED_ARG(sp);
1988 return msgget(key, flags);
1992 return semget(key, (int) SvIV(nsv), flags);
1996 return shmget(key, (size_t) SvUV(nsv), flags);
1998 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
2000 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2003 return -1; /* should never happen */
2007 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
2012 const I32 id = SvIVx(*++mark);
2014 const I32 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
2016 const I32 cmd = SvIVx(*++mark);
2017 SV * const astr = *++mark;
2018 STRLEN infosize = 0;
2019 I32 getinfo = (cmd == IPC_STAT);
2021 PERL_ARGS_ASSERT_DO_IPCCTL;
2022 PERL_UNUSED_ARG(sp);
2028 if (cmd == IPC_STAT || cmd == IPC_SET)
2029 infosize = sizeof(struct msqid_ds);
2034 if (cmd == IPC_STAT || cmd == IPC_SET)
2035 infosize = sizeof(struct shmid_ds);
2041 if (cmd == IPC_STAT || cmd == IPC_SET)
2042 infosize = sizeof(struct semid_ds);
2043 else if (cmd == GETALL || cmd == SETALL)
2045 struct semid_ds semds;
2047 #ifdef EXTRA_F_IN_SEMUN_BUF
2048 semun.buff = &semds;
2052 getinfo = (cmd == GETALL);
2053 if (Semctl(id, 0, IPC_STAT, semun) == -1)
2055 infosize = semds.sem_nsems * sizeof(short);
2056 /* "short" is technically wrong but much more portable
2057 than guessing about u_?short(_t)? */
2060 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2064 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
2066 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2074 SvPV_force_nolen(astr);
2075 a = SvGROW(astr, infosize+1);
2080 a = SvPV(astr, len);
2081 if (len != infosize)
2082 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
2090 const IV i = SvIV(astr);
2091 a = INT2PTR(char *,i); /* ouch */
2098 ret = msgctl(id, cmd, (struct msqid_ds *)a);
2104 union semun unsemds;
2106 #ifdef EXTRA_F_IN_SEMUN_BUF
2107 unsemds.buff = (struct semid_ds *)a;
2109 unsemds.buf = (struct semid_ds *)a;
2111 ret = Semctl(id, n, cmd, unsemds);
2113 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2120 ret = shmctl(id, cmd, (struct shmid_ds *)a);
2124 if (getinfo && ret >= 0) {
2125 SvCUR_set(astr, infosize);
2126 *SvEND(astr) = '\0';
2133 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
2138 const I32 id = SvIVx(*++mark);
2139 SV * const mstr = *++mark;
2140 const I32 flags = SvIVx(*++mark);
2141 const char * const mbuf = SvPV_const(mstr, len);
2142 const I32 msize = len - sizeof(long);
2144 PERL_ARGS_ASSERT_DO_MSGSND;
2145 PERL_UNUSED_ARG(sp);
2148 Perl_croak(aTHX_ "Arg too short for msgsnd");
2150 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
2152 PERL_UNUSED_ARG(sp);
2153 PERL_UNUSED_ARG(mark);
2154 Perl_croak(aTHX_ "msgsnd not implemented");
2159 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
2165 I32 msize, flags, ret;
2166 const I32 id = SvIVx(*++mark);
2167 SV * const mstr = *++mark;
2169 PERL_ARGS_ASSERT_DO_MSGRCV;
2170 PERL_UNUSED_ARG(sp);
2172 /* suppress warning when reading into undef var --jhi */
2174 sv_setpvs(mstr, "");
2175 msize = SvIVx(*++mark);
2176 mtype = (long)SvIVx(*++mark);
2177 flags = SvIVx(*++mark);
2178 SvPV_force_nolen(mstr);
2179 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
2182 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
2184 SvCUR_set(mstr, sizeof(long)+ret);
2185 *SvEND(mstr) = '\0';
2186 #ifndef INCOMPLETE_TAINTS
2187 /* who knows who has been playing with this message? */
2193 PERL_UNUSED_ARG(sp);
2194 PERL_UNUSED_ARG(mark);
2195 Perl_croak(aTHX_ "msgrcv not implemented");
2200 Perl_do_semop(pTHX_ SV **mark, SV **sp)
2205 const I32 id = SvIVx(*++mark);
2206 SV * const opstr = *++mark;
2207 const char * const opbuf = SvPV_const(opstr, opsize);
2209 PERL_ARGS_ASSERT_DO_SEMOP;
2210 PERL_UNUSED_ARG(sp);
2212 if (opsize < 3 * SHORTSIZE
2213 || (opsize % (3 * SHORTSIZE))) {
2214 SETERRNO(EINVAL,LIB_INVARG);
2218 /* We can't assume that sizeof(struct sembuf) == 3 * sizeof(short). */
2220 const int nsops = opsize / (3 * sizeof (short));
2222 short * const ops = (short *) opbuf;
2224 struct sembuf *temps, *t;
2227 Newx (temps, nsops, struct sembuf);
2235 result = semop(id, temps, nsops);
2249 Perl_croak(aTHX_ "semop not implemented");
2254 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
2259 struct shmid_ds shmds;
2260 const I32 id = SvIVx(*++mark);
2261 SV * const mstr = *++mark;
2262 const I32 mpos = SvIVx(*++mark);
2263 const I32 msize = SvIVx(*++mark);
2265 PERL_ARGS_ASSERT_DO_SHMIO;
2266 PERL_UNUSED_ARG(sp);
2269 if (shmctl(id, IPC_STAT, &shmds) == -1)
2271 if (mpos < 0 || msize < 0
2272 || (size_t)mpos + msize > (size_t)shmds.shm_segsz) {
2273 SETERRNO(EFAULT,SS_ACCVIO); /* can't do as caller requested */
2276 shm = (char *)shmat(id, NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
2277 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2279 if (optype == OP_SHMREAD) {
2281 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2283 sv_setpvs(mstr, "");
2284 SvPV_force_nolen(mstr);
2285 mbuf = SvGROW(mstr, (STRLEN)msize+1);
2287 Copy(shm + mpos, mbuf, msize, char);
2288 SvCUR_set(mstr, msize);
2289 *SvEND(mstr) = '\0';
2291 #ifndef INCOMPLETE_TAINTS
2292 /* who knows who has been playing with this shared memory? */
2299 const char *mbuf = SvPV_const(mstr, len);
2300 const I32 n = ((I32)len > msize) ? msize : (I32)len;
2301 Copy(mbuf, shm + mpos, n, char);
2303 memzero(shm + mpos + n, msize - n);
2307 Perl_croak(aTHX_ "shm I/O not implemented");
2311 #endif /* SYSV IPC */
2316 =for apidoc start_glob
2318 Function called by C<do_readline> to spawn a glob (or do the glob inside
2319 perl on VMS). This code used to be inline, but now perl uses C<File::Glob>
2320 this glob starter is only used by miniperl during the build process.
2321 Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up.
2327 Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
2330 SV * const tmpcmd = newSV(0);
2333 PERL_ARGS_ASSERT_START_GLOB;
2337 #ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
2338 /* since spawning off a process is a real performance hit */
2345 fp = Perl_vms_start_glob(aTHX_ tmpglob, io);
2350 sv_setpv(tmpcmd, "for a in ");
2351 sv_catsv(tmpcmd, tmpglob);
2352 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
2355 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
2356 sv_catsv(tmpcmd, tmpglob);
2358 sv_setpv(tmpcmd, "perlglob ");
2359 sv_catsv(tmpcmd, tmpglob);
2360 sv_catpv(tmpcmd, " |");
2365 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
2366 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
2367 sv_catsv(tmpcmd, tmpglob);
2368 sv_catpv(tmpcmd, "' 2>/dev/null |");
2370 sv_setpv(tmpcmd, "echo ");
2371 sv_catsv(tmpcmd, tmpglob);
2373 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
2375 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
2378 #endif /* !DOSISH */
2379 (void)do_open(PL_last_in_gv, (char*)SvPVX_const(tmpcmd), SvCUR(tmpcmd),
2380 FALSE, O_RDONLY, 0, NULL);
2389 * c-indentation-style: bsd
2391 * indent-tabs-mode: t
2394 * ex: set ts=8 sts=4 sw=4 noet: