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 #ifdef SOCKS_64BIT_BUG
60 typedef struct __s64_iobuffer {
61 struct __s64_iobuffer *next, *last; /* Queue pointer */
62 PerlIO *fp; /* assigned file pointer */
63 int cnt; /* Buffer counter */
64 int size; /* Buffer size */
65 int *buffer; /* the buffer */
68 static S64_IOB *_s64_get_buffer( PerlIO *f);
69 static S64_IOB *_s64_create_buffer( PerlIO *f);
70 static int _s64_malloc( S64_IOB *ptr);
74 Perl_do_open(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
75 int rawmode, int rawperm, PerlIO *supplied_fp)
77 return do_open9(gv, name, len, as_raw, rawmode, rawperm,
78 supplied_fp, Nullsv, 0);
82 Perl_do_open9(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
83 int rawmode, int rawperm, PerlIO *supplied_fp, SV *svs,
86 register IO *io = GvIOn(gv);
87 PerlIO *saveifp = Nullfp;
88 PerlIO *saveofp = Nullfp;
89 char savetype = IoTYPE_CLOSED;
94 bool was_fdopen = FALSE;
95 bool in_raw = 0, in_crlf = 0, out_raw = 0, out_crlf = 0;
98 char mode[4]; /* stdio file mode ("r\0", "rb\0", "r+b\0" etc.) */
100 Zero(mode,sizeof(mode),char);
101 PL_forkprocess = 1; /* assume true if no fork */
103 /* Collect default raw/crlf info from the op */
104 if (PL_op && PL_op->op_type == OP_OPEN) {
105 /* set up disciplines */
106 U8 flags = PL_op->op_private;
107 in_raw = (flags & OPpOPEN_IN_RAW);
108 in_crlf = (flags & OPpOPEN_IN_CRLF);
109 out_raw = (flags & OPpOPEN_OUT_RAW);
110 out_crlf = (flags & OPpOPEN_OUT_CRLF);
113 /* If currently open - close before we re-open */
115 fd = PerlIO_fileno(IoIFP(io));
116 if (IoTYPE(io) == IoTYPE_STD)
118 else if (fd <= PL_maxsysfd) {
121 savetype = IoTYPE(io);
124 else if (IoTYPE(io) == IoTYPE_PIPE)
125 result = PerlProc_pclose(IoIFP(io));
126 else if (IoIFP(io) != IoOFP(io)) {
128 result = PerlIO_close(IoOFP(io));
129 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
132 result = PerlIO_close(IoIFP(io));
135 result = PerlIO_close(IoIFP(io));
136 if (result == EOF && fd > PL_maxsysfd)
137 PerlIO_printf(Perl_error_log,
138 "Warning: unable to close filehandle %s properly.\n",
140 IoOFP(io) = IoIFP(io) = Nullfp;
144 /* sysopen style args, i.e. integer mode and permissions */
146 #if defined(USE_64_BIT_RAWIO) && defined(O_LARGEFILE)
147 rawmode |= O_LARGEFILE;
151 #define O_ACCMODE 3 /* Assume traditional implementation */
154 switch (result = rawmode & O_ACCMODE) {
156 IoTYPE(io) = IoTYPE_RDONLY;
159 IoTYPE(io) = IoTYPE_WRONLY;
163 IoTYPE(io) = IoTYPE_RDWR;
167 writing = (result > 0);
168 fd = PerlLIO_open3(name, rawmode, rawperm);
174 if (result == O_RDONLY) {
178 else if (rawmode & O_APPEND) {
180 if (result != O_WRONLY)
185 if (result == O_WRONLY)
192 if (rawmode & O_BINARY)
195 fp = PerlIO_fdopen(fd, mode);
201 /* Regular (non-sys) open */
207 type = savepvn(name, len);
210 /* Loose trailing white space */
211 while (tend > type && isSPACE(tend[-1]))
214 /* New style explict name, type is just mode and discipline/layer info */
216 name = SvPV(svs, l) ;
218 name = savepvn(name, len);
221 for (; isSPACE(*type); type++) ;
228 if (*type == IoTYPE_RDWR && (!num_svs || tend > type+1 && tend[-1] != IoTYPE_PIPE)) { /* scary */
233 if (*type == IoTYPE_PIPE) {
235 if (type[1] != IoTYPE_STD) {
237 Perl_croak(aTHX_ "Unknown open() mode '%.*s'", (int)olen, oname);
242 for (type++; isSPACE(*type); type++) ;
247 if (*name == '\0') { /* command is missing 19990114 */
249 if (ckWARN(WARN_PIPE))
250 Perl_warner(aTHX_ WARN_PIPE, "Missing command in piped open");
254 if (strNE(name,"-") || num_svs)
256 TAINT_PROPER("piped open");
257 if (!num_svs && name[len-1] == '|') {
260 if (ckWARN(WARN_PIPE))
261 Perl_warner(aTHX_ WARN_PIPE, "Can't open bidirectional pipe");
269 fp = PerlProc_popen(name,mode);
271 else if (*type == IoTYPE_WRONLY) {
272 TAINT_PROPER("open");
274 if (*type == IoTYPE_WRONLY) {
275 /* Two IoTYPE_WRONLYs in a row make for an IoTYPE_APPEND. */
276 mode[0] = IoTYPE(io) = IoTYPE_APPEND;
299 if (!*name && supplied_fp)
303 for (; isSPACE(*name); name++) ;
308 gv = gv_fetchpv(name,FALSE,SVt_PVIO);
312 SETERRNO(EINVAL,SS$_IVCHAN);
317 PerlIO *fp = IoIFP(thatio);
318 /* Flush stdio buffer before dup. --mjd
319 * Unfortunately SEEK_CURing 0 seems to
320 * be optimized away on most platforms;
321 * only Solaris and Linux seem to flush
324 /* sfio fails to clear error on next
325 sfwrite, contrary to documentation.
327 if (PerlIO_seek(fp, 0, SEEK_CUR) == -1)
330 /* On the other hand, do all platforms
331 * take gracefully to flushing a read-only
332 * filehandle? Perhaps we should do
333 * fsetpos(src)+fgetpos(dst)? --nik */
335 fd = PerlIO_fileno(fp);
336 /* When dup()ing STDIN, STDOUT or STDERR
337 * explicitly set appropriate access mode */
338 if (IoIFP(thatio) == PerlIO_stdout()
339 || IoIFP(thatio) == PerlIO_stderr())
340 IoTYPE(io) = IoTYPE_WRONLY;
341 else if (IoIFP(thatio) == PerlIO_stdin())
342 IoTYPE(io) = IoTYPE_RDONLY;
343 /* When dup()ing a socket, say result is
345 else if (IoTYPE(thatio) == IoTYPE_SOCKET)
346 IoTYPE(io) = IoTYPE_SOCKET;
352 fd = PerlLIO_dup(fd);
355 if (!(fp = PerlIO_fdopen(fd,mode))) {
363 for (; isSPACE(*type); type++) ;
364 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
367 fp = PerlIO_stdout();
368 IoTYPE(io) = IoTYPE_STD;
371 fp = PerlIO_open((num_svs ? name : type), mode);
375 else if (*type == IoTYPE_RDONLY) {
377 for (type++; isSPACE(*type); type++) ;
388 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
392 IoTYPE(io) = IoTYPE_STD;
395 fp = PerlIO_open((num_svs ? name : type), mode);
397 else if ((num_svs && type[0] == IoTYPE_STD && type[1] == IoTYPE_PIPE) ||
398 (!num_svs && tend > type+1 && tend[-1] == IoTYPE_PIPE)) {
400 type += 2; /* skip over '-|' */
404 while (tend > type && isSPACE(tend[-1]))
407 for (; isSPACE(*type); type++) ;
411 if (*name == '\0') { /* command is missing 19990114 */
413 if (ckWARN(WARN_PIPE))
414 Perl_warner(aTHX_ WARN_PIPE, "Missing command in piped open");
418 if (strNE(name,"-") || num_svs)
420 TAINT_PROPER("piped open");
426 fp = PerlProc_popen(name,mode);
427 IoTYPE(io) = IoTYPE_PIPE;
433 IoTYPE(io) = IoTYPE_RDONLY;
435 for (; isSPACE(*name); name++) ;
441 if (strEQ(name,"-")) {
443 IoTYPE(io) = IoTYPE_STD;
446 fp = PerlIO_open(name,mode);
452 if (ckWARN(WARN_NEWLINE) && IoTYPE(io) == IoTYPE_RDONLY && strchr(name, '\n'))
453 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "open");
456 if (IoTYPE(io) && IoTYPE(io) != IoTYPE_PIPE && IoTYPE(io) != IoTYPE_STD) {
458 if (PerlLIO_fstat(PerlIO_fileno(fp),&PL_statbuf) < 0) {
459 (void)PerlIO_close(fp);
462 if (S_ISSOCK(PL_statbuf.st_mode))
463 IoTYPE(io) = IoTYPE_SOCKET; /* in case a socket was passed in to us */
467 !(PL_statbuf.st_mode & S_IFMT)
471 && IoTYPE(io) != IoTYPE_WRONLY /* Dups of STD* filehandles already have */
472 && IoTYPE(io) != IoTYPE_RDONLY /* type so they aren't marked as sockets */
473 ) { /* on OS's that return 0 on fstat()ed pipe */
475 Sock_size_t buflen = sizeof tmpbuf;
476 if (PerlSock_getsockname(PerlIO_fileno(fp), (struct sockaddr *)tmpbuf,
478 || errno != ENOTSOCK)
479 IoTYPE(io) = IoTYPE_SOCKET; /* some OS's return 0 on fstat()ed socket */
480 /* but some return 0 for streams too, sigh */
484 if (saveifp) { /* must use old fp? */
485 fd = PerlIO_fileno(saveifp);
487 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
488 if (saveofp != saveifp) { /* was a socket? */
489 PerlIO_close(saveofp);
494 if (fd != PerlIO_fileno(fp)) {
498 PerlLIO_dup2(PerlIO_fileno(fp), fd);
500 sv = *av_fetch(PL_fdpid,PerlIO_fileno(fp),TRUE);
501 (void)SvUPGRADE(sv, SVt_IV);
504 sv = *av_fetch(PL_fdpid,fd,TRUE);
506 (void)SvUPGRADE(sv, SVt_IV);
515 #if defined(HAS_FCNTL) && defined(F_SETFD)
517 int save_errno = errno;
518 fd = PerlIO_fileno(fp);
519 fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
525 /* Need to supply default type info from open.pm */
526 SV *layers = PL_curcop->cop_io;
530 type = SvPV(layers,len);
531 if (type && mode[0] != 'r') {
532 /* Skip to write part */
533 char *s = strchr(type,0);
534 if (s && (s-type) < len) {
539 else if (O_BINARY != O_TEXT) {
544 while (isSPACE(*type)) type++;
546 if (PerlIO_apply_layers(aTHX_ IoIFP(io),mode,type) != 0) {
552 IoFLAGS(io) &= ~IOf_NOLINE;
555 if (IoTYPE(io) == IoTYPE_SOCKET
556 || (IoTYPE(io) == IoTYPE_WRONLY && S_ISCHR(PL_statbuf.st_mode)) )
559 if (!(IoOFP(io) = PerlIO_fdopen(PerlIO_fileno(fp),mode))) {
565 if (PerlIO_apply_layers(aTHX_ IoOFP(io),mode,type) != 0) {
566 PerlIO_close(IoOFP(io));
582 IoTYPE(io) = savetype;
587 Perl_nextargv(pTHX_ register GV *gv)
590 #ifndef FLEXFILENAMES
599 PL_argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO);
600 if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
601 IoFLAGS(io) &= ~IOf_START;
603 if (!PL_argvout_stack)
604 PL_argvout_stack = newAV();
605 av_push(PL_argvout_stack, SvREFCNT_inc(PL_defoutgv));
608 if (PL_filemode & (S_ISUID|S_ISGID)) {
609 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
611 (void)fchmod(PL_lastfd,PL_filemode);
613 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
617 while (av_len(GvAV(gv)) >= 0) {
620 sv = av_shift(GvAV(gv));
622 sv_setsv(GvSV(gv),sv);
623 SvSETMAGIC(GvSV(gv));
624 PL_oldname = SvPVx(GvSV(gv), oldlen);
625 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,Nullfp)) {
627 TAINT_PROPER("inplace open");
628 if (oldlen == 1 && *PL_oldname == '-') {
629 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
630 return IoIFP(GvIOp(gv));
632 #ifndef FLEXFILENAMES
633 filedev = PL_statbuf.st_dev;
634 fileino = PL_statbuf.st_ino;
636 PL_filemode = PL_statbuf.st_mode;
637 fileuid = PL_statbuf.st_uid;
638 filegid = PL_statbuf.st_gid;
639 if (!S_ISREG(PL_filemode)) {
640 if (ckWARN_d(WARN_INPLACE))
641 Perl_warner(aTHX_ WARN_INPLACE,
642 "Can't do inplace edit: %s is not a regular file",
648 char *star = strchr(PL_inplace, '*');
650 char *begin = PL_inplace;
651 sv_setpvn(sv, "", 0);
653 sv_catpvn(sv, begin, star - begin);
654 sv_catpvn(sv, PL_oldname, oldlen);
656 } while ((star = strchr(begin, '*')));
661 sv_catpv(sv,PL_inplace);
663 #ifndef FLEXFILENAMES
664 if (PerlLIO_stat(SvPVX(sv),&PL_statbuf) >= 0
665 && PL_statbuf.st_dev == filedev
666 && PL_statbuf.st_ino == fileino
668 || (_djstat_fail_bits & _STFAIL_TRUENAME)!=0
672 if (ckWARN_d(WARN_INPLACE))
673 Perl_warner(aTHX_ WARN_INPLACE,
674 "Can't do inplace edit: %s would not be unique",
681 #if !defined(DOSISH) && !defined(__CYGWIN__)
682 if (PerlLIO_rename(PL_oldname,SvPVX(sv)) < 0) {
683 if (ckWARN_d(WARN_INPLACE))
684 Perl_warner(aTHX_ WARN_INPLACE,
685 "Can't rename %s to %s: %s, skipping file",
686 PL_oldname, SvPVX(sv), Strerror(errno) );
692 (void)PerlLIO_unlink(SvPVX(sv));
693 (void)PerlLIO_rename(PL_oldname,SvPVX(sv));
694 do_open(gv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,O_RDONLY,0,Nullfp);
697 (void)UNLINK(SvPVX(sv));
698 if (link(PL_oldname,SvPVX(sv)) < 0) {
699 if (ckWARN_d(WARN_INPLACE))
700 Perl_warner(aTHX_ WARN_INPLACE,
701 "Can't rename %s to %s: %s, skipping file",
702 PL_oldname, SvPVX(sv), Strerror(errno) );
706 (void)UNLINK(PL_oldname);
710 #if !defined(DOSISH) && !defined(AMIGAOS)
711 # ifndef VMS /* Don't delete; use automatic file versioning */
712 if (UNLINK(PL_oldname) < 0) {
713 if (ckWARN_d(WARN_INPLACE))
714 Perl_warner(aTHX_ WARN_INPLACE,
715 "Can't remove %s: %s, skipping file",
716 PL_oldname, Strerror(errno) );
722 Perl_croak(aTHX_ "Can't do inplace edit without backup");
726 sv_setpvn(sv,">",!PL_inplace);
727 sv_catpvn(sv,PL_oldname,oldlen);
728 SETERRNO(0,0); /* in case sprintf set errno */
730 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
731 O_WRONLY|O_CREAT|O_TRUNC,0,Nullfp))
733 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
734 O_WRONLY|O_CREAT|OPEN_EXCL,0666,Nullfp))
737 if (ckWARN_d(WARN_INPLACE))
738 Perl_warner(aTHX_ WARN_INPLACE, "Can't do inplace edit on %s: %s",
739 PL_oldname, Strerror(errno) );
743 setdefout(PL_argvoutgv);
744 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
745 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
747 (void)fchmod(PL_lastfd,PL_filemode);
749 # if !(defined(WIN32) && defined(__BORLANDC__))
750 /* Borland runtime creates a readonly file! */
751 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
754 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
756 (void)fchown(PL_lastfd,fileuid,filegid);
759 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
764 return IoIFP(GvIOp(gv));
768 if (ckWARN_d(WARN_INPLACE)) {
770 if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0
771 && !S_ISREG(PL_statbuf.st_mode))
773 Perl_warner(aTHX_ WARN_INPLACE,
774 "Can't do inplace edit: %s is not a regular file",
778 Perl_warner(aTHX_ WARN_INPLACE, "Can't open %s: %s",
779 PL_oldname, Strerror(eno));
783 if (io && (IoFLAGS(io) & IOf_ARGV))
784 IoFLAGS(io) |= IOf_START;
786 (void)do_close(PL_argvoutgv,FALSE);
787 if (io && (IoFLAGS(io) & IOf_ARGV)
788 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
790 GV *oldout = (GV*)av_pop(PL_argvout_stack);
792 SvREFCNT_dec(oldout);
795 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
802 Perl_do_pipe(pTHX_ SV *sv, GV *rgv, GV *wgv)
821 if (PerlProc_pipe(fd) < 0)
823 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r");
824 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w");
825 IoIFP(wstio) = IoOFP(wstio);
826 IoTYPE(rstio) = IoTYPE_RDONLY;
827 IoTYPE(wstio) = IoTYPE_WRONLY;
828 if (!IoIFP(rstio) || !IoOFP(wstio)) {
829 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
830 else PerlLIO_close(fd[0]);
831 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
832 else PerlLIO_close(fd[1]);
836 sv_setsv(sv,&PL_sv_yes);
840 sv_setsv(sv,&PL_sv_undef);
845 /* explicit renamed to avoid C++ conflict -- kja */
847 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
854 if (!gv || SvTYPE(gv) != SVt_PVGV) {
856 SETERRNO(EBADF,SS$_IVCHAN);
860 if (!io) { /* never opened */
863 if (ckWARN(WARN_UNOPENED)) /* no check for closed here */
864 report_evil_fh(gv, io, PL_op->op_type);
865 SETERRNO(EBADF,SS$_IVCHAN);
869 retval = io_close(io, not_implicit);
873 IoLINES_LEFT(io) = IoPAGE_LEN(io);
875 IoTYPE(io) = IoTYPE_CLOSED;
880 Perl_io_close(pTHX_ IO *io, bool not_implicit)
886 if (IoTYPE(io) == IoTYPE_PIPE) {
887 status = PerlProc_pclose(IoIFP(io));
889 STATUS_NATIVE_SET(status);
890 retval = (STATUS_POSIX == 0);
893 retval = (status != -1);
896 else if (IoTYPE(io) == IoTYPE_STD)
899 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
900 retval = (PerlIO_close(IoOFP(io)) != EOF);
901 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
904 retval = (PerlIO_close(IoIFP(io)) != EOF);
906 IoOFP(io) = IoIFP(io) = Nullfp;
908 else if (not_implicit) {
909 SETERRNO(EBADF,SS$_IVCHAN);
916 Perl_do_eof(pTHX_ GV *gv)
926 else if (ckWARN(WARN_IO)
927 && (IoTYPE(io) == IoTYPE_WRONLY || IoIFP(io) == PerlIO_stdout()
928 || IoIFP(io) == PerlIO_stderr()))
930 /* integrate to report_evil_fh()? */
933 SV* sv = sv_newmortal();
934 gv_efullname4(sv, gv, Nullch, FALSE);
935 name = SvPV_nolen(sv);
938 Perl_warner(aTHX_ WARN_IO,
939 "Filehandle %s opened only for output", name);
941 Perl_warner(aTHX_ WARN_IO,
942 "Filehandle opened only for output");
947 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
948 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
949 return FALSE; /* this is the most usual case */
952 ch = PerlIO_getc(IoIFP(io));
954 (void)PerlIO_ungetc(IoIFP(io),ch);
958 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
959 if (PerlIO_get_cnt(IoIFP(io)) < -1)
960 PerlIO_set_cnt(IoIFP(io),-1);
962 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
963 if (!nextargv(PL_argvgv)) /* get another fp handy */
967 return TRUE; /* normal fp, definitely end of file */
973 Perl_do_tell(pTHX_ GV *gv)
978 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
979 #ifdef ULTRIX_STDIO_BOTCH
981 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
983 return PerlIO_tell(fp);
987 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
988 report_evil_fh(gv, io, PL_op->op_type);
990 SETERRNO(EBADF,RMS$_IFI);
995 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
1000 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1001 #ifdef ULTRIX_STDIO_BOTCH
1003 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1005 return PerlIO_seek(fp, pos, whence) >= 0;
1009 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1010 report_evil_fh(gv, io, PL_op->op_type);
1012 SETERRNO(EBADF,RMS$_IFI);
1017 Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
1020 register PerlIO *fp;
1022 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
1023 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
1026 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1027 report_evil_fh(gv, io, PL_op->op_type);
1029 SETERRNO(EBADF,RMS$_IFI);
1034 Perl_mode_from_discipline(pTHX_ SV *discp)
1036 int mode = O_BINARY;
1039 char *s = SvPV(discp,len);
1044 if (len > 3 && strnEQ(s+1, "raw", 3)
1045 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1054 if (len > 4 && strnEQ(s+1, "crlf", 4)
1055 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1064 goto fail_discipline;
1067 else if (isSPACE(*s)) {
1074 end = strchr(s+1, ':');
1077 Perl_croak(aTHX_ "Unknown discipline '%.*s'", end-s, s);
1085 Perl_do_binmode(pTHX_ PerlIO *fp, int iotype, int mode)
1088 # if defined(atarist) || defined(__MINT__)
1089 if (!PerlIO_flush(fp)) {
1090 if (mode & O_BINARY)
1091 ((FILE*)fp)->_flag |= _IOBIN;
1093 ((FILE*)fp)->_flag &= ~ _IOBIN;
1098 if (PerlLIO_setmode(PerlIO_fileno(fp), mode) != -1) {
1099 # if defined(WIN32) && defined(__BORLANDC__)
1100 /* The translation mode of the stream is maintained independent
1101 * of the translation mode of the fd in the Borland RTL (heavy
1102 * digging through their runtime sources reveal). User has to
1103 * set the mode explicitly for the stream (though they don't
1104 * document this anywhere). GSAR 97-5-24
1106 PerlIO_seek(fp,0L,0);
1107 if (mode & O_BINARY)
1108 ((FILE*)fp)->flags |= _F_BIN;
1110 ((FILE*)fp)->flags &= ~ _F_BIN;
1118 # if defined(USEMYBINMODE)
1119 if (my_binmode(fp, iotype, mode) != FALSE)
1129 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP)
1130 /* code courtesy of William Kucharski */
1133 I32 my_chsize(fd, length)
1134 I32 fd; /* file descriptor */
1135 Off_t length; /* length to set file to */
1138 struct stat filebuf;
1140 if (PerlLIO_fstat(fd, &filebuf) < 0)
1143 if (filebuf.st_size < length) {
1145 /* extend file length */
1147 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
1150 /* write a "0" byte */
1152 if ((PerlLIO_write(fd, "", 1)) != 1)
1156 /* truncate length */
1160 fl.l_start = length;
1161 fl.l_type = F_WRLCK; /* write lock on file space */
1164 * This relies on the UNDOCUMENTED F_FREESP argument to
1165 * fcntl(2), which truncates the file so that it ends at the
1166 * position indicated by fl.l_start.
1168 * Will minor miracles never cease?
1171 if (fcntl(fd, F_FREESP, &fl) < 0)
1178 #endif /* F_FREESP */
1181 Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
1183 register char *tmps;
1186 /* assuming fp is checked earlier */
1192 if (SvIOK(sv) && SvIVX(sv) != 0) {
1193 PerlIO_printf(fp, PL_ofmt, (NV)SvIVX(sv));
1194 return !PerlIO_error(fp);
1196 if ( (SvNOK(sv) && SvNVX(sv) != 0.0)
1197 || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) {
1198 PerlIO_printf(fp, PL_ofmt, SvNVX(sv));
1199 return !PerlIO_error(fp);
1202 switch (SvTYPE(sv)) {
1206 if (ckWARN(WARN_UNINITIALIZED))
1215 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
1217 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
1218 return !PerlIO_error(fp);
1223 /* XXX Fix this when the I/O disciplines arrive. XXX */
1225 sv_utf8_downgrade(sv, FALSE);
1227 tmps = SvPV(sv, len);
1230 /* To detect whether the process is about to overstep its
1231 * filesize limit we would need getrlimit(). We could then
1232 * also transparently raise the limit with setrlimit() --
1233 * but only until the system hard limit/the filesystem limit,
1234 * at which we would get EPERM. Note that when using buffered
1235 * io the write failure can be delayed until the flush/close. --jhi */
1236 if (len && (PerlIO_write(fp,tmps,len) == 0))
1238 return !PerlIO_error(fp);
1248 if (PL_op->op_flags & OPf_REF) {
1253 if (io && IoIFP(io)) {
1255 sv_setpv(PL_statname,"");
1256 PL_laststype = OP_STAT;
1257 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
1261 return PL_laststatval;
1262 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1263 report_evil_fh(gv, io, PL_op->op_type);
1265 sv_setpv(PL_statname,"");
1266 return (PL_laststatval = -1);
1274 if (SvTYPE(sv) == SVt_PVGV) {
1278 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
1285 sv_setpv(PL_statname, s);
1286 PL_laststype = OP_STAT;
1287 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1288 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
1289 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "stat");
1290 return PL_laststatval;
1300 if (PL_op->op_flags & OPf_REF) {
1302 if (cGVOP_gv == PL_defgv) {
1303 if (PL_laststype != OP_LSTAT)
1304 Perl_croak(aTHX_ "The stat preceding -l _ wasn't an lstat");
1305 return PL_laststatval;
1307 Perl_croak(aTHX_ "You can't use -l on a filehandle");
1310 PL_laststype = OP_LSTAT;
1314 sv_setpv(PL_statname,SvPV(sv, n_a));
1315 PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache);
1316 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
1317 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "lstat");
1318 return PL_laststatval;
1322 Perl_do_aexec(pTHX_ SV *really, register SV **mark, register SV **sp)
1324 return do_aexec5(really, mark, sp, 0, 0);
1328 Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1329 int fd, int do_report)
1331 #ifdef MACOS_TRADITIONAL
1332 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1340 New(401,PL_Argv, sp - mark + 1, char*);
1342 while (++mark <= sp) {
1344 *a++ = SvPVx(*mark, n_a);
1349 if (*PL_Argv[0] != '/') /* will execvp use PATH? */
1350 TAINT_ENV(); /* testing IFS here is overkill, probably */
1351 if (really && *(tmps = SvPV(really, n_a)))
1352 PerlProc_execvp(tmps,PL_Argv);
1354 PerlProc_execvp(PL_Argv[0],PL_Argv);
1355 if (ckWARN(WARN_EXEC))
1356 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1357 PL_Argv[0], Strerror(errno));
1361 PerlLIO_write(fd, (void*)&e, sizeof(int));
1371 Perl_do_execfree(pTHX)
1375 PL_Argv = Null(char **);
1383 #if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC) && !defined(MACOS_TRADITIONAL)
1386 Perl_do_exec(pTHX_ char *cmd)
1388 return do_exec3(cmd,0,0);
1392 Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report)
1398 while (*cmd && isSPACE(*cmd))
1401 /* save an extra exec if possible */
1404 if (strnEQ(cmd,PL_cshname,PL_cshlen) && strnEQ(cmd+PL_cshlen," -c",3)) {
1406 s = cmd+PL_cshlen+3;
1420 if (s[-1] == '\'') {
1422 PerlProc_execl(PL_cshname,"csh", flags,ncmd,(char*)0);
1430 /* see if there are shell metacharacters in it */
1432 if (*cmd == '.' && isSPACE(cmd[1]))
1435 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1438 for (s = cmd; *s && isALNUM(*s); s++) ; /* catch VAR=val gizmo */
1442 for (s = cmd; *s; s++) {
1443 if (*s != ' ' && !isALPHA(*s) && strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1444 if (*s == '\n' && !s[1]) {
1448 /* handle the 2>&1 construct at the end */
1449 if (*s == '>' && s[1] == '&' && s[2] == '1'
1450 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1451 && (!s[3] || isSPACE(s[3])))
1455 while (*t && isSPACE(*t))
1457 if (!*t && (dup2(1,2) != -1)) {
1463 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
1468 New(402,PL_Argv, (s - cmd) / 2 + 2, char*);
1469 PL_Cmd = savepvn(cmd, s-cmd);
1471 for (s = PL_Cmd; *s;) {
1472 while (*s && isSPACE(*s)) s++;
1475 while (*s && !isSPACE(*s)) s++;
1481 PerlProc_execvp(PL_Argv[0],PL_Argv);
1482 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1490 if (ckWARN(WARN_EXEC))
1491 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1492 PL_Argv[0], Strerror(errno));
1494 PerlLIO_write(fd, (void*)&e, sizeof(int));
1503 #endif /* OS2 || WIN32 */
1506 Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
1511 register I32 tot = 0;
1514 SV **oldmark = mark;
1517 #define APPLY_TAINT_PROPER() \
1519 if (PL_tainted) { TAINT_PROPER(what); } \
1522 /* This is a first heuristic; it doesn't catch tainting magic. */
1524 while (++mark <= sp) {
1525 if (SvTAINTED(*mark)) {
1535 APPLY_TAINT_PROPER();
1538 APPLY_TAINT_PROPER();
1540 while (++mark <= sp) {
1541 char *name = SvPVx(*mark, n_a);
1542 APPLY_TAINT_PROPER();
1543 if (PerlLIO_chmod(name, val))
1551 APPLY_TAINT_PROPER();
1552 if (sp - mark > 2) {
1553 val = SvIVx(*++mark);
1554 val2 = SvIVx(*++mark);
1555 APPLY_TAINT_PROPER();
1557 while (++mark <= sp) {
1558 char *name = SvPVx(*mark, n_a);
1559 APPLY_TAINT_PROPER();
1560 if (PerlLIO_chown(name, val, val2))
1567 XXX Should we make lchown() directly available from perl?
1568 For now, we'll let Configure test for HAS_LCHOWN, but do
1569 nothing in the core.
1575 APPLY_TAINT_PROPER();
1578 s = SvPVx(*++mark, n_a);
1580 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1582 if (!(val = whichsig(s)))
1583 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
1587 APPLY_TAINT_PROPER();
1590 /* kill() doesn't do process groups (job trees?) under VMS */
1591 if (val < 0) val = -val;
1592 if (val == SIGKILL) {
1593 # include <starlet.h>
1594 /* Use native sys$delprc() to insure that target process is
1595 * deleted; supervisor-mode images don't pay attention to
1596 * CRTL's emulation of Unix-style signals and kill()
1598 while (++mark <= sp) {
1599 I32 proc = SvIVx(*mark);
1600 register unsigned long int __vmssts;
1601 APPLY_TAINT_PROPER();
1602 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1606 case SS$_NOSUCHNODE:
1607 SETERRNO(ESRCH,__vmssts);
1610 SETERRNO(EPERM,__vmssts);
1613 SETERRNO(EVMSERR,__vmssts);
1622 while (++mark <= sp) {
1623 I32 proc = SvIVx(*mark);
1624 APPLY_TAINT_PROPER();
1626 if (PerlProc_killpg(proc,val)) /* BSD */
1628 if (PerlProc_kill(-proc,val)) /* SYSV */
1634 while (++mark <= sp) {
1635 I32 proc = SvIVx(*mark);
1636 APPLY_TAINT_PROPER();
1637 if (PerlProc_kill(proc, val))
1645 APPLY_TAINT_PROPER();
1647 while (++mark <= sp) {
1648 s = SvPVx(*mark, n_a);
1649 APPLY_TAINT_PROPER();
1650 if (PL_euid || PL_unsafe) {
1654 else { /* don't let root wipe out directories without -U */
1655 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1667 APPLY_TAINT_PROPER();
1668 if (sp - mark > 2) {
1669 #if defined(I_UTIME) || defined(VMS)
1670 struct utimbuf utbuf;
1678 Zero(&utbuf, sizeof utbuf, char);
1680 utbuf.actime = (Time_t)SvNVx(*++mark); /* time accessed */
1681 utbuf.modtime = (Time_t)SvNVx(*++mark); /* time modified */
1683 utbuf.actime = (Time_t)SvIVx(*++mark); /* time accessed */
1684 utbuf.modtime = (Time_t)SvIVx(*++mark); /* time modified */
1686 APPLY_TAINT_PROPER();
1688 while (++mark <= sp) {
1689 char *name = SvPVx(*mark, n_a);
1690 APPLY_TAINT_PROPER();
1691 if (PerlLIO_utime(name, &utbuf))
1702 #undef APPLY_TAINT_PROPER
1705 /* Do the permissions allow some operation? Assumes statcache already set. */
1706 #ifndef VMS /* VMS' cando is in vms.c */
1708 Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register Stat_t *statbufp)
1709 /* Note: we use `effective' both for uids and gids.
1710 * Here we are betting on Uid_t being equal or wider than Gid_t. */
1713 /* [Comments and code from Len Reed]
1714 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1715 * to write-protected files. The execute permission bit is set
1716 * by the Miscrosoft C library stat() function for the following:
1721 * All files and directories are readable.
1722 * Directories and special files, e.g. "CON", cannot be
1724 * [Comment by Tom Dinger -- a directory can have the write-protect
1725 * bit set in the file system, but DOS permits changes to
1726 * the directory anyway. In addition, all bets are off
1727 * here for networked software, such as Novell and
1731 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1732 * too so it will actually look into the files for magic numbers
1734 return (mode & statbufp->st_mode) ? TRUE : FALSE;
1736 #else /* ! DOSISH */
1737 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
1738 if (mode == S_IXUSR) {
1739 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1743 return TRUE; /* root reads and writes anything */
1746 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1747 if (statbufp->st_mode & mode)
1748 return TRUE; /* ok as "user" */
1750 else if (ingroup(statbufp->st_gid,effective)) {
1751 if (statbufp->st_mode & mode >> 3)
1752 return TRUE; /* ok as "group" */
1754 else if (statbufp->st_mode & mode >> 6)
1755 return TRUE; /* ok as "other" */
1757 #endif /* ! DOSISH */
1762 Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective)
1764 #ifdef MACOS_TRADITIONAL
1765 /* This is simply not correct for AppleShare, but fix it yerself. */
1768 if (testgid == (effective ? PL_egid : PL_gid))
1770 #ifdef HAS_GETGROUPS
1775 Groups_t gary[NGROUPS];
1778 anum = getgroups(NGROUPS,gary);
1780 if (gary[anum] == testgid)
1788 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1791 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
1797 key = (key_t)SvNVx(*++mark);
1798 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1799 flags = SvIVx(*++mark);
1805 return msgget(key, flags);
1809 return semget(key, n, flags);
1813 return shmget(key, n, flags);
1815 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1817 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1820 return -1; /* should never happen */
1824 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
1829 I32 id, n, cmd, infosize, getinfo;
1832 id = SvIVx(*++mark);
1833 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1834 cmd = SvIVx(*++mark);
1837 getinfo = (cmd == IPC_STAT);
1843 if (cmd == IPC_STAT || cmd == IPC_SET)
1844 infosize = sizeof(struct msqid_ds);
1849 if (cmd == IPC_STAT || cmd == IPC_SET)
1850 infosize = sizeof(struct shmid_ds);
1856 if (cmd == IPC_STAT || cmd == IPC_SET)
1857 infosize = sizeof(struct semid_ds);
1858 else if (cmd == GETALL || cmd == SETALL)
1860 struct semid_ds semds;
1862 #ifdef EXTRA_F_IN_SEMUN_BUF
1863 semun.buff = &semds;
1867 getinfo = (cmd == GETALL);
1868 if (Semctl(id, 0, IPC_STAT, semun) == -1)
1870 infosize = semds.sem_nsems * sizeof(short);
1871 /* "short" is technically wrong but much more portable
1872 than guessing about u_?short(_t)? */
1875 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1879 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1881 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1890 SvPV_force(astr, len);
1891 a = SvGROW(astr, infosize+1);
1895 a = SvPV(astr, len);
1896 if (len != infosize)
1897 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
1906 a = INT2PTR(char *,i); /* ouch */
1913 ret = msgctl(id, cmd, (struct msqid_ds *)a);
1919 union semun unsemds;
1921 #ifdef EXTRA_F_IN_SEMUN_BUF
1922 unsemds.buff = (struct semid_ds *)a;
1924 unsemds.buf = (struct semid_ds *)a;
1926 ret = Semctl(id, n, cmd, unsemds);
1928 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1935 ret = shmctl(id, cmd, (struct shmid_ds *)a);
1939 if (getinfo && ret >= 0) {
1940 SvCUR_set(astr, infosize);
1941 *SvEND(astr) = '\0';
1948 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
1954 I32 id, msize, flags;
1957 id = SvIVx(*++mark);
1959 flags = SvIVx(*++mark);
1960 mbuf = SvPV(mstr, len);
1961 if ((msize = len - sizeof(long)) < 0)
1962 Perl_croak(aTHX_ "Arg too short for msgsnd");
1964 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
1966 Perl_croak(aTHX_ "msgsnd not implemented");
1971 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
1978 I32 id, msize, flags, ret;
1981 id = SvIVx(*++mark);
1983 /* suppress warning when reading into undef var --jhi */
1985 sv_setpvn(mstr, "", 0);
1986 msize = SvIVx(*++mark);
1987 mtype = (long)SvIVx(*++mark);
1988 flags = SvIVx(*++mark);
1989 SvPV_force(mstr, len);
1990 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
1993 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
1995 SvCUR_set(mstr, sizeof(long)+ret);
1996 *SvEND(mstr) = '\0';
1997 #ifndef INCOMPLETE_TAINTS
1998 /* who knows who has been playing with this message? */
2004 Perl_croak(aTHX_ "msgrcv not implemented");
2009 Perl_do_semop(pTHX_ SV **mark, SV **sp)
2018 id = SvIVx(*++mark);
2020 opbuf = SvPV(opstr, opsize);
2021 if (opsize < sizeof(struct sembuf)
2022 || (opsize % sizeof(struct sembuf)) != 0) {
2023 SETERRNO(EINVAL,LIB$_INVARG);
2027 return semop(id, (struct sembuf *)opbuf, opsize/sizeof(struct sembuf));
2029 Perl_croak(aTHX_ "semop not implemented");
2034 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
2040 I32 id, mpos, msize;
2042 struct shmid_ds shmds;
2044 id = SvIVx(*++mark);
2046 mpos = SvIVx(*++mark);
2047 msize = SvIVx(*++mark);
2049 if (shmctl(id, IPC_STAT, &shmds) == -1)
2051 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
2052 SETERRNO(EFAULT,SS$_ACCVIO); /* can't do as caller requested */
2055 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
2056 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2058 if (optype == OP_SHMREAD) {
2059 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2061 sv_setpvn(mstr, "", 0);
2062 SvPV_force(mstr, len);
2063 mbuf = SvGROW(mstr, msize+1);
2065 Copy(shm + mpos, mbuf, msize, char);
2066 SvCUR_set(mstr, msize);
2067 *SvEND(mstr) = '\0';
2069 #ifndef INCOMPLETE_TAINTS
2070 /* who knows who has been playing with this shared memory? */
2077 mbuf = SvPV(mstr, len);
2078 if ((n = len) > msize)
2080 Copy(mbuf, shm + mpos, n, char);
2082 memzero(shm + mpos + n, msize - n);
2086 Perl_croak(aTHX_ "shm I/O not implemented");
2090 #endif /* SYSV IPC */
2092 #ifdef SOCKS_64BIT_BUG
2095 ** getc and ungetc wrappers for the 64 bit problems with SOCKS 5 support
2096 ** Workaround to the problem, that SOCKS maps a socket 'getc' to revc
2097 ** without checking the ungetc buffer.
2100 static S64_IOB *s64_buffer = (S64_IOB *) NULL;
2102 /* get a buffered stream pointer */
2103 static S64_IOB *S_s64_get_buffer( PerlIO *fp) {
2104 S64_IOB *ptr = s64_buffer;
2105 while( ptr && ptr->fp != fp)
2110 /* create a buffered stream pointer */
2111 static S64_IOB *_s64_create_buffer( PerlIO *f) {
2112 S64_IOB *ptr = malloc( sizeof( S64_IOB));
2115 ptr->cnt = ptr->size = 0;
2116 ptr->buffer = (int *) NULL;
2117 ptr->next = s64_buffer;
2118 ptr->last = (S64_IOB *) NULL;
2119 if( s64_buffer) s64_buffer->last = ptr;
2125 /* delete a buffered stream pointer */
2126 void Perl_do_s64_delete_buffer( PerlIO *f) {
2127 S64_IOB *ptr = _s64_get_buffer(f);
2129 /* fix the stream pointer according to the bytes buffered */
2130 /* required, if this is called in a seek-context */
2131 if( ptr->cnt) fseek(f,-ptr->cnt,SEEK_CUR);
2132 if( ptr->buffer) free( ptr->buffer);
2134 ptr->last->next = ptr->next;
2136 s64_buffer = ptr->next;
2141 /* internal buffer management */
2142 #define _S64_BUFFER_SIZE 32
2143 static int _s64_malloc( S64_IOB *ptr) {
2146 ptr->buffer = (int *) calloc( _S64_BUFFER_SIZE, sizeof( int));
2147 ptr->size = ptr->cnt = 0;
2149 ptr->buffer = (int *) realloc( ptr->buffer, ptr->size + _S64_BUFFER_SIZE);
2155 ptr->size += _S64_BUFFER_SIZE;
2163 /* SOCKS 64 bit getc replacement */
2164 int Perl_do_s64_getc( PerlIO *f) {
2165 S64_IOB *ptr = _s64_get_buffer(f);
2168 return( ptr->buffer[--ptr->cnt]);
2173 /* SOCKS 64 bit ungetc replacement */
2174 int Perl_do_s64_ungetc( int ch, PerlIO *f) {
2175 S64_IOB *ptr = _s64_get_buffer(f);
2177 if( !ptr) ptr=_s64_create_buffer(f);
2178 if( !ptr) return( EOF);
2179 if( !ptr->buffer || (ptr->buffer && ptr->cnt >= ptr->size))
2180 if( !_s64_malloc( ptr)) return( EOF);
2181 ptr->buffer[ptr->cnt++] = ch;
2186 /* SOCKS 64 bit fread replacement */
2187 SSize_t Perl_do_s64_fread(void *buf, SSize_t count, PerlIO* f) {
2189 char *bufptr = (char *) buf;
2190 S64_IOB *ptr = _s64_get_buffer(f);
2192 while( ptr->cnt && count) {
2193 *bufptr++ = ptr->buffer[--ptr->cnt];
2198 len += (SSize_t)fread(bufptr,1,count,f);
2203 /* SOCKS 64 bit fseek replacement */
2204 int Perl_do_s64_seek(PerlIO* f, Off_t offset, int whence) {
2205 S64_IOB *ptr = _s64_get_buffer(f);
2207 /* Simply clear the buffer and seek if the position is absolute */
2208 if( SEEK_SET == whence || SEEK_END == whence) {
2209 if( ptr) ptr->cnt = 0;
2211 /* In case of relative positioning clear the buffer and calculate */
2212 /* a fixed offset */
2213 } else if( SEEK_CUR == whence) {
2215 offset -= (Off_t)ptr->cnt;
2220 /* leave out buffer untouched otherwise, because fseek will fail */
2222 return( fseeko( f, offset, whence));
2225 /* SOCKS 64 bit ftell replacement */
2226 Off_t Perl_do_s64_tell(PerlIO* f) {
2228 S64_IOB *ptr = _s64_get_buffer(f);
2231 return( ftello(f) - offset);