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_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_fetchpv(type,0,SVt_PVIO);
340 thatio = GvIO(thatgv);
344 SETERRNO(EINVAL,SS_IVCHAN);
348 if ((that_fp = IoIFP(thatio))) {
349 /* Flush stdio buffer before dup. --mjd
350 * Unfortunately SEEK_CURing 0 seems to
351 * be optimized away on most platforms;
352 * only Solaris and Linux seem to flush
355 /* sfio fails to clear error on next
356 sfwrite, contrary to documentation.
358 if (PerlIO_seek(that_fp, 0, SEEK_CUR) == -1)
359 PerlIO_clearerr(that_fp);
361 /* On the other hand, do all platforms
362 * take gracefully to flushing a read-only
363 * filehandle? Perhaps we should do
364 * fsetpos(src)+fgetpos(dst)? --nik */
365 PerlIO_flush(that_fp);
366 fd = PerlIO_fileno(that_fp);
367 /* When dup()ing STDIN, STDOUT or STDERR
368 * explicitly set appropriate access mode */
369 if (that_fp == PerlIO_stdout()
370 || that_fp == PerlIO_stderr())
371 IoTYPE(io) = IoTYPE_WRONLY;
372 else if (that_fp == PerlIO_stdin())
373 IoTYPE(io) = IoTYPE_RDONLY;
374 /* When dup()ing a socket, say result is
376 else if (IoTYPE(thatio) == IoTYPE_SOCKET)
377 IoTYPE(io) = IoTYPE_SOCKET;
385 fp = PerlIO_fdupopen(aTHX_ that_fp, NULL, dodup);
389 fd = PerlLIO_dup(fd);
392 if (!(fp = PerlIO_openn(aTHX_ type,mode,fd,0,0,NULL,num_svs,svp))) {
393 if (dodup && fd >= 0)
400 for (; isSPACE(*type); type++) ;
401 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
403 fp = PerlIO_stdout();
404 IoTYPE(io) = IoTYPE_STD;
406 Perl_croak(aTHX_ "More than one argument to '>%c' open",IoTYPE_STD);
411 namesv = sv_2mortal(newSVpvn(type,tend - type));
416 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
419 if (!fp && type && *type && *type != ':' && !isIDFIRST(*type))
420 goto unknown_open_mode;
421 } /* IoTYPE_WRONLY */
422 else if (*type == IoTYPE_RDONLY) {
423 for (type++; isSPACE(*type); type++) ;
427 strlcat(mode, "b", PERL_MODE_MAX);
429 strlcat(mode, "t", PERL_MODE_MAX);
439 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
442 IoTYPE(io) = IoTYPE_STD;
444 Perl_croak(aTHX_ "More than one argument to '<%c' open",IoTYPE_STD);
449 namesv = sv_2mortal(newSVpvn(type,tend - type));
454 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
456 if (!fp && type && *type && *type != ':' && !isIDFIRST(*type))
457 goto unknown_open_mode;
458 } /* IoTYPE_RDONLY */
459 else if ((num_svs && /* '-|...' or '...|' */
460 type[0] == IoTYPE_STD && type[1] == IoTYPE_PIPE) ||
461 (!num_svs && tend > type+1 && tend[-1] == IoTYPE_PIPE)) {
463 type += 2; /* skip over '-|' */
467 while (tend > type && isSPACE(tend[-1]))
469 for (; isSPACE(*type); type++)
475 /* command is missing 19990114 */
476 if (ckWARN(WARN_PIPE))
477 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open");
481 if (!(*name == '-' && name[1] == '\0') || num_svs)
483 TAINT_PROPER("piped open");
488 strlcat(mode, "b", PERL_MODE_MAX);
490 strlcat(mode, "t", PERL_MODE_MAX);
499 fp = PerlProc_popen_list(mode,num_svs,svp);
502 fp = PerlProc_popen(name,mode);
504 IoTYPE(io) = IoTYPE_PIPE;
506 for (; isSPACE(*type); type++) ;
508 if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) {
514 else { /* layer(Args) */
516 goto unknown_open_mode;
518 IoTYPE(io) = IoTYPE_RDONLY;
519 for (; isSPACE(*name); name++)
525 strlcat(mode, "b", PERL_MODE_MAX);
527 strlcat(mode, "t", PERL_MODE_MAX);
535 if (*name == '-' && name[1] == '\0') {
537 IoTYPE(io) = IoTYPE_STD;
541 namesv = sv_2mortal(newSVpvn(type,tend - type));
546 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
551 if (IoTYPE(io) == IoTYPE_RDONLY && ckWARN(WARN_NEWLINE)
552 && strchr(oname, '\n')
555 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
559 if (ckWARN(WARN_IO)) {
560 if ((IoTYPE(io) == IoTYPE_RDONLY) &&
561 (fp == PerlIO_stdout() || fp == PerlIO_stderr())) {
562 Perl_warner(aTHX_ packWARN(WARN_IO),
563 "Filehandle STD%s reopened as %s only for input",
564 ((fp == PerlIO_stdout()) ? "OUT" : "ERR"),
567 else if ((IoTYPE(io) == IoTYPE_WRONLY) && fp == PerlIO_stdin()) {
568 Perl_warner(aTHX_ packWARN(WARN_IO),
569 "Filehandle STDIN reopened as %s only for output",
574 fd = PerlIO_fileno(fp);
575 /* If there is no fd (e.g. PerlIO::scalar) assume it isn't a
576 * socket - this covers PerlIO::scalar - otherwise unless we "know" the
577 * type probe for socket-ness.
579 if (IoTYPE(io) && IoTYPE(io) != IoTYPE_PIPE && IoTYPE(io) != IoTYPE_STD && fd >= 0) {
580 if (PerlLIO_fstat(fd,&PL_statbuf) < 0) {
581 /* If PerlIO claims to have fd we had better be able to fstat() it. */
582 (void) PerlIO_close(fp);
586 if (S_ISSOCK(PL_statbuf.st_mode))
587 IoTYPE(io) = IoTYPE_SOCKET; /* in case a socket was passed in to us */
591 !(PL_statbuf.st_mode & S_IFMT)
595 && IoTYPE(io) != IoTYPE_WRONLY /* Dups of STD* filehandles already have */
596 && IoTYPE(io) != IoTYPE_RDONLY /* type so they aren't marked as sockets */
597 ) { /* on OS's that return 0 on fstat()ed pipe */
599 Sock_size_t buflen = sizeof tmpbuf;
600 if (PerlSock_getsockname(fd, (struct sockaddr *)tmpbuf, &buflen) >= 0
601 || errno != ENOTSOCK)
602 IoTYPE(io) = IoTYPE_SOCKET; /* some OS's return 0 on fstat()ed socket */
603 /* but some return 0 for streams too, sigh */
605 #endif /* HAS_SOCKET */
606 #endif /* !PERL_MICRO */
610 * If this is a standard handle we discard all the layer stuff
611 * and just dup the fd into whatever was on the handle before !
614 if (saveifp) { /* must use old fp? */
615 /* If fd is less that PL_maxsysfd i.e. STDIN..STDERR
616 then dup the new fileno down
619 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
620 if (saveofp != saveifp) { /* was a socket? */
621 PerlIO_close(saveofp);
625 /* Still a small can-of-worms here if (say) PerlIO::scalar
626 is assigned to (say) STDOUT - for now let dup2() fail
627 and provide the error
629 if (PerlLIO_dup2(fd, savefd) < 0) {
630 (void)PerlIO_close(fp);
634 if (savefd != PerlIO_fileno(PerlIO_stdin())) {
635 char newname[FILENAME_MAX+1];
636 if (PerlIO_getname(fp, newname)) {
637 if (fd == PerlIO_fileno(PerlIO_stdout()))
638 Perl_vmssetuserlnm(aTHX_ "SYS$OUTPUT", newname);
639 if (fd == PerlIO_fileno(PerlIO_stderr()))
640 Perl_vmssetuserlnm(aTHX_ "SYS$ERROR", newname);
646 /* PL_fdpid isn't used on Windows, so avoid this useless work.
647 * XXX Probably the same for a lot of other places. */
653 sv = *av_fetch(PL_fdpid,fd,TRUE);
654 SvUPGRADE(sv, SVt_IV);
657 sv = *av_fetch(PL_fdpid,savefd,TRUE);
658 SvUPGRADE(sv, SVt_IV);
665 /* need to close fp without closing underlying fd */
666 int ofd = PerlIO_fileno(fp);
667 int dupfd = PerlLIO_dup(ofd);
668 #if defined(HAS_FCNTL) && defined(F_SETFD)
669 /* Assume if we have F_SETFD we have F_GETFD */
670 int coe = fcntl(ofd,F_GETFD);
673 PerlLIO_dup2(dupfd,ofd);
674 #if defined(HAS_FCNTL) && defined(F_SETFD)
675 /* The dup trick has lost close-on-exec on ofd */
676 fcntl(ofd,F_SETFD, coe);
678 PerlLIO_close(dupfd);
685 fd = PerlIO_fileno(fp);
687 #if defined(HAS_FCNTL) && defined(F_SETFD)
689 const int save_errno = errno;
690 fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
696 IoFLAGS(io) &= ~IOf_NOLINE;
698 if (IoTYPE(io) == IoTYPE_SOCKET
699 || (IoTYPE(io) == IoTYPE_WRONLY && fd >= 0 && S_ISCHR(PL_statbuf.st_mode)) ) {
701 if (*s == IoTYPE_IMPLICIT || *s == IoTYPE_NUMERIC)
704 if (!(IoOFP(io) = PerlIO_openn(aTHX_ type,s,fd,0,0,NULL,0,svp))) {
718 IoTYPE(io) = savetype;
723 Perl_nextargv(pTHX_ register GV *gv)
726 #ifndef FLEXFILENAMES
732 IO * const io = GvIOp(gv);
735 PL_argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO);
736 if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
737 IoFLAGS(io) &= ~IOf_START;
739 if (!PL_argvout_stack)
740 PL_argvout_stack = newAV();
741 av_push(PL_argvout_stack, SvREFCNT_inc(PL_defoutgv));
744 if (PL_filemode & (S_ISUID|S_ISGID)) {
745 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
748 (void)fchmod(PL_lastfd,PL_filemode);
750 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
757 while (av_len(GvAV(gv)) >= 0) {
759 sv = av_shift(GvAV(gv));
761 sv_setsv(GvSVn(gv),sv);
762 SvSETMAGIC(GvSV(gv));
763 PL_oldname = SvPVx(GvSV(gv), oldlen);
764 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,Nullfp)) {
766 TAINT_PROPER("inplace open");
767 if (oldlen == 1 && *PL_oldname == '-') {
768 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
769 return IoIFP(GvIOp(gv));
771 #ifndef FLEXFILENAMES
772 filedev = PL_statbuf.st_dev;
773 fileino = PL_statbuf.st_ino;
775 PL_filemode = PL_statbuf.st_mode;
776 fileuid = PL_statbuf.st_uid;
777 filegid = PL_statbuf.st_gid;
778 if (!S_ISREG(PL_filemode)) {
779 if (ckWARN_d(WARN_INPLACE))
780 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
781 "Can't do inplace edit: %s is not a regular file",
787 const char *star = strchr(PL_inplace, '*');
789 const char *begin = PL_inplace;
790 sv_setpvn(sv, "", 0);
792 sv_catpvn(sv, begin, star - begin);
793 sv_catpvn(sv, PL_oldname, oldlen);
795 } while ((star = strchr(begin, '*')));
800 sv_catpv(sv,PL_inplace);
802 #ifndef FLEXFILENAMES
803 if ((PerlLIO_stat(SvPVX_const(sv),&PL_statbuf) >= 0
804 && PL_statbuf.st_dev == filedev
805 && PL_statbuf.st_ino == fileino)
807 || ((_djstat_fail_bits & _STFAIL_TRUENAME)!=0)
811 if (ckWARN_d(WARN_INPLACE))
812 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
813 "Can't do inplace edit: %"SVf" would not be unique",
820 #if !defined(DOSISH) && !defined(__CYGWIN__) && !defined(EPOC)
821 if (PerlLIO_rename(PL_oldname,SvPVX_const(sv)) < 0) {
822 if (ckWARN_d(WARN_INPLACE))
823 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
824 "Can't rename %s to %"SVf": %s, skipping file",
825 PL_oldname, sv, Strerror(errno) );
831 (void)PerlLIO_unlink(SvPVX_const(sv));
832 (void)PerlLIO_rename(PL_oldname,SvPVX_const(sv));
833 do_open(gv,(char*)SvPVX_const(sv),SvCUR(sv),PL_inplace!=0,
837 (void)UNLINK(SvPVX_const(sv));
838 if (link(PL_oldname,SvPVX_const(sv)) < 0) {
839 if (ckWARN_d(WARN_INPLACE))
840 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
841 "Can't rename %s to %"SVf": %s, skipping file",
842 PL_oldname, sv, Strerror(errno) );
846 (void)UNLINK(PL_oldname);
850 #if !defined(DOSISH) && !defined(AMIGAOS)
851 # ifndef VMS /* Don't delete; use automatic file versioning */
852 if (UNLINK(PL_oldname) < 0) {
853 if (ckWARN_d(WARN_INPLACE))
854 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
855 "Can't remove %s: %s, skipping file",
856 PL_oldname, Strerror(errno) );
862 Perl_croak(aTHX_ "Can't do inplace edit without backup");
866 sv_setpvn(sv,">",!PL_inplace);
867 sv_catpvn(sv,PL_oldname,oldlen);
868 SETERRNO(0,0); /* in case sprintf set errno */
870 if (!do_open(PL_argvoutgv,(char*)SvPVX_const(sv),SvCUR(sv),
871 PL_inplace!=0,O_WRONLY|O_CREAT|O_TRUNC,0,Nullfp))
873 if (!do_open(PL_argvoutgv,(char*)SvPVX_const(sv),SvCUR(sv),
874 PL_inplace!=0,O_WRONLY|O_CREAT|OPEN_EXCL,0666,
878 if (ckWARN_d(WARN_INPLACE))
879 Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't do inplace edit on %s: %s",
880 PL_oldname, Strerror(errno) );
884 setdefout(PL_argvoutgv);
885 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
886 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
888 (void)fchmod(PL_lastfd,PL_filemode);
890 # if !(defined(WIN32) && defined(__BORLANDC__))
891 /* Borland runtime creates a readonly file! */
892 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
895 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
897 (void)fchown(PL_lastfd,fileuid,filegid);
900 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
905 return IoIFP(GvIOp(gv));
908 if (ckWARN_d(WARN_INPLACE)) {
909 const int eno = errno;
910 if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0
911 && !S_ISREG(PL_statbuf.st_mode))
913 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
914 "Can't do inplace edit: %s is not a regular file",
918 Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't open %s: %s",
919 PL_oldname, Strerror(eno));
923 if (io && (IoFLAGS(io) & IOf_ARGV))
924 IoFLAGS(io) |= IOf_START;
926 (void)do_close(PL_argvoutgv,FALSE);
927 if (io && (IoFLAGS(io) & IOf_ARGV)
928 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
930 GV *oldout = (GV*)av_pop(PL_argvout_stack);
932 SvREFCNT_dec(oldout);
935 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
940 /* explicit renamed to avoid C++ conflict -- kja */
942 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
949 if (!gv || SvTYPE(gv) != SVt_PVGV) {
951 SETERRNO(EBADF,SS_IVCHAN);
955 if (!io) { /* never opened */
957 if (ckWARN(WARN_UNOPENED)) /* no check for closed here */
958 report_evil_fh(gv, io, PL_op->op_type);
959 SETERRNO(EBADF,SS_IVCHAN);
963 retval = io_close(io, not_implicit);
967 IoLINES_LEFT(io) = IoPAGE_LEN(io);
969 IoTYPE(io) = IoTYPE_CLOSED;
974 Perl_io_close(pTHX_ IO *io, bool not_implicit)
979 if (IoTYPE(io) == IoTYPE_PIPE) {
980 const int status = PerlProc_pclose(IoIFP(io));
982 STATUS_NATIVE_CHILD_SET(status);
983 retval = (STATUS_UNIX == 0);
986 retval = (status != -1);
989 else if (IoTYPE(io) == IoTYPE_STD)
992 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
993 const bool prev_err = PerlIO_error(IoOFP(io));
994 retval = (PerlIO_close(IoOFP(io)) != EOF && !prev_err);
995 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
998 const bool prev_err = PerlIO_error(IoIFP(io));
999 retval = (PerlIO_close(IoIFP(io)) != EOF && !prev_err);
1002 IoOFP(io) = IoIFP(io) = Nullfp;
1004 else if (not_implicit) {
1005 SETERRNO(EBADF,SS_IVCHAN);
1012 Perl_do_eof(pTHX_ GV *gv)
1014 register IO * const io = GvIO(gv);
1018 else if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
1019 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1022 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
1023 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
1024 return FALSE; /* this is the most usual case */
1028 /* getc and ungetc can stomp on errno */
1029 const int saverrno = errno;
1030 const int ch = PerlIO_getc(IoIFP(io));
1032 (void)PerlIO_ungetc(IoIFP(io),ch);
1039 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
1040 if (PerlIO_get_cnt(IoIFP(io)) < -1)
1041 PerlIO_set_cnt(IoIFP(io),-1);
1043 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
1044 if (gv != PL_argvgv || !nextargv(gv)) /* get another fp handy */
1048 return TRUE; /* normal fp, definitely end of file */
1054 Perl_do_tell(pTHX_ GV *gv)
1056 register IO *io = 0;
1057 register PerlIO *fp;
1059 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1060 #ifdef ULTRIX_STDIO_BOTCH
1062 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1064 return PerlIO_tell(fp);
1066 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1067 report_evil_fh(gv, io, PL_op->op_type);
1068 SETERRNO(EBADF,RMS_IFI);
1073 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
1075 register IO *io = 0;
1076 register PerlIO *fp;
1078 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1079 #ifdef ULTRIX_STDIO_BOTCH
1081 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1083 return PerlIO_seek(fp, pos, whence) >= 0;
1085 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1086 report_evil_fh(gv, io, PL_op->op_type);
1087 SETERRNO(EBADF,RMS_IFI);
1092 Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
1094 register IO *io = 0;
1095 register PerlIO *fp;
1097 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
1098 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
1099 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1100 report_evil_fh(gv, io, PL_op->op_type);
1101 SETERRNO(EBADF,RMS_IFI);
1106 Perl_mode_from_discipline(pTHX_ SV *discp)
1108 int mode = O_BINARY;
1111 const char *s = SvPV_const(discp,len);
1116 if (s[2] == 'a' && s[3] == 'w'
1117 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1126 if (s[2] == 'r' && s[3] == 'l' && s[4] == 'f'
1127 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1136 goto fail_discipline;
1139 else if (isSPACE(*s)) {
1146 end = strchr(s+1, ':');
1149 #ifndef PERLIO_LAYERS
1150 Perl_croak(aTHX_ "IO layers (like '%.*s') unavailable", end-s, s);
1161 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE)
1163 my_chsize(int fd, Off_t length)
1166 /* code courtesy of William Kucharski */
1172 if (PerlLIO_fstat(fd, &filebuf) < 0)
1175 if (filebuf.st_size < length) {
1177 /* extend file length */
1179 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
1182 /* write a "0" byte */
1184 if ((PerlLIO_write(fd, "", 1)) != 1)
1188 /* truncate length */
1192 fl.l_start = length;
1193 fl.l_type = F_WRLCK; /* write lock on file space */
1196 * This relies on the UNDOCUMENTED F_FREESP argument to
1197 * fcntl(2), which truncates the file so that it ends at the
1198 * position indicated by fl.l_start.
1200 * Will minor miracles never cease?
1203 if (fcntl(fd, F_FREESP, &fl) < 0)
1209 Perl_croak_nocontext("truncate not implemented");
1210 #endif /* F_FREESP */
1213 #endif /* !HAS_TRUNCATE && !HAS_CHSIZE */
1216 Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
1218 register const char *tmps;
1221 /* assuming fp is checked earlier */
1224 switch (SvTYPE(sv)) {
1226 if (ckWARN(WARN_UNINITIALIZED))
1233 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
1235 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
1236 return !PerlIO_error(fp);
1240 if (PerlIO_isutf8(fp)) {
1242 sv_utf8_upgrade_flags(sv = sv_mortalcopy(sv),
1243 SV_GMAGIC|SV_UTF8_NO_ENCODING);
1245 else if (DO_UTF8(sv)) {
1246 if (!sv_utf8_downgrade((sv = sv_mortalcopy(sv)), TRUE)
1247 && ckWARN_d(WARN_UTF8))
1249 Perl_warner(aTHX_ packWARN(WARN_UTF8), "Wide character in print");
1252 tmps = SvPV_const(sv, len);
1255 /* To detect whether the process is about to overstep its
1256 * filesize limit we would need getrlimit(). We could then
1257 * also transparently raise the limit with setrlimit() --
1258 * but only until the system hard limit/the filesystem limit,
1259 * at which we would get EPERM. Note that when using buffered
1260 * io the write failure can be delayed until the flush/close. --jhi */
1261 if (len && (PerlIO_write(fp,tmps,len) == 0))
1263 return !PerlIO_error(fp);
1273 if (PL_op->op_flags & OPf_REF) {
1278 if (io && IoIFP(io)) {
1280 sv_setpvn(PL_statname,"", 0);
1281 PL_laststype = OP_STAT;
1282 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
1286 return PL_laststatval;
1287 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1288 report_evil_fh(gv, io, PL_op->op_type);
1290 sv_setpvn(PL_statname,"", 0);
1291 return (PL_laststatval = -1);
1294 else if (PL_op->op_private & OPpFT_STACKED) {
1295 return PL_laststatval;
1298 SV* const sv = POPs;
1302 if (SvTYPE(sv) == SVt_PVGV) {
1306 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
1311 s = SvPV_const(sv, len);
1313 sv_setpvn(PL_statname, s, len);
1314 s = SvPVX_const(PL_statname); /* s now NUL-terminated */
1315 PL_laststype = OP_STAT;
1316 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1317 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
1318 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
1319 return PL_laststatval;
1327 static const char no_prev_lstat[] = "The stat preceding -l _ wasn't an lstat";
1330 if (PL_op->op_flags & OPf_REF) {
1332 if (cGVOP_gv == PL_defgv) {
1333 if (PL_laststype != OP_LSTAT)
1334 Perl_croak(aTHX_ no_prev_lstat);
1335 return PL_laststatval;
1337 if (ckWARN(WARN_IO)) {
1338 Perl_warner(aTHX_ packWARN(WARN_IO), "Use of -l on filehandle %s",
1340 return (PL_laststatval = -1);
1343 else if (PL_laststype != OP_LSTAT
1344 && (PL_op->op_private & OPpFT_STACKED) && ckWARN(WARN_IO))
1345 Perl_croak(aTHX_ no_prev_lstat);
1347 PL_laststype = OP_LSTAT;
1351 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV && ckWARN(WARN_IO)) {
1352 Perl_warner(aTHX_ packWARN(WARN_IO), "Use of -l on filehandle %s",
1353 GvENAME((GV*) SvRV(sv)));
1354 return (PL_laststatval = -1);
1356 /* XXX Do really need to be calling SvPV() all these times? */
1357 sv_setpv(PL_statname,SvPV_nolen_const(sv));
1358 PL_laststatval = PerlLIO_lstat(SvPV_nolen_const(sv),&PL_statcache);
1359 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(sv), '\n'))
1360 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "lstat");
1361 return PL_laststatval;
1365 Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1366 int fd, int do_report)
1369 #if defined(MACOS_TRADITIONAL) || defined(__SYMBIAN32__)
1370 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1374 const char *tmps = Nullch;
1375 Newx(PL_Argv, sp - mark + 1, char*);
1378 while (++mark <= sp) {
1380 *a++ = (char*)SvPV_nolen_const(*mark);
1386 tmps = SvPV_nolen_const(really);
1387 if ((!really && *PL_Argv[0] != '/') ||
1388 (really && *tmps != '/')) /* will execvp use PATH? */
1389 TAINT_ENV(); /* testing IFS here is overkill, probably */
1391 if (really && *tmps)
1392 PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
1394 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1396 if (ckWARN(WARN_EXEC))
1397 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
1398 (really ? tmps : PL_Argv[0]), Strerror(errno));
1400 const int e = errno;
1402 PerlLIO_write(fd, (void*)&e, sizeof(int));
1412 Perl_do_execfree(pTHX)
1415 PL_Argv = Null(char **);
1420 #ifdef PERL_DEFAULT_DO_EXEC3_IMPLEMENTATION
1423 Perl_do_exec3(pTHX_ const char *incmd, int fd, int do_report)
1430 /* Make a copy so we can change it */
1431 const int cmdlen = strlen(incmd);
1432 Newx(cmd, cmdlen+1, char);
1433 strncpy(cmd, incmd, cmdlen);
1436 while (*cmd && isSPACE(*cmd))
1439 /* save an extra exec if possible */
1443 char flags[PERL_FLAGS_MAX];
1444 if (strnEQ(cmd,PL_cshname,PL_cshlen) &&
1445 strnEQ(cmd+PL_cshlen," -c",3)) {
1447 strlcpy(flags, "-c", PERL_FLAGS_MAX);
1451 s = cmd+PL_cshlen+3;
1455 strlcat(flags, "f", PERL_FLAGS_MAX);
1463 char * const ncmd = s;
1469 if (s[-1] == '\'') {
1472 PerlProc_execl(PL_cshname,"csh", flags, ncmd, (char*)0);
1483 /* see if there are shell metacharacters in it */
1485 if (*cmd == '.' && isSPACE(cmd[1]))
1488 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1491 for (s = cmd; *s && isALNUM(*s); s++) ; /* catch VAR=val gizmo */
1495 for (s = cmd; *s; s++) {
1496 if (*s != ' ' && !isALPHA(*s) &&
1497 strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1498 if (*s == '\n' && !s[1]) {
1502 /* handle the 2>&1 construct at the end */
1503 if (*s == '>' && s[1] == '&' && s[2] == '1'
1504 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1505 && (!s[3] || isSPACE(s[3])))
1507 const char *t = s + 3;
1509 while (*t && isSPACE(*t))
1511 if (!*t && (PerlLIO_dup2(1,2) != -1)) {
1518 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
1525 Newx(PL_Argv, (s - cmd) / 2 + 2, char*);
1526 PL_Cmd = savepvn(cmd, s-cmd);
1528 for (s = PL_Cmd; *s;) {
1529 while (*s && isSPACE(*s)) s++;
1532 while (*s && !isSPACE(*s)) s++;
1539 PerlProc_execvp(PL_Argv[0],PL_Argv);
1541 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1546 if (ckWARN(WARN_EXEC))
1547 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
1548 PL_Argv[0], Strerror(errno));
1550 const int e = errno;
1551 PerlLIO_write(fd, (void*)&e, sizeof(int));
1561 #endif /* OS2 || WIN32 */
1564 Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
1567 register I32 tot = 0;
1568 const char *const what = PL_op_name[type];
1570 SV ** const oldmark = mark;
1572 /* Doing this ahead of the switch statement preserves the old behaviour,
1573 where attempting to use kill as a taint test test would fail on
1574 platforms where kill was not defined. */
1576 if (type == OP_KILL)
1577 Perl_die(aTHX_ PL_no_func, what);
1580 if (type == OP_CHOWN)
1581 Perl_die(aTHX_ PL_no_func, what);
1585 #define APPLY_TAINT_PROPER() \
1587 if (PL_tainted) { TAINT_PROPER(what); } \
1590 /* This is a first heuristic; it doesn't catch tainting magic. */
1592 while (++mark <= sp) {
1593 if (SvTAINTED(*mark)) {
1602 APPLY_TAINT_PROPER();
1605 APPLY_TAINT_PROPER();
1607 while (++mark <= sp) {
1609 if (SvTYPE(*mark) == SVt_PVGV) {
1612 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1614 APPLY_TAINT_PROPER();
1615 if (fchmod(PerlIO_fileno(IoIFP(GvIOn(gv))), val))
1618 Perl_die(aTHX_ PL_no_func, "fchmod");
1625 else if (SvROK(*mark) && SvTYPE(SvRV(*mark)) == SVt_PVGV) {
1626 gv = (GV*)SvRV(*mark);
1630 const char *name = SvPV_nolen_const(*mark);
1631 APPLY_TAINT_PROPER();
1632 if (PerlLIO_chmod(name, val))
1640 APPLY_TAINT_PROPER();
1641 if (sp - mark > 2) {
1643 val = SvIVx(*++mark);
1644 val2 = SvIVx(*++mark);
1645 APPLY_TAINT_PROPER();
1647 while (++mark <= sp) {
1649 if (SvTYPE(*mark) == SVt_PVGV) {
1652 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1654 APPLY_TAINT_PROPER();
1655 if (fchown(PerlIO_fileno(IoIFP(GvIOn(gv))), val, val2))
1658 Perl_die(aTHX_ PL_no_func, "fchown");
1665 else if (SvROK(*mark) && SvTYPE(SvRV(*mark)) == SVt_PVGV) {
1666 gv = (GV*)SvRV(*mark);
1670 const char *name = SvPV_nolen_const(*mark);
1671 APPLY_TAINT_PROPER();
1672 if (PerlLIO_chown(name, val, val2))
1680 XXX Should we make lchown() directly available from perl?
1681 For now, we'll let Configure test for HAS_LCHOWN, but do
1682 nothing in the core.
1687 APPLY_TAINT_PROPER();
1690 s = SvPVx_nolen_const(*++mark);
1692 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1694 if ((val = whichsig(s)) < 0)
1695 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
1699 APPLY_TAINT_PROPER();
1702 /* kill() doesn't do process groups (job trees?) under VMS */
1703 if (val < 0) val = -val;
1704 if (val == SIGKILL) {
1705 # include <starlet.h>
1706 /* Use native sys$delprc() to insure that target process is
1707 * deleted; supervisor-mode images don't pay attention to
1708 * CRTL's emulation of Unix-style signals and kill()
1710 while (++mark <= sp) {
1711 I32 proc = SvIVx(*mark);
1712 register unsigned long int __vmssts;
1713 APPLY_TAINT_PROPER();
1714 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1718 case SS$_NOSUCHNODE:
1719 SETERRNO(ESRCH,__vmssts);
1722 SETERRNO(EPERM,__vmssts);
1725 SETERRNO(EVMSERR,__vmssts);
1734 while (++mark <= sp) {
1735 const I32 proc = SvIVx(*mark);
1736 APPLY_TAINT_PROPER();
1738 if (PerlProc_killpg(proc,val)) /* BSD */
1740 if (PerlProc_kill(-proc,val)) /* SYSV */
1746 while (++mark <= sp) {
1747 const I32 proc = SvIVx(*mark);
1748 APPLY_TAINT_PROPER();
1749 if (PerlProc_kill(proc, val))
1756 APPLY_TAINT_PROPER();
1758 while (++mark <= sp) {
1759 s = SvPV_nolen_const(*mark);
1760 APPLY_TAINT_PROPER();
1761 if (PL_euid || PL_unsafe) {
1765 else { /* don't let root wipe out directories without -U */
1766 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1775 #if defined(HAS_UTIME) || defined(HAS_FUTIMES)
1777 APPLY_TAINT_PROPER();
1778 if (sp - mark > 2) {
1779 #if defined(HAS_FUTIMES)
1780 struct timeval utbuf[2];
1781 void *utbufp = utbuf;
1782 #elif defined(I_UTIME) || defined(VMS)
1783 struct utimbuf utbuf;
1784 struct utimbuf *utbufp = &utbuf;
1790 void *utbufp = &utbuf;
1793 SV* const accessed = *++mark;
1794 SV* const modified = *++mark;
1796 /* Be like C, and if both times are undefined, let the C
1797 * library figure out what to do. This usually means
1798 * "current time". */
1800 if ( accessed == &PL_sv_undef && modified == &PL_sv_undef )
1803 Zero(&utbuf, sizeof utbuf, char);
1805 utbuf[0].tv_sec = (long)SvIVx(accessed); /* time accessed */
1806 utbuf[0].tv_usec = 0;
1807 utbuf[1].tv_sec = (long)SvIVx(modified); /* time modified */
1808 utbuf[1].tv_usec = 0;
1809 #elif defined(BIG_TIME)
1810 utbuf.actime = (Time_t)SvNVx(accessed); /* time accessed */
1811 utbuf.modtime = (Time_t)SvNVx(modified); /* time modified */
1813 utbuf.actime = (Time_t)SvIVx(accessed); /* time accessed */
1814 utbuf.modtime = (Time_t)SvIVx(modified); /* time modified */
1817 APPLY_TAINT_PROPER();
1819 while (++mark <= sp) {
1821 if (SvTYPE(*mark) == SVt_PVGV) {
1824 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1826 APPLY_TAINT_PROPER();
1827 if (futimes(PerlIO_fileno(IoIFP(GvIOn(gv))), utbufp))
1830 Perl_die(aTHX_ PL_no_func, "futimes");
1837 else if (SvROK(*mark) && SvTYPE(SvRV(*mark)) == SVt_PVGV) {
1838 gv = (GV*)SvRV(*mark);
1842 const char * const name = SvPV_nolen_const(*mark);
1843 APPLY_TAINT_PROPER();
1845 if (utimes(name, utbufp))
1847 if (PerlLIO_utime(name, utbufp))
1861 #undef APPLY_TAINT_PROPER
1864 /* Do the permissions allow some operation? Assumes statcache already set. */
1865 #ifndef VMS /* VMS' cando is in vms.c */
1867 Perl_cando(pTHX_ Mode_t mode, bool effective, register const Stat_t *statbufp)
1868 /* effective is a flag, true for EUID, or for checking if the effective gid
1869 * is in the list of groups returned from getgroups().
1873 /* [Comments and code from Len Reed]
1874 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1875 * to write-protected files. The execute permission bit is set
1876 * by the Miscrosoft C library stat() function for the following:
1881 * All files and directories are readable.
1882 * Directories and special files, e.g. "CON", cannot be
1884 * [Comment by Tom Dinger -- a directory can have the write-protect
1885 * bit set in the file system, but DOS permits changes to
1886 * the directory anyway. In addition, all bets are off
1887 * here for networked software, such as Novell and
1891 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1892 * too so it will actually look into the files for magic numbers
1894 return (mode & statbufp->st_mode) ? TRUE : FALSE;
1896 #else /* ! DOSISH */
1897 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
1898 if (mode == S_IXUSR) {
1899 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1903 return TRUE; /* root reads and writes anything */
1906 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1907 if (statbufp->st_mode & mode)
1908 return TRUE; /* ok as "user" */
1910 else if (ingroup(statbufp->st_gid,effective)) {
1911 if (statbufp->st_mode & mode >> 3)
1912 return TRUE; /* ok as "group" */
1914 else if (statbufp->st_mode & mode >> 6)
1915 return TRUE; /* ok as "other" */
1917 #endif /* ! DOSISH */
1922 Perl_ingroup(pTHX_ Gid_t testgid, bool effective)
1924 #ifdef MACOS_TRADITIONAL
1925 /* This is simply not correct for AppleShare, but fix it yerself. */
1928 if (testgid == (effective ? PL_egid : PL_gid))
1930 #ifdef HAS_GETGROUPS
1935 Groups_t gary[NGROUPS];
1938 anum = getgroups(NGROUPS,gary);
1940 if (gary[anum] == testgid)
1948 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1951 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
1953 const key_t key = (key_t)SvNVx(*++mark);
1954 const I32 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1955 const I32 flags = SvIVx(*++mark);
1963 return msgget(key, flags);
1967 return semget(key, n, flags);
1971 return shmget(key, n, flags);
1973 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1975 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1978 return -1; /* should never happen */
1982 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
1986 const I32 id = SvIVx(*++mark);
1987 const I32 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1988 const I32 cmd = SvIVx(*++mark);
1989 SV * const astr = *++mark;
1990 STRLEN infosize = 0;
1991 I32 getinfo = (cmd == IPC_STAT);
1993 PERL_UNUSED_ARG(sp);
1999 if (cmd == IPC_STAT || cmd == IPC_SET)
2000 infosize = sizeof(struct msqid_ds);
2005 if (cmd == IPC_STAT || cmd == IPC_SET)
2006 infosize = sizeof(struct shmid_ds);
2012 if (cmd == IPC_STAT || cmd == IPC_SET)
2013 infosize = sizeof(struct semid_ds);
2014 else if (cmd == GETALL || cmd == SETALL)
2016 struct semid_ds semds;
2018 #ifdef EXTRA_F_IN_SEMUN_BUF
2019 semun.buff = &semds;
2023 getinfo = (cmd == GETALL);
2024 if (Semctl(id, 0, IPC_STAT, semun) == -1)
2026 infosize = semds.sem_nsems * sizeof(short);
2027 /* "short" is technically wrong but much more portable
2028 than guessing about u_?short(_t)? */
2031 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2035 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
2037 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2045 SvPV_force_nolen(astr);
2046 a = SvGROW(astr, infosize+1);
2051 a = SvPV(astr, len);
2052 if (len != infosize)
2053 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
2061 const IV i = SvIV(astr);
2062 a = INT2PTR(char *,i); /* ouch */
2069 ret = msgctl(id, cmd, (struct msqid_ds *)a);
2075 union semun unsemds;
2077 #ifdef EXTRA_F_IN_SEMUN_BUF
2078 unsemds.buff = (struct semid_ds *)a;
2080 unsemds.buf = (struct semid_ds *)a;
2082 ret = Semctl(id, n, cmd, unsemds);
2084 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2091 ret = shmctl(id, cmd, (struct shmid_ds *)a);
2095 if (getinfo && ret >= 0) {
2096 SvCUR_set(astr, infosize);
2097 *SvEND(astr) = '\0';
2104 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
2108 const I32 id = SvIVx(*++mark);
2109 SV * const mstr = *++mark;
2110 const I32 flags = SvIVx(*++mark);
2111 const char * const mbuf = SvPV_const(mstr, len);
2112 const I32 msize = len - sizeof(long);
2114 PERL_UNUSED_ARG(sp);
2117 Perl_croak(aTHX_ "Arg too short for msgsnd");
2119 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
2121 Perl_croak(aTHX_ "msgsnd not implemented");
2126 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
2131 I32 msize, flags, ret;
2132 const I32 id = SvIVx(*++mark);
2133 SV * const mstr = *++mark;
2134 PERL_UNUSED_ARG(sp);
2136 /* suppress warning when reading into undef var --jhi */
2138 sv_setpvn(mstr, "", 0);
2139 msize = SvIVx(*++mark);
2140 mtype = (long)SvIVx(*++mark);
2141 flags = SvIVx(*++mark);
2142 SvPV_force_nolen(mstr);
2143 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
2146 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
2148 SvCUR_set(mstr, sizeof(long)+ret);
2149 *SvEND(mstr) = '\0';
2150 #ifndef INCOMPLETE_TAINTS
2151 /* who knows who has been playing with this message? */
2157 Perl_croak(aTHX_ "msgrcv not implemented");
2162 Perl_do_semop(pTHX_ SV **mark, SV **sp)
2166 const I32 id = SvIVx(*++mark);
2167 SV * const opstr = *++mark;
2168 const char * const opbuf = SvPV_const(opstr, opsize);
2169 PERL_UNUSED_ARG(sp);
2171 if (opsize < 3 * SHORTSIZE
2172 || (opsize % (3 * SHORTSIZE))) {
2173 SETERRNO(EINVAL,LIB_INVARG);
2177 /* We can't assume that sizeof(struct sembuf) == 3 * sizeof(short). */
2179 const int nsops = opsize / (3 * sizeof (short));
2181 short * const ops = (short *) opbuf;
2183 struct sembuf *temps, *t;
2186 Newx (temps, nsops, struct sembuf);
2194 result = semop(id, temps, nsops);
2208 Perl_croak(aTHX_ "semop not implemented");
2213 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
2217 struct shmid_ds shmds;
2218 const I32 id = SvIVx(*++mark);
2219 SV * const mstr = *++mark;
2220 const I32 mpos = SvIVx(*++mark);
2221 const I32 msize = SvIVx(*++mark);
2222 PERL_UNUSED_ARG(sp);
2225 if (shmctl(id, IPC_STAT, &shmds) == -1)
2227 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
2228 SETERRNO(EFAULT,SS_ACCVIO); /* can't do as caller requested */
2231 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
2232 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2234 if (optype == OP_SHMREAD) {
2236 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2238 sv_setpvn(mstr, "", 0);
2239 SvPV_force_nolen(mstr);
2240 mbuf = SvGROW(mstr, msize+1);
2242 Copy(shm + mpos, mbuf, msize, char);
2243 SvCUR_set(mstr, msize);
2244 *SvEND(mstr) = '\0';
2246 #ifndef INCOMPLETE_TAINTS
2247 /* who knows who has been playing with this shared memory? */
2255 const char *mbuf = SvPV_const(mstr, len);
2256 if ((n = len) > msize)
2258 Copy(mbuf, shm + mpos, n, char);
2260 memzero(shm + mpos + n, msize - n);
2264 Perl_croak(aTHX_ "shm I/O not implemented");
2268 #endif /* SYSV IPC */
2273 =for apidoc start_glob
2275 Function called by C<do_readline> to spawn a glob (or do the glob inside
2276 perl on VMS). This code used to be inline, but now perl uses C<File::Glob>
2277 this glob starter is only used by miniperl during the build process.
2278 Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up.
2284 Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
2287 SV * const tmpcmd = NEWSV(55, 0);
2291 #ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
2292 /* since spawning off a process is a real performance hit */
2294 #include <descrip.h>
2295 #include <lib$routines.h>
2298 char rslt[NAM$C_MAXRSS+1+sizeof(unsigned short int)] = {'\0','\0'};
2299 char vmsspec[NAM$C_MAXRSS+1];
2300 char * const rstr = rslt + sizeof(unsigned short int);
2301 char *begin, *end, *cp;
2302 $DESCRIPTOR(dfltdsc,"SYS$DISK:[]*.*;");
2305 struct dsc$descriptor_s wilddsc
2306 = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
2307 struct dsc$descriptor_vs rsdsc
2308 = {sizeof rslt, DSC$K_DTYPE_VT, DSC$K_CLASS_VS, rslt};
2309 unsigned long int cxt = 0, sts = 0, ok = 1, hasdir = 0, hasver = 0, isunix = 0;
2311 /* We could find out if there's an explicit dev/dir or version
2312 by peeking into lib$find_file's internal context at
2313 ((struct NAM *)((struct FAB *)cxt)->fab$l_nam)->nam$l_fnb
2314 but that's unsupported, so I don't want to do it now and
2315 have it bite someone in the future. */
2316 cp = SvPV(tmpglob,i);
2318 if (cp[i] == ';') hasver = 1;
2320 if (sts) hasver = 1;
2324 hasdir = isunix = 1;
2327 if (cp[i] == ']' || cp[i] == '>' || cp[i] == ':') {
2332 if ((tmpfp = PerlIO_tmpfile()) != NULL) {
2334 if (!PerlLIO_stat(SvPVX_const(tmpglob),&st) && S_ISDIR(st.st_mode))
2335 ok = ((wilddsc.dsc$a_pointer = tovmspath(SvPVX(tmpglob),vmsspec)) != NULL);
2336 else ok = ((wilddsc.dsc$a_pointer = tovmsspec(SvPVX(tmpglob),vmsspec)) != NULL);
2337 if (ok) wilddsc.dsc$w_length = (unsigned short int) strlen(wilddsc.dsc$a_pointer);
2338 for (cp=wilddsc.dsc$a_pointer; ok && cp && *cp; cp++)
2339 if (*cp == '?') *cp = '%'; /* VMS style single-char wildcard */
2340 while (ok && ((sts = lib$find_file(&wilddsc,&rsdsc,&cxt,
2341 &dfltdsc,NULL,NULL,NULL))&1)) {
2342 /* with varying string, 1st word of buffer contains result length */
2343 end = rstr + *((unsigned short int*)rslt);
2344 if (!hasver) while (*end != ';' && end > rstr) end--;
2345 *(end++) = '\n'; *end = '\0';
2346 for (cp = rstr; *cp; cp++) *cp = _tolower(*cp);
2348 if (isunix) trim_unixpath(rstr,SvPVX(tmpglob),1);
2353 while (*(--begin) != ']' && *begin != '>') ;
2356 ok = (PerlIO_puts(tmpfp,begin) != EOF);
2358 if (cxt) (void)lib$find_file_end(&cxt);
2359 if (ok && sts != RMS$_NMF &&
2360 sts != RMS$_DNF && sts != RMS_FNF) ok = 0;
2363 SETERRNO((sts == RMS$_SYN ? EINVAL : EVMSERR),sts);
2365 PerlIO_close(tmpfp);
2369 PerlIO_rewind(tmpfp);
2370 IoTYPE(io) = IoTYPE_RDONLY;
2371 IoIFP(io) = fp = tmpfp;
2372 IoFLAGS(io) &= ~IOf_UNTAINT; /* maybe redundant */
2377 #ifdef MACOS_TRADITIONAL
2378 sv_setpv(tmpcmd, "glob ");
2379 sv_catsv(tmpcmd, tmpglob);
2380 sv_catpv(tmpcmd, " |");
2384 sv_setpv(tmpcmd, "for a in ");
2385 sv_catsv(tmpcmd, tmpglob);
2386 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
2389 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
2390 sv_catsv(tmpcmd, tmpglob);
2392 sv_setpv(tmpcmd, "perlglob ");
2393 sv_catsv(tmpcmd, tmpglob);
2394 sv_catpv(tmpcmd, " |");
2399 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
2400 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
2401 sv_catsv(tmpcmd, tmpglob);
2402 sv_catpv(tmpcmd, "' 2>/dev/null |");
2404 sv_setpv(tmpcmd, "echo ");
2405 sv_catsv(tmpcmd, tmpglob);
2407 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
2409 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
2412 #endif /* !DOSISH */
2413 #endif /* MACOS_TRADITIONAL */
2414 (void)do_open(PL_last_in_gv, (char*)SvPVX_const(tmpcmd), SvCUR(tmpcmd),
2415 FALSE, O_RDONLY, 0, Nullfp);
2424 * c-indentation-style: bsd
2426 * indent-tabs-mode: t
2429 * ex: set ts=8 sts=4 sw=4 noet: