3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, 2004, 2005, 2006, 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_openn(pTHX_ GV *gv, register const char *oname, I32 len, int as_raw,
63 int rawmode, int rawperm, PerlIO *supplied_fp, SV **svp,
67 register IO * const io = GvIOn(gv);
68 PerlIO *saveifp = Nullfp;
69 PerlIO *saveofp = Nullfp;
71 char savetype = IoTYPE_CLOSED;
76 bool was_fdopen = FALSE;
77 bool in_raw = 0, in_crlf = 0, out_raw = 0, out_crlf = 0;
79 char mode[PERL_MODE_MAX]; /* stdio file mode ("r\0", "rb\0", "r+b\0" etc.) */
82 Zero(mode,sizeof(mode),char);
83 PL_forkprocess = 1; /* assume true if no fork */
85 /* Collect default raw/crlf info from the op */
86 if (PL_op && PL_op->op_type == OP_OPEN) {
87 /* set up IO layers */
88 const U8 flags = PL_op->op_private;
89 in_raw = (flags & OPpOPEN_IN_RAW);
90 in_crlf = (flags & OPpOPEN_IN_CRLF);
91 out_raw = (flags & OPpOPEN_OUT_RAW);
92 out_crlf = (flags & OPpOPEN_OUT_CRLF);
95 /* If currently open - close before we re-open */
97 fd = PerlIO_fileno(IoIFP(io));
98 if (IoTYPE(io) == IoTYPE_STD) {
99 /* This is a clone of one of STD* handles */
102 else if (fd >= 0 && fd <= PL_maxsysfd) {
103 /* This is one of the original STD* handles */
106 savetype = IoTYPE(io);
110 else if (IoTYPE(io) == IoTYPE_PIPE)
111 result = PerlProc_pclose(IoIFP(io));
112 else if (IoIFP(io) != IoOFP(io)) {
114 result = PerlIO_close(IoOFP(io));
115 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
118 result = PerlIO_close(IoIFP(io));
121 result = PerlIO_close(IoIFP(io));
122 if (result == EOF && fd > PL_maxsysfd) {
123 /* Why is this not Perl_warn*() call ? */
124 PerlIO_printf(Perl_error_log,
125 "Warning: unable to close filehandle %s properly.\n",
128 IoOFP(io) = IoIFP(io) = Nullfp;
132 /* sysopen style args, i.e. integer mode and permissions */
134 const int appendtrunc =
136 #ifdef O_APPEND /* Not fully portable. */
139 #ifdef O_TRUNC /* Not fully portable. */
143 const int modifyingmode = O_WRONLY|O_RDWR|O_CREAT|appendtrunc;
147 Perl_croak(aTHX_ "panic: sysopen with multiple args");
155 It might be (in OS/390 and Mac OS Classic it is)
161 This means that simple & with O_RDWR would look
162 like O_RDONLY is present. Therefore we have to
165 if ((ismodifying = (rawmode & modifyingmode))) {
166 if ((ismodifying & O_WRONLY) == O_WRONLY ||
167 (ismodifying & O_RDWR) == O_RDWR ||
168 (ismodifying & (O_CREAT|appendtrunc)))
169 TAINT_PROPER("sysopen");
171 mode[ix++] = IoTYPE_NUMERIC; /* Marker to openn to use numeric "sysopen" */
173 #if defined(USE_64_BIT_RAWIO) && defined(O_LARGEFILE)
174 rawmode |= O_LARGEFILE; /* Transparently largefiley. */
177 IoTYPE(io) = PerlIO_intmode2str(rawmode, &mode[ix], &writing);
179 namesv = sv_2mortal(newSVpv(oname,0));
183 fp = PerlIO_openn(aTHX_ type, mode, -1, rawmode, rawperm, NULL, num_svs, svp);
186 /* Regular (non-sys) open */
191 PerlIO *that_fp = NULL;
193 type = savepvn(oname, len);
197 /* Lose leading and trailing white space */
198 for (; isSPACE(*type); type++) ;
199 while (tend > type && isSPACE(tend[-1]))
203 /* New style explicit name, type is just mode and layer info */
205 if (SvROK(*svp) && !strchr(oname,'&')) {
207 Perl_warner(aTHX_ packWARN(WARN_IO),
208 "Can't open a reference");
209 SETERRNO(EINVAL, LIB_INVARG);
212 #endif /* USE_STDIO */
213 name = SvOK(*svp) ? savesvpv (*svp) : savepvn ("", 0);
221 if ((*type == IoTYPE_RDWR) && /* scary */
222 (*(type+1) == IoTYPE_RDONLY || *(type+1) == IoTYPE_WRONLY) &&
223 ((!num_svs || (tend > type+1 && tend[-1] != IoTYPE_PIPE)))) {
224 TAINT_PROPER("open");
229 if (*type == IoTYPE_PIPE) {
231 if (type[1] != IoTYPE_STD) {
233 Perl_croak(aTHX_ "Unknown open() mode '%.*s'", (int)olen, oname);
237 for (type++; isSPACE(*type); type++) ;
243 /* command is missing 19990114 */
244 if (ckWARN(WARN_PIPE))
245 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open");
249 if ((*name == '-' && name[1] == '\0') || num_svs)
251 TAINT_PROPER("piped open");
252 if (!num_svs && name[len-1] == '|') {
254 if (ckWARN(WARN_PIPE))
255 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Can't open bidirectional pipe");
261 strlcat(mode, "b", PERL_MODE_MAX);
263 strlcat(mode, "t", PERL_MODE_MAX);
271 fp = PerlProc_popen_list(mode, num_svs, svp);
274 fp = PerlProc_popen(name,mode);
278 if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) {
284 else if (*type == IoTYPE_WRONLY) {
285 TAINT_PROPER("open");
287 if (*type == IoTYPE_WRONLY) {
288 /* Two IoTYPE_WRONLYs in a row make for an IoTYPE_APPEND. */
289 mode[0] = IoTYPE(io) = IoTYPE_APPEND;
299 strlcat(mode, "b", PERL_MODE_MAX);
301 strlcat(mode, "t", PERL_MODE_MAX);
310 dodup = PERLIO_DUP_FD;
316 if (!num_svs && !*type && supplied_fp) {
317 /* "<+&" etc. is used by typemaps */
322 Perl_croak(aTHX_ "More than one argument to '%c&' open",IoTYPE(io));
324 for (; isSPACE(*type); type++) ;
325 if (num_svs && (SvIOK(*svp) || (SvPOK(*svp) && looks_like_number(*svp)))) {
329 else if (isDIGIT(*type)) {
335 thatio = sv_2io(*svp);
339 thatgv = gv_fetchpvn_flags(type, type-tend,
341 thatio = GvIO(thatgv);
345 SETERRNO(EINVAL,SS_IVCHAN);
349 if ((that_fp = IoIFP(thatio))) {
350 /* Flush stdio buffer before dup. --mjd
351 * Unfortunately SEEK_CURing 0 seems to
352 * be optimized away on most platforms;
353 * only Solaris and Linux seem to flush
356 /* sfio fails to clear error on next
357 sfwrite, contrary to documentation.
359 if (PerlIO_seek(that_fp, 0, SEEK_CUR) == -1)
360 PerlIO_clearerr(that_fp);
362 /* On the other hand, do all platforms
363 * take gracefully to flushing a read-only
364 * filehandle? Perhaps we should do
365 * fsetpos(src)+fgetpos(dst)? --nik */
366 PerlIO_flush(that_fp);
367 fd = PerlIO_fileno(that_fp);
368 /* When dup()ing STDIN, STDOUT or STDERR
369 * explicitly set appropriate access mode */
370 if (that_fp == PerlIO_stdout()
371 || that_fp == PerlIO_stderr())
372 IoTYPE(io) = IoTYPE_WRONLY;
373 else if (that_fp == PerlIO_stdin())
374 IoTYPE(io) = IoTYPE_RDONLY;
375 /* When dup()ing a socket, say result is
377 else if (IoTYPE(thatio) == IoTYPE_SOCKET)
378 IoTYPE(io) = IoTYPE_SOCKET;
386 fp = PerlIO_fdupopen(aTHX_ that_fp, NULL, dodup);
390 fd = PerlLIO_dup(fd);
393 if (!(fp = PerlIO_openn(aTHX_ type,mode,fd,0,0,NULL,num_svs,svp))) {
394 if (dodup && fd >= 0)
401 for (; isSPACE(*type); type++) ;
402 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
404 fp = PerlIO_stdout();
405 IoTYPE(io) = IoTYPE_STD;
407 Perl_croak(aTHX_ "More than one argument to '>%c' open",IoTYPE_STD);
412 namesv = sv_2mortal(newSVpvn(type,tend - type));
417 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
420 if (!fp && type && *type && *type != ':' && !isIDFIRST(*type))
421 goto unknown_open_mode;
422 } /* IoTYPE_WRONLY */
423 else if (*type == IoTYPE_RDONLY) {
424 for (type++; isSPACE(*type); type++) ;
428 strlcat(mode, "b", PERL_MODE_MAX);
430 strlcat(mode, "t", PERL_MODE_MAX);
440 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
443 IoTYPE(io) = IoTYPE_STD;
445 Perl_croak(aTHX_ "More than one argument to '<%c' open",IoTYPE_STD);
450 namesv = sv_2mortal(newSVpvn(type,tend - type));
455 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
457 if (!fp && type && *type && *type != ':' && !isIDFIRST(*type))
458 goto unknown_open_mode;
459 } /* IoTYPE_RDONLY */
460 else if ((num_svs && /* '-|...' or '...|' */
461 type[0] == IoTYPE_STD && type[1] == IoTYPE_PIPE) ||
462 (!num_svs && tend > type+1 && tend[-1] == IoTYPE_PIPE)) {
464 type += 2; /* skip over '-|' */
468 while (tend > type && isSPACE(tend[-1]))
470 for (; isSPACE(*type); type++)
476 /* command is missing 19990114 */
477 if (ckWARN(WARN_PIPE))
478 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open");
482 if (!(*name == '-' && name[1] == '\0') || num_svs)
484 TAINT_PROPER("piped open");
489 strlcat(mode, "b", PERL_MODE_MAX);
491 strlcat(mode, "t", PERL_MODE_MAX);
500 fp = PerlProc_popen_list(mode,num_svs,svp);
503 fp = PerlProc_popen(name,mode);
505 IoTYPE(io) = IoTYPE_PIPE;
507 for (; isSPACE(*type); type++) ;
509 if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) {
515 else { /* layer(Args) */
517 goto unknown_open_mode;
519 IoTYPE(io) = IoTYPE_RDONLY;
520 for (; isSPACE(*name); name++)
526 strlcat(mode, "b", PERL_MODE_MAX);
528 strlcat(mode, "t", PERL_MODE_MAX);
536 if (*name == '-' && name[1] == '\0') {
538 IoTYPE(io) = IoTYPE_STD;
542 namesv = sv_2mortal(newSVpvn(type,tend - type));
547 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
552 if (IoTYPE(io) == IoTYPE_RDONLY && ckWARN(WARN_NEWLINE)
553 && strchr(oname, '\n')
556 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
560 if (ckWARN(WARN_IO)) {
561 if ((IoTYPE(io) == IoTYPE_RDONLY) &&
562 (fp == PerlIO_stdout() || fp == PerlIO_stderr())) {
563 Perl_warner(aTHX_ packWARN(WARN_IO),
564 "Filehandle STD%s reopened as %s only for input",
565 ((fp == PerlIO_stdout()) ? "OUT" : "ERR"),
568 else if ((IoTYPE(io) == IoTYPE_WRONLY) && fp == PerlIO_stdin()) {
569 Perl_warner(aTHX_ packWARN(WARN_IO),
570 "Filehandle STDIN reopened as %s only for output",
575 fd = PerlIO_fileno(fp);
576 /* If there is no fd (e.g. PerlIO::scalar) assume it isn't a
577 * socket - this covers PerlIO::scalar - otherwise unless we "know" the
578 * type probe for socket-ness.
580 if (IoTYPE(io) && IoTYPE(io) != IoTYPE_PIPE && IoTYPE(io) != IoTYPE_STD && fd >= 0) {
581 if (PerlLIO_fstat(fd,&PL_statbuf) < 0) {
582 /* If PerlIO claims to have fd we had better be able to fstat() it. */
583 (void) PerlIO_close(fp);
587 if (S_ISSOCK(PL_statbuf.st_mode))
588 IoTYPE(io) = IoTYPE_SOCKET; /* in case a socket was passed in to us */
592 !(PL_statbuf.st_mode & S_IFMT)
596 && IoTYPE(io) != IoTYPE_WRONLY /* Dups of STD* filehandles already have */
597 && IoTYPE(io) != IoTYPE_RDONLY /* type so they aren't marked as sockets */
598 ) { /* on OS's that return 0 on fstat()ed pipe */
600 Sock_size_t buflen = sizeof tmpbuf;
601 if (PerlSock_getsockname(fd, (struct sockaddr *)tmpbuf, &buflen) >= 0
602 || errno != ENOTSOCK)
603 IoTYPE(io) = IoTYPE_SOCKET; /* some OS's return 0 on fstat()ed socket */
604 /* but some return 0 for streams too, sigh */
606 #endif /* HAS_SOCKET */
607 #endif /* !PERL_MICRO */
611 * If this is a standard handle we discard all the layer stuff
612 * and just dup the fd into whatever was on the handle before !
615 if (saveifp) { /* must use old fp? */
616 /* If fd is less that PL_maxsysfd i.e. STDIN..STDERR
617 then dup the new fileno down
620 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
621 if (saveofp != saveifp) { /* was a socket? */
622 PerlIO_close(saveofp);
626 /* Still a small can-of-worms here if (say) PerlIO::scalar
627 is assigned to (say) STDOUT - for now let dup2() fail
628 and provide the error
630 if (PerlLIO_dup2(fd, savefd) < 0) {
631 (void)PerlIO_close(fp);
635 if (savefd != PerlIO_fileno(PerlIO_stdin())) {
636 char newname[FILENAME_MAX+1];
637 if (PerlIO_getname(fp, newname)) {
638 if (fd == PerlIO_fileno(PerlIO_stdout()))
639 Perl_vmssetuserlnm(aTHX_ "SYS$OUTPUT", newname);
640 if (fd == PerlIO_fileno(PerlIO_stderr()))
641 Perl_vmssetuserlnm(aTHX_ "SYS$ERROR", newname);
647 /* PL_fdpid isn't used on Windows, so avoid this useless work.
648 * XXX Probably the same for a lot of other places. */
654 sv = *av_fetch(PL_fdpid,fd,TRUE);
655 SvUPGRADE(sv, SVt_IV);
658 sv = *av_fetch(PL_fdpid,savefd,TRUE);
659 SvUPGRADE(sv, SVt_IV);
666 /* need to close fp without closing underlying fd */
667 int ofd = PerlIO_fileno(fp);
668 int dupfd = PerlLIO_dup(ofd);
669 #if defined(HAS_FCNTL) && defined(F_SETFD)
670 /* Assume if we have F_SETFD we have F_GETFD */
671 int coe = fcntl(ofd,F_GETFD);
674 PerlLIO_dup2(dupfd,ofd);
675 #if defined(HAS_FCNTL) && defined(F_SETFD)
676 /* The dup trick has lost close-on-exec on ofd */
677 fcntl(ofd,F_SETFD, coe);
679 PerlLIO_close(dupfd);
686 fd = PerlIO_fileno(fp);
688 #if defined(HAS_FCNTL) && defined(F_SETFD)
690 const int save_errno = errno;
691 fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
697 IoFLAGS(io) &= ~IOf_NOLINE;
699 if (IoTYPE(io) == IoTYPE_SOCKET
700 || (IoTYPE(io) == IoTYPE_WRONLY && fd >= 0 && S_ISCHR(PL_statbuf.st_mode)) ) {
702 if (*s == IoTYPE_IMPLICIT || *s == IoTYPE_NUMERIC)
705 if (!(IoOFP(io) = PerlIO_openn(aTHX_ type,s,fd,0,0,NULL,0,svp))) {
719 IoTYPE(io) = savetype;
724 Perl_nextargv(pTHX_ register GV *gv)
728 #ifndef FLEXFILENAMES
734 IO * const io = GvIOp(gv);
737 PL_argvoutgv = gv_fetchpvs("ARGVOUT",TRUE,SVt_PVIO);
738 if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
739 IoFLAGS(io) &= ~IOf_START;
741 if (!PL_argvout_stack)
742 PL_argvout_stack = newAV();
743 av_push(PL_argvout_stack, SvREFCNT_inc(PL_defoutgv));
746 if (PL_filemode & (S_ISUID|S_ISGID)) {
747 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
750 (void)fchmod(PL_lastfd,PL_filemode);
752 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
759 while (av_len(GvAV(gv)) >= 0) {
761 sv = av_shift(GvAV(gv));
763 sv_setsv(GvSVn(gv),sv);
764 SvSETMAGIC(GvSV(gv));
765 PL_oldname = SvPVx(GvSV(gv), oldlen);
766 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,Nullfp)) {
768 TAINT_PROPER("inplace open");
769 if (oldlen == 1 && *PL_oldname == '-') {
770 setdefout(gv_fetchpvs("STDOUT",TRUE,SVt_PVIO));
771 return IoIFP(GvIOp(gv));
773 #ifndef FLEXFILENAMES
774 filedev = PL_statbuf.st_dev;
775 fileino = PL_statbuf.st_ino;
777 PL_filemode = PL_statbuf.st_mode;
778 fileuid = PL_statbuf.st_uid;
779 filegid = PL_statbuf.st_gid;
780 if (!S_ISREG(PL_filemode)) {
781 if (ckWARN_d(WARN_INPLACE))
782 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
783 "Can't do inplace edit: %s is not a regular file",
789 const char *star = strchr(PL_inplace, '*');
791 const char *begin = PL_inplace;
792 sv_setpvn(sv, "", 0);
794 sv_catpvn(sv, begin, star - begin);
795 sv_catpvn(sv, PL_oldname, oldlen);
797 } while ((star = strchr(begin, '*')));
802 sv_catpv(sv,PL_inplace);
804 #ifndef FLEXFILENAMES
805 if ((PerlLIO_stat(SvPVX_const(sv),&PL_statbuf) >= 0
806 && PL_statbuf.st_dev == filedev
807 && PL_statbuf.st_ino == fileino)
809 || ((_djstat_fail_bits & _STFAIL_TRUENAME)!=0)
813 if (ckWARN_d(WARN_INPLACE))
814 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
815 "Can't do inplace edit: %"SVf" would not be unique",
822 #if !defined(DOSISH) && !defined(__CYGWIN__) && !defined(EPOC)
823 if (PerlLIO_rename(PL_oldname,SvPVX_const(sv)) < 0) {
824 if (ckWARN_d(WARN_INPLACE))
825 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
826 "Can't rename %s to %"SVf": %s, skipping file",
827 PL_oldname, sv, Strerror(errno) );
833 (void)PerlLIO_unlink(SvPVX_const(sv));
834 (void)PerlLIO_rename(PL_oldname,SvPVX_const(sv));
835 do_open(gv,(char*)SvPVX_const(sv),SvCUR(sv),PL_inplace!=0,
839 (void)UNLINK(SvPVX_const(sv));
840 if (link(PL_oldname,SvPVX_const(sv)) < 0) {
841 if (ckWARN_d(WARN_INPLACE))
842 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
843 "Can't rename %s to %"SVf": %s, skipping file",
844 PL_oldname, sv, Strerror(errno) );
848 (void)UNLINK(PL_oldname);
852 #if !defined(DOSISH) && !defined(AMIGAOS)
853 # ifndef VMS /* Don't delete; use automatic file versioning */
854 if (UNLINK(PL_oldname) < 0) {
855 if (ckWARN_d(WARN_INPLACE))
856 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
857 "Can't remove %s: %s, skipping file",
858 PL_oldname, Strerror(errno) );
864 Perl_croak(aTHX_ "Can't do inplace edit without backup");
868 sv_setpvn(sv,">",!PL_inplace);
869 sv_catpvn(sv,PL_oldname,oldlen);
870 SETERRNO(0,0); /* in case sprintf set errno */
872 if (!do_open(PL_argvoutgv,(char*)SvPVX_const(sv),SvCUR(sv),
873 PL_inplace!=0,O_WRONLY|O_CREAT|O_TRUNC,0,Nullfp))
875 if (!do_open(PL_argvoutgv,(char*)SvPVX_const(sv),SvCUR(sv),
876 PL_inplace!=0,O_WRONLY|O_CREAT|OPEN_EXCL,0666,
880 if (ckWARN_d(WARN_INPLACE))
881 Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't do inplace edit on %s: %s",
882 PL_oldname, Strerror(errno) );
886 setdefout(PL_argvoutgv);
887 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
888 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
890 (void)fchmod(PL_lastfd,PL_filemode);
892 # if !(defined(WIN32) && defined(__BORLANDC__))
893 /* Borland runtime creates a readonly file! */
894 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
897 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
899 (void)fchown(PL_lastfd,fileuid,filegid);
902 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
907 return IoIFP(GvIOp(gv));
910 if (ckWARN_d(WARN_INPLACE)) {
911 const int eno = errno;
912 if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0
913 && !S_ISREG(PL_statbuf.st_mode))
915 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
916 "Can't do inplace edit: %s is not a regular file",
920 Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't open %s: %s",
921 PL_oldname, Strerror(eno));
925 if (io && (IoFLAGS(io) & IOf_ARGV))
926 IoFLAGS(io) |= IOf_START;
928 (void)do_close(PL_argvoutgv,FALSE);
929 if (io && (IoFLAGS(io) & IOf_ARGV)
930 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
932 GV *oldout = (GV*)av_pop(PL_argvout_stack);
934 SvREFCNT_dec(oldout);
937 setdefout(gv_fetchpvs("STDOUT",TRUE,SVt_PVIO));
942 /* explicit renamed to avoid C++ conflict -- kja */
944 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
952 if (!gv || SvTYPE(gv) != SVt_PVGV) {
954 SETERRNO(EBADF,SS_IVCHAN);
958 if (!io) { /* never opened */
960 if (ckWARN(WARN_UNOPENED)) /* no check for closed here */
961 report_evil_fh(gv, io, PL_op->op_type);
962 SETERRNO(EBADF,SS_IVCHAN);
966 retval = io_close(io, not_implicit);
970 IoLINES_LEFT(io) = IoPAGE_LEN(io);
972 IoTYPE(io) = IoTYPE_CLOSED;
977 Perl_io_close(pTHX_ IO *io, bool not_implicit)
983 if (IoTYPE(io) == IoTYPE_PIPE) {
984 const int status = PerlProc_pclose(IoIFP(io));
986 STATUS_NATIVE_CHILD_SET(status);
987 retval = (STATUS_UNIX == 0);
990 retval = (status != -1);
993 else if (IoTYPE(io) == IoTYPE_STD)
996 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
997 const bool prev_err = PerlIO_error(IoOFP(io));
998 retval = (PerlIO_close(IoOFP(io)) != EOF && !prev_err);
999 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
1002 const bool prev_err = PerlIO_error(IoIFP(io));
1003 retval = (PerlIO_close(IoIFP(io)) != EOF && !prev_err);
1006 IoOFP(io) = IoIFP(io) = Nullfp;
1008 else if (not_implicit) {
1009 SETERRNO(EBADF,SS_IVCHAN);
1016 Perl_do_eof(pTHX_ GV *gv)
1019 register IO * const io = GvIO(gv);
1023 else if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
1024 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1027 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
1028 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
1029 return FALSE; /* this is the most usual case */
1033 /* getc and ungetc can stomp on errno */
1034 const int saverrno = errno;
1035 const int ch = PerlIO_getc(IoIFP(io));
1037 (void)PerlIO_ungetc(IoIFP(io),ch);
1044 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
1045 if (PerlIO_get_cnt(IoIFP(io)) < -1)
1046 PerlIO_set_cnt(IoIFP(io),-1);
1048 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
1049 if (gv != PL_argvgv || !nextargv(gv)) /* get another fp handy */
1053 return TRUE; /* normal fp, definitely end of file */
1059 Perl_do_tell(pTHX_ GV *gv)
1062 register IO *io = NULL;
1063 register PerlIO *fp;
1065 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1066 #ifdef ULTRIX_STDIO_BOTCH
1068 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1070 return PerlIO_tell(fp);
1072 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1073 report_evil_fh(gv, io, PL_op->op_type);
1074 SETERRNO(EBADF,RMS_IFI);
1079 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
1082 register IO *io = NULL;
1083 register PerlIO *fp;
1085 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1086 #ifdef ULTRIX_STDIO_BOTCH
1088 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1090 return PerlIO_seek(fp, pos, whence) >= 0;
1092 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1093 report_evil_fh(gv, io, PL_op->op_type);
1094 SETERRNO(EBADF,RMS_IFI);
1099 Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
1102 register IO *io = NULL;
1103 register PerlIO *fp;
1105 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
1106 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
1107 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1108 report_evil_fh(gv, io, PL_op->op_type);
1109 SETERRNO(EBADF,RMS_IFI);
1114 Perl_mode_from_discipline(pTHX_ SV *discp)
1116 int mode = O_BINARY;
1119 const char *s = SvPV_const(discp,len);
1124 if (s[2] == 'a' && s[3] == 'w'
1125 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1134 if (s[2] == 'r' && s[3] == 'l' && s[4] == 'f'
1135 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1144 goto fail_discipline;
1147 else if (isSPACE(*s)) {
1154 end = strchr(s+1, ':');
1157 #ifndef PERLIO_LAYERS
1158 Perl_croak(aTHX_ "IO layers (like '%.*s') unavailable", end-s, s);
1169 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE)
1171 my_chsize(int fd, Off_t length)
1174 /* code courtesy of William Kucharski */
1180 if (PerlLIO_fstat(fd, &filebuf) < 0)
1183 if (filebuf.st_size < length) {
1185 /* extend file length */
1187 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
1190 /* write a "0" byte */
1192 if ((PerlLIO_write(fd, "", 1)) != 1)
1196 /* truncate length */
1200 fl.l_start = length;
1201 fl.l_type = F_WRLCK; /* write lock on file space */
1204 * This relies on the UNDOCUMENTED F_FREESP argument to
1205 * fcntl(2), which truncates the file so that it ends at the
1206 * position indicated by fl.l_start.
1208 * Will minor miracles never cease?
1211 if (fcntl(fd, F_FREESP, &fl) < 0)
1217 Perl_croak_nocontext("truncate not implemented");
1218 #endif /* F_FREESP */
1221 #endif /* !HAS_TRUNCATE && !HAS_CHSIZE */
1224 Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
1227 register const char *tmps;
1230 /* assuming fp is checked earlier */
1233 switch (SvTYPE(sv)) {
1235 if (ckWARN(WARN_UNINITIALIZED))
1242 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
1244 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
1245 return !PerlIO_error(fp);
1249 if (PerlIO_isutf8(fp)) {
1251 sv_utf8_upgrade_flags(sv = sv_mortalcopy(sv),
1252 SV_GMAGIC|SV_UTF8_NO_ENCODING);
1254 else if (DO_UTF8(sv)) {
1255 if (!sv_utf8_downgrade((sv = sv_mortalcopy(sv)), TRUE)
1256 && ckWARN_d(WARN_UTF8))
1258 Perl_warner(aTHX_ packWARN(WARN_UTF8), "Wide character in print");
1261 tmps = SvPV_const(sv, len);
1264 /* To detect whether the process is about to overstep its
1265 * filesize limit we would need getrlimit(). We could then
1266 * also transparently raise the limit with setrlimit() --
1267 * but only until the system hard limit/the filesystem limit,
1268 * at which we would get EPERM. Note that when using buffered
1269 * io the write failure can be delayed until the flush/close. --jhi */
1270 if (len && (PerlIO_write(fp,tmps,len) == 0))
1272 return !PerlIO_error(fp);
1283 if (PL_op->op_flags & OPf_REF) {
1288 if (io && IoIFP(io)) {
1290 sv_setpvn(PL_statname,"", 0);
1291 PL_laststype = OP_STAT;
1292 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
1296 return PL_laststatval;
1297 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1298 report_evil_fh(gv, io, PL_op->op_type);
1300 sv_setpvn(PL_statname,"", 0);
1301 return (PL_laststatval = -1);
1304 else if (PL_op->op_private & OPpFT_STACKED) {
1305 return PL_laststatval;
1308 SV* const sv = POPs;
1312 if (SvTYPE(sv) == SVt_PVGV) {
1316 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
1321 s = SvPV_const(sv, len);
1323 sv_setpvn(PL_statname, s, len);
1324 s = SvPVX_const(PL_statname); /* s now NUL-terminated */
1325 PL_laststype = OP_STAT;
1326 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1327 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
1328 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
1329 return PL_laststatval;
1338 static const char no_prev_lstat[] = "The stat preceding -l _ wasn't an lstat";
1341 if (PL_op->op_flags & OPf_REF) {
1343 if (cGVOP_gv == PL_defgv) {
1344 if (PL_laststype != OP_LSTAT)
1345 Perl_croak(aTHX_ no_prev_lstat);
1346 return PL_laststatval;
1348 if (ckWARN(WARN_IO)) {
1349 Perl_warner(aTHX_ packWARN(WARN_IO), "Use of -l on filehandle %s",
1351 return (PL_laststatval = -1);
1354 else if (PL_laststype != OP_LSTAT
1355 && (PL_op->op_private & OPpFT_STACKED) && ckWARN(WARN_IO))
1356 Perl_croak(aTHX_ no_prev_lstat);
1358 PL_laststype = OP_LSTAT;
1362 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV && ckWARN(WARN_IO)) {
1363 Perl_warner(aTHX_ packWARN(WARN_IO), "Use of -l on filehandle %s",
1364 GvENAME((GV*) SvRV(sv)));
1365 return (PL_laststatval = -1);
1367 /* XXX Do really need to be calling SvPV() all these times? */
1368 sv_setpv(PL_statname,SvPV_nolen_const(sv));
1369 PL_laststatval = PerlLIO_lstat(SvPV_nolen_const(sv),&PL_statcache);
1370 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(sv), '\n'))
1371 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "lstat");
1372 return PL_laststatval;
1376 Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1377 int fd, int do_report)
1380 #if defined(MACOS_TRADITIONAL) || defined(__SYMBIAN32__)
1381 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1385 const char *tmps = NULL;
1386 Newx(PL_Argv, sp - mark + 1, char*);
1389 while (++mark <= sp) {
1391 *a++ = (char*)SvPV_nolen_const(*mark);
1397 tmps = SvPV_nolen_const(really);
1398 if ((!really && *PL_Argv[0] != '/') ||
1399 (really && *tmps != '/')) /* will execvp use PATH? */
1400 TAINT_ENV(); /* testing IFS here is overkill, probably */
1402 if (really && *tmps)
1403 PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
1405 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1407 if (ckWARN(WARN_EXEC))
1408 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
1409 (really ? tmps : PL_Argv[0]), Strerror(errno));
1411 const int e = errno;
1413 PerlLIO_write(fd, (void*)&e, sizeof(int));
1423 Perl_do_execfree(pTHX)
1427 PL_Argv = Null(char **);
1432 #ifdef PERL_DEFAULT_DO_EXEC3_IMPLEMENTATION
1435 Perl_do_exec3(pTHX_ const char *incmd, int fd, int do_report)
1442 /* Make a copy so we can change it */
1443 const int cmdlen = strlen(incmd);
1444 Newx(cmd, cmdlen+1, char);
1445 strncpy(cmd, incmd, cmdlen);
1448 while (*cmd && isSPACE(*cmd))
1451 /* save an extra exec if possible */
1455 char flags[PERL_FLAGS_MAX];
1456 if (strnEQ(cmd,PL_cshname,PL_cshlen) &&
1457 strnEQ(cmd+PL_cshlen," -c",3)) {
1459 strlcpy(flags, "-c", PERL_FLAGS_MAX);
1463 s = cmd+PL_cshlen+3;
1467 strlcat(flags, "f", PERL_FLAGS_MAX);
1475 char * const ncmd = s;
1481 if (s[-1] == '\'') {
1484 PerlProc_execl(PL_cshname,"csh", flags, ncmd, (char*)0);
1495 /* see if there are shell metacharacters in it */
1497 if (*cmd == '.' && isSPACE(cmd[1]))
1500 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1503 for (s = cmd; *s && isALNUM(*s); s++) ; /* catch VAR=val gizmo */
1507 for (s = cmd; *s; s++) {
1508 if (*s != ' ' && !isALPHA(*s) &&
1509 strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1510 if (*s == '\n' && !s[1]) {
1514 /* handle the 2>&1 construct at the end */
1515 if (*s == '>' && s[1] == '&' && s[2] == '1'
1516 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1517 && (!s[3] || isSPACE(s[3])))
1519 const char *t = s + 3;
1521 while (*t && isSPACE(*t))
1523 if (!*t && (PerlLIO_dup2(1,2) != -1)) {
1530 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
1537 Newx(PL_Argv, (s - cmd) / 2 + 2, char*);
1538 PL_Cmd = savepvn(cmd, s-cmd);
1540 for (s = PL_Cmd; *s;) {
1541 while (*s && isSPACE(*s)) s++;
1544 while (*s && !isSPACE(*s)) s++;
1551 PerlProc_execvp(PL_Argv[0],PL_Argv);
1553 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1557 if (ckWARN(WARN_EXEC))
1558 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
1559 PL_Argv[0], Strerror(errno));
1561 const int e = errno;
1562 PerlLIO_write(fd, (const void*)&e, sizeof(int));
1571 #endif /* OS2 || WIN32 */
1574 Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
1578 register I32 tot = 0;
1579 const char *const what = PL_op_name[type];
1581 SV ** const oldmark = mark;
1583 /* Doing this ahead of the switch statement preserves the old behaviour,
1584 where attempting to use kill as a taint test test would fail on
1585 platforms where kill was not defined. */
1587 if (type == OP_KILL)
1588 Perl_die(aTHX_ PL_no_func, what);
1591 if (type == OP_CHOWN)
1592 Perl_die(aTHX_ PL_no_func, what);
1596 #define APPLY_TAINT_PROPER() \
1598 if (PL_tainted) { TAINT_PROPER(what); } \
1601 /* This is a first heuristic; it doesn't catch tainting magic. */
1603 while (++mark <= sp) {
1604 if (SvTAINTED(*mark)) {
1613 APPLY_TAINT_PROPER();
1616 APPLY_TAINT_PROPER();
1618 while (++mark <= sp) {
1620 if (SvTYPE(*mark) == SVt_PVGV) {
1623 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1625 APPLY_TAINT_PROPER();
1626 if (fchmod(PerlIO_fileno(IoIFP(GvIOn(gv))), val))
1629 Perl_die(aTHX_ PL_no_func, "fchmod");
1636 else if (SvROK(*mark) && SvTYPE(SvRV(*mark)) == SVt_PVGV) {
1637 gv = (GV*)SvRV(*mark);
1641 const char *name = SvPV_nolen_const(*mark);
1642 APPLY_TAINT_PROPER();
1643 if (PerlLIO_chmod(name, val))
1651 APPLY_TAINT_PROPER();
1652 if (sp - mark > 2) {
1654 val = SvIVx(*++mark);
1655 val2 = SvIVx(*++mark);
1656 APPLY_TAINT_PROPER();
1658 while (++mark <= sp) {
1660 if (SvTYPE(*mark) == SVt_PVGV) {
1663 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1665 APPLY_TAINT_PROPER();
1666 if (fchown(PerlIO_fileno(IoIFP(GvIOn(gv))), val, val2))
1669 Perl_die(aTHX_ PL_no_func, "fchown");
1676 else if (SvROK(*mark) && SvTYPE(SvRV(*mark)) == SVt_PVGV) {
1677 gv = (GV*)SvRV(*mark);
1681 const char *name = SvPV_nolen_const(*mark);
1682 APPLY_TAINT_PROPER();
1683 if (PerlLIO_chown(name, val, val2))
1691 XXX Should we make lchown() directly available from perl?
1692 For now, we'll let Configure test for HAS_LCHOWN, but do
1693 nothing in the core.
1698 APPLY_TAINT_PROPER();
1701 s = SvPVx_nolen_const(*++mark);
1703 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1705 if ((val = whichsig(s)) < 0)
1706 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
1710 APPLY_TAINT_PROPER();
1713 /* kill() doesn't do process groups (job trees?) under VMS */
1714 if (val < 0) val = -val;
1715 if (val == SIGKILL) {
1716 # include <starlet.h>
1717 /* Use native sys$delprc() to insure that target process is
1718 * deleted; supervisor-mode images don't pay attention to
1719 * CRTL's emulation of Unix-style signals and kill()
1721 while (++mark <= sp) {
1722 I32 proc = SvIVx(*mark);
1723 register unsigned long int __vmssts;
1724 APPLY_TAINT_PROPER();
1725 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1729 case SS$_NOSUCHNODE:
1730 SETERRNO(ESRCH,__vmssts);
1733 SETERRNO(EPERM,__vmssts);
1736 SETERRNO(EVMSERR,__vmssts);
1745 while (++mark <= sp) {
1746 const I32 proc = SvIVx(*mark);
1747 APPLY_TAINT_PROPER();
1749 if (PerlProc_killpg(proc,val)) /* BSD */
1751 if (PerlProc_kill(-proc,val)) /* SYSV */
1757 while (++mark <= sp) {
1758 const I32 proc = SvIVx(*mark);
1759 APPLY_TAINT_PROPER();
1760 if (PerlProc_kill(proc, val))
1767 APPLY_TAINT_PROPER();
1769 while (++mark <= sp) {
1770 s = SvPV_nolen_const(*mark);
1771 APPLY_TAINT_PROPER();
1772 if (PL_euid || PL_unsafe) {
1776 else { /* don't let root wipe out directories without -U */
1777 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1786 #if defined(HAS_UTIME) || defined(HAS_FUTIMES)
1788 APPLY_TAINT_PROPER();
1789 if (sp - mark > 2) {
1790 #if defined(HAS_FUTIMES)
1791 struct timeval utbuf[2];
1792 void *utbufp = utbuf;
1793 #elif defined(I_UTIME) || defined(VMS)
1794 struct utimbuf utbuf;
1795 struct utimbuf *utbufp = &utbuf;
1801 void *utbufp = &utbuf;
1804 SV* const accessed = *++mark;
1805 SV* const modified = *++mark;
1807 /* Be like C, and if both times are undefined, let the C
1808 * library figure out what to do. This usually means
1809 * "current time". */
1811 if ( accessed == &PL_sv_undef && modified == &PL_sv_undef )
1814 Zero(&utbuf, sizeof utbuf, char);
1816 utbuf[0].tv_sec = (long)SvIVx(accessed); /* time accessed */
1817 utbuf[0].tv_usec = 0;
1818 utbuf[1].tv_sec = (long)SvIVx(modified); /* time modified */
1819 utbuf[1].tv_usec = 0;
1820 #elif defined(BIG_TIME)
1821 utbuf.actime = (Time_t)SvNVx(accessed); /* time accessed */
1822 utbuf.modtime = (Time_t)SvNVx(modified); /* time modified */
1824 utbuf.actime = (Time_t)SvIVx(accessed); /* time accessed */
1825 utbuf.modtime = (Time_t)SvIVx(modified); /* time modified */
1828 APPLY_TAINT_PROPER();
1830 while (++mark <= sp) {
1832 if (SvTYPE(*mark) == SVt_PVGV) {
1835 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1837 APPLY_TAINT_PROPER();
1838 if (futimes(PerlIO_fileno(IoIFP(GvIOn(gv))), utbufp))
1841 Perl_die(aTHX_ PL_no_func, "futimes");
1848 else if (SvROK(*mark) && SvTYPE(SvRV(*mark)) == SVt_PVGV) {
1849 gv = (GV*)SvRV(*mark);
1853 const char * const name = SvPV_nolen_const(*mark);
1854 APPLY_TAINT_PROPER();
1856 if (utimes(name, utbufp))
1858 if (PerlLIO_utime(name, utbufp))
1872 #undef APPLY_TAINT_PROPER
1875 /* Do the permissions allow some operation? Assumes statcache already set. */
1876 #ifndef VMS /* VMS' cando is in vms.c */
1878 Perl_cando(pTHX_ Mode_t mode, bool effective, register const Stat_t *statbufp)
1879 /* effective is a flag, true for EUID, or for checking if the effective gid
1880 * is in the list of groups returned from getgroups().
1885 /* [Comments and code from Len Reed]
1886 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1887 * to write-protected files. The execute permission bit is set
1888 * by the Miscrosoft C library stat() function for the following:
1893 * All files and directories are readable.
1894 * Directories and special files, e.g. "CON", cannot be
1896 * [Comment by Tom Dinger -- a directory can have the write-protect
1897 * bit set in the file system, but DOS permits changes to
1898 * the directory anyway. In addition, all bets are off
1899 * here for networked software, such as Novell and
1903 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1904 * too so it will actually look into the files for magic numbers
1906 return (mode & statbufp->st_mode) ? TRUE : FALSE;
1908 #else /* ! DOSISH */
1909 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
1910 if (mode == S_IXUSR) {
1911 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1915 return TRUE; /* root reads and writes anything */
1918 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1919 if (statbufp->st_mode & mode)
1920 return TRUE; /* ok as "user" */
1922 else if (ingroup(statbufp->st_gid,effective)) {
1923 if (statbufp->st_mode & mode >> 3)
1924 return TRUE; /* ok as "group" */
1926 else if (statbufp->st_mode & mode >> 6)
1927 return TRUE; /* ok as "other" */
1929 #endif /* ! DOSISH */
1934 Perl_ingroup(pTHX_ Gid_t testgid, bool effective)
1936 #ifdef MACOS_TRADITIONAL
1937 /* This is simply not correct for AppleShare, but fix it yerself. */
1941 if (testgid == (effective ? PL_egid : PL_gid))
1943 #ifdef HAS_GETGROUPS
1945 Groups_t *gary = NULL;
1949 anum = getgroups(0, gary);
1950 Newx(gary, anum, Groups_t);
1951 anum = getgroups(anum, gary);
1953 if (gary[anum] == testgid) {
1967 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1970 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
1973 const key_t key = (key_t)SvNVx(*++mark);
1974 const I32 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1975 const I32 flags = SvIVx(*++mark);
1983 return msgget(key, flags);
1987 return semget(key, n, flags);
1991 return shmget(key, n, flags);
1993 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1995 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1998 return -1; /* should never happen */
2002 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
2007 const I32 id = SvIVx(*++mark);
2008 const I32 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
2009 const I32 cmd = SvIVx(*++mark);
2010 SV * const astr = *++mark;
2011 STRLEN infosize = 0;
2012 I32 getinfo = (cmd == IPC_STAT);
2014 PERL_UNUSED_ARG(sp);
2020 if (cmd == IPC_STAT || cmd == IPC_SET)
2021 infosize = sizeof(struct msqid_ds);
2026 if (cmd == IPC_STAT || cmd == IPC_SET)
2027 infosize = sizeof(struct shmid_ds);
2033 if (cmd == IPC_STAT || cmd == IPC_SET)
2034 infosize = sizeof(struct semid_ds);
2035 else if (cmd == GETALL || cmd == SETALL)
2037 struct semid_ds semds;
2039 #ifdef EXTRA_F_IN_SEMUN_BUF
2040 semun.buff = &semds;
2044 getinfo = (cmd == GETALL);
2045 if (Semctl(id, 0, IPC_STAT, semun) == -1)
2047 infosize = semds.sem_nsems * sizeof(short);
2048 /* "short" is technically wrong but much more portable
2049 than guessing about u_?short(_t)? */
2052 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2056 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
2058 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2066 SvPV_force_nolen(astr);
2067 a = SvGROW(astr, infosize+1);
2072 a = SvPV(astr, len);
2073 if (len != infosize)
2074 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
2082 const IV i = SvIV(astr);
2083 a = INT2PTR(char *,i); /* ouch */
2090 ret = msgctl(id, cmd, (struct msqid_ds *)a);
2096 union semun unsemds;
2098 #ifdef EXTRA_F_IN_SEMUN_BUF
2099 unsemds.buff = (struct semid_ds *)a;
2101 unsemds.buf = (struct semid_ds *)a;
2103 ret = Semctl(id, n, cmd, unsemds);
2105 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2112 ret = shmctl(id, cmd, (struct shmid_ds *)a);
2116 if (getinfo && ret >= 0) {
2117 SvCUR_set(astr, infosize);
2118 *SvEND(astr) = '\0';
2125 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
2130 const I32 id = SvIVx(*++mark);
2131 SV * const mstr = *++mark;
2132 const I32 flags = SvIVx(*++mark);
2133 const char * const mbuf = SvPV_const(mstr, len);
2134 const I32 msize = len - sizeof(long);
2136 PERL_UNUSED_ARG(sp);
2139 Perl_croak(aTHX_ "Arg too short for msgsnd");
2141 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
2143 Perl_croak(aTHX_ "msgsnd not implemented");
2148 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
2154 I32 msize, flags, ret;
2155 const I32 id = SvIVx(*++mark);
2156 SV * const mstr = *++mark;
2157 PERL_UNUSED_ARG(sp);
2159 /* suppress warning when reading into undef var --jhi */
2161 sv_setpvn(mstr, "", 0);
2162 msize = SvIVx(*++mark);
2163 mtype = (long)SvIVx(*++mark);
2164 flags = SvIVx(*++mark);
2165 SvPV_force_nolen(mstr);
2166 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
2169 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
2171 SvCUR_set(mstr, sizeof(long)+ret);
2172 *SvEND(mstr) = '\0';
2173 #ifndef INCOMPLETE_TAINTS
2174 /* who knows who has been playing with this message? */
2180 Perl_croak(aTHX_ "msgrcv not implemented");
2185 Perl_do_semop(pTHX_ SV **mark, SV **sp)
2190 const I32 id = SvIVx(*++mark);
2191 SV * const opstr = *++mark;
2192 const char * const opbuf = SvPV_const(opstr, opsize);
2193 PERL_UNUSED_ARG(sp);
2195 if (opsize < 3 * SHORTSIZE
2196 || (opsize % (3 * SHORTSIZE))) {
2197 SETERRNO(EINVAL,LIB_INVARG);
2201 /* We can't assume that sizeof(struct sembuf) == 3 * sizeof(short). */
2203 const int nsops = opsize / (3 * sizeof (short));
2205 short * const ops = (short *) opbuf;
2207 struct sembuf *temps, *t;
2210 Newx (temps, nsops, struct sembuf);
2218 result = semop(id, temps, nsops);
2232 Perl_croak(aTHX_ "semop not implemented");
2237 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
2242 struct shmid_ds shmds;
2243 const I32 id = SvIVx(*++mark);
2244 SV * const mstr = *++mark;
2245 const I32 mpos = SvIVx(*++mark);
2246 const I32 msize = SvIVx(*++mark);
2247 PERL_UNUSED_ARG(sp);
2250 if (shmctl(id, IPC_STAT, &shmds) == -1)
2252 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
2253 SETERRNO(EFAULT,SS_ACCVIO); /* can't do as caller requested */
2256 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
2257 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2259 if (optype == OP_SHMREAD) {
2261 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2263 sv_setpvn(mstr, "", 0);
2264 SvPV_force_nolen(mstr);
2265 mbuf = SvGROW(mstr, msize+1);
2267 Copy(shm + mpos, mbuf, msize, char);
2268 SvCUR_set(mstr, msize);
2269 *SvEND(mstr) = '\0';
2271 #ifndef INCOMPLETE_TAINTS
2272 /* who knows who has been playing with this shared memory? */
2280 const char *mbuf = SvPV_const(mstr, len);
2281 if ((n = len) > msize)
2283 Copy(mbuf, shm + mpos, n, char);
2285 memzero(shm + mpos + n, msize - n);
2289 Perl_croak(aTHX_ "shm I/O not implemented");
2293 #endif /* SYSV IPC */
2298 =for apidoc start_glob
2300 Function called by C<do_readline> to spawn a glob (or do the glob inside
2301 perl on VMS). This code used to be inline, but now perl uses C<File::Glob>
2302 this glob starter is only used by miniperl during the build process.
2303 Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up.
2309 Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
2312 SV * const tmpcmd = newSV(0);
2316 #ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
2317 /* since spawning off a process is a real performance hit */
2319 #include <descrip.h>
2320 #include <lib$routines.h>
2323 char rslt[NAM$C_MAXRSS+1+sizeof(unsigned short int)] = {'\0','\0'};
2324 char vmsspec[NAM$C_MAXRSS+1];
2325 char * const rstr = rslt + sizeof(unsigned short int);
2326 char *begin, *end, *cp;
2327 $DESCRIPTOR(dfltdsc,"SYS$DISK:[]*.*;");
2330 struct dsc$descriptor_s wilddsc
2331 = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
2332 struct dsc$descriptor_vs rsdsc
2333 = {sizeof rslt, DSC$K_DTYPE_VT, DSC$K_CLASS_VS, rslt};
2334 unsigned long int cxt = 0, sts = 0, ok = 1, hasdir = 0, hasver = 0, isunix = 0;
2336 /* We could find out if there's an explicit dev/dir or version
2337 by peeking into lib$find_file's internal context at
2338 ((struct NAM *)((struct FAB *)cxt)->fab$l_nam)->nam$l_fnb
2339 but that's unsupported, so I don't want to do it now and
2340 have it bite someone in the future. */
2341 cp = SvPV(tmpglob,i);
2343 if (cp[i] == ';') hasver = 1;
2345 if (sts) hasver = 1;
2349 hasdir = isunix = 1;
2352 if (cp[i] == ']' || cp[i] == '>' || cp[i] == ':') {
2357 if ((tmpfp = PerlIO_tmpfile()) != NULL) {
2359 if (!PerlLIO_stat(SvPVX_const(tmpglob),&st) && S_ISDIR(st.st_mode))
2360 ok = ((wilddsc.dsc$a_pointer = tovmspath(SvPVX(tmpglob),vmsspec)) != NULL);
2361 else ok = ((wilddsc.dsc$a_pointer = tovmsspec(SvPVX(tmpglob),vmsspec)) != NULL);
2362 if (ok) wilddsc.dsc$w_length = (unsigned short int) strlen(wilddsc.dsc$a_pointer);
2363 for (cp=wilddsc.dsc$a_pointer; ok && cp && *cp; cp++)
2364 if (*cp == '?') *cp = '%'; /* VMS style single-char wildcard */
2365 while (ok && ((sts = lib$find_file(&wilddsc,&rsdsc,&cxt,
2366 &dfltdsc,NULL,NULL,NULL))&1)) {
2367 /* with varying string, 1st word of buffer contains result length */
2368 end = rstr + *((unsigned short int*)rslt);
2369 if (!hasver) while (*end != ';' && end > rstr) end--;
2370 *(end++) = '\n'; *end = '\0';
2371 for (cp = rstr; *cp; cp++) *cp = _tolower(*cp);
2373 if (isunix) trim_unixpath(rstr,SvPVX(tmpglob),1);
2378 while (*(--begin) != ']' && *begin != '>') ;
2381 ok = (PerlIO_puts(tmpfp,begin) != EOF);
2383 if (cxt) (void)lib$find_file_end(&cxt);
2384 if (ok && sts != RMS$_NMF &&
2385 sts != RMS$_DNF && sts != RMS_FNF) ok = 0;
2388 SETERRNO((sts == RMS$_SYN ? EINVAL : EVMSERR),sts);
2390 PerlIO_close(tmpfp);
2394 PerlIO_rewind(tmpfp);
2395 IoTYPE(io) = IoTYPE_RDONLY;
2396 IoIFP(io) = fp = tmpfp;
2397 IoFLAGS(io) &= ~IOf_UNTAINT; /* maybe redundant */
2402 #ifdef MACOS_TRADITIONAL
2403 sv_setpv(tmpcmd, "glob ");
2404 sv_catsv(tmpcmd, tmpglob);
2405 sv_catpv(tmpcmd, " |");
2409 sv_setpv(tmpcmd, "for a in ");
2410 sv_catsv(tmpcmd, tmpglob);
2411 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
2414 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
2415 sv_catsv(tmpcmd, tmpglob);
2417 sv_setpv(tmpcmd, "perlglob ");
2418 sv_catsv(tmpcmd, tmpglob);
2419 sv_catpv(tmpcmd, " |");
2424 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
2425 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
2426 sv_catsv(tmpcmd, tmpglob);
2427 sv_catpv(tmpcmd, "' 2>/dev/null |");
2429 sv_setpv(tmpcmd, "echo ");
2430 sv_catsv(tmpcmd, tmpglob);
2432 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
2434 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
2437 #endif /* !DOSISH */
2438 #endif /* MACOS_TRADITIONAL */
2439 (void)do_open(PL_last_in_gv, (char*)SvPVX_const(tmpcmd), SvCUR(tmpcmd),
2440 FALSE, O_RDONLY, 0, Nullfp);
2449 * c-indentation-style: bsd
2451 * indent-tabs-mode: t
2454 * ex: set ts=8 sts=4 sw=4 noet: