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
33 # ifndef USE_CROSS_COMPILE
40 #define PERLIO_NOT_STDIO 0
41 #if !defined(PERLIO_IS_STDIO) && !defined(USE_SFIO)
47 * This file provides those parts of PerlIO abstraction
48 * which are not #defined in perlio.h.
49 * Which these are depends on various Configure #ifdef's
53 #define PERL_IN_PERLIO_C
56 #ifdef PERL_IMPLICIT_CONTEXT
64 /* Missing proto on LynxOS */
68 /* Call the callback or PerlIOBase, and return failure. */
69 #define Perl_PerlIO_or_Base(f, callback, base, failure, args) \
70 if (PerlIOValid(f)) { \
71 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;\
72 if (tab && tab->callback) \
73 return (*tab->callback) args; \
75 return PerlIOBase_ ## base args; \
78 SETERRNO(EBADF, SS_IVCHAN); \
81 /* Call the callback or fail, and return failure. */
82 #define Perl_PerlIO_or_fail(f, callback, failure, args) \
83 if (PerlIOValid(f)) { \
84 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;\
85 if (tab && tab->callback) \
86 return (*tab->callback) args; \
87 SETERRNO(EINVAL, LIB_INVARG); \
90 SETERRNO(EBADF, SS_IVCHAN); \
93 /* Call the callback or PerlIOBase, and be void. */
94 #define Perl_PerlIO_or_Base_void(f, callback, base, args) \
95 if (PerlIOValid(f)) { \
96 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;\
97 if (tab && tab->callback) \
98 (*tab->callback) args; \
100 PerlIOBase_ ## base args; \
103 SETERRNO(EBADF, SS_IVCHAN)
105 /* Call the callback or fail, and be void. */
106 #define Perl_PerlIO_or_fail_void(f, callback, args) \
107 if (PerlIOValid(f)) { \
108 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;\
109 if (tab && tab->callback) \
110 (*tab->callback) args; \
112 SETERRNO(EINVAL, LIB_INVARG); \
115 SETERRNO(EBADF, SS_IVCHAN)
119 perlsio_binmode(FILE *fp, int iotype, int mode)
122 * This used to be contents of do_binmode in doio.c
125 # if defined(atarist) || defined(__MINT__)
128 ((FILE *) fp)->_flag |= _IOBIN;
130 ((FILE *) fp)->_flag &= ~_IOBIN;
137 if (PerlLIO_setmode(fp, mode) != -1) {
139 if (PerlLIO_setmode(fileno(fp), mode) != -1) {
141 # if defined(WIN32) && defined(__BORLANDC__)
143 * The translation mode of the stream is maintained independent
145 * the translation mode of the fd in the Borland RTL (heavy
146 * digging through their runtime sources reveal). User has to
148 * the mode explicitly for the stream (though they don't
150 * this anywhere). GSAR 97-5-24
156 fp->flags &= ~_F_BIN;
164 # if defined(USEMYBINMODE)
166 if (my_binmode(fp, iotype, mode) != FALSE)
172 PERL_UNUSED_ARG(iotype);
173 PERL_UNUSED_ARG(mode);
181 #define O_ACCMODE 3 /* Assume traditional implementation */
185 PerlIO_intmode2str(int rawmode, char *mode, int *writing)
187 const int result = rawmode & O_ACCMODE;
192 ptype = IoTYPE_RDONLY;
195 ptype = IoTYPE_WRONLY;
203 *writing = (result != O_RDONLY);
205 if (result == O_RDONLY) {
209 else if (rawmode & O_APPEND) {
211 if (result != O_WRONLY)
216 if (result == O_WRONLY)
223 if (rawmode & O_BINARY)
229 #ifndef PERLIO_LAYERS
231 PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names)
233 if (!names || !*names
234 || strEQ(names, ":crlf")
235 || strEQ(names, ":raw")
236 || strEQ(names, ":bytes")
240 Perl_croak(aTHX_ "Cannot apply \"%s\" in non-PerlIO perl", names);
248 PerlIO_destruct(pTHX)
253 PerlIO_binmode(pTHX_ PerlIO *fp, int iotype, int mode, const char *names)
256 PERL_UNUSED_ARG(iotype);
257 PERL_UNUSED_ARG(mode);
258 PERL_UNUSED_ARG(names);
261 return perlsio_binmode(fp, iotype, mode);
266 PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
268 #if defined(PERL_MICRO) || defined(__SYMBIAN32__)
271 #ifdef PERL_IMPLICIT_SYS
272 return PerlSIO_fdupopen(f);
275 return win32_fdupopen(f);
278 const int fd = PerlLIO_dup(PerlIO_fileno(f));
282 const int omode = djgpp_get_stream_mode(f);
284 const int omode = fcntl(fd, F_GETFL);
286 PerlIO_intmode2str(omode,mode,NULL);
287 /* the r+ is a hack */
288 return PerlIO_fdopen(fd, mode);
293 SETERRNO(EBADF, SS_IVCHAN);
303 * De-mux PerlIO_openn() into fdopen, freopen and fopen type entries
307 PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd,
308 int imode, int perm, PerlIO *old, int narg, SV **args)
312 Perl_croak(aTHX_ "More than one argument to open");
314 if (*args == &PL_sv_undef)
315 return PerlIO_tmpfile();
317 const char *name = SvPV_nolen_const(*args);
318 if (*mode == IoTYPE_NUMERIC) {
319 fd = PerlLIO_open3(name, imode, perm);
321 return PerlIO_fdopen(fd, mode + 1);
324 return PerlIO_reopen(name, mode, old);
327 return PerlIO_open(name, mode);
332 return PerlIO_fdopen(fd, (char *) mode);
337 XS(XS_PerlIO__Layer__find)
341 Perl_croak(aTHX_ "Usage class->find(name[,load])");
343 const char * const name = SvPV_nolen_const(ST(1));
344 ST(0) = (strEQ(name, "crlf")
345 || strEQ(name, "raw")) ? &PL_sv_yes : &PL_sv_undef;
352 Perl_boot_core_PerlIO(pTHX)
354 newXS("PerlIO::Layer::find", XS_PerlIO__Layer__find, __FILE__);
360 #ifdef PERLIO_IS_STDIO
367 * Does nothing (yet) except force this file to be included in perl
368 * binary. That allows this file to force inclusion of other functions
369 * that may be required by loadable extensions e.g. for
370 * FileHandle::tmpfile
374 #undef PerlIO_tmpfile
381 #else /* PERLIO_IS_STDIO */
389 * This section is just to make sure these functions get pulled in from
393 #undef PerlIO_tmpfile
405 * Force this file to be included in perl binary. Which allows this
406 * file to force inclusion of other functions that may be required by
407 * loadable extensions e.g. for FileHandle::tmpfile
411 * Hack sfio does its own 'autoflush' on stdout in common cases. Flush
412 * results in a lot of lseek()s to regular files and lot of small
415 sfset(sfstdout, SF_SHARE, 0);
418 /* This is not the reverse of PerlIO_exportFILE(), PerlIO_releaseFILE() is. */
420 PerlIO_importFILE(FILE *stdio, const char *mode)
422 const int fd = fileno(stdio);
423 if (!mode || !*mode) {
426 return PerlIO_fdopen(fd, mode);
430 PerlIO_findFILE(PerlIO *pio)
432 const int fd = PerlIO_fileno(pio);
433 FILE * const f = fdopen(fd, "r+");
435 if (!f && errno == EINVAL)
437 if (!f && errno == EINVAL)
444 /*======================================================================================*/
446 * Implement all the PerlIO interface ourselves.
452 * We _MUST_ have <unistd.h> if we are using lseek() and may have large
459 #include <sys/mman.h>
463 PerlIO_debug(const char *fmt, ...)
468 if (!PL_perlio_debug_fd && !PL_tainting && PL_uid == PL_euid && PL_gid == PL_egid) {
469 const char * const s = PerlEnv_getenv("PERLIO_DEBUG");
471 PL_perlio_debug_fd = PerlLIO_open3(s, O_WRONLY | O_CREAT | O_APPEND, 0666);
473 PL_perlio_debug_fd = -1;
475 if (PL_perlio_debug_fd > 0) {
478 const char * const s = CopFILE(PL_curcop);
479 /* Use fixed buffer as sv_catpvf etc. needs SVs */
481 const STRLEN len = my_sprintf(buffer, "%.40s:%" IVdf " ", s ? s : "(none)", (IV) CopLINE(PL_curcop));
482 # ifdef USE_VSNPRINTF
483 const STRLEN len2 = vsnprintf(buffer+len, sizeof(buffer) - len, fmt, ap);
485 const STRLEN len2 = vsprintf(buffer+len, fmt, ap);
486 # endif /* USE_VSNPRINTF */
487 PerlLIO_write(PL_perlio_debug_fd, buffer, len + len2);
489 const char *s = CopFILE(PL_curcop);
491 SV * const sv = newSVpvs("");
492 Perl_sv_catpvf(aTHX_ sv, "%s:%" IVdf " ", s ? s : "(none)",
493 (IV) CopLINE(PL_curcop));
494 Perl_sv_vcatpvf(aTHX_ sv, fmt, &ap);
496 s = SvPV_const(sv, len);
497 PerlLIO_write(PL_perlio_debug_fd, s, len);
504 /*--------------------------------------------------------------------------------------*/
507 * Inner level routines
511 * Table of pointers to the PerlIO structs (malloc'ed)
513 #define PERLIO_TABLE_SIZE 64
516 PerlIO_allocate(pTHX)
520 * Find a free slot in the table, allocating new table as necessary
525 while ((f = *last)) {
527 last = (PerlIO **) (f);
528 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
534 Newxz(f,PERLIO_TABLE_SIZE,PerlIO);
542 #undef PerlIO_fdupopen
544 PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
546 if (PerlIOValid(f)) {
547 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
548 PerlIO_debug("fdupopen f=%p param=%p\n",(void*)f,(void*)param);
550 return (*tab->Dup)(aTHX_ PerlIO_allocate(aTHX), f, param, flags);
552 return PerlIOBase_dup(aTHX_ PerlIO_allocate(aTHX), f, param, flags);
556 SETERRNO(EBADF, SS_IVCHAN);
562 PerlIO_cleantable(pTHX_ PerlIO **tablep)
564 PerlIO * const table = *tablep;
567 PerlIO_cleantable(aTHX_(PerlIO **) & (table[0]));
568 for (i = PERLIO_TABLE_SIZE - 1; i > 0; i--) {
569 PerlIO * const f = table + i;
581 PerlIO_list_alloc(pTHX)
585 Newxz(list, 1, PerlIO_list_t);
591 PerlIO_list_free(pTHX_ PerlIO_list_t *list)
594 if (--list->refcnt == 0) {
597 for (i = 0; i < list->cur; i++) {
598 if (list->array[i].arg)
599 SvREFCNT_dec(list->array[i].arg);
601 Safefree(list->array);
609 PerlIO_list_push(pTHX_ PerlIO_list_t *list, PerlIO_funcs *funcs, SV *arg)
615 if (list->cur >= list->len) {
618 Renew(list->array, list->len, PerlIO_pair_t);
620 Newx(list->array, list->len, PerlIO_pair_t);
622 p = &(list->array[list->cur++]);
624 if ((p->arg = arg)) {
625 SvREFCNT_inc_simple_void_NN(arg);
630 PerlIO_clone_list(pTHX_ PerlIO_list_t *proto, CLONE_PARAMS *param)
632 PerlIO_list_t *list = NULL;
635 list = PerlIO_list_alloc(aTHX);
636 for (i=0; i < proto->cur; i++) {
638 if (proto->array[i].arg)
639 arg = PerlIO_sv_dup(aTHX_ proto->array[i].arg,param);
640 PerlIO_list_push(aTHX_ list, proto->array[i].funcs, arg);
647 PerlIO_clone(pTHX_ PerlInterpreter *proto, CLONE_PARAMS *param)
650 PerlIO **table = &proto->Iperlio;
653 PL_known_layers = PerlIO_clone_list(aTHX_ proto->Iknown_layers, param);
654 PL_def_layerlist = PerlIO_clone_list(aTHX_ proto->Idef_layerlist, param);
655 PerlIO_allocate(aTHX); /* root slot is never used */
656 PerlIO_debug("Clone %p from %p\n",(void*)aTHX,(void*)proto);
657 while ((f = *table)) {
659 table = (PerlIO **) (f++);
660 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
662 (void) fp_dup(f, 0, param);
669 PERL_UNUSED_ARG(proto);
670 PERL_UNUSED_ARG(param);
675 PerlIO_destruct(pTHX)
678 PerlIO **table = &PL_perlio;
681 PerlIO_debug("Destruct %p\n",(void*)aTHX);
683 while ((f = *table)) {
685 table = (PerlIO **) (f++);
686 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
690 if (l->tab->kind & PERLIO_K_DESTRUCT) {
691 PerlIO_debug("Destruct popping %s\n", l->tab->name);
705 PerlIO_pop(pTHX_ PerlIO *f)
707 const PerlIOl *l = *f;
709 PerlIO_debug("PerlIO_pop f=%p %s\n", (void*)f, l->tab->name);
710 if (l->tab->Popped) {
712 * If popped returns non-zero do not free its layer structure
713 * it has either done so itself, or it is shared and still in
716 if ((*l->tab->Popped) (aTHX_ f) != 0)
724 /* Return as an array the stack of layers on a filehandle. Note that
725 * the stack is returned top-first in the array, and there are three
726 * times as many array elements as there are layers in the stack: the
727 * first element of a layer triplet is the name, the second one is the
728 * arguments, and the third one is the flags. */
731 PerlIO_get_layers(pTHX_ PerlIO *f)
734 AV * const av = newAV();
736 if (PerlIOValid(f)) {
737 PerlIOl *l = PerlIOBase(f);
740 SV * const name = l->tab && l->tab->name ?
741 newSVpv(l->tab->name, 0) : &PL_sv_undef;
742 SV * const arg = l->tab && l->tab->Getarg ?
743 (*l->tab->Getarg)(aTHX_ &l, 0, 0) : &PL_sv_undef;
746 av_push(av, newSViv((IV)l->flags));
754 /*--------------------------------------------------------------------------------------*/
756 * XS Interface for perl code
760 PerlIO_find_layer(pTHX_ const char *name, STRLEN len, int load)
764 if ((SSize_t) len <= 0)
766 for (i = 0; i < PL_known_layers->cur; i++) {
767 PerlIO_funcs * const f = PL_known_layers->array[i].funcs;
768 if (memEQ(f->name, name, len) && f->name[len] == 0) {
769 PerlIO_debug("%.*s => %p\n", (int) len, name, (void*)f);
773 if (load && PL_subname && PL_def_layerlist
774 && PL_def_layerlist->cur >= 2) {
775 if (PL_in_load_module) {
776 Perl_croak(aTHX_ "Recursive call to Perl_load_module in PerlIO_find_layer");
779 SV * const pkgsv = newSVpvs("PerlIO");
780 SV * const layer = newSVpvn(name, len);
781 CV * const cv = get_cv("PerlIO::Layer::NoWarnings", FALSE);
783 SAVEINT(PL_in_load_module);
785 SAVEGENERICSV(PL_warnhook);
786 PL_warnhook = (SV *) (SvREFCNT_inc_simple_NN(cv));
790 * The two SVs are magically freed by load_module
792 Perl_load_module(aTHX_ 0, pkgsv, NULL, layer, NULL);
795 return PerlIO_find_layer(aTHX_ name, len, 0);
798 PerlIO_debug("Cannot find %.*s\n", (int) len, name);
802 #ifdef USE_ATTRIBUTES_FOR_PERLIO
805 perlio_mg_set(pTHX_ SV *sv, MAGIC *mg)
808 IO * const io = GvIOn((GV *) SvRV(sv));
809 PerlIO * const ifp = IoIFP(io);
810 PerlIO * const ofp = IoOFP(io);
811 Perl_warn(aTHX_ "set %" SVf " %p %p %p", sv, io, ifp, ofp);
817 perlio_mg_get(pTHX_ SV *sv, MAGIC *mg)
820 IO * const io = GvIOn((GV *) SvRV(sv));
821 PerlIO * const ifp = IoIFP(io);
822 PerlIO * const ofp = IoOFP(io);
823 Perl_warn(aTHX_ "get %" SVf " %p %p %p", sv, io, ifp, ofp);
829 perlio_mg_clear(pTHX_ SV *sv, MAGIC *mg)
831 Perl_warn(aTHX_ "clear %" SVf, sv);
836 perlio_mg_free(pTHX_ SV *sv, MAGIC *mg)
838 Perl_warn(aTHX_ "free %" SVf, sv);
842 MGVTBL perlio_vtab = {
850 XS(XS_io_MODIFY_SCALAR_ATTRIBUTES)
853 SV * const sv = SvRV(ST(1));
854 AV * const av = newAV();
858 sv_magic(sv, (SV *) av, PERL_MAGIC_ext, NULL, 0);
860 mg = mg_find(sv, PERL_MAGIC_ext);
861 mg->mg_virtual = &perlio_vtab;
863 Perl_warn(aTHX_ "attrib %" SVf, sv);
864 for (i = 2; i < items; i++) {
866 const char * const name = SvPV_const(ST(i), len);
867 SV * const layer = PerlIO_find_layer(aTHX_ name, len, 1);
869 av_push(av, SvREFCNT_inc_simple_NN(layer));
880 #endif /* USE_ATTIBUTES_FOR_PERLIO */
883 PerlIO_tab_sv(pTHX_ PerlIO_funcs *tab)
885 HV * const stash = gv_stashpvs("PerlIO::Layer", TRUE);
886 SV * const sv = sv_bless(newRV_noinc(newSViv(PTR2IV(tab))), stash);
890 XS(XS_PerlIO__Layer__NoWarnings)
892 /* This is used as a %SIG{__WARN__} handler to supress warnings
893 during loading of layers.
898 PerlIO_debug("warning:%s\n",SvPV_nolen_const(ST(0)));
902 XS(XS_PerlIO__Layer__find)
907 Perl_croak(aTHX_ "Usage class->find(name[,load])");
910 const char * const name = SvPV_const(ST(1), len);
911 const bool load = (items > 2) ? SvTRUE(ST(2)) : 0;
912 PerlIO_funcs * const layer = PerlIO_find_layer(aTHX_ name, len, load);
914 (layer) ? sv_2mortal(PerlIO_tab_sv(aTHX_ layer)) :
921 PerlIO_define_layer(pTHX_ PerlIO_funcs *tab)
924 if (!PL_known_layers)
925 PL_known_layers = PerlIO_list_alloc(aTHX);
926 PerlIO_list_push(aTHX_ PL_known_layers, tab, NULL);
927 PerlIO_debug("define %s %p\n", tab->name, (void*)tab);
931 PerlIO_parse_layers(pTHX_ PerlIO_list_t *av, const char *names)
935 const char *s = names;
937 while (isSPACE(*s) || *s == ':')
942 const char *as = NULL;
944 if (!isIDFIRST(*s)) {
946 * Message is consistent with how attribute lists are
947 * passed. Even though this means "foo : : bar" is
948 * seen as an invalid separator character.
950 const char q = ((*s == '\'') ? '"' : '\'');
951 if (ckWARN(WARN_LAYER))
952 Perl_warner(aTHX_ packWARN(WARN_LAYER),
953 "Invalid separator character %c%c%c in PerlIO layer specification %s",
955 SETERRNO(EINVAL, LIB_INVARG);
960 } while (isALNUM(*e));
976 * It's a nul terminated string, not allowed
977 * to \ the terminating null. Anything other
978 * character is passed over.
988 if (ckWARN(WARN_LAYER))
989 Perl_warner(aTHX_ packWARN(WARN_LAYER),
990 "Argument list not closed for PerlIO layer \"%.*s\"",
1002 PerlIO_funcs * const layer =
1003 PerlIO_find_layer(aTHX_ s, llen, 1);
1005 PerlIO_list_push(aTHX_ av, layer,
1011 if (ckWARN(WARN_LAYER))
1012 Perl_warner(aTHX_ packWARN(WARN_LAYER), "Unknown PerlIO layer \"%.*s\"",
1025 PerlIO_default_buffer(pTHX_ PerlIO_list_t *av)
1028 PERLIO_FUNCS_DECL(*tab) = &PerlIO_perlio;
1029 #ifdef PERLIO_USING_CRLF
1032 if (PerlIO_stdio.Set_ptrcnt)
1033 tab = &PerlIO_stdio;
1035 PerlIO_debug("Pushing %s\n", tab->name);
1036 PerlIO_list_push(aTHX_ av, PerlIO_find_layer(aTHX_ tab->name, 0, 0),
1041 PerlIO_arg_fetch(PerlIO_list_t *av, IV n)
1043 return av->array[n].arg;
1047 PerlIO_layer_fetch(pTHX_ PerlIO_list_t *av, IV n, PerlIO_funcs *def)
1049 if (n >= 0 && n < av->cur) {
1050 PerlIO_debug("Layer %" IVdf " is %s\n", n,
1051 av->array[n].funcs->name);
1052 return av->array[n].funcs;
1055 Perl_croak(aTHX_ "panic: PerlIO layer array corrupt");
1060 PerlIOPop_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
1062 PERL_UNUSED_ARG(mode);
1063 PERL_UNUSED_ARG(arg);
1064 PERL_UNUSED_ARG(tab);
1065 if (PerlIOValid(f)) {
1067 PerlIO_pop(aTHX_ f);
1073 PERLIO_FUNCS_DECL(PerlIO_remove) = {
1074 sizeof(PerlIO_funcs),
1077 PERLIO_K_DUMMY | PERLIO_K_UTF8,
1097 NULL, /* get_base */
1098 NULL, /* get_bufsiz */
1101 NULL, /* set_ptrcnt */
1105 PerlIO_default_layers(pTHX)
1108 if (!PL_def_layerlist) {
1109 const char * const s = (PL_tainting) ? NULL : PerlEnv_getenv("PERLIO");
1110 PERLIO_FUNCS_DECL(*osLayer) = &PerlIO_unix;
1111 PL_def_layerlist = PerlIO_list_alloc(aTHX);
1112 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_unix));
1114 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_win32));
1116 osLayer = &PerlIO_win32;
1119 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_raw));
1120 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_perlio));
1121 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_stdio));
1122 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_crlf));
1124 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_mmap));
1126 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_utf8));
1127 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_remove));
1128 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_byte));
1129 PerlIO_list_push(aTHX_ PL_def_layerlist,
1130 PerlIO_find_layer(aTHX_ osLayer->name, 0, 0),
1133 PerlIO_parse_layers(aTHX_ PL_def_layerlist, s);
1136 PerlIO_default_buffer(aTHX_ PL_def_layerlist);
1139 if (PL_def_layerlist->cur < 2) {
1140 PerlIO_default_buffer(aTHX_ PL_def_layerlist);
1142 return PL_def_layerlist;
1146 Perl_boot_core_PerlIO(pTHX)
1148 #ifdef USE_ATTRIBUTES_FOR_PERLIO
1149 newXS("io::MODIFY_SCALAR_ATTRIBUTES", XS_io_MODIFY_SCALAR_ATTRIBUTES,
1152 newXS("PerlIO::Layer::find", XS_PerlIO__Layer__find, __FILE__);
1153 newXS("PerlIO::Layer::NoWarnings", XS_PerlIO__Layer__NoWarnings, __FILE__);
1157 PerlIO_default_layer(pTHX_ I32 n)
1160 PerlIO_list_t * const av = PerlIO_default_layers(aTHX);
1163 return PerlIO_layer_fetch(aTHX_ av, n, PERLIO_FUNCS_CAST(&PerlIO_stdio));
1166 #define PerlIO_default_top() PerlIO_default_layer(aTHX_ -1)
1167 #define PerlIO_default_btm() PerlIO_default_layer(aTHX_ 0)
1170 PerlIO_stdstreams(pTHX)
1174 PerlIO_allocate(aTHX);
1175 PerlIO_fdopen(0, "Ir" PERLIO_STDTEXT);
1176 PerlIO_fdopen(1, "Iw" PERLIO_STDTEXT);
1177 PerlIO_fdopen(2, "Iw" PERLIO_STDTEXT);
1182 PerlIO_push(pTHX_ PerlIO *f, PERLIO_FUNCS_DECL(*tab), const char *mode, SV *arg)
1184 if (tab->fsize != sizeof(PerlIO_funcs)) {
1186 Perl_croak(aTHX_ "Layer does not match this perl");
1190 if (tab->size < sizeof(PerlIOl)) {
1193 /* Real layer with a data area */
1196 Newxz(temp, tab->size, char);
1200 l->tab = (PerlIO_funcs*) tab;
1202 PerlIO_debug("PerlIO_push f=%p %s %s %p\n",
1203 (void*)f, tab->name,
1204 (mode) ? mode : "(Null)", (void*)arg);
1205 if (*l->tab->Pushed &&
1207 (aTHX_ f, mode, arg, (PerlIO_funcs*) tab) != 0) {
1208 PerlIO_pop(aTHX_ f);
1217 /* Pseudo-layer where push does its own stack adjust */
1218 PerlIO_debug("PerlIO_push f=%p %s %s %p\n", (void*)f, tab->name,
1219 (mode) ? mode : "(Null)", (void*)arg);
1221 (*tab->Pushed) (aTHX_ f, mode, arg, (PerlIO_funcs*) tab) != 0) {
1229 PerlIOBase_binmode(pTHX_ PerlIO *f)
1231 if (PerlIOValid(f)) {
1232 /* Is layer suitable for raw stream ? */
1233 if (PerlIOBase(f)->tab->kind & PERLIO_K_RAW) {
1234 /* Yes - turn off UTF-8-ness, to undo UTF-8 locale effects */
1235 PerlIOBase(f)->flags &= ~PERLIO_F_UTF8;
1238 /* Not suitable - pop it */
1239 PerlIO_pop(aTHX_ f);
1247 PerlIORaw_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
1249 PERL_UNUSED_ARG(mode);
1250 PERL_UNUSED_ARG(arg);
1251 PERL_UNUSED_ARG(tab);
1253 if (PerlIOValid(f)) {
1258 * Strip all layers that are not suitable for a raw stream
1261 while (t && (l = *t)) {
1262 if (l->tab->Binmode) {
1263 /* Has a handler - normal case */
1264 if ((*l->tab->Binmode)(aTHX_ f) == 0) {
1266 /* Layer still there - move down a layer */
1275 /* No handler - pop it */
1276 PerlIO_pop(aTHX_ t);
1279 if (PerlIOValid(f)) {
1280 PerlIO_debug(":raw f=%p :%s\n", (void*)f, PerlIOBase(f)->tab->name);
1288 PerlIO_apply_layera(pTHX_ PerlIO *f, const char *mode,
1289 PerlIO_list_t *layers, IV n, IV max)
1293 PerlIO_funcs * const tab = PerlIO_layer_fetch(aTHX_ layers, n, NULL);
1295 if (!PerlIO_push(aTHX_ f, tab, mode, PerlIOArg)) {
1306 PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names)
1310 PerlIO_list_t * const layers = PerlIO_list_alloc(aTHX);
1311 code = PerlIO_parse_layers(aTHX_ layers, names);
1313 code = PerlIO_apply_layera(aTHX_ f, mode, layers, 0, layers->cur);
1315 PerlIO_list_free(aTHX_ layers);
1321 /*--------------------------------------------------------------------------------------*/
1323 * Given the abstraction above the public API functions
1327 PerlIO_binmode(pTHX_ PerlIO *f, int iotype, int mode, const char *names)
1329 PerlIO_debug("PerlIO_binmode f=%p %s %c %x %s\n", (void*)f,
1330 (PerlIOBase(f)) ? PerlIOBase(f)->tab->name : "(Null)",
1331 iotype, mode, (names) ? names : "(Null)");
1334 /* Do not flush etc. if (e.g.) switching encodings.
1335 if a pushed layer knows it needs to flush lower layers
1336 (for example :unix which is never going to call them)
1337 it can do the flush when it is pushed.
1339 return PerlIO_apply_layers(aTHX_ f, NULL, names) == 0 ? TRUE : FALSE;
1342 /* Fake 5.6 legacy of using this call to turn ON O_TEXT */
1343 #ifdef PERLIO_USING_CRLF
1344 /* Legacy binmode only has meaning if O_TEXT has a value distinct from
1345 O_BINARY so we can look for it in mode.
1347 if (!(mode & O_BINARY)) {
1349 /* FIXME?: Looking down the layer stack seems wrong,
1350 but is a way of reaching past (say) an encoding layer
1351 to flip CRLF-ness of the layer(s) below
1354 /* Perhaps we should turn on bottom-most aware layer
1355 e.g. Ilya's idea that UNIX TTY could serve
1357 if (PerlIOBase(f)->tab->kind & PERLIO_K_CANCRLF) {
1358 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF)) {
1359 /* Not in text mode - flush any pending stuff and flip it */
1361 PerlIOBase(f)->flags |= PERLIO_F_CRLF;
1363 /* Only need to turn it on in one layer so we are done */
1368 /* Not finding a CRLF aware layer presumably means we are binary
1369 which is not what was requested - so we failed
1370 We _could_ push :crlf layer but so could caller
1375 /* Legacy binmode is now _defined_ as being equivalent to pushing :raw
1376 So code that used to be here is now in PerlIORaw_pushed().
1378 return PerlIO_push(aTHX_ f, PERLIO_FUNCS_CAST(&PerlIO_raw), NULL, NULL) ? TRUE : FALSE;
1383 PerlIO__close(pTHX_ PerlIO *f)
1385 if (PerlIOValid(f)) {
1386 PerlIO_funcs * const tab = PerlIOBase(f)->tab;
1387 if (tab && tab->Close)
1388 return (*tab->Close)(aTHX_ f);
1390 return PerlIOBase_close(aTHX_ f);
1393 SETERRNO(EBADF, SS_IVCHAN);
1399 Perl_PerlIO_close(pTHX_ PerlIO *f)
1401 const int code = PerlIO__close(aTHX_ f);
1402 while (PerlIOValid(f)) {
1403 PerlIO_pop(aTHX_ f);
1409 Perl_PerlIO_fileno(pTHX_ PerlIO *f)
1412 Perl_PerlIO_or_Base(f, Fileno, fileno, -1, (aTHX_ f));
1416 PerlIO_context_layers(pTHX_ const char *mode)
1419 const char *type = NULL;
1421 * Need to supply default layer info from open.pm
1424 SV * const layers = PL_curcop->cop_io;
1427 type = SvPV_const(layers, len);
1428 if (type && mode[0] != 'r') {
1430 * Skip to write part
1432 const char * const s = strchr(type, 0);
1433 if (s && (STRLEN)(s - type) < len) {
1442 static PerlIO_funcs *
1443 PerlIO_layer_from_ref(pTHX_ SV *sv)
1447 * For any scalar type load the handler which is bundled with perl
1449 if (SvTYPE(sv) < SVt_PVAV)
1450 return PerlIO_find_layer(aTHX_ STR_WITH_LEN("scalar"), 1);
1453 * For other types allow if layer is known but don't try and load it
1455 switch (SvTYPE(sv)) {
1457 return PerlIO_find_layer(aTHX_ STR_WITH_LEN("Array"), 0);
1459 return PerlIO_find_layer(aTHX_ STR_WITH_LEN("Hash"), 0);
1461 return PerlIO_find_layer(aTHX_ STR_WITH_LEN("Code"), 0);
1463 return PerlIO_find_layer(aTHX_ STR_WITH_LEN("Glob"), 0);
1469 PerlIO_resolve_layers(pTHX_ const char *layers,
1470 const char *mode, int narg, SV **args)
1473 PerlIO_list_t *def = PerlIO_default_layers(aTHX);
1476 PerlIO_stdstreams(aTHX);
1478 SV * const arg = *args;
1480 * If it is a reference but not an object see if we have a handler
1483 if (SvROK(arg) && !sv_isobject(arg)) {
1484 PerlIO_funcs * const handler = PerlIO_layer_from_ref(aTHX_ SvRV(arg));
1486 def = PerlIO_list_alloc(aTHX);
1487 PerlIO_list_push(aTHX_ def, handler, &PL_sv_undef);
1491 * Don't fail if handler cannot be found :via(...) etc. may do
1492 * something sensible else we will just stringfy and open
1497 if (!layers || !*layers)
1498 layers = PerlIO_context_layers(aTHX_ mode);
1499 if (layers && *layers) {
1503 av = PerlIO_list_alloc(aTHX);
1504 for (i = 0; i < def->cur; i++) {
1505 PerlIO_list_push(aTHX_ av, def->array[i].funcs,
1512 if (PerlIO_parse_layers(aTHX_ av, layers) == 0) {
1516 PerlIO_list_free(aTHX_ av);
1528 PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd,
1529 int imode, int perm, PerlIO *f, int narg, SV **args)
1532 if (!f && narg == 1 && *args == &PL_sv_undef) {
1533 if ((f = PerlIO_tmpfile())) {
1534 if (!layers || !*layers)
1535 layers = PerlIO_context_layers(aTHX_ mode);
1536 if (layers && *layers)
1537 PerlIO_apply_layers(aTHX_ f, mode, layers);
1541 PerlIO_list_t *layera;
1543 PerlIO_funcs *tab = NULL;
1544 if (PerlIOValid(f)) {
1546 * This is "reopen" - it is not tested as perl does not use it
1550 layera = PerlIO_list_alloc(aTHX);
1552 SV * const arg = (l->tab->Getarg)
1553 ? (*l->tab->Getarg) (aTHX_ &l, NULL, 0)
1555 PerlIO_list_push(aTHX_ layera, l->tab, arg);
1556 l = *PerlIONext(&l);
1560 layera = PerlIO_resolve_layers(aTHX_ layers, mode, narg, args);
1566 * Start at "top" of layer stack
1568 n = layera->cur - 1;
1570 PerlIO_funcs * const t = PerlIO_layer_fetch(aTHX_ layera, n, NULL);
1579 * Found that layer 'n' can do opens - call it
1581 if (narg > 1 && !(tab->kind & PERLIO_K_MULTIARG)) {
1582 Perl_croak(aTHX_ "More than one argument to open(,':%s')",tab->name);
1584 PerlIO_debug("openn(%s,'%s','%s',%d,%x,%o,%p,%d,%p)\n",
1585 tab->name, layers ? layers : "(Null)", mode, fd,
1586 imode, perm, (void*)f, narg, (void*)args);
1588 f = (*tab->Open) (aTHX_ tab, layera, n, mode, fd, imode, perm,
1591 SETERRNO(EINVAL, LIB_INVARG);
1595 if (n + 1 < layera->cur) {
1597 * More layers above the one that we used to open -
1600 if (PerlIO_apply_layera(aTHX_ f, mode, layera, n + 1, layera->cur) != 0) {
1601 /* If pushing layers fails close the file */
1608 PerlIO_list_free(aTHX_ layera);
1615 Perl_PerlIO_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
1617 Perl_PerlIO_or_Base(f, Read, read, -1, (aTHX_ f, vbuf, count));
1621 Perl_PerlIO_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
1623 Perl_PerlIO_or_Base(f, Unread, unread, -1, (aTHX_ f, vbuf, count));
1627 Perl_PerlIO_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
1629 Perl_PerlIO_or_fail(f, Write, -1, (aTHX_ f, vbuf, count));
1633 Perl_PerlIO_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
1635 Perl_PerlIO_or_fail(f, Seek, -1, (aTHX_ f, offset, whence));
1639 Perl_PerlIO_tell(pTHX_ PerlIO *f)
1641 Perl_PerlIO_or_fail(f, Tell, -1, (aTHX_ f));
1645 Perl_PerlIO_flush(pTHX_ PerlIO *f)
1650 const PerlIO_funcs *tab = PerlIOBase(f)->tab;
1652 if (tab && tab->Flush)
1653 return (*tab->Flush) (aTHX_ f);
1655 return 0; /* If no Flush defined, silently succeed. */
1658 PerlIO_debug("Cannot flush f=%p\n", (void*)f);
1659 SETERRNO(EBADF, SS_IVCHAN);
1665 * Is it good API design to do flush-all on NULL, a potentially
1666 * errorneous input? Maybe some magical value (PerlIO*
1667 * PERLIO_FLUSH_ALL = (PerlIO*)-1;)? Yes, stdio does similar
1668 * things on fflush(NULL), but should we be bound by their design
1671 PerlIO **table = &PL_perlio;
1673 while ((f = *table)) {
1675 table = (PerlIO **) (f++);
1676 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
1677 if (*f && PerlIO_flush(f) != 0)
1687 PerlIOBase_flush_linebuf(pTHX)
1690 PerlIO **table = &PL_perlio;
1692 while ((f = *table)) {
1694 table = (PerlIO **) (f++);
1695 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
1698 flags & (PERLIO_F_LINEBUF | PERLIO_F_CANWRITE))
1699 == (PERLIO_F_LINEBUF | PERLIO_F_CANWRITE))
1707 Perl_PerlIO_fill(pTHX_ PerlIO *f)
1709 Perl_PerlIO_or_fail(f, Fill, -1, (aTHX_ f));
1713 PerlIO_isutf8(PerlIO *f)
1716 return (PerlIOBase(f)->flags & PERLIO_F_UTF8) != 0;
1718 SETERRNO(EBADF, SS_IVCHAN);
1724 Perl_PerlIO_eof(pTHX_ PerlIO *f)
1726 Perl_PerlIO_or_Base(f, Eof, eof, -1, (aTHX_ f));
1730 Perl_PerlIO_error(pTHX_ PerlIO *f)
1732 Perl_PerlIO_or_Base(f, Error, error, -1, (aTHX_ f));
1736 Perl_PerlIO_clearerr(pTHX_ PerlIO *f)
1738 Perl_PerlIO_or_Base_void(f, Clearerr, clearerr, (aTHX_ f));
1742 Perl_PerlIO_setlinebuf(pTHX_ PerlIO *f)
1744 Perl_PerlIO_or_Base_void(f, Setlinebuf, setlinebuf, (aTHX_ f));
1748 PerlIO_has_base(PerlIO *f)
1750 if (PerlIOValid(f)) {
1751 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
1754 return (tab->Get_base != NULL);
1755 SETERRNO(EINVAL, LIB_INVARG);
1758 SETERRNO(EBADF, SS_IVCHAN);
1764 PerlIO_fast_gets(PerlIO *f)
1766 if (PerlIOValid(f) && (PerlIOBase(f)->flags & PERLIO_F_FASTGETS)) {
1767 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
1770 return (tab->Set_ptrcnt != NULL);
1771 SETERRNO(EINVAL, LIB_INVARG);
1774 SETERRNO(EBADF, SS_IVCHAN);
1780 PerlIO_has_cntptr(PerlIO *f)
1782 if (PerlIOValid(f)) {
1783 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
1786 return (tab->Get_ptr != NULL && tab->Get_cnt != NULL);
1787 SETERRNO(EINVAL, LIB_INVARG);
1790 SETERRNO(EBADF, SS_IVCHAN);
1796 PerlIO_canset_cnt(PerlIO *f)
1798 if (PerlIOValid(f)) {
1799 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
1802 return (tab->Set_ptrcnt != NULL);
1803 SETERRNO(EINVAL, LIB_INVARG);
1806 SETERRNO(EBADF, SS_IVCHAN);
1812 Perl_PerlIO_get_base(pTHX_ PerlIO *f)
1814 Perl_PerlIO_or_fail(f, Get_base, NULL, (aTHX_ f));
1818 Perl_PerlIO_get_bufsiz(pTHX_ PerlIO *f)
1820 Perl_PerlIO_or_fail(f, Get_bufsiz, -1, (aTHX_ f));
1824 Perl_PerlIO_get_ptr(pTHX_ PerlIO *f)
1826 Perl_PerlIO_or_fail(f, Get_ptr, NULL, (aTHX_ f));
1830 Perl_PerlIO_get_cnt(pTHX_ PerlIO *f)
1832 Perl_PerlIO_or_fail(f, Get_cnt, -1, (aTHX_ f));
1836 Perl_PerlIO_set_cnt(pTHX_ PerlIO *f, int cnt)
1838 Perl_PerlIO_or_fail_void(f, Set_ptrcnt, (aTHX_ f, NULL, cnt));
1842 Perl_PerlIO_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, int cnt)
1844 Perl_PerlIO_or_fail_void(f, Set_ptrcnt, (aTHX_ f, ptr, cnt));
1848 /*--------------------------------------------------------------------------------------*/
1850 * utf8 and raw dummy layers
1854 PerlIOUtf8_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
1856 PERL_UNUSED_CONTEXT;
1857 PERL_UNUSED_ARG(mode);
1858 PERL_UNUSED_ARG(arg);
1859 if (PerlIOValid(f)) {
1860 if (tab->kind & PERLIO_K_UTF8)
1861 PerlIOBase(f)->flags |= PERLIO_F_UTF8;
1863 PerlIOBase(f)->flags &= ~PERLIO_F_UTF8;
1869 PERLIO_FUNCS_DECL(PerlIO_utf8) = {
1870 sizeof(PerlIO_funcs),
1873 PERLIO_K_DUMMY | PERLIO_K_UTF8,
1893 NULL, /* get_base */
1894 NULL, /* get_bufsiz */
1897 NULL, /* set_ptrcnt */
1900 PERLIO_FUNCS_DECL(PerlIO_byte) = {
1901 sizeof(PerlIO_funcs),
1924 NULL, /* get_base */
1925 NULL, /* get_bufsiz */
1928 NULL, /* set_ptrcnt */
1932 PerlIORaw_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
1933 IV n, const char *mode, int fd, int imode, int perm,
1934 PerlIO *old, int narg, SV **args)
1936 PerlIO_funcs * const tab = PerlIO_default_btm();
1937 PERL_UNUSED_ARG(self);
1938 if (tab && tab->Open)
1939 return (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
1941 SETERRNO(EINVAL, LIB_INVARG);
1945 PERLIO_FUNCS_DECL(PerlIO_raw) = {
1946 sizeof(PerlIO_funcs),
1969 NULL, /* get_base */
1970 NULL, /* get_bufsiz */
1973 NULL, /* set_ptrcnt */
1975 /*--------------------------------------------------------------------------------------*/
1976 /*--------------------------------------------------------------------------------------*/
1978 * "Methods" of the "base class"
1982 PerlIOBase_fileno(pTHX_ PerlIO *f)
1984 return PerlIOValid(f) ? PerlIO_fileno(PerlIONext(f)) : -1;
1988 PerlIO_modestr(PerlIO * f, char *buf)
1991 if (PerlIOValid(f)) {
1992 const IV flags = PerlIOBase(f)->flags;
1993 if (flags & PERLIO_F_APPEND) {
1995 if (flags & PERLIO_F_CANREAD) {
1999 else if (flags & PERLIO_F_CANREAD) {
2001 if (flags & PERLIO_F_CANWRITE)
2004 else if (flags & PERLIO_F_CANWRITE) {
2006 if (flags & PERLIO_F_CANREAD) {
2010 #ifdef PERLIO_USING_CRLF
2011 if (!(flags & PERLIO_F_CRLF))
2021 PerlIOBase_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
2023 PerlIOl * const l = PerlIOBase(f);
2024 PERL_UNUSED_CONTEXT;
2025 PERL_UNUSED_ARG(arg);
2027 l->flags &= ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE |
2028 PERLIO_F_TRUNCATE | PERLIO_F_APPEND);
2029 if (tab->Set_ptrcnt != NULL)
2030 l->flags |= PERLIO_F_FASTGETS;
2032 if (*mode == IoTYPE_NUMERIC || *mode == IoTYPE_IMPLICIT)
2036 l->flags |= PERLIO_F_CANREAD;
2039 l->flags |= PERLIO_F_APPEND | PERLIO_F_CANWRITE;
2042 l->flags |= PERLIO_F_TRUNCATE | PERLIO_F_CANWRITE;
2045 SETERRNO(EINVAL, LIB_INVARG);
2051 l->flags |= PERLIO_F_CANREAD | PERLIO_F_CANWRITE;
2054 l->flags &= ~PERLIO_F_CRLF;
2057 l->flags |= PERLIO_F_CRLF;
2060 SETERRNO(EINVAL, LIB_INVARG);
2067 l->flags |= l->next->flags &
2068 (PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_TRUNCATE |
2073 PerlIO_debug("PerlIOBase_pushed f=%p %s %s fl=%08" UVxf " (%s)\n",
2074 f, PerlIOBase(f)->tab->name, (omode) ? omode : "(Null)",
2075 l->flags, PerlIO_modestr(f, temp));
2081 PerlIOBase_popped(pTHX_ PerlIO *f)
2083 PERL_UNUSED_CONTEXT;
2089 PerlIOBase_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
2092 * Save the position as current head considers it
2094 const Off_t old = PerlIO_tell(f);
2095 PerlIO_push(aTHX_ f, PERLIO_FUNCS_CAST(&PerlIO_pending), "r", NULL);
2096 PerlIOSelf(f, PerlIOBuf)->posn = old;
2097 return PerlIOBuf_unread(aTHX_ f, vbuf, count);
2101 PerlIOBase_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
2103 STDCHAR *buf = (STDCHAR *) vbuf;
2105 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD)) {
2106 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2107 SETERRNO(EBADF, SS_IVCHAN);
2113 SSize_t avail = PerlIO_get_cnt(f);
2116 take = ((SSize_t)count < avail) ? (SSize_t)count : avail;
2118 STDCHAR *ptr = PerlIO_get_ptr(f);
2119 Copy(ptr, buf, take, STDCHAR);
2120 PerlIO_set_ptrcnt(f, ptr + take, (avail -= take));
2123 if (avail == 0) /* set_ptrcnt could have reset avail */
2126 if (count > 0 && avail <= 0) {
2127 if (PerlIO_fill(f) != 0)
2132 return (buf - (STDCHAR *) vbuf);
2138 PerlIOBase_noop_ok(pTHX_ PerlIO *f)
2140 PERL_UNUSED_CONTEXT;
2146 PerlIOBase_noop_fail(pTHX_ PerlIO *f)
2148 PERL_UNUSED_CONTEXT;
2154 PerlIOBase_close(pTHX_ PerlIO *f)
2157 if (PerlIOValid(f)) {
2158 PerlIO *n = PerlIONext(f);
2159 code = PerlIO_flush(f);
2160 PerlIOBase(f)->flags &=
2161 ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_OPEN);
2162 while (PerlIOValid(n)) {
2163 const PerlIO_funcs * const tab = PerlIOBase(n)->tab;
2164 if (tab && tab->Close) {
2165 if ((*tab->Close)(aTHX_ n) != 0)
2170 PerlIOBase(n)->flags &=
2171 ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_OPEN);
2177 SETERRNO(EBADF, SS_IVCHAN);
2183 PerlIOBase_eof(pTHX_ PerlIO *f)
2185 PERL_UNUSED_CONTEXT;
2186 if (PerlIOValid(f)) {
2187 return (PerlIOBase(f)->flags & PERLIO_F_EOF) != 0;
2193 PerlIOBase_error(pTHX_ PerlIO *f)
2195 PERL_UNUSED_CONTEXT;
2196 if (PerlIOValid(f)) {
2197 return (PerlIOBase(f)->flags & PERLIO_F_ERROR) != 0;
2203 PerlIOBase_clearerr(pTHX_ PerlIO *f)
2205 if (PerlIOValid(f)) {
2206 PerlIO * const n = PerlIONext(f);
2207 PerlIOBase(f)->flags &= ~(PERLIO_F_ERROR | PERLIO_F_EOF);
2214 PerlIOBase_setlinebuf(pTHX_ PerlIO *f)
2216 PERL_UNUSED_CONTEXT;
2217 if (PerlIOValid(f)) {
2218 PerlIOBase(f)->flags |= PERLIO_F_LINEBUF;
2223 PerlIO_sv_dup(pTHX_ SV *arg, CLONE_PARAMS *param)
2229 return sv_dup(arg, param);
2232 return newSVsv(arg);
2235 PERL_UNUSED_ARG(param);
2236 return newSVsv(arg);
2241 PerlIOBase_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
2243 PerlIO * const nexto = PerlIONext(o);
2244 if (PerlIOValid(nexto)) {
2245 const PerlIO_funcs * const tab = PerlIOBase(nexto)->tab;
2246 if (tab && tab->Dup)
2247 f = (*tab->Dup)(aTHX_ f, nexto, param, flags);
2249 f = PerlIOBase_dup(aTHX_ f, nexto, param, flags);
2252 PerlIO_funcs * const self = PerlIOBase(o)->tab;
2255 PerlIO_debug("PerlIOBase_dup %s f=%p o=%p param=%p\n",
2256 self->name, (void*)f, (void*)o, (void*)param);
2258 arg = (*self->Getarg)(aTHX_ o, param, flags);
2262 f = PerlIO_push(aTHX_ f, self, PerlIO_modestr(o,buf), arg);
2271 perl_mutex PerlIO_mutex;
2274 /* PL_perlio_fd_refcnt[] is in intrpvar.h */
2276 /* Must be called with PerlIO_mutex locked. */
2278 S_more_refcounted_fds(pTHX_ const int new_fd) {
2280 const int old_max = PL_perlio_fd_refcnt_size;
2281 const int new_max = 16 + (new_fd & ~15);
2284 PerlIO_debug("More fds - old=%d, need %d, new=%d\n",
2285 old_max, new_fd, new_max);
2287 if (new_fd < old_max) {
2291 assert (new_max > new_fd);
2294 = PerlMemShared_realloc(PL_perlio_fd_refcnt, new_max * sizeof(int));
2298 MUTEX_UNLOCK(&PerlIO_mutex);
2300 /* Can't use PerlIO to write as it allocates memory */
2301 PerlLIO_write(PerlIO_fileno(Perl_error_log),
2302 PL_no_mem, strlen(PL_no_mem));
2306 PL_perlio_fd_refcnt_size = new_max;
2307 PL_perlio_fd_refcnt = new_array;
2309 PerlIO_debug("Zeroing %p, %d\n",
2310 (void*)(new_array + old_max),
2313 Zero(new_array + old_max, new_max - old_max, int);
2320 /* Place holder for stdstreams call ??? */
2322 MUTEX_INIT(&PerlIO_mutex);
2324 PERL_UNUSED_CONTEXT;
2329 PerlIOUnix_refcnt_inc(int fd)
2336 MUTEX_LOCK(&PerlIO_mutex);
2338 if (fd >= PL_perlio_fd_refcnt_size)
2339 S_more_refcounted_fds(aTHX_ fd);
2341 PL_perlio_fd_refcnt[fd]++;
2342 PerlIO_debug("fd %d refcnt=%d\n",fd,PL_perlio_fd_refcnt[fd]);
2345 MUTEX_UNLOCK(&PerlIO_mutex);
2351 PerlIOUnix_refcnt_dec(int fd)
2358 MUTEX_LOCK(&PerlIO_mutex);
2360 /* XXX should this be a panic? */
2361 if (fd >= PL_perlio_fd_refcnt_size)
2362 S_more_refcounted_fds(aTHX_ fd);
2364 /* XXX should this be a panic if it drops below 0? */
2365 cnt = --PL_perlio_fd_refcnt[fd];
2366 PerlIO_debug("fd %d refcnt=%d\n",fd,cnt);
2368 MUTEX_UNLOCK(&PerlIO_mutex);
2375 PerlIO_cleanup(pTHX)
2380 PerlIO_debug("Cleanup layers for %p\n",(void*)aTHX);
2382 PerlIO_debug("Cleanup layers\n");
2384 /* Raise STDIN..STDERR refcount so we don't close them */
2385 for (i=0; i < 3; i++)
2386 PerlIOUnix_refcnt_inc(i);
2387 PerlIO_cleantable(aTHX_ &PL_perlio);
2388 /* Restore STDIN..STDERR refcount */
2389 for (i=0; i < 3; i++)
2390 PerlIOUnix_refcnt_dec(i);
2392 if (PL_known_layers) {
2393 PerlIO_list_free(aTHX_ PL_known_layers);
2394 PL_known_layers = NULL;
2396 if (PL_def_layerlist) {
2397 PerlIO_list_free(aTHX_ PL_def_layerlist);
2398 PL_def_layerlist = NULL;
2404 /*--------------------------------------------------------------------------------------*/
2406 * Bottom-most level for UNIX-like case
2410 struct _PerlIO base; /* The generic part */
2411 int fd; /* UNIX like file descriptor */
2412 int oflags; /* open/fcntl flags */
2416 PerlIOUnix_oflags(const char *mode)
2419 if (*mode == IoTYPE_IMPLICIT || *mode == IoTYPE_NUMERIC)
2424 if (*++mode == '+') {
2431 oflags = O_CREAT | O_TRUNC;
2432 if (*++mode == '+') {
2441 oflags = O_CREAT | O_APPEND;
2442 if (*++mode == '+') {
2455 else if (*mode == 't') {
2457 oflags &= ~O_BINARY;
2461 * Always open in binary mode
2464 if (*mode || oflags == -1) {
2465 SETERRNO(EINVAL, LIB_INVARG);
2472 PerlIOUnix_fileno(pTHX_ PerlIO *f)
2474 PERL_UNUSED_CONTEXT;
2475 return PerlIOSelf(f, PerlIOUnix)->fd;
2479 PerlIOUnix_setfd(pTHX_ PerlIO *f, int fd, int imode)
2481 PerlIOUnix * const s = PerlIOSelf(f, PerlIOUnix);
2484 if (PerlLIO_fstat(fd, &st) == 0) {
2485 if (!S_ISREG(st.st_mode)) {
2486 PerlIO_debug("%d is not regular file\n",fd);
2487 PerlIOBase(f)->flags |= PERLIO_F_NOTREG;
2490 PerlIO_debug("%d _is_ a regular file\n",fd);
2496 PerlIOUnix_refcnt_inc(fd);
2497 PERL_UNUSED_CONTEXT;
2501 PerlIOUnix_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
2503 IV code = PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
2504 if (*PerlIONext(f)) {
2505 /* We never call down so do any pending stuff now */
2506 PerlIO_flush(PerlIONext(f));
2508 * XXX could (or should) we retrieve the oflags from the open file
2509 * handle rather than believing the "mode" we are passed in? XXX
2510 * Should the value on NULL mode be 0 or -1?
2512 PerlIOUnix_setfd(aTHX_ f, PerlIO_fileno(PerlIONext(f)),
2513 mode ? PerlIOUnix_oflags(mode) : -1);
2515 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
2521 PerlIOUnix_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
2523 const int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2525 PERL_UNUSED_CONTEXT;
2526 if (PerlIOBase(f)->flags & PERLIO_F_NOTREG) {
2528 SETERRNO(ESPIPE, LIB_INVARG);
2530 SETERRNO(EINVAL, LIB_INVARG);
2534 new_loc = PerlLIO_lseek(fd, offset, whence);
2535 if (new_loc == (Off_t) - 1)
2537 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
2542 PerlIOUnix_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2543 IV n, const char *mode, int fd, int imode,
2544 int perm, PerlIO *f, int narg, SV **args)
2546 if (PerlIOValid(f)) {
2547 if (PerlIOBase(f)->flags & PERLIO_F_OPEN)
2548 (*PerlIOBase(f)->tab->Close)(aTHX_ f);
2551 if (*mode == IoTYPE_NUMERIC)
2554 imode = PerlIOUnix_oflags(mode);
2558 const char *path = SvPV_nolen_const(*args);
2559 fd = PerlLIO_open3(path, imode, perm);
2563 if (*mode == IoTYPE_IMPLICIT)
2566 f = PerlIO_allocate(aTHX);
2568 if (!PerlIOValid(f)) {
2569 if (!(f = PerlIO_push(aTHX_ f, self, mode, PerlIOArg))) {
2573 PerlIOUnix_setfd(aTHX_ f, fd, imode);
2574 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
2575 if (*mode == IoTYPE_APPEND)
2576 PerlIOUnix_seek(aTHX_ f, 0, SEEK_END);
2583 * FIXME: pop layers ???
2591 PerlIOUnix_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
2593 const PerlIOUnix * const os = PerlIOSelf(o, PerlIOUnix);
2595 if (flags & PERLIO_DUP_FD) {
2596 fd = PerlLIO_dup(fd);
2599 f = PerlIOBase_dup(aTHX_ f, o, param, flags);
2601 /* If all went well overwrite fd in dup'ed lay with the dup()'ed fd */
2602 PerlIOUnix_setfd(aTHX_ f, fd, os->oflags);
2611 PerlIOUnix_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
2614 const int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2615 #ifdef PERLIO_STD_SPECIAL
2617 return PERLIO_STD_IN(fd, vbuf, count);
2619 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD) ||
2620 PerlIOBase(f)->flags & (PERLIO_F_EOF|PERLIO_F_ERROR)) {
2624 const SSize_t len = PerlLIO_read(fd, vbuf, count);
2625 if (len >= 0 || errno != EINTR) {
2627 if (errno != EAGAIN) {
2628 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2631 else if (len == 0 && count != 0) {
2632 PerlIOBase(f)->flags |= PERLIO_F_EOF;
2643 PerlIOUnix_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
2646 const int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2647 #ifdef PERLIO_STD_SPECIAL
2648 if (fd == 1 || fd == 2)
2649 return PERLIO_STD_OUT(fd, vbuf, count);
2652 const SSize_t len = PerlLIO_write(fd, vbuf, count);
2653 if (len >= 0 || errno != EINTR) {
2655 if (errno != EAGAIN) {
2656 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2667 PerlIOUnix_tell(pTHX_ PerlIO *f)
2669 PERL_UNUSED_CONTEXT;
2671 return PerlLIO_lseek(PerlIOSelf(f, PerlIOUnix)->fd, 0, SEEK_CUR);
2676 PerlIOUnix_close(pTHX_ PerlIO *f)
2679 const int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2681 if (PerlIOBase(f)->flags & PERLIO_F_OPEN) {
2682 if (PerlIOUnix_refcnt_dec(fd) > 0) {
2683 PerlIOBase(f)->flags &= ~PERLIO_F_OPEN;
2688 SETERRNO(EBADF,SS_IVCHAN);
2691 while (PerlLIO_close(fd) != 0) {
2692 if (errno != EINTR) {
2699 PerlIOBase(f)->flags &= ~PERLIO_F_OPEN;
2704 PERLIO_FUNCS_DECL(PerlIO_unix) = {
2705 sizeof(PerlIO_funcs),
2712 PerlIOBase_binmode, /* binmode */
2722 PerlIOBase_noop_ok, /* flush */
2723 PerlIOBase_noop_fail, /* fill */
2726 PerlIOBase_clearerr,
2727 PerlIOBase_setlinebuf,
2728 NULL, /* get_base */
2729 NULL, /* get_bufsiz */
2732 NULL, /* set_ptrcnt */
2735 /*--------------------------------------------------------------------------------------*/
2740 #if defined(VMS) && !defined(STDIO_BUFFER_WRITABLE)
2741 /* perl5.8 - This ensures the last minute VMS ungetc fix is not
2742 broken by the last second glibc 2.3 fix
2744 #define STDIO_BUFFER_WRITABLE
2749 struct _PerlIO base;
2750 FILE *stdio; /* The stream */
2754 PerlIOStdio_fileno(pTHX_ PerlIO *f)
2756 PERL_UNUSED_CONTEXT;
2758 if (PerlIOValid(f)) {
2759 FILE * const s = PerlIOSelf(f, PerlIOStdio)->stdio;
2761 return PerlSIO_fileno(s);
2768 PerlIOStdio_mode(const char *mode, char *tmode)
2770 char * const ret = tmode;
2776 #if defined(PERLIO_USING_CRLF) || defined(__CYGWIN__)
2784 PerlIOStdio_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
2787 if (PerlIOValid(f) && PerlIOValid(n = PerlIONext(f))) {
2788 PerlIO_funcs * const toptab = PerlIOBase(n)->tab;
2789 if (toptab == tab) {
2790 /* Top is already stdio - pop self (duplicate) and use original */
2791 PerlIO_pop(aTHX_ f);
2794 const int fd = PerlIO_fileno(n);
2797 if (fd >= 0 && (stdio = PerlSIO_fdopen(fd,
2798 mode = PerlIOStdio_mode(mode, tmode)))) {
2799 PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
2800 /* We never call down so do any pending stuff now */
2801 PerlIO_flush(PerlIONext(f));
2808 return PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
2813 PerlIO_importFILE(FILE *stdio, const char *mode)
2819 if (!mode || !*mode) {
2820 /* We need to probe to see how we can open the stream
2821 so start with read/write and then try write and read
2822 we dup() so that we can fclose without loosing the fd.
2824 Note that the errno value set by a failing fdopen
2825 varies between stdio implementations.
2827 const int fd = PerlLIO_dup(fileno(stdio));
2828 FILE *f2 = PerlSIO_fdopen(fd, (mode = "r+"));
2830 f2 = PerlSIO_fdopen(fd, (mode = "w"));
2833 f2 = PerlSIO_fdopen(fd, (mode = "r"));
2836 /* Don't seem to be able to open */
2842 if ((f = PerlIO_push(aTHX_(f = PerlIO_allocate(aTHX)), PERLIO_FUNCS_CAST(&PerlIO_stdio), mode, NULL))) {
2843 s = PerlIOSelf(f, PerlIOStdio);
2851 PerlIOStdio_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2852 IV n, const char *mode, int fd, int imode,
2853 int perm, PerlIO *f, int narg, SV **args)
2856 if (PerlIOValid(f)) {
2857 const char * const path = SvPV_nolen_const(*args);
2858 PerlIOStdio * const s = PerlIOSelf(f, PerlIOStdio);
2860 PerlIOUnix_refcnt_dec(fileno(s->stdio));
2861 stdio = PerlSIO_freopen(path, (mode = PerlIOStdio_mode(mode, tmode)),
2866 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2871 const char * const path = SvPV_nolen_const(*args);
2872 if (*mode == IoTYPE_NUMERIC) {
2874 fd = PerlLIO_open3(path, imode, perm);
2878 bool appended = FALSE;
2880 /* Cygwin wants its 'b' early. */
2882 mode = PerlIOStdio_mode(mode, tmode);
2884 stdio = PerlSIO_fopen(path, mode);
2887 f = PerlIO_allocate(aTHX);
2890 mode = PerlIOStdio_mode(mode, tmode);
2891 f = PerlIO_push(aTHX_ f, self, mode, PerlIOArg);
2893 PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
2894 PerlIOUnix_refcnt_inc(fileno(stdio));
2896 PerlSIO_fclose(stdio);
2908 if (*mode == IoTYPE_IMPLICIT) {
2915 stdio = PerlSIO_stdin;
2918 stdio = PerlSIO_stdout;
2921 stdio = PerlSIO_stderr;
2926 stdio = PerlSIO_fdopen(fd, mode =
2927 PerlIOStdio_mode(mode, tmode));
2931 f = PerlIO_allocate(aTHX);
2933 if ((f = PerlIO_push(aTHX_ f, self, mode, PerlIOArg))) {
2934 PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
2935 PerlIOUnix_refcnt_inc(fileno(stdio));
2945 PerlIOStdio_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
2947 /* This assumes no layers underneath - which is what
2948 happens, but is not how I remember it. NI-S 2001/10/16
2950 if ((f = PerlIOBase_dup(aTHX_ f, o, param, flags))) {
2951 FILE *stdio = PerlIOSelf(o, PerlIOStdio)->stdio;
2952 const int fd = fileno(stdio);
2954 if (flags & PERLIO_DUP_FD) {
2955 const int dfd = PerlLIO_dup(fileno(stdio));
2957 stdio = PerlSIO_fdopen(dfd, PerlIO_modestr(o,mode));
2962 /* FIXME: To avoid messy error recovery if dup fails
2963 re-use the existing stdio as though flag was not set
2967 stdio = PerlSIO_fdopen(fd, PerlIO_modestr(o,mode));
2969 PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
2970 PerlIOUnix_refcnt_inc(fileno(stdio));
2976 PerlIOStdio_invalidate_fileno(pTHX_ FILE *f)
2978 PERL_UNUSED_CONTEXT;
2980 /* XXX this could use PerlIO_canset_fileno() and
2981 * PerlIO_set_fileno() support from Configure
2983 # if defined(__UCLIBC__)
2984 /* uClibc must come before glibc because it defines __GLIBC__ as well. */
2987 # elif defined(__GLIBC__)
2988 /* There may be a better way for GLIBC:
2989 - libio.h defines a flag to not close() on cleanup
2993 # elif defined(__sun__)
2995 # elif defined(__hpux)
2999 /* Next one ->_file seems to be a reasonable fallback, i.e. if
3000 your platform does not have special entry try this one.
3001 [For OSF only have confirmation for Tru64 (alpha)
3002 but assume other OSFs will be similar.]
3004 # elif defined(_AIX) || defined(__osf__) || defined(__irix__)
3007 # elif defined(__FreeBSD__)
3008 /* There may be a better way on FreeBSD:
3009 - we could insert a dummy func in the _close function entry
3010 f->_close = (int (*)(void *)) dummy_close;
3014 # elif defined(__OpenBSD__)
3015 /* There may be a better way on OpenBSD:
3016 - we could insert a dummy func in the _close function entry
3017 f->_close = (int (*)(void *)) dummy_close;
3021 # elif defined(__EMX__)
3022 /* f->_flags &= ~_IOOPEN; */ /* Will leak stream->_buffer */
3025 # elif defined(__CYGWIN__)
3026 /* There may be a better way on CYGWIN:
3027 - we could insert a dummy func in the _close function entry
3028 f->_close = (int (*)(void *)) dummy_close;
3032 # elif defined(WIN32)
3033 # if defined(__BORLANDC__)
3034 f->fd = PerlLIO_dup(fileno(f));
3035 # elif defined(UNDER_CE)
3036 /* WIN_CE does not have access to FILE internals, it hardly has FILE
3045 /* Sarathy's code did this - we fall back to a dup/dup2 hack
3046 (which isn't thread safe) instead
3048 # error "Don't know how to set FILE.fileno on your platform"
3056 PerlIOStdio_close(pTHX_ PerlIO *f)
3058 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3064 const int fd = fileno(stdio);
3069 #ifdef SOCKS5_VERSION_NAME
3070 /* Socks lib overrides close() but stdio isn't linked to
3071 that library (though we are) - so we must call close()
3072 on sockets on stdio's behalf.
3075 Sock_size_t optlen = sizeof(int);
3076 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, (void *) &optval, &optlen) == 0)
3079 if (PerlIOUnix_refcnt_dec(fd) > 0) /* File descriptor still in use */
3082 /* For STD* handles, don't close stdio, since we shared the FILE *, too. */
3083 if (stdio == stdin) /* Some stdios are buggy fflush-ing inputs */
3085 if (stdio == stdout || stdio == stderr)
3086 return PerlIO_flush(f);
3087 /* Tricky - must fclose(stdio) to free memory but not close(fd)
3088 Use Sarathy's trick from maint-5.6 to invalidate the
3089 fileno slot of the FILE *
3091 result = PerlIO_flush(f);
3093 invalidate = PerlIOStdio_invalidate_fileno(aTHX_ stdio);
3095 dupfd = PerlLIO_dup(fd);
3097 result = PerlSIO_fclose(stdio);
3098 /* We treat error from stdio as success if we invalidated
3099 errno may NOT be expected EBADF
3101 if (invalidate && result != 0) {
3105 #ifdef SOCKS5_VERSION_NAME
3106 /* in SOCKS' case, let close() determine return value */
3110 PerlLIO_dup2(dupfd,fd);
3111 PerlLIO_close(dupfd);
3118 PerlIOStdio_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
3121 FILE * const s = PerlIOSelf(f, PerlIOStdio)->stdio;
3125 STDCHAR *buf = (STDCHAR *) vbuf;
3127 * Perl is expecting PerlIO_getc() to fill the buffer Linux's
3128 * stdio does not do that for fread()
3130 const int ch = PerlSIO_fgetc(s);
3137 got = PerlSIO_fread(vbuf, 1, count, s);
3138 if (got == 0 && PerlSIO_ferror(s))
3140 if (got >= 0 || errno != EINTR)
3143 SETERRNO(0,0); /* just in case */
3149 PerlIOStdio_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3152 FILE * const s = PerlIOSelf(f, PerlIOStdio)->stdio;
3154 #ifdef STDIO_BUFFER_WRITABLE
3155 if (PerlIO_fast_gets(f) && PerlIO_has_base(f)) {
3156 STDCHAR *buf = ((STDCHAR *) vbuf) + count;
3157 STDCHAR *base = PerlIO_get_base(f);
3158 SSize_t cnt = PerlIO_get_cnt(f);
3159 STDCHAR *ptr = PerlIO_get_ptr(f);
3160 SSize_t avail = ptr - base;
3162 if (avail > count) {
3166 Move(buf-avail,ptr,avail,STDCHAR);
3169 PerlIO_set_ptrcnt(f,ptr,cnt+avail);
3170 if (PerlSIO_feof(s) && unread >= 0)
3171 PerlSIO_clearerr(s);
3176 if (PerlIO_has_cntptr(f)) {
3177 /* We can get pointer to buffer but not its base
3178 Do ungetc() but check chars are ending up in the
3181 STDCHAR *eptr = (STDCHAR*)PerlSIO_get_ptr(s);
3182 STDCHAR *buf = ((STDCHAR *) vbuf) + count;
3184 const int ch = *--buf & 0xFF;
3185 if (ungetc(ch,s) != ch) {
3186 /* ungetc did not work */
3189 if ((STDCHAR*)PerlSIO_get_ptr(s) != --eptr || ((*eptr & 0xFF) != ch)) {
3190 /* Did not change pointer as expected */
3191 fgetc(s); /* get char back again */
3201 unread += PerlIOBase_unread(aTHX_ f, vbuf, count);
3207 PerlIOStdio_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3212 got = PerlSIO_fwrite(vbuf, 1, count,
3213 PerlIOSelf(f, PerlIOStdio)->stdio);
3214 if (got >= 0 || errno != EINTR)
3217 SETERRNO(0,0); /* just in case */
3223 PerlIOStdio_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
3225 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3226 PERL_UNUSED_CONTEXT;
3228 return PerlSIO_fseek(stdio, offset, whence);
3232 PerlIOStdio_tell(pTHX_ PerlIO *f)
3234 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3235 PERL_UNUSED_CONTEXT;
3237 return PerlSIO_ftell(stdio);
3241 PerlIOStdio_flush(pTHX_ PerlIO *f)
3243 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3244 PERL_UNUSED_CONTEXT;
3246 if (PerlIOBase(f)->flags & PERLIO_F_CANWRITE) {
3247 return PerlSIO_fflush(stdio);
3253 * FIXME: This discards ungetc() and pre-read stuff which is not
3254 * right if this is just a "sync" from a layer above Suspect right
3255 * design is to do _this_ but not have layer above flush this
3256 * layer read-to-read
3259 * Not writeable - sync by attempting a seek
3261 const int err = errno;
3262 if (PerlSIO_fseek(stdio, (Off_t) 0, SEEK_CUR) != 0)
3270 PerlIOStdio_eof(pTHX_ PerlIO *f)
3272 PERL_UNUSED_CONTEXT;
3274 return PerlSIO_feof(PerlIOSelf(f, PerlIOStdio)->stdio);
3278 PerlIOStdio_error(pTHX_ PerlIO *f)
3280 PERL_UNUSED_CONTEXT;
3282 return PerlSIO_ferror(PerlIOSelf(f, PerlIOStdio)->stdio);
3286 PerlIOStdio_clearerr(pTHX_ PerlIO *f)
3288 PERL_UNUSED_CONTEXT;
3290 PerlSIO_clearerr(PerlIOSelf(f, PerlIOStdio)->stdio);
3294 PerlIOStdio_setlinebuf(pTHX_ PerlIO *f)
3296 PERL_UNUSED_CONTEXT;
3298 #ifdef HAS_SETLINEBUF
3299 PerlSIO_setlinebuf(PerlIOSelf(f, PerlIOStdio)->stdio);
3301 PerlSIO_setvbuf(PerlIOSelf(f, PerlIOStdio)->stdio, NULL, _IOLBF, 0);
3307 PerlIOStdio_get_base(pTHX_ PerlIO *f)
3309 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3310 return (STDCHAR*)PerlSIO_get_base(stdio);
3314 PerlIOStdio_get_bufsiz(pTHX_ PerlIO *f)
3316 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3317 return PerlSIO_get_bufsiz(stdio);
3321 #ifdef USE_STDIO_PTR
3323 PerlIOStdio_get_ptr(pTHX_ PerlIO *f)
3325 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3326 return (STDCHAR*)PerlSIO_get_ptr(stdio);
3330 PerlIOStdio_get_cnt(pTHX_ PerlIO *f)
3332 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3333 return PerlSIO_get_cnt(stdio);
3337 PerlIOStdio_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
3339 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3341 #ifdef STDIO_PTR_LVALUE
3342 PerlSIO_set_ptr(stdio, (void*)ptr); /* LHS STDCHAR* cast non-portable */
3343 #ifdef STDIO_PTR_LVAL_SETS_CNT
3344 if (PerlSIO_get_cnt(stdio) != (cnt)) {
3345 assert(PerlSIO_get_cnt(stdio) == (cnt));
3348 #if (!defined(STDIO_PTR_LVAL_NOCHANGE_CNT))
3350 * Setting ptr _does_ change cnt - we are done
3354 #else /* STDIO_PTR_LVALUE */
3356 #endif /* STDIO_PTR_LVALUE */
3359 * Now (or only) set cnt
3361 #ifdef STDIO_CNT_LVALUE
3362 PerlSIO_set_cnt(stdio, cnt);
3363 #else /* STDIO_CNT_LVALUE */
3364 #if (defined(STDIO_PTR_LVALUE) && defined(STDIO_PTR_LVAL_SETS_CNT))
3365 PerlSIO_set_ptr(stdio,
3366 PerlSIO_get_ptr(stdio) + (PerlSIO_get_cnt(stdio) -
3368 #else /* STDIO_PTR_LVAL_SETS_CNT */
3370 #endif /* STDIO_PTR_LVAL_SETS_CNT */
3371 #endif /* STDIO_CNT_LVALUE */
3378 PerlIOStdio_fill(pTHX_ PerlIO *f)
3380 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3382 PERL_UNUSED_CONTEXT;
3385 * fflush()ing read-only streams can cause trouble on some stdio-s
3387 if ((PerlIOBase(f)->flags & PERLIO_F_CANWRITE)) {
3388 if (PerlSIO_fflush(stdio) != 0)
3391 c = PerlSIO_fgetc(stdio);
3395 #if (defined(STDIO_PTR_LVALUE) && (defined(STDIO_CNT_LVALUE) || defined(STDIO_PTR_LVAL_SETS_CNT)))
3397 #ifdef STDIO_BUFFER_WRITABLE
3398 if (PerlIO_fast_gets(f) && PerlIO_has_base(f)) {
3399 /* Fake ungetc() to the real buffer in case system's ungetc
3402 STDCHAR *base = (STDCHAR*)PerlSIO_get_base(stdio);
3403 SSize_t cnt = PerlSIO_get_cnt(stdio);
3404 STDCHAR *ptr = (STDCHAR*)PerlSIO_get_ptr(stdio);
3405 if (ptr == base+1) {
3406 *--ptr = (STDCHAR) c;
3407 PerlIOStdio_set_ptrcnt(aTHX_ f,ptr,cnt+1);
3408 if (PerlSIO_feof(stdio))
3409 PerlSIO_clearerr(stdio);
3415 if (PerlIO_has_cntptr(f)) {
3417 if (PerlIOStdio_unread(aTHX_ f,&ch,1) == 1) {
3424 /* An ungetc()d char is handled separately from the regular
3425 * buffer, so we stuff it in the buffer ourselves.
3426 * Should never get called as should hit code above
3428 *(--((*stdio)->_ptr)) = (unsigned char) c;
3431 /* If buffer snoop scheme above fails fall back to
3434 if (PerlSIO_ungetc(c, stdio) != c)
3442 PERLIO_FUNCS_DECL(PerlIO_stdio) = {
3443 sizeof(PerlIO_funcs),
3445 sizeof(PerlIOStdio),
3446 PERLIO_K_BUFFERED|PERLIO_K_RAW,
3450 PerlIOBase_binmode, /* binmode */
3464 PerlIOStdio_clearerr,
3465 PerlIOStdio_setlinebuf,
3467 PerlIOStdio_get_base,
3468 PerlIOStdio_get_bufsiz,
3473 #ifdef USE_STDIO_PTR
3474 PerlIOStdio_get_ptr,
3475 PerlIOStdio_get_cnt,
3476 # if defined(HAS_FAST_STDIO) && defined(USE_FAST_STDIO)
3477 PerlIOStdio_set_ptrcnt,
3480 # endif /* HAS_FAST_STDIO && USE_FAST_STDIO */
3485 #endif /* USE_STDIO_PTR */
3488 /* Note that calls to PerlIO_exportFILE() are reversed using
3489 * PerlIO_releaseFILE(), not importFILE. */
3491 PerlIO_exportFILE(PerlIO * f, const char *mode)
3495 if (PerlIOValid(f)) {
3498 if (!mode || !*mode) {
3499 mode = PerlIO_modestr(f, buf);
3501 stdio = PerlSIO_fdopen(PerlIO_fileno(f), mode);
3505 /* De-link any lower layers so new :stdio sticks */
3507 if ((f2 = PerlIO_push(aTHX_ f, PERLIO_FUNCS_CAST(&PerlIO_stdio), buf, NULL))) {
3508 PerlIOStdio *s = PerlIOSelf((f = f2), PerlIOStdio);
3510 /* Link previous lower layers under new one */
3514 /* restore layers list */
3524 PerlIO_findFILE(PerlIO *f)
3528 if (l->tab == &PerlIO_stdio) {
3529 PerlIOStdio *s = PerlIOSelf(&l, PerlIOStdio);
3532 l = *PerlIONext(&l);
3534 /* Uses fallback "mode" via PerlIO_modestr() in PerlIO_exportFILE */
3535 return PerlIO_exportFILE(f, NULL);
3538 /* Use this to reverse PerlIO_exportFILE calls. */
3540 PerlIO_releaseFILE(PerlIO *p, FILE *f)
3545 if (l->tab == &PerlIO_stdio) {
3546 PerlIOStdio *s = PerlIOSelf(&l, PerlIOStdio);
3547 if (s->stdio == f) {
3549 PerlIO_pop(aTHX_ p);
3558 /*--------------------------------------------------------------------------------------*/
3560 * perlio buffer layer
3564 PerlIOBuf_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
3566 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3567 const int fd = PerlIO_fileno(f);
3568 if (fd >= 0 && PerlLIO_isatty(fd)) {
3569 PerlIOBase(f)->flags |= PERLIO_F_LINEBUF | PERLIO_F_TTY;
3571 if (*PerlIONext(f)) {
3572 const Off_t posn = PerlIO_tell(PerlIONext(f));
3573 if (posn != (Off_t) - 1) {
3577 return PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
3581 PerlIOBuf_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
3582 IV n, const char *mode, int fd, int imode, int perm,
3583 PerlIO *f, int narg, SV **args)
3585 if (PerlIOValid(f)) {
3586 PerlIO *next = PerlIONext(f);
3588 PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIOBase(next)->tab);
3589 if (tab && tab->Open)
3591 (*tab->Open)(aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
3593 if (!next || (*PerlIOBase(f)->tab->Pushed) (aTHX_ f, mode, PerlIOArg, self) != 0) {
3598 PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIO_default_btm());
3600 if (*mode == IoTYPE_IMPLICIT) {
3606 if (tab && tab->Open)
3607 f = (*tab->Open)(aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
3610 SETERRNO(EINVAL, LIB_INVARG);
3612 if (PerlIO_push(aTHX_ f, self, mode, PerlIOArg) == 0) {
3614 * if push fails during open, open fails. close will pop us.
3619 fd = PerlIO_fileno(f);
3620 if (init && fd == 2) {
3622 * Initial stderr is unbuffered
3624 PerlIOBase(f)->flags |= PERLIO_F_UNBUF;
3626 #ifdef PERLIO_USING_CRLF
3627 # ifdef PERLIO_IS_BINMODE_FD
3628 if (PERLIO_IS_BINMODE_FD(fd))
3629 PerlIO_binmode(aTHX_ f, '<'/*not used*/, O_BINARY, NULL);
3633 * do something about failing setmode()? --jhi
3635 PerlLIO_setmode(fd, O_BINARY);
3644 * This "flush" is akin to sfio's sync in that it handles files in either
3645 * read or write state. For write state, we put the postponed data through
3646 * the next layers. For read state, we seek() the next layers to the
3647 * offset given by current position in the buffer, and discard the buffer
3648 * state (XXXX supposed to be for seek()able buffers only, but now it is done
3649 * in any case?). Then the pass the stick further in chain.
3652 PerlIOBuf_flush(pTHX_ PerlIO *f)
3654 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3656 PerlIO *n = PerlIONext(f);
3657 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF) {
3659 * write() the buffer
3661 const STDCHAR *buf = b->buf;
3662 const STDCHAR *p = buf;
3663 while (p < b->ptr) {
3664 SSize_t count = PerlIO_write(n, p, b->ptr - p);
3668 else if (count < 0 || PerlIO_error(n)) {
3669 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
3674 b->posn += (p - buf);
3676 else if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
3677 STDCHAR *buf = PerlIO_get_base(f);
3679 * Note position change
3681 b->posn += (b->ptr - buf);
3682 if (b->ptr < b->end) {
3683 /* We did not consume all of it - try and seek downstream to
3684 our logical position
3686 if (PerlIOValid(n) && PerlIO_seek(n, b->posn, SEEK_SET) == 0) {
3687 /* Reload n as some layers may pop themselves on seek */
3688 b->posn = PerlIO_tell(n = PerlIONext(f));
3691 /* Seek failed (e.g. pipe or tty). Do NOT clear buffer or pre-read
3692 data is lost for good - so return saying "ok" having undone
3695 b->posn -= (b->ptr - buf);
3700 b->ptr = b->end = b->buf;
3701 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
3702 /* We check for Valid because of dubious decision to make PerlIO_flush(NULL) flush all */
3703 if (PerlIOValid(n) && PerlIO_flush(n) != 0)
3708 /* This discards the content of the buffer after b->ptr, and rereads
3709 * the buffer from the position off in the layer downstream; here off
3710 * is at offset corresponding to b->ptr - b->buf.
3713 PerlIOBuf_fill(pTHX_ PerlIO *f)
3715 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3716 PerlIO *n = PerlIONext(f);
3719 * Down-stream flush is defined not to loose read data so is harmless.
3720 * we would not normally be fill'ing if there was data left in anycase.
3722 if (PerlIO_flush(f) != 0) /* XXXX Check that its seek() succeeded?! */
3724 if (PerlIOBase(f)->flags & PERLIO_F_TTY)
3725 PerlIOBase_flush_linebuf(aTHX);
3728 PerlIO_get_base(f); /* allocate via vtable */
3730 assert(b->buf); /* The b->buf does get allocated via the vtable system. */
3732 b->ptr = b->end = b->buf;
3734 if (!PerlIOValid(n)) {
3735 PerlIOBase(f)->flags |= PERLIO_F_EOF;
3739 if (PerlIO_fast_gets(n)) {
3741 * Layer below is also buffered. We do _NOT_ want to call its
3742 * ->Read() because that will loop till it gets what we asked for
3743 * which may hang on a pipe etc. Instead take anything it has to
3744 * hand, or ask it to fill _once_.
3746 avail = PerlIO_get_cnt(n);
3748 avail = PerlIO_fill(n);
3750 avail = PerlIO_get_cnt(n);
3752 if (!PerlIO_error(n) && PerlIO_eof(n))
3757 STDCHAR *ptr = PerlIO_get_ptr(n);
3758 const SSize_t cnt = avail;
3759 if (avail > (SSize_t)b->bufsiz)
3761 Copy(ptr, b->buf, avail, STDCHAR);
3762 PerlIO_set_ptrcnt(n, ptr + avail, cnt - avail);
3766 avail = PerlIO_read(n, b->ptr, b->bufsiz);
3770 PerlIOBase(f)->flags |= PERLIO_F_EOF;
3772 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
3775 b->end = b->buf + avail;
3776 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3781 PerlIOBuf_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
3783 if (PerlIOValid(f)) {
3784 const PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3787 return PerlIOBase_read(aTHX_ f, vbuf, count);
3793 PerlIOBuf_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3795 const STDCHAR *buf = (const STDCHAR *) vbuf + count;
3796 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3799 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
3804 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
3806 * Buffer is already a read buffer, we can overwrite any chars
3807 * which have been read back to buffer start
3809 avail = (b->ptr - b->buf);
3813 * Buffer is idle, set it up so whole buffer is available for
3817 b->end = b->buf + avail;
3819 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3821 * Buffer extends _back_ from where we are now
3823 b->posn -= b->bufsiz;
3825 if (avail > (SSize_t) count) {
3827 * If we have space for more than count, just move count
3835 * In simple stdio-like ungetc() case chars will be already
3838 if (buf != b->ptr) {
3839 Copy(buf, b->ptr, avail, STDCHAR);
3843 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
3847 unread += PerlIOBase_unread(aTHX_ f, vbuf, count);
3853 PerlIOBuf_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3855 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3856 const STDCHAR *buf = (const STDCHAR *) vbuf;
3857 const STDCHAR *flushptr = buf;
3861 if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
3863 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
3864 if (PerlIO_flush(f) != 0) {
3868 if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF) {
3869 flushptr = buf + count;
3870 while (flushptr > buf && *(flushptr - 1) != '\n')
3874 SSize_t avail = b->bufsiz - (b->ptr - b->buf);
3875 if ((SSize_t) count < avail)
3877 if (flushptr > buf && flushptr <= buf + avail)
3878 avail = flushptr - buf;
3879 PerlIOBase(f)->flags |= PERLIO_F_WRBUF;
3881 Copy(buf, b->ptr, avail, STDCHAR);
3886 if (buf == flushptr)
3889 if (b->ptr >= (b->buf + b->bufsiz))
3892 if (PerlIOBase(f)->flags & PERLIO_F_UNBUF)
3898 PerlIOBuf_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
3901 if ((code = PerlIO_flush(f)) == 0) {
3902 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
3903 code = PerlIO_seek(PerlIONext(f), offset, whence);
3905 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3906 b->posn = PerlIO_tell(PerlIONext(f));
3913 PerlIOBuf_tell(pTHX_ PerlIO *f)
3915 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3917 * b->posn is file position where b->buf was read, or will be written
3919 Off_t posn = b->posn;
3920 if ((PerlIOBase(f)->flags & PERLIO_F_APPEND) &&
3921 (PerlIOBase(f)->flags & PERLIO_F_WRBUF)) {
3923 /* As O_APPEND files are normally shared in some sense it is better
3928 /* when file is NOT shared then this is sufficient */
3929 PerlIO_seek(PerlIONext(f),0, SEEK_END);
3931 posn = b->posn = PerlIO_tell(PerlIONext(f));
3935 * If buffer is valid adjust position by amount in buffer
3937 posn += (b->ptr - b->buf);
3943 PerlIOBuf_popped(pTHX_ PerlIO *f)
3945 const IV code = PerlIOBase_popped(aTHX_ f);
3946 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3947 if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
3950 b->ptr = b->end = b->buf = NULL;
3951 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
3956 PerlIOBuf_close(pTHX_ PerlIO *f)
3958 const IV code = PerlIOBase_close(aTHX_ f);
3959 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3960 if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
3963 b->ptr = b->end = b->buf = NULL;
3964 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
3969 PerlIOBuf_get_ptr(pTHX_ PerlIO *f)
3971 const PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3978 PerlIOBuf_get_cnt(pTHX_ PerlIO *f)
3980 const PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3983 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF)
3984 return (b->end - b->ptr);
3989 PerlIOBuf_get_base(pTHX_ PerlIO *f)
3991 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3992 PERL_UNUSED_CONTEXT;
3997 b->buf = Newxz(b->buf,b->bufsiz, STDCHAR);
3999 b->buf = (STDCHAR *) & b->oneword;
4000 b->bufsiz = sizeof(b->oneword);
4002 b->end = b->ptr = b->buf;
4008 PerlIOBuf_bufsiz(pTHX_ PerlIO *f)
4010 const PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
4013 return (b->end - b->buf);
4017 PerlIOBuf_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
4019 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
4023 if (PerlIO_get_cnt(f) != cnt || b->ptr < b->buf) {
4024 assert(PerlIO_get_cnt(f) == cnt);
4025 assert(b->ptr >= b->buf);
4027 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
4031 PerlIOBuf_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
4033 return PerlIOBase_dup(aTHX_ f, o, param, flags);
4038 PERLIO_FUNCS_DECL(PerlIO_perlio) = {
4039 sizeof(PerlIO_funcs),
4042 PERLIO_K_BUFFERED|PERLIO_K_RAW,
4046 PerlIOBase_binmode, /* binmode */
4060 PerlIOBase_clearerr,
4061 PerlIOBase_setlinebuf,
4066 PerlIOBuf_set_ptrcnt,
4069 /*--------------------------------------------------------------------------------------*/
4071 * Temp layer to hold unread chars when cannot do it any other way
4075 PerlIOPending_fill(pTHX_ PerlIO *f)
4078 * Should never happen
4085 PerlIOPending_close(pTHX_ PerlIO *f)
4088 * A tad tricky - flush pops us, then we close new top
4091 return PerlIO_close(f);
4095 PerlIOPending_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
4098 * A tad tricky - flush pops us, then we seek new top
4101 return PerlIO_seek(f, offset, whence);
4106 PerlIOPending_flush(pTHX_ PerlIO *f)
4108 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
4109 if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
4113 PerlIO_pop(aTHX_ f);
4118 PerlIOPending_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
4124 PerlIOBuf_set_ptrcnt(aTHX_ f, ptr, cnt);
4129 PerlIOPending_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
4131 const IV code = PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
4132 PerlIOl * const l = PerlIOBase(f);
4134 * Our PerlIO_fast_gets must match what we are pushed on, or sv_gets()
4135 * etc. get muddled when it changes mid-string when we auto-pop.
4137 l->flags = (l->flags & ~(PERLIO_F_FASTGETS | PERLIO_F_UTF8)) |
4138 (PerlIOBase(PerlIONext(f))->
4139 flags & (PERLIO_F_FASTGETS | PERLIO_F_UTF8));
4144 PerlIOPending_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
4146 SSize_t avail = PerlIO_get_cnt(f);
4148 if ((SSize_t)count < avail)
4151 got = PerlIOBuf_read(aTHX_ f, vbuf, avail);
4152 if (got >= 0 && got < (SSize_t)count) {
4153 const SSize_t more =
4154 PerlIO_read(f, ((STDCHAR *) vbuf) + got, count - got);
4155 if (more >= 0 || got == 0)
4161 PERLIO_FUNCS_DECL(PerlIO_pending) = {
4162 sizeof(PerlIO_funcs),
4165 PERLIO_K_BUFFERED|PERLIO_K_RAW, /* not sure about RAW here */
4166 PerlIOPending_pushed,
4169 PerlIOBase_binmode, /* binmode */
4178 PerlIOPending_close,
4179 PerlIOPending_flush,
4183 PerlIOBase_clearerr,
4184 PerlIOBase_setlinebuf,
4189 PerlIOPending_set_ptrcnt,
4194 /*--------------------------------------------------------------------------------------*/
4196 * crlf - translation On read translate CR,LF to "\n" we do this by
4197 * overriding ptr/cnt entries to hand back a line at a time and keeping a
4198 * record of which nl we "lied" about. On write translate "\n" to CR,LF
4200 * c->nl points on the first byte of CR LF pair when it is temporarily
4201 * replaced by LF, or to the last CR of the buffer. In the former case
4202 * the caller thinks that the buffer ends at c->nl + 1, in the latter
4203 * that it ends at c->nl; these two cases can be distinguished by
4204 * *c->nl. c->nl is set during _getcnt() call, and unset during
4205 * _unread() and _flush() calls.
4206 * It only matters for read operations.
4210 PerlIOBuf base; /* PerlIOBuf stuff */
4211 STDCHAR *nl; /* Position of crlf we "lied" about in the
4216 PerlIOCrlf_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
4219 PerlIOBase(f)->flags |= PERLIO_F_CRLF;
4220 code = PerlIOBuf_pushed(aTHX_ f, mode, arg, tab);
4222 PerlIO_debug("PerlIOCrlf_pushed f=%p %s %s fl=%08" UVxf "\n",
4223 f, PerlIOBase(f)->tab->name, (mode) ? mode : "(Null)",
4224 PerlIOBase(f)->flags);
4227 /* Enable the first CRLF capable layer you can find, but if none
4228 * found, the one we just pushed is fine. This results in at
4229 * any given moment at most one CRLF-capable layer being enabled
4230 * in the whole layer stack. */
4231 PerlIO *g = PerlIONext(f);
4233 PerlIOl *b = PerlIOBase(g);
4234 if (b && b->tab == &PerlIO_crlf) {
4235 if (!(b->flags & PERLIO_F_CRLF))
4236 b->flags |= PERLIO_F_CRLF;
4237 PerlIO_pop(aTHX_ f);
4248 PerlIOCrlf_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
4250 PerlIOCrlf * const c = PerlIOSelf(f, PerlIOCrlf);
4251 if (c->nl) { /* XXXX Shouldn't it be done only if b->ptr > c->nl? */
4255 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF))
4256 return PerlIOBuf_unread(aTHX_ f, vbuf, count);
4258 const STDCHAR *buf = (const STDCHAR *) vbuf + count;
4259 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
4261 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
4266 if (!(PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
4267 b->end = b->ptr = b->buf + b->bufsiz;
4268 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
4269 b->posn -= b->bufsiz;
4271 while (count > 0 && b->ptr > b->buf) {
4272 const int ch = *--buf;
4274 if (b->ptr - 2 >= b->buf) {
4281 /* If b->ptr - 1 == b->buf, we are undoing reading 0xa */
4282 *--(b->ptr) = 0xa; /* Works even if 0xa == '\r' */
4298 /* XXXX This code assumes that buffer size >=2, but does not check it... */
4300 PerlIOCrlf_get_cnt(pTHX_ PerlIO *f)
4302 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
4305 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
4306 PerlIOCrlf * const c = PerlIOSelf(f, PerlIOCrlf);
4307 if ((PerlIOBase(f)->flags & PERLIO_F_CRLF) && (!c->nl || *c->nl == 0xd)) {
4308 STDCHAR *nl = (c->nl) ? c->nl : b->ptr;
4310 while (nl < b->end && *nl != 0xd)
4312 if (nl < b->end && *nl == 0xd) {
4314 if (nl + 1 < b->end) {
4321 * Not CR,LF but just CR
4329 * Blast - found CR as last char in buffer
4334 * They may not care, defer work as long as
4338 return (nl - b->ptr);
4342 b->ptr++; /* say we have read it as far as
4343 * flush() is concerned */
4344 b->buf++; /* Leave space in front of buffer */
4345 /* Note as we have moved buf up flush's
4347 will naturally make posn point at CR
4349 b->bufsiz--; /* Buffer is thus smaller */
4350 code = PerlIO_fill(f); /* Fetch some more */
4351 b->bufsiz++; /* Restore size for next time */
4352 b->buf--; /* Point at space */
4353 b->ptr = nl = b->buf; /* Which is what we hand
4355 *nl = 0xd; /* Fill in the CR */
4357 goto test; /* fill() call worked */
4359 * CR at EOF - just fall through
4361 /* Should we clear EOF though ??? */
4366 return (((c->nl) ? (c->nl + 1) : b->end) - b->ptr);
4372 PerlIOCrlf_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
4374 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
4375 PerlIOCrlf * const c = PerlIOSelf(f, PerlIOCrlf);
4381 if (ptr == b->end && *c->nl == 0xd) {
4382 /* Defered CR at end of buffer case - we lied about count */
4395 * Test code - delete when it works ...
4397 IV flags = PerlIOBase(f)->flags;
4398 STDCHAR *chk = (c->nl) ? (c->nl+1) : b->end;
4399 if (ptr+cnt == c->nl && c->nl+1 == b->end && *c->nl == 0xd) {
4400 /* Defered CR at end of buffer case - we lied about count */
4406 Perl_croak(aTHX_ "ptr wrong %p != %p fl=%08" UVxf
4407 " nl=%p e=%p for %d", ptr, chk, flags, c->nl,
4415 * They have taken what we lied about
4423 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
4427 PerlIOCrlf_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
4429 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF))
4430 return PerlIOBuf_write(aTHX_ f, vbuf, count);
4432 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
4433 const STDCHAR *buf = (const STDCHAR *) vbuf;
4434 const STDCHAR * const ebuf = buf + count;
4437 if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
4439 while (buf < ebuf) {
4440 const STDCHAR * const eptr = b->buf + b->bufsiz;
4441 PerlIOBase(f)->flags |= PERLIO_F_WRBUF;
4442 while (buf < ebuf && b->ptr < eptr) {
4444 if ((b->ptr + 2) > eptr) {
4452 *(b->ptr)++ = 0xd; /* CR */
4453 *(b->ptr)++ = 0xa; /* LF */
4455 if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF) {
4462 *(b->ptr)++ = *buf++;
4464 if (b->ptr >= eptr) {
4470 if (PerlIOBase(f)->flags & PERLIO_F_UNBUF)
4472 return (buf - (STDCHAR *) vbuf);
4477 PerlIOCrlf_flush(pTHX_ PerlIO *f)
4479 PerlIOCrlf * const c = PerlIOSelf(f, PerlIOCrlf);
4484 return PerlIOBuf_flush(aTHX_ f);
4488 PerlIOCrlf_binmode(pTHX_ PerlIO *f)
4490 if ((PerlIOBase(f)->flags & PERLIO_F_CRLF)) {
4491 /* In text mode - flush any pending stuff and flip it */
4492 PerlIOBase(f)->flags &= ~PERLIO_F_CRLF;
4493 #ifndef PERLIO_USING_CRLF
4494 /* CRLF is unusual case - if this is just the :crlf layer pop it */
4495 if (PerlIOBase(f)->tab == &PerlIO_crlf) {
4496 PerlIO_pop(aTHX_ f);
4503 PERLIO_FUNCS_DECL(PerlIO_crlf) = {
4504 sizeof(PerlIO_funcs),
4507 PERLIO_K_BUFFERED | PERLIO_K_CANCRLF | PERLIO_K_RAW,
4509 PerlIOBuf_popped, /* popped */
4511 PerlIOCrlf_binmode, /* binmode */
4515 PerlIOBuf_read, /* generic read works with ptr/cnt lies */
4516 PerlIOCrlf_unread, /* Put CR,LF in buffer for each '\n' */
4517 PerlIOCrlf_write, /* Put CR,LF in buffer for each '\n' */
4525 PerlIOBase_clearerr,
4526 PerlIOBase_setlinebuf,
4531 PerlIOCrlf_set_ptrcnt,
4535 /*--------------------------------------------------------------------------------------*/
4537 * mmap as "buffer" layer
4541 PerlIOBuf base; /* PerlIOBuf stuff */
4542 Mmap_t mptr; /* Mapped address */
4543 Size_t len; /* mapped length */
4544 STDCHAR *bbuf; /* malloced buffer if map fails */
4548 PerlIOMmap_map(pTHX_ PerlIO *f)
4551 PerlIOMmap * const m = PerlIOSelf(f, PerlIOMmap);
4552 const IV flags = PerlIOBase(f)->flags;
4556 if (flags & PERLIO_F_CANREAD) {
4557 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
4558 const int fd = PerlIO_fileno(f);
4560 code = Fstat(fd, &st);
4561 if (code == 0 && S_ISREG(st.st_mode)) {
4562 SSize_t len = st.st_size - b->posn;
4565 if (PL_mmap_page_size <= 0)
4566 Perl_croak(aTHX_ "panic: bad pagesize %" IVdf,
4570 * This is a hack - should never happen - open should
4573 b->posn = PerlIO_tell(PerlIONext(f));
4575 posn = (b->posn / PL_mmap_page_size) * PL_mmap_page_size;
4576 len = st.st_size - posn;
4577 m->mptr = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, posn);
4578 if (m->mptr && m->mptr != (Mmap_t) - 1) {
4579 #if 0 && defined(HAS_MADVISE) && defined(MADV_SEQUENTIAL)
4580 madvise(m->mptr, len, MADV_SEQUENTIAL);
4582 #if 0 && defined(HAS_MADVISE) && defined(MADV_WILLNEED)
4583 madvise(m->mptr, len, MADV_WILLNEED);
4585 PerlIOBase(f)->flags =
4586 (flags & ~PERLIO_F_EOF) | PERLIO_F_RDBUF;
4587 b->end = ((STDCHAR *) m->mptr) + len;
4588 b->buf = ((STDCHAR *) m->mptr) + (b->posn - posn);
4597 PerlIOBase(f)->flags =
4598 flags | PERLIO_F_EOF | PERLIO_F_RDBUF;
4600 b->ptr = b->end = b->ptr;
4609 PerlIOMmap_unmap(pTHX_ PerlIO *f)
4611 PerlIOMmap * const m = PerlIOSelf(f, PerlIOMmap);
4614 PerlIOBuf * const b = &m->base;
4616 code = munmap(m->mptr, m->len);
4620 if (PerlIO_seek(PerlIONext(f), b->posn, SEEK_SET) != 0)
4623 b->ptr = b->end = b->buf;
4624 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
4630 PerlIOMmap_get_base(pTHX_ PerlIO *f)
4632 PerlIOMmap * const m = PerlIOSelf(f, PerlIOMmap);
4633 PerlIOBuf * const b = &m->base;
4634 if (b->buf && (PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
4636 * Already have a readbuffer in progress
4642 * We have a write buffer or flushed PerlIOBuf read buffer
4644 m->bbuf = b->buf; /* save it in case we need it again */
4645 b->buf = NULL; /* Clear to trigger below */
4648 PerlIOMmap_map(aTHX_ f); /* Try and map it */
4651 * Map did not work - recover PerlIOBuf buffer if we have one
4656 b->ptr = b->end = b->buf;
4659 return PerlIOBuf_get_base(aTHX_ f);
4663 PerlIOMmap_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
4665 PerlIOMmap * const m = PerlIOSelf(f, PerlIOMmap);
4666 PerlIOBuf * const b = &m->base;
4667 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
4669 if (b->ptr && (b->ptr - count) >= b->buf
4670 && memEQ(b->ptr - count, vbuf, count)) {
4672 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
4677 * Loose the unwritable mapped buffer
4681 * If flush took the "buffer" see if we have one from before
4683 if (!b->buf && m->bbuf)
4686 PerlIOBuf_get_base(aTHX_ f);
4690 return PerlIOBuf_unread(aTHX_ f, vbuf, count);
4694 PerlIOMmap_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
4696 PerlIOMmap * const m = PerlIOSelf(f, PerlIOMmap);
4697 PerlIOBuf * const b = &m->base;
4699 if (!b->buf || !(PerlIOBase(f)->flags & PERLIO_F_WRBUF)) {
4701 * No, or wrong sort of, buffer
4704 if (PerlIOMmap_unmap(aTHX_ f) != 0)
4708 * If unmap took the "buffer" see if we have one from before
4710 if (!b->buf && m->bbuf)
4713 PerlIOBuf_get_base(aTHX_ f);
4717 return PerlIOBuf_write(aTHX_ f, vbuf, count);
4721 PerlIOMmap_flush(pTHX_ PerlIO *f)
4723 PerlIOMmap * const m = PerlIOSelf(f, PerlIOMmap);
4724 PerlIOBuf * const b = &m->base;
4725 IV code = PerlIOBuf_flush(aTHX_ f);
4727 * Now we are "synced" at PerlIOBuf level
4734 if (PerlIOMmap_unmap(aTHX_ f) != 0)
4739 * We seem to have a PerlIOBuf buffer which was not mapped
4740 * remember it in case we need one later
4749 PerlIOMmap_fill(pTHX_ PerlIO *f)
4751 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
4752 IV code = PerlIO_flush(f);
4753 if (code == 0 && !b->buf) {
4754 code = PerlIOMmap_map(aTHX_ f);
4756 if (code == 0 && !(PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
4757 code = PerlIOBuf_fill(aTHX_ f);
4763 PerlIOMmap_close(pTHX_ PerlIO *f)
4765 PerlIOMmap * const m = PerlIOSelf(f, PerlIOMmap);
4766 PerlIOBuf * const b = &m->base;
4767 IV code = PerlIO_flush(f);
4771 b->ptr = b->end = b->buf;
4773 if (PerlIOBuf_close(aTHX_ f) != 0)
4779 PerlIOMmap_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
4781 return PerlIOBase_dup(aTHX_ f, o, param, flags);
4785 PERLIO_FUNCS_DECL(PerlIO_mmap) = {
4786 sizeof(PerlIO_funcs),
4789 PERLIO_K_BUFFERED|PERLIO_K_RAW,
4793 PerlIOBase_binmode, /* binmode */
4807 PerlIOBase_clearerr,
4808 PerlIOBase_setlinebuf,
4809 PerlIOMmap_get_base,
4813 PerlIOBuf_set_ptrcnt,
4816 #endif /* HAS_MMAP */
4819 Perl_PerlIO_stdin(pTHX)
4823 PerlIO_stdstreams(aTHX);
4825 return &PL_perlio[1];
4829 Perl_PerlIO_stdout(pTHX)
4833 PerlIO_stdstreams(aTHX);
4835 return &PL_perlio[2];
4839 Perl_PerlIO_stderr(pTHX)
4843 PerlIO_stdstreams(aTHX);
4845 return &PL_perlio[3];
4848 /*--------------------------------------------------------------------------------------*/
4851 PerlIO_getname(PerlIO *f, char *buf)
4856 bool exported = FALSE;
4857 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
4859 stdio = PerlIO_exportFILE(f,0);
4863 name = fgetname(stdio, buf);
4864 if (exported) PerlIO_releaseFILE(f,stdio);
4869 PERL_UNUSED_ARG(buf);
4870 Perl_croak(aTHX_ "Don't know how to get file name");
4876 /*--------------------------------------------------------------------------------------*/
4878 * Functions which can be called on any kind of PerlIO implemented in
4882 #undef PerlIO_fdopen
4884 PerlIO_fdopen(int fd, const char *mode)
4887 return PerlIO_openn(aTHX_ NULL, mode, fd, 0, 0, NULL, 0, NULL);
4892 PerlIO_open(const char *path, const char *mode)
4895 SV *name = sv_2mortal(newSVpv(path, 0));
4896 return PerlIO_openn(aTHX_ NULL, mode, -1, 0, 0, NULL, 1, &name);
4899 #undef Perlio_reopen
4901 PerlIO_reopen(const char *path, const char *mode, PerlIO *f)
4904 SV *name = sv_2mortal(newSVpv(path,0));
4905 return PerlIO_openn(aTHX_ NULL, mode, -1, 0, 0, f, 1, &name);
4910 PerlIO_getc(PerlIO *f)
4914 if ( 1 == PerlIO_read(f, buf, 1) ) {
4915 return (unsigned char) buf[0];
4920 #undef PerlIO_ungetc
4922 PerlIO_ungetc(PerlIO *f, int ch)
4927 if (PerlIO_unread(f, &buf, 1) == 1)
4935 PerlIO_putc(PerlIO *f, int ch)
4939 return PerlIO_write(f, &buf, 1);
4944 PerlIO_puts(PerlIO *f, const char *s)
4947 return PerlIO_write(f, s, strlen(s));
4950 #undef PerlIO_rewind
4952 PerlIO_rewind(PerlIO *f)
4955 PerlIO_seek(f, (Off_t) 0, SEEK_SET);
4959 #undef PerlIO_vprintf
4961 PerlIO_vprintf(PerlIO *f, const char *fmt, va_list ap)
4964 SV * const sv = newSVpvs("");
4970 Perl_va_copy(ap, apc);
4971 sv_vcatpvf(sv, fmt, &apc);
4973 sv_vcatpvf(sv, fmt, &ap);
4975 s = SvPV_const(sv, len);
4976 wrote = PerlIO_write(f, s, len);
4981 #undef PerlIO_printf
4983 PerlIO_printf(PerlIO *f, const char *fmt, ...)
4988 result = PerlIO_vprintf(f, fmt, ap);
4993 #undef PerlIO_stdoutf
4995 PerlIO_stdoutf(const char *fmt, ...)
5001 result = PerlIO_vprintf(PerlIO_stdout(), fmt, ap);
5006 #undef PerlIO_tmpfile
5008 PerlIO_tmpfile(void)
5013 const int fd = win32_tmpfd();
5015 f = PerlIO_fdopen(fd, "w+b");
5017 # if defined(HAS_MKSTEMP) && ! defined(VMS) && ! defined(OS2)
5018 SV * const sv = newSVpvs("/tmp/PerlIO_XXXXXX");
5020 * I have no idea how portable mkstemp() is ... NI-S
5022 const int fd = mkstemp(SvPVX(sv));
5024 f = PerlIO_fdopen(fd, "w+");
5026 PerlIOBase(f)->flags |= PERLIO_F_TEMP;
5027 PerlLIO_unlink(SvPVX_const(sv));
5030 # else /* !HAS_MKSTEMP, fallback to stdio tmpfile(). */
5031 FILE * const stdio = PerlSIO_tmpfile();
5034 if ((f = PerlIO_push(aTHX_(PerlIO_allocate(aTHX)),
5035 PERLIO_FUNCS_CAST(&PerlIO_stdio),
5037 PerlIOStdio * const s = PerlIOSelf(f, PerlIOStdio);
5043 # endif /* else HAS_MKSTEMP */
5044 #endif /* else WIN32 */
5051 #endif /* USE_SFIO */
5052 #endif /* PERLIO_IS_STDIO */
5054 /*======================================================================================*/
5056 * Now some functions in terms of above which may be needed even if we are
5057 * not in true PerlIO mode
5061 #undef PerlIO_setpos
5063 PerlIO_setpos(PerlIO *f, SV *pos)
5068 const Off_t * const posn = (Off_t *) SvPV(pos, len);
5069 if (f && len == sizeof(Off_t))
5070 return PerlIO_seek(f, *posn, SEEK_SET);
5072 SETERRNO(EINVAL, SS_IVCHAN);
5076 #undef PerlIO_setpos
5078 PerlIO_setpos(PerlIO *f, SV *pos)
5083 Fpos_t * const fpos = (Fpos_t *) SvPV(pos, len);
5084 if (f && len == sizeof(Fpos_t)) {
5085 #if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64)
5086 return fsetpos64(f, fpos);
5088 return fsetpos(f, fpos);
5092 SETERRNO(EINVAL, SS_IVCHAN);
5098 #undef PerlIO_getpos
5100 PerlIO_getpos(PerlIO *f, SV *pos)
5103 Off_t posn = PerlIO_tell(f);
5104 sv_setpvn(pos, (char *) &posn, sizeof(posn));
5105 return (posn == (Off_t) - 1) ? -1 : 0;
5108 #undef PerlIO_getpos
5110 PerlIO_getpos(PerlIO *f, SV *pos)
5115 #if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64)
5116 code = fgetpos64(f, &fpos);
5118 code = fgetpos(f, &fpos);
5120 sv_setpvn(pos, (char *) &fpos, sizeof(fpos));
5125 #if (defined(PERLIO_IS_STDIO) || !defined(USE_SFIO)) && !defined(HAS_VPRINTF)
5128 vprintf(char *pat, char *args)
5130 _doprnt(pat, args, stdout);
5131 return 0; /* wrong, but perl doesn't use the return
5136 vfprintf(FILE *fd, char *pat, char *args)
5138 _doprnt(pat, args, fd);
5139 return 0; /* wrong, but perl doesn't use the return
5145 #ifndef PerlIO_vsprintf
5147 PerlIO_vsprintf(char *s, int n, const char *fmt, va_list ap)
5150 #ifdef USE_VSNPRINTF
5151 const int val = vsnprintf(s, n > 0 ? n : 0, fmt, ap);
5153 const int val = vsprintf(s, fmt, ap);
5154 #endif /* #ifdef USE_VSNPRINTF */
5156 if (strlen(s) >= (STRLEN) n) {
5158 (void) PerlIO_puts(Perl_error_log,
5159 "panic: sprintf overflow - memory corrupted!\n");
5167 #ifndef PerlIO_sprintf
5169 PerlIO_sprintf(char *s, int n, const char *fmt, ...)
5174 result = PerlIO_vsprintf(s, n, fmt, ap);
5182 * c-indentation-style: bsd
5184 * indent-tabs-mode: t
5187 * ex: set ts=8 sts=4 sw=4 noet: