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,
75 return do_openn(gv, name, len, as_raw, rawmode, rawperm,
76 supplied_fp, &svs, 1);
80 Perl_do_openn(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
81 int rawmode, int rawperm, PerlIO *supplied_fp, SV **svp,
84 register IO *io = GvIOn(gv);
85 PerlIO *saveifp = Nullfp;
86 PerlIO *saveofp = Nullfp;
88 char savetype = IoTYPE_CLOSED;
93 bool was_fdopen = FALSE;
94 bool in_raw = 0, in_crlf = 0, out_raw = 0, out_crlf = 0;
96 char mode[PERL_MODE_MAX]; /* stdio file mode ("r\0", "rb\0", "r+b\0" etc.) */
99 Zero(mode,sizeof(mode),char);
100 PL_forkprocess = 1; /* assume true if no fork */
102 /* Collect default raw/crlf info from the op */
103 if (PL_op && PL_op->op_type == OP_OPEN) {
104 /* set up IO layers */
105 U8 flags = PL_op->op_private;
106 in_raw = (flags & OPpOPEN_IN_RAW);
107 in_crlf = (flags & OPpOPEN_IN_CRLF);
108 out_raw = (flags & OPpOPEN_OUT_RAW);
109 out_crlf = (flags & OPpOPEN_OUT_CRLF);
112 /* If currently open - close before we re-open */
114 fd = PerlIO_fileno(IoIFP(io));
115 if (IoTYPE(io) == IoTYPE_STD) {
116 /* This is a clone of one of STD* handles */
119 else if (fd >= 0 && fd <= PL_maxsysfd) {
120 /* This is one of the original STD* handles */
123 savetype = IoTYPE(io);
127 else if (IoTYPE(io) == IoTYPE_PIPE)
128 result = PerlProc_pclose(IoIFP(io));
129 else if (IoIFP(io) != IoOFP(io)) {
131 result = PerlIO_close(IoOFP(io));
132 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
135 result = PerlIO_close(IoIFP(io));
138 result = PerlIO_close(IoIFP(io));
139 if (result == EOF && fd > PL_maxsysfd) {
140 /* Why is this not Perl_warn*() call ? */
141 PerlIO_printf(Perl_error_log,
142 "Warning: unable to close filehandle %s properly.\n",
145 IoOFP(io) = IoIFP(io) = Nullfp;
149 /* sysopen style args, i.e. integer mode and permissions */
151 const int appendtrunc =
153 #ifdef O_APPEND /* Not fully portable. */
156 #ifdef O_TRUNC /* Not fully portable. */
160 const int modifyingmode = 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 */
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) ? savesvpv (*svp) : savepvn ("", 0);
239 if ((*type == IoTYPE_RDWR) && /* scary */
240 (*(type+1) == IoTYPE_RDONLY || *(type+1) == IoTYPE_WRONLY) &&
241 ((!num_svs || (tend > type+1 && tend[-1] != IoTYPE_PIPE)))) {
242 TAINT_PROPER("open");
247 if (*type == IoTYPE_PIPE) {
249 if (type[1] != IoTYPE_STD) {
251 Perl_croak(aTHX_ "Unknown open() mode '%.*s'", (int)olen, oname);
256 for (type++; isSPACE(*type); type++) ;
262 /* command is missing 19990114 */
263 if (ckWARN(WARN_PIPE))
264 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open");
268 if ((*name == '-' && name[1] == '\0') || num_svs)
270 TAINT_PROPER("piped open");
271 if (!num_svs && name[len-1] == '|') {
273 if (ckWARN(WARN_PIPE))
274 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Can't open bidirectional pipe");
280 strlcat(mode, "b", PERL_MODE_MAX);
282 strlcat(mode, "t", PERL_MODE_MAX);
290 fp = PerlProc_popen_list(mode, num_svs, svp);
293 fp = PerlProc_popen(name,mode);
297 if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) {
303 else if (*type == IoTYPE_WRONLY) {
304 TAINT_PROPER("open");
306 if (*type == IoTYPE_WRONLY) {
307 /* Two IoTYPE_WRONLYs in a row make for an IoTYPE_APPEND. */
308 mode[0] = IoTYPE(io) = IoTYPE_APPEND;
318 strlcat(mode, "b", PERL_MODE_MAX);
320 strlcat(mode, "t", PERL_MODE_MAX);
329 dodup = PERLIO_DUP_FD;
335 if (!num_svs && !*type && supplied_fp) {
336 /* "<+&" etc. is used by typemaps */
341 Perl_croak(aTHX_ "More than one argument to '%c&' open",IoTYPE(io));
344 for (; isSPACE(*type); type++) ;
345 if (num_svs && (SvIOK(*svp) || (SvPOK(*svp) && looks_like_number(*svp)))) {
349 else if (isDIGIT(*type)) {
355 thatio = sv_2io(*svp);
359 thatgv = gv_fetchpv(type,FALSE,SVt_PVIO);
360 thatio = GvIO(thatgv);
364 SETERRNO(EINVAL,SS_IVCHAN);
368 if ((that_fp = IoIFP(thatio))) {
369 /* Flush stdio buffer before dup. --mjd
370 * Unfortunately SEEK_CURing 0 seems to
371 * be optimized away on most platforms;
372 * only Solaris and Linux seem to flush
375 /* sfio fails to clear error on next
376 sfwrite, contrary to documentation.
378 if (PerlIO_seek(that_fp, 0, SEEK_CUR) == -1)
379 PerlIO_clearerr(that_fp);
381 /* On the other hand, do all platforms
382 * take gracefully to flushing a read-only
383 * filehandle? Perhaps we should do
384 * fsetpos(src)+fgetpos(dst)? --nik */
385 PerlIO_flush(that_fp);
386 fd = PerlIO_fileno(that_fp);
387 /* When dup()ing STDIN, STDOUT or STDERR
388 * explicitly set appropriate access mode */
389 if (that_fp == PerlIO_stdout()
390 || that_fp == PerlIO_stderr())
391 IoTYPE(io) = IoTYPE_WRONLY;
392 else if (that_fp == PerlIO_stdin())
393 IoTYPE(io) = IoTYPE_RDONLY;
394 /* When dup()ing a socket, say result is
396 else if (IoTYPE(thatio) == IoTYPE_SOCKET)
397 IoTYPE(io) = IoTYPE_SOCKET;
405 fp = PerlIO_fdupopen(aTHX_ that_fp, NULL, dodup);
409 fd = PerlLIO_dup(fd);
412 if (!(fp = PerlIO_openn(aTHX_ type,mode,fd,0,0,NULL,num_svs,svp))) {
421 for (; isSPACE(*type); type++) ;
422 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
425 fp = PerlIO_stdout();
426 IoTYPE(io) = IoTYPE_STD;
428 Perl_croak(aTHX_ "More than one argument to '>%c' open",IoTYPE_STD);
433 namesv = sv_2mortal(newSVpvn(type,strlen(type)));
438 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
441 if (!fp && type && *type && *type != ':' && !isIDFIRST(*type))
442 goto unknown_open_mode;
443 } /* IoTYPE_WRONLY */
444 else if (*type == IoTYPE_RDONLY) {
446 for (type++; isSPACE(*type); type++) ;
450 strlcat(mode, "b", PERL_MODE_MAX);
452 strlcat(mode, "t", PERL_MODE_MAX);
462 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
466 IoTYPE(io) = IoTYPE_STD;
468 Perl_croak(aTHX_ "More than one argument to '<%c' open",IoTYPE_STD);
473 namesv = sv_2mortal(newSVpvn(type,strlen(type)));
478 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
480 if (!fp && type && *type && *type != ':' && !isIDFIRST(*type))
481 goto unknown_open_mode;
482 } /* IoTYPE_RDONLY */
483 else if ((num_svs && /* '-|...' or '...|' */
484 type[0] == IoTYPE_STD && type[1] == IoTYPE_PIPE) ||
485 (!num_svs && tend > type+1 && tend[-1] == IoTYPE_PIPE)) {
487 type += 2; /* skip over '-|' */
491 while (tend > type && isSPACE(tend[-1]))
494 for (; isSPACE(*type); type++) ;
499 /* command is missing 19990114 */
500 if (ckWARN(WARN_PIPE))
501 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open");
505 if (!(*name == '-' && name[1] == '\0') || num_svs)
507 TAINT_PROPER("piped open");
512 strlcat(mode, "b", PERL_MODE_MAX);
514 strlcat(mode, "t", PERL_MODE_MAX);
523 fp = PerlProc_popen_list(mode,num_svs,svp);
526 fp = PerlProc_popen(name,mode);
528 IoTYPE(io) = IoTYPE_PIPE;
530 for (; isSPACE(*type); type++) ;
532 if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) {
538 else { /* layer(Args) */
540 goto unknown_open_mode;
542 IoTYPE(io) = IoTYPE_RDONLY;
544 for (; isSPACE(*name); name++) ;
549 strlcat(mode, "b", PERL_MODE_MAX);
551 strlcat(mode, "t", PERL_MODE_MAX);
559 if (*name == '-' && name[1] == '\0') {
561 IoTYPE(io) = IoTYPE_STD;
565 namesv = sv_2mortal(newSVpvn(type,strlen(type)));
570 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
575 if (ckWARN(WARN_NEWLINE) && IoTYPE(io) == IoTYPE_RDONLY && strchr(name, '\n'))
576 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
580 if (ckWARN(WARN_IO)) {
581 if ((IoTYPE(io) == IoTYPE_RDONLY) &&
582 (fp == PerlIO_stdout() || fp == PerlIO_stderr())) {
583 Perl_warner(aTHX_ packWARN(WARN_IO),
584 "Filehandle STD%s reopened as %s only for input",
585 ((fp == PerlIO_stdout()) ? "OUT" : "ERR"),
588 else if ((IoTYPE(io) == IoTYPE_WRONLY) && fp == PerlIO_stdin()) {
589 Perl_warner(aTHX_ packWARN(WARN_IO),
590 "Filehandle STDIN reopened as %s only for output",
595 fd = PerlIO_fileno(fp);
596 /* If there is no fd (e.g. PerlIO::scalar) assume it isn't a
597 * socket - this covers PerlIO::scalar - otherwise unless we "know" the
598 * type probe for socket-ness.
600 if (IoTYPE(io) && IoTYPE(io) != IoTYPE_PIPE && IoTYPE(io) != IoTYPE_STD && fd >= 0) {
601 if (PerlLIO_fstat(fd,&PL_statbuf) < 0) {
602 /* If PerlIO claims to have fd we had better be able to fstat() it. */
603 (void) PerlIO_close(fp);
607 if (S_ISSOCK(PL_statbuf.st_mode))
608 IoTYPE(io) = IoTYPE_SOCKET; /* in case a socket was passed in to us */
612 !(PL_statbuf.st_mode & S_IFMT)
616 && IoTYPE(io) != IoTYPE_WRONLY /* Dups of STD* filehandles already have */
617 && IoTYPE(io) != IoTYPE_RDONLY /* type so they aren't marked as sockets */
618 ) { /* on OS's that return 0 on fstat()ed pipe */
620 Sock_size_t buflen = sizeof tmpbuf;
621 if (PerlSock_getsockname(fd, (struct sockaddr *)tmpbuf, &buflen) >= 0
622 || errno != ENOTSOCK)
623 IoTYPE(io) = IoTYPE_SOCKET; /* some OS's return 0 on fstat()ed socket */
624 /* but some return 0 for streams too, sigh */
626 #endif /* HAS_SOCKET */
627 #endif /* !PERL_MICRO */
631 * If this is a standard handle we discard all the layer stuff
632 * and just dup the fd into whatever was on the handle before !
635 if (saveifp) { /* must use old fp? */
636 /* If fd is less that PL_maxsysfd i.e. STDIN..STDERR
637 then dup the new fileno down
640 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
641 if (saveofp != saveifp) { /* was a socket? */
642 PerlIO_close(saveofp);
646 /* Still a small can-of-worms here if (say) PerlIO::scalar
647 is assigned to (say) STDOUT - for now let dup2() fail
648 and provide the error
650 if (PerlLIO_dup2(fd, savefd) < 0) {
651 (void)PerlIO_close(fp);
655 if (savefd != PerlIO_fileno(PerlIO_stdin())) {
656 char newname[FILENAME_MAX+1];
657 if (PerlIO_getname(fp, newname)) {
658 if (fd == PerlIO_fileno(PerlIO_stdout()))
659 Perl_vmssetuserlnm(aTHX_ "SYS$OUTPUT", newname);
660 if (fd == PerlIO_fileno(PerlIO_stderr()))
661 Perl_vmssetuserlnm(aTHX_ "SYS$ERROR", newname);
667 /* PL_fdpid isn't used on Windows, so avoid this useless work.
668 * XXX Probably the same for a lot of other places. */
674 sv = *av_fetch(PL_fdpid,fd,TRUE);
675 (void)SvUPGRADE(sv, SVt_IV);
678 sv = *av_fetch(PL_fdpid,savefd,TRUE);
679 (void)SvUPGRADE(sv, SVt_IV);
686 /* need to close fp without closing underlying fd */
687 int ofd = PerlIO_fileno(fp);
688 int dupfd = PerlLIO_dup(ofd);
689 #if defined(HAS_FCNTL) && defined(F_SETFD)
690 /* Assume if we have F_SETFD we have F_GETFD */
691 int coe = fcntl(ofd,F_GETFD);
694 PerlLIO_dup2(dupfd,ofd);
695 #if defined(HAS_FCNTL) && defined(F_SETFD)
696 /* The dup trick has lost close-on-exec on ofd */
697 fcntl(ofd,F_SETFD, coe);
699 PerlLIO_close(dupfd);
706 fd = PerlIO_fileno(fp);
708 #if defined(HAS_FCNTL) && defined(F_SETFD)
710 int save_errno = errno;
711 fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
717 IoFLAGS(io) &= ~IOf_NOLINE;
719 if (IoTYPE(io) == IoTYPE_SOCKET
720 || (IoTYPE(io) == IoTYPE_WRONLY && fd >= 0 && S_ISCHR(PL_statbuf.st_mode)) ) {
722 if (*s == IoTYPE_IMPLICIT || *s == IoTYPE_NUMERIC)
725 if (!(IoOFP(io) = PerlIO_openn(aTHX_ type,s,fd,0,0,NULL,0,svp))) {
739 IoTYPE(io) = savetype;
744 Perl_nextargv(pTHX_ register GV *gv)
747 #ifndef FLEXFILENAMES
756 PL_argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO);
757 if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
758 IoFLAGS(io) &= ~IOf_START;
760 if (!PL_argvout_stack)
761 PL_argvout_stack = newAV();
762 av_push(PL_argvout_stack, SvREFCNT_inc(PL_defoutgv));
765 if (PL_filemode & (S_ISUID|S_ISGID)) {
766 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
769 (void)fchmod(PL_lastfd,PL_filemode);
771 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
778 while (av_len(GvAV(gv)) >= 0) {
780 sv = av_shift(GvAV(gv));
782 sv_setsv(GvSV(gv),sv);
783 SvSETMAGIC(GvSV(gv));
784 PL_oldname = SvPVx(GvSV(gv), oldlen);
785 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,Nullfp)) {
787 TAINT_PROPER("inplace open");
788 if (oldlen == 1 && *PL_oldname == '-') {
789 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
790 return IoIFP(GvIOp(gv));
792 #ifndef FLEXFILENAMES
793 filedev = PL_statbuf.st_dev;
794 fileino = PL_statbuf.st_ino;
796 PL_filemode = PL_statbuf.st_mode;
797 fileuid = PL_statbuf.st_uid;
798 filegid = PL_statbuf.st_gid;
799 if (!S_ISREG(PL_filemode)) {
800 if (ckWARN_d(WARN_INPLACE))
801 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
802 "Can't do inplace edit: %s is not a regular file",
808 char *star = strchr(PL_inplace, '*');
810 char *begin = PL_inplace;
811 sv_setpvn(sv, "", 0);
813 sv_catpvn(sv, begin, star - begin);
814 sv_catpvn(sv, PL_oldname, oldlen);
816 } while ((star = strchr(begin, '*')));
821 sv_catpv(sv,PL_inplace);
823 #ifndef FLEXFILENAMES
824 if ((PerlLIO_stat(SvPVX(sv),&PL_statbuf) >= 0
825 && PL_statbuf.st_dev == filedev
826 && PL_statbuf.st_ino == fileino)
828 || ((_djstat_fail_bits & _STFAIL_TRUENAME)!=0)
832 if (ckWARN_d(WARN_INPLACE))
833 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
834 "Can't do inplace edit: %"SVf" would not be unique",
841 #if !defined(DOSISH) && !defined(__CYGWIN__) && !defined(EPOC)
842 if (PerlLIO_rename(PL_oldname,SvPVX(sv)) < 0) {
843 if (ckWARN_d(WARN_INPLACE))
844 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
845 "Can't rename %s to %"SVf": %s, skipping file",
846 PL_oldname, sv, Strerror(errno) );
852 (void)PerlLIO_unlink(SvPVX(sv));
853 (void)PerlLIO_rename(PL_oldname,SvPVX(sv));
854 do_open(gv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,O_RDONLY,0,Nullfp);
857 (void)UNLINK(SvPVX(sv));
858 if (link(PL_oldname,SvPVX(sv)) < 0) {
859 if (ckWARN_d(WARN_INPLACE))
860 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
861 "Can't rename %s to %"SVf": %s, skipping file",
862 PL_oldname, sv, Strerror(errno) );
866 (void)UNLINK(PL_oldname);
870 #if !defined(DOSISH) && !defined(AMIGAOS)
871 # ifndef VMS /* Don't delete; use automatic file versioning */
872 if (UNLINK(PL_oldname) < 0) {
873 if (ckWARN_d(WARN_INPLACE))
874 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
875 "Can't remove %s: %s, skipping file",
876 PL_oldname, Strerror(errno) );
882 Perl_croak(aTHX_ "Can't do inplace edit without backup");
886 sv_setpvn(sv,">",!PL_inplace);
887 sv_catpvn(sv,PL_oldname,oldlen);
888 SETERRNO(0,0); /* in case sprintf set errno */
890 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
891 O_WRONLY|O_CREAT|O_TRUNC,0,Nullfp))
893 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
894 O_WRONLY|O_CREAT|OPEN_EXCL,0666,Nullfp))
897 if (ckWARN_d(WARN_INPLACE))
898 Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't do inplace edit on %s: %s",
899 PL_oldname, Strerror(errno) );
903 setdefout(PL_argvoutgv);
904 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
905 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
907 (void)fchmod(PL_lastfd,PL_filemode);
909 # if !(defined(WIN32) && defined(__BORLANDC__))
910 /* Borland runtime creates a readonly file! */
911 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
914 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
916 (void)fchown(PL_lastfd,fileuid,filegid);
919 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
924 return IoIFP(GvIOp(gv));
927 if (ckWARN_d(WARN_INPLACE)) {
928 const int eno = errno;
929 if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0
930 && !S_ISREG(PL_statbuf.st_mode))
932 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
933 "Can't do inplace edit: %s is not a regular file",
937 Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't open %s: %s",
938 PL_oldname, Strerror(eno));
942 if (io && (IoFLAGS(io) & IOf_ARGV))
943 IoFLAGS(io) |= IOf_START;
945 (void)do_close(PL_argvoutgv,FALSE);
946 if (io && (IoFLAGS(io) & IOf_ARGV)
947 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
949 GV *oldout = (GV*)av_pop(PL_argvout_stack);
951 SvREFCNT_dec(oldout);
954 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
961 Perl_do_pipe(pTHX_ SV *sv, GV *rgv, GV *wgv)
980 if (PerlProc_pipe(fd) < 0)
982 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"PIPE_OPEN_MODE);
983 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"PIPE_OPEN_MODE);
984 IoOFP(rstio) = IoIFP(rstio);
985 IoIFP(wstio) = IoOFP(wstio);
986 IoTYPE(rstio) = IoTYPE_RDONLY;
987 IoTYPE(wstio) = IoTYPE_WRONLY;
988 if (!IoIFP(rstio) || !IoOFP(wstio)) {
989 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
990 else PerlLIO_close(fd[0]);
991 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
992 else PerlLIO_close(fd[1]);
996 sv_setsv(sv,&PL_sv_yes);
1000 sv_setsv(sv,&PL_sv_undef);
1005 /* explicit renamed to avoid C++ conflict -- kja */
1007 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
1014 if (!gv || SvTYPE(gv) != SVt_PVGV) {
1016 SETERRNO(EBADF,SS_IVCHAN);
1020 if (!io) { /* never opened */
1022 if (ckWARN(WARN_UNOPENED)) /* no check for closed here */
1023 report_evil_fh(gv, io, PL_op->op_type);
1024 SETERRNO(EBADF,SS_IVCHAN);
1028 retval = io_close(io, not_implicit);
1032 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1034 IoTYPE(io) = IoTYPE_CLOSED;
1039 Perl_io_close(pTHX_ IO *io, bool not_implicit)
1041 bool retval = FALSE;
1044 if (IoTYPE(io) == IoTYPE_PIPE) {
1045 const int status = PerlProc_pclose(IoIFP(io));
1047 STATUS_NATIVE_SET(status);
1048 retval = (STATUS_POSIX == 0);
1051 retval = (status != -1);
1054 else if (IoTYPE(io) == IoTYPE_STD)
1057 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
1058 bool prev_err = PerlIO_error(IoOFP(io));
1059 retval = (PerlIO_close(IoOFP(io)) != EOF && !prev_err);
1060 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
1063 bool prev_err = PerlIO_error(IoIFP(io));
1064 retval = (PerlIO_close(IoIFP(io)) != EOF && !prev_err);
1067 IoOFP(io) = IoIFP(io) = Nullfp;
1069 else if (not_implicit) {
1070 SETERRNO(EBADF,SS_IVCHAN);
1077 Perl_do_eof(pTHX_ GV *gv)
1086 else if (ckWARN(WARN_IO) && (IoTYPE(io) == IoTYPE_WRONLY))
1087 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1092 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
1093 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
1094 return FALSE; /* this is the most usual case */
1097 saverrno = errno; /* getc and ungetc can stomp on errno */
1098 ch = PerlIO_getc(IoIFP(io));
1100 (void)PerlIO_ungetc(IoIFP(io),ch);
1106 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
1107 if (PerlIO_get_cnt(IoIFP(io)) < -1)
1108 PerlIO_set_cnt(IoIFP(io),-1);
1110 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
1111 if (gv != PL_argvgv || !nextargv(gv)) /* get another fp handy */
1115 return TRUE; /* normal fp, definitely end of file */
1121 Perl_do_tell(pTHX_ GV *gv)
1123 register IO *io = 0;
1124 register PerlIO *fp;
1126 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1127 #ifdef ULTRIX_STDIO_BOTCH
1129 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1131 return PerlIO_tell(fp);
1133 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1134 report_evil_fh(gv, io, PL_op->op_type);
1135 SETERRNO(EBADF,RMS_IFI);
1140 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
1142 register IO *io = 0;
1143 register PerlIO *fp;
1145 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1146 #ifdef ULTRIX_STDIO_BOTCH
1148 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1150 return PerlIO_seek(fp, pos, whence) >= 0;
1152 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1153 report_evil_fh(gv, io, PL_op->op_type);
1154 SETERRNO(EBADF,RMS_IFI);
1159 Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
1161 register IO *io = 0;
1162 register PerlIO *fp;
1164 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
1165 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
1166 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1167 report_evil_fh(gv, io, PL_op->op_type);
1168 SETERRNO(EBADF,RMS_IFI);
1173 Perl_mode_from_discipline(pTHX_ SV *discp)
1175 int mode = O_BINARY;
1178 const char *s = SvPV(discp,len);
1183 if (s[2] == 'a' && s[3] == 'w'
1184 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1193 if (s[2] == 'r' && s[3] == 'l' && s[4] == 'f'
1194 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1203 goto fail_discipline;
1206 else if (isSPACE(*s)) {
1213 end = strchr(s+1, ':');
1216 #ifndef PERLIO_LAYERS
1217 Perl_croak(aTHX_ "IO layers (like '%.*s') unavailable", end-s, s);
1229 Perl_do_binmode(pTHX_ PerlIO *fp, int iotype, int mode)
1231 /* The old body of this is now in non-LAYER part of perlio.c
1232 * This is a stub for any XS code which might have been calling it.
1234 const char *name = ":raw";
1235 #ifdef PERLIO_USING_CRLF
1236 if (!(mode & O_BINARY))
1239 return PerlIO_binmode(aTHX_ fp, iotype, mode, name);
1242 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE)
1243 I32 my_chsize(fd, length)
1244 I32 fd; /* file descriptor */
1245 Off_t length; /* length to set file to */
1248 /* code courtesy of William Kucharski */
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)
1293 DIE(aTHX_ "truncate not implemented");
1294 #endif /* F_FREESP */
1296 #endif /* !HAS_TRUNCATE && !HAS_CHSIZE */
1299 Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
1301 register const char *tmps;
1304 /* assuming fp is checked earlier */
1310 if (SvIOK(sv) && SvIVX(sv) != 0) {
1311 PerlIO_printf(fp, PL_ofmt, (NV)SvIVX(sv));
1312 return !PerlIO_error(fp);
1314 if ( (SvNOK(sv) && SvNVX(sv) != 0.0)
1315 || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) {
1316 PerlIO_printf(fp, PL_ofmt, SvNVX(sv));
1317 return !PerlIO_error(fp);
1320 switch (SvTYPE(sv)) {
1322 if (ckWARN(WARN_UNINITIALIZED))
1330 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
1332 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
1333 return !PerlIO_error(fp);
1337 if (PerlIO_isutf8(fp)) {
1339 sv_utf8_upgrade_flags(sv = sv_mortalcopy(sv),
1340 SV_GMAGIC|SV_UTF8_NO_ENCODING);
1342 else if (DO_UTF8(sv)) {
1343 if (!sv_utf8_downgrade((sv = sv_mortalcopy(sv)), TRUE)
1344 && ckWARN_d(WARN_UTF8))
1346 Perl_warner(aTHX_ packWARN(WARN_UTF8), "Wide character in print");
1349 tmps = SvPV(sv, len);
1352 /* To detect whether the process is about to overstep its
1353 * filesize limit we would need getrlimit(). We could then
1354 * also transparently raise the limit with setrlimit() --
1355 * but only until the system hard limit/the filesystem limit,
1356 * at which we would get EPERM. Note that when using buffered
1357 * io the write failure can be delayed until the flush/close. --jhi */
1358 if (len && (PerlIO_write(fp,tmps,len) == 0))
1360 return !PerlIO_error(fp);
1370 if (PL_op->op_flags & OPf_REF) {
1375 if (io && IoIFP(io)) {
1377 sv_setpvn(PL_statname,"", 0);
1378 PL_laststype = OP_STAT;
1379 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
1383 return PL_laststatval;
1384 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1385 report_evil_fh(gv, io, PL_op->op_type);
1387 sv_setpvn(PL_statname,"", 0);
1388 return (PL_laststatval = -1);
1396 if (SvTYPE(sv) == SVt_PVGV) {
1400 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
1407 sv_setpvn(PL_statname, s, len);
1408 s = SvPVX(PL_statname); /* s now NUL-terminated */
1409 PL_laststype = OP_STAT;
1410 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1411 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
1412 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
1413 return PL_laststatval;
1423 if (PL_op->op_flags & OPf_REF) {
1425 if (cGVOP_gv == PL_defgv) {
1426 if (PL_laststype != OP_LSTAT)
1427 Perl_croak(aTHX_ "The stat preceding -l _ wasn't an lstat");
1428 return PL_laststatval;
1430 if (ckWARN(WARN_IO)) {
1431 Perl_warner(aTHX_ packWARN(WARN_IO), "Use of -l on filehandle %s",
1433 return (PL_laststatval = -1);
1437 PL_laststype = OP_LSTAT;
1441 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV && ckWARN(WARN_IO)) {
1442 Perl_warner(aTHX_ packWARN(WARN_IO), "Use of -l on filehandle %s",
1443 GvENAME((GV*) SvRV(sv)));
1444 return (PL_laststatval = -1);
1446 /* XXX Do really need to be calling SvPV() all these times? */
1447 sv_setpv(PL_statname,SvPV(sv, n_a));
1448 PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache);
1449 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
1450 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "lstat");
1451 return PL_laststatval;
1456 Perl_do_aexec(pTHX_ SV *really, register SV **mark, register SV **sp)
1458 return do_aexec5(really, mark, sp, 0, 0);
1463 Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1464 int fd, int do_report)
1466 #ifdef MACOS_TRADITIONAL
1467 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1470 const char *tmps = Nullch;
1474 New(401,PL_Argv, sp - mark + 1, char*);
1476 while (++mark <= sp) {
1478 *a++ = SvPVx(*mark, n_a);
1484 tmps = SvPV(really, n_a);
1485 if ((!really && *PL_Argv[0] != '/') ||
1486 (really && *tmps != '/')) /* will execvp use PATH? */
1487 TAINT_ENV(); /* testing IFS here is overkill, probably */
1489 if (really && *tmps)
1490 PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
1492 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1494 if (ckWARN(WARN_EXEC))
1495 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
1496 (really ? tmps : PL_Argv[0]), Strerror(errno));
1500 PerlLIO_write(fd, (void*)&e, sizeof(int));
1510 Perl_do_execfree(pTHX)
1514 PL_Argv = Null(char **);
1522 #if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC) && !defined(MACOS_TRADITIONAL)
1525 Perl_do_exec(pTHX_ char *cmd)
1527 return do_exec3(cmd,0,0);
1531 Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report)
1536 while (*cmd && isSPACE(*cmd))
1539 /* save an extra exec if possible */
1543 char flags[PERL_FLAGS_MAX];
1544 if (strnEQ(cmd,PL_cshname,PL_cshlen) &&
1545 strnEQ(cmd+PL_cshlen," -c",3)) {
1547 strlcpy(flags, "-c", PERL_FLAGS_MAX);
1551 s = cmd+PL_cshlen+3;
1555 strlcat(flags, "f", PERL_FLAGS_MAX);
1569 if (s[-1] == '\'') {
1572 PerlProc_execl(PL_cshname,"csh", flags, ncmd, (char*)0);
1582 /* see if there are shell metacharacters in it */
1584 if (*cmd == '.' && isSPACE(cmd[1]))
1587 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1590 for (s = cmd; *s && isALNUM(*s); s++) ; /* catch VAR=val gizmo */
1594 for (s = cmd; *s; s++) {
1595 if (*s != ' ' && !isALPHA(*s) &&
1596 strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1597 if (*s == '\n' && !s[1]) {
1601 /* handle the 2>&1 construct at the end */
1602 if (*s == '>' && s[1] == '&' && s[2] == '1'
1603 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1604 && (!s[3] || isSPACE(s[3])))
1606 const char *t = s + 3;
1608 while (*t && isSPACE(*t))
1610 if (!*t && (PerlLIO_dup2(1,2) != -1)) {
1617 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
1623 New(402,PL_Argv, (s - cmd) / 2 + 2, char*);
1624 PL_Cmd = savepvn(cmd, s-cmd);
1626 for (s = PL_Cmd; *s;) {
1627 while (*s && isSPACE(*s)) s++;
1630 while (*s && !isSPACE(*s)) s++;
1637 PerlProc_execvp(PL_Argv[0],PL_Argv);
1639 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1644 if (ckWARN(WARN_EXEC))
1645 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
1646 PL_Argv[0], Strerror(errno));
1649 PerlLIO_write(fd, (void*)&e, sizeof(int));
1658 #endif /* OS2 || WIN32 */
1661 Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
1664 register I32 tot = 0;
1667 SV **oldmark = mark;
1670 #define APPLY_TAINT_PROPER() \
1672 if (PL_tainted) { TAINT_PROPER(what); } \
1675 /* This is a first heuristic; it doesn't catch tainting magic. */
1677 while (++mark <= sp) {
1678 if (SvTAINTED(*mark)) {
1688 APPLY_TAINT_PROPER();
1691 APPLY_TAINT_PROPER();
1693 while (++mark <= sp) {
1694 const char *name = SvPVx(*mark, n_a);
1695 APPLY_TAINT_PROPER();
1696 if (PerlLIO_chmod(name, val))
1704 APPLY_TAINT_PROPER();
1705 if (sp - mark > 2) {
1707 val = SvIVx(*++mark);
1708 val2 = SvIVx(*++mark);
1709 APPLY_TAINT_PROPER();
1711 while (++mark <= sp) {
1712 const char *name = SvPVx(*mark, n_a);
1713 APPLY_TAINT_PROPER();
1714 if (PerlLIO_chown(name, val, val2))
1721 XXX Should we make lchown() directly available from perl?
1722 For now, we'll let Configure test for HAS_LCHOWN, but do
1723 nothing in the core.
1729 APPLY_TAINT_PROPER();
1732 s = SvPVx(*++mark, n_a);
1734 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1736 if ((val = whichsig(s)) < 0)
1737 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
1741 APPLY_TAINT_PROPER();
1744 /* kill() doesn't do process groups (job trees?) under VMS */
1745 if (val < 0) val = -val;
1746 if (val == SIGKILL) {
1747 # include <starlet.h>
1748 /* Use native sys$delprc() to insure that target process is
1749 * deleted; supervisor-mode images don't pay attention to
1750 * CRTL's emulation of Unix-style signals and kill()
1752 while (++mark <= sp) {
1753 I32 proc = SvIVx(*mark);
1754 register unsigned long int __vmssts;
1755 APPLY_TAINT_PROPER();
1756 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1760 case SS$_NOSUCHNODE:
1761 SETERRNO(ESRCH,__vmssts);
1764 SETERRNO(EPERM,__vmssts);
1767 SETERRNO(EVMSERR,__vmssts);
1776 while (++mark <= sp) {
1777 I32 proc = SvIVx(*mark);
1778 APPLY_TAINT_PROPER();
1780 if (PerlProc_killpg(proc,val)) /* BSD */
1782 if (PerlProc_kill(-proc,val)) /* SYSV */
1788 while (++mark <= sp) {
1789 I32 proc = SvIVx(*mark);
1790 APPLY_TAINT_PROPER();
1791 if (PerlProc_kill(proc, val))
1799 APPLY_TAINT_PROPER();
1801 while (++mark <= sp) {
1802 s = SvPVx(*mark, n_a);
1803 APPLY_TAINT_PROPER();
1804 if (PL_euid || PL_unsafe) {
1808 else { /* don't let root wipe out directories without -U */
1809 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1821 APPLY_TAINT_PROPER();
1822 if (sp - mark > 2) {
1823 #if defined(I_UTIME) || defined(VMS)
1824 struct utimbuf utbuf;
1825 struct utimbuf *utbufp = &utbuf;
1831 void *utbufp = &utbuf;
1834 SV* accessed = *++mark;
1835 SV* modified = *++mark;
1837 /* Be like C, and if both times are undefined, let the C
1838 * library figure out what to do. This usually means
1839 * "current time". */
1841 if ( accessed == &PL_sv_undef && modified == &PL_sv_undef )
1844 Zero(&utbuf, sizeof utbuf, char);
1846 utbuf.actime = (Time_t)SvNVx(accessed); /* time accessed */
1847 utbuf.modtime = (Time_t)SvNVx(modified); /* time modified */
1849 utbuf.actime = (Time_t)SvIVx(accessed); /* time accessed */
1850 utbuf.modtime = (Time_t)SvIVx(modified); /* time modified */
1853 APPLY_TAINT_PROPER();
1855 while (++mark <= sp) {
1857 const char *name = SvPVx(*mark, n_a);
1858 APPLY_TAINT_PROPER();
1859 if (PerlLIO_utime(name, utbufp))
1870 #undef APPLY_TAINT_PROPER
1873 /* Do the permissions allow some operation? Assumes statcache already set. */
1874 #ifndef VMS /* VMS' cando is in vms.c */
1876 Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register const Stat_t *statbufp)
1877 /* Note: we use `effective' both for uids and gids.
1878 * Here we are betting on Uid_t being equal or wider than Gid_t. */
1881 /* [Comments and code from Len Reed]
1882 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1883 * to write-protected files. The execute permission bit is set
1884 * by the Miscrosoft C library stat() function for the following:
1889 * All files and directories are readable.
1890 * Directories and special files, e.g. "CON", cannot be
1892 * [Comment by Tom Dinger -- a directory can have the write-protect
1893 * bit set in the file system, but DOS permits changes to
1894 * the directory anyway. In addition, all bets are off
1895 * here for networked software, such as Novell and
1899 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1900 * too so it will actually look into the files for magic numbers
1902 return (mode & statbufp->st_mode) ? TRUE : FALSE;
1904 #else /* ! DOSISH */
1905 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
1906 if (mode == S_IXUSR) {
1907 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1911 return TRUE; /* root reads and writes anything */
1914 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1915 if (statbufp->st_mode & mode)
1916 return TRUE; /* ok as "user" */
1918 else if (ingroup(statbufp->st_gid,effective)) {
1919 if (statbufp->st_mode & mode >> 3)
1920 return TRUE; /* ok as "group" */
1922 else if (statbufp->st_mode & mode >> 6)
1923 return TRUE; /* ok as "other" */
1925 #endif /* ! DOSISH */
1930 Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective)
1932 #ifdef MACOS_TRADITIONAL
1933 /* This is simply not correct for AppleShare, but fix it yerself. */
1936 if (testgid == (effective ? PL_egid : PL_gid))
1938 #ifdef HAS_GETGROUPS
1943 Groups_t gary[NGROUPS];
1946 anum = getgroups(NGROUPS,gary);
1948 if (gary[anum] == testgid)
1956 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1959 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
1961 key_t key = (key_t)SvNVx(*++mark);
1962 const I32 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1963 const I32 flags = SvIVx(*++mark);
1971 return msgget(key, flags);
1975 return semget(key, n, flags);
1979 return shmget(key, n, flags);
1981 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1983 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1986 return -1; /* should never happen */
1990 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
1994 I32 infosize, getinfo;
1996 const I32 id = SvIVx(*++mark);
1997 const I32 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1998 const I32 cmd = SvIVx(*++mark);
2003 getinfo = (cmd == IPC_STAT);
2009 if (cmd == IPC_STAT || cmd == IPC_SET)
2010 infosize = sizeof(struct msqid_ds);
2015 if (cmd == IPC_STAT || cmd == IPC_SET)
2016 infosize = sizeof(struct shmid_ds);
2022 if (cmd == IPC_STAT || cmd == IPC_SET)
2023 infosize = sizeof(struct semid_ds);
2024 else if (cmd == GETALL || cmd == SETALL)
2026 struct semid_ds semds;
2028 #ifdef EXTRA_F_IN_SEMUN_BUF
2029 semun.buff = &semds;
2033 getinfo = (cmd == GETALL);
2034 if (Semctl(id, 0, IPC_STAT, semun) == -1)
2036 infosize = semds.sem_nsems * sizeof(short);
2037 /* "short" is technically wrong but much more portable
2038 than guessing about u_?short(_t)? */
2041 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2045 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
2047 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2056 SvPV_force(astr, len);
2057 a = SvGROW(astr, infosize+1);
2061 a = SvPV(astr, len);
2062 if (len != infosize)
2063 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
2072 a = INT2PTR(char *,i); /* ouch */
2079 ret = msgctl(id, cmd, (struct msqid_ds *)a);
2085 union semun unsemds;
2087 #ifdef EXTRA_F_IN_SEMUN_BUF
2088 unsemds.buff = (struct semid_ds *)a;
2090 unsemds.buf = (struct semid_ds *)a;
2092 ret = Semctl(id, n, cmd, unsemds);
2094 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2101 ret = shmctl(id, cmd, (struct shmid_ds *)a);
2105 if (getinfo && ret >= 0) {
2106 SvCUR_set(astr, infosize);
2107 *SvEND(astr) = '\0';
2114 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
2121 const I32 id = SvIVx(*++mark);
2125 flags = SvIVx(*++mark);
2126 mbuf = SvPV(mstr, len);
2127 if ((msize = len - sizeof(long)) < 0)
2128 Perl_croak(aTHX_ "Arg too short for msgsnd");
2130 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
2132 Perl_croak(aTHX_ "msgsnd not implemented");
2137 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
2143 I32 msize, flags, ret;
2145 const I32 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)
2181 const I32 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 const 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)
2235 struct shmid_ds shmds;
2236 const I32 id = SvIVx(*++mark);
2240 mpos = SvIVx(*++mark);
2241 msize = SvIVx(*++mark);
2243 if (shmctl(id, IPC_STAT, &shmds) == -1)
2245 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
2246 SETERRNO(EFAULT,SS_ACCVIO); /* can't do as caller requested */
2249 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
2250 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2252 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 const char *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);