2 * perlio.c Copyright (c) 1996-2005, Nick Ing-Simmons You may distribute
3 * under the terms of either the GNU General Public License or the
4 * Artistic License, as specified in the README file.
8 * Hour after hour for nearly three weary days he had jogged up and down,
9 * over passes, and through long dales, and across many streams.
12 /* This file contains the functions needed to implement PerlIO, which
13 * is Perl's private replacement for the C stdio library. This is used
14 * by default unless you compile with -Uuseperlio or run with
15 * PERLIO=:stdio (but don't do this unless you know what you're doing)
19 * If we have ActivePerl-like PERL_IMPLICIT_SYS then we need a dTHX to get
20 * at the dispatch tables, even when we do not need it for other reasons.
21 * Invent a dSYS macro to abstract this out
23 #ifdef PERL_IMPLICIT_SYS
36 #define PERLIO_NOT_STDIO 0
37 #if !defined(PERLIO_IS_STDIO) && !defined(USE_SFIO)
43 * This file provides those parts of PerlIO abstraction
44 * which are not #defined in perlio.h.
45 * Which these are depends on various Configure #ifdef's
49 #define PERL_IN_PERLIO_C
52 #ifdef PERL_IMPLICIT_CONTEXT
59 #define PERLIO_MAX_REFCOUNTABLE_FD 2048
62 /* Missing proto on LynxOS */
66 /* Call the callback or PerlIOBase, and return failure. */
67 #define Perl_PerlIO_or_Base(f, callback, base, failure, args) \
68 if (PerlIOValid(f)) { \
69 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;\
70 if (tab && tab->callback) \
71 return (*tab->callback) args; \
73 return PerlIOBase_ ## base args; \
76 SETERRNO(EBADF, SS_IVCHAN); \
79 /* Call the callback or fail, and return failure. */
80 #define Perl_PerlIO_or_fail(f, callback, failure, args) \
81 if (PerlIOValid(f)) { \
82 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;\
83 if (tab && tab->callback) \
84 return (*tab->callback) args; \
85 SETERRNO(EINVAL, LIB_INVARG); \
88 SETERRNO(EBADF, SS_IVCHAN); \
91 /* Call the callback or PerlIOBase, and be void. */
92 #define Perl_PerlIO_or_Base_void(f, callback, base, args) \
93 if (PerlIOValid(f)) { \
94 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;\
95 if (tab && tab->callback) \
96 (*tab->callback) args; \
98 PerlIOBase_ ## base args; \
101 SETERRNO(EBADF, SS_IVCHAN)
103 /* Call the callback or fail, and be void. */
104 #define Perl_PerlIO_or_fail_void(f, callback, args) \
105 if (PerlIOValid(f)) { \
106 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;\
107 if (tab && tab->callback) \
108 (*tab->callback) args; \
110 SETERRNO(EINVAL, LIB_INVARG); \
113 SETERRNO(EBADF, SS_IVCHAN)
117 perlsio_binmode(FILE *fp, int iotype, int mode)
120 * This used to be contents of do_binmode in doio.c
123 # if defined(atarist) || defined(__MINT__)
126 ((FILE *) fp)->_flag |= _IOBIN;
128 ((FILE *) fp)->_flag &= ~_IOBIN;
135 if (PerlLIO_setmode(fp, mode) != -1) {
137 if (PerlLIO_setmode(fileno(fp), mode) != -1) {
139 # if defined(WIN32) && defined(__BORLANDC__)
141 * The translation mode of the stream is maintained independent
143 * the translation mode of the fd in the Borland RTL (heavy
144 * digging through their runtime sources reveal). User has to
146 * the mode explicitly for the stream (though they don't
148 * this anywhere). GSAR 97-5-24
154 fp->flags &= ~_F_BIN;
162 # if defined(USEMYBINMODE)
164 if (my_binmode(fp, iotype, mode) != FALSE)
170 PERL_UNUSED_ARG(iotype);
171 PERL_UNUSED_ARG(mode);
179 #define O_ACCMODE 3 /* Assume traditional implementation */
183 PerlIO_intmode2str(int rawmode, char *mode, int *writing)
185 const int result = rawmode & O_ACCMODE;
190 ptype = IoTYPE_RDONLY;
193 ptype = IoTYPE_WRONLY;
201 *writing = (result != O_RDONLY);
203 if (result == O_RDONLY) {
207 else if (rawmode & O_APPEND) {
209 if (result != O_WRONLY)
214 if (result == O_WRONLY)
221 if (rawmode & O_BINARY)
227 #ifndef PERLIO_LAYERS
229 PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names)
231 if (!names || !*names
232 || strEQ(names, ":crlf")
233 || strEQ(names, ":raw")
234 || strEQ(names, ":bytes")
238 Perl_croak(aTHX_ "Cannot apply \"%s\" in non-PerlIO perl", names);
246 PerlIO_destruct(pTHX)
251 PerlIO_binmode(pTHX_ PerlIO *fp, int iotype, int mode, const char *names)
254 PERL_UNUSED_ARG(iotype);
255 PERL_UNUSED_ARG(mode);
256 PERL_UNUSED_ARG(names);
259 return perlsio_binmode(fp, iotype, mode);
264 PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
266 #if defined(PERL_MICRO) || defined(__SYMBIAN32__)
269 #ifdef PERL_IMPLICIT_SYS
270 return PerlSIO_fdupopen(f);
273 return win32_fdupopen(f);
276 const int fd = PerlLIO_dup(PerlIO_fileno(f));
280 const int omode = djgpp_get_stream_mode(f);
282 const int omode = fcntl(fd, F_GETFL);
284 PerlIO_intmode2str(omode,mode,NULL);
285 /* the r+ is a hack */
286 return PerlIO_fdopen(fd, mode);
291 SETERRNO(EBADF, SS_IVCHAN);
301 * De-mux PerlIO_openn() into fdopen, freopen and fopen type entries
305 PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd,
306 int imode, int perm, PerlIO *old, int narg, SV **args)
310 Perl_croak(aTHX_ "More than one argument to open");
312 if (*args == &PL_sv_undef)
313 return PerlIO_tmpfile();
315 const char *name = SvPV_nolen_const(*args);
316 if (*mode == IoTYPE_NUMERIC) {
317 fd = PerlLIO_open3(name, imode, perm);
319 return PerlIO_fdopen(fd, mode + 1);
322 return PerlIO_reopen(name, mode, old);
325 return PerlIO_open(name, mode);
330 return PerlIO_fdopen(fd, (char *) mode);
335 XS(XS_PerlIO__Layer__find)
339 Perl_croak(aTHX_ "Usage class->find(name[,load])");
341 const char * const name = SvPV_nolen_const(ST(1));
342 ST(0) = (strEQ(name, "crlf")
343 || strEQ(name, "raw")) ? &PL_sv_yes : &PL_sv_undef;
350 Perl_boot_core_PerlIO(pTHX)
352 newXS("PerlIO::Layer::find", XS_PerlIO__Layer__find, __FILE__);
358 #ifdef PERLIO_IS_STDIO
364 * Does nothing (yet) except force this file to be included in perl
365 * binary. That allows this file to force inclusion of other functions
366 * that may be required by loadable extensions e.g. for
367 * FileHandle::tmpfile
371 #undef PerlIO_tmpfile
378 #else /* PERLIO_IS_STDIO */
386 * This section is just to make sure these functions get pulled in from
390 #undef PerlIO_tmpfile
401 * Force this file to be included in perl binary. Which allows this
402 * file to force inclusion of other functions that may be required by
403 * loadable extensions e.g. for FileHandle::tmpfile
407 * Hack sfio does its own 'autoflush' on stdout in common cases. Flush
408 * results in a lot of lseek()s to regular files and lot of small
411 sfset(sfstdout, SF_SHARE, 0);
414 /* This is not the reverse of PerlIO_exportFILE(), PerlIO_releaseFILE() is. */
416 PerlIO_importFILE(FILE *stdio, const char *mode)
418 const int fd = fileno(stdio);
419 if (!mode || !*mode) {
422 return PerlIO_fdopen(fd, mode);
426 PerlIO_findFILE(PerlIO *pio)
428 const int fd = PerlIO_fileno(pio);
429 FILE * const f = fdopen(fd, "r+");
431 if (!f && errno == EINVAL)
433 if (!f && errno == EINVAL)
440 /*======================================================================================*/
442 * Implement all the PerlIO interface ourselves.
448 * We _MUST_ have <unistd.h> if we are using lseek() and may have large
455 #include <sys/mman.h>
459 PerlIO_debug(const char *fmt, ...)
464 if (!PL_perlio_debug_fd && !PL_tainting && PL_uid == PL_euid && PL_gid == PL_egid) {
465 const char * const s = PerlEnv_getenv("PERLIO_DEBUG");
467 PL_perlio_debug_fd = PerlLIO_open3(s, O_WRONLY | O_CREAT | O_APPEND, 0666);
469 PL_perlio_debug_fd = -1;
471 if (PL_perlio_debug_fd > 0) {
474 const char * const s = CopFILE(PL_curcop);
475 /* Use fixed buffer as sv_catpvf etc. needs SVs */
477 const STRLEN len = my_sprintf(buffer, "%.40s:%" IVdf " ", s ? s : "(none)", (IV) CopLINE(PL_curcop));
478 const STRLEN len2 = vsprintf(buffer+len, fmt, ap);
479 PerlLIO_write(PL_perlio_debug_fd, buffer, len + len2);
481 const char *s = CopFILE(PL_curcop);
483 SV * const sv = newSVpvn("", 0);
484 Perl_sv_catpvf(aTHX_ sv, "%s:%" IVdf " ", s ? s : "(none)",
485 (IV) CopLINE(PL_curcop));
486 Perl_sv_vcatpvf(aTHX_ sv, fmt, &ap);
488 s = SvPV_const(sv, len);
489 PerlLIO_write(PL_perlio_debug_fd, s, len);
496 /*--------------------------------------------------------------------------------------*/
499 * Inner level routines
503 * Table of pointers to the PerlIO structs (malloc'ed)
505 #define PERLIO_TABLE_SIZE 64
508 PerlIO_allocate(pTHX)
511 * Find a free slot in the table, allocating new table as necessary
516 while ((f = *last)) {
518 last = (PerlIO **) (f);
519 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
525 Newxz(f,PERLIO_TABLE_SIZE,PerlIO);
533 #undef PerlIO_fdupopen
535 PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
537 if (PerlIOValid(f)) {
538 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
539 PerlIO_debug("fdupopen f=%p param=%p\n",(void*)f,(void*)param);
541 return (*tab->Dup)(aTHX_ PerlIO_allocate(aTHX), f, param, flags);
543 return PerlIOBase_dup(aTHX_ PerlIO_allocate(aTHX), f, param, flags);
547 SETERRNO(EBADF, SS_IVCHAN);
553 PerlIO_cleantable(pTHX_ PerlIO **tablep)
555 PerlIO * const table = *tablep;
558 PerlIO_cleantable(aTHX_(PerlIO **) & (table[0]));
559 for (i = PERLIO_TABLE_SIZE - 1; i > 0; i--) {
560 PerlIO * const f = table + i;
572 PerlIO_list_alloc(pTHX)
575 Newxz(list, 1, PerlIO_list_t);
581 PerlIO_list_free(pTHX_ PerlIO_list_t *list)
584 if (--list->refcnt == 0) {
587 for (i = 0; i < list->cur; i++) {
588 if (list->array[i].arg)
589 SvREFCNT_dec(list->array[i].arg);
591 Safefree(list->array);
599 PerlIO_list_push(pTHX_ PerlIO_list_t *list, PerlIO_funcs *funcs, SV *arg)
602 if (list->cur >= list->len) {
605 Renew(list->array, list->len, PerlIO_pair_t);
607 Newx(list->array, list->len, PerlIO_pair_t);
609 p = &(list->array[list->cur++]);
611 if ((p->arg = arg)) {
612 (void)SvREFCNT_inc(arg);
617 PerlIO_clone_list(pTHX_ PerlIO_list_t *proto, CLONE_PARAMS *param)
619 PerlIO_list_t *list = (PerlIO_list_t *) NULL;
622 list = PerlIO_list_alloc(aTHX);
623 for (i=0; i < proto->cur; i++) {
625 if (proto->array[i].arg)
626 arg = PerlIO_sv_dup(aTHX_ proto->array[i].arg,param);
627 PerlIO_list_push(aTHX_ list, proto->array[i].funcs, arg);
634 PerlIO_clone(pTHX_ PerlInterpreter *proto, CLONE_PARAMS *param)
637 PerlIO **table = &proto->Iperlio;
640 PL_known_layers = PerlIO_clone_list(aTHX_ proto->Iknown_layers, param);
641 PL_def_layerlist = PerlIO_clone_list(aTHX_ proto->Idef_layerlist, param);
642 PerlIO_allocate(aTHX); /* root slot is never used */
643 PerlIO_debug("Clone %p from %p\n",aTHX,proto);
644 while ((f = *table)) {
646 table = (PerlIO **) (f++);
647 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
649 (void) fp_dup(f, 0, param);
655 PERL_UNUSED_ARG(proto);
656 PERL_UNUSED_ARG(param);
661 PerlIO_destruct(pTHX)
663 PerlIO **table = &PL_perlio;
666 PerlIO_debug("Destruct %p\n",aTHX);
668 while ((f = *table)) {
670 table = (PerlIO **) (f++);
671 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
675 if (l->tab->kind & PERLIO_K_DESTRUCT) {
676 PerlIO_debug("Destruct popping %s\n", l->tab->name);
690 PerlIO_pop(pTHX_ PerlIO *f)
692 const PerlIOl *l = *f;
694 PerlIO_debug("PerlIO_pop f=%p %s\n", (void*)f, l->tab->name);
695 if (l->tab->Popped) {
697 * If popped returns non-zero do not free its layer structure
698 * it has either done so itself, or it is shared and still in
701 if ((*l->tab->Popped) (aTHX_ f) != 0)
709 /* Return as an array the stack of layers on a filehandle. Note that
710 * the stack is returned top-first in the array, and there are three
711 * times as many array elements as there are layers in the stack: the
712 * first element of a layer triplet is the name, the second one is the
713 * arguments, and the third one is the flags. */
716 PerlIO_get_layers(pTHX_ PerlIO *f)
718 AV * const av = newAV();
720 if (PerlIOValid(f)) {
721 PerlIOl *l = PerlIOBase(f);
724 SV * const name = l->tab && l->tab->name ?
725 newSVpv(l->tab->name, 0) : &PL_sv_undef;
726 SV * const arg = l->tab && l->tab->Getarg ?
727 (*l->tab->Getarg)(aTHX_ &l, 0, 0) : &PL_sv_undef;
730 av_push(av, newSViv((IV)l->flags));
738 /*--------------------------------------------------------------------------------------*/
740 * XS Interface for perl code
744 PerlIO_find_layer(pTHX_ const char *name, STRLEN len, int load)
748 if ((SSize_t) len <= 0)
750 for (i = 0; i < PL_known_layers->cur; i++) {
751 PerlIO_funcs * const f = PL_known_layers->array[i].funcs;
752 if (memEQ(f->name, name, len) && f->name[len] == 0) {
753 PerlIO_debug("%.*s => %p\n", (int) len, name, (void*)f);
757 if (load && PL_subname && PL_def_layerlist
758 && PL_def_layerlist->cur >= 2) {
759 if (PL_in_load_module) {
760 Perl_croak(aTHX_ "Recursive call to Perl_load_module in PerlIO_find_layer");
763 SV * const pkgsv = newSVpvn("PerlIO", 6);
764 SV * const layer = newSVpvn(name, len);
765 CV * const cv = get_cv("PerlIO::Layer::NoWarnings", FALSE);
767 SAVEINT(PL_in_load_module);
769 SAVEGENERICSV(PL_warnhook);
770 (void)SvREFCNT_inc(cv);
771 PL_warnhook = (SV *) cv;
775 * The two SVs are magically freed by load_module
777 Perl_load_module(aTHX_ 0, pkgsv, Nullsv, layer, Nullsv);
780 return PerlIO_find_layer(aTHX_ name, len, 0);
783 PerlIO_debug("Cannot find %.*s\n", (int) len, name);
787 #ifdef USE_ATTRIBUTES_FOR_PERLIO
790 perlio_mg_set(pTHX_ SV *sv, MAGIC *mg)
793 IO * const io = GvIOn((GV *) SvRV(sv));
794 PerlIO * const ifp = IoIFP(io);
795 PerlIO * const ofp = IoOFP(io);
796 Perl_warn(aTHX_ "set %" SVf " %p %p %p", sv, io, ifp, ofp);
802 perlio_mg_get(pTHX_ SV *sv, MAGIC *mg)
805 IO * const io = GvIOn((GV *) SvRV(sv));
806 PerlIO * const ifp = IoIFP(io);
807 PerlIO * const ofp = IoOFP(io);
808 Perl_warn(aTHX_ "get %" SVf " %p %p %p", sv, io, ifp, ofp);
814 perlio_mg_clear(pTHX_ SV *sv, MAGIC *mg)
816 Perl_warn(aTHX_ "clear %" SVf, sv);
821 perlio_mg_free(pTHX_ SV *sv, MAGIC *mg)
823 Perl_warn(aTHX_ "free %" SVf, sv);
827 MGVTBL perlio_vtab = {
835 XS(XS_io_MODIFY_SCALAR_ATTRIBUTES)
838 SV * const sv = SvRV(ST(1));
839 AV * const av = newAV();
843 sv_magic(sv, (SV *) av, PERL_MAGIC_ext, NULL, 0);
845 mg = mg_find(sv, PERL_MAGIC_ext);
846 mg->mg_virtual = &perlio_vtab;
848 Perl_warn(aTHX_ "attrib %" SVf, sv);
849 for (i = 2; i < items; i++) {
851 const char * const name = SvPV_const(ST(i), len);
852 SV * const layer = PerlIO_find_layer(aTHX_ name, len, 1);
854 av_push(av, SvREFCNT_inc(layer));
865 #endif /* USE_ATTIBUTES_FOR_PERLIO */
868 PerlIO_tab_sv(pTHX_ PerlIO_funcs *tab)
870 HV * const stash = gv_stashpv("PerlIO::Layer", TRUE);
871 SV * const sv = sv_bless(newRV_noinc(newSViv(PTR2IV(tab))), stash);
875 XS(XS_PerlIO__Layer__NoWarnings)
877 /* This is used as a %SIG{__WARN__} handler to supress warnings
878 during loading of layers.
882 PerlIO_debug("warning:%s\n",SvPV_nolen_const(ST(0)));
886 XS(XS_PerlIO__Layer__find)
890 Perl_croak(aTHX_ "Usage class->find(name[,load])");
893 const char * const name = SvPV_const(ST(1), len);
894 const bool load = (items > 2) ? SvTRUE(ST(2)) : 0;
895 PerlIO_funcs * const layer = PerlIO_find_layer(aTHX_ name, len, load);
897 (layer) ? sv_2mortal(PerlIO_tab_sv(aTHX_ layer)) :
904 PerlIO_define_layer(pTHX_ PerlIO_funcs *tab)
906 if (!PL_known_layers)
907 PL_known_layers = PerlIO_list_alloc(aTHX);
908 PerlIO_list_push(aTHX_ PL_known_layers, tab, Nullsv);
909 PerlIO_debug("define %s %p\n", tab->name, (void*)tab);
913 PerlIO_parse_layers(pTHX_ PerlIO_list_t *av, const char *names)
916 const char *s = names;
918 while (isSPACE(*s) || *s == ':')
923 const char *as = Nullch;
925 if (!isIDFIRST(*s)) {
927 * Message is consistent with how attribute lists are
928 * passed. Even though this means "foo : : bar" is
929 * seen as an invalid separator character.
931 const char q = ((*s == '\'') ? '"' : '\'');
932 if (ckWARN(WARN_LAYER))
933 Perl_warner(aTHX_ packWARN(WARN_LAYER),
934 "Invalid separator character %c%c%c in PerlIO layer specification %s",
936 SETERRNO(EINVAL, LIB_INVARG);
941 } while (isALNUM(*e));
957 * It's a nul terminated string, not allowed
958 * to \ the terminating null. Anything other
959 * character is passed over.
969 if (ckWARN(WARN_LAYER))
970 Perl_warner(aTHX_ packWARN(WARN_LAYER),
971 "Argument list not closed for PerlIO layer \"%.*s\"",
983 PerlIO_funcs * const layer =
984 PerlIO_find_layer(aTHX_ s, llen, 1);
986 PerlIO_list_push(aTHX_ av, layer,
992 if (ckWARN(WARN_LAYER))
993 Perl_warner(aTHX_ packWARN(WARN_LAYER), "Unknown PerlIO layer \"%.*s\"",
1006 PerlIO_default_buffer(pTHX_ PerlIO_list_t *av)
1008 PERLIO_FUNCS_DECL(*tab) = &PerlIO_perlio;
1009 #ifdef PERLIO_USING_CRLF
1012 if (PerlIO_stdio.Set_ptrcnt)
1013 tab = &PerlIO_stdio;
1015 PerlIO_debug("Pushing %s\n", tab->name);
1016 PerlIO_list_push(aTHX_ av, PerlIO_find_layer(aTHX_ tab->name, 0, 0),
1021 PerlIO_arg_fetch(PerlIO_list_t *av, IV n)
1023 return av->array[n].arg;
1027 PerlIO_layer_fetch(pTHX_ PerlIO_list_t *av, IV n, PerlIO_funcs *def)
1029 if (n >= 0 && n < av->cur) {
1030 PerlIO_debug("Layer %" IVdf " is %s\n", n,
1031 av->array[n].funcs->name);
1032 return av->array[n].funcs;
1035 Perl_croak(aTHX_ "panic: PerlIO layer array corrupt");
1040 PerlIOPop_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
1042 PERL_UNUSED_ARG(mode);
1043 PERL_UNUSED_ARG(arg);
1044 PERL_UNUSED_ARG(tab);
1045 if (PerlIOValid(f)) {
1047 PerlIO_pop(aTHX_ f);
1053 PERLIO_FUNCS_DECL(PerlIO_remove) = {
1054 sizeof(PerlIO_funcs),
1057 PERLIO_K_DUMMY | PERLIO_K_UTF8,
1077 NULL, /* get_base */
1078 NULL, /* get_bufsiz */
1081 NULL, /* set_ptrcnt */
1085 PerlIO_default_layers(pTHX)
1087 if (!PL_def_layerlist) {
1088 const char * const s = (PL_tainting) ? Nullch : PerlEnv_getenv("PERLIO");
1089 PERLIO_FUNCS_DECL(*osLayer) = &PerlIO_unix;
1090 PL_def_layerlist = PerlIO_list_alloc(aTHX);
1091 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_unix));
1093 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_win32));
1095 osLayer = &PerlIO_win32;
1098 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_raw));
1099 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_perlio));
1100 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_stdio));
1101 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_crlf));
1103 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_mmap));
1105 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_utf8));
1106 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_remove));
1107 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_byte));
1108 PerlIO_list_push(aTHX_ PL_def_layerlist,
1109 PerlIO_find_layer(aTHX_ osLayer->name, 0, 0),
1112 PerlIO_parse_layers(aTHX_ PL_def_layerlist, s);
1115 PerlIO_default_buffer(aTHX_ PL_def_layerlist);
1118 if (PL_def_layerlist->cur < 2) {
1119 PerlIO_default_buffer(aTHX_ PL_def_layerlist);
1121 return PL_def_layerlist;
1125 Perl_boot_core_PerlIO(pTHX)
1127 #ifdef USE_ATTRIBUTES_FOR_PERLIO
1128 newXS("io::MODIFY_SCALAR_ATTRIBUTES", XS_io_MODIFY_SCALAR_ATTRIBUTES,
1131 newXS("PerlIO::Layer::find", XS_PerlIO__Layer__find, __FILE__);
1132 newXS("PerlIO::Layer::NoWarnings", XS_PerlIO__Layer__NoWarnings, __FILE__);
1136 PerlIO_default_layer(pTHX_ I32 n)
1138 PerlIO_list_t * const av = PerlIO_default_layers(aTHX);
1141 return PerlIO_layer_fetch(aTHX_ av, n, PERLIO_FUNCS_CAST(&PerlIO_stdio));
1144 #define PerlIO_default_top() PerlIO_default_layer(aTHX_ -1)
1145 #define PerlIO_default_btm() PerlIO_default_layer(aTHX_ 0)
1148 PerlIO_stdstreams(pTHX)
1151 PerlIO_allocate(aTHX);
1152 PerlIO_fdopen(0, "Ir" PERLIO_STDTEXT);
1153 PerlIO_fdopen(1, "Iw" PERLIO_STDTEXT);
1154 PerlIO_fdopen(2, "Iw" PERLIO_STDTEXT);
1159 PerlIO_push(pTHX_ PerlIO *f, PERLIO_FUNCS_DECL(*tab), const char *mode, SV *arg)
1161 if (tab->fsize != sizeof(PerlIO_funcs)) {
1163 Perl_croak(aTHX_ "Layer does not match this perl");
1167 if (tab->size < sizeof(PerlIOl)) {
1170 /* Real layer with a data area */
1171 Newxc(l,tab->size,char,PerlIOl);
1173 Zero(l, tab->size, char);
1175 l->tab = (PerlIO_funcs*) tab;
1177 PerlIO_debug("PerlIO_push f=%p %s %s %p\n", (void*)f, tab->name,
1178 (mode) ? mode : "(Null)", (void*)arg);
1179 if (*l->tab->Pushed &&
1180 (*l->tab->Pushed) (aTHX_ f, mode, arg, (PerlIO_funcs*) tab) != 0) {
1181 PerlIO_pop(aTHX_ f);
1187 /* Pseudo-layer where push does its own stack adjust */
1188 PerlIO_debug("PerlIO_push f=%p %s %s %p\n", (void*)f, tab->name,
1189 (mode) ? mode : "(Null)", (void*)arg);
1191 (*tab->Pushed) (aTHX_ f, mode, arg, (PerlIO_funcs*) tab) != 0) {
1199 PerlIOBase_binmode(pTHX_ PerlIO *f)
1201 if (PerlIOValid(f)) {
1202 /* Is layer suitable for raw stream ? */
1203 if (PerlIOBase(f)->tab->kind & PERLIO_K_RAW) {
1204 /* Yes - turn off UTF-8-ness, to undo UTF-8 locale effects */
1205 PerlIOBase(f)->flags &= ~PERLIO_F_UTF8;
1208 /* Not suitable - pop it */
1209 PerlIO_pop(aTHX_ f);
1217 PerlIORaw_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
1219 PERL_UNUSED_ARG(mode);
1220 PERL_UNUSED_ARG(arg);
1221 PERL_UNUSED_ARG(tab);
1223 if (PerlIOValid(f)) {
1228 * Strip all layers that are not suitable for a raw stream
1231 while (t && (l = *t)) {
1232 if (l->tab->Binmode) {
1233 /* Has a handler - normal case */
1234 if ((*l->tab->Binmode)(aTHX_ f) == 0) {
1236 /* Layer still there - move down a layer */
1245 /* No handler - pop it */
1246 PerlIO_pop(aTHX_ t);
1249 if (PerlIOValid(f)) {
1250 PerlIO_debug(":raw f=%p :%s\n", (void*)f, PerlIOBase(f)->tab->name);
1258 PerlIO_apply_layera(pTHX_ PerlIO *f, const char *mode,
1259 PerlIO_list_t *layers, IV n, IV max)
1263 PerlIO_funcs * const tab = PerlIO_layer_fetch(aTHX_ layers, n, NULL);
1265 if (!PerlIO_push(aTHX_ f, tab, mode, PerlIOArg)) {
1276 PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names)
1280 PerlIO_list_t * const layers = PerlIO_list_alloc(aTHX);
1281 code = PerlIO_parse_layers(aTHX_ layers, names);
1283 code = PerlIO_apply_layera(aTHX_ f, mode, layers, 0, layers->cur);
1285 PerlIO_list_free(aTHX_ layers);
1291 /*--------------------------------------------------------------------------------------*/
1293 * Given the abstraction above the public API functions
1297 PerlIO_binmode(pTHX_ PerlIO *f, int iotype, int mode, const char *names)
1299 PerlIO_debug("PerlIO_binmode f=%p %s %c %x %s\n",
1300 (void*)f, PerlIOBase(f)->tab->name, iotype, mode,
1301 (names) ? names : "(Null)");
1303 /* Do not flush etc. if (e.g.) switching encodings.
1304 if a pushed layer knows it needs to flush lower layers
1305 (for example :unix which is never going to call them)
1306 it can do the flush when it is pushed.
1308 return PerlIO_apply_layers(aTHX_ f, NULL, names) == 0 ? TRUE : FALSE;
1311 /* Fake 5.6 legacy of using this call to turn ON O_TEXT */
1312 #ifdef PERLIO_USING_CRLF
1313 /* Legacy binmode only has meaning if O_TEXT has a value distinct from
1314 O_BINARY so we can look for it in mode.
1316 if (!(mode & O_BINARY)) {
1318 /* FIXME?: Looking down the layer stack seems wrong,
1319 but is a way of reaching past (say) an encoding layer
1320 to flip CRLF-ness of the layer(s) below
1323 /* Perhaps we should turn on bottom-most aware layer
1324 e.g. Ilya's idea that UNIX TTY could serve
1326 if (PerlIOBase(f)->tab->kind & PERLIO_K_CANCRLF) {
1327 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF)) {
1328 /* Not in text mode - flush any pending stuff and flip it */
1330 PerlIOBase(f)->flags |= PERLIO_F_CRLF;
1332 /* Only need to turn it on in one layer so we are done */
1337 /* Not finding a CRLF aware layer presumably means we are binary
1338 which is not what was requested - so we failed
1339 We _could_ push :crlf layer but so could caller
1344 /* Legacy binmode is now _defined_ as being equivalent to pushing :raw
1345 So code that used to be here is now in PerlIORaw_pushed().
1347 return PerlIO_push(aTHX_ f, PERLIO_FUNCS_CAST(&PerlIO_raw), Nullch, Nullsv) ? TRUE : FALSE;
1352 PerlIO__close(pTHX_ PerlIO *f)
1354 if (PerlIOValid(f)) {
1355 PerlIO_funcs * const tab = PerlIOBase(f)->tab;
1356 if (tab && tab->Close)
1357 return (*tab->Close)(aTHX_ f);
1359 return PerlIOBase_close(aTHX_ f);
1362 SETERRNO(EBADF, SS_IVCHAN);
1368 Perl_PerlIO_close(pTHX_ PerlIO *f)
1370 const int code = PerlIO__close(aTHX_ f);
1371 while (PerlIOValid(f)) {
1372 PerlIO_pop(aTHX_ f);
1378 Perl_PerlIO_fileno(pTHX_ PerlIO *f)
1380 Perl_PerlIO_or_Base(f, Fileno, fileno, -1, (aTHX_ f));
1384 PerlIO_context_layers(pTHX_ const char *mode)
1386 const char *type = NULL;
1388 * Need to supply default layer info from open.pm
1391 SV * const layers = PL_curcop->cop_io;
1394 type = SvPV_const(layers, len);
1395 if (type && mode[0] != 'r') {
1397 * Skip to write part
1399 const char * const s = strchr(type, 0);
1400 if (s && (STRLEN)(s - type) < len) {
1409 static PerlIO_funcs *
1410 PerlIO_layer_from_ref(pTHX_ SV *sv)
1413 * For any scalar type load the handler which is bundled with perl
1415 if (SvTYPE(sv) < SVt_PVAV)
1416 return PerlIO_find_layer(aTHX_ "scalar", 6, 1);
1419 * For other types allow if layer is known but don't try and load it
1421 switch (SvTYPE(sv)) {
1423 return PerlIO_find_layer(aTHX_ "Array", 5, 0);
1425 return PerlIO_find_layer(aTHX_ "Hash", 4, 0);
1427 return PerlIO_find_layer(aTHX_ "Code", 4, 0);
1429 return PerlIO_find_layer(aTHX_ "Glob", 4, 0);
1435 PerlIO_resolve_layers(pTHX_ const char *layers,
1436 const char *mode, int narg, SV **args)
1438 PerlIO_list_t *def = PerlIO_default_layers(aTHX);
1441 PerlIO_stdstreams(aTHX);
1443 SV * const arg = *args;
1445 * If it is a reference but not an object see if we have a handler
1448 if (SvROK(arg) && !sv_isobject(arg)) {
1449 PerlIO_funcs * const handler = PerlIO_layer_from_ref(aTHX_ SvRV(arg));
1451 def = PerlIO_list_alloc(aTHX);
1452 PerlIO_list_push(aTHX_ def, handler, &PL_sv_undef);
1456 * Don't fail if handler cannot be found :via(...) etc. may do
1457 * something sensible else we will just stringfy and open
1462 if (!layers || !*layers)
1463 layers = PerlIO_context_layers(aTHX_ mode);
1464 if (layers && *layers) {
1468 av = PerlIO_list_alloc(aTHX);
1469 for (i = 0; i < def->cur; i++) {
1470 PerlIO_list_push(aTHX_ av, def->array[i].funcs,
1477 if (PerlIO_parse_layers(aTHX_ av, layers) == 0) {
1481 PerlIO_list_free(aTHX_ av);
1482 return (PerlIO_list_t *) NULL;
1493 PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd,
1494 int imode, int perm, PerlIO *f, int narg, SV **args)
1496 if (!f && narg == 1 && *args == &PL_sv_undef) {
1497 if ((f = PerlIO_tmpfile())) {
1498 if (!layers || !*layers)
1499 layers = PerlIO_context_layers(aTHX_ mode);
1500 if (layers && *layers)
1501 PerlIO_apply_layers(aTHX_ f, mode, layers);
1505 PerlIO_list_t *layera;
1507 PerlIO_funcs *tab = NULL;
1508 if (PerlIOValid(f)) {
1510 * This is "reopen" - it is not tested as perl does not use it
1514 layera = PerlIO_list_alloc(aTHX);
1516 SV * const arg = (l->tab->Getarg)
1517 ? (*l->tab->Getarg) (aTHX_ &l, NULL, 0)
1519 PerlIO_list_push(aTHX_ layera, l->tab, arg);
1520 l = *PerlIONext(&l);
1524 layera = PerlIO_resolve_layers(aTHX_ layers, mode, narg, args);
1530 * Start at "top" of layer stack
1532 n = layera->cur - 1;
1534 PerlIO_funcs * const t = PerlIO_layer_fetch(aTHX_ layera, n, NULL);
1543 * Found that layer 'n' can do opens - call it
1545 if (narg > 1 && !(tab->kind & PERLIO_K_MULTIARG)) {
1546 Perl_croak(aTHX_ "More than one argument to open(,':%s')",tab->name);
1548 PerlIO_debug("openn(%s,'%s','%s',%d,%x,%o,%p,%d,%p)\n",
1549 tab->name, layers, mode, fd, imode, perm,
1550 (void*)f, narg, (void*)args);
1552 f = (*tab->Open) (aTHX_ tab, layera, n, mode, fd, imode, perm,
1555 SETERRNO(EINVAL, LIB_INVARG);
1559 if (n + 1 < layera->cur) {
1561 * More layers above the one that we used to open -
1564 if (PerlIO_apply_layera(aTHX_ f, mode, layera, n + 1, layera->cur) != 0) {
1565 /* If pushing layers fails close the file */
1572 PerlIO_list_free(aTHX_ layera);
1579 Perl_PerlIO_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
1581 Perl_PerlIO_or_Base(f, Read, read, -1, (aTHX_ f, vbuf, count));
1585 Perl_PerlIO_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
1587 Perl_PerlIO_or_Base(f, Unread, unread, -1, (aTHX_ f, vbuf, count));
1591 Perl_PerlIO_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
1593 Perl_PerlIO_or_fail(f, Write, -1, (aTHX_ f, vbuf, count));
1597 Perl_PerlIO_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
1599 Perl_PerlIO_or_fail(f, Seek, -1, (aTHX_ f, offset, whence));
1603 Perl_PerlIO_tell(pTHX_ PerlIO *f)
1605 Perl_PerlIO_or_fail(f, Tell, -1, (aTHX_ f));
1609 Perl_PerlIO_flush(pTHX_ PerlIO *f)
1613 const PerlIO_funcs *tab = PerlIOBase(f)->tab;
1615 if (tab && tab->Flush)
1616 return (*tab->Flush) (aTHX_ f);
1618 return 0; /* If no Flush defined, silently succeed. */
1621 PerlIO_debug("Cannot flush f=%p\n", (void*)f);
1622 SETERRNO(EBADF, SS_IVCHAN);
1628 * Is it good API design to do flush-all on NULL, a potentially
1629 * errorneous input? Maybe some magical value (PerlIO*
1630 * PERLIO_FLUSH_ALL = (PerlIO*)-1;)? Yes, stdio does similar
1631 * things on fflush(NULL), but should we be bound by their design
1634 PerlIO **table = &PL_perlio;
1636 while ((f = *table)) {
1638 table = (PerlIO **) (f++);
1639 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
1640 if (*f && PerlIO_flush(f) != 0)
1650 PerlIOBase_flush_linebuf(pTHX)
1652 PerlIO **table = &PL_perlio;
1654 while ((f = *table)) {
1656 table = (PerlIO **) (f++);
1657 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
1660 flags & (PERLIO_F_LINEBUF | PERLIO_F_CANWRITE))
1661 == (PERLIO_F_LINEBUF | PERLIO_F_CANWRITE))
1669 Perl_PerlIO_fill(pTHX_ PerlIO *f)
1671 Perl_PerlIO_or_fail(f, Fill, -1, (aTHX_ f));
1675 PerlIO_isutf8(PerlIO *f)
1678 return (PerlIOBase(f)->flags & PERLIO_F_UTF8) != 0;
1680 SETERRNO(EBADF, SS_IVCHAN);
1686 Perl_PerlIO_eof(pTHX_ PerlIO *f)
1688 Perl_PerlIO_or_Base(f, Eof, eof, -1, (aTHX_ f));
1692 Perl_PerlIO_error(pTHX_ PerlIO *f)
1694 Perl_PerlIO_or_Base(f, Error, error, -1, (aTHX_ f));
1698 Perl_PerlIO_clearerr(pTHX_ PerlIO *f)
1700 Perl_PerlIO_or_Base_void(f, Clearerr, clearerr, (aTHX_ f));
1704 Perl_PerlIO_setlinebuf(pTHX_ PerlIO *f)
1706 Perl_PerlIO_or_Base_void(f, Setlinebuf, setlinebuf, (aTHX_ f));
1710 PerlIO_has_base(PerlIO *f)
1712 if (PerlIOValid(f)) {
1713 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
1716 return (tab->Get_base != NULL);
1717 SETERRNO(EINVAL, LIB_INVARG);
1720 SETERRNO(EBADF, SS_IVCHAN);
1726 PerlIO_fast_gets(PerlIO *f)
1728 if (PerlIOValid(f) && (PerlIOBase(f)->flags & PERLIO_F_FASTGETS)) {
1729 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
1732 return (tab->Set_ptrcnt != NULL);
1733 SETERRNO(EINVAL, LIB_INVARG);
1736 SETERRNO(EBADF, SS_IVCHAN);
1742 PerlIO_has_cntptr(PerlIO *f)
1744 if (PerlIOValid(f)) {
1745 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
1748 return (tab->Get_ptr != NULL && tab->Get_cnt != NULL);
1749 SETERRNO(EINVAL, LIB_INVARG);
1752 SETERRNO(EBADF, SS_IVCHAN);
1758 PerlIO_canset_cnt(PerlIO *f)
1760 if (PerlIOValid(f)) {
1761 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
1764 return (tab->Set_ptrcnt != NULL);
1765 SETERRNO(EINVAL, LIB_INVARG);
1768 SETERRNO(EBADF, SS_IVCHAN);
1774 Perl_PerlIO_get_base(pTHX_ PerlIO *f)
1776 Perl_PerlIO_or_fail(f, Get_base, NULL, (aTHX_ f));
1780 Perl_PerlIO_get_bufsiz(pTHX_ PerlIO *f)
1782 Perl_PerlIO_or_fail(f, Get_bufsiz, -1, (aTHX_ f));
1786 Perl_PerlIO_get_ptr(pTHX_ PerlIO *f)
1788 Perl_PerlIO_or_fail(f, Get_ptr, NULL, (aTHX_ f));
1792 Perl_PerlIO_get_cnt(pTHX_ PerlIO *f)
1794 Perl_PerlIO_or_fail(f, Get_cnt, -1, (aTHX_ f));
1798 Perl_PerlIO_set_cnt(pTHX_ PerlIO *f, int cnt)
1800 Perl_PerlIO_or_fail_void(f, Set_ptrcnt, (aTHX_ f, NULL, cnt));
1804 Perl_PerlIO_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, int cnt)
1806 Perl_PerlIO_or_fail_void(f, Set_ptrcnt, (aTHX_ f, ptr, cnt));
1810 /*--------------------------------------------------------------------------------------*/
1812 * utf8 and raw dummy layers
1816 PerlIOUtf8_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
1818 PERL_UNUSED_ARG(mode);
1819 PERL_UNUSED_ARG(arg);
1820 if (PerlIOValid(f)) {
1821 if (tab->kind & PERLIO_K_UTF8)
1822 PerlIOBase(f)->flags |= PERLIO_F_UTF8;
1824 PerlIOBase(f)->flags &= ~PERLIO_F_UTF8;
1830 PERLIO_FUNCS_DECL(PerlIO_utf8) = {
1831 sizeof(PerlIO_funcs),
1834 PERLIO_K_DUMMY | PERLIO_K_UTF8,
1854 NULL, /* get_base */
1855 NULL, /* get_bufsiz */
1858 NULL, /* set_ptrcnt */
1861 PERLIO_FUNCS_DECL(PerlIO_byte) = {
1862 sizeof(PerlIO_funcs),
1885 NULL, /* get_base */
1886 NULL, /* get_bufsiz */
1889 NULL, /* set_ptrcnt */
1893 PerlIORaw_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
1894 IV n, const char *mode, int fd, int imode, int perm,
1895 PerlIO *old, int narg, SV **args)
1897 PerlIO_funcs * const tab = PerlIO_default_btm();
1898 PERL_UNUSED_ARG(self);
1899 if (tab && tab->Open)
1900 return (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
1902 SETERRNO(EINVAL, LIB_INVARG);
1906 PERLIO_FUNCS_DECL(PerlIO_raw) = {
1907 sizeof(PerlIO_funcs),
1930 NULL, /* get_base */
1931 NULL, /* get_bufsiz */
1934 NULL, /* set_ptrcnt */
1936 /*--------------------------------------------------------------------------------------*/
1937 /*--------------------------------------------------------------------------------------*/
1939 * "Methods" of the "base class"
1943 PerlIOBase_fileno(pTHX_ PerlIO *f)
1945 return PerlIOValid(f) ? PerlIO_fileno(PerlIONext(f)) : -1;
1949 PerlIO_modestr(PerlIO * f, char *buf)
1952 if (PerlIOValid(f)) {
1953 const IV flags = PerlIOBase(f)->flags;
1954 if (flags & PERLIO_F_APPEND) {
1956 if (flags & PERLIO_F_CANREAD) {
1960 else if (flags & PERLIO_F_CANREAD) {
1962 if (flags & PERLIO_F_CANWRITE)
1965 else if (flags & PERLIO_F_CANWRITE) {
1967 if (flags & PERLIO_F_CANREAD) {
1971 #ifdef PERLIO_USING_CRLF
1972 if (!(flags & PERLIO_F_CRLF))
1982 PerlIOBase_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
1984 PerlIOl * const l = PerlIOBase(f);
1985 PERL_UNUSED_ARG(arg);
1987 l->flags &= ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE |
1988 PERLIO_F_TRUNCATE | PERLIO_F_APPEND);
1989 if (tab->Set_ptrcnt != NULL)
1990 l->flags |= PERLIO_F_FASTGETS;
1992 if (*mode == IoTYPE_NUMERIC || *mode == IoTYPE_IMPLICIT)
1996 l->flags |= PERLIO_F_CANREAD;
1999 l->flags |= PERLIO_F_APPEND | PERLIO_F_CANWRITE;
2002 l->flags |= PERLIO_F_TRUNCATE | PERLIO_F_CANWRITE;
2005 SETERRNO(EINVAL, LIB_INVARG);
2011 l->flags |= PERLIO_F_CANREAD | PERLIO_F_CANWRITE;
2014 l->flags &= ~PERLIO_F_CRLF;
2017 l->flags |= PERLIO_F_CRLF;
2020 SETERRNO(EINVAL, LIB_INVARG);
2027 l->flags |= l->next->flags &
2028 (PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_TRUNCATE |
2033 PerlIO_debug("PerlIOBase_pushed f=%p %s %s fl=%08" UVxf " (%s)\n",
2034 f, PerlIOBase(f)->tab->name, (omode) ? omode : "(Null)",
2035 l->flags, PerlIO_modestr(f, temp));
2041 PerlIOBase_popped(pTHX_ PerlIO *f)
2048 PerlIOBase_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
2051 * Save the position as current head considers it
2053 const Off_t old = PerlIO_tell(f);
2054 PerlIO_push(aTHX_ f, PERLIO_FUNCS_CAST(&PerlIO_pending), "r", Nullsv);
2055 PerlIOSelf(f, PerlIOBuf)->posn = old;
2056 return PerlIOBuf_unread(aTHX_ f, vbuf, count);
2060 PerlIOBase_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
2062 STDCHAR *buf = (STDCHAR *) vbuf;
2064 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD)) {
2065 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2066 SETERRNO(EBADF, SS_IVCHAN);
2072 SSize_t avail = PerlIO_get_cnt(f);
2075 take = ((SSize_t)count < avail) ? count : avail;
2077 STDCHAR *ptr = PerlIO_get_ptr(f);
2078 Copy(ptr, buf, take, STDCHAR);
2079 PerlIO_set_ptrcnt(f, ptr + take, (avail -= take));
2082 if (avail == 0) /* set_ptrcnt could have reset avail */
2085 if (count > 0 && avail <= 0) {
2086 if (PerlIO_fill(f) != 0)
2091 return (buf - (STDCHAR *) vbuf);
2097 PerlIOBase_noop_ok(pTHX_ PerlIO *f)
2104 PerlIOBase_noop_fail(pTHX_ PerlIO *f)
2111 PerlIOBase_close(pTHX_ PerlIO *f)
2114 if (PerlIOValid(f)) {
2115 PerlIO *n = PerlIONext(f);
2116 code = PerlIO_flush(f);
2117 PerlIOBase(f)->flags &=
2118 ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_OPEN);
2119 while (PerlIOValid(n)) {
2120 const PerlIO_funcs * const tab = PerlIOBase(n)->tab;
2121 if (tab && tab->Close) {
2122 if ((*tab->Close)(aTHX_ n) != 0)
2127 PerlIOBase(n)->flags &=
2128 ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_OPEN);
2134 SETERRNO(EBADF, SS_IVCHAN);
2140 PerlIOBase_eof(pTHX_ PerlIO *f)
2142 if (PerlIOValid(f)) {
2143 return (PerlIOBase(f)->flags & PERLIO_F_EOF) != 0;
2149 PerlIOBase_error(pTHX_ PerlIO *f)
2151 if (PerlIOValid(f)) {
2152 return (PerlIOBase(f)->flags & PERLIO_F_ERROR) != 0;
2158 PerlIOBase_clearerr(pTHX_ PerlIO *f)
2160 if (PerlIOValid(f)) {
2161 PerlIO * const n = PerlIONext(f);
2162 PerlIOBase(f)->flags &= ~(PERLIO_F_ERROR | PERLIO_F_EOF);
2169 PerlIOBase_setlinebuf(pTHX_ PerlIO *f)
2171 if (PerlIOValid(f)) {
2172 PerlIOBase(f)->flags |= PERLIO_F_LINEBUF;
2177 PerlIO_sv_dup(pTHX_ SV *arg, CLONE_PARAMS *param)
2183 return sv_dup(arg, param);
2186 return newSVsv(arg);
2189 PERL_UNUSED_ARG(param);
2190 return newSVsv(arg);
2195 PerlIOBase_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
2197 PerlIO * const nexto = PerlIONext(o);
2198 if (PerlIOValid(nexto)) {
2199 const PerlIO_funcs * const tab = PerlIOBase(nexto)->tab;
2200 if (tab && tab->Dup)
2201 f = (*tab->Dup)(aTHX_ f, nexto, param, flags);
2203 f = PerlIOBase_dup(aTHX_ f, nexto, param, flags);
2206 PerlIO_funcs * const self = PerlIOBase(o)->tab;
2209 PerlIO_debug("PerlIOBase_dup %s f=%p o=%p param=%p\n",
2210 self->name, (void*)f, (void*)o, (void*)param);
2212 arg = (*self->Getarg)(aTHX_ o, param, flags);
2216 f = PerlIO_push(aTHX_ f, self, PerlIO_modestr(o,buf), arg);
2225 perl_mutex PerlIO_mutex;
2228 /* PL_perlio_fd_refcnt[] is in intrpvar.h */
2233 /* Place holder for stdstreams call ??? */
2235 MUTEX_INIT(&PerlIO_mutex);
2240 PerlIOUnix_refcnt_inc(int fd)
2243 if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
2245 MUTEX_LOCK(&PerlIO_mutex);
2247 PL_perlio_fd_refcnt[fd]++;
2248 PerlIO_debug("fd %d refcnt=%d\n",fd,PL_perlio_fd_refcnt[fd]);
2250 MUTEX_UNLOCK(&PerlIO_mutex);
2256 PerlIOUnix_refcnt_dec(int fd)
2260 if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
2262 MUTEX_LOCK(&PerlIO_mutex);
2264 cnt = --PL_perlio_fd_refcnt[fd];
2265 PerlIO_debug("fd %d refcnt=%d\n",fd,cnt);
2267 MUTEX_UNLOCK(&PerlIO_mutex);
2274 PerlIO_cleanup(pTHX)
2278 PerlIO_debug("Cleanup layers for %p\n",aTHX);
2280 PerlIO_debug("Cleanup layers\n");
2282 /* Raise STDIN..STDERR refcount so we don't close them */
2283 for (i=0; i < 3; i++)
2284 PerlIOUnix_refcnt_inc(i);
2285 PerlIO_cleantable(aTHX_ &PL_perlio);
2286 /* Restore STDIN..STDERR refcount */
2287 for (i=0; i < 3; i++)
2288 PerlIOUnix_refcnt_dec(i);
2290 if (PL_known_layers) {
2291 PerlIO_list_free(aTHX_ PL_known_layers);
2292 PL_known_layers = NULL;
2294 if (PL_def_layerlist) {
2295 PerlIO_list_free(aTHX_ PL_def_layerlist);
2296 PL_def_layerlist = NULL;
2302 /*--------------------------------------------------------------------------------------*/
2304 * Bottom-most level for UNIX-like case
2308 struct _PerlIO base; /* The generic part */
2309 int fd; /* UNIX like file descriptor */
2310 int oflags; /* open/fcntl flags */
2314 PerlIOUnix_oflags(const char *mode)
2317 if (*mode == IoTYPE_IMPLICIT || *mode == IoTYPE_NUMERIC)
2322 if (*++mode == '+') {
2329 oflags = O_CREAT | O_TRUNC;
2330 if (*++mode == '+') {
2339 oflags = O_CREAT | O_APPEND;
2340 if (*++mode == '+') {
2353 else if (*mode == 't') {
2355 oflags &= ~O_BINARY;
2359 * Always open in binary mode
2362 if (*mode || oflags == -1) {
2363 SETERRNO(EINVAL, LIB_INVARG);
2370 PerlIOUnix_fileno(pTHX_ PerlIO *f)
2372 return PerlIOSelf(f, PerlIOUnix)->fd;
2376 PerlIOUnix_setfd(pTHX_ PerlIO *f, int fd, int imode)
2378 PerlIOUnix * const s = PerlIOSelf(f, PerlIOUnix);
2381 if (PerlLIO_fstat(fd, &st) == 0) {
2382 if (!S_ISREG(st.st_mode)) {
2383 PerlIO_debug("%d is not regular file\n",fd);
2384 PerlIOBase(f)->flags |= PERLIO_F_NOTREG;
2387 PerlIO_debug("%d _is_ a regular file\n",fd);
2393 PerlIOUnix_refcnt_inc(fd);
2397 PerlIOUnix_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
2399 IV code = PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
2400 if (*PerlIONext(f)) {
2401 /* We never call down so do any pending stuff now */
2402 PerlIO_flush(PerlIONext(f));
2404 * XXX could (or should) we retrieve the oflags from the open file
2405 * handle rather than believing the "mode" we are passed in? XXX
2406 * Should the value on NULL mode be 0 or -1?
2408 PerlIOUnix_setfd(aTHX_ f, PerlIO_fileno(PerlIONext(f)),
2409 mode ? PerlIOUnix_oflags(mode) : -1);
2411 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
2417 PerlIOUnix_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
2419 const int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2421 if (PerlIOBase(f)->flags & PERLIO_F_NOTREG) {
2423 SETERRNO(ESPIPE, LIB_INVARG);
2425 SETERRNO(EINVAL, LIB_INVARG);
2429 new_loc = PerlLIO_lseek(fd, offset, whence);
2430 if (new_loc == (Off_t) - 1)
2432 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
2437 PerlIOUnix_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2438 IV n, const char *mode, int fd, int imode,
2439 int perm, PerlIO *f, int narg, SV **args)
2441 if (PerlIOValid(f)) {
2442 if (PerlIOBase(f)->flags & PERLIO_F_OPEN)
2443 (*PerlIOBase(f)->tab->Close)(aTHX_ f);
2446 if (*mode == IoTYPE_NUMERIC)
2449 imode = PerlIOUnix_oflags(mode);
2453 const char *path = SvPV_nolen_const(*args);
2454 fd = PerlLIO_open3(path, imode, perm);
2458 if (*mode == IoTYPE_IMPLICIT)
2461 f = PerlIO_allocate(aTHX);
2463 if (!PerlIOValid(f)) {
2464 if (!(f = PerlIO_push(aTHX_ f, self, mode, PerlIOArg))) {
2468 PerlIOUnix_setfd(aTHX_ f, fd, imode);
2469 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
2470 if (*mode == IoTYPE_APPEND)
2471 PerlIOUnix_seek(aTHX_ f, 0, SEEK_END);
2477 * FIXME: pop layers ???
2485 PerlIOUnix_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
2487 const PerlIOUnix * const os = PerlIOSelf(o, PerlIOUnix);
2489 if (flags & PERLIO_DUP_FD) {
2490 fd = PerlLIO_dup(fd);
2492 if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
2493 f = PerlIOBase_dup(aTHX_ f, o, param, flags);
2495 /* If all went well overwrite fd in dup'ed lay with the dup()'ed fd */
2496 PerlIOUnix_setfd(aTHX_ f, fd, os->oflags);
2505 PerlIOUnix_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
2507 const int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2508 #ifdef PERLIO_STD_SPECIAL
2510 return PERLIO_STD_IN(fd, vbuf, count);
2512 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD) ||
2513 PerlIOBase(f)->flags & (PERLIO_F_EOF|PERLIO_F_ERROR)) {
2517 const SSize_t len = PerlLIO_read(fd, vbuf, count);
2518 if (len >= 0 || errno != EINTR) {
2520 if (errno != EAGAIN) {
2521 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2524 else if (len == 0 && count != 0) {
2525 PerlIOBase(f)->flags |= PERLIO_F_EOF;
2536 PerlIOUnix_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
2538 const int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2539 #ifdef PERLIO_STD_SPECIAL
2540 if (fd == 1 || fd == 2)
2541 return PERLIO_STD_OUT(fd, vbuf, count);
2544 const SSize_t len = PerlLIO_write(fd, vbuf, count);
2545 if (len >= 0 || errno != EINTR) {
2547 if (errno != EAGAIN) {
2548 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2559 PerlIOUnix_tell(pTHX_ PerlIO *f)
2561 return PerlLIO_lseek(PerlIOSelf(f, PerlIOUnix)->fd, 0, SEEK_CUR);
2566 PerlIOUnix_close(pTHX_ PerlIO *f)
2568 const int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2570 if (PerlIOBase(f)->flags & PERLIO_F_OPEN) {
2571 if (PerlIOUnix_refcnt_dec(fd) > 0) {
2572 PerlIOBase(f)->flags &= ~PERLIO_F_OPEN;
2577 SETERRNO(EBADF,SS_IVCHAN);
2580 while (PerlLIO_close(fd) != 0) {
2581 if (errno != EINTR) {
2588 PerlIOBase(f)->flags &= ~PERLIO_F_OPEN;
2593 PERLIO_FUNCS_DECL(PerlIO_unix) = {
2594 sizeof(PerlIO_funcs),
2601 PerlIOBase_binmode, /* binmode */
2611 PerlIOBase_noop_ok, /* flush */
2612 PerlIOBase_noop_fail, /* fill */
2615 PerlIOBase_clearerr,
2616 PerlIOBase_setlinebuf,
2617 NULL, /* get_base */
2618 NULL, /* get_bufsiz */
2621 NULL, /* set_ptrcnt */
2624 /*--------------------------------------------------------------------------------------*/
2629 #if defined(VMS) && !defined(STDIO_BUFFER_WRITABLE)
2630 /* perl5.8 - This ensures the last minute VMS ungetc fix is not
2631 broken by the last second glibc 2.3 fix
2633 #define STDIO_BUFFER_WRITABLE
2638 struct _PerlIO base;
2639 FILE *stdio; /* The stream */
2643 PerlIOStdio_fileno(pTHX_ PerlIO *f)
2645 if (PerlIOValid(f)) {
2646 FILE * const s = PerlIOSelf(f, PerlIOStdio)->stdio;
2648 return PerlSIO_fileno(s);
2655 PerlIOStdio_mode(const char *mode, char *tmode)
2657 char * const ret = tmode;
2663 #if defined(PERLIO_USING_CRLF) || defined(__CYGWIN__)
2671 PerlIOStdio_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
2674 if (PerlIOValid(f) && PerlIOValid(n = PerlIONext(f))) {
2675 PerlIO_funcs * const toptab = PerlIOBase(n)->tab;
2676 if (toptab == tab) {
2677 /* Top is already stdio - pop self (duplicate) and use original */
2678 PerlIO_pop(aTHX_ f);
2681 const int fd = PerlIO_fileno(n);
2684 if (fd >= 0 && (stdio = PerlSIO_fdopen(fd,
2685 mode = PerlIOStdio_mode(mode, tmode)))) {
2686 PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
2687 /* We never call down so do any pending stuff now */
2688 PerlIO_flush(PerlIONext(f));
2695 return PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
2700 PerlIO_importFILE(FILE *stdio, const char *mode)
2706 if (!mode || !*mode) {
2707 /* We need to probe to see how we can open the stream
2708 so start with read/write and then try write and read
2709 we dup() so that we can fclose without loosing the fd.
2711 Note that the errno value set by a failing fdopen
2712 varies between stdio implementations.
2714 const int fd = PerlLIO_dup(fileno(stdio));
2715 FILE *f2 = PerlSIO_fdopen(fd, (mode = "r+"));
2717 f2 = PerlSIO_fdopen(fd, (mode = "w"));
2720 f2 = PerlSIO_fdopen(fd, (mode = "r"));
2723 /* Don't seem to be able to open */
2729 if ((f = PerlIO_push(aTHX_(f = PerlIO_allocate(aTHX)), PERLIO_FUNCS_CAST(&PerlIO_stdio), mode, Nullsv))) {
2730 s = PerlIOSelf(f, PerlIOStdio);
2738 PerlIOStdio_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2739 IV n, const char *mode, int fd, int imode,
2740 int perm, PerlIO *f, int narg, SV **args)
2743 if (PerlIOValid(f)) {
2744 const char * const path = SvPV_nolen_const(*args);
2745 PerlIOStdio * const s = PerlIOSelf(f, PerlIOStdio);
2747 PerlIOUnix_refcnt_dec(fileno(s->stdio));
2748 stdio = PerlSIO_freopen(path, (mode = PerlIOStdio_mode(mode, tmode)),
2753 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2758 const char * const path = SvPV_nolen_const(*args);
2759 if (*mode == IoTYPE_NUMERIC) {
2761 fd = PerlLIO_open3(path, imode, perm);
2765 bool appended = FALSE;
2767 /* Cygwin wants its 'b' early. */
2769 mode = PerlIOStdio_mode(mode, tmode);
2771 stdio = PerlSIO_fopen(path, mode);
2775 f = PerlIO_allocate(aTHX);
2778 mode = PerlIOStdio_mode(mode, tmode);
2779 f = PerlIO_push(aTHX_ f, self, mode, PerlIOArg);
2781 s = PerlIOSelf(f, PerlIOStdio);
2783 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2795 if (*mode == IoTYPE_IMPLICIT) {
2802 stdio = PerlSIO_stdin;
2805 stdio = PerlSIO_stdout;
2808 stdio = PerlSIO_stderr;
2813 stdio = PerlSIO_fdopen(fd, mode =
2814 PerlIOStdio_mode(mode, tmode));
2818 f = PerlIO_allocate(aTHX);
2820 if ((f = PerlIO_push(aTHX_ f, self, mode, PerlIOArg))) {
2821 PerlIOStdio * const s = PerlIOSelf(f, PerlIOStdio);
2823 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2833 PerlIOStdio_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
2835 /* This assumes no layers underneath - which is what
2836 happens, but is not how I remember it. NI-S 2001/10/16
2838 if ((f = PerlIOBase_dup(aTHX_ f, o, param, flags))) {
2839 FILE *stdio = PerlIOSelf(o, PerlIOStdio)->stdio;
2840 const int fd = fileno(stdio);
2842 if (flags & PERLIO_DUP_FD) {
2843 const int dfd = PerlLIO_dup(fileno(stdio));
2845 stdio = PerlSIO_fdopen(dfd, PerlIO_modestr(o,mode));
2849 /* FIXME: To avoid messy error recovery if dup fails
2850 re-use the existing stdio as though flag was not set
2854 stdio = PerlSIO_fdopen(fd, PerlIO_modestr(o,mode));
2856 PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
2857 PerlIOUnix_refcnt_inc(fileno(stdio));
2863 PerlIOStdio_invalidate_fileno(pTHX_ FILE *f)
2865 /* XXX this could use PerlIO_canset_fileno() and
2866 * PerlIO_set_fileno() support from Configure
2868 # if defined(__UCLIBC__)
2869 /* uClibc must come before glibc because it defines __GLIBC__ as well. */
2872 # elif defined(__GLIBC__)
2873 /* There may be a better way for GLIBC:
2874 - libio.h defines a flag to not close() on cleanup
2878 # elif defined(__sun__)
2880 /* On solaris, if _LP64 is defined, the FILE structure is this:
2886 * It turns out that the fd is stored in the top 32 bits of
2887 * file->__pad[4]. The lower 32 bits contain flags. file->pad[5] appears
2888 * to contain a pointer or offset into another structure. All the
2889 * remaining fields are zero.
2891 * We set the top bits to -1 (0xFFFFFFFF).
2893 f->__pad[4] |= 0xffffffff00000000L;
2894 assert(fileno(f) == 0xffffffff);
2895 # else /* !defined(_LP64) */
2896 /* _file is just a unsigned char :-(
2897 Not clear why we dup() rather than using -1
2898 even if that would be treated as 0xFF - so will
2901 f->_file = PerlLIO_dup(fileno(f));
2902 # endif /* defined(_LP64) */
2904 # elif defined(__hpux)
2908 /* Next one ->_file seems to be a reasonable fallback, i.e. if
2909 your platform does not have special entry try this one.
2910 [For OSF only have confirmation for Tru64 (alpha)
2911 but assume other OSFs will be similar.]
2913 # elif defined(_AIX) || defined(__osf__) || defined(__irix__)
2916 # elif defined(__FreeBSD__)
2917 /* There may be a better way on FreeBSD:
2918 - we could insert a dummy func in the _close function entry
2919 f->_close = (int (*)(void *)) dummy_close;
2923 # elif defined(__OpenBSD__)
2924 /* There may be a better way on OpenBSD:
2925 - we could insert a dummy func in the _close function entry
2926 f->_close = (int (*)(void *)) dummy_close;
2930 # elif defined(__EMX__)
2931 /* f->_flags &= ~_IOOPEN; */ /* Will leak stream->_buffer */
2934 # elif defined(__CYGWIN__)
2935 /* There may be a better way on CYGWIN:
2936 - we could insert a dummy func in the _close function entry
2937 f->_close = (int (*)(void *)) dummy_close;
2941 # elif defined(WIN32)
2942 # if defined(__BORLANDC__)
2943 f->fd = PerlLIO_dup(fileno(f));
2944 # elif defined(UNDER_CE)
2945 /* WIN_CE does not have access to FILE internals, it hardly has FILE
2954 /* Sarathy's code did this - we fall back to a dup/dup2 hack
2955 (which isn't thread safe) instead
2957 # error "Don't know how to set FILE.fileno on your platform"
2965 PerlIOStdio_close(pTHX_ PerlIO *f)
2967 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2973 const int fd = fileno(stdio);
2979 #ifdef SOCKS5_VERSION_NAME
2980 /* Socks lib overrides close() but stdio isn't linked to
2981 that library (though we are) - so we must call close()
2982 on sockets on stdio's behalf.
2985 Sock_size_t optlen = sizeof(int);
2986 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, (void *) &optval, &optlen) == 0) {
2991 if (PerlIOUnix_refcnt_dec(fd) > 0) {
2992 /* File descriptor still in use */
2997 /* For STD* handles don't close the stdio at all
2998 this is because we have shared the FILE * too
3000 if (stdio == stdin) {
3001 /* Some stdios are buggy fflush-ing inputs */
3004 else if (stdio == stdout || stdio == stderr) {
3005 return PerlIO_flush(f);
3007 /* Tricky - must fclose(stdio) to free memory but not close(fd)
3008 Use Sarathy's trick from maint-5.6 to invalidate the
3009 fileno slot of the FILE *
3011 result = PerlIO_flush(f);
3013 if (!(invalidate = PerlIOStdio_invalidate_fileno(aTHX_ stdio))) {
3014 dupfd = PerlLIO_dup(fd);
3017 result = PerlSIO_fclose(stdio);
3018 /* We treat error from stdio as success if we invalidated
3019 errno may NOT be expected EBADF
3021 if (invalidate && result != 0) {
3026 /* in SOCKS case let close() determine return value */
3030 PerlLIO_dup2(dupfd,fd);
3031 PerlLIO_close(dupfd);
3038 PerlIOStdio_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
3040 FILE * const s = PerlIOSelf(f, PerlIOStdio)->stdio;
3044 STDCHAR *buf = (STDCHAR *) vbuf;
3046 * Perl is expecting PerlIO_getc() to fill the buffer Linux's
3047 * stdio does not do that for fread()
3049 const int ch = PerlSIO_fgetc(s);
3056 got = PerlSIO_fread(vbuf, 1, count, s);
3057 if (got == 0 && PerlSIO_ferror(s))
3059 if (got >= 0 || errno != EINTR)
3062 SETERRNO(0,0); /* just in case */
3068 PerlIOStdio_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3071 FILE * const s = PerlIOSelf(f, PerlIOStdio)->stdio;
3073 #ifdef STDIO_BUFFER_WRITABLE
3074 if (PerlIO_fast_gets(f) && PerlIO_has_base(f)) {
3075 STDCHAR *buf = ((STDCHAR *) vbuf) + count;
3076 STDCHAR *base = PerlIO_get_base(f);
3077 SSize_t cnt = PerlIO_get_cnt(f);
3078 STDCHAR *ptr = PerlIO_get_ptr(f);
3079 SSize_t avail = ptr - base;
3081 if (avail > count) {
3085 Move(buf-avail,ptr,avail,STDCHAR);
3088 PerlIO_set_ptrcnt(f,ptr,cnt+avail);
3089 if (PerlSIO_feof(s) && unread >= 0)
3090 PerlSIO_clearerr(s);
3095 if (PerlIO_has_cntptr(f)) {
3096 /* We can get pointer to buffer but not its base
3097 Do ungetc() but check chars are ending up in the
3100 STDCHAR *eptr = (STDCHAR*)PerlSIO_get_ptr(s);
3101 STDCHAR *buf = ((STDCHAR *) vbuf) + count;
3103 const int ch = *--buf & 0xFF;
3104 if (ungetc(ch,s) != ch) {
3105 /* ungetc did not work */
3108 if ((STDCHAR*)PerlSIO_get_ptr(s) != --eptr || ((*eptr & 0xFF) != ch)) {
3109 /* Did not change pointer as expected */
3110 fgetc(s); /* get char back again */
3120 unread += PerlIOBase_unread(aTHX_ f, vbuf, count);
3126 PerlIOStdio_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3130 got = PerlSIO_fwrite(vbuf, 1, count,
3131 PerlIOSelf(f, PerlIOStdio)->stdio);
3132 if (got >= 0 || errno != EINTR)
3135 SETERRNO(0,0); /* just in case */
3141 PerlIOStdio_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
3143 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3144 return PerlSIO_fseek(stdio, offset, whence);
3148 PerlIOStdio_tell(pTHX_ PerlIO *f)
3150 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3151 return PerlSIO_ftell(stdio);
3155 PerlIOStdio_flush(pTHX_ PerlIO *f)
3157 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3158 if (PerlIOBase(f)->flags & PERLIO_F_CANWRITE) {
3159 return PerlSIO_fflush(stdio);
3164 * FIXME: This discards ungetc() and pre-read stuff which is not
3165 * right if this is just a "sync" from a layer above Suspect right
3166 * design is to do _this_ but not have layer above flush this
3167 * layer read-to-read
3170 * Not writeable - sync by attempting a seek
3173 if (PerlSIO_fseek(stdio, (Off_t) 0, SEEK_CUR) != 0)
3181 PerlIOStdio_eof(pTHX_ PerlIO *f)
3183 return PerlSIO_feof(PerlIOSelf(f, PerlIOStdio)->stdio);
3187 PerlIOStdio_error(pTHX_ PerlIO *f)
3189 return PerlSIO_ferror(PerlIOSelf(f, PerlIOStdio)->stdio);
3193 PerlIOStdio_clearerr(pTHX_ PerlIO *f)
3195 PerlSIO_clearerr(PerlIOSelf(f, PerlIOStdio)->stdio);
3199 PerlIOStdio_setlinebuf(pTHX_ PerlIO *f)
3201 #ifdef HAS_SETLINEBUF
3202 PerlSIO_setlinebuf(PerlIOSelf(f, PerlIOStdio)->stdio);
3204 PerlSIO_setvbuf(PerlIOSelf(f, PerlIOStdio)->stdio, Nullch, _IOLBF, 0);
3210 PerlIOStdio_get_base(pTHX_ PerlIO *f)
3212 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3213 return (STDCHAR*)PerlSIO_get_base(stdio);
3217 PerlIOStdio_get_bufsiz(pTHX_ PerlIO *f)
3219 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3220 return PerlSIO_get_bufsiz(stdio);
3224 #ifdef USE_STDIO_PTR
3226 PerlIOStdio_get_ptr(pTHX_ PerlIO *f)
3228 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3229 return (STDCHAR*)PerlSIO_get_ptr(stdio);
3233 PerlIOStdio_get_cnt(pTHX_ PerlIO *f)
3235 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3236 return PerlSIO_get_cnt(stdio);
3240 PerlIOStdio_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
3242 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3244 #ifdef STDIO_PTR_LVALUE
3245 PerlSIO_set_ptr(stdio, (void*)ptr); /* LHS STDCHAR* cast non-portable */
3246 #ifdef STDIO_PTR_LVAL_SETS_CNT
3247 if (PerlSIO_get_cnt(stdio) != (cnt)) {
3248 assert(PerlSIO_get_cnt(stdio) == (cnt));
3251 #if (!defined(STDIO_PTR_LVAL_NOCHANGE_CNT))
3253 * Setting ptr _does_ change cnt - we are done
3257 #else /* STDIO_PTR_LVALUE */
3259 #endif /* STDIO_PTR_LVALUE */
3262 * Now (or only) set cnt
3264 #ifdef STDIO_CNT_LVALUE
3265 PerlSIO_set_cnt(stdio, cnt);
3266 #else /* STDIO_CNT_LVALUE */
3267 #if (defined(STDIO_PTR_LVALUE) && defined(STDIO_PTR_LVAL_SETS_CNT))
3268 PerlSIO_set_ptr(stdio,
3269 PerlSIO_get_ptr(stdio) + (PerlSIO_get_cnt(stdio) -
3271 #else /* STDIO_PTR_LVAL_SETS_CNT */
3273 #endif /* STDIO_PTR_LVAL_SETS_CNT */
3274 #endif /* STDIO_CNT_LVALUE */
3281 PerlIOStdio_fill(pTHX_ PerlIO *f)
3283 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3286 * fflush()ing read-only streams can cause trouble on some stdio-s
3288 if ((PerlIOBase(f)->flags & PERLIO_F_CANWRITE)) {
3289 if (PerlSIO_fflush(stdio) != 0)
3292 c = PerlSIO_fgetc(stdio);
3296 #if (defined(STDIO_PTR_LVALUE) && (defined(STDIO_CNT_LVALUE) || defined(STDIO_PTR_LVAL_SETS_CNT)))
3298 #ifdef STDIO_BUFFER_WRITABLE
3299 if (PerlIO_fast_gets(f) && PerlIO_has_base(f)) {
3300 /* Fake ungetc() to the real buffer in case system's ungetc
3303 STDCHAR *base = (STDCHAR*)PerlSIO_get_base(stdio);
3304 SSize_t cnt = PerlSIO_get_cnt(stdio);
3305 STDCHAR *ptr = (STDCHAR*)PerlSIO_get_ptr(stdio);
3306 if (ptr == base+1) {
3307 *--ptr = (STDCHAR) c;
3308 PerlIOStdio_set_ptrcnt(aTHX_ f,ptr,cnt+1);
3309 if (PerlSIO_feof(stdio))
3310 PerlSIO_clearerr(stdio);
3316 if (PerlIO_has_cntptr(f)) {
3318 if (PerlIOStdio_unread(aTHX_ f,&ch,1) == 1) {
3325 /* An ungetc()d char is handled separately from the regular
3326 * buffer, so we stuff it in the buffer ourselves.
3327 * Should never get called as should hit code above
3329 *(--((*stdio)->_ptr)) = (unsigned char) c;
3332 /* If buffer snoop scheme above fails fall back to
3335 if (PerlSIO_ungetc(c, stdio) != c)
3343 PERLIO_FUNCS_DECL(PerlIO_stdio) = {
3344 sizeof(PerlIO_funcs),
3346 sizeof(PerlIOStdio),
3347 PERLIO_K_BUFFERED|PERLIO_K_RAW,
3351 PerlIOBase_binmode, /* binmode */
3365 PerlIOStdio_clearerr,
3366 PerlIOStdio_setlinebuf,
3368 PerlIOStdio_get_base,
3369 PerlIOStdio_get_bufsiz,
3374 #ifdef USE_STDIO_PTR
3375 PerlIOStdio_get_ptr,
3376 PerlIOStdio_get_cnt,
3377 # if defined(HAS_FAST_STDIO) && defined(USE_FAST_STDIO)
3378 PerlIOStdio_set_ptrcnt,
3381 # endif /* HAS_FAST_STDIO && USE_FAST_STDIO */
3386 #endif /* USE_STDIO_PTR */
3389 /* Note that calls to PerlIO_exportFILE() are reversed using
3390 * PerlIO_releaseFILE(), not importFILE. */
3392 PerlIO_exportFILE(PerlIO * f, const char *mode)
3396 if (PerlIOValid(f)) {
3399 if (!mode || !*mode) {
3400 mode = PerlIO_modestr(f, buf);
3402 stdio = PerlSIO_fdopen(PerlIO_fileno(f), mode);
3406 /* De-link any lower layers so new :stdio sticks */
3408 if ((f2 = PerlIO_push(aTHX_ f, PERLIO_FUNCS_CAST(&PerlIO_stdio), buf, Nullsv))) {
3409 PerlIOStdio *s = PerlIOSelf((f = f2), PerlIOStdio);
3411 /* Link previous lower layers under new one */
3415 /* restore layers list */
3425 PerlIO_findFILE(PerlIO *f)
3429 if (l->tab == &PerlIO_stdio) {
3430 PerlIOStdio *s = PerlIOSelf(&l, PerlIOStdio);
3433 l = *PerlIONext(&l);
3435 /* Uses fallback "mode" via PerlIO_modestr() in PerlIO_exportFILE */
3436 return PerlIO_exportFILE(f, Nullch);
3439 /* Use this to reverse PerlIO_exportFILE calls. */
3441 PerlIO_releaseFILE(PerlIO *p, FILE *f)
3446 if (l->tab == &PerlIO_stdio) {
3447 PerlIOStdio *s = PerlIOSelf(&l, PerlIOStdio);
3448 if (s->stdio == f) {
3450 PerlIO_pop(aTHX_ p);
3459 /*--------------------------------------------------------------------------------------*/
3461 * perlio buffer layer
3465 PerlIOBuf_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
3467 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3468 const int fd = PerlIO_fileno(f);
3469 if (fd >= 0 && PerlLIO_isatty(fd)) {
3470 PerlIOBase(f)->flags |= PERLIO_F_LINEBUF | PERLIO_F_TTY;
3472 if (*PerlIONext(f)) {
3473 const Off_t posn = PerlIO_tell(PerlIONext(f));
3474 if (posn != (Off_t) - 1) {
3478 return PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
3482 PerlIOBuf_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
3483 IV n, const char *mode, int fd, int imode, int perm,
3484 PerlIO *f, int narg, SV **args)
3486 if (PerlIOValid(f)) {
3487 PerlIO *next = PerlIONext(f);
3489 PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIOBase(next)->tab);
3490 if (tab && tab->Open)
3492 (*tab->Open)(aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
3494 if (!next || (*PerlIOBase(f)->tab->Pushed) (aTHX_ f, mode, PerlIOArg, self) != 0) {
3499 PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIO_default_btm());
3501 if (*mode == IoTYPE_IMPLICIT) {
3507 if (tab && tab->Open)
3508 f = (*tab->Open)(aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
3511 SETERRNO(EINVAL, LIB_INVARG);
3513 if (PerlIO_push(aTHX_ f, self, mode, PerlIOArg) == 0) {
3515 * if push fails during open, open fails. close will pop us.
3520 fd = PerlIO_fileno(f);
3521 if (init && fd == 2) {
3523 * Initial stderr is unbuffered
3525 PerlIOBase(f)->flags |= PERLIO_F_UNBUF;
3527 #ifdef PERLIO_USING_CRLF
3528 # ifdef PERLIO_IS_BINMODE_FD
3529 if (PERLIO_IS_BINMODE_FD(fd))
3530 PerlIO_binmode(aTHX_ f, '<'/*not used*/, O_BINARY, Nullch);
3534 * do something about failing setmode()? --jhi
3536 PerlLIO_setmode(fd, O_BINARY);
3545 * This "flush" is akin to sfio's sync in that it handles files in either
3546 * read or write state. For write state, we put the postponed data through
3547 * the next layers. For read state, we seek() the next layers to the
3548 * offset given by current position in the buffer, and discard the buffer
3549 * state (XXXX supposed to be for seek()able buffers only, but now it is done
3550 * in any case?). Then the pass the stick further in chain.
3553 PerlIOBuf_flush(pTHX_ PerlIO *f)
3555 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3557 PerlIO *n = PerlIONext(f);
3558 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF) {
3560 * write() the buffer
3562 const STDCHAR *buf = b->buf;
3563 const STDCHAR *p = buf;
3564 while (p < b->ptr) {
3565 SSize_t count = PerlIO_write(n, p, b->ptr - p);
3569 else if (count < 0 || PerlIO_error(n)) {
3570 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
3575 b->posn += (p - buf);
3577 else if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
3578 STDCHAR *buf = PerlIO_get_base(f);
3580 * Note position change
3582 b->posn += (b->ptr - buf);
3583 if (b->ptr < b->end) {
3584 /* We did not consume all of it - try and seek downstream to
3585 our logical position
3587 if (PerlIOValid(n) && PerlIO_seek(n, b->posn, SEEK_SET) == 0) {
3588 /* Reload n as some layers may pop themselves on seek */
3589 b->posn = PerlIO_tell(n = PerlIONext(f));
3592 /* Seek failed (e.g. pipe or tty). Do NOT clear buffer or pre-read
3593 data is lost for good - so return saying "ok" having undone
3596 b->posn -= (b->ptr - buf);
3601 b->ptr = b->end = b->buf;
3602 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
3603 /* We check for Valid because of dubious decision to make PerlIO_flush(NULL) flush all */
3604 if (PerlIOValid(n) && PerlIO_flush(n) != 0)
3609 /* This discards the content of the buffer after b->ptr, and rereads
3610 * the buffer from the position off in the layer downstream; here off
3611 * is at offset corresponding to b->ptr - b->buf.
3614 PerlIOBuf_fill(pTHX_ PerlIO *f)
3616 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3617 PerlIO *n = PerlIONext(f);
3620 * Down-stream flush is defined not to loose read data so is harmless.
3621 * we would not normally be fill'ing if there was data left in anycase.
3623 if (PerlIO_flush(f) != 0) /* XXXX Check that its seek() succeeded?! */
3625 if (PerlIOBase(f)->flags & PERLIO_F_TTY)
3626 PerlIOBase_flush_linebuf(aTHX);
3629 PerlIO_get_base(f); /* allocate via vtable */
3631 b->ptr = b->end = b->buf;
3633 if (!PerlIOValid(n)) {
3634 PerlIOBase(f)->flags |= PERLIO_F_EOF;
3638 if (PerlIO_fast_gets(n)) {
3640 * Layer below is also buffered. We do _NOT_ want to call its
3641 * ->Read() because that will loop till it gets what we asked for
3642 * which may hang on a pipe etc. Instead take anything it has to
3643 * hand, or ask it to fill _once_.
3645 avail = PerlIO_get_cnt(n);
3647 avail = PerlIO_fill(n);
3649 avail = PerlIO_get_cnt(n);
3651 if (!PerlIO_error(n) && PerlIO_eof(n))
3656 STDCHAR *ptr = PerlIO_get_ptr(n);
3657 const SSize_t cnt = avail;
3658 if (avail > (SSize_t)b->bufsiz)
3660 Copy(ptr, b->buf, avail, STDCHAR);
3661 PerlIO_set_ptrcnt(n, ptr + avail, cnt - avail);
3665 avail = PerlIO_read(n, b->ptr, b->bufsiz);
3669 PerlIOBase(f)->flags |= PERLIO_F_EOF;
3671 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
3674 b->end = b->buf + avail;
3675 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3680 PerlIOBuf_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
3682 if (PerlIOValid(f)) {
3683 const PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3686 return PerlIOBase_read(aTHX_ f, vbuf, count);
3692 PerlIOBuf_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3694 const STDCHAR *buf = (const STDCHAR *) vbuf + count;
3695 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3698 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
3703 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
3705 * Buffer is already a read buffer, we can overwrite any chars
3706 * which have been read back to buffer start
3708 avail = (b->ptr - b->buf);
3712 * Buffer is idle, set it up so whole buffer is available for
3716 b->end = b->buf + avail;
3718 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3720 * Buffer extends _back_ from where we are now
3722 b->posn -= b->bufsiz;
3724 if (avail > (SSize_t) count) {
3726 * If we have space for more than count, just move count
3734 * In simple stdio-like ungetc() case chars will be already
3737 if (buf != b->ptr) {
3738 Copy(buf, b->ptr, avail, STDCHAR);
3742 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
3746 unread += PerlIOBase_unread(aTHX_ f, vbuf, count);
3752 PerlIOBuf_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3754 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3755 const STDCHAR *buf = (const STDCHAR *) vbuf;
3756 const STDCHAR *flushptr = buf;
3760 if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
3762 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
3763 if (PerlIO_flush(f) != 0) {
3767 if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF) {
3768 flushptr = buf + count;
3769 while (flushptr > buf && *(flushptr - 1) != '\n')
3773 SSize_t avail = b->bufsiz - (b->ptr - b->buf);
3774 if ((SSize_t) count < avail)
3776 if (flushptr > buf && flushptr <= buf + avail)
3777 avail = flushptr - buf;
3778 PerlIOBase(f)->flags |= PERLIO_F_WRBUF;
3780 Copy(buf, b->ptr, avail, STDCHAR);
3785 if (buf == flushptr)
3788 if (b->ptr >= (b->buf + b->bufsiz))
3791 if (PerlIOBase(f)->flags & PERLIO_F_UNBUF)
3797 PerlIOBuf_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
3800 if ((code = PerlIO_flush(f)) == 0) {
3801 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
3802 code = PerlIO_seek(PerlIONext(f), offset, whence);
3804 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3805 b->posn = PerlIO_tell(PerlIONext(f));
3812 PerlIOBuf_tell(pTHX_ PerlIO *f)
3814 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3816 * b->posn is file position where b->buf was read, or will be written
3818 Off_t posn = b->posn;
3819 if ((PerlIOBase(f)->flags & PERLIO_F_APPEND) &&
3820 (PerlIOBase(f)->flags & PERLIO_F_WRBUF)) {
3822 /* As O_APPEND files are normally shared in some sense it is better
3827 /* when file is NOT shared then this is sufficient */
3828 PerlIO_seek(PerlIONext(f),0, SEEK_END);
3830 posn = b->posn = PerlIO_tell(PerlIONext(f));
3834 * If buffer is valid adjust position by amount in buffer
3836 posn += (b->ptr - b->buf);
3842 PerlIOBuf_popped(pTHX_ PerlIO *f)
3844 const IV code = PerlIOBase_popped(aTHX_ f);
3845 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3846 if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
3849 b->ptr = b->end = b->buf = NULL;
3850 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
3855 PerlIOBuf_close(pTHX_ PerlIO *f)
3857 const IV code = PerlIOBase_close(aTHX_ f);
3858 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3859 if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
3862 b->ptr = b->end = b->buf = NULL;
3863 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
3868 PerlIOBuf_get_ptr(pTHX_ PerlIO *f)
3870 const PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3877 PerlIOBuf_get_cnt(pTHX_ PerlIO *f)
3879 const PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3882 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF)
3883 return (b->end - b->ptr);
3888 PerlIOBuf_get_base(pTHX_ PerlIO *f)
3890 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3894 b->buf = Newxz(b->buf,b->bufsiz, STDCHAR);
3896 b->buf = (STDCHAR *) & b->oneword;
3897 b->bufsiz = sizeof(b->oneword);
3899 b->end = b->ptr = b->buf;
3905 PerlIOBuf_bufsiz(pTHX_ PerlIO *f)
3907 const PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3910 return (b->end - b->buf);
3914 PerlIOBuf_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
3916 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3920 if (PerlIO_get_cnt(f) != cnt || b->ptr < b->buf) {
3921 assert(PerlIO_get_cnt(f) == cnt);
3922 assert(b->ptr >= b->buf);
3924 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3928 PerlIOBuf_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
3930 return PerlIOBase_dup(aTHX_ f, o, param, flags);
3935 PERLIO_FUNCS_DECL(PerlIO_perlio) = {
3936 sizeof(PerlIO_funcs),
3939 PERLIO_K_BUFFERED|PERLIO_K_RAW,
3943 PerlIOBase_binmode, /* binmode */
3957 PerlIOBase_clearerr,
3958 PerlIOBase_setlinebuf,
3963 PerlIOBuf_set_ptrcnt,
3966 /*--------------------------------------------------------------------------------------*/
3968 * Temp layer to hold unread chars when cannot do it any other way
3972 PerlIOPending_fill(pTHX_ PerlIO *f)
3975 * Should never happen
3982 PerlIOPending_close(pTHX_ PerlIO *f)
3985 * A tad tricky - flush pops us, then we close new top
3988 return PerlIO_close(f);
3992 PerlIOPending_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
3995 * A tad tricky - flush pops us, then we seek new top
3998 return PerlIO_seek(f, offset, whence);
4003 PerlIOPending_flush(pTHX_ PerlIO *f)
4005 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
4006 if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
4010 PerlIO_pop(aTHX_ f);
4015 PerlIOPending_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
4021 PerlIOBuf_set_ptrcnt(aTHX_ f, ptr, cnt);
4026 PerlIOPending_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
4028 const IV code = PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
4029 PerlIOl * const l = PerlIOBase(f);
4031 * Our PerlIO_fast_gets must match what we are pushed on, or sv_gets()
4032 * etc. get muddled when it changes mid-string when we auto-pop.
4034 l->flags = (l->flags & ~(PERLIO_F_FASTGETS | PERLIO_F_UTF8)) |
4035 (PerlIOBase(PerlIONext(f))->
4036 flags & (PERLIO_F_FASTGETS | PERLIO_F_UTF8));
4041 PerlIOPending_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
4043 SSize_t avail = PerlIO_get_cnt(f);
4045 if ((SSize_t)count < avail)
4048 got = PerlIOBuf_read(aTHX_ f, vbuf, avail);
4049 if (got >= 0 && got < (SSize_t)count) {
4050 const SSize_t more =
4051 PerlIO_read(f, ((STDCHAR *) vbuf) + got, count - got);
4052 if (more >= 0 || got == 0)
4058 PERLIO_FUNCS_DECL(PerlIO_pending) = {
4059 sizeof(PerlIO_funcs),
4062 PERLIO_K_BUFFERED|PERLIO_K_RAW, /* not sure about RAW here */
4063 PerlIOPending_pushed,
4066 PerlIOBase_binmode, /* binmode */
4075 PerlIOPending_close,
4076 PerlIOPending_flush,
4080 PerlIOBase_clearerr,
4081 PerlIOBase_setlinebuf,
4086 PerlIOPending_set_ptrcnt,
4091 /*--------------------------------------------------------------------------------------*/
4093 * crlf - translation On read translate CR,LF to "\n" we do this by
4094 * overriding ptr/cnt entries to hand back a line at a time and keeping a
4095 * record of which nl we "lied" about. On write translate "\n" to CR,LF
4097 * c->nl points on the first byte of CR LF pair when it is temporarily
4098 * replaced by LF, or to the last CR of the buffer. In the former case
4099 * the caller thinks that the buffer ends at c->nl + 1, in the latter
4100 * that it ends at c->nl; these two cases can be distinguished by
4101 * *c->nl. c->nl is set during _getcnt() call, and unset during
4102 * _unread() and _flush() calls.
4103 * It only matters for read operations.
4107 PerlIOBuf base; /* PerlIOBuf stuff */
4108 STDCHAR *nl; /* Position of crlf we "lied" about in the
4113 PerlIOCrlf_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
4116 PerlIOBase(f)->flags |= PERLIO_F_CRLF;
4117 code = PerlIOBuf_pushed(aTHX_ f, mode, arg, tab);
4119 PerlIO_debug("PerlIOCrlf_pushed f=%p %s %s fl=%08" UVxf "\n",
4120 f, PerlIOBase(f)->tab->name, (mode) ? mode : "(Null)",
4121 PerlIOBase(f)->flags);
4124 /* Enable the first CRLF capable layer you can find, but if none
4125 * found, the one we just pushed is fine. This results in at
4126 * any given moment at most one CRLF-capable layer being enabled
4127 * in the whole layer stack. */
4128 PerlIO *g = PerlIONext(f);
4130 PerlIOl *b = PerlIOBase(g);
4131 if (b && b->tab == &PerlIO_crlf) {
4132 if (!(b->flags & PERLIO_F_CRLF))
4133 b->flags |= PERLIO_F_CRLF;
4134 PerlIO_pop(aTHX_ f);
4145 PerlIOCrlf_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
4147 PerlIOCrlf * const c = PerlIOSelf(f, PerlIOCrlf);
4148 if (c->nl) { /* XXXX Shouldn't it be done only if b->ptr > c->nl? */
4152 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF))
4153 return PerlIOBuf_unread(aTHX_ f, vbuf, count);
4155 const STDCHAR *buf = (const STDCHAR *) vbuf + count;
4156 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
4158 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
4163 if (!(PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
4164 b->end = b->ptr = b->buf + b->bufsiz;
4165 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
4166 b->posn -= b->bufsiz;
4168 while (count > 0 && b->ptr > b->buf) {
4169 const int ch = *--buf;
4171 if (b->ptr - 2 >= b->buf) {
4178 /* If b->ptr - 1 == b->buf, we are undoing reading 0xa */
4179 *--(b->ptr) = 0xa; /* Works even if 0xa == '\r' */
4195 /* XXXX This code assumes that buffer size >=2, but does not check it... */
4197 PerlIOCrlf_get_cnt(pTHX_ PerlIO *f)
4199 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
4202 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
4203 PerlIOCrlf * const c = PerlIOSelf(f, PerlIOCrlf);
4204 if ((PerlIOBase(f)->flags & PERLIO_F_CRLF) && (!c->nl || *c->nl == 0xd)) {
4205 STDCHAR *nl = (c->nl) ? c->nl : b->ptr;
4207 while (nl < b->end && *nl != 0xd)
4209 if (nl < b->end && *nl == 0xd) {
4211 if (nl + 1 < b->end) {
4218 * Not CR,LF but just CR
4226 * Blast - found CR as last char in buffer
4231 * They may not care, defer work as long as
4235 return (nl - b->ptr);
4239 b->ptr++; /* say we have read it as far as
4240 * flush() is concerned */
4241 b->buf++; /* Leave space in front of buffer */
4242 /* Note as we have moved buf up flush's
4244 will naturally make posn point at CR
4246 b->bufsiz--; /* Buffer is thus smaller */
4247 code = PerlIO_fill(f); /* Fetch some more */
4248 b->bufsiz++; /* Restore size for next time */
4249 b->buf--; /* Point at space */
4250 b->ptr = nl = b->buf; /* Which is what we hand
4252 *nl = 0xd; /* Fill in the CR */
4254 goto test; /* fill() call worked */
4256 * CR at EOF - just fall through
4258 /* Should we clear EOF though ??? */
4263 return (((c->nl) ? (c->nl + 1) : b->end) - b->ptr);
4269 PerlIOCrlf_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
4271 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
4272 PerlIOCrlf * const c = PerlIOSelf(f, PerlIOCrlf);
4278 if (ptr == b->end && *c->nl == 0xd) {
4279 /* Defered CR at end of buffer case - we lied about count */
4291 * Test code - delete when it works ...
4293 IV flags = PerlIOBase(f)->flags;
4294 STDCHAR *chk = (c->nl) ? (c->nl+1) : b->end;
4295 if (ptr+cnt == c->nl && c->nl+1 == b->end && *c->nl == 0xd) {
4296 /* Defered CR at end of buffer case - we lied about count */
4302 Perl_croak(aTHX_ "ptr wrong %p != %p fl=%08" UVxf
4303 " nl=%p e=%p for %d", ptr, chk, flags, c->nl,
4311 * They have taken what we lied about
4319 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
4323 PerlIOCrlf_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
4325 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF))
4326 return PerlIOBuf_write(aTHX_ f, vbuf, count);
4328 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
4329 const STDCHAR *buf = (const STDCHAR *) vbuf;
4330 const STDCHAR * const ebuf = buf + count;
4333 if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
4335 while (buf < ebuf) {
4336 const STDCHAR * const eptr = b->buf + b->bufsiz;
4337 PerlIOBase(f)->flags |= PERLIO_F_WRBUF;
4338 while (buf < ebuf && b->ptr < eptr) {
4340 if ((b->ptr + 2) > eptr) {
4348 *(b->ptr)++ = 0xd; /* CR */
4349 *(b->ptr)++ = 0xa; /* LF */
4351 if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF) {
4358 *(b->ptr)++ = *buf++;
4360 if (b->ptr >= eptr) {
4366 if (PerlIOBase(f)->flags & PERLIO_F_UNBUF)
4368 return (buf - (STDCHAR *) vbuf);
4373 PerlIOCrlf_flush(pTHX_ PerlIO *f)
4375 PerlIOCrlf * const c = PerlIOSelf(f, PerlIOCrlf);
4380 return PerlIOBuf_flush(aTHX_ f);
4384 PerlIOCrlf_binmode(pTHX_ PerlIO *f)
4386 if ((PerlIOBase(f)->flags & PERLIO_F_CRLF)) {
4387 /* In text mode - flush any pending stuff and flip it */
4388 PerlIOBase(f)->flags &= ~PERLIO_F_CRLF;
4389 #ifndef PERLIO_USING_CRLF
4390 /* CRLF is unusual case - if this is just the :crlf layer pop it */
4391 if (PerlIOBase(f)->tab == &PerlIO_crlf) {
4392 PerlIO_pop(aTHX_ f);
4399 PERLIO_FUNCS_DECL(PerlIO_crlf) = {
4400 sizeof(PerlIO_funcs),
4403 PERLIO_K_BUFFERED | PERLIO_K_CANCRLF | PERLIO_K_RAW,
4405 PerlIOBuf_popped, /* popped */
4407 PerlIOCrlf_binmode, /* binmode */
4411 PerlIOBuf_read, /* generic read works with ptr/cnt lies */
4412 PerlIOCrlf_unread, /* Put CR,LF in buffer for each '\n' */
4413 PerlIOCrlf_write, /* Put CR,LF in buffer for each '\n' */
4421 PerlIOBase_clearerr,
4422 PerlIOBase_setlinebuf,
4427 PerlIOCrlf_set_ptrcnt,
4431 /*--------------------------------------------------------------------------------------*/
4433 * mmap as "buffer" layer
4437 PerlIOBuf base; /* PerlIOBuf stuff */
4438 Mmap_t mptr; /* Mapped address */
4439 Size_t len; /* mapped length */
4440 STDCHAR *bbuf; /* malloced buffer if map fails */
4444 PerlIOMmap_map(pTHX_ PerlIO *f)
4447 PerlIOMmap * const m = PerlIOSelf(f, PerlIOMmap);
4448 const IV flags = PerlIOBase(f)->flags;
4452 if (flags & PERLIO_F_CANREAD) {
4453 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
4454 const int fd = PerlIO_fileno(f);
4456 code = Fstat(fd, &st);
4457 if (code == 0 && S_ISREG(st.st_mode)) {
4458 SSize_t len = st.st_size - b->posn;
4461 if (PL_mmap_page_size <= 0)
4462 Perl_croak(aTHX_ "panic: bad pagesize %" IVdf,
4466 * This is a hack - should never happen - open should
4469 b->posn = PerlIO_tell(PerlIONext(f));
4471 posn = (b->posn / PL_mmap_page_size) * PL_mmap_page_size;
4472 len = st.st_size - posn;
4473 m->mptr = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, posn);
4474 if (m->mptr && m->mptr != (Mmap_t) - 1) {
4475 #if 0 && defined(HAS_MADVISE) && defined(MADV_SEQUENTIAL)
4476 madvise(m->mptr, len, MADV_SEQUENTIAL);
4478 #if 0 && defined(HAS_MADVISE) && defined(MADV_WILLNEED)
4479 madvise(m->mptr, len, MADV_WILLNEED);
4481 PerlIOBase(f)->flags =
4482 (flags & ~PERLIO_F_EOF) | PERLIO_F_RDBUF;
4483 b->end = ((STDCHAR *) m->mptr) + len;
4484 b->buf = ((STDCHAR *) m->mptr) + (b->posn - posn);
4493 PerlIOBase(f)->flags =
4494 flags | PERLIO_F_EOF | PERLIO_F_RDBUF;
4496 b->ptr = b->end = b->ptr;
4505 PerlIOMmap_unmap(pTHX_ PerlIO *f)
4507 PerlIOMmap * const m = PerlIOSelf(f, PerlIOMmap);
4510 PerlIOBuf * const b = &m->base;
4512 code = munmap(m->mptr, m->len);
4516 if (PerlIO_seek(PerlIONext(f), b->posn, SEEK_SET) != 0)
4519 b->ptr = b->end = b->buf;
4520 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
4526 PerlIOMmap_get_base(pTHX_ PerlIO *f)
4528 PerlIOMmap * const m = PerlIOSelf(f, PerlIOMmap);
4529 PerlIOBuf * const b = &m->base;
4530 if (b->buf && (PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
4532 * Already have a readbuffer in progress
4538 * We have a write buffer or flushed PerlIOBuf read buffer
4540 m->bbuf = b->buf; /* save it in case we need it again */
4541 b->buf = NULL; /* Clear to trigger below */
4544 PerlIOMmap_map(aTHX_ f); /* Try and map it */
4547 * Map did not work - recover PerlIOBuf buffer if we have one
4552 b->ptr = b->end = b->buf;
4555 return PerlIOBuf_get_base(aTHX_ f);
4559 PerlIOMmap_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
4561 PerlIOMmap * const m = PerlIOSelf(f, PerlIOMmap);
4562 PerlIOBuf * const b = &m->base;
4563 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
4565 if (b->ptr && (b->ptr - count) >= b->buf
4566 && memEQ(b->ptr - count, vbuf, count)) {
4568 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
4573 * Loose the unwritable mapped buffer
4577 * If flush took the "buffer" see if we have one from before
4579 if (!b->buf && m->bbuf)
4582 PerlIOBuf_get_base(aTHX_ f);
4586 return PerlIOBuf_unread(aTHX_ f, vbuf, count);
4590 PerlIOMmap_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
4592 PerlIOMmap * const m = PerlIOSelf(f, PerlIOMmap);
4593 PerlIOBuf * const b = &m->base;
4595 if (!b->buf || !(PerlIOBase(f)->flags & PERLIO_F_WRBUF)) {
4597 * No, or wrong sort of, buffer
4600 if (PerlIOMmap_unmap(aTHX_ f) != 0)
4604 * If unmap took the "buffer" see if we have one from before
4606 if (!b->buf && m->bbuf)
4609 PerlIOBuf_get_base(aTHX_ f);
4613 return PerlIOBuf_write(aTHX_ f, vbuf, count);
4617 PerlIOMmap_flush(pTHX_ PerlIO *f)
4619 PerlIOMmap * const m = PerlIOSelf(f, PerlIOMmap);
4620 PerlIOBuf * const b = &m->base;
4621 IV code = PerlIOBuf_flush(aTHX_ f);
4623 * Now we are "synced" at PerlIOBuf level
4630 if (PerlIOMmap_unmap(aTHX_ f) != 0)
4635 * We seem to have a PerlIOBuf buffer which was not mapped
4636 * remember it in case we need one later
4645 PerlIOMmap_fill(pTHX_ PerlIO *f)
4647 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
4648 IV code = PerlIO_flush(f);
4649 if (code == 0 && !b->buf) {
4650 code = PerlIOMmap_map(aTHX_ f);
4652 if (code == 0 && !(PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
4653 code = PerlIOBuf_fill(aTHX_ f);
4659 PerlIOMmap_close(pTHX_ PerlIO *f)
4661 PerlIOMmap * const m = PerlIOSelf(f, PerlIOMmap);
4662 PerlIOBuf * const b = &m->base;
4663 IV code = PerlIO_flush(f);
4667 b->ptr = b->end = b->buf;
4669 if (PerlIOBuf_close(aTHX_ f) != 0)
4675 PerlIOMmap_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
4677 return PerlIOBase_dup(aTHX_ f, o, param, flags);
4681 PERLIO_FUNCS_DECL(PerlIO_mmap) = {
4682 sizeof(PerlIO_funcs),
4685 PERLIO_K_BUFFERED|PERLIO_K_RAW,
4689 PerlIOBase_binmode, /* binmode */
4703 PerlIOBase_clearerr,
4704 PerlIOBase_setlinebuf,
4705 PerlIOMmap_get_base,
4709 PerlIOBuf_set_ptrcnt,
4712 #endif /* HAS_MMAP */
4715 Perl_PerlIO_stdin(pTHX)
4718 PerlIO_stdstreams(aTHX);
4720 return &PL_perlio[1];
4724 Perl_PerlIO_stdout(pTHX)
4727 PerlIO_stdstreams(aTHX);
4729 return &PL_perlio[2];
4733 Perl_PerlIO_stderr(pTHX)
4736 PerlIO_stdstreams(aTHX);
4738 return &PL_perlio[3];
4741 /*--------------------------------------------------------------------------------------*/
4744 PerlIO_getname(PerlIO *f, char *buf)
4749 bool exported = FALSE;
4750 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
4752 stdio = PerlIO_exportFILE(f,0);
4756 name = fgetname(stdio, buf);
4757 if (exported) PerlIO_releaseFILE(f,stdio);
4762 PERL_UNUSED_ARG(buf);
4763 Perl_croak(aTHX_ "Don't know how to get file name");
4769 /*--------------------------------------------------------------------------------------*/
4771 * Functions which can be called on any kind of PerlIO implemented in
4775 #undef PerlIO_fdopen
4777 PerlIO_fdopen(int fd, const char *mode)
4780 return PerlIO_openn(aTHX_ Nullch, mode, fd, 0, 0, NULL, 0, NULL);
4785 PerlIO_open(const char *path, const char *mode)
4788 SV *name = sv_2mortal(newSVpv(path, 0));
4789 return PerlIO_openn(aTHX_ Nullch, mode, -1, 0, 0, NULL, 1, &name);
4792 #undef Perlio_reopen
4794 PerlIO_reopen(const char *path, const char *mode, PerlIO *f)
4797 SV *name = sv_2mortal(newSVpv(path,0));
4798 return PerlIO_openn(aTHX_ Nullch, mode, -1, 0, 0, f, 1, &name);
4803 PerlIO_getc(PerlIO *f)
4807 if ( 1 == PerlIO_read(f, buf, 1) ) {
4808 return (unsigned char) buf[0];
4813 #undef PerlIO_ungetc
4815 PerlIO_ungetc(PerlIO *f, int ch)
4820 if (PerlIO_unread(f, &buf, 1) == 1)
4828 PerlIO_putc(PerlIO *f, int ch)
4832 return PerlIO_write(f, &buf, 1);
4837 PerlIO_puts(PerlIO *f, const char *s)
4840 return PerlIO_write(f, s, strlen(s));
4843 #undef PerlIO_rewind
4845 PerlIO_rewind(PerlIO *f)
4848 PerlIO_seek(f, (Off_t) 0, SEEK_SET);
4852 #undef PerlIO_vprintf
4854 PerlIO_vprintf(PerlIO *f, const char *fmt, va_list ap)
4857 SV * const sv = newSVpvn("", 0);
4863 Perl_va_copy(ap, apc);
4864 sv_vcatpvf(sv, fmt, &apc);
4866 sv_vcatpvf(sv, fmt, &ap);
4868 s = SvPV_const(sv, len);
4869 wrote = PerlIO_write(f, s, len);
4874 #undef PerlIO_printf
4876 PerlIO_printf(PerlIO *f, const char *fmt, ...)
4881 result = PerlIO_vprintf(f, fmt, ap);
4886 #undef PerlIO_stdoutf
4888 PerlIO_stdoutf(const char *fmt, ...)
4894 result = PerlIO_vprintf(PerlIO_stdout(), fmt, ap);
4899 #undef PerlIO_tmpfile
4901 PerlIO_tmpfile(void)
4906 const int fd = win32_tmpfd();
4908 f = PerlIO_fdopen(fd, "w+b");
4910 # if defined(HAS_MKSTEMP) && ! defined(VMS) && ! defined(OS2)
4911 SV * const sv = newSVpv("/tmp/PerlIO_XXXXXX", 0);
4913 * I have no idea how portable mkstemp() is ... NI-S
4915 const int fd = mkstemp(SvPVX(sv));
4917 f = PerlIO_fdopen(fd, "w+");
4919 PerlIOBase(f)->flags |= PERLIO_F_TEMP;
4920 PerlLIO_unlink(SvPVX_const(sv));
4923 # else /* !HAS_MKSTEMP, fallback to stdio tmpfile(). */
4924 FILE * const stdio = PerlSIO_tmpfile();
4927 if ((f = PerlIO_push(aTHX_(PerlIO_allocate(aTHX)),
4928 PERLIO_FUNCS_CAST(&PerlIO_stdio),
4930 PerlIOStdio * const s = PerlIOSelf(f, PerlIOStdio);
4936 # endif /* else HAS_MKSTEMP */
4937 #endif /* else WIN32 */
4944 #endif /* USE_SFIO */
4945 #endif /* PERLIO_IS_STDIO */
4947 /*======================================================================================*/
4949 * Now some functions in terms of above which may be needed even if we are
4950 * not in true PerlIO mode
4954 #undef PerlIO_setpos
4956 PerlIO_setpos(PerlIO *f, SV *pos)
4961 const Off_t * const posn = (Off_t *) SvPV(pos, len);
4962 if (f && len == sizeof(Off_t))
4963 return PerlIO_seek(f, *posn, SEEK_SET);
4965 SETERRNO(EINVAL, SS_IVCHAN);
4969 #undef PerlIO_setpos
4971 PerlIO_setpos(PerlIO *f, SV *pos)
4976 Fpos_t * const fpos = (Fpos_t *) SvPV(pos, len);
4977 if (f && len == sizeof(Fpos_t)) {
4978 #if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64)
4979 return fsetpos64(f, fpos);
4981 return fsetpos(f, fpos);
4985 SETERRNO(EINVAL, SS_IVCHAN);
4991 #undef PerlIO_getpos
4993 PerlIO_getpos(PerlIO *f, SV *pos)
4996 Off_t posn = PerlIO_tell(f);
4997 sv_setpvn(pos, (char *) &posn, sizeof(posn));
4998 return (posn == (Off_t) - 1) ? -1 : 0;
5001 #undef PerlIO_getpos
5003 PerlIO_getpos(PerlIO *f, SV *pos)
5008 #if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64)
5009 code = fgetpos64(f, &fpos);
5011 code = fgetpos(f, &fpos);
5013 sv_setpvn(pos, (char *) &fpos, sizeof(fpos));
5018 #if (defined(PERLIO_IS_STDIO) || !defined(USE_SFIO)) && !defined(HAS_VPRINTF)
5021 vprintf(char *pat, char *args)
5023 _doprnt(pat, args, stdout);
5024 return 0; /* wrong, but perl doesn't use the return
5029 vfprintf(FILE *fd, char *pat, char *args)
5031 _doprnt(pat, args, fd);
5032 return 0; /* wrong, but perl doesn't use the return
5038 #ifndef PerlIO_vsprintf
5040 PerlIO_vsprintf(char *s, int n, const char *fmt, va_list ap)
5043 const int val = vsprintf(s, fmt, ap);
5045 if (strlen(s) >= (STRLEN) n) {
5047 (void) PerlIO_puts(Perl_error_log,
5048 "panic: sprintf overflow - memory corrupted!\n");
5056 #ifndef PerlIO_sprintf
5058 PerlIO_sprintf(char *s, int n, const char *fmt, ...)
5063 result = PerlIO_vsprintf(s, n, fmt, ap);
5071 * c-indentation-style: bsd
5073 * indent-tabs-mode: t
5076 * ex: set ts=8 sts=4 sw=4 noet: