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++) ;
436 if (strEQ(name,"-")) {
438 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");
457 IoTYPE(io) != IoTYPE_PIPE && IoTYPE(io) != IoTYPE_STD) {
459 if (PerlLIO_fstat(PerlIO_fileno(fp),&PL_statbuf) < 0) {
460 (void)PerlIO_close(fp);
463 if (S_ISSOCK(PL_statbuf.st_mode))
464 IoTYPE(io) = IoTYPE_SOCKET; /* in case a socket was passed in to us */
468 !(PL_statbuf.st_mode & S_IFMT)
472 && IoTYPE(io) != IoTYPE_WRONLY /* Dups of STD* filehandles already have */
473 && IoTYPE(io) != IoTYPE_RDONLY /* type so they aren't marked as sockets */
474 ) { /* on OS's that return 0 on fstat()ed pipe */
476 Sock_size_t buflen = sizeof tmpbuf;
477 if (PerlSock_getsockname(PerlIO_fileno(fp), (struct sockaddr *)tmpbuf,
479 || errno != ENOTSOCK)
480 IoTYPE(io) = IoTYPE_SOCKET; /* some OS's return 0 on fstat()ed socket */
481 /* but some return 0 for streams too, sigh */
485 if (saveifp) { /* must use old fp? */
486 fd = PerlIO_fileno(saveifp);
488 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
489 if (saveofp != saveifp) { /* was a socket? */
490 PerlIO_close(saveofp);
495 if (fd != PerlIO_fileno(fp)) {
499 PerlLIO_dup2(PerlIO_fileno(fp), fd);
501 sv = *av_fetch(PL_fdpid,PerlIO_fileno(fp),TRUE);
502 (void)SvUPGRADE(sv, SVt_IV);
505 sv = *av_fetch(PL_fdpid,fd,TRUE);
507 (void)SvUPGRADE(sv, SVt_IV);
516 #if defined(HAS_FCNTL) && defined(F_SETFD)
518 int save_errno = errno;
519 fd = PerlIO_fileno(fp);
520 fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
526 /* Need to supply default type info from open.pm */
527 SV *layers = PL_curcop->cop_io;
531 type = SvPV(layers,len);
532 if (type && mode[0] != 'r') {
533 /* Skip to write part */
534 char *s = strchr(type,0);
535 if (s && (s-type) < len) {
540 else if (O_BINARY != O_TEXT) {
545 while (isSPACE(*type)) type++;
547 if (PerlIO_apply_layers(aTHX_ IoIFP(io),mode,type) != 0) {
553 IoFLAGS(io) &= ~IOf_NOLINE;
556 if (IoTYPE(io) == IoTYPE_SOCKET
557 || (IoTYPE(io) == IoTYPE_WRONLY && S_ISCHR(PL_statbuf.st_mode)) )
560 if (!(IoOFP(io) = PerlIO_fdopen(PerlIO_fileno(fp),mode))) {
566 if (PerlIO_apply_layers(aTHX_ IoOFP(io),mode,type) != 0) {
567 PerlIO_close(IoOFP(io));
583 IoTYPE(io) = savetype;
588 Perl_nextargv(pTHX_ register GV *gv)
591 #ifndef FLEXFILENAMES
600 PL_argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO);
601 if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
602 IoFLAGS(io) &= ~IOf_START;
604 if (!PL_argvout_stack)
605 PL_argvout_stack = newAV();
606 av_push(PL_argvout_stack, SvREFCNT_inc(PL_defoutgv));
609 if (PL_filemode & (S_ISUID|S_ISGID)) {
610 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
612 (void)fchmod(PL_lastfd,PL_filemode);
614 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
618 while (av_len(GvAV(gv)) >= 0) {
621 sv = av_shift(GvAV(gv));
623 sv_setsv(GvSV(gv),sv);
624 SvSETMAGIC(GvSV(gv));
625 PL_oldname = SvPVx(GvSV(gv), oldlen);
626 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,Nullfp)) {
628 TAINT_PROPER("inplace open");
629 if (oldlen == 1 && *PL_oldname == '-') {
630 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
631 return IoIFP(GvIOp(gv));
633 #ifndef FLEXFILENAMES
634 filedev = PL_statbuf.st_dev;
635 fileino = PL_statbuf.st_ino;
637 PL_filemode = PL_statbuf.st_mode;
638 fileuid = PL_statbuf.st_uid;
639 filegid = PL_statbuf.st_gid;
640 if (!S_ISREG(PL_filemode)) {
641 if (ckWARN_d(WARN_INPLACE))
642 Perl_warner(aTHX_ WARN_INPLACE,
643 "Can't do inplace edit: %s is not a regular file",
649 char *star = strchr(PL_inplace, '*');
651 char *begin = PL_inplace;
652 sv_setpvn(sv, "", 0);
654 sv_catpvn(sv, begin, star - begin);
655 sv_catpvn(sv, PL_oldname, oldlen);
657 } while ((star = strchr(begin, '*')));
662 sv_catpv(sv,PL_inplace);
664 #ifndef FLEXFILENAMES
665 if (PerlLIO_stat(SvPVX(sv),&PL_statbuf) >= 0
666 && PL_statbuf.st_dev == filedev
667 && PL_statbuf.st_ino == fileino
669 || (_djstat_fail_bits & _STFAIL_TRUENAME)!=0
673 if (ckWARN_d(WARN_INPLACE))
674 Perl_warner(aTHX_ WARN_INPLACE,
675 "Can't do inplace edit: %s would not be unique",
682 #if !defined(DOSISH) && !defined(__CYGWIN__)
683 if (PerlLIO_rename(PL_oldname,SvPVX(sv)) < 0) {
684 if (ckWARN_d(WARN_INPLACE))
685 Perl_warner(aTHX_ WARN_INPLACE,
686 "Can't rename %s to %s: %s, skipping file",
687 PL_oldname, SvPVX(sv), Strerror(errno) );
693 (void)PerlLIO_unlink(SvPVX(sv));
694 (void)PerlLIO_rename(PL_oldname,SvPVX(sv));
695 do_open(gv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,O_RDONLY,0,Nullfp);
698 (void)UNLINK(SvPVX(sv));
699 if (link(PL_oldname,SvPVX(sv)) < 0) {
700 if (ckWARN_d(WARN_INPLACE))
701 Perl_warner(aTHX_ WARN_INPLACE,
702 "Can't rename %s to %s: %s, skipping file",
703 PL_oldname, SvPVX(sv), Strerror(errno) );
707 (void)UNLINK(PL_oldname);
711 #if !defined(DOSISH) && !defined(AMIGAOS)
712 # ifndef VMS /* Don't delete; use automatic file versioning */
713 if (UNLINK(PL_oldname) < 0) {
714 if (ckWARN_d(WARN_INPLACE))
715 Perl_warner(aTHX_ WARN_INPLACE,
716 "Can't remove %s: %s, skipping file",
717 PL_oldname, Strerror(errno) );
723 Perl_croak(aTHX_ "Can't do inplace edit without backup");
727 sv_setpvn(sv,">",!PL_inplace);
728 sv_catpvn(sv,PL_oldname,oldlen);
729 SETERRNO(0,0); /* in case sprintf set errno */
731 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
732 O_WRONLY|O_CREAT|O_TRUNC,0,Nullfp))
734 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
735 O_WRONLY|O_CREAT|OPEN_EXCL,0666,Nullfp))
738 if (ckWARN_d(WARN_INPLACE))
739 Perl_warner(aTHX_ WARN_INPLACE, "Can't do inplace edit on %s: %s",
740 PL_oldname, Strerror(errno) );
744 setdefout(PL_argvoutgv);
745 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
746 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
748 (void)fchmod(PL_lastfd,PL_filemode);
750 # if !(defined(WIN32) && defined(__BORLANDC__))
751 /* Borland runtime creates a readonly file! */
752 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
755 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
757 (void)fchown(PL_lastfd,fileuid,filegid);
760 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
765 return IoIFP(GvIOp(gv));
769 if (ckWARN_d(WARN_INPLACE)) {
771 if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0
772 && !S_ISREG(PL_statbuf.st_mode))
774 Perl_warner(aTHX_ WARN_INPLACE,
775 "Can't do inplace edit: %s is not a regular file",
779 Perl_warner(aTHX_ WARN_INPLACE, "Can't open %s: %s",
780 PL_oldname, Strerror(eno));
784 if (io && (IoFLAGS(io) & IOf_ARGV))
785 IoFLAGS(io) |= IOf_START;
787 (void)do_close(PL_argvoutgv,FALSE);
788 if (io && (IoFLAGS(io) & IOf_ARGV)
789 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
791 GV *oldout = (GV*)av_pop(PL_argvout_stack);
793 SvREFCNT_dec(oldout);
796 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
803 Perl_do_pipe(pTHX_ SV *sv, GV *rgv, GV *wgv)
822 if (PerlProc_pipe(fd) < 0)
824 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r");
825 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w");
826 IoIFP(wstio) = IoOFP(wstio);
827 IoTYPE(rstio) = IoTYPE_RDONLY;
828 IoTYPE(wstio) = IoTYPE_WRONLY;
829 if (!IoIFP(rstio) || !IoOFP(wstio)) {
830 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
831 else PerlLIO_close(fd[0]);
832 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
833 else PerlLIO_close(fd[1]);
837 sv_setsv(sv,&PL_sv_yes);
841 sv_setsv(sv,&PL_sv_undef);
846 /* explicit renamed to avoid C++ conflict -- kja */
848 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
855 if (!gv || SvTYPE(gv) != SVt_PVGV) {
857 SETERRNO(EBADF,SS$_IVCHAN);
861 if (!io) { /* never opened */
864 if (ckWARN(WARN_UNOPENED)) /* no check for closed here */
865 report_evil_fh(gv, io, PL_op->op_type);
866 SETERRNO(EBADF,SS$_IVCHAN);
870 retval = io_close(io, not_implicit);
874 IoLINES_LEFT(io) = IoPAGE_LEN(io);
876 IoTYPE(io) = IoTYPE_CLOSED;
881 Perl_io_close(pTHX_ IO *io, bool not_implicit)
887 if (IoTYPE(io) == IoTYPE_PIPE) {
888 status = PerlProc_pclose(IoIFP(io));
890 STATUS_NATIVE_SET(status);
891 retval = (STATUS_POSIX == 0);
894 retval = (status != -1);
897 else if (IoTYPE(io) == IoTYPE_STD)
900 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
901 retval = (PerlIO_close(IoOFP(io)) != EOF);
902 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
905 retval = (PerlIO_close(IoIFP(io)) != EOF);
907 IoOFP(io) = IoIFP(io) = Nullfp;
909 else if (not_implicit) {
910 SETERRNO(EBADF,SS$_IVCHAN);
917 Perl_do_eof(pTHX_ GV *gv)
927 else if (ckWARN(WARN_IO)
928 && (IoTYPE(io) == IoTYPE_WRONLY || IoIFP(io) == PerlIO_stdout()
929 || IoIFP(io) == PerlIO_stderr()))
931 /* integrate to report_evil_fh()? */
934 SV* sv = sv_newmortal();
935 gv_efullname4(sv, gv, Nullch, FALSE);
936 name = SvPV_nolen(sv);
939 Perl_warner(aTHX_ WARN_IO,
940 "Filehandle %s opened only for output", name);
942 Perl_warner(aTHX_ WARN_IO,
943 "Filehandle opened only for output");
948 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
949 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
950 return FALSE; /* this is the most usual case */
953 ch = PerlIO_getc(IoIFP(io));
955 (void)PerlIO_ungetc(IoIFP(io),ch);
959 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
960 if (PerlIO_get_cnt(IoIFP(io)) < -1)
961 PerlIO_set_cnt(IoIFP(io),-1);
963 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
964 if (!nextargv(PL_argvgv)) /* get another fp handy */
968 return TRUE; /* normal fp, definitely end of file */
974 Perl_do_tell(pTHX_ GV *gv)
979 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
980 #ifdef ULTRIX_STDIO_BOTCH
982 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
984 return PerlIO_tell(fp);
988 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
989 report_evil_fh(gv, io, PL_op->op_type);
991 SETERRNO(EBADF,RMS$_IFI);
996 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
1001 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
1002 #ifdef ULTRIX_STDIO_BOTCH
1004 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1006 return PerlIO_seek(fp, pos, whence) >= 0;
1010 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1011 report_evil_fh(gv, io, PL_op->op_type);
1013 SETERRNO(EBADF,RMS$_IFI);
1018 Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
1021 register PerlIO *fp;
1023 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
1024 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
1027 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1028 report_evil_fh(gv, io, PL_op->op_type);
1030 SETERRNO(EBADF,RMS$_IFI);
1035 Perl_mode_from_discipline(pTHX_ SV *discp)
1037 int mode = O_BINARY;
1040 char *s = SvPV(discp,len);
1045 if (len > 3 && strnEQ(s+1, "raw", 3)
1046 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1055 if (len > 4 && strnEQ(s+1, "crlf", 4)
1056 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1065 goto fail_discipline;
1068 else if (isSPACE(*s)) {
1075 end = strchr(s+1, ':');
1078 Perl_croak(aTHX_ "Unknown discipline '%.*s'", end-s, s);
1086 Perl_do_binmode(pTHX_ PerlIO *fp, int iotype, int mode)
1089 # if defined(atarist) || defined(__MINT__)
1090 if (!PerlIO_flush(fp)) {
1091 if (mode & O_BINARY)
1092 ((FILE*)fp)->_flag |= _IOBIN;
1094 ((FILE*)fp)->_flag &= ~ _IOBIN;
1099 if (PerlLIO_setmode(PerlIO_fileno(fp), mode) != -1) {
1100 # if defined(WIN32) && defined(__BORLANDC__)
1101 /* The translation mode of the stream is maintained independent
1102 * of the translation mode of the fd in the Borland RTL (heavy
1103 * digging through their runtime sources reveal). User has to
1104 * set the mode explicitly for the stream (though they don't
1105 * document this anywhere). GSAR 97-5-24
1107 PerlIO_seek(fp,0L,0);
1108 if (mode & O_BINARY)
1109 ((FILE*)fp)->flags |= _F_BIN;
1111 ((FILE*)fp)->flags &= ~ _F_BIN;
1119 # if defined(USEMYBINMODE)
1120 if (my_binmode(fp, iotype, mode) != FALSE)
1130 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP)
1131 /* code courtesy of William Kucharski */
1134 I32 my_chsize(fd, length)
1135 I32 fd; /* file descriptor */
1136 Off_t length; /* length to set file to */
1139 struct stat filebuf;
1141 if (PerlLIO_fstat(fd, &filebuf) < 0)
1144 if (filebuf.st_size < length) {
1146 /* extend file length */
1148 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
1151 /* write a "0" byte */
1153 if ((PerlLIO_write(fd, "", 1)) != 1)
1157 /* truncate length */
1161 fl.l_start = length;
1162 fl.l_type = F_WRLCK; /* write lock on file space */
1165 * This relies on the UNDOCUMENTED F_FREESP argument to
1166 * fcntl(2), which truncates the file so that it ends at the
1167 * position indicated by fl.l_start.
1169 * Will minor miracles never cease?
1172 if (fcntl(fd, F_FREESP, &fl) < 0)
1179 #endif /* F_FREESP */
1182 Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
1184 register char *tmps;
1187 /* assuming fp is checked earlier */
1193 if (SvIOK(sv) && SvIVX(sv) != 0) {
1194 PerlIO_printf(fp, PL_ofmt, (NV)SvIVX(sv));
1195 return !PerlIO_error(fp);
1197 if ( (SvNOK(sv) && SvNVX(sv) != 0.0)
1198 || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) {
1199 PerlIO_printf(fp, PL_ofmt, SvNVX(sv));
1200 return !PerlIO_error(fp);
1203 switch (SvTYPE(sv)) {
1207 if (ckWARN(WARN_UNINITIALIZED))
1216 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
1218 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
1219 return !PerlIO_error(fp);
1224 /* XXX Fix this when the I/O disciplines arrive. XXX */
1226 sv_utf8_downgrade(sv, FALSE);
1228 tmps = SvPV(sv, len);
1231 /* To detect whether the process is about to overstep its
1232 * filesize limit we would need getrlimit(). We could then
1233 * also transparently raise the limit with setrlimit() --
1234 * but only until the system hard limit/the filesystem limit,
1235 * at which we would get EPERM. Note that when using buffered
1236 * io the write failure can be delayed until the flush/close. --jhi */
1237 if (len && (PerlIO_write(fp,tmps,len) == 0))
1239 return !PerlIO_error(fp);
1249 if (PL_op->op_flags & OPf_REF) {
1254 if (io && IoIFP(io)) {
1256 sv_setpv(PL_statname,"");
1257 PL_laststype = OP_STAT;
1258 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
1262 return PL_laststatval;
1263 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1264 report_evil_fh(gv, io, PL_op->op_type);
1266 sv_setpv(PL_statname,"");
1267 return (PL_laststatval = -1);
1275 if (SvTYPE(sv) == SVt_PVGV) {
1279 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
1286 sv_setpv(PL_statname, s);
1287 PL_laststype = OP_STAT;
1288 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1289 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
1290 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "stat");
1291 return PL_laststatval;
1301 if (PL_op->op_flags & OPf_REF) {
1303 if (cGVOP_gv == PL_defgv) {
1304 if (PL_laststype != OP_LSTAT)
1305 Perl_croak(aTHX_ "The stat preceding -l _ wasn't an lstat");
1306 return PL_laststatval;
1308 Perl_croak(aTHX_ "You can't use -l on a filehandle");
1311 PL_laststype = OP_LSTAT;
1315 sv_setpv(PL_statname,SvPV(sv, n_a));
1316 PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache);
1317 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
1318 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "lstat");
1319 return PL_laststatval;
1323 Perl_do_aexec(pTHX_ SV *really, register SV **mark, register SV **sp)
1325 return do_aexec5(really, mark, sp, 0, 0);
1329 Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1330 int fd, int do_report)
1332 #ifdef MACOS_TRADITIONAL
1333 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1341 New(401,PL_Argv, sp - mark + 1, char*);
1343 while (++mark <= sp) {
1345 *a++ = SvPVx(*mark, n_a);
1350 if (*PL_Argv[0] != '/') /* will execvp use PATH? */
1351 TAINT_ENV(); /* testing IFS here is overkill, probably */
1352 if (really && *(tmps = SvPV(really, n_a)))
1353 PerlProc_execvp(tmps,PL_Argv);
1355 PerlProc_execvp(PL_Argv[0],PL_Argv);
1356 if (ckWARN(WARN_EXEC))
1357 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1358 PL_Argv[0], Strerror(errno));
1362 PerlLIO_write(fd, (void*)&e, sizeof(int));
1372 Perl_do_execfree(pTHX)
1376 PL_Argv = Null(char **);
1384 #if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC) && !defined(MACOS_TRADITIONAL)
1387 Perl_do_exec(pTHX_ char *cmd)
1389 return do_exec3(cmd,0,0);
1393 Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report)
1399 while (*cmd && isSPACE(*cmd))
1402 /* save an extra exec if possible */
1405 if (strnEQ(cmd,PL_cshname,PL_cshlen) && strnEQ(cmd+PL_cshlen," -c",3)) {
1407 s = cmd+PL_cshlen+3;
1421 if (s[-1] == '\'') {
1423 PerlProc_execl(PL_cshname,"csh", flags,ncmd,(char*)0);
1431 /* see if there are shell metacharacters in it */
1433 if (*cmd == '.' && isSPACE(cmd[1]))
1436 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1439 for (s = cmd; *s && isALNUM(*s); s++) ; /* catch VAR=val gizmo */
1443 for (s = cmd; *s; s++) {
1444 if (*s != ' ' && !isALPHA(*s) && strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1445 if (*s == '\n' && !s[1]) {
1449 /* handle the 2>&1 construct at the end */
1450 if (*s == '>' && s[1] == '&' && s[2] == '1'
1451 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1452 && (!s[3] || isSPACE(s[3])))
1456 while (*t && isSPACE(*t))
1458 if (!*t && (dup2(1,2) != -1)) {
1464 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
1469 New(402,PL_Argv, (s - cmd) / 2 + 2, char*);
1470 PL_Cmd = savepvn(cmd, s-cmd);
1472 for (s = PL_Cmd; *s;) {
1473 while (*s && isSPACE(*s)) s++;
1476 while (*s && !isSPACE(*s)) s++;
1482 PerlProc_execvp(PL_Argv[0],PL_Argv);
1483 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1491 if (ckWARN(WARN_EXEC))
1492 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1493 PL_Argv[0], Strerror(errno));
1495 PerlLIO_write(fd, (void*)&e, sizeof(int));
1504 #endif /* OS2 || WIN32 */
1507 Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
1512 register I32 tot = 0;
1515 SV **oldmark = mark;
1518 #define APPLY_TAINT_PROPER() \
1520 if (PL_tainted) { TAINT_PROPER(what); } \
1523 /* This is a first heuristic; it doesn't catch tainting magic. */
1525 while (++mark <= sp) {
1526 if (SvTAINTED(*mark)) {
1536 APPLY_TAINT_PROPER();
1539 APPLY_TAINT_PROPER();
1541 while (++mark <= sp) {
1542 char *name = SvPVx(*mark, n_a);
1543 APPLY_TAINT_PROPER();
1544 if (PerlLIO_chmod(name, val))
1552 APPLY_TAINT_PROPER();
1553 if (sp - mark > 2) {
1554 val = SvIVx(*++mark);
1555 val2 = SvIVx(*++mark);
1556 APPLY_TAINT_PROPER();
1558 while (++mark <= sp) {
1559 char *name = SvPVx(*mark, n_a);
1560 APPLY_TAINT_PROPER();
1561 if (PerlLIO_chown(name, val, val2))
1568 XXX Should we make lchown() directly available from perl?
1569 For now, we'll let Configure test for HAS_LCHOWN, but do
1570 nothing in the core.
1576 APPLY_TAINT_PROPER();
1579 s = SvPVx(*++mark, n_a);
1581 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1583 if (!(val = whichsig(s)))
1584 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
1588 APPLY_TAINT_PROPER();
1591 /* kill() doesn't do process groups (job trees?) under VMS */
1592 if (val < 0) val = -val;
1593 if (val == SIGKILL) {
1594 # include <starlet.h>
1595 /* Use native sys$delprc() to insure that target process is
1596 * deleted; supervisor-mode images don't pay attention to
1597 * CRTL's emulation of Unix-style signals and kill()
1599 while (++mark <= sp) {
1600 I32 proc = SvIVx(*mark);
1601 register unsigned long int __vmssts;
1602 APPLY_TAINT_PROPER();
1603 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1607 case SS$_NOSUCHNODE:
1608 SETERRNO(ESRCH,__vmssts);
1611 SETERRNO(EPERM,__vmssts);
1614 SETERRNO(EVMSERR,__vmssts);
1623 while (++mark <= sp) {
1624 I32 proc = SvIVx(*mark);
1625 APPLY_TAINT_PROPER();
1627 if (PerlProc_killpg(proc,val)) /* BSD */
1629 if (PerlProc_kill(-proc,val)) /* SYSV */
1635 while (++mark <= sp) {
1636 I32 proc = SvIVx(*mark);
1637 APPLY_TAINT_PROPER();
1638 if (PerlProc_kill(proc, val))
1646 APPLY_TAINT_PROPER();
1648 while (++mark <= sp) {
1649 s = SvPVx(*mark, n_a);
1650 APPLY_TAINT_PROPER();
1651 if (PL_euid || PL_unsafe) {
1655 else { /* don't let root wipe out directories without -U */
1656 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1668 APPLY_TAINT_PROPER();
1669 if (sp - mark > 2) {
1670 #if defined(I_UTIME) || defined(VMS)
1671 struct utimbuf utbuf;
1679 Zero(&utbuf, sizeof utbuf, char);
1681 utbuf.actime = (Time_t)SvNVx(*++mark); /* time accessed */
1682 utbuf.modtime = (Time_t)SvNVx(*++mark); /* time modified */
1684 utbuf.actime = (Time_t)SvIVx(*++mark); /* time accessed */
1685 utbuf.modtime = (Time_t)SvIVx(*++mark); /* time modified */
1687 APPLY_TAINT_PROPER();
1689 while (++mark <= sp) {
1690 char *name = SvPVx(*mark, n_a);
1691 APPLY_TAINT_PROPER();
1692 if (PerlLIO_utime(name, &utbuf))
1703 #undef APPLY_TAINT_PROPER
1706 /* Do the permissions allow some operation? Assumes statcache already set. */
1707 #ifndef VMS /* VMS' cando is in vms.c */
1709 Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register Stat_t *statbufp)
1710 /* Note: we use `effective' both for uids and gids.
1711 * Here we are betting on Uid_t being equal or wider than Gid_t. */
1714 /* [Comments and code from Len Reed]
1715 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1716 * to write-protected files. The execute permission bit is set
1717 * by the Miscrosoft C library stat() function for the following:
1722 * All files and directories are readable.
1723 * Directories and special files, e.g. "CON", cannot be
1725 * [Comment by Tom Dinger -- a directory can have the write-protect
1726 * bit set in the file system, but DOS permits changes to
1727 * the directory anyway. In addition, all bets are off
1728 * here for networked software, such as Novell and
1732 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1733 * too so it will actually look into the files for magic numbers
1735 return (mode & statbufp->st_mode) ? TRUE : FALSE;
1737 #else /* ! DOSISH */
1738 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
1739 if (mode == S_IXUSR) {
1740 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1744 return TRUE; /* root reads and writes anything */
1747 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1748 if (statbufp->st_mode & mode)
1749 return TRUE; /* ok as "user" */
1751 else if (ingroup(statbufp->st_gid,effective)) {
1752 if (statbufp->st_mode & mode >> 3)
1753 return TRUE; /* ok as "group" */
1755 else if (statbufp->st_mode & mode >> 6)
1756 return TRUE; /* ok as "other" */
1758 #endif /* ! DOSISH */
1763 Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective)
1765 #ifdef MACOS_TRADITIONAL
1766 /* This is simply not correct for AppleShare, but fix it yerself. */
1769 if (testgid == (effective ? PL_egid : PL_gid))
1771 #ifdef HAS_GETGROUPS
1776 Groups_t gary[NGROUPS];
1779 anum = getgroups(NGROUPS,gary);
1781 if (gary[anum] == testgid)
1789 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1792 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
1798 key = (key_t)SvNVx(*++mark);
1799 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1800 flags = SvIVx(*++mark);
1806 return msgget(key, flags);
1810 return semget(key, n, flags);
1814 return shmget(key, n, flags);
1816 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1818 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1821 return -1; /* should never happen */
1825 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
1830 I32 id, n, cmd, infosize, getinfo;
1833 id = SvIVx(*++mark);
1834 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1835 cmd = SvIVx(*++mark);
1838 getinfo = (cmd == IPC_STAT);
1844 if (cmd == IPC_STAT || cmd == IPC_SET)
1845 infosize = sizeof(struct msqid_ds);
1850 if (cmd == IPC_STAT || cmd == IPC_SET)
1851 infosize = sizeof(struct shmid_ds);
1857 if (cmd == IPC_STAT || cmd == IPC_SET)
1858 infosize = sizeof(struct semid_ds);
1859 else if (cmd == GETALL || cmd == SETALL)
1861 struct semid_ds semds;
1863 #ifdef EXTRA_F_IN_SEMUN_BUF
1864 semun.buff = &semds;
1868 getinfo = (cmd == GETALL);
1869 if (Semctl(id, 0, IPC_STAT, semun) == -1)
1871 infosize = semds.sem_nsems * sizeof(short);
1872 /* "short" is technically wrong but much more portable
1873 than guessing about u_?short(_t)? */
1876 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1880 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1882 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1891 SvPV_force(astr, len);
1892 a = SvGROW(astr, infosize+1);
1896 a = SvPV(astr, len);
1897 if (len != infosize)
1898 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
1907 a = INT2PTR(char *,i); /* ouch */
1914 ret = msgctl(id, cmd, (struct msqid_ds *)a);
1920 union semun unsemds;
1922 #ifdef EXTRA_F_IN_SEMUN_BUF
1923 unsemds.buff = (struct semid_ds *)a;
1925 unsemds.buf = (struct semid_ds *)a;
1927 ret = Semctl(id, n, cmd, unsemds);
1929 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1936 ret = shmctl(id, cmd, (struct shmid_ds *)a);
1940 if (getinfo && ret >= 0) {
1941 SvCUR_set(astr, infosize);
1942 *SvEND(astr) = '\0';
1949 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
1955 I32 id, msize, flags;
1958 id = SvIVx(*++mark);
1960 flags = SvIVx(*++mark);
1961 mbuf = SvPV(mstr, len);
1962 if ((msize = len - sizeof(long)) < 0)
1963 Perl_croak(aTHX_ "Arg too short for msgsnd");
1965 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
1967 Perl_croak(aTHX_ "msgsnd not implemented");
1972 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
1979 I32 id, msize, flags, ret;
1982 id = SvIVx(*++mark);
1984 /* suppress warning when reading into undef var --jhi */
1986 sv_setpvn(mstr, "", 0);
1987 msize = SvIVx(*++mark);
1988 mtype = (long)SvIVx(*++mark);
1989 flags = SvIVx(*++mark);
1990 SvPV_force(mstr, len);
1991 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
1994 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
1996 SvCUR_set(mstr, sizeof(long)+ret);
1997 *SvEND(mstr) = '\0';
1998 #ifndef INCOMPLETE_TAINTS
1999 /* who knows who has been playing with this message? */
2005 Perl_croak(aTHX_ "msgrcv not implemented");
2010 Perl_do_semop(pTHX_ SV **mark, SV **sp)
2019 id = SvIVx(*++mark);
2021 opbuf = SvPV(opstr, opsize);
2022 if (opsize < sizeof(struct sembuf)
2023 || (opsize % sizeof(struct sembuf)) != 0) {
2024 SETERRNO(EINVAL,LIB$_INVARG);
2028 return semop(id, (struct sembuf *)opbuf, opsize/sizeof(struct sembuf));
2030 Perl_croak(aTHX_ "semop not implemented");
2035 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
2041 I32 id, mpos, msize;
2043 struct shmid_ds shmds;
2045 id = SvIVx(*++mark);
2047 mpos = SvIVx(*++mark);
2048 msize = SvIVx(*++mark);
2050 if (shmctl(id, IPC_STAT, &shmds) == -1)
2052 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
2053 SETERRNO(EFAULT,SS$_ACCVIO); /* can't do as caller requested */
2056 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
2057 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2059 if (optype == OP_SHMREAD) {
2060 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2062 sv_setpvn(mstr, "", 0);
2063 SvPV_force(mstr, len);
2064 mbuf = SvGROW(mstr, msize+1);
2066 Copy(shm + mpos, mbuf, msize, char);
2067 SvCUR_set(mstr, msize);
2068 *SvEND(mstr) = '\0';
2070 #ifndef INCOMPLETE_TAINTS
2071 /* who knows who has been playing with this shared memory? */
2078 mbuf = SvPV(mstr, len);
2079 if ((n = len) > msize)
2081 Copy(mbuf, shm + mpos, n, char);
2083 memzero(shm + mpos + n, msize - n);
2087 Perl_croak(aTHX_ "shm I/O not implemented");
2091 #endif /* SYSV IPC */
2094 ** getc and ungetc wrappers for the 64 bit problems with SOCKS 5 support
2095 ** Workaround to the problem, that SOCKS maps a socket 'getc' to revc
2096 ** without checking the ungetc buffer.
2098 #ifdef SOCKS_64BIT_BUG
2099 static S64_IOB *s64_buffer = (S64_IOB *) NULL;
2101 /* get a buffered stream pointer */
2102 static S64_IOB *_s64_get_buffer( PerlIO *f) {
2103 S64_IOB *ptr = s64_buffer;
2104 while( ptr && ptr->fp != f)
2109 /* create a buffered stream pointer */
2110 static S64_IOB *_s64_create_buffer( PerlIO *f) {
2111 S64_IOB *ptr = malloc( sizeof( S64_IOB));
2114 ptr->cnt = ptr->size = 0;
2115 ptr->buffer = (int *) NULL;
2116 ptr->next = s64_buffer;
2117 ptr->last = (S64_IOB *) NULL;
2118 if( s64_buffer) s64_buffer->last = ptr;
2124 /* delete a buffered stream pointer */
2125 void Perl_do_s64_delete_buffer( PerlIO *f) {
2126 S64_IOB *ptr = _s64_get_buffer(f);
2128 /* fix the stream pointer according to the bytes buffered */
2129 /* required, if this is called in a seek-context */
2130 if( ptr->cnt) fseek(f,-ptr->cnt,SEEK_CUR);
2131 if( ptr->buffer) free( ptr->buffer);
2133 ptr->last->next = ptr->next;
2135 s64_buffer = ptr->next;
2140 /* internal buffer management */
2141 #define _S64_BUFFER_SIZE 32
2142 static int _s64_malloc( S64_IOB *ptr) {
2145 ptr->buffer = (int *) calloc( _S64_BUFFER_SIZE, sizeof( int));
2146 ptr->size = ptr->cnt = 0;
2148 ptr->buffer = (int *) realloc( ptr->buffer, ptr->size + _S64_BUFFER_SIZE);
2154 ptr->size += _S64_BUFFER_SIZE;
2162 /* SOCKS 64 bit getc replacement */
2163 int Perl_do_s64_getc( PerlIO *f) {
2164 S64_IOB *ptr = _s64_get_buffer(f);
2167 return( ptr->buffer[--ptr->cnt]);
2172 /* SOCKS 64 bit ungetc replacement */
2173 int Perl_do_s64_ungetc( int ch, PerlIO *f) {
2174 S64_IOB *ptr = _s64_get_buffer(f);
2176 if( !ptr) ptr=_s64_create_buffer(f);
2177 if( !ptr) return( EOF);
2178 if( !ptr->buffer || (ptr->buffer && ptr->cnt >= ptr->size))
2179 if( !_s64_malloc( ptr)) return( EOF);
2180 ptr->buffer[ptr->cnt++] = ch;
2185 /* SOCKS 64 bit fread replacement */
2186 SSize_t Perl_do_s64_fread(void *buf, SSize_t count, PerlIO* f) {
2188 char *bufptr = (char *) buf;
2189 S64_IOB *ptr = _s64_get_buffer(f);
2191 while( ptr->cnt && count) {
2192 *bufptr++ = ptr->buffer[--ptr->cnt];
2197 len += (SSize_t)fread(bufptr,1,count,f);
2202 /* SOCKS 64 bit fseek replacement */
2203 int Perl_do_s64_seek(PerlIO* f, Off_t offset, int whence) {
2204 S64_IOB *ptr = _s64_get_buffer(f);
2206 /* Simply clear the buffer and seek if the position is absolute */
2207 if( SEEK_SET == whence || SEEK_END == whence) {
2208 if( ptr) ptr->cnt = 0;
2210 /* In case of relative positioning clear the buffer and calculate */
2211 /* a fixed offset */
2212 } else if( SEEK_CUR == whence) {
2214 offset -= (Off_t)ptr->cnt;
2219 /* leave out buffer untouched otherwise, because fseek will fail */
2221 return( fseeko( f, offset, whence));
2224 /* SOCKS 64 bit ftell replacement */
2225 Off_t Perl_do_s64_tell(PerlIO* f) {
2227 S64_IOB *ptr = _s64_get_buffer(f);
2230 return( ftello(f) - offset);