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) && defined(F_FREESP)
1246 /* code courtesy of William Kucharski */
1249 I32 my_chsize(fd, length)
1250 I32 fd; /* file descriptor */
1251 Off_t length; /* length to set file to */
1256 if (PerlLIO_fstat(fd, &filebuf) < 0)
1259 if (filebuf.st_size < length) {
1261 /* extend file length */
1263 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
1266 /* write a "0" byte */
1268 if ((PerlLIO_write(fd, "", 1)) != 1)
1272 /* truncate length */
1276 fl.l_start = length;
1277 fl.l_type = F_WRLCK; /* write lock on file space */
1280 * This relies on the UNDOCUMENTED F_FREESP argument to
1281 * fcntl(2), which truncates the file so that it ends at the
1282 * position indicated by fl.l_start.
1284 * Will minor miracles never cease?
1287 if (fcntl(fd, F_FREESP, &fl) < 0)
1294 #endif /* F_FREESP */
1297 Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
1299 register const char *tmps;
1302 /* assuming fp is checked earlier */
1308 if (SvIOK(sv) && SvIVX(sv) != 0) {
1309 PerlIO_printf(fp, PL_ofmt, (NV)SvIVX(sv));
1310 return !PerlIO_error(fp);
1312 if ( (SvNOK(sv) && SvNVX(sv) != 0.0)
1313 || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) {
1314 PerlIO_printf(fp, PL_ofmt, SvNVX(sv));
1315 return !PerlIO_error(fp);
1318 switch (SvTYPE(sv)) {
1320 if (ckWARN(WARN_UNINITIALIZED))
1328 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
1330 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
1331 return !PerlIO_error(fp);
1335 if (PerlIO_isutf8(fp)) {
1337 sv_utf8_upgrade_flags(sv = sv_mortalcopy(sv),
1338 SV_GMAGIC|SV_UTF8_NO_ENCODING);
1340 else if (DO_UTF8(sv)) {
1341 if (!sv_utf8_downgrade((sv = sv_mortalcopy(sv)), TRUE)
1342 && ckWARN_d(WARN_UTF8))
1344 Perl_warner(aTHX_ packWARN(WARN_UTF8), "Wide character in print");
1347 tmps = SvPV(sv, len);
1350 /* To detect whether the process is about to overstep its
1351 * filesize limit we would need getrlimit(). We could then
1352 * also transparently raise the limit with setrlimit() --
1353 * but only until the system hard limit/the filesystem limit,
1354 * at which we would get EPERM. Note that when using buffered
1355 * io the write failure can be delayed until the flush/close. --jhi */
1356 if (len && (PerlIO_write(fp,tmps,len) == 0))
1358 return !PerlIO_error(fp);
1368 if (PL_op->op_flags & OPf_REF) {
1373 if (io && IoIFP(io)) {
1375 sv_setpv(PL_statname,"");
1376 PL_laststype = OP_STAT;
1377 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
1381 return PL_laststatval;
1382 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1383 report_evil_fh(gv, io, PL_op->op_type);
1385 sv_setpv(PL_statname,"");
1386 return (PL_laststatval = -1);
1389 else if (PL_op->op_private & OPpFT_STACKED) {
1390 return PL_laststatval;
1397 if (SvTYPE(sv) == SVt_PVGV) {
1401 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
1408 sv_setpvn(PL_statname, s, len);
1409 s = SvPVX(PL_statname); /* s now NUL-terminated */
1410 PL_laststype = OP_STAT;
1411 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1412 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
1413 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
1414 return PL_laststatval;
1418 static char no_prev_lstat[] = "The stat preceding -l _ wasn't an lstat";
1426 if (PL_op->op_flags & OPf_REF) {
1428 if (cGVOP_gv == PL_defgv) {
1429 if (PL_laststype != OP_LSTAT)
1430 Perl_croak(aTHX_ no_prev_lstat);
1431 return PL_laststatval;
1433 if (ckWARN(WARN_IO)) {
1434 Perl_warner(aTHX_ packWARN(WARN_IO), "Use of -l on filehandle %s",
1436 return (PL_laststatval = -1);
1439 else if (ckWARN(WARN_IO) && PL_laststype != OP_LSTAT
1440 && (PL_op->op_private & OPpFT_STACKED))
1441 Perl_croak(aTHX_ no_prev_lstat);
1443 PL_laststype = OP_LSTAT;
1447 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV && ckWARN(WARN_IO)) {
1448 Perl_warner(aTHX_ packWARN(WARN_IO), "Use of -l on filehandle %s",
1449 GvENAME((GV*) SvRV(sv)));
1450 return (PL_laststatval = -1);
1452 sv_setpv(PL_statname,SvPV(sv, n_a));
1453 PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache);
1454 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
1455 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "lstat");
1456 return PL_laststatval;
1461 Perl_do_aexec(pTHX_ SV *really, register SV **mark, register SV **sp)
1463 return do_aexec5(really, mark, sp, 0, 0);
1468 Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1469 int fd, int do_report)
1471 #ifdef MACOS_TRADITIONAL
1472 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1475 const char *tmps = Nullch;
1479 New(401,PL_Argv, sp - mark + 1, char*);
1481 while (++mark <= sp) {
1483 *a++ = SvPVx(*mark, n_a);
1489 tmps = SvPV(really, n_a);
1490 if ((!really && *PL_Argv[0] != '/') ||
1491 (really && *tmps != '/')) /* will execvp use PATH? */
1492 TAINT_ENV(); /* testing IFS here is overkill, probably */
1494 if (really && *tmps)
1495 PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
1497 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1499 if (ckWARN(WARN_EXEC))
1500 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
1501 (really ? tmps : PL_Argv[0]), Strerror(errno));
1505 PerlLIO_write(fd, (void*)&e, sizeof(int));
1515 Perl_do_execfree(pTHX)
1519 PL_Argv = Null(char **);
1527 #if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC) && !defined(MACOS_TRADITIONAL)
1530 Perl_do_exec(pTHX_ char *cmd)
1532 return do_exec3(cmd,0,0);
1536 Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report)
1541 while (*cmd && isSPACE(*cmd))
1544 /* save an extra exec if possible */
1548 char flags[PERL_FLAGS_MAX];
1549 if (strnEQ(cmd,PL_cshname,PL_cshlen) &&
1550 strnEQ(cmd+PL_cshlen," -c",3)) {
1552 strlcpy(flags, "-c", PERL_FLAGS_MAX);
1556 s = cmd+PL_cshlen+3;
1560 strlcat(flags, "f", PERL_FLAGS_MAX);
1574 if (s[-1] == '\'') {
1577 PerlProc_execl(PL_cshname,"csh", flags, ncmd, (char*)0);
1587 /* see if there are shell metacharacters in it */
1589 if (*cmd == '.' && isSPACE(cmd[1]))
1592 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1595 for (s = cmd; *s && isALNUM(*s); s++) ; /* catch VAR=val gizmo */
1599 for (s = cmd; *s; s++) {
1600 if (*s != ' ' && !isALPHA(*s) &&
1601 strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1602 if (*s == '\n' && !s[1]) {
1606 /* handle the 2>&1 construct at the end */
1607 if (*s == '>' && s[1] == '&' && s[2] == '1'
1608 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1609 && (!s[3] || isSPACE(s[3])))
1613 while (*t && isSPACE(*t))
1615 if (!*t && (PerlLIO_dup2(1,2) != -1)) {
1622 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
1628 New(402,PL_Argv, (s - cmd) / 2 + 2, char*);
1629 PL_Cmd = savepvn(cmd, s-cmd);
1631 for (s = PL_Cmd; *s;) {
1632 while (*s && isSPACE(*s)) s++;
1635 while (*s && !isSPACE(*s)) s++;
1642 PerlProc_execvp(PL_Argv[0],PL_Argv);
1644 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1651 if (ckWARN(WARN_EXEC))
1652 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
1653 PL_Argv[0], Strerror(errno));
1655 PerlLIO_write(fd, (void*)&e, sizeof(int));
1664 #endif /* OS2 || WIN32 */
1667 Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
1671 register I32 tot = 0;
1674 SV **oldmark = mark;
1677 #define APPLY_TAINT_PROPER() \
1679 if (PL_tainted) { TAINT_PROPER(what); } \
1682 /* This is a first heuristic; it doesn't catch tainting magic. */
1684 while (++mark <= sp) {
1685 if (SvTAINTED(*mark)) {
1695 APPLY_TAINT_PROPER();
1698 APPLY_TAINT_PROPER();
1700 while (++mark <= sp) {
1701 char *name = SvPVx(*mark, n_a);
1702 APPLY_TAINT_PROPER();
1703 if (PerlLIO_chmod(name, val))
1711 APPLY_TAINT_PROPER();
1712 if (sp - mark > 2) {
1713 val = SvIVx(*++mark);
1714 val2 = SvIVx(*++mark);
1715 APPLY_TAINT_PROPER();
1717 while (++mark <= sp) {
1718 char *name = SvPVx(*mark, n_a);
1719 APPLY_TAINT_PROPER();
1720 if (PerlLIO_chown(name, val, val2))
1727 XXX Should we make lchown() directly available from perl?
1728 For now, we'll let Configure test for HAS_LCHOWN, but do
1729 nothing in the core.
1735 APPLY_TAINT_PROPER();
1738 s = SvPVx(*++mark, n_a);
1740 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1742 if ((val = whichsig(s)) < 0)
1743 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
1747 APPLY_TAINT_PROPER();
1750 /* kill() doesn't do process groups (job trees?) under VMS */
1751 if (val < 0) val = -val;
1752 if (val == SIGKILL) {
1753 # include <starlet.h>
1754 /* Use native sys$delprc() to insure that target process is
1755 * deleted; supervisor-mode images don't pay attention to
1756 * CRTL's emulation of Unix-style signals and kill()
1758 while (++mark <= sp) {
1759 I32 proc = SvIVx(*mark);
1760 register unsigned long int __vmssts;
1761 APPLY_TAINT_PROPER();
1762 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1766 case SS$_NOSUCHNODE:
1767 SETERRNO(ESRCH,__vmssts);
1770 SETERRNO(EPERM,__vmssts);
1773 SETERRNO(EVMSERR,__vmssts);
1782 while (++mark <= sp) {
1783 I32 proc = SvIVx(*mark);
1784 APPLY_TAINT_PROPER();
1786 if (PerlProc_killpg(proc,val)) /* BSD */
1788 if (PerlProc_kill(-proc,val)) /* SYSV */
1794 while (++mark <= sp) {
1795 I32 proc = SvIVx(*mark);
1796 APPLY_TAINT_PROPER();
1797 if (PerlProc_kill(proc, val))
1805 APPLY_TAINT_PROPER();
1807 while (++mark <= sp) {
1808 s = SvPVx(*mark, n_a);
1809 APPLY_TAINT_PROPER();
1810 if (PL_euid || PL_unsafe) {
1814 else { /* don't let root wipe out directories without -U */
1815 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1827 APPLY_TAINT_PROPER();
1828 if (sp - mark > 2) {
1829 #if defined(I_UTIME) || defined(VMS)
1830 struct utimbuf utbuf;
1838 SV* accessed = *++mark;
1839 SV* modified = *++mark;
1840 void * utbufp = &utbuf;
1842 /* Be like C, and if both times are undefined, let the C
1843 * library figure out what to do. This usually means
1844 * "current time". */
1846 if ( accessed == &PL_sv_undef && modified == &PL_sv_undef )
1849 Zero(&utbuf, sizeof utbuf, char);
1851 utbuf.actime = (Time_t)SvNVx(accessed); /* time accessed */
1852 utbuf.modtime = (Time_t)SvNVx(modified); /* time modified */
1854 utbuf.actime = (Time_t)SvIVx(accessed); /* time accessed */
1855 utbuf.modtime = (Time_t)SvIVx(modified); /* time modified */
1858 APPLY_TAINT_PROPER();
1860 while (++mark <= sp) {
1861 char *name = SvPVx(*mark, n_a);
1862 APPLY_TAINT_PROPER();
1863 if (PerlLIO_utime(name, utbufp))
1874 #undef APPLY_TAINT_PROPER
1877 /* Do the permissions allow some operation? Assumes statcache already set. */
1878 #ifndef VMS /* VMS' cando is in vms.c */
1880 Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register Stat_t *statbufp)
1881 /* Note: we use `effective' both for uids and gids.
1882 * Here we are betting on Uid_t being equal or wider than Gid_t. */
1885 /* [Comments and code from Len Reed]
1886 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1887 * to write-protected files. The execute permission bit is set
1888 * by the Miscrosoft C library stat() function for the following:
1893 * All files and directories are readable.
1894 * Directories and special files, e.g. "CON", cannot be
1896 * [Comment by Tom Dinger -- a directory can have the write-protect
1897 * bit set in the file system, but DOS permits changes to
1898 * the directory anyway. In addition, all bets are off
1899 * here for networked software, such as Novell and
1903 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1904 * too so it will actually look into the files for magic numbers
1906 return (mode & statbufp->st_mode) ? TRUE : FALSE;
1908 #else /* ! DOSISH */
1909 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
1910 if (mode == S_IXUSR) {
1911 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1915 return TRUE; /* root reads and writes anything */
1918 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1919 if (statbufp->st_mode & mode)
1920 return TRUE; /* ok as "user" */
1922 else if (ingroup(statbufp->st_gid,effective)) {
1923 if (statbufp->st_mode & mode >> 3)
1924 return TRUE; /* ok as "group" */
1926 else if (statbufp->st_mode & mode >> 6)
1927 return TRUE; /* ok as "other" */
1929 #endif /* ! DOSISH */
1934 Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective)
1936 #ifdef MACOS_TRADITIONAL
1937 /* This is simply not correct for AppleShare, but fix it yerself. */
1940 if (testgid == (effective ? PL_egid : PL_gid))
1942 #ifdef HAS_GETGROUPS
1947 Groups_t gary[NGROUPS];
1950 anum = getgroups(NGROUPS,gary);
1952 if (gary[anum] == testgid)
1960 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1963 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
1968 key = (key_t)SvNVx(*++mark);
1969 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1970 flags = SvIVx(*++mark);
1976 return msgget(key, flags);
1980 return semget(key, n, flags);
1984 return shmget(key, n, flags);
1986 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1988 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1991 return -1; /* should never happen */
1995 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
1999 I32 id, n, cmd, infosize, getinfo;
2002 id = SvIVx(*++mark);
2003 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
2004 cmd = SvIVx(*++mark);
2007 getinfo = (cmd == IPC_STAT);
2013 if (cmd == IPC_STAT || cmd == IPC_SET)
2014 infosize = sizeof(struct msqid_ds);
2019 if (cmd == IPC_STAT || cmd == IPC_SET)
2020 infosize = sizeof(struct shmid_ds);
2026 if (cmd == IPC_STAT || cmd == IPC_SET)
2027 infosize = sizeof(struct semid_ds);
2028 else if (cmd == GETALL || cmd == SETALL)
2030 struct semid_ds semds;
2032 #ifdef EXTRA_F_IN_SEMUN_BUF
2033 semun.buff = &semds;
2037 getinfo = (cmd == GETALL);
2038 if (Semctl(id, 0, IPC_STAT, semun) == -1)
2040 infosize = semds.sem_nsems * sizeof(short);
2041 /* "short" is technically wrong but much more portable
2042 than guessing about u_?short(_t)? */
2045 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2049 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
2051 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2060 SvPV_force(astr, len);
2061 a = SvGROW(astr, infosize+1);
2065 a = SvPV(astr, len);
2066 if (len != infosize)
2067 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
2076 a = INT2PTR(char *,i); /* ouch */
2083 ret = msgctl(id, cmd, (struct msqid_ds *)a);
2089 union semun unsemds;
2091 #ifdef EXTRA_F_IN_SEMUN_BUF
2092 unsemds.buff = (struct semid_ds *)a;
2094 unsemds.buf = (struct semid_ds *)a;
2096 ret = Semctl(id, n, cmd, unsemds);
2098 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2105 ret = shmctl(id, cmd, (struct shmid_ds *)a);
2109 if (getinfo && ret >= 0) {
2110 SvCUR_set(astr, infosize);
2111 *SvEND(astr) = '\0';
2118 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
2123 I32 id, msize, flags;
2126 id = SvIVx(*++mark);
2128 flags = SvIVx(*++mark);
2129 mbuf = SvPV(mstr, len);
2130 if ((msize = len - sizeof(long)) < 0)
2131 Perl_croak(aTHX_ "Arg too short for msgsnd");
2133 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
2135 Perl_croak(aTHX_ "msgsnd not implemented");
2140 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
2146 I32 id, msize, flags, ret;
2149 id = SvIVx(*++mark);
2151 /* suppress warning when reading into undef var --jhi */
2153 sv_setpvn(mstr, "", 0);
2154 msize = SvIVx(*++mark);
2155 mtype = (long)SvIVx(*++mark);
2156 flags = SvIVx(*++mark);
2157 SvPV_force(mstr, len);
2158 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
2161 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
2163 SvCUR_set(mstr, sizeof(long)+ret);
2164 *SvEND(mstr) = '\0';
2165 #ifndef INCOMPLETE_TAINTS
2166 /* who knows who has been playing with this message? */
2172 Perl_croak(aTHX_ "msgrcv not implemented");
2177 Perl_do_semop(pTHX_ SV **mark, SV **sp)
2185 id = SvIVx(*++mark);
2187 opbuf = SvPV(opstr, opsize);
2188 if (opsize < 3 * SHORTSIZE
2189 || (opsize % (3 * SHORTSIZE))) {
2190 SETERRNO(EINVAL,LIB_INVARG);
2194 /* We can't assume that sizeof(struct sembuf) == 3 * sizeof(short). */
2196 int nsops = opsize / (3 * sizeof (short));
2198 short *ops = (short *) opbuf;
2200 struct sembuf *temps, *t;
2203 New (0, temps, nsops, struct sembuf);
2211 result = semop(id, temps, nsops);
2225 Perl_croak(aTHX_ "semop not implemented");
2230 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
2235 I32 id, mpos, msize;
2237 struct shmid_ds shmds;
2239 id = SvIVx(*++mark);
2241 mpos = SvIVx(*++mark);
2242 msize = SvIVx(*++mark);
2244 if (shmctl(id, IPC_STAT, &shmds) == -1)
2246 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
2247 SETERRNO(EFAULT,SS_ACCVIO); /* can't do as caller requested */
2250 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
2251 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2253 if (optype == OP_SHMREAD) {
2254 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2256 sv_setpvn(mstr, "", 0);
2257 SvPV_force(mstr, len);
2258 mbuf = SvGROW(mstr, msize+1);
2260 Copy(shm + mpos, mbuf, msize, char);
2261 SvCUR_set(mstr, msize);
2262 *SvEND(mstr) = '\0';
2264 #ifndef INCOMPLETE_TAINTS
2265 /* who knows who has been playing with this shared memory? */
2272 mbuf = SvPV(mstr, len);
2273 if ((n = len) > msize)
2275 Copy(mbuf, shm + mpos, n, char);
2277 memzero(shm + mpos + n, msize - n);
2281 Perl_croak(aTHX_ "shm I/O not implemented");
2285 #endif /* SYSV IPC */
2290 =for apidoc start_glob
2292 Function called by C<do_readline> to spawn a glob (or do the glob inside
2293 perl on VMS). This code used to be inline, but now perl uses C<File::Glob>
2294 this glob starter is only used by miniperl during the build process.
2295 Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up.
2301 Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
2303 SV *tmpcmd = NEWSV(55, 0);
2307 #ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
2308 /* since spawning off a process is a real performance hit */
2310 #include <descrip.h>
2311 #include <lib$routines.h>
2314 char rslt[NAM$C_MAXRSS+1+sizeof(unsigned short int)] = {'\0','\0'};
2315 char vmsspec[NAM$C_MAXRSS+1];
2316 char *rstr = rslt + sizeof(unsigned short int), *begin, *end, *cp;
2317 $DESCRIPTOR(dfltdsc,"SYS$DISK:[]*.*;");
2320 struct dsc$descriptor_s wilddsc
2321 = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
2322 struct dsc$descriptor_vs rsdsc
2323 = {sizeof rslt, DSC$K_DTYPE_VT, DSC$K_CLASS_VS, rslt};
2324 unsigned long int cxt = 0, sts = 0, ok = 1, hasdir = 0, hasver = 0, isunix = 0;
2326 /* We could find out if there's an explicit dev/dir or version
2327 by peeking into lib$find_file's internal context at
2328 ((struct NAM *)((struct FAB *)cxt)->fab$l_nam)->nam$l_fnb
2329 but that's unsupported, so I don't want to do it now and
2330 have it bite someone in the future. */
2331 cp = SvPV(tmpglob,i);
2333 if (cp[i] == ';') hasver = 1;
2335 if (sts) hasver = 1;
2339 hasdir = isunix = 1;
2342 if (cp[i] == ']' || cp[i] == '>' || cp[i] == ':') {
2347 if ((tmpfp = PerlIO_tmpfile()) != NULL) {
2349 if (!PerlLIO_stat(SvPVX(tmpglob),&st) && S_ISDIR(st.st_mode))
2350 ok = ((wilddsc.dsc$a_pointer = tovmspath(SvPVX(tmpglob),vmsspec)) != NULL);
2351 else ok = ((wilddsc.dsc$a_pointer = tovmsspec(SvPVX(tmpglob),vmsspec)) != NULL);
2352 if (ok) wilddsc.dsc$w_length = (unsigned short int) strlen(wilddsc.dsc$a_pointer);
2353 for (cp=wilddsc.dsc$a_pointer; ok && cp && *cp; cp++)
2354 if (*cp == '?') *cp = '%'; /* VMS style single-char wildcard */
2355 while (ok && ((sts = lib$find_file(&wilddsc,&rsdsc,&cxt,
2356 &dfltdsc,NULL,NULL,NULL))&1)) {
2357 /* with varying string, 1st word of buffer contains result length */
2358 end = rstr + *((unsigned short int*)rslt);
2359 if (!hasver) while (*end != ';' && end > rstr) end--;
2360 *(end++) = '\n'; *end = '\0';
2361 for (cp = rstr; *cp; cp++) *cp = _tolower(*cp);
2363 if (isunix) trim_unixpath(rstr,SvPVX(tmpglob),1);
2368 while (*(--begin) != ']' && *begin != '>') ;
2371 ok = (PerlIO_puts(tmpfp,begin) != EOF);
2373 if (cxt) (void)lib$find_file_end(&cxt);
2374 if (ok && sts != RMS$_NMF &&
2375 sts != RMS$_DNF && sts != RMS_FNF) ok = 0;
2378 SETERRNO((sts == RMS$_SYN ? EINVAL : EVMSERR),sts);
2380 PerlIO_close(tmpfp);
2384 PerlIO_rewind(tmpfp);
2385 IoTYPE(io) = IoTYPE_RDONLY;
2386 IoIFP(io) = fp = tmpfp;
2387 IoFLAGS(io) &= ~IOf_UNTAINT; /* maybe redundant */
2392 #ifdef MACOS_TRADITIONAL
2393 sv_setpv(tmpcmd, "glob ");
2394 sv_catsv(tmpcmd, tmpglob);
2395 sv_catpv(tmpcmd, " |");
2399 sv_setpv(tmpcmd, "for a in ");
2400 sv_catsv(tmpcmd, tmpglob);
2401 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
2404 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
2405 sv_catsv(tmpcmd, tmpglob);
2407 sv_setpv(tmpcmd, "perlglob ");
2408 sv_catsv(tmpcmd, tmpglob);
2409 sv_catpv(tmpcmd, " |");
2414 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
2415 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
2416 sv_catsv(tmpcmd, tmpglob);
2417 sv_catpv(tmpcmd, "' 2>/dev/null |");
2419 sv_setpv(tmpcmd, "echo ");
2420 sv_catsv(tmpcmd, tmpglob);
2422 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
2424 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
2427 #endif /* !DOSISH */
2428 #endif /* MACOS_TRADITIONAL */
2429 (void)do_open(PL_last_in_gv, SvPVX(tmpcmd), SvCUR(tmpcmd),
2430 FALSE, O_RDONLY, 0, Nullfp);