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 */
152 #ifdef O_APPEND /* Not fully portable. */
155 #ifdef O_TRUNC /* Not fully portable. */
160 O_WRONLY|O_RDWR|O_CREAT|appendtrunc;
164 Perl_croak(aTHX_ "panic: sysopen with multiple args");
172 It might be (in OS/390 and Mac OS Classic it is)
178 This means that simple & with O_RDWR would look
179 like O_RDONLY is present. Therefore we have to
182 if ((ismodifying = (rawmode & modifyingmode))) {
183 if ((ismodifying & O_WRONLY) == O_WRONLY ||
184 (ismodifying & O_RDWR) == O_RDWR ||
185 (ismodifying & (O_CREAT|appendtrunc)))
186 TAINT_PROPER("sysopen");
188 mode[ix++] = IoTYPE_NUMERIC; /* Marker to openn to use numeric "sysopen" */
190 #if defined(USE_64_BIT_RAWIO) && defined(O_LARGEFILE)
191 rawmode |= O_LARGEFILE; /* Transparently largefiley. */
194 IoTYPE(io) = PerlIO_intmode2str(rawmode, &mode[ix], &writing);
196 namesv = sv_2mortal(newSVpvn(name,strlen(name)));
200 fp = PerlIO_openn(aTHX_ type, mode, -1, rawmode, rawperm, NULL, num_svs, svp);
203 /* Regular (non-sys) open */
208 PerlIO *that_fp = NULL;
210 type = savepvn(name, len);
214 /* Lose leading and trailing white space */
216 for (; isSPACE(*type); type++) ;
217 while (tend > type && isSPACE(tend[-1]))
221 /* New style explicit name, type is just mode and layer info */
224 if (SvROK(*svp) && !strchr(name,'&')) {
226 Perl_warner(aTHX_ packWARN(WARN_IO),
227 "Can't open a reference");
228 SETERRNO(EINVAL, LIB_INVARG);
231 #endif /* USE_STDIO */
232 name = SvOK(*svp) ? SvPV(*svp, l) : "";
234 name = savepvn(name, len);
242 if ((*type == IoTYPE_RDWR) && /* scary */
243 (*(type+1) == IoTYPE_RDONLY || *(type+1) == IoTYPE_WRONLY) &&
244 ((!num_svs || (tend > type+1 && tend[-1] != IoTYPE_PIPE)))) {
245 TAINT_PROPER("open");
250 if (*type == IoTYPE_PIPE) {
252 if (type[1] != IoTYPE_STD) {
254 Perl_croak(aTHX_ "Unknown open() mode '%.*s'", (int)olen, oname);
259 for (type++; isSPACE(*type); type++) ;
265 /* command is missing 19990114 */
266 if (ckWARN(WARN_PIPE))
267 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open");
271 if ((*name == '-' && name[1] == '\0') || num_svs)
273 TAINT_PROPER("piped open");
274 if (!num_svs && name[len-1] == '|') {
276 if (ckWARN(WARN_PIPE))
277 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Can't open bidirectional pipe");
283 strlcat(mode, "b", PERL_MODE_MAX);
285 strlcat(mode, "t", PERL_MODE_MAX);
293 fp = PerlProc_popen_list(mode, num_svs, svp);
296 fp = PerlProc_popen(name,mode);
300 if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) {
306 else if (*type == IoTYPE_WRONLY) {
307 TAINT_PROPER("open");
309 if (*type == IoTYPE_WRONLY) {
310 /* Two IoTYPE_WRONLYs in a row make for an IoTYPE_APPEND. */
311 mode[0] = IoTYPE(io) = IoTYPE_APPEND;
321 strlcat(mode, "b", PERL_MODE_MAX);
323 strlcat(mode, "t", PERL_MODE_MAX);
332 dodup = PERLIO_DUP_FD;
338 if (!num_svs && !*type && supplied_fp) {
339 /* "<+&" etc. is used by typemaps */
344 Perl_croak(aTHX_ "More than one argument to '%c&' open",IoTYPE(io));
347 for (; isSPACE(*type); type++) ;
348 if (num_svs && (SvIOK(*svp) || (SvPOK(*svp) && looks_like_number(*svp)))) {
352 else if (isDIGIT(*type)) {
358 thatio = sv_2io(*svp);
362 thatgv = gv_fetchpv(type,FALSE,SVt_PVIO);
363 thatio = GvIO(thatgv);
367 SETERRNO(EINVAL,SS_IVCHAN);
371 if ((that_fp = IoIFP(thatio))) {
372 /* Flush stdio buffer before dup. --mjd
373 * Unfortunately SEEK_CURing 0 seems to
374 * be optimized away on most platforms;
375 * only Solaris and Linux seem to flush
378 /* sfio fails to clear error on next
379 sfwrite, contrary to documentation.
381 if (PerlIO_seek(that_fp, 0, SEEK_CUR) == -1)
382 PerlIO_clearerr(that_fp);
384 /* On the other hand, do all platforms
385 * take gracefully to flushing a read-only
386 * filehandle? Perhaps we should do
387 * fsetpos(src)+fgetpos(dst)? --nik */
388 PerlIO_flush(that_fp);
389 fd = PerlIO_fileno(that_fp);
390 /* When dup()ing STDIN, STDOUT or STDERR
391 * explicitly set appropriate access mode */
392 if (that_fp == PerlIO_stdout()
393 || that_fp == PerlIO_stderr())
394 IoTYPE(io) = IoTYPE_WRONLY;
395 else if (that_fp == PerlIO_stdin())
396 IoTYPE(io) = IoTYPE_RDONLY;
397 /* When dup()ing a socket, say result is
399 else if (IoTYPE(thatio) == IoTYPE_SOCKET)
400 IoTYPE(io) = IoTYPE_SOCKET;
408 fp = PerlIO_fdupopen(aTHX_ that_fp, NULL, dodup);
412 fd = PerlLIO_dup(fd);
415 if (!(fp = PerlIO_openn(aTHX_ type,mode,fd,0,0,NULL,num_svs,svp))) {
424 for (; isSPACE(*type); type++) ;
425 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
428 fp = PerlIO_stdout();
429 IoTYPE(io) = IoTYPE_STD;
431 Perl_croak(aTHX_ "More than one argument to '>%c' open",IoTYPE_STD);
436 namesv = sv_2mortal(newSVpvn(type,strlen(type)));
441 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
444 if (!fp && type && *type && *type != ':' && !isIDFIRST(*type))
445 goto unknown_open_mode;
446 } /* IoTYPE_WRONLY */
447 else if (*type == IoTYPE_RDONLY) {
449 for (type++; isSPACE(*type); type++) ;
453 strlcat(mode, "b", PERL_MODE_MAX);
455 strlcat(mode, "t", PERL_MODE_MAX);
465 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
469 IoTYPE(io) = IoTYPE_STD;
471 Perl_croak(aTHX_ "More than one argument to '<%c' open",IoTYPE_STD);
476 namesv = sv_2mortal(newSVpvn(type,strlen(type)));
481 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
483 if (!fp && type && *type && *type != ':' && !isIDFIRST(*type))
484 goto unknown_open_mode;
485 } /* IoTYPE_RDONLY */
486 else if ((num_svs && /* '-|...' or '...|' */
487 type[0] == IoTYPE_STD && type[1] == IoTYPE_PIPE) ||
488 (!num_svs && tend > type+1 && tend[-1] == IoTYPE_PIPE)) {
490 type += 2; /* skip over '-|' */
494 while (tend > type && isSPACE(tend[-1]))
497 for (; isSPACE(*type); type++) ;
502 /* command is missing 19990114 */
503 if (ckWARN(WARN_PIPE))
504 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open");
508 if (!(*name == '-' && name[1] == '\0') || num_svs)
510 TAINT_PROPER("piped open");
515 strlcat(mode, "b", PERL_MODE_MAX);
517 strlcat(mode, "t", PERL_MODE_MAX);
526 fp = PerlProc_popen_list(mode,num_svs,svp);
529 fp = PerlProc_popen(name,mode);
531 IoTYPE(io) = IoTYPE_PIPE;
533 for (; isSPACE(*type); type++) ;
535 if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) {
541 else { /* layer(Args) */
543 goto unknown_open_mode;
545 IoTYPE(io) = IoTYPE_RDONLY;
547 for (; isSPACE(*name); name++) ;
552 strlcat(mode, "b", PERL_MODE_MAX);
554 strlcat(mode, "t", PERL_MODE_MAX);
562 if (*name == '-' && name[1] == '\0') {
564 IoTYPE(io) = IoTYPE_STD;
568 namesv = sv_2mortal(newSVpvn(type,strlen(type)));
573 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
578 if (ckWARN(WARN_NEWLINE) && IoTYPE(io) == IoTYPE_RDONLY && strchr(name, '\n'))
579 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
583 if (ckWARN(WARN_IO)) {
584 if ((IoTYPE(io) == IoTYPE_RDONLY) &&
585 (fp == PerlIO_stdout() || fp == PerlIO_stderr())) {
586 Perl_warner(aTHX_ packWARN(WARN_IO),
587 "Filehandle STD%s reopened as %s only for input",
588 ((fp == PerlIO_stdout()) ? "OUT" : "ERR"),
591 else if ((IoTYPE(io) == IoTYPE_WRONLY) && fp == PerlIO_stdin()) {
592 Perl_warner(aTHX_ packWARN(WARN_IO),
593 "Filehandle STDIN reopened as %s only for output",
598 fd = PerlIO_fileno(fp);
599 /* If there is no fd (e.g. PerlIO::scalar) assume it isn't a
600 * socket - this covers PerlIO::scalar - otherwise unless we "know" the
601 * type probe for socket-ness.
603 if (IoTYPE(io) && IoTYPE(io) != IoTYPE_PIPE && IoTYPE(io) != IoTYPE_STD && fd >= 0) {
604 if (PerlLIO_fstat(fd,&PL_statbuf) < 0) {
605 /* If PerlIO claims to have fd we had better be able to fstat() it. */
606 (void) PerlIO_close(fp);
610 if (S_ISSOCK(PL_statbuf.st_mode))
611 IoTYPE(io) = IoTYPE_SOCKET; /* in case a socket was passed in to us */
615 !(PL_statbuf.st_mode & S_IFMT)
619 && IoTYPE(io) != IoTYPE_WRONLY /* Dups of STD* filehandles already have */
620 && IoTYPE(io) != IoTYPE_RDONLY /* type so they aren't marked as sockets */
621 ) { /* on OS's that return 0 on fstat()ed pipe */
623 Sock_size_t buflen = sizeof tmpbuf;
624 if (PerlSock_getsockname(fd, (struct sockaddr *)tmpbuf, &buflen) >= 0
625 || errno != ENOTSOCK)
626 IoTYPE(io) = IoTYPE_SOCKET; /* some OS's return 0 on fstat()ed socket */
627 /* but some return 0 for streams too, sigh */
629 #endif /* HAS_SOCKET */
630 #endif /* !PERL_MICRO */
634 * If this is a standard handle we discard all the layer stuff
635 * and just dup the fd into whatever was on the handle before !
638 if (saveifp) { /* must use old fp? */
639 /* If fd is less that PL_maxsysfd i.e. STDIN..STDERR
640 then dup the new fileno down
643 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
644 if (saveofp != saveifp) { /* was a socket? */
645 PerlIO_close(saveofp);
649 /* Still a small can-of-worms here if (say) PerlIO::scalar
650 is assigned to (say) STDOUT - for now let dup2() fail
651 and provide the error
653 if (PerlLIO_dup2(fd, savefd) < 0) {
654 (void)PerlIO_close(fp);
658 if (savefd != PerlIO_fileno(PerlIO_stdin())) {
659 char newname[FILENAME_MAX+1];
660 if (PerlIO_getname(fp, newname)) {
661 if (fd == PerlIO_fileno(PerlIO_stdout()))
662 Perl_vmssetuserlnm(aTHX_ "SYS$OUTPUT", newname);
663 if (fd == PerlIO_fileno(PerlIO_stderr()))
664 Perl_vmssetuserlnm(aTHX_ "SYS$ERROR", newname);
670 /* PL_fdpid isn't used on Windows, so avoid this useless work.
671 * XXX Probably the same for a lot of other places. */
677 sv = *av_fetch(PL_fdpid,fd,TRUE);
678 (void)SvUPGRADE(sv, SVt_IV);
681 sv = *av_fetch(PL_fdpid,savefd,TRUE);
682 (void)SvUPGRADE(sv, SVt_IV);
689 /* need to close fp without closing underlying fd */
690 int ofd = PerlIO_fileno(fp);
691 int dupfd = PerlLIO_dup(ofd);
692 #if defined(HAS_FCNTL) && defined(F_SETFD)
693 /* Assume if we have F_SETFD we have F_GETFD */
694 int coe = fcntl(ofd,F_GETFD);
697 PerlLIO_dup2(dupfd,ofd);
698 #if defined(HAS_FCNTL) && defined(F_SETFD)
699 /* The dup trick has lost close-on-exec on ofd */
700 fcntl(ofd,F_SETFD, coe);
702 PerlLIO_close(dupfd);
709 fd = PerlIO_fileno(fp);
711 #if defined(HAS_FCNTL) && defined(F_SETFD)
713 int save_errno = errno;
714 fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
720 IoFLAGS(io) &= ~IOf_NOLINE;
722 if (IoTYPE(io) == IoTYPE_SOCKET
723 || (IoTYPE(io) == IoTYPE_WRONLY && fd >= 0 && S_ISCHR(PL_statbuf.st_mode)) ) {
725 if (*s == IoTYPE_IMPLICIT || *s == IoTYPE_NUMERIC)
728 if (!(IoOFP(io) = PerlIO_openn(aTHX_ type,s,fd,0,0,NULL,0,svp))) {
742 IoTYPE(io) = savetype;
747 Perl_nextargv(pTHX_ register GV *gv)
750 #ifndef FLEXFILENAMES
759 PL_argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO);
760 if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
761 IoFLAGS(io) &= ~IOf_START;
763 if (!PL_argvout_stack)
764 PL_argvout_stack = newAV();
765 av_push(PL_argvout_stack, SvREFCNT_inc(PL_defoutgv));
768 if (PL_filemode & (S_ISUID|S_ISGID)) {
769 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
772 (void)fchmod(PL_lastfd,PL_filemode);
774 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
781 while (av_len(GvAV(gv)) >= 0) {
783 sv = av_shift(GvAV(gv));
785 sv_setsv(GvSV(gv),sv);
786 SvSETMAGIC(GvSV(gv));
787 PL_oldname = SvPVx(GvSV(gv), oldlen);
788 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,Nullfp)) {
790 TAINT_PROPER("inplace open");
791 if (oldlen == 1 && *PL_oldname == '-') {
792 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
793 return IoIFP(GvIOp(gv));
795 #ifndef FLEXFILENAMES
796 filedev = PL_statbuf.st_dev;
797 fileino = PL_statbuf.st_ino;
799 PL_filemode = PL_statbuf.st_mode;
800 fileuid = PL_statbuf.st_uid;
801 filegid = PL_statbuf.st_gid;
802 if (!S_ISREG(PL_filemode)) {
803 if (ckWARN_d(WARN_INPLACE))
804 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
805 "Can't do inplace edit: %s is not a regular file",
811 char *star = strchr(PL_inplace, '*');
813 char *begin = PL_inplace;
814 sv_setpvn(sv, "", 0);
816 sv_catpvn(sv, begin, star - begin);
817 sv_catpvn(sv, PL_oldname, oldlen);
819 } while ((star = strchr(begin, '*')));
824 sv_catpv(sv,PL_inplace);
826 #ifndef FLEXFILENAMES
827 if ((PerlLIO_stat(SvPVX(sv),&PL_statbuf) >= 0
828 && PL_statbuf.st_dev == filedev
829 && PL_statbuf.st_ino == fileino)
831 || ((_djstat_fail_bits & _STFAIL_TRUENAME)!=0)
835 if (ckWARN_d(WARN_INPLACE))
836 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
837 "Can't do inplace edit: %"SVf" would not be unique",
844 #if !defined(DOSISH) && !defined(__CYGWIN__) && !defined(EPOC)
845 if (PerlLIO_rename(PL_oldname,SvPVX(sv)) < 0) {
846 if (ckWARN_d(WARN_INPLACE))
847 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
848 "Can't rename %s to %"SVf": %s, skipping file",
849 PL_oldname, sv, Strerror(errno) );
855 (void)PerlLIO_unlink(SvPVX(sv));
856 (void)PerlLIO_rename(PL_oldname,SvPVX(sv));
857 do_open(gv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,O_RDONLY,0,Nullfp);
860 (void)UNLINK(SvPVX(sv));
861 if (link(PL_oldname,SvPVX(sv)) < 0) {
862 if (ckWARN_d(WARN_INPLACE))
863 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
864 "Can't rename %s to %"SVf": %s, skipping file",
865 PL_oldname, sv, Strerror(errno) );
869 (void)UNLINK(PL_oldname);
873 #if !defined(DOSISH) && !defined(AMIGAOS)
874 # ifndef VMS /* Don't delete; use automatic file versioning */
875 if (UNLINK(PL_oldname) < 0) {
876 if (ckWARN_d(WARN_INPLACE))
877 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
878 "Can't remove %s: %s, skipping file",
879 PL_oldname, Strerror(errno) );
885 Perl_croak(aTHX_ "Can't do inplace edit without backup");
889 sv_setpvn(sv,">",!PL_inplace);
890 sv_catpvn(sv,PL_oldname,oldlen);
891 SETERRNO(0,0); /* in case sprintf set errno */
893 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
894 O_WRONLY|O_CREAT|O_TRUNC,0,Nullfp))
896 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
897 O_WRONLY|O_CREAT|OPEN_EXCL,0666,Nullfp))
900 if (ckWARN_d(WARN_INPLACE))
901 Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't do inplace edit on %s: %s",
902 PL_oldname, Strerror(errno) );
906 setdefout(PL_argvoutgv);
907 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
908 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
910 (void)fchmod(PL_lastfd,PL_filemode);
912 # if !(defined(WIN32) && defined(__BORLANDC__))
913 /* Borland runtime creates a readonly file! */
914 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
917 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
919 (void)fchown(PL_lastfd,fileuid,filegid);
922 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
927 return IoIFP(GvIOp(gv));
930 if (ckWARN_d(WARN_INPLACE)) {
932 if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0
933 && !S_ISREG(PL_statbuf.st_mode))
935 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
936 "Can't do inplace edit: %s is not a regular file",
940 Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't open %s: %s",
941 PL_oldname, Strerror(eno));
945 if (io && (IoFLAGS(io) & IOf_ARGV))
946 IoFLAGS(io) |= IOf_START;
948 (void)do_close(PL_argvoutgv,FALSE);
949 if (io && (IoFLAGS(io) & IOf_ARGV)
950 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
952 GV *oldout = (GV*)av_pop(PL_argvout_stack);
954 SvREFCNT_dec(oldout);
957 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
964 Perl_do_pipe(pTHX_ SV *sv, GV *rgv, GV *wgv)
983 if (PerlProc_pipe(fd) < 0)
985 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"PIPE_OPEN_MODE);
986 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"PIPE_OPEN_MODE);
987 IoOFP(rstio) = IoIFP(rstio);
988 IoIFP(wstio) = IoOFP(wstio);
989 IoTYPE(rstio) = IoTYPE_RDONLY;
990 IoTYPE(wstio) = IoTYPE_WRONLY;
991 if (!IoIFP(rstio) || !IoOFP(wstio)) {
992 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
993 else PerlLIO_close(fd[0]);
994 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
995 else PerlLIO_close(fd[1]);
999 sv_setsv(sv,&PL_sv_yes);
1003 sv_setsv(sv,&PL_sv_undef);
1008 /* explicit renamed to avoid C++ conflict -- kja */
1010 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
1017 if (!gv || SvTYPE(gv) != SVt_PVGV) {
1019 SETERRNO(EBADF,SS_IVCHAN);
1023 if (!io) { /* never opened */
1025 if (ckWARN(WARN_UNOPENED)) /* no check for closed here */
1026 report_evil_fh(gv, io, PL_op->op_type);
1027 SETERRNO(EBADF,SS_IVCHAN);
1031 retval = io_close(io, not_implicit);
1035 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1037 IoTYPE(io) = IoTYPE_CLOSED;
1042 Perl_io_close(pTHX_ IO *io, bool not_implicit)
1044 bool retval = FALSE;
1048 if (IoTYPE(io) == IoTYPE_PIPE) {
1049 status = PerlProc_pclose(IoIFP(io));
1051 STATUS_NATIVE_SET(status);
1052 retval = (STATUS_POSIX == 0);
1055 retval = (status != -1);
1058 else if (IoTYPE(io) == IoTYPE_STD)
1061 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
1062 retval = (PerlIO_close(IoOFP(io)) != EOF);
1063 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
1066 retval = (PerlIO_close(IoIFP(io)) != EOF);
1068 IoOFP(io) = IoIFP(io) = Nullfp;
1070 else if (not_implicit) {
1071 SETERRNO(EBADF,SS_IVCHAN);
1078 Perl_do_eof(pTHX_ GV *gv)
1087 else if (ckWARN(WARN_IO) && (IoTYPE(io) == IoTYPE_WRONLY))
1088 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1093 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
1094 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
1095 return FALSE; /* this is the most usual case */
1098 saverrno = errno; /* getc and ungetc can stomp on errno */
1099 ch = PerlIO_getc(IoIFP(io));
1101 (void)PerlIO_ungetc(IoIFP(io),ch);
1107 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
1108 if (PerlIO_get_cnt(IoIFP(io)) < -1)
1109 PerlIO_set_cnt(IoIFP(io),-1);
1111 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
1112 if (gv != PL_argvgv || !nextargv(gv)) /* get another fp handy */
1116 return TRUE; /* normal fp, definitely end of file */
1122 Perl_do_tell(pTHX_ GV *gv)
1124 register IO *io = 0;
1125 register PerlIO *fp;
1127 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1128 #ifdef ULTRIX_STDIO_BOTCH
1130 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1132 return PerlIO_tell(fp);
1134 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1135 report_evil_fh(gv, io, PL_op->op_type);
1136 SETERRNO(EBADF,RMS_IFI);
1141 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
1143 register IO *io = 0;
1144 register PerlIO *fp;
1146 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1147 #ifdef ULTRIX_STDIO_BOTCH
1149 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1151 return PerlIO_seek(fp, pos, whence) >= 0;
1153 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1154 report_evil_fh(gv, io, PL_op->op_type);
1155 SETERRNO(EBADF,RMS_IFI);
1160 Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
1162 register IO *io = 0;
1163 register PerlIO *fp;
1165 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
1166 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
1167 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1168 report_evil_fh(gv, io, PL_op->op_type);
1169 SETERRNO(EBADF,RMS_IFI);
1174 Perl_mode_from_discipline(pTHX_ SV *discp)
1176 int mode = O_BINARY;
1179 char *s = SvPV(discp,len);
1184 if (s[2] == 'a' && s[3] == 'w'
1185 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1194 if (s[2] == 'r' && s[3] == 'l' && s[4] == 'f'
1195 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1204 goto fail_discipline;
1207 else if (isSPACE(*s)) {
1214 end = strchr(s+1, ':');
1217 #ifndef PERLIO_LAYERS
1218 Perl_croak(aTHX_ "IO layers (like '%.*s') unavailable", end-s, s);
1230 Perl_do_binmode(pTHX_ PerlIO *fp, int iotype, int mode)
1232 /* The old body of this is now in non-LAYER part of perlio.c
1233 * This is a stub for any XS code which might have been calling it.
1235 char *name = ":raw";
1236 #ifdef PERLIO_USING_CRLF
1237 if (!(mode & O_BINARY))
1240 return PerlIO_binmode(aTHX_ fp, iotype, mode, name);
1243 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP)
1244 /* code courtesy of William Kucharski */
1247 I32 my_chsize(fd, length)
1248 I32 fd; /* file descriptor */
1249 Off_t length; /* length to set file to */
1254 if (PerlLIO_fstat(fd, &filebuf) < 0)
1257 if (filebuf.st_size < length) {
1259 /* extend file length */
1261 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
1264 /* write a "0" byte */
1266 if ((PerlLIO_write(fd, "", 1)) != 1)
1270 /* truncate length */
1274 fl.l_start = length;
1275 fl.l_type = F_WRLCK; /* write lock on file space */
1278 * This relies on the UNDOCUMENTED F_FREESP argument to
1279 * fcntl(2), which truncates the file so that it ends at the
1280 * position indicated by fl.l_start.
1282 * Will minor miracles never cease?
1285 if (fcntl(fd, F_FREESP, &fl) < 0)
1292 #endif /* F_FREESP */
1295 Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
1297 register char *tmps;
1300 /* assuming fp is checked earlier */
1306 if (SvIOK(sv) && SvIVX(sv) != 0) {
1307 PerlIO_printf(fp, PL_ofmt, (NV)SvIVX(sv));
1308 return !PerlIO_error(fp);
1310 if ( (SvNOK(sv) && SvNVX(sv) != 0.0)
1311 || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) {
1312 PerlIO_printf(fp, PL_ofmt, SvNVX(sv));
1313 return !PerlIO_error(fp);
1316 switch (SvTYPE(sv)) {
1318 if (ckWARN(WARN_UNINITIALIZED))
1326 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
1328 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
1329 return !PerlIO_error(fp);
1333 if (PerlIO_isutf8(fp)) {
1335 sv_utf8_upgrade_flags(sv = sv_mortalcopy(sv),
1336 SV_GMAGIC|SV_UTF8_NO_ENCODING);
1338 else if (DO_UTF8(sv)) {
1339 if (!sv_utf8_downgrade((sv = sv_mortalcopy(sv)), TRUE)
1340 && ckWARN_d(WARN_UTF8))
1342 Perl_warner(aTHX_ packWARN(WARN_UTF8), "Wide character in print");
1345 tmps = SvPV(sv, len);
1348 /* To detect whether the process is about to overstep its
1349 * filesize limit we would need getrlimit(). We could then
1350 * also transparently raise the limit with setrlimit() --
1351 * but only until the system hard limit/the filesystem limit,
1352 * at which we would get EPERM. Note that when using buffered
1353 * io the write failure can be delayed until the flush/close. --jhi */
1354 if (len && (PerlIO_write(fp,tmps,len) == 0))
1356 return !PerlIO_error(fp);
1366 if (PL_op->op_flags & OPf_REF) {
1371 if (io && IoIFP(io)) {
1373 sv_setpv(PL_statname,"");
1374 PL_laststype = OP_STAT;
1375 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
1379 return PL_laststatval;
1380 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1381 report_evil_fh(gv, io, PL_op->op_type);
1383 sv_setpv(PL_statname,"");
1384 return (PL_laststatval = -1);
1387 else if (PL_op->op_private & OPpFT_STACKED) {
1388 return PL_laststatval;
1395 if (SvTYPE(sv) == SVt_PVGV) {
1399 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
1406 sv_setpvn(PL_statname, s, len);
1407 s = SvPVX(PL_statname); /* s now NUL-terminated */
1408 PL_laststype = OP_STAT;
1409 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1410 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
1411 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
1412 return PL_laststatval;
1416 static char no_prev_lstat[] = "The stat preceding -l _ wasn't an lstat";
1424 if (PL_op->op_flags & OPf_REF) {
1426 if (cGVOP_gv == PL_defgv) {
1427 if (PL_laststype != OP_LSTAT)
1428 Perl_croak(aTHX_ no_prev_lstat);
1429 return PL_laststatval;
1431 if (ckWARN(WARN_IO)) {
1432 Perl_warner(aTHX_ packWARN(WARN_IO), "Use of -l on filehandle %s",
1434 return (PL_laststatval = -1);
1437 else if (ckWARN(WARN_IO) && PL_laststype != OP_LSTAT
1438 && (PL_op->op_private & OPpFT_STACKED))
1439 Perl_croak(aTHX_ no_prev_lstat);
1441 PL_laststype = OP_LSTAT;
1445 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV && ckWARN(WARN_IO)) {
1446 Perl_warner(aTHX_ packWARN(WARN_IO), "Use of -l on filehandle %s",
1447 GvENAME((GV*) SvRV(sv)));
1448 return (PL_laststatval = -1);
1450 sv_setpv(PL_statname,SvPV(sv, n_a));
1451 PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache);
1452 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
1453 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "lstat");
1454 return PL_laststatval;
1459 Perl_do_aexec(pTHX_ SV *really, register SV **mark, register SV **sp)
1461 return do_aexec5(really, mark, sp, 0, 0);
1466 Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1467 int fd, int do_report)
1469 #ifdef MACOS_TRADITIONAL
1470 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1473 char *tmps = Nullch;
1477 New(401,PL_Argv, sp - mark + 1, char*);
1479 while (++mark <= sp) {
1481 *a++ = SvPVx(*mark, n_a);
1487 tmps = SvPV(really, n_a);
1488 if ((!really && *PL_Argv[0] != '/') ||
1489 (really && *tmps != '/')) /* will execvp use PATH? */
1490 TAINT_ENV(); /* testing IFS here is overkill, probably */
1492 if (really && *tmps)
1493 PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
1495 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1497 if (ckWARN(WARN_EXEC))
1498 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
1499 (really ? tmps : PL_Argv[0]), Strerror(errno));
1503 PerlLIO_write(fd, (void*)&e, sizeof(int));
1513 Perl_do_execfree(pTHX)
1517 PL_Argv = Null(char **);
1525 #if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC) && !defined(MACOS_TRADITIONAL)
1528 Perl_do_exec(pTHX_ char *cmd)
1530 return do_exec3(cmd,0,0);
1534 Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report)
1539 while (*cmd && isSPACE(*cmd))
1542 /* save an extra exec if possible */
1546 char flags[PERL_FLAGS_MAX];
1547 if (strnEQ(cmd,PL_cshname,PL_cshlen) &&
1548 strnEQ(cmd+PL_cshlen," -c",3)) {
1550 strlcpy(flags, "-c", PERL_FLAGS_MAX);
1554 s = cmd+PL_cshlen+3;
1558 strlcat(flags, "f", PERL_FLAGS_MAX);
1572 if (s[-1] == '\'') {
1575 PerlProc_execl(PL_cshname,"csh", flags, ncmd, (char*)0);
1585 /* see if there are shell metacharacters in it */
1587 if (*cmd == '.' && isSPACE(cmd[1]))
1590 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1593 for (s = cmd; *s && isALNUM(*s); s++) ; /* catch VAR=val gizmo */
1597 for (s = cmd; *s; s++) {
1598 if (*s != ' ' && !isALPHA(*s) &&
1599 strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1600 if (*s == '\n' && !s[1]) {
1604 /* handle the 2>&1 construct at the end */
1605 if (*s == '>' && s[1] == '&' && s[2] == '1'
1606 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1607 && (!s[3] || isSPACE(s[3])))
1611 while (*t && isSPACE(*t))
1613 if (!*t && (PerlLIO_dup2(1,2) != -1)) {
1620 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
1626 New(402,PL_Argv, (s - cmd) / 2 + 2, char*);
1627 PL_Cmd = savepvn(cmd, s-cmd);
1629 for (s = PL_Cmd; *s;) {
1630 while (*s && isSPACE(*s)) s++;
1633 while (*s && !isSPACE(*s)) s++;
1640 PerlProc_execvp(PL_Argv[0],PL_Argv);
1642 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1649 if (ckWARN(WARN_EXEC))
1650 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
1651 PL_Argv[0], Strerror(errno));
1653 PerlLIO_write(fd, (void*)&e, sizeof(int));
1662 #endif /* OS2 || WIN32 */
1665 Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
1669 register I32 tot = 0;
1672 SV **oldmark = mark;
1675 #define APPLY_TAINT_PROPER() \
1677 if (PL_tainted) { TAINT_PROPER(what); } \
1680 /* This is a first heuristic; it doesn't catch tainting magic. */
1682 while (++mark <= sp) {
1683 if (SvTAINTED(*mark)) {
1693 APPLY_TAINT_PROPER();
1696 APPLY_TAINT_PROPER();
1698 while (++mark <= sp) {
1699 char *name = SvPVx(*mark, n_a);
1700 APPLY_TAINT_PROPER();
1701 if (PerlLIO_chmod(name, val))
1709 APPLY_TAINT_PROPER();
1710 if (sp - mark > 2) {
1711 val = SvIVx(*++mark);
1712 val2 = SvIVx(*++mark);
1713 APPLY_TAINT_PROPER();
1715 while (++mark <= sp) {
1716 char *name = SvPVx(*mark, n_a);
1717 APPLY_TAINT_PROPER();
1718 if (PerlLIO_chown(name, val, val2))
1725 XXX Should we make lchown() directly available from perl?
1726 For now, we'll let Configure test for HAS_LCHOWN, but do
1727 nothing in the core.
1733 APPLY_TAINT_PROPER();
1736 s = SvPVx(*++mark, n_a);
1738 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1740 if ((val = whichsig(s)) < 0)
1741 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
1745 APPLY_TAINT_PROPER();
1748 /* kill() doesn't do process groups (job trees?) under VMS */
1749 if (val < 0) val = -val;
1750 if (val == SIGKILL) {
1751 # include <starlet.h>
1752 /* Use native sys$delprc() to insure that target process is
1753 * deleted; supervisor-mode images don't pay attention to
1754 * CRTL's emulation of Unix-style signals and kill()
1756 while (++mark <= sp) {
1757 I32 proc = SvIVx(*mark);
1758 register unsigned long int __vmssts;
1759 APPLY_TAINT_PROPER();
1760 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1764 case SS$_NOSUCHNODE:
1765 SETERRNO(ESRCH,__vmssts);
1768 SETERRNO(EPERM,__vmssts);
1771 SETERRNO(EVMSERR,__vmssts);
1780 while (++mark <= sp) {
1781 I32 proc = SvIVx(*mark);
1782 APPLY_TAINT_PROPER();
1784 if (PerlProc_killpg(proc,val)) /* BSD */
1786 if (PerlProc_kill(-proc,val)) /* SYSV */
1792 while (++mark <= sp) {
1793 I32 proc = SvIVx(*mark);
1794 APPLY_TAINT_PROPER();
1795 if (PerlProc_kill(proc, val))
1803 APPLY_TAINT_PROPER();
1805 while (++mark <= sp) {
1806 s = SvPVx(*mark, n_a);
1807 APPLY_TAINT_PROPER();
1808 if (PL_euid || PL_unsafe) {
1812 else { /* don't let root wipe out directories without -U */
1813 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1825 APPLY_TAINT_PROPER();
1826 if (sp - mark > 2) {
1827 #if defined(I_UTIME) || defined(VMS)
1828 struct utimbuf utbuf;
1836 SV* accessed = *++mark;
1837 SV* modified = *++mark;
1838 void * utbufp = &utbuf;
1840 /* Be like C, and if both times are undefined, let the C
1841 * library figure out what to do. This usually means
1842 * "current time". */
1844 if ( accessed == &PL_sv_undef && modified == &PL_sv_undef )
1847 Zero(&utbuf, sizeof utbuf, char);
1849 utbuf.actime = (Time_t)SvNVx(accessed); /* time accessed */
1850 utbuf.modtime = (Time_t)SvNVx(modified); /* time modified */
1852 utbuf.actime = (Time_t)SvIVx(accessed); /* time accessed */
1853 utbuf.modtime = (Time_t)SvIVx(modified); /* time modified */
1856 APPLY_TAINT_PROPER();
1858 while (++mark <= sp) {
1859 char *name = SvPVx(*mark, n_a);
1860 APPLY_TAINT_PROPER();
1861 if (PerlLIO_utime(name, utbufp))
1872 #undef APPLY_TAINT_PROPER
1875 /* Do the permissions allow some operation? Assumes statcache already set. */
1876 #ifndef VMS /* VMS' cando is in vms.c */
1878 Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register Stat_t *statbufp)
1879 /* Note: we use `effective' both for uids and gids.
1880 * Here we are betting on Uid_t being equal or wider than Gid_t. */
1883 /* [Comments and code from Len Reed]
1884 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1885 * to write-protected files. The execute permission bit is set
1886 * by the Miscrosoft C library stat() function for the following:
1891 * All files and directories are readable.
1892 * Directories and special files, e.g. "CON", cannot be
1894 * [Comment by Tom Dinger -- a directory can have the write-protect
1895 * bit set in the file system, but DOS permits changes to
1896 * the directory anyway. In addition, all bets are off
1897 * here for networked software, such as Novell and
1901 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1902 * too so it will actually look into the files for magic numbers
1904 return (mode & statbufp->st_mode) ? TRUE : FALSE;
1906 #else /* ! DOSISH */
1907 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
1908 if (mode == S_IXUSR) {
1909 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1913 return TRUE; /* root reads and writes anything */
1916 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1917 if (statbufp->st_mode & mode)
1918 return TRUE; /* ok as "user" */
1920 else if (ingroup(statbufp->st_gid,effective)) {
1921 if (statbufp->st_mode & mode >> 3)
1922 return TRUE; /* ok as "group" */
1924 else if (statbufp->st_mode & mode >> 6)
1925 return TRUE; /* ok as "other" */
1927 #endif /* ! DOSISH */
1932 Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective)
1934 #ifdef MACOS_TRADITIONAL
1935 /* This is simply not correct for AppleShare, but fix it yerself. */
1938 if (testgid == (effective ? PL_egid : PL_gid))
1940 #ifdef HAS_GETGROUPS
1945 Groups_t gary[NGROUPS];
1948 anum = getgroups(NGROUPS,gary);
1950 if (gary[anum] == testgid)
1958 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1961 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
1966 key = (key_t)SvNVx(*++mark);
1967 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1968 flags = SvIVx(*++mark);
1974 return msgget(key, flags);
1978 return semget(key, n, flags);
1982 return shmget(key, n, flags);
1984 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1986 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1989 return -1; /* should never happen */
1993 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
1997 I32 id, n, cmd, infosize, getinfo;
2000 id = SvIVx(*++mark);
2001 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
2002 cmd = SvIVx(*++mark);
2005 getinfo = (cmd == IPC_STAT);
2011 if (cmd == IPC_STAT || cmd == IPC_SET)
2012 infosize = sizeof(struct msqid_ds);
2017 if (cmd == IPC_STAT || cmd == IPC_SET)
2018 infosize = sizeof(struct shmid_ds);
2024 if (cmd == IPC_STAT || cmd == IPC_SET)
2025 infosize = sizeof(struct semid_ds);
2026 else if (cmd == GETALL || cmd == SETALL)
2028 struct semid_ds semds;
2030 #ifdef EXTRA_F_IN_SEMUN_BUF
2031 semun.buff = &semds;
2035 getinfo = (cmd == GETALL);
2036 if (Semctl(id, 0, IPC_STAT, semun) == -1)
2038 infosize = semds.sem_nsems * sizeof(short);
2039 /* "short" is technically wrong but much more portable
2040 than guessing about u_?short(_t)? */
2043 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2047 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
2049 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2058 SvPV_force(astr, len);
2059 a = SvGROW(astr, infosize+1);
2063 a = SvPV(astr, len);
2064 if (len != infosize)
2065 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
2074 a = INT2PTR(char *,i); /* ouch */
2081 ret = msgctl(id, cmd, (struct msqid_ds *)a);
2087 union semun unsemds;
2089 #ifdef EXTRA_F_IN_SEMUN_BUF
2090 unsemds.buff = (struct semid_ds *)a;
2092 unsemds.buf = (struct semid_ds *)a;
2094 ret = Semctl(id, n, cmd, unsemds);
2096 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2103 ret = shmctl(id, cmd, (struct shmid_ds *)a);
2107 if (getinfo && ret >= 0) {
2108 SvCUR_set(astr, infosize);
2109 *SvEND(astr) = '\0';
2116 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
2121 I32 id, msize, flags;
2124 id = SvIVx(*++mark);
2126 flags = SvIVx(*++mark);
2127 mbuf = SvPV(mstr, len);
2128 if ((msize = len - sizeof(long)) < 0)
2129 Perl_croak(aTHX_ "Arg too short for msgsnd");
2131 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
2133 Perl_croak(aTHX_ "msgsnd not implemented");
2138 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
2144 I32 id, msize, flags, ret;
2147 id = SvIVx(*++mark);
2149 /* suppress warning when reading into undef var --jhi */
2151 sv_setpvn(mstr, "", 0);
2152 msize = SvIVx(*++mark);
2153 mtype = (long)SvIVx(*++mark);
2154 flags = SvIVx(*++mark);
2155 SvPV_force(mstr, len);
2156 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
2159 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
2161 SvCUR_set(mstr, sizeof(long)+ret);
2162 *SvEND(mstr) = '\0';
2163 #ifndef INCOMPLETE_TAINTS
2164 /* who knows who has been playing with this message? */
2170 Perl_croak(aTHX_ "msgrcv not implemented");
2175 Perl_do_semop(pTHX_ SV **mark, SV **sp)
2183 id = SvIVx(*++mark);
2185 opbuf = SvPV(opstr, opsize);
2186 if (opsize < 3 * SHORTSIZE
2187 || (opsize % (3 * SHORTSIZE))) {
2188 SETERRNO(EINVAL,LIB_INVARG);
2192 /* We can't assume that sizeof(struct sembuf) == 3 * sizeof(short). */
2194 int nsops = opsize / (3 * sizeof (short));
2196 short *ops = (short *) opbuf;
2198 struct sembuf *temps, *t;
2201 New (0, temps, nsops, struct sembuf);
2209 result = semop(id, temps, nsops);
2223 Perl_croak(aTHX_ "semop not implemented");
2228 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
2233 I32 id, mpos, msize;
2235 struct shmid_ds shmds;
2237 id = SvIVx(*++mark);
2239 mpos = SvIVx(*++mark);
2240 msize = SvIVx(*++mark);
2242 if (shmctl(id, IPC_STAT, &shmds) == -1)
2244 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
2245 SETERRNO(EFAULT,SS_ACCVIO); /* can't do as caller requested */
2248 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
2249 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2251 if (optype == OP_SHMREAD) {
2252 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2254 sv_setpvn(mstr, "", 0);
2255 SvPV_force(mstr, len);
2256 mbuf = SvGROW(mstr, msize+1);
2258 Copy(shm + mpos, mbuf, msize, char);
2259 SvCUR_set(mstr, msize);
2260 *SvEND(mstr) = '\0';
2262 #ifndef INCOMPLETE_TAINTS
2263 /* who knows who has been playing with this shared memory? */
2270 mbuf = SvPV(mstr, len);
2271 if ((n = len) > msize)
2273 Copy(mbuf, shm + mpos, n, char);
2275 memzero(shm + mpos + n, msize - n);
2279 Perl_croak(aTHX_ "shm I/O not implemented");
2283 #endif /* SYSV IPC */
2288 =for apidoc start_glob
2290 Function called by C<do_readline> to spawn a glob (or do the glob inside
2291 perl on VMS). This code used to be inline, but now perl uses C<File::Glob>
2292 this glob starter is only used by miniperl during the build process.
2293 Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up.
2299 Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
2301 SV *tmpcmd = NEWSV(55, 0);
2305 #ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
2306 /* since spawning off a process is a real performance hit */
2308 #include <descrip.h>
2309 #include <lib$routines.h>
2312 char rslt[NAM$C_MAXRSS+1+sizeof(unsigned short int)] = {'\0','\0'};
2313 char vmsspec[NAM$C_MAXRSS+1];
2314 char *rstr = rslt + sizeof(unsigned short int), *begin, *end, *cp;
2315 $DESCRIPTOR(dfltdsc,"SYS$DISK:[]*.*;");
2318 struct dsc$descriptor_s wilddsc
2319 = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
2320 struct dsc$descriptor_vs rsdsc
2321 = {sizeof rslt, DSC$K_DTYPE_VT, DSC$K_CLASS_VS, rslt};
2322 unsigned long int cxt = 0, sts = 0, ok = 1, hasdir = 0, hasver = 0, isunix = 0;
2324 /* We could find out if there's an explicit dev/dir or version
2325 by peeking into lib$find_file's internal context at
2326 ((struct NAM *)((struct FAB *)cxt)->fab$l_nam)->nam$l_fnb
2327 but that's unsupported, so I don't want to do it now and
2328 have it bite someone in the future. */
2329 cp = SvPV(tmpglob,i);
2331 if (cp[i] == ';') hasver = 1;
2333 if (sts) hasver = 1;
2337 hasdir = isunix = 1;
2340 if (cp[i] == ']' || cp[i] == '>' || cp[i] == ':') {
2345 if ((tmpfp = PerlIO_tmpfile()) != NULL) {
2347 if (!PerlLIO_stat(SvPVX(tmpglob),&st) && S_ISDIR(st.st_mode))
2348 ok = ((wilddsc.dsc$a_pointer = tovmspath(SvPVX(tmpglob),vmsspec)) != NULL);
2349 else ok = ((wilddsc.dsc$a_pointer = tovmsspec(SvPVX(tmpglob),vmsspec)) != NULL);
2350 if (ok) wilddsc.dsc$w_length = (unsigned short int) strlen(wilddsc.dsc$a_pointer);
2351 for (cp=wilddsc.dsc$a_pointer; ok && cp && *cp; cp++)
2352 if (*cp == '?') *cp = '%'; /* VMS style single-char wildcard */
2353 while (ok && ((sts = lib$find_file(&wilddsc,&rsdsc,&cxt,
2354 &dfltdsc,NULL,NULL,NULL))&1)) {
2355 /* with varying string, 1st word of buffer contains result length */
2356 end = rstr + *((unsigned short int*)rslt);
2357 if (!hasver) while (*end != ';' && end > rstr) end--;
2358 *(end++) = '\n'; *end = '\0';
2359 for (cp = rstr; *cp; cp++) *cp = _tolower(*cp);
2361 if (isunix) trim_unixpath(rstr,SvPVX(tmpglob),1);
2366 while (*(--begin) != ']' && *begin != '>') ;
2369 ok = (PerlIO_puts(tmpfp,begin) != EOF);
2371 if (cxt) (void)lib$find_file_end(&cxt);
2372 if (ok && sts != RMS$_NMF &&
2373 sts != RMS$_DNF && sts != RMS_FNF) ok = 0;
2376 SETERRNO((sts == RMS$_SYN ? EINVAL : EVMSERR),sts);
2378 PerlIO_close(tmpfp);
2382 PerlIO_rewind(tmpfp);
2383 IoTYPE(io) = IoTYPE_RDONLY;
2384 IoIFP(io) = fp = tmpfp;
2385 IoFLAGS(io) &= ~IOf_UNTAINT; /* maybe redundant */
2390 #ifdef MACOS_TRADITIONAL
2391 sv_setpv(tmpcmd, "glob ");
2392 sv_catsv(tmpcmd, tmpglob);
2393 sv_catpv(tmpcmd, " |");
2397 sv_setpv(tmpcmd, "for a in ");
2398 sv_catsv(tmpcmd, tmpglob);
2399 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
2402 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
2403 sv_catsv(tmpcmd, tmpglob);
2405 sv_setpv(tmpcmd, "perlglob ");
2406 sv_catsv(tmpcmd, tmpglob);
2407 sv_catpv(tmpcmd, " |");
2412 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
2413 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
2414 sv_catsv(tmpcmd, tmpglob);
2415 sv_catpv(tmpcmd, "' 2>/dev/null |");
2417 sv_setpv(tmpcmd, "echo ");
2418 sv_catsv(tmpcmd, tmpglob);
2420 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
2422 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
2425 #endif /* !DOSISH */
2426 #endif /* MACOS_TRADITIONAL */
2427 (void)do_open(PL_last_in_gv, SvPVX(tmpcmd), SvCUR(tmpcmd),
2428 FALSE, O_RDONLY, 0, Nullfp);