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 const 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_openn(pTHX_ GV *gv, register const char *oname, I32 len, int as_raw,
71 int rawmode, int rawperm, PerlIO *supplied_fp, SV **svp,
75 register IO * const io = GvIOn(gv);
76 PerlIO *saveifp = Nullfp;
77 PerlIO *saveofp = Nullfp;
79 char savetype = IoTYPE_CLOSED;
84 bool was_fdopen = FALSE;
85 bool in_raw = 0, in_crlf = 0, out_raw = 0, out_crlf = 0;
87 char mode[PERL_MODE_MAX]; /* stdio file mode ("r\0", "rb\0", "r+b\0" etc.) */
90 Zero(mode,sizeof(mode),char);
91 PL_forkprocess = 1; /* assume true if no fork */
93 /* Collect default raw/crlf info from the op */
94 if (PL_op && PL_op->op_type == OP_OPEN) {
95 /* set up IO layers */
96 const U8 flags = PL_op->op_private;
97 in_raw = (flags & OPpOPEN_IN_RAW);
98 in_crlf = (flags & OPpOPEN_IN_CRLF);
99 out_raw = (flags & OPpOPEN_OUT_RAW);
100 out_crlf = (flags & OPpOPEN_OUT_CRLF);
103 /* If currently open - close before we re-open */
105 fd = PerlIO_fileno(IoIFP(io));
106 if (IoTYPE(io) == IoTYPE_STD) {
107 /* This is a clone of one of STD* handles */
110 else if (fd >= 0 && fd <= PL_maxsysfd) {
111 /* This is one of the original STD* handles */
114 savetype = IoTYPE(io);
118 else if (IoTYPE(io) == IoTYPE_PIPE)
119 result = PerlProc_pclose(IoIFP(io));
120 else if (IoIFP(io) != IoOFP(io)) {
122 result = PerlIO_close(IoOFP(io));
123 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
126 result = PerlIO_close(IoIFP(io));
129 result = PerlIO_close(IoIFP(io));
130 if (result == EOF && fd > PL_maxsysfd) {
131 /* Why is this not Perl_warn*() call ? */
132 PerlIO_printf(Perl_error_log,
133 "Warning: unable to close filehandle %s properly.\n",
136 IoOFP(io) = IoIFP(io) = Nullfp;
140 /* sysopen style args, i.e. integer mode and permissions */
142 const int appendtrunc =
144 #ifdef O_APPEND /* Not fully portable. */
147 #ifdef O_TRUNC /* Not fully portable. */
151 const int modifyingmode = O_WRONLY|O_RDWR|O_CREAT|appendtrunc;
155 Perl_croak(aTHX_ "panic: sysopen with multiple args");
163 It might be (in OS/390 and Mac OS Classic it is)
169 This means that simple & with O_RDWR would look
170 like O_RDONLY is present. Therefore we have to
173 if ((ismodifying = (rawmode & modifyingmode))) {
174 if ((ismodifying & O_WRONLY) == O_WRONLY ||
175 (ismodifying & O_RDWR) == O_RDWR ||
176 (ismodifying & (O_CREAT|appendtrunc)))
177 TAINT_PROPER("sysopen");
179 mode[ix++] = IoTYPE_NUMERIC; /* Marker to openn to use numeric "sysopen" */
181 #if defined(USE_64_BIT_RAWIO) && defined(O_LARGEFILE)
182 rawmode |= O_LARGEFILE; /* Transparently largefiley. */
185 IoTYPE(io) = PerlIO_intmode2str(rawmode, &mode[ix], &writing);
187 namesv = sv_2mortal(newSVpvn(oname,strlen(oname)));
191 fp = PerlIO_openn(aTHX_ type, mode, -1, rawmode, rawperm, NULL, num_svs, svp);
194 /* Regular (non-sys) open */
199 PerlIO *that_fp = NULL;
201 type = savepvn(oname, len);
205 /* Lose leading and trailing white space */
206 for (; isSPACE(*type); type++) ;
207 while (tend > type && isSPACE(tend[-1]))
211 /* New style explicit name, type is just mode and layer info */
213 if (SvROK(*svp) && !strchr(oname,'&')) {
215 Perl_warner(aTHX_ packWARN(WARN_IO),
216 "Can't open a reference");
217 SETERRNO(EINVAL, LIB_INVARG);
220 #endif /* USE_STDIO */
221 name = SvOK(*svp) ? savesvpv (*svp) : savepvn ("", 0);
229 if ((*type == IoTYPE_RDWR) && /* scary */
230 (*(type+1) == IoTYPE_RDONLY || *(type+1) == IoTYPE_WRONLY) &&
231 ((!num_svs || (tend > type+1 && tend[-1] != IoTYPE_PIPE)))) {
232 TAINT_PROPER("open");
237 if (*type == IoTYPE_PIPE) {
239 if (type[1] != IoTYPE_STD) {
241 Perl_croak(aTHX_ "Unknown open() mode '%.*s'", (int)olen, oname);
245 for (type++; isSPACE(*type); type++) ;
251 /* command is missing 19990114 */
252 if (ckWARN(WARN_PIPE))
253 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open");
257 if ((*name == '-' && name[1] == '\0') || num_svs)
259 TAINT_PROPER("piped open");
260 if (!num_svs && name[len-1] == '|') {
262 if (ckWARN(WARN_PIPE))
263 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Can't open bidirectional pipe");
269 strlcat(mode, "b", PERL_MODE_MAX);
271 strlcat(mode, "t", PERL_MODE_MAX);
279 fp = PerlProc_popen_list(mode, num_svs, svp);
282 fp = PerlProc_popen(name,mode);
286 if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) {
292 else if (*type == IoTYPE_WRONLY) {
293 TAINT_PROPER("open");
295 if (*type == IoTYPE_WRONLY) {
296 /* Two IoTYPE_WRONLYs in a row make for an IoTYPE_APPEND. */
297 mode[0] = IoTYPE(io) = IoTYPE_APPEND;
307 strlcat(mode, "b", PERL_MODE_MAX);
309 strlcat(mode, "t", PERL_MODE_MAX);
318 dodup = PERLIO_DUP_FD;
324 if (!num_svs && !*type && supplied_fp) {
325 /* "<+&" etc. is used by typemaps */
330 Perl_croak(aTHX_ "More than one argument to '%c&' open",IoTYPE(io));
332 for (; isSPACE(*type); type++) ;
333 if (num_svs && (SvIOK(*svp) || (SvPOK(*svp) && looks_like_number(*svp)))) {
337 else if (isDIGIT(*type)) {
343 thatio = sv_2io(*svp);
347 thatgv = gv_fetchpv(type,FALSE,SVt_PVIO);
348 thatio = GvIO(thatgv);
352 SETERRNO(EINVAL,SS_IVCHAN);
356 if ((that_fp = IoIFP(thatio))) {
357 /* Flush stdio buffer before dup. --mjd
358 * Unfortunately SEEK_CURing 0 seems to
359 * be optimized away on most platforms;
360 * only Solaris and Linux seem to flush
363 /* sfio fails to clear error on next
364 sfwrite, contrary to documentation.
366 if (PerlIO_seek(that_fp, 0, SEEK_CUR) == -1)
367 PerlIO_clearerr(that_fp);
369 /* On the other hand, do all platforms
370 * take gracefully to flushing a read-only
371 * filehandle? Perhaps we should do
372 * fsetpos(src)+fgetpos(dst)? --nik */
373 PerlIO_flush(that_fp);
374 fd = PerlIO_fileno(that_fp);
375 /* When dup()ing STDIN, STDOUT or STDERR
376 * explicitly set appropriate access mode */
377 if (that_fp == PerlIO_stdout()
378 || that_fp == PerlIO_stderr())
379 IoTYPE(io) = IoTYPE_WRONLY;
380 else if (that_fp == PerlIO_stdin())
381 IoTYPE(io) = IoTYPE_RDONLY;
382 /* When dup()ing a socket, say result is
384 else if (IoTYPE(thatio) == IoTYPE_SOCKET)
385 IoTYPE(io) = IoTYPE_SOCKET;
393 fp = PerlIO_fdupopen(aTHX_ that_fp, NULL, dodup);
397 fd = PerlLIO_dup(fd);
400 if (!(fp = PerlIO_openn(aTHX_ type,mode,fd,0,0,NULL,num_svs,svp))) {
401 if (dodup && fd >= 0)
408 for (; isSPACE(*type); type++) ;
409 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
411 fp = PerlIO_stdout();
412 IoTYPE(io) = IoTYPE_STD;
414 Perl_croak(aTHX_ "More than one argument to '>%c' open",IoTYPE_STD);
419 namesv = sv_2mortal(newSVpvn(type,strlen(type)));
424 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
427 if (!fp && type && *type && *type != ':' && !isIDFIRST(*type))
428 goto unknown_open_mode;
429 } /* IoTYPE_WRONLY */
430 else if (*type == IoTYPE_RDONLY) {
431 for (type++; isSPACE(*type); type++) ;
435 strlcat(mode, "b", PERL_MODE_MAX);
437 strlcat(mode, "t", PERL_MODE_MAX);
447 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
450 IoTYPE(io) = IoTYPE_STD;
452 Perl_croak(aTHX_ "More than one argument to '<%c' open",IoTYPE_STD);
457 namesv = sv_2mortal(newSVpvn(type,strlen(type)));
462 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
464 if (!fp && type && *type && *type != ':' && !isIDFIRST(*type))
465 goto unknown_open_mode;
466 } /* IoTYPE_RDONLY */
467 else if ((num_svs && /* '-|...' or '...|' */
468 type[0] == IoTYPE_STD && type[1] == IoTYPE_PIPE) ||
469 (!num_svs && tend > type+1 && tend[-1] == IoTYPE_PIPE)) {
471 type += 2; /* skip over '-|' */
475 while (tend > type && isSPACE(tend[-1]))
477 for (; isSPACE(*type); type++)
483 /* command is missing 19990114 */
484 if (ckWARN(WARN_PIPE))
485 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open");
489 if (!(*name == '-' && name[1] == '\0') || num_svs)
491 TAINT_PROPER("piped open");
496 strlcat(mode, "b", PERL_MODE_MAX);
498 strlcat(mode, "t", PERL_MODE_MAX);
507 fp = PerlProc_popen_list(mode,num_svs,svp);
510 fp = PerlProc_popen(name,mode);
512 IoTYPE(io) = IoTYPE_PIPE;
514 for (; isSPACE(*type); type++) ;
516 if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) {
522 else { /* layer(Args) */
524 goto unknown_open_mode;
526 IoTYPE(io) = IoTYPE_RDONLY;
527 for (; isSPACE(*name); name++)
533 strlcat(mode, "b", PERL_MODE_MAX);
535 strlcat(mode, "t", PERL_MODE_MAX);
543 if (*name == '-' && name[1] == '\0') {
545 IoTYPE(io) = IoTYPE_STD;
549 namesv = sv_2mortal(newSVpvn(type,strlen(type)));
554 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
559 if (IoTYPE(io) == IoTYPE_RDONLY && ckWARN(WARN_NEWLINE)
560 && strchr(oname, '\n')
563 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
567 if (ckWARN(WARN_IO)) {
568 if ((IoTYPE(io) == IoTYPE_RDONLY) &&
569 (fp == PerlIO_stdout() || fp == PerlIO_stderr())) {
570 Perl_warner(aTHX_ packWARN(WARN_IO),
571 "Filehandle STD%s reopened as %s only for input",
572 ((fp == PerlIO_stdout()) ? "OUT" : "ERR"),
575 else if ((IoTYPE(io) == IoTYPE_WRONLY) && fp == PerlIO_stdin()) {
576 Perl_warner(aTHX_ packWARN(WARN_IO),
577 "Filehandle STDIN reopened as %s only for output",
582 fd = PerlIO_fileno(fp);
583 /* If there is no fd (e.g. PerlIO::scalar) assume it isn't a
584 * socket - this covers PerlIO::scalar - otherwise unless we "know" the
585 * type probe for socket-ness.
587 if (IoTYPE(io) && IoTYPE(io) != IoTYPE_PIPE && IoTYPE(io) != IoTYPE_STD && fd >= 0) {
588 if (PerlLIO_fstat(fd,&PL_statbuf) < 0) {
589 /* If PerlIO claims to have fd we had better be able to fstat() it. */
590 (void) PerlIO_close(fp);
594 if (S_ISSOCK(PL_statbuf.st_mode))
595 IoTYPE(io) = IoTYPE_SOCKET; /* in case a socket was passed in to us */
599 !(PL_statbuf.st_mode & S_IFMT)
603 && IoTYPE(io) != IoTYPE_WRONLY /* Dups of STD* filehandles already have */
604 && IoTYPE(io) != IoTYPE_RDONLY /* type so they aren't marked as sockets */
605 ) { /* on OS's that return 0 on fstat()ed pipe */
607 Sock_size_t buflen = sizeof tmpbuf;
608 if (PerlSock_getsockname(fd, (struct sockaddr *)tmpbuf, &buflen) >= 0
609 || errno != ENOTSOCK)
610 IoTYPE(io) = IoTYPE_SOCKET; /* some OS's return 0 on fstat()ed socket */
611 /* but some return 0 for streams too, sigh */
613 #endif /* HAS_SOCKET */
614 #endif /* !PERL_MICRO */
618 * If this is a standard handle we discard all the layer stuff
619 * and just dup the fd into whatever was on the handle before !
622 if (saveifp) { /* must use old fp? */
623 /* If fd is less that PL_maxsysfd i.e. STDIN..STDERR
624 then dup the new fileno down
627 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
628 if (saveofp != saveifp) { /* was a socket? */
629 PerlIO_close(saveofp);
633 /* Still a small can-of-worms here if (say) PerlIO::scalar
634 is assigned to (say) STDOUT - for now let dup2() fail
635 and provide the error
637 if (PerlLIO_dup2(fd, savefd) < 0) {
638 (void)PerlIO_close(fp);
642 if (savefd != PerlIO_fileno(PerlIO_stdin())) {
643 char newname[FILENAME_MAX+1];
644 if (PerlIO_getname(fp, newname)) {
645 if (fd == PerlIO_fileno(PerlIO_stdout()))
646 Perl_vmssetuserlnm(aTHX_ "SYS$OUTPUT", newname);
647 if (fd == PerlIO_fileno(PerlIO_stderr()))
648 Perl_vmssetuserlnm(aTHX_ "SYS$ERROR", newname);
654 /* PL_fdpid isn't used on Windows, so avoid this useless work.
655 * XXX Probably the same for a lot of other places. */
661 sv = *av_fetch(PL_fdpid,fd,TRUE);
662 SvUPGRADE(sv, SVt_IV);
665 sv = *av_fetch(PL_fdpid,savefd,TRUE);
666 SvUPGRADE(sv, SVt_IV);
673 /* need to close fp without closing underlying fd */
674 int ofd = PerlIO_fileno(fp);
675 int dupfd = PerlLIO_dup(ofd);
676 #if defined(HAS_FCNTL) && defined(F_SETFD)
677 /* Assume if we have F_SETFD we have F_GETFD */
678 int coe = fcntl(ofd,F_GETFD);
681 PerlLIO_dup2(dupfd,ofd);
682 #if defined(HAS_FCNTL) && defined(F_SETFD)
683 /* The dup trick has lost close-on-exec on ofd */
684 fcntl(ofd,F_SETFD, coe);
686 PerlLIO_close(dupfd);
693 fd = PerlIO_fileno(fp);
695 #if defined(HAS_FCNTL) && defined(F_SETFD)
697 int save_errno = errno;
698 fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
704 IoFLAGS(io) &= ~IOf_NOLINE;
706 if (IoTYPE(io) == IoTYPE_SOCKET
707 || (IoTYPE(io) == IoTYPE_WRONLY && fd >= 0 && S_ISCHR(PL_statbuf.st_mode)) ) {
709 if (*s == IoTYPE_IMPLICIT || *s == IoTYPE_NUMERIC)
712 if (!(IoOFP(io) = PerlIO_openn(aTHX_ type,s,fd,0,0,NULL,0,svp))) {
726 IoTYPE(io) = savetype;
731 Perl_nextargv(pTHX_ register GV *gv)
734 #ifndef FLEXFILENAMES
740 IO * const io = GvIOp(gv);
743 PL_argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO);
744 if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
745 IoFLAGS(io) &= ~IOf_START;
747 if (!PL_argvout_stack)
748 PL_argvout_stack = newAV();
749 av_push(PL_argvout_stack, SvREFCNT_inc(PL_defoutgv));
752 if (PL_filemode & (S_ISUID|S_ISGID)) {
753 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
756 (void)fchmod(PL_lastfd,PL_filemode);
758 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
765 while (av_len(GvAV(gv)) >= 0) {
767 sv = av_shift(GvAV(gv));
769 sv_setsv(GvSVn(gv),sv);
770 SvSETMAGIC(GvSV(gv));
771 PL_oldname = SvPVx(GvSV(gv), oldlen);
772 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,Nullfp)) {
774 TAINT_PROPER("inplace open");
775 if (oldlen == 1 && *PL_oldname == '-') {
776 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
777 return IoIFP(GvIOp(gv));
779 #ifndef FLEXFILENAMES
780 filedev = PL_statbuf.st_dev;
781 fileino = PL_statbuf.st_ino;
783 PL_filemode = PL_statbuf.st_mode;
784 fileuid = PL_statbuf.st_uid;
785 filegid = PL_statbuf.st_gid;
786 if (!S_ISREG(PL_filemode)) {
787 if (ckWARN_d(WARN_INPLACE))
788 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
789 "Can't do inplace edit: %s is not a regular file",
795 const char *star = strchr(PL_inplace, '*');
797 const char *begin = PL_inplace;
798 sv_setpvn(sv, "", 0);
800 sv_catpvn(sv, begin, star - begin);
801 sv_catpvn(sv, PL_oldname, oldlen);
803 } while ((star = strchr(begin, '*')));
808 sv_catpv(sv,PL_inplace);
810 #ifndef FLEXFILENAMES
811 if ((PerlLIO_stat(SvPVX_const(sv),&PL_statbuf) >= 0
812 && PL_statbuf.st_dev == filedev
813 && PL_statbuf.st_ino == fileino)
815 || ((_djstat_fail_bits & _STFAIL_TRUENAME)!=0)
819 if (ckWARN_d(WARN_INPLACE))
820 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
821 "Can't do inplace edit: %"SVf" would not be unique",
828 #if !defined(DOSISH) && !defined(__CYGWIN__) && !defined(EPOC)
829 if (PerlLIO_rename(PL_oldname,SvPVX_const(sv)) < 0) {
830 if (ckWARN_d(WARN_INPLACE))
831 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
832 "Can't rename %s to %"SVf": %s, skipping file",
833 PL_oldname, sv, Strerror(errno) );
839 (void)PerlLIO_unlink(SvPVX_const(sv));
840 (void)PerlLIO_rename(PL_oldname,SvPVX_const(sv));
841 do_open(gv,(char*)SvPVX_const(sv),SvCUR(sv),PL_inplace!=0,
845 (void)UNLINK(SvPVX_const(sv));
846 if (link(PL_oldname,SvPVX_const(sv)) < 0) {
847 if (ckWARN_d(WARN_INPLACE))
848 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
849 "Can't rename %s to %"SVf": %s, skipping file",
850 PL_oldname, sv, Strerror(errno) );
854 (void)UNLINK(PL_oldname);
858 #if !defined(DOSISH) && !defined(AMIGAOS)
859 # ifndef VMS /* Don't delete; use automatic file versioning */
860 if (UNLINK(PL_oldname) < 0) {
861 if (ckWARN_d(WARN_INPLACE))
862 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
863 "Can't remove %s: %s, skipping file",
864 PL_oldname, Strerror(errno) );
870 Perl_croak(aTHX_ "Can't do inplace edit without backup");
874 sv_setpvn(sv,">",!PL_inplace);
875 sv_catpvn(sv,PL_oldname,oldlen);
876 SETERRNO(0,0); /* in case sprintf set errno */
878 if (!do_open(PL_argvoutgv,(char*)SvPVX_const(sv),SvCUR(sv),
879 PL_inplace!=0,O_WRONLY|O_CREAT|O_TRUNC,0,Nullfp))
881 if (!do_open(PL_argvoutgv,(char*)SvPVX_const(sv),SvCUR(sv),
882 PL_inplace!=0,O_WRONLY|O_CREAT|OPEN_EXCL,0666,
886 if (ckWARN_d(WARN_INPLACE))
887 Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't do inplace edit on %s: %s",
888 PL_oldname, Strerror(errno) );
892 setdefout(PL_argvoutgv);
893 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
894 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
896 (void)fchmod(PL_lastfd,PL_filemode);
898 # if !(defined(WIN32) && defined(__BORLANDC__))
899 /* Borland runtime creates a readonly file! */
900 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
903 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
905 (void)fchown(PL_lastfd,fileuid,filegid);
908 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
913 return IoIFP(GvIOp(gv));
916 if (ckWARN_d(WARN_INPLACE)) {
917 const int eno = errno;
918 if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0
919 && !S_ISREG(PL_statbuf.st_mode))
921 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
922 "Can't do inplace edit: %s is not a regular file",
926 Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't open %s: %s",
927 PL_oldname, Strerror(eno));
931 if (io && (IoFLAGS(io) & IOf_ARGV))
932 IoFLAGS(io) |= IOf_START;
934 (void)do_close(PL_argvoutgv,FALSE);
935 if (io && (IoFLAGS(io) & IOf_ARGV)
936 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
938 GV *oldout = (GV*)av_pop(PL_argvout_stack);
940 SvREFCNT_dec(oldout);
943 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
950 Perl_do_pipe(pTHX_ SV *sv, GV *rgv, GV *wgv)
969 if (PerlProc_pipe(fd) < 0)
971 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"PIPE_OPEN_MODE);
972 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"PIPE_OPEN_MODE);
973 IoOFP(rstio) = IoIFP(rstio);
974 IoIFP(wstio) = IoOFP(wstio);
975 IoTYPE(rstio) = IoTYPE_RDONLY;
976 IoTYPE(wstio) = IoTYPE_WRONLY;
977 if (!IoIFP(rstio) || !IoOFP(wstio)) {
978 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
979 else PerlLIO_close(fd[0]);
980 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
981 else PerlLIO_close(fd[1]);
985 sv_setsv(sv,&PL_sv_yes);
989 sv_setsv(sv,&PL_sv_undef);
994 /* explicit renamed to avoid C++ conflict -- kja */
996 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
1003 if (!gv || SvTYPE(gv) != SVt_PVGV) {
1005 SETERRNO(EBADF,SS_IVCHAN);
1009 if (!io) { /* never opened */
1011 if (ckWARN(WARN_UNOPENED)) /* no check for closed here */
1012 report_evil_fh(gv, io, PL_op->op_type);
1013 SETERRNO(EBADF,SS_IVCHAN);
1017 retval = io_close(io, not_implicit);
1021 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1023 IoTYPE(io) = IoTYPE_CLOSED;
1028 Perl_io_close(pTHX_ IO *io, bool not_implicit)
1030 bool retval = FALSE;
1033 if (IoTYPE(io) == IoTYPE_PIPE) {
1034 const int status = PerlProc_pclose(IoIFP(io));
1036 STATUS_NATIVE_CHILD_SET(status);
1037 retval = (STATUS_UNIX == 0);
1040 retval = (status != -1);
1043 else if (IoTYPE(io) == IoTYPE_STD)
1046 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
1047 bool prev_err = PerlIO_error(IoOFP(io));
1048 retval = (PerlIO_close(IoOFP(io)) != EOF && !prev_err);
1049 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
1052 bool prev_err = PerlIO_error(IoIFP(io));
1053 retval = (PerlIO_close(IoIFP(io)) != EOF && !prev_err);
1056 IoOFP(io) = IoIFP(io) = Nullfp;
1058 else if (not_implicit) {
1059 SETERRNO(EBADF,SS_IVCHAN);
1066 Perl_do_eof(pTHX_ GV *gv)
1075 else if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
1076 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1081 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
1082 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
1083 return FALSE; /* this is the most usual case */
1086 saverrno = errno; /* getc and ungetc can stomp on errno */
1087 ch = PerlIO_getc(IoIFP(io));
1089 (void)PerlIO_ungetc(IoIFP(io),ch);
1095 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
1096 if (PerlIO_get_cnt(IoIFP(io)) < -1)
1097 PerlIO_set_cnt(IoIFP(io),-1);
1099 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
1100 if (gv != PL_argvgv || !nextargv(gv)) /* get another fp handy */
1104 return TRUE; /* normal fp, definitely end of file */
1110 Perl_do_tell(pTHX_ GV *gv)
1112 register IO *io = 0;
1113 register PerlIO *fp;
1115 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1116 #ifdef ULTRIX_STDIO_BOTCH
1118 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1120 return PerlIO_tell(fp);
1122 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1123 report_evil_fh(gv, io, PL_op->op_type);
1124 SETERRNO(EBADF,RMS_IFI);
1129 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
1131 register IO *io = 0;
1132 register PerlIO *fp;
1134 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1135 #ifdef ULTRIX_STDIO_BOTCH
1137 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1139 return PerlIO_seek(fp, pos, whence) >= 0;
1141 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1142 report_evil_fh(gv, io, PL_op->op_type);
1143 SETERRNO(EBADF,RMS_IFI);
1148 Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
1150 register IO *io = 0;
1151 register PerlIO *fp;
1153 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
1154 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
1155 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1156 report_evil_fh(gv, io, PL_op->op_type);
1157 SETERRNO(EBADF,RMS_IFI);
1162 Perl_mode_from_discipline(pTHX_ SV *discp)
1164 int mode = O_BINARY;
1167 const char *s = SvPV_const(discp,len);
1172 if (s[2] == 'a' && s[3] == 'w'
1173 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1182 if (s[2] == 'r' && s[3] == 'l' && s[4] == 'f'
1183 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1192 goto fail_discipline;
1195 else if (isSPACE(*s)) {
1202 end = strchr(s+1, ':');
1205 #ifndef PERLIO_LAYERS
1206 Perl_croak(aTHX_ "IO layers (like '%.*s') unavailable", end-s, s);
1217 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE)
1219 my_chsize(int fd, Off_t length)
1222 /* code courtesy of William Kucharski */
1228 if (PerlLIO_fstat(fd, &filebuf) < 0)
1231 if (filebuf.st_size < length) {
1233 /* extend file length */
1235 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
1238 /* write a "0" byte */
1240 if ((PerlLIO_write(fd, "", 1)) != 1)
1244 /* truncate length */
1248 fl.l_start = length;
1249 fl.l_type = F_WRLCK; /* write lock on file space */
1252 * This relies on the UNDOCUMENTED F_FREESP argument to
1253 * fcntl(2), which truncates the file so that it ends at the
1254 * position indicated by fl.l_start.
1256 * Will minor miracles never cease?
1259 if (fcntl(fd, F_FREESP, &fl) < 0)
1265 Perl_croak_nocontext("truncate not implemented");
1266 #endif /* F_FREESP */
1269 #endif /* !HAS_TRUNCATE && !HAS_CHSIZE */
1272 Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
1274 register const char *tmps;
1277 /* assuming fp is checked earlier */
1280 switch (SvTYPE(sv)) {
1282 if (ckWARN(WARN_UNINITIALIZED))
1289 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
1291 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
1292 return !PerlIO_error(fp);
1296 if (PerlIO_isutf8(fp)) {
1298 sv_utf8_upgrade_flags(sv = sv_mortalcopy(sv),
1299 SV_GMAGIC|SV_UTF8_NO_ENCODING);
1301 else if (DO_UTF8(sv)) {
1302 if (!sv_utf8_downgrade((sv = sv_mortalcopy(sv)), TRUE)
1303 && ckWARN_d(WARN_UTF8))
1305 Perl_warner(aTHX_ packWARN(WARN_UTF8), "Wide character in print");
1308 tmps = SvPV_const(sv, len);
1311 /* To detect whether the process is about to overstep its
1312 * filesize limit we would need getrlimit(). We could then
1313 * also transparently raise the limit with setrlimit() --
1314 * but only until the system hard limit/the filesystem limit,
1315 * at which we would get EPERM. Note that when using buffered
1316 * io the write failure can be delayed until the flush/close. --jhi */
1317 if (len && (PerlIO_write(fp,tmps,len) == 0))
1319 return !PerlIO_error(fp);
1329 if (PL_op->op_flags & OPf_REF) {
1334 if (io && IoIFP(io)) {
1336 sv_setpvn(PL_statname,"", 0);
1337 PL_laststype = OP_STAT;
1338 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
1342 return PL_laststatval;
1343 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1344 report_evil_fh(gv, io, PL_op->op_type);
1346 sv_setpvn(PL_statname,"", 0);
1347 return (PL_laststatval = -1);
1350 else if (PL_op->op_private & OPpFT_STACKED) {
1351 return PL_laststatval;
1358 if (SvTYPE(sv) == SVt_PVGV) {
1362 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
1367 s = SvPV_const(sv, len);
1369 sv_setpvn(PL_statname, s, len);
1370 s = SvPVX_const(PL_statname); /* s now NUL-terminated */
1371 PL_laststype = OP_STAT;
1372 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1373 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
1374 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
1375 return PL_laststatval;
1379 static const char no_prev_lstat[] = "The stat preceding -l _ wasn't an lstat";
1386 if (PL_op->op_flags & OPf_REF) {
1388 if (cGVOP_gv == PL_defgv) {
1389 if (PL_laststype != OP_LSTAT)
1390 Perl_croak(aTHX_ no_prev_lstat);
1391 return PL_laststatval;
1393 if (ckWARN(WARN_IO)) {
1394 Perl_warner(aTHX_ packWARN(WARN_IO), "Use of -l on filehandle %s",
1396 return (PL_laststatval = -1);
1399 else if (PL_laststype != OP_LSTAT
1400 && (PL_op->op_private & OPpFT_STACKED) && ckWARN(WARN_IO))
1401 Perl_croak(aTHX_ no_prev_lstat);
1403 PL_laststype = OP_LSTAT;
1407 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV && ckWARN(WARN_IO)) {
1408 Perl_warner(aTHX_ packWARN(WARN_IO), "Use of -l on filehandle %s",
1409 GvENAME((GV*) SvRV(sv)));
1410 return (PL_laststatval = -1);
1412 /* XXX Do really need to be calling SvPV() all these times? */
1413 sv_setpv(PL_statname,SvPV_nolen_const(sv));
1414 PL_laststatval = PerlLIO_lstat(SvPV_nolen_const(sv),&PL_statcache);
1415 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(sv), '\n'))
1416 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "lstat");
1417 return PL_laststatval;
1422 Perl_do_aexec(pTHX_ SV *really, register SV **mark, register SV **sp)
1424 return do_aexec5(really, mark, sp, 0, 0);
1429 Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1430 int fd, int do_report)
1433 #if defined(MACOS_TRADITIONAL) || defined(__SYMBIAN32__)
1434 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1438 const char *tmps = Nullch;
1439 Newx(PL_Argv, sp - mark + 1, char*);
1442 while (++mark <= sp) {
1444 *a++ = (char*)SvPV_nolen_const(*mark);
1450 tmps = SvPV_nolen_const(really);
1451 if ((!really && *PL_Argv[0] != '/') ||
1452 (really && *tmps != '/')) /* will execvp use PATH? */
1453 TAINT_ENV(); /* testing IFS here is overkill, probably */
1455 if (really && *tmps)
1456 PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
1458 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1460 if (ckWARN(WARN_EXEC))
1461 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
1462 (really ? tmps : PL_Argv[0]), Strerror(errno));
1466 PerlLIO_write(fd, (void*)&e, sizeof(int));
1476 Perl_do_execfree(pTHX)
1479 PL_Argv = Null(char **);
1484 #if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC) && !defined(__SYMBIAN32__) && !defined(MACOS_TRADITIONAL)
1487 Perl_do_exec(pTHX_ const char *cmd)
1489 return do_exec3(cmd,0,0);
1493 Perl_do_exec3(pTHX_ const char *incmd, int fd, int do_report)
1501 /* Make a copy so we can change it */
1502 cmdlen = strlen(incmd);
1503 Newx(cmd, cmdlen+1, char);
1504 strncpy(cmd, incmd, cmdlen);
1507 while (*cmd && isSPACE(*cmd))
1510 /* save an extra exec if possible */
1514 char flags[PERL_FLAGS_MAX];
1515 if (strnEQ(cmd,PL_cshname,PL_cshlen) &&
1516 strnEQ(cmd+PL_cshlen," -c",3)) {
1518 strlcpy(flags, "-c", PERL_FLAGS_MAX);
1522 s = cmd+PL_cshlen+3;
1526 strlcat(flags, "f", PERL_FLAGS_MAX);
1540 if (s[-1] == '\'') {
1543 PerlProc_execl(PL_cshname,"csh", flags, ncmd, (char*)0);
1554 /* see if there are shell metacharacters in it */
1556 if (*cmd == '.' && isSPACE(cmd[1]))
1559 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1562 for (s = cmd; *s && isALNUM(*s); s++) ; /* catch VAR=val gizmo */
1566 for (s = cmd; *s; s++) {
1567 if (*s != ' ' && !isALPHA(*s) &&
1568 strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1569 if (*s == '\n' && !s[1]) {
1573 /* handle the 2>&1 construct at the end */
1574 if (*s == '>' && s[1] == '&' && s[2] == '1'
1575 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1576 && (!s[3] || isSPACE(s[3])))
1578 const char *t = s + 3;
1580 while (*t && isSPACE(*t))
1582 if (!*t && (PerlLIO_dup2(1,2) != -1)) {
1589 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
1596 Newx(PL_Argv, (s - cmd) / 2 + 2, char*);
1597 PL_Cmd = savepvn(cmd, s-cmd);
1599 for (s = PL_Cmd; *s;) {
1600 while (*s && isSPACE(*s)) s++;
1603 while (*s && !isSPACE(*s)) s++;
1610 PerlProc_execvp(PL_Argv[0],PL_Argv);
1612 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1617 if (ckWARN(WARN_EXEC))
1618 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
1619 PL_Argv[0], Strerror(errno));
1622 PerlLIO_write(fd, (void*)&e, sizeof(int));
1632 #endif /* OS2 || WIN32 */
1635 Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
1638 register I32 tot = 0;
1641 SV ** const oldmark = mark;
1643 #define APPLY_TAINT_PROPER() \
1645 if (PL_tainted) { TAINT_PROPER(what); } \
1648 /* This is a first heuristic; it doesn't catch tainting magic. */
1650 while (++mark <= sp) {
1651 if (SvTAINTED(*mark)) {
1661 APPLY_TAINT_PROPER();
1664 APPLY_TAINT_PROPER();
1666 while (++mark <= sp) {
1668 if (SvTYPE(*mark) == SVt_PVGV) {
1671 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1673 APPLY_TAINT_PROPER();
1674 if (fchmod(PerlIO_fileno(IoIFP(GvIOn(gv))), val))
1677 Perl_die(aTHX_ PL_no_func, "fchmod");
1684 else if (SvROK(*mark) && SvTYPE(SvRV(*mark)) == SVt_PVGV) {
1685 gv = (GV*)SvRV(*mark);
1689 const char *name = SvPV_nolen_const(*mark);
1690 APPLY_TAINT_PROPER();
1691 if (PerlLIO_chmod(name, val))
1700 APPLY_TAINT_PROPER();
1701 if (sp - mark > 2) {
1703 val = SvIVx(*++mark);
1704 val2 = SvIVx(*++mark);
1705 APPLY_TAINT_PROPER();
1707 while (++mark <= sp) {
1709 if (SvTYPE(*mark) == SVt_PVGV) {
1712 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1714 APPLY_TAINT_PROPER();
1715 if (fchown(PerlIO_fileno(IoIFP(GvIOn(gv))), val, val2))
1718 Perl_die(aTHX_ PL_no_func, "fchown");
1725 else if (SvROK(*mark) && SvTYPE(SvRV(*mark)) == SVt_PVGV) {
1726 gv = (GV*)SvRV(*mark);
1730 const char *name = SvPV_nolen_const(*mark);
1731 APPLY_TAINT_PROPER();
1732 if (PerlLIO_chown(name, val, val2))
1740 XXX Should we make lchown() directly available from perl?
1741 For now, we'll let Configure test for HAS_LCHOWN, but do
1742 nothing in the core.
1748 APPLY_TAINT_PROPER();
1751 s = SvPVx_nolen_const(*++mark);
1753 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1755 if ((val = whichsig(s)) < 0)
1756 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
1760 APPLY_TAINT_PROPER();
1763 /* kill() doesn't do process groups (job trees?) under VMS */
1764 if (val < 0) val = -val;
1765 if (val == SIGKILL) {
1766 # include <starlet.h>
1767 /* Use native sys$delprc() to insure that target process is
1768 * deleted; supervisor-mode images don't pay attention to
1769 * CRTL's emulation of Unix-style signals and kill()
1771 while (++mark <= sp) {
1772 I32 proc = SvIVx(*mark);
1773 register unsigned long int __vmssts;
1774 APPLY_TAINT_PROPER();
1775 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1779 case SS$_NOSUCHNODE:
1780 SETERRNO(ESRCH,__vmssts);
1783 SETERRNO(EPERM,__vmssts);
1786 SETERRNO(EVMSERR,__vmssts);
1795 while (++mark <= sp) {
1796 I32 proc = SvIVx(*mark);
1797 APPLY_TAINT_PROPER();
1799 if (PerlProc_killpg(proc,val)) /* BSD */
1801 if (PerlProc_kill(-proc,val)) /* SYSV */
1807 while (++mark <= sp) {
1808 I32 proc = SvIVx(*mark);
1809 APPLY_TAINT_PROPER();
1810 if (PerlProc_kill(proc, val))
1818 APPLY_TAINT_PROPER();
1820 while (++mark <= sp) {
1821 s = SvPV_nolen_const(*mark);
1822 APPLY_TAINT_PROPER();
1823 if (PL_euid || PL_unsafe) {
1827 else { /* don't let root wipe out directories without -U */
1828 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1840 APPLY_TAINT_PROPER();
1841 if (sp - mark > 2) {
1842 #if defined(I_UTIME) || defined(VMS)
1843 struct utimbuf utbuf;
1844 struct utimbuf *utbufp = &utbuf;
1850 void *utbufp = &utbuf;
1853 SV* accessed = *++mark;
1854 SV* modified = *++mark;
1856 /* Be like C, and if both times are undefined, let the C
1857 * library figure out what to do. This usually means
1858 * "current time". */
1860 if ( accessed == &PL_sv_undef && modified == &PL_sv_undef )
1863 Zero(&utbuf, sizeof utbuf, char);
1865 utbuf.actime = (Time_t)SvNVx(accessed); /* time accessed */
1866 utbuf.modtime = (Time_t)SvNVx(modified); /* time modified */
1868 utbuf.actime = (Time_t)SvIVx(accessed); /* time accessed */
1869 utbuf.modtime = (Time_t)SvIVx(modified); /* time modified */
1872 APPLY_TAINT_PROPER();
1874 while (++mark <= sp) {
1875 char *name = SvPV_nolen(*mark);
1876 APPLY_TAINT_PROPER();
1877 if (PerlLIO_utime(name, utbufp))
1888 #undef APPLY_TAINT_PROPER
1891 /* Do the permissions allow some operation? Assumes statcache already set. */
1892 #ifndef VMS /* VMS' cando is in vms.c */
1894 Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register const Stat_t *statbufp)
1895 /* Note: we use "effective" both for uids and gids.
1896 * Here we are betting on Uid_t being equal or wider than Gid_t. */
1899 /* [Comments and code from Len Reed]
1900 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1901 * to write-protected files. The execute permission bit is set
1902 * by the Miscrosoft C library stat() function for the following:
1907 * All files and directories are readable.
1908 * Directories and special files, e.g. "CON", cannot be
1910 * [Comment by Tom Dinger -- a directory can have the write-protect
1911 * bit set in the file system, but DOS permits changes to
1912 * the directory anyway. In addition, all bets are off
1913 * here for networked software, such as Novell and
1917 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1918 * too so it will actually look into the files for magic numbers
1920 return (mode & statbufp->st_mode) ? TRUE : FALSE;
1922 #else /* ! DOSISH */
1923 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
1924 if (mode == S_IXUSR) {
1925 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1929 return TRUE; /* root reads and writes anything */
1932 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1933 if (statbufp->st_mode & mode)
1934 return TRUE; /* ok as "user" */
1936 else if (ingroup(statbufp->st_gid,effective)) {
1937 if (statbufp->st_mode & mode >> 3)
1938 return TRUE; /* ok as "group" */
1940 else if (statbufp->st_mode & mode >> 6)
1941 return TRUE; /* ok as "other" */
1943 #endif /* ! DOSISH */
1948 Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective)
1950 #ifdef MACOS_TRADITIONAL
1951 /* This is simply not correct for AppleShare, but fix it yerself. */
1954 if (testgid == (effective ? PL_egid : PL_gid))
1956 #ifdef HAS_GETGROUPS
1961 Groups_t gary[NGROUPS];
1964 anum = getgroups(NGROUPS,gary);
1966 if (gary[anum] == testgid)
1974 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1977 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
1979 key_t key = (key_t)SvNVx(*++mark);
1980 const I32 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1981 const I32 flags = SvIVx(*++mark);
1989 return msgget(key, flags);
1993 return semget(key, n, flags);
1997 return shmget(key, n, flags);
1999 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
2001 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2004 return -1; /* should never happen */
2008 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
2015 const I32 id = SvIVx(*++mark);
2016 const I32 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
2017 const I32 cmd = SvIVx(*++mark);
2018 PERL_UNUSED_ARG(sp);
2022 getinfo = (cmd == IPC_STAT);
2028 if (cmd == IPC_STAT || cmd == IPC_SET)
2029 infosize = sizeof(struct msqid_ds);
2034 if (cmd == IPC_STAT || cmd == IPC_SET)
2035 infosize = sizeof(struct shmid_ds);
2041 if (cmd == IPC_STAT || cmd == IPC_SET)
2042 infosize = sizeof(struct semid_ds);
2043 else if (cmd == GETALL || cmd == SETALL)
2045 struct semid_ds semds;
2047 #ifdef EXTRA_F_IN_SEMUN_BUF
2048 semun.buff = &semds;
2052 getinfo = (cmd == GETALL);
2053 if (Semctl(id, 0, IPC_STAT, semun) == -1)
2055 infosize = semds.sem_nsems * sizeof(short);
2056 /* "short" is technically wrong but much more portable
2057 than guessing about u_?short(_t)? */
2060 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2064 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
2066 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2074 SvPV_force_nolen(astr);
2075 a = SvGROW(astr, infosize+1);
2080 a = SvPV(astr, len);
2081 if (len != infosize)
2082 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
2091 a = INT2PTR(char *,i); /* ouch */
2098 ret = msgctl(id, cmd, (struct msqid_ds *)a);
2104 union semun unsemds;
2106 #ifdef EXTRA_F_IN_SEMUN_BUF
2107 unsemds.buff = (struct semid_ds *)a;
2109 unsemds.buf = (struct semid_ds *)a;
2111 ret = Semctl(id, n, cmd, unsemds);
2113 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2120 ret = shmctl(id, cmd, (struct shmid_ds *)a);
2124 if (getinfo && ret >= 0) {
2125 SvCUR_set(astr, infosize);
2126 *SvEND(astr) = '\0';
2133 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
2140 const I32 id = SvIVx(*++mark);
2141 PERL_UNUSED_ARG(sp);
2144 flags = SvIVx(*++mark);
2145 mbuf = SvPV_const(mstr, len);
2146 if ((msize = len - sizeof(long)) < 0)
2147 Perl_croak(aTHX_ "Arg too short for msgsnd");
2149 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
2151 Perl_croak(aTHX_ "msgsnd not implemented");
2156 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
2162 I32 msize, flags, ret;
2163 const I32 id = SvIVx(*++mark);
2164 PERL_UNUSED_ARG(sp);
2167 /* suppress warning when reading into undef var --jhi */
2169 sv_setpvn(mstr, "", 0);
2170 msize = SvIVx(*++mark);
2171 mtype = (long)SvIVx(*++mark);
2172 flags = SvIVx(*++mark);
2173 SvPV_force_nolen(mstr);
2174 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
2177 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
2179 SvCUR_set(mstr, sizeof(long)+ret);
2180 *SvEND(mstr) = '\0';
2181 #ifndef INCOMPLETE_TAINTS
2182 /* who knows who has been playing with this message? */
2188 Perl_croak(aTHX_ "msgrcv not implemented");
2193 Perl_do_semop(pTHX_ SV **mark, SV **sp)
2199 const I32 id = SvIVx(*++mark);
2200 PERL_UNUSED_ARG(sp);
2203 opbuf = SvPV_const(opstr, opsize);
2204 if (opsize < 3 * SHORTSIZE
2205 || (opsize % (3 * SHORTSIZE))) {
2206 SETERRNO(EINVAL,LIB_INVARG);
2210 /* We can't assume that sizeof(struct sembuf) == 3 * sizeof(short). */
2212 const int nsops = opsize / (3 * sizeof (short));
2214 short *ops = (short *) opbuf;
2216 struct sembuf *temps, *t;
2219 Newx (temps, nsops, struct sembuf);
2227 result = semop(id, temps, nsops);
2241 Perl_croak(aTHX_ "semop not implemented");
2246 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
2252 struct shmid_ds shmds;
2253 const I32 id = SvIVx(*++mark);
2254 PERL_UNUSED_ARG(sp);
2257 mpos = SvIVx(*++mark);
2258 msize = SvIVx(*++mark);
2260 if (shmctl(id, IPC_STAT, &shmds) == -1)
2262 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
2263 SETERRNO(EFAULT,SS_ACCVIO); /* can't do as caller requested */
2266 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
2267 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2269 if (optype == OP_SHMREAD) {
2271 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2273 sv_setpvn(mstr, "", 0);
2274 SvPV_force_nolen(mstr);
2275 mbuf = SvGROW(mstr, msize+1);
2277 Copy(shm + mpos, mbuf, msize, char);
2278 SvCUR_set(mstr, msize);
2279 *SvEND(mstr) = '\0';
2281 #ifndef INCOMPLETE_TAINTS
2282 /* who knows who has been playing with this shared memory? */
2290 const char *mbuf = SvPV_const(mstr, len);
2291 if ((n = len) > msize)
2293 Copy(mbuf, shm + mpos, n, char);
2295 memzero(shm + mpos + n, msize - n);
2299 Perl_croak(aTHX_ "shm I/O not implemented");
2303 #endif /* SYSV IPC */
2308 =for apidoc start_glob
2310 Function called by C<do_readline> to spawn a glob (or do the glob inside
2311 perl on VMS). This code used to be inline, but now perl uses C<File::Glob>
2312 this glob starter is only used by miniperl during the build process.
2313 Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up.
2319 Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
2322 SV * const tmpcmd = NEWSV(55, 0);
2326 #ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
2327 /* since spawning off a process is a real performance hit */
2329 #include <descrip.h>
2330 #include <lib$routines.h>
2333 char rslt[NAM$C_MAXRSS+1+sizeof(unsigned short int)] = {'\0','\0'};
2334 char vmsspec[NAM$C_MAXRSS+1];
2335 char * const rstr = rslt + sizeof(unsigned short int);
2336 char *begin, *end, *cp;
2337 $DESCRIPTOR(dfltdsc,"SYS$DISK:[]*.*;");
2340 struct dsc$descriptor_s wilddsc
2341 = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
2342 struct dsc$descriptor_vs rsdsc
2343 = {sizeof rslt, DSC$K_DTYPE_VT, DSC$K_CLASS_VS, rslt};
2344 unsigned long int cxt = 0, sts = 0, ok = 1, hasdir = 0, hasver = 0, isunix = 0;
2346 /* We could find out if there's an explicit dev/dir or version
2347 by peeking into lib$find_file's internal context at
2348 ((struct NAM *)((struct FAB *)cxt)->fab$l_nam)->nam$l_fnb
2349 but that's unsupported, so I don't want to do it now and
2350 have it bite someone in the future. */
2351 cp = SvPV(tmpglob,i);
2353 if (cp[i] == ';') hasver = 1;
2355 if (sts) hasver = 1;
2359 hasdir = isunix = 1;
2362 if (cp[i] == ']' || cp[i] == '>' || cp[i] == ':') {
2367 if ((tmpfp = PerlIO_tmpfile()) != NULL) {
2369 if (!PerlLIO_stat(SvPVX_const(tmpglob),&st) && S_ISDIR(st.st_mode))
2370 ok = ((wilddsc.dsc$a_pointer = tovmspath(SvPVX(tmpglob),vmsspec)) != NULL);
2371 else ok = ((wilddsc.dsc$a_pointer = tovmsspec(SvPVX(tmpglob),vmsspec)) != NULL);
2372 if (ok) wilddsc.dsc$w_length = (unsigned short int) strlen(wilddsc.dsc$a_pointer);
2373 for (cp=wilddsc.dsc$a_pointer; ok && cp && *cp; cp++)
2374 if (*cp == '?') *cp = '%'; /* VMS style single-char wildcard */
2375 while (ok && ((sts = lib$find_file(&wilddsc,&rsdsc,&cxt,
2376 &dfltdsc,NULL,NULL,NULL))&1)) {
2377 /* with varying string, 1st word of buffer contains result length */
2378 end = rstr + *((unsigned short int*)rslt);
2379 if (!hasver) while (*end != ';' && end > rstr) end--;
2380 *(end++) = '\n'; *end = '\0';
2381 for (cp = rstr; *cp; cp++) *cp = _tolower(*cp);
2383 if (isunix) trim_unixpath(rstr,SvPVX(tmpglob),1);
2388 while (*(--begin) != ']' && *begin != '>') ;
2391 ok = (PerlIO_puts(tmpfp,begin) != EOF);
2393 if (cxt) (void)lib$find_file_end(&cxt);
2394 if (ok && sts != RMS$_NMF &&
2395 sts != RMS$_DNF && sts != RMS_FNF) ok = 0;
2398 SETERRNO((sts == RMS$_SYN ? EINVAL : EVMSERR),sts);
2400 PerlIO_close(tmpfp);
2404 PerlIO_rewind(tmpfp);
2405 IoTYPE(io) = IoTYPE_RDONLY;
2406 IoIFP(io) = fp = tmpfp;
2407 IoFLAGS(io) &= ~IOf_UNTAINT; /* maybe redundant */
2412 #ifdef MACOS_TRADITIONAL
2413 sv_setpv(tmpcmd, "glob ");
2414 sv_catsv(tmpcmd, tmpglob);
2415 sv_catpv(tmpcmd, " |");
2419 sv_setpv(tmpcmd, "for a in ");
2420 sv_catsv(tmpcmd, tmpglob);
2421 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
2424 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
2425 sv_catsv(tmpcmd, tmpglob);
2427 sv_setpv(tmpcmd, "perlglob ");
2428 sv_catsv(tmpcmd, tmpglob);
2429 sv_catpv(tmpcmd, " |");
2434 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
2435 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
2436 sv_catsv(tmpcmd, tmpglob);
2437 sv_catpv(tmpcmd, "' 2>/dev/null |");
2439 sv_setpv(tmpcmd, "echo ");
2440 sv_catsv(tmpcmd, tmpglob);
2442 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
2444 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
2447 #endif /* !DOSISH */
2448 #endif /* MACOS_TRADITIONAL */
2449 (void)do_open(PL_last_in_gv, (char*)SvPVX_const(tmpcmd), SvCUR(tmpcmd),
2450 FALSE, O_RDONLY, 0, Nullfp);
2459 * c-indentation-style: bsd
2461 * indent-tabs-mode: t
2464 * ex: set ts=8 sts=4 sw=4 noet: