3 * Copyright (c) 1991-2000, Larry Wall
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
11 * "Far below them they saw the white waters pour into a foaming bowl, and
12 * then swirl darkly about a deep oval basin in the rocks, until they found
13 * their way out again through a narrow gate, and flowed away, fuming and
14 * chattering, into calmer and more level reaches."
18 #define PERL_IN_DOIO_C
21 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
30 # ifndef HAS_SHMAT_PROTOTYPE
31 extern Shmat_t shmat (int, char *, int);
37 # if defined(_MSC_VER) || defined(__MINGW32__)
38 # include <sys/utime.h>
45 # define OPEN_EXCL O_EXCL
50 #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
55 Perl_do_open(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
56 int rawmode, int rawperm, PerlIO *supplied_fp)
58 return do_open9(gv, name, len, as_raw, rawmode, rawperm,
59 supplied_fp, Nullsv, 0);
63 Perl_do_open9(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
64 int rawmode, int rawperm, PerlIO *supplied_fp, SV *svs,
67 register IO *io = GvIOn(gv);
68 PerlIO *saveifp = Nullfp;
69 PerlIO *saveofp = Nullfp;
70 char savetype = IoTYPE_CLOSED;
75 bool was_fdopen = FALSE;
76 bool in_raw = 0, in_crlf = 0, out_raw = 0, out_crlf = 0;
79 char mode[4]; /* stdio file mode ("r\0", "rb\0", "r+b\0" etc.) */
81 Zero(mode,sizeof(mode),char);
82 PL_forkprocess = 1; /* assume true if no fork */
84 /* Collect default raw/crlf info from the op */
85 if (PL_op && PL_op->op_type == OP_OPEN) {
86 /* set up disciplines */
87 U8 flags = PL_op->op_private;
88 in_raw = (flags & OPpOPEN_IN_RAW);
89 in_crlf = (flags & OPpOPEN_IN_CRLF);
90 out_raw = (flags & OPpOPEN_OUT_RAW);
91 out_crlf = (flags & OPpOPEN_OUT_CRLF);
94 /* If currently open - close before we re-open */
96 fd = PerlIO_fileno(IoIFP(io));
97 if (IoTYPE(io) == IoTYPE_STD)
99 else if (fd <= PL_maxsysfd) {
102 savetype = IoTYPE(io);
105 else if (IoTYPE(io) == IoTYPE_PIPE)
106 result = PerlProc_pclose(IoIFP(io));
107 else if (IoIFP(io) != IoOFP(io)) {
109 result = PerlIO_close(IoOFP(io));
110 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
113 result = PerlIO_close(IoIFP(io));
116 result = PerlIO_close(IoIFP(io));
117 if (result == EOF && fd > PL_maxsysfd)
118 PerlIO_printf(Perl_error_log,
119 "Warning: unable to close filehandle %s properly.\n",
121 IoOFP(io) = IoIFP(io) = Nullfp;
125 /* sysopen style args, i.e. integer mode and permissions */
127 #if defined(USE_64_BIT_RAWIO) && defined(O_LARGEFILE)
128 rawmode |= O_LARGEFILE;
132 #define O_ACCMODE 3 /* Assume traditional implementation */
135 switch (result = rawmode & O_ACCMODE) {
137 IoTYPE(io) = IoTYPE_RDONLY;
140 IoTYPE(io) = IoTYPE_WRONLY;
144 IoTYPE(io) = IoTYPE_RDWR;
148 writing = (result > 0);
149 fd = PerlLIO_open3(name, rawmode, rawperm);
155 if (result == O_RDONLY) {
159 else if (rawmode & O_APPEND) {
161 if (result != O_WRONLY)
166 if (result == O_WRONLY)
173 if (rawmode & O_BINARY)
176 fp = PerlIO_fdopen(fd, mode);
182 /* Regular (non-sys) open */
188 type = savepvn(name, len);
191 /* Loose trailing white space */
192 while (tend > type && isSPACE(tend[-1]))
195 /* New style explict name, type is just mode and discipline/layer info */
197 name = SvPV(svs, l) ;
199 name = savepvn(name, len);
202 for (; isSPACE(*type); type++) ;
209 if (*type == IoTYPE_RDWR && (!num_svs || tend > type+1 && tend[-1] != IoTYPE_PIPE)) { /* scary */
214 if (*type == IoTYPE_PIPE) {
216 if (type[1] != IoTYPE_STD) {
218 Perl_croak(aTHX_ "Unknown open() mode '%.*s'", (int)olen, oname);
223 for (type++; isSPACE(*type); type++) ;
228 if (*name == '\0') { /* command is missing 19990114 */
230 if (ckWARN(WARN_PIPE))
231 Perl_warner(aTHX_ WARN_PIPE, "Missing command in piped open");
235 if (strNE(name,"-") || num_svs)
237 TAINT_PROPER("piped open");
238 if (!num_svs && name[len-1] == '|') {
241 if (ckWARN(WARN_PIPE))
242 Perl_warner(aTHX_ WARN_PIPE, "Can't open bidirectional pipe");
250 fp = PerlProc_popen(name,mode);
252 else if (*type == IoTYPE_WRONLY) {
253 TAINT_PROPER("open");
255 if (*type == IoTYPE_WRONLY) {
256 /* Two IoTYPE_WRONLYs in a row make for an IoTYPE_APPEND. */
257 mode[0] = IoTYPE(io) = IoTYPE_APPEND;
280 if (!*name && supplied_fp)
284 for (; isSPACE(*name); name++) ;
289 gv = gv_fetchpv(name,FALSE,SVt_PVIO);
293 SETERRNO(EINVAL,SS$_IVCHAN);
298 PerlIO *fp = IoIFP(thatio);
299 /* Flush stdio buffer before dup. --mjd
300 * Unfortunately SEEK_CURing 0 seems to
301 * be optimized away on most platforms;
302 * only Solaris and Linux seem to flush
305 /* sfio fails to clear error on next
306 sfwrite, contrary to documentation.
308 if (PerlIO_seek(fp, 0, SEEK_CUR) == -1)
311 /* On the other hand, do all platforms
312 * take gracefully to flushing a read-only
313 * filehandle? Perhaps we should do
314 * fsetpos(src)+fgetpos(dst)? --nik */
316 fd = PerlIO_fileno(fp);
317 /* When dup()ing STDIN, STDOUT or STDERR
318 * explicitly set appropriate access mode */
319 if (IoIFP(thatio) == PerlIO_stdout()
320 || IoIFP(thatio) == PerlIO_stderr())
321 IoTYPE(io) = IoTYPE_WRONLY;
322 else if (IoIFP(thatio) == PerlIO_stdin())
323 IoTYPE(io) = IoTYPE_RDONLY;
324 /* When dup()ing a socket, say result is
326 else if (IoTYPE(thatio) == IoTYPE_SOCKET)
327 IoTYPE(io) = IoTYPE_SOCKET;
333 fd = PerlLIO_dup(fd);
336 if (!(fp = PerlIO_fdopen(fd,mode))) {
344 for (; isSPACE(*type); type++) ;
345 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
348 fp = PerlIO_stdout();
349 IoTYPE(io) = IoTYPE_STD;
352 fp = PerlIO_open((num_svs ? name : type), mode);
356 else if (*type == IoTYPE_RDONLY) {
358 for (type++; isSPACE(*type); type++) ;
369 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
373 IoTYPE(io) = IoTYPE_STD;
376 fp = PerlIO_open((num_svs ? name : type), mode);
378 else if ((num_svs && type[0] == IoTYPE_STD && type[1] == IoTYPE_PIPE) ||
379 (!num_svs && tend > type+1 && tend[-1] == IoTYPE_PIPE)) {
381 type += 2; /* skip over '-|' */
385 while (tend > type && isSPACE(tend[-1]))
388 for (; isSPACE(*type); type++) ;
392 if (*name == '\0') { /* command is missing 19990114 */
394 if (ckWARN(WARN_PIPE))
395 Perl_warner(aTHX_ WARN_PIPE, "Missing command in piped open");
399 if (strNE(name,"-") || num_svs)
401 TAINT_PROPER("piped open");
407 fp = PerlProc_popen(name,mode);
408 IoTYPE(io) = IoTYPE_PIPE;
414 IoTYPE(io) = IoTYPE_RDONLY;
416 for (; isSPACE(*name); name++) ;
422 if (strEQ(name,"-")) {
424 IoTYPE(io) = IoTYPE_STD;
427 fp = PerlIO_open(name,mode);
433 if (ckWARN(WARN_NEWLINE) && IoTYPE(io) == IoTYPE_RDONLY && strchr(name, '\n'))
434 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "open");
437 if (IoTYPE(io) && IoTYPE(io) != IoTYPE_PIPE && IoTYPE(io) != IoTYPE_STD) {
439 if (PerlLIO_fstat(PerlIO_fileno(fp),&PL_statbuf) < 0) {
440 (void)PerlIO_close(fp);
443 if (S_ISSOCK(PL_statbuf.st_mode))
444 IoTYPE(io) = IoTYPE_SOCKET; /* in case a socket was passed in to us */
448 !(PL_statbuf.st_mode & S_IFMT)
452 && IoTYPE(io) != IoTYPE_WRONLY /* Dups of STD* filehandles already have */
453 && IoTYPE(io) != IoTYPE_RDONLY /* type so they aren't marked as sockets */
454 ) { /* on OS's that return 0 on fstat()ed pipe */
456 Sock_size_t buflen = sizeof tmpbuf;
457 if (PerlSock_getsockname(PerlIO_fileno(fp), (struct sockaddr *)tmpbuf,
459 || errno != ENOTSOCK)
460 IoTYPE(io) = IoTYPE_SOCKET; /* some OS's return 0 on fstat()ed socket */
461 /* but some return 0 for streams too, sigh */
465 if (saveifp) { /* must use old fp? */
466 fd = PerlIO_fileno(saveifp);
468 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
469 if (saveofp != saveifp) { /* was a socket? */
470 PerlIO_close(saveofp);
475 if (fd != PerlIO_fileno(fp)) {
479 PerlLIO_dup2(PerlIO_fileno(fp), fd);
481 sv = *av_fetch(PL_fdpid,PerlIO_fileno(fp),TRUE);
482 (void)SvUPGRADE(sv, SVt_IV);
485 sv = *av_fetch(PL_fdpid,fd,TRUE);
487 (void)SvUPGRADE(sv, SVt_IV);
496 #if defined(HAS_FCNTL) && defined(F_SETFD)
498 int save_errno = errno;
499 fd = PerlIO_fileno(fp);
500 fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
506 /* Need to supply default type info from open.pm */
507 SV *layers = PL_curcop->cop_io;
511 type = SvPV(layers,len);
512 if (type && mode[0] != 'r') {
513 /* Skip to write part */
514 char *s = strchr(type,0);
515 if (s && (s-type) < len) {
520 else if (O_BINARY != O_TEXT) {
525 while (isSPACE(*type)) type++;
527 if (PerlIO_apply_layers(aTHX_ IoIFP(io),mode,type) != 0) {
533 IoFLAGS(io) &= ~IOf_NOLINE;
536 if (IoTYPE(io) == IoTYPE_SOCKET
537 || (IoTYPE(io) == IoTYPE_WRONLY && S_ISCHR(PL_statbuf.st_mode)) )
540 if (!(IoOFP(io) = PerlIO_fdopen(PerlIO_fileno(fp),mode))) {
546 if (PerlIO_apply_layers(aTHX_ IoOFP(io),mode,type) != 0) {
547 PerlIO_close(IoOFP(io));
563 IoTYPE(io) = savetype;
568 Perl_nextargv(pTHX_ register GV *gv)
571 #ifndef FLEXFILENAMES
580 PL_argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO);
581 if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
582 IoFLAGS(io) &= ~IOf_START;
584 if (!PL_argvout_stack)
585 PL_argvout_stack = newAV();
586 av_push(PL_argvout_stack, SvREFCNT_inc(PL_defoutgv));
589 if (PL_filemode & (S_ISUID|S_ISGID)) {
590 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
592 (void)fchmod(PL_lastfd,PL_filemode);
594 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
598 while (av_len(GvAV(gv)) >= 0) {
601 sv = av_shift(GvAV(gv));
603 sv_setsv(GvSV(gv),sv);
604 SvSETMAGIC(GvSV(gv));
605 PL_oldname = SvPVx(GvSV(gv), oldlen);
606 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,Nullfp)) {
608 TAINT_PROPER("inplace open");
609 if (oldlen == 1 && *PL_oldname == '-') {
610 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
611 return IoIFP(GvIOp(gv));
613 #ifndef FLEXFILENAMES
614 filedev = PL_statbuf.st_dev;
615 fileino = PL_statbuf.st_ino;
617 PL_filemode = PL_statbuf.st_mode;
618 fileuid = PL_statbuf.st_uid;
619 filegid = PL_statbuf.st_gid;
620 if (!S_ISREG(PL_filemode)) {
621 if (ckWARN_d(WARN_INPLACE))
622 Perl_warner(aTHX_ WARN_INPLACE,
623 "Can't do inplace edit: %s is not a regular file",
629 char *star = strchr(PL_inplace, '*');
631 char *begin = PL_inplace;
632 sv_setpvn(sv, "", 0);
634 sv_catpvn(sv, begin, star - begin);
635 sv_catpvn(sv, PL_oldname, oldlen);
637 } while ((star = strchr(begin, '*')));
642 sv_catpv(sv,PL_inplace);
644 #ifndef FLEXFILENAMES
645 if (PerlLIO_stat(SvPVX(sv),&PL_statbuf) >= 0
646 && PL_statbuf.st_dev == filedev
647 && PL_statbuf.st_ino == fileino
649 || (_djstat_fail_bits & _STFAIL_TRUENAME)!=0
653 if (ckWARN_d(WARN_INPLACE))
654 Perl_warner(aTHX_ WARN_INPLACE,
655 "Can't do inplace edit: %s would not be unique",
662 #if !defined(DOSISH) && !defined(__CYGWIN__)
663 if (PerlLIO_rename(PL_oldname,SvPVX(sv)) < 0) {
664 if (ckWARN_d(WARN_INPLACE))
665 Perl_warner(aTHX_ WARN_INPLACE,
666 "Can't rename %s to %s: %s, skipping file",
667 PL_oldname, SvPVX(sv), Strerror(errno) );
673 (void)PerlLIO_unlink(SvPVX(sv));
674 (void)PerlLIO_rename(PL_oldname,SvPVX(sv));
675 do_open(gv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,O_RDONLY,0,Nullfp);
678 (void)UNLINK(SvPVX(sv));
679 if (link(PL_oldname,SvPVX(sv)) < 0) {
680 if (ckWARN_d(WARN_INPLACE))
681 Perl_warner(aTHX_ WARN_INPLACE,
682 "Can't rename %s to %s: %s, skipping file",
683 PL_oldname, SvPVX(sv), Strerror(errno) );
687 (void)UNLINK(PL_oldname);
691 #if !defined(DOSISH) && !defined(AMIGAOS)
692 # ifndef VMS /* Don't delete; use automatic file versioning */
693 if (UNLINK(PL_oldname) < 0) {
694 if (ckWARN_d(WARN_INPLACE))
695 Perl_warner(aTHX_ WARN_INPLACE,
696 "Can't remove %s: %s, skipping file",
697 PL_oldname, Strerror(errno) );
703 Perl_croak(aTHX_ "Can't do inplace edit without backup");
707 sv_setpvn(sv,">",!PL_inplace);
708 sv_catpvn(sv,PL_oldname,oldlen);
709 SETERRNO(0,0); /* in case sprintf set errno */
711 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
712 O_WRONLY|O_CREAT|O_TRUNC,0,Nullfp))
714 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
715 O_WRONLY|O_CREAT|OPEN_EXCL,0666,Nullfp))
718 if (ckWARN_d(WARN_INPLACE))
719 Perl_warner(aTHX_ WARN_INPLACE, "Can't do inplace edit on %s: %s",
720 PL_oldname, Strerror(errno) );
724 setdefout(PL_argvoutgv);
725 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
726 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
728 (void)fchmod(PL_lastfd,PL_filemode);
730 # if !(defined(WIN32) && defined(__BORLANDC__))
731 /* Borland runtime creates a readonly file! */
732 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
735 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
737 (void)fchown(PL_lastfd,fileuid,filegid);
740 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
745 return IoIFP(GvIOp(gv));
749 if (ckWARN_d(WARN_INPLACE)) {
751 if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0
752 && !S_ISREG(PL_statbuf.st_mode))
754 Perl_warner(aTHX_ WARN_INPLACE,
755 "Can't do inplace edit: %s is not a regular file",
759 Perl_warner(aTHX_ WARN_INPLACE, "Can't open %s: %s",
760 PL_oldname, Strerror(eno));
764 if (io && (IoFLAGS(io) & IOf_ARGV))
765 IoFLAGS(io) |= IOf_START;
767 (void)do_close(PL_argvoutgv,FALSE);
768 if (io && (IoFLAGS(io) & IOf_ARGV)
769 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
771 GV *oldout = (GV*)av_pop(PL_argvout_stack);
773 SvREFCNT_dec(oldout);
776 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
783 Perl_do_pipe(pTHX_ SV *sv, GV *rgv, GV *wgv)
802 if (PerlProc_pipe(fd) < 0)
804 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r");
805 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w");
806 IoIFP(wstio) = IoOFP(wstio);
807 IoTYPE(rstio) = IoTYPE_RDONLY;
808 IoTYPE(wstio) = IoTYPE_WRONLY;
809 if (!IoIFP(rstio) || !IoOFP(wstio)) {
810 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
811 else PerlLIO_close(fd[0]);
812 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
813 else PerlLIO_close(fd[1]);
817 sv_setsv(sv,&PL_sv_yes);
821 sv_setsv(sv,&PL_sv_undef);
826 /* explicit renamed to avoid C++ conflict -- kja */
828 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
835 if (!gv || SvTYPE(gv) != SVt_PVGV) {
837 SETERRNO(EBADF,SS$_IVCHAN);
841 if (!io) { /* never opened */
844 if (ckWARN(WARN_UNOPENED)) /* no check for closed here */
845 report_evil_fh(gv, io, PL_op->op_type);
846 SETERRNO(EBADF,SS$_IVCHAN);
850 retval = io_close(io, not_implicit);
854 IoLINES_LEFT(io) = IoPAGE_LEN(io);
856 IoTYPE(io) = IoTYPE_CLOSED;
861 Perl_io_close(pTHX_ IO *io, bool not_implicit)
867 if (IoTYPE(io) == IoTYPE_PIPE) {
868 status = PerlProc_pclose(IoIFP(io));
870 STATUS_NATIVE_SET(status);
871 retval = (STATUS_POSIX == 0);
874 retval = (status != -1);
877 else if (IoTYPE(io) == IoTYPE_STD)
880 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
881 retval = (PerlIO_close(IoOFP(io)) != EOF);
882 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
885 retval = (PerlIO_close(IoIFP(io)) != EOF);
887 IoOFP(io) = IoIFP(io) = Nullfp;
889 else if (not_implicit) {
890 SETERRNO(EBADF,SS$_IVCHAN);
897 Perl_do_eof(pTHX_ GV *gv)
907 else if (ckWARN(WARN_IO)
908 && (IoTYPE(io) == IoTYPE_WRONLY || IoIFP(io) == PerlIO_stdout()
909 || IoIFP(io) == PerlIO_stderr()))
911 /* integrate to report_evil_fh()? */
914 SV* sv = sv_newmortal();
915 gv_efullname4(sv, gv, Nullch, FALSE);
916 name = SvPV_nolen(sv);
919 Perl_warner(aTHX_ WARN_IO,
920 "Filehandle %s opened only for output", name);
922 Perl_warner(aTHX_ WARN_IO,
923 "Filehandle opened only for output");
928 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
929 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
930 return FALSE; /* this is the most usual case */
933 ch = PerlIO_getc(IoIFP(io));
935 (void)PerlIO_ungetc(IoIFP(io),ch);
939 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
940 if (PerlIO_get_cnt(IoIFP(io)) < -1)
941 PerlIO_set_cnt(IoIFP(io),-1);
943 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
944 if (!nextargv(PL_argvgv)) /* get another fp handy */
948 return TRUE; /* normal fp, definitely end of file */
954 Perl_do_tell(pTHX_ GV *gv)
959 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
960 #ifdef ULTRIX_STDIO_BOTCH
962 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
964 return PerlIO_tell(fp);
968 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
969 report_evil_fh(gv, io, PL_op->op_type);
971 SETERRNO(EBADF,RMS$_IFI);
976 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
981 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
982 #ifdef ULTRIX_STDIO_BOTCH
984 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
986 return PerlIO_seek(fp, pos, whence) >= 0;
990 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
991 report_evil_fh(gv, io, PL_op->op_type);
993 SETERRNO(EBADF,RMS$_IFI);
998 Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
1001 register PerlIO *fp;
1003 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
1004 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
1007 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1008 report_evil_fh(gv, io, PL_op->op_type);
1010 SETERRNO(EBADF,RMS$_IFI);
1015 Perl_mode_from_discipline(pTHX_ SV *discp)
1017 int mode = O_BINARY;
1020 char *s = SvPV(discp,len);
1025 if (len > 3 && strnEQ(s+1, "raw", 3)
1026 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1035 if (len > 4 && strnEQ(s+1, "crlf", 4)
1036 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1045 goto fail_discipline;
1048 else if (isSPACE(*s)) {
1055 end = strchr(s+1, ':');
1058 Perl_croak(aTHX_ "Unknown discipline '%.*s'", end-s, s);
1066 Perl_do_binmode(pTHX_ PerlIO *fp, int iotype, int mode)
1069 # if defined(atarist) || defined(__MINT__)
1070 if (!PerlIO_flush(fp)) {
1071 if (mode & O_BINARY)
1072 ((FILE*)fp)->_flag |= _IOBIN;
1074 ((FILE*)fp)->_flag &= ~ _IOBIN;
1079 if (PerlLIO_setmode(PerlIO_fileno(fp), mode) != -1) {
1080 # if defined(WIN32) && defined(__BORLANDC__)
1081 /* The translation mode of the stream is maintained independent
1082 * of the translation mode of the fd in the Borland RTL (heavy
1083 * digging through their runtime sources reveal). User has to
1084 * set the mode explicitly for the stream (though they don't
1085 * document this anywhere). GSAR 97-5-24
1087 PerlIO_seek(fp,0L,0);
1088 if (mode & O_BINARY)
1089 ((FILE*)fp)->flags |= _F_BIN;
1091 ((FILE*)fp)->flags &= ~ _F_BIN;
1099 # if defined(USEMYBINMODE)
1100 if (my_binmode(fp, iotype, mode) != FALSE)
1110 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP)
1111 /* code courtesy of William Kucharski */
1114 I32 my_chsize(fd, length)
1115 I32 fd; /* file descriptor */
1116 Off_t length; /* length to set file to */
1119 struct stat filebuf;
1121 if (PerlLIO_fstat(fd, &filebuf) < 0)
1124 if (filebuf.st_size < length) {
1126 /* extend file length */
1128 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
1131 /* write a "0" byte */
1133 if ((PerlLIO_write(fd, "", 1)) != 1)
1137 /* truncate length */
1141 fl.l_start = length;
1142 fl.l_type = F_WRLCK; /* write lock on file space */
1145 * This relies on the UNDOCUMENTED F_FREESP argument to
1146 * fcntl(2), which truncates the file so that it ends at the
1147 * position indicated by fl.l_start.
1149 * Will minor miracles never cease?
1152 if (fcntl(fd, F_FREESP, &fl) < 0)
1159 #endif /* F_FREESP */
1162 Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
1164 register char *tmps;
1167 /* assuming fp is checked earlier */
1173 if (SvIOK(sv) && SvIVX(sv) != 0) {
1174 PerlIO_printf(fp, PL_ofmt, (NV)SvIVX(sv));
1175 return !PerlIO_error(fp);
1177 if ( (SvNOK(sv) && SvNVX(sv) != 0.0)
1178 || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) {
1179 PerlIO_printf(fp, PL_ofmt, SvNVX(sv));
1180 return !PerlIO_error(fp);
1183 switch (SvTYPE(sv)) {
1187 if (ckWARN(WARN_UNINITIALIZED))
1196 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
1198 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
1199 return !PerlIO_error(fp);
1204 /* XXX Fix this when the I/O disciplines arrive. XXX */
1206 sv_utf8_downgrade(sv, FALSE);
1208 tmps = SvPV(sv, len);
1211 /* To detect whether the process is about to overstep its
1212 * filesize limit we would need getrlimit(). We could then
1213 * also transparently raise the limit with setrlimit() --
1214 * but only until the system hard limit/the filesystem limit,
1215 * at which we would get EPERM. Note that when using buffered
1216 * io the write failure can be delayed until the flush/close. --jhi */
1217 if (len && (PerlIO_write(fp,tmps,len) == 0))
1219 return !PerlIO_error(fp);
1229 if (PL_op->op_flags & OPf_REF) {
1234 if (io && IoIFP(io)) {
1236 sv_setpv(PL_statname,"");
1237 PL_laststype = OP_STAT;
1238 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
1242 return PL_laststatval;
1243 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1244 report_evil_fh(gv, io, PL_op->op_type);
1246 sv_setpv(PL_statname,"");
1247 return (PL_laststatval = -1);
1255 if (SvTYPE(sv) == SVt_PVGV) {
1259 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
1266 sv_setpv(PL_statname, s);
1267 PL_laststype = OP_STAT;
1268 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1269 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
1270 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "stat");
1271 return PL_laststatval;
1281 if (PL_op->op_flags & OPf_REF) {
1283 if (cGVOP_gv == PL_defgv) {
1284 if (PL_laststype != OP_LSTAT)
1285 Perl_croak(aTHX_ "The stat preceding -l _ wasn't an lstat");
1286 return PL_laststatval;
1288 Perl_croak(aTHX_ "You can't use -l on a filehandle");
1291 PL_laststype = OP_LSTAT;
1295 sv_setpv(PL_statname,SvPV(sv, n_a));
1296 PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache);
1297 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
1298 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "lstat");
1299 return PL_laststatval;
1303 Perl_do_aexec(pTHX_ SV *really, register SV **mark, register SV **sp)
1305 return do_aexec5(really, mark, sp, 0, 0);
1309 Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1310 int fd, int do_report)
1312 #ifdef MACOS_TRADITIONAL
1313 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1321 New(401,PL_Argv, sp - mark + 1, char*);
1323 while (++mark <= sp) {
1325 *a++ = SvPVx(*mark, n_a);
1330 if (*PL_Argv[0] != '/') /* will execvp use PATH? */
1331 TAINT_ENV(); /* testing IFS here is overkill, probably */
1332 if (really && *(tmps = SvPV(really, n_a)))
1333 PerlProc_execvp(tmps,PL_Argv);
1335 PerlProc_execvp(PL_Argv[0],PL_Argv);
1336 if (ckWARN(WARN_EXEC))
1337 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1338 PL_Argv[0], Strerror(errno));
1342 PerlLIO_write(fd, (void*)&e, sizeof(int));
1352 Perl_do_execfree(pTHX)
1356 PL_Argv = Null(char **);
1364 #if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC) && !defined(MACOS_TRADITIONAL)
1367 Perl_do_exec(pTHX_ char *cmd)
1369 return do_exec3(cmd,0,0);
1373 Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report)
1379 while (*cmd && isSPACE(*cmd))
1382 /* save an extra exec if possible */
1385 if (strnEQ(cmd,PL_cshname,PL_cshlen) && strnEQ(cmd+PL_cshlen," -c",3)) {
1387 s = cmd+PL_cshlen+3;
1401 if (s[-1] == '\'') {
1403 PerlProc_execl(PL_cshname,"csh", flags,ncmd,(char*)0);
1411 /* see if there are shell metacharacters in it */
1413 if (*cmd == '.' && isSPACE(cmd[1]))
1416 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1419 for (s = cmd; *s && isALNUM(*s); s++) ; /* catch VAR=val gizmo */
1423 for (s = cmd; *s; s++) {
1424 if (*s != ' ' && !isALPHA(*s) && strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1425 if (*s == '\n' && !s[1]) {
1429 /* handle the 2>&1 construct at the end */
1430 if (*s == '>' && s[1] == '&' && s[2] == '1'
1431 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1432 && (!s[3] || isSPACE(s[3])))
1436 while (*t && isSPACE(*t))
1438 if (!*t && (dup2(1,2) != -1)) {
1444 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
1449 New(402,PL_Argv, (s - cmd) / 2 + 2, char*);
1450 PL_Cmd = savepvn(cmd, s-cmd);
1452 for (s = PL_Cmd; *s;) {
1453 while (*s && isSPACE(*s)) s++;
1456 while (*s && !isSPACE(*s)) s++;
1462 PerlProc_execvp(PL_Argv[0],PL_Argv);
1463 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1471 if (ckWARN(WARN_EXEC))
1472 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
1473 PL_Argv[0], Strerror(errno));
1475 PerlLIO_write(fd, (void*)&e, sizeof(int));
1484 #endif /* OS2 || WIN32 */
1487 Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
1492 register I32 tot = 0;
1495 SV **oldmark = mark;
1498 #define APPLY_TAINT_PROPER() \
1500 if (PL_tainted) { TAINT_PROPER(what); } \
1503 /* This is a first heuristic; it doesn't catch tainting magic. */
1505 while (++mark <= sp) {
1506 if (SvTAINTED(*mark)) {
1516 APPLY_TAINT_PROPER();
1519 APPLY_TAINT_PROPER();
1521 while (++mark <= sp) {
1522 char *name = SvPVx(*mark, n_a);
1523 APPLY_TAINT_PROPER();
1524 if (PerlLIO_chmod(name, val))
1532 APPLY_TAINT_PROPER();
1533 if (sp - mark > 2) {
1534 val = SvIVx(*++mark);
1535 val2 = SvIVx(*++mark);
1536 APPLY_TAINT_PROPER();
1538 while (++mark <= sp) {
1539 char *name = SvPVx(*mark, n_a);
1540 APPLY_TAINT_PROPER();
1541 if (PerlLIO_chown(name, val, val2))
1548 XXX Should we make lchown() directly available from perl?
1549 For now, we'll let Configure test for HAS_LCHOWN, but do
1550 nothing in the core.
1556 APPLY_TAINT_PROPER();
1559 s = SvPVx(*++mark, n_a);
1561 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1563 if (!(val = whichsig(s)))
1564 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
1568 APPLY_TAINT_PROPER();
1571 /* kill() doesn't do process groups (job trees?) under VMS */
1572 if (val < 0) val = -val;
1573 if (val == SIGKILL) {
1574 # include <starlet.h>
1575 /* Use native sys$delprc() to insure that target process is
1576 * deleted; supervisor-mode images don't pay attention to
1577 * CRTL's emulation of Unix-style signals and kill()
1579 while (++mark <= sp) {
1580 I32 proc = SvIVx(*mark);
1581 register unsigned long int __vmssts;
1582 APPLY_TAINT_PROPER();
1583 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1587 case SS$_NOSUCHNODE:
1588 SETERRNO(ESRCH,__vmssts);
1591 SETERRNO(EPERM,__vmssts);
1594 SETERRNO(EVMSERR,__vmssts);
1603 while (++mark <= sp) {
1604 I32 proc = SvIVx(*mark);
1605 APPLY_TAINT_PROPER();
1607 if (PerlProc_killpg(proc,val)) /* BSD */
1609 if (PerlProc_kill(-proc,val)) /* SYSV */
1615 while (++mark <= sp) {
1616 I32 proc = SvIVx(*mark);
1617 APPLY_TAINT_PROPER();
1618 if (PerlProc_kill(proc, val))
1626 APPLY_TAINT_PROPER();
1628 while (++mark <= sp) {
1629 s = SvPVx(*mark, n_a);
1630 APPLY_TAINT_PROPER();
1631 if (PL_euid || PL_unsafe) {
1635 else { /* don't let root wipe out directories without -U */
1636 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1648 APPLY_TAINT_PROPER();
1649 if (sp - mark > 2) {
1650 #if defined(I_UTIME) || defined(VMS)
1651 struct utimbuf utbuf;
1659 Zero(&utbuf, sizeof utbuf, char);
1661 utbuf.actime = (Time_t)SvNVx(*++mark); /* time accessed */
1662 utbuf.modtime = (Time_t)SvNVx(*++mark); /* time modified */
1664 utbuf.actime = (Time_t)SvIVx(*++mark); /* time accessed */
1665 utbuf.modtime = (Time_t)SvIVx(*++mark); /* time modified */
1667 APPLY_TAINT_PROPER();
1669 while (++mark <= sp) {
1670 char *name = SvPVx(*mark, n_a);
1671 APPLY_TAINT_PROPER();
1672 if (PerlLIO_utime(name, &utbuf))
1683 #undef APPLY_TAINT_PROPER
1686 /* Do the permissions allow some operation? Assumes statcache already set. */
1687 #ifndef VMS /* VMS' cando is in vms.c */
1689 Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register Stat_t *statbufp)
1690 /* Note: we use `effective' both for uids and gids.
1691 * Here we are betting on Uid_t being equal or wider than Gid_t. */
1694 /* [Comments and code from Len Reed]
1695 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1696 * to write-protected files. The execute permission bit is set
1697 * by the Miscrosoft C library stat() function for the following:
1702 * All files and directories are readable.
1703 * Directories and special files, e.g. "CON", cannot be
1705 * [Comment by Tom Dinger -- a directory can have the write-protect
1706 * bit set in the file system, but DOS permits changes to
1707 * the directory anyway. In addition, all bets are off
1708 * here for networked software, such as Novell and
1712 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1713 * too so it will actually look into the files for magic numbers
1715 return (mode & statbufp->st_mode) ? TRUE : FALSE;
1717 #else /* ! DOSISH */
1718 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
1719 if (mode == S_IXUSR) {
1720 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1724 return TRUE; /* root reads and writes anything */
1727 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1728 if (statbufp->st_mode & mode)
1729 return TRUE; /* ok as "user" */
1731 else if (ingroup(statbufp->st_gid,effective)) {
1732 if (statbufp->st_mode & mode >> 3)
1733 return TRUE; /* ok as "group" */
1735 else if (statbufp->st_mode & mode >> 6)
1736 return TRUE; /* ok as "other" */
1738 #endif /* ! DOSISH */
1743 Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective)
1745 #ifdef MACOS_TRADITIONAL
1746 /* This is simply not correct for AppleShare, but fix it yerself. */
1749 if (testgid == (effective ? PL_egid : PL_gid))
1751 #ifdef HAS_GETGROUPS
1756 Groups_t gary[NGROUPS];
1759 anum = getgroups(NGROUPS,gary);
1761 if (gary[anum] == testgid)
1769 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1772 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
1778 key = (key_t)SvNVx(*++mark);
1779 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1780 flags = SvIVx(*++mark);
1786 return msgget(key, flags);
1790 return semget(key, n, flags);
1794 return shmget(key, n, flags);
1796 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1798 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1801 return -1; /* should never happen */
1805 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
1810 I32 id, n, cmd, infosize, getinfo;
1813 id = SvIVx(*++mark);
1814 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1815 cmd = SvIVx(*++mark);
1818 getinfo = (cmd == IPC_STAT);
1824 if (cmd == IPC_STAT || cmd == IPC_SET)
1825 infosize = sizeof(struct msqid_ds);
1830 if (cmd == IPC_STAT || cmd == IPC_SET)
1831 infosize = sizeof(struct shmid_ds);
1837 if (cmd == IPC_STAT || cmd == IPC_SET)
1838 infosize = sizeof(struct semid_ds);
1839 else if (cmd == GETALL || cmd == SETALL)
1841 struct semid_ds semds;
1843 #ifdef EXTRA_F_IN_SEMUN_BUF
1844 semun.buff = &semds;
1848 getinfo = (cmd == GETALL);
1849 if (Semctl(id, 0, IPC_STAT, semun) == -1)
1851 infosize = semds.sem_nsems * sizeof(short);
1852 /* "short" is technically wrong but much more portable
1853 than guessing about u_?short(_t)? */
1856 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1860 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1862 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1871 SvPV_force(astr, len);
1872 a = SvGROW(astr, infosize+1);
1876 a = SvPV(astr, len);
1877 if (len != infosize)
1878 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
1887 a = INT2PTR(char *,i); /* ouch */
1894 ret = msgctl(id, cmd, (struct msqid_ds *)a);
1900 union semun unsemds;
1902 #ifdef EXTRA_F_IN_SEMUN_BUF
1903 unsemds.buff = (struct semid_ds *)a;
1905 unsemds.buf = (struct semid_ds *)a;
1907 ret = Semctl(id, n, cmd, unsemds);
1909 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
1916 ret = shmctl(id, cmd, (struct shmid_ds *)a);
1920 if (getinfo && ret >= 0) {
1921 SvCUR_set(astr, infosize);
1922 *SvEND(astr) = '\0';
1929 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
1935 I32 id, msize, flags;
1938 id = SvIVx(*++mark);
1940 flags = SvIVx(*++mark);
1941 mbuf = SvPV(mstr, len);
1942 if ((msize = len - sizeof(long)) < 0)
1943 Perl_croak(aTHX_ "Arg too short for msgsnd");
1945 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
1947 Perl_croak(aTHX_ "msgsnd not implemented");
1952 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
1959 I32 id, msize, flags, ret;
1962 id = SvIVx(*++mark);
1964 /* suppress warning when reading into undef var --jhi */
1966 sv_setpvn(mstr, "", 0);
1967 msize = SvIVx(*++mark);
1968 mtype = (long)SvIVx(*++mark);
1969 flags = SvIVx(*++mark);
1970 SvPV_force(mstr, len);
1971 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
1974 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
1976 SvCUR_set(mstr, sizeof(long)+ret);
1977 *SvEND(mstr) = '\0';
1978 #ifndef INCOMPLETE_TAINTS
1979 /* who knows who has been playing with this message? */
1985 Perl_croak(aTHX_ "msgrcv not implemented");
1990 Perl_do_semop(pTHX_ SV **mark, SV **sp)
1999 id = SvIVx(*++mark);
2001 opbuf = SvPV(opstr, opsize);
2002 if (opsize < sizeof(struct sembuf)
2003 || (opsize % sizeof(struct sembuf)) != 0) {
2004 SETERRNO(EINVAL,LIB$_INVARG);
2008 return semop(id, (struct sembuf *)opbuf, opsize/sizeof(struct sembuf));
2010 Perl_croak(aTHX_ "semop not implemented");
2015 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
2021 I32 id, mpos, msize;
2023 struct shmid_ds shmds;
2025 id = SvIVx(*++mark);
2027 mpos = SvIVx(*++mark);
2028 msize = SvIVx(*++mark);
2030 if (shmctl(id, IPC_STAT, &shmds) == -1)
2032 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
2033 SETERRNO(EFAULT,SS$_ACCVIO); /* can't do as caller requested */
2036 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
2037 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2039 if (optype == OP_SHMREAD) {
2040 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2042 sv_setpvn(mstr, "", 0);
2043 SvPV_force(mstr, len);
2044 mbuf = SvGROW(mstr, msize+1);
2046 Copy(shm + mpos, mbuf, msize, char);
2047 SvCUR_set(mstr, msize);
2048 *SvEND(mstr) = '\0';
2050 #ifndef INCOMPLETE_TAINTS
2051 /* who knows who has been playing with this shared memory? */
2058 mbuf = SvPV(mstr, len);
2059 if ((n = len) > msize)
2061 Copy(mbuf, shm + mpos, n, char);
2063 memzero(shm + mpos + n, msize - n);
2067 Perl_croak(aTHX_ "shm I/O not implemented");
2071 #endif /* SYSV IPC */
2073 #ifdef SOCKS_64BIT_BUG
2076 ** getc and ungetc wrappers for the 64 bit problems with SOCKS 5 support
2077 ** Workaround to the problem, that SOCKS maps a socket 'getc' to revc
2078 ** without checking the ungetc buffer.
2081 /* Not very thread-safe? */
2082 static S64_IOB *s64_buffer = (S64_IOB *) NULL;
2084 /* initialize the buffer area */
2085 /* required after a fork(2) call in order to remove side effects */
2086 void Perl_do_s64_init_buffer() {
2087 s64_buffer = (S64_IOB *) NULL;
2090 /* get a buffered stream pointer */
2091 static S64_IOB *S_s64_get_buffer( PerlIO *fp) {
2092 S64_IOB *ptr = s64_buffer;
2093 while( ptr && ptr->fp != fp)
2098 /* create a buffered stream pointer */
2099 static S64_IOB *S_s64_create_buffer( PerlIO *f) {
2100 S64_IOB *ptr = malloc( sizeof( S64_IOB));
2103 ptr->cnt = ptr->size = 0;
2104 ptr->buffer = (int *) NULL;
2105 ptr->next = s64_buffer;
2106 ptr->last = (S64_IOB *) NULL;
2107 if( s64_buffer) s64_buffer->last = ptr;
2113 /* delete a buffered stream pointer */
2114 void Perl_do_s64_delete_buffer( PerlIO *f) {
2115 S64_IOB *ptr = S_s64_get_buffer(f);
2117 /* fix the stream pointer according to the bytes buffered */
2118 /* required, if this is called in a seek-context */
2119 if( ptr->cnt) fseek(f,-ptr->cnt,SEEK_CUR);
2120 if( ptr->buffer) free( ptr->buffer);
2122 ptr->last->next = ptr->next;
2124 s64_buffer = ptr->next;
2129 /* internal buffer management */
2130 #define _S64_BUFFER_SIZE 32
2131 static int S_s64_malloc( S64_IOB *ptr) {
2134 ptr->buffer = (int *) calloc( _S64_BUFFER_SIZE, sizeof( int));
2135 ptr->size = ptr->cnt = 0;
2137 ptr->buffer = (int *) realloc( ptr->buffer, ptr->size + _S64_BUFFER_SIZE);
2143 ptr->size += _S64_BUFFER_SIZE;
2151 /* SOCKS 64 bit getc replacement */
2152 int Perl_do_s64_getc( PerlIO *f) {
2153 S64_IOB *ptr = S_s64_get_buffer(f);
2156 return( ptr->buffer[--ptr->cnt]);
2161 /* SOCKS 64 bit ungetc replacement */
2162 int Perl_do_s64_ungetc( int ch, PerlIO *f) {
2163 S64_IOB *ptr = S_s64_get_buffer(f);
2165 if( !ptr) ptr = S_s64_create_buffer(f);
2166 if( !ptr) return( EOF);
2167 if( !ptr->buffer || (ptr->buffer && ptr->cnt >= ptr->size))
2168 if( !S_s64_malloc( ptr)) return( EOF);
2169 ptr->buffer[ptr->cnt++] = ch;
2174 /* SOCKS 64 bit fread replacement */
2175 SSize_t Perl_do_s64_fread(void *buf, SSize_t count, PerlIO* f) {
2177 char *bufptr = (char *) buf;
2178 S64_IOB *ptr = S_s64_get_buffer(f);
2180 while( ptr->cnt && count) {
2181 *bufptr++ = ptr->buffer[--ptr->cnt];
2186 len += (SSize_t)fread(bufptr,1,count,f);
2191 /* SOCKS 64 bit fseek replacement */
2192 int Perl_do_s64_seek(PerlIO* f, Off_t offset, int whence) {
2193 S64_IOB *ptr = S_s64_get_buffer(f);
2195 /* Simply clear the buffer and seek if the position is absolute */
2196 if( SEEK_SET == whence || SEEK_END == whence) {
2197 if( ptr) ptr->cnt = 0;
2199 /* In case of relative positioning clear the buffer and calculate */
2200 /* a fixed offset */
2201 } else if( SEEK_CUR == whence) {
2203 offset -= (Off_t)ptr->cnt;
2208 /* leave out buffer untouched otherwise, because fseek will fail */
2210 return( fseeko( f, offset, whence));
2213 /* SOCKS 64 bit ftell replacement */
2214 Off_t Perl_do_s64_tell(PerlIO* f) {
2216 S64_IOB *ptr = S_s64_get_buffer(f);
2219 return( ftello(f) - offset);