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_open9(pTHX_ GV *gv, register const char *name, I32 len, int as_raw,
71 int rawmode, int rawperm, PerlIO *supplied_fp, SV *svs,
74 PERL_UNUSED_ARG(num_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 const char *oname, I32 len, int as_raw,
81 int rawmode, int rawperm, PerlIO *supplied_fp, SV **svp,
85 register IO * const io = GvIOn(gv);
86 PerlIO *saveifp = Nullfp;
87 PerlIO *saveofp = Nullfp;
89 char savetype = IoTYPE_CLOSED;
94 bool was_fdopen = FALSE;
95 bool in_raw = 0, in_crlf = 0, out_raw = 0, out_crlf = 0;
97 char mode[PERL_MODE_MAX]; /* stdio file mode ("r\0", "rb\0", "r+b\0" etc.) */
100 Zero(mode,sizeof(mode),char);
101 PL_forkprocess = 1; /* assume true if no fork */
103 /* Collect default raw/crlf info from the op */
104 if (PL_op && PL_op->op_type == OP_OPEN) {
105 /* set up IO layers */
106 const U8 flags = PL_op->op_private;
107 in_raw = (flags & OPpOPEN_IN_RAW);
108 in_crlf = (flags & OPpOPEN_IN_CRLF);
109 out_raw = (flags & OPpOPEN_OUT_RAW);
110 out_crlf = (flags & OPpOPEN_OUT_CRLF);
113 /* If currently open - close before we re-open */
115 fd = PerlIO_fileno(IoIFP(io));
116 if (IoTYPE(io) == IoTYPE_STD) {
117 /* This is a clone of one of STD* handles */
120 else if (fd >= 0 && fd <= PL_maxsysfd) {
121 /* This is one of the original STD* handles */
124 savetype = IoTYPE(io);
128 else if (IoTYPE(io) == IoTYPE_PIPE)
129 result = PerlProc_pclose(IoIFP(io));
130 else if (IoIFP(io) != IoOFP(io)) {
132 result = PerlIO_close(IoOFP(io));
133 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
136 result = PerlIO_close(IoIFP(io));
139 result = PerlIO_close(IoIFP(io));
140 if (result == EOF && fd > PL_maxsysfd) {
141 /* Why is this not Perl_warn*() call ? */
142 PerlIO_printf(Perl_error_log,
143 "Warning: unable to close filehandle %s properly.\n",
146 IoOFP(io) = IoIFP(io) = Nullfp;
150 /* sysopen style args, i.e. integer mode and permissions */
152 const int appendtrunc =
154 #ifdef O_APPEND /* Not fully portable. */
157 #ifdef O_TRUNC /* Not fully portable. */
161 const int modifyingmode = O_WRONLY|O_RDWR|O_CREAT|appendtrunc;
165 Perl_croak(aTHX_ "panic: sysopen with multiple args");
173 It might be (in OS/390 and Mac OS Classic it is)
179 This means that simple & with O_RDWR would look
180 like O_RDONLY is present. Therefore we have to
183 if ((ismodifying = (rawmode & modifyingmode))) {
184 if ((ismodifying & O_WRONLY) == O_WRONLY ||
185 (ismodifying & O_RDWR) == O_RDWR ||
186 (ismodifying & (O_CREAT|appendtrunc)))
187 TAINT_PROPER("sysopen");
189 mode[ix++] = IoTYPE_NUMERIC; /* Marker to openn to use numeric "sysopen" */
191 #if defined(USE_64_BIT_RAWIO) && defined(O_LARGEFILE)
192 rawmode |= O_LARGEFILE; /* Transparently largefiley. */
195 IoTYPE(io) = PerlIO_intmode2str(rawmode, &mode[ix], &writing);
197 namesv = sv_2mortal(newSVpvn(oname,strlen(oname)));
201 fp = PerlIO_openn(aTHX_ type, mode, -1, rawmode, rawperm, NULL, num_svs, svp);
204 /* Regular (non-sys) open */
209 PerlIO *that_fp = NULL;
211 type = savepvn(oname, len);
215 /* 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(oname,'&')) {
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);
255 for (type++; isSPACE(*type); type++) ;
261 /* command is missing 19990114 */
262 if (ckWARN(WARN_PIPE))
263 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open");
267 if ((*name == '-' && name[1] == '\0') || num_svs)
269 TAINT_PROPER("piped open");
270 if (!num_svs && name[len-1] == '|') {
272 if (ckWARN(WARN_PIPE))
273 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Can't open bidirectional pipe");
279 strlcat(mode, "b", PERL_MODE_MAX);
281 strlcat(mode, "t", PERL_MODE_MAX);
289 fp = PerlProc_popen_list(mode, num_svs, svp);
292 fp = PerlProc_popen(name,mode);
296 if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) {
302 else if (*type == IoTYPE_WRONLY) {
303 TAINT_PROPER("open");
305 if (*type == IoTYPE_WRONLY) {
306 /* Two IoTYPE_WRONLYs in a row make for an IoTYPE_APPEND. */
307 mode[0] = IoTYPE(io) = IoTYPE_APPEND;
317 strlcat(mode, "b", PERL_MODE_MAX);
319 strlcat(mode, "t", PERL_MODE_MAX);
328 dodup = PERLIO_DUP_FD;
334 if (!num_svs && !*type && supplied_fp) {
335 /* "<+&" etc. is used by typemaps */
340 Perl_croak(aTHX_ "More than one argument to '%c&' open",IoTYPE(io));
342 for (; isSPACE(*type); type++) ;
343 if (num_svs && (SvIOK(*svp) || (SvPOK(*svp) && looks_like_number(*svp)))) {
347 else if (isDIGIT(*type)) {
353 thatio = sv_2io(*svp);
357 thatgv = gv_fetchpv(type,FALSE,SVt_PVIO);
358 thatio = GvIO(thatgv);
362 SETERRNO(EINVAL,SS_IVCHAN);
366 if ((that_fp = IoIFP(thatio))) {
367 /* Flush stdio buffer before dup. --mjd
368 * Unfortunately SEEK_CURing 0 seems to
369 * be optimized away on most platforms;
370 * only Solaris and Linux seem to flush
373 /* sfio fails to clear error on next
374 sfwrite, contrary to documentation.
376 if (PerlIO_seek(that_fp, 0, SEEK_CUR) == -1)
377 PerlIO_clearerr(that_fp);
379 /* On the other hand, do all platforms
380 * take gracefully to flushing a read-only
381 * filehandle? Perhaps we should do
382 * fsetpos(src)+fgetpos(dst)? --nik */
383 PerlIO_flush(that_fp);
384 fd = PerlIO_fileno(that_fp);
385 /* When dup()ing STDIN, STDOUT or STDERR
386 * explicitly set appropriate access mode */
387 if (that_fp == PerlIO_stdout()
388 || that_fp == PerlIO_stderr())
389 IoTYPE(io) = IoTYPE_WRONLY;
390 else if (that_fp == PerlIO_stdin())
391 IoTYPE(io) = IoTYPE_RDONLY;
392 /* When dup()ing a socket, say result is
394 else if (IoTYPE(thatio) == IoTYPE_SOCKET)
395 IoTYPE(io) = IoTYPE_SOCKET;
403 fp = PerlIO_fdupopen(aTHX_ that_fp, NULL, dodup);
407 fd = PerlLIO_dup(fd);
410 if (!(fp = PerlIO_openn(aTHX_ type,mode,fd,0,0,NULL,num_svs,svp))) {
411 if (dodup && fd >= 0)
418 for (; isSPACE(*type); type++) ;
419 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
421 fp = PerlIO_stdout();
422 IoTYPE(io) = IoTYPE_STD;
424 Perl_croak(aTHX_ "More than one argument to '>%c' open",IoTYPE_STD);
429 namesv = sv_2mortal(newSVpvn(type,strlen(type)));
434 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
437 if (!fp && type && *type && *type != ':' && !isIDFIRST(*type))
438 goto unknown_open_mode;
439 } /* IoTYPE_WRONLY */
440 else if (*type == IoTYPE_RDONLY) {
441 for (type++; isSPACE(*type); type++) ;
445 strlcat(mode, "b", PERL_MODE_MAX);
447 strlcat(mode, "t", PERL_MODE_MAX);
457 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
460 IoTYPE(io) = IoTYPE_STD;
462 Perl_croak(aTHX_ "More than one argument to '<%c' open",IoTYPE_STD);
467 namesv = sv_2mortal(newSVpvn(type,strlen(type)));
472 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
474 if (!fp && type && *type && *type != ':' && !isIDFIRST(*type))
475 goto unknown_open_mode;
476 } /* IoTYPE_RDONLY */
477 else if ((num_svs && /* '-|...' or '...|' */
478 type[0] == IoTYPE_STD && type[1] == IoTYPE_PIPE) ||
479 (!num_svs && tend > type+1 && tend[-1] == IoTYPE_PIPE)) {
481 type += 2; /* skip over '-|' */
485 while (tend > type && isSPACE(tend[-1]))
487 for (; isSPACE(*type); type++)
493 /* command is missing 19990114 */
494 if (ckWARN(WARN_PIPE))
495 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open");
499 if (!(*name == '-' && name[1] == '\0') || num_svs)
501 TAINT_PROPER("piped open");
506 strlcat(mode, "b", PERL_MODE_MAX);
508 strlcat(mode, "t", PERL_MODE_MAX);
517 fp = PerlProc_popen_list(mode,num_svs,svp);
520 fp = PerlProc_popen(name,mode);
522 IoTYPE(io) = IoTYPE_PIPE;
524 for (; isSPACE(*type); type++) ;
526 if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) {
532 else { /* layer(Args) */
534 goto unknown_open_mode;
536 IoTYPE(io) = IoTYPE_RDONLY;
537 for (; isSPACE(*name); name++)
543 strlcat(mode, "b", PERL_MODE_MAX);
545 strlcat(mode, "t", PERL_MODE_MAX);
553 if (*name == '-' && name[1] == '\0') {
555 IoTYPE(io) = IoTYPE_STD;
559 namesv = sv_2mortal(newSVpvn(type,strlen(type)));
564 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
569 if (IoTYPE(io) == IoTYPE_RDONLY && ckWARN(WARN_NEWLINE)
570 && strchr(oname, '\n')
573 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
577 if (ckWARN(WARN_IO)) {
578 if ((IoTYPE(io) == IoTYPE_RDONLY) &&
579 (fp == PerlIO_stdout() || fp == PerlIO_stderr())) {
580 Perl_warner(aTHX_ packWARN(WARN_IO),
581 "Filehandle STD%s reopened as %s only for input",
582 ((fp == PerlIO_stdout()) ? "OUT" : "ERR"),
585 else if ((IoTYPE(io) == IoTYPE_WRONLY) && fp == PerlIO_stdin()) {
586 Perl_warner(aTHX_ packWARN(WARN_IO),
587 "Filehandle STDIN reopened as %s only for output",
592 fd = PerlIO_fileno(fp);
593 /* If there is no fd (e.g. PerlIO::scalar) assume it isn't a
594 * socket - this covers PerlIO::scalar - otherwise unless we "know" the
595 * type probe for socket-ness.
597 if (IoTYPE(io) && IoTYPE(io) != IoTYPE_PIPE && IoTYPE(io) != IoTYPE_STD && fd >= 0) {
598 if (PerlLIO_fstat(fd,&PL_statbuf) < 0) {
599 /* If PerlIO claims to have fd we had better be able to fstat() it. */
600 (void) PerlIO_close(fp);
604 if (S_ISSOCK(PL_statbuf.st_mode))
605 IoTYPE(io) = IoTYPE_SOCKET; /* in case a socket was passed in to us */
609 !(PL_statbuf.st_mode & S_IFMT)
613 && IoTYPE(io) != IoTYPE_WRONLY /* Dups of STD* filehandles already have */
614 && IoTYPE(io) != IoTYPE_RDONLY /* type so they aren't marked as sockets */
615 ) { /* on OS's that return 0 on fstat()ed pipe */
617 Sock_size_t buflen = sizeof tmpbuf;
618 if (PerlSock_getsockname(fd, (struct sockaddr *)tmpbuf, &buflen) >= 0
619 || errno != ENOTSOCK)
620 IoTYPE(io) = IoTYPE_SOCKET; /* some OS's return 0 on fstat()ed socket */
621 /* but some return 0 for streams too, sigh */
623 #endif /* HAS_SOCKET */
624 #endif /* !PERL_MICRO */
628 * If this is a standard handle we discard all the layer stuff
629 * and just dup the fd into whatever was on the handle before !
632 if (saveifp) { /* must use old fp? */
633 /* If fd is less that PL_maxsysfd i.e. STDIN..STDERR
634 then dup the new fileno down
637 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
638 if (saveofp != saveifp) { /* was a socket? */
639 PerlIO_close(saveofp);
643 /* Still a small can-of-worms here if (say) PerlIO::scalar
644 is assigned to (say) STDOUT - for now let dup2() fail
645 and provide the error
647 if (PerlLIO_dup2(fd, savefd) < 0) {
648 (void)PerlIO_close(fp);
652 if (savefd != PerlIO_fileno(PerlIO_stdin())) {
653 char newname[FILENAME_MAX+1];
654 if (PerlIO_getname(fp, newname)) {
655 if (fd == PerlIO_fileno(PerlIO_stdout()))
656 Perl_vmssetuserlnm(aTHX_ "SYS$OUTPUT", newname);
657 if (fd == PerlIO_fileno(PerlIO_stderr()))
658 Perl_vmssetuserlnm(aTHX_ "SYS$ERROR", newname);
664 /* PL_fdpid isn't used on Windows, so avoid this useless work.
665 * XXX Probably the same for a lot of other places. */
671 sv = *av_fetch(PL_fdpid,fd,TRUE);
672 SvUPGRADE(sv, SVt_IV);
675 sv = *av_fetch(PL_fdpid,savefd,TRUE);
676 SvUPGRADE(sv, SVt_IV);
683 /* need to close fp without closing underlying fd */
684 int ofd = PerlIO_fileno(fp);
685 int dupfd = PerlLIO_dup(ofd);
686 #if defined(HAS_FCNTL) && defined(F_SETFD)
687 /* Assume if we have F_SETFD we have F_GETFD */
688 int coe = fcntl(ofd,F_GETFD);
691 PerlLIO_dup2(dupfd,ofd);
692 #if defined(HAS_FCNTL) && defined(F_SETFD)
693 /* The dup trick has lost close-on-exec on ofd */
694 fcntl(ofd,F_SETFD, coe);
696 PerlLIO_close(dupfd);
703 fd = PerlIO_fileno(fp);
705 #if defined(HAS_FCNTL) && defined(F_SETFD)
707 int save_errno = errno;
708 fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
714 IoFLAGS(io) &= ~IOf_NOLINE;
716 if (IoTYPE(io) == IoTYPE_SOCKET
717 || (IoTYPE(io) == IoTYPE_WRONLY && fd >= 0 && S_ISCHR(PL_statbuf.st_mode)) ) {
719 if (*s == IoTYPE_IMPLICIT || *s == IoTYPE_NUMERIC)
722 if (!(IoOFP(io) = PerlIO_openn(aTHX_ type,s,fd,0,0,NULL,0,svp))) {
736 IoTYPE(io) = savetype;
741 Perl_nextargv(pTHX_ register GV *gv)
744 #ifndef FLEXFILENAMES
750 IO * const io = GvIOp(gv);
753 PL_argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO);
754 if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
755 IoFLAGS(io) &= ~IOf_START;
757 if (!PL_argvout_stack)
758 PL_argvout_stack = newAV();
759 av_push(PL_argvout_stack, SvREFCNT_inc(PL_defoutgv));
762 if (PL_filemode & (S_ISUID|S_ISGID)) {
763 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
766 (void)fchmod(PL_lastfd,PL_filemode);
768 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
775 while (av_len(GvAV(gv)) >= 0) {
777 sv = av_shift(GvAV(gv));
779 sv_setsv(GvSVn(gv),sv);
780 SvSETMAGIC(GvSV(gv));
781 PL_oldname = SvPVx(GvSV(gv), oldlen);
782 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,Nullfp)) {
784 TAINT_PROPER("inplace open");
785 if (oldlen == 1 && *PL_oldname == '-') {
786 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
787 return IoIFP(GvIOp(gv));
789 #ifndef FLEXFILENAMES
790 filedev = PL_statbuf.st_dev;
791 fileino = PL_statbuf.st_ino;
793 PL_filemode = PL_statbuf.st_mode;
794 fileuid = PL_statbuf.st_uid;
795 filegid = PL_statbuf.st_gid;
796 if (!S_ISREG(PL_filemode)) {
797 if (ckWARN_d(WARN_INPLACE))
798 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
799 "Can't do inplace edit: %s is not a regular file",
805 const char *star = strchr(PL_inplace, '*');
807 const char *begin = PL_inplace;
808 sv_setpvn(sv, "", 0);
810 sv_catpvn(sv, begin, star - begin);
811 sv_catpvn(sv, PL_oldname, oldlen);
813 } while ((star = strchr(begin, '*')));
818 sv_catpv(sv,PL_inplace);
820 #ifndef FLEXFILENAMES
821 if ((PerlLIO_stat(SvPVX_const(sv),&PL_statbuf) >= 0
822 && PL_statbuf.st_dev == filedev
823 && PL_statbuf.st_ino == fileino)
825 || ((_djstat_fail_bits & _STFAIL_TRUENAME)!=0)
829 if (ckWARN_d(WARN_INPLACE))
830 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
831 "Can't do inplace edit: %"SVf" would not be unique",
838 #if !defined(DOSISH) && !defined(__CYGWIN__) && !defined(EPOC)
839 if (PerlLIO_rename(PL_oldname,SvPVX_const(sv)) < 0) {
840 if (ckWARN_d(WARN_INPLACE))
841 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
842 "Can't rename %s to %"SVf": %s, skipping file",
843 PL_oldname, sv, Strerror(errno) );
849 (void)PerlLIO_unlink(SvPVX_const(sv));
850 (void)PerlLIO_rename(PL_oldname,SvPVX_const(sv));
851 do_open(gv,(char*)SvPVX_const(sv),SvCUR(sv),PL_inplace!=0,
855 (void)UNLINK(SvPVX_const(sv));
856 if (link(PL_oldname,SvPVX_const(sv)) < 0) {
857 if (ckWARN_d(WARN_INPLACE))
858 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
859 "Can't rename %s to %"SVf": %s, skipping file",
860 PL_oldname, sv, Strerror(errno) );
864 (void)UNLINK(PL_oldname);
868 #if !defined(DOSISH) && !defined(AMIGAOS)
869 # ifndef VMS /* Don't delete; use automatic file versioning */
870 if (UNLINK(PL_oldname) < 0) {
871 if (ckWARN_d(WARN_INPLACE))
872 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
873 "Can't remove %s: %s, skipping file",
874 PL_oldname, Strerror(errno) );
880 Perl_croak(aTHX_ "Can't do inplace edit without backup");
884 sv_setpvn(sv,">",!PL_inplace);
885 sv_catpvn(sv,PL_oldname,oldlen);
886 SETERRNO(0,0); /* in case sprintf set errno */
888 if (!do_open(PL_argvoutgv,(char*)SvPVX_const(sv),SvCUR(sv),
889 PL_inplace!=0,O_WRONLY|O_CREAT|O_TRUNC,0,Nullfp))
891 if (!do_open(PL_argvoutgv,(char*)SvPVX_const(sv),SvCUR(sv),
892 PL_inplace!=0,O_WRONLY|O_CREAT|OPEN_EXCL,0666,
896 if (ckWARN_d(WARN_INPLACE))
897 Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't do inplace edit on %s: %s",
898 PL_oldname, Strerror(errno) );
902 setdefout(PL_argvoutgv);
903 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
904 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
906 (void)fchmod(PL_lastfd,PL_filemode);
908 # if !(defined(WIN32) && defined(__BORLANDC__))
909 /* Borland runtime creates a readonly file! */
910 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
913 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
915 (void)fchown(PL_lastfd,fileuid,filegid);
918 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
923 return IoIFP(GvIOp(gv));
926 if (ckWARN_d(WARN_INPLACE)) {
927 const int eno = errno;
928 if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0
929 && !S_ISREG(PL_statbuf.st_mode))
931 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
932 "Can't do inplace edit: %s is not a regular file",
936 Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't open %s: %s",
937 PL_oldname, Strerror(eno));
941 if (io && (IoFLAGS(io) & IOf_ARGV))
942 IoFLAGS(io) |= IOf_START;
944 (void)do_close(PL_argvoutgv,FALSE);
945 if (io && (IoFLAGS(io) & IOf_ARGV)
946 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
948 GV *oldout = (GV*)av_pop(PL_argvout_stack);
950 SvREFCNT_dec(oldout);
953 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
960 Perl_do_pipe(pTHX_ SV *sv, GV *rgv, GV *wgv)
979 if (PerlProc_pipe(fd) < 0)
981 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"PIPE_OPEN_MODE);
982 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"PIPE_OPEN_MODE);
983 IoOFP(rstio) = IoIFP(rstio);
984 IoIFP(wstio) = IoOFP(wstio);
985 IoTYPE(rstio) = IoTYPE_RDONLY;
986 IoTYPE(wstio) = IoTYPE_WRONLY;
987 if (!IoIFP(rstio) || !IoOFP(wstio)) {
988 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
989 else PerlLIO_close(fd[0]);
990 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
991 else PerlLIO_close(fd[1]);
995 sv_setsv(sv,&PL_sv_yes);
999 sv_setsv(sv,&PL_sv_undef);
1004 /* explicit renamed to avoid C++ conflict -- kja */
1006 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
1013 if (!gv || SvTYPE(gv) != SVt_PVGV) {
1015 SETERRNO(EBADF,SS_IVCHAN);
1019 if (!io) { /* never opened */
1021 if (ckWARN(WARN_UNOPENED)) /* no check for closed here */
1022 report_evil_fh(gv, io, PL_op->op_type);
1023 SETERRNO(EBADF,SS_IVCHAN);
1027 retval = io_close(io, not_implicit);
1031 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1033 IoTYPE(io) = IoTYPE_CLOSED;
1038 Perl_io_close(pTHX_ IO *io, bool not_implicit)
1040 bool retval = FALSE;
1043 if (IoTYPE(io) == IoTYPE_PIPE) {
1044 const int status = PerlProc_pclose(IoIFP(io));
1046 STATUS_NATIVE_CHILD_SET(status);
1047 retval = (STATUS_UNIX == 0);
1050 retval = (status != -1);
1053 else if (IoTYPE(io) == IoTYPE_STD)
1056 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
1057 bool prev_err = PerlIO_error(IoOFP(io));
1058 retval = (PerlIO_close(IoOFP(io)) != EOF && !prev_err);
1059 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
1062 bool prev_err = PerlIO_error(IoIFP(io));
1063 retval = (PerlIO_close(IoIFP(io)) != EOF && !prev_err);
1066 IoOFP(io) = IoIFP(io) = Nullfp;
1068 else if (not_implicit) {
1069 SETERRNO(EBADF,SS_IVCHAN);
1076 Perl_do_eof(pTHX_ GV *gv)
1085 else if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
1086 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1091 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
1092 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
1093 return FALSE; /* this is the most usual case */
1096 saverrno = errno; /* getc and ungetc can stomp on errno */
1097 ch = PerlIO_getc(IoIFP(io));
1099 (void)PerlIO_ungetc(IoIFP(io),ch);
1105 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
1106 if (PerlIO_get_cnt(IoIFP(io)) < -1)
1107 PerlIO_set_cnt(IoIFP(io),-1);
1109 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
1110 if (gv != PL_argvgv || !nextargv(gv)) /* get another fp handy */
1114 return TRUE; /* normal fp, definitely end of file */
1120 Perl_do_tell(pTHX_ GV *gv)
1122 register IO *io = 0;
1123 register PerlIO *fp;
1125 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1126 #ifdef ULTRIX_STDIO_BOTCH
1128 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1130 return PerlIO_tell(fp);
1132 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1133 report_evil_fh(gv, io, PL_op->op_type);
1134 SETERRNO(EBADF,RMS_IFI);
1139 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
1141 register IO *io = 0;
1142 register PerlIO *fp;
1144 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1145 #ifdef ULTRIX_STDIO_BOTCH
1147 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1149 return PerlIO_seek(fp, pos, whence) >= 0;
1151 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1152 report_evil_fh(gv, io, PL_op->op_type);
1153 SETERRNO(EBADF,RMS_IFI);
1158 Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
1160 register IO *io = 0;
1161 register PerlIO *fp;
1163 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
1164 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
1165 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1166 report_evil_fh(gv, io, PL_op->op_type);
1167 SETERRNO(EBADF,RMS_IFI);
1172 Perl_mode_from_discipline(pTHX_ SV *discp)
1174 int mode = O_BINARY;
1177 const char *s = SvPV_const(discp,len);
1182 if (s[2] == 'a' && s[3] == 'w'
1183 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1192 if (s[2] == 'r' && s[3] == 'l' && s[4] == 'f'
1193 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1202 goto fail_discipline;
1205 else if (isSPACE(*s)) {
1212 end = strchr(s+1, ':');
1215 #ifndef PERLIO_LAYERS
1216 Perl_croak(aTHX_ "IO layers (like '%.*s') unavailable", end-s, s);
1228 Perl_do_binmode(pTHX_ PerlIO *fp, int iotype, int mode)
1230 /* The old body of this is now in non-LAYER part of perlio.c
1231 * This is a stub for any XS code which might have been calling it.
1233 const char *name = ":raw";
1234 #ifdef PERLIO_USING_CRLF
1235 if (!(mode & O_BINARY))
1238 return PerlIO_binmode(aTHX_ fp, iotype, mode, name);
1241 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE)
1243 my_chsize(int fd, Off_t length)
1246 /* code courtesy of William Kucharski */
1252 if (PerlLIO_fstat(fd, &filebuf) < 0)
1255 if (filebuf.st_size < length) {
1257 /* extend file length */
1259 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
1262 /* write a "0" byte */
1264 if ((PerlLIO_write(fd, "", 1)) != 1)
1268 /* truncate length */
1272 fl.l_start = length;
1273 fl.l_type = F_WRLCK; /* write lock on file space */
1276 * This relies on the UNDOCUMENTED F_FREESP argument to
1277 * fcntl(2), which truncates the file so that it ends at the
1278 * position indicated by fl.l_start.
1280 * Will minor miracles never cease?
1283 if (fcntl(fd, F_FREESP, &fl) < 0)
1289 Perl_croak_nocontext("truncate not implemented");
1290 #endif /* F_FREESP */
1293 #endif /* !HAS_TRUNCATE && !HAS_CHSIZE */
1296 Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
1298 register const char *tmps;
1301 /* assuming fp is checked earlier */
1304 switch (SvTYPE(sv)) {
1306 if (ckWARN(WARN_UNINITIALIZED))
1313 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
1315 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
1316 return !PerlIO_error(fp);
1320 if (PerlIO_isutf8(fp)) {
1322 sv_utf8_upgrade_flags(sv = sv_mortalcopy(sv),
1323 SV_GMAGIC|SV_UTF8_NO_ENCODING);
1325 else if (DO_UTF8(sv)) {
1326 if (!sv_utf8_downgrade((sv = sv_mortalcopy(sv)), TRUE)
1327 && ckWARN_d(WARN_UTF8))
1329 Perl_warner(aTHX_ packWARN(WARN_UTF8), "Wide character in print");
1332 tmps = SvPV_const(sv, len);
1335 /* To detect whether the process is about to overstep its
1336 * filesize limit we would need getrlimit(). We could then
1337 * also transparently raise the limit with setrlimit() --
1338 * but only until the system hard limit/the filesystem limit,
1339 * at which we would get EPERM. Note that when using buffered
1340 * io the write failure can be delayed until the flush/close. --jhi */
1341 if (len && (PerlIO_write(fp,tmps,len) == 0))
1343 return !PerlIO_error(fp);
1353 if (PL_op->op_flags & OPf_REF) {
1358 if (io && IoIFP(io)) {
1360 sv_setpvn(PL_statname,"", 0);
1361 PL_laststype = OP_STAT;
1362 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
1366 return PL_laststatval;
1367 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1368 report_evil_fh(gv, io, PL_op->op_type);
1370 sv_setpvn(PL_statname,"", 0);
1371 return (PL_laststatval = -1);
1374 else if (PL_op->op_private & OPpFT_STACKED) {
1375 return PL_laststatval;
1382 if (SvTYPE(sv) == SVt_PVGV) {
1386 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
1391 s = SvPV_const(sv, len);
1393 sv_setpvn(PL_statname, s, len);
1394 s = SvPVX_const(PL_statname); /* s now NUL-terminated */
1395 PL_laststype = OP_STAT;
1396 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1397 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
1398 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
1399 return PL_laststatval;
1403 static const char no_prev_lstat[] = "The stat preceding -l _ wasn't an lstat";
1410 if (PL_op->op_flags & OPf_REF) {
1412 if (cGVOP_gv == PL_defgv) {
1413 if (PL_laststype != OP_LSTAT)
1414 Perl_croak(aTHX_ no_prev_lstat);
1415 return PL_laststatval;
1417 if (ckWARN(WARN_IO)) {
1418 Perl_warner(aTHX_ packWARN(WARN_IO), "Use of -l on filehandle %s",
1420 return (PL_laststatval = -1);
1423 else if (PL_laststype != OP_LSTAT
1424 && (PL_op->op_private & OPpFT_STACKED) && ckWARN(WARN_IO))
1425 Perl_croak(aTHX_ no_prev_lstat);
1427 PL_laststype = OP_LSTAT;
1431 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV && ckWARN(WARN_IO)) {
1432 Perl_warner(aTHX_ packWARN(WARN_IO), "Use of -l on filehandle %s",
1433 GvENAME((GV*) SvRV(sv)));
1434 return (PL_laststatval = -1);
1436 /* XXX Do really need to be calling SvPV() all these times? */
1437 sv_setpv(PL_statname,SvPV_nolen_const(sv));
1438 PL_laststatval = PerlLIO_lstat(SvPV_nolen_const(sv),&PL_statcache);
1439 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(sv), '\n'))
1440 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "lstat");
1441 return PL_laststatval;
1446 Perl_do_aexec(pTHX_ SV *really, register SV **mark, register SV **sp)
1448 return do_aexec5(really, mark, sp, 0, 0);
1453 Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1454 int fd, int do_report)
1457 #if defined(MACOS_TRADITIONAL) || defined(__SYMBIAN32__)
1458 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1462 const char *tmps = Nullch;
1463 Newx(PL_Argv, sp - mark + 1, char*);
1466 while (++mark <= sp) {
1468 *a++ = (char*)SvPV_nolen_const(*mark);
1474 tmps = SvPV_nolen_const(really);
1475 if ((!really && *PL_Argv[0] != '/') ||
1476 (really && *tmps != '/')) /* will execvp use PATH? */
1477 TAINT_ENV(); /* testing IFS here is overkill, probably */
1479 if (really && *tmps)
1480 PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
1482 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1484 if (ckWARN(WARN_EXEC))
1485 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
1486 (really ? tmps : PL_Argv[0]), Strerror(errno));
1490 PerlLIO_write(fd, (void*)&e, sizeof(int));
1500 Perl_do_execfree(pTHX)
1503 PL_Argv = Null(char **);
1508 #if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC) && !defined(__SYMBIAN32__) && !defined(MACOS_TRADITIONAL)
1511 Perl_do_exec(pTHX_ const char *cmd)
1513 return do_exec3(cmd,0,0);
1517 Perl_do_exec3(pTHX_ const char *incmd, int fd, int do_report)
1525 /* Make a copy so we can change it */
1526 cmdlen = strlen(incmd);
1527 Newx(cmd, cmdlen+1, char);
1528 strncpy(cmd, incmd, cmdlen);
1531 while (*cmd && isSPACE(*cmd))
1534 /* save an extra exec if possible */
1538 char flags[PERL_FLAGS_MAX];
1539 if (strnEQ(cmd,PL_cshname,PL_cshlen) &&
1540 strnEQ(cmd+PL_cshlen," -c",3)) {
1542 strlcpy(flags, "-c", PERL_FLAGS_MAX);
1546 s = cmd+PL_cshlen+3;
1550 strlcat(flags, "f", PERL_FLAGS_MAX);
1564 if (s[-1] == '\'') {
1567 PerlProc_execl(PL_cshname,"csh", flags, ncmd, (char*)0);
1578 /* see if there are shell metacharacters in it */
1580 if (*cmd == '.' && isSPACE(cmd[1]))
1583 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1586 for (s = cmd; *s && isALNUM(*s); s++) ; /* catch VAR=val gizmo */
1590 for (s = cmd; *s; s++) {
1591 if (*s != ' ' && !isALPHA(*s) &&
1592 strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1593 if (*s == '\n' && !s[1]) {
1597 /* handle the 2>&1 construct at the end */
1598 if (*s == '>' && s[1] == '&' && s[2] == '1'
1599 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1600 && (!s[3] || isSPACE(s[3])))
1602 const char *t = s + 3;
1604 while (*t && isSPACE(*t))
1606 if (!*t && (PerlLIO_dup2(1,2) != -1)) {
1613 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
1620 Newx(PL_Argv, (s - cmd) / 2 + 2, char*);
1621 PL_Cmd = savepvn(cmd, s-cmd);
1623 for (s = PL_Cmd; *s;) {
1624 while (*s && isSPACE(*s)) s++;
1627 while (*s && !isSPACE(*s)) s++;
1634 PerlProc_execvp(PL_Argv[0],PL_Argv);
1636 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1641 if (ckWARN(WARN_EXEC))
1642 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
1643 PL_Argv[0], Strerror(errno));
1646 PerlLIO_write(fd, (void*)&e, sizeof(int));
1656 #endif /* OS2 || WIN32 */
1659 Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
1662 register I32 tot = 0;
1665 SV ** const oldmark = mark;
1667 #define APPLY_TAINT_PROPER() \
1669 if (PL_tainted) { TAINT_PROPER(what); } \
1672 /* This is a first heuristic; it doesn't catch tainting magic. */
1674 while (++mark <= sp) {
1675 if (SvTAINTED(*mark)) {
1685 APPLY_TAINT_PROPER();
1688 APPLY_TAINT_PROPER();
1690 while (++mark <= sp) {
1692 if (SvTYPE(*mark) == SVt_PVGV) {
1695 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1697 APPLY_TAINT_PROPER();
1698 if (fchmod(PerlIO_fileno(IoIFP(GvIOn(gv))), val))
1701 Perl_die(aTHX_ PL_no_func, "fchmod");
1708 else if (SvROK(*mark) && SvTYPE(SvRV(*mark)) == SVt_PVGV) {
1709 gv = (GV*)SvRV(*mark);
1713 const char *name = SvPV_nolen_const(*mark);
1714 APPLY_TAINT_PROPER();
1715 if (PerlLIO_chmod(name, val))
1724 APPLY_TAINT_PROPER();
1725 if (sp - mark > 2) {
1727 val = SvIVx(*++mark);
1728 val2 = SvIVx(*++mark);
1729 APPLY_TAINT_PROPER();
1731 while (++mark <= sp) {
1733 if (SvTYPE(*mark) == SVt_PVGV) {
1736 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1738 APPLY_TAINT_PROPER();
1739 if (fchown(PerlIO_fileno(IoIFP(GvIOn(gv))), val, val2))
1742 Perl_die(aTHX_ PL_no_func, "fchown");
1749 else if (SvROK(*mark) && SvTYPE(SvRV(*mark)) == SVt_PVGV) {
1750 gv = (GV*)SvRV(*mark);
1754 const char *name = SvPV_nolen_const(*mark);
1755 APPLY_TAINT_PROPER();
1756 if (PerlLIO_chown(name, val, val2))
1764 XXX Should we make lchown() directly available from perl?
1765 For now, we'll let Configure test for HAS_LCHOWN, but do
1766 nothing in the core.
1772 APPLY_TAINT_PROPER();
1775 s = SvPVx_nolen_const(*++mark);
1777 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1779 if ((val = whichsig(s)) < 0)
1780 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
1784 APPLY_TAINT_PROPER();
1787 /* kill() doesn't do process groups (job trees?) under VMS */
1788 if (val < 0) val = -val;
1789 if (val == SIGKILL) {
1790 # include <starlet.h>
1791 /* Use native sys$delprc() to insure that target process is
1792 * deleted; supervisor-mode images don't pay attention to
1793 * CRTL's emulation of Unix-style signals and kill()
1795 while (++mark <= sp) {
1796 I32 proc = SvIVx(*mark);
1797 register unsigned long int __vmssts;
1798 APPLY_TAINT_PROPER();
1799 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1803 case SS$_NOSUCHNODE:
1804 SETERRNO(ESRCH,__vmssts);
1807 SETERRNO(EPERM,__vmssts);
1810 SETERRNO(EVMSERR,__vmssts);
1819 while (++mark <= sp) {
1820 I32 proc = SvIVx(*mark);
1821 APPLY_TAINT_PROPER();
1823 if (PerlProc_killpg(proc,val)) /* BSD */
1825 if (PerlProc_kill(-proc,val)) /* SYSV */
1831 while (++mark <= sp) {
1832 I32 proc = SvIVx(*mark);
1833 APPLY_TAINT_PROPER();
1834 if (PerlProc_kill(proc, val))
1842 APPLY_TAINT_PROPER();
1844 while (++mark <= sp) {
1845 s = SvPV_nolen_const(*mark);
1846 APPLY_TAINT_PROPER();
1847 if (PL_euid || PL_unsafe) {
1851 else { /* don't let root wipe out directories without -U */
1852 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1864 APPLY_TAINT_PROPER();
1865 if (sp - mark > 2) {
1866 #if defined(I_UTIME) || defined(VMS)
1867 struct utimbuf utbuf;
1868 struct utimbuf *utbufp = &utbuf;
1874 void *utbufp = &utbuf;
1877 SV* accessed = *++mark;
1878 SV* modified = *++mark;
1880 /* Be like C, and if both times are undefined, let the C
1881 * library figure out what to do. This usually means
1882 * "current time". */
1884 if ( accessed == &PL_sv_undef && modified == &PL_sv_undef )
1887 Zero(&utbuf, sizeof utbuf, char);
1889 utbuf.actime = (Time_t)SvNVx(accessed); /* time accessed */
1890 utbuf.modtime = (Time_t)SvNVx(modified); /* time modified */
1892 utbuf.actime = (Time_t)SvIVx(accessed); /* time accessed */
1893 utbuf.modtime = (Time_t)SvIVx(modified); /* time modified */
1896 APPLY_TAINT_PROPER();
1898 while (++mark <= sp) {
1899 char *name = SvPV_nolen(*mark);
1900 APPLY_TAINT_PROPER();
1901 if (PerlLIO_utime(name, utbufp))
1912 #undef APPLY_TAINT_PROPER
1915 /* Do the permissions allow some operation? Assumes statcache already set. */
1916 #ifndef VMS /* VMS' cando is in vms.c */
1918 Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register const Stat_t *statbufp)
1919 /* Note: we use "effective" both for uids and gids.
1920 * Here we are betting on Uid_t being equal or wider than Gid_t. */
1923 /* [Comments and code from Len Reed]
1924 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1925 * to write-protected files. The execute permission bit is set
1926 * by the Miscrosoft C library stat() function for the following:
1931 * All files and directories are readable.
1932 * Directories and special files, e.g. "CON", cannot be
1934 * [Comment by Tom Dinger -- a directory can have the write-protect
1935 * bit set in the file system, but DOS permits changes to
1936 * the directory anyway. In addition, all bets are off
1937 * here for networked software, such as Novell and
1941 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1942 * too so it will actually look into the files for magic numbers
1944 return (mode & statbufp->st_mode) ? TRUE : FALSE;
1946 #else /* ! DOSISH */
1947 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
1948 if (mode == S_IXUSR) {
1949 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1953 return TRUE; /* root reads and writes anything */
1956 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1957 if (statbufp->st_mode & mode)
1958 return TRUE; /* ok as "user" */
1960 else if (ingroup(statbufp->st_gid,effective)) {
1961 if (statbufp->st_mode & mode >> 3)
1962 return TRUE; /* ok as "group" */
1964 else if (statbufp->st_mode & mode >> 6)
1965 return TRUE; /* ok as "other" */
1967 #endif /* ! DOSISH */
1972 Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective)
1974 #ifdef MACOS_TRADITIONAL
1975 /* This is simply not correct for AppleShare, but fix it yerself. */
1978 if (testgid == (effective ? PL_egid : PL_gid))
1980 #ifdef HAS_GETGROUPS
1985 Groups_t gary[NGROUPS];
1988 anum = getgroups(NGROUPS,gary);
1990 if (gary[anum] == testgid)
1998 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
2001 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
2003 key_t key = (key_t)SvNVx(*++mark);
2004 const I32 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
2005 const I32 flags = SvIVx(*++mark);
2013 return msgget(key, flags);
2017 return semget(key, n, flags);
2021 return shmget(key, n, flags);
2023 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
2025 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2028 return -1; /* should never happen */
2032 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
2039 const I32 id = SvIVx(*++mark);
2040 const I32 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
2041 const I32 cmd = SvIVx(*++mark);
2042 PERL_UNUSED_ARG(sp);
2046 getinfo = (cmd == IPC_STAT);
2052 if (cmd == IPC_STAT || cmd == IPC_SET)
2053 infosize = sizeof(struct msqid_ds);
2058 if (cmd == IPC_STAT || cmd == IPC_SET)
2059 infosize = sizeof(struct shmid_ds);
2065 if (cmd == IPC_STAT || cmd == IPC_SET)
2066 infosize = sizeof(struct semid_ds);
2067 else if (cmd == GETALL || cmd == SETALL)
2069 struct semid_ds semds;
2071 #ifdef EXTRA_F_IN_SEMUN_BUF
2072 semun.buff = &semds;
2076 getinfo = (cmd == GETALL);
2077 if (Semctl(id, 0, IPC_STAT, semun) == -1)
2079 infosize = semds.sem_nsems * sizeof(short);
2080 /* "short" is technically wrong but much more portable
2081 than guessing about u_?short(_t)? */
2084 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2088 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
2090 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2098 SvPV_force_nolen(astr);
2099 a = SvGROW(astr, infosize+1);
2104 a = SvPV(astr, len);
2105 if (len != infosize)
2106 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
2115 a = INT2PTR(char *,i); /* ouch */
2122 ret = msgctl(id, cmd, (struct msqid_ds *)a);
2128 union semun unsemds;
2130 #ifdef EXTRA_F_IN_SEMUN_BUF
2131 unsemds.buff = (struct semid_ds *)a;
2133 unsemds.buf = (struct semid_ds *)a;
2135 ret = Semctl(id, n, cmd, unsemds);
2137 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2144 ret = shmctl(id, cmd, (struct shmid_ds *)a);
2148 if (getinfo && ret >= 0) {
2149 SvCUR_set(astr, infosize);
2150 *SvEND(astr) = '\0';
2157 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
2164 const I32 id = SvIVx(*++mark);
2165 PERL_UNUSED_ARG(sp);
2168 flags = SvIVx(*++mark);
2169 mbuf = SvPV_const(mstr, len);
2170 if ((msize = len - sizeof(long)) < 0)
2171 Perl_croak(aTHX_ "Arg too short for msgsnd");
2173 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
2175 Perl_croak(aTHX_ "msgsnd not implemented");
2180 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
2186 I32 msize, flags, ret;
2187 const I32 id = SvIVx(*++mark);
2188 PERL_UNUSED_ARG(sp);
2191 /* suppress warning when reading into undef var --jhi */
2193 sv_setpvn(mstr, "", 0);
2194 msize = SvIVx(*++mark);
2195 mtype = (long)SvIVx(*++mark);
2196 flags = SvIVx(*++mark);
2197 SvPV_force_nolen(mstr);
2198 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
2201 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
2203 SvCUR_set(mstr, sizeof(long)+ret);
2204 *SvEND(mstr) = '\0';
2205 #ifndef INCOMPLETE_TAINTS
2206 /* who knows who has been playing with this message? */
2212 Perl_croak(aTHX_ "msgrcv not implemented");
2217 Perl_do_semop(pTHX_ SV **mark, SV **sp)
2223 const I32 id = SvIVx(*++mark);
2224 PERL_UNUSED_ARG(sp);
2227 opbuf = SvPV_const(opstr, opsize);
2228 if (opsize < 3 * SHORTSIZE
2229 || (opsize % (3 * SHORTSIZE))) {
2230 SETERRNO(EINVAL,LIB_INVARG);
2234 /* We can't assume that sizeof(struct sembuf) == 3 * sizeof(short). */
2236 const int nsops = opsize / (3 * sizeof (short));
2238 short *ops = (short *) opbuf;
2240 struct sembuf *temps, *t;
2243 Newx (temps, nsops, struct sembuf);
2251 result = semop(id, temps, nsops);
2265 Perl_croak(aTHX_ "semop not implemented");
2270 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
2276 struct shmid_ds shmds;
2277 const I32 id = SvIVx(*++mark);
2278 PERL_UNUSED_ARG(sp);
2281 mpos = SvIVx(*++mark);
2282 msize = SvIVx(*++mark);
2284 if (shmctl(id, IPC_STAT, &shmds) == -1)
2286 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
2287 SETERRNO(EFAULT,SS_ACCVIO); /* can't do as caller requested */
2290 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
2291 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2293 if (optype == OP_SHMREAD) {
2295 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2297 sv_setpvn(mstr, "", 0);
2298 SvPV_force_nolen(mstr);
2299 mbuf = SvGROW(mstr, msize+1);
2301 Copy(shm + mpos, mbuf, msize, char);
2302 SvCUR_set(mstr, msize);
2303 *SvEND(mstr) = '\0';
2305 #ifndef INCOMPLETE_TAINTS
2306 /* who knows who has been playing with this shared memory? */
2314 const char *mbuf = SvPV_const(mstr, len);
2315 if ((n = len) > msize)
2317 Copy(mbuf, shm + mpos, n, char);
2319 memzero(shm + mpos + n, msize - n);
2323 Perl_croak(aTHX_ "shm I/O not implemented");
2327 #endif /* SYSV IPC */
2332 =for apidoc start_glob
2334 Function called by C<do_readline> to spawn a glob (or do the glob inside
2335 perl on VMS). This code used to be inline, but now perl uses C<File::Glob>
2336 this glob starter is only used by miniperl during the build process.
2337 Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up.
2343 Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
2346 SV * const tmpcmd = NEWSV(55, 0);
2350 #ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
2351 /* since spawning off a process is a real performance hit */
2353 #include <descrip.h>
2354 #include <lib$routines.h>
2357 char rslt[NAM$C_MAXRSS+1+sizeof(unsigned short int)] = {'\0','\0'};
2358 char vmsspec[NAM$C_MAXRSS+1];
2359 char * const rstr = rslt + sizeof(unsigned short int);
2360 char *begin, *end, *cp;
2361 $DESCRIPTOR(dfltdsc,"SYS$DISK:[]*.*;");
2364 struct dsc$descriptor_s wilddsc
2365 = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
2366 struct dsc$descriptor_vs rsdsc
2367 = {sizeof rslt, DSC$K_DTYPE_VT, DSC$K_CLASS_VS, rslt};
2368 unsigned long int cxt = 0, sts = 0, ok = 1, hasdir = 0, hasver = 0, isunix = 0;
2370 /* We could find out if there's an explicit dev/dir or version
2371 by peeking into lib$find_file's internal context at
2372 ((struct NAM *)((struct FAB *)cxt)->fab$l_nam)->nam$l_fnb
2373 but that's unsupported, so I don't want to do it now and
2374 have it bite someone in the future. */
2375 cp = SvPV(tmpglob,i);
2377 if (cp[i] == ';') hasver = 1;
2379 if (sts) hasver = 1;
2383 hasdir = isunix = 1;
2386 if (cp[i] == ']' || cp[i] == '>' || cp[i] == ':') {
2391 if ((tmpfp = PerlIO_tmpfile()) != NULL) {
2393 if (!PerlLIO_stat(SvPVX_const(tmpglob),&st) && S_ISDIR(st.st_mode))
2394 ok = ((wilddsc.dsc$a_pointer = tovmspath(SvPVX(tmpglob),vmsspec)) != NULL);
2395 else ok = ((wilddsc.dsc$a_pointer = tovmsspec(SvPVX(tmpglob),vmsspec)) != NULL);
2396 if (ok) wilddsc.dsc$w_length = (unsigned short int) strlen(wilddsc.dsc$a_pointer);
2397 for (cp=wilddsc.dsc$a_pointer; ok && cp && *cp; cp++)
2398 if (*cp == '?') *cp = '%'; /* VMS style single-char wildcard */
2399 while (ok && ((sts = lib$find_file(&wilddsc,&rsdsc,&cxt,
2400 &dfltdsc,NULL,NULL,NULL))&1)) {
2401 /* with varying string, 1st word of buffer contains result length */
2402 end = rstr + *((unsigned short int*)rslt);
2403 if (!hasver) while (*end != ';' && end > rstr) end--;
2404 *(end++) = '\n'; *end = '\0';
2405 for (cp = rstr; *cp; cp++) *cp = _tolower(*cp);
2407 if (isunix) trim_unixpath(rstr,SvPVX(tmpglob),1);
2412 while (*(--begin) != ']' && *begin != '>') ;
2415 ok = (PerlIO_puts(tmpfp,begin) != EOF);
2417 if (cxt) (void)lib$find_file_end(&cxt);
2418 if (ok && sts != RMS$_NMF &&
2419 sts != RMS$_DNF && sts != RMS_FNF) ok = 0;
2422 SETERRNO((sts == RMS$_SYN ? EINVAL : EVMSERR),sts);
2424 PerlIO_close(tmpfp);
2428 PerlIO_rewind(tmpfp);
2429 IoTYPE(io) = IoTYPE_RDONLY;
2430 IoIFP(io) = fp = tmpfp;
2431 IoFLAGS(io) &= ~IOf_UNTAINT; /* maybe redundant */
2436 #ifdef MACOS_TRADITIONAL
2437 sv_setpv(tmpcmd, "glob ");
2438 sv_catsv(tmpcmd, tmpglob);
2439 sv_catpv(tmpcmd, " |");
2443 sv_setpv(tmpcmd, "for a in ");
2444 sv_catsv(tmpcmd, tmpglob);
2445 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
2448 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
2449 sv_catsv(tmpcmd, tmpglob);
2451 sv_setpv(tmpcmd, "perlglob ");
2452 sv_catsv(tmpcmd, tmpglob);
2453 sv_catpv(tmpcmd, " |");
2458 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
2459 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
2460 sv_catsv(tmpcmd, tmpglob);
2461 sv_catpv(tmpcmd, "' 2>/dev/null |");
2463 sv_setpv(tmpcmd, "echo ");
2464 sv_catsv(tmpcmd, tmpglob);
2466 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
2468 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
2471 #endif /* !DOSISH */
2472 #endif /* MACOS_TRADITIONAL */
2473 (void)do_open(PL_last_in_gv, (char*)SvPVX_const(tmpcmd), SvCUR(tmpcmd),
2474 FALSE, O_RDONLY, 0, Nullfp);
2483 * c-indentation-style: bsd
2485 * indent-tabs-mode: t
2488 * ex: set ts=8 sts=4 sw=4 noet: