3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4 * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 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.
17 * [p.684 of _The Lord of the Rings_, IV/vi: "The Forbidden Pool"]
20 /* This file contains functions that do the actual I/O on behalf of ops.
21 * For example, pp_print() calls the do_print() function in this file for
22 * each argument needing printing.
26 #define PERL_IN_DOIO_C
29 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
38 # ifndef HAS_SHMAT_PROTOTYPE
39 extern Shmat_t shmat (int, char *, int);
45 # if defined(_MSC_VER) || defined(__MINGW32__)
46 # include <sys/utime.h>
53 # define OPEN_EXCL O_EXCL
58 #define PERL_MODE_MAX 8
59 #define PERL_FLAGS_MAX 10
64 Perl_do_openn(pTHX_ GV *gv, register const char *oname, I32 len, int as_raw,
65 int rawmode, int rawperm, PerlIO *supplied_fp, SV **svp,
69 register IO * const io = GvIOn(gv);
70 PerlIO *saveifp = NULL;
71 PerlIO *saveofp = NULL;
73 char savetype = IoTYPE_CLOSED;
78 bool was_fdopen = FALSE;
79 bool in_raw = 0, in_crlf = 0, out_raw = 0, out_crlf = 0;
81 char mode[PERL_MODE_MAX]; /* file mode ("r\0", "rb\0", "ab\0" etc.) */
84 PERL_ARGS_ASSERT_DO_OPENN;
86 Zero(mode,sizeof(mode),char);
87 PL_forkprocess = 1; /* assume true if no fork */
89 /* Collect default raw/crlf info from the op */
90 if (PL_op && PL_op->op_type == OP_OPEN) {
91 /* set up IO layers */
92 const U8 flags = PL_op->op_private;
93 in_raw = (flags & OPpOPEN_IN_RAW);
94 in_crlf = (flags & OPpOPEN_IN_CRLF);
95 out_raw = (flags & OPpOPEN_OUT_RAW);
96 out_crlf = (flags & OPpOPEN_OUT_CRLF);
99 /* If currently open - close before we re-open */
101 fd = PerlIO_fileno(IoIFP(io));
102 if (IoTYPE(io) == IoTYPE_STD) {
103 /* This is a clone of one of STD* handles */
106 else if (fd >= 0 && fd <= PL_maxsysfd) {
107 /* This is one of the original STD* handles */
110 savetype = IoTYPE(io);
114 else if (IoTYPE(io) == IoTYPE_PIPE)
115 result = PerlProc_pclose(IoIFP(io));
116 else if (IoIFP(io) != IoOFP(io)) {
118 result = PerlIO_close(IoOFP(io));
119 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
122 result = PerlIO_close(IoIFP(io));
125 result = PerlIO_close(IoIFP(io));
126 if (result == EOF && fd > PL_maxsysfd) {
127 /* Why is this not Perl_warn*() call ? */
128 PerlIO_printf(Perl_error_log,
129 "Warning: unable to close filehandle %s properly.\n",
132 IoOFP(io) = IoIFP(io) = NULL;
136 /* sysopen style args, i.e. integer mode and permissions */
138 const int appendtrunc =
140 #ifdef O_APPEND /* Not fully portable. */
143 #ifdef O_TRUNC /* Not fully portable. */
147 const int modifyingmode = O_WRONLY|O_RDWR|O_CREAT|appendtrunc;
151 Perl_croak(aTHX_ "panic: sysopen with multiple args");
159 It might be (in OS/390 and Mac OS Classic it is)
165 This means that simple & with O_RDWR would look
166 like O_RDONLY is present. Therefore we have to
169 if ((ismodifying = (rawmode & modifyingmode))) {
170 if ((ismodifying & O_WRONLY) == O_WRONLY ||
171 (ismodifying & O_RDWR) == O_RDWR ||
172 (ismodifying & (O_CREAT|appendtrunc)))
173 TAINT_PROPER("sysopen");
175 mode[ix++] = IoTYPE_NUMERIC; /* Marker to openn to use numeric "sysopen" */
177 #if defined(USE_64_BIT_RAWIO) && defined(O_LARGEFILE)
178 rawmode |= O_LARGEFILE; /* Transparently largefiley. */
181 IoTYPE(io) = PerlIO_intmode2str(rawmode, &mode[ix], &writing);
183 namesv = newSVpvn_flags(oname, len, SVs_TEMP);
187 fp = PerlIO_openn(aTHX_ type, mode, -1, rawmode, rawperm, NULL, num_svs, svp);
190 /* Regular (non-sys) open */
196 type = savepvn(oname, len);
200 /* Lose leading and trailing white space */
201 while (isSPACE(*type))
203 while (tend > type && isSPACE(tend[-1]))
207 /* New style explicit name, type is just mode and layer info */
209 if (SvROK(*svp) && !strchr(oname,'&')) {
211 Perl_warner(aTHX_ packWARN(WARN_IO),
212 "Can't open a reference");
213 SETERRNO(EINVAL, LIB_INVARG);
216 #endif /* USE_STDIO */
217 name = SvOK(*svp) ? savesvpv (*svp) : savepvs ("");
225 if ((*type == IoTYPE_RDWR) && /* scary */
226 (*(type+1) == IoTYPE_RDONLY || *(type+1) == IoTYPE_WRONLY) &&
227 ((!num_svs || (tend > type+1 && tend[-1] != IoTYPE_PIPE)))) {
228 TAINT_PROPER("open");
233 if (*type == IoTYPE_PIPE) {
235 if (type[1] != IoTYPE_STD) {
237 Perl_croak(aTHX_ "Unknown open() mode '%.*s'", (int)olen, oname);
243 } while (isSPACE(*type));
249 /* command is missing 19990114 */
250 if (ckWARN(WARN_PIPE))
251 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open");
255 if (!(*name == '-' && name[1] == '\0') || num_svs)
257 TAINT_PROPER("piped open");
258 if (!num_svs && name[len-1] == '|') {
260 if (ckWARN(WARN_PIPE))
261 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Can't open bidirectional pipe");
270 fp = PerlProc_popen_list(mode, num_svs, svp);
273 fp = PerlProc_popen(name,mode);
277 if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) {
283 else if (*type == IoTYPE_WRONLY) {
284 TAINT_PROPER("open");
286 if (*type == IoTYPE_WRONLY) {
287 /* Two IoTYPE_WRONLYs in a row make for an IoTYPE_APPEND. */
288 mode[0] = IoTYPE(io) = IoTYPE_APPEND;
302 dodup = PERLIO_DUP_FD;
308 if (!num_svs && !*type && supplied_fp) {
309 /* "<+&" etc. is used by typemaps */
313 PerlIO *that_fp = NULL;
315 /* diag_listed_as: More than one argument to '%s' open */
316 Perl_croak(aTHX_ "More than one argument to '%c&' open",IoTYPE(io));
318 while (isSPACE(*type))
320 if (num_svs && (SvIOK(*svp) || (SvPOK(*svp) && looks_like_number(*svp)))) {
324 else if (isDIGIT(*type)) {
330 thatio = sv_2io(*svp);
333 GV * const thatgv = gv_fetchpvn_flags(type, tend - type,
335 thatio = GvIO(thatgv);
339 SETERRNO(EINVAL,SS_IVCHAN);
343 if ((that_fp = IoIFP(thatio))) {
344 /* Flush stdio buffer before dup. --mjd
345 * Unfortunately SEEK_CURing 0 seems to
346 * be optimized away on most platforms;
347 * only Solaris and Linux seem to flush
350 /* sfio fails to clear error on next
351 sfwrite, contrary to documentation.
353 if (PerlIO_seek(that_fp, 0, SEEK_CUR) == -1)
354 PerlIO_clearerr(that_fp);
356 /* On the other hand, do all platforms
357 * take gracefully to flushing a read-only
358 * filehandle? Perhaps we should do
359 * fsetpos(src)+fgetpos(dst)? --nik */
360 PerlIO_flush(that_fp);
361 fd = PerlIO_fileno(that_fp);
362 /* When dup()ing STDIN, STDOUT or STDERR
363 * explicitly set appropriate access mode */
364 if (that_fp == PerlIO_stdout()
365 || that_fp == PerlIO_stderr())
366 IoTYPE(io) = IoTYPE_WRONLY;
367 else if (that_fp == PerlIO_stdin())
368 IoTYPE(io) = IoTYPE_RDONLY;
369 /* When dup()ing a socket, say result is
371 else if (IoTYPE(thatio) == IoTYPE_SOCKET)
372 IoTYPE(io) = IoTYPE_SOCKET;
380 fp = PerlIO_fdupopen(aTHX_ that_fp, NULL, dodup);
384 fd = PerlLIO_dup(fd);
387 if (!(fp = PerlIO_openn(aTHX_ type,mode,fd,0,0,NULL,num_svs,svp))) {
388 if (dodup && fd >= 0)
395 while (isSPACE(*type))
397 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
399 fp = PerlIO_stdout();
400 IoTYPE(io) = IoTYPE_STD;
402 /* diag_listed_as: More than one argument to '%s' open */
403 Perl_croak(aTHX_ "More than one argument to '>%c' open",IoTYPE_STD);
408 namesv = newSVpvn_flags(type, tend - type, SVs_TEMP);
413 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
416 if (!fp && type && *type && *type != ':' && !isIDFIRST(*type))
417 goto unknown_open_mode;
418 } /* IoTYPE_WRONLY */
419 else if (*type == IoTYPE_RDONLY) {
422 } while (isSPACE(*type));
431 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
434 IoTYPE(io) = IoTYPE_STD;
436 /* diag_listed_as: More than one argument to '%s' open */
437 Perl_croak(aTHX_ "More than one argument to '<%c' open",IoTYPE_STD);
442 namesv = newSVpvn_flags(type, tend - type, SVs_TEMP);
447 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
449 if (!fp && type && *type && *type != ':' && !isIDFIRST(*type))
450 goto unknown_open_mode;
451 } /* IoTYPE_RDONLY */
452 else if ((num_svs && /* '-|...' or '...|' */
453 type[0] == IoTYPE_STD && type[1] == IoTYPE_PIPE) ||
454 (!num_svs && tend > type+1 && tend[-1] == IoTYPE_PIPE)) {
456 type += 2; /* skip over '-|' */
460 while (tend > type && isSPACE(tend[-1]))
462 for (; isSPACE(*type); type++)
468 /* command is missing 19990114 */
469 if (ckWARN(WARN_PIPE))
470 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open");
474 if (!(*name == '-' && name[1] == '\0') || num_svs)
476 TAINT_PROPER("piped open");
485 fp = PerlProc_popen_list(mode,num_svs,svp);
488 fp = PerlProc_popen(name,mode);
490 IoTYPE(io) = IoTYPE_PIPE;
492 while (isSPACE(*type))
495 if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) {
501 else { /* layer(Args) */
503 goto unknown_open_mode;
505 IoTYPE(io) = IoTYPE_RDONLY;
506 for (; isSPACE(*name); name++)
515 if (*name == '-' && name[1] == '\0') {
517 IoTYPE(io) = IoTYPE_STD;
521 namesv = newSVpvn_flags(type, tend - type, SVs_TEMP);
526 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
531 if (IoTYPE(io) == IoTYPE_RDONLY && ckWARN(WARN_NEWLINE)
532 && strchr(oname, '\n')
535 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
539 if (ckWARN(WARN_IO)) {
540 if ((IoTYPE(io) == IoTYPE_RDONLY) &&
541 (fp == PerlIO_stdout() || fp == PerlIO_stderr())) {
542 Perl_warner(aTHX_ packWARN(WARN_IO),
543 "Filehandle STD%s reopened as %s only for input",
544 ((fp == PerlIO_stdout()) ? "OUT" : "ERR"),
547 else if ((IoTYPE(io) == IoTYPE_WRONLY) && fp == PerlIO_stdin()) {
548 Perl_warner(aTHX_ packWARN(WARN_IO),
549 "Filehandle STDIN reopened as %s only for output",
554 fd = PerlIO_fileno(fp);
555 /* If there is no fd (e.g. PerlIO::scalar) assume it isn't a
556 * socket - this covers PerlIO::scalar - otherwise unless we "know" the
557 * type probe for socket-ness.
559 if (IoTYPE(io) && IoTYPE(io) != IoTYPE_PIPE && IoTYPE(io) != IoTYPE_STD && fd >= 0) {
560 if (PerlLIO_fstat(fd,&PL_statbuf) < 0) {
561 /* If PerlIO claims to have fd we had better be able to fstat() it. */
562 (void) PerlIO_close(fp);
566 if (S_ISSOCK(PL_statbuf.st_mode))
567 IoTYPE(io) = IoTYPE_SOCKET; /* in case a socket was passed in to us */
571 !(PL_statbuf.st_mode & S_IFMT)
575 && IoTYPE(io) != IoTYPE_WRONLY /* Dups of STD* filehandles already have */
576 && IoTYPE(io) != IoTYPE_RDONLY /* type so they aren't marked as sockets */
577 ) { /* on OS's that return 0 on fstat()ed pipe */
579 Sock_size_t buflen = sizeof tmpbuf;
580 if (PerlSock_getsockname(fd, (struct sockaddr *)tmpbuf, &buflen) >= 0
581 || errno != ENOTSOCK)
582 IoTYPE(io) = IoTYPE_SOCKET; /* some OS's return 0 on fstat()ed socket */
583 /* but some return 0 for streams too, sigh */
585 #endif /* HAS_SOCKET */
586 #endif /* !PERL_MICRO */
590 * If this is a standard handle we discard all the layer stuff
591 * and just dup the fd into whatever was on the handle before !
594 if (saveifp) { /* must use old fp? */
595 /* If fd is less that PL_maxsysfd i.e. STDIN..STDERR
596 then dup the new fileno down
599 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
600 if (saveofp != saveifp) { /* was a socket? */
601 PerlIO_close(saveofp);
605 /* Still a small can-of-worms here if (say) PerlIO::scalar
606 is assigned to (say) STDOUT - for now let dup2() fail
607 and provide the error
609 if (PerlLIO_dup2(fd, savefd) < 0) {
610 (void)PerlIO_close(fp);
614 if (savefd != PerlIO_fileno(PerlIO_stdin())) {
615 char newname[FILENAME_MAX+1];
616 if (PerlIO_getname(fp, newname)) {
617 if (fd == PerlIO_fileno(PerlIO_stdout()))
618 Perl_vmssetuserlnm(aTHX_ "SYS$OUTPUT", newname);
619 if (fd == PerlIO_fileno(PerlIO_stderr()))
620 Perl_vmssetuserlnm(aTHX_ "SYS$ERROR", newname);
626 /* PL_fdpid isn't used on Windows, so avoid this useless work.
627 * XXX Probably the same for a lot of other places. */
632 sv = *av_fetch(PL_fdpid,fd,TRUE);
633 SvUPGRADE(sv, SVt_IV);
636 sv = *av_fetch(PL_fdpid,savefd,TRUE);
637 SvUPGRADE(sv, SVt_IV);
643 /* need to close fp without closing underlying fd */
644 int ofd = PerlIO_fileno(fp);
645 int dupfd = PerlLIO_dup(ofd);
646 #if defined(HAS_FCNTL) && defined(F_SETFD)
647 /* Assume if we have F_SETFD we have F_GETFD */
648 int coe = fcntl(ofd,F_GETFD);
651 PerlLIO_dup2(dupfd,ofd);
652 #if defined(HAS_FCNTL) && defined(F_SETFD)
653 /* The dup trick has lost close-on-exec on ofd */
654 fcntl(ofd,F_SETFD, coe);
656 PerlLIO_close(dupfd);
663 fd = PerlIO_fileno(fp);
665 #if defined(HAS_FCNTL) && defined(F_SETFD)
668 fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
674 IoFLAGS(io) &= ~IOf_NOLINE;
676 if (IoTYPE(io) == IoTYPE_SOCKET
677 || (IoTYPE(io) == IoTYPE_WRONLY && fd >= 0 && S_ISCHR(PL_statbuf.st_mode)) ) {
679 if (*s == IoTYPE_IMPLICIT || *s == IoTYPE_NUMERIC)
682 if (!(IoOFP(io) = PerlIO_openn(aTHX_ type,s,fd,0,0,NULL,0,svp))) {
696 IoTYPE(io) = savetype;
701 Perl_nextargv(pTHX_ register GV *gv)
705 #ifndef FLEXFILENAMES
711 IO * const io = GvIOp(gv);
713 PERL_ARGS_ASSERT_NEXTARGV;
716 PL_argvoutgv = gv_fetchpvs("ARGVOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
717 if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
718 IoFLAGS(io) &= ~IOf_START;
721 Perl_av_create_and_push(aTHX_ &PL_argvout_stack,
722 SvREFCNT_inc_simple_NN(PL_defoutgv));
725 if (PL_filemode & (S_ISUID|S_ISGID)) {
726 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
729 (void)fchmod(PL_lastfd,PL_filemode);
731 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
738 while (av_len(GvAV(gv)) >= 0) {
740 sv = av_shift(GvAV(gv));
742 sv_setsv(GvSVn(gv),sv);
743 SvSETMAGIC(GvSV(gv));
744 PL_oldname = SvPVx(GvSV(gv), oldlen);
745 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,NULL)) {
747 TAINT_PROPER("inplace open");
748 if (oldlen == 1 && *PL_oldname == '-') {
749 setdefout(gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL,
751 return IoIFP(GvIOp(gv));
753 #ifndef FLEXFILENAMES
754 filedev = PL_statbuf.st_dev;
755 fileino = PL_statbuf.st_ino;
757 PL_filemode = PL_statbuf.st_mode;
758 fileuid = PL_statbuf.st_uid;
759 filegid = PL_statbuf.st_gid;
760 if (!S_ISREG(PL_filemode)) {
761 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
762 "Can't do inplace edit: %s is not a regular file",
768 const char *star = strchr(PL_inplace, '*');
770 const char *begin = PL_inplace;
773 sv_catpvn(sv, begin, star - begin);
774 sv_catpvn(sv, PL_oldname, oldlen);
776 } while ((star = strchr(begin, '*')));
781 sv_catpv(sv,PL_inplace);
783 #ifndef FLEXFILENAMES
784 if ((PerlLIO_stat(SvPVX_const(sv),&PL_statbuf) >= 0
785 && PL_statbuf.st_dev == filedev
786 && PL_statbuf.st_ino == fileino)
788 || ((_djstat_fail_bits & _STFAIL_TRUENAME)!=0)
792 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
793 "Can't do inplace edit: %"SVf" would not be unique",
800 #if !defined(DOSISH) && !defined(__CYGWIN__) && !defined(EPOC)
801 if (PerlLIO_rename(PL_oldname,SvPVX_const(sv)) < 0) {
802 Perl_ck_warner_d(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),TRUE,O_RDONLY,0,NULL);
815 (void)UNLINK(SvPVX_const(sv));
816 if (link(PL_oldname,SvPVX_const(sv)) < 0) {
817 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
818 "Can't rename %s to %"SVf": %s, skipping file",
819 PL_oldname, SVfARG(sv), Strerror(errno) );
823 (void)UNLINK(PL_oldname);
827 #if !defined(DOSISH) && !defined(AMIGAOS)
828 # ifndef VMS /* Don't delete; use automatic file versioning */
829 if (UNLINK(PL_oldname) < 0) {
830 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
831 "Can't remove %s: %s, skipping file",
832 PL_oldname, Strerror(errno) );
838 Perl_croak(aTHX_ "Can't do inplace edit without backup");
842 sv_setpvn(sv,PL_oldname,oldlen);
843 SETERRNO(0,0); /* in case sprintf set errno */
844 if (!Perl_do_openn(aTHX_ PL_argvoutgv, (char*)SvPVX_const(sv),
847 O_WRONLY|O_CREAT|O_TRUNC,0,
849 O_WRONLY|O_CREAT|OPEN_EXCL,0600,
852 Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE), "Can't do inplace edit on %s: %s",
853 PL_oldname, Strerror(errno) );
857 setdefout(PL_argvoutgv);
858 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
859 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
861 (void)fchmod(PL_lastfd,PL_filemode);
863 # if !(defined(WIN32) && defined(__BORLANDC__))
864 /* Borland runtime creates a readonly file! */
865 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
868 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
870 (void)fchown(PL_lastfd,fileuid,filegid);
873 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
878 return IoIFP(GvIOp(gv));
881 if (ckWARN_d(WARN_INPLACE)) {
882 const int eno = errno;
883 if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0
884 && !S_ISREG(PL_statbuf.st_mode))
886 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
887 "Can't do inplace edit: %s is not a regular file",
891 Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't open %s: %s",
892 PL_oldname, Strerror(eno));
896 if (io && (IoFLAGS(io) & IOf_ARGV))
897 IoFLAGS(io) |= IOf_START;
899 (void)do_close(PL_argvoutgv,FALSE);
900 if (io && (IoFLAGS(io) & IOf_ARGV)
901 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
903 GV * const oldout = MUTABLE_GV(av_pop(PL_argvout_stack));
905 SvREFCNT_dec(oldout);
908 setdefout(gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO));
913 /* explicit renamed to avoid C++ conflict -- kja */
915 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
923 if (!gv || !isGV_with_GP(gv)) {
925 SETERRNO(EBADF,SS_IVCHAN);
929 if (!io) { /* never opened */
931 if (ckWARN(WARN_UNOPENED)) /* no check for closed here */
932 report_evil_fh(gv, io, PL_op->op_type);
933 SETERRNO(EBADF,SS_IVCHAN);
937 retval = io_close(io, not_implicit);
941 IoLINES_LEFT(io) = IoPAGE_LEN(io);
943 IoTYPE(io) = IoTYPE_CLOSED;
948 Perl_io_close(pTHX_ IO *io, bool not_implicit)
953 PERL_ARGS_ASSERT_IO_CLOSE;
956 if (IoTYPE(io) == IoTYPE_PIPE) {
957 const int status = PerlProc_pclose(IoIFP(io));
959 STATUS_NATIVE_CHILD_SET(status);
960 retval = (STATUS_UNIX == 0);
963 retval = (status != -1);
966 else if (IoTYPE(io) == IoTYPE_STD)
969 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
970 const bool prev_err = PerlIO_error(IoOFP(io));
971 retval = (PerlIO_close(IoOFP(io)) != EOF && !prev_err);
972 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
975 const bool prev_err = PerlIO_error(IoIFP(io));
976 retval = (PerlIO_close(IoIFP(io)) != EOF && !prev_err);
979 IoOFP(io) = IoIFP(io) = NULL;
981 else if (not_implicit) {
982 SETERRNO(EBADF,SS_IVCHAN);
989 Perl_do_eof(pTHX_ GV *gv)
992 register IO * const io = GvIO(gv);
994 PERL_ARGS_ASSERT_DO_EOF;
998 else if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
999 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1002 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
1003 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
1004 return FALSE; /* this is the most usual case */
1008 /* getc and ungetc can stomp on errno */
1010 const int ch = PerlIO_getc(IoIFP(io));
1012 (void)PerlIO_ungetc(IoIFP(io),ch);
1019 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
1020 if (PerlIO_get_cnt(IoIFP(io)) < -1)
1021 PerlIO_set_cnt(IoIFP(io),-1);
1023 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
1024 if (gv != PL_argvgv || !nextargv(gv)) /* get another fp handy */
1028 return TRUE; /* normal fp, definitely end of file */
1034 Perl_do_tell(pTHX_ GV *gv)
1037 register IO *io = NULL;
1038 register PerlIO *fp;
1040 PERL_ARGS_ASSERT_DO_TELL;
1042 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1043 #ifdef ULTRIX_STDIO_BOTCH
1045 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1047 return PerlIO_tell(fp);
1049 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1050 report_evil_fh(gv, io, PL_op->op_type);
1051 SETERRNO(EBADF,RMS_IFI);
1056 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
1059 register IO *io = NULL;
1060 register PerlIO *fp;
1062 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1063 #ifdef ULTRIX_STDIO_BOTCH
1065 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1067 return PerlIO_seek(fp, pos, whence) >= 0;
1069 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1070 report_evil_fh(gv, io, PL_op->op_type);
1071 SETERRNO(EBADF,RMS_IFI);
1076 Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
1079 register IO *io = NULL;
1080 register PerlIO *fp;
1082 PERL_ARGS_ASSERT_DO_SYSSEEK;
1084 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
1085 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
1086 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1087 report_evil_fh(gv, io, PL_op->op_type);
1088 SETERRNO(EBADF,RMS_IFI);
1093 Perl_mode_from_discipline(pTHX_ const char *s, STRLEN len)
1095 int mode = O_BINARY;
1101 if (s[2] == 'a' && s[3] == 'w'
1102 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1111 if (s[2] == 'r' && s[3] == 'l' && s[4] == 'f'
1112 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1121 goto fail_discipline;
1124 else if (isSPACE(*s)) {
1131 end = strchr(s+1, ':');
1134 #ifndef PERLIO_LAYERS
1135 Perl_croak(aTHX_ "IO layers (like '%.*s') unavailable", end-s, s);
1146 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE)
1148 my_chsize(int fd, Off_t length)
1151 /* code courtesy of William Kucharski */
1156 if (PerlLIO_fstat(fd, &filebuf) < 0)
1159 if (filebuf.st_size < length) {
1161 /* extend file length */
1163 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
1166 /* write a "0" byte */
1168 if ((PerlLIO_write(fd, "", 1)) != 1)
1172 /* truncate length */
1176 fl.l_start = length;
1177 fl.l_type = F_WRLCK; /* write lock on file space */
1180 * This relies on the UNDOCUMENTED F_FREESP argument to
1181 * fcntl(2), which truncates the file so that it ends at the
1182 * position indicated by fl.l_start.
1184 * Will minor miracles never cease?
1187 if (fcntl(fd, F_FREESP, &fl) < 0)
1193 Perl_croak_nocontext("truncate not implemented");
1194 #endif /* F_FREESP */
1197 #endif /* !HAS_TRUNCATE && !HAS_CHSIZE */
1200 Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
1204 PERL_ARGS_ASSERT_DO_PRINT;
1206 /* assuming fp is checked earlier */
1209 if (SvTYPE(sv) == SVt_IV && SvIOK(sv)) {
1210 assert(!SvGMAGICAL(sv));
1212 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
1214 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
1215 return !PerlIO_error(fp);
1219 /* Do this first to trigger any overloading. */
1220 const char *tmps = SvPV_const(sv, len);
1224 if (PerlIO_isutf8(fp)) {
1226 /* We don't modify the original scalar. */
1227 tmpbuf = bytes_to_utf8((const U8*) tmps, &len);
1228 tmps = (char *) tmpbuf;
1231 else if (DO_UTF8(sv)) {
1232 STRLEN tmplen = len;
1234 U8 * const result = bytes_from_utf8((const U8*) tmps, &tmplen, &utf8);
1237 tmps = (char *) tmpbuf;
1241 assert((char *)result == tmps);
1242 Perl_ck_warner_d(aTHX_ packWARN(WARN_UTF8),
1243 "Wide character in print");
1246 /* To detect whether the process is about to overstep its
1247 * filesize limit we would need getrlimit(). We could then
1248 * also transparently raise the limit with setrlimit() --
1249 * but only until the system hard limit/the filesystem limit,
1250 * at which we would get EPERM. Note that when using buffered
1251 * io the write failure can be delayed until the flush/close. --jhi */
1252 if (len && (PerlIO_write(fp,tmps,len) == 0))
1255 return happy ? !PerlIO_error(fp) : FALSE;
1267 if (PL_op->op_flags & OPf_REF) {
1272 return PL_laststatval;
1275 PL_laststype = OP_STAT;
1277 sv_setpvs(PL_statname, "");
1280 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
1281 } else if (IoDIRP(io)) {
1282 return (PL_laststatval = PerlLIO_fstat(my_dirfd(IoDIRP(io)), &PL_statcache));
1284 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1285 report_evil_fh(gv, io, PL_op->op_type);
1286 return (PL_laststatval = -1);
1289 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1290 report_evil_fh(gv, io, PL_op->op_type);
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 (isGV_with_GP(sv)) {
1303 gv = MUTABLE_GV(sv);
1306 else if (SvROK(sv) && isGV_with_GP(SvRV(sv))) {
1307 gv = MUTABLE_GV(SvRV(sv));
1310 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
1311 io = MUTABLE_IO(SvRV(sv));
1313 goto do_fstat_have_io;
1316 s = SvPV_const(sv, len);
1318 sv_setpvn(PL_statname, s, len);
1319 s = SvPVX_const(PL_statname); /* s now NUL-terminated */
1320 PL_laststype = OP_STAT;
1321 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1322 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
1323 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
1324 return PL_laststatval;
1333 static const char no_prev_lstat[] = "The stat preceding -l _ wasn't an lstat";
1337 if (PL_op->op_flags & OPf_REF) {
1339 if (cGVOP_gv == PL_defgv) {
1340 if (PL_laststype != OP_LSTAT)
1341 Perl_croak(aTHX_ no_prev_lstat);
1342 return PL_laststatval;
1344 if (ckWARN(WARN_IO)) {
1345 Perl_warner(aTHX_ packWARN(WARN_IO), "Use of -l on filehandle %s",
1347 return (PL_laststatval = -1);
1350 else if (PL_laststype != OP_LSTAT
1351 && (PL_op->op_private & OPpFT_STACKED) && ckWARN(WARN_IO))
1352 Perl_croak(aTHX_ no_prev_lstat);
1354 PL_laststype = OP_LSTAT;
1358 if (SvROK(sv) && isGV_with_GP(SvRV(sv)) && ckWARN(WARN_IO)) {
1359 Perl_warner(aTHX_ packWARN(WARN_IO), "Use of -l on filehandle %s",
1360 GvENAME((const GV *)SvRV(sv)));
1361 return (PL_laststatval = -1);
1363 file = SvPV_nolen_const(sv);
1364 sv_setpv(PL_statname,file);
1365 PL_laststatval = PerlLIO_lstat(file,&PL_statcache);
1366 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(file, '\n'))
1367 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "lstat");
1368 return PL_laststatval;
1372 S_exec_failed(pTHX_ const char *cmd, int fd, int do_report)
1374 const int e = errno;
1375 PERL_ARGS_ASSERT_EXEC_FAILED;
1376 if (ckWARN(WARN_EXEC))
1377 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
1380 PerlLIO_write(fd, (void*)&e, sizeof(int));
1386 Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1387 int fd, int do_report)
1390 PERL_ARGS_ASSERT_DO_AEXEC5;
1391 #if defined(__SYMBIAN32__) || defined(__LIBCATAMOUNT__)
1392 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1396 const char *tmps = NULL;
1397 Newx(PL_Argv, sp - mark + 1, const char*);
1400 while (++mark <= sp) {
1402 *a++ = SvPV_nolen_const(*mark);
1408 tmps = SvPV_nolen_const(really);
1409 if ((!really && *PL_Argv[0] != '/') ||
1410 (really && *tmps != '/')) /* will execvp use PATH? */
1411 TAINT_ENV(); /* testing IFS here is overkill, probably */
1413 if (really && *tmps)
1414 PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
1416 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1418 S_exec_failed(aTHX_ (really ? tmps : PL_Argv[0]), fd, do_report);
1426 Perl_do_execfree(pTHX)
1435 #ifdef PERL_DEFAULT_DO_EXEC3_IMPLEMENTATION
1438 Perl_do_exec3(pTHX_ const char *incmd, int fd, int do_report)
1441 register const char **a;
1445 /* Make a copy so we can change it */
1446 const Size_t cmdlen = strlen(incmd) + 1;
1448 PERL_ARGS_ASSERT_DO_EXEC3;
1450 Newx(buf, cmdlen, char);
1452 memcpy(cmd, incmd, cmdlen);
1454 while (*cmd && isSPACE(*cmd))
1457 /* save an extra exec if possible */
1461 char flags[PERL_FLAGS_MAX];
1462 if (strnEQ(cmd,PL_cshname,PL_cshlen) &&
1463 strnEQ(cmd+PL_cshlen," -c",3)) {
1464 my_strlcpy(flags, "-c", PERL_FLAGS_MAX);
1465 s = cmd+PL_cshlen+3;
1468 my_strlcat(flags, "f", PERL_FLAGS_MAX - 2);
1473 char * const ncmd = s;
1479 if (s[-1] == '\'') {
1482 PerlProc_execl(PL_cshname, "csh", flags, ncmd, (char*)NULL);
1485 S_exec_failed(aTHX_ PL_cshname, fd, do_report);
1494 /* see if there are shell metacharacters in it */
1496 if (*cmd == '.' && isSPACE(cmd[1]))
1499 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1504 s++; /* catch VAR=val gizmo */
1508 for (s = cmd; *s; s++) {
1509 if (*s != ' ' && !isALPHA(*s) &&
1510 strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1511 if (*s == '\n' && !s[1]) {
1515 /* handle the 2>&1 construct at the end */
1516 if (*s == '>' && s[1] == '&' && s[2] == '1'
1517 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1518 && (!s[3] || isSPACE(s[3])))
1520 const char *t = s + 3;
1522 while (*t && isSPACE(*t))
1524 if (!*t && (PerlLIO_dup2(1,2) != -1)) {
1531 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char *)NULL);
1533 S_exec_failed(aTHX_ PL_sh_path, fd, do_report);
1539 Newx(PL_Argv, (s - cmd) / 2 + 2, const char*);
1540 PL_Cmd = savepvn(cmd, s-cmd);
1542 for (s = PL_Cmd; *s;) {
1547 while (*s && !isSPACE(*s))
1555 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1557 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1561 S_exec_failed(aTHX_ PL_Argv[0], fd, do_report);
1568 #endif /* OS2 || WIN32 */
1571 Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
1575 register I32 tot = 0;
1576 const char *const what = PL_op_name[type];
1578 SV ** const oldmark = mark;
1580 PERL_ARGS_ASSERT_APPLY;
1582 /* Doing this ahead of the switch statement preserves the old behaviour,
1583 where attempting to use kill as a taint test test would fail on
1584 platforms where kill was not defined. */
1586 if (type == OP_KILL)
1587 Perl_die(aTHX_ PL_no_func, what);
1590 if (type == OP_CHOWN)
1591 Perl_die(aTHX_ PL_no_func, what);
1595 #define APPLY_TAINT_PROPER() \
1597 if (PL_tainted) { TAINT_PROPER(what); } \
1600 /* This is a first heuristic; it doesn't catch tainting magic. */
1602 while (++mark <= sp) {
1603 if (SvTAINTED(*mark)) {
1612 APPLY_TAINT_PROPER();
1615 APPLY_TAINT_PROPER();
1617 while (++mark <= sp) {
1619 if (isGV_with_GP(*mark)) {
1620 gv = MUTABLE_GV(*mark);
1622 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1624 APPLY_TAINT_PROPER();
1625 if (fchmod(PerlIO_fileno(IoIFP(GvIOn(gv))), val))
1628 Perl_die(aTHX_ PL_no_func, "fchmod");
1635 else if (SvROK(*mark) && isGV_with_GP(SvRV(*mark))) {
1636 gv = MUTABLE_GV(SvRV(*mark));
1640 const char *name = SvPV_nolen_const(*mark);
1641 APPLY_TAINT_PROPER();
1642 if (PerlLIO_chmod(name, val))
1650 APPLY_TAINT_PROPER();
1651 if (sp - mark > 2) {
1653 val = SvIVx(*++mark);
1654 val2 = SvIVx(*++mark);
1655 APPLY_TAINT_PROPER();
1657 while (++mark <= sp) {
1659 if (isGV_with_GP(*mark)) {
1660 gv = MUTABLE_GV(*mark);
1662 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1664 APPLY_TAINT_PROPER();
1665 if (fchown(PerlIO_fileno(IoIFP(GvIOn(gv))), val, val2))
1668 Perl_die(aTHX_ PL_no_func, "fchown");
1675 else if (SvROK(*mark) && isGV_with_GP(SvRV(*mark))) {
1676 gv = MUTABLE_GV(SvRV(*mark));
1680 const char *name = SvPV_nolen_const(*mark);
1681 APPLY_TAINT_PROPER();
1682 if (PerlLIO_chown(name, val, val2))
1690 XXX Should we make lchown() directly available from perl?
1691 For now, we'll let Configure test for HAS_LCHOWN, but do
1692 nothing in the core.
1697 APPLY_TAINT_PROPER();
1700 s = SvPVx_nolen_const(*++mark);
1702 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1704 if ((val = whichsig(s)) < 0)
1705 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
1709 APPLY_TAINT_PROPER();
1712 /* kill() doesn't do process groups (job trees?) under VMS */
1713 if (val < 0) val = -val;
1714 if (val == SIGKILL) {
1715 # include <starlet.h>
1716 /* Use native sys$delprc() to insure that target process is
1717 * deleted; supervisor-mode images don't pay attention to
1718 * CRTL's emulation of Unix-style signals and kill()
1720 while (++mark <= sp) {
1722 register unsigned long int __vmssts;
1723 if (!(SvIOK(*mark) || SvNOK(*mark) || looks_like_number(*mark)))
1724 Perl_croak(aTHX_ "Can't kill a non-numeric process ID");
1726 APPLY_TAINT_PROPER();
1727 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1731 case SS$_NOSUCHNODE:
1732 SETERRNO(ESRCH,__vmssts);
1735 SETERRNO(EPERM,__vmssts);
1738 SETERRNO(EVMSERR,__vmssts);
1747 while (++mark <= sp) {
1749 if (!(SvIOK(*mark) || SvNOK(*mark) || looks_like_number(*mark)))
1750 Perl_croak(aTHX_ "Can't kill a non-numeric process ID");
1752 APPLY_TAINT_PROPER();
1754 if (PerlProc_killpg(proc,val)) /* BSD */
1756 if (PerlProc_kill(-proc,val)) /* SYSV */
1762 while (++mark <= sp) {
1764 if (!(SvIOK(*mark) || SvNOK(*mark) || looks_like_number(*mark)))
1765 Perl_croak(aTHX_ "Can't kill a non-numeric process ID");
1767 APPLY_TAINT_PROPER();
1768 if (PerlProc_kill(proc, val))
1775 APPLY_TAINT_PROPER();
1777 while (++mark <= sp) {
1778 s = SvPV_nolen_const(*mark);
1779 APPLY_TAINT_PROPER();
1780 if (PL_euid || PL_unsafe) {
1784 else { /* don't let root wipe out directories without -U */
1785 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1794 #if defined(HAS_UTIME) || defined(HAS_FUTIMES)
1796 APPLY_TAINT_PROPER();
1797 if (sp - mark > 2) {
1798 #if defined(HAS_FUTIMES)
1799 struct timeval utbuf[2];
1800 void *utbufp = utbuf;
1801 #elif defined(I_UTIME) || defined(VMS)
1802 struct utimbuf utbuf;
1803 struct utimbuf *utbufp = &utbuf;
1809 void *utbufp = &utbuf;
1812 SV* const accessed = *++mark;
1813 SV* const modified = *++mark;
1815 /* Be like C, and if both times are undefined, let the C
1816 * library figure out what to do. This usually means
1817 * "current time". */
1819 if ( accessed == &PL_sv_undef && modified == &PL_sv_undef )
1822 Zero(&utbuf, sizeof utbuf, char);
1824 utbuf[0].tv_sec = (long)SvIV(accessed); /* time accessed */
1825 utbuf[0].tv_usec = 0;
1826 utbuf[1].tv_sec = (long)SvIV(modified); /* time modified */
1827 utbuf[1].tv_usec = 0;
1828 #elif defined(BIG_TIME)
1829 utbuf.actime = (Time_t)SvNV(accessed); /* time accessed */
1830 utbuf.modtime = (Time_t)SvNV(modified); /* time modified */
1832 utbuf.actime = (Time_t)SvIV(accessed); /* time accessed */
1833 utbuf.modtime = (Time_t)SvIV(modified); /* time modified */
1836 APPLY_TAINT_PROPER();
1838 while (++mark <= sp) {
1840 if (isGV_with_GP(*mark)) {
1841 gv = MUTABLE_GV(*mark);
1843 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1845 APPLY_TAINT_PROPER();
1846 if (futimes(PerlIO_fileno(IoIFP(GvIOn(gv))),
1847 (struct timeval *) utbufp))
1850 Perl_die(aTHX_ PL_no_func, "futimes");
1857 else if (SvROK(*mark) && isGV_with_GP(SvRV(*mark))) {
1858 gv = MUTABLE_GV(SvRV(*mark));
1862 const char * const name = SvPV_nolen_const(*mark);
1863 APPLY_TAINT_PROPER();
1865 if (utimes(name, (struct timeval *)utbufp))
1867 if (PerlLIO_utime(name, utbufp))
1881 #undef APPLY_TAINT_PROPER
1884 /* Do the permissions allow some operation? Assumes statcache already set. */
1885 #ifndef VMS /* VMS' cando is in vms.c */
1887 Perl_cando(pTHX_ Mode_t mode, bool effective, register const Stat_t *statbufp)
1888 /* effective is a flag, true for EUID, or for checking if the effective gid
1889 * is in the list of groups returned from getgroups().
1894 PERL_ARGS_ASSERT_CANDO;
1897 /* [Comments and code from Len Reed]
1898 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1899 * to write-protected files. The execute permission bit is set
1900 * by the Miscrosoft C library stat() function for the following:
1905 * All files and directories are readable.
1906 * Directories and special files, e.g. "CON", cannot be
1908 * [Comment by Tom Dinger -- a directory can have the write-protect
1909 * bit set in the file system, but DOS permits changes to
1910 * the directory anyway. In addition, all bets are off
1911 * here for networked software, such as Novell and
1915 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1916 * too so it will actually look into the files for magic numbers
1918 return (mode & statbufp->st_mode) ? TRUE : FALSE;
1920 #else /* ! DOSISH */
1921 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
1922 if (mode == S_IXUSR) {
1923 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1927 return TRUE; /* root reads and writes anything */
1930 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1931 if (statbufp->st_mode & mode)
1932 return TRUE; /* ok as "user" */
1934 else if (ingroup(statbufp->st_gid,effective)) {
1935 if (statbufp->st_mode & mode >> 3)
1936 return TRUE; /* ok as "group" */
1938 else if (statbufp->st_mode & mode >> 6)
1939 return TRUE; /* ok as "other" */
1941 #endif /* ! DOSISH */
1946 S_ingroup(pTHX_ Gid_t testgid, bool effective)
1949 if (testgid == (effective ? PL_egid : PL_gid))
1951 #ifdef HAS_GETGROUPS
1953 Groups_t *gary = NULL;
1957 anum = getgroups(0, gary);
1958 Newx(gary, anum, Groups_t);
1959 anum = getgroups(anum, gary);
1961 if (gary[anum] == testgid) {
1974 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1977 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
1980 const key_t key = (key_t)SvNVx(*++mark);
1981 SV *nsv = optype == OP_MSGGET ? NULL : *++mark;
1982 const I32 flags = SvIVx(*++mark);
1984 PERL_ARGS_ASSERT_DO_IPCGET;
1985 PERL_UNUSED_ARG(sp);
1992 return msgget(key, flags);
1996 return semget(key, (int) SvIV(nsv), flags);
2000 return shmget(key, (size_t) SvUV(nsv), flags);
2002 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
2004 /* diag_listed_as: msg%s not implemented */
2005 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2008 return -1; /* should never happen */
2012 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
2017 const I32 id = SvIVx(*++mark);
2019 const I32 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
2021 const I32 cmd = SvIVx(*++mark);
2022 SV * const astr = *++mark;
2023 STRLEN infosize = 0;
2024 I32 getinfo = (cmd == IPC_STAT);
2026 PERL_ARGS_ASSERT_DO_IPCCTL;
2027 PERL_UNUSED_ARG(sp);
2033 if (cmd == IPC_STAT || cmd == IPC_SET)
2034 infosize = sizeof(struct msqid_ds);
2039 if (cmd == IPC_STAT || cmd == IPC_SET)
2040 infosize = sizeof(struct shmid_ds);
2046 if (cmd == IPC_STAT || cmd == IPC_SET)
2047 infosize = sizeof(struct semid_ds);
2048 else if (cmd == GETALL || cmd == SETALL)
2050 struct semid_ds semds;
2052 #ifdef EXTRA_F_IN_SEMUN_BUF
2053 semun.buff = &semds;
2057 getinfo = (cmd == GETALL);
2058 if (Semctl(id, 0, IPC_STAT, semun) == -1)
2060 infosize = semds.sem_nsems * sizeof(short);
2061 /* "short" is technically wrong but much more portable
2062 than guessing about u_?short(_t)? */
2065 /* diag_listed_as: sem%s not implemented */
2066 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2070 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
2072 /* diag_listed_as: shm%s not implemented */
2073 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2081 SvPV_force_nolen(astr);
2082 a = SvGROW(astr, infosize+1);
2087 a = SvPV(astr, len);
2088 if (len != infosize)
2089 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
2097 const IV i = SvIV(astr);
2098 a = INT2PTR(char *,i); /* ouch */
2105 ret = msgctl(id, cmd, (struct msqid_ds *)a);
2111 union semun unsemds;
2113 #ifdef EXTRA_F_IN_SEMUN_BUF
2114 unsemds.buff = (struct semid_ds *)a;
2116 unsemds.buf = (struct semid_ds *)a;
2118 ret = Semctl(id, n, cmd, unsemds);
2120 /* diag_listed_as: sem%s not implemented */
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 /* diag_listed_as: msg%s not implemented */
2163 Perl_croak(aTHX_ "msgsnd not implemented");
2168 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
2174 I32 msize, flags, ret;
2175 const I32 id = SvIVx(*++mark);
2176 SV * const mstr = *++mark;
2178 PERL_ARGS_ASSERT_DO_MSGRCV;
2179 PERL_UNUSED_ARG(sp);
2181 /* suppress warning when reading into undef var --jhi */
2183 sv_setpvs(mstr, "");
2184 msize = SvIVx(*++mark);
2185 mtype = (long)SvIVx(*++mark);
2186 flags = SvIVx(*++mark);
2187 SvPV_force_nolen(mstr);
2188 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
2191 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
2193 SvCUR_set(mstr, sizeof(long)+ret);
2194 *SvEND(mstr) = '\0';
2195 #ifndef INCOMPLETE_TAINTS
2196 /* who knows who has been playing with this message? */
2202 PERL_UNUSED_ARG(sp);
2203 PERL_UNUSED_ARG(mark);
2204 /* diag_listed_as: msg%s not implemented */
2205 Perl_croak(aTHX_ "msgrcv not implemented");
2210 Perl_do_semop(pTHX_ SV **mark, SV **sp)
2215 const I32 id = SvIVx(*++mark);
2216 SV * const opstr = *++mark;
2217 const char * const opbuf = SvPV_const(opstr, opsize);
2219 PERL_ARGS_ASSERT_DO_SEMOP;
2220 PERL_UNUSED_ARG(sp);
2222 if (opsize < 3 * SHORTSIZE
2223 || (opsize % (3 * SHORTSIZE))) {
2224 SETERRNO(EINVAL,LIB_INVARG);
2228 /* We can't assume that sizeof(struct sembuf) == 3 * sizeof(short). */
2230 const int nsops = opsize / (3 * sizeof (short));
2232 short * const ops = (short *) opbuf;
2234 struct sembuf *temps, *t;
2237 Newx (temps, nsops, struct sembuf);
2245 result = semop(id, temps, nsops);
2259 /* diag_listed_as: sem%s not implemented */
2260 Perl_croak(aTHX_ "semop not implemented");
2265 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
2270 struct shmid_ds shmds;
2271 const I32 id = SvIVx(*++mark);
2272 SV * const mstr = *++mark;
2273 const I32 mpos = SvIVx(*++mark);
2274 const I32 msize = SvIVx(*++mark);
2276 PERL_ARGS_ASSERT_DO_SHMIO;
2277 PERL_UNUSED_ARG(sp);
2280 if (shmctl(id, IPC_STAT, &shmds) == -1)
2282 if (mpos < 0 || msize < 0
2283 || (size_t)mpos + msize > (size_t)shmds.shm_segsz) {
2284 SETERRNO(EFAULT,SS_ACCVIO); /* can't do as caller requested */
2287 shm = (char *)shmat(id, NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
2288 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2290 if (optype == OP_SHMREAD) {
2292 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2294 sv_setpvs(mstr, "");
2295 SvPV_force_nolen(mstr);
2296 mbuf = SvGROW(mstr, (STRLEN)msize+1);
2298 Copy(shm + mpos, mbuf, msize, char);
2299 SvCUR_set(mstr, msize);
2300 *SvEND(mstr) = '\0';
2302 #ifndef INCOMPLETE_TAINTS
2303 /* who knows who has been playing with this shared memory? */
2310 const char *mbuf = SvPV_const(mstr, len);
2311 const I32 n = ((I32)len > msize) ? msize : (I32)len;
2312 Copy(mbuf, shm + mpos, n, char);
2314 memzero(shm + mpos + n, msize - n);
2318 /* diag_listed_as: shm%s not implemented */
2319 Perl_croak(aTHX_ "shm I/O not implemented");
2323 #endif /* SYSV IPC */
2328 =for apidoc start_glob
2330 Function called by C<do_readline> to spawn a glob (or do the glob inside
2331 perl on VMS). This code used to be inline, but now perl uses C<File::Glob>
2332 this glob starter is only used by miniperl during the build process.
2333 Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up.
2339 Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
2342 SV * const tmpcmd = newSV(0);
2345 PERL_ARGS_ASSERT_START_GLOB;
2349 #ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
2350 /* since spawning off a process is a real performance hit */
2357 fp = Perl_vms_start_glob(aTHX_ tmpglob, io);
2362 sv_setpv(tmpcmd, "for a in ");
2363 sv_catsv(tmpcmd, tmpglob);
2364 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
2367 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
2368 sv_catsv(tmpcmd, tmpglob);
2370 sv_setpv(tmpcmd, "perlglob ");
2371 sv_catsv(tmpcmd, tmpglob);
2372 sv_catpv(tmpcmd, " |");
2377 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
2378 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
2379 sv_catsv(tmpcmd, tmpglob);
2380 sv_catpv(tmpcmd, "' 2>/dev/null |");
2382 sv_setpv(tmpcmd, "echo ");
2383 sv_catsv(tmpcmd, tmpglob);
2385 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
2387 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
2390 #endif /* !DOSISH */
2391 (void)do_open(PL_last_in_gv, (char*)SvPVX_const(tmpcmd), SvCUR(tmpcmd),
2392 FALSE, O_RDONLY, 0, NULL);
2401 * c-indentation-style: bsd
2403 * indent-tabs-mode: t
2406 * ex: set ts=8 sts=4 sw=4 noet: