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 = NULL;
69 PerlIO *saveofp = NULL;
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]; /* file mode ("r\0", "rb\0", "ab\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) = NULL;
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 while (isSPACE(*type))
200 while (tend > type && isSPACE(tend[-1]))
204 /* New style explicit name, type is just mode and layer info */
206 if (SvROK(*svp) && !strchr(oname,'&')) {
208 Perl_warner(aTHX_ packWARN(WARN_IO),
209 "Can't open a reference");
210 SETERRNO(EINVAL, LIB_INVARG);
213 #endif /* USE_STDIO */
214 name = SvOK(*svp) ? savesvpv (*svp) : savepvn ("", 0);
222 if ((*type == IoTYPE_RDWR) && /* scary */
223 (*(type+1) == IoTYPE_RDONLY || *(type+1) == IoTYPE_WRONLY) &&
224 ((!num_svs || (tend > type+1 && tend[-1] != IoTYPE_PIPE)))) {
225 TAINT_PROPER("open");
230 if (*type == IoTYPE_PIPE) {
232 if (type[1] != IoTYPE_STD) {
234 Perl_croak(aTHX_ "Unknown open() mode '%.*s'", (int)olen, oname);
240 } while (isSPACE(*type));
246 /* command is missing 19990114 */
247 if (ckWARN(WARN_PIPE))
248 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open");
252 if (!(*name == '-' && name[1] == '\0') || num_svs)
254 TAINT_PROPER("piped open");
255 if (!num_svs && name[len-1] == '|') {
257 if (ckWARN(WARN_PIPE))
258 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Can't open bidirectional pipe");
264 strlcat(mode, "b", PERL_MODE_MAX - 1);
266 strlcat(mode, "t", PERL_MODE_MAX - 1);
274 fp = PerlProc_popen_list(mode, num_svs, svp);
277 fp = PerlProc_popen(name,mode);
281 if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) {
287 else if (*type == IoTYPE_WRONLY) {
288 TAINT_PROPER("open");
290 if (*type == IoTYPE_WRONLY) {
291 /* Two IoTYPE_WRONLYs in a row make for an IoTYPE_APPEND. */
292 mode[0] = IoTYPE(io) = IoTYPE_APPEND;
302 strlcat(mode, "b", PERL_MODE_MAX - 1);
304 strlcat(mode, "t", PERL_MODE_MAX - 1);
313 dodup = PERLIO_DUP_FD;
319 if (!num_svs && !*type && supplied_fp) {
320 /* "<+&" etc. is used by typemaps */
325 Perl_croak(aTHX_ "More than one argument to '%c&' open",IoTYPE(io));
327 while (isSPACE(*type))
329 if (num_svs && (SvIOK(*svp) || (SvPOK(*svp) && looks_like_number(*svp)))) {
333 else if (isDIGIT(*type)) {
339 thatio = sv_2io(*svp);
343 thatgv = gv_fetchpvn_flags(type, tend - type,
345 thatio = GvIO(thatgv);
349 SETERRNO(EINVAL,SS_IVCHAN);
353 if ((that_fp = IoIFP(thatio))) {
354 /* Flush stdio buffer before dup. --mjd
355 * Unfortunately SEEK_CURing 0 seems to
356 * be optimized away on most platforms;
357 * only Solaris and Linux seem to flush
360 /* sfio fails to clear error on next
361 sfwrite, contrary to documentation.
363 if (PerlIO_seek(that_fp, 0, SEEK_CUR) == -1)
364 PerlIO_clearerr(that_fp);
366 /* On the other hand, do all platforms
367 * take gracefully to flushing a read-only
368 * filehandle? Perhaps we should do
369 * fsetpos(src)+fgetpos(dst)? --nik */
370 PerlIO_flush(that_fp);
371 fd = PerlIO_fileno(that_fp);
372 /* When dup()ing STDIN, STDOUT or STDERR
373 * explicitly set appropriate access mode */
374 if (that_fp == PerlIO_stdout()
375 || that_fp == PerlIO_stderr())
376 IoTYPE(io) = IoTYPE_WRONLY;
377 else if (that_fp == PerlIO_stdin())
378 IoTYPE(io) = IoTYPE_RDONLY;
379 /* When dup()ing a socket, say result is
381 else if (IoTYPE(thatio) == IoTYPE_SOCKET)
382 IoTYPE(io) = IoTYPE_SOCKET;
390 fp = PerlIO_fdupopen(aTHX_ that_fp, NULL, dodup);
394 fd = PerlLIO_dup(fd);
397 if (!(fp = PerlIO_openn(aTHX_ type,mode,fd,0,0,NULL,num_svs,svp))) {
398 if (dodup && fd >= 0)
405 while (isSPACE(*type))
407 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
409 fp = PerlIO_stdout();
410 IoTYPE(io) = IoTYPE_STD;
412 Perl_croak(aTHX_ "More than one argument to '>%c' open",IoTYPE_STD);
417 namesv = sv_2mortal(newSVpvn(type,tend - type));
422 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
425 if (!fp && type && *type && *type != ':' && !isIDFIRST(*type))
426 goto unknown_open_mode;
427 } /* IoTYPE_WRONLY */
428 else if (*type == IoTYPE_RDONLY) {
431 } while (isSPACE(*type));
435 strlcat(mode, "b", PERL_MODE_MAX - 1);
437 strlcat(mode, "t", PERL_MODE_MAX - 1);
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,tend - 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 - 1);
498 strlcat(mode, "t", PERL_MODE_MAX - 1);
507 fp = PerlProc_popen_list(mode,num_svs,svp);
510 fp = PerlProc_popen(name,mode);
512 IoTYPE(io) = IoTYPE_PIPE;
514 while (isSPACE(*type))
517 if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) {
523 else { /* layer(Args) */
525 goto unknown_open_mode;
527 IoTYPE(io) = IoTYPE_RDONLY;
528 for (; isSPACE(*name); name++)
534 strlcat(mode, "b", PERL_MODE_MAX - 1);
536 strlcat(mode, "t", PERL_MODE_MAX - 1);
544 if (*name == '-' && name[1] == '\0') {
546 IoTYPE(io) = IoTYPE_STD;
550 namesv = sv_2mortal(newSVpvn(type,tend - type));
555 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
560 if (IoTYPE(io) == IoTYPE_RDONLY && ckWARN(WARN_NEWLINE)
561 && strchr(oname, '\n')
564 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
568 if (ckWARN(WARN_IO)) {
569 if ((IoTYPE(io) == IoTYPE_RDONLY) &&
570 (fp == PerlIO_stdout() || fp == PerlIO_stderr())) {
571 Perl_warner(aTHX_ packWARN(WARN_IO),
572 "Filehandle STD%s reopened as %s only for input",
573 ((fp == PerlIO_stdout()) ? "OUT" : "ERR"),
576 else if ((IoTYPE(io) == IoTYPE_WRONLY) && fp == PerlIO_stdin()) {
577 Perl_warner(aTHX_ packWARN(WARN_IO),
578 "Filehandle STDIN reopened as %s only for output",
583 fd = PerlIO_fileno(fp);
584 /* If there is no fd (e.g. PerlIO::scalar) assume it isn't a
585 * socket - this covers PerlIO::scalar - otherwise unless we "know" the
586 * type probe for socket-ness.
588 if (IoTYPE(io) && IoTYPE(io) != IoTYPE_PIPE && IoTYPE(io) != IoTYPE_STD && fd >= 0) {
589 if (PerlLIO_fstat(fd,&PL_statbuf) < 0) {
590 /* If PerlIO claims to have fd we had better be able to fstat() it. */
591 (void) PerlIO_close(fp);
595 if (S_ISSOCK(PL_statbuf.st_mode))
596 IoTYPE(io) = IoTYPE_SOCKET; /* in case a socket was passed in to us */
600 !(PL_statbuf.st_mode & S_IFMT)
604 && IoTYPE(io) != IoTYPE_WRONLY /* Dups of STD* filehandles already have */
605 && IoTYPE(io) != IoTYPE_RDONLY /* type so they aren't marked as sockets */
606 ) { /* on OS's that return 0 on fstat()ed pipe */
608 Sock_size_t buflen = sizeof tmpbuf;
609 if (PerlSock_getsockname(fd, (struct sockaddr *)tmpbuf, &buflen) >= 0
610 || errno != ENOTSOCK)
611 IoTYPE(io) = IoTYPE_SOCKET; /* some OS's return 0 on fstat()ed socket */
612 /* but some return 0 for streams too, sigh */
614 #endif /* HAS_SOCKET */
615 #endif /* !PERL_MICRO */
619 * If this is a standard handle we discard all the layer stuff
620 * and just dup the fd into whatever was on the handle before !
623 if (saveifp) { /* must use old fp? */
624 /* If fd is less that PL_maxsysfd i.e. STDIN..STDERR
625 then dup the new fileno down
628 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
629 if (saveofp != saveifp) { /* was a socket? */
630 PerlIO_close(saveofp);
634 /* Still a small can-of-worms here if (say) PerlIO::scalar
635 is assigned to (say) STDOUT - for now let dup2() fail
636 and provide the error
638 if (PerlLIO_dup2(fd, savefd) < 0) {
639 (void)PerlIO_close(fp);
643 if (savefd != PerlIO_fileno(PerlIO_stdin())) {
644 char newname[FILENAME_MAX+1];
645 if (PerlIO_getname(fp, newname)) {
646 if (fd == PerlIO_fileno(PerlIO_stdout()))
647 Perl_vmssetuserlnm(aTHX_ "SYS$OUTPUT", newname);
648 if (fd == PerlIO_fileno(PerlIO_stderr()))
649 Perl_vmssetuserlnm(aTHX_ "SYS$ERROR", newname);
655 /* PL_fdpid isn't used on Windows, so avoid this useless work.
656 * XXX Probably the same for a lot of other places. */
662 sv = *av_fetch(PL_fdpid,fd,TRUE);
663 SvUPGRADE(sv, SVt_IV);
666 sv = *av_fetch(PL_fdpid,savefd,TRUE);
667 SvUPGRADE(sv, SVt_IV);
674 /* need to close fp without closing underlying fd */
675 int ofd = PerlIO_fileno(fp);
676 int dupfd = PerlLIO_dup(ofd);
677 #if defined(HAS_FCNTL) && defined(F_SETFD)
678 /* Assume if we have F_SETFD we have F_GETFD */
679 int coe = fcntl(ofd,F_GETFD);
682 PerlLIO_dup2(dupfd,ofd);
683 #if defined(HAS_FCNTL) && defined(F_SETFD)
684 /* The dup trick has lost close-on-exec on ofd */
685 fcntl(ofd,F_SETFD, coe);
687 PerlLIO_close(dupfd);
694 fd = PerlIO_fileno(fp);
696 #if defined(HAS_FCNTL) && defined(F_SETFD)
698 const int save_errno = errno;
699 fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
705 IoFLAGS(io) &= ~IOf_NOLINE;
707 if (IoTYPE(io) == IoTYPE_SOCKET
708 || (IoTYPE(io) == IoTYPE_WRONLY && fd >= 0 && S_ISCHR(PL_statbuf.st_mode)) ) {
710 if (*s == IoTYPE_IMPLICIT || *s == IoTYPE_NUMERIC)
713 if (!(IoOFP(io) = PerlIO_openn(aTHX_ type,s,fd,0,0,NULL,0,svp))) {
727 IoTYPE(io) = savetype;
732 Perl_nextargv(pTHX_ register GV *gv)
736 #ifndef FLEXFILENAMES
742 IO * const io = GvIOp(gv);
745 PL_argvoutgv = gv_fetchpvs("ARGVOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
746 if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
747 IoFLAGS(io) &= ~IOf_START;
749 if (!PL_argvout_stack)
750 PL_argvout_stack = newAV();
752 av_push(PL_argvout_stack, SvREFCNT_inc_simple_NN(PL_defoutgv));
755 if (PL_filemode & (S_ISUID|S_ISGID)) {
756 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
759 (void)fchmod(PL_lastfd,PL_filemode);
761 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
768 while (av_len(GvAV(gv)) >= 0) {
770 sv = av_shift(GvAV(gv));
772 sv_setsv(GvSVn(gv),sv);
773 SvSETMAGIC(GvSV(gv));
774 PL_oldname = SvPVx(GvSV(gv), oldlen);
775 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,NULL)) {
777 TAINT_PROPER("inplace open");
778 if (oldlen == 1 && *PL_oldname == '-') {
779 setdefout(gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL,
781 return IoIFP(GvIOp(gv));
783 #ifndef FLEXFILENAMES
784 filedev = PL_statbuf.st_dev;
785 fileino = PL_statbuf.st_ino;
787 PL_filemode = PL_statbuf.st_mode;
788 fileuid = PL_statbuf.st_uid;
789 filegid = PL_statbuf.st_gid;
790 if (!S_ISREG(PL_filemode)) {
791 if (ckWARN_d(WARN_INPLACE))
792 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
793 "Can't do inplace edit: %s is not a regular file",
799 const char *star = strchr(PL_inplace, '*');
801 const char *begin = PL_inplace;
802 sv_setpvn(sv, "", 0);
804 sv_catpvn(sv, begin, star - begin);
805 sv_catpvn(sv, PL_oldname, oldlen);
807 } while ((star = strchr(begin, '*')));
812 sv_catpv(sv,PL_inplace);
814 #ifndef FLEXFILENAMES
815 if ((PerlLIO_stat(SvPVX_const(sv),&PL_statbuf) >= 0
816 && PL_statbuf.st_dev == filedev
817 && PL_statbuf.st_ino == fileino)
819 || ((_djstat_fail_bits & _STFAIL_TRUENAME)!=0)
823 if (ckWARN_d(WARN_INPLACE))
824 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
825 "Can't do inplace edit: %"SVf" would not be unique",
832 #if !defined(DOSISH) && !defined(__CYGWIN__) && !defined(EPOC)
833 if (PerlLIO_rename(PL_oldname,SvPVX_const(sv)) < 0) {
834 if (ckWARN_d(WARN_INPLACE))
835 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
836 "Can't rename %s to %"SVf": %s, skipping file",
837 PL_oldname, (void*)sv, Strerror(errno));
843 (void)PerlLIO_unlink(SvPVX_const(sv));
844 (void)PerlLIO_rename(PL_oldname,SvPVX_const(sv));
845 do_open(gv,(char*)SvPVX_const(sv),SvCUR(sv),PL_inplace!=0,
849 (void)UNLINK(SvPVX_const(sv));
850 if (link(PL_oldname,SvPVX_const(sv)) < 0) {
851 if (ckWARN_d(WARN_INPLACE))
852 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
853 "Can't rename %s to %"SVf": %s, skipping file",
854 PL_oldname, sv, Strerror(errno) );
858 (void)UNLINK(PL_oldname);
862 #if !defined(DOSISH) && !defined(AMIGAOS)
863 # ifndef VMS /* Don't delete; use automatic file versioning */
864 if (UNLINK(PL_oldname) < 0) {
865 if (ckWARN_d(WARN_INPLACE))
866 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
867 "Can't remove %s: %s, skipping file",
868 PL_oldname, Strerror(errno) );
874 Perl_croak(aTHX_ "Can't do inplace edit without backup");
878 sv_setpvn(sv,">",!PL_inplace);
879 sv_catpvn(sv,PL_oldname,oldlen);
880 SETERRNO(0,0); /* in case sprintf set errno */
882 if (!do_open(PL_argvoutgv,(char*)SvPVX_const(sv),SvCUR(sv),
883 PL_inplace!=0,O_WRONLY|O_CREAT|O_TRUNC,0,NULL))
885 if (!do_open(PL_argvoutgv,(char*)SvPVX_const(sv),SvCUR(sv),
886 PL_inplace!=0,O_WRONLY|O_CREAT|OPEN_EXCL,0666,
890 if (ckWARN_d(WARN_INPLACE))
891 Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't do inplace edit on %s: %s",
892 PL_oldname, Strerror(errno) );
896 setdefout(PL_argvoutgv);
897 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
898 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
900 (void)fchmod(PL_lastfd,PL_filemode);
902 # if !(defined(WIN32) && defined(__BORLANDC__))
903 /* Borland runtime creates a readonly file! */
904 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
907 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
909 (void)fchown(PL_lastfd,fileuid,filegid);
912 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
917 return IoIFP(GvIOp(gv));
920 if (ckWARN_d(WARN_INPLACE)) {
921 const int eno = errno;
922 if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0
923 && !S_ISREG(PL_statbuf.st_mode))
925 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
926 "Can't do inplace edit: %s is not a regular file",
930 Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't open %s: %s",
931 PL_oldname, Strerror(eno));
935 if (io && (IoFLAGS(io) & IOf_ARGV))
936 IoFLAGS(io) |= IOf_START;
938 (void)do_close(PL_argvoutgv,FALSE);
939 if (io && (IoFLAGS(io) & IOf_ARGV)
940 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
942 GV *oldout = (GV*)av_pop(PL_argvout_stack);
944 SvREFCNT_dec(oldout);
947 setdefout(gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO));
952 /* explicit renamed to avoid C++ conflict -- kja */
954 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
962 if (!gv || SvTYPE(gv) != SVt_PVGV) {
964 SETERRNO(EBADF,SS_IVCHAN);
968 if (!io) { /* never opened */
970 if (ckWARN(WARN_UNOPENED)) /* no check for closed here */
971 report_evil_fh(gv, io, PL_op->op_type);
972 SETERRNO(EBADF,SS_IVCHAN);
976 retval = io_close(io, not_implicit);
980 IoLINES_LEFT(io) = IoPAGE_LEN(io);
982 IoTYPE(io) = IoTYPE_CLOSED;
987 Perl_io_close(pTHX_ IO *io, bool not_implicit)
993 if (IoTYPE(io) == IoTYPE_PIPE) {
994 const int status = PerlProc_pclose(IoIFP(io));
996 STATUS_NATIVE_CHILD_SET(status);
997 retval = (STATUS_UNIX == 0);
1000 retval = (status != -1);
1003 else if (IoTYPE(io) == IoTYPE_STD)
1006 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
1007 const bool prev_err = PerlIO_error(IoOFP(io));
1008 retval = (PerlIO_close(IoOFP(io)) != EOF && !prev_err);
1009 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
1012 const bool prev_err = PerlIO_error(IoIFP(io));
1013 retval = (PerlIO_close(IoIFP(io)) != EOF && !prev_err);
1016 IoOFP(io) = IoIFP(io) = NULL;
1018 else if (not_implicit) {
1019 SETERRNO(EBADF,SS_IVCHAN);
1026 Perl_do_eof(pTHX_ GV *gv)
1029 register IO * const io = GvIO(gv);
1033 else if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
1034 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1037 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
1038 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
1039 return FALSE; /* this is the most usual case */
1043 /* getc and ungetc can stomp on errno */
1044 const int saverrno = errno;
1045 const int ch = PerlIO_getc(IoIFP(io));
1047 (void)PerlIO_ungetc(IoIFP(io),ch);
1054 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
1055 if (PerlIO_get_cnt(IoIFP(io)) < -1)
1056 PerlIO_set_cnt(IoIFP(io),-1);
1058 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
1059 if (gv != PL_argvgv || !nextargv(gv)) /* get another fp handy */
1063 return TRUE; /* normal fp, definitely end of file */
1069 Perl_do_tell(pTHX_ GV *gv)
1072 register IO *io = NULL;
1073 register PerlIO *fp;
1075 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1076 #ifdef ULTRIX_STDIO_BOTCH
1078 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1080 return PerlIO_tell(fp);
1082 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1083 report_evil_fh(gv, io, PL_op->op_type);
1084 SETERRNO(EBADF,RMS_IFI);
1089 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
1092 register IO *io = NULL;
1093 register PerlIO *fp;
1095 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1096 #ifdef ULTRIX_STDIO_BOTCH
1098 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1100 return PerlIO_seek(fp, pos, whence) >= 0;
1102 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1103 report_evil_fh(gv, io, PL_op->op_type);
1104 SETERRNO(EBADF,RMS_IFI);
1109 Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
1112 register IO *io = NULL;
1113 register PerlIO *fp;
1115 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
1116 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
1117 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1118 report_evil_fh(gv, io, PL_op->op_type);
1119 SETERRNO(EBADF,RMS_IFI);
1124 Perl_mode_from_discipline(pTHX_ SV *discp)
1126 int mode = O_BINARY;
1129 const char *s = SvPV_const(discp,len);
1134 if (s[2] == 'a' && s[3] == 'w'
1135 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1144 if (s[2] == 'r' && s[3] == 'l' && s[4] == 'f'
1145 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1154 goto fail_discipline;
1157 else if (isSPACE(*s)) {
1164 end = strchr(s+1, ':');
1167 #ifndef PERLIO_LAYERS
1168 Perl_croak(aTHX_ "IO layers (like '%.*s') unavailable", end-s, s);
1179 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE)
1181 my_chsize(int fd, Off_t length)
1184 /* code courtesy of William Kucharski */
1190 if (PerlLIO_fstat(fd, &filebuf) < 0)
1193 if (filebuf.st_size < length) {
1195 /* extend file length */
1197 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
1200 /* write a "0" byte */
1202 if ((PerlLIO_write(fd, "", 1)) != 1)
1206 /* truncate length */
1210 fl.l_start = length;
1211 fl.l_type = F_WRLCK; /* write lock on file space */
1214 * This relies on the UNDOCUMENTED F_FREESP argument to
1215 * fcntl(2), which truncates the file so that it ends at the
1216 * position indicated by fl.l_start.
1218 * Will minor miracles never cease?
1221 if (fcntl(fd, F_FREESP, &fl) < 0)
1227 Perl_croak_nocontext("truncate not implemented");
1228 #endif /* F_FREESP */
1231 #endif /* !HAS_TRUNCATE && !HAS_CHSIZE */
1234 Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
1237 register const char *tmps;
1242 /* assuming fp is checked earlier */
1245 switch (SvTYPE(sv)) {
1247 if (ckWARN(WARN_UNINITIALIZED))
1252 assert(!SvGMAGICAL(sv));
1254 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
1256 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
1257 return !PerlIO_error(fp);
1261 /* Do this first to trigger any overloading. */
1262 tmps = SvPV_const(sv, len);
1263 if (PerlIO_isutf8(fp)) {
1265 /* We don't modify the original scalar. */
1266 tmpbuf = bytes_to_utf8((const U8*) tmps, &len);
1267 tmps = (char *) tmpbuf;
1270 else if (DO_UTF8(sv)) {
1271 STRLEN tmplen = len;
1273 U8 *result = bytes_from_utf8((const U8*) tmps, &tmplen, &utf8);
1276 tmps = (char *) tmpbuf;
1280 assert((char *)result == tmps);
1281 if (ckWARN_d(WARN_UTF8)) {
1282 Perl_warner(aTHX_ packWARN(WARN_UTF8),
1283 "Wide character in print");
1289 /* To detect whether the process is about to overstep its
1290 * filesize limit we would need getrlimit(). We could then
1291 * also transparently raise the limit with setrlimit() --
1292 * but only until the system hard limit/the filesystem limit,
1293 * at which we would get EPERM. Note that when using buffered
1294 * io the write failure can be delayed until the flush/close. --jhi */
1295 if (len && (PerlIO_write(fp,tmps,len) == 0))
1299 return happy ? !PerlIO_error(fp) : FALSE;
1310 if (PL_op->op_flags & OPf_REF) {
1315 if (io && IoIFP(io)) {
1317 sv_setpvn(PL_statname,"", 0);
1318 PL_laststype = OP_STAT;
1319 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
1323 return PL_laststatval;
1324 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1325 report_evil_fh(gv, io, PL_op->op_type);
1327 sv_setpvn(PL_statname,"", 0);
1328 return (PL_laststatval = -1);
1331 else if (PL_op->op_private & OPpFT_STACKED) {
1332 return PL_laststatval;
1335 SV* const sv = POPs;
1339 if (SvTYPE(sv) == SVt_PVGV) {
1343 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
1348 s = SvPV_const(sv, len);
1350 sv_setpvn(PL_statname, s, len);
1351 s = SvPVX_const(PL_statname); /* s now NUL-terminated */
1352 PL_laststype = OP_STAT;
1353 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1354 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
1355 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
1356 return PL_laststatval;
1365 static const char no_prev_lstat[] = "The stat preceding -l _ wasn't an lstat";
1368 if (PL_op->op_flags & OPf_REF) {
1370 if (cGVOP_gv == PL_defgv) {
1371 if (PL_laststype != OP_LSTAT)
1372 Perl_croak(aTHX_ no_prev_lstat);
1373 return PL_laststatval;
1375 if (ckWARN(WARN_IO)) {
1376 Perl_warner(aTHX_ packWARN(WARN_IO), "Use of -l on filehandle %s",
1378 return (PL_laststatval = -1);
1381 else if (PL_laststype != OP_LSTAT
1382 && (PL_op->op_private & OPpFT_STACKED) && ckWARN(WARN_IO))
1383 Perl_croak(aTHX_ no_prev_lstat);
1385 PL_laststype = OP_LSTAT;
1389 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV && ckWARN(WARN_IO)) {
1390 Perl_warner(aTHX_ packWARN(WARN_IO), "Use of -l on filehandle %s",
1391 GvENAME((GV*) SvRV(sv)));
1392 return (PL_laststatval = -1);
1394 /* XXX Do really need to be calling SvPV() all these times? */
1395 sv_setpv(PL_statname,SvPV_nolen_const(sv));
1396 PL_laststatval = PerlLIO_lstat(SvPV_nolen_const(sv),&PL_statcache);
1397 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(sv), '\n'))
1398 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "lstat");
1399 return PL_laststatval;
1403 Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1404 int fd, int do_report)
1407 #if defined(MACOS_TRADITIONAL) || defined(__SYMBIAN32__)
1408 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1412 const char *tmps = NULL;
1413 Newx(PL_Argv, sp - mark + 1, char*);
1416 while (++mark <= sp) {
1418 *a++ = (char*)SvPV_nolen_const(*mark);
1424 tmps = SvPV_nolen_const(really);
1425 if ((!really && *PL_Argv[0] != '/') ||
1426 (really && *tmps != '/')) /* will execvp use PATH? */
1427 TAINT_ENV(); /* testing IFS here is overkill, probably */
1429 if (really && *tmps)
1430 PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
1432 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1434 if (ckWARN(WARN_EXEC))
1435 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
1436 (really ? tmps : PL_Argv[0]), Strerror(errno));
1438 const int e = errno;
1440 PerlLIO_write(fd, (void*)&e, sizeof(int));
1450 Perl_do_execfree(pTHX)
1459 #ifdef PERL_DEFAULT_DO_EXEC3_IMPLEMENTATION
1462 Perl_do_exec3(pTHX_ const char *incmd, int fd, int do_report)
1469 /* Make a copy so we can change it */
1470 const int cmdlen = strlen(incmd);
1471 Newx(cmd, cmdlen+1, char);
1472 strncpy(cmd, incmd, cmdlen);
1475 while (*cmd && isSPACE(*cmd))
1478 /* save an extra exec if possible */
1482 char flags[PERL_FLAGS_MAX];
1483 if (strnEQ(cmd,PL_cshname,PL_cshlen) &&
1484 strnEQ(cmd+PL_cshlen," -c",3)) {
1486 strlcpy(flags, "-c", PERL_FLAGS_MAX);
1490 s = cmd+PL_cshlen+3;
1494 strlcat(flags, "f", PERL_FLAGS_MAX - 2);
1502 char * const ncmd = s;
1508 if (s[-1] == '\'') {
1511 PerlProc_execl(PL_cshname,"csh", flags, ncmd, (char*)0);
1522 /* see if there are shell metacharacters in it */
1524 if (*cmd == '.' && isSPACE(cmd[1]))
1527 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1532 s++; /* catch VAR=val gizmo */
1536 for (s = cmd; *s; s++) {
1537 if (*s != ' ' && !isALPHA(*s) &&
1538 strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1539 if (*s == '\n' && !s[1]) {
1543 /* handle the 2>&1 construct at the end */
1544 if (*s == '>' && s[1] == '&' && s[2] == '1'
1545 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1546 && (!s[3] || isSPACE(s[3])))
1548 const char *t = s + 3;
1550 while (*t && isSPACE(*t))
1552 if (!*t && (PerlLIO_dup2(1,2) != -1)) {
1559 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
1566 Newx(PL_Argv, (s - cmd) / 2 + 2, char*);
1567 PL_Cmd = savepvn(cmd, s-cmd);
1569 for (s = PL_Cmd; *s;) {
1574 while (*s && !isSPACE(*s))
1582 PerlProc_execvp(PL_Argv[0],PL_Argv);
1584 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1588 if (ckWARN(WARN_EXEC))
1589 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
1590 PL_Argv[0], Strerror(errno));
1592 const int e = errno;
1593 PerlLIO_write(fd, (const void*)&e, sizeof(int));
1602 #endif /* OS2 || WIN32 */
1605 Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
1609 register I32 tot = 0;
1610 const char *const what = PL_op_name[type];
1612 SV ** const oldmark = mark;
1614 /* Doing this ahead of the switch statement preserves the old behaviour,
1615 where attempting to use kill as a taint test test would fail on
1616 platforms where kill was not defined. */
1618 if (type == OP_KILL)
1619 Perl_die(aTHX_ PL_no_func, what);
1622 if (type == OP_CHOWN)
1623 Perl_die(aTHX_ PL_no_func, what);
1627 #define APPLY_TAINT_PROPER() \
1629 if (PL_tainted) { TAINT_PROPER(what); } \
1632 /* This is a first heuristic; it doesn't catch tainting magic. */
1634 while (++mark <= sp) {
1635 if (SvTAINTED(*mark)) {
1644 APPLY_TAINT_PROPER();
1647 APPLY_TAINT_PROPER();
1649 while (++mark <= sp) {
1651 if (SvTYPE(*mark) == SVt_PVGV) {
1654 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1656 APPLY_TAINT_PROPER();
1657 if (fchmod(PerlIO_fileno(IoIFP(GvIOn(gv))), val))
1660 Perl_die(aTHX_ PL_no_func, "fchmod");
1667 else if (SvROK(*mark) && SvTYPE(SvRV(*mark)) == SVt_PVGV) {
1668 gv = (GV*)SvRV(*mark);
1672 const char *name = SvPV_nolen_const(*mark);
1673 APPLY_TAINT_PROPER();
1674 if (PerlLIO_chmod(name, val))
1682 APPLY_TAINT_PROPER();
1683 if (sp - mark > 2) {
1685 val = SvIVx(*++mark);
1686 val2 = SvIVx(*++mark);
1687 APPLY_TAINT_PROPER();
1689 while (++mark <= sp) {
1691 if (SvTYPE(*mark) == SVt_PVGV) {
1694 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1696 APPLY_TAINT_PROPER();
1697 if (fchown(PerlIO_fileno(IoIFP(GvIOn(gv))), val, val2))
1700 Perl_die(aTHX_ PL_no_func, "fchown");
1707 else if (SvROK(*mark) && SvTYPE(SvRV(*mark)) == SVt_PVGV) {
1708 gv = (GV*)SvRV(*mark);
1712 const char *name = SvPV_nolen_const(*mark);
1713 APPLY_TAINT_PROPER();
1714 if (PerlLIO_chown(name, val, val2))
1722 XXX Should we make lchown() directly available from perl?
1723 For now, we'll let Configure test for HAS_LCHOWN, but do
1724 nothing in the core.
1729 APPLY_TAINT_PROPER();
1732 s = SvPVx_nolen_const(*++mark);
1734 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1736 if ((val = whichsig(s)) < 0)
1737 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
1741 APPLY_TAINT_PROPER();
1744 /* kill() doesn't do process groups (job trees?) under VMS */
1745 if (val < 0) val = -val;
1746 if (val == SIGKILL) {
1747 # include <starlet.h>
1748 /* Use native sys$delprc() to insure that target process is
1749 * deleted; supervisor-mode images don't pay attention to
1750 * CRTL's emulation of Unix-style signals and kill()
1752 while (++mark <= sp) {
1753 I32 proc = SvIVx(*mark);
1754 register unsigned long int __vmssts;
1755 APPLY_TAINT_PROPER();
1756 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1760 case SS$_NOSUCHNODE:
1761 SETERRNO(ESRCH,__vmssts);
1764 SETERRNO(EPERM,__vmssts);
1767 SETERRNO(EVMSERR,__vmssts);
1776 while (++mark <= sp) {
1777 const I32 proc = SvIVx(*mark);
1778 APPLY_TAINT_PROPER();
1780 if (PerlProc_killpg(proc,val)) /* BSD */
1782 if (PerlProc_kill(-proc,val)) /* SYSV */
1788 while (++mark <= sp) {
1789 const I32 proc = SvIVx(*mark);
1790 APPLY_TAINT_PROPER();
1791 if (PerlProc_kill(proc, val))
1798 APPLY_TAINT_PROPER();
1800 while (++mark <= sp) {
1801 s = SvPV_nolen_const(*mark);
1802 APPLY_TAINT_PROPER();
1803 if (PL_euid || PL_unsafe) {
1807 else { /* don't let root wipe out directories without -U */
1808 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1817 #if defined(HAS_UTIME) || defined(HAS_FUTIMES)
1819 APPLY_TAINT_PROPER();
1820 if (sp - mark > 2) {
1821 #if defined(HAS_FUTIMES)
1822 struct timeval utbuf[2];
1823 void *utbufp = utbuf;
1824 #elif defined(I_UTIME) || defined(VMS)
1825 struct utimbuf utbuf;
1826 struct utimbuf *utbufp = &utbuf;
1832 void *utbufp = &utbuf;
1835 SV* const accessed = *++mark;
1836 SV* const modified = *++mark;
1838 /* Be like C, and if both times are undefined, let the C
1839 * library figure out what to do. This usually means
1840 * "current time". */
1842 if ( accessed == &PL_sv_undef && modified == &PL_sv_undef )
1845 Zero(&utbuf, sizeof utbuf, char);
1847 utbuf[0].tv_sec = (long)SvIVx(accessed); /* time accessed */
1848 utbuf[0].tv_usec = 0;
1849 utbuf[1].tv_sec = (long)SvIVx(modified); /* time modified */
1850 utbuf[1].tv_usec = 0;
1851 #elif defined(BIG_TIME)
1852 utbuf.actime = (Time_t)SvNVx(accessed); /* time accessed */
1853 utbuf.modtime = (Time_t)SvNVx(modified); /* time modified */
1855 utbuf.actime = (Time_t)SvIVx(accessed); /* time accessed */
1856 utbuf.modtime = (Time_t)SvIVx(modified); /* time modified */
1859 APPLY_TAINT_PROPER();
1861 while (++mark <= sp) {
1863 if (SvTYPE(*mark) == SVt_PVGV) {
1866 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1868 APPLY_TAINT_PROPER();
1869 if (futimes(PerlIO_fileno(IoIFP(GvIOn(gv))), utbufp))
1872 Perl_die(aTHX_ PL_no_func, "futimes");
1879 else if (SvROK(*mark) && SvTYPE(SvRV(*mark)) == SVt_PVGV) {
1880 gv = (GV*)SvRV(*mark);
1884 const char * const name = SvPV_nolen_const(*mark);
1885 APPLY_TAINT_PROPER();
1887 if (utimes(name, utbufp))
1889 if (PerlLIO_utime(name, utbufp))
1903 #undef APPLY_TAINT_PROPER
1906 /* Do the permissions allow some operation? Assumes statcache already set. */
1907 #ifndef VMS /* VMS' cando is in vms.c */
1909 Perl_cando(pTHX_ Mode_t mode, bool effective, register const Stat_t *statbufp)
1910 /* effective is a flag, true for EUID, or for checking if the effective gid
1911 * is in the list of groups returned from getgroups().
1916 /* [Comments and code from Len Reed]
1917 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1918 * to write-protected files. The execute permission bit is set
1919 * by the Miscrosoft C library stat() function for the following:
1924 * All files and directories are readable.
1925 * Directories and special files, e.g. "CON", cannot be
1927 * [Comment by Tom Dinger -- a directory can have the write-protect
1928 * bit set in the file system, but DOS permits changes to
1929 * the directory anyway. In addition, all bets are off
1930 * here for networked software, such as Novell and
1934 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1935 * too so it will actually look into the files for magic numbers
1937 return (mode & statbufp->st_mode) ? TRUE : FALSE;
1939 #else /* ! DOSISH */
1940 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
1941 if (mode == S_IXUSR) {
1942 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1946 return TRUE; /* root reads and writes anything */
1949 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1950 if (statbufp->st_mode & mode)
1951 return TRUE; /* ok as "user" */
1953 else if (ingroup(statbufp->st_gid,effective)) {
1954 if (statbufp->st_mode & mode >> 3)
1955 return TRUE; /* ok as "group" */
1957 else if (statbufp->st_mode & mode >> 6)
1958 return TRUE; /* ok as "other" */
1960 #endif /* ! DOSISH */
1965 Perl_ingroup(pTHX_ Gid_t testgid, bool effective)
1967 #ifdef MACOS_TRADITIONAL
1968 /* This is simply not correct for AppleShare, but fix it yerself. */
1972 if (testgid == (effective ? PL_egid : PL_gid))
1974 #ifdef HAS_GETGROUPS
1976 Groups_t *gary = NULL;
1980 anum = getgroups(0, gary);
1981 Newx(gary, anum, Groups_t);
1982 anum = getgroups(anum, gary);
1984 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)
2004 const key_t key = (key_t)SvNVx(*++mark);
2005 const I32 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
2006 const I32 flags = SvIVx(*++mark);
2008 PERL_UNUSED_ARG(sp);
2015 return msgget(key, flags);
2019 return semget(key, n, flags);
2023 return shmget(key, n, flags);
2025 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
2027 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2030 return -1; /* should never happen */
2034 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
2039 const I32 id = SvIVx(*++mark);
2041 const I32 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
2043 const I32 cmd = SvIVx(*++mark);
2044 SV * const astr = *++mark;
2045 STRLEN infosize = 0;
2046 I32 getinfo = (cmd == IPC_STAT);
2048 PERL_UNUSED_ARG(sp);
2054 if (cmd == IPC_STAT || cmd == IPC_SET)
2055 infosize = sizeof(struct msqid_ds);
2060 if (cmd == IPC_STAT || cmd == IPC_SET)
2061 infosize = sizeof(struct shmid_ds);
2067 if (cmd == IPC_STAT || cmd == IPC_SET)
2068 infosize = sizeof(struct semid_ds);
2069 else if (cmd == GETALL || cmd == SETALL)
2071 struct semid_ds semds;
2073 #ifdef EXTRA_F_IN_SEMUN_BUF
2074 semun.buff = &semds;
2078 getinfo = (cmd == GETALL);
2079 if (Semctl(id, 0, IPC_STAT, semun) == -1)
2081 infosize = semds.sem_nsems * sizeof(short);
2082 /* "short" is technically wrong but much more portable
2083 than guessing about u_?short(_t)? */
2086 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2090 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
2092 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2100 SvPV_force_nolen(astr);
2101 a = SvGROW(astr, infosize+1);
2106 a = SvPV(astr, len);
2107 if (len != infosize)
2108 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
2116 const IV i = SvIV(astr);
2117 a = INT2PTR(char *,i); /* ouch */
2124 ret = msgctl(id, cmd, (struct msqid_ds *)a);
2130 union semun unsemds;
2132 #ifdef EXTRA_F_IN_SEMUN_BUF
2133 unsemds.buff = (struct semid_ds *)a;
2135 unsemds.buf = (struct semid_ds *)a;
2137 ret = Semctl(id, n, cmd, unsemds);
2139 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2146 ret = shmctl(id, cmd, (struct shmid_ds *)a);
2150 if (getinfo && ret >= 0) {
2151 SvCUR_set(astr, infosize);
2152 *SvEND(astr) = '\0';
2159 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
2164 const I32 id = SvIVx(*++mark);
2165 SV * const mstr = *++mark;
2166 const I32 flags = SvIVx(*++mark);
2167 const char * const mbuf = SvPV_const(mstr, len);
2168 const I32 msize = len - sizeof(long);
2170 PERL_UNUSED_ARG(sp);
2173 Perl_croak(aTHX_ "Arg too short for msgsnd");
2175 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
2177 Perl_croak(aTHX_ "msgsnd not implemented");
2182 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
2188 I32 msize, flags, ret;
2189 const I32 id = SvIVx(*++mark);
2190 SV * const mstr = *++mark;
2191 PERL_UNUSED_ARG(sp);
2193 /* suppress warning when reading into undef var --jhi */
2195 sv_setpvn(mstr, "", 0);
2196 msize = SvIVx(*++mark);
2197 mtype = (long)SvIVx(*++mark);
2198 flags = SvIVx(*++mark);
2199 SvPV_force_nolen(mstr);
2200 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
2203 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
2205 SvCUR_set(mstr, sizeof(long)+ret);
2206 *SvEND(mstr) = '\0';
2207 #ifndef INCOMPLETE_TAINTS
2208 /* who knows who has been playing with this message? */
2214 Perl_croak(aTHX_ "msgrcv not implemented");
2219 Perl_do_semop(pTHX_ SV **mark, SV **sp)
2224 const I32 id = SvIVx(*++mark);
2225 SV * const opstr = *++mark;
2226 const char * const opbuf = SvPV_const(opstr, opsize);
2227 PERL_UNUSED_ARG(sp);
2229 if (opsize < 3 * SHORTSIZE
2230 || (opsize % (3 * SHORTSIZE))) {
2231 SETERRNO(EINVAL,LIB_INVARG);
2235 /* We can't assume that sizeof(struct sembuf) == 3 * sizeof(short). */
2237 const int nsops = opsize / (3 * sizeof (short));
2239 short * const ops = (short *) opbuf;
2241 struct sembuf *temps, *t;
2244 Newx (temps, nsops, struct sembuf);
2252 result = semop(id, temps, nsops);
2266 Perl_croak(aTHX_ "semop not implemented");
2271 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
2276 struct shmid_ds shmds;
2277 const I32 id = SvIVx(*++mark);
2278 SV * const mstr = *++mark;
2279 const I32 mpos = SvIVx(*++mark);
2280 const I32 msize = SvIVx(*++mark);
2281 PERL_UNUSED_ARG(sp);
2284 if (shmctl(id, IPC_STAT, &shmds) == -1)
2286 if (mpos < 0 || msize < 0 || (size_t)mpos + msize > shmds.shm_segsz) {
2287 SETERRNO(EFAULT,SS_ACCVIO); /* can't do as caller requested */
2290 shm = (char *)shmat(id, 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, (STRLEN)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(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 */
2358 fp = Perl_vms_start_glob(aTHX_ tmpglob, io);
2361 #ifdef MACOS_TRADITIONAL
2362 sv_setpv(tmpcmd, "glob ");
2363 sv_catsv(tmpcmd, tmpglob);
2364 sv_catpv(tmpcmd, " |");
2368 sv_setpv(tmpcmd, "for a in ");
2369 sv_catsv(tmpcmd, tmpglob);
2370 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
2373 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
2374 sv_catsv(tmpcmd, tmpglob);
2376 sv_setpv(tmpcmd, "perlglob ");
2377 sv_catsv(tmpcmd, tmpglob);
2378 sv_catpv(tmpcmd, " |");
2383 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
2384 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
2385 sv_catsv(tmpcmd, tmpglob);
2386 sv_catpv(tmpcmd, "' 2>/dev/null |");
2388 sv_setpv(tmpcmd, "echo ");
2389 sv_catsv(tmpcmd, tmpglob);
2391 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
2393 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
2396 #endif /* !DOSISH */
2397 #endif /* MACOS_TRADITIONAL */
2398 (void)do_open(PL_last_in_gv, (char*)SvPVX_const(tmpcmd), SvCUR(tmpcmd),
2399 FALSE, O_RDONLY, 0, NULL);
2408 * c-indentation-style: bsd
2410 * indent-tabs-mode: t
2413 * ex: set ts=8 sts=4 sw=4 noet: