3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 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 PERL_ARGS_ASSERT_DO_OPENN;
84 Zero(mode,sizeof(mode),char);
85 PL_forkprocess = 1; /* assume true if no fork */
87 /* Collect default raw/crlf info from the op */
88 if (PL_op && PL_op->op_type == OP_OPEN) {
89 /* set up IO layers */
90 const U8 flags = PL_op->op_private;
91 in_raw = (flags & OPpOPEN_IN_RAW);
92 in_crlf = (flags & OPpOPEN_IN_CRLF);
93 out_raw = (flags & OPpOPEN_OUT_RAW);
94 out_crlf = (flags & OPpOPEN_OUT_CRLF);
97 /* If currently open - close before we re-open */
99 fd = PerlIO_fileno(IoIFP(io));
100 if (IoTYPE(io) == IoTYPE_STD) {
101 /* This is a clone of one of STD* handles */
104 else if (fd >= 0 && fd <= PL_maxsysfd) {
105 /* This is one of the original STD* handles */
108 savetype = IoTYPE(io);
112 else if (IoTYPE(io) == IoTYPE_PIPE)
113 result = PerlProc_pclose(IoIFP(io));
114 else if (IoIFP(io) != IoOFP(io)) {
116 result = PerlIO_close(IoOFP(io));
117 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
120 result = PerlIO_close(IoIFP(io));
123 result = PerlIO_close(IoIFP(io));
124 if (result == EOF && fd > PL_maxsysfd) {
125 /* Why is this not Perl_warn*() call ? */
126 PerlIO_printf(Perl_error_log,
127 "Warning: unable to close filehandle %s properly.\n",
130 IoOFP(io) = IoIFP(io) = NULL;
134 /* sysopen style args, i.e. integer mode and permissions */
136 const int appendtrunc =
138 #ifdef O_APPEND /* Not fully portable. */
141 #ifdef O_TRUNC /* Not fully portable. */
145 const int modifyingmode = O_WRONLY|O_RDWR|O_CREAT|appendtrunc;
149 Perl_croak(aTHX_ "panic: sysopen with multiple args");
157 It might be (in OS/390 and Mac OS Classic it is)
163 This means that simple & with O_RDWR would look
164 like O_RDONLY is present. Therefore we have to
167 if ((ismodifying = (rawmode & modifyingmode))) {
168 if ((ismodifying & O_WRONLY) == O_WRONLY ||
169 (ismodifying & O_RDWR) == O_RDWR ||
170 (ismodifying & (O_CREAT|appendtrunc)))
171 TAINT_PROPER("sysopen");
173 mode[ix++] = IoTYPE_NUMERIC; /* Marker to openn to use numeric "sysopen" */
175 #if defined(USE_64_BIT_RAWIO) && defined(O_LARGEFILE)
176 rawmode |= O_LARGEFILE; /* Transparently largefiley. */
179 IoTYPE(io) = PerlIO_intmode2str(rawmode, &mode[ix], &writing);
181 namesv = newSVpvn_flags(oname, len, SVs_TEMP);
185 fp = PerlIO_openn(aTHX_ type, mode, -1, rawmode, rawperm, NULL, num_svs, svp);
188 /* Regular (non-sys) open */
194 type = savepvn(oname, len);
198 /* Lose leading and trailing white space */
199 while (isSPACE(*type))
201 while (tend > type && isSPACE(tend[-1]))
205 /* New style explicit name, type is just mode and layer info */
207 if (SvROK(*svp) && !strchr(oname,'&')) {
209 Perl_warner(aTHX_ packWARN(WARN_IO),
210 "Can't open a reference");
211 SETERRNO(EINVAL, LIB_INVARG);
214 #endif /* USE_STDIO */
215 name = SvOK(*svp) ? savesvpv (*svp) : savepvn ("", 0);
223 if ((*type == IoTYPE_RDWR) && /* scary */
224 (*(type+1) == IoTYPE_RDONLY || *(type+1) == IoTYPE_WRONLY) &&
225 ((!num_svs || (tend > type+1 && tend[-1] != IoTYPE_PIPE)))) {
226 TAINT_PROPER("open");
231 if (*type == IoTYPE_PIPE) {
233 if (type[1] != IoTYPE_STD) {
235 Perl_croak(aTHX_ "Unknown open() mode '%.*s'", (int)olen, oname);
241 } while (isSPACE(*type));
247 /* command is missing 19990114 */
248 if (ckWARN(WARN_PIPE))
249 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open");
253 if (!(*name == '-' && name[1] == '\0') || num_svs)
255 TAINT_PROPER("piped open");
256 if (!num_svs && name[len-1] == '|') {
258 if (ckWARN(WARN_PIPE))
259 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Can't open bidirectional pipe");
268 fp = PerlProc_popen_list(mode, num_svs, svp);
271 fp = PerlProc_popen(name,mode);
275 if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) {
281 else if (*type == IoTYPE_WRONLY) {
282 TAINT_PROPER("open");
284 if (*type == IoTYPE_WRONLY) {
285 /* Two IoTYPE_WRONLYs in a row make for an IoTYPE_APPEND. */
286 mode[0] = IoTYPE(io) = IoTYPE_APPEND;
300 dodup = PERLIO_DUP_FD;
306 if (!num_svs && !*type && supplied_fp) {
307 /* "<+&" etc. is used by typemaps */
311 PerlIO *that_fp = NULL;
313 Perl_croak(aTHX_ "More than one argument to '%c&' open",IoTYPE(io));
315 while (isSPACE(*type))
317 if (num_svs && (SvIOK(*svp) || (SvPOK(*svp) && looks_like_number(*svp)))) {
321 else if (isDIGIT(*type)) {
327 thatio = sv_2io(*svp);
330 GV * const thatgv = gv_fetchpvn_flags(type, tend - type,
332 thatio = GvIO(thatgv);
336 SETERRNO(EINVAL,SS_IVCHAN);
340 if ((that_fp = IoIFP(thatio))) {
341 /* Flush stdio buffer before dup. --mjd
342 * Unfortunately SEEK_CURing 0 seems to
343 * be optimized away on most platforms;
344 * only Solaris and Linux seem to flush
347 /* sfio fails to clear error on next
348 sfwrite, contrary to documentation.
350 if (PerlIO_seek(that_fp, 0, SEEK_CUR) == -1)
351 PerlIO_clearerr(that_fp);
353 /* On the other hand, do all platforms
354 * take gracefully to flushing a read-only
355 * filehandle? Perhaps we should do
356 * fsetpos(src)+fgetpos(dst)? --nik */
357 PerlIO_flush(that_fp);
358 fd = PerlIO_fileno(that_fp);
359 /* When dup()ing STDIN, STDOUT or STDERR
360 * explicitly set appropriate access mode */
361 if (that_fp == PerlIO_stdout()
362 || that_fp == PerlIO_stderr())
363 IoTYPE(io) = IoTYPE_WRONLY;
364 else if (that_fp == PerlIO_stdin())
365 IoTYPE(io) = IoTYPE_RDONLY;
366 /* When dup()ing a socket, say result is
368 else if (IoTYPE(thatio) == IoTYPE_SOCKET)
369 IoTYPE(io) = IoTYPE_SOCKET;
377 fp = PerlIO_fdupopen(aTHX_ that_fp, NULL, dodup);
381 fd = PerlLIO_dup(fd);
384 if (!(fp = PerlIO_openn(aTHX_ type,mode,fd,0,0,NULL,num_svs,svp))) {
385 if (dodup && fd >= 0)
392 while (isSPACE(*type))
394 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
396 fp = PerlIO_stdout();
397 IoTYPE(io) = IoTYPE_STD;
399 Perl_croak(aTHX_ "More than one argument to '>%c' open",IoTYPE_STD);
404 namesv = newSVpvn_flags(type, tend - type, SVs_TEMP);
409 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
412 if (!fp && type && *type && *type != ':' && !isIDFIRST(*type))
413 goto unknown_open_mode;
414 } /* IoTYPE_WRONLY */
415 else if (*type == IoTYPE_RDONLY) {
418 } while (isSPACE(*type));
427 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
430 IoTYPE(io) = IoTYPE_STD;
432 Perl_croak(aTHX_ "More than one argument to '<%c' open",IoTYPE_STD);
437 namesv = newSVpvn_flags(type, tend - type, SVs_TEMP);
442 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
444 if (!fp && type && *type && *type != ':' && !isIDFIRST(*type))
445 goto unknown_open_mode;
446 } /* IoTYPE_RDONLY */
447 else if ((num_svs && /* '-|...' or '...|' */
448 type[0] == IoTYPE_STD && type[1] == IoTYPE_PIPE) ||
449 (!num_svs && tend > type+1 && tend[-1] == IoTYPE_PIPE)) {
451 type += 2; /* skip over '-|' */
455 while (tend > type && isSPACE(tend[-1]))
457 for (; isSPACE(*type); type++)
463 /* command is missing 19990114 */
464 if (ckWARN(WARN_PIPE))
465 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open");
469 if (!(*name == '-' && name[1] == '\0') || num_svs)
471 TAINT_PROPER("piped open");
480 fp = PerlProc_popen_list(mode,num_svs,svp);
483 fp = PerlProc_popen(name,mode);
485 IoTYPE(io) = IoTYPE_PIPE;
487 while (isSPACE(*type))
490 if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) {
496 else { /* layer(Args) */
498 goto unknown_open_mode;
500 IoTYPE(io) = IoTYPE_RDONLY;
501 for (; isSPACE(*name); name++)
510 if (*name == '-' && name[1] == '\0') {
512 IoTYPE(io) = IoTYPE_STD;
516 namesv = newSVpvn_flags(type, tend - type, SVs_TEMP);
521 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
526 if (IoTYPE(io) == IoTYPE_RDONLY && ckWARN(WARN_NEWLINE)
527 && strchr(oname, '\n')
530 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
534 if (ckWARN(WARN_IO)) {
535 if ((IoTYPE(io) == IoTYPE_RDONLY) &&
536 (fp == PerlIO_stdout() || fp == PerlIO_stderr())) {
537 Perl_warner(aTHX_ packWARN(WARN_IO),
538 "Filehandle STD%s reopened as %s only for input",
539 ((fp == PerlIO_stdout()) ? "OUT" : "ERR"),
542 else if ((IoTYPE(io) == IoTYPE_WRONLY) && fp == PerlIO_stdin()) {
543 Perl_warner(aTHX_ packWARN(WARN_IO),
544 "Filehandle STDIN reopened as %s only for output",
549 fd = PerlIO_fileno(fp);
550 /* If there is no fd (e.g. PerlIO::scalar) assume it isn't a
551 * socket - this covers PerlIO::scalar - otherwise unless we "know" the
552 * type probe for socket-ness.
554 if (IoTYPE(io) && IoTYPE(io) != IoTYPE_PIPE && IoTYPE(io) != IoTYPE_STD && fd >= 0) {
555 if (PerlLIO_fstat(fd,&PL_statbuf) < 0) {
556 /* If PerlIO claims to have fd we had better be able to fstat() it. */
557 (void) PerlIO_close(fp);
561 if (S_ISSOCK(PL_statbuf.st_mode))
562 IoTYPE(io) = IoTYPE_SOCKET; /* in case a socket was passed in to us */
566 !(PL_statbuf.st_mode & S_IFMT)
570 && IoTYPE(io) != IoTYPE_WRONLY /* Dups of STD* filehandles already have */
571 && IoTYPE(io) != IoTYPE_RDONLY /* type so they aren't marked as sockets */
572 ) { /* on OS's that return 0 on fstat()ed pipe */
574 Sock_size_t buflen = sizeof tmpbuf;
575 if (PerlSock_getsockname(fd, (struct sockaddr *)tmpbuf, &buflen) >= 0
576 || errno != ENOTSOCK)
577 IoTYPE(io) = IoTYPE_SOCKET; /* some OS's return 0 on fstat()ed socket */
578 /* but some return 0 for streams too, sigh */
580 #endif /* HAS_SOCKET */
581 #endif /* !PERL_MICRO */
585 * If this is a standard handle we discard all the layer stuff
586 * and just dup the fd into whatever was on the handle before !
589 if (saveifp) { /* must use old fp? */
590 /* If fd is less that PL_maxsysfd i.e. STDIN..STDERR
591 then dup the new fileno down
594 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
595 if (saveofp != saveifp) { /* was a socket? */
596 PerlIO_close(saveofp);
600 /* Still a small can-of-worms here if (say) PerlIO::scalar
601 is assigned to (say) STDOUT - for now let dup2() fail
602 and provide the error
604 if (PerlLIO_dup2(fd, savefd) < 0) {
605 (void)PerlIO_close(fp);
609 if (savefd != PerlIO_fileno(PerlIO_stdin())) {
610 char newname[FILENAME_MAX+1];
611 if (PerlIO_getname(fp, newname)) {
612 if (fd == PerlIO_fileno(PerlIO_stdout()))
613 Perl_vmssetuserlnm(aTHX_ "SYS$OUTPUT", newname);
614 if (fd == PerlIO_fileno(PerlIO_stderr()))
615 Perl_vmssetuserlnm(aTHX_ "SYS$ERROR", newname);
621 /* PL_fdpid isn't used on Windows, so avoid this useless work.
622 * XXX Probably the same for a lot of other places. */
628 sv = *av_fetch(PL_fdpid,fd,TRUE);
629 SvUPGRADE(sv, SVt_IV);
632 sv = *av_fetch(PL_fdpid,savefd,TRUE);
633 SvUPGRADE(sv, SVt_IV);
640 /* need to close fp without closing underlying fd */
641 int ofd = PerlIO_fileno(fp);
642 int dupfd = PerlLIO_dup(ofd);
643 #if defined(HAS_FCNTL) && defined(F_SETFD)
644 /* Assume if we have F_SETFD we have F_GETFD */
645 int coe = fcntl(ofd,F_GETFD);
648 PerlLIO_dup2(dupfd,ofd);
649 #if defined(HAS_FCNTL) && defined(F_SETFD)
650 /* The dup trick has lost close-on-exec on ofd */
651 fcntl(ofd,F_SETFD, coe);
653 PerlLIO_close(dupfd);
660 fd = PerlIO_fileno(fp);
662 #if defined(HAS_FCNTL) && defined(F_SETFD)
664 const int save_errno = errno;
665 fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
671 IoFLAGS(io) &= ~IOf_NOLINE;
673 if (IoTYPE(io) == IoTYPE_SOCKET
674 || (IoTYPE(io) == IoTYPE_WRONLY && fd >= 0 && S_ISCHR(PL_statbuf.st_mode)) ) {
676 if (*s == IoTYPE_IMPLICIT || *s == IoTYPE_NUMERIC)
679 if (!(IoOFP(io) = PerlIO_openn(aTHX_ type,s,fd,0,0,NULL,0,svp))) {
693 IoTYPE(io) = savetype;
698 Perl_nextargv(pTHX_ register GV *gv)
702 #ifndef FLEXFILENAMES
708 IO * const io = GvIOp(gv);
710 PERL_ARGS_ASSERT_NEXTARGV;
713 PL_argvoutgv = gv_fetchpvs("ARGVOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
714 if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
715 IoFLAGS(io) &= ~IOf_START;
718 Perl_av_create_and_push(aTHX_ &PL_argvout_stack,
719 SvREFCNT_inc_simple_NN(PL_defoutgv));
722 if (PL_filemode & (S_ISUID|S_ISGID)) {
723 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
726 (void)fchmod(PL_lastfd,PL_filemode);
728 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
735 while (av_len(GvAV(gv)) >= 0) {
737 sv = av_shift(GvAV(gv));
739 sv_setsv(GvSVn(gv),sv);
740 SvSETMAGIC(GvSV(gv));
741 PL_oldname = SvPVx(GvSV(gv), oldlen);
742 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,NULL)) {
744 TAINT_PROPER("inplace open");
745 if (oldlen == 1 && *PL_oldname == '-') {
746 setdefout(gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL,
748 return IoIFP(GvIOp(gv));
750 #ifndef FLEXFILENAMES
751 filedev = PL_statbuf.st_dev;
752 fileino = PL_statbuf.st_ino;
754 PL_filemode = PL_statbuf.st_mode;
755 fileuid = PL_statbuf.st_uid;
756 filegid = PL_statbuf.st_gid;
757 if (!S_ISREG(PL_filemode)) {
758 if (ckWARN_d(WARN_INPLACE))
759 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
760 "Can't do inplace edit: %s is not a regular file",
766 const char *star = strchr(PL_inplace, '*');
768 const char *begin = PL_inplace;
769 sv_setpvn(sv, "", 0);
771 sv_catpvn(sv, begin, star - begin);
772 sv_catpvn(sv, PL_oldname, oldlen);
774 } while ((star = strchr(begin, '*')));
779 sv_catpv(sv,PL_inplace);
781 #ifndef FLEXFILENAMES
782 if ((PerlLIO_stat(SvPVX_const(sv),&PL_statbuf) >= 0
783 && PL_statbuf.st_dev == filedev
784 && PL_statbuf.st_ino == fileino)
786 || ((_djstat_fail_bits & _STFAIL_TRUENAME)!=0)
790 if (ckWARN_d(WARN_INPLACE))
791 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
792 "Can't do inplace edit: %"SVf" would not be unique",
799 #if !defined(DOSISH) && !defined(__CYGWIN__) && !defined(EPOC)
800 if (PerlLIO_rename(PL_oldname,SvPVX_const(sv)) < 0) {
801 if (ckWARN_d(WARN_INPLACE))
802 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
803 "Can't rename %s to %"SVf": %s, skipping file",
804 PL_oldname, SVfARG(sv), Strerror(errno));
810 (void)PerlLIO_unlink(SvPVX_const(sv));
811 (void)PerlLIO_rename(PL_oldname,SvPVX_const(sv));
812 do_open(gv,(char*)SvPVX_const(sv),SvCUR(sv),PL_inplace!=0,
816 (void)UNLINK(SvPVX_const(sv));
817 if (link(PL_oldname,SvPVX_const(sv)) < 0) {
818 if (ckWARN_d(WARN_INPLACE))
819 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
820 "Can't rename %s to %"SVf": %s, skipping file",
821 PL_oldname, SVfARG(sv), Strerror(errno) );
825 (void)UNLINK(PL_oldname);
829 #if !defined(DOSISH) && !defined(AMIGAOS)
830 # ifndef VMS /* Don't delete; use automatic file versioning */
831 if (UNLINK(PL_oldname) < 0) {
832 if (ckWARN_d(WARN_INPLACE))
833 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
834 "Can't remove %s: %s, skipping file",
835 PL_oldname, Strerror(errno) );
841 Perl_croak(aTHX_ "Can't do inplace edit without backup");
845 sv_setpvn(sv,">",!PL_inplace);
846 sv_catpvn(sv,PL_oldname,oldlen);
847 SETERRNO(0,0); /* in case sprintf set errno */
849 if (!do_open(PL_argvoutgv,(char*)SvPVX_const(sv),SvCUR(sv),
850 PL_inplace!=0,O_WRONLY|O_CREAT|O_TRUNC,0,NULL))
852 if (!do_open(PL_argvoutgv,(char*)SvPVX_const(sv),SvCUR(sv),
853 PL_inplace!=0,O_WRONLY|O_CREAT|OPEN_EXCL,0666,
857 if (ckWARN_d(WARN_INPLACE))
858 Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't do inplace edit on %s: %s",
859 PL_oldname, Strerror(errno) );
863 setdefout(PL_argvoutgv);
864 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
865 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
867 (void)fchmod(PL_lastfd,PL_filemode);
869 # if !(defined(WIN32) && defined(__BORLANDC__))
870 /* Borland runtime creates a readonly file! */
871 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
874 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
876 (void)fchown(PL_lastfd,fileuid,filegid);
879 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
884 return IoIFP(GvIOp(gv));
887 if (ckWARN_d(WARN_INPLACE)) {
888 const int eno = errno;
889 if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0
890 && !S_ISREG(PL_statbuf.st_mode))
892 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
893 "Can't do inplace edit: %s is not a regular file",
897 Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't open %s: %s",
898 PL_oldname, Strerror(eno));
902 if (io && (IoFLAGS(io) & IOf_ARGV))
903 IoFLAGS(io) |= IOf_START;
905 (void)do_close(PL_argvoutgv,FALSE);
906 if (io && (IoFLAGS(io) & IOf_ARGV)
907 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
909 GV * const oldout = (GV*)av_pop(PL_argvout_stack);
911 SvREFCNT_dec(oldout);
914 setdefout(gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO));
919 /* explicit renamed to avoid C++ conflict -- kja */
921 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
929 if (!gv || !isGV_with_GP(gv)) {
931 SETERRNO(EBADF,SS_IVCHAN);
935 if (!io) { /* never opened */
937 if (ckWARN(WARN_UNOPENED)) /* no check for closed here */
938 report_evil_fh(gv, io, PL_op->op_type);
939 SETERRNO(EBADF,SS_IVCHAN);
943 retval = io_close(io, not_implicit);
947 IoLINES_LEFT(io) = IoPAGE_LEN(io);
949 IoTYPE(io) = IoTYPE_CLOSED;
954 Perl_io_close(pTHX_ IO *io, bool not_implicit)
959 PERL_ARGS_ASSERT_IO_CLOSE;
962 if (IoTYPE(io) == IoTYPE_PIPE) {
963 const int status = PerlProc_pclose(IoIFP(io));
965 STATUS_NATIVE_CHILD_SET(status);
966 retval = (STATUS_UNIX == 0);
969 retval = (status != -1);
972 else if (IoTYPE(io) == IoTYPE_STD)
975 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
976 const bool prev_err = PerlIO_error(IoOFP(io));
977 retval = (PerlIO_close(IoOFP(io)) != EOF && !prev_err);
978 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
981 const bool prev_err = PerlIO_error(IoIFP(io));
982 retval = (PerlIO_close(IoIFP(io)) != EOF && !prev_err);
985 IoOFP(io) = IoIFP(io) = NULL;
987 else if (not_implicit) {
988 SETERRNO(EBADF,SS_IVCHAN);
995 Perl_do_eof(pTHX_ GV *gv)
998 register IO * const io = GvIO(gv);
1000 PERL_ARGS_ASSERT_DO_EOF;
1004 else if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
1005 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1008 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
1009 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
1010 return FALSE; /* this is the most usual case */
1014 /* getc and ungetc can stomp on errno */
1015 const int saverrno = errno;
1016 const int ch = PerlIO_getc(IoIFP(io));
1018 (void)PerlIO_ungetc(IoIFP(io),ch);
1025 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
1026 if (PerlIO_get_cnt(IoIFP(io)) < -1)
1027 PerlIO_set_cnt(IoIFP(io),-1);
1029 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
1030 if (gv != PL_argvgv || !nextargv(gv)) /* get another fp handy */
1034 return TRUE; /* normal fp, definitely end of file */
1040 Perl_do_tell(pTHX_ GV *gv)
1043 register IO *io = NULL;
1044 register PerlIO *fp;
1046 PERL_ARGS_ASSERT_DO_TELL;
1048 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1049 #ifdef ULTRIX_STDIO_BOTCH
1051 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1053 return PerlIO_tell(fp);
1055 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1056 report_evil_fh(gv, io, PL_op->op_type);
1057 SETERRNO(EBADF,RMS_IFI);
1062 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
1065 register IO *io = NULL;
1066 register PerlIO *fp;
1068 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1069 #ifdef ULTRIX_STDIO_BOTCH
1071 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1073 return PerlIO_seek(fp, pos, whence) >= 0;
1075 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1076 report_evil_fh(gv, io, PL_op->op_type);
1077 SETERRNO(EBADF,RMS_IFI);
1082 Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
1085 register IO *io = NULL;
1086 register PerlIO *fp;
1088 PERL_ARGS_ASSERT_DO_SYSSEEK;
1090 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
1091 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
1092 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1093 report_evil_fh(gv, io, PL_op->op_type);
1094 SETERRNO(EBADF,RMS_IFI);
1099 Perl_mode_from_discipline(pTHX_ const char *s, STRLEN len)
1101 int mode = O_BINARY;
1107 if (s[2] == 'a' && s[3] == 'w'
1108 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1117 if (s[2] == 'r' && s[3] == 'l' && s[4] == 'f'
1118 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1127 goto fail_discipline;
1130 else if (isSPACE(*s)) {
1137 end = strchr(s+1, ':');
1140 #ifndef PERLIO_LAYERS
1141 Perl_croak(aTHX_ "IO layers (like '%.*s') unavailable", end-s, s);
1152 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE)
1154 my_chsize(int fd, Off_t length)
1157 /* code courtesy of William Kucharski */
1162 if (PerlLIO_fstat(fd, &filebuf) < 0)
1165 if (filebuf.st_size < length) {
1167 /* extend file length */
1169 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
1172 /* write a "0" byte */
1174 if ((PerlLIO_write(fd, "", 1)) != 1)
1178 /* truncate length */
1182 fl.l_start = length;
1183 fl.l_type = F_WRLCK; /* write lock on file space */
1186 * This relies on the UNDOCUMENTED F_FREESP argument to
1187 * fcntl(2), which truncates the file so that it ends at the
1188 * position indicated by fl.l_start.
1190 * Will minor miracles never cease?
1193 if (fcntl(fd, F_FREESP, &fl) < 0)
1199 Perl_croak_nocontext("truncate not implemented");
1200 #endif /* F_FREESP */
1203 #endif /* !HAS_TRUNCATE && !HAS_CHSIZE */
1206 Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
1210 PERL_ARGS_ASSERT_DO_PRINT;
1212 /* assuming fp is checked earlier */
1215 if (SvTYPE(sv) == SVt_IV && SvIOK(sv)) {
1216 assert(!SvGMAGICAL(sv));
1218 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
1220 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
1221 return !PerlIO_error(fp);
1225 /* Do this first to trigger any overloading. */
1226 const char *tmps = SvPV_const(sv, len);
1230 if (PerlIO_isutf8(fp)) {
1232 /* We don't modify the original scalar. */
1233 tmpbuf = bytes_to_utf8((const U8*) tmps, &len);
1234 tmps = (char *) tmpbuf;
1237 else if (DO_UTF8(sv)) {
1238 STRLEN tmplen = len;
1240 U8 * const result = bytes_from_utf8((const U8*) tmps, &tmplen, &utf8);
1243 tmps = (char *) tmpbuf;
1247 assert((char *)result == tmps);
1248 if (ckWARN_d(WARN_UTF8)) {
1249 Perl_warner(aTHX_ packWARN(WARN_UTF8),
1250 "Wide character in print");
1254 /* To detect whether the process is about to overstep its
1255 * filesize limit we would need getrlimit(). We could then
1256 * also transparently raise the limit with setrlimit() --
1257 * but only until the system hard limit/the filesystem limit,
1258 * at which we would get EPERM. Note that when using buffered
1259 * io the write failure can be delayed until the flush/close. --jhi */
1260 if (len && (PerlIO_write(fp,tmps,len) == 0))
1263 return happy ? !PerlIO_error(fp) : FALSE;
1275 if (PL_op->op_flags & OPf_REF) {
1280 return PL_laststatval;
1283 PL_laststype = OP_STAT;
1285 sv_setpvn(PL_statname, "", 0);
1288 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
1289 } else if (IoDIRP(io)) {
1290 return (PL_laststatval = PerlLIO_fstat(my_dirfd(IoDIRP(io)), &PL_statcache));
1292 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1293 report_evil_fh(gv, io, PL_op->op_type);
1294 return (PL_laststatval = -1);
1297 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1298 report_evil_fh(gv, io, PL_op->op_type);
1299 return (PL_laststatval = -1);
1302 else if (PL_op->op_private & OPpFT_STACKED) {
1303 return PL_laststatval;
1306 SV* const sv = POPs;
1310 if (isGV_with_GP(sv)) {
1314 else if (SvROK(sv) && isGV_with_GP(SvRV(sv))) {
1318 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
1321 goto do_fstat_have_io;
1324 s = SvPV_const(sv, len);
1326 sv_setpvn(PL_statname, s, len);
1327 s = SvPVX_const(PL_statname); /* s now NUL-terminated */
1328 PL_laststype = OP_STAT;
1329 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1330 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
1331 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
1332 return PL_laststatval;
1341 static const char no_prev_lstat[] = "The stat preceding -l _ wasn't an lstat";
1345 if (PL_op->op_flags & OPf_REF) {
1347 if (cGVOP_gv == PL_defgv) {
1348 if (PL_laststype != OP_LSTAT)
1349 Perl_croak(aTHX_ no_prev_lstat);
1350 return PL_laststatval;
1352 if (ckWARN(WARN_IO)) {
1353 Perl_warner(aTHX_ packWARN(WARN_IO), "Use of -l on filehandle %s",
1355 return (PL_laststatval = -1);
1358 else if (PL_laststype != OP_LSTAT
1359 && (PL_op->op_private & OPpFT_STACKED) && ckWARN(WARN_IO))
1360 Perl_croak(aTHX_ no_prev_lstat);
1362 PL_laststype = OP_LSTAT;
1366 if (SvROK(sv) && isGV_with_GP(SvRV(sv)) && ckWARN(WARN_IO)) {
1367 Perl_warner(aTHX_ packWARN(WARN_IO), "Use of -l on filehandle %s",
1368 GvENAME((GV*) SvRV(sv)));
1369 return (PL_laststatval = -1);
1371 file = SvPV_nolen_const(sv);
1372 sv_setpv(PL_statname,file);
1373 PL_laststatval = PerlLIO_lstat(file,&PL_statcache);
1374 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(file, '\n'))
1375 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "lstat");
1376 return PL_laststatval;
1380 S_exec_failed(pTHX_ const char *cmd, int fd, int do_report)
1382 const int e = errno;
1383 PERL_ARGS_ASSERT_EXEC_FAILED;
1384 if (ckWARN(WARN_EXEC))
1385 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
1388 PerlLIO_write(fd, (void*)&e, sizeof(int));
1394 Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1395 int fd, int do_report)
1398 PERL_ARGS_ASSERT_DO_AEXEC5;
1399 #if defined(MACOS_TRADITIONAL) || defined(__SYMBIAN32__) || defined(__LIBCATAMOUNT__)
1400 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1404 const char *tmps = NULL;
1405 Newx(PL_Argv, sp - mark + 1, const char*);
1408 while (++mark <= sp) {
1410 *a++ = SvPV_nolen_const(*mark);
1416 tmps = SvPV_nolen_const(really);
1417 if ((!really && *PL_Argv[0] != '/') ||
1418 (really && *tmps != '/')) /* will execvp use PATH? */
1419 TAINT_ENV(); /* testing IFS here is overkill, probably */
1421 if (really && *tmps)
1422 PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
1424 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1426 S_exec_failed(aTHX_ (really ? tmps : PL_Argv[0]), fd, do_report);
1434 Perl_do_execfree(pTHX)
1443 #ifdef PERL_DEFAULT_DO_EXEC3_IMPLEMENTATION
1446 Perl_do_exec3(pTHX_ const char *incmd, int fd, int do_report)
1449 register const char **a;
1453 /* Make a copy so we can change it */
1454 const Size_t cmdlen = strlen(incmd) + 1;
1456 PERL_ARGS_ASSERT_DO_EXEC3;
1458 Newx(buf, cmdlen, char);
1460 memcpy(cmd, incmd, cmdlen);
1462 while (*cmd && isSPACE(*cmd))
1465 /* save an extra exec if possible */
1469 char flags[PERL_FLAGS_MAX];
1470 if (strnEQ(cmd,PL_cshname,PL_cshlen) &&
1471 strnEQ(cmd+PL_cshlen," -c",3)) {
1472 my_strlcpy(flags, "-c", PERL_FLAGS_MAX);
1473 s = cmd+PL_cshlen+3;
1476 my_strlcat(flags, "f", PERL_FLAGS_MAX - 2);
1481 char * const ncmd = s;
1487 if (s[-1] == '\'') {
1490 PerlProc_execl(PL_cshname, "csh", flags, ncmd, (char*)NULL);
1493 S_exec_failed(aTHX_ PL_cshname, fd, do_report);
1502 /* see if there are shell metacharacters in it */
1504 if (*cmd == '.' && isSPACE(cmd[1]))
1507 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1512 s++; /* catch VAR=val gizmo */
1516 for (s = cmd; *s; s++) {
1517 if (*s != ' ' && !isALPHA(*s) &&
1518 strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1519 if (*s == '\n' && !s[1]) {
1523 /* handle the 2>&1 construct at the end */
1524 if (*s == '>' && s[1] == '&' && s[2] == '1'
1525 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1526 && (!s[3] || isSPACE(s[3])))
1528 const char *t = s + 3;
1530 while (*t && isSPACE(*t))
1532 if (!*t && (PerlLIO_dup2(1,2) != -1)) {
1539 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char *)NULL);
1541 S_exec_failed(aTHX_ PL_sh_path, fd, do_report);
1547 Newx(PL_Argv, (s - cmd) / 2 + 2, const char*);
1548 PL_Cmd = savepvn(cmd, s-cmd);
1550 for (s = PL_Cmd; *s;) {
1555 while (*s && !isSPACE(*s))
1563 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1565 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1569 S_exec_failed(aTHX_ PL_Argv[0], fd, do_report);
1576 #endif /* OS2 || WIN32 */
1579 Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
1583 register I32 tot = 0;
1584 const char *const what = PL_op_name[type];
1586 SV ** const oldmark = mark;
1588 PERL_ARGS_ASSERT_APPLY;
1590 /* Doing this ahead of the switch statement preserves the old behaviour,
1591 where attempting to use kill as a taint test test would fail on
1592 platforms where kill was not defined. */
1594 if (type == OP_KILL)
1595 Perl_die(aTHX_ PL_no_func, what);
1598 if (type == OP_CHOWN)
1599 Perl_die(aTHX_ PL_no_func, what);
1603 #define APPLY_TAINT_PROPER() \
1605 if (PL_tainted) { TAINT_PROPER(what); } \
1608 /* This is a first heuristic; it doesn't catch tainting magic. */
1610 while (++mark <= sp) {
1611 if (SvTAINTED(*mark)) {
1620 APPLY_TAINT_PROPER();
1623 APPLY_TAINT_PROPER();
1625 while (++mark <= sp) {
1627 if (isGV_with_GP(*mark)) {
1630 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1632 APPLY_TAINT_PROPER();
1633 if (fchmod(PerlIO_fileno(IoIFP(GvIOn(gv))), val))
1636 Perl_die(aTHX_ PL_no_func, "fchmod");
1643 else if (SvROK(*mark) && isGV_with_GP(SvRV(*mark))) {
1644 gv = (GV*)SvRV(*mark);
1648 const char *name = SvPV_nolen_const(*mark);
1649 APPLY_TAINT_PROPER();
1650 if (PerlLIO_chmod(name, val))
1658 APPLY_TAINT_PROPER();
1659 if (sp - mark > 2) {
1661 val = SvIVx(*++mark);
1662 val2 = SvIVx(*++mark);
1663 APPLY_TAINT_PROPER();
1665 while (++mark <= sp) {
1667 if (isGV_with_GP(*mark)) {
1670 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1672 APPLY_TAINT_PROPER();
1673 if (fchown(PerlIO_fileno(IoIFP(GvIOn(gv))), val, val2))
1676 Perl_die(aTHX_ PL_no_func, "fchown");
1683 else if (SvROK(*mark) && isGV_with_GP(SvRV(*mark))) {
1684 gv = (GV*)SvRV(*mark);
1688 const char *name = SvPV_nolen_const(*mark);
1689 APPLY_TAINT_PROPER();
1690 if (PerlLIO_chown(name, val, val2))
1698 XXX Should we make lchown() directly available from perl?
1699 For now, we'll let Configure test for HAS_LCHOWN, but do
1700 nothing in the core.
1705 APPLY_TAINT_PROPER();
1708 s = SvPVx_nolen_const(*++mark);
1710 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1712 if ((val = whichsig(s)) < 0)
1713 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
1717 APPLY_TAINT_PROPER();
1720 /* kill() doesn't do process groups (job trees?) under VMS */
1721 if (val < 0) val = -val;
1722 if (val == SIGKILL) {
1723 # include <starlet.h>
1724 /* Use native sys$delprc() to insure that target process is
1725 * deleted; supervisor-mode images don't pay attention to
1726 * CRTL's emulation of Unix-style signals and kill()
1728 while (++mark <= sp) {
1729 I32 proc = SvIV(*mark);
1730 register unsigned long int __vmssts;
1731 APPLY_TAINT_PROPER();
1732 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1736 case SS$_NOSUCHNODE:
1737 SETERRNO(ESRCH,__vmssts);
1740 SETERRNO(EPERM,__vmssts);
1743 SETERRNO(EVMSERR,__vmssts);
1752 while (++mark <= sp) {
1753 const I32 proc = SvIV(*mark);
1754 APPLY_TAINT_PROPER();
1756 if (PerlProc_killpg(proc,val)) /* BSD */
1758 if (PerlProc_kill(-proc,val)) /* SYSV */
1764 while (++mark <= sp) {
1765 const I32 proc = SvIV(*mark);
1766 APPLY_TAINT_PROPER();
1767 if (PerlProc_kill(proc, val))
1774 APPLY_TAINT_PROPER();
1776 while (++mark <= sp) {
1777 s = SvPV_nolen_const(*mark);
1778 APPLY_TAINT_PROPER();
1779 if (PL_euid || PL_unsafe) {
1783 else { /* don't let root wipe out directories without -U */
1784 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1793 #if defined(HAS_UTIME) || defined(HAS_FUTIMES)
1795 APPLY_TAINT_PROPER();
1796 if (sp - mark > 2) {
1797 #if defined(HAS_FUTIMES)
1798 struct timeval utbuf[2];
1799 void *utbufp = utbuf;
1800 #elif defined(I_UTIME) || defined(VMS)
1801 struct utimbuf utbuf;
1802 struct utimbuf *utbufp = &utbuf;
1808 void *utbufp = &utbuf;
1811 SV* const accessed = *++mark;
1812 SV* const modified = *++mark;
1814 /* Be like C, and if both times are undefined, let the C
1815 * library figure out what to do. This usually means
1816 * "current time". */
1818 if ( accessed == &PL_sv_undef && modified == &PL_sv_undef )
1821 Zero(&utbuf, sizeof utbuf, char);
1823 utbuf[0].tv_sec = (long)SvIV(accessed); /* time accessed */
1824 utbuf[0].tv_usec = 0;
1825 utbuf[1].tv_sec = (long)SvIV(modified); /* time modified */
1826 utbuf[1].tv_usec = 0;
1827 #elif defined(BIG_TIME)
1828 utbuf.actime = (Time_t)SvNV(accessed); /* time accessed */
1829 utbuf.modtime = (Time_t)SvNV(modified); /* time modified */
1831 utbuf.actime = (Time_t)SvIV(accessed); /* time accessed */
1832 utbuf.modtime = (Time_t)SvIV(modified); /* time modified */
1835 APPLY_TAINT_PROPER();
1837 while (++mark <= sp) {
1839 if (isGV_with_GP(*mark)) {
1842 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1844 APPLY_TAINT_PROPER();
1845 if (futimes(PerlIO_fileno(IoIFP(GvIOn(gv))),
1846 (struct timeval *) utbufp))
1849 Perl_die(aTHX_ PL_no_func, "futimes");
1856 else if (SvROK(*mark) && isGV_with_GP(SvRV(*mark))) {
1857 gv = (GV*)SvRV(*mark);
1861 const char * const name = SvPV_nolen_const(*mark);
1862 APPLY_TAINT_PROPER();
1864 if (utimes(name, (struct timeval *)utbufp))
1866 if (PerlLIO_utime(name, utbufp))
1880 #undef APPLY_TAINT_PROPER
1883 /* Do the permissions allow some operation? Assumes statcache already set. */
1884 #ifndef VMS /* VMS' cando is in vms.c */
1886 Perl_cando(pTHX_ Mode_t mode, bool effective, register const Stat_t *statbufp)
1887 /* effective is a flag, true for EUID, or for checking if the effective gid
1888 * is in the list of groups returned from getgroups().
1893 PERL_ARGS_ASSERT_CANDO;
1896 /* [Comments and code from Len Reed]
1897 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1898 * to write-protected files. The execute permission bit is set
1899 * by the Miscrosoft C library stat() function for the following:
1904 * All files and directories are readable.
1905 * Directories and special files, e.g. "CON", cannot be
1907 * [Comment by Tom Dinger -- a directory can have the write-protect
1908 * bit set in the file system, but DOS permits changes to
1909 * the directory anyway. In addition, all bets are off
1910 * here for networked software, such as Novell and
1914 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1915 * too so it will actually look into the files for magic numbers
1917 return (mode & statbufp->st_mode) ? TRUE : FALSE;
1919 #else /* ! DOSISH */
1920 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
1921 if (mode == S_IXUSR) {
1922 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1926 return TRUE; /* root reads and writes anything */
1929 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1930 if (statbufp->st_mode & mode)
1931 return TRUE; /* ok as "user" */
1933 else if (ingroup(statbufp->st_gid,effective)) {
1934 if (statbufp->st_mode & mode >> 3)
1935 return TRUE; /* ok as "group" */
1937 else if (statbufp->st_mode & mode >> 6)
1938 return TRUE; /* ok as "other" */
1940 #endif /* ! DOSISH */
1945 Perl_ingroup(pTHX_ Gid_t testgid, bool effective)
1947 #ifdef MACOS_TRADITIONAL
1948 /* This is simply not correct for AppleShare, but fix it yerself. */
1952 if (testgid == (effective ? PL_egid : PL_gid))
1954 #ifdef HAS_GETGROUPS
1956 Groups_t *gary = NULL;
1960 anum = getgroups(0, gary);
1961 Newx(gary, anum, Groups_t);
1962 anum = getgroups(anum, gary);
1964 if (gary[anum] == testgid) {
1978 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1981 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
1984 const key_t key = (key_t)SvNVx(*++mark);
1985 const I32 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1986 const I32 flags = SvIVx(*++mark);
1988 PERL_ARGS_ASSERT_DO_IPCGET;
1989 PERL_UNUSED_ARG(sp);
1996 return msgget(key, flags);
2000 return semget(key, n, flags);
2004 return shmget(key, n, flags);
2006 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
2008 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2011 return -1; /* should never happen */
2015 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
2020 const I32 id = SvIVx(*++mark);
2022 const I32 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
2024 const I32 cmd = SvIVx(*++mark);
2025 SV * const astr = *++mark;
2026 STRLEN infosize = 0;
2027 I32 getinfo = (cmd == IPC_STAT);
2029 PERL_ARGS_ASSERT_DO_IPCCTL;
2030 PERL_UNUSED_ARG(sp);
2036 if (cmd == IPC_STAT || cmd == IPC_SET)
2037 infosize = sizeof(struct msqid_ds);
2042 if (cmd == IPC_STAT || cmd == IPC_SET)
2043 infosize = sizeof(struct shmid_ds);
2049 if (cmd == IPC_STAT || cmd == IPC_SET)
2050 infosize = sizeof(struct semid_ds);
2051 else if (cmd == GETALL || cmd == SETALL)
2053 struct semid_ds semds;
2055 #ifdef EXTRA_F_IN_SEMUN_BUF
2056 semun.buff = &semds;
2060 getinfo = (cmd == GETALL);
2061 if (Semctl(id, 0, IPC_STAT, semun) == -1)
2063 infosize = semds.sem_nsems * sizeof(short);
2064 /* "short" is technically wrong but much more portable
2065 than guessing about u_?short(_t)? */
2068 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2072 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
2074 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2082 SvPV_force_nolen(astr);
2083 a = SvGROW(astr, infosize+1);
2088 a = SvPV(astr, len);
2089 if (len != infosize)
2090 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
2098 const IV i = SvIV(astr);
2099 a = INT2PTR(char *,i); /* ouch */
2106 ret = msgctl(id, cmd, (struct msqid_ds *)a);
2112 union semun unsemds;
2114 #ifdef EXTRA_F_IN_SEMUN_BUF
2115 unsemds.buff = (struct semid_ds *)a;
2117 unsemds.buf = (struct semid_ds *)a;
2119 ret = Semctl(id, n, cmd, unsemds);
2121 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2128 ret = shmctl(id, cmd, (struct shmid_ds *)a);
2132 if (getinfo && ret >= 0) {
2133 SvCUR_set(astr, infosize);
2134 *SvEND(astr) = '\0';
2141 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
2146 const I32 id = SvIVx(*++mark);
2147 SV * const mstr = *++mark;
2148 const I32 flags = SvIVx(*++mark);
2149 const char * const mbuf = SvPV_const(mstr, len);
2150 const I32 msize = len - sizeof(long);
2152 PERL_ARGS_ASSERT_DO_MSGSND;
2153 PERL_UNUSED_ARG(sp);
2156 Perl_croak(aTHX_ "Arg too short for msgsnd");
2158 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
2160 PERL_UNUSED_ARG(sp);
2161 PERL_UNUSED_ARG(mark);
2162 Perl_croak(aTHX_ "msgsnd not implemented");
2167 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
2173 I32 msize, flags, ret;
2174 const I32 id = SvIVx(*++mark);
2175 SV * const mstr = *++mark;
2177 PERL_ARGS_ASSERT_DO_MSGRCV;
2178 PERL_UNUSED_ARG(sp);
2180 /* suppress warning when reading into undef var --jhi */
2182 sv_setpvn(mstr, "", 0);
2183 msize = SvIVx(*++mark);
2184 mtype = (long)SvIVx(*++mark);
2185 flags = SvIVx(*++mark);
2186 SvPV_force_nolen(mstr);
2187 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
2190 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
2192 SvCUR_set(mstr, sizeof(long)+ret);
2193 *SvEND(mstr) = '\0';
2194 #ifndef INCOMPLETE_TAINTS
2195 /* who knows who has been playing with this message? */
2201 PERL_UNUSED_ARG(sp);
2202 PERL_UNUSED_ARG(mark);
2203 Perl_croak(aTHX_ "msgrcv not implemented");
2208 Perl_do_semop(pTHX_ SV **mark, SV **sp)
2213 const I32 id = SvIVx(*++mark);
2214 SV * const opstr = *++mark;
2215 const char * const opbuf = SvPV_const(opstr, opsize);
2217 PERL_ARGS_ASSERT_DO_SEMOP;
2218 PERL_UNUSED_ARG(sp);
2220 if (opsize < 3 * SHORTSIZE
2221 || (opsize % (3 * SHORTSIZE))) {
2222 SETERRNO(EINVAL,LIB_INVARG);
2226 /* We can't assume that sizeof(struct sembuf) == 3 * sizeof(short). */
2228 const int nsops = opsize / (3 * sizeof (short));
2230 short * const ops = (short *) opbuf;
2232 struct sembuf *temps, *t;
2235 Newx (temps, nsops, struct sembuf);
2243 result = semop(id, temps, nsops);
2257 Perl_croak(aTHX_ "semop not implemented");
2262 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
2267 struct shmid_ds shmds;
2268 const I32 id = SvIVx(*++mark);
2269 SV * const mstr = *++mark;
2270 const I32 mpos = SvIVx(*++mark);
2271 const I32 msize = SvIVx(*++mark);
2273 PERL_ARGS_ASSERT_DO_SHMIO;
2274 PERL_UNUSED_ARG(sp);
2277 if (shmctl(id, IPC_STAT, &shmds) == -1)
2279 if (mpos < 0 || msize < 0
2280 || (size_t)mpos + msize > (size_t)shmds.shm_segsz) {
2281 SETERRNO(EFAULT,SS_ACCVIO); /* can't do as caller requested */
2284 shm = (char *)shmat(id, NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
2285 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2287 if (optype == OP_SHMREAD) {
2289 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2291 sv_setpvn(mstr, "", 0);
2292 SvPV_force_nolen(mstr);
2293 mbuf = SvGROW(mstr, (STRLEN)msize+1);
2295 Copy(shm + mpos, mbuf, msize, char);
2296 SvCUR_set(mstr, msize);
2297 *SvEND(mstr) = '\0';
2299 #ifndef INCOMPLETE_TAINTS
2300 /* who knows who has been playing with this shared memory? */
2307 const char *mbuf = SvPV_const(mstr, len);
2308 const I32 n = ((I32)len > msize) ? msize : (I32)len;
2309 Copy(mbuf, shm + mpos, n, char);
2311 memzero(shm + mpos + n, msize - n);
2315 Perl_croak(aTHX_ "shm I/O not implemented");
2319 #endif /* SYSV IPC */
2324 =for apidoc start_glob
2326 Function called by C<do_readline> to spawn a glob (or do the glob inside
2327 perl on VMS). This code used to be inline, but now perl uses C<File::Glob>
2328 this glob starter is only used by miniperl during the build process.
2329 Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up.
2335 Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
2338 SV * const tmpcmd = newSV(0);
2341 PERL_ARGS_ASSERT_START_GLOB;
2345 #ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
2346 /* since spawning off a process is a real performance hit */
2353 fp = Perl_vms_start_glob(aTHX_ tmpglob, io);
2356 #ifdef MACOS_TRADITIONAL
2357 sv_setpv(tmpcmd, "glob ");
2358 sv_catsv(tmpcmd, tmpglob);
2359 sv_catpv(tmpcmd, " |");
2363 sv_setpv(tmpcmd, "for a in ");
2364 sv_catsv(tmpcmd, tmpglob);
2365 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
2368 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
2369 sv_catsv(tmpcmd, tmpglob);
2371 sv_setpv(tmpcmd, "perlglob ");
2372 sv_catsv(tmpcmd, tmpglob);
2373 sv_catpv(tmpcmd, " |");
2378 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
2379 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
2380 sv_catsv(tmpcmd, tmpglob);
2381 sv_catpv(tmpcmd, "' 2>/dev/null |");
2383 sv_setpv(tmpcmd, "echo ");
2384 sv_catsv(tmpcmd, tmpglob);
2386 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
2388 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
2391 #endif /* !DOSISH */
2392 #endif /* MACOS_TRADITIONAL */
2393 (void)do_open(PL_last_in_gv, (char*)SvPVX_const(tmpcmd), SvCUR(tmpcmd),
2394 FALSE, O_RDONLY, 0, NULL);
2403 * c-indentation-style: bsd
2405 * indent-tabs-mode: t
2408 * ex: set ts=8 sts=4 sw=4 noet: