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)
55 Perl_do_open(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
56 int rawmode, int rawperm, PerlIO *supplied_fp)
58 return do_open9(gv, name, len, as_raw, rawmode, rawperm,
59 supplied_fp, Nullsv, 0);
63 Perl_do_open9(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
64 int rawmode, int rawperm, PerlIO *supplied_fp, SV *svs,
67 register IO *io = GvIOn(gv);
68 PerlIO *saveifp = Nullfp;
69 PerlIO *saveofp = Nullfp;
70 char savetype = IoTYPE_CLOSED;
75 bool was_fdopen = FALSE;
76 bool in_raw = 0, in_crlf = 0, out_raw = 0, out_crlf = 0;
78 PL_forkprocess = 1; /* assume true if no fork */
80 if (PL_op && PL_op->op_type == OP_OPEN) {
81 /* set up disciplines */
82 U8 flags = PL_op->op_private;
83 in_raw = (flags & OPpOPEN_IN_RAW);
84 in_crlf = (flags & OPpOPEN_IN_CRLF);
85 out_raw = (flags & OPpOPEN_OUT_RAW);
86 out_crlf = (flags & OPpOPEN_OUT_CRLF);
90 fd = PerlIO_fileno(IoIFP(io));
91 if (IoTYPE(io) == IoTYPE_STD)
93 else if (fd <= PL_maxsysfd) {
96 savetype = IoTYPE(io);
99 else if (IoTYPE(io) == IoTYPE_PIPE)
100 result = PerlProc_pclose(IoIFP(io));
101 else if (IoIFP(io) != IoOFP(io)) {
103 result = PerlIO_close(IoOFP(io));
104 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
107 result = PerlIO_close(IoIFP(io));
110 result = PerlIO_close(IoIFP(io));
111 if (result == EOF && fd > PL_maxsysfd)
112 PerlIO_printf(Perl_error_log,
113 "Warning: unable to close filehandle %s properly.\n",
115 IoOFP(io) = IoIFP(io) = Nullfp;
119 #if defined(USE_64_BIT_RAWIO) && defined(O_LARGEFILE)
120 rawmode |= O_LARGEFILE;
124 #define O_ACCMODE 3 /* Assume traditional implementation */
127 switch (result = rawmode & O_ACCMODE) {
129 IoTYPE(io) = IoTYPE_RDONLY;
132 IoTYPE(io) = IoTYPE_WRONLY;
136 IoTYPE(io) = IoTYPE_RDWR;
140 writing = (result > 0);
141 fd = PerlLIO_open3(name, rawmode, rawperm);
148 if (result == O_RDONLY)
151 else if (rawmode & O_APPEND) {
153 if (result != O_WRONLY)
158 if (result == O_WRONLY)
165 if (rawmode & O_BINARY)
168 fp = PerlIO_fdopen(fd, fpmode);
178 char mode[4]; /* stdio file mode ("r\0", "rb\0", "r+b\0" etc.) */
181 type = savepvn(name, len);
186 name = SvPV(svs, l) ;
188 name = savepvn(name, len);
192 while (tlen && isSPACE(type[tlen-1]))
197 mode[0] = mode[1] = mode[2] = mode[3] = '\0';
199 if (*type == IoTYPE_RDWR && tlen > 1 && type[tlen-1] != IoTYPE_PIPE) { /* scary */
205 if (*type == IoTYPE_PIPE) {
206 if (num_svs && (tlen != 2 || type[1] != IoTYPE_STD)) {
208 Perl_croak(aTHX_ "Unknown open() mode '%.*s'", (int)olen, oname);
211 for (type++, tlen--; isSPACE(*type); type++, tlen--) ;
216 if (*name == '\0') { /* command is missing 19990114 */
217 if (ckWARN(WARN_PIPE))
218 Perl_warner(aTHX_ WARN_PIPE, "Missing command in piped open");
222 if (strNE(name,"-") || num_svs)
224 TAINT_PROPER("piped open");
225 if (name[len-1] == '|') {
227 if (ckWARN(WARN_PIPE))
228 Perl_warner(aTHX_ WARN_PIPE, "Can't open bidirectional pipe");
238 fp = PerlProc_popen(name,mode);
242 else if (*type == IoTYPE_WRONLY) {
243 TAINT_PROPER("open");
245 if (*type == IoTYPE_WRONLY) {
246 /* Two IoTYPE_WRONLYs in a row make for an IoTYPE_APPEND. */
247 mode[0] = IoTYPE(io) = IoTYPE_APPEND;
260 if (num_svs && tlen != 1)
271 if (!*name && supplied_fp)
275 for (; isSPACE(*name); name++) ;
280 gv = gv_fetchpv(name,FALSE,SVt_PVIO);
284 SETERRNO(EINVAL,SS$_IVCHAN);
289 PerlIO *fp = IoIFP(thatio);
290 /* Flush stdio buffer before dup. --mjd
291 * Unfortunately SEEK_CURing 0 seems to
292 * be optimized away on most platforms;
293 * only Solaris and Linux seem to flush
296 /* sfio fails to clear error on next
297 sfwrite, contrary to documentation.
299 if (PerlIO_seek(fp, 0, SEEK_CUR) == -1)
302 /* On the other hand, do all platforms
303 * take gracefully to flushing a read-only
304 * filehandle? Perhaps we should do
305 * fsetpos(src)+fgetpos(dst)? --nik */
307 fd = PerlIO_fileno(fp);
308 /* When dup()ing STDIN, STDOUT or STDERR
309 * explicitly set appropriate access mode */
310 if (IoIFP(thatio) == PerlIO_stdout()
311 || IoIFP(thatio) == PerlIO_stderr())
312 IoTYPE(io) = IoTYPE_WRONLY;
313 else if (IoIFP(thatio) == PerlIO_stdin())
314 IoTYPE(io) = IoTYPE_RDONLY;
315 /* When dup()ing a socket, say result is
317 else if (IoTYPE(thatio) == IoTYPE_SOCKET)
318 IoTYPE(io) = IoTYPE_SOCKET;
324 fd = PerlLIO_dup(fd);
327 if (!(fp = PerlIO_fdopen(fd,mode))) {
335 for (; isSPACE(*type); type++) ;
336 if (*type == IoTYPE_STD && !type[1]) {
337 fp = PerlIO_stdout();
338 IoTYPE(io) = IoTYPE_STD;
341 fp = PerlIO_open((num_svs ? name : type), mode);
345 else if (*type == IoTYPE_RDONLY) {
346 if (num_svs && tlen != 1)
349 for (type++; isSPACE(*type); type++) ;
360 if (*type == IoTYPE_STD && !type[1]) {
362 IoTYPE(io) = IoTYPE_STD;
365 fp = PerlIO_open((num_svs ? name : type), mode);
367 else if (tlen > 1 && type[tlen-1] == IoTYPE_PIPE) {
369 if (tlen != 2 || type[0] != IoTYPE_STD)
374 while (tlen && isSPACE(type[tlen-1]))
377 for (; isSPACE(*type); type++) ;
380 if (*name == '\0') { /* command is missing 19990114 */
381 if (ckWARN(WARN_PIPE))
382 Perl_warner(aTHX_ WARN_PIPE, "Missing command in piped open");
386 if (strNE(name,"-") || num_svs)
388 TAINT_PROPER("piped open");
397 fp = PerlProc_popen(name,mode);
399 IoTYPE(io) = IoTYPE_PIPE;
405 IoTYPE(io) = IoTYPE_RDONLY;
407 for (; isSPACE(*name); name++) ;
408 if (strEQ(name,"-")) {
410 IoTYPE(io) = IoTYPE_STD;
420 fp = PerlIO_open(name,mode);
425 if (ckWARN(WARN_NEWLINE) && IoTYPE(io) == IoTYPE_RDONLY && strchr(name, '\n'))
426 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "open");
429 if (IoTYPE(io) && IoTYPE(io) != IoTYPE_PIPE && IoTYPE(io) != IoTYPE_STD) {
430 if (PerlLIO_fstat(PerlIO_fileno(fp),&PL_statbuf) < 0) {
431 (void)PerlIO_close(fp);
434 if (S_ISSOCK(PL_statbuf.st_mode))
435 IoTYPE(io) = IoTYPE_SOCKET; /* in case a socket was passed in to us */
439 !(PL_statbuf.st_mode & S_IFMT)
443 && IoTYPE(io) != IoTYPE_WRONLY /* Dups of STD* filehandles already have */
444 && IoTYPE(io) != IoTYPE_RDONLY /* type so they aren't marked as sockets */
445 ) { /* on OS's that return 0 on fstat()ed pipe */
447 Sock_size_t buflen = sizeof tmpbuf;
448 if (PerlSock_getsockname(PerlIO_fileno(fp), (struct sockaddr *)tmpbuf,
450 || errno != ENOTSOCK)
451 IoTYPE(io) = IoTYPE_SOCKET; /* some OS's return 0 on fstat()ed socket */
452 /* but some return 0 for streams too, sigh */
456 if (saveifp) { /* must use old fp? */
457 fd = PerlIO_fileno(saveifp);
459 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
460 if (saveofp != saveifp) { /* was a socket? */
461 PerlIO_close(saveofp);
466 if (fd != PerlIO_fileno(fp)) {
470 PerlLIO_dup2(PerlIO_fileno(fp), fd);
472 if (fd != PerlIO_fileno(PerlIO_stdin())) {
473 char newname[FILENAME_MAX+1];
474 if (fgetname(fp, newname)) {
475 if (fd == PerlIO_fileno(PerlIO_stdout())) Perl_vmssetuserlnm("SYS$OUTPUT", newname);
476 if (fd == PerlIO_fileno(PerlIO_stderr())) Perl_vmssetuserlnm("SYS$ERROR", newname);
481 sv = *av_fetch(PL_fdpid,PerlIO_fileno(fp),TRUE);
482 (void)SvUPGRADE(sv, SVt_IV);
485 sv = *av_fetch(PL_fdpid,fd,TRUE);
487 (void)SvUPGRADE(sv, SVt_IV);
496 #if defined(HAS_FCNTL) && defined(F_SETFD)
498 int save_errno = errno;
499 fd = PerlIO_fileno(fp);
500 fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
505 IoFLAGS(io) &= ~IOf_NOLINE;
507 if (IoTYPE(io) == IoTYPE_SOCKET
508 || (IoTYPE(io) == IoTYPE_WRONLY && S_ISCHR(PL_statbuf.st_mode)) )
518 if (!(IoOFP(io) = PerlIO_fdopen(PerlIO_fileno(fp),mode))) {
532 IoTYPE(io) = savetype;
537 Perl_nextargv(pTHX_ register GV *gv)
540 #ifndef FLEXFILENAMES
549 PL_argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO);
550 if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
551 IoFLAGS(io) &= ~IOf_START;
553 if (!PL_argvout_stack)
554 PL_argvout_stack = newAV();
555 av_push(PL_argvout_stack, SvREFCNT_inc(PL_defoutgv));
558 if (PL_filemode & (S_ISUID|S_ISGID)) {
559 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
561 (void)fchmod(PL_lastfd,PL_filemode);
563 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
567 while (av_len(GvAV(gv)) >= 0) {
569 sv = av_shift(GvAV(gv));
571 sv_setsv(GvSV(gv),sv);
572 SvSETMAGIC(GvSV(gv));
573 PL_oldname = SvPVx(GvSV(gv), oldlen);
574 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,Nullfp)) {
576 TAINT_PROPER("inplace open");
577 if (oldlen == 1 && *PL_oldname == '-') {
578 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
579 return IoIFP(GvIOp(gv));
581 #ifndef FLEXFILENAMES
582 filedev = PL_statbuf.st_dev;
583 fileino = PL_statbuf.st_ino;
585 PL_filemode = PL_statbuf.st_mode;
586 fileuid = PL_statbuf.st_uid;
587 filegid = PL_statbuf.st_gid;
588 if (!S_ISREG(PL_filemode)) {
589 if (ckWARN_d(WARN_INPLACE))
590 Perl_warner(aTHX_ WARN_INPLACE,
591 "Can't do inplace edit: %s is not a regular file",
597 char *star = strchr(PL_inplace, '*');
599 char *begin = PL_inplace;
600 sv_setpvn(sv, "", 0);
602 sv_catpvn(sv, begin, star - begin);
603 sv_catpvn(sv, PL_oldname, oldlen);
605 } while ((star = strchr(begin, '*')));
610 sv_catpv(sv,PL_inplace);
612 #ifndef FLEXFILENAMES
613 if (PerlLIO_stat(SvPVX(sv),&PL_statbuf) >= 0
614 && PL_statbuf.st_dev == filedev
615 && PL_statbuf.st_ino == fileino
617 || (_djstat_fail_bits & _STFAIL_TRUENAME)!=0
621 if (ckWARN_d(WARN_INPLACE))
622 Perl_warner(aTHX_ WARN_INPLACE,
623 "Can't do inplace edit: %s would not be unique",
630 #if !defined(DOSISH) && !defined(__CYGWIN__)
631 if (PerlLIO_rename(PL_oldname,SvPVX(sv)) < 0) {
632 if (ckWARN_d(WARN_INPLACE))
633 Perl_warner(aTHX_ WARN_INPLACE,
634 "Can't rename %s to %s: %s, skipping file",
635 PL_oldname, SvPVX(sv), Strerror(errno) );
641 (void)PerlLIO_unlink(SvPVX(sv));
642 (void)PerlLIO_rename(PL_oldname,SvPVX(sv));
643 do_open(gv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,O_RDONLY,0,Nullfp);
646 (void)UNLINK(SvPVX(sv));
647 if (link(PL_oldname,SvPVX(sv)) < 0) {
648 if (ckWARN_d(WARN_INPLACE))
649 Perl_warner(aTHX_ WARN_INPLACE,
650 "Can't rename %s to %s: %s, skipping file",
651 PL_oldname, SvPVX(sv), Strerror(errno) );
655 (void)UNLINK(PL_oldname);
659 #if !defined(DOSISH) && !defined(AMIGAOS)
660 # ifndef VMS /* Don't delete; use automatic file versioning */
661 if (UNLINK(PL_oldname) < 0) {
662 if (ckWARN_d(WARN_INPLACE))
663 Perl_warner(aTHX_ WARN_INPLACE,
664 "Can't remove %s: %s, skipping file",
665 PL_oldname, Strerror(errno) );
671 Perl_croak(aTHX_ "Can't do inplace edit without backup");
675 sv_setpvn(sv,">",!PL_inplace);
676 sv_catpvn(sv,PL_oldname,oldlen);
677 SETERRNO(0,0); /* in case sprintf set errno */
679 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
680 O_WRONLY|O_CREAT|O_TRUNC,0,Nullfp))
682 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
683 O_WRONLY|O_CREAT|OPEN_EXCL,0666,Nullfp))
686 if (ckWARN_d(WARN_INPLACE))
687 Perl_warner(aTHX_ WARN_INPLACE, "Can't do inplace edit on %s: %s",
688 PL_oldname, Strerror(errno) );
692 setdefout(PL_argvoutgv);
693 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
694 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
696 (void)fchmod(PL_lastfd,PL_filemode);
698 # if !(defined(WIN32) && defined(__BORLANDC__))
699 /* Borland runtime creates a readonly file! */
700 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
703 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
705 (void)fchown(PL_lastfd,fileuid,filegid);
708 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
713 return IoIFP(GvIOp(gv));
716 if (ckWARN_d(WARN_INPLACE)) {
718 if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0
719 && !S_ISREG(PL_statbuf.st_mode))
721 Perl_warner(aTHX_ WARN_INPLACE,
722 "Can't do inplace edit: %s is not a regular file",
726 Perl_warner(aTHX_ WARN_INPLACE, "Can't open %s: %s",
727 PL_oldname, Strerror(eno));
731 if (io && (IoFLAGS(io) & IOf_ARGV))
732 IoFLAGS(io) |= IOf_START;
734 (void)do_close(PL_argvoutgv,FALSE);
735 if (io && (IoFLAGS(io) & IOf_ARGV)
736 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
738 GV *oldout = (GV*)av_pop(PL_argvout_stack);
740 SvREFCNT_dec(oldout);
743 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
750 Perl_do_pipe(pTHX_ SV *sv, GV *rgv, GV *wgv)
769 if (PerlProc_pipe(fd) < 0)
771 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r");
772 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w");
773 IoIFP(wstio) = IoOFP(wstio);
774 IoTYPE(rstio) = IoTYPE_RDONLY;
775 IoTYPE(wstio) = IoTYPE_WRONLY;
776 if (!IoIFP(rstio) || !IoOFP(wstio)) {
777 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
778 else PerlLIO_close(fd[0]);
779 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
780 else PerlLIO_close(fd[1]);
784 sv_setsv(sv,&PL_sv_yes);
788 sv_setsv(sv,&PL_sv_undef);
793 /* explicit renamed to avoid C++ conflict -- kja */
795 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
802 if (!gv || SvTYPE(gv) != SVt_PVGV) {
804 SETERRNO(EBADF,SS$_IVCHAN);
808 if (!io) { /* never opened */
810 if (ckWARN(WARN_UNOPENED)) /* no check for closed here */
811 report_evil_fh(gv, io, PL_op->op_type);
812 SETERRNO(EBADF,SS$_IVCHAN);
816 retval = io_close(io, not_implicit);
820 IoLINES_LEFT(io) = IoPAGE_LEN(io);
822 IoTYPE(io) = IoTYPE_CLOSED;
827 Perl_io_close(pTHX_ IO *io, bool not_implicit)
833 if (IoTYPE(io) == IoTYPE_PIPE) {
834 status = PerlProc_pclose(IoIFP(io));
836 STATUS_NATIVE_SET(status);
837 retval = (STATUS_POSIX == 0);
840 retval = (status != -1);
843 else if (IoTYPE(io) == IoTYPE_STD)
846 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
847 retval = (PerlIO_close(IoOFP(io)) != EOF);
848 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
851 retval = (PerlIO_close(IoIFP(io)) != EOF);
853 IoOFP(io) = IoIFP(io) = Nullfp;
855 else if (not_implicit) {
856 SETERRNO(EBADF,SS$_IVCHAN);
863 Perl_do_eof(pTHX_ GV *gv)
872 else if (ckWARN(WARN_IO)
873 && (IoTYPE(io) == IoTYPE_WRONLY || IoIFP(io) == PerlIO_stdout()
874 || IoIFP(io) == PerlIO_stderr()))
876 /* integrate to report_evil_fh()? */
879 SV* sv = sv_newmortal();
880 gv_efullname4(sv, gv, Nullch, FALSE);
881 name = SvPV_nolen(sv);
884 Perl_warner(aTHX_ WARN_IO,
885 "Filehandle %s opened only for output", name);
887 Perl_warner(aTHX_ WARN_IO,
888 "Filehandle opened only for output");
893 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
894 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
895 return FALSE; /* this is the most usual case */
898 ch = PerlIO_getc(IoIFP(io));
900 (void)PerlIO_ungetc(IoIFP(io),ch);
903 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
904 if (PerlIO_get_cnt(IoIFP(io)) < -1)
905 PerlIO_set_cnt(IoIFP(io),-1);
907 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
908 if (!nextargv(PL_argvgv)) /* get another fp handy */
912 return TRUE; /* normal fp, definitely end of file */
918 Perl_do_tell(pTHX_ GV *gv)
923 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
924 #ifdef ULTRIX_STDIO_BOTCH
926 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
928 return PerlIO_tell(fp);
930 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
931 report_evil_fh(gv, io, PL_op->op_type);
932 SETERRNO(EBADF,RMS$_IFI);
937 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
942 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
943 #ifdef ULTRIX_STDIO_BOTCH
945 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
947 return PerlIO_seek(fp, pos, whence) >= 0;
949 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
950 report_evil_fh(gv, io, PL_op->op_type);
951 SETERRNO(EBADF,RMS$_IFI);
956 Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
961 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
962 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
963 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
964 report_evil_fh(gv, io, PL_op->op_type);
965 SETERRNO(EBADF,RMS$_IFI);
970 Perl_mode_from_discipline(pTHX_ SV *discp)
975 char *s = SvPV(discp,len);
980 if (len > 3 && strnEQ(s+1, "raw", 3)
981 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
990 if (len > 4 && strnEQ(s+1, "crlf", 4)
991 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1000 goto fail_discipline;
1003 else if (isSPACE(*s)) {
1010 end = strchr(s+1, ':');
1013 Perl_croak(aTHX_ "Unknown discipline '%.*s'", end-s, s);
1021 Perl_do_binmode(pTHX_ PerlIO *fp, int iotype, int mode)
1024 # if defined(atarist) || defined(__MINT__)
1025 if (!PerlIO_flush(fp)) {
1026 if (mode & O_BINARY)
1027 ((FILE*)fp)->_flag |= _IOBIN;
1029 ((FILE*)fp)->_flag &= ~ _IOBIN;
1034 if (PerlLIO_setmode(PerlIO_fileno(fp), mode) != -1) {
1035 # if defined(WIN32) && defined(__BORLANDC__)
1036 /* The translation mode of the stream is maintained independent
1037 * of the translation mode of the fd in the Borland RTL (heavy
1038 * digging through their runtime sources reveal). User has to
1039 * set the mode explicitly for the stream (though they don't
1040 * document this anywhere). GSAR 97-5-24
1042 PerlIO_seek(fp,0L,0);
1043 if (mode & O_BINARY)
1044 ((FILE*)fp)->flags |= _F_BIN;
1046 ((FILE*)fp)->flags &= ~ _F_BIN;
1054 # if defined(USEMYBINMODE)
1055 if (my_binmode(fp, iotype, mode) != FALSE)
1065 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP)
1066 /* code courtesy of William Kucharski */
1069 I32 my_chsize(fd, length)
1070 I32 fd; /* file descriptor */
1071 Off_t length; /* length to set file to */
1074 struct stat filebuf;
1076 if (PerlLIO_fstat(fd, &filebuf) < 0)
1079 if (filebuf.st_size < length) {
1081 /* extend file length */
1083 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
1086 /* write a "0" byte */
1088 if ((PerlLIO_write(fd, "", 1)) != 1)
1092 /* truncate length */
1096 fl.l_start = length;
1097 fl.l_type = F_WRLCK; /* write lock on file space */
1100 * This relies on the UNDOCUMENTED F_FREESP argument to
1101 * fcntl(2), which truncates the file so that it ends at the
1102 * position indicated by fl.l_start.
1104 * Will minor miracles never cease?
1107 if (fcntl(fd, F_FREESP, &fl) < 0)
1114 #endif /* F_FREESP */
1117 Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
1119 register char *tmps;
1122 /* assuming fp is checked earlier */
1128 if (SvIOK(sv) && SvIVX(sv) != 0) {
1129 PerlIO_printf(fp, PL_ofmt, (NV)SvIVX(sv));
1130 return !PerlIO_error(fp);
1132 if ( (SvNOK(sv) && SvNVX(sv) != 0.0)
1133 || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) {
1134 PerlIO_printf(fp, PL_ofmt, SvNVX(sv));
1135 return !PerlIO_error(fp);
1138 switch (SvTYPE(sv)) {
1140 if (ckWARN(WARN_UNINITIALIZED))
1148 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
1150 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
1151 return !PerlIO_error(fp);
1155 tmps = SvPV(sv, len);
1158 /* To detect whether the process is about to overstep its
1159 * filesize limit we would need getrlimit(). We could then
1160 * also transparently raise the limit with setrlimit() --
1161 * but only until the system hard limit/the filesystem limit,
1162 * at which we would get EPERM. Note that when using buffered
1163 * io the write failure can be delayed until the flush/close. --jhi */
1164 if (len && (PerlIO_write(fp,tmps,len) == 0))
1166 return !PerlIO_error(fp);
1176 if (PL_op->op_flags & OPf_REF) {
1181 if (io && IoIFP(io)) {
1183 sv_setpv(PL_statname,"");
1184 PL_laststype = OP_STAT;
1185 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
1189 return PL_laststatval;
1190 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1191 report_evil_fh(gv, io, PL_op->op_type);
1193 sv_setpv(PL_statname,"");
1194 return (PL_laststatval = -1);
1202 if (SvTYPE(sv) == SVt_PVGV) {
1206 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
1213 sv_setpv(PL_statname, s);
1214 PL_laststype = OP_STAT;
1215 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1216 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
1217 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "stat");
1218 return PL_laststatval;
1228 if (PL_op->op_flags & OPf_REF) {
1230 if (cGVOP_gv == PL_defgv) {
1231 if (PL_laststype != OP_LSTAT)
1232 Perl_croak(aTHX_ "The stat preceding -l _ wasn't an lstat");
1233 return PL_laststatval;
1235 Perl_croak(aTHX_ "You can't use -l on a filehandle");
1238 PL_laststype = OP_LSTAT;
1242 sv_setpv(PL_statname,SvPV(sv, n_a));
1243 PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache);
1244 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
1245 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "lstat");
1246 return PL_laststatval;
1250 Perl_do_aexec(pTHX_ SV *really, register SV **mark, register SV **sp)
1252 return do_aexec5(really, mark, sp, 0, 0);
1256 Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1257 int fd, int do_report)
1259 #ifdef MACOS_TRADITIONAL
1260 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1267 New(401,PL_Argv, sp - mark + 1, char*);
1269 while (++mark <= sp) {
1271 *a++ = SvPVx(*mark, n_a);
1277 tmps = SvPV(really, n_a);
1278 if ((!really && *PL_Argv[0] != '/') ||
1279 (really && *tmps != '/')) /* will execvp use PATH? */
1280 TAINT_ENV(); /* testing IFS here is overkill, probably */
1281 if (really && *tmps)
1282 PerlProc_execvp(tmps,PL_Argv);
1284 PerlProc_execvp(PL_Argv[0],PL_Argv);
1285 if (ckWARN(WARN_EXEC))
1286 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1287 (really ? tmps : PL_Argv[0]), Strerror(errno));
1291 PerlLIO_write(fd, (void*)&e, sizeof(int));
1301 Perl_do_execfree(pTHX)
1305 PL_Argv = Null(char **);
1313 #if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC) && !defined(MACOS_TRADITIONAL)
1316 Perl_do_exec(pTHX_ char *cmd)
1318 return do_exec3(cmd,0,0);
1322 Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report)
1328 while (*cmd && isSPACE(*cmd))
1331 /* save an extra exec if possible */
1334 if (strnEQ(cmd,PL_cshname,PL_cshlen) && strnEQ(cmd+PL_cshlen," -c",3)) {
1336 s = cmd+PL_cshlen+3;
1350 if (s[-1] == '\'') {
1352 PerlProc_execl(PL_cshname,"csh", flags,ncmd,(char*)0);
1360 /* see if there are shell metacharacters in it */
1362 if (*cmd == '.' && isSPACE(cmd[1]))
1365 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1368 for (s = cmd; *s && isALNUM(*s); s++) ; /* catch VAR=val gizmo */
1372 for (s = cmd; *s; s++) {
1373 if (*s != ' ' && !isALPHA(*s) && strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1374 if (*s == '\n' && !s[1]) {
1378 /* handle the 2>&1 construct at the end */
1379 if (*s == '>' && s[1] == '&' && s[2] == '1'
1380 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1381 && (!s[3] || isSPACE(s[3])))
1385 while (*t && isSPACE(*t))
1387 if (!*t && (dup2(1,2) != -1)) {
1393 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
1398 New(402,PL_Argv, (s - cmd) / 2 + 2, char*);
1399 PL_Cmd = savepvn(cmd, s-cmd);
1401 for (s = PL_Cmd; *s;) {
1402 while (*s && isSPACE(*s)) s++;
1405 while (*s && !isSPACE(*s)) s++;
1411 PerlProc_execvp(PL_Argv[0],PL_Argv);
1412 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1419 if (ckWARN(WARN_EXEC))
1420 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1421 PL_Argv[0], Strerror(errno));
1423 PerlLIO_write(fd, (void*)&e, sizeof(int));
1432 #endif /* OS2 || WIN32 */
1435 Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
1439 register I32 tot = 0;
1442 SV **oldmark = mark;
1445 #define APPLY_TAINT_PROPER() \
1447 if (PL_tainted) { TAINT_PROPER(what); } \
1450 /* This is a first heuristic; it doesn't catch tainting magic. */
1452 while (++mark <= sp) {
1453 if (SvTAINTED(*mark)) {
1463 APPLY_TAINT_PROPER();
1466 APPLY_TAINT_PROPER();
1468 while (++mark <= sp) {
1469 char *name = SvPVx(*mark, n_a);
1470 APPLY_TAINT_PROPER();
1471 if (PerlLIO_chmod(name, val))
1479 APPLY_TAINT_PROPER();
1480 if (sp - mark > 2) {
1481 val = SvIVx(*++mark);
1482 val2 = SvIVx(*++mark);
1483 APPLY_TAINT_PROPER();
1485 while (++mark <= sp) {
1486 char *name = SvPVx(*mark, n_a);
1487 APPLY_TAINT_PROPER();
1488 if (PerlLIO_chown(name, val, val2))
1495 XXX Should we make lchown() directly available from perl?
1496 For now, we'll let Configure test for HAS_LCHOWN, but do
1497 nothing in the core.
1503 APPLY_TAINT_PROPER();
1506 s = SvPVx(*++mark, n_a);
1508 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1510 if (!(val = whichsig(s)))
1511 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
1515 APPLY_TAINT_PROPER();
1518 /* kill() doesn't do process groups (job trees?) under VMS */
1519 if (val < 0) val = -val;
1520 if (val == SIGKILL) {
1521 # include <starlet.h>
1522 /* Use native sys$delprc() to insure that target process is
1523 * deleted; supervisor-mode images don't pay attention to
1524 * CRTL's emulation of Unix-style signals and kill()
1526 while (++mark <= sp) {
1527 I32 proc = SvIVx(*mark);
1528 register unsigned long int __vmssts;
1529 APPLY_TAINT_PROPER();
1530 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1534 case SS$_NOSUCHNODE:
1535 SETERRNO(ESRCH,__vmssts);
1538 SETERRNO(EPERM,__vmssts);
1541 SETERRNO(EVMSERR,__vmssts);
1550 while (++mark <= sp) {
1551 I32 proc = SvIVx(*mark);
1552 APPLY_TAINT_PROPER();
1554 if (PerlProc_killpg(proc,val)) /* BSD */
1556 if (PerlProc_kill(-proc,val)) /* SYSV */
1562 while (++mark <= sp) {
1563 I32 proc = SvIVx(*mark);
1564 APPLY_TAINT_PROPER();
1565 if (PerlProc_kill(proc, val))
1573 APPLY_TAINT_PROPER();
1575 while (++mark <= sp) {
1576 s = SvPVx(*mark, n_a);
1577 APPLY_TAINT_PROPER();
1578 if (PL_euid || PL_unsafe) {
1582 else { /* don't let root wipe out directories without -U */
1583 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1595 APPLY_TAINT_PROPER();
1596 if (sp - mark > 2) {
1597 #if defined(I_UTIME) || defined(VMS)
1598 struct utimbuf utbuf;
1606 Zero(&utbuf, sizeof utbuf, char);
1608 utbuf.actime = (Time_t)SvNVx(*++mark); /* time accessed */
1609 utbuf.modtime = (Time_t)SvNVx(*++mark); /* time modified */
1611 utbuf.actime = (Time_t)SvIVx(*++mark); /* time accessed */
1612 utbuf.modtime = (Time_t)SvIVx(*++mark); /* time modified */
1614 APPLY_TAINT_PROPER();
1616 while (++mark <= sp) {
1617 char *name = SvPVx(*mark, n_a);
1618 APPLY_TAINT_PROPER();
1619 if (PerlLIO_utime(name, &utbuf))
1630 #undef APPLY_TAINT_PROPER
1633 /* Do the permissions allow some operation? Assumes statcache already set. */
1634 #ifndef VMS /* VMS' cando is in vms.c */
1636 Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register Stat_t *statbufp)
1637 /* Note: we use `effective' both for uids and gids.
1638 * Here we are betting on Uid_t being equal or wider than Gid_t. */
1641 /* [Comments and code from Len Reed]
1642 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1643 * to write-protected files. The execute permission bit is set
1644 * by the Miscrosoft C library stat() function for the following:
1649 * All files and directories are readable.
1650 * Directories and special files, e.g. "CON", cannot be
1652 * [Comment by Tom Dinger -- a directory can have the write-protect
1653 * bit set in the file system, but DOS permits changes to
1654 * the directory anyway. In addition, all bets are off
1655 * here for networked software, such as Novell and
1659 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1660 * too so it will actually look into the files for magic numbers
1662 return (mode & statbufp->st_mode) ? TRUE : FALSE;
1664 #else /* ! DOSISH */
1665 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
1666 if (mode == S_IXUSR) {
1667 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1671 return TRUE; /* root reads and writes anything */
1674 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1675 if (statbufp->st_mode & mode)
1676 return TRUE; /* ok as "user" */
1678 else if (ingroup(statbufp->st_gid,effective)) {
1679 if (statbufp->st_mode & mode >> 3)
1680 return TRUE; /* ok as "group" */
1682 else if (statbufp->st_mode & mode >> 6)
1683 return TRUE; /* ok as "other" */
1685 #endif /* ! DOSISH */
1690 Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective)
1692 #ifdef MACOS_TRADITIONAL
1693 /* This is simply not correct for AppleShare, but fix it yerself. */
1696 if (testgid == (effective ? PL_egid : PL_gid))
1698 #ifdef HAS_GETGROUPS
1703 Groups_t gary[NGROUPS];
1706 anum = getgroups(NGROUPS,gary);
1708 if (gary[anum] == testgid)
1716 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1719 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
1724 key = (key_t)SvNVx(*++mark);
1725 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1726 flags = SvIVx(*++mark);
1732 return msgget(key, flags);
1736 return semget(key, n, flags);
1740 return shmget(key, n, flags);
1742 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1744 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1747 return -1; /* should never happen */
1751 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
1755 I32 id, n, cmd, infosize, getinfo;
1758 id = SvIVx(*++mark);
1759 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1760 cmd = SvIVx(*++mark);
1763 getinfo = (cmd == IPC_STAT);
1769 if (cmd == IPC_STAT || cmd == IPC_SET)
1770 infosize = sizeof(struct msqid_ds);
1775 if (cmd == IPC_STAT || cmd == IPC_SET)
1776 infosize = sizeof(struct shmid_ds);
1782 if (cmd == IPC_STAT || cmd == IPC_SET)
1783 infosize = sizeof(struct semid_ds);
1784 else if (cmd == GETALL || cmd == SETALL)
1786 struct semid_ds semds;
1788 #ifdef EXTRA_F_IN_SEMUN_BUF
1789 semun.buff = &semds;
1793 getinfo = (cmd == GETALL);
1794 if (Semctl(id, 0, IPC_STAT, semun) == -1)
1796 infosize = semds.sem_nsems * sizeof(short);
1797 /* "short" is technically wrong but much more portable
1798 than guessing about u_?short(_t)? */
1801 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1805 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1807 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1816 SvPV_force(astr, len);
1817 a = SvGROW(astr, infosize+1);
1821 a = SvPV(astr, len);
1822 if (len != infosize)
1823 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
1832 a = INT2PTR(char *,i); /* ouch */
1839 ret = msgctl(id, cmd, (struct msqid_ds *)a);
1845 union semun unsemds;
1847 #ifdef EXTRA_F_IN_SEMUN_BUF
1848 unsemds.buff = (struct semid_ds *)a;
1850 unsemds.buf = (struct semid_ds *)a;
1852 ret = Semctl(id, n, cmd, unsemds);
1854 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1861 ret = shmctl(id, cmd, (struct shmid_ds *)a);
1865 if (getinfo && ret >= 0) {
1866 SvCUR_set(astr, infosize);
1867 *SvEND(astr) = '\0';
1874 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
1879 I32 id, msize, flags;
1882 id = SvIVx(*++mark);
1884 flags = SvIVx(*++mark);
1885 mbuf = SvPV(mstr, len);
1886 if ((msize = len - sizeof(long)) < 0)
1887 Perl_croak(aTHX_ "Arg too short for msgsnd");
1889 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
1891 Perl_croak(aTHX_ "msgsnd not implemented");
1896 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
1902 I32 id, msize, flags, ret;
1905 id = SvIVx(*++mark);
1907 /* suppress warning when reading into undef var --jhi */
1909 sv_setpvn(mstr, "", 0);
1910 msize = SvIVx(*++mark);
1911 mtype = (long)SvIVx(*++mark);
1912 flags = SvIVx(*++mark);
1913 SvPV_force(mstr, len);
1914 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
1917 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
1919 SvCUR_set(mstr, sizeof(long)+ret);
1920 *SvEND(mstr) = '\0';
1921 #ifndef INCOMPLETE_TAINTS
1922 /* who knows who has been playing with this message? */
1928 Perl_croak(aTHX_ "msgrcv not implemented");
1933 Perl_do_semop(pTHX_ SV **mark, SV **sp)
1941 id = SvIVx(*++mark);
1943 opbuf = SvPV(opstr, opsize);
1944 if (opsize < sizeof(struct sembuf)
1945 || (opsize % sizeof(struct sembuf)) != 0) {
1946 SETERRNO(EINVAL,LIB$_INVARG);
1950 return semop(id, (struct sembuf *)opbuf, opsize/sizeof(struct sembuf));
1952 Perl_croak(aTHX_ "semop not implemented");
1957 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
1962 I32 id, mpos, msize;
1964 struct shmid_ds shmds;
1966 id = SvIVx(*++mark);
1968 mpos = SvIVx(*++mark);
1969 msize = SvIVx(*++mark);
1971 if (shmctl(id, IPC_STAT, &shmds) == -1)
1973 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
1974 SETERRNO(EFAULT,SS$_ACCVIO); /* can't do as caller requested */
1977 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
1978 if (shm == (char *)-1) /* I hate System V IPC, I really do */
1980 if (optype == OP_SHMREAD) {
1981 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
1983 sv_setpvn(mstr, "", 0);
1984 SvPV_force(mstr, len);
1985 mbuf = SvGROW(mstr, msize+1);
1987 Copy(shm + mpos, mbuf, msize, char);
1988 SvCUR_set(mstr, msize);
1989 *SvEND(mstr) = '\0';
1991 #ifndef INCOMPLETE_TAINTS
1992 /* who knows who has been playing with this shared memory? */
1999 mbuf = SvPV(mstr, len);
2000 if ((n = len) > msize)
2002 Copy(mbuf, shm + mpos, n, char);
2004 memzero(shm + mpos + n, msize - n);
2008 Perl_croak(aTHX_ "shm I/O not implemented");
2012 #endif /* SYSV IPC */