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 #ifdef SOCKS_64BIT_BUG
55 typedef struct __s64_iobuffer {
56 struct __s64_iobuffer *next, *last; /* Queue pointer */
57 PerlIO *fp; /* assigned file pointer */
58 int cnt; /* Buffer counter */
59 int size; /* Buffer size */
60 int *buffer; /* the buffer */
63 static S64_IOB *_s64_get_buffer( PerlIO *f);
64 static S64_IOB *_s64_create_buffer( PerlIO *f);
65 static int _s64_malloc( S64_IOB *ptr);
69 Perl_do_open(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
70 int rawmode, int rawperm, PerlIO *supplied_fp)
72 return do_open9(gv, name, len, as_raw, rawmode, rawperm,
73 supplied_fp, Nullsv, 0);
77 Perl_do_open9(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
78 int rawmode, int rawperm, PerlIO *supplied_fp, SV *svs,
81 register IO *io = GvIOn(gv);
82 PerlIO *saveifp = Nullfp;
83 PerlIO *saveofp = Nullfp;
84 char savetype = IoTYPE_CLOSED;
89 bool was_fdopen = FALSE;
90 bool in_raw = 0, in_crlf = 0, out_raw = 0, out_crlf = 0;
93 char mode[4]; /* stdio file mode ("r\0", "rb\0", "r+b\0" etc.) */
95 Zero(mode,sizeof(mode),char);
96 PL_forkprocess = 1; /* assume true if no fork */
98 /* Collect default raw/crlf info from the op */
99 if (PL_op && PL_op->op_type == OP_OPEN) {
100 /* set up disciplines */
101 U8 flags = PL_op->op_private;
102 in_raw = (flags & OPpOPEN_IN_RAW);
103 in_crlf = (flags & OPpOPEN_IN_CRLF);
104 out_raw = (flags & OPpOPEN_OUT_RAW);
105 out_crlf = (flags & OPpOPEN_OUT_CRLF);
108 /* If currently open - close before we re-open */
110 fd = PerlIO_fileno(IoIFP(io));
111 if (IoTYPE(io) == IoTYPE_STD)
113 else if (fd <= PL_maxsysfd) {
116 savetype = IoTYPE(io);
119 else if (IoTYPE(io) == IoTYPE_PIPE)
120 result = PerlProc_pclose(IoIFP(io));
121 else if (IoIFP(io) != IoOFP(io)) {
123 result = PerlIO_close(IoOFP(io));
124 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
127 result = PerlIO_close(IoIFP(io));
130 result = PerlIO_close(IoIFP(io));
131 if (result == EOF && fd > PL_maxsysfd)
132 PerlIO_printf(Perl_error_log,
133 "Warning: unable to close filehandle %s properly.\n",
135 IoOFP(io) = IoIFP(io) = Nullfp;
139 /* sysopen style args, i.e. integer mode and permissions */
141 #if defined(USE_64_BIT_RAWIO) && defined(O_LARGEFILE)
142 rawmode |= O_LARGEFILE;
146 #define O_ACCMODE 3 /* Assume traditional implementation */
149 switch (result = rawmode & O_ACCMODE) {
151 IoTYPE(io) = IoTYPE_RDONLY;
154 IoTYPE(io) = IoTYPE_WRONLY;
158 IoTYPE(io) = IoTYPE_RDWR;
162 writing = (result > 0);
163 fd = PerlLIO_open3(name, rawmode, rawperm);
169 if (result == O_RDONLY) {
173 else if (rawmode & O_APPEND) {
175 if (result != O_WRONLY)
180 if (result == O_WRONLY)
187 if (rawmode & O_BINARY)
190 fp = PerlIO_fdopen(fd, mode);
196 /* Regular (non-sys) open */
202 type = savepvn(name, len);
205 /* Loose trailing white space */
206 while (tend > type && isSPACE(tend[-1]))
209 /* New style explict name, type is just mode and discipline/layer info */
211 name = SvPV(svs, l) ;
213 name = savepvn(name, len);
216 for (; isSPACE(*type); type++) ;
223 if (*type == IoTYPE_RDWR && (!num_svs || tend > type+1 && tend[-1] != IoTYPE_PIPE)) { /* scary */
228 if (*type == IoTYPE_PIPE) {
230 if (type[1] != IoTYPE_STD) {
232 Perl_croak(aTHX_ "Unknown open() mode '%.*s'", (int)olen, oname);
237 for (type++; isSPACE(*type); type++) ;
242 if (*name == '\0') { /* command is missing 19990114 */
244 if (ckWARN(WARN_PIPE))
245 Perl_warner(aTHX_ WARN_PIPE, "Missing command in piped open");
249 if (strNE(name,"-") || num_svs)
251 TAINT_PROPER("piped open");
252 if (!num_svs && name[len-1] == '|') {
255 if (ckWARN(WARN_PIPE))
256 Perl_warner(aTHX_ WARN_PIPE, "Can't open bidirectional pipe");
264 fp = PerlProc_popen(name,mode);
266 else if (*type == IoTYPE_WRONLY) {
267 TAINT_PROPER("open");
269 if (*type == IoTYPE_WRONLY) {
270 /* Two IoTYPE_WRONLYs in a row make for an IoTYPE_APPEND. */
271 mode[0] = IoTYPE(io) = IoTYPE_APPEND;
294 if (!*name && supplied_fp)
298 for (; isSPACE(*name); name++) ;
303 gv = gv_fetchpv(name,FALSE,SVt_PVIO);
307 SETERRNO(EINVAL,SS$_IVCHAN);
312 PerlIO *fp = IoIFP(thatio);
313 /* Flush stdio buffer before dup. --mjd
314 * Unfortunately SEEK_CURing 0 seems to
315 * be optimized away on most platforms;
316 * only Solaris and Linux seem to flush
319 /* sfio fails to clear error on next
320 sfwrite, contrary to documentation.
322 if (PerlIO_seek(fp, 0, SEEK_CUR) == -1)
325 /* On the other hand, do all platforms
326 * take gracefully to flushing a read-only
327 * filehandle? Perhaps we should do
328 * fsetpos(src)+fgetpos(dst)? --nik */
330 fd = PerlIO_fileno(fp);
331 /* When dup()ing STDIN, STDOUT or STDERR
332 * explicitly set appropriate access mode */
333 if (IoIFP(thatio) == PerlIO_stdout()
334 || IoIFP(thatio) == PerlIO_stderr())
335 IoTYPE(io) = IoTYPE_WRONLY;
336 else if (IoIFP(thatio) == PerlIO_stdin())
337 IoTYPE(io) = IoTYPE_RDONLY;
338 /* When dup()ing a socket, say result is
340 else if (IoTYPE(thatio) == IoTYPE_SOCKET)
341 IoTYPE(io) = IoTYPE_SOCKET;
347 fd = PerlLIO_dup(fd);
350 if (!(fp = PerlIO_fdopen(fd,mode))) {
358 for (; isSPACE(*type); type++) ;
359 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
362 fp = PerlIO_stdout();
363 IoTYPE(io) = IoTYPE_STD;
366 fp = PerlIO_open((num_svs ? name : type), mode);
370 else if (*type == IoTYPE_RDONLY) {
372 for (type++; isSPACE(*type); type++) ;
383 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
387 IoTYPE(io) = IoTYPE_STD;
390 fp = PerlIO_open((num_svs ? name : type), mode);
392 else if ((num_svs && type[0] == IoTYPE_STD && type[1] == IoTYPE_PIPE) ||
393 (!num_svs && tend > type+1 && tend[-1] == IoTYPE_PIPE)) {
395 type += 2; /* skip over '-|' */
399 while (tend > type && isSPACE(tend[-1]))
402 for (; isSPACE(*type); type++) ;
406 if (*name == '\0') { /* command is missing 19990114 */
408 if (ckWARN(WARN_PIPE))
409 Perl_warner(aTHX_ WARN_PIPE, "Missing command in piped open");
413 if (strNE(name,"-") || num_svs)
415 TAINT_PROPER("piped open");
421 fp = PerlProc_popen(name,mode);
422 IoTYPE(io) = IoTYPE_PIPE;
428 IoTYPE(io) = IoTYPE_RDONLY;
430 for (; isSPACE(*name); name++) ;
436 if (strEQ(name,"-")) {
438 IoTYPE(io) = IoTYPE_STD;
441 fp = PerlIO_open(name,mode);
447 if (ckWARN(WARN_NEWLINE) && IoTYPE(io) == IoTYPE_RDONLY && strchr(name, '\n'))
448 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "open");
451 if (IoTYPE(io) && IoTYPE(io) != IoTYPE_PIPE && IoTYPE(io) != IoTYPE_STD) {
453 if (PerlLIO_fstat(PerlIO_fileno(fp),&PL_statbuf) < 0) {
454 (void)PerlIO_close(fp);
457 if (S_ISSOCK(PL_statbuf.st_mode))
458 IoTYPE(io) = IoTYPE_SOCKET; /* in case a socket was passed in to us */
462 !(PL_statbuf.st_mode & S_IFMT)
466 && IoTYPE(io) != IoTYPE_WRONLY /* Dups of STD* filehandles already have */
467 && IoTYPE(io) != IoTYPE_RDONLY /* type so they aren't marked as sockets */
468 ) { /* on OS's that return 0 on fstat()ed pipe */
470 Sock_size_t buflen = sizeof tmpbuf;
471 if (PerlSock_getsockname(PerlIO_fileno(fp), (struct sockaddr *)tmpbuf,
473 || errno != ENOTSOCK)
474 IoTYPE(io) = IoTYPE_SOCKET; /* some OS's return 0 on fstat()ed socket */
475 /* but some return 0 for streams too, sigh */
479 if (saveifp) { /* must use old fp? */
480 fd = PerlIO_fileno(saveifp);
482 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
483 if (saveofp != saveifp) { /* was a socket? */
484 PerlIO_close(saveofp);
489 if (fd != PerlIO_fileno(fp)) {
493 PerlLIO_dup2(PerlIO_fileno(fp), fd);
495 sv = *av_fetch(PL_fdpid,PerlIO_fileno(fp),TRUE);
496 (void)SvUPGRADE(sv, SVt_IV);
499 sv = *av_fetch(PL_fdpid,fd,TRUE);
501 (void)SvUPGRADE(sv, SVt_IV);
510 #if defined(HAS_FCNTL) && defined(F_SETFD)
512 int save_errno = errno;
513 fd = PerlIO_fileno(fp);
514 fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
520 /* Need to supply default type info from open.pm */
521 SV *layers = PL_curcop->cop_io;
525 type = SvPV(layers,len);
526 if (type && mode[0] != 'r') {
527 /* Skip to write part */
528 char *s = strchr(type,0);
529 if (s && (s-type) < len) {
534 else if (O_BINARY != O_TEXT) {
539 while (isSPACE(*type)) type++;
541 if (PerlIO_apply_layers(aTHX_ IoIFP(io),mode,type) != 0) {
547 IoFLAGS(io) &= ~IOf_NOLINE;
550 if (IoTYPE(io) == IoTYPE_SOCKET
551 || (IoTYPE(io) == IoTYPE_WRONLY && S_ISCHR(PL_statbuf.st_mode)) )
554 if (!(IoOFP(io) = PerlIO_fdopen(PerlIO_fileno(fp),mode))) {
560 if (PerlIO_apply_layers(aTHX_ IoOFP(io),mode,type) != 0) {
561 PerlIO_close(IoOFP(io));
577 IoTYPE(io) = savetype;
582 Perl_nextargv(pTHX_ register GV *gv)
585 #ifndef FLEXFILENAMES
594 PL_argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO);
595 if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
596 IoFLAGS(io) &= ~IOf_START;
598 if (!PL_argvout_stack)
599 PL_argvout_stack = newAV();
600 av_push(PL_argvout_stack, SvREFCNT_inc(PL_defoutgv));
603 if (PL_filemode & (S_ISUID|S_ISGID)) {
604 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
606 (void)fchmod(PL_lastfd,PL_filemode);
608 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
612 while (av_len(GvAV(gv)) >= 0) {
615 sv = av_shift(GvAV(gv));
617 sv_setsv(GvSV(gv),sv);
618 SvSETMAGIC(GvSV(gv));
619 PL_oldname = SvPVx(GvSV(gv), oldlen);
620 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,Nullfp)) {
622 TAINT_PROPER("inplace open");
623 if (oldlen == 1 && *PL_oldname == '-') {
624 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
625 return IoIFP(GvIOp(gv));
627 #ifndef FLEXFILENAMES
628 filedev = PL_statbuf.st_dev;
629 fileino = PL_statbuf.st_ino;
631 PL_filemode = PL_statbuf.st_mode;
632 fileuid = PL_statbuf.st_uid;
633 filegid = PL_statbuf.st_gid;
634 if (!S_ISREG(PL_filemode)) {
635 if (ckWARN_d(WARN_INPLACE))
636 Perl_warner(aTHX_ WARN_INPLACE,
637 "Can't do inplace edit: %s is not a regular file",
643 char *star = strchr(PL_inplace, '*');
645 char *begin = PL_inplace;
646 sv_setpvn(sv, "", 0);
648 sv_catpvn(sv, begin, star - begin);
649 sv_catpvn(sv, PL_oldname, oldlen);
651 } while ((star = strchr(begin, '*')));
656 sv_catpv(sv,PL_inplace);
658 #ifndef FLEXFILENAMES
659 if (PerlLIO_stat(SvPVX(sv),&PL_statbuf) >= 0
660 && PL_statbuf.st_dev == filedev
661 && PL_statbuf.st_ino == fileino
663 || (_djstat_fail_bits & _STFAIL_TRUENAME)!=0
667 if (ckWARN_d(WARN_INPLACE))
668 Perl_warner(aTHX_ WARN_INPLACE,
669 "Can't do inplace edit: %s would not be unique",
676 #if !defined(DOSISH) && !defined(__CYGWIN__)
677 if (PerlLIO_rename(PL_oldname,SvPVX(sv)) < 0) {
678 if (ckWARN_d(WARN_INPLACE))
679 Perl_warner(aTHX_ WARN_INPLACE,
680 "Can't rename %s to %s: %s, skipping file",
681 PL_oldname, SvPVX(sv), Strerror(errno) );
687 (void)PerlLIO_unlink(SvPVX(sv));
688 (void)PerlLIO_rename(PL_oldname,SvPVX(sv));
689 do_open(gv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,O_RDONLY,0,Nullfp);
692 (void)UNLINK(SvPVX(sv));
693 if (link(PL_oldname,SvPVX(sv)) < 0) {
694 if (ckWARN_d(WARN_INPLACE))
695 Perl_warner(aTHX_ WARN_INPLACE,
696 "Can't rename %s to %s: %s, skipping file",
697 PL_oldname, SvPVX(sv), Strerror(errno) );
701 (void)UNLINK(PL_oldname);
705 #if !defined(DOSISH) && !defined(AMIGAOS)
706 # ifndef VMS /* Don't delete; use automatic file versioning */
707 if (UNLINK(PL_oldname) < 0) {
708 if (ckWARN_d(WARN_INPLACE))
709 Perl_warner(aTHX_ WARN_INPLACE,
710 "Can't remove %s: %s, skipping file",
711 PL_oldname, Strerror(errno) );
717 Perl_croak(aTHX_ "Can't do inplace edit without backup");
721 sv_setpvn(sv,">",!PL_inplace);
722 sv_catpvn(sv,PL_oldname,oldlen);
723 SETERRNO(0,0); /* in case sprintf set errno */
725 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
726 O_WRONLY|O_CREAT|O_TRUNC,0,Nullfp))
728 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
729 O_WRONLY|O_CREAT|OPEN_EXCL,0666,Nullfp))
732 if (ckWARN_d(WARN_INPLACE))
733 Perl_warner(aTHX_ WARN_INPLACE, "Can't do inplace edit on %s: %s",
734 PL_oldname, Strerror(errno) );
738 setdefout(PL_argvoutgv);
739 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
740 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
742 (void)fchmod(PL_lastfd,PL_filemode);
744 # if !(defined(WIN32) && defined(__BORLANDC__))
745 /* Borland runtime creates a readonly file! */
746 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
749 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
751 (void)fchown(PL_lastfd,fileuid,filegid);
754 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
759 return IoIFP(GvIOp(gv));
763 if (ckWARN_d(WARN_INPLACE)) {
765 if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0
766 && !S_ISREG(PL_statbuf.st_mode))
768 Perl_warner(aTHX_ WARN_INPLACE,
769 "Can't do inplace edit: %s is not a regular file",
773 Perl_warner(aTHX_ WARN_INPLACE, "Can't open %s: %s",
774 PL_oldname, Strerror(eno));
778 if (io && (IoFLAGS(io) & IOf_ARGV))
779 IoFLAGS(io) |= IOf_START;
781 (void)do_close(PL_argvoutgv,FALSE);
782 if (io && (IoFLAGS(io) & IOf_ARGV)
783 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
785 GV *oldout = (GV*)av_pop(PL_argvout_stack);
787 SvREFCNT_dec(oldout);
790 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
797 Perl_do_pipe(pTHX_ SV *sv, GV *rgv, GV *wgv)
816 if (PerlProc_pipe(fd) < 0)
818 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r");
819 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w");
820 IoIFP(wstio) = IoOFP(wstio);
821 IoTYPE(rstio) = IoTYPE_RDONLY;
822 IoTYPE(wstio) = IoTYPE_WRONLY;
823 if (!IoIFP(rstio) || !IoOFP(wstio)) {
824 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
825 else PerlLIO_close(fd[0]);
826 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
827 else PerlLIO_close(fd[1]);
831 sv_setsv(sv,&PL_sv_yes);
835 sv_setsv(sv,&PL_sv_undef);
840 /* explicit renamed to avoid C++ conflict -- kja */
842 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
849 if (!gv || SvTYPE(gv) != SVt_PVGV) {
851 SETERRNO(EBADF,SS$_IVCHAN);
855 if (!io) { /* never opened */
858 if (ckWARN(WARN_UNOPENED)) /* no check for closed here */
859 report_evil_fh(gv, io, PL_op->op_type);
860 SETERRNO(EBADF,SS$_IVCHAN);
864 retval = io_close(io, not_implicit);
868 IoLINES_LEFT(io) = IoPAGE_LEN(io);
870 IoTYPE(io) = IoTYPE_CLOSED;
875 Perl_io_close(pTHX_ IO *io, bool not_implicit)
881 if (IoTYPE(io) == IoTYPE_PIPE) {
882 status = PerlProc_pclose(IoIFP(io));
884 STATUS_NATIVE_SET(status);
885 retval = (STATUS_POSIX == 0);
888 retval = (status != -1);
891 else if (IoTYPE(io) == IoTYPE_STD)
894 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
895 retval = (PerlIO_close(IoOFP(io)) != EOF);
896 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
899 retval = (PerlIO_close(IoIFP(io)) != EOF);
901 IoOFP(io) = IoIFP(io) = Nullfp;
903 else if (not_implicit) {
904 SETERRNO(EBADF,SS$_IVCHAN);
911 Perl_do_eof(pTHX_ GV *gv)
921 else if (ckWARN(WARN_IO)
922 && (IoTYPE(io) == IoTYPE_WRONLY || IoIFP(io) == PerlIO_stdout()
923 || IoIFP(io) == PerlIO_stderr()))
925 /* integrate to report_evil_fh()? */
928 SV* sv = sv_newmortal();
929 gv_efullname4(sv, gv, Nullch, FALSE);
930 name = SvPV_nolen(sv);
933 Perl_warner(aTHX_ WARN_IO,
934 "Filehandle %s opened only for output", name);
936 Perl_warner(aTHX_ WARN_IO,
937 "Filehandle opened only for output");
942 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
943 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
944 return FALSE; /* this is the most usual case */
947 ch = PerlIO_getc(IoIFP(io));
949 (void)PerlIO_ungetc(IoIFP(io),ch);
953 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
954 if (PerlIO_get_cnt(IoIFP(io)) < -1)
955 PerlIO_set_cnt(IoIFP(io),-1);
957 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
958 if (!nextargv(PL_argvgv)) /* get another fp handy */
962 return TRUE; /* normal fp, definitely end of file */
968 Perl_do_tell(pTHX_ GV *gv)
973 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
974 #ifdef ULTRIX_STDIO_BOTCH
976 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
978 return PerlIO_tell(fp);
982 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
983 report_evil_fh(gv, io, PL_op->op_type);
985 SETERRNO(EBADF,RMS$_IFI);
990 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
995 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
996 #ifdef ULTRIX_STDIO_BOTCH
998 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
1000 return PerlIO_seek(fp, pos, whence) >= 0;
1004 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1005 report_evil_fh(gv, io, PL_op->op_type);
1007 SETERRNO(EBADF,RMS$_IFI);
1012 Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
1015 register PerlIO *fp;
1017 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
1018 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
1021 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1022 report_evil_fh(gv, io, PL_op->op_type);
1024 SETERRNO(EBADF,RMS$_IFI);
1029 Perl_mode_from_discipline(pTHX_ SV *discp)
1031 int mode = O_BINARY;
1034 char *s = SvPV(discp,len);
1039 if (len > 3 && strnEQ(s+1, "raw", 3)
1040 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1049 if (len > 4 && strnEQ(s+1, "crlf", 4)
1050 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1059 goto fail_discipline;
1062 else if (isSPACE(*s)) {
1069 end = strchr(s+1, ':');
1072 Perl_croak(aTHX_ "Unknown discipline '%.*s'", end-s, s);
1080 Perl_do_binmode(pTHX_ PerlIO *fp, int iotype, int mode)
1083 # if defined(atarist) || defined(__MINT__)
1084 if (!PerlIO_flush(fp)) {
1085 if (mode & O_BINARY)
1086 ((FILE*)fp)->_flag |= _IOBIN;
1088 ((FILE*)fp)->_flag &= ~ _IOBIN;
1093 if (PerlLIO_setmode(PerlIO_fileno(fp), mode) != -1) {
1094 # if defined(WIN32) && defined(__BORLANDC__)
1095 /* The translation mode of the stream is maintained independent
1096 * of the translation mode of the fd in the Borland RTL (heavy
1097 * digging through their runtime sources reveal). User has to
1098 * set the mode explicitly for the stream (though they don't
1099 * document this anywhere). GSAR 97-5-24
1101 PerlIO_seek(fp,0L,0);
1102 if (mode & O_BINARY)
1103 ((FILE*)fp)->flags |= _F_BIN;
1105 ((FILE*)fp)->flags &= ~ _F_BIN;
1113 # if defined(USEMYBINMODE)
1114 if (my_binmode(fp, iotype, mode) != FALSE)
1124 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP)
1125 /* code courtesy of William Kucharski */
1128 I32 my_chsize(fd, length)
1129 I32 fd; /* file descriptor */
1130 Off_t length; /* length to set file to */
1133 struct stat filebuf;
1135 if (PerlLIO_fstat(fd, &filebuf) < 0)
1138 if (filebuf.st_size < length) {
1140 /* extend file length */
1142 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
1145 /* write a "0" byte */
1147 if ((PerlLIO_write(fd, "", 1)) != 1)
1151 /* truncate length */
1155 fl.l_start = length;
1156 fl.l_type = F_WRLCK; /* write lock on file space */
1159 * This relies on the UNDOCUMENTED F_FREESP argument to
1160 * fcntl(2), which truncates the file so that it ends at the
1161 * position indicated by fl.l_start.
1163 * Will minor miracles never cease?
1166 if (fcntl(fd, F_FREESP, &fl) < 0)
1173 #endif /* F_FREESP */
1176 Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
1178 register char *tmps;
1181 /* assuming fp is checked earlier */
1187 if (SvIOK(sv) && SvIVX(sv) != 0) {
1188 PerlIO_printf(fp, PL_ofmt, (NV)SvIVX(sv));
1189 return !PerlIO_error(fp);
1191 if ( (SvNOK(sv) && SvNVX(sv) != 0.0)
1192 || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) {
1193 PerlIO_printf(fp, PL_ofmt, SvNVX(sv));
1194 return !PerlIO_error(fp);
1197 switch (SvTYPE(sv)) {
1201 if (ckWARN(WARN_UNINITIALIZED))
1210 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
1212 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
1213 return !PerlIO_error(fp);
1218 /* XXX Fix this when the I/O disciplines arrive. XXX */
1220 sv_utf8_downgrade(sv, FALSE);
1222 tmps = SvPV(sv, len);
1225 /* To detect whether the process is about to overstep its
1226 * filesize limit we would need getrlimit(). We could then
1227 * also transparently raise the limit with setrlimit() --
1228 * but only until the system hard limit/the filesystem limit,
1229 * at which we would get EPERM. Note that when using buffered
1230 * io the write failure can be delayed until the flush/close. --jhi */
1231 if (len && (PerlIO_write(fp,tmps,len) == 0))
1233 return !PerlIO_error(fp);
1243 if (PL_op->op_flags & OPf_REF) {
1248 if (io && IoIFP(io)) {
1250 sv_setpv(PL_statname,"");
1251 PL_laststype = OP_STAT;
1252 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
1256 return PL_laststatval;
1257 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1258 report_evil_fh(gv, io, PL_op->op_type);
1260 sv_setpv(PL_statname,"");
1261 return (PL_laststatval = -1);
1269 if (SvTYPE(sv) == SVt_PVGV) {
1273 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
1280 sv_setpv(PL_statname, s);
1281 PL_laststype = OP_STAT;
1282 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1283 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
1284 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "stat");
1285 return PL_laststatval;
1295 if (PL_op->op_flags & OPf_REF) {
1297 if (cGVOP_gv == PL_defgv) {
1298 if (PL_laststype != OP_LSTAT)
1299 Perl_croak(aTHX_ "The stat preceding -l _ wasn't an lstat");
1300 return PL_laststatval;
1302 Perl_croak(aTHX_ "You can't use -l on a filehandle");
1305 PL_laststype = OP_LSTAT;
1309 sv_setpv(PL_statname,SvPV(sv, n_a));
1310 PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache);
1311 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
1312 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "lstat");
1313 return PL_laststatval;
1317 Perl_do_aexec(pTHX_ SV *really, register SV **mark, register SV **sp)
1319 return do_aexec5(really, mark, sp, 0, 0);
1323 Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1324 int fd, int do_report)
1326 #ifdef MACOS_TRADITIONAL
1327 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1335 New(401,PL_Argv, sp - mark + 1, char*);
1337 while (++mark <= sp) {
1339 *a++ = SvPVx(*mark, n_a);
1344 if (*PL_Argv[0] != '/') /* will execvp use PATH? */
1345 TAINT_ENV(); /* testing IFS here is overkill, probably */
1346 if (really && *(tmps = SvPV(really, n_a)))
1347 PerlProc_execvp(tmps,PL_Argv);
1349 PerlProc_execvp(PL_Argv[0],PL_Argv);
1350 if (ckWARN(WARN_EXEC))
1351 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1352 PL_Argv[0], Strerror(errno));
1356 PerlLIO_write(fd, (void*)&e, sizeof(int));
1366 Perl_do_execfree(pTHX)
1370 PL_Argv = Null(char **);
1378 #if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC) && !defined(MACOS_TRADITIONAL)
1381 Perl_do_exec(pTHX_ char *cmd)
1383 return do_exec3(cmd,0,0);
1387 Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report)
1393 while (*cmd && isSPACE(*cmd))
1396 /* save an extra exec if possible */
1399 if (strnEQ(cmd,PL_cshname,PL_cshlen) && strnEQ(cmd+PL_cshlen," -c",3)) {
1401 s = cmd+PL_cshlen+3;
1415 if (s[-1] == '\'') {
1417 PerlProc_execl(PL_cshname,"csh", flags,ncmd,(char*)0);
1425 /* see if there are shell metacharacters in it */
1427 if (*cmd == '.' && isSPACE(cmd[1]))
1430 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1433 for (s = cmd; *s && isALNUM(*s); s++) ; /* catch VAR=val gizmo */
1437 for (s = cmd; *s; s++) {
1438 if (*s != ' ' && !isALPHA(*s) && strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1439 if (*s == '\n' && !s[1]) {
1443 /* handle the 2>&1 construct at the end */
1444 if (*s == '>' && s[1] == '&' && s[2] == '1'
1445 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1446 && (!s[3] || isSPACE(s[3])))
1450 while (*t && isSPACE(*t))
1452 if (!*t && (dup2(1,2) != -1)) {
1458 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
1463 New(402,PL_Argv, (s - cmd) / 2 + 2, char*);
1464 PL_Cmd = savepvn(cmd, s-cmd);
1466 for (s = PL_Cmd; *s;) {
1467 while (*s && isSPACE(*s)) s++;
1470 while (*s && !isSPACE(*s)) s++;
1476 PerlProc_execvp(PL_Argv[0],PL_Argv);
1477 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1485 if (ckWARN(WARN_EXEC))
1486 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1487 PL_Argv[0], Strerror(errno));
1489 PerlLIO_write(fd, (void*)&e, sizeof(int));
1498 #endif /* OS2 || WIN32 */
1501 Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
1506 register I32 tot = 0;
1509 SV **oldmark = mark;
1512 #define APPLY_TAINT_PROPER() \
1514 if (PL_tainted) { TAINT_PROPER(what); } \
1517 /* This is a first heuristic; it doesn't catch tainting magic. */
1519 while (++mark <= sp) {
1520 if (SvTAINTED(*mark)) {
1530 APPLY_TAINT_PROPER();
1533 APPLY_TAINT_PROPER();
1535 while (++mark <= sp) {
1536 char *name = SvPVx(*mark, n_a);
1537 APPLY_TAINT_PROPER();
1538 if (PerlLIO_chmod(name, val))
1546 APPLY_TAINT_PROPER();
1547 if (sp - mark > 2) {
1548 val = SvIVx(*++mark);
1549 val2 = SvIVx(*++mark);
1550 APPLY_TAINT_PROPER();
1552 while (++mark <= sp) {
1553 char *name = SvPVx(*mark, n_a);
1554 APPLY_TAINT_PROPER();
1555 if (PerlLIO_chown(name, val, val2))
1562 XXX Should we make lchown() directly available from perl?
1563 For now, we'll let Configure test for HAS_LCHOWN, but do
1564 nothing in the core.
1570 APPLY_TAINT_PROPER();
1573 s = SvPVx(*++mark, n_a);
1575 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1577 if (!(val = whichsig(s)))
1578 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
1582 APPLY_TAINT_PROPER();
1585 /* kill() doesn't do process groups (job trees?) under VMS */
1586 if (val < 0) val = -val;
1587 if (val == SIGKILL) {
1588 # include <starlet.h>
1589 /* Use native sys$delprc() to insure that target process is
1590 * deleted; supervisor-mode images don't pay attention to
1591 * CRTL's emulation of Unix-style signals and kill()
1593 while (++mark <= sp) {
1594 I32 proc = SvIVx(*mark);
1595 register unsigned long int __vmssts;
1596 APPLY_TAINT_PROPER();
1597 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1601 case SS$_NOSUCHNODE:
1602 SETERRNO(ESRCH,__vmssts);
1605 SETERRNO(EPERM,__vmssts);
1608 SETERRNO(EVMSERR,__vmssts);
1617 while (++mark <= sp) {
1618 I32 proc = SvIVx(*mark);
1619 APPLY_TAINT_PROPER();
1621 if (PerlProc_killpg(proc,val)) /* BSD */
1623 if (PerlProc_kill(-proc,val)) /* SYSV */
1629 while (++mark <= sp) {
1630 I32 proc = SvIVx(*mark);
1631 APPLY_TAINT_PROPER();
1632 if (PerlProc_kill(proc, val))
1640 APPLY_TAINT_PROPER();
1642 while (++mark <= sp) {
1643 s = SvPVx(*mark, n_a);
1644 APPLY_TAINT_PROPER();
1645 if (PL_euid || PL_unsafe) {
1649 else { /* don't let root wipe out directories without -U */
1650 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1662 APPLY_TAINT_PROPER();
1663 if (sp - mark > 2) {
1664 #if defined(I_UTIME) || defined(VMS)
1665 struct utimbuf utbuf;
1673 Zero(&utbuf, sizeof utbuf, char);
1675 utbuf.actime = (Time_t)SvNVx(*++mark); /* time accessed */
1676 utbuf.modtime = (Time_t)SvNVx(*++mark); /* time modified */
1678 utbuf.actime = (Time_t)SvIVx(*++mark); /* time accessed */
1679 utbuf.modtime = (Time_t)SvIVx(*++mark); /* time modified */
1681 APPLY_TAINT_PROPER();
1683 while (++mark <= sp) {
1684 char *name = SvPVx(*mark, n_a);
1685 APPLY_TAINT_PROPER();
1686 if (PerlLIO_utime(name, &utbuf))
1697 #undef APPLY_TAINT_PROPER
1700 /* Do the permissions allow some operation? Assumes statcache already set. */
1701 #ifndef VMS /* VMS' cando is in vms.c */
1703 Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register Stat_t *statbufp)
1704 /* Note: we use `effective' both for uids and gids.
1705 * Here we are betting on Uid_t being equal or wider than Gid_t. */
1708 /* [Comments and code from Len Reed]
1709 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1710 * to write-protected files. The execute permission bit is set
1711 * by the Miscrosoft C library stat() function for the following:
1716 * All files and directories are readable.
1717 * Directories and special files, e.g. "CON", cannot be
1719 * [Comment by Tom Dinger -- a directory can have the write-protect
1720 * bit set in the file system, but DOS permits changes to
1721 * the directory anyway. In addition, all bets are off
1722 * here for networked software, such as Novell and
1726 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1727 * too so it will actually look into the files for magic numbers
1729 return (mode & statbufp->st_mode) ? TRUE : FALSE;
1731 #else /* ! DOSISH */
1732 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
1733 if (mode == S_IXUSR) {
1734 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1738 return TRUE; /* root reads and writes anything */
1741 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1742 if (statbufp->st_mode & mode)
1743 return TRUE; /* ok as "user" */
1745 else if (ingroup(statbufp->st_gid,effective)) {
1746 if (statbufp->st_mode & mode >> 3)
1747 return TRUE; /* ok as "group" */
1749 else if (statbufp->st_mode & mode >> 6)
1750 return TRUE; /* ok as "other" */
1752 #endif /* ! DOSISH */
1757 Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective)
1759 #ifdef MACOS_TRADITIONAL
1760 /* This is simply not correct for AppleShare, but fix it yerself. */
1763 if (testgid == (effective ? PL_egid : PL_gid))
1765 #ifdef HAS_GETGROUPS
1770 Groups_t gary[NGROUPS];
1773 anum = getgroups(NGROUPS,gary);
1775 if (gary[anum] == testgid)
1783 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1786 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
1792 key = (key_t)SvNVx(*++mark);
1793 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1794 flags = SvIVx(*++mark);
1800 return msgget(key, flags);
1804 return semget(key, n, flags);
1808 return shmget(key, n, flags);
1810 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1812 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1815 return -1; /* should never happen */
1819 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
1824 I32 id, n, cmd, infosize, getinfo;
1827 id = SvIVx(*++mark);
1828 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1829 cmd = SvIVx(*++mark);
1832 getinfo = (cmd == IPC_STAT);
1838 if (cmd == IPC_STAT || cmd == IPC_SET)
1839 infosize = sizeof(struct msqid_ds);
1844 if (cmd == IPC_STAT || cmd == IPC_SET)
1845 infosize = sizeof(struct shmid_ds);
1851 if (cmd == IPC_STAT || cmd == IPC_SET)
1852 infosize = sizeof(struct semid_ds);
1853 else if (cmd == GETALL || cmd == SETALL)
1855 struct semid_ds semds;
1857 #ifdef EXTRA_F_IN_SEMUN_BUF
1858 semun.buff = &semds;
1862 getinfo = (cmd == GETALL);
1863 if (Semctl(id, 0, IPC_STAT, semun) == -1)
1865 infosize = semds.sem_nsems * sizeof(short);
1866 /* "short" is technically wrong but much more portable
1867 than guessing about u_?short(_t)? */
1870 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1874 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1876 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1885 SvPV_force(astr, len);
1886 a = SvGROW(astr, infosize+1);
1890 a = SvPV(astr, len);
1891 if (len != infosize)
1892 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
1901 a = INT2PTR(char *,i); /* ouch */
1908 ret = msgctl(id, cmd, (struct msqid_ds *)a);
1914 union semun unsemds;
1916 #ifdef EXTRA_F_IN_SEMUN_BUF
1917 unsemds.buff = (struct semid_ds *)a;
1919 unsemds.buf = (struct semid_ds *)a;
1921 ret = Semctl(id, n, cmd, unsemds);
1923 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1930 ret = shmctl(id, cmd, (struct shmid_ds *)a);
1934 if (getinfo && ret >= 0) {
1935 SvCUR_set(astr, infosize);
1936 *SvEND(astr) = '\0';
1943 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
1949 I32 id, msize, flags;
1952 id = SvIVx(*++mark);
1954 flags = SvIVx(*++mark);
1955 mbuf = SvPV(mstr, len);
1956 if ((msize = len - sizeof(long)) < 0)
1957 Perl_croak(aTHX_ "Arg too short for msgsnd");
1959 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
1961 Perl_croak(aTHX_ "msgsnd not implemented");
1966 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
1973 I32 id, msize, flags, ret;
1976 id = SvIVx(*++mark);
1978 /* suppress warning when reading into undef var --jhi */
1980 sv_setpvn(mstr, "", 0);
1981 msize = SvIVx(*++mark);
1982 mtype = (long)SvIVx(*++mark);
1983 flags = SvIVx(*++mark);
1984 SvPV_force(mstr, len);
1985 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
1988 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
1990 SvCUR_set(mstr, sizeof(long)+ret);
1991 *SvEND(mstr) = '\0';
1992 #ifndef INCOMPLETE_TAINTS
1993 /* who knows who has been playing with this message? */
1999 Perl_croak(aTHX_ "msgrcv not implemented");
2004 Perl_do_semop(pTHX_ SV **mark, SV **sp)
2013 id = SvIVx(*++mark);
2015 opbuf = SvPV(opstr, opsize);
2016 if (opsize < sizeof(struct sembuf)
2017 || (opsize % sizeof(struct sembuf)) != 0) {
2018 SETERRNO(EINVAL,LIB$_INVARG);
2022 return semop(id, (struct sembuf *)opbuf, opsize/sizeof(struct sembuf));
2024 Perl_croak(aTHX_ "semop not implemented");
2029 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
2035 I32 id, mpos, msize;
2037 struct shmid_ds shmds;
2039 id = SvIVx(*++mark);
2041 mpos = SvIVx(*++mark);
2042 msize = SvIVx(*++mark);
2044 if (shmctl(id, IPC_STAT, &shmds) == -1)
2046 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
2047 SETERRNO(EFAULT,SS$_ACCVIO); /* can't do as caller requested */
2050 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
2051 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2053 if (optype == OP_SHMREAD) {
2054 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2056 sv_setpvn(mstr, "", 0);
2057 SvPV_force(mstr, len);
2058 mbuf = SvGROW(mstr, msize+1);
2060 Copy(shm + mpos, mbuf, msize, char);
2061 SvCUR_set(mstr, msize);
2062 *SvEND(mstr) = '\0';
2064 #ifndef INCOMPLETE_TAINTS
2065 /* who knows who has been playing with this shared memory? */
2072 mbuf = SvPV(mstr, len);
2073 if ((n = len) > msize)
2075 Copy(mbuf, shm + mpos, n, char);
2077 memzero(shm + mpos + n, msize - n);
2081 Perl_croak(aTHX_ "shm I/O not implemented");
2085 #endif /* SYSV IPC */
2087 #ifdef SOCKS_64BIT_BUG
2090 ** getc and ungetc wrappers for the 64 bit problems with SOCKS 5 support
2091 ** Workaround to the problem, that SOCKS maps a socket 'getc' to revc
2092 ** without checking the ungetc buffer.
2095 static S64_IOB *s64_buffer = (S64_IOB *) NULL;
2097 /* get a buffered stream pointer */
2098 static S64_IOB *S_s64_get_buffer( PerlIO *fp) {
2099 S64_IOB *ptr = s64_buffer;
2100 while( ptr && ptr->fp != fp)
2105 /* create a buffered stream pointer */
2106 static S64_IOB *_s64_create_buffer( PerlIO *f) {
2107 S64_IOB *ptr = malloc( sizeof( S64_IOB));
2110 ptr->cnt = ptr->size = 0;
2111 ptr->buffer = (int *) NULL;
2112 ptr->next = s64_buffer;
2113 ptr->last = (S64_IOB *) NULL;
2114 if( s64_buffer) s64_buffer->last = ptr;
2120 /* delete a buffered stream pointer */
2121 void Perl_do_s64_delete_buffer( PerlIO *f) {
2122 S64_IOB *ptr = _s64_get_buffer(f);
2124 /* fix the stream pointer according to the bytes buffered */
2125 /* required, if this is called in a seek-context */
2126 if( ptr->cnt) fseek(f,-ptr->cnt,SEEK_CUR);
2127 if( ptr->buffer) free( ptr->buffer);
2129 ptr->last->next = ptr->next;
2131 s64_buffer = ptr->next;
2136 /* internal buffer management */
2137 #define _S64_BUFFER_SIZE 32
2138 static int _s64_malloc( S64_IOB *ptr) {
2141 ptr->buffer = (int *) calloc( _S64_BUFFER_SIZE, sizeof( int));
2142 ptr->size = ptr->cnt = 0;
2144 ptr->buffer = (int *) realloc( ptr->buffer, ptr->size + _S64_BUFFER_SIZE);
2150 ptr->size += _S64_BUFFER_SIZE;
2158 /* SOCKS 64 bit getc replacement */
2159 int Perl_do_s64_getc( PerlIO *f) {
2160 S64_IOB *ptr = _s64_get_buffer(f);
2163 return( ptr->buffer[--ptr->cnt]);
2168 /* SOCKS 64 bit ungetc replacement */
2169 int Perl_do_s64_ungetc( int ch, PerlIO *f) {
2170 S64_IOB *ptr = _s64_get_buffer(f);
2172 if( !ptr) ptr=_s64_create_buffer(f);
2173 if( !ptr) return( EOF);
2174 if( !ptr->buffer || (ptr->buffer && ptr->cnt >= ptr->size))
2175 if( !_s64_malloc( ptr)) return( EOF);
2176 ptr->buffer[ptr->cnt++] = ch;
2181 /* SOCKS 64 bit fread replacement */
2182 SSize_t Perl_do_s64_fread(void *buf, SSize_t count, PerlIO* f) {
2184 char *bufptr = (char *) buf;
2185 S64_IOB *ptr = _s64_get_buffer(f);
2187 while( ptr->cnt && count) {
2188 *bufptr++ = ptr->buffer[--ptr->cnt];
2193 len += (SSize_t)fread(bufptr,1,count,f);
2198 /* SOCKS 64 bit fseek replacement */
2199 int Perl_do_s64_seek(PerlIO* f, Off_t offset, int whence) {
2200 S64_IOB *ptr = _s64_get_buffer(f);
2202 /* Simply clear the buffer and seek if the position is absolute */
2203 if( SEEK_SET == whence || SEEK_END == whence) {
2204 if( ptr) ptr->cnt = 0;
2206 /* In case of relative positioning clear the buffer and calculate */
2207 /* a fixed offset */
2208 } else if( SEEK_CUR == whence) {
2210 offset -= (Off_t)ptr->cnt;
2215 /* leave out buffer untouched otherwise, because fseek will fail */
2217 return( fseeko( f, offset, whence));
2220 /* SOCKS 64 bit ftell replacement */
2221 Off_t Perl_do_s64_tell(PerlIO* f) {
2223 S64_IOB *ptr = _s64_get_buffer(f);
2226 return( ftello(f) - offset);