2 * perlio.c Copyright (c) 1996-2006, 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 of
142 * the translation mode of the fd in the Borland RTL (heavy
143 * digging through their runtime sources reveal). User has to set
144 * the mode explicitly for the stream (though they don't document
145 * this anywhere). GSAR 97-5-24
151 fp->flags &= ~_F_BIN;
159 # if defined(USEMYBINMODE)
161 if (my_binmode(fp, iotype, mode) != FALSE)
167 PERL_UNUSED_ARG(iotype);
168 PERL_UNUSED_ARG(mode);
176 #define O_ACCMODE 3 /* Assume traditional implementation */
180 PerlIO_intmode2str(int rawmode, char *mode, int *writing)
182 const int result = rawmode & O_ACCMODE;
187 ptype = IoTYPE_RDONLY;
190 ptype = IoTYPE_WRONLY;
198 *writing = (result != O_RDONLY);
200 if (result == O_RDONLY) {
204 else if (rawmode & O_APPEND) {
206 if (result != O_WRONLY)
211 if (result == O_WRONLY)
218 if (rawmode & O_BINARY)
224 #ifndef PERLIO_LAYERS
226 PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names)
228 if (!names || !*names
229 || strEQ(names, ":crlf")
230 || strEQ(names, ":raw")
231 || strEQ(names, ":bytes")
235 Perl_croak(aTHX_ "Cannot apply \"%s\" in non-PerlIO perl", names);
243 PerlIO_destruct(pTHX)
248 PerlIO_binmode(pTHX_ PerlIO *fp, int iotype, int mode, const char *names)
251 PERL_UNUSED_ARG(iotype);
252 PERL_UNUSED_ARG(mode);
253 PERL_UNUSED_ARG(names);
256 return perlsio_binmode(fp, iotype, mode);
261 PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
263 #if defined(PERL_MICRO) || defined(SYMBIAN)
266 #ifdef PERL_IMPLICIT_SYS
267 return PerlSIO_fdupopen(f);
270 return win32_fdupopen(f);
273 const int fd = PerlLIO_dup(PerlIO_fileno(f));
276 int omode = fcntl(fd, F_GETFL);
278 omode = djgpp_get_stream_mode(f);
280 PerlIO_intmode2str(omode,mode,NULL);
281 /* the r+ is a hack */
282 return PerlIO_fdopen(fd, mode);
287 SETERRNO(EBADF, SS_IVCHAN);
297 * De-mux PerlIO_openn() into fdopen, freopen and fopen type entries
301 PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd,
302 int imode, int perm, PerlIO *old, int narg, SV **args)
306 Perl_croak(aTHX_ "More than one argument to open");
308 if (*args == &PL_sv_undef)
309 return PerlIO_tmpfile();
311 const char *name = SvPV_nolen_const(*args);
312 if (*mode == IoTYPE_NUMERIC) {
313 fd = PerlLIO_open3(name, imode, perm);
315 return PerlIO_fdopen(fd, mode + 1);
318 return PerlIO_reopen(name, mode, old);
321 return PerlIO_open(name, mode);
326 return PerlIO_fdopen(fd, (char *) mode);
331 XS(XS_PerlIO__Layer__find)
335 Perl_croak(aTHX_ "Usage class->find(name[,load])");
337 const char *name = SvPV_nolen_const(ST(1));
338 ST(0) = (strEQ(name, "crlf")
339 || strEQ(name, "raw")) ? &PL_sv_yes : &PL_sv_undef;
346 Perl_boot_core_PerlIO(pTHX)
348 newXS("PerlIO::Layer::find", XS_PerlIO__Layer__find, __FILE__);
354 #ifdef PERLIO_IS_STDIO
360 * Does nothing (yet) except force this file to be included in perl
361 * binary. That allows this file to force inclusion of other functions
362 * that may be required by loadable extensions e.g. for
363 * FileHandle::tmpfile
367 #undef PerlIO_tmpfile
374 #else /* PERLIO_IS_STDIO */
382 * This section is just to make sure these functions get pulled in from
386 #undef PerlIO_tmpfile
397 * Force this file to be included in perl binary. Which allows this
398 * file to force inclusion of other functions that may be required by
399 * loadable extensions e.g. for FileHandle::tmpfile
403 * Hack sfio does its own 'autoflush' on stdout in common cases. Flush
404 * results in a lot of lseek()s to regular files and lot of small
407 sfset(sfstdout, SF_SHARE, 0);
410 /* This is not the reverse of PerlIO_exportFILE(), PerlIO_releaseFILE() is. */
412 PerlIO_importFILE(FILE *stdio, const char *mode)
414 const int fd = fileno(stdio);
415 if (!mode || !*mode) {
418 return PerlIO_fdopen(fd, mode);
422 PerlIO_findFILE(PerlIO *pio)
424 const int fd = PerlIO_fileno(pio);
425 FILE * const f = fdopen(fd, "r+");
427 if (!f && errno == EINVAL)
429 if (!f && errno == EINVAL)
436 /*======================================================================================*/
438 * Implement all the PerlIO interface ourselves.
444 * We _MUST_ have <unistd.h> if we are using lseek() and may have large
451 #include <sys/mman.h>
455 PerlIO_debug(const char *fmt, ...)
461 if (!dbg && !PL_tainting && PL_uid == PL_euid && PL_gid == PL_egid) {
462 const char *s = PerlEnv_getenv("PERLIO_DEBUG");
464 dbg = PerlLIO_open3(s, O_WRONLY | O_CREAT | O_APPEND, 0666);
470 const char *s = CopFILE(PL_curcop);
473 /* Use fixed buffer as sv_catpvf etc. needs SVs */
477 len = sprintf(buffer, "%.40s:%" IVdf " ", s, (IV) CopLINE(PL_curcop));
478 vsprintf(buffer+len, fmt, ap);
479 PerlLIO_write(dbg, buffer, strlen(buffer));
481 SV *sv = newSVpvn("", 0);
484 Perl_sv_catpvf(aTHX_ sv, "%s:%" IVdf " ", s,
485 (IV) CopLINE(PL_curcop));
486 Perl_sv_vcatpvf(aTHX_ sv, fmt, &ap);
488 s = SvPV_const(sv, len);
489 PerlLIO_write(dbg, 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 *table = *tablep;
558 PerlIO_cleantable(aTHX_(PerlIO **) & (table[0]));
559 for (i = PERLIO_TABLE_SIZE - 1; i > 0; i--) {
560 PerlIO *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)
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)
720 if (PerlIOValid(f)) {
721 PerlIOl *l = PerlIOBase(f);
724 SV *name = l->tab && l->tab->name ?
725 newSVpv(l->tab->name, 0) : &PL_sv_undef;
726 SV *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)
747 if ((SSize_t) len <= 0)
749 for (i = 0; i < PL_known_layers->cur; i++) {
750 PerlIO_funcs * const f = PL_known_layers->array[i].funcs;
751 if (memEQ(f->name, name, len) && f->name[len] == 0) {
752 PerlIO_debug("%.*s => %p\n", (int) len, name, (void*)f);
756 if (load && PL_subname && PL_def_layerlist
757 && PL_def_layerlist->cur >= 2) {
758 if (PL_in_load_module) {
759 Perl_croak(aTHX_ "Recursive call to Perl_load_module in PerlIO_find_layer");
762 SV * const pkgsv = newSVpvn("PerlIO", 6);
763 SV * const layer = newSVpvn(name, len);
764 CV * const cv = get_cv("PerlIO::Layer::NoWarnings", FALSE);
766 SAVEINT(PL_in_load_module);
768 SAVEGENERICSV(PL_warnhook);
769 (void)SvREFCNT_inc(cv);
770 PL_warnhook = (SV *) cv;
774 * The two SVs are magically freed by load_module
776 Perl_load_module(aTHX_ 0, pkgsv, Nullsv, layer, Nullsv);
779 return PerlIO_find_layer(aTHX_ name, len, 0);
782 PerlIO_debug("Cannot find %.*s\n", (int) len, name);
786 #ifdef USE_ATTRIBUTES_FOR_PERLIO
789 perlio_mg_set(pTHX_ SV *sv, MAGIC *mg)
792 IO *io = GvIOn((GV *) SvRV(sv));
793 PerlIO *ifp = IoIFP(io);
794 PerlIO *ofp = IoOFP(io);
795 Perl_warn(aTHX_ "set %" SVf " %p %p %p", sv, io, ifp, ofp);
801 perlio_mg_get(pTHX_ SV *sv, MAGIC *mg)
804 IO *io = GvIOn((GV *) SvRV(sv));
805 PerlIO *ifp = IoIFP(io);
806 PerlIO *ofp = IoOFP(io);
807 Perl_warn(aTHX_ "get %" SVf " %p %p %p", sv, io, ifp, ofp);
813 perlio_mg_clear(pTHX_ SV *sv, MAGIC *mg)
815 Perl_warn(aTHX_ "clear %" SVf, sv);
820 perlio_mg_free(pTHX_ SV *sv, MAGIC *mg)
822 Perl_warn(aTHX_ "free %" SVf, sv);
826 MGVTBL perlio_vtab = {
834 XS(XS_io_MODIFY_SCALAR_ATTRIBUTES)
837 SV *sv = SvRV(ST(1));
842 sv_magic(sv, (SV *) av, PERL_MAGIC_ext, NULL, 0);
844 mg = mg_find(sv, PERL_MAGIC_ext);
845 mg->mg_virtual = &perlio_vtab;
847 Perl_warn(aTHX_ "attrib %" SVf, sv);
848 for (i = 2; i < items; i++) {
850 const char *name = SvPV_const(ST(i), len);
851 SV *layer = PerlIO_find_layer(aTHX_ name, len, 1);
853 av_push(av, SvREFCNT_inc(layer));
864 #endif /* USE_ATTIBUTES_FOR_PERLIO */
867 PerlIO_tab_sv(pTHX_ PerlIO_funcs *tab)
869 HV * const stash = gv_stashpv("PerlIO::Layer", TRUE);
870 SV * const sv = sv_bless(newRV_noinc(newSViv(PTR2IV(tab))), stash);
874 XS(XS_PerlIO__Layer__NoWarnings)
876 /* This is used as a %SIG{__WARN__} handler to supress warnings
877 during loading of layers.
881 PerlIO_debug("warning:%s\n",SvPV_nolen_const(ST(0)));
885 XS(XS_PerlIO__Layer__find)
889 Perl_croak(aTHX_ "Usage class->find(name[,load])");
892 const char * const name = SvPV_const(ST(1), len);
893 const bool load = (items > 2) ? SvTRUE(ST(2)) : 0;
894 PerlIO_funcs * const layer = PerlIO_find_layer(aTHX_ name, len, load);
896 (layer) ? sv_2mortal(PerlIO_tab_sv(aTHX_ layer)) :
903 PerlIO_define_layer(pTHX_ PerlIO_funcs *tab)
905 if (!PL_known_layers)
906 PL_known_layers = PerlIO_list_alloc(aTHX);
907 PerlIO_list_push(aTHX_ PL_known_layers, tab, Nullsv);
908 PerlIO_debug("define %s %p\n", tab->name, (void*)tab);
912 PerlIO_parse_layers(pTHX_ PerlIO_list_t *av, const char *names)
915 const char *s = names;
917 while (isSPACE(*s) || *s == ':')
922 const char *as = Nullch;
924 if (!isIDFIRST(*s)) {
926 * Message is consistent with how attribute lists are
927 * passed. Even though this means "foo : : bar" is
928 * seen as an invalid separator character.
930 const char q = ((*s == '\'') ? '"' : '\'');
931 if (ckWARN(WARN_LAYER))
932 Perl_warner(aTHX_ packWARN(WARN_LAYER),
933 "Invalid separator character %c%c%c in PerlIO layer specification %s",
935 SETERRNO(EINVAL, LIB_INVARG);
940 } while (isALNUM(*e));
956 * It's a nul terminated string, not allowed
957 * to \ the terminating null. Anything other
958 * character is passed over.
968 if (ckWARN(WARN_LAYER))
969 Perl_warner(aTHX_ packWARN(WARN_LAYER),
970 "Argument list not closed for PerlIO layer \"%.*s\"",
982 PerlIO_funcs * const layer =
983 PerlIO_find_layer(aTHX_ s, llen, 1);
985 PerlIO_list_push(aTHX_ av, layer,
991 if (ckWARN(WARN_LAYER))
992 Perl_warner(aTHX_ packWARN(WARN_LAYER), "Unknown PerlIO layer \"%.*s\"",
1005 PerlIO_default_buffer(pTHX_ PerlIO_list_t *av)
1007 PERLIO_FUNCS_DECL(*tab) = &PerlIO_perlio;
1008 #ifdef PERLIO_USING_CRLF
1011 if (PerlIO_stdio.Set_ptrcnt)
1012 tab = &PerlIO_stdio;
1014 PerlIO_debug("Pushing %s\n", tab->name);
1015 PerlIO_list_push(aTHX_ av, PerlIO_find_layer(aTHX_ tab->name, 0, 0),
1020 PerlIO_arg_fetch(PerlIO_list_t *av, IV n)
1022 return av->array[n].arg;
1026 PerlIO_layer_fetch(pTHX_ PerlIO_list_t *av, IV n, PerlIO_funcs *def)
1028 if (n >= 0 && n < av->cur) {
1029 PerlIO_debug("Layer %" IVdf " is %s\n", n,
1030 av->array[n].funcs->name);
1031 return av->array[n].funcs;
1034 Perl_croak(aTHX_ "panic: PerlIO layer array corrupt");
1039 PerlIOPop_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
1041 PERL_UNUSED_ARG(mode);
1042 PERL_UNUSED_ARG(arg);
1043 PERL_UNUSED_ARG(tab);
1044 if (PerlIOValid(f)) {
1046 PerlIO_pop(aTHX_ f);
1052 PERLIO_FUNCS_DECL(PerlIO_remove) = {
1053 sizeof(PerlIO_funcs),
1056 PERLIO_K_DUMMY | PERLIO_K_UTF8,
1076 NULL, /* get_base */
1077 NULL, /* get_bufsiz */
1080 NULL, /* set_ptrcnt */
1084 PerlIO_default_layers(pTHX)
1086 if (!PL_def_layerlist) {
1087 const char *s = (PL_tainting) ? Nullch : PerlEnv_getenv("PERLIO");
1088 PERLIO_FUNCS_DECL(*osLayer) = &PerlIO_unix;
1089 PL_def_layerlist = PerlIO_list_alloc(aTHX);
1090 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_unix));
1092 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_win32));
1094 osLayer = &PerlIO_win32;
1097 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_raw));
1098 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_perlio));
1099 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_stdio));
1100 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_crlf));
1102 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_mmap));
1104 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_utf8));
1105 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_remove));
1106 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_byte));
1107 PerlIO_list_push(aTHX_ PL_def_layerlist,
1108 PerlIO_find_layer(aTHX_ osLayer->name, 0, 0),
1111 PerlIO_parse_layers(aTHX_ PL_def_layerlist, s);
1114 PerlIO_default_buffer(aTHX_ PL_def_layerlist);
1117 if (PL_def_layerlist->cur < 2) {
1118 PerlIO_default_buffer(aTHX_ PL_def_layerlist);
1120 return PL_def_layerlist;
1124 Perl_boot_core_PerlIO(pTHX)
1126 #ifdef USE_ATTRIBUTES_FOR_PERLIO
1127 newXS("io::MODIFY_SCALAR_ATTRIBUTES", XS_io_MODIFY_SCALAR_ATTRIBUTES,
1130 newXS("PerlIO::Layer::find", XS_PerlIO__Layer__find, __FILE__);
1131 newXS("PerlIO::Layer::NoWarnings", XS_PerlIO__Layer__NoWarnings, __FILE__);
1135 PerlIO_default_layer(pTHX_ I32 n)
1137 PerlIO_list_t * const av = PerlIO_default_layers(aTHX);
1140 return PerlIO_layer_fetch(aTHX_ av, n, PERLIO_FUNCS_CAST(&PerlIO_stdio));
1143 #define PerlIO_default_top() PerlIO_default_layer(aTHX_ -1)
1144 #define PerlIO_default_btm() PerlIO_default_layer(aTHX_ 0)
1147 PerlIO_stdstreams(pTHX)
1150 PerlIO_allocate(aTHX);
1151 PerlIO_fdopen(0, "Ir" PERLIO_STDTEXT);
1152 PerlIO_fdopen(1, "Iw" PERLIO_STDTEXT);
1153 PerlIO_fdopen(2, "Iw" PERLIO_STDTEXT);
1158 PerlIO_push(pTHX_ PerlIO *f, PERLIO_FUNCS_DECL(*tab), const char *mode, SV *arg)
1160 if (tab->fsize != sizeof(PerlIO_funcs)) {
1162 Perl_croak(aTHX_ "Layer does not match this perl");
1166 if (tab->size < sizeof(PerlIOl)) {
1169 /* Real layer with a data area */
1170 Newxc(l,tab->size,char,PerlIOl);
1172 Zero(l, tab->size, char);
1174 l->tab = (PerlIO_funcs*) tab;
1176 PerlIO_debug("PerlIO_push f=%p %s %s %p\n", (void*)f, tab->name,
1177 (mode) ? mode : "(Null)", (void*)arg);
1178 if (*l->tab->Pushed &&
1179 (*l->tab->Pushed) (aTHX_ f, mode, arg, (PerlIO_funcs*) tab) != 0) {
1180 PerlIO_pop(aTHX_ f);
1186 /* Pseudo-layer where push does its own stack adjust */
1187 PerlIO_debug("PerlIO_push f=%p %s %s %p\n", (void*)f, tab->name,
1188 (mode) ? mode : "(Null)", (void*)arg);
1190 (*tab->Pushed) (aTHX_ f, mode, arg, (PerlIO_funcs*) tab) != 0) {
1198 PerlIOBase_binmode(pTHX_ PerlIO *f)
1200 if (PerlIOValid(f)) {
1201 /* Is layer suitable for raw stream ? */
1202 if (PerlIOBase(f)->tab->kind & PERLIO_K_RAW) {
1203 /* Yes - turn off UTF-8-ness, to undo UTF-8 locale effects */
1204 PerlIOBase(f)->flags &= ~PERLIO_F_UTF8;
1207 /* Not suitable - pop it */
1208 PerlIO_pop(aTHX_ f);
1216 PerlIORaw_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
1218 PERL_UNUSED_ARG(mode);
1219 PERL_UNUSED_ARG(arg);
1220 PERL_UNUSED_ARG(tab);
1222 if (PerlIOValid(f)) {
1227 * Strip all layers that are not suitable for a raw stream
1230 while (t && (l = *t)) {
1231 if (l->tab->Binmode) {
1232 /* Has a handler - normal case */
1233 if ((*l->tab->Binmode)(aTHX_ f) == 0) {
1235 /* Layer still there - move down a layer */
1244 /* No handler - pop it */
1245 PerlIO_pop(aTHX_ t);
1248 if (PerlIOValid(f)) {
1249 PerlIO_debug(":raw f=%p :%s\n", (void*)f, PerlIOBase(f)->tab->name);
1257 PerlIO_apply_layera(pTHX_ PerlIO *f, const char *mode,
1258 PerlIO_list_t *layers, IV n, IV max)
1262 PerlIO_funcs * const tab = PerlIO_layer_fetch(aTHX_ layers, n, NULL);
1264 if (!PerlIO_push(aTHX_ f, tab, mode, PerlIOArg)) {
1275 PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names)
1279 PerlIO_list_t * const layers = PerlIO_list_alloc(aTHX);
1280 code = PerlIO_parse_layers(aTHX_ layers, names);
1282 code = PerlIO_apply_layera(aTHX_ f, mode, layers, 0, layers->cur);
1284 PerlIO_list_free(aTHX_ layers);
1290 /*--------------------------------------------------------------------------------------*/
1292 * Given the abstraction above the public API functions
1296 PerlIO_binmode(pTHX_ PerlIO *f, int iotype, int mode, const char *names)
1298 PerlIO_debug("PerlIO_binmode f=%p %s %c %x %s\n",
1299 (void*)f, PerlIOBase(f)->tab->name, iotype, mode,
1300 (names) ? names : "(Null)");
1302 /* Do not flush etc. if (e.g.) switching encodings.
1303 if a pushed layer knows it needs to flush lower layers
1304 (for example :unix which is never going to call them)
1305 it can do the flush when it is pushed.
1307 return PerlIO_apply_layers(aTHX_ f, NULL, names) == 0 ? TRUE : FALSE;
1310 /* Fake 5.6 legacy of using this call to turn ON O_TEXT */
1311 #ifdef PERLIO_USING_CRLF
1312 /* Legacy binmode only has meaning if O_TEXT has a value distinct from
1313 O_BINARY so we can look for it in mode.
1315 if (!(mode & O_BINARY)) {
1317 /* FIXME?: Looking down the layer stack seems wrong,
1318 but is a way of reaching past (say) an encoding layer
1319 to flip CRLF-ness of the layer(s) below
1322 /* Perhaps we should turn on bottom-most aware layer
1323 e.g. Ilya's idea that UNIX TTY could serve
1325 if (PerlIOBase(f)->tab->kind & PERLIO_K_CANCRLF) {
1326 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF)) {
1327 /* Not in text mode - flush any pending stuff and flip it */
1329 PerlIOBase(f)->flags |= PERLIO_F_CRLF;
1331 /* Only need to turn it on in one layer so we are done */
1336 /* Not finding a CRLF aware layer presumably means we are binary
1337 which is not what was requested - so we failed
1338 We _could_ push :crlf layer but so could caller
1343 /* Legacy binmode is now _defined_ as being equivalent to pushing :raw
1344 So code that used to be here is now in PerlIORaw_pushed().
1346 return PerlIO_push(aTHX_ f, PERLIO_FUNCS_CAST(&PerlIO_raw), Nullch, Nullsv) ? TRUE : FALSE;
1351 PerlIO__close(pTHX_ PerlIO *f)
1353 if (PerlIOValid(f)) {
1354 PerlIO_funcs * const tab = PerlIOBase(f)->tab;
1355 if (tab && tab->Close)
1356 return (*tab->Close)(aTHX_ f);
1358 return PerlIOBase_close(aTHX_ f);
1361 SETERRNO(EBADF, SS_IVCHAN);
1367 Perl_PerlIO_close(pTHX_ PerlIO *f)
1369 const int code = PerlIO__close(aTHX_ f);
1370 while (PerlIOValid(f)) {
1371 PerlIO_pop(aTHX_ f);
1377 Perl_PerlIO_fileno(pTHX_ PerlIO *f)
1379 Perl_PerlIO_or_Base(f, Fileno, fileno, -1, (aTHX_ f));
1383 PerlIO_context_layers(pTHX_ const char *mode)
1385 const char *type = NULL;
1387 * Need to supply default layer info from open.pm
1390 SV *layers = PL_curcop->cop_io;
1393 type = SvPV_const(layers, len);
1394 if (type && mode[0] != 'r') {
1396 * Skip to write part
1398 const char *s = strchr(type, 0);
1399 if (s && (STRLEN)(s - type) < len) {
1408 static PerlIO_funcs *
1409 PerlIO_layer_from_ref(pTHX_ SV *sv)
1412 * For any scalar type load the handler which is bundled with perl
1414 if (SvTYPE(sv) < SVt_PVAV)
1415 return PerlIO_find_layer(aTHX_ "scalar", 6, 1);
1418 * For other types allow if layer is known but don't try and load it
1420 switch (SvTYPE(sv)) {
1422 return PerlIO_find_layer(aTHX_ "Array", 5, 0);
1424 return PerlIO_find_layer(aTHX_ "Hash", 4, 0);
1426 return PerlIO_find_layer(aTHX_ "Code", 4, 0);
1428 return PerlIO_find_layer(aTHX_ "Glob", 4, 0);
1434 PerlIO_resolve_layers(pTHX_ const char *layers,
1435 const char *mode, int narg, SV **args)
1437 PerlIO_list_t *def = PerlIO_default_layers(aTHX);
1440 PerlIO_stdstreams(aTHX);
1444 * If it is a reference but not an object see if we have a handler
1447 if (SvROK(arg) && !sv_isobject(arg)) {
1448 PerlIO_funcs * const handler = PerlIO_layer_from_ref(aTHX_ SvRV(arg));
1450 def = PerlIO_list_alloc(aTHX);
1451 PerlIO_list_push(aTHX_ def, handler, &PL_sv_undef);
1455 * Don't fail if handler cannot be found :via(...) etc. may do
1456 * something sensible else we will just stringfy and open
1462 layers = PerlIO_context_layers(aTHX_ mode);
1463 if (layers && *layers) {
1467 av = PerlIO_list_alloc(aTHX);
1468 for (i = 0; i < def->cur; i++) {
1469 PerlIO_list_push(aTHX_ av, def->array[i].funcs,
1476 if (PerlIO_parse_layers(aTHX_ av, layers) == 0) {
1480 PerlIO_list_free(aTHX_ av);
1481 return (PerlIO_list_t *) NULL;
1492 PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd,
1493 int imode, int perm, PerlIO *f, int narg, SV **args)
1495 if (!f && narg == 1 && *args == &PL_sv_undef) {
1496 if ((f = PerlIO_tmpfile())) {
1498 layers = PerlIO_context_layers(aTHX_ mode);
1499 if (layers && *layers)
1500 PerlIO_apply_layers(aTHX_ f, mode, layers);
1504 PerlIO_list_t *layera;
1506 PerlIO_funcs *tab = NULL;
1507 if (PerlIOValid(f)) {
1509 * This is "reopen" - it is not tested as perl does not use it
1513 layera = PerlIO_list_alloc(aTHX);
1515 SV * const arg = (l->tab->Getarg)
1516 ? (*l->tab->Getarg) (aTHX_ &l, NULL, 0)
1518 PerlIO_list_push(aTHX_ layera, l->tab, arg);
1519 l = *PerlIONext(&l);
1523 layera = PerlIO_resolve_layers(aTHX_ layers, mode, narg, args);
1529 * Start at "top" of layer stack
1531 n = layera->cur - 1;
1533 PerlIO_funcs * const t = PerlIO_layer_fetch(aTHX_ layera, n, NULL);
1542 * Found that layer 'n' can do opens - call it
1544 if (narg > 1 && !(tab->kind & PERLIO_K_MULTIARG)) {
1545 Perl_croak(aTHX_ "More than one argument to open(,':%s')",tab->name);
1547 PerlIO_debug("openn(%s,'%s','%s',%d,%x,%o,%p,%d,%p)\n",
1548 tab->name, layers, mode, fd, imode, perm,
1549 (void*)f, narg, (void*)args);
1551 f = (*tab->Open) (aTHX_ tab, layera, n, mode, fd, imode, perm,
1554 SETERRNO(EINVAL, LIB_INVARG);
1558 if (n + 1 < layera->cur) {
1560 * More layers above the one that we used to open -
1563 if (PerlIO_apply_layera(aTHX_ f, mode, layera, n + 1, layera->cur) != 0) {
1564 /* If pushing layers fails close the file */
1571 PerlIO_list_free(aTHX_ layera);
1578 Perl_PerlIO_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
1580 Perl_PerlIO_or_Base(f, Read, read, -1, (aTHX_ f, vbuf, count));
1584 Perl_PerlIO_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
1586 Perl_PerlIO_or_Base(f, Unread, unread, -1, (aTHX_ f, vbuf, count));
1590 Perl_PerlIO_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
1592 Perl_PerlIO_or_fail(f, Write, -1, (aTHX_ f, vbuf, count));
1596 Perl_PerlIO_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
1598 Perl_PerlIO_or_fail(f, Seek, -1, (aTHX_ f, offset, whence));
1602 Perl_PerlIO_tell(pTHX_ PerlIO *f)
1604 Perl_PerlIO_or_fail(f, Tell, -1, (aTHX_ f));
1608 Perl_PerlIO_flush(pTHX_ PerlIO *f)
1612 const PerlIO_funcs *tab = PerlIOBase(f)->tab;
1614 if (tab && tab->Flush)
1615 return (*tab->Flush) (aTHX_ f);
1617 return 0; /* If no Flush defined, silently succeed. */
1620 PerlIO_debug("Cannot flush f=%p\n", (void*)f);
1621 SETERRNO(EBADF, SS_IVCHAN);
1627 * Is it good API design to do flush-all on NULL, a potentially
1628 * errorneous input? Maybe some magical value (PerlIO*
1629 * PERLIO_FLUSH_ALL = (PerlIO*)-1;)? Yes, stdio does similar
1630 * things on fflush(NULL), but should we be bound by their design
1633 PerlIO **table = &PL_perlio;
1635 while ((f = *table)) {
1637 table = (PerlIO **) (f++);
1638 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
1639 if (*f && PerlIO_flush(f) != 0)
1649 PerlIOBase_flush_linebuf(pTHX)
1651 PerlIO **table = &PL_perlio;
1653 while ((f = *table)) {
1655 table = (PerlIO **) (f++);
1656 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
1659 flags & (PERLIO_F_LINEBUF | PERLIO_F_CANWRITE))
1660 == (PERLIO_F_LINEBUF | PERLIO_F_CANWRITE))
1668 Perl_PerlIO_fill(pTHX_ PerlIO *f)
1670 Perl_PerlIO_or_fail(f, Fill, -1, (aTHX_ f));
1674 PerlIO_isutf8(PerlIO *f)
1677 return (PerlIOBase(f)->flags & PERLIO_F_UTF8) != 0;
1679 SETERRNO(EBADF, SS_IVCHAN);
1685 Perl_PerlIO_eof(pTHX_ PerlIO *f)
1687 Perl_PerlIO_or_Base(f, Eof, eof, -1, (aTHX_ f));
1691 Perl_PerlIO_error(pTHX_ PerlIO *f)
1693 Perl_PerlIO_or_Base(f, Error, error, -1, (aTHX_ f));
1697 Perl_PerlIO_clearerr(pTHX_ PerlIO *f)
1699 Perl_PerlIO_or_Base_void(f, Clearerr, clearerr, (aTHX_ f));
1703 Perl_PerlIO_setlinebuf(pTHX_ PerlIO *f)
1705 Perl_PerlIO_or_Base_void(f, Setlinebuf, setlinebuf, (aTHX_ f));
1709 PerlIO_has_base(PerlIO *f)
1711 if (PerlIOValid(f)) {
1712 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
1715 return (tab->Get_base != NULL);
1716 SETERRNO(EINVAL, LIB_INVARG);
1719 SETERRNO(EBADF, SS_IVCHAN);
1725 PerlIO_fast_gets(PerlIO *f)
1727 if (PerlIOValid(f) && (PerlIOBase(f)->flags & PERLIO_F_FASTGETS)) {
1728 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
1731 return (tab->Set_ptrcnt != NULL);
1732 SETERRNO(EINVAL, LIB_INVARG);
1735 SETERRNO(EBADF, SS_IVCHAN);
1741 PerlIO_has_cntptr(PerlIO *f)
1743 if (PerlIOValid(f)) {
1744 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
1747 return (tab->Get_ptr != NULL && tab->Get_cnt != NULL);
1748 SETERRNO(EINVAL, LIB_INVARG);
1751 SETERRNO(EBADF, SS_IVCHAN);
1757 PerlIO_canset_cnt(PerlIO *f)
1759 if (PerlIOValid(f)) {
1760 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
1763 return (tab->Set_ptrcnt != NULL);
1764 SETERRNO(EINVAL, LIB_INVARG);
1767 SETERRNO(EBADF, SS_IVCHAN);
1773 Perl_PerlIO_get_base(pTHX_ PerlIO *f)
1775 Perl_PerlIO_or_fail(f, Get_base, NULL, (aTHX_ f));
1779 Perl_PerlIO_get_bufsiz(pTHX_ PerlIO *f)
1781 Perl_PerlIO_or_fail(f, Get_bufsiz, -1, (aTHX_ f));
1785 Perl_PerlIO_get_ptr(pTHX_ PerlIO *f)
1787 Perl_PerlIO_or_fail(f, Get_ptr, NULL, (aTHX_ f));
1791 Perl_PerlIO_get_cnt(pTHX_ PerlIO *f)
1793 Perl_PerlIO_or_fail(f, Get_cnt, -1, (aTHX_ f));
1797 Perl_PerlIO_set_cnt(pTHX_ PerlIO *f, int cnt)
1799 Perl_PerlIO_or_fail_void(f, Set_ptrcnt, (aTHX_ f, NULL, cnt));
1803 Perl_PerlIO_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, int cnt)
1805 Perl_PerlIO_or_fail_void(f, Set_ptrcnt, (aTHX_ f, ptr, cnt));
1809 /*--------------------------------------------------------------------------------------*/
1811 * utf8 and raw dummy layers
1815 PerlIOUtf8_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
1817 PERL_UNUSED_ARG(mode);
1818 PERL_UNUSED_ARG(arg);
1819 if (PerlIOValid(f)) {
1820 if (tab->kind & PERLIO_K_UTF8)
1821 PerlIOBase(f)->flags |= PERLIO_F_UTF8;
1823 PerlIOBase(f)->flags &= ~PERLIO_F_UTF8;
1829 PERLIO_FUNCS_DECL(PerlIO_utf8) = {
1830 sizeof(PerlIO_funcs),
1833 PERLIO_K_DUMMY | PERLIO_K_UTF8,
1853 NULL, /* get_base */
1854 NULL, /* get_bufsiz */
1857 NULL, /* set_ptrcnt */
1860 PERLIO_FUNCS_DECL(PerlIO_byte) = {
1861 sizeof(PerlIO_funcs),
1884 NULL, /* get_base */
1885 NULL, /* get_bufsiz */
1888 NULL, /* set_ptrcnt */
1892 PerlIORaw_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
1893 IV n, const char *mode, int fd, int imode, int perm,
1894 PerlIO *old, int narg, SV **args)
1896 PerlIO_funcs * const tab = PerlIO_default_btm();
1897 PERL_UNUSED_ARG(self);
1898 if (tab && tab->Open)
1899 return (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
1901 SETERRNO(EINVAL, LIB_INVARG);
1905 PERLIO_FUNCS_DECL(PerlIO_raw) = {
1906 sizeof(PerlIO_funcs),
1929 NULL, /* get_base */
1930 NULL, /* get_bufsiz */
1933 NULL, /* set_ptrcnt */
1935 /*--------------------------------------------------------------------------------------*/
1936 /*--------------------------------------------------------------------------------------*/
1938 * "Methods" of the "base class"
1942 PerlIOBase_fileno(pTHX_ PerlIO *f)
1944 return PerlIOValid(f) ? PerlIO_fileno(PerlIONext(f)) : -1;
1948 PerlIO_modestr(PerlIO * f, char *buf)
1951 if (PerlIOValid(f)) {
1952 const IV flags = PerlIOBase(f)->flags;
1953 if (flags & PERLIO_F_APPEND) {
1955 if (flags & PERLIO_F_CANREAD) {
1959 else if (flags & PERLIO_F_CANREAD) {
1961 if (flags & PERLIO_F_CANWRITE)
1964 else if (flags & PERLIO_F_CANWRITE) {
1966 if (flags & PERLIO_F_CANREAD) {
1970 #ifdef PERLIO_USING_CRLF
1971 if (!(flags & PERLIO_F_CRLF))
1981 PerlIOBase_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
1983 PerlIOl * const l = PerlIOBase(f);
1984 PERL_UNUSED_ARG(arg);
1986 l->flags &= ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE |
1987 PERLIO_F_TRUNCATE | PERLIO_F_APPEND);
1988 if (tab->Set_ptrcnt != NULL)
1989 l->flags |= PERLIO_F_FASTGETS;
1991 if (*mode == IoTYPE_NUMERIC || *mode == IoTYPE_IMPLICIT)
1995 l->flags |= PERLIO_F_CANREAD;
1998 l->flags |= PERLIO_F_APPEND | PERLIO_F_CANWRITE;
2001 l->flags |= PERLIO_F_TRUNCATE | PERLIO_F_CANWRITE;
2004 SETERRNO(EINVAL, LIB_INVARG);
2010 l->flags |= PERLIO_F_CANREAD | PERLIO_F_CANWRITE;
2013 l->flags &= ~PERLIO_F_CRLF;
2016 l->flags |= PERLIO_F_CRLF;
2019 SETERRNO(EINVAL, LIB_INVARG);
2026 l->flags |= l->next->flags &
2027 (PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_TRUNCATE |
2032 PerlIO_debug("PerlIOBase_pushed f=%p %s %s fl=%08" UVxf " (%s)\n",
2033 f, PerlIOBase(f)->tab->name, (omode) ? omode : "(Null)",
2034 l->flags, PerlIO_modestr(f, temp));
2040 PerlIOBase_popped(pTHX_ PerlIO *f)
2047 PerlIOBase_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
2050 * Save the position as current head considers it
2052 const Off_t old = PerlIO_tell(f);
2053 PerlIO_push(aTHX_ f, PERLIO_FUNCS_CAST(&PerlIO_pending), "r", Nullsv);
2054 PerlIOSelf(f, PerlIOBuf)->posn = old;
2055 return PerlIOBuf_unread(aTHX_ f, vbuf, count);
2059 PerlIOBase_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
2061 STDCHAR *buf = (STDCHAR *) vbuf;
2063 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD)) {
2064 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2065 SETERRNO(EBADF, SS_IVCHAN);
2071 SSize_t avail = PerlIO_get_cnt(f);
2074 take = ((SSize_t)count < avail) ? count : avail;
2076 STDCHAR *ptr = PerlIO_get_ptr(f);
2077 Copy(ptr, buf, take, STDCHAR);
2078 PerlIO_set_ptrcnt(f, ptr + take, (avail -= take));
2081 if (avail == 0) /* set_ptrcnt could have reset avail */
2084 if (count > 0 && avail <= 0) {
2085 if (PerlIO_fill(f) != 0)
2090 return (buf - (STDCHAR *) vbuf);
2096 PerlIOBase_noop_ok(pTHX_ PerlIO *f)
2103 PerlIOBase_noop_fail(pTHX_ PerlIO *f)
2110 PerlIOBase_close(pTHX_ PerlIO *f)
2113 if (PerlIOValid(f)) {
2114 PerlIO *n = PerlIONext(f);
2115 code = PerlIO_flush(f);
2116 PerlIOBase(f)->flags &=
2117 ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_OPEN);
2118 while (PerlIOValid(n)) {
2119 const PerlIO_funcs * const tab = PerlIOBase(n)->tab;
2120 if (tab && tab->Close) {
2121 if ((*tab->Close)(aTHX_ n) != 0)
2126 PerlIOBase(n)->flags &=
2127 ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_OPEN);
2133 SETERRNO(EBADF, SS_IVCHAN);
2139 PerlIOBase_eof(pTHX_ PerlIO *f)
2141 if (PerlIOValid(f)) {
2142 return (PerlIOBase(f)->flags & PERLIO_F_EOF) != 0;
2148 PerlIOBase_error(pTHX_ PerlIO *f)
2150 if (PerlIOValid(f)) {
2151 return (PerlIOBase(f)->flags & PERLIO_F_ERROR) != 0;
2157 PerlIOBase_clearerr(pTHX_ PerlIO *f)
2159 if (PerlIOValid(f)) {
2160 PerlIO *n = PerlIONext(f);
2161 PerlIOBase(f)->flags &= ~(PERLIO_F_ERROR | PERLIO_F_EOF);
2168 PerlIOBase_setlinebuf(pTHX_ PerlIO *f)
2170 if (PerlIOValid(f)) {
2171 PerlIOBase(f)->flags |= PERLIO_F_LINEBUF;
2176 PerlIO_sv_dup(pTHX_ SV *arg, CLONE_PARAMS *param)
2182 return sv_dup(arg, param);
2185 return newSVsv(arg);
2188 PERL_UNUSED_ARG(param);
2189 return newSVsv(arg);
2194 PerlIOBase_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
2196 PerlIO * const nexto = PerlIONext(o);
2197 if (PerlIOValid(nexto)) {
2198 const PerlIO_funcs * const tab = PerlIOBase(nexto)->tab;
2199 if (tab && tab->Dup)
2200 f = (*tab->Dup)(aTHX_ f, nexto, param, flags);
2202 f = PerlIOBase_dup(aTHX_ f, nexto, param, flags);
2205 PerlIO_funcs *self = PerlIOBase(o)->tab;
2208 PerlIO_debug("PerlIOBase_dup %s f=%p o=%p param=%p\n",
2209 self->name, (void*)f, (void*)o, (void*)param);
2211 arg = (*self->Getarg)(aTHX_ o, param, flags);
2215 f = PerlIO_push(aTHX_ f, self, PerlIO_modestr(o,buf), arg);
2224 perl_mutex PerlIO_mutex;
2226 int PerlIO_fd_refcnt[PERLIO_MAX_REFCOUNTABLE_FD];
2231 /* Place holder for stdstreams call ??? */
2233 MUTEX_INIT(&PerlIO_mutex);
2238 PerlIOUnix_refcnt_inc(int fd)
2240 if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
2242 MUTEX_LOCK(&PerlIO_mutex);
2244 PerlIO_fd_refcnt[fd]++;
2245 PerlIO_debug("fd %d refcnt=%d\n",fd,PerlIO_fd_refcnt[fd]);
2247 MUTEX_UNLOCK(&PerlIO_mutex);
2253 PerlIOUnix_refcnt_dec(int fd)
2256 if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
2258 MUTEX_LOCK(&PerlIO_mutex);
2260 cnt = --PerlIO_fd_refcnt[fd];
2261 PerlIO_debug("fd %d refcnt=%d\n",fd,cnt);
2263 MUTEX_UNLOCK(&PerlIO_mutex);
2270 PerlIO_cleanup(pTHX)
2274 PerlIO_debug("Cleanup layers for %p\n",aTHX);
2276 PerlIO_debug("Cleanup layers\n");
2278 /* Raise STDIN..STDERR refcount so we don't close them */
2279 for (i=0; i < 3; i++)
2280 PerlIOUnix_refcnt_inc(i);
2281 PerlIO_cleantable(aTHX_ &PL_perlio);
2282 /* Restore STDIN..STDERR refcount */
2283 for (i=0; i < 3; i++)
2284 PerlIOUnix_refcnt_dec(i);
2286 if (PL_known_layers) {
2287 PerlIO_list_free(aTHX_ PL_known_layers);
2288 PL_known_layers = NULL;
2290 if (PL_def_layerlist) {
2291 PerlIO_list_free(aTHX_ PL_def_layerlist);
2292 PL_def_layerlist = NULL;
2298 /*--------------------------------------------------------------------------------------*/
2300 * Bottom-most level for UNIX-like case
2304 struct _PerlIO base; /* The generic part */
2305 int fd; /* UNIX like file descriptor */
2306 int oflags; /* open/fcntl flags */
2310 PerlIOUnix_oflags(const char *mode)
2313 if (*mode == IoTYPE_IMPLICIT || *mode == IoTYPE_NUMERIC)
2318 if (*++mode == '+') {
2325 oflags = O_CREAT | O_TRUNC;
2326 if (*++mode == '+') {
2335 oflags = O_CREAT | O_APPEND;
2336 if (*++mode == '+') {
2349 else if (*mode == 't') {
2351 oflags &= ~O_BINARY;
2355 * Always open in binary mode
2358 if (*mode || oflags == -1) {
2359 SETERRNO(EINVAL, LIB_INVARG);
2366 PerlIOUnix_fileno(pTHX_ PerlIO *f)
2368 return PerlIOSelf(f, PerlIOUnix)->fd;
2372 PerlIOUnix_setfd(pTHX_ PerlIO *f, int fd, int imode)
2374 PerlIOUnix * const s = PerlIOSelf(f, PerlIOUnix);
2377 if (PerlLIO_fstat(fd, &st) == 0) {
2378 if (!S_ISREG(st.st_mode)) {
2379 PerlIO_debug("%d is not regular file\n",fd);
2380 PerlIOBase(f)->flags |= PERLIO_F_NOTREG;
2383 PerlIO_debug("%d _is_ a regular file\n",fd);
2389 PerlIOUnix_refcnt_inc(fd);
2393 PerlIOUnix_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
2395 IV code = PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
2396 if (*PerlIONext(f)) {
2397 /* We never call down so do any pending stuff now */
2398 PerlIO_flush(PerlIONext(f));
2400 * XXX could (or should) we retrieve the oflags from the open file
2401 * handle rather than believing the "mode" we are passed in? XXX
2402 * Should the value on NULL mode be 0 or -1?
2404 PerlIOUnix_setfd(aTHX_ f, PerlIO_fileno(PerlIONext(f)),
2405 mode ? PerlIOUnix_oflags(mode) : -1);
2407 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
2413 PerlIOUnix_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
2415 const int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2417 if (PerlIOBase(f)->flags & PERLIO_F_NOTREG) {
2419 SETERRNO(ESPIPE, LIB_INVARG);
2421 SETERRNO(EINVAL, LIB_INVARG);
2425 new_loc = PerlLIO_lseek(fd, offset, whence);
2426 if (new_loc == (Off_t) - 1)
2430 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
2435 PerlIOUnix_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2436 IV n, const char *mode, int fd, int imode,
2437 int perm, PerlIO *f, int narg, SV **args)
2439 if (PerlIOValid(f)) {
2440 if (PerlIOBase(f)->flags & PERLIO_F_OPEN)
2441 (*PerlIOBase(f)->tab->Close)(aTHX_ f);
2444 if (*mode == IoTYPE_NUMERIC)
2447 imode = PerlIOUnix_oflags(mode);
2451 const char *path = SvPV_nolen_const(*args);
2452 fd = PerlLIO_open3(path, imode, perm);
2456 if (*mode == IoTYPE_IMPLICIT)
2459 f = PerlIO_allocate(aTHX);
2461 if (!PerlIOValid(f)) {
2462 if (!(f = PerlIO_push(aTHX_ f, self, mode, PerlIOArg))) {
2466 PerlIOUnix_setfd(aTHX_ f, fd, imode);
2467 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
2468 if (*mode == IoTYPE_APPEND)
2469 PerlIOUnix_seek(aTHX_ f, 0, SEEK_END);
2475 * FIXME: pop layers ???
2483 PerlIOUnix_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
2485 PerlIOUnix *os = PerlIOSelf(o, PerlIOUnix);
2487 if (flags & PERLIO_DUP_FD) {
2488 fd = PerlLIO_dup(fd);
2490 if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
2491 f = PerlIOBase_dup(aTHX_ f, o, param, flags);
2493 /* If all went well overwrite fd in dup'ed lay with the dup()'ed fd */
2494 PerlIOUnix_setfd(aTHX_ f, fd, os->oflags);
2503 PerlIOUnix_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
2505 const int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2506 #ifdef PERLIO_STD_SPECIAL
2508 return PERLIO_STD_IN(fd, vbuf, count);
2510 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD) ||
2511 PerlIOBase(f)->flags & (PERLIO_F_EOF|PERLIO_F_ERROR)) {
2515 const SSize_t len = PerlLIO_read(fd, vbuf, count);
2516 if (len >= 0 || errno != EINTR) {
2518 if (errno != EAGAIN) {
2519 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2522 else if (len == 0 && count != 0) {
2523 PerlIOBase(f)->flags |= PERLIO_F_EOF;
2534 PerlIOUnix_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
2536 const int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2537 #ifdef PERLIO_STD_SPECIAL
2538 if (fd == 1 || fd == 2)
2539 return PERLIO_STD_OUT(fd, vbuf, count);
2542 const SSize_t len = PerlLIO_write(fd, vbuf, count);
2543 if (len >= 0 || errno != EINTR) {
2545 if (errno != EAGAIN) {
2546 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2557 PerlIOUnix_tell(pTHX_ PerlIO *f)
2559 return PerlLIO_lseek(PerlIOSelf(f, PerlIOUnix)->fd, 0, SEEK_CUR);
2564 PerlIOUnix_close(pTHX_ PerlIO *f)
2566 const int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2568 if (PerlIOBase(f)->flags & PERLIO_F_OPEN) {
2569 if (PerlIOUnix_refcnt_dec(fd) > 0) {
2570 PerlIOBase(f)->flags &= ~PERLIO_F_OPEN;
2575 SETERRNO(EBADF,SS_IVCHAN);
2578 while (PerlLIO_close(fd) != 0) {
2579 if (errno != EINTR) {
2586 PerlIOBase(f)->flags &= ~PERLIO_F_OPEN;
2591 PERLIO_FUNCS_DECL(PerlIO_unix) = {
2592 sizeof(PerlIO_funcs),
2599 PerlIOBase_binmode, /* binmode */
2609 PerlIOBase_noop_ok, /* flush */
2610 PerlIOBase_noop_fail, /* fill */
2613 PerlIOBase_clearerr,
2614 PerlIOBase_setlinebuf,
2615 NULL, /* get_base */
2616 NULL, /* get_bufsiz */
2619 NULL, /* set_ptrcnt */
2622 /*--------------------------------------------------------------------------------------*/
2627 #if defined(VMS) && !defined(STDIO_BUFFER_WRITABLE)
2628 /* perl5.8 - This ensures the last minute VMS ungetc fix is not
2629 broken by the last second glibc 2.3 fix
2631 #define STDIO_BUFFER_WRITABLE
2636 struct _PerlIO base;
2637 FILE *stdio; /* The stream */
2641 PerlIOStdio_fileno(pTHX_ PerlIO *f)
2644 if (PerlIOValid(f) && (s = PerlIOSelf(f, PerlIOStdio)->stdio)) {
2645 return PerlSIO_fileno(s);
2652 PerlIOStdio_mode(const char *mode, char *tmode)
2654 char * const ret = tmode;
2660 #if defined(PERLIO_USING_CRLF) || defined(__CYGWIN__)
2668 PerlIOStdio_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
2671 if (PerlIOValid(f) && PerlIOValid(n = PerlIONext(f))) {
2672 PerlIO_funcs * const toptab = PerlIOBase(n)->tab;
2673 if (toptab == tab) {
2674 /* Top is already stdio - pop self (duplicate) and use original */
2675 PerlIO_pop(aTHX_ f);
2678 const int fd = PerlIO_fileno(n);
2681 if (fd >= 0 && (stdio = PerlSIO_fdopen(fd,
2682 mode = PerlIOStdio_mode(mode, tmode)))) {
2683 PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
2684 /* We never call down so do any pending stuff now */
2685 PerlIO_flush(PerlIONext(f));
2692 return PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
2697 PerlIO_importFILE(FILE *stdio, const char *mode)
2703 if (!mode || !*mode) {
2704 /* We need to probe to see how we can open the stream
2705 so start with read/write and then try write and read
2706 we dup() so that we can fclose without loosing the fd.
2708 Note that the errno value set by a failing fdopen
2709 varies between stdio implementations.
2711 const int fd = PerlLIO_dup(fileno(stdio));
2712 FILE *f2 = PerlSIO_fdopen(fd, (mode = "r+"));
2714 f2 = PerlSIO_fdopen(fd, (mode = "w"));
2717 f2 = PerlSIO_fdopen(fd, (mode = "r"));
2720 /* Don't seem to be able to open */
2726 if ((f = PerlIO_push(aTHX_(f = PerlIO_allocate(aTHX)), PERLIO_FUNCS_CAST(&PerlIO_stdio), mode, Nullsv))) {
2727 s = PerlIOSelf(f, PerlIOStdio);
2735 PerlIOStdio_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2736 IV n, const char *mode, int fd, int imode,
2737 int perm, PerlIO *f, int narg, SV **args)
2740 if (PerlIOValid(f)) {
2741 const char *path = SvPV_nolen_const(*args);
2742 PerlIOStdio *s = PerlIOSelf(f, PerlIOStdio);
2744 PerlIOUnix_refcnt_dec(fileno(s->stdio));
2745 stdio = PerlSIO_freopen(path, (mode = PerlIOStdio_mode(mode, tmode)),
2750 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2755 const char *path = SvPV_nolen_const(*args);
2756 if (*mode == IoTYPE_NUMERIC) {
2758 fd = PerlLIO_open3(path, imode, perm);
2762 bool appended = FALSE;
2764 /* Cygwin wants its 'b' early. */
2766 mode = PerlIOStdio_mode(mode, tmode);
2768 stdio = PerlSIO_fopen(path, mode);
2772 f = PerlIO_allocate(aTHX);
2775 mode = PerlIOStdio_mode(mode, tmode);
2776 f = PerlIO_push(aTHX_ f, self, mode, PerlIOArg);
2778 s = PerlIOSelf(f, PerlIOStdio);
2780 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2792 if (*mode == IoTYPE_IMPLICIT) {
2799 stdio = PerlSIO_stdin;
2802 stdio = PerlSIO_stdout;
2805 stdio = PerlSIO_stderr;
2810 stdio = PerlSIO_fdopen(fd, mode =
2811 PerlIOStdio_mode(mode, tmode));
2815 f = PerlIO_allocate(aTHX);
2817 if ((f = PerlIO_push(aTHX_ f, self, mode, PerlIOArg))) {
2818 PerlIOStdio *s = PerlIOSelf(f, PerlIOStdio);
2820 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2830 PerlIOStdio_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
2832 /* This assumes no layers underneath - which is what
2833 happens, but is not how I remember it. NI-S 2001/10/16
2835 if ((f = PerlIOBase_dup(aTHX_ f, o, param, flags))) {
2836 FILE *stdio = PerlIOSelf(o, PerlIOStdio)->stdio;
2837 const int fd = fileno(stdio);
2839 if (flags & PERLIO_DUP_FD) {
2840 const int dfd = PerlLIO_dup(fileno(stdio));
2842 stdio = PerlSIO_fdopen(dfd, PerlIO_modestr(o,mode));
2846 /* FIXME: To avoid messy error recovery if dup fails
2847 re-use the existing stdio as though flag was not set
2851 stdio = PerlSIO_fdopen(fd, PerlIO_modestr(o,mode));
2853 PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
2854 PerlIOUnix_refcnt_inc(fileno(stdio));
2860 PerlIOStdio_invalidate_fileno(pTHX_ FILE *f)
2862 /* XXX this could use PerlIO_canset_fileno() and
2863 * PerlIO_set_fileno() support from Configure
2865 # if defined(__UCLIBC__)
2866 /* uClibc must come before glibc because it defines __GLIBC__ as well. */
2869 # elif defined(__GLIBC__)
2870 /* There may be a better way for GLIBC:
2871 - libio.h defines a flag to not close() on cleanup
2875 # elif defined(__sun__)
2877 /* On solaris, if _LP64 is defined, the FILE structure is this:
2883 * It turns out that the fd is stored in the top 32 bits of
2884 * file->__pad[4]. The lower 32 bits contain flags. file->pad[5] appears
2885 * to contain a pointer or offset into another structure. All the
2886 * remaining fields are zero.
2888 * We set the top bits to -1 (0xFFFFFFFF).
2890 f->__pad[4] |= 0xffffffff00000000L;
2891 assert(fileno(f) == 0xffffffff);
2892 # else /* !defined(_LP64) */
2893 /* _file is just a unsigned char :-(
2894 Not clear why we dup() rather than using -1
2895 even if that would be treated as 0xFF - so will
2898 f->_file = PerlLIO_dup(fileno(f));
2899 # endif /* defined(_LP64) */
2901 # elif defined(__hpux)
2905 /* Next one ->_file seems to be a reasonable fallback, i.e. if
2906 your platform does not have special entry try this one.
2907 [For OSF only have confirmation for Tru64 (alpha)
2908 but assume other OSFs will be similar.]
2910 # elif defined(_AIX) || defined(__osf__) || defined(__irix__)
2913 # elif defined(__FreeBSD__)
2914 /* There may be a better way on FreeBSD:
2915 - we could insert a dummy func in the _close function entry
2916 f->_close = (int (*)(void *)) dummy_close;
2920 # elif defined(__OpenBSD__)
2921 /* There may be a better way on OpenBSD:
2922 - we could insert a dummy func in the _close function entry
2923 f->_close = (int (*)(void *)) dummy_close;
2927 # elif defined(__EMX__)
2928 /* f->_flags &= ~_IOOPEN; */ /* Will leak stream->_buffer */
2931 # elif defined(__CYGWIN__)
2932 /* There may be a better way on CYGWIN:
2933 - we could insert a dummy func in the _close function entry
2934 f->_close = (int (*)(void *)) dummy_close;
2938 # elif defined(WIN32)
2939 # if defined(__BORLANDC__)
2940 f->fd = PerlLIO_dup(fileno(f));
2941 # elif defined(UNDER_CE)
2942 /* WIN_CE does not have access to FILE internals, it hardly has FILE
2951 /* Sarathy's code did this - we fall back to a dup/dup2 hack
2952 (which isn't thread safe) instead
2954 # error "Don't know how to set FILE.fileno on your platform"
2962 PerlIOStdio_close(pTHX_ PerlIO *f)
2964 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2970 const int fd = fileno(stdio);
2976 #ifdef SOCKS5_VERSION_NAME
2977 /* Socks lib overrides close() but stdio isn't linked to
2978 that library (though we are) - so we must call close()
2979 on sockets on stdio's behalf.
2982 Sock_size_t optlen = sizeof(int);
2983 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, (void *) &optval, &optlen) == 0) {
2988 if (PerlIOUnix_refcnt_dec(fd) > 0) {
2989 /* File descriptor still in use */
2994 /* For STD* handles don't close the stdio at all
2995 this is because we have shared the FILE * too
2997 if (stdio == stdin) {
2998 /* Some stdios are buggy fflush-ing inputs */
3001 else if (stdio == stdout || stdio == stderr) {
3002 return PerlIO_flush(f);
3004 /* Tricky - must fclose(stdio) to free memory but not close(fd)
3005 Use Sarathy's trick from maint-5.6 to invalidate the
3006 fileno slot of the FILE *
3008 result = PerlIO_flush(f);
3010 if (!(invalidate = PerlIOStdio_invalidate_fileno(aTHX_ stdio))) {
3011 dupfd = PerlLIO_dup(fd);
3014 result = PerlSIO_fclose(stdio);
3015 /* We treat error from stdio as success if we invalidated
3016 errno may NOT be expected EBADF
3018 if (invalidate && result != 0) {
3023 /* in SOCKS case let close() determine return value */
3027 PerlLIO_dup2(dupfd,fd);
3028 PerlLIO_close(dupfd);
3035 PerlIOStdio_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
3037 FILE *s = PerlIOSelf(f, PerlIOStdio)->stdio;
3041 STDCHAR *buf = (STDCHAR *) vbuf;
3043 * Perl is expecting PerlIO_getc() to fill the buffer Linux's
3044 * stdio does not do that for fread()
3046 const int ch = PerlSIO_fgetc(s);
3053 got = PerlSIO_fread(vbuf, 1, count, s);
3054 if (got == 0 && PerlSIO_ferror(s))
3056 if (got >= 0 || errno != EINTR)
3059 SETERRNO(0,0); /* just in case */
3065 PerlIOStdio_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3068 FILE *s = PerlIOSelf(f, PerlIOStdio)->stdio;
3070 #ifdef STDIO_BUFFER_WRITABLE
3071 if (PerlIO_fast_gets(f) && PerlIO_has_base(f)) {
3072 STDCHAR *buf = ((STDCHAR *) vbuf) + count;
3073 STDCHAR *base = PerlIO_get_base(f);
3074 SSize_t cnt = PerlIO_get_cnt(f);
3075 STDCHAR *ptr = PerlIO_get_ptr(f);
3076 SSize_t avail = ptr - base;
3078 if (avail > count) {
3082 Move(buf-avail,ptr,avail,STDCHAR);
3085 PerlIO_set_ptrcnt(f,ptr,cnt+avail);
3086 if (PerlSIO_feof(s) && unread >= 0)
3087 PerlSIO_clearerr(s);
3092 if (PerlIO_has_cntptr(f)) {
3093 /* We can get pointer to buffer but not its base
3094 Do ungetc() but check chars are ending up in the
3097 STDCHAR *eptr = (STDCHAR*)PerlSIO_get_ptr(s);
3098 STDCHAR *buf = ((STDCHAR *) vbuf) + count;
3100 const int ch = *--buf & 0xFF;
3101 if (ungetc(ch,s) != ch) {
3102 /* ungetc did not work */
3105 if ((STDCHAR*)PerlSIO_get_ptr(s) != --eptr || ((*eptr & 0xFF) != ch)) {
3106 /* Did not change pointer as expected */
3107 fgetc(s); /* get char back again */
3117 unread += PerlIOBase_unread(aTHX_ f, vbuf, count);
3123 PerlIOStdio_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3127 got = PerlSIO_fwrite(vbuf, 1, count,
3128 PerlIOSelf(f, PerlIOStdio)->stdio);
3129 if (got >= 0 || errno != EINTR)
3132 SETERRNO(0,0); /* just in case */
3138 PerlIOStdio_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
3140 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3141 return PerlSIO_fseek(stdio, offset, whence);
3145 PerlIOStdio_tell(pTHX_ PerlIO *f)
3147 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3148 return PerlSIO_ftell(stdio);
3152 PerlIOStdio_flush(pTHX_ PerlIO *f)
3154 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3155 if (PerlIOBase(f)->flags & PERLIO_F_CANWRITE) {
3156 return PerlSIO_fflush(stdio);
3161 * FIXME: This discards ungetc() and pre-read stuff which is not
3162 * right if this is just a "sync" from a layer above Suspect right
3163 * design is to do _this_ but not have layer above flush this
3164 * layer read-to-read
3167 * Not writeable - sync by attempting a seek
3170 if (PerlSIO_fseek(stdio, (Off_t) 0, SEEK_CUR) != 0)
3178 PerlIOStdio_eof(pTHX_ PerlIO *f)
3180 return PerlSIO_feof(PerlIOSelf(f, PerlIOStdio)->stdio);
3184 PerlIOStdio_error(pTHX_ PerlIO *f)
3186 return PerlSIO_ferror(PerlIOSelf(f, PerlIOStdio)->stdio);
3190 PerlIOStdio_clearerr(pTHX_ PerlIO *f)
3192 PerlSIO_clearerr(PerlIOSelf(f, PerlIOStdio)->stdio);
3196 PerlIOStdio_setlinebuf(pTHX_ PerlIO *f)
3198 #ifdef HAS_SETLINEBUF
3199 PerlSIO_setlinebuf(PerlIOSelf(f, PerlIOStdio)->stdio);
3201 PerlSIO_setvbuf(PerlIOSelf(f, PerlIOStdio)->stdio, Nullch, _IOLBF, 0);
3207 PerlIOStdio_get_base(pTHX_ PerlIO *f)
3209 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3210 return (STDCHAR*)PerlSIO_get_base(stdio);
3214 PerlIOStdio_get_bufsiz(pTHX_ PerlIO *f)
3216 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3217 return PerlSIO_get_bufsiz(stdio);
3221 #ifdef USE_STDIO_PTR
3223 PerlIOStdio_get_ptr(pTHX_ PerlIO *f)
3225 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3226 return (STDCHAR*)PerlSIO_get_ptr(stdio);
3230 PerlIOStdio_get_cnt(pTHX_ PerlIO *f)
3232 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3233 return PerlSIO_get_cnt(stdio);
3237 PerlIOStdio_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
3239 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3241 #ifdef STDIO_PTR_LVALUE
3242 PerlSIO_set_ptr(stdio, (void*)ptr); /* LHS STDCHAR* cast non-portable */
3243 #ifdef STDIO_PTR_LVAL_SETS_CNT
3244 if (PerlSIO_get_cnt(stdio) != (cnt)) {
3245 assert(PerlSIO_get_cnt(stdio) == (cnt));
3248 #if (!defined(STDIO_PTR_LVAL_NOCHANGE_CNT))
3250 * Setting ptr _does_ change cnt - we are done
3254 #else /* STDIO_PTR_LVALUE */
3256 #endif /* STDIO_PTR_LVALUE */
3259 * Now (or only) set cnt
3261 #ifdef STDIO_CNT_LVALUE
3262 PerlSIO_set_cnt(stdio, cnt);
3263 #else /* STDIO_CNT_LVALUE */
3264 #if (defined(STDIO_PTR_LVALUE) && defined(STDIO_PTR_LVAL_SETS_CNT))
3265 PerlSIO_set_ptr(stdio,
3266 PerlSIO_get_ptr(stdio) + (PerlSIO_get_cnt(stdio) -
3268 #else /* STDIO_PTR_LVAL_SETS_CNT */
3270 #endif /* STDIO_PTR_LVAL_SETS_CNT */
3271 #endif /* STDIO_CNT_LVALUE */
3278 PerlIOStdio_fill(pTHX_ PerlIO *f)
3280 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3283 * fflush()ing read-only streams can cause trouble on some stdio-s
3285 if ((PerlIOBase(f)->flags & PERLIO_F_CANWRITE)) {
3286 if (PerlSIO_fflush(stdio) != 0)
3289 c = PerlSIO_fgetc(stdio);
3293 #if (defined(STDIO_PTR_LVALUE) && (defined(STDIO_CNT_LVALUE) || defined(STDIO_PTR_LVAL_SETS_CNT)))
3295 #ifdef STDIO_BUFFER_WRITABLE
3296 if (PerlIO_fast_gets(f) && PerlIO_has_base(f)) {
3297 /* Fake ungetc() to the real buffer in case system's ungetc
3300 STDCHAR *base = (STDCHAR*)PerlSIO_get_base(stdio);
3301 SSize_t cnt = PerlSIO_get_cnt(stdio);
3302 STDCHAR *ptr = (STDCHAR*)PerlSIO_get_ptr(stdio);
3303 if (ptr == base+1) {
3304 *--ptr = (STDCHAR) c;
3305 PerlIOStdio_set_ptrcnt(aTHX_ f,ptr,cnt+1);
3306 if (PerlSIO_feof(stdio))
3307 PerlSIO_clearerr(stdio);
3313 if (PerlIO_has_cntptr(f)) {
3315 if (PerlIOStdio_unread(aTHX_ f,&ch,1) == 1) {
3322 /* An ungetc()d char is handled separately from the regular
3323 * buffer, so we stuff it in the buffer ourselves.
3324 * Should never get called as should hit code above
3326 *(--((*stdio)->_ptr)) = (unsigned char) c;
3329 /* If buffer snoop scheme above fails fall back to
3332 if (PerlSIO_ungetc(c, stdio) != c)
3340 PERLIO_FUNCS_DECL(PerlIO_stdio) = {
3341 sizeof(PerlIO_funcs),
3343 sizeof(PerlIOStdio),
3344 PERLIO_K_BUFFERED|PERLIO_K_RAW,
3348 PerlIOBase_binmode, /* binmode */
3362 PerlIOStdio_clearerr,
3363 PerlIOStdio_setlinebuf,
3365 PerlIOStdio_get_base,
3366 PerlIOStdio_get_bufsiz,
3371 #ifdef USE_STDIO_PTR
3372 PerlIOStdio_get_ptr,
3373 PerlIOStdio_get_cnt,
3374 # if defined(HAS_FAST_STDIO) && defined(USE_FAST_STDIO)
3375 PerlIOStdio_set_ptrcnt,
3378 # endif /* HAS_FAST_STDIO && USE_FAST_STDIO */
3383 #endif /* USE_STDIO_PTR */
3386 /* Note that calls to PerlIO_exportFILE() are reversed using
3387 * PerlIO_releaseFILE(), not importFILE. */
3389 PerlIO_exportFILE(PerlIO * f, const char *mode)
3393 if (PerlIOValid(f)) {
3396 if (!mode || !*mode) {
3397 mode = PerlIO_modestr(f, buf);
3399 stdio = PerlSIO_fdopen(PerlIO_fileno(f), mode);
3403 /* De-link any lower layers so new :stdio sticks */
3405 if ((f2 = PerlIO_push(aTHX_ f, PERLIO_FUNCS_CAST(&PerlIO_stdio), buf, Nullsv))) {
3406 PerlIOStdio *s = PerlIOSelf((f = f2), PerlIOStdio);
3408 /* Link previous lower layers under new one */
3412 /* restore layers list */
3422 PerlIO_findFILE(PerlIO *f)
3426 if (l->tab == &PerlIO_stdio) {
3427 PerlIOStdio *s = PerlIOSelf(&l, PerlIOStdio);
3430 l = *PerlIONext(&l);
3432 /* Uses fallback "mode" via PerlIO_modestr() in PerlIO_exportFILE */
3433 return PerlIO_exportFILE(f, Nullch);
3436 /* Use this to reverse PerlIO_exportFILE calls. */
3438 PerlIO_releaseFILE(PerlIO *p, FILE *f)
3442 if (l->tab == &PerlIO_stdio) {
3443 PerlIOStdio *s = PerlIOSelf(&l, PerlIOStdio);
3444 if (s->stdio == f) {
3446 PerlIO_pop(aTHX_ p);
3455 /*--------------------------------------------------------------------------------------*/
3457 * perlio buffer layer
3461 PerlIOBuf_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
3463 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3464 const int fd = PerlIO_fileno(f);
3465 if (fd >= 0 && PerlLIO_isatty(fd)) {
3466 PerlIOBase(f)->flags |= PERLIO_F_LINEBUF | PERLIO_F_TTY;
3468 if (*PerlIONext(f)) {
3469 const Off_t posn = PerlIO_tell(PerlIONext(f));
3470 if (posn != (Off_t) - 1) {
3474 return PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
3478 PerlIOBuf_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
3479 IV n, const char *mode, int fd, int imode, int perm,
3480 PerlIO *f, int narg, SV **args)
3482 if (PerlIOValid(f)) {
3483 PerlIO *next = PerlIONext(f);
3485 PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIOBase(next)->tab);
3486 if (tab && tab->Open)
3488 (*tab->Open)(aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
3490 if (!next || (*PerlIOBase(f)->tab->Pushed) (aTHX_ f, mode, PerlIOArg, self) != 0) {
3495 PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIO_default_btm());
3497 if (*mode == IoTYPE_IMPLICIT) {
3503 if (tab && tab->Open)
3504 f = (*tab->Open)(aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
3507 SETERRNO(EINVAL, LIB_INVARG);
3509 if (PerlIO_push(aTHX_ f, self, mode, PerlIOArg) == 0) {
3511 * if push fails during open, open fails. close will pop us.
3516 fd = PerlIO_fileno(f);
3517 if (init && fd == 2) {
3519 * Initial stderr is unbuffered
3521 PerlIOBase(f)->flags |= PERLIO_F_UNBUF;
3523 #ifdef PERLIO_USING_CRLF
3524 # ifdef PERLIO_IS_BINMODE_FD
3525 if (PERLIO_IS_BINMODE_FD(fd))
3526 PerlIO_binmode(aTHX_ f, '<'/*not used*/, O_BINARY, Nullch);
3530 * do something about failing setmode()? --jhi
3532 PerlLIO_setmode(fd, O_BINARY);
3541 * This "flush" is akin to sfio's sync in that it handles files in either
3542 * read or write state. For write state, we put the postponed data through
3543 * the next layers. For read state, we seek() the next layers to the
3544 * offset given by current position in the buffer, and discard the buffer
3545 * state (XXXX supposed to be for seek()able buffers only, but now it is done
3546 * in any case?). Then the pass the stick further in chain.
3549 PerlIOBuf_flush(pTHX_ PerlIO *f)
3551 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3553 PerlIO *n = PerlIONext(f);
3554 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF) {
3556 * write() the buffer
3558 const STDCHAR *buf = b->buf;
3559 const STDCHAR *p = buf;
3560 while (p < b->ptr) {
3561 SSize_t count = PerlIO_write(n, p, b->ptr - p);
3565 else if (count < 0 || PerlIO_error(n)) {
3566 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
3571 b->posn += (p - buf);
3573 else if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
3574 STDCHAR *buf = PerlIO_get_base(f);
3576 * Note position change
3578 b->posn += (b->ptr - buf);
3579 if (b->ptr < b->end) {
3580 /* We did not consume all of it - try and seek downstream to
3581 our logical position
3583 if (PerlIOValid(n) && PerlIO_seek(n, b->posn, SEEK_SET) == 0) {
3584 /* Reload n as some layers may pop themselves on seek */
3585 b->posn = PerlIO_tell(n = PerlIONext(f));
3588 /* Seek failed (e.g. pipe or tty). Do NOT clear buffer or pre-read
3589 data is lost for good - so return saying "ok" having undone
3592 b->posn -= (b->ptr - buf);
3597 b->ptr = b->end = b->buf;
3598 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
3599 /* We check for Valid because of dubious decision to make PerlIO_flush(NULL) flush all */
3600 if (PerlIOValid(n) && PerlIO_flush(n) != 0)
3605 /* This discards the content of the buffer after b->ptr, and rereads
3606 * the buffer from the position off in the layer downstream; here off
3607 * is at offset corresponding to b->ptr - b->buf.
3610 PerlIOBuf_fill(pTHX_ PerlIO *f)
3612 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3613 PerlIO *n = PerlIONext(f);
3616 * Down-stream flush is defined not to loose read data so is harmless.
3617 * we would not normally be fill'ing if there was data left in anycase.
3619 if (PerlIO_flush(f) != 0) /* XXXX Check that its seek() succeeded?! */
3621 if (PerlIOBase(f)->flags & PERLIO_F_TTY)
3622 PerlIOBase_flush_linebuf(aTHX);
3625 PerlIO_get_base(f); /* allocate via vtable */
3627 b->ptr = b->end = b->buf;
3629 if (!PerlIOValid(n)) {
3630 PerlIOBase(f)->flags |= PERLIO_F_EOF;
3634 if (PerlIO_fast_gets(n)) {
3636 * Layer below is also buffered. We do _NOT_ want to call its
3637 * ->Read() because that will loop till it gets what we asked for
3638 * which may hang on a pipe etc. Instead take anything it has to
3639 * hand, or ask it to fill _once_.
3641 avail = PerlIO_get_cnt(n);
3643 avail = PerlIO_fill(n);
3645 avail = PerlIO_get_cnt(n);
3647 if (!PerlIO_error(n) && PerlIO_eof(n))
3652 STDCHAR *ptr = PerlIO_get_ptr(n);
3653 SSize_t cnt = avail;
3654 if (avail > (SSize_t)b->bufsiz)
3656 Copy(ptr, b->buf, avail, STDCHAR);
3657 PerlIO_set_ptrcnt(n, ptr + avail, cnt - avail);
3661 avail = PerlIO_read(n, b->ptr, b->bufsiz);
3665 PerlIOBase(f)->flags |= PERLIO_F_EOF;
3667 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
3670 b->end = b->buf + avail;
3671 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3676 PerlIOBuf_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
3678 if (PerlIOValid(f)) {
3679 const PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3682 return PerlIOBase_read(aTHX_ f, vbuf, count);
3688 PerlIOBuf_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3690 const STDCHAR *buf = (const STDCHAR *) vbuf + count;
3691 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3694 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
3699 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
3701 * Buffer is already a read buffer, we can overwrite any chars
3702 * which have been read back to buffer start
3704 avail = (b->ptr - b->buf);
3708 * Buffer is idle, set it up so whole buffer is available for
3712 b->end = b->buf + avail;
3714 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3716 * Buffer extends _back_ from where we are now
3718 b->posn -= b->bufsiz;
3720 if (avail > (SSize_t) count) {
3722 * If we have space for more than count, just move count
3730 * In simple stdio-like ungetc() case chars will be already
3733 if (buf != b->ptr) {
3734 Copy(buf, b->ptr, avail, STDCHAR);
3738 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
3742 unread += PerlIOBase_unread(aTHX_ f, vbuf, count);
3748 PerlIOBuf_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3750 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3751 const STDCHAR *buf = (const STDCHAR *) vbuf;
3752 const STDCHAR *flushptr = buf;
3756 if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
3758 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
3759 if (PerlIO_flush(f) != 0) {
3763 if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF) {
3764 flushptr = buf + count;
3765 while (flushptr > buf && *(flushptr - 1) != '\n')
3769 SSize_t avail = b->bufsiz - (b->ptr - b->buf);
3770 if ((SSize_t) count < avail)
3772 if (flushptr > buf && flushptr <= buf + avail)
3773 avail = flushptr - buf;
3774 PerlIOBase(f)->flags |= PERLIO_F_WRBUF;
3776 Copy(buf, b->ptr, avail, STDCHAR);
3781 if (buf == flushptr)
3784 if (b->ptr >= (b->buf + b->bufsiz))
3787 if (PerlIOBase(f)->flags & PERLIO_F_UNBUF)
3793 PerlIOBuf_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
3796 if ((code = PerlIO_flush(f)) == 0) {
3797 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
3798 code = PerlIO_seek(PerlIONext(f), offset, whence);
3800 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3801 b->posn = PerlIO_tell(PerlIONext(f));
3808 PerlIOBuf_tell(pTHX_ PerlIO *f)
3810 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3812 * b->posn is file position where b->buf was read, or will be written
3814 Off_t posn = b->posn;
3815 if ((PerlIOBase(f)->flags & PERLIO_F_APPEND) &&
3816 (PerlIOBase(f)->flags & PERLIO_F_WRBUF)) {
3818 /* As O_APPEND files are normally shared in some sense it is better
3823 /* when file is NOT shared then this is sufficient */
3824 PerlIO_seek(PerlIONext(f),0, SEEK_END);
3826 posn = b->posn = PerlIO_tell(PerlIONext(f));
3830 * If buffer is valid adjust position by amount in buffer
3832 posn += (b->ptr - b->buf);
3838 PerlIOBuf_popped(pTHX_ PerlIO *f)
3840 const IV code = PerlIOBase_popped(aTHX_ f);
3841 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3842 if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
3846 b->ptr = b->end = b->buf;
3847 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
3852 PerlIOBuf_close(pTHX_ PerlIO *f)
3854 const IV code = PerlIOBase_close(aTHX_ f);
3855 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3856 if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
3860 b->ptr = b->end = b->buf;
3861 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
3866 PerlIOBuf_get_ptr(pTHX_ PerlIO *f)
3868 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3875 PerlIOBuf_get_cnt(pTHX_ PerlIO *f)
3877 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3880 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF)
3881 return (b->end - b->ptr);
3886 PerlIOBuf_get_base(pTHX_ PerlIO *f)
3888 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3892 b->buf = Newxz(b->buf,b->bufsiz, STDCHAR);
3894 b->buf = (STDCHAR *) & b->oneword;
3895 b->bufsiz = sizeof(b->oneword);
3904 PerlIOBuf_bufsiz(pTHX_ PerlIO *f)
3906 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3909 return (b->end - b->buf);
3913 PerlIOBuf_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
3915 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3919 if (PerlIO_get_cnt(f) != cnt || b->ptr < b->buf) {
3920 assert(PerlIO_get_cnt(f) == cnt);
3921 assert(b->ptr >= b->buf);
3923 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3927 PerlIOBuf_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
3929 return PerlIOBase_dup(aTHX_ f, o, param, flags);
3934 PERLIO_FUNCS_DECL(PerlIO_perlio) = {
3935 sizeof(PerlIO_funcs),
3938 PERLIO_K_BUFFERED|PERLIO_K_RAW,
3942 PerlIOBase_binmode, /* binmode */
3956 PerlIOBase_clearerr,
3957 PerlIOBase_setlinebuf,
3962 PerlIOBuf_set_ptrcnt,
3965 /*--------------------------------------------------------------------------------------*/
3967 * Temp layer to hold unread chars when cannot do it any other way
3971 PerlIOPending_fill(pTHX_ PerlIO *f)
3974 * Should never happen
3981 PerlIOPending_close(pTHX_ PerlIO *f)
3984 * A tad tricky - flush pops us, then we close new top
3987 return PerlIO_close(f);
3991 PerlIOPending_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
3994 * A tad tricky - flush pops us, then we seek new top
3997 return PerlIO_seek(f, offset, whence);
4002 PerlIOPending_flush(pTHX_ PerlIO *f)
4004 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
4005 if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
4009 PerlIO_pop(aTHX_ f);
4014 PerlIOPending_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
4020 PerlIOBuf_set_ptrcnt(aTHX_ f, ptr, cnt);
4025 PerlIOPending_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
4027 const IV code = PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
4028 PerlIOl *l = PerlIOBase(f);
4030 * Our PerlIO_fast_gets must match what we are pushed on, or sv_gets()
4031 * etc. get muddled when it changes mid-string when we auto-pop.
4033 l->flags = (l->flags & ~(PERLIO_F_FASTGETS | PERLIO_F_UTF8)) |
4034 (PerlIOBase(PerlIONext(f))->
4035 flags & (PERLIO_F_FASTGETS | PERLIO_F_UTF8));
4040 PerlIOPending_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
4042 SSize_t avail = PerlIO_get_cnt(f);
4044 if ((SSize_t)count < avail)
4047 got = PerlIOBuf_read(aTHX_ f, vbuf, avail);
4048 if (got >= 0 && got < (SSize_t)count) {
4049 const SSize_t more =
4050 PerlIO_read(f, ((STDCHAR *) vbuf) + got, count - got);
4051 if (more >= 0 || got == 0)
4057 PERLIO_FUNCS_DECL(PerlIO_pending) = {
4058 sizeof(PerlIO_funcs),
4061 PERLIO_K_BUFFERED|PERLIO_K_RAW, /* not sure about RAW here */
4062 PerlIOPending_pushed,
4065 PerlIOBase_binmode, /* binmode */
4074 PerlIOPending_close,
4075 PerlIOPending_flush,
4079 PerlIOBase_clearerr,
4080 PerlIOBase_setlinebuf,
4085 PerlIOPending_set_ptrcnt,
4090 /*--------------------------------------------------------------------------------------*/
4092 * crlf - translation On read translate CR,LF to "\n" we do this by
4093 * overriding ptr/cnt entries to hand back a line at a time and keeping a
4094 * record of which nl we "lied" about. On write translate "\n" to CR,LF
4096 * c->nl points on the first byte of CR LF pair when it is temporarily
4097 * replaced by LF, or to the last CR of the buffer. In the former case
4098 * the caller thinks that the buffer ends at c->nl + 1, in the latter
4099 * that it ends at c->nl; these two cases can be distinguished by
4100 * *c->nl. c->nl is set during _getcnt() call, and unset during
4101 * _unread() and _flush() calls.
4102 * It only matters for read operations.
4106 PerlIOBuf base; /* PerlIOBuf stuff */
4107 STDCHAR *nl; /* Position of crlf we "lied" about in the
4112 PerlIOCrlf_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
4115 PerlIOBase(f)->flags |= PERLIO_F_CRLF;
4116 code = PerlIOBuf_pushed(aTHX_ f, mode, arg, tab);
4118 PerlIO_debug("PerlIOCrlf_pushed f=%p %s %s fl=%08" UVxf "\n",
4119 f, PerlIOBase(f)->tab->name, (mode) ? mode : "(Null)",
4120 PerlIOBase(f)->flags);
4123 /* Enable the first CRLF capable layer you can find, but if none
4124 * found, the one we just pushed is fine. This results in at
4125 * any given moment at most one CRLF-capable layer being enabled
4126 * in the whole layer stack. */
4127 PerlIO *g = PerlIONext(f);
4129 PerlIOl *b = PerlIOBase(g);
4130 if (b && b->tab == &PerlIO_crlf) {
4131 if (!(b->flags & PERLIO_F_CRLF))
4132 b->flags |= PERLIO_F_CRLF;
4133 PerlIO_pop(aTHX_ f);
4144 PerlIOCrlf_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
4146 PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
4147 if (c->nl) { /* XXXX Shouldn't it be done only if b->ptr > c->nl? */
4151 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF))
4152 return PerlIOBuf_unread(aTHX_ f, vbuf, count);
4154 const STDCHAR *buf = (const STDCHAR *) vbuf + count;
4155 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
4157 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
4162 if (!(PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
4163 b->end = b->ptr = b->buf + b->bufsiz;
4164 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
4165 b->posn -= b->bufsiz;
4167 while (count > 0 && b->ptr > b->buf) {
4170 if (b->ptr - 2 >= b->buf) {
4177 /* If b->ptr - 1 == b->buf, we are undoing reading 0xa */
4178 *--(b->ptr) = 0xa; /* Works even if 0xa == '\r' */
4194 /* XXXX This code assumes that buffer size >=2, but does not check it... */
4196 PerlIOCrlf_get_cnt(pTHX_ PerlIO *f)
4198 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
4201 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
4202 PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
4203 if ((PerlIOBase(f)->flags & PERLIO_F_CRLF) && (!c->nl || *c->nl == 0xd)) {
4204 STDCHAR *nl = (c->nl) ? c->nl : b->ptr;
4206 while (nl < b->end && *nl != 0xd)
4208 if (nl < b->end && *nl == 0xd) {
4210 if (nl + 1 < b->end) {
4217 * Not CR,LF but just CR
4225 * Blast - found CR as last char in buffer
4230 * They may not care, defer work as long as
4234 return (nl - b->ptr);
4238 b->ptr++; /* say we have read it as far as
4239 * flush() is concerned */
4240 b->buf++; /* Leave space in front of buffer */
4241 /* Note as we have moved buf up flush's
4243 will naturally make posn point at CR
4245 b->bufsiz--; /* Buffer is thus smaller */
4246 code = PerlIO_fill(f); /* Fetch some more */
4247 b->bufsiz++; /* Restore size for next time */
4248 b->buf--; /* Point at space */
4249 b->ptr = nl = b->buf; /* Which is what we hand
4251 *nl = 0xd; /* Fill in the CR */
4253 goto test; /* fill() call worked */
4255 * CR at EOF - just fall through
4257 /* Should we clear EOF though ??? */
4262 return (((c->nl) ? (c->nl + 1) : b->end) - b->ptr);
4268 PerlIOCrlf_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
4270 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
4271 PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
4277 if (ptr == b->end && *c->nl == 0xd) {
4278 /* Defered CR at end of buffer case - we lied about count */
4290 * Test code - delete when it works ...
4292 IV flags = PerlIOBase(f)->flags;
4293 STDCHAR *chk = (c->nl) ? (c->nl+1) : b->end;
4294 if (ptr+cnt == c->nl && c->nl+1 == b->end && *c->nl == 0xd) {
4295 /* Defered CR at end of buffer case - we lied about count */
4301 Perl_croak(aTHX_ "ptr wrong %p != %p fl=%08" UVxf
4302 " nl=%p e=%p for %d", ptr, chk, flags, c->nl,
4310 * They have taken what we lied about
4318 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
4322 PerlIOCrlf_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
4324 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF))
4325 return PerlIOBuf_write(aTHX_ f, vbuf, count);
4327 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
4328 const STDCHAR *buf = (const STDCHAR *) vbuf;
4329 const STDCHAR *ebuf = buf + count;
4332 if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
4334 while (buf < ebuf) {
4335 STDCHAR *eptr = b->buf + b->bufsiz;
4336 PerlIOBase(f)->flags |= PERLIO_F_WRBUF;
4337 while (buf < ebuf && b->ptr < eptr) {
4339 if ((b->ptr + 2) > eptr) {
4347 *(b->ptr)++ = 0xd; /* CR */
4348 *(b->ptr)++ = 0xa; /* LF */
4350 if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF) {
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 *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 */
4443 static size_t page_size = 0;
4446 PerlIOMmap_map(pTHX_ PerlIO *f)
4448 PerlIOMmap * const m = PerlIOSelf(f, PerlIOMmap);
4449 const IV flags = PerlIOBase(f)->flags;
4453 if (flags & PERLIO_F_CANREAD) {
4454 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
4455 const int fd = PerlIO_fileno(f);
4457 code = Fstat(fd, &st);
4458 if (code == 0 && S_ISREG(st.st_mode)) {
4459 SSize_t len = st.st_size - b->posn;
4463 #if defined(HAS_SYSCONF) && (defined(_SC_PAGESIZE) || defined(_SC_PAGE_SIZE))
4465 SETERRNO(0, SS_NORMAL);
4466 # ifdef _SC_PAGESIZE
4467 page_size = sysconf(_SC_PAGESIZE);
4469 page_size = sysconf(_SC_PAGE_SIZE);
4471 if ((long) page_size < 0) {
4476 (void) SvUPGRADE(error, SVt_PV);
4477 msg = SvPVx(error, n_a);
4478 Perl_croak(aTHX_ "panic: sysconf: %s",
4483 "panic: sysconf: pagesize unknown");
4487 # ifdef HAS_GETPAGESIZE
4488 page_size = getpagesize();
4490 # if defined(I_SYS_PARAM) && defined(PAGESIZE)
4491 page_size = PAGESIZE; /* compiletime, bad */
4495 if ((IV) page_size <= 0)
4496 Perl_croak(aTHX_ "panic: bad pagesize %" IVdf,
4501 * This is a hack - should never happen - open should
4504 b->posn = PerlIO_tell(PerlIONext(f));
4506 posn = (b->posn / page_size) * page_size;
4507 len = st.st_size - posn;
4508 m->mptr = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, posn);
4509 if (m->mptr && m->mptr != (Mmap_t) - 1) {
4510 #if 0 && defined(HAS_MADVISE) && defined(MADV_SEQUENTIAL)
4511 madvise(m->mptr, len, MADV_SEQUENTIAL);
4513 #if 0 && defined(HAS_MADVISE) && defined(MADV_WILLNEED)
4514 madvise(m->mptr, len, MADV_WILLNEED);
4516 PerlIOBase(f)->flags =
4517 (flags & ~PERLIO_F_EOF) | PERLIO_F_RDBUF;
4518 b->end = ((STDCHAR *) m->mptr) + len;
4519 b->buf = ((STDCHAR *) m->mptr) + (b->posn - posn);
4528 PerlIOBase(f)->flags =
4529 flags | PERLIO_F_EOF | PERLIO_F_RDBUF;
4531 b->ptr = b->end = b->ptr;
4540 PerlIOMmap_unmap(pTHX_ PerlIO *f)
4542 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
4543 PerlIOBuf *b = &m->base;
4547 code = munmap(m->mptr, m->len);
4551 if (PerlIO_seek(PerlIONext(f), b->posn, SEEK_SET) != 0)
4554 b->ptr = b->end = b->buf;
4555 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
4561 PerlIOMmap_get_base(pTHX_ PerlIO *f)
4563 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
4564 PerlIOBuf *b = &m->base;
4565 if (b->buf && (PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
4567 * Already have a readbuffer in progress
4573 * We have a write buffer or flushed PerlIOBuf read buffer
4575 m->bbuf = b->buf; /* save it in case we need it again */
4576 b->buf = NULL; /* Clear to trigger below */
4579 PerlIOMmap_map(aTHX_ f); /* Try and map it */
4582 * Map did not work - recover PerlIOBuf buffer if we have one
4587 b->ptr = b->end = b->buf;
4590 return PerlIOBuf_get_base(aTHX_ f);
4594 PerlIOMmap_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
4596 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
4597 PerlIOBuf *b = &m->base;
4598 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
4600 if (b->ptr && (b->ptr - count) >= b->buf
4601 && memEQ(b->ptr - count, vbuf, count)) {
4603 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
4608 * Loose the unwritable mapped buffer
4612 * If flush took the "buffer" see if we have one from before
4614 if (!b->buf && m->bbuf)
4617 PerlIOBuf_get_base(aTHX_ f);
4621 return PerlIOBuf_unread(aTHX_ f, vbuf, count);
4625 PerlIOMmap_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
4627 PerlIOMmap * const m = PerlIOSelf(f, PerlIOMmap);
4628 PerlIOBuf * const b = &m->base;
4630 if (!b->buf || !(PerlIOBase(f)->flags & PERLIO_F_WRBUF)) {
4632 * No, or wrong sort of, buffer
4635 if (PerlIOMmap_unmap(aTHX_ f) != 0)
4639 * If unmap took the "buffer" see if we have one from before
4641 if (!b->buf && m->bbuf)
4644 PerlIOBuf_get_base(aTHX_ f);
4648 return PerlIOBuf_write(aTHX_ f, vbuf, count);
4652 PerlIOMmap_flush(pTHX_ PerlIO *f)
4654 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
4655 PerlIOBuf *b = &m->base;
4656 IV code = PerlIOBuf_flush(aTHX_ f);
4658 * Now we are "synced" at PerlIOBuf level
4665 if (PerlIOMmap_unmap(aTHX_ f) != 0)
4670 * We seem to have a PerlIOBuf buffer which was not mapped
4671 * remember it in case we need one later
4680 PerlIOMmap_fill(pTHX_ PerlIO *f)
4682 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
4683 IV code = PerlIO_flush(f);
4684 if (code == 0 && !b->buf) {
4685 code = PerlIOMmap_map(aTHX_ f);
4687 if (code == 0 && !(PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
4688 code = PerlIOBuf_fill(aTHX_ f);
4694 PerlIOMmap_close(pTHX_ PerlIO *f)
4696 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
4697 PerlIOBuf *b = &m->base;
4698 IV code = PerlIO_flush(f);
4702 b->ptr = b->end = b->buf;
4704 if (PerlIOBuf_close(aTHX_ f) != 0)
4710 PerlIOMmap_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
4712 return PerlIOBase_dup(aTHX_ f, o, param, flags);
4716 PERLIO_FUNCS_DECL(PerlIO_mmap) = {
4717 sizeof(PerlIO_funcs),
4720 PERLIO_K_BUFFERED|PERLIO_K_RAW,
4724 PerlIOBase_binmode, /* binmode */
4738 PerlIOBase_clearerr,
4739 PerlIOBase_setlinebuf,
4740 PerlIOMmap_get_base,
4744 PerlIOBuf_set_ptrcnt,
4747 #endif /* HAS_MMAP */
4750 Perl_PerlIO_stdin(pTHX)
4753 PerlIO_stdstreams(aTHX);
4755 return &PL_perlio[1];
4759 Perl_PerlIO_stdout(pTHX)
4762 PerlIO_stdstreams(aTHX);
4764 return &PL_perlio[2];
4768 Perl_PerlIO_stderr(pTHX)
4771 PerlIO_stdstreams(aTHX);
4773 return &PL_perlio[3];
4776 /*--------------------------------------------------------------------------------------*/
4779 PerlIO_getname(PerlIO *f, char *buf)
4784 bool exported = FALSE;
4785 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
4787 stdio = PerlIO_exportFILE(f,0);
4791 name = fgetname(stdio, buf);
4792 if (exported) PerlIO_releaseFILE(f,stdio);
4797 PERL_UNUSED_ARG(buf);
4798 Perl_croak(aTHX_ "Don't know how to get file name");
4804 /*--------------------------------------------------------------------------------------*/
4806 * Functions which can be called on any kind of PerlIO implemented in
4810 #undef PerlIO_fdopen
4812 PerlIO_fdopen(int fd, const char *mode)
4815 return PerlIO_openn(aTHX_ Nullch, mode, fd, 0, 0, NULL, 0, NULL);
4820 PerlIO_open(const char *path, const char *mode)
4823 SV *name = sv_2mortal(newSVpvn(path, strlen(path)));
4824 return PerlIO_openn(aTHX_ Nullch, mode, -1, 0, 0, NULL, 1, &name);
4827 #undef Perlio_reopen
4829 PerlIO_reopen(const char *path, const char *mode, PerlIO *f)
4832 SV *name = sv_2mortal(newSVpvn(path, strlen(path)));
4833 return PerlIO_openn(aTHX_ Nullch, mode, -1, 0, 0, f, 1, &name);
4838 PerlIO_getc(PerlIO *f)
4842 if ( 1 == PerlIO_read(f, buf, 1) ) {
4843 return (unsigned char) buf[0];
4848 #undef PerlIO_ungetc
4850 PerlIO_ungetc(PerlIO *f, int ch)
4855 if (PerlIO_unread(f, &buf, 1) == 1)
4863 PerlIO_putc(PerlIO *f, int ch)
4867 return PerlIO_write(f, &buf, 1);
4872 PerlIO_puts(PerlIO *f, const char *s)
4875 STRLEN len = strlen(s);
4876 return PerlIO_write(f, s, len);
4879 #undef PerlIO_rewind
4881 PerlIO_rewind(PerlIO *f)
4884 PerlIO_seek(f, (Off_t) 0, SEEK_SET);
4888 #undef PerlIO_vprintf
4890 PerlIO_vprintf(PerlIO *f, const char *fmt, va_list ap)
4893 SV *sv = newSVpvn("", 0);
4899 Perl_va_copy(ap, apc);
4900 sv_vcatpvf(sv, fmt, &apc);
4902 sv_vcatpvf(sv, fmt, &ap);
4904 s = SvPV_const(sv, len);
4905 wrote = PerlIO_write(f, s, len);
4910 #undef PerlIO_printf
4912 PerlIO_printf(PerlIO *f, const char *fmt, ...)
4917 result = PerlIO_vprintf(f, fmt, ap);
4922 #undef PerlIO_stdoutf
4924 PerlIO_stdoutf(const char *fmt, ...)
4930 result = PerlIO_vprintf(PerlIO_stdout(), fmt, ap);
4935 #undef PerlIO_tmpfile
4937 PerlIO_tmpfile(void)
4942 const int fd = win32_tmpfd();
4944 f = PerlIO_fdopen(fd, "w+b");
4946 # if defined(HAS_MKSTEMP) && ! defined(VMS) && ! defined(OS2)
4947 SV *sv = newSVpv("/tmp/PerlIO_XXXXXX", 0);
4949 * I have no idea how portable mkstemp() is ... NI-S
4951 const int fd = mkstemp(SvPVX(sv));
4953 f = PerlIO_fdopen(fd, "w+");
4955 PerlIOBase(f)->flags |= PERLIO_F_TEMP;
4956 PerlLIO_unlink(SvPVX_const(sv));
4959 # else /* !HAS_MKSTEMP, fallback to stdio tmpfile(). */
4960 FILE *stdio = PerlSIO_tmpfile();
4963 if ((f = PerlIO_push(aTHX_(PerlIO_allocate(aTHX)),
4964 PERLIO_FUNCS_CAST(&PerlIO_stdio),
4966 PerlIOStdio *s = PerlIOSelf(f, PerlIOStdio);
4972 # endif /* else HAS_MKSTEMP */
4973 #endif /* else WIN32 */
4980 #endif /* USE_SFIO */
4981 #endif /* PERLIO_IS_STDIO */
4983 /*======================================================================================*/
4985 * Now some functions in terms of above which may be needed even if we are
4986 * not in true PerlIO mode
4990 #undef PerlIO_setpos
4992 PerlIO_setpos(PerlIO *f, SV *pos)
4997 Off_t *posn = (Off_t *) SvPV(pos, len);
4998 if (f && len == sizeof(Off_t))
4999 return PerlIO_seek(f, *posn, SEEK_SET);
5001 SETERRNO(EINVAL, SS_IVCHAN);
5005 #undef PerlIO_setpos
5007 PerlIO_setpos(PerlIO *f, SV *pos)
5012 Fpos_t *fpos = (Fpos_t *) SvPV(pos, len);
5013 if (f && len == sizeof(Fpos_t)) {
5014 #if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64)
5015 return fsetpos64(f, fpos);
5017 return fsetpos(f, fpos);
5021 SETERRNO(EINVAL, SS_IVCHAN);
5027 #undef PerlIO_getpos
5029 PerlIO_getpos(PerlIO *f, SV *pos)
5032 Off_t posn = PerlIO_tell(f);
5033 sv_setpvn(pos, (char *) &posn, sizeof(posn));
5034 return (posn == (Off_t) - 1) ? -1 : 0;
5037 #undef PerlIO_getpos
5039 PerlIO_getpos(PerlIO *f, SV *pos)
5044 #if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64)
5045 code = fgetpos64(f, &fpos);
5047 code = fgetpos(f, &fpos);
5049 sv_setpvn(pos, (char *) &fpos, sizeof(fpos));
5054 #if (defined(PERLIO_IS_STDIO) || !defined(USE_SFIO)) && !defined(HAS_VPRINTF)
5057 vprintf(char *pat, char *args)
5059 _doprnt(pat, args, stdout);
5060 return 0; /* wrong, but perl doesn't use the return
5065 vfprintf(FILE *fd, char *pat, char *args)
5067 _doprnt(pat, args, fd);
5068 return 0; /* wrong, but perl doesn't use the return
5074 #ifndef PerlIO_vsprintf
5076 PerlIO_vsprintf(char *s, int n, const char *fmt, va_list ap)
5078 const int val = vsprintf(s, fmt, ap);
5080 if (strlen(s) >= (STRLEN) n) {
5082 (void) PerlIO_puts(Perl_error_log,
5083 "panic: sprintf overflow - memory corrupted!\n");
5091 #ifndef PerlIO_sprintf
5093 PerlIO_sprintf(char *s, int n, const char *fmt, ...)
5098 result = PerlIO_vsprintf(s, n, fmt, ap);
5106 * c-indentation-style: bsd
5108 * indent-tabs-mode: t
5111 * ex: set ts=8 sts=4 sw=4 noet: