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. */
59 #if defined(HAS_SOCKET) && !defined(VMS) /* VMS handles sockets via vmsish.h */
60 # include <sys/socket.h>
61 # if defined(USE_SOCKS) && defined(I_SOCKS)
69 # include <net/errno.h>
75 Perl_do_open(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
76 int rawmode, int rawperm, PerlIO *supplied_fp)
78 return do_open9(gv, name, len, as_raw, rawmode, rawperm,
79 supplied_fp, Nullsv, 0);
83 Perl_do_open9(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
84 int rawmode, int rawperm, PerlIO *supplied_fp, SV *svs,
87 register IO *io = GvIOn(gv);
88 PerlIO *saveifp = Nullfp;
89 PerlIO *saveofp = Nullfp;
90 char savetype = IoTYPE_CLOSED;
95 bool was_fdopen = FALSE;
96 bool in_raw = 0, in_crlf = 0, out_raw = 0, out_crlf = 0;
98 PL_forkprocess = 1; /* assume true if no fork */
100 if (PL_op && PL_op->op_type == OP_OPEN) {
101 /* set up disciplines */
102 U8 flags = PL_op->op_private;
103 in_raw = (flags & OPpOPEN_IN_RAW);
104 in_crlf = (flags & OPpOPEN_IN_CRLF);
105 out_raw = (flags & OPpOPEN_OUT_RAW);
106 out_crlf = (flags & OPpOPEN_OUT_CRLF);
110 fd = PerlIO_fileno(IoIFP(io));
111 if (IoTYPE(io) == IoTYPE_STD)
113 else if (fd <= PL_maxsysfd) {
116 savetype = IoTYPE(io);
119 else if (IoTYPE(io) == IoTYPE_PIPE)
120 result = PerlProc_pclose(IoIFP(io));
121 else if (IoIFP(io) != IoOFP(io)) {
123 result = PerlIO_close(IoOFP(io));
124 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
127 result = PerlIO_close(IoIFP(io));
130 result = PerlIO_close(IoIFP(io));
131 if (result == EOF && fd > PL_maxsysfd)
132 PerlIO_printf(Perl_error_log,
133 "Warning: unable to close filehandle %s properly.\n",
135 IoOFP(io) = IoIFP(io) = Nullfp;
139 #if defined(USE_64_BIT_RAWIO) && defined(O_LARGEFILE)
140 rawmode |= O_LARGEFILE;
144 #define O_ACCMODE 3 /* Assume traditional implementation */
147 switch (result = rawmode & O_ACCMODE) {
149 IoTYPE(io) = IoTYPE_RDONLY;
152 IoTYPE(io) = IoTYPE_WRONLY;
156 IoTYPE(io) = IoTYPE_RDWR;
160 writing = (result > 0);
161 fd = PerlLIO_open3(name, rawmode, rawperm);
168 if (result == O_RDONLY)
171 else if (rawmode & O_APPEND) {
173 if (result != O_WRONLY)
178 if (result == O_WRONLY)
185 if (rawmode & O_BINARY)
188 fp = PerlIO_fdopen(fd, fpmode);
198 char mode[4]; /* stdio file mode ("r\0", "rb\0", "r+b\0" etc.) */
201 type = savepvn(name, len);
206 name = SvPV(svs, l) ;
208 name = savepvn(name, len);
212 while (tlen && isSPACE(type[tlen-1]))
217 mode[0] = mode[1] = mode[2] = mode[3] = '\0';
219 if (*type == IoTYPE_RDWR && tlen > 1 && type[tlen-1] != IoTYPE_PIPE) { /* scary */
225 if (*type == IoTYPE_PIPE) {
226 if (num_svs && (tlen != 2 || type[1] != IoTYPE_STD)) {
228 Perl_croak(aTHX_ "Unknown open() mode '%.*s'", (int)olen, oname);
231 for (type++, tlen--; isSPACE(*type); type++, tlen--) ;
236 if (*name == '\0') { /* command is missing 19990114 */
238 if (ckWARN(WARN_PIPE))
239 Perl_warner(aTHX_ WARN_PIPE, "Missing command in piped open");
243 if (strNE(name,"-") || num_svs)
245 TAINT_PROPER("piped open");
246 if (name[len-1] == '|') {
249 if (ckWARN(WARN_PIPE))
250 Perl_warner(aTHX_ WARN_PIPE, "Can't open bidirectional pipe");
260 fp = PerlProc_popen(name,mode);
264 else if (*type == IoTYPE_WRONLY) {
265 TAINT_PROPER("open");
267 if (*type == IoTYPE_WRONLY) {
268 /* Two IoTYPE_WRONLYs in a row make for an IoTYPE_APPEND. */
269 mode[0] = IoTYPE(io) = IoTYPE_APPEND;
282 if (num_svs && tlen != 1)
293 if (!*name && supplied_fp)
297 for (; isSPACE(*name); name++) ;
302 gv = gv_fetchpv(name,FALSE,SVt_PVIO);
306 SETERRNO(EINVAL,SS$_IVCHAN);
311 PerlIO *fp = IoIFP(thatio);
312 /* Flush stdio buffer before dup. --mjd
313 * Unfortunately SEEK_CURing 0 seems to
314 * be optimized away on most platforms;
315 * only Solaris and Linux seem to flush
318 /* sfio fails to clear error on next
319 sfwrite, contrary to documentation.
321 if (PerlIO_seek(fp, 0, SEEK_CUR) == -1)
324 /* On the other hand, do all platforms
325 * take gracefully to flushing a read-only
326 * filehandle? Perhaps we should do
327 * fsetpos(src)+fgetpos(dst)? --nik */
329 fd = PerlIO_fileno(fp);
330 /* When dup()ing STDIN, STDOUT or STDERR
331 * explicitly set appropriate access mode */
332 if (IoIFP(thatio) == PerlIO_stdout()
333 || IoIFP(thatio) == PerlIO_stderr())
334 IoTYPE(io) = IoTYPE_WRONLY;
335 else if (IoIFP(thatio) == PerlIO_stdin())
336 IoTYPE(io) = IoTYPE_RDONLY;
337 /* When dup()ing a socket, say result is
339 else if (IoTYPE(thatio) == IoTYPE_SOCKET)
340 IoTYPE(io) = IoTYPE_SOCKET;
346 fd = PerlLIO_dup(fd);
349 if (!(fp = PerlIO_fdopen(fd,mode))) {
357 for (; isSPACE(*type); type++) ;
358 if (*type == IoTYPE_STD && !type[1]) {
359 fp = PerlIO_stdout();
360 IoTYPE(io) = IoTYPE_STD;
363 fp = PerlIO_open((num_svs ? name : type), mode);
367 else if (*type == IoTYPE_RDONLY) {
368 if (num_svs && tlen != 1)
371 for (type++; isSPACE(*type); type++) ;
382 if (*type == IoTYPE_STD && !type[1]) {
384 IoTYPE(io) = IoTYPE_STD;
387 fp = PerlIO_open((num_svs ? name : type), mode);
389 else if (tlen > 1 && type[tlen-1] == IoTYPE_PIPE) {
391 if (tlen != 2 || type[0] != IoTYPE_STD)
396 while (tlen && isSPACE(type[tlen-1]))
399 for (; isSPACE(*type); type++) ;
402 if (*name == '\0') { /* command is missing 19990114 */
404 if (ckWARN(WARN_PIPE))
405 Perl_warner(aTHX_ WARN_PIPE, "Missing command in piped open");
409 if (strNE(name,"-") || num_svs)
411 TAINT_PROPER("piped open");
420 fp = PerlProc_popen(name,mode);
422 IoTYPE(io) = IoTYPE_PIPE;
428 IoTYPE(io) = IoTYPE_RDONLY;
430 for (; isSPACE(*name); name++) ;
431 if (strEQ(name,"-")) {
433 IoTYPE(io) = IoTYPE_STD;
443 fp = PerlIO_open(name,mode);
449 if (ckWARN(WARN_NEWLINE) && IoTYPE(io) == IoTYPE_RDONLY && strchr(name, '\n'))
450 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "open");
454 IoTYPE(io) != IoTYPE_PIPE && IoTYPE(io) != IoTYPE_STD) {
456 if (PerlLIO_fstat(PerlIO_fileno(fp),&PL_statbuf) < 0) {
457 (void)PerlIO_close(fp);
460 if (S_ISSOCK(PL_statbuf.st_mode))
461 IoTYPE(io) = IoTYPE_SOCKET; /* in case a socket was passed in to us */
465 !(PL_statbuf.st_mode & S_IFMT)
469 && IoTYPE(io) != IoTYPE_WRONLY /* Dups of STD* filehandles already have */
470 && IoTYPE(io) != IoTYPE_RDONLY /* type so they aren't marked as sockets */
471 ) { /* on OS's that return 0 on fstat()ed pipe */
473 Sock_size_t buflen = sizeof tmpbuf;
474 if (PerlSock_getsockname(PerlIO_fileno(fp), (struct sockaddr *)tmpbuf,
476 || errno != ENOTSOCK)
477 IoTYPE(io) = IoTYPE_SOCKET; /* some OS's return 0 on fstat()ed socket */
478 /* but some return 0 for streams too, sigh */
482 if (saveifp) { /* must use old fp? */
483 fd = PerlIO_fileno(saveifp);
485 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
486 if (saveofp != saveifp) { /* was a socket? */
487 PerlIO_close(saveofp);
492 if (fd != PerlIO_fileno(fp)) {
496 PerlLIO_dup2(PerlIO_fileno(fp), fd);
498 sv = *av_fetch(PL_fdpid,PerlIO_fileno(fp),TRUE);
499 (void)SvUPGRADE(sv, SVt_IV);
502 sv = *av_fetch(PL_fdpid,fd,TRUE);
504 (void)SvUPGRADE(sv, SVt_IV);
513 #if defined(HAS_FCNTL) && defined(F_SETFD)
515 int save_errno = errno;
516 fd = PerlIO_fileno(fp);
517 fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
522 IoFLAGS(io) &= ~IOf_NOLINE;
525 if (IoTYPE(io) == IoTYPE_SOCKET
526 || (IoTYPE(io) == IoTYPE_WRONLY && S_ISCHR(PL_statbuf.st_mode)) )
536 if (!(IoOFP(io) = PerlIO_fdopen(PerlIO_fileno(fp),mode))) {
550 IoTYPE(io) = savetype;
555 Perl_nextargv(pTHX_ register GV *gv)
558 #ifndef FLEXFILENAMES
567 PL_argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO);
568 if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
569 IoFLAGS(io) &= ~IOf_START;
571 if (!PL_argvout_stack)
572 PL_argvout_stack = newAV();
573 av_push(PL_argvout_stack, SvREFCNT_inc(PL_defoutgv));
576 if (PL_filemode & (S_ISUID|S_ISGID)) {
577 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
579 (void)fchmod(PL_lastfd,PL_filemode);
581 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
585 while (av_len(GvAV(gv)) >= 0) {
588 sv = av_shift(GvAV(gv));
590 sv_setsv(GvSV(gv),sv);
591 SvSETMAGIC(GvSV(gv));
592 PL_oldname = SvPVx(GvSV(gv), oldlen);
593 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,Nullfp)) {
595 TAINT_PROPER("inplace open");
596 if (oldlen == 1 && *PL_oldname == '-') {
597 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
598 return IoIFP(GvIOp(gv));
600 #ifndef FLEXFILENAMES
601 filedev = PL_statbuf.st_dev;
602 fileino = PL_statbuf.st_ino;
604 PL_filemode = PL_statbuf.st_mode;
605 fileuid = PL_statbuf.st_uid;
606 filegid = PL_statbuf.st_gid;
607 if (!S_ISREG(PL_filemode)) {
608 if (ckWARN_d(WARN_INPLACE))
609 Perl_warner(aTHX_ WARN_INPLACE,
610 "Can't do inplace edit: %s is not a regular file",
616 char *star = strchr(PL_inplace, '*');
618 char *begin = PL_inplace;
619 sv_setpvn(sv, "", 0);
621 sv_catpvn(sv, begin, star - begin);
622 sv_catpvn(sv, PL_oldname, oldlen);
624 } while ((star = strchr(begin, '*')));
629 sv_catpv(sv,PL_inplace);
631 #ifndef FLEXFILENAMES
632 if (PerlLIO_stat(SvPVX(sv),&PL_statbuf) >= 0
633 && PL_statbuf.st_dev == filedev
634 && PL_statbuf.st_ino == fileino
636 || (_djstat_fail_bits & _STFAIL_TRUENAME)!=0
640 if (ckWARN_d(WARN_INPLACE))
641 Perl_warner(aTHX_ WARN_INPLACE,
642 "Can't do inplace edit: %s would not be unique",
649 #if !defined(DOSISH) && !defined(__CYGWIN__)
650 if (PerlLIO_rename(PL_oldname,SvPVX(sv)) < 0) {
651 if (ckWARN_d(WARN_INPLACE))
652 Perl_warner(aTHX_ WARN_INPLACE,
653 "Can't rename %s to %s: %s, skipping file",
654 PL_oldname, SvPVX(sv), Strerror(errno) );
660 (void)PerlLIO_unlink(SvPVX(sv));
661 (void)PerlLIO_rename(PL_oldname,SvPVX(sv));
662 do_open(gv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,O_RDONLY,0,Nullfp);
665 (void)UNLINK(SvPVX(sv));
666 if (link(PL_oldname,SvPVX(sv)) < 0) {
667 if (ckWARN_d(WARN_INPLACE))
668 Perl_warner(aTHX_ WARN_INPLACE,
669 "Can't rename %s to %s: %s, skipping file",
670 PL_oldname, SvPVX(sv), Strerror(errno) );
674 (void)UNLINK(PL_oldname);
678 #if !defined(DOSISH) && !defined(AMIGAOS)
679 # ifndef VMS /* Don't delete; use automatic file versioning */
680 if (UNLINK(PL_oldname) < 0) {
681 if (ckWARN_d(WARN_INPLACE))
682 Perl_warner(aTHX_ WARN_INPLACE,
683 "Can't remove %s: %s, skipping file",
684 PL_oldname, Strerror(errno) );
690 Perl_croak(aTHX_ "Can't do inplace edit without backup");
694 sv_setpvn(sv,">",!PL_inplace);
695 sv_catpvn(sv,PL_oldname,oldlen);
696 SETERRNO(0,0); /* in case sprintf set errno */
698 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
699 O_WRONLY|O_CREAT|O_TRUNC,0,Nullfp))
701 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
702 O_WRONLY|O_CREAT|OPEN_EXCL,0666,Nullfp))
705 if (ckWARN_d(WARN_INPLACE))
706 Perl_warner(aTHX_ WARN_INPLACE, "Can't do inplace edit on %s: %s",
707 PL_oldname, Strerror(errno) );
711 setdefout(PL_argvoutgv);
712 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
713 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
715 (void)fchmod(PL_lastfd,PL_filemode);
717 # if !(defined(WIN32) && defined(__BORLANDC__))
718 /* Borland runtime creates a readonly file! */
719 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
722 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
724 (void)fchown(PL_lastfd,fileuid,filegid);
727 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
732 return IoIFP(GvIOp(gv));
736 if (ckWARN_d(WARN_INPLACE)) {
738 if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0
739 && !S_ISREG(PL_statbuf.st_mode))
741 Perl_warner(aTHX_ WARN_INPLACE,
742 "Can't do inplace edit: %s is not a regular file",
746 Perl_warner(aTHX_ WARN_INPLACE, "Can't open %s: %s",
747 PL_oldname, Strerror(eno));
751 if (io && (IoFLAGS(io) & IOf_ARGV))
752 IoFLAGS(io) |= IOf_START;
754 (void)do_close(PL_argvoutgv,FALSE);
755 if (io && (IoFLAGS(io) & IOf_ARGV)
756 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
758 GV *oldout = (GV*)av_pop(PL_argvout_stack);
760 SvREFCNT_dec(oldout);
763 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
770 Perl_do_pipe(pTHX_ SV *sv, GV *rgv, GV *wgv)
789 if (PerlProc_pipe(fd) < 0)
791 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r");
792 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w");
793 IoIFP(wstio) = IoOFP(wstio);
794 IoTYPE(rstio) = IoTYPE_RDONLY;
795 IoTYPE(wstio) = IoTYPE_WRONLY;
796 if (!IoIFP(rstio) || !IoOFP(wstio)) {
797 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
798 else PerlLIO_close(fd[0]);
799 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
800 else PerlLIO_close(fd[1]);
804 sv_setsv(sv,&PL_sv_yes);
808 sv_setsv(sv,&PL_sv_undef);
813 /* explicit renamed to avoid C++ conflict -- kja */
815 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
822 if (!gv || SvTYPE(gv) != SVt_PVGV) {
824 SETERRNO(EBADF,SS$_IVCHAN);
828 if (!io) { /* never opened */
831 if (ckWARN(WARN_UNOPENED)) /* no check for closed here */
832 report_evil_fh(gv, io, PL_op->op_type);
833 SETERRNO(EBADF,SS$_IVCHAN);
837 retval = io_close(io, not_implicit);
841 IoLINES_LEFT(io) = IoPAGE_LEN(io);
843 IoTYPE(io) = IoTYPE_CLOSED;
848 Perl_io_close(pTHX_ IO *io, bool not_implicit)
854 if (IoTYPE(io) == IoTYPE_PIPE) {
855 status = PerlProc_pclose(IoIFP(io));
857 STATUS_NATIVE_SET(status);
858 retval = (STATUS_POSIX == 0);
861 retval = (status != -1);
864 else if (IoTYPE(io) == IoTYPE_STD)
867 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
868 retval = (PerlIO_close(IoOFP(io)) != EOF);
869 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
872 retval = (PerlIO_close(IoIFP(io)) != EOF);
874 IoOFP(io) = IoIFP(io) = Nullfp;
876 else if (not_implicit) {
877 SETERRNO(EBADF,SS$_IVCHAN);
884 Perl_do_eof(pTHX_ GV *gv)
894 else if (ckWARN(WARN_IO)
895 && (IoTYPE(io) == IoTYPE_WRONLY || IoIFP(io) == PerlIO_stdout()
896 || IoIFP(io) == PerlIO_stderr()))
898 /* integrate to report_evil_fh()? */
901 SV* sv = sv_newmortal();
902 gv_efullname4(sv, gv, Nullch, FALSE);
903 name = SvPV_nolen(sv);
906 Perl_warner(aTHX_ WARN_IO,
907 "Filehandle %s opened only for output", name);
909 Perl_warner(aTHX_ WARN_IO,
910 "Filehandle opened only for output");
915 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
916 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
917 return FALSE; /* this is the most usual case */
920 ch = PerlIO_getc(IoIFP(io));
922 (void)PerlIO_ungetc(IoIFP(io),ch);
925 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
926 if (PerlIO_get_cnt(IoIFP(io)) < -1)
927 PerlIO_set_cnt(IoIFP(io),-1);
929 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
930 if (!nextargv(PL_argvgv)) /* get another fp handy */
934 return TRUE; /* normal fp, definitely end of file */
940 Perl_do_tell(pTHX_ GV *gv)
945 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
946 #ifdef ULTRIX_STDIO_BOTCH
948 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
950 return PerlIO_tell(fp);
954 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
955 report_evil_fh(gv, io, PL_op->op_type);
957 SETERRNO(EBADF,RMS$_IFI);
962 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
967 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
968 #ifdef ULTRIX_STDIO_BOTCH
970 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
972 return PerlIO_seek(fp, pos, whence) >= 0;
976 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
977 report_evil_fh(gv, io, PL_op->op_type);
979 SETERRNO(EBADF,RMS$_IFI);
984 Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
989 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
990 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
993 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
994 report_evil_fh(gv, io, PL_op->op_type);
996 SETERRNO(EBADF,RMS$_IFI);
1001 Perl_mode_from_discipline(pTHX_ SV *discp)
1003 int mode = O_BINARY;
1006 char *s = SvPV(discp,len);
1011 if (len > 3 && strnEQ(s+1, "raw", 3)
1012 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1021 if (len > 4 && strnEQ(s+1, "crlf", 4)
1022 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1031 goto fail_discipline;
1034 else if (isSPACE(*s)) {
1041 end = strchr(s+1, ':');
1044 Perl_croak(aTHX_ "Unknown discipline '%.*s'", end-s, s);
1052 Perl_do_binmode(pTHX_ PerlIO *fp, int iotype, int mode)
1055 # if defined(atarist) || defined(__MINT__)
1056 if (!PerlIO_flush(fp)) {
1057 if (mode & O_BINARY)
1058 ((FILE*)fp)->_flag |= _IOBIN;
1060 ((FILE*)fp)->_flag &= ~ _IOBIN;
1065 if (PerlLIO_setmode(PerlIO_fileno(fp), mode) != -1) {
1066 # if defined(WIN32) && defined(__BORLANDC__)
1067 /* The translation mode of the stream is maintained independent
1068 * of the translation mode of the fd in the Borland RTL (heavy
1069 * digging through their runtime sources reveal). User has to
1070 * set the mode explicitly for the stream (though they don't
1071 * document this anywhere). GSAR 97-5-24
1073 PerlIO_seek(fp,0L,0);
1074 if (mode & O_BINARY)
1075 ((FILE*)fp)->flags |= _F_BIN;
1077 ((FILE*)fp)->flags &= ~ _F_BIN;
1085 # if defined(USEMYBINMODE)
1086 if (my_binmode(fp, iotype, mode) != FALSE)
1096 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP)
1097 /* code courtesy of William Kucharski */
1100 I32 my_chsize(fd, length)
1101 I32 fd; /* file descriptor */
1102 Off_t length; /* length to set file to */
1105 struct stat filebuf;
1107 if (PerlLIO_fstat(fd, &filebuf) < 0)
1110 if (filebuf.st_size < length) {
1112 /* extend file length */
1114 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
1117 /* write a "0" byte */
1119 if ((PerlLIO_write(fd, "", 1)) != 1)
1123 /* truncate length */
1127 fl.l_start = length;
1128 fl.l_type = F_WRLCK; /* write lock on file space */
1131 * This relies on the UNDOCUMENTED F_FREESP argument to
1132 * fcntl(2), which truncates the file so that it ends at the
1133 * position indicated by fl.l_start.
1135 * Will minor miracles never cease?
1138 if (fcntl(fd, F_FREESP, &fl) < 0)
1145 #endif /* F_FREESP */
1148 Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
1150 register char *tmps;
1153 /* assuming fp is checked earlier */
1159 if (SvIOK(sv) && SvIVX(sv) != 0) {
1160 PerlIO_printf(fp, PL_ofmt, (NV)SvIVX(sv));
1161 return !PerlIO_error(fp);
1163 if ( (SvNOK(sv) && SvNVX(sv) != 0.0)
1164 || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) {
1165 PerlIO_printf(fp, PL_ofmt, SvNVX(sv));
1166 return !PerlIO_error(fp);
1169 switch (SvTYPE(sv)) {
1173 if (ckWARN(WARN_UNINITIALIZED))
1182 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
1184 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
1185 return !PerlIO_error(fp);
1190 /* XXX Fix this when the I/O disciplines arrive. XXX */
1192 sv_utf8_downgrade(sv, FALSE);
1194 tmps = SvPV(sv, len);
1197 /* To detect whether the process is about to overstep its
1198 * filesize limit we would need getrlimit(). We could then
1199 * also transparently raise the limit with setrlimit() --
1200 * but only until the system hard limit/the filesystem limit,
1201 * at which we would get EPERM. Note that when using buffered
1202 * io the write failure can be delayed until the flush/close. --jhi */
1203 if (len && (PerlIO_write(fp,tmps,len) == 0 || PerlIO_error(fp)))
1205 return !PerlIO_error(fp);
1215 if (PL_op->op_flags & OPf_REF) {
1220 if (io && IoIFP(io)) {
1222 sv_setpv(PL_statname,"");
1223 PL_laststype = OP_STAT;
1224 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
1228 return PL_laststatval;
1229 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1230 report_evil_fh(gv, io, PL_op->op_type);
1232 sv_setpv(PL_statname,"");
1233 return (PL_laststatval = -1);
1241 if (SvTYPE(sv) == SVt_PVGV) {
1245 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
1252 sv_setpv(PL_statname, s);
1253 PL_laststype = OP_STAT;
1254 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1255 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
1256 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "stat");
1257 return PL_laststatval;
1267 if (PL_op->op_flags & OPf_REF) {
1269 if (cGVOP_gv == PL_defgv) {
1270 if (PL_laststype != OP_LSTAT)
1271 Perl_croak(aTHX_ "The stat preceding -l _ wasn't an lstat");
1272 return PL_laststatval;
1274 Perl_croak(aTHX_ "You can't use -l on a filehandle");
1277 PL_laststype = OP_LSTAT;
1281 sv_setpv(PL_statname,SvPV(sv, n_a));
1282 PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache);
1283 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
1284 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "lstat");
1285 return PL_laststatval;
1289 Perl_do_aexec(pTHX_ SV *really, register SV **mark, register SV **sp)
1291 return do_aexec5(really, mark, sp, 0, 0);
1295 Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1296 int fd, int do_report)
1298 #ifdef MACOS_TRADITIONAL
1299 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1307 New(401,PL_Argv, sp - mark + 1, char*);
1309 while (++mark <= sp) {
1311 *a++ = SvPVx(*mark, n_a);
1316 if (*PL_Argv[0] != '/') /* will execvp use PATH? */
1317 TAINT_ENV(); /* testing IFS here is overkill, probably */
1318 if (really && *(tmps = SvPV(really, n_a)))
1319 PerlProc_execvp(tmps,PL_Argv);
1321 PerlProc_execvp(PL_Argv[0],PL_Argv);
1322 if (ckWARN(WARN_EXEC))
1323 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1324 PL_Argv[0], Strerror(errno));
1328 PerlLIO_write(fd, (void*)&e, sizeof(int));
1338 Perl_do_execfree(pTHX)
1342 PL_Argv = Null(char **);
1350 #if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC) && !defined(MACOS_TRADITIONAL)
1353 Perl_do_exec(pTHX_ char *cmd)
1355 return do_exec3(cmd,0,0);
1359 Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report)
1365 while (*cmd && isSPACE(*cmd))
1368 /* save an extra exec if possible */
1371 if (strnEQ(cmd,PL_cshname,PL_cshlen) && strnEQ(cmd+PL_cshlen," -c",3)) {
1373 s = cmd+PL_cshlen+3;
1387 if (s[-1] == '\'') {
1389 PerlProc_execl(PL_cshname,"csh", flags,ncmd,(char*)0);
1397 /* see if there are shell metacharacters in it */
1399 if (*cmd == '.' && isSPACE(cmd[1]))
1402 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1405 for (s = cmd; *s && isALNUM(*s); s++) ; /* catch VAR=val gizmo */
1409 for (s = cmd; *s; s++) {
1410 if (*s != ' ' && !isALPHA(*s) && strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1411 if (*s == '\n' && !s[1]) {
1415 /* handle the 2>&1 construct at the end */
1416 if (*s == '>' && s[1] == '&' && s[2] == '1'
1417 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1418 && (!s[3] || isSPACE(s[3])))
1422 while (*t && isSPACE(*t))
1424 if (!*t && (dup2(1,2) != -1)) {
1430 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
1435 New(402,PL_Argv, (s - cmd) / 2 + 2, char*);
1436 PL_Cmd = savepvn(cmd, s-cmd);
1438 for (s = PL_Cmd; *s;) {
1439 while (*s && isSPACE(*s)) s++;
1442 while (*s && !isSPACE(*s)) s++;
1448 PerlProc_execvp(PL_Argv[0],PL_Argv);
1449 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1457 if (ckWARN(WARN_EXEC))
1458 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1459 PL_Argv[0], Strerror(errno));
1461 PerlLIO_write(fd, (void*)&e, sizeof(int));
1470 #endif /* OS2 || WIN32 */
1473 Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
1478 register I32 tot = 0;
1481 SV **oldmark = mark;
1484 #define APPLY_TAINT_PROPER() \
1486 if (PL_tainted) { TAINT_PROPER(what); } \
1489 /* This is a first heuristic; it doesn't catch tainting magic. */
1491 while (++mark <= sp) {
1492 if (SvTAINTED(*mark)) {
1502 APPLY_TAINT_PROPER();
1505 APPLY_TAINT_PROPER();
1507 while (++mark <= sp) {
1508 char *name = SvPVx(*mark, n_a);
1509 APPLY_TAINT_PROPER();
1510 if (PerlLIO_chmod(name, val))
1518 APPLY_TAINT_PROPER();
1519 if (sp - mark > 2) {
1520 val = SvIVx(*++mark);
1521 val2 = SvIVx(*++mark);
1522 APPLY_TAINT_PROPER();
1524 while (++mark <= sp) {
1525 char *name = SvPVx(*mark, n_a);
1526 APPLY_TAINT_PROPER();
1527 if (PerlLIO_chown(name, val, val2))
1534 XXX Should we make lchown() directly available from perl?
1535 For now, we'll let Configure test for HAS_LCHOWN, but do
1536 nothing in the core.
1542 APPLY_TAINT_PROPER();
1545 s = SvPVx(*++mark, n_a);
1547 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1549 if (!(val = whichsig(s)))
1550 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
1554 APPLY_TAINT_PROPER();
1557 /* kill() doesn't do process groups (job trees?) under VMS */
1558 if (val < 0) val = -val;
1559 if (val == SIGKILL) {
1560 # include <starlet.h>
1561 /* Use native sys$delprc() to insure that target process is
1562 * deleted; supervisor-mode images don't pay attention to
1563 * CRTL's emulation of Unix-style signals and kill()
1565 while (++mark <= sp) {
1566 I32 proc = SvIVx(*mark);
1567 register unsigned long int __vmssts;
1568 APPLY_TAINT_PROPER();
1569 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1573 case SS$_NOSUCHNODE:
1574 SETERRNO(ESRCH,__vmssts);
1577 SETERRNO(EPERM,__vmssts);
1580 SETERRNO(EVMSERR,__vmssts);
1589 while (++mark <= sp) {
1590 I32 proc = SvIVx(*mark);
1591 APPLY_TAINT_PROPER();
1593 if (PerlProc_killpg(proc,val)) /* BSD */
1595 if (PerlProc_kill(-proc,val)) /* SYSV */
1601 while (++mark <= sp) {
1602 I32 proc = SvIVx(*mark);
1603 APPLY_TAINT_PROPER();
1604 if (PerlProc_kill(proc, val))
1612 APPLY_TAINT_PROPER();
1614 while (++mark <= sp) {
1615 s = SvPVx(*mark, n_a);
1616 APPLY_TAINT_PROPER();
1617 if (PL_euid || PL_unsafe) {
1621 else { /* don't let root wipe out directories without -U */
1622 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1634 APPLY_TAINT_PROPER();
1635 if (sp - mark > 2) {
1636 #if defined(I_UTIME) || defined(VMS)
1637 struct utimbuf utbuf;
1645 Zero(&utbuf, sizeof utbuf, char);
1647 utbuf.actime = (Time_t)SvNVx(*++mark); /* time accessed */
1648 utbuf.modtime = (Time_t)SvNVx(*++mark); /* time modified */
1650 utbuf.actime = (Time_t)SvIVx(*++mark); /* time accessed */
1651 utbuf.modtime = (Time_t)SvIVx(*++mark); /* time modified */
1653 APPLY_TAINT_PROPER();
1655 while (++mark <= sp) {
1656 char *name = SvPVx(*mark, n_a);
1657 APPLY_TAINT_PROPER();
1658 if (PerlLIO_utime(name, &utbuf))
1669 #undef APPLY_TAINT_PROPER
1672 /* Do the permissions allow some operation? Assumes statcache already set. */
1673 #ifndef VMS /* VMS' cando is in vms.c */
1675 Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register Stat_t *statbufp)
1676 /* Note: we use `effective' both for uids and gids.
1677 * Here we are betting on Uid_t being equal or wider than Gid_t. */
1680 /* [Comments and code from Len Reed]
1681 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1682 * to write-protected files. The execute permission bit is set
1683 * by the Miscrosoft C library stat() function for the following:
1688 * All files and directories are readable.
1689 * Directories and special files, e.g. "CON", cannot be
1691 * [Comment by Tom Dinger -- a directory can have the write-protect
1692 * bit set in the file system, but DOS permits changes to
1693 * the directory anyway. In addition, all bets are off
1694 * here for networked software, such as Novell and
1698 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1699 * too so it will actually look into the files for magic numbers
1701 return (mode & statbufp->st_mode) ? TRUE : FALSE;
1703 #else /* ! DOSISH */
1704 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
1705 if (mode == S_IXUSR) {
1706 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1710 return TRUE; /* root reads and writes anything */
1713 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1714 if (statbufp->st_mode & mode)
1715 return TRUE; /* ok as "user" */
1717 else if (ingroup(statbufp->st_gid,effective)) {
1718 if (statbufp->st_mode & mode >> 3)
1719 return TRUE; /* ok as "group" */
1721 else if (statbufp->st_mode & mode >> 6)
1722 return TRUE; /* ok as "other" */
1724 #endif /* ! DOSISH */
1729 Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective)
1731 #ifdef MACOS_TRADITIONAL
1732 /* This is simply not correct for AppleShare, but fix it yerself. */
1735 if (testgid == (effective ? PL_egid : PL_gid))
1737 #ifdef HAS_GETGROUPS
1742 Groups_t gary[NGROUPS];
1745 anum = getgroups(NGROUPS,gary);
1747 if (gary[anum] == testgid)
1755 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1758 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
1764 key = (key_t)SvNVx(*++mark);
1765 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1766 flags = SvIVx(*++mark);
1772 return msgget(key, flags);
1776 return semget(key, n, flags);
1780 return shmget(key, n, flags);
1782 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1784 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1787 return -1; /* should never happen */
1791 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
1796 I32 id, n, cmd, infosize, getinfo;
1799 id = SvIVx(*++mark);
1800 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1801 cmd = SvIVx(*++mark);
1804 getinfo = (cmd == IPC_STAT);
1810 if (cmd == IPC_STAT || cmd == IPC_SET)
1811 infosize = sizeof(struct msqid_ds);
1816 if (cmd == IPC_STAT || cmd == IPC_SET)
1817 infosize = sizeof(struct shmid_ds);
1823 if (cmd == IPC_STAT || cmd == IPC_SET)
1824 infosize = sizeof(struct semid_ds);
1825 else if (cmd == GETALL || cmd == SETALL)
1827 struct semid_ds semds;
1829 #ifdef EXTRA_F_IN_SEMUN_BUF
1830 semun.buff = &semds;
1834 getinfo = (cmd == GETALL);
1835 if (Semctl(id, 0, IPC_STAT, semun) == -1)
1837 infosize = semds.sem_nsems * sizeof(short);
1838 /* "short" is technically wrong but much more portable
1839 than guessing about u_?short(_t)? */
1842 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1846 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1848 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1857 SvPV_force(astr, len);
1858 a = SvGROW(astr, infosize+1);
1862 a = SvPV(astr, len);
1863 if (len != infosize)
1864 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
1873 a = INT2PTR(char *,i); /* ouch */
1880 ret = msgctl(id, cmd, (struct msqid_ds *)a);
1886 union semun unsemds;
1888 #ifdef EXTRA_F_IN_SEMUN_BUF
1889 unsemds.buff = (struct semid_ds *)a;
1891 unsemds.buf = (struct semid_ds *)a;
1893 ret = Semctl(id, n, cmd, unsemds);
1895 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1902 ret = shmctl(id, cmd, (struct shmid_ds *)a);
1906 if (getinfo && ret >= 0) {
1907 SvCUR_set(astr, infosize);
1908 *SvEND(astr) = '\0';
1915 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
1921 I32 id, msize, flags;
1924 id = SvIVx(*++mark);
1926 flags = SvIVx(*++mark);
1927 mbuf = SvPV(mstr, len);
1928 if ((msize = len - sizeof(long)) < 0)
1929 Perl_croak(aTHX_ "Arg too short for msgsnd");
1931 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
1933 Perl_croak(aTHX_ "msgsnd not implemented");
1938 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
1945 I32 id, msize, flags, ret;
1948 id = SvIVx(*++mark);
1950 /* suppress warning when reading into undef var --jhi */
1952 sv_setpvn(mstr, "", 0);
1953 msize = SvIVx(*++mark);
1954 mtype = (long)SvIVx(*++mark);
1955 flags = SvIVx(*++mark);
1956 SvPV_force(mstr, len);
1957 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
1960 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
1962 SvCUR_set(mstr, sizeof(long)+ret);
1963 *SvEND(mstr) = '\0';
1964 #ifndef INCOMPLETE_TAINTS
1965 /* who knows who has been playing with this message? */
1971 Perl_croak(aTHX_ "msgrcv not implemented");
1976 Perl_do_semop(pTHX_ SV **mark, SV **sp)
1985 id = SvIVx(*++mark);
1987 opbuf = SvPV(opstr, opsize);
1988 if (opsize < sizeof(struct sembuf)
1989 || (opsize % sizeof(struct sembuf)) != 0) {
1990 SETERRNO(EINVAL,LIB$_INVARG);
1994 return semop(id, (struct sembuf *)opbuf, opsize/sizeof(struct sembuf));
1996 Perl_croak(aTHX_ "semop not implemented");
2001 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
2007 I32 id, mpos, msize;
2009 struct shmid_ds shmds;
2011 id = SvIVx(*++mark);
2013 mpos = SvIVx(*++mark);
2014 msize = SvIVx(*++mark);
2016 if (shmctl(id, IPC_STAT, &shmds) == -1)
2018 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
2019 SETERRNO(EFAULT,SS$_ACCVIO); /* can't do as caller requested */
2022 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
2023 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2025 if (optype == OP_SHMREAD) {
2026 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2028 sv_setpvn(mstr, "", 0);
2029 SvPV_force(mstr, len);
2030 mbuf = SvGROW(mstr, msize+1);
2032 Copy(shm + mpos, mbuf, msize, char);
2033 SvCUR_set(mstr, msize);
2034 *SvEND(mstr) = '\0';
2036 #ifndef INCOMPLETE_TAINTS
2037 /* who knows who has been playing with this shared memory? */
2044 mbuf = SvPV(mstr, len);
2045 if ((n = len) > msize)
2047 Copy(mbuf, shm + mpos, n, char);
2049 memzero(shm + mpos + n, msize - n);
2053 Perl_croak(aTHX_ "shm I/O not implemented");
2057 #endif /* SYSV IPC */