3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, 2004, 2005, by Larry Wall and others
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
12 * "Far below them they saw the white waters pour into a foaming bowl, and
13 * then swirl darkly about a deep oval basin in the rocks, until they found
14 * their way out again through a narrow gate, and flowed away, fuming and
15 * chattering, into calmer and more level reaches."
18 /* This file contains functions that do the actual I/O on behalf of ops.
19 * For example, pp_print() calls the do_print() function in this file for
20 * each argument needing printing.
24 #define PERL_IN_DOIO_C
27 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
36 # ifndef HAS_SHMAT_PROTOTYPE
37 extern Shmat_t shmat (int, char *, int);
43 # if defined(_MSC_VER) || defined(__MINGW32__)
44 # include <sys/utime.h>
51 # define OPEN_EXCL O_EXCL
56 #define PERL_MODE_MAX 8
57 #define PERL_FLAGS_MAX 10
62 Perl_do_open(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
63 int rawmode, int rawperm, PerlIO *supplied_fp)
65 return do_openn(gv, name, len, as_raw, rawmode, rawperm,
66 supplied_fp, (SV **) NULL, 0);
70 Perl_do_open9(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
71 int rawmode, int rawperm, PerlIO *supplied_fp, SV *svs,
74 return do_openn(gv, name, len, as_raw, rawmode, rawperm,
75 supplied_fp, &svs, 1);
79 Perl_do_openn(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
80 int rawmode, int rawperm, PerlIO *supplied_fp, SV **svp,
83 register IO *io = GvIOn(gv);
84 PerlIO *saveifp = Nullfp;
85 PerlIO *saveofp = Nullfp;
87 char savetype = IoTYPE_CLOSED;
92 bool was_fdopen = FALSE;
93 bool in_raw = 0, in_crlf = 0, out_raw = 0, out_crlf = 0;
95 char mode[PERL_MODE_MAX]; /* stdio file mode ("r\0", "rb\0", "r+b\0" etc.) */
98 Zero(mode,sizeof(mode),char);
99 PL_forkprocess = 1; /* assume true if no fork */
101 /* Collect default raw/crlf info from the op */
102 if (PL_op && PL_op->op_type == OP_OPEN) {
103 /* set up IO layers */
104 U8 flags = PL_op->op_private;
105 in_raw = (flags & OPpOPEN_IN_RAW);
106 in_crlf = (flags & OPpOPEN_IN_CRLF);
107 out_raw = (flags & OPpOPEN_OUT_RAW);
108 out_crlf = (flags & OPpOPEN_OUT_CRLF);
111 /* If currently open - close before we re-open */
113 fd = PerlIO_fileno(IoIFP(io));
114 if (IoTYPE(io) == IoTYPE_STD) {
115 /* This is a clone of one of STD* handles */
118 else if (fd >= 0 && fd <= PL_maxsysfd) {
119 /* This is one of the original STD* handles */
122 savetype = IoTYPE(io);
126 else if (IoTYPE(io) == IoTYPE_PIPE)
127 result = PerlProc_pclose(IoIFP(io));
128 else if (IoIFP(io) != IoOFP(io)) {
130 result = PerlIO_close(IoOFP(io));
131 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
134 result = PerlIO_close(IoIFP(io));
137 result = PerlIO_close(IoIFP(io));
138 if (result == EOF && fd > PL_maxsysfd) {
139 /* Why is this not Perl_warn*() call ? */
140 PerlIO_printf(Perl_error_log,
141 "Warning: unable to close filehandle %s properly.\n",
144 IoOFP(io) = IoIFP(io) = Nullfp;
148 /* sysopen style args, i.e. integer mode and permissions */
150 const int appendtrunc =
152 #ifdef O_APPEND /* Not fully portable. */
155 #ifdef O_TRUNC /* Not fully portable. */
159 int modifyingmode = O_WRONLY|O_RDWR|O_CREAT|appendtrunc;
163 Perl_croak(aTHX_ "panic: sysopen with multiple args");
171 It might be (in OS/390 and Mac OS Classic it is)
177 This means that simple & with O_RDWR would look
178 like O_RDONLY is present. Therefore we have to
181 if ((ismodifying = (rawmode & modifyingmode))) {
182 if ((ismodifying & O_WRONLY) == O_WRONLY ||
183 (ismodifying & O_RDWR) == O_RDWR ||
184 (ismodifying & (O_CREAT|appendtrunc)))
185 TAINT_PROPER("sysopen");
187 mode[ix++] = IoTYPE_NUMERIC; /* Marker to openn to use numeric "sysopen" */
189 #if defined(USE_64_BIT_RAWIO) && defined(O_LARGEFILE)
190 rawmode |= O_LARGEFILE; /* Transparently largefiley. */
193 IoTYPE(io) = PerlIO_intmode2str(rawmode, &mode[ix], &writing);
195 namesv = sv_2mortal(newSVpvn(name,strlen(name)));
199 fp = PerlIO_openn(aTHX_ type, mode, -1, rawmode, rawperm, NULL, num_svs, svp);
202 /* Regular (non-sys) open */
207 PerlIO *that_fp = NULL;
209 type = savepvn(name, len);
213 /* Lose leading and trailing white space */
215 for (; isSPACE(*type); type++) ;
216 while (tend > type && isSPACE(tend[-1]))
220 /* New style explicit name, type is just mode and layer info */
223 if (SvROK(*svp) && !strchr(name,'&')) {
225 Perl_warner(aTHX_ packWARN(WARN_IO),
226 "Can't open a reference");
227 SETERRNO(EINVAL, LIB_INVARG);
230 #endif /* USE_STDIO */
231 name = SvOK(*svp) ? SvPV(*svp, l) : "";
233 name = savepvn(name, len);
241 if ((*type == IoTYPE_RDWR) && /* scary */
242 (*(type+1) == IoTYPE_RDONLY || *(type+1) == IoTYPE_WRONLY) &&
243 ((!num_svs || (tend > type+1 && tend[-1] != IoTYPE_PIPE)))) {
244 TAINT_PROPER("open");
249 if (*type == IoTYPE_PIPE) {
251 if (type[1] != IoTYPE_STD) {
253 Perl_croak(aTHX_ "Unknown open() mode '%.*s'", (int)olen, oname);
258 for (type++; isSPACE(*type); type++) ;
264 /* command is missing 19990114 */
265 if (ckWARN(WARN_PIPE))
266 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open");
270 if ((*name == '-' && name[1] == '\0') || num_svs)
272 TAINT_PROPER("piped open");
273 if (!num_svs && name[len-1] == '|') {
275 if (ckWARN(WARN_PIPE))
276 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Can't open bidirectional pipe");
282 strlcat(mode, "b", PERL_MODE_MAX);
284 strlcat(mode, "t", PERL_MODE_MAX);
292 fp = PerlProc_popen_list(mode, num_svs, svp);
295 fp = PerlProc_popen(name,mode);
299 if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) {
305 else if (*type == IoTYPE_WRONLY) {
306 TAINT_PROPER("open");
308 if (*type == IoTYPE_WRONLY) {
309 /* Two IoTYPE_WRONLYs in a row make for an IoTYPE_APPEND. */
310 mode[0] = IoTYPE(io) = IoTYPE_APPEND;
320 strlcat(mode, "b", PERL_MODE_MAX);
322 strlcat(mode, "t", PERL_MODE_MAX);
331 dodup = PERLIO_DUP_FD;
337 if (!num_svs && !*type && supplied_fp) {
338 /* "<+&" etc. is used by typemaps */
343 Perl_croak(aTHX_ "More than one argument to '%c&' open",IoTYPE(io));
346 for (; isSPACE(*type); type++) ;
347 if (num_svs && (SvIOK(*svp) || (SvPOK(*svp) && looks_like_number(*svp)))) {
351 else if (isDIGIT(*type)) {
357 thatio = sv_2io(*svp);
361 thatgv = gv_fetchpv(type,FALSE,SVt_PVIO);
362 thatio = GvIO(thatgv);
366 SETERRNO(EINVAL,SS_IVCHAN);
370 if ((that_fp = IoIFP(thatio))) {
371 /* Flush stdio buffer before dup. --mjd
372 * Unfortunately SEEK_CURing 0 seems to
373 * be optimized away on most platforms;
374 * only Solaris and Linux seem to flush
377 /* sfio fails to clear error on next
378 sfwrite, contrary to documentation.
380 if (PerlIO_seek(that_fp, 0, SEEK_CUR) == -1)
381 PerlIO_clearerr(that_fp);
383 /* On the other hand, do all platforms
384 * take gracefully to flushing a read-only
385 * filehandle? Perhaps we should do
386 * fsetpos(src)+fgetpos(dst)? --nik */
387 PerlIO_flush(that_fp);
388 fd = PerlIO_fileno(that_fp);
389 /* When dup()ing STDIN, STDOUT or STDERR
390 * explicitly set appropriate access mode */
391 if (that_fp == PerlIO_stdout()
392 || that_fp == PerlIO_stderr())
393 IoTYPE(io) = IoTYPE_WRONLY;
394 else if (that_fp == PerlIO_stdin())
395 IoTYPE(io) = IoTYPE_RDONLY;
396 /* When dup()ing a socket, say result is
398 else if (IoTYPE(thatio) == IoTYPE_SOCKET)
399 IoTYPE(io) = IoTYPE_SOCKET;
407 fp = PerlIO_fdupopen(aTHX_ that_fp, NULL, dodup);
411 fd = PerlLIO_dup(fd);
414 if (!(fp = PerlIO_openn(aTHX_ type,mode,fd,0,0,NULL,num_svs,svp))) {
423 for (; isSPACE(*type); type++) ;
424 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
427 fp = PerlIO_stdout();
428 IoTYPE(io) = IoTYPE_STD;
430 Perl_croak(aTHX_ "More than one argument to '>%c' open",IoTYPE_STD);
435 namesv = sv_2mortal(newSVpvn(type,strlen(type)));
440 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
443 if (!fp && type && *type && *type != ':' && !isIDFIRST(*type))
444 goto unknown_open_mode;
445 } /* IoTYPE_WRONLY */
446 else if (*type == IoTYPE_RDONLY) {
448 for (type++; isSPACE(*type); type++) ;
452 strlcat(mode, "b", PERL_MODE_MAX);
454 strlcat(mode, "t", PERL_MODE_MAX);
464 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
468 IoTYPE(io) = IoTYPE_STD;
470 Perl_croak(aTHX_ "More than one argument to '<%c' open",IoTYPE_STD);
475 namesv = sv_2mortal(newSVpvn(type,strlen(type)));
480 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
482 if (!fp && type && *type && *type != ':' && !isIDFIRST(*type))
483 goto unknown_open_mode;
484 } /* IoTYPE_RDONLY */
485 else if ((num_svs && /* '-|...' or '...|' */
486 type[0] == IoTYPE_STD && type[1] == IoTYPE_PIPE) ||
487 (!num_svs && tend > type+1 && tend[-1] == IoTYPE_PIPE)) {
489 type += 2; /* skip over '-|' */
493 while (tend > type && isSPACE(tend[-1]))
496 for (; isSPACE(*type); type++) ;
501 /* command is missing 19990114 */
502 if (ckWARN(WARN_PIPE))
503 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open");
507 if (!(*name == '-' && name[1] == '\0') || num_svs)
509 TAINT_PROPER("piped open");
514 strlcat(mode, "b", PERL_MODE_MAX);
516 strlcat(mode, "t", PERL_MODE_MAX);
525 fp = PerlProc_popen_list(mode,num_svs,svp);
528 fp = PerlProc_popen(name,mode);
530 IoTYPE(io) = IoTYPE_PIPE;
532 for (; isSPACE(*type); type++) ;
534 if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) {
540 else { /* layer(Args) */
542 goto unknown_open_mode;
544 IoTYPE(io) = IoTYPE_RDONLY;
546 for (; isSPACE(*name); name++) ;
551 strlcat(mode, "b", PERL_MODE_MAX);
553 strlcat(mode, "t", PERL_MODE_MAX);
561 if (*name == '-' && name[1] == '\0') {
563 IoTYPE(io) = IoTYPE_STD;
567 namesv = sv_2mortal(newSVpvn(type,strlen(type)));
572 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
577 if (ckWARN(WARN_NEWLINE) && IoTYPE(io) == IoTYPE_RDONLY && strchr(name, '\n'))
578 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
582 if (ckWARN(WARN_IO)) {
583 if ((IoTYPE(io) == IoTYPE_RDONLY) &&
584 (fp == PerlIO_stdout() || fp == PerlIO_stderr())) {
585 Perl_warner(aTHX_ packWARN(WARN_IO),
586 "Filehandle STD%s reopened as %s only for input",
587 ((fp == PerlIO_stdout()) ? "OUT" : "ERR"),
590 else if ((IoTYPE(io) == IoTYPE_WRONLY) && fp == PerlIO_stdin()) {
591 Perl_warner(aTHX_ packWARN(WARN_IO),
592 "Filehandle STDIN reopened as %s only for output",
597 fd = PerlIO_fileno(fp);
598 /* If there is no fd (e.g. PerlIO::scalar) assume it isn't a
599 * socket - this covers PerlIO::scalar - otherwise unless we "know" the
600 * type probe for socket-ness.
602 if (IoTYPE(io) && IoTYPE(io) != IoTYPE_PIPE && IoTYPE(io) != IoTYPE_STD && fd >= 0) {
603 if (PerlLIO_fstat(fd,&PL_statbuf) < 0) {
604 /* If PerlIO claims to have fd we had better be able to fstat() it. */
605 (void) PerlIO_close(fp);
609 if (S_ISSOCK(PL_statbuf.st_mode))
610 IoTYPE(io) = IoTYPE_SOCKET; /* in case a socket was passed in to us */
614 !(PL_statbuf.st_mode & S_IFMT)
618 && IoTYPE(io) != IoTYPE_WRONLY /* Dups of STD* filehandles already have */
619 && IoTYPE(io) != IoTYPE_RDONLY /* type so they aren't marked as sockets */
620 ) { /* on OS's that return 0 on fstat()ed pipe */
622 Sock_size_t buflen = sizeof tmpbuf;
623 if (PerlSock_getsockname(fd, (struct sockaddr *)tmpbuf, &buflen) >= 0
624 || errno != ENOTSOCK)
625 IoTYPE(io) = IoTYPE_SOCKET; /* some OS's return 0 on fstat()ed socket */
626 /* but some return 0 for streams too, sigh */
628 #endif /* HAS_SOCKET */
629 #endif /* !PERL_MICRO */
633 * If this is a standard handle we discard all the layer stuff
634 * and just dup the fd into whatever was on the handle before !
637 if (saveifp) { /* must use old fp? */
638 /* If fd is less that PL_maxsysfd i.e. STDIN..STDERR
639 then dup the new fileno down
642 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
643 if (saveofp != saveifp) { /* was a socket? */
644 PerlIO_close(saveofp);
648 /* Still a small can-of-worms here if (say) PerlIO::scalar
649 is assigned to (say) STDOUT - for now let dup2() fail
650 and provide the error
652 if (PerlLIO_dup2(fd, savefd) < 0) {
653 (void)PerlIO_close(fp);
657 if (savefd != PerlIO_fileno(PerlIO_stdin())) {
658 char newname[FILENAME_MAX+1];
659 if (PerlIO_getname(fp, newname)) {
660 if (fd == PerlIO_fileno(PerlIO_stdout()))
661 Perl_vmssetuserlnm(aTHX_ "SYS$OUTPUT", newname);
662 if (fd == PerlIO_fileno(PerlIO_stderr()))
663 Perl_vmssetuserlnm(aTHX_ "SYS$ERROR", newname);
669 /* PL_fdpid isn't used on Windows, so avoid this useless work.
670 * XXX Probably the same for a lot of other places. */
676 sv = *av_fetch(PL_fdpid,fd,TRUE);
677 (void)SvUPGRADE(sv, SVt_IV);
680 sv = *av_fetch(PL_fdpid,savefd,TRUE);
681 (void)SvUPGRADE(sv, SVt_IV);
688 /* need to close fp without closing underlying fd */
689 int ofd = PerlIO_fileno(fp);
690 int dupfd = PerlLIO_dup(ofd);
691 #if defined(HAS_FCNTL) && defined(F_SETFD)
692 /* Assume if we have F_SETFD we have F_GETFD */
693 int coe = fcntl(ofd,F_GETFD);
696 PerlLIO_dup2(dupfd,ofd);
697 #if defined(HAS_FCNTL) && defined(F_SETFD)
698 /* The dup trick has lost close-on-exec on ofd */
699 fcntl(ofd,F_SETFD, coe);
701 PerlLIO_close(dupfd);
708 fd = PerlIO_fileno(fp);
710 #if defined(HAS_FCNTL) && defined(F_SETFD)
712 int save_errno = errno;
713 fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
719 IoFLAGS(io) &= ~IOf_NOLINE;
721 if (IoTYPE(io) == IoTYPE_SOCKET
722 || (IoTYPE(io) == IoTYPE_WRONLY && fd >= 0 && S_ISCHR(PL_statbuf.st_mode)) ) {
724 if (*s == IoTYPE_IMPLICIT || *s == IoTYPE_NUMERIC)
727 if (!(IoOFP(io) = PerlIO_openn(aTHX_ type,s,fd,0,0,NULL,0,svp))) {
741 IoTYPE(io) = savetype;
746 Perl_nextargv(pTHX_ register GV *gv)
749 #ifndef FLEXFILENAMES
758 PL_argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO);
759 if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
760 IoFLAGS(io) &= ~IOf_START;
762 if (!PL_argvout_stack)
763 PL_argvout_stack = newAV();
764 av_push(PL_argvout_stack, SvREFCNT_inc(PL_defoutgv));
767 if (PL_filemode & (S_ISUID|S_ISGID)) {
768 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
771 (void)fchmod(PL_lastfd,PL_filemode);
773 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
780 while (av_len(GvAV(gv)) >= 0) {
782 sv = av_shift(GvAV(gv));
784 sv_setsv(GvSV(gv),sv);
785 SvSETMAGIC(GvSV(gv));
786 PL_oldname = SvPVx(GvSV(gv), oldlen);
787 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,Nullfp)) {
789 TAINT_PROPER("inplace open");
790 if (oldlen == 1 && *PL_oldname == '-') {
791 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
792 return IoIFP(GvIOp(gv));
794 #ifndef FLEXFILENAMES
795 filedev = PL_statbuf.st_dev;
796 fileino = PL_statbuf.st_ino;
798 PL_filemode = PL_statbuf.st_mode;
799 fileuid = PL_statbuf.st_uid;
800 filegid = PL_statbuf.st_gid;
801 if (!S_ISREG(PL_filemode)) {
802 if (ckWARN_d(WARN_INPLACE))
803 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
804 "Can't do inplace edit: %s is not a regular file",
810 char *star = strchr(PL_inplace, '*');
812 char *begin = PL_inplace;
813 sv_setpvn(sv, "", 0);
815 sv_catpvn(sv, begin, star - begin);
816 sv_catpvn(sv, PL_oldname, oldlen);
818 } while ((star = strchr(begin, '*')));
823 sv_catpv(sv,PL_inplace);
825 #ifndef FLEXFILENAMES
826 if ((PerlLIO_stat(SvPVX(sv),&PL_statbuf) >= 0
827 && PL_statbuf.st_dev == filedev
828 && PL_statbuf.st_ino == fileino)
830 || ((_djstat_fail_bits & _STFAIL_TRUENAME)!=0)
834 if (ckWARN_d(WARN_INPLACE))
835 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
836 "Can't do inplace edit: %"SVf" would not be unique",
843 #if !defined(DOSISH) && !defined(__CYGWIN__) && !defined(EPOC)
844 if (PerlLIO_rename(PL_oldname,SvPVX(sv)) < 0) {
845 if (ckWARN_d(WARN_INPLACE))
846 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
847 "Can't rename %s to %"SVf": %s, skipping file",
848 PL_oldname, sv, Strerror(errno) );
854 (void)PerlLIO_unlink(SvPVX(sv));
855 (void)PerlLIO_rename(PL_oldname,SvPVX(sv));
856 do_open(gv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,O_RDONLY,0,Nullfp);
859 (void)UNLINK(SvPVX(sv));
860 if (link(PL_oldname,SvPVX(sv)) < 0) {
861 if (ckWARN_d(WARN_INPLACE))
862 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
863 "Can't rename %s to %"SVf": %s, skipping file",
864 PL_oldname, sv, Strerror(errno) );
868 (void)UNLINK(PL_oldname);
872 #if !defined(DOSISH) && !defined(AMIGAOS)
873 # ifndef VMS /* Don't delete; use automatic file versioning */
874 if (UNLINK(PL_oldname) < 0) {
875 if (ckWARN_d(WARN_INPLACE))
876 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
877 "Can't remove %s: %s, skipping file",
878 PL_oldname, Strerror(errno) );
884 Perl_croak(aTHX_ "Can't do inplace edit without backup");
888 sv_setpvn(sv,">",!PL_inplace);
889 sv_catpvn(sv,PL_oldname,oldlen);
890 SETERRNO(0,0); /* in case sprintf set errno */
892 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
893 O_WRONLY|O_CREAT|O_TRUNC,0,Nullfp))
895 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
896 O_WRONLY|O_CREAT|OPEN_EXCL,0666,Nullfp))
899 if (ckWARN_d(WARN_INPLACE))
900 Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't do inplace edit on %s: %s",
901 PL_oldname, Strerror(errno) );
905 setdefout(PL_argvoutgv);
906 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
907 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
909 (void)fchmod(PL_lastfd,PL_filemode);
911 # if !(defined(WIN32) && defined(__BORLANDC__))
912 /* Borland runtime creates a readonly file! */
913 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
916 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
918 (void)fchown(PL_lastfd,fileuid,filegid);
921 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
926 return IoIFP(GvIOp(gv));
929 if (ckWARN_d(WARN_INPLACE)) {
931 if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0
932 && !S_ISREG(PL_statbuf.st_mode))
934 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
935 "Can't do inplace edit: %s is not a regular file",
939 Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't open %s: %s",
940 PL_oldname, Strerror(eno));
944 if (io && (IoFLAGS(io) & IOf_ARGV))
945 IoFLAGS(io) |= IOf_START;
947 (void)do_close(PL_argvoutgv,FALSE);
948 if (io && (IoFLAGS(io) & IOf_ARGV)
949 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
951 GV *oldout = (GV*)av_pop(PL_argvout_stack);
953 SvREFCNT_dec(oldout);
956 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
963 Perl_do_pipe(pTHX_ SV *sv, GV *rgv, GV *wgv)
982 if (PerlProc_pipe(fd) < 0)
984 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"PIPE_OPEN_MODE);
985 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"PIPE_OPEN_MODE);
986 IoOFP(rstio) = IoIFP(rstio);
987 IoIFP(wstio) = IoOFP(wstio);
988 IoTYPE(rstio) = IoTYPE_RDONLY;
989 IoTYPE(wstio) = IoTYPE_WRONLY;
990 if (!IoIFP(rstio) || !IoOFP(wstio)) {
991 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
992 else PerlLIO_close(fd[0]);
993 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
994 else PerlLIO_close(fd[1]);
998 sv_setsv(sv,&PL_sv_yes);
1002 sv_setsv(sv,&PL_sv_undef);
1007 /* explicit renamed to avoid C++ conflict -- kja */
1009 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
1016 if (!gv || SvTYPE(gv) != SVt_PVGV) {
1018 SETERRNO(EBADF,SS_IVCHAN);
1022 if (!io) { /* never opened */
1024 if (ckWARN(WARN_UNOPENED)) /* no check for closed here */
1025 report_evil_fh(gv, io, PL_op->op_type);
1026 SETERRNO(EBADF,SS_IVCHAN);
1030 retval = io_close(io, not_implicit);
1034 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1036 IoTYPE(io) = IoTYPE_CLOSED;
1041 Perl_io_close(pTHX_ IO *io, bool not_implicit)
1043 bool retval = FALSE;
1047 if (IoTYPE(io) == IoTYPE_PIPE) {
1048 status = PerlProc_pclose(IoIFP(io));
1050 STATUS_NATIVE_SET(status);
1051 retval = (STATUS_POSIX == 0);
1054 retval = (status != -1);
1057 else if (IoTYPE(io) == IoTYPE_STD)
1060 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
1061 bool prev_err = PerlIO_error(IoOFP(io));
1062 retval = (PerlIO_close(IoOFP(io)) != EOF && !prev_err);
1063 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
1066 bool prev_err = PerlIO_error(IoIFP(io));
1067 retval = (PerlIO_close(IoIFP(io)) != EOF && !prev_err);
1070 IoOFP(io) = IoIFP(io) = Nullfp;
1072 else if (not_implicit) {
1073 SETERRNO(EBADF,SS_IVCHAN);
1080 Perl_do_eof(pTHX_ GV *gv)
1089 else if (ckWARN(WARN_IO) && (IoTYPE(io) == IoTYPE_WRONLY))
1090 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1095 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
1096 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
1097 return FALSE; /* this is the most usual case */
1100 saverrno = errno; /* getc and ungetc can stomp on errno */
1101 ch = PerlIO_getc(IoIFP(io));
1103 (void)PerlIO_ungetc(IoIFP(io),ch);
1109 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
1110 if (PerlIO_get_cnt(IoIFP(io)) < -1)
1111 PerlIO_set_cnt(IoIFP(io),-1);
1113 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
1114 if (gv != PL_argvgv || !nextargv(gv)) /* get another fp handy */
1118 return TRUE; /* normal fp, definitely end of file */
1124 Perl_do_tell(pTHX_ GV *gv)
1126 register IO *io = 0;
1127 register PerlIO *fp;
1129 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1130 #ifdef ULTRIX_STDIO_BOTCH
1132 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1134 return PerlIO_tell(fp);
1136 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1137 report_evil_fh(gv, io, PL_op->op_type);
1138 SETERRNO(EBADF,RMS_IFI);
1143 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
1145 register IO *io = 0;
1146 register PerlIO *fp;
1148 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1149 #ifdef ULTRIX_STDIO_BOTCH
1151 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1153 return PerlIO_seek(fp, pos, whence) >= 0;
1155 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1156 report_evil_fh(gv, io, PL_op->op_type);
1157 SETERRNO(EBADF,RMS_IFI);
1162 Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
1164 register IO *io = 0;
1165 register PerlIO *fp;
1167 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
1168 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
1169 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1170 report_evil_fh(gv, io, PL_op->op_type);
1171 SETERRNO(EBADF,RMS_IFI);
1176 Perl_mode_from_discipline(pTHX_ SV *discp)
1178 int mode = O_BINARY;
1181 char *s = SvPV(discp,len);
1186 if (s[2] == 'a' && s[3] == 'w'
1187 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1196 if (s[2] == 'r' && s[3] == 'l' && s[4] == 'f'
1197 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1206 goto fail_discipline;
1209 else if (isSPACE(*s)) {
1216 end = strchr(s+1, ':');
1219 #ifndef PERLIO_LAYERS
1220 Perl_croak(aTHX_ "IO layers (like '%.*s') unavailable", end-s, s);
1232 Perl_do_binmode(pTHX_ PerlIO *fp, int iotype, int mode)
1234 /* The old body of this is now in non-LAYER part of perlio.c
1235 * This is a stub for any XS code which might have been calling it.
1237 const char *name = ":raw";
1238 #ifdef PERLIO_USING_CRLF
1239 if (!(mode & O_BINARY))
1242 return PerlIO_binmode(aTHX_ fp, iotype, mode, name);
1245 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE)
1246 I32 my_chsize(fd, length)
1247 I32 fd; /* file descriptor */
1248 Off_t length; /* length to set file to */
1251 /* code courtesy of William Kucharski */
1257 if (PerlLIO_fstat(fd, &filebuf) < 0)
1260 if (filebuf.st_size < length) {
1262 /* extend file length */
1264 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
1267 /* write a "0" byte */
1269 if ((PerlLIO_write(fd, "", 1)) != 1)
1273 /* truncate length */
1277 fl.l_start = length;
1278 fl.l_type = F_WRLCK; /* write lock on file space */
1281 * This relies on the UNDOCUMENTED F_FREESP argument to
1282 * fcntl(2), which truncates the file so that it ends at the
1283 * position indicated by fl.l_start.
1285 * Will minor miracles never cease?
1288 if (fcntl(fd, F_FREESP, &fl) < 0)
1296 DIE(aTHX_ "truncate not implemented");
1297 #endif /* F_FREESP */
1299 #endif /* !HAS_TRUNCATE && !HAS_CHSIZE */
1302 Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
1304 register const char *tmps;
1307 /* assuming fp is checked earlier */
1313 if (SvIOK(sv) && SvIVX(sv) != 0) {
1314 PerlIO_printf(fp, PL_ofmt, (NV)SvIVX(sv));
1315 return !PerlIO_error(fp);
1317 if ( (SvNOK(sv) && SvNVX(sv) != 0.0)
1318 || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) {
1319 PerlIO_printf(fp, PL_ofmt, SvNVX(sv));
1320 return !PerlIO_error(fp);
1323 switch (SvTYPE(sv)) {
1325 if (ckWARN(WARN_UNINITIALIZED))
1333 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
1335 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
1336 return !PerlIO_error(fp);
1340 if (PerlIO_isutf8(fp)) {
1342 sv_utf8_upgrade_flags(sv = sv_mortalcopy(sv),
1343 SV_GMAGIC|SV_UTF8_NO_ENCODING);
1345 else if (DO_UTF8(sv)) {
1346 if (!sv_utf8_downgrade((sv = sv_mortalcopy(sv)), TRUE)
1347 && ckWARN_d(WARN_UTF8))
1349 Perl_warner(aTHX_ packWARN(WARN_UTF8), "Wide character in print");
1352 tmps = SvPV(sv, len);
1355 /* To detect whether the process is about to overstep its
1356 * filesize limit we would need getrlimit(). We could then
1357 * also transparently raise the limit with setrlimit() --
1358 * but only until the system hard limit/the filesystem limit,
1359 * at which we would get EPERM. Note that when using buffered
1360 * io the write failure can be delayed until the flush/close. --jhi */
1361 if (len && (PerlIO_write(fp,tmps,len) == 0))
1363 return !PerlIO_error(fp);
1373 if (PL_op->op_flags & OPf_REF) {
1378 if (io && IoIFP(io)) {
1380 sv_setpv(PL_statname,"");
1381 PL_laststype = OP_STAT;
1382 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
1386 return PL_laststatval;
1387 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1388 report_evil_fh(gv, io, PL_op->op_type);
1390 sv_setpv(PL_statname,"");
1391 return (PL_laststatval = -1);
1394 else if (PL_op->op_private & OPpFT_STACKED) {
1395 return PL_laststatval;
1402 if (SvTYPE(sv) == SVt_PVGV) {
1406 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
1413 sv_setpvn(PL_statname, s, len);
1414 s = SvPVX(PL_statname); /* s now NUL-terminated */
1415 PL_laststype = OP_STAT;
1416 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1417 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
1418 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
1419 return PL_laststatval;
1423 static char no_prev_lstat[] = "The stat preceding -l _ wasn't an lstat";
1431 if (PL_op->op_flags & OPf_REF) {
1433 if (cGVOP_gv == PL_defgv) {
1434 if (PL_laststype != OP_LSTAT)
1435 Perl_croak(aTHX_ no_prev_lstat);
1436 return PL_laststatval;
1438 if (ckWARN(WARN_IO)) {
1439 Perl_warner(aTHX_ packWARN(WARN_IO), "Use of -l on filehandle %s",
1441 return (PL_laststatval = -1);
1444 else if (ckWARN(WARN_IO) && PL_laststype != OP_LSTAT
1445 && (PL_op->op_private & OPpFT_STACKED))
1446 Perl_croak(aTHX_ no_prev_lstat);
1448 PL_laststype = OP_LSTAT;
1452 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV && ckWARN(WARN_IO)) {
1453 Perl_warner(aTHX_ packWARN(WARN_IO), "Use of -l on filehandle %s",
1454 GvENAME((GV*) SvRV(sv)));
1455 return (PL_laststatval = -1);
1457 sv_setpv(PL_statname,SvPV(sv, n_a));
1458 PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache);
1459 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
1460 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "lstat");
1461 return PL_laststatval;
1466 Perl_do_aexec(pTHX_ SV *really, register SV **mark, register SV **sp)
1468 return do_aexec5(really, mark, sp, 0, 0);
1473 Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1474 int fd, int do_report)
1476 #ifdef MACOS_TRADITIONAL
1477 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1480 const char *tmps = Nullch;
1484 New(401,PL_Argv, sp - mark + 1, char*);
1486 while (++mark <= sp) {
1488 *a++ = SvPVx(*mark, n_a);
1494 tmps = SvPV(really, n_a);
1495 if ((!really && *PL_Argv[0] != '/') ||
1496 (really && *tmps != '/')) /* will execvp use PATH? */
1497 TAINT_ENV(); /* testing IFS here is overkill, probably */
1499 if (really && *tmps)
1500 PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
1502 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1504 if (ckWARN(WARN_EXEC))
1505 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
1506 (really ? tmps : PL_Argv[0]), Strerror(errno));
1510 PerlLIO_write(fd, (void*)&e, sizeof(int));
1520 Perl_do_execfree(pTHX)
1524 PL_Argv = Null(char **);
1532 #if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC) && !defined(MACOS_TRADITIONAL)
1535 Perl_do_exec(pTHX_ char *cmd)
1537 return do_exec3(cmd,0,0);
1541 Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report)
1546 while (*cmd && isSPACE(*cmd))
1549 /* save an extra exec if possible */
1553 char flags[PERL_FLAGS_MAX];
1554 if (strnEQ(cmd,PL_cshname,PL_cshlen) &&
1555 strnEQ(cmd+PL_cshlen," -c",3)) {
1557 strlcpy(flags, "-c", PERL_FLAGS_MAX);
1561 s = cmd+PL_cshlen+3;
1565 strlcat(flags, "f", PERL_FLAGS_MAX);
1579 if (s[-1] == '\'') {
1582 PerlProc_execl(PL_cshname,"csh", flags, ncmd, (char*)0);
1592 /* see if there are shell metacharacters in it */
1594 if (*cmd == '.' && isSPACE(cmd[1]))
1597 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1600 for (s = cmd; *s && isALNUM(*s); s++) ; /* catch VAR=val gizmo */
1604 for (s = cmd; *s; s++) {
1605 if (*s != ' ' && !isALPHA(*s) &&
1606 strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1607 if (*s == '\n' && !s[1]) {
1611 /* handle the 2>&1 construct at the end */
1612 if (*s == '>' && s[1] == '&' && s[2] == '1'
1613 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1614 && (!s[3] || isSPACE(s[3])))
1618 while (*t && isSPACE(*t))
1620 if (!*t && (PerlLIO_dup2(1,2) != -1)) {
1627 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
1633 New(402,PL_Argv, (s - cmd) / 2 + 2, char*);
1634 PL_Cmd = savepvn(cmd, s-cmd);
1636 for (s = PL_Cmd; *s;) {
1637 while (*s && isSPACE(*s)) s++;
1640 while (*s && !isSPACE(*s)) s++;
1647 PerlProc_execvp(PL_Argv[0],PL_Argv);
1649 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1656 if (ckWARN(WARN_EXEC))
1657 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
1658 PL_Argv[0], Strerror(errno));
1660 PerlLIO_write(fd, (void*)&e, sizeof(int));
1669 #endif /* OS2 || WIN32 */
1672 Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
1676 register I32 tot = 0;
1679 SV **oldmark = mark;
1682 #define APPLY_TAINT_PROPER() \
1684 if (PL_tainted) { TAINT_PROPER(what); } \
1687 /* This is a first heuristic; it doesn't catch tainting magic. */
1689 while (++mark <= sp) {
1690 if (SvTAINTED(*mark)) {
1700 APPLY_TAINT_PROPER();
1703 APPLY_TAINT_PROPER();
1705 while (++mark <= sp) {
1706 char *name = SvPVx(*mark, n_a);
1707 APPLY_TAINT_PROPER();
1708 if (PerlLIO_chmod(name, val))
1716 APPLY_TAINT_PROPER();
1717 if (sp - mark > 2) {
1718 val = SvIVx(*++mark);
1719 val2 = SvIVx(*++mark);
1720 APPLY_TAINT_PROPER();
1722 while (++mark <= sp) {
1723 char *name = SvPVx(*mark, n_a);
1724 APPLY_TAINT_PROPER();
1725 if (PerlLIO_chown(name, val, val2))
1732 XXX Should we make lchown() directly available from perl?
1733 For now, we'll let Configure test for HAS_LCHOWN, but do
1734 nothing in the core.
1740 APPLY_TAINT_PROPER();
1743 s = SvPVx(*++mark, n_a);
1745 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1747 if ((val = whichsig(s)) < 0)
1748 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
1752 APPLY_TAINT_PROPER();
1755 /* kill() doesn't do process groups (job trees?) under VMS */
1756 if (val < 0) val = -val;
1757 if (val == SIGKILL) {
1758 # include <starlet.h>
1759 /* Use native sys$delprc() to insure that target process is
1760 * deleted; supervisor-mode images don't pay attention to
1761 * CRTL's emulation of Unix-style signals and kill()
1763 while (++mark <= sp) {
1764 I32 proc = SvIVx(*mark);
1765 register unsigned long int __vmssts;
1766 APPLY_TAINT_PROPER();
1767 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1771 case SS$_NOSUCHNODE:
1772 SETERRNO(ESRCH,__vmssts);
1775 SETERRNO(EPERM,__vmssts);
1778 SETERRNO(EVMSERR,__vmssts);
1787 while (++mark <= sp) {
1788 I32 proc = SvIVx(*mark);
1789 APPLY_TAINT_PROPER();
1791 if (PerlProc_killpg(proc,val)) /* BSD */
1793 if (PerlProc_kill(-proc,val)) /* SYSV */
1799 while (++mark <= sp) {
1800 I32 proc = SvIVx(*mark);
1801 APPLY_TAINT_PROPER();
1802 if (PerlProc_kill(proc, val))
1810 APPLY_TAINT_PROPER();
1812 while (++mark <= sp) {
1813 s = SvPVx(*mark, n_a);
1814 APPLY_TAINT_PROPER();
1815 if (PL_euid || PL_unsafe) {
1819 else { /* don't let root wipe out directories without -U */
1820 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1832 APPLY_TAINT_PROPER();
1833 if (sp - mark > 2) {
1834 #if defined(I_UTIME) || defined(VMS)
1835 struct utimbuf utbuf;
1843 SV* accessed = *++mark;
1844 SV* modified = *++mark;
1845 void * utbufp = &utbuf;
1847 /* Be like C, and if both times are undefined, let the C
1848 * library figure out what to do. This usually means
1849 * "current time". */
1851 if ( accessed == &PL_sv_undef && modified == &PL_sv_undef )
1854 Zero(&utbuf, sizeof utbuf, char);
1856 utbuf.actime = (Time_t)SvNVx(accessed); /* time accessed */
1857 utbuf.modtime = (Time_t)SvNVx(modified); /* time modified */
1859 utbuf.actime = (Time_t)SvIVx(accessed); /* time accessed */
1860 utbuf.modtime = (Time_t)SvIVx(modified); /* time modified */
1863 APPLY_TAINT_PROPER();
1865 while (++mark <= sp) {
1866 char *name = SvPVx(*mark, n_a);
1867 APPLY_TAINT_PROPER();
1868 if (PerlLIO_utime(name, utbufp))
1879 #undef APPLY_TAINT_PROPER
1882 /* Do the permissions allow some operation? Assumes statcache already set. */
1883 #ifndef VMS /* VMS' cando is in vms.c */
1885 Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register Stat_t *statbufp)
1886 /* Note: we use `effective' both for uids and gids.
1887 * Here we are betting on Uid_t being equal or wider than Gid_t. */
1890 /* [Comments and code from Len Reed]
1891 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1892 * to write-protected files. The execute permission bit is set
1893 * by the Miscrosoft C library stat() function for the following:
1898 * All files and directories are readable.
1899 * Directories and special files, e.g. "CON", cannot be
1901 * [Comment by Tom Dinger -- a directory can have the write-protect
1902 * bit set in the file system, but DOS permits changes to
1903 * the directory anyway. In addition, all bets are off
1904 * here for networked software, such as Novell and
1908 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1909 * too so it will actually look into the files for magic numbers
1911 return (mode & statbufp->st_mode) ? TRUE : FALSE;
1913 #else /* ! DOSISH */
1914 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
1915 if (mode == S_IXUSR) {
1916 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1920 return TRUE; /* root reads and writes anything */
1923 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1924 if (statbufp->st_mode & mode)
1925 return TRUE; /* ok as "user" */
1927 else if (ingroup(statbufp->st_gid,effective)) {
1928 if (statbufp->st_mode & mode >> 3)
1929 return TRUE; /* ok as "group" */
1931 else if (statbufp->st_mode & mode >> 6)
1932 return TRUE; /* ok as "other" */
1934 #endif /* ! DOSISH */
1939 Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective)
1941 #ifdef MACOS_TRADITIONAL
1942 /* This is simply not correct for AppleShare, but fix it yerself. */
1945 if (testgid == (effective ? PL_egid : PL_gid))
1947 #ifdef HAS_GETGROUPS
1952 Groups_t gary[NGROUPS];
1955 anum = getgroups(NGROUPS,gary);
1957 if (gary[anum] == testgid)
1965 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1968 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
1973 key = (key_t)SvNVx(*++mark);
1974 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1975 flags = SvIVx(*++mark);
1981 return msgget(key, flags);
1985 return semget(key, n, flags);
1989 return shmget(key, n, flags);
1991 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1993 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1996 return -1; /* should never happen */
2000 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
2004 I32 id, n, cmd, infosize, getinfo;
2007 id = SvIVx(*++mark);
2008 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
2009 cmd = SvIVx(*++mark);
2012 getinfo = (cmd == IPC_STAT);
2018 if (cmd == IPC_STAT || cmd == IPC_SET)
2019 infosize = sizeof(struct msqid_ds);
2024 if (cmd == IPC_STAT || cmd == IPC_SET)
2025 infosize = sizeof(struct shmid_ds);
2031 if (cmd == IPC_STAT || cmd == IPC_SET)
2032 infosize = sizeof(struct semid_ds);
2033 else if (cmd == GETALL || cmd == SETALL)
2035 struct semid_ds semds;
2037 #ifdef EXTRA_F_IN_SEMUN_BUF
2038 semun.buff = &semds;
2042 getinfo = (cmd == GETALL);
2043 if (Semctl(id, 0, IPC_STAT, semun) == -1)
2045 infosize = semds.sem_nsems * sizeof(short);
2046 /* "short" is technically wrong but much more portable
2047 than guessing about u_?short(_t)? */
2050 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2054 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
2056 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2065 SvPV_force(astr, len);
2066 a = SvGROW(astr, infosize+1);
2070 a = SvPV(astr, len);
2071 if (len != infosize)
2072 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
2081 a = INT2PTR(char *,i); /* ouch */
2088 ret = msgctl(id, cmd, (struct msqid_ds *)a);
2094 union semun unsemds;
2096 #ifdef EXTRA_F_IN_SEMUN_BUF
2097 unsemds.buff = (struct semid_ds *)a;
2099 unsemds.buf = (struct semid_ds *)a;
2101 ret = Semctl(id, n, cmd, unsemds);
2103 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2110 ret = shmctl(id, cmd, (struct shmid_ds *)a);
2114 if (getinfo && ret >= 0) {
2115 SvCUR_set(astr, infosize);
2116 *SvEND(astr) = '\0';
2123 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
2128 I32 id, msize, flags;
2131 id = SvIVx(*++mark);
2133 flags = SvIVx(*++mark);
2134 mbuf = SvPV(mstr, len);
2135 if ((msize = len - sizeof(long)) < 0)
2136 Perl_croak(aTHX_ "Arg too short for msgsnd");
2138 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
2140 Perl_croak(aTHX_ "msgsnd not implemented");
2145 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
2151 I32 id, msize, flags, ret;
2154 id = SvIVx(*++mark);
2156 /* suppress warning when reading into undef var --jhi */
2158 sv_setpvn(mstr, "", 0);
2159 msize = SvIVx(*++mark);
2160 mtype = (long)SvIVx(*++mark);
2161 flags = SvIVx(*++mark);
2162 SvPV_force(mstr, len);
2163 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
2166 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
2168 SvCUR_set(mstr, sizeof(long)+ret);
2169 *SvEND(mstr) = '\0';
2170 #ifndef INCOMPLETE_TAINTS
2171 /* who knows who has been playing with this message? */
2177 Perl_croak(aTHX_ "msgrcv not implemented");
2182 Perl_do_semop(pTHX_ SV **mark, SV **sp)
2190 id = SvIVx(*++mark);
2192 opbuf = SvPV(opstr, opsize);
2193 if (opsize < 3 * SHORTSIZE
2194 || (opsize % (3 * SHORTSIZE))) {
2195 SETERRNO(EINVAL,LIB_INVARG);
2199 /* We can't assume that sizeof(struct sembuf) == 3 * sizeof(short). */
2201 int nsops = opsize / (3 * sizeof (short));
2203 short *ops = (short *) opbuf;
2205 struct sembuf *temps, *t;
2208 New (0, temps, nsops, struct sembuf);
2216 result = semop(id, temps, nsops);
2230 Perl_croak(aTHX_ "semop not implemented");
2235 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
2240 I32 id, mpos, msize;
2242 struct shmid_ds shmds;
2244 id = SvIVx(*++mark);
2246 mpos = SvIVx(*++mark);
2247 msize = SvIVx(*++mark);
2249 if (shmctl(id, IPC_STAT, &shmds) == -1)
2251 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
2252 SETERRNO(EFAULT,SS_ACCVIO); /* can't do as caller requested */
2255 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
2256 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2258 if (optype == OP_SHMREAD) {
2259 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2261 sv_setpvn(mstr, "", 0);
2262 SvPV_force(mstr, len);
2263 mbuf = SvGROW(mstr, msize+1);
2265 Copy(shm + mpos, mbuf, msize, char);
2266 SvCUR_set(mstr, msize);
2267 *SvEND(mstr) = '\0';
2269 #ifndef INCOMPLETE_TAINTS
2270 /* who knows who has been playing with this shared memory? */
2277 mbuf = SvPV(mstr, len);
2278 if ((n = len) > msize)
2280 Copy(mbuf, shm + mpos, n, char);
2282 memzero(shm + mpos + n, msize - n);
2286 Perl_croak(aTHX_ "shm I/O not implemented");
2290 #endif /* SYSV IPC */
2295 =for apidoc start_glob
2297 Function called by C<do_readline> to spawn a glob (or do the glob inside
2298 perl on VMS). This code used to be inline, but now perl uses C<File::Glob>
2299 this glob starter is only used by miniperl during the build process.
2300 Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up.
2306 Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
2308 SV *tmpcmd = NEWSV(55, 0);
2312 #ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
2313 /* since spawning off a process is a real performance hit */
2315 #include <descrip.h>
2316 #include <lib$routines.h>
2319 char rslt[NAM$C_MAXRSS+1+sizeof(unsigned short int)] = {'\0','\0'};
2320 char vmsspec[NAM$C_MAXRSS+1];
2321 char *rstr = rslt + sizeof(unsigned short int), *begin, *end, *cp;
2322 $DESCRIPTOR(dfltdsc,"SYS$DISK:[]*.*;");
2325 struct dsc$descriptor_s wilddsc
2326 = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
2327 struct dsc$descriptor_vs rsdsc
2328 = {sizeof rslt, DSC$K_DTYPE_VT, DSC$K_CLASS_VS, rslt};
2329 unsigned long int cxt = 0, sts = 0, ok = 1, hasdir = 0, hasver = 0, isunix = 0;
2331 /* We could find out if there's an explicit dev/dir or version
2332 by peeking into lib$find_file's internal context at
2333 ((struct NAM *)((struct FAB *)cxt)->fab$l_nam)->nam$l_fnb
2334 but that's unsupported, so I don't want to do it now and
2335 have it bite someone in the future. */
2336 cp = SvPV(tmpglob,i);
2338 if (cp[i] == ';') hasver = 1;
2340 if (sts) hasver = 1;
2344 hasdir = isunix = 1;
2347 if (cp[i] == ']' || cp[i] == '>' || cp[i] == ':') {
2352 if ((tmpfp = PerlIO_tmpfile()) != NULL) {
2354 if (!PerlLIO_stat(SvPVX(tmpglob),&st) && S_ISDIR(st.st_mode))
2355 ok = ((wilddsc.dsc$a_pointer = tovmspath(SvPVX(tmpglob),vmsspec)) != NULL);
2356 else ok = ((wilddsc.dsc$a_pointer = tovmsspec(SvPVX(tmpglob),vmsspec)) != NULL);
2357 if (ok) wilddsc.dsc$w_length = (unsigned short int) strlen(wilddsc.dsc$a_pointer);
2358 for (cp=wilddsc.dsc$a_pointer; ok && cp && *cp; cp++)
2359 if (*cp == '?') *cp = '%'; /* VMS style single-char wildcard */
2360 while (ok && ((sts = lib$find_file(&wilddsc,&rsdsc,&cxt,
2361 &dfltdsc,NULL,NULL,NULL))&1)) {
2362 /* with varying string, 1st word of buffer contains result length */
2363 end = rstr + *((unsigned short int*)rslt);
2364 if (!hasver) while (*end != ';' && end > rstr) end--;
2365 *(end++) = '\n'; *end = '\0';
2366 for (cp = rstr; *cp; cp++) *cp = _tolower(*cp);
2368 if (isunix) trim_unixpath(rstr,SvPVX(tmpglob),1);
2373 while (*(--begin) != ']' && *begin != '>') ;
2376 ok = (PerlIO_puts(tmpfp,begin) != EOF);
2378 if (cxt) (void)lib$find_file_end(&cxt);
2379 if (ok && sts != RMS$_NMF &&
2380 sts != RMS$_DNF && sts != RMS_FNF) ok = 0;
2383 SETERRNO((sts == RMS$_SYN ? EINVAL : EVMSERR),sts);
2385 PerlIO_close(tmpfp);
2389 PerlIO_rewind(tmpfp);
2390 IoTYPE(io) = IoTYPE_RDONLY;
2391 IoIFP(io) = fp = tmpfp;
2392 IoFLAGS(io) &= ~IOf_UNTAINT; /* maybe redundant */
2397 #ifdef MACOS_TRADITIONAL
2398 sv_setpv(tmpcmd, "glob ");
2399 sv_catsv(tmpcmd, tmpglob);
2400 sv_catpv(tmpcmd, " |");
2404 sv_setpv(tmpcmd, "for a in ");
2405 sv_catsv(tmpcmd, tmpglob);
2406 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
2409 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
2410 sv_catsv(tmpcmd, tmpglob);
2412 sv_setpv(tmpcmd, "perlglob ");
2413 sv_catsv(tmpcmd, tmpglob);
2414 sv_catpv(tmpcmd, " |");
2419 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
2420 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
2421 sv_catsv(tmpcmd, tmpglob);
2422 sv_catpv(tmpcmd, "' 2>/dev/null |");
2424 sv_setpv(tmpcmd, "echo ");
2425 sv_catsv(tmpcmd, tmpglob);
2427 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
2429 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
2432 #endif /* !DOSISH */
2433 #endif /* MACOS_TRADITIONAL */
2434 (void)do_open(PL_last_in_gv, SvPVX(tmpcmd), SvCUR(tmpcmd),
2435 FALSE, O_RDONLY, 0, Nullfp);