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 bool prev_err = PerlIO_error(IoOFP(io));
1063 retval = (PerlIO_close(IoOFP(io)) != EOF && !prev_err);
1064 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
1067 bool prev_err = PerlIO_error(IoIFP(io));
1068 retval = (PerlIO_close(IoIFP(io)) != EOF && !prev_err);
1071 IoOFP(io) = IoIFP(io) = Nullfp;
1073 else if (not_implicit) {
1074 SETERRNO(EBADF,SS_IVCHAN);
1081 Perl_do_eof(pTHX_ GV *gv)
1090 else if (ckWARN(WARN_IO) && (IoTYPE(io) == IoTYPE_WRONLY))
1091 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1096 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
1097 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
1098 return FALSE; /* this is the most usual case */
1101 saverrno = errno; /* getc and ungetc can stomp on errno */
1102 ch = PerlIO_getc(IoIFP(io));
1104 (void)PerlIO_ungetc(IoIFP(io),ch);
1110 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
1111 if (PerlIO_get_cnt(IoIFP(io)) < -1)
1112 PerlIO_set_cnt(IoIFP(io),-1);
1114 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
1115 if (gv != PL_argvgv || !nextargv(gv)) /* get another fp handy */
1119 return TRUE; /* normal fp, definitely end of file */
1125 Perl_do_tell(pTHX_ GV *gv)
1127 register IO *io = 0;
1128 register PerlIO *fp;
1130 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1131 #ifdef ULTRIX_STDIO_BOTCH
1133 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1135 return PerlIO_tell(fp);
1137 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1138 report_evil_fh(gv, io, PL_op->op_type);
1139 SETERRNO(EBADF,RMS_IFI);
1144 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
1146 register IO *io = 0;
1147 register PerlIO *fp;
1149 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1150 #ifdef ULTRIX_STDIO_BOTCH
1152 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1154 return PerlIO_seek(fp, pos, whence) >= 0;
1156 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1157 report_evil_fh(gv, io, PL_op->op_type);
1158 SETERRNO(EBADF,RMS_IFI);
1163 Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
1165 register IO *io = 0;
1166 register PerlIO *fp;
1168 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
1169 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
1170 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1171 report_evil_fh(gv, io, PL_op->op_type);
1172 SETERRNO(EBADF,RMS_IFI);
1177 Perl_mode_from_discipline(pTHX_ SV *discp)
1179 int mode = O_BINARY;
1182 char *s = SvPV(discp,len);
1187 if (s[2] == 'a' && s[3] == 'w'
1188 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1197 if (s[2] == 'r' && s[3] == 'l' && s[4] == 'f'
1198 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1207 goto fail_discipline;
1210 else if (isSPACE(*s)) {
1217 end = strchr(s+1, ':');
1220 #ifndef PERLIO_LAYERS
1221 Perl_croak(aTHX_ "IO layers (like '%.*s') unavailable", end-s, s);
1233 Perl_do_binmode(pTHX_ PerlIO *fp, int iotype, int mode)
1235 /* The old body of this is now in non-LAYER part of perlio.c
1236 * This is a stub for any XS code which might have been calling it.
1238 char *name = ":raw";
1239 #ifdef PERLIO_USING_CRLF
1240 if (!(mode & O_BINARY))
1243 return PerlIO_binmode(aTHX_ fp, iotype, mode, name);
1246 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP)
1247 /* code courtesy of William Kucharski */
1250 I32 my_chsize(fd, length)
1251 I32 fd; /* file descriptor */
1252 Off_t length; /* length to set file to */
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)
1295 #endif /* F_FREESP */
1298 Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
1300 register char *tmps;
1303 /* assuming fp is checked earlier */
1309 if (SvIOK(sv) && SvIVX(sv) != 0) {
1310 PerlIO_printf(fp, PL_ofmt, (NV)SvIVX(sv));
1311 return !PerlIO_error(fp);
1313 if ( (SvNOK(sv) && SvNVX(sv) != 0.0)
1314 || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) {
1315 PerlIO_printf(fp, PL_ofmt, SvNVX(sv));
1316 return !PerlIO_error(fp);
1319 switch (SvTYPE(sv)) {
1321 if (ckWARN(WARN_UNINITIALIZED))
1329 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
1331 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
1332 return !PerlIO_error(fp);
1336 if (PerlIO_isutf8(fp)) {
1338 sv_utf8_upgrade_flags(sv = sv_mortalcopy(sv),
1339 SV_GMAGIC|SV_UTF8_NO_ENCODING);
1341 else if (DO_UTF8(sv)) {
1342 if (!sv_utf8_downgrade((sv = sv_mortalcopy(sv)), TRUE)
1343 && ckWARN_d(WARN_UTF8))
1345 Perl_warner(aTHX_ packWARN(WARN_UTF8), "Wide character in print");
1348 tmps = SvPV(sv, len);
1351 /* To detect whether the process is about to overstep its
1352 * filesize limit we would need getrlimit(). We could then
1353 * also transparently raise the limit with setrlimit() --
1354 * but only until the system hard limit/the filesystem limit,
1355 * at which we would get EPERM. Note that when using buffered
1356 * io the write failure can be delayed until the flush/close. --jhi */
1357 if (len && (PerlIO_write(fp,tmps,len) == 0))
1359 return !PerlIO_error(fp);
1369 if (PL_op->op_flags & OPf_REF) {
1374 if (io && IoIFP(io)) {
1376 sv_setpv(PL_statname,"");
1377 PL_laststype = OP_STAT;
1378 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
1382 return PL_laststatval;
1383 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1384 report_evil_fh(gv, io, PL_op->op_type);
1386 sv_setpv(PL_statname,"");
1387 return (PL_laststatval = -1);
1390 else if (PL_op->op_private & OPpFT_STACKED) {
1391 return PL_laststatval;
1398 if (SvTYPE(sv) == SVt_PVGV) {
1402 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
1409 sv_setpvn(PL_statname, s, len);
1410 s = SvPVX(PL_statname); /* s now NUL-terminated */
1411 PL_laststype = OP_STAT;
1412 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1413 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
1414 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
1415 return PL_laststatval;
1419 static char no_prev_lstat[] = "The stat preceding -l _ wasn't an lstat";
1427 if (PL_op->op_flags & OPf_REF) {
1429 if (cGVOP_gv == PL_defgv) {
1430 if (PL_laststype != OP_LSTAT)
1431 Perl_croak(aTHX_ no_prev_lstat);
1432 return PL_laststatval;
1434 if (ckWARN(WARN_IO)) {
1435 Perl_warner(aTHX_ packWARN(WARN_IO), "Use of -l on filehandle %s",
1437 return (PL_laststatval = -1);
1440 else if (ckWARN(WARN_IO) && PL_laststype != OP_LSTAT
1441 && (PL_op->op_private & OPpFT_STACKED))
1442 Perl_croak(aTHX_ no_prev_lstat);
1444 PL_laststype = OP_LSTAT;
1448 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV && ckWARN(WARN_IO)) {
1449 Perl_warner(aTHX_ packWARN(WARN_IO), "Use of -l on filehandle %s",
1450 GvENAME((GV*) SvRV(sv)));
1451 return (PL_laststatval = -1);
1453 sv_setpv(PL_statname,SvPV(sv, n_a));
1454 PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache);
1455 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
1456 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "lstat");
1457 return PL_laststatval;
1462 Perl_do_aexec(pTHX_ SV *really, register SV **mark, register SV **sp)
1464 return do_aexec5(really, mark, sp, 0, 0);
1469 Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1470 int fd, int do_report)
1472 #ifdef MACOS_TRADITIONAL
1473 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1476 char *tmps = Nullch;
1480 New(401,PL_Argv, sp - mark + 1, char*);
1482 while (++mark <= sp) {
1484 *a++ = SvPVx(*mark, n_a);
1490 tmps = SvPV(really, n_a);
1491 if ((!really && *PL_Argv[0] != '/') ||
1492 (really && *tmps != '/')) /* will execvp use PATH? */
1493 TAINT_ENV(); /* testing IFS here is overkill, probably */
1495 if (really && *tmps)
1496 PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
1498 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1500 if (ckWARN(WARN_EXEC))
1501 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
1502 (really ? tmps : PL_Argv[0]), Strerror(errno));
1506 PerlLIO_write(fd, (void*)&e, sizeof(int));
1516 Perl_do_execfree(pTHX)
1520 PL_Argv = Null(char **);
1528 #if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC) && !defined(MACOS_TRADITIONAL)
1531 Perl_do_exec(pTHX_ char *cmd)
1533 return do_exec3(cmd,0,0);
1537 Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report)
1542 while (*cmd && isSPACE(*cmd))
1545 /* save an extra exec if possible */
1549 char flags[PERL_FLAGS_MAX];
1550 if (strnEQ(cmd,PL_cshname,PL_cshlen) &&
1551 strnEQ(cmd+PL_cshlen," -c",3)) {
1553 strlcpy(flags, "-c", PERL_FLAGS_MAX);
1557 s = cmd+PL_cshlen+3;
1561 strlcat(flags, "f", PERL_FLAGS_MAX);
1575 if (s[-1] == '\'') {
1578 PerlProc_execl(PL_cshname,"csh", flags, ncmd, (char*)0);
1588 /* see if there are shell metacharacters in it */
1590 if (*cmd == '.' && isSPACE(cmd[1]))
1593 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1596 for (s = cmd; *s && isALNUM(*s); s++) ; /* catch VAR=val gizmo */
1600 for (s = cmd; *s; s++) {
1601 if (*s != ' ' && !isALPHA(*s) &&
1602 strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1603 if (*s == '\n' && !s[1]) {
1607 /* handle the 2>&1 construct at the end */
1608 if (*s == '>' && s[1] == '&' && s[2] == '1'
1609 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1610 && (!s[3] || isSPACE(s[3])))
1614 while (*t && isSPACE(*t))
1616 if (!*t && (PerlLIO_dup2(1,2) != -1)) {
1623 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
1629 New(402,PL_Argv, (s - cmd) / 2 + 2, char*);
1630 PL_Cmd = savepvn(cmd, s-cmd);
1632 for (s = PL_Cmd; *s;) {
1633 while (*s && isSPACE(*s)) s++;
1636 while (*s && !isSPACE(*s)) s++;
1643 PerlProc_execvp(PL_Argv[0],PL_Argv);
1645 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1652 if (ckWARN(WARN_EXEC))
1653 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
1654 PL_Argv[0], Strerror(errno));
1656 PerlLIO_write(fd, (void*)&e, sizeof(int));
1665 #endif /* OS2 || WIN32 */
1668 Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
1672 register I32 tot = 0;
1675 SV **oldmark = mark;
1678 #define APPLY_TAINT_PROPER() \
1680 if (PL_tainted) { TAINT_PROPER(what); } \
1683 /* This is a first heuristic; it doesn't catch tainting magic. */
1685 while (++mark <= sp) {
1686 if (SvTAINTED(*mark)) {
1696 APPLY_TAINT_PROPER();
1699 APPLY_TAINT_PROPER();
1701 while (++mark <= sp) {
1702 char *name = SvPVx(*mark, n_a);
1703 APPLY_TAINT_PROPER();
1704 if (PerlLIO_chmod(name, val))
1712 APPLY_TAINT_PROPER();
1713 if (sp - mark > 2) {
1714 val = SvIVx(*++mark);
1715 val2 = SvIVx(*++mark);
1716 APPLY_TAINT_PROPER();
1718 while (++mark <= sp) {
1719 char *name = SvPVx(*mark, n_a);
1720 APPLY_TAINT_PROPER();
1721 if (PerlLIO_chown(name, val, val2))
1728 XXX Should we make lchown() directly available from perl?
1729 For now, we'll let Configure test for HAS_LCHOWN, but do
1730 nothing in the core.
1736 APPLY_TAINT_PROPER();
1739 s = SvPVx(*++mark, n_a);
1741 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1743 if ((val = whichsig(s)) < 0)
1744 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
1748 APPLY_TAINT_PROPER();
1751 /* kill() doesn't do process groups (job trees?) under VMS */
1752 if (val < 0) val = -val;
1753 if (val == SIGKILL) {
1754 # include <starlet.h>
1755 /* Use native sys$delprc() to insure that target process is
1756 * deleted; supervisor-mode images don't pay attention to
1757 * CRTL's emulation of Unix-style signals and kill()
1759 while (++mark <= sp) {
1760 I32 proc = SvIVx(*mark);
1761 register unsigned long int __vmssts;
1762 APPLY_TAINT_PROPER();
1763 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1767 case SS$_NOSUCHNODE:
1768 SETERRNO(ESRCH,__vmssts);
1771 SETERRNO(EPERM,__vmssts);
1774 SETERRNO(EVMSERR,__vmssts);
1783 while (++mark <= sp) {
1784 I32 proc = SvIVx(*mark);
1785 APPLY_TAINT_PROPER();
1787 if (PerlProc_killpg(proc,val)) /* BSD */
1789 if (PerlProc_kill(-proc,val)) /* SYSV */
1795 while (++mark <= sp) {
1796 I32 proc = SvIVx(*mark);
1797 APPLY_TAINT_PROPER();
1798 if (PerlProc_kill(proc, val))
1806 APPLY_TAINT_PROPER();
1808 while (++mark <= sp) {
1809 s = SvPVx(*mark, n_a);
1810 APPLY_TAINT_PROPER();
1811 if (PL_euid || PL_unsafe) {
1815 else { /* don't let root wipe out directories without -U */
1816 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1828 APPLY_TAINT_PROPER();
1829 if (sp - mark > 2) {
1830 #if defined(I_UTIME) || defined(VMS)
1831 struct utimbuf utbuf;
1839 SV* accessed = *++mark;
1840 SV* modified = *++mark;
1841 void * utbufp = &utbuf;
1843 /* Be like C, and if both times are undefined, let the C
1844 * library figure out what to do. This usually means
1845 * "current time". */
1847 if ( accessed == &PL_sv_undef && modified == &PL_sv_undef )
1850 Zero(&utbuf, sizeof utbuf, char);
1852 utbuf.actime = (Time_t)SvNVx(accessed); /* time accessed */
1853 utbuf.modtime = (Time_t)SvNVx(modified); /* time modified */
1855 utbuf.actime = (Time_t)SvIVx(accessed); /* time accessed */
1856 utbuf.modtime = (Time_t)SvIVx(modified); /* time modified */
1859 APPLY_TAINT_PROPER();
1861 while (++mark <= sp) {
1862 char *name = SvPVx(*mark, n_a);
1863 APPLY_TAINT_PROPER();
1864 if (PerlLIO_utime(name, utbufp))
1875 #undef APPLY_TAINT_PROPER
1878 /* Do the permissions allow some operation? Assumes statcache already set. */
1879 #ifndef VMS /* VMS' cando is in vms.c */
1881 Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register Stat_t *statbufp)
1882 /* Note: we use `effective' both for uids and gids.
1883 * Here we are betting on Uid_t being equal or wider than Gid_t. */
1886 /* [Comments and code from Len Reed]
1887 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1888 * to write-protected files. The execute permission bit is set
1889 * by the Miscrosoft C library stat() function for the following:
1894 * All files and directories are readable.
1895 * Directories and special files, e.g. "CON", cannot be
1897 * [Comment by Tom Dinger -- a directory can have the write-protect
1898 * bit set in the file system, but DOS permits changes to
1899 * the directory anyway. In addition, all bets are off
1900 * here for networked software, such as Novell and
1904 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1905 * too so it will actually look into the files for magic numbers
1907 return (mode & statbufp->st_mode) ? TRUE : FALSE;
1909 #else /* ! DOSISH */
1910 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
1911 if (mode == S_IXUSR) {
1912 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1916 return TRUE; /* root reads and writes anything */
1919 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1920 if (statbufp->st_mode & mode)
1921 return TRUE; /* ok as "user" */
1923 else if (ingroup(statbufp->st_gid,effective)) {
1924 if (statbufp->st_mode & mode >> 3)
1925 return TRUE; /* ok as "group" */
1927 else if (statbufp->st_mode & mode >> 6)
1928 return TRUE; /* ok as "other" */
1930 #endif /* ! DOSISH */
1935 Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective)
1937 #ifdef MACOS_TRADITIONAL
1938 /* This is simply not correct for AppleShare, but fix it yerself. */
1941 if (testgid == (effective ? PL_egid : PL_gid))
1943 #ifdef HAS_GETGROUPS
1948 Groups_t gary[NGROUPS];
1951 anum = getgroups(NGROUPS,gary);
1953 if (gary[anum] == testgid)
1961 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1964 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
1969 key = (key_t)SvNVx(*++mark);
1970 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1971 flags = SvIVx(*++mark);
1977 return msgget(key, flags);
1981 return semget(key, n, flags);
1985 return shmget(key, n, flags);
1987 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1989 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1992 return -1; /* should never happen */
1996 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
2000 I32 id, n, cmd, infosize, getinfo;
2003 id = SvIVx(*++mark);
2004 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
2005 cmd = SvIVx(*++mark);
2008 getinfo = (cmd == IPC_STAT);
2014 if (cmd == IPC_STAT || cmd == IPC_SET)
2015 infosize = sizeof(struct msqid_ds);
2020 if (cmd == IPC_STAT || cmd == IPC_SET)
2021 infosize = sizeof(struct shmid_ds);
2027 if (cmd == IPC_STAT || cmd == IPC_SET)
2028 infosize = sizeof(struct semid_ds);
2029 else if (cmd == GETALL || cmd == SETALL)
2031 struct semid_ds semds;
2033 #ifdef EXTRA_F_IN_SEMUN_BUF
2034 semun.buff = &semds;
2038 getinfo = (cmd == GETALL);
2039 if (Semctl(id, 0, IPC_STAT, semun) == -1)
2041 infosize = semds.sem_nsems * sizeof(short);
2042 /* "short" is technically wrong but much more portable
2043 than guessing about u_?short(_t)? */
2046 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2050 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
2052 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2061 SvPV_force(astr, len);
2062 a = SvGROW(astr, infosize+1);
2066 a = SvPV(astr, len);
2067 if (len != infosize)
2068 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
2077 a = INT2PTR(char *,i); /* ouch */
2084 ret = msgctl(id, cmd, (struct msqid_ds *)a);
2090 union semun unsemds;
2092 #ifdef EXTRA_F_IN_SEMUN_BUF
2093 unsemds.buff = (struct semid_ds *)a;
2095 unsemds.buf = (struct semid_ds *)a;
2097 ret = Semctl(id, n, cmd, unsemds);
2099 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2106 ret = shmctl(id, cmd, (struct shmid_ds *)a);
2110 if (getinfo && ret >= 0) {
2111 SvCUR_set(astr, infosize);
2112 *SvEND(astr) = '\0';
2119 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
2124 I32 id, msize, flags;
2127 id = SvIVx(*++mark);
2129 flags = SvIVx(*++mark);
2130 mbuf = SvPV(mstr, len);
2131 if ((msize = len - sizeof(long)) < 0)
2132 Perl_croak(aTHX_ "Arg too short for msgsnd");
2134 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
2136 Perl_croak(aTHX_ "msgsnd not implemented");
2141 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
2147 I32 id, msize, flags, ret;
2150 id = SvIVx(*++mark);
2152 /* suppress warning when reading into undef var --jhi */
2154 sv_setpvn(mstr, "", 0);
2155 msize = SvIVx(*++mark);
2156 mtype = (long)SvIVx(*++mark);
2157 flags = SvIVx(*++mark);
2158 SvPV_force(mstr, len);
2159 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
2162 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
2164 SvCUR_set(mstr, sizeof(long)+ret);
2165 *SvEND(mstr) = '\0';
2166 #ifndef INCOMPLETE_TAINTS
2167 /* who knows who has been playing with this message? */
2173 Perl_croak(aTHX_ "msgrcv not implemented");
2178 Perl_do_semop(pTHX_ SV **mark, SV **sp)
2186 id = SvIVx(*++mark);
2188 opbuf = SvPV(opstr, opsize);
2189 if (opsize < 3 * SHORTSIZE
2190 || (opsize % (3 * SHORTSIZE))) {
2191 SETERRNO(EINVAL,LIB_INVARG);
2195 /* We can't assume that sizeof(struct sembuf) == 3 * sizeof(short). */
2197 int nsops = opsize / (3 * sizeof (short));
2199 short *ops = (short *) opbuf;
2201 struct sembuf *temps, *t;
2204 New (0, temps, nsops, struct sembuf);
2212 result = semop(id, temps, nsops);
2226 Perl_croak(aTHX_ "semop not implemented");
2231 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
2236 I32 id, mpos, msize;
2238 struct shmid_ds shmds;
2240 id = SvIVx(*++mark);
2242 mpos = SvIVx(*++mark);
2243 msize = SvIVx(*++mark);
2245 if (shmctl(id, IPC_STAT, &shmds) == -1)
2247 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
2248 SETERRNO(EFAULT,SS_ACCVIO); /* can't do as caller requested */
2251 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
2252 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2254 if (optype == OP_SHMREAD) {
2255 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2257 sv_setpvn(mstr, "", 0);
2258 SvPV_force(mstr, len);
2259 mbuf = SvGROW(mstr, msize+1);
2261 Copy(shm + mpos, mbuf, msize, char);
2262 SvCUR_set(mstr, msize);
2263 *SvEND(mstr) = '\0';
2265 #ifndef INCOMPLETE_TAINTS
2266 /* who knows who has been playing with this shared memory? */
2273 mbuf = SvPV(mstr, len);
2274 if ((n = len) > msize)
2276 Copy(mbuf, shm + mpos, n, char);
2278 memzero(shm + mpos + n, msize - n);
2282 Perl_croak(aTHX_ "shm I/O not implemented");
2286 #endif /* SYSV IPC */
2291 =for apidoc start_glob
2293 Function called by C<do_readline> to spawn a glob (or do the glob inside
2294 perl on VMS). This code used to be inline, but now perl uses C<File::Glob>
2295 this glob starter is only used by miniperl during the build process.
2296 Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up.
2302 Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
2304 SV *tmpcmd = NEWSV(55, 0);
2308 #ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
2309 /* since spawning off a process is a real performance hit */
2311 #include <descrip.h>
2312 #include <lib$routines.h>
2315 char rslt[NAM$C_MAXRSS+1+sizeof(unsigned short int)] = {'\0','\0'};
2316 char vmsspec[NAM$C_MAXRSS+1];
2317 char *rstr = rslt + sizeof(unsigned short int), *begin, *end, *cp;
2318 $DESCRIPTOR(dfltdsc,"SYS$DISK:[]*.*;");
2321 struct dsc$descriptor_s wilddsc
2322 = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
2323 struct dsc$descriptor_vs rsdsc
2324 = {sizeof rslt, DSC$K_DTYPE_VT, DSC$K_CLASS_VS, rslt};
2325 unsigned long int cxt = 0, sts = 0, ok = 1, hasdir = 0, hasver = 0, isunix = 0;
2327 /* We could find out if there's an explicit dev/dir or version
2328 by peeking into lib$find_file's internal context at
2329 ((struct NAM *)((struct FAB *)cxt)->fab$l_nam)->nam$l_fnb
2330 but that's unsupported, so I don't want to do it now and
2331 have it bite someone in the future. */
2332 cp = SvPV(tmpglob,i);
2334 if (cp[i] == ';') hasver = 1;
2336 if (sts) hasver = 1;
2340 hasdir = isunix = 1;
2343 if (cp[i] == ']' || cp[i] == '>' || cp[i] == ':') {
2348 if ((tmpfp = PerlIO_tmpfile()) != NULL) {
2350 if (!PerlLIO_stat(SvPVX(tmpglob),&st) && S_ISDIR(st.st_mode))
2351 ok = ((wilddsc.dsc$a_pointer = tovmspath(SvPVX(tmpglob),vmsspec)) != NULL);
2352 else ok = ((wilddsc.dsc$a_pointer = tovmsspec(SvPVX(tmpglob),vmsspec)) != NULL);
2353 if (ok) wilddsc.dsc$w_length = (unsigned short int) strlen(wilddsc.dsc$a_pointer);
2354 for (cp=wilddsc.dsc$a_pointer; ok && cp && *cp; cp++)
2355 if (*cp == '?') *cp = '%'; /* VMS style single-char wildcard */
2356 while (ok && ((sts = lib$find_file(&wilddsc,&rsdsc,&cxt,
2357 &dfltdsc,NULL,NULL,NULL))&1)) {
2358 /* with varying string, 1st word of buffer contains result length */
2359 end = rstr + *((unsigned short int*)rslt);
2360 if (!hasver) while (*end != ';' && end > rstr) end--;
2361 *(end++) = '\n'; *end = '\0';
2362 for (cp = rstr; *cp; cp++) *cp = _tolower(*cp);
2364 if (isunix) trim_unixpath(rstr,SvPVX(tmpglob),1);
2369 while (*(--begin) != ']' && *begin != '>') ;
2372 ok = (PerlIO_puts(tmpfp,begin) != EOF);
2374 if (cxt) (void)lib$find_file_end(&cxt);
2375 if (ok && sts != RMS$_NMF &&
2376 sts != RMS$_DNF && sts != RMS_FNF) ok = 0;
2379 SETERRNO((sts == RMS$_SYN ? EINVAL : EVMSERR),sts);
2381 PerlIO_close(tmpfp);
2385 PerlIO_rewind(tmpfp);
2386 IoTYPE(io) = IoTYPE_RDONLY;
2387 IoIFP(io) = fp = tmpfp;
2388 IoFLAGS(io) &= ~IOf_UNTAINT; /* maybe redundant */
2393 #ifdef MACOS_TRADITIONAL
2394 sv_setpv(tmpcmd, "glob ");
2395 sv_catsv(tmpcmd, tmpglob);
2396 sv_catpv(tmpcmd, " |");
2400 sv_setpv(tmpcmd, "for a in ");
2401 sv_catsv(tmpcmd, tmpglob);
2402 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
2405 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
2406 sv_catsv(tmpcmd, tmpglob);
2408 sv_setpv(tmpcmd, "perlglob ");
2409 sv_catsv(tmpcmd, tmpglob);
2410 sv_catpv(tmpcmd, " |");
2415 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
2416 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
2417 sv_catsv(tmpcmd, tmpglob);
2418 sv_catpv(tmpcmd, "' 2>/dev/null |");
2420 sv_setpv(tmpcmd, "echo ");
2421 sv_catsv(tmpcmd, tmpglob);
2423 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
2425 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
2428 #endif /* !DOSISH */
2429 #endif /* MACOS_TRADITIONAL */
2430 (void)do_open(PL_last_in_gv, SvPVX(tmpcmd), SvCUR(tmpcmd),
2431 FALSE, O_RDONLY, 0, Nullfp);