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 */
66 Perl_do_open(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
67 int rawmode, int rawperm, PerlIO *supplied_fp)
69 return do_open9(gv, name, len, as_raw, rawmode, rawperm,
70 supplied_fp, Nullsv, 0);
74 Perl_do_open9(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
75 int rawmode, int rawperm, PerlIO *supplied_fp, SV *svs,
78 register IO *io = GvIOn(gv);
79 PerlIO *saveifp = Nullfp;
80 PerlIO *saveofp = Nullfp;
81 char savetype = IoTYPE_CLOSED;
86 bool was_fdopen = FALSE;
87 bool in_raw = 0, in_crlf = 0, out_raw = 0, out_crlf = 0;
90 char mode[4]; /* stdio file mode ("r\0", "rb\0", "r+b\0" etc.) */
92 Zero(mode,sizeof(mode),char);
93 PL_forkprocess = 1; /* assume true if no fork */
95 /* Collect default raw/crlf info from the op */
96 if (PL_op && PL_op->op_type == OP_OPEN) {
97 /* set up disciplines */
98 U8 flags = PL_op->op_private;
99 in_raw = (flags & OPpOPEN_IN_RAW);
100 in_crlf = (flags & OPpOPEN_IN_CRLF);
101 out_raw = (flags & OPpOPEN_OUT_RAW);
102 out_crlf = (flags & OPpOPEN_OUT_CRLF);
105 /* If currently open - close before we re-open */
107 fd = PerlIO_fileno(IoIFP(io));
108 if (IoTYPE(io) == IoTYPE_STD)
110 else if (fd <= PL_maxsysfd) {
113 savetype = IoTYPE(io);
116 else if (IoTYPE(io) == IoTYPE_PIPE)
117 result = PerlProc_pclose(IoIFP(io));
118 else if (IoIFP(io) != IoOFP(io)) {
120 result = PerlIO_close(IoOFP(io));
121 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
124 result = PerlIO_close(IoIFP(io));
127 result = PerlIO_close(IoIFP(io));
128 if (result == EOF && fd > PL_maxsysfd)
129 PerlIO_printf(Perl_error_log,
130 "Warning: unable to close filehandle %s properly.\n",
132 IoOFP(io) = IoIFP(io) = Nullfp;
136 /* sysopen style args, i.e. integer mode and permissions */
138 #if defined(USE_64_BIT_RAWIO) && defined(O_LARGEFILE)
139 rawmode |= O_LARGEFILE;
143 #define O_ACCMODE 3 /* Assume traditional implementation */
146 switch (result = rawmode & O_ACCMODE) {
148 IoTYPE(io) = IoTYPE_RDONLY;
151 IoTYPE(io) = IoTYPE_WRONLY;
155 IoTYPE(io) = IoTYPE_RDWR;
159 writing = (result > 0);
160 fd = PerlLIO_open3(name, rawmode, rawperm);
166 if (result == O_RDONLY) {
170 else if (rawmode & O_APPEND) {
172 if (result != O_WRONLY)
177 if (result == O_WRONLY)
184 if (rawmode & O_BINARY)
187 fp = PerlIO_fdopen(fd, mode);
193 /* Regular (non-sys) open */
199 type = savepvn(name, len);
202 /* Loose trailing white space */
203 while (tend > type && isSPACE(tend[-1]))
206 /* New style explict name, type is just mode and discipline/layer info */
208 name = SvPV(svs, l) ;
210 name = savepvn(name, len);
213 for (; isSPACE(*type); type++) ;
220 if (*type == IoTYPE_RDWR && (!num_svs || tend > type+1 && tend[-1] != IoTYPE_PIPE)) { /* scary */
225 if (*type == IoTYPE_PIPE) {
227 if (type[1] != IoTYPE_STD) {
229 Perl_croak(aTHX_ "Unknown open() mode '%.*s'", (int)olen, oname);
234 for (type++; isSPACE(*type); type++) ;
239 if (*name == '\0') { /* command is missing 19990114 */
241 if (ckWARN(WARN_PIPE))
242 Perl_warner(aTHX_ WARN_PIPE, "Missing command in piped open");
246 if (strNE(name,"-") || num_svs)
248 TAINT_PROPER("piped open");
249 if (!num_svs && name[len-1] == '|') {
252 if (ckWARN(WARN_PIPE))
253 Perl_warner(aTHX_ WARN_PIPE, "Can't open bidirectional pipe");
261 fp = PerlProc_popen(name,mode);
263 else if (*type == IoTYPE_WRONLY) {
264 TAINT_PROPER("open");
266 if (*type == IoTYPE_WRONLY) {
267 /* Two IoTYPE_WRONLYs in a row make for an IoTYPE_APPEND. */
268 mode[0] = IoTYPE(io) = IoTYPE_APPEND;
291 if (!*name && supplied_fp)
295 for (; isSPACE(*name); name++) ;
300 gv = gv_fetchpv(name,FALSE,SVt_PVIO);
304 SETERRNO(EINVAL,SS$_IVCHAN);
309 PerlIO *fp = IoIFP(thatio);
310 /* Flush stdio buffer before dup. --mjd
311 * Unfortunately SEEK_CURing 0 seems to
312 * be optimized away on most platforms;
313 * only Solaris and Linux seem to flush
316 /* sfio fails to clear error on next
317 sfwrite, contrary to documentation.
319 if (PerlIO_seek(fp, 0, SEEK_CUR) == -1)
322 /* On the other hand, do all platforms
323 * take gracefully to flushing a read-only
324 * filehandle? Perhaps we should do
325 * fsetpos(src)+fgetpos(dst)? --nik */
327 fd = PerlIO_fileno(fp);
328 /* When dup()ing STDIN, STDOUT or STDERR
329 * explicitly set appropriate access mode */
330 if (IoIFP(thatio) == PerlIO_stdout()
331 || IoIFP(thatio) == PerlIO_stderr())
332 IoTYPE(io) = IoTYPE_WRONLY;
333 else if (IoIFP(thatio) == PerlIO_stdin())
334 IoTYPE(io) = IoTYPE_RDONLY;
335 /* When dup()ing a socket, say result is
337 else if (IoTYPE(thatio) == IoTYPE_SOCKET)
338 IoTYPE(io) = IoTYPE_SOCKET;
344 fd = PerlLIO_dup(fd);
347 if (!(fp = PerlIO_fdopen(fd,mode))) {
355 for (; isSPACE(*type); type++) ;
356 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
359 fp = PerlIO_stdout();
360 IoTYPE(io) = IoTYPE_STD;
363 fp = PerlIO_open((num_svs ? name : type), mode);
367 else if (*type == IoTYPE_RDONLY) {
369 for (type++; isSPACE(*type); type++) ;
380 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
384 IoTYPE(io) = IoTYPE_STD;
387 fp = PerlIO_open((num_svs ? name : type), mode);
389 else if ((num_svs && type[0] == IoTYPE_STD && type[1] == IoTYPE_PIPE) ||
390 (!num_svs && tend > type+1 && tend[-1] == IoTYPE_PIPE)) {
392 type += 2; /* skip over '-|' */
396 while (tend > type && isSPACE(tend[-1]))
399 for (; isSPACE(*type); type++) ;
403 if (*name == '\0') { /* command is missing 19990114 */
405 if (ckWARN(WARN_PIPE))
406 Perl_warner(aTHX_ WARN_PIPE, "Missing command in piped open");
410 if (strNE(name,"-") || num_svs)
412 TAINT_PROPER("piped open");
418 fp = PerlProc_popen(name,mode);
419 IoTYPE(io) = IoTYPE_PIPE;
425 IoTYPE(io) = IoTYPE_RDONLY;
427 for (; isSPACE(*name); name++) ;
433 if (strEQ(name,"-")) {
435 IoTYPE(io) = IoTYPE_STD;
438 fp = PerlIO_open(name,mode);
444 if (ckWARN(WARN_NEWLINE) && IoTYPE(io) == IoTYPE_RDONLY && strchr(name, '\n'))
445 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "open");
448 if (IoTYPE(io) && IoTYPE(io) != IoTYPE_PIPE && IoTYPE(io) != IoTYPE_STD) {
450 if (PerlLIO_fstat(PerlIO_fileno(fp),&PL_statbuf) < 0) {
451 (void)PerlIO_close(fp);
454 if (S_ISSOCK(PL_statbuf.st_mode))
455 IoTYPE(io) = IoTYPE_SOCKET; /* in case a socket was passed in to us */
459 !(PL_statbuf.st_mode & S_IFMT)
463 && IoTYPE(io) != IoTYPE_WRONLY /* Dups of STD* filehandles already have */
464 && IoTYPE(io) != IoTYPE_RDONLY /* type so they aren't marked as sockets */
465 ) { /* on OS's that return 0 on fstat()ed pipe */
467 Sock_size_t buflen = sizeof tmpbuf;
468 if (PerlSock_getsockname(PerlIO_fileno(fp), (struct sockaddr *)tmpbuf,
470 || errno != ENOTSOCK)
471 IoTYPE(io) = IoTYPE_SOCKET; /* some OS's return 0 on fstat()ed socket */
472 /* but some return 0 for streams too, sigh */
476 if (saveifp) { /* must use old fp? */
477 fd = PerlIO_fileno(saveifp);
479 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
480 if (saveofp != saveifp) { /* was a socket? */
481 PerlIO_close(saveofp);
486 if (fd != PerlIO_fileno(fp)) {
490 PerlLIO_dup2(PerlIO_fileno(fp), fd);
492 sv = *av_fetch(PL_fdpid,PerlIO_fileno(fp),TRUE);
493 (void)SvUPGRADE(sv, SVt_IV);
496 sv = *av_fetch(PL_fdpid,fd,TRUE);
498 (void)SvUPGRADE(sv, SVt_IV);
507 #if defined(HAS_FCNTL) && defined(F_SETFD)
509 int save_errno = errno;
510 fd = PerlIO_fileno(fp);
511 fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
517 /* Need to supply default type info from open.pm */
518 SV *layers = PL_curcop->cop_io;
522 type = SvPV(layers,len);
523 if (type && mode[0] != 'r') {
524 /* Skip to write part */
525 char *s = strchr(type,0);
526 if (s && (s-type) < len) {
531 else if (O_BINARY != O_TEXT) {
536 while (isSPACE(*type)) type++;
538 if (PerlIO_apply_layers(aTHX_ IoIFP(io),mode,type) != 0) {
544 IoFLAGS(io) &= ~IOf_NOLINE;
547 if (IoTYPE(io) == IoTYPE_SOCKET
548 || (IoTYPE(io) == IoTYPE_WRONLY && S_ISCHR(PL_statbuf.st_mode)) )
551 if (!(IoOFP(io) = PerlIO_fdopen(PerlIO_fileno(fp),mode))) {
557 if (PerlIO_apply_layers(aTHX_ IoOFP(io),mode,type) != 0) {
558 PerlIO_close(IoOFP(io));
574 IoTYPE(io) = savetype;
579 Perl_nextargv(pTHX_ register GV *gv)
582 #ifndef FLEXFILENAMES
591 PL_argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO);
592 if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
593 IoFLAGS(io) &= ~IOf_START;
595 if (!PL_argvout_stack)
596 PL_argvout_stack = newAV();
597 av_push(PL_argvout_stack, SvREFCNT_inc(PL_defoutgv));
600 if (PL_filemode & (S_ISUID|S_ISGID)) {
601 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
603 (void)fchmod(PL_lastfd,PL_filemode);
605 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
609 while (av_len(GvAV(gv)) >= 0) {
612 sv = av_shift(GvAV(gv));
614 sv_setsv(GvSV(gv),sv);
615 SvSETMAGIC(GvSV(gv));
616 PL_oldname = SvPVx(GvSV(gv), oldlen);
617 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,Nullfp)) {
619 TAINT_PROPER("inplace open");
620 if (oldlen == 1 && *PL_oldname == '-') {
621 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
622 return IoIFP(GvIOp(gv));
624 #ifndef FLEXFILENAMES
625 filedev = PL_statbuf.st_dev;
626 fileino = PL_statbuf.st_ino;
628 PL_filemode = PL_statbuf.st_mode;
629 fileuid = PL_statbuf.st_uid;
630 filegid = PL_statbuf.st_gid;
631 if (!S_ISREG(PL_filemode)) {
632 if (ckWARN_d(WARN_INPLACE))
633 Perl_warner(aTHX_ WARN_INPLACE,
634 "Can't do inplace edit: %s is not a regular file",
640 char *star = strchr(PL_inplace, '*');
642 char *begin = PL_inplace;
643 sv_setpvn(sv, "", 0);
645 sv_catpvn(sv, begin, star - begin);
646 sv_catpvn(sv, PL_oldname, oldlen);
648 } while ((star = strchr(begin, '*')));
653 sv_catpv(sv,PL_inplace);
655 #ifndef FLEXFILENAMES
656 if (PerlLIO_stat(SvPVX(sv),&PL_statbuf) >= 0
657 && PL_statbuf.st_dev == filedev
658 && PL_statbuf.st_ino == fileino
660 || (_djstat_fail_bits & _STFAIL_TRUENAME)!=0
664 if (ckWARN_d(WARN_INPLACE))
665 Perl_warner(aTHX_ WARN_INPLACE,
666 "Can't do inplace edit: %s would not be unique",
673 #if !defined(DOSISH) && !defined(__CYGWIN__)
674 if (PerlLIO_rename(PL_oldname,SvPVX(sv)) < 0) {
675 if (ckWARN_d(WARN_INPLACE))
676 Perl_warner(aTHX_ WARN_INPLACE,
677 "Can't rename %s to %s: %s, skipping file",
678 PL_oldname, SvPVX(sv), Strerror(errno) );
684 (void)PerlLIO_unlink(SvPVX(sv));
685 (void)PerlLIO_rename(PL_oldname,SvPVX(sv));
686 do_open(gv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,O_RDONLY,0,Nullfp);
689 (void)UNLINK(SvPVX(sv));
690 if (link(PL_oldname,SvPVX(sv)) < 0) {
691 if (ckWARN_d(WARN_INPLACE))
692 Perl_warner(aTHX_ WARN_INPLACE,
693 "Can't rename %s to %s: %s, skipping file",
694 PL_oldname, SvPVX(sv), Strerror(errno) );
698 (void)UNLINK(PL_oldname);
702 #if !defined(DOSISH) && !defined(AMIGAOS)
703 # ifndef VMS /* Don't delete; use automatic file versioning */
704 if (UNLINK(PL_oldname) < 0) {
705 if (ckWARN_d(WARN_INPLACE))
706 Perl_warner(aTHX_ WARN_INPLACE,
707 "Can't remove %s: %s, skipping file",
708 PL_oldname, Strerror(errno) );
714 Perl_croak(aTHX_ "Can't do inplace edit without backup");
718 sv_setpvn(sv,">",!PL_inplace);
719 sv_catpvn(sv,PL_oldname,oldlen);
720 SETERRNO(0,0); /* in case sprintf set errno */
722 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
723 O_WRONLY|O_CREAT|O_TRUNC,0,Nullfp))
725 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
726 O_WRONLY|O_CREAT|OPEN_EXCL,0666,Nullfp))
729 if (ckWARN_d(WARN_INPLACE))
730 Perl_warner(aTHX_ WARN_INPLACE, "Can't do inplace edit on %s: %s",
731 PL_oldname, Strerror(errno) );
735 setdefout(PL_argvoutgv);
736 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
737 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
739 (void)fchmod(PL_lastfd,PL_filemode);
741 # if !(defined(WIN32) && defined(__BORLANDC__))
742 /* Borland runtime creates a readonly file! */
743 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
746 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
748 (void)fchown(PL_lastfd,fileuid,filegid);
751 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
756 return IoIFP(GvIOp(gv));
760 if (ckWARN_d(WARN_INPLACE)) {
762 if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0
763 && !S_ISREG(PL_statbuf.st_mode))
765 Perl_warner(aTHX_ WARN_INPLACE,
766 "Can't do inplace edit: %s is not a regular file",
770 Perl_warner(aTHX_ WARN_INPLACE, "Can't open %s: %s",
771 PL_oldname, Strerror(eno));
775 if (io && (IoFLAGS(io) & IOf_ARGV))
776 IoFLAGS(io) |= IOf_START;
778 (void)do_close(PL_argvoutgv,FALSE);
779 if (io && (IoFLAGS(io) & IOf_ARGV)
780 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
782 GV *oldout = (GV*)av_pop(PL_argvout_stack);
784 SvREFCNT_dec(oldout);
787 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
794 Perl_do_pipe(pTHX_ SV *sv, GV *rgv, GV *wgv)
813 if (PerlProc_pipe(fd) < 0)
815 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r");
816 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w");
817 IoIFP(wstio) = IoOFP(wstio);
818 IoTYPE(rstio) = IoTYPE_RDONLY;
819 IoTYPE(wstio) = IoTYPE_WRONLY;
820 if (!IoIFP(rstio) || !IoOFP(wstio)) {
821 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
822 else PerlLIO_close(fd[0]);
823 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
824 else PerlLIO_close(fd[1]);
828 sv_setsv(sv,&PL_sv_yes);
832 sv_setsv(sv,&PL_sv_undef);
837 /* explicit renamed to avoid C++ conflict -- kja */
839 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
846 if (!gv || SvTYPE(gv) != SVt_PVGV) {
848 SETERRNO(EBADF,SS$_IVCHAN);
852 if (!io) { /* never opened */
855 if (ckWARN(WARN_UNOPENED)) /* no check for closed here */
856 report_evil_fh(gv, io, PL_op->op_type);
857 SETERRNO(EBADF,SS$_IVCHAN);
861 retval = io_close(io, not_implicit);
865 IoLINES_LEFT(io) = IoPAGE_LEN(io);
867 IoTYPE(io) = IoTYPE_CLOSED;
872 Perl_io_close(pTHX_ IO *io, bool not_implicit)
878 if (IoTYPE(io) == IoTYPE_PIPE) {
879 status = PerlProc_pclose(IoIFP(io));
881 STATUS_NATIVE_SET(status);
882 retval = (STATUS_POSIX == 0);
885 retval = (status != -1);
888 else if (IoTYPE(io) == IoTYPE_STD)
891 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
892 retval = (PerlIO_close(IoOFP(io)) != EOF);
893 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
896 retval = (PerlIO_close(IoIFP(io)) != EOF);
898 IoOFP(io) = IoIFP(io) = Nullfp;
900 else if (not_implicit) {
901 SETERRNO(EBADF,SS$_IVCHAN);
908 Perl_do_eof(pTHX_ GV *gv)
918 else if (ckWARN(WARN_IO)
919 && (IoTYPE(io) == IoTYPE_WRONLY || IoIFP(io) == PerlIO_stdout()
920 || IoIFP(io) == PerlIO_stderr()))
922 /* integrate to report_evil_fh()? */
925 SV* sv = sv_newmortal();
926 gv_efullname4(sv, gv, Nullch, FALSE);
927 name = SvPV_nolen(sv);
930 Perl_warner(aTHX_ WARN_IO,
931 "Filehandle %s opened only for output", name);
933 Perl_warner(aTHX_ WARN_IO,
934 "Filehandle opened only for output");
939 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
940 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
941 return FALSE; /* this is the most usual case */
944 ch = PerlIO_getc(IoIFP(io));
946 (void)PerlIO_ungetc(IoIFP(io),ch);
950 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
951 if (PerlIO_get_cnt(IoIFP(io)) < -1)
952 PerlIO_set_cnt(IoIFP(io),-1);
954 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
955 if (!nextargv(PL_argvgv)) /* get another fp handy */
959 return TRUE; /* normal fp, definitely end of file */
965 Perl_do_tell(pTHX_ GV *gv)
970 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
971 #ifdef ULTRIX_STDIO_BOTCH
973 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
975 return PerlIO_tell(fp);
979 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
980 report_evil_fh(gv, io, PL_op->op_type);
982 SETERRNO(EBADF,RMS$_IFI);
987 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
992 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
993 #ifdef ULTRIX_STDIO_BOTCH
995 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
997 return PerlIO_seek(fp, pos, whence) >= 0;
1001 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1002 report_evil_fh(gv, io, PL_op->op_type);
1004 SETERRNO(EBADF,RMS$_IFI);
1009 Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
1012 register PerlIO *fp;
1014 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
1015 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
1018 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1019 report_evil_fh(gv, io, PL_op->op_type);
1021 SETERRNO(EBADF,RMS$_IFI);
1026 Perl_mode_from_discipline(pTHX_ SV *discp)
1028 int mode = O_BINARY;
1031 char *s = SvPV(discp,len);
1036 if (len > 3 && strnEQ(s+1, "raw", 3)
1037 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1046 if (len > 4 && strnEQ(s+1, "crlf", 4)
1047 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1056 goto fail_discipline;
1059 else if (isSPACE(*s)) {
1066 end = strchr(s+1, ':');
1069 Perl_croak(aTHX_ "Unknown discipline '%.*s'", end-s, s);
1077 Perl_do_binmode(pTHX_ PerlIO *fp, int iotype, int mode)
1080 # if defined(atarist) || defined(__MINT__)
1081 if (!PerlIO_flush(fp)) {
1082 if (mode & O_BINARY)
1083 ((FILE*)fp)->_flag |= _IOBIN;
1085 ((FILE*)fp)->_flag &= ~ _IOBIN;
1090 if (PerlLIO_setmode(PerlIO_fileno(fp), mode) != -1) {
1091 # if defined(WIN32) && defined(__BORLANDC__)
1092 /* The translation mode of the stream is maintained independent
1093 * of the translation mode of the fd in the Borland RTL (heavy
1094 * digging through their runtime sources reveal). User has to
1095 * set the mode explicitly for the stream (though they don't
1096 * document this anywhere). GSAR 97-5-24
1098 PerlIO_seek(fp,0L,0);
1099 if (mode & O_BINARY)
1100 ((FILE*)fp)->flags |= _F_BIN;
1102 ((FILE*)fp)->flags &= ~ _F_BIN;
1110 # if defined(USEMYBINMODE)
1111 if (my_binmode(fp, iotype, mode) != FALSE)
1121 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP)
1122 /* code courtesy of William Kucharski */
1125 I32 my_chsize(fd, length)
1126 I32 fd; /* file descriptor */
1127 Off_t length; /* length to set file to */
1130 struct stat filebuf;
1132 if (PerlLIO_fstat(fd, &filebuf) < 0)
1135 if (filebuf.st_size < length) {
1137 /* extend file length */
1139 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
1142 /* write a "0" byte */
1144 if ((PerlLIO_write(fd, "", 1)) != 1)
1148 /* truncate length */
1152 fl.l_start = length;
1153 fl.l_type = F_WRLCK; /* write lock on file space */
1156 * This relies on the UNDOCUMENTED F_FREESP argument to
1157 * fcntl(2), which truncates the file so that it ends at the
1158 * position indicated by fl.l_start.
1160 * Will minor miracles never cease?
1163 if (fcntl(fd, F_FREESP, &fl) < 0)
1170 #endif /* F_FREESP */
1173 Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
1175 register char *tmps;
1178 /* assuming fp is checked earlier */
1184 if (SvIOK(sv) && SvIVX(sv) != 0) {
1185 PerlIO_printf(fp, PL_ofmt, (NV)SvIVX(sv));
1186 return !PerlIO_error(fp);
1188 if ( (SvNOK(sv) && SvNVX(sv) != 0.0)
1189 || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) {
1190 PerlIO_printf(fp, PL_ofmt, SvNVX(sv));
1191 return !PerlIO_error(fp);
1194 switch (SvTYPE(sv)) {
1198 if (ckWARN(WARN_UNINITIALIZED))
1207 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
1209 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
1210 return !PerlIO_error(fp);
1215 /* XXX Fix this when the I/O disciplines arrive. XXX */
1217 sv_utf8_downgrade(sv, FALSE);
1219 tmps = SvPV(sv, len);
1222 /* To detect whether the process is about to overstep its
1223 * filesize limit we would need getrlimit(). We could then
1224 * also transparently raise the limit with setrlimit() --
1225 * but only until the system hard limit/the filesystem limit,
1226 * at which we would get EPERM. Note that when using buffered
1227 * io the write failure can be delayed until the flush/close. --jhi */
1228 if (len && (PerlIO_write(fp,tmps,len) == 0))
1230 return !PerlIO_error(fp);
1240 if (PL_op->op_flags & OPf_REF) {
1245 if (io && IoIFP(io)) {
1247 sv_setpv(PL_statname,"");
1248 PL_laststype = OP_STAT;
1249 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
1253 return PL_laststatval;
1254 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1255 report_evil_fh(gv, io, PL_op->op_type);
1257 sv_setpv(PL_statname,"");
1258 return (PL_laststatval = -1);
1266 if (SvTYPE(sv) == SVt_PVGV) {
1270 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
1277 sv_setpv(PL_statname, s);
1278 PL_laststype = OP_STAT;
1279 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1280 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
1281 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "stat");
1282 return PL_laststatval;
1292 if (PL_op->op_flags & OPf_REF) {
1294 if (cGVOP_gv == PL_defgv) {
1295 if (PL_laststype != OP_LSTAT)
1296 Perl_croak(aTHX_ "The stat preceding -l _ wasn't an lstat");
1297 return PL_laststatval;
1299 Perl_croak(aTHX_ "You can't use -l on a filehandle");
1302 PL_laststype = OP_LSTAT;
1306 sv_setpv(PL_statname,SvPV(sv, n_a));
1307 PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache);
1308 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
1309 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "lstat");
1310 return PL_laststatval;
1314 Perl_do_aexec(pTHX_ SV *really, register SV **mark, register SV **sp)
1316 return do_aexec5(really, mark, sp, 0, 0);
1320 Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1321 int fd, int do_report)
1323 #ifdef MACOS_TRADITIONAL
1324 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1332 New(401,PL_Argv, sp - mark + 1, char*);
1334 while (++mark <= sp) {
1336 *a++ = SvPVx(*mark, n_a);
1341 if (*PL_Argv[0] != '/') /* will execvp use PATH? */
1342 TAINT_ENV(); /* testing IFS here is overkill, probably */
1343 if (really && *(tmps = SvPV(really, n_a)))
1344 PerlProc_execvp(tmps,PL_Argv);
1346 PerlProc_execvp(PL_Argv[0],PL_Argv);
1347 if (ckWARN(WARN_EXEC))
1348 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1349 PL_Argv[0], Strerror(errno));
1353 PerlLIO_write(fd, (void*)&e, sizeof(int));
1363 Perl_do_execfree(pTHX)
1367 PL_Argv = Null(char **);
1375 #if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC) && !defined(MACOS_TRADITIONAL)
1378 Perl_do_exec(pTHX_ char *cmd)
1380 return do_exec3(cmd,0,0);
1384 Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report)
1390 while (*cmd && isSPACE(*cmd))
1393 /* save an extra exec if possible */
1396 if (strnEQ(cmd,PL_cshname,PL_cshlen) && strnEQ(cmd+PL_cshlen," -c",3)) {
1398 s = cmd+PL_cshlen+3;
1412 if (s[-1] == '\'') {
1414 PerlProc_execl(PL_cshname,"csh", flags,ncmd,(char*)0);
1422 /* see if there are shell metacharacters in it */
1424 if (*cmd == '.' && isSPACE(cmd[1]))
1427 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1430 for (s = cmd; *s && isALNUM(*s); s++) ; /* catch VAR=val gizmo */
1434 for (s = cmd; *s; s++) {
1435 if (*s != ' ' && !isALPHA(*s) && strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1436 if (*s == '\n' && !s[1]) {
1440 /* handle the 2>&1 construct at the end */
1441 if (*s == '>' && s[1] == '&' && s[2] == '1'
1442 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1443 && (!s[3] || isSPACE(s[3])))
1447 while (*t && isSPACE(*t))
1449 if (!*t && (dup2(1,2) != -1)) {
1455 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
1460 New(402,PL_Argv, (s - cmd) / 2 + 2, char*);
1461 PL_Cmd = savepvn(cmd, s-cmd);
1463 for (s = PL_Cmd; *s;) {
1464 while (*s && isSPACE(*s)) s++;
1467 while (*s && !isSPACE(*s)) s++;
1473 PerlProc_execvp(PL_Argv[0],PL_Argv);
1474 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1482 if (ckWARN(WARN_EXEC))
1483 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1484 PL_Argv[0], Strerror(errno));
1486 PerlLIO_write(fd, (void*)&e, sizeof(int));
1495 #endif /* OS2 || WIN32 */
1498 Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
1503 register I32 tot = 0;
1506 SV **oldmark = mark;
1509 #define APPLY_TAINT_PROPER() \
1511 if (PL_tainted) { TAINT_PROPER(what); } \
1514 /* This is a first heuristic; it doesn't catch tainting magic. */
1516 while (++mark <= sp) {
1517 if (SvTAINTED(*mark)) {
1527 APPLY_TAINT_PROPER();
1530 APPLY_TAINT_PROPER();
1532 while (++mark <= sp) {
1533 char *name = SvPVx(*mark, n_a);
1534 APPLY_TAINT_PROPER();
1535 if (PerlLIO_chmod(name, val))
1543 APPLY_TAINT_PROPER();
1544 if (sp - mark > 2) {
1545 val = SvIVx(*++mark);
1546 val2 = SvIVx(*++mark);
1547 APPLY_TAINT_PROPER();
1549 while (++mark <= sp) {
1550 char *name = SvPVx(*mark, n_a);
1551 APPLY_TAINT_PROPER();
1552 if (PerlLIO_chown(name, val, val2))
1559 XXX Should we make lchown() directly available from perl?
1560 For now, we'll let Configure test for HAS_LCHOWN, but do
1561 nothing in the core.
1567 APPLY_TAINT_PROPER();
1570 s = SvPVx(*++mark, n_a);
1572 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1574 if (!(val = whichsig(s)))
1575 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
1579 APPLY_TAINT_PROPER();
1582 /* kill() doesn't do process groups (job trees?) under VMS */
1583 if (val < 0) val = -val;
1584 if (val == SIGKILL) {
1585 # include <starlet.h>
1586 /* Use native sys$delprc() to insure that target process is
1587 * deleted; supervisor-mode images don't pay attention to
1588 * CRTL's emulation of Unix-style signals and kill()
1590 while (++mark <= sp) {
1591 I32 proc = SvIVx(*mark);
1592 register unsigned long int __vmssts;
1593 APPLY_TAINT_PROPER();
1594 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1598 case SS$_NOSUCHNODE:
1599 SETERRNO(ESRCH,__vmssts);
1602 SETERRNO(EPERM,__vmssts);
1605 SETERRNO(EVMSERR,__vmssts);
1614 while (++mark <= sp) {
1615 I32 proc = SvIVx(*mark);
1616 APPLY_TAINT_PROPER();
1618 if (PerlProc_killpg(proc,val)) /* BSD */
1620 if (PerlProc_kill(-proc,val)) /* SYSV */
1626 while (++mark <= sp) {
1627 I32 proc = SvIVx(*mark);
1628 APPLY_TAINT_PROPER();
1629 if (PerlProc_kill(proc, val))
1637 APPLY_TAINT_PROPER();
1639 while (++mark <= sp) {
1640 s = SvPVx(*mark, n_a);
1641 APPLY_TAINT_PROPER();
1642 if (PL_euid || PL_unsafe) {
1646 else { /* don't let root wipe out directories without -U */
1647 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1659 APPLY_TAINT_PROPER();
1660 if (sp - mark > 2) {
1661 #if defined(I_UTIME) || defined(VMS)
1662 struct utimbuf utbuf;
1670 Zero(&utbuf, sizeof utbuf, char);
1672 utbuf.actime = (Time_t)SvNVx(*++mark); /* time accessed */
1673 utbuf.modtime = (Time_t)SvNVx(*++mark); /* time modified */
1675 utbuf.actime = (Time_t)SvIVx(*++mark); /* time accessed */
1676 utbuf.modtime = (Time_t)SvIVx(*++mark); /* time modified */
1678 APPLY_TAINT_PROPER();
1680 while (++mark <= sp) {
1681 char *name = SvPVx(*mark, n_a);
1682 APPLY_TAINT_PROPER();
1683 if (PerlLIO_utime(name, &utbuf))
1694 #undef APPLY_TAINT_PROPER
1697 /* Do the permissions allow some operation? Assumes statcache already set. */
1698 #ifndef VMS /* VMS' cando is in vms.c */
1700 Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register Stat_t *statbufp)
1701 /* Note: we use `effective' both for uids and gids.
1702 * Here we are betting on Uid_t being equal or wider than Gid_t. */
1705 /* [Comments and code from Len Reed]
1706 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1707 * to write-protected files. The execute permission bit is set
1708 * by the Miscrosoft C library stat() function for the following:
1713 * All files and directories are readable.
1714 * Directories and special files, e.g. "CON", cannot be
1716 * [Comment by Tom Dinger -- a directory can have the write-protect
1717 * bit set in the file system, but DOS permits changes to
1718 * the directory anyway. In addition, all bets are off
1719 * here for networked software, such as Novell and
1723 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1724 * too so it will actually look into the files for magic numbers
1726 return (mode & statbufp->st_mode) ? TRUE : FALSE;
1728 #else /* ! DOSISH */
1729 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
1730 if (mode == S_IXUSR) {
1731 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1735 return TRUE; /* root reads and writes anything */
1738 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1739 if (statbufp->st_mode & mode)
1740 return TRUE; /* ok as "user" */
1742 else if (ingroup(statbufp->st_gid,effective)) {
1743 if (statbufp->st_mode & mode >> 3)
1744 return TRUE; /* ok as "group" */
1746 else if (statbufp->st_mode & mode >> 6)
1747 return TRUE; /* ok as "other" */
1749 #endif /* ! DOSISH */
1754 Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective)
1756 #ifdef MACOS_TRADITIONAL
1757 /* This is simply not correct for AppleShare, but fix it yerself. */
1760 if (testgid == (effective ? PL_egid : PL_gid))
1762 #ifdef HAS_GETGROUPS
1767 Groups_t gary[NGROUPS];
1770 anum = getgroups(NGROUPS,gary);
1772 if (gary[anum] == testgid)
1780 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1783 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
1789 key = (key_t)SvNVx(*++mark);
1790 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1791 flags = SvIVx(*++mark);
1797 return msgget(key, flags);
1801 return semget(key, n, flags);
1805 return shmget(key, n, flags);
1807 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1809 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1812 return -1; /* should never happen */
1816 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
1821 I32 id, n, cmd, infosize, getinfo;
1824 id = SvIVx(*++mark);
1825 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1826 cmd = SvIVx(*++mark);
1829 getinfo = (cmd == IPC_STAT);
1835 if (cmd == IPC_STAT || cmd == IPC_SET)
1836 infosize = sizeof(struct msqid_ds);
1841 if (cmd == IPC_STAT || cmd == IPC_SET)
1842 infosize = sizeof(struct shmid_ds);
1848 if (cmd == IPC_STAT || cmd == IPC_SET)
1849 infosize = sizeof(struct semid_ds);
1850 else if (cmd == GETALL || cmd == SETALL)
1852 struct semid_ds semds;
1854 #ifdef EXTRA_F_IN_SEMUN_BUF
1855 semun.buff = &semds;
1859 getinfo = (cmd == GETALL);
1860 if (Semctl(id, 0, IPC_STAT, semun) == -1)
1862 infosize = semds.sem_nsems * sizeof(short);
1863 /* "short" is technically wrong but much more portable
1864 than guessing about u_?short(_t)? */
1867 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1871 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1873 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1882 SvPV_force(astr, len);
1883 a = SvGROW(astr, infosize+1);
1887 a = SvPV(astr, len);
1888 if (len != infosize)
1889 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
1898 a = INT2PTR(char *,i); /* ouch */
1905 ret = msgctl(id, cmd, (struct msqid_ds *)a);
1911 union semun unsemds;
1913 #ifdef EXTRA_F_IN_SEMUN_BUF
1914 unsemds.buff = (struct semid_ds *)a;
1916 unsemds.buf = (struct semid_ds *)a;
1918 ret = Semctl(id, n, cmd, unsemds);
1920 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1927 ret = shmctl(id, cmd, (struct shmid_ds *)a);
1931 if (getinfo && ret >= 0) {
1932 SvCUR_set(astr, infosize);
1933 *SvEND(astr) = '\0';
1940 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
1946 I32 id, msize, flags;
1949 id = SvIVx(*++mark);
1951 flags = SvIVx(*++mark);
1952 mbuf = SvPV(mstr, len);
1953 if ((msize = len - sizeof(long)) < 0)
1954 Perl_croak(aTHX_ "Arg too short for msgsnd");
1956 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
1958 Perl_croak(aTHX_ "msgsnd not implemented");
1963 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
1970 I32 id, msize, flags, ret;
1973 id = SvIVx(*++mark);
1975 /* suppress warning when reading into undef var --jhi */
1977 sv_setpvn(mstr, "", 0);
1978 msize = SvIVx(*++mark);
1979 mtype = (long)SvIVx(*++mark);
1980 flags = SvIVx(*++mark);
1981 SvPV_force(mstr, len);
1982 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
1985 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
1987 SvCUR_set(mstr, sizeof(long)+ret);
1988 *SvEND(mstr) = '\0';
1989 #ifndef INCOMPLETE_TAINTS
1990 /* who knows who has been playing with this message? */
1996 Perl_croak(aTHX_ "msgrcv not implemented");
2001 Perl_do_semop(pTHX_ SV **mark, SV **sp)
2010 id = SvIVx(*++mark);
2012 opbuf = SvPV(opstr, opsize);
2013 if (opsize < sizeof(struct sembuf)
2014 || (opsize % sizeof(struct sembuf)) != 0) {
2015 SETERRNO(EINVAL,LIB$_INVARG);
2019 return semop(id, (struct sembuf *)opbuf, opsize/sizeof(struct sembuf));
2021 Perl_croak(aTHX_ "semop not implemented");
2026 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
2032 I32 id, mpos, msize;
2034 struct shmid_ds shmds;
2036 id = SvIVx(*++mark);
2038 mpos = SvIVx(*++mark);
2039 msize = SvIVx(*++mark);
2041 if (shmctl(id, IPC_STAT, &shmds) == -1)
2043 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
2044 SETERRNO(EFAULT,SS$_ACCVIO); /* can't do as caller requested */
2047 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
2048 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2050 if (optype == OP_SHMREAD) {
2051 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2053 sv_setpvn(mstr, "", 0);
2054 SvPV_force(mstr, len);
2055 mbuf = SvGROW(mstr, msize+1);
2057 Copy(shm + mpos, mbuf, msize, char);
2058 SvCUR_set(mstr, msize);
2059 *SvEND(mstr) = '\0';
2061 #ifndef INCOMPLETE_TAINTS
2062 /* who knows who has been playing with this shared memory? */
2069 mbuf = SvPV(mstr, len);
2070 if ((n = len) > msize)
2072 Copy(mbuf, shm + mpos, n, char);
2074 memzero(shm + mpos + n, msize - n);
2078 Perl_croak(aTHX_ "shm I/O not implemented");
2082 #endif /* SYSV IPC */
2084 #ifdef SOCKS_64BIT_BUG
2087 ** getc and ungetc wrappers for the 64 bit problems with SOCKS 5 support
2088 ** Workaround to the problem, that SOCKS maps a socket 'getc' to revc
2089 ** without checking the ungetc buffer.
2092 static S64_IOB *s64_buffer = (S64_IOB *) NULL;
2094 /* initialize the buffer area */
2095 /* required after a fork(2) call in order to remove side effects */
2096 void Perl_do_s64_init_buffer() {
2097 s64_buffer = (S64_IOB *) NULL;
2100 /* get a buffered stream pointer */
2101 static S64_IOB *S_s64_get_buffer( PerlIO *fp) {
2102 S64_IOB *ptr = s64_buffer;
2103 while( ptr && ptr->fp != fp)
2108 /* create a buffered stream pointer */
2109 static S64_IOB *S_s64_create_buffer( PerlIO *f) {
2110 S64_IOB *ptr = malloc( sizeof( S64_IOB));
2113 ptr->cnt = ptr->size = 0;
2114 ptr->buffer = (int *) NULL;
2115 ptr->next = s64_buffer;
2116 ptr->last = (S64_IOB *) NULL;
2117 if( s64_buffer) s64_buffer->last = ptr;
2123 /* delete a buffered stream pointer */
2124 void Perl_do_s64_delete_buffer( PerlIO *f) {
2125 S64_IOB *ptr = _s64_get_buffer(f);
2127 /* fix the stream pointer according to the bytes buffered */
2128 /* required, if this is called in a seek-context */
2129 if( ptr->cnt) fseek(f,-ptr->cnt,SEEK_CUR);
2130 if( ptr->buffer) free( ptr->buffer);
2132 ptr->last->next = ptr->next;
2134 s64_buffer = ptr->next;
2139 /* internal buffer management */
2140 #define _S64_BUFFER_SIZE 32
2141 static int S_s64_malloc( S64_IOB *ptr) {
2144 ptr->buffer = (int *) calloc( _S64_BUFFER_SIZE, sizeof( int));
2145 ptr->size = ptr->cnt = 0;
2147 ptr->buffer = (int *) realloc( ptr->buffer, ptr->size + _S64_BUFFER_SIZE);
2153 ptr->size += _S64_BUFFER_SIZE;
2161 /* SOCKS 64 bit getc replacement */
2162 int Perl_do_s64_getc( PerlIO *f) {
2163 S64_IOB *ptr = _s64_get_buffer(f);
2166 return( ptr->buffer[--ptr->cnt]);
2171 /* SOCKS 64 bit ungetc replacement */
2172 int Perl_do_s64_ungetc( int ch, PerlIO *f) {
2173 S64_IOB *ptr = _s64_get_buffer(f);
2175 if( !ptr) ptr=_s64_create_buffer(f);
2176 if( !ptr) return( EOF);
2177 if( !ptr->buffer || (ptr->buffer && ptr->cnt >= ptr->size))
2178 if( !_s64_malloc( ptr)) return( EOF);
2179 ptr->buffer[ptr->cnt++] = ch;
2184 /* SOCKS 64 bit fread replacement */
2185 SSize_t Perl_do_s64_fread(void *buf, SSize_t count, PerlIO* f) {
2187 char *bufptr = (char *) buf;
2188 S64_IOB *ptr = _s64_get_buffer(f);
2190 while( ptr->cnt && count) {
2191 *bufptr++ = ptr->buffer[--ptr->cnt];
2196 len += (SSize_t)fread(bufptr,1,count,f);
2201 /* SOCKS 64 bit fseek replacement */
2202 int Perl_do_s64_seek(PerlIO* f, Off_t offset, int whence) {
2203 S64_IOB *ptr = _s64_get_buffer(f);
2205 /* Simply clear the buffer and seek if the position is absolute */
2206 if( SEEK_SET == whence || SEEK_END == whence) {
2207 if( ptr) ptr->cnt = 0;
2209 /* In case of relative positioning clear the buffer and calculate */
2210 /* a fixed offset */
2211 } else if( SEEK_CUR == whence) {
2213 offset -= (Off_t)ptr->cnt;
2218 /* leave out buffer untouched otherwise, because fseek will fail */
2220 return( fseeko( f, offset, whence));
2223 /* SOCKS 64 bit ftell replacement */
2224 Off_t Perl_do_s64_tell(PerlIO* f) {
2226 S64_IOB *ptr = _s64_get_buffer(f);
2229 return( ftello(f) - offset);