3 * Copyright (c) 1991-2000, Larry Wall
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
11 * "Far below them they saw the white waters pour into a foaming bowl, and
12 * then swirl darkly about a deep oval basin in the rocks, until they found
13 * their way out again through a narrow gate, and flowed away, fuming and
14 * chattering, into calmer and more level reaches."
18 #define PERL_IN_DOIO_C
21 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
30 # ifndef HAS_SHMAT_PROTOTYPE
31 extern Shmat_t shmat (int, char *, int);
37 # if defined(_MSC_VER) || defined(__MINGW32__)
38 # include <sys/utime.h>
45 # define OPEN_EXCL O_EXCL
50 #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
54 /* XXX If this causes problems, set i_unistd=undef in the hint file. */
60 Perl_do_open(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
61 int rawmode, int rawperm, PerlIO *supplied_fp)
63 return do_open9(gv, name, len, as_raw, rawmode, rawperm,
64 supplied_fp, Nullsv, 0);
68 Perl_do_open9(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
69 int rawmode, int rawperm, PerlIO *supplied_fp, SV *svs,
72 register IO *io = GvIOn(gv);
73 PerlIO *saveifp = Nullfp;
74 PerlIO *saveofp = Nullfp;
75 char savetype = IoTYPE_CLOSED;
80 bool was_fdopen = FALSE;
81 bool in_raw = 0, in_crlf = 0, out_raw = 0, out_crlf = 0;
83 PL_forkprocess = 1; /* assume true if no fork */
85 if (PL_op && PL_op->op_type == OP_OPEN) {
86 /* set up disciplines */
87 U8 flags = PL_op->op_private;
88 in_raw = (flags & OPpOPEN_IN_RAW);
89 in_crlf = (flags & OPpOPEN_IN_CRLF);
90 out_raw = (flags & OPpOPEN_OUT_RAW);
91 out_crlf = (flags & OPpOPEN_OUT_CRLF);
95 fd = PerlIO_fileno(IoIFP(io));
96 if (IoTYPE(io) == IoTYPE_STD)
98 else if (fd <= PL_maxsysfd) {
101 savetype = IoTYPE(io);
104 else if (IoTYPE(io) == IoTYPE_PIPE)
105 result = PerlProc_pclose(IoIFP(io));
106 else if (IoIFP(io) != IoOFP(io)) {
108 result = PerlIO_close(IoOFP(io));
109 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
112 result = PerlIO_close(IoIFP(io));
115 result = PerlIO_close(IoIFP(io));
116 if (result == EOF && fd > PL_maxsysfd)
117 PerlIO_printf(Perl_error_log,
118 "Warning: unable to close filehandle %s properly.\n",
120 IoOFP(io) = IoIFP(io) = Nullfp;
124 #if defined(USE_64_BIT_RAWIO) && defined(O_LARGEFILE)
125 rawmode |= O_LARGEFILE;
129 #define O_ACCMODE 3 /* Assume traditional implementation */
132 switch (result = rawmode & O_ACCMODE) {
134 IoTYPE(io) = IoTYPE_RDONLY;
137 IoTYPE(io) = IoTYPE_WRONLY;
141 IoTYPE(io) = IoTYPE_RDWR;
145 writing = (result > 0);
146 fd = PerlLIO_open3(name, rawmode, rawperm);
153 if (result == O_RDONLY)
156 else if (rawmode & O_APPEND) {
158 if (result != O_WRONLY)
163 if (result == O_WRONLY)
170 if (rawmode & O_BINARY)
173 fp = PerlIO_fdopen(fd, fpmode);
183 char mode[4]; /* stdio file mode ("r\0", "rb\0", "r+b\0" etc.) */
186 type = savepvn(name, len);
191 name = SvPV(svs, l) ;
193 name = savepvn(name, len);
197 while (tlen && isSPACE(type[tlen-1]))
202 mode[0] = mode[1] = mode[2] = mode[3] = '\0';
204 if (*type == IoTYPE_RDWR && tlen > 1 && type[tlen-1] != IoTYPE_PIPE) { /* scary */
210 if (*type == IoTYPE_PIPE) {
211 if (num_svs && (tlen != 2 || type[1] != IoTYPE_STD)) {
213 Perl_croak(aTHX_ "Unknown open() mode '%.*s'", (int)olen, oname);
216 for (type++, tlen--; isSPACE(*type); type++, tlen--) ;
221 if (*name == '\0') { /* command is missing 19990114 */
223 if (ckWARN(WARN_PIPE))
224 Perl_warner(aTHX_ WARN_PIPE, "Missing command in piped open");
228 if (strNE(name,"-") || num_svs)
230 TAINT_PROPER("piped open");
231 if (name[len-1] == '|') {
234 if (ckWARN(WARN_PIPE))
235 Perl_warner(aTHX_ WARN_PIPE, "Can't open bidirectional pipe");
245 fp = PerlProc_popen(name,mode);
249 else if (*type == IoTYPE_WRONLY) {
250 TAINT_PROPER("open");
252 if (*type == IoTYPE_WRONLY) {
253 /* Two IoTYPE_WRONLYs in a row make for an IoTYPE_APPEND. */
254 mode[0] = IoTYPE(io) = IoTYPE_APPEND;
267 if (num_svs && tlen != 1)
278 if (!*name && supplied_fp)
282 for (; isSPACE(*name); name++) ;
287 gv = gv_fetchpv(name,FALSE,SVt_PVIO);
291 SETERRNO(EINVAL,SS$_IVCHAN);
296 PerlIO *fp = IoIFP(thatio);
297 /* Flush stdio buffer before dup. --mjd
298 * Unfortunately SEEK_CURing 0 seems to
299 * be optimized away on most platforms;
300 * only Solaris and Linux seem to flush
303 /* sfio fails to clear error on next
304 sfwrite, contrary to documentation.
306 if (PerlIO_seek(fp, 0, SEEK_CUR) == -1)
309 /* On the other hand, do all platforms
310 * take gracefully to flushing a read-only
311 * filehandle? Perhaps we should do
312 * fsetpos(src)+fgetpos(dst)? --nik */
314 fd = PerlIO_fileno(fp);
315 /* When dup()ing STDIN, STDOUT or STDERR
316 * explicitly set appropriate access mode */
317 if (IoIFP(thatio) == PerlIO_stdout()
318 || IoIFP(thatio) == PerlIO_stderr())
319 IoTYPE(io) = IoTYPE_WRONLY;
320 else if (IoIFP(thatio) == PerlIO_stdin())
321 IoTYPE(io) = IoTYPE_RDONLY;
322 /* When dup()ing a socket, say result is
324 else if (IoTYPE(thatio) == IoTYPE_SOCKET)
325 IoTYPE(io) = IoTYPE_SOCKET;
331 fd = PerlLIO_dup(fd);
334 if (!(fp = PerlIO_fdopen(fd,mode))) {
342 for (; isSPACE(*type); type++) ;
343 if (*type == IoTYPE_STD && !type[1]) {
344 fp = PerlIO_stdout();
345 IoTYPE(io) = IoTYPE_STD;
348 fp = PerlIO_open((num_svs ? name : type), mode);
352 else if (*type == IoTYPE_RDONLY) {
353 if (num_svs && tlen != 1)
356 for (type++; isSPACE(*type); type++) ;
367 if (*type == IoTYPE_STD && !type[1]) {
369 IoTYPE(io) = IoTYPE_STD;
372 fp = PerlIO_open((num_svs ? name : type), mode);
374 else if (tlen > 1 && type[tlen-1] == IoTYPE_PIPE) {
376 if (tlen != 2 || type[0] != IoTYPE_STD)
381 while (tlen && isSPACE(type[tlen-1]))
384 for (; isSPACE(*type); type++) ;
387 if (*name == '\0') { /* command is missing 19990114 */
389 if (ckWARN(WARN_PIPE))
390 Perl_warner(aTHX_ WARN_PIPE, "Missing command in piped open");
394 if (strNE(name,"-") || num_svs)
396 TAINT_PROPER("piped open");
405 fp = PerlProc_popen(name,mode);
407 IoTYPE(io) = IoTYPE_PIPE;
413 IoTYPE(io) = IoTYPE_RDONLY;
415 for (; isSPACE(*name); name++) ;
416 if (strEQ(name,"-")) {
418 IoTYPE(io) = IoTYPE_STD;
428 fp = PerlIO_open(name,mode);
434 if (ckWARN(WARN_NEWLINE) && IoTYPE(io) == IoTYPE_RDONLY && strchr(name, '\n'))
435 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "open");
439 IoTYPE(io) != IoTYPE_PIPE && IoTYPE(io) != IoTYPE_STD) {
441 if (PerlLIO_fstat(PerlIO_fileno(fp),&PL_statbuf) < 0) {
442 (void)PerlIO_close(fp);
445 if (S_ISSOCK(PL_statbuf.st_mode))
446 IoTYPE(io) = IoTYPE_SOCKET; /* in case a socket was passed in to us */
450 !(PL_statbuf.st_mode & S_IFMT)
454 && IoTYPE(io) != IoTYPE_WRONLY /* Dups of STD* filehandles already have */
455 && IoTYPE(io) != IoTYPE_RDONLY /* type so they aren't marked as sockets */
456 ) { /* on OS's that return 0 on fstat()ed pipe */
458 Sock_size_t buflen = sizeof tmpbuf;
459 if (PerlSock_getsockname(PerlIO_fileno(fp), (struct sockaddr *)tmpbuf,
461 || errno != ENOTSOCK)
462 IoTYPE(io) = IoTYPE_SOCKET; /* some OS's return 0 on fstat()ed socket */
463 /* but some return 0 for streams too, sigh */
467 if (saveifp) { /* must use old fp? */
468 fd = PerlIO_fileno(saveifp);
470 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
471 if (saveofp != saveifp) { /* was a socket? */
472 PerlIO_close(saveofp);
477 if (fd != PerlIO_fileno(fp)) {
481 PerlLIO_dup2(PerlIO_fileno(fp), fd);
483 sv = *av_fetch(PL_fdpid,PerlIO_fileno(fp),TRUE);
484 (void)SvUPGRADE(sv, SVt_IV);
487 sv = *av_fetch(PL_fdpid,fd,TRUE);
489 (void)SvUPGRADE(sv, SVt_IV);
498 #if defined(HAS_FCNTL) && defined(F_SETFD)
500 int save_errno = errno;
501 fd = PerlIO_fileno(fp);
502 fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
507 IoFLAGS(io) &= ~IOf_NOLINE;
510 if (IoTYPE(io) == IoTYPE_SOCKET
511 || (IoTYPE(io) == IoTYPE_WRONLY && S_ISCHR(PL_statbuf.st_mode)) )
521 if (!(IoOFP(io) = PerlIO_fdopen(PerlIO_fileno(fp),mode))) {
535 IoTYPE(io) = savetype;
540 Perl_nextargv(pTHX_ register GV *gv)
543 #ifndef FLEXFILENAMES
552 PL_argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO);
553 if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
554 IoFLAGS(io) &= ~IOf_START;
556 if (!PL_argvout_stack)
557 PL_argvout_stack = newAV();
558 av_push(PL_argvout_stack, SvREFCNT_inc(PL_defoutgv));
561 if (PL_filemode & (S_ISUID|S_ISGID)) {
562 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
564 (void)fchmod(PL_lastfd,PL_filemode);
566 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
570 while (av_len(GvAV(gv)) >= 0) {
573 sv = av_shift(GvAV(gv));
575 sv_setsv(GvSV(gv),sv);
576 SvSETMAGIC(GvSV(gv));
577 PL_oldname = SvPVx(GvSV(gv), oldlen);
578 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,Nullfp)) {
580 TAINT_PROPER("inplace open");
581 if (oldlen == 1 && *PL_oldname == '-') {
582 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
583 return IoIFP(GvIOp(gv));
585 #ifndef FLEXFILENAMES
586 filedev = PL_statbuf.st_dev;
587 fileino = PL_statbuf.st_ino;
589 PL_filemode = PL_statbuf.st_mode;
590 fileuid = PL_statbuf.st_uid;
591 filegid = PL_statbuf.st_gid;
592 if (!S_ISREG(PL_filemode)) {
593 if (ckWARN_d(WARN_INPLACE))
594 Perl_warner(aTHX_ WARN_INPLACE,
595 "Can't do inplace edit: %s is not a regular file",
601 char *star = strchr(PL_inplace, '*');
603 char *begin = PL_inplace;
604 sv_setpvn(sv, "", 0);
606 sv_catpvn(sv, begin, star - begin);
607 sv_catpvn(sv, PL_oldname, oldlen);
609 } while ((star = strchr(begin, '*')));
614 sv_catpv(sv,PL_inplace);
616 #ifndef FLEXFILENAMES
617 if (PerlLIO_stat(SvPVX(sv),&PL_statbuf) >= 0
618 && PL_statbuf.st_dev == filedev
619 && PL_statbuf.st_ino == fileino
621 || (_djstat_fail_bits & _STFAIL_TRUENAME)!=0
625 if (ckWARN_d(WARN_INPLACE))
626 Perl_warner(aTHX_ WARN_INPLACE,
627 "Can't do inplace edit: %s would not be unique",
634 #if !defined(DOSISH) && !defined(__CYGWIN__)
635 if (PerlLIO_rename(PL_oldname,SvPVX(sv)) < 0) {
636 if (ckWARN_d(WARN_INPLACE))
637 Perl_warner(aTHX_ WARN_INPLACE,
638 "Can't rename %s to %s: %s, skipping file",
639 PL_oldname, SvPVX(sv), Strerror(errno) );
645 (void)PerlLIO_unlink(SvPVX(sv));
646 (void)PerlLIO_rename(PL_oldname,SvPVX(sv));
647 do_open(gv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,O_RDONLY,0,Nullfp);
650 (void)UNLINK(SvPVX(sv));
651 if (link(PL_oldname,SvPVX(sv)) < 0) {
652 if (ckWARN_d(WARN_INPLACE))
653 Perl_warner(aTHX_ WARN_INPLACE,
654 "Can't rename %s to %s: %s, skipping file",
655 PL_oldname, SvPVX(sv), Strerror(errno) );
659 (void)UNLINK(PL_oldname);
663 #if !defined(DOSISH) && !defined(AMIGAOS)
664 # ifndef VMS /* Don't delete; use automatic file versioning */
665 if (UNLINK(PL_oldname) < 0) {
666 if (ckWARN_d(WARN_INPLACE))
667 Perl_warner(aTHX_ WARN_INPLACE,
668 "Can't remove %s: %s, skipping file",
669 PL_oldname, Strerror(errno) );
675 Perl_croak(aTHX_ "Can't do inplace edit without backup");
679 sv_setpvn(sv,">",!PL_inplace);
680 sv_catpvn(sv,PL_oldname,oldlen);
681 SETERRNO(0,0); /* in case sprintf set errno */
683 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
684 O_WRONLY|O_CREAT|O_TRUNC,0,Nullfp))
686 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
687 O_WRONLY|O_CREAT|OPEN_EXCL,0666,Nullfp))
690 if (ckWARN_d(WARN_INPLACE))
691 Perl_warner(aTHX_ WARN_INPLACE, "Can't do inplace edit on %s: %s",
692 PL_oldname, Strerror(errno) );
696 setdefout(PL_argvoutgv);
697 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
698 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
700 (void)fchmod(PL_lastfd,PL_filemode);
702 # if !(defined(WIN32) && defined(__BORLANDC__))
703 /* Borland runtime creates a readonly file! */
704 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
707 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
709 (void)fchown(PL_lastfd,fileuid,filegid);
712 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
717 return IoIFP(GvIOp(gv));
721 if (ckWARN_d(WARN_INPLACE)) {
723 if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0
724 && !S_ISREG(PL_statbuf.st_mode))
726 Perl_warner(aTHX_ WARN_INPLACE,
727 "Can't do inplace edit: %s is not a regular file",
731 Perl_warner(aTHX_ WARN_INPLACE, "Can't open %s: %s",
732 PL_oldname, Strerror(eno));
736 if (io && (IoFLAGS(io) & IOf_ARGV))
737 IoFLAGS(io) |= IOf_START;
739 (void)do_close(PL_argvoutgv,FALSE);
740 if (io && (IoFLAGS(io) & IOf_ARGV)
741 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
743 GV *oldout = (GV*)av_pop(PL_argvout_stack);
745 SvREFCNT_dec(oldout);
748 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
755 Perl_do_pipe(pTHX_ SV *sv, GV *rgv, GV *wgv)
774 if (PerlProc_pipe(fd) < 0)
776 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r");
777 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w");
778 IoIFP(wstio) = IoOFP(wstio);
779 IoTYPE(rstio) = IoTYPE_RDONLY;
780 IoTYPE(wstio) = IoTYPE_WRONLY;
781 if (!IoIFP(rstio) || !IoOFP(wstio)) {
782 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
783 else PerlLIO_close(fd[0]);
784 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
785 else PerlLIO_close(fd[1]);
789 sv_setsv(sv,&PL_sv_yes);
793 sv_setsv(sv,&PL_sv_undef);
798 /* explicit renamed to avoid C++ conflict -- kja */
800 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
807 if (!gv || SvTYPE(gv) != SVt_PVGV) {
809 SETERRNO(EBADF,SS$_IVCHAN);
813 if (!io) { /* never opened */
816 if (ckWARN(WARN_UNOPENED)) /* no check for closed here */
817 report_evil_fh(gv, io, PL_op->op_type);
818 SETERRNO(EBADF,SS$_IVCHAN);
822 retval = io_close(io, not_implicit);
826 IoLINES_LEFT(io) = IoPAGE_LEN(io);
828 IoTYPE(io) = IoTYPE_CLOSED;
833 Perl_io_close(pTHX_ IO *io, bool not_implicit)
839 if (IoTYPE(io) == IoTYPE_PIPE) {
840 status = PerlProc_pclose(IoIFP(io));
842 STATUS_NATIVE_SET(status);
843 retval = (STATUS_POSIX == 0);
846 retval = (status != -1);
849 else if (IoTYPE(io) == IoTYPE_STD)
852 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
853 retval = (PerlIO_close(IoOFP(io)) != EOF);
854 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
857 retval = (PerlIO_close(IoIFP(io)) != EOF);
859 IoOFP(io) = IoIFP(io) = Nullfp;
861 else if (not_implicit) {
862 SETERRNO(EBADF,SS$_IVCHAN);
869 Perl_do_eof(pTHX_ GV *gv)
879 else if (ckWARN(WARN_IO)
880 && (IoTYPE(io) == IoTYPE_WRONLY || IoIFP(io) == PerlIO_stdout()
881 || IoIFP(io) == PerlIO_stderr()))
883 /* integrate to report_evil_fh()? */
886 SV* sv = sv_newmortal();
887 gv_efullname4(sv, gv, Nullch, FALSE);
888 name = SvPV_nolen(sv);
891 Perl_warner(aTHX_ WARN_IO,
892 "Filehandle %s opened only for output", name);
894 Perl_warner(aTHX_ WARN_IO,
895 "Filehandle opened only for output");
900 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
901 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
902 return FALSE; /* this is the most usual case */
905 ch = PerlIO_getc(IoIFP(io));
907 (void)PerlIO_ungetc(IoIFP(io),ch);
910 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
911 if (PerlIO_get_cnt(IoIFP(io)) < -1)
912 PerlIO_set_cnt(IoIFP(io),-1);
914 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
915 if (!nextargv(PL_argvgv)) /* get another fp handy */
919 return TRUE; /* normal fp, definitely end of file */
925 Perl_do_tell(pTHX_ GV *gv)
930 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
931 #ifdef ULTRIX_STDIO_BOTCH
933 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
935 return PerlIO_tell(fp);
939 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
940 report_evil_fh(gv, io, PL_op->op_type);
942 SETERRNO(EBADF,RMS$_IFI);
947 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
952 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
953 #ifdef ULTRIX_STDIO_BOTCH
955 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
957 return PerlIO_seek(fp, pos, whence) >= 0;
961 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
962 report_evil_fh(gv, io, PL_op->op_type);
964 SETERRNO(EBADF,RMS$_IFI);
969 Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
974 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
975 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
978 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
979 report_evil_fh(gv, io, PL_op->op_type);
981 SETERRNO(EBADF,RMS$_IFI);
986 Perl_mode_from_discipline(pTHX_ SV *discp)
991 char *s = SvPV(discp,len);
996 if (len > 3 && strnEQ(s+1, "raw", 3)
997 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1006 if (len > 4 && strnEQ(s+1, "crlf", 4)
1007 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1016 goto fail_discipline;
1019 else if (isSPACE(*s)) {
1026 end = strchr(s+1, ':');
1029 Perl_croak(aTHX_ "Unknown discipline '%.*s'", end-s, s);
1037 Perl_do_binmode(pTHX_ PerlIO *fp, int iotype, int mode)
1040 # if defined(atarist) || defined(__MINT__)
1041 if (!PerlIO_flush(fp)) {
1042 if (mode & O_BINARY)
1043 ((FILE*)fp)->_flag |= _IOBIN;
1045 ((FILE*)fp)->_flag &= ~ _IOBIN;
1050 if (PerlLIO_setmode(PerlIO_fileno(fp), mode) != -1) {
1051 # if defined(WIN32) && defined(__BORLANDC__)
1052 /* The translation mode of the stream is maintained independent
1053 * of the translation mode of the fd in the Borland RTL (heavy
1054 * digging through their runtime sources reveal). User has to
1055 * set the mode explicitly for the stream (though they don't
1056 * document this anywhere). GSAR 97-5-24
1058 PerlIO_seek(fp,0L,0);
1059 if (mode & O_BINARY)
1060 ((FILE*)fp)->flags |= _F_BIN;
1062 ((FILE*)fp)->flags &= ~ _F_BIN;
1070 # if defined(USEMYBINMODE)
1071 if (my_binmode(fp, iotype, mode) != FALSE)
1081 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP)
1082 /* code courtesy of William Kucharski */
1085 I32 my_chsize(fd, length)
1086 I32 fd; /* file descriptor */
1087 Off_t length; /* length to set file to */
1090 struct stat filebuf;
1092 if (PerlLIO_fstat(fd, &filebuf) < 0)
1095 if (filebuf.st_size < length) {
1097 /* extend file length */
1099 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
1102 /* write a "0" byte */
1104 if ((PerlLIO_write(fd, "", 1)) != 1)
1108 /* truncate length */
1112 fl.l_start = length;
1113 fl.l_type = F_WRLCK; /* write lock on file space */
1116 * This relies on the UNDOCUMENTED F_FREESP argument to
1117 * fcntl(2), which truncates the file so that it ends at the
1118 * position indicated by fl.l_start.
1120 * Will minor miracles never cease?
1123 if (fcntl(fd, F_FREESP, &fl) < 0)
1130 #endif /* F_FREESP */
1133 Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
1135 register char *tmps;
1138 /* assuming fp is checked earlier */
1144 if (SvIOK(sv) && SvIVX(sv) != 0) {
1145 PerlIO_printf(fp, PL_ofmt, (NV)SvIVX(sv));
1146 return !PerlIO_error(fp);
1148 if ( (SvNOK(sv) && SvNVX(sv) != 0.0)
1149 || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) {
1150 PerlIO_printf(fp, PL_ofmt, SvNVX(sv));
1151 return !PerlIO_error(fp);
1154 switch (SvTYPE(sv)) {
1158 if (ckWARN(WARN_UNINITIALIZED))
1167 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
1169 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
1170 return !PerlIO_error(fp);
1175 /* XXX Fix this when the I/O disciplines arrive. XXX */
1177 sv_utf8_downgrade(sv, FALSE);
1179 tmps = SvPV(sv, len);
1182 /* To detect whether the process is about to overstep its
1183 * filesize limit we would need getrlimit(). We could then
1184 * also transparently raise the limit with setrlimit() --
1185 * but only until the system hard limit/the filesystem limit,
1186 * at which we would get EPERM. Note that when using buffered
1187 * io the write failure can be delayed until the flush/close. --jhi */
1188 if (len && (PerlIO_write(fp,tmps,len) == 0))
1190 return !PerlIO_error(fp);
1200 if (PL_op->op_flags & OPf_REF) {
1205 if (io && IoIFP(io)) {
1207 sv_setpv(PL_statname,"");
1208 PL_laststype = OP_STAT;
1209 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
1213 return PL_laststatval;
1214 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1215 report_evil_fh(gv, io, PL_op->op_type);
1217 sv_setpv(PL_statname,"");
1218 return (PL_laststatval = -1);
1226 if (SvTYPE(sv) == SVt_PVGV) {
1230 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
1237 sv_setpv(PL_statname, s);
1238 PL_laststype = OP_STAT;
1239 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1240 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
1241 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "stat");
1242 return PL_laststatval;
1252 if (PL_op->op_flags & OPf_REF) {
1254 if (cGVOP_gv == PL_defgv) {
1255 if (PL_laststype != OP_LSTAT)
1256 Perl_croak(aTHX_ "The stat preceding -l _ wasn't an lstat");
1257 return PL_laststatval;
1259 Perl_croak(aTHX_ "You can't use -l on a filehandle");
1262 PL_laststype = OP_LSTAT;
1266 sv_setpv(PL_statname,SvPV(sv, n_a));
1267 PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache);
1268 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
1269 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "lstat");
1270 return PL_laststatval;
1274 Perl_do_aexec(pTHX_ SV *really, register SV **mark, register SV **sp)
1276 return do_aexec5(really, mark, sp, 0, 0);
1280 Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1281 int fd, int do_report)
1283 #ifdef MACOS_TRADITIONAL
1284 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1292 New(401,PL_Argv, sp - mark + 1, char*);
1294 while (++mark <= sp) {
1296 *a++ = SvPVx(*mark, n_a);
1301 if (*PL_Argv[0] != '/') /* will execvp use PATH? */
1302 TAINT_ENV(); /* testing IFS here is overkill, probably */
1303 if (really && *(tmps = SvPV(really, n_a)))
1304 PerlProc_execvp(tmps,PL_Argv);
1306 PerlProc_execvp(PL_Argv[0],PL_Argv);
1307 if (ckWARN(WARN_EXEC))
1308 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1309 PL_Argv[0], Strerror(errno));
1313 PerlLIO_write(fd, (void*)&e, sizeof(int));
1323 Perl_do_execfree(pTHX)
1327 PL_Argv = Null(char **);
1335 #if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC) && !defined(MACOS_TRADITIONAL)
1338 Perl_do_exec(pTHX_ char *cmd)
1340 return do_exec3(cmd,0,0);
1344 Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report)
1350 while (*cmd && isSPACE(*cmd))
1353 /* save an extra exec if possible */
1356 if (strnEQ(cmd,PL_cshname,PL_cshlen) && strnEQ(cmd+PL_cshlen," -c",3)) {
1358 s = cmd+PL_cshlen+3;
1372 if (s[-1] == '\'') {
1374 PerlProc_execl(PL_cshname,"csh", flags,ncmd,(char*)0);
1382 /* see if there are shell metacharacters in it */
1384 if (*cmd == '.' && isSPACE(cmd[1]))
1387 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1390 for (s = cmd; *s && isALNUM(*s); s++) ; /* catch VAR=val gizmo */
1394 for (s = cmd; *s; s++) {
1395 if (*s != ' ' && !isALPHA(*s) && strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1396 if (*s == '\n' && !s[1]) {
1400 /* handle the 2>&1 construct at the end */
1401 if (*s == '>' && s[1] == '&' && s[2] == '1'
1402 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1403 && (!s[3] || isSPACE(s[3])))
1407 while (*t && isSPACE(*t))
1409 if (!*t && (dup2(1,2) != -1)) {
1415 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
1420 New(402,PL_Argv, (s - cmd) / 2 + 2, char*);
1421 PL_Cmd = savepvn(cmd, s-cmd);
1423 for (s = PL_Cmd; *s;) {
1424 while (*s && isSPACE(*s)) s++;
1427 while (*s && !isSPACE(*s)) s++;
1433 PerlProc_execvp(PL_Argv[0],PL_Argv);
1434 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1442 if (ckWARN(WARN_EXEC))
1443 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1444 PL_Argv[0], Strerror(errno));
1446 PerlLIO_write(fd, (void*)&e, sizeof(int));
1455 #endif /* OS2 || WIN32 */
1458 Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
1463 register I32 tot = 0;
1466 SV **oldmark = mark;
1469 #define APPLY_TAINT_PROPER() \
1471 if (PL_tainted) { TAINT_PROPER(what); } \
1474 /* This is a first heuristic; it doesn't catch tainting magic. */
1476 while (++mark <= sp) {
1477 if (SvTAINTED(*mark)) {
1487 APPLY_TAINT_PROPER();
1490 APPLY_TAINT_PROPER();
1492 while (++mark <= sp) {
1493 char *name = SvPVx(*mark, n_a);
1494 APPLY_TAINT_PROPER();
1495 if (PerlLIO_chmod(name, val))
1503 APPLY_TAINT_PROPER();
1504 if (sp - mark > 2) {
1505 val = SvIVx(*++mark);
1506 val2 = SvIVx(*++mark);
1507 APPLY_TAINT_PROPER();
1509 while (++mark <= sp) {
1510 char *name = SvPVx(*mark, n_a);
1511 APPLY_TAINT_PROPER();
1512 if (PerlLIO_chown(name, val, val2))
1519 XXX Should we make lchown() directly available from perl?
1520 For now, we'll let Configure test for HAS_LCHOWN, but do
1521 nothing in the core.
1527 APPLY_TAINT_PROPER();
1530 s = SvPVx(*++mark, n_a);
1532 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1534 if (!(val = whichsig(s)))
1535 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
1539 APPLY_TAINT_PROPER();
1542 /* kill() doesn't do process groups (job trees?) under VMS */
1543 if (val < 0) val = -val;
1544 if (val == SIGKILL) {
1545 # include <starlet.h>
1546 /* Use native sys$delprc() to insure that target process is
1547 * deleted; supervisor-mode images don't pay attention to
1548 * CRTL's emulation of Unix-style signals and kill()
1550 while (++mark <= sp) {
1551 I32 proc = SvIVx(*mark);
1552 register unsigned long int __vmssts;
1553 APPLY_TAINT_PROPER();
1554 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1558 case SS$_NOSUCHNODE:
1559 SETERRNO(ESRCH,__vmssts);
1562 SETERRNO(EPERM,__vmssts);
1565 SETERRNO(EVMSERR,__vmssts);
1574 while (++mark <= sp) {
1575 I32 proc = SvIVx(*mark);
1576 APPLY_TAINT_PROPER();
1578 if (PerlProc_killpg(proc,val)) /* BSD */
1580 if (PerlProc_kill(-proc,val)) /* SYSV */
1586 while (++mark <= sp) {
1587 I32 proc = SvIVx(*mark);
1588 APPLY_TAINT_PROPER();
1589 if (PerlProc_kill(proc, val))
1597 APPLY_TAINT_PROPER();
1599 while (++mark <= sp) {
1600 s = SvPVx(*mark, n_a);
1601 APPLY_TAINT_PROPER();
1602 if (PL_euid || PL_unsafe) {
1606 else { /* don't let root wipe out directories without -U */
1607 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1619 APPLY_TAINT_PROPER();
1620 if (sp - mark > 2) {
1621 #if defined(I_UTIME) || defined(VMS)
1622 struct utimbuf utbuf;
1630 Zero(&utbuf, sizeof utbuf, char);
1632 utbuf.actime = (Time_t)SvNVx(*++mark); /* time accessed */
1633 utbuf.modtime = (Time_t)SvNVx(*++mark); /* time modified */
1635 utbuf.actime = (Time_t)SvIVx(*++mark); /* time accessed */
1636 utbuf.modtime = (Time_t)SvIVx(*++mark); /* time modified */
1638 APPLY_TAINT_PROPER();
1640 while (++mark <= sp) {
1641 char *name = SvPVx(*mark, n_a);
1642 APPLY_TAINT_PROPER();
1643 if (PerlLIO_utime(name, &utbuf))
1654 #undef APPLY_TAINT_PROPER
1657 /* Do the permissions allow some operation? Assumes statcache already set. */
1658 #ifndef VMS /* VMS' cando is in vms.c */
1660 Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register Stat_t *statbufp)
1661 /* Note: we use `effective' both for uids and gids.
1662 * Here we are betting on Uid_t being equal or wider than Gid_t. */
1665 /* [Comments and code from Len Reed]
1666 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1667 * to write-protected files. The execute permission bit is set
1668 * by the Miscrosoft C library stat() function for the following:
1673 * All files and directories are readable.
1674 * Directories and special files, e.g. "CON", cannot be
1676 * [Comment by Tom Dinger -- a directory can have the write-protect
1677 * bit set in the file system, but DOS permits changes to
1678 * the directory anyway. In addition, all bets are off
1679 * here for networked software, such as Novell and
1683 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1684 * too so it will actually look into the files for magic numbers
1686 return (mode & statbufp->st_mode) ? TRUE : FALSE;
1688 #else /* ! DOSISH */
1689 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
1690 if (mode == S_IXUSR) {
1691 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1695 return TRUE; /* root reads and writes anything */
1698 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1699 if (statbufp->st_mode & mode)
1700 return TRUE; /* ok as "user" */
1702 else if (ingroup(statbufp->st_gid,effective)) {
1703 if (statbufp->st_mode & mode >> 3)
1704 return TRUE; /* ok as "group" */
1706 else if (statbufp->st_mode & mode >> 6)
1707 return TRUE; /* ok as "other" */
1709 #endif /* ! DOSISH */
1714 Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective)
1716 #ifdef MACOS_TRADITIONAL
1717 /* This is simply not correct for AppleShare, but fix it yerself. */
1720 if (testgid == (effective ? PL_egid : PL_gid))
1722 #ifdef HAS_GETGROUPS
1727 Groups_t gary[NGROUPS];
1730 anum = getgroups(NGROUPS,gary);
1732 if (gary[anum] == testgid)
1740 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1743 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
1749 key = (key_t)SvNVx(*++mark);
1750 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1751 flags = SvIVx(*++mark);
1757 return msgget(key, flags);
1761 return semget(key, n, flags);
1765 return shmget(key, n, flags);
1767 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1769 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1772 return -1; /* should never happen */
1776 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
1781 I32 id, n, cmd, infosize, getinfo;
1784 id = SvIVx(*++mark);
1785 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1786 cmd = SvIVx(*++mark);
1789 getinfo = (cmd == IPC_STAT);
1795 if (cmd == IPC_STAT || cmd == IPC_SET)
1796 infosize = sizeof(struct msqid_ds);
1801 if (cmd == IPC_STAT || cmd == IPC_SET)
1802 infosize = sizeof(struct shmid_ds);
1808 if (cmd == IPC_STAT || cmd == IPC_SET)
1809 infosize = sizeof(struct semid_ds);
1810 else if (cmd == GETALL || cmd == SETALL)
1812 struct semid_ds semds;
1814 #ifdef EXTRA_F_IN_SEMUN_BUF
1815 semun.buff = &semds;
1819 getinfo = (cmd == GETALL);
1820 if (Semctl(id, 0, IPC_STAT, semun) == -1)
1822 infosize = semds.sem_nsems * sizeof(short);
1823 /* "short" is technically wrong but much more portable
1824 than guessing about u_?short(_t)? */
1827 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1831 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1833 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1842 SvPV_force(astr, len);
1843 a = SvGROW(astr, infosize+1);
1847 a = SvPV(astr, len);
1848 if (len != infosize)
1849 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
1858 a = INT2PTR(char *,i); /* ouch */
1865 ret = msgctl(id, cmd, (struct msqid_ds *)a);
1871 union semun unsemds;
1873 #ifdef EXTRA_F_IN_SEMUN_BUF
1874 unsemds.buff = (struct semid_ds *)a;
1876 unsemds.buf = (struct semid_ds *)a;
1878 ret = Semctl(id, n, cmd, unsemds);
1880 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1887 ret = shmctl(id, cmd, (struct shmid_ds *)a);
1891 if (getinfo && ret >= 0) {
1892 SvCUR_set(astr, infosize);
1893 *SvEND(astr) = '\0';
1900 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
1906 I32 id, msize, flags;
1909 id = SvIVx(*++mark);
1911 flags = SvIVx(*++mark);
1912 mbuf = SvPV(mstr, len);
1913 if ((msize = len - sizeof(long)) < 0)
1914 Perl_croak(aTHX_ "Arg too short for msgsnd");
1916 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
1918 Perl_croak(aTHX_ "msgsnd not implemented");
1923 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
1930 I32 id, msize, flags, ret;
1933 id = SvIVx(*++mark);
1935 /* suppress warning when reading into undef var --jhi */
1937 sv_setpvn(mstr, "", 0);
1938 msize = SvIVx(*++mark);
1939 mtype = (long)SvIVx(*++mark);
1940 flags = SvIVx(*++mark);
1941 SvPV_force(mstr, len);
1942 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
1945 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
1947 SvCUR_set(mstr, sizeof(long)+ret);
1948 *SvEND(mstr) = '\0';
1949 #ifndef INCOMPLETE_TAINTS
1950 /* who knows who has been playing with this message? */
1956 Perl_croak(aTHX_ "msgrcv not implemented");
1961 Perl_do_semop(pTHX_ SV **mark, SV **sp)
1970 id = SvIVx(*++mark);
1972 opbuf = SvPV(opstr, opsize);
1973 if (opsize < sizeof(struct sembuf)
1974 || (opsize % sizeof(struct sembuf)) != 0) {
1975 SETERRNO(EINVAL,LIB$_INVARG);
1979 return semop(id, (struct sembuf *)opbuf, opsize/sizeof(struct sembuf));
1981 Perl_croak(aTHX_ "semop not implemented");
1986 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
1992 I32 id, mpos, msize;
1994 struct shmid_ds shmds;
1996 id = SvIVx(*++mark);
1998 mpos = SvIVx(*++mark);
1999 msize = SvIVx(*++mark);
2001 if (shmctl(id, IPC_STAT, &shmds) == -1)
2003 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
2004 SETERRNO(EFAULT,SS$_ACCVIO); /* can't do as caller requested */
2007 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
2008 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2010 if (optype == OP_SHMREAD) {
2011 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2013 sv_setpvn(mstr, "", 0);
2014 SvPV_force(mstr, len);
2015 mbuf = SvGROW(mstr, msize+1);
2017 Copy(shm + mpos, mbuf, msize, char);
2018 SvCUR_set(mstr, msize);
2019 *SvEND(mstr) = '\0';
2021 #ifndef INCOMPLETE_TAINTS
2022 /* who knows who has been playing with this shared memory? */
2029 mbuf = SvPV(mstr, len);
2030 if ((n = len) > msize)
2032 Copy(mbuf, shm + mpos, n, char);
2034 memzero(shm + mpos + n, msize - n);
2038 Perl_croak(aTHX_ "shm I/O not implemented");
2042 #endif /* SYSV IPC */