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
143 * the translation mode of the fd in the Borland RTL (heavy
144 * digging through their runtime sources reveal). User has to
146 * the mode explicitly for the stream (though they don't
148 * this anywhere). GSAR 97-5-24
154 fp->flags &= ~_F_BIN;
162 # if defined(USEMYBINMODE)
164 if (my_binmode(fp, iotype, mode) != FALSE)
170 PERL_UNUSED_ARG(iotype);
171 PERL_UNUSED_ARG(mode);
179 #define O_ACCMODE 3 /* Assume traditional implementation */
183 PerlIO_intmode2str(int rawmode, char *mode, int *writing)
185 const int result = rawmode & O_ACCMODE;
190 ptype = IoTYPE_RDONLY;
193 ptype = IoTYPE_WRONLY;
201 *writing = (result != O_RDONLY);
203 if (result == O_RDONLY) {
207 else if (rawmode & O_APPEND) {
209 if (result != O_WRONLY)
214 if (result == O_WRONLY)
221 if (rawmode & O_BINARY)
227 #ifndef PERLIO_LAYERS
229 PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names)
231 if (!names || !*names
232 || strEQ(names, ":crlf")
233 || strEQ(names, ":raw")
234 || strEQ(names, ":bytes")
238 Perl_croak(aTHX_ "Cannot apply \"%s\" in non-PerlIO perl", names);
246 PerlIO_destruct(pTHX)
251 PerlIO_binmode(pTHX_ PerlIO *fp, int iotype, int mode, const char *names)
254 PERL_UNUSED_ARG(iotype);
255 PERL_UNUSED_ARG(mode);
256 PERL_UNUSED_ARG(names);
259 return perlsio_binmode(fp, iotype, mode);
264 PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
266 #if defined(PERL_MICRO) || defined(__SYMBIAN32__)
269 #ifdef PERL_IMPLICIT_SYS
270 return PerlSIO_fdupopen(f);
273 return win32_fdupopen(f);
276 const int fd = PerlLIO_dup(PerlIO_fileno(f));
280 const int omode = djgpp_get_stream_mode(f);
282 const int omode = fcntl(fd, F_GETFL);
284 PerlIO_intmode2str(omode,mode,NULL);
285 /* the r+ is a hack */
286 return PerlIO_fdopen(fd, mode);
291 SETERRNO(EBADF, SS_IVCHAN);
301 * De-mux PerlIO_openn() into fdopen, freopen and fopen type entries
305 PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd,
306 int imode, int perm, PerlIO *old, int narg, SV **args)
310 Perl_croak(aTHX_ "More than one argument to open");
312 if (*args == &PL_sv_undef)
313 return PerlIO_tmpfile();
315 const char *name = SvPV_nolen_const(*args);
316 if (*mode == IoTYPE_NUMERIC) {
317 fd = PerlLIO_open3(name, imode, perm);
319 return PerlIO_fdopen(fd, mode + 1);
322 return PerlIO_reopen(name, mode, old);
325 return PerlIO_open(name, mode);
330 return PerlIO_fdopen(fd, (char *) mode);
335 XS(XS_PerlIO__Layer__find)
339 Perl_croak(aTHX_ "Usage class->find(name[,load])");
341 const char * const name = SvPV_nolen_const(ST(1));
342 ST(0) = (strEQ(name, "crlf")
343 || strEQ(name, "raw")) ? &PL_sv_yes : &PL_sv_undef;
350 Perl_boot_core_PerlIO(pTHX)
352 newXS("PerlIO::Layer::find", XS_PerlIO__Layer__find, __FILE__);
358 #ifdef PERLIO_IS_STDIO
364 * Does nothing (yet) except force this file to be included in perl
365 * binary. That allows this file to force inclusion of other functions
366 * that may be required by loadable extensions e.g. for
367 * FileHandle::tmpfile
371 #undef PerlIO_tmpfile
378 #else /* PERLIO_IS_STDIO */
386 * This section is just to make sure these functions get pulled in from
390 #undef PerlIO_tmpfile
401 * Force this file to be included in perl binary. Which allows this
402 * file to force inclusion of other functions that may be required by
403 * loadable extensions e.g. for FileHandle::tmpfile
407 * Hack sfio does its own 'autoflush' on stdout in common cases. Flush
408 * results in a lot of lseek()s to regular files and lot of small
411 sfset(sfstdout, SF_SHARE, 0);
414 /* This is not the reverse of PerlIO_exportFILE(), PerlIO_releaseFILE() is. */
416 PerlIO_importFILE(FILE *stdio, const char *mode)
418 const int fd = fileno(stdio);
419 if (!mode || !*mode) {
422 return PerlIO_fdopen(fd, mode);
426 PerlIO_findFILE(PerlIO *pio)
428 const int fd = PerlIO_fileno(pio);
429 FILE * const f = fdopen(fd, "r+");
431 if (!f && errno == EINVAL)
433 if (!f && errno == EINVAL)
440 /*======================================================================================*/
442 * Implement all the PerlIO interface ourselves.
448 * We _MUST_ have <unistd.h> if we are using lseek() and may have large
455 #include <sys/mman.h>
459 PerlIO_debug(const char *fmt, ...)
464 if (!PL_perlio_debug_fd && !PL_tainting && PL_uid == PL_euid && PL_gid == PL_egid) {
465 const char * const s = PerlEnv_getenv("PERLIO_DEBUG");
467 PL_perlio_debug_fd = PerlLIO_open3(s, O_WRONLY | O_CREAT | O_APPEND, 0666);
469 PL_perlio_debug_fd = -1;
471 if (PL_perlio_debug_fd > 0) {
474 const char * const s = CopFILE(PL_curcop);
475 /* Use fixed buffer as sv_catpvf etc. needs SVs */
477 const STRLEN len = my_sprintf(buffer, "%.40s:%" IVdf " ", s ? s : "(none)", (IV) CopLINE(PL_curcop));
478 const STRLEN len2 = vsprintf(buffer+len, fmt, ap);
479 PerlLIO_write(PL_perlio_debug_fd, buffer, len + len2);
481 const char *s = CopFILE(PL_curcop);
483 SV * const sv = newSVpvs("");
484 Perl_sv_catpvf(aTHX_ sv, "%s:%" IVdf " ", s ? s : "(none)",
485 (IV) CopLINE(PL_curcop));
486 Perl_sv_vcatpvf(aTHX_ sv, fmt, &ap);
488 s = SvPV_const(sv, len);
489 PerlLIO_write(PL_perlio_debug_fd, s, len);
496 /*--------------------------------------------------------------------------------------*/
499 * Inner level routines
503 * Table of pointers to the PerlIO structs (malloc'ed)
505 #define PERLIO_TABLE_SIZE 64
508 PerlIO_allocate(pTHX)
512 * Find a free slot in the table, allocating new table as necessary
517 while ((f = *last)) {
519 last = (PerlIO **) (f);
520 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
526 Newxz(f,PERLIO_TABLE_SIZE,PerlIO);
534 #undef PerlIO_fdupopen
536 PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
538 if (PerlIOValid(f)) {
539 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
540 PerlIO_debug("fdupopen f=%p param=%p\n",(void*)f,(void*)param);
542 return (*tab->Dup)(aTHX_ PerlIO_allocate(aTHX), f, param, flags);
544 return PerlIOBase_dup(aTHX_ PerlIO_allocate(aTHX), f, param, flags);
548 SETERRNO(EBADF, SS_IVCHAN);
554 PerlIO_cleantable(pTHX_ PerlIO **tablep)
556 PerlIO * const table = *tablep;
559 PerlIO_cleantable(aTHX_(PerlIO **) & (table[0]));
560 for (i = PERLIO_TABLE_SIZE - 1; i > 0; i--) {
561 PerlIO * const f = table + i;
573 PerlIO_list_alloc(pTHX)
576 Newxz(list, 1, PerlIO_list_t);
582 PerlIO_list_free(pTHX_ PerlIO_list_t *list)
585 if (--list->refcnt == 0) {
588 for (i = 0; i < list->cur; i++) {
589 if (list->array[i].arg)
590 SvREFCNT_dec(list->array[i].arg);
592 Safefree(list->array);
600 PerlIO_list_push(pTHX_ PerlIO_list_t *list, PerlIO_funcs *funcs, SV *arg)
604 if (list->cur >= list->len) {
607 Renew(list->array, list->len, PerlIO_pair_t);
609 Newx(list->array, list->len, PerlIO_pair_t);
611 p = &(list->array[list->cur++]);
613 if ((p->arg = arg)) {
614 (void)SvREFCNT_inc(arg);
619 PerlIO_clone_list(pTHX_ PerlIO_list_t *proto, CLONE_PARAMS *param)
621 PerlIO_list_t *list = (PerlIO_list_t *) NULL;
624 list = PerlIO_list_alloc(aTHX);
625 for (i=0; i < proto->cur; i++) {
627 if (proto->array[i].arg)
628 arg = PerlIO_sv_dup(aTHX_ proto->array[i].arg,param);
629 PerlIO_list_push(aTHX_ list, proto->array[i].funcs, arg);
636 PerlIO_clone(pTHX_ PerlInterpreter *proto, CLONE_PARAMS *param)
639 PerlIO **table = &proto->Iperlio;
642 PL_known_layers = PerlIO_clone_list(aTHX_ proto->Iknown_layers, param);
643 PL_def_layerlist = PerlIO_clone_list(aTHX_ proto->Idef_layerlist, param);
644 PerlIO_allocate(aTHX); /* root slot is never used */
645 PerlIO_debug("Clone %p from %p\n",aTHX,proto);
646 while ((f = *table)) {
648 table = (PerlIO **) (f++);
649 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
651 (void) fp_dup(f, 0, param);
657 PERL_UNUSED_ARG(proto);
658 PERL_UNUSED_ARG(param);
663 PerlIO_destruct(pTHX)
666 PerlIO **table = &PL_perlio;
669 PerlIO_debug("Destruct %p\n",aTHX);
671 while ((f = *table)) {
673 table = (PerlIO **) (f++);
674 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
678 if (l->tab->kind & PERLIO_K_DESTRUCT) {
679 PerlIO_debug("Destruct popping %s\n", l->tab->name);
693 PerlIO_pop(pTHX_ PerlIO *f)
695 const PerlIOl *l = *f;
697 PerlIO_debug("PerlIO_pop f=%p %s\n", (void*)f, l->tab->name);
698 if (l->tab->Popped) {
700 * If popped returns non-zero do not free its layer structure
701 * it has either done so itself, or it is shared and still in
704 if ((*l->tab->Popped) (aTHX_ f) != 0)
712 /* Return as an array the stack of layers on a filehandle. Note that
713 * the stack is returned top-first in the array, and there are three
714 * times as many array elements as there are layers in the stack: the
715 * first element of a layer triplet is the name, the second one is the
716 * arguments, and the third one is the flags. */
719 PerlIO_get_layers(pTHX_ PerlIO *f)
722 AV * const av = newAV();
724 if (PerlIOValid(f)) {
725 PerlIOl *l = PerlIOBase(f);
728 SV * const name = l->tab && l->tab->name ?
729 newSVpv(l->tab->name, 0) : &PL_sv_undef;
730 SV * const arg = l->tab && l->tab->Getarg ?
731 (*l->tab->Getarg)(aTHX_ &l, 0, 0) : &PL_sv_undef;
734 av_push(av, newSViv((IV)l->flags));
742 /*--------------------------------------------------------------------------------------*/
744 * XS Interface for perl code
748 PerlIO_find_layer(pTHX_ const char *name, STRLEN len, int load)
752 if ((SSize_t) len <= 0)
754 for (i = 0; i < PL_known_layers->cur; i++) {
755 PerlIO_funcs * const f = PL_known_layers->array[i].funcs;
756 if (memEQ(f->name, name, len) && f->name[len] == 0) {
757 PerlIO_debug("%.*s => %p\n", (int) len, name, (void*)f);
761 if (load && PL_subname && PL_def_layerlist
762 && PL_def_layerlist->cur >= 2) {
763 if (PL_in_load_module) {
764 Perl_croak(aTHX_ "Recursive call to Perl_load_module in PerlIO_find_layer");
767 SV * const pkgsv = newSVpvs("PerlIO");
768 SV * const layer = newSVpvn(name, len);
769 CV * const cv = get_cv("PerlIO::Layer::NoWarnings", FALSE);
771 SAVEINT(PL_in_load_module);
773 SAVEGENERICSV(PL_warnhook);
774 (void)SvREFCNT_inc(cv);
775 PL_warnhook = (SV *) cv;
779 * The two SVs are magically freed by load_module
781 Perl_load_module(aTHX_ 0, pkgsv, Nullsv, layer, Nullsv);
784 return PerlIO_find_layer(aTHX_ name, len, 0);
787 PerlIO_debug("Cannot find %.*s\n", (int) len, name);
791 #ifdef USE_ATTRIBUTES_FOR_PERLIO
794 perlio_mg_set(pTHX_ SV *sv, MAGIC *mg)
797 IO * const io = GvIOn((GV *) SvRV(sv));
798 PerlIO * const ifp = IoIFP(io);
799 PerlIO * const ofp = IoOFP(io);
800 Perl_warn(aTHX_ "set %" SVf " %p %p %p", sv, io, ifp, ofp);
806 perlio_mg_get(pTHX_ SV *sv, MAGIC *mg)
809 IO * const io = GvIOn((GV *) SvRV(sv));
810 PerlIO * const ifp = IoIFP(io);
811 PerlIO * const ofp = IoOFP(io);
812 Perl_warn(aTHX_ "get %" SVf " %p %p %p", sv, io, ifp, ofp);
818 perlio_mg_clear(pTHX_ SV *sv, MAGIC *mg)
820 Perl_warn(aTHX_ "clear %" SVf, sv);
825 perlio_mg_free(pTHX_ SV *sv, MAGIC *mg)
827 Perl_warn(aTHX_ "free %" SVf, sv);
831 MGVTBL perlio_vtab = {
839 XS(XS_io_MODIFY_SCALAR_ATTRIBUTES)
842 SV * const sv = SvRV(ST(1));
843 AV * const av = newAV();
847 sv_magic(sv, (SV *) av, PERL_MAGIC_ext, NULL, 0);
849 mg = mg_find(sv, PERL_MAGIC_ext);
850 mg->mg_virtual = &perlio_vtab;
852 Perl_warn(aTHX_ "attrib %" SVf, sv);
853 for (i = 2; i < items; i++) {
855 const char * const name = SvPV_const(ST(i), len);
856 SV * const layer = PerlIO_find_layer(aTHX_ name, len, 1);
858 av_push(av, SvREFCNT_inc(layer));
869 #endif /* USE_ATTIBUTES_FOR_PERLIO */
872 PerlIO_tab_sv(pTHX_ PerlIO_funcs *tab)
874 HV * const stash = gv_stashpvs("PerlIO::Layer", TRUE);
875 SV * const sv = sv_bless(newRV_noinc(newSViv(PTR2IV(tab))), stash);
879 XS(XS_PerlIO__Layer__NoWarnings)
881 /* This is used as a %SIG{__WARN__} handler to supress warnings
882 during loading of layers.
887 PerlIO_debug("warning:%s\n",SvPV_nolen_const(ST(0)));
891 XS(XS_PerlIO__Layer__find)
896 Perl_croak(aTHX_ "Usage class->find(name[,load])");
899 const char * const name = SvPV_const(ST(1), len);
900 const bool load = (items > 2) ? SvTRUE(ST(2)) : 0;
901 PerlIO_funcs * const layer = PerlIO_find_layer(aTHX_ name, len, load);
903 (layer) ? sv_2mortal(PerlIO_tab_sv(aTHX_ layer)) :
910 PerlIO_define_layer(pTHX_ PerlIO_funcs *tab)
913 if (!PL_known_layers)
914 PL_known_layers = PerlIO_list_alloc(aTHX);
915 PerlIO_list_push(aTHX_ PL_known_layers, tab, Nullsv);
916 PerlIO_debug("define %s %p\n", tab->name, (void*)tab);
920 PerlIO_parse_layers(pTHX_ PerlIO_list_t *av, const char *names)
924 const char *s = names;
926 while (isSPACE(*s) || *s == ':')
931 const char *as = Nullch;
933 if (!isIDFIRST(*s)) {
935 * Message is consistent with how attribute lists are
936 * passed. Even though this means "foo : : bar" is
937 * seen as an invalid separator character.
939 const char q = ((*s == '\'') ? '"' : '\'');
940 if (ckWARN(WARN_LAYER))
941 Perl_warner(aTHX_ packWARN(WARN_LAYER),
942 "Invalid separator character %c%c%c in PerlIO layer specification %s",
944 SETERRNO(EINVAL, LIB_INVARG);
949 } while (isALNUM(*e));
965 * It's a nul terminated string, not allowed
966 * to \ the terminating null. Anything other
967 * character is passed over.
977 if (ckWARN(WARN_LAYER))
978 Perl_warner(aTHX_ packWARN(WARN_LAYER),
979 "Argument list not closed for PerlIO layer \"%.*s\"",
991 PerlIO_funcs * const layer =
992 PerlIO_find_layer(aTHX_ s, llen, 1);
994 PerlIO_list_push(aTHX_ av, layer,
1000 if (ckWARN(WARN_LAYER))
1001 Perl_warner(aTHX_ packWARN(WARN_LAYER), "Unknown PerlIO layer \"%.*s\"",
1014 PerlIO_default_buffer(pTHX_ PerlIO_list_t *av)
1017 PERLIO_FUNCS_DECL(*tab) = &PerlIO_perlio;
1018 #ifdef PERLIO_USING_CRLF
1021 if (PerlIO_stdio.Set_ptrcnt)
1022 tab = &PerlIO_stdio;
1024 PerlIO_debug("Pushing %s\n", tab->name);
1025 PerlIO_list_push(aTHX_ av, PerlIO_find_layer(aTHX_ tab->name, 0, 0),
1030 PerlIO_arg_fetch(PerlIO_list_t *av, IV n)
1032 return av->array[n].arg;
1036 PerlIO_layer_fetch(pTHX_ PerlIO_list_t *av, IV n, PerlIO_funcs *def)
1038 if (n >= 0 && n < av->cur) {
1039 PerlIO_debug("Layer %" IVdf " is %s\n", n,
1040 av->array[n].funcs->name);
1041 return av->array[n].funcs;
1044 Perl_croak(aTHX_ "panic: PerlIO layer array corrupt");
1049 PerlIOPop_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
1051 PERL_UNUSED_ARG(mode);
1052 PERL_UNUSED_ARG(arg);
1053 PERL_UNUSED_ARG(tab);
1054 if (PerlIOValid(f)) {
1056 PerlIO_pop(aTHX_ f);
1062 PERLIO_FUNCS_DECL(PerlIO_remove) = {
1063 sizeof(PerlIO_funcs),
1066 PERLIO_K_DUMMY | PERLIO_K_UTF8,
1086 NULL, /* get_base */
1087 NULL, /* get_bufsiz */
1090 NULL, /* set_ptrcnt */
1094 PerlIO_default_layers(pTHX)
1097 if (!PL_def_layerlist) {
1098 const char * const s = (PL_tainting) ? Nullch : PerlEnv_getenv("PERLIO");
1099 PERLIO_FUNCS_DECL(*osLayer) = &PerlIO_unix;
1100 PL_def_layerlist = PerlIO_list_alloc(aTHX);
1101 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_unix));
1103 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_win32));
1105 osLayer = &PerlIO_win32;
1108 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_raw));
1109 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_perlio));
1110 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_stdio));
1111 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_crlf));
1113 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_mmap));
1115 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_utf8));
1116 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_remove));
1117 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_byte));
1118 PerlIO_list_push(aTHX_ PL_def_layerlist,
1119 PerlIO_find_layer(aTHX_ osLayer->name, 0, 0),
1122 PerlIO_parse_layers(aTHX_ PL_def_layerlist, s);
1125 PerlIO_default_buffer(aTHX_ PL_def_layerlist);
1128 if (PL_def_layerlist->cur < 2) {
1129 PerlIO_default_buffer(aTHX_ PL_def_layerlist);
1131 return PL_def_layerlist;
1135 Perl_boot_core_PerlIO(pTHX)
1137 #ifdef USE_ATTRIBUTES_FOR_PERLIO
1138 newXS("io::MODIFY_SCALAR_ATTRIBUTES", XS_io_MODIFY_SCALAR_ATTRIBUTES,
1141 newXS("PerlIO::Layer::find", XS_PerlIO__Layer__find, __FILE__);
1142 newXS("PerlIO::Layer::NoWarnings", XS_PerlIO__Layer__NoWarnings, __FILE__);
1146 PerlIO_default_layer(pTHX_ I32 n)
1149 PerlIO_list_t * const av = PerlIO_default_layers(aTHX);
1152 return PerlIO_layer_fetch(aTHX_ av, n, PERLIO_FUNCS_CAST(&PerlIO_stdio));
1155 #define PerlIO_default_top() PerlIO_default_layer(aTHX_ -1)
1156 #define PerlIO_default_btm() PerlIO_default_layer(aTHX_ 0)
1159 PerlIO_stdstreams(pTHX)
1163 PerlIO_allocate(aTHX);
1164 PerlIO_fdopen(0, "Ir" PERLIO_STDTEXT);
1165 PerlIO_fdopen(1, "Iw" PERLIO_STDTEXT);
1166 PerlIO_fdopen(2, "Iw" PERLIO_STDTEXT);
1171 PerlIO_push(pTHX_ PerlIO *f, PERLIO_FUNCS_DECL(*tab), const char *mode, SV *arg)
1173 if (tab->fsize != sizeof(PerlIO_funcs)) {
1175 Perl_croak(aTHX_ "Layer does not match this perl");
1179 if (tab->size < sizeof(PerlIOl)) {
1182 /* Real layer with a data area */
1183 Newxc(l,tab->size,char,PerlIOl);
1185 Zero(l, tab->size, char);
1187 l->tab = (PerlIO_funcs*) tab;
1189 PerlIO_debug("PerlIO_push f=%p %s %s %p\n", (void*)f, tab->name,
1190 (mode) ? mode : "(Null)", (void*)arg);
1191 if (*l->tab->Pushed &&
1192 (*l->tab->Pushed) (aTHX_ f, mode, arg, (PerlIO_funcs*) tab) != 0) {
1193 PerlIO_pop(aTHX_ f);
1199 /* Pseudo-layer where push does its own stack adjust */
1200 PerlIO_debug("PerlIO_push f=%p %s %s %p\n", (void*)f, tab->name,
1201 (mode) ? mode : "(Null)", (void*)arg);
1203 (*tab->Pushed) (aTHX_ f, mode, arg, (PerlIO_funcs*) tab) != 0) {
1211 PerlIOBase_binmode(pTHX_ PerlIO *f)
1213 if (PerlIOValid(f)) {
1214 /* Is layer suitable for raw stream ? */
1215 if (PerlIOBase(f)->tab->kind & PERLIO_K_RAW) {
1216 /* Yes - turn off UTF-8-ness, to undo UTF-8 locale effects */
1217 PerlIOBase(f)->flags &= ~PERLIO_F_UTF8;
1220 /* Not suitable - pop it */
1221 PerlIO_pop(aTHX_ f);
1229 PerlIORaw_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
1231 PERL_UNUSED_ARG(mode);
1232 PERL_UNUSED_ARG(arg);
1233 PERL_UNUSED_ARG(tab);
1235 if (PerlIOValid(f)) {
1240 * Strip all layers that are not suitable for a raw stream
1243 while (t && (l = *t)) {
1244 if (l->tab->Binmode) {
1245 /* Has a handler - normal case */
1246 if ((*l->tab->Binmode)(aTHX_ f) == 0) {
1248 /* Layer still there - move down a layer */
1257 /* No handler - pop it */
1258 PerlIO_pop(aTHX_ t);
1261 if (PerlIOValid(f)) {
1262 PerlIO_debug(":raw f=%p :%s\n", (void*)f, PerlIOBase(f)->tab->name);
1270 PerlIO_apply_layera(pTHX_ PerlIO *f, const char *mode,
1271 PerlIO_list_t *layers, IV n, IV max)
1275 PerlIO_funcs * const tab = PerlIO_layer_fetch(aTHX_ layers, n, NULL);
1277 if (!PerlIO_push(aTHX_ f, tab, mode, PerlIOArg)) {
1288 PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names)
1292 PerlIO_list_t * const layers = PerlIO_list_alloc(aTHX);
1293 code = PerlIO_parse_layers(aTHX_ layers, names);
1295 code = PerlIO_apply_layera(aTHX_ f, mode, layers, 0, layers->cur);
1297 PerlIO_list_free(aTHX_ layers);
1303 /*--------------------------------------------------------------------------------------*/
1305 * Given the abstraction above the public API functions
1309 PerlIO_binmode(pTHX_ PerlIO *f, int iotype, int mode, const char *names)
1311 PerlIO_debug("PerlIO_binmode f=%p %s %c %x %s\n", (void*)f,
1312 (PerlIOBase(f)) ? PerlIOBase(f)->tab->name : "(Null)",
1313 iotype, mode, (names) ? names : "(Null)");
1316 /* Do not flush etc. if (e.g.) switching encodings.
1317 if a pushed layer knows it needs to flush lower layers
1318 (for example :unix which is never going to call them)
1319 it can do the flush when it is pushed.
1321 return PerlIO_apply_layers(aTHX_ f, NULL, names) == 0 ? TRUE : FALSE;
1324 /* Fake 5.6 legacy of using this call to turn ON O_TEXT */
1325 #ifdef PERLIO_USING_CRLF
1326 /* Legacy binmode only has meaning if O_TEXT has a value distinct from
1327 O_BINARY so we can look for it in mode.
1329 if (!(mode & O_BINARY)) {
1331 /* FIXME?: Looking down the layer stack seems wrong,
1332 but is a way of reaching past (say) an encoding layer
1333 to flip CRLF-ness of the layer(s) below
1336 /* Perhaps we should turn on bottom-most aware layer
1337 e.g. Ilya's idea that UNIX TTY could serve
1339 if (PerlIOBase(f)->tab->kind & PERLIO_K_CANCRLF) {
1340 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF)) {
1341 /* Not in text mode - flush any pending stuff and flip it */
1343 PerlIOBase(f)->flags |= PERLIO_F_CRLF;
1345 /* Only need to turn it on in one layer so we are done */
1350 /* Not finding a CRLF aware layer presumably means we are binary
1351 which is not what was requested - so we failed
1352 We _could_ push :crlf layer but so could caller
1357 /* Legacy binmode is now _defined_ as being equivalent to pushing :raw
1358 So code that used to be here is now in PerlIORaw_pushed().
1360 return PerlIO_push(aTHX_ f, PERLIO_FUNCS_CAST(&PerlIO_raw), Nullch, Nullsv) ? TRUE : FALSE;
1365 PerlIO__close(pTHX_ PerlIO *f)
1367 if (PerlIOValid(f)) {
1368 PerlIO_funcs * const tab = PerlIOBase(f)->tab;
1369 if (tab && tab->Close)
1370 return (*tab->Close)(aTHX_ f);
1372 return PerlIOBase_close(aTHX_ f);
1375 SETERRNO(EBADF, SS_IVCHAN);
1381 Perl_PerlIO_close(pTHX_ PerlIO *f)
1383 const int code = PerlIO__close(aTHX_ f);
1384 while (PerlIOValid(f)) {
1385 PerlIO_pop(aTHX_ f);
1391 Perl_PerlIO_fileno(pTHX_ PerlIO *f)
1394 Perl_PerlIO_or_Base(f, Fileno, fileno, -1, (aTHX_ f));
1398 PerlIO_context_layers(pTHX_ const char *mode)
1401 const char *type = NULL;
1403 * Need to supply default layer info from open.pm
1406 SV * const layers = PL_curcop->cop_io;
1409 type = SvPV_const(layers, len);
1410 if (type && mode[0] != 'r') {
1412 * Skip to write part
1414 const char * const s = strchr(type, 0);
1415 if (s && (STRLEN)(s - type) < len) {
1424 static PerlIO_funcs *
1425 PerlIO_layer_from_ref(pTHX_ SV *sv)
1429 * For any scalar type load the handler which is bundled with perl
1431 if (SvTYPE(sv) < SVt_PVAV)
1432 return PerlIO_find_layer(aTHX_ STR_WITH_LEN("scalar"), 1);
1435 * For other types allow if layer is known but don't try and load it
1437 switch (SvTYPE(sv)) {
1439 return PerlIO_find_layer(aTHX_ STR_WITH_LEN("Array"), 0);
1441 return PerlIO_find_layer(aTHX_ STR_WITH_LEN("Hash"), 0);
1443 return PerlIO_find_layer(aTHX_ STR_WITH_LEN("Code"), 0);
1445 return PerlIO_find_layer(aTHX_ STR_WITH_LEN("Glob"), 0);
1451 PerlIO_resolve_layers(pTHX_ const char *layers,
1452 const char *mode, int narg, SV **args)
1455 PerlIO_list_t *def = PerlIO_default_layers(aTHX);
1458 PerlIO_stdstreams(aTHX);
1460 SV * const arg = *args;
1462 * If it is a reference but not an object see if we have a handler
1465 if (SvROK(arg) && !sv_isobject(arg)) {
1466 PerlIO_funcs * const handler = PerlIO_layer_from_ref(aTHX_ SvRV(arg));
1468 def = PerlIO_list_alloc(aTHX);
1469 PerlIO_list_push(aTHX_ def, handler, &PL_sv_undef);
1473 * Don't fail if handler cannot be found :via(...) etc. may do
1474 * something sensible else we will just stringfy and open
1479 if (!layers || !*layers)
1480 layers = PerlIO_context_layers(aTHX_ mode);
1481 if (layers && *layers) {
1485 av = PerlIO_list_alloc(aTHX);
1486 for (i = 0; i < def->cur; i++) {
1487 PerlIO_list_push(aTHX_ av, def->array[i].funcs,
1494 if (PerlIO_parse_layers(aTHX_ av, layers) == 0) {
1498 PerlIO_list_free(aTHX_ av);
1499 return (PerlIO_list_t *) NULL;
1510 PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd,
1511 int imode, int perm, PerlIO *f, int narg, SV **args)
1514 if (!f && narg == 1 && *args == &PL_sv_undef) {
1515 if ((f = PerlIO_tmpfile())) {
1516 if (!layers || !*layers)
1517 layers = PerlIO_context_layers(aTHX_ mode);
1518 if (layers && *layers)
1519 PerlIO_apply_layers(aTHX_ f, mode, layers);
1523 PerlIO_list_t *layera;
1525 PerlIO_funcs *tab = NULL;
1526 if (PerlIOValid(f)) {
1528 * This is "reopen" - it is not tested as perl does not use it
1532 layera = PerlIO_list_alloc(aTHX);
1534 SV * const arg = (l->tab->Getarg)
1535 ? (*l->tab->Getarg) (aTHX_ &l, NULL, 0)
1537 PerlIO_list_push(aTHX_ layera, l->tab, arg);
1538 l = *PerlIONext(&l);
1542 layera = PerlIO_resolve_layers(aTHX_ layers, mode, narg, args);
1548 * Start at "top" of layer stack
1550 n = layera->cur - 1;
1552 PerlIO_funcs * const t = PerlIO_layer_fetch(aTHX_ layera, n, NULL);
1561 * Found that layer 'n' can do opens - call it
1563 if (narg > 1 && !(tab->kind & PERLIO_K_MULTIARG)) {
1564 Perl_croak(aTHX_ "More than one argument to open(,':%s')",tab->name);
1566 PerlIO_debug("openn(%s,'%s','%s',%d,%x,%o,%p,%d,%p)\n",
1567 tab->name, layers ? layers : "(Null)", mode, fd,
1568 imode, perm, (void*)f, narg, (void*)args);
1570 f = (*tab->Open) (aTHX_ tab, layera, n, mode, fd, imode, perm,
1573 SETERRNO(EINVAL, LIB_INVARG);
1577 if (n + 1 < layera->cur) {
1579 * More layers above the one that we used to open -
1582 if (PerlIO_apply_layera(aTHX_ f, mode, layera, n + 1, layera->cur) != 0) {
1583 /* If pushing layers fails close the file */
1590 PerlIO_list_free(aTHX_ layera);
1597 Perl_PerlIO_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
1599 Perl_PerlIO_or_Base(f, Read, read, -1, (aTHX_ f, vbuf, count));
1603 Perl_PerlIO_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
1605 Perl_PerlIO_or_Base(f, Unread, unread, -1, (aTHX_ f, vbuf, count));
1609 Perl_PerlIO_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
1611 Perl_PerlIO_or_fail(f, Write, -1, (aTHX_ f, vbuf, count));
1615 Perl_PerlIO_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
1617 Perl_PerlIO_or_fail(f, Seek, -1, (aTHX_ f, offset, whence));
1621 Perl_PerlIO_tell(pTHX_ PerlIO *f)
1623 Perl_PerlIO_or_fail(f, Tell, -1, (aTHX_ f));
1627 Perl_PerlIO_flush(pTHX_ PerlIO *f)
1632 const PerlIO_funcs *tab = PerlIOBase(f)->tab;
1634 if (tab && tab->Flush)
1635 return (*tab->Flush) (aTHX_ f);
1637 return 0; /* If no Flush defined, silently succeed. */
1640 PerlIO_debug("Cannot flush f=%p\n", (void*)f);
1641 SETERRNO(EBADF, SS_IVCHAN);
1647 * Is it good API design to do flush-all on NULL, a potentially
1648 * errorneous input? Maybe some magical value (PerlIO*
1649 * PERLIO_FLUSH_ALL = (PerlIO*)-1;)? Yes, stdio does similar
1650 * things on fflush(NULL), but should we be bound by their design
1653 PerlIO **table = &PL_perlio;
1655 while ((f = *table)) {
1657 table = (PerlIO **) (f++);
1658 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
1659 if (*f && PerlIO_flush(f) != 0)
1669 PerlIOBase_flush_linebuf(pTHX)
1672 PerlIO **table = &PL_perlio;
1674 while ((f = *table)) {
1676 table = (PerlIO **) (f++);
1677 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
1680 flags & (PERLIO_F_LINEBUF | PERLIO_F_CANWRITE))
1681 == (PERLIO_F_LINEBUF | PERLIO_F_CANWRITE))
1689 Perl_PerlIO_fill(pTHX_ PerlIO *f)
1691 Perl_PerlIO_or_fail(f, Fill, -1, (aTHX_ f));
1695 PerlIO_isutf8(PerlIO *f)
1698 return (PerlIOBase(f)->flags & PERLIO_F_UTF8) != 0;
1700 SETERRNO(EBADF, SS_IVCHAN);
1706 Perl_PerlIO_eof(pTHX_ PerlIO *f)
1708 Perl_PerlIO_or_Base(f, Eof, eof, -1, (aTHX_ f));
1712 Perl_PerlIO_error(pTHX_ PerlIO *f)
1714 Perl_PerlIO_or_Base(f, Error, error, -1, (aTHX_ f));
1718 Perl_PerlIO_clearerr(pTHX_ PerlIO *f)
1720 Perl_PerlIO_or_Base_void(f, Clearerr, clearerr, (aTHX_ f));
1724 Perl_PerlIO_setlinebuf(pTHX_ PerlIO *f)
1726 Perl_PerlIO_or_Base_void(f, Setlinebuf, setlinebuf, (aTHX_ f));
1730 PerlIO_has_base(PerlIO *f)
1732 if (PerlIOValid(f)) {
1733 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
1736 return (tab->Get_base != NULL);
1737 SETERRNO(EINVAL, LIB_INVARG);
1740 SETERRNO(EBADF, SS_IVCHAN);
1746 PerlIO_fast_gets(PerlIO *f)
1748 if (PerlIOValid(f) && (PerlIOBase(f)->flags & PERLIO_F_FASTGETS)) {
1749 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
1752 return (tab->Set_ptrcnt != NULL);
1753 SETERRNO(EINVAL, LIB_INVARG);
1756 SETERRNO(EBADF, SS_IVCHAN);
1762 PerlIO_has_cntptr(PerlIO *f)
1764 if (PerlIOValid(f)) {
1765 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
1768 return (tab->Get_ptr != NULL && tab->Get_cnt != NULL);
1769 SETERRNO(EINVAL, LIB_INVARG);
1772 SETERRNO(EBADF, SS_IVCHAN);
1778 PerlIO_canset_cnt(PerlIO *f)
1780 if (PerlIOValid(f)) {
1781 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
1784 return (tab->Set_ptrcnt != NULL);
1785 SETERRNO(EINVAL, LIB_INVARG);
1788 SETERRNO(EBADF, SS_IVCHAN);
1794 Perl_PerlIO_get_base(pTHX_ PerlIO *f)
1796 Perl_PerlIO_or_fail(f, Get_base, NULL, (aTHX_ f));
1800 Perl_PerlIO_get_bufsiz(pTHX_ PerlIO *f)
1802 Perl_PerlIO_or_fail(f, Get_bufsiz, -1, (aTHX_ f));
1806 Perl_PerlIO_get_ptr(pTHX_ PerlIO *f)
1808 Perl_PerlIO_or_fail(f, Get_ptr, NULL, (aTHX_ f));
1812 Perl_PerlIO_get_cnt(pTHX_ PerlIO *f)
1814 Perl_PerlIO_or_fail(f, Get_cnt, -1, (aTHX_ f));
1818 Perl_PerlIO_set_cnt(pTHX_ PerlIO *f, int cnt)
1820 Perl_PerlIO_or_fail_void(f, Set_ptrcnt, (aTHX_ f, NULL, cnt));
1824 Perl_PerlIO_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, int cnt)
1826 Perl_PerlIO_or_fail_void(f, Set_ptrcnt, (aTHX_ f, ptr, cnt));
1830 /*--------------------------------------------------------------------------------------*/
1832 * utf8 and raw dummy layers
1836 PerlIOUtf8_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
1838 PERL_UNUSED_ARG(mode);
1839 PERL_UNUSED_ARG(arg);
1840 if (PerlIOValid(f)) {
1841 if (tab->kind & PERLIO_K_UTF8)
1842 PerlIOBase(f)->flags |= PERLIO_F_UTF8;
1844 PerlIOBase(f)->flags &= ~PERLIO_F_UTF8;
1850 PERLIO_FUNCS_DECL(PerlIO_utf8) = {
1851 sizeof(PerlIO_funcs),
1854 PERLIO_K_DUMMY | PERLIO_K_UTF8,
1874 NULL, /* get_base */
1875 NULL, /* get_bufsiz */
1878 NULL, /* set_ptrcnt */
1881 PERLIO_FUNCS_DECL(PerlIO_byte) = {
1882 sizeof(PerlIO_funcs),
1905 NULL, /* get_base */
1906 NULL, /* get_bufsiz */
1909 NULL, /* set_ptrcnt */
1913 PerlIORaw_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
1914 IV n, const char *mode, int fd, int imode, int perm,
1915 PerlIO *old, int narg, SV **args)
1917 PerlIO_funcs * const tab = PerlIO_default_btm();
1918 PERL_UNUSED_ARG(self);
1919 if (tab && tab->Open)
1920 return (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
1922 SETERRNO(EINVAL, LIB_INVARG);
1926 PERLIO_FUNCS_DECL(PerlIO_raw) = {
1927 sizeof(PerlIO_funcs),
1950 NULL, /* get_base */
1951 NULL, /* get_bufsiz */
1954 NULL, /* set_ptrcnt */
1956 /*--------------------------------------------------------------------------------------*/
1957 /*--------------------------------------------------------------------------------------*/
1959 * "Methods" of the "base class"
1963 PerlIOBase_fileno(pTHX_ PerlIO *f)
1965 return PerlIOValid(f) ? PerlIO_fileno(PerlIONext(f)) : -1;
1969 PerlIO_modestr(PerlIO * f, char *buf)
1972 if (PerlIOValid(f)) {
1973 const IV flags = PerlIOBase(f)->flags;
1974 if (flags & PERLIO_F_APPEND) {
1976 if (flags & PERLIO_F_CANREAD) {
1980 else if (flags & PERLIO_F_CANREAD) {
1982 if (flags & PERLIO_F_CANWRITE)
1985 else if (flags & PERLIO_F_CANWRITE) {
1987 if (flags & PERLIO_F_CANREAD) {
1991 #ifdef PERLIO_USING_CRLF
1992 if (!(flags & PERLIO_F_CRLF))
2002 PerlIOBase_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
2004 PerlIOl * const l = PerlIOBase(f);
2005 PERL_UNUSED_ARG(arg);
2007 l->flags &= ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE |
2008 PERLIO_F_TRUNCATE | PERLIO_F_APPEND);
2009 if (tab->Set_ptrcnt != NULL)
2010 l->flags |= PERLIO_F_FASTGETS;
2012 if (*mode == IoTYPE_NUMERIC || *mode == IoTYPE_IMPLICIT)
2016 l->flags |= PERLIO_F_CANREAD;
2019 l->flags |= PERLIO_F_APPEND | PERLIO_F_CANWRITE;
2022 l->flags |= PERLIO_F_TRUNCATE | PERLIO_F_CANWRITE;
2025 SETERRNO(EINVAL, LIB_INVARG);
2031 l->flags |= PERLIO_F_CANREAD | PERLIO_F_CANWRITE;
2034 l->flags &= ~PERLIO_F_CRLF;
2037 l->flags |= PERLIO_F_CRLF;
2040 SETERRNO(EINVAL, LIB_INVARG);
2047 l->flags |= l->next->flags &
2048 (PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_TRUNCATE |
2053 PerlIO_debug("PerlIOBase_pushed f=%p %s %s fl=%08" UVxf " (%s)\n",
2054 f, PerlIOBase(f)->tab->name, (omode) ? omode : "(Null)",
2055 l->flags, PerlIO_modestr(f, temp));
2061 PerlIOBase_popped(pTHX_ PerlIO *f)
2068 PerlIOBase_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
2071 * Save the position as current head considers it
2073 const Off_t old = PerlIO_tell(f);
2074 PerlIO_push(aTHX_ f, PERLIO_FUNCS_CAST(&PerlIO_pending), "r", Nullsv);
2075 PerlIOSelf(f, PerlIOBuf)->posn = old;
2076 return PerlIOBuf_unread(aTHX_ f, vbuf, count);
2080 PerlIOBase_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
2082 STDCHAR *buf = (STDCHAR *) vbuf;
2084 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD)) {
2085 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2086 SETERRNO(EBADF, SS_IVCHAN);
2092 SSize_t avail = PerlIO_get_cnt(f);
2095 take = ((SSize_t)count < avail) ? count : avail;
2097 STDCHAR *ptr = PerlIO_get_ptr(f);
2098 Copy(ptr, buf, take, STDCHAR);
2099 PerlIO_set_ptrcnt(f, ptr + take, (avail -= take));
2102 if (avail == 0) /* set_ptrcnt could have reset avail */
2105 if (count > 0 && avail <= 0) {
2106 if (PerlIO_fill(f) != 0)
2111 return (buf - (STDCHAR *) vbuf);
2117 PerlIOBase_noop_ok(pTHX_ PerlIO *f)
2124 PerlIOBase_noop_fail(pTHX_ PerlIO *f)
2131 PerlIOBase_close(pTHX_ PerlIO *f)
2134 if (PerlIOValid(f)) {
2135 PerlIO *n = PerlIONext(f);
2136 code = PerlIO_flush(f);
2137 PerlIOBase(f)->flags &=
2138 ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_OPEN);
2139 while (PerlIOValid(n)) {
2140 const PerlIO_funcs * const tab = PerlIOBase(n)->tab;
2141 if (tab && tab->Close) {
2142 if ((*tab->Close)(aTHX_ n) != 0)
2147 PerlIOBase(n)->flags &=
2148 ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_OPEN);
2154 SETERRNO(EBADF, SS_IVCHAN);
2160 PerlIOBase_eof(pTHX_ PerlIO *f)
2162 if (PerlIOValid(f)) {
2163 return (PerlIOBase(f)->flags & PERLIO_F_EOF) != 0;
2169 PerlIOBase_error(pTHX_ PerlIO *f)
2171 if (PerlIOValid(f)) {
2172 return (PerlIOBase(f)->flags & PERLIO_F_ERROR) != 0;
2178 PerlIOBase_clearerr(pTHX_ PerlIO *f)
2180 if (PerlIOValid(f)) {
2181 PerlIO * const n = PerlIONext(f);
2182 PerlIOBase(f)->flags &= ~(PERLIO_F_ERROR | PERLIO_F_EOF);
2189 PerlIOBase_setlinebuf(pTHX_ PerlIO *f)
2191 if (PerlIOValid(f)) {
2192 PerlIOBase(f)->flags |= PERLIO_F_LINEBUF;
2197 PerlIO_sv_dup(pTHX_ SV *arg, CLONE_PARAMS *param)
2203 return sv_dup(arg, param);
2206 return newSVsv(arg);
2209 PERL_UNUSED_ARG(param);
2210 return newSVsv(arg);
2215 PerlIOBase_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
2217 PerlIO * const nexto = PerlIONext(o);
2218 if (PerlIOValid(nexto)) {
2219 const PerlIO_funcs * const tab = PerlIOBase(nexto)->tab;
2220 if (tab && tab->Dup)
2221 f = (*tab->Dup)(aTHX_ f, nexto, param, flags);
2223 f = PerlIOBase_dup(aTHX_ f, nexto, param, flags);
2226 PerlIO_funcs * const self = PerlIOBase(o)->tab;
2229 PerlIO_debug("PerlIOBase_dup %s f=%p o=%p param=%p\n",
2230 self->name, (void*)f, (void*)o, (void*)param);
2232 arg = (*self->Getarg)(aTHX_ o, param, flags);
2236 f = PerlIO_push(aTHX_ f, self, PerlIO_modestr(o,buf), arg);
2245 perl_mutex PerlIO_mutex;
2248 /* PL_perlio_fd_refcnt[] is in intrpvar.h */
2253 /* Place holder for stdstreams call ??? */
2255 MUTEX_INIT(&PerlIO_mutex);
2260 PerlIOUnix_refcnt_inc(int fd)
2263 if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
2266 MUTEX_LOCK(&PerlIO_mutex);
2268 PL_perlio_fd_refcnt[fd]++;
2269 PerlIO_debug("fd %d refcnt=%d\n",fd,PL_perlio_fd_refcnt[fd]);
2271 MUTEX_UNLOCK(&PerlIO_mutex);
2277 PerlIOUnix_refcnt_dec(int fd)
2281 if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
2284 MUTEX_LOCK(&PerlIO_mutex);
2286 cnt = --PL_perlio_fd_refcnt[fd];
2287 PerlIO_debug("fd %d refcnt=%d\n",fd,cnt);
2289 MUTEX_UNLOCK(&PerlIO_mutex);
2296 PerlIO_cleanup(pTHX)
2301 PerlIO_debug("Cleanup layers for %p\n",aTHX);
2303 PerlIO_debug("Cleanup layers\n");
2305 /* Raise STDIN..STDERR refcount so we don't close them */
2306 for (i=0; i < 3; i++)
2307 PerlIOUnix_refcnt_inc(i);
2308 PerlIO_cleantable(aTHX_ &PL_perlio);
2309 /* Restore STDIN..STDERR refcount */
2310 for (i=0; i < 3; i++)
2311 PerlIOUnix_refcnt_dec(i);
2313 if (PL_known_layers) {
2314 PerlIO_list_free(aTHX_ PL_known_layers);
2315 PL_known_layers = NULL;
2317 if (PL_def_layerlist) {
2318 PerlIO_list_free(aTHX_ PL_def_layerlist);
2319 PL_def_layerlist = NULL;
2325 /*--------------------------------------------------------------------------------------*/
2327 * Bottom-most level for UNIX-like case
2331 struct _PerlIO base; /* The generic part */
2332 int fd; /* UNIX like file descriptor */
2333 int oflags; /* open/fcntl flags */
2337 PerlIOUnix_oflags(const char *mode)
2340 if (*mode == IoTYPE_IMPLICIT || *mode == IoTYPE_NUMERIC)
2345 if (*++mode == '+') {
2352 oflags = O_CREAT | O_TRUNC;
2353 if (*++mode == '+') {
2362 oflags = O_CREAT | O_APPEND;
2363 if (*++mode == '+') {
2376 else if (*mode == 't') {
2378 oflags &= ~O_BINARY;
2382 * Always open in binary mode
2385 if (*mode || oflags == -1) {
2386 SETERRNO(EINVAL, LIB_INVARG);
2393 PerlIOUnix_fileno(pTHX_ PerlIO *f)
2395 return PerlIOSelf(f, PerlIOUnix)->fd;
2399 PerlIOUnix_setfd(pTHX_ PerlIO *f, int fd, int imode)
2401 PerlIOUnix * const s = PerlIOSelf(f, PerlIOUnix);
2404 if (PerlLIO_fstat(fd, &st) == 0) {
2405 if (!S_ISREG(st.st_mode)) {
2406 PerlIO_debug("%d is not regular file\n",fd);
2407 PerlIOBase(f)->flags |= PERLIO_F_NOTREG;
2410 PerlIO_debug("%d _is_ a regular file\n",fd);
2416 PerlIOUnix_refcnt_inc(fd);
2420 PerlIOUnix_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
2422 IV code = PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
2423 if (*PerlIONext(f)) {
2424 /* We never call down so do any pending stuff now */
2425 PerlIO_flush(PerlIONext(f));
2427 * XXX could (or should) we retrieve the oflags from the open file
2428 * handle rather than believing the "mode" we are passed in? XXX
2429 * Should the value on NULL mode be 0 or -1?
2431 PerlIOUnix_setfd(aTHX_ f, PerlIO_fileno(PerlIONext(f)),
2432 mode ? PerlIOUnix_oflags(mode) : -1);
2434 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
2440 PerlIOUnix_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
2442 const int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2444 if (PerlIOBase(f)->flags & PERLIO_F_NOTREG) {
2446 SETERRNO(ESPIPE, LIB_INVARG);
2448 SETERRNO(EINVAL, LIB_INVARG);
2452 new_loc = PerlLIO_lseek(fd, offset, whence);
2453 if (new_loc == (Off_t) - 1)
2455 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
2460 PerlIOUnix_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2461 IV n, const char *mode, int fd, int imode,
2462 int perm, PerlIO *f, int narg, SV **args)
2464 if (PerlIOValid(f)) {
2465 if (PerlIOBase(f)->flags & PERLIO_F_OPEN)
2466 (*PerlIOBase(f)->tab->Close)(aTHX_ f);
2469 if (*mode == IoTYPE_NUMERIC)
2472 imode = PerlIOUnix_oflags(mode);
2476 const char *path = SvPV_nolen_const(*args);
2477 fd = PerlLIO_open3(path, imode, perm);
2481 if (*mode == IoTYPE_IMPLICIT)
2484 f = PerlIO_allocate(aTHX);
2486 if (!PerlIOValid(f)) {
2487 if (!(f = PerlIO_push(aTHX_ f, self, mode, PerlIOArg))) {
2491 PerlIOUnix_setfd(aTHX_ f, fd, imode);
2492 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
2493 if (*mode == IoTYPE_APPEND)
2494 PerlIOUnix_seek(aTHX_ f, 0, SEEK_END);
2500 * FIXME: pop layers ???
2508 PerlIOUnix_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
2510 const PerlIOUnix * const os = PerlIOSelf(o, PerlIOUnix);
2512 if (flags & PERLIO_DUP_FD) {
2513 fd = PerlLIO_dup(fd);
2515 if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
2516 f = PerlIOBase_dup(aTHX_ f, o, param, flags);
2518 /* If all went well overwrite fd in dup'ed lay with the dup()'ed fd */
2519 PerlIOUnix_setfd(aTHX_ f, fd, os->oflags);
2528 PerlIOUnix_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
2531 const int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2532 #ifdef PERLIO_STD_SPECIAL
2534 return PERLIO_STD_IN(fd, vbuf, count);
2536 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD) ||
2537 PerlIOBase(f)->flags & (PERLIO_F_EOF|PERLIO_F_ERROR)) {
2541 const SSize_t len = PerlLIO_read(fd, vbuf, count);
2542 if (len >= 0 || errno != EINTR) {
2544 if (errno != EAGAIN) {
2545 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2548 else if (len == 0 && count != 0) {
2549 PerlIOBase(f)->flags |= PERLIO_F_EOF;
2560 PerlIOUnix_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
2563 const int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2564 #ifdef PERLIO_STD_SPECIAL
2565 if (fd == 1 || fd == 2)
2566 return PERLIO_STD_OUT(fd, vbuf, count);
2569 const SSize_t len = PerlLIO_write(fd, vbuf, count);
2570 if (len >= 0 || errno != EINTR) {
2572 if (errno != EAGAIN) {
2573 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2584 PerlIOUnix_tell(pTHX_ PerlIO *f)
2586 return PerlLIO_lseek(PerlIOSelf(f, PerlIOUnix)->fd, 0, SEEK_CUR);
2591 PerlIOUnix_close(pTHX_ PerlIO *f)
2594 const int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2596 if (PerlIOBase(f)->flags & PERLIO_F_OPEN) {
2597 if (PerlIOUnix_refcnt_dec(fd) > 0) {
2598 PerlIOBase(f)->flags &= ~PERLIO_F_OPEN;
2603 SETERRNO(EBADF,SS_IVCHAN);
2606 while (PerlLIO_close(fd) != 0) {
2607 if (errno != EINTR) {
2614 PerlIOBase(f)->flags &= ~PERLIO_F_OPEN;
2619 PERLIO_FUNCS_DECL(PerlIO_unix) = {
2620 sizeof(PerlIO_funcs),
2627 PerlIOBase_binmode, /* binmode */
2637 PerlIOBase_noop_ok, /* flush */
2638 PerlIOBase_noop_fail, /* fill */
2641 PerlIOBase_clearerr,
2642 PerlIOBase_setlinebuf,
2643 NULL, /* get_base */
2644 NULL, /* get_bufsiz */
2647 NULL, /* set_ptrcnt */
2650 /*--------------------------------------------------------------------------------------*/
2655 #if defined(VMS) && !defined(STDIO_BUFFER_WRITABLE)
2656 /* perl5.8 - This ensures the last minute VMS ungetc fix is not
2657 broken by the last second glibc 2.3 fix
2659 #define STDIO_BUFFER_WRITABLE
2664 struct _PerlIO base;
2665 FILE *stdio; /* The stream */
2669 PerlIOStdio_fileno(pTHX_ PerlIO *f)
2671 if (PerlIOValid(f)) {
2672 FILE * const s = PerlIOSelf(f, PerlIOStdio)->stdio;
2674 return PerlSIO_fileno(s);
2681 PerlIOStdio_mode(const char *mode, char *tmode)
2683 char * const ret = tmode;
2689 #if defined(PERLIO_USING_CRLF) || defined(__CYGWIN__)
2697 PerlIOStdio_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
2700 if (PerlIOValid(f) && PerlIOValid(n = PerlIONext(f))) {
2701 PerlIO_funcs * const toptab = PerlIOBase(n)->tab;
2702 if (toptab == tab) {
2703 /* Top is already stdio - pop self (duplicate) and use original */
2704 PerlIO_pop(aTHX_ f);
2707 const int fd = PerlIO_fileno(n);
2710 if (fd >= 0 && (stdio = PerlSIO_fdopen(fd,
2711 mode = PerlIOStdio_mode(mode, tmode)))) {
2712 PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
2713 /* We never call down so do any pending stuff now */
2714 PerlIO_flush(PerlIONext(f));
2721 return PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
2726 PerlIO_importFILE(FILE *stdio, const char *mode)
2732 if (!mode || !*mode) {
2733 /* We need to probe to see how we can open the stream
2734 so start with read/write and then try write and read
2735 we dup() so that we can fclose without loosing the fd.
2737 Note that the errno value set by a failing fdopen
2738 varies between stdio implementations.
2740 const int fd = PerlLIO_dup(fileno(stdio));
2741 FILE *f2 = PerlSIO_fdopen(fd, (mode = "r+"));
2743 f2 = PerlSIO_fdopen(fd, (mode = "w"));
2746 f2 = PerlSIO_fdopen(fd, (mode = "r"));
2749 /* Don't seem to be able to open */
2755 if ((f = PerlIO_push(aTHX_(f = PerlIO_allocate(aTHX)), PERLIO_FUNCS_CAST(&PerlIO_stdio), mode, Nullsv))) {
2756 s = PerlIOSelf(f, PerlIOStdio);
2764 PerlIOStdio_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2765 IV n, const char *mode, int fd, int imode,
2766 int perm, PerlIO *f, int narg, SV **args)
2769 if (PerlIOValid(f)) {
2770 const char * const path = SvPV_nolen_const(*args);
2771 PerlIOStdio * const s = PerlIOSelf(f, PerlIOStdio);
2773 PerlIOUnix_refcnt_dec(fileno(s->stdio));
2774 stdio = PerlSIO_freopen(path, (mode = PerlIOStdio_mode(mode, tmode)),
2779 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2784 const char * const path = SvPV_nolen_const(*args);
2785 if (*mode == IoTYPE_NUMERIC) {
2787 fd = PerlLIO_open3(path, imode, perm);
2791 bool appended = FALSE;
2793 /* Cygwin wants its 'b' early. */
2795 mode = PerlIOStdio_mode(mode, tmode);
2797 stdio = PerlSIO_fopen(path, mode);
2801 f = PerlIO_allocate(aTHX);
2804 mode = PerlIOStdio_mode(mode, tmode);
2805 f = PerlIO_push(aTHX_ f, self, mode, PerlIOArg);
2807 s = PerlIOSelf(f, PerlIOStdio);
2809 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2821 if (*mode == IoTYPE_IMPLICIT) {
2828 stdio = PerlSIO_stdin;
2831 stdio = PerlSIO_stdout;
2834 stdio = PerlSIO_stderr;
2839 stdio = PerlSIO_fdopen(fd, mode =
2840 PerlIOStdio_mode(mode, tmode));
2844 f = PerlIO_allocate(aTHX);
2846 if ((f = PerlIO_push(aTHX_ f, self, mode, PerlIOArg))) {
2847 PerlIOStdio * const s = PerlIOSelf(f, PerlIOStdio);
2849 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2859 PerlIOStdio_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
2861 /* This assumes no layers underneath - which is what
2862 happens, but is not how I remember it. NI-S 2001/10/16
2864 if ((f = PerlIOBase_dup(aTHX_ f, o, param, flags))) {
2865 FILE *stdio = PerlIOSelf(o, PerlIOStdio)->stdio;
2866 const int fd = fileno(stdio);
2868 if (flags & PERLIO_DUP_FD) {
2869 const int dfd = PerlLIO_dup(fileno(stdio));
2871 stdio = PerlSIO_fdopen(dfd, PerlIO_modestr(o,mode));
2875 /* FIXME: To avoid messy error recovery if dup fails
2876 re-use the existing stdio as though flag was not set
2880 stdio = PerlSIO_fdopen(fd, PerlIO_modestr(o,mode));
2882 PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
2883 PerlIOUnix_refcnt_inc(fileno(stdio));
2889 PerlIOStdio_invalidate_fileno(pTHX_ FILE *f)
2891 /* XXX this could use PerlIO_canset_fileno() and
2892 * PerlIO_set_fileno() support from Configure
2894 # if defined(__UCLIBC__)
2895 /* uClibc must come before glibc because it defines __GLIBC__ as well. */
2898 # elif defined(__GLIBC__)
2899 /* There may be a better way for GLIBC:
2900 - libio.h defines a flag to not close() on cleanup
2904 # elif defined(__sun__)
2906 /* On solaris, if _LP64 is defined, the FILE structure is this:
2912 * It turns out that the fd is stored in the top 32 bits of
2913 * file->__pad[4]. The lower 32 bits contain flags. file->pad[5] appears
2914 * to contain a pointer or offset into another structure. All the
2915 * remaining fields are zero.
2917 * We set the top bits to -1 (0xFFFFFFFF).
2919 f->__pad[4] |= 0xffffffff00000000L;
2920 assert(fileno(f) == 0xffffffff);
2921 # else /* !defined(_LP64) */
2922 /* _file is just a unsigned char :-(
2923 Not clear why we dup() rather than using -1
2924 even if that would be treated as 0xFF - so will
2927 f->_file = PerlLIO_dup(fileno(f));
2928 # endif /* defined(_LP64) */
2930 # elif defined(__hpux)
2934 /* Next one ->_file seems to be a reasonable fallback, i.e. if
2935 your platform does not have special entry try this one.
2936 [For OSF only have confirmation for Tru64 (alpha)
2937 but assume other OSFs will be similar.]
2939 # elif defined(_AIX) || defined(__osf__) || defined(__irix__)
2942 # elif defined(__FreeBSD__)
2943 /* There may be a better way on FreeBSD:
2944 - we could insert a dummy func in the _close function entry
2945 f->_close = (int (*)(void *)) dummy_close;
2949 # elif defined(__OpenBSD__)
2950 /* There may be a better way on OpenBSD:
2951 - we could insert a dummy func in the _close function entry
2952 f->_close = (int (*)(void *)) dummy_close;
2956 # elif defined(__EMX__)
2957 /* f->_flags &= ~_IOOPEN; */ /* Will leak stream->_buffer */
2960 # elif defined(__CYGWIN__)
2961 /* There may be a better way on CYGWIN:
2962 - we could insert a dummy func in the _close function entry
2963 f->_close = (int (*)(void *)) dummy_close;
2967 # elif defined(WIN32)
2968 # if defined(__BORLANDC__)
2969 f->fd = PerlLIO_dup(fileno(f));
2970 # elif defined(UNDER_CE)
2971 /* WIN_CE does not have access to FILE internals, it hardly has FILE
2980 /* Sarathy's code did this - we fall back to a dup/dup2 hack
2981 (which isn't thread safe) instead
2983 # error "Don't know how to set FILE.fileno on your platform"
2991 PerlIOStdio_close(pTHX_ PerlIO *f)
2993 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2999 const int fd = fileno(stdio);
3005 #ifdef SOCKS5_VERSION_NAME
3006 /* Socks lib overrides close() but stdio isn't linked to
3007 that library (though we are) - so we must call close()
3008 on sockets on stdio's behalf.
3011 Sock_size_t optlen = sizeof(int);
3012 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, (void *) &optval, &optlen) == 0) {
3017 if (PerlIOUnix_refcnt_dec(fd) > 0) {
3018 /* File descriptor still in use */
3023 /* For STD* handles don't close the stdio at all
3024 this is because we have shared the FILE * too
3026 if (stdio == stdin) {
3027 /* Some stdios are buggy fflush-ing inputs */
3030 else if (stdio == stdout || stdio == stderr) {
3031 return PerlIO_flush(f);
3033 /* Tricky - must fclose(stdio) to free memory but not close(fd)
3034 Use Sarathy's trick from maint-5.6 to invalidate the
3035 fileno slot of the FILE *
3037 result = PerlIO_flush(f);
3039 if (!(invalidate = PerlIOStdio_invalidate_fileno(aTHX_ stdio))) {
3040 dupfd = PerlLIO_dup(fd);
3043 result = PerlSIO_fclose(stdio);
3044 /* We treat error from stdio as success if we invalidated
3045 errno may NOT be expected EBADF
3047 if (invalidate && result != 0) {
3052 /* in SOCKS case let close() determine return value */
3056 PerlLIO_dup2(dupfd,fd);
3057 PerlLIO_close(dupfd);
3064 PerlIOStdio_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
3067 FILE * const s = PerlIOSelf(f, PerlIOStdio)->stdio;
3071 STDCHAR *buf = (STDCHAR *) vbuf;
3073 * Perl is expecting PerlIO_getc() to fill the buffer Linux's
3074 * stdio does not do that for fread()
3076 const int ch = PerlSIO_fgetc(s);
3083 got = PerlSIO_fread(vbuf, 1, count, s);
3084 if (got == 0 && PerlSIO_ferror(s))
3086 if (got >= 0 || errno != EINTR)
3089 SETERRNO(0,0); /* just in case */
3095 PerlIOStdio_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3098 FILE * const s = PerlIOSelf(f, PerlIOStdio)->stdio;
3100 #ifdef STDIO_BUFFER_WRITABLE
3101 if (PerlIO_fast_gets(f) && PerlIO_has_base(f)) {
3102 STDCHAR *buf = ((STDCHAR *) vbuf) + count;
3103 STDCHAR *base = PerlIO_get_base(f);
3104 SSize_t cnt = PerlIO_get_cnt(f);
3105 STDCHAR *ptr = PerlIO_get_ptr(f);
3106 SSize_t avail = ptr - base;
3108 if (avail > count) {
3112 Move(buf-avail,ptr,avail,STDCHAR);
3115 PerlIO_set_ptrcnt(f,ptr,cnt+avail);
3116 if (PerlSIO_feof(s) && unread >= 0)
3117 PerlSIO_clearerr(s);
3122 if (PerlIO_has_cntptr(f)) {
3123 /* We can get pointer to buffer but not its base
3124 Do ungetc() but check chars are ending up in the
3127 STDCHAR *eptr = (STDCHAR*)PerlSIO_get_ptr(s);
3128 STDCHAR *buf = ((STDCHAR *) vbuf) + count;
3130 const int ch = *--buf & 0xFF;
3131 if (ungetc(ch,s) != ch) {
3132 /* ungetc did not work */
3135 if ((STDCHAR*)PerlSIO_get_ptr(s) != --eptr || ((*eptr & 0xFF) != ch)) {
3136 /* Did not change pointer as expected */
3137 fgetc(s); /* get char back again */
3147 unread += PerlIOBase_unread(aTHX_ f, vbuf, count);
3153 PerlIOStdio_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3158 got = PerlSIO_fwrite(vbuf, 1, count,
3159 PerlIOSelf(f, PerlIOStdio)->stdio);
3160 if (got >= 0 || errno != EINTR)
3163 SETERRNO(0,0); /* just in case */
3169 PerlIOStdio_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
3171 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3172 return PerlSIO_fseek(stdio, offset, whence);
3176 PerlIOStdio_tell(pTHX_ PerlIO *f)
3178 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3179 return PerlSIO_ftell(stdio);
3183 PerlIOStdio_flush(pTHX_ PerlIO *f)
3185 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3186 if (PerlIOBase(f)->flags & PERLIO_F_CANWRITE) {
3187 return PerlSIO_fflush(stdio);
3192 * FIXME: This discards ungetc() and pre-read stuff which is not
3193 * right if this is just a "sync" from a layer above Suspect right
3194 * design is to do _this_ but not have layer above flush this
3195 * layer read-to-read
3198 * Not writeable - sync by attempting a seek
3200 const int err = errno;
3201 if (PerlSIO_fseek(stdio, (Off_t) 0, SEEK_CUR) != 0)
3209 PerlIOStdio_eof(pTHX_ PerlIO *f)
3211 return PerlSIO_feof(PerlIOSelf(f, PerlIOStdio)->stdio);
3215 PerlIOStdio_error(pTHX_ PerlIO *f)
3217 return PerlSIO_ferror(PerlIOSelf(f, PerlIOStdio)->stdio);
3221 PerlIOStdio_clearerr(pTHX_ PerlIO *f)
3223 PerlSIO_clearerr(PerlIOSelf(f, PerlIOStdio)->stdio);
3227 PerlIOStdio_setlinebuf(pTHX_ PerlIO *f)
3229 #ifdef HAS_SETLINEBUF
3230 PerlSIO_setlinebuf(PerlIOSelf(f, PerlIOStdio)->stdio);
3232 PerlSIO_setvbuf(PerlIOSelf(f, PerlIOStdio)->stdio, Nullch, _IOLBF, 0);
3238 PerlIOStdio_get_base(pTHX_ PerlIO *f)
3240 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3241 return (STDCHAR*)PerlSIO_get_base(stdio);
3245 PerlIOStdio_get_bufsiz(pTHX_ PerlIO *f)
3247 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3248 return PerlSIO_get_bufsiz(stdio);
3252 #ifdef USE_STDIO_PTR
3254 PerlIOStdio_get_ptr(pTHX_ PerlIO *f)
3256 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3257 return (STDCHAR*)PerlSIO_get_ptr(stdio);
3261 PerlIOStdio_get_cnt(pTHX_ PerlIO *f)
3263 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3264 return PerlSIO_get_cnt(stdio);
3268 PerlIOStdio_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
3270 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3272 #ifdef STDIO_PTR_LVALUE
3273 PerlSIO_set_ptr(stdio, (void*)ptr); /* LHS STDCHAR* cast non-portable */
3274 #ifdef STDIO_PTR_LVAL_SETS_CNT
3275 if (PerlSIO_get_cnt(stdio) != (cnt)) {
3276 assert(PerlSIO_get_cnt(stdio) == (cnt));
3279 #if (!defined(STDIO_PTR_LVAL_NOCHANGE_CNT))
3281 * Setting ptr _does_ change cnt - we are done
3285 #else /* STDIO_PTR_LVALUE */
3287 #endif /* STDIO_PTR_LVALUE */
3290 * Now (or only) set cnt
3292 #ifdef STDIO_CNT_LVALUE
3293 PerlSIO_set_cnt(stdio, cnt);
3294 #else /* STDIO_CNT_LVALUE */
3295 #if (defined(STDIO_PTR_LVALUE) && defined(STDIO_PTR_LVAL_SETS_CNT))
3296 PerlSIO_set_ptr(stdio,
3297 PerlSIO_get_ptr(stdio) + (PerlSIO_get_cnt(stdio) -
3299 #else /* STDIO_PTR_LVAL_SETS_CNT */
3301 #endif /* STDIO_PTR_LVAL_SETS_CNT */
3302 #endif /* STDIO_CNT_LVALUE */
3309 PerlIOStdio_fill(pTHX_ PerlIO *f)
3311 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3314 * fflush()ing read-only streams can cause trouble on some stdio-s
3316 if ((PerlIOBase(f)->flags & PERLIO_F_CANWRITE)) {
3317 if (PerlSIO_fflush(stdio) != 0)
3320 c = PerlSIO_fgetc(stdio);
3324 #if (defined(STDIO_PTR_LVALUE) && (defined(STDIO_CNT_LVALUE) || defined(STDIO_PTR_LVAL_SETS_CNT)))
3326 #ifdef STDIO_BUFFER_WRITABLE
3327 if (PerlIO_fast_gets(f) && PerlIO_has_base(f)) {
3328 /* Fake ungetc() to the real buffer in case system's ungetc
3331 STDCHAR *base = (STDCHAR*)PerlSIO_get_base(stdio);
3332 SSize_t cnt = PerlSIO_get_cnt(stdio);
3333 STDCHAR *ptr = (STDCHAR*)PerlSIO_get_ptr(stdio);
3334 if (ptr == base+1) {
3335 *--ptr = (STDCHAR) c;
3336 PerlIOStdio_set_ptrcnt(aTHX_ f,ptr,cnt+1);
3337 if (PerlSIO_feof(stdio))
3338 PerlSIO_clearerr(stdio);
3344 if (PerlIO_has_cntptr(f)) {
3346 if (PerlIOStdio_unread(aTHX_ f,&ch,1) == 1) {
3353 /* An ungetc()d char is handled separately from the regular
3354 * buffer, so we stuff it in the buffer ourselves.
3355 * Should never get called as should hit code above
3357 *(--((*stdio)->_ptr)) = (unsigned char) c;
3360 /* If buffer snoop scheme above fails fall back to
3363 if (PerlSIO_ungetc(c, stdio) != c)
3371 PERLIO_FUNCS_DECL(PerlIO_stdio) = {
3372 sizeof(PerlIO_funcs),
3374 sizeof(PerlIOStdio),
3375 PERLIO_K_BUFFERED|PERLIO_K_RAW,
3379 PerlIOBase_binmode, /* binmode */
3393 PerlIOStdio_clearerr,
3394 PerlIOStdio_setlinebuf,
3396 PerlIOStdio_get_base,
3397 PerlIOStdio_get_bufsiz,
3402 #ifdef USE_STDIO_PTR
3403 PerlIOStdio_get_ptr,
3404 PerlIOStdio_get_cnt,
3405 # if defined(HAS_FAST_STDIO) && defined(USE_FAST_STDIO)
3406 PerlIOStdio_set_ptrcnt,
3409 # endif /* HAS_FAST_STDIO && USE_FAST_STDIO */
3414 #endif /* USE_STDIO_PTR */
3417 /* Note that calls to PerlIO_exportFILE() are reversed using
3418 * PerlIO_releaseFILE(), not importFILE. */
3420 PerlIO_exportFILE(PerlIO * f, const char *mode)
3424 if (PerlIOValid(f)) {
3427 if (!mode || !*mode) {
3428 mode = PerlIO_modestr(f, buf);
3430 stdio = PerlSIO_fdopen(PerlIO_fileno(f), mode);
3434 /* De-link any lower layers so new :stdio sticks */
3436 if ((f2 = PerlIO_push(aTHX_ f, PERLIO_FUNCS_CAST(&PerlIO_stdio), buf, Nullsv))) {
3437 PerlIOStdio *s = PerlIOSelf((f = f2), PerlIOStdio);
3439 /* Link previous lower layers under new one */
3443 /* restore layers list */
3453 PerlIO_findFILE(PerlIO *f)
3457 if (l->tab == &PerlIO_stdio) {
3458 PerlIOStdio *s = PerlIOSelf(&l, PerlIOStdio);
3461 l = *PerlIONext(&l);
3463 /* Uses fallback "mode" via PerlIO_modestr() in PerlIO_exportFILE */
3464 return PerlIO_exportFILE(f, Nullch);
3467 /* Use this to reverse PerlIO_exportFILE calls. */
3469 PerlIO_releaseFILE(PerlIO *p, FILE *f)
3474 if (l->tab == &PerlIO_stdio) {
3475 PerlIOStdio *s = PerlIOSelf(&l, PerlIOStdio);
3476 if (s->stdio == f) {
3478 PerlIO_pop(aTHX_ p);
3487 /*--------------------------------------------------------------------------------------*/
3489 * perlio buffer layer
3493 PerlIOBuf_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
3495 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3496 const int fd = PerlIO_fileno(f);
3497 if (fd >= 0 && PerlLIO_isatty(fd)) {
3498 PerlIOBase(f)->flags |= PERLIO_F_LINEBUF | PERLIO_F_TTY;
3500 if (*PerlIONext(f)) {
3501 const Off_t posn = PerlIO_tell(PerlIONext(f));
3502 if (posn != (Off_t) - 1) {
3506 return PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
3510 PerlIOBuf_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
3511 IV n, const char *mode, int fd, int imode, int perm,
3512 PerlIO *f, int narg, SV **args)
3514 if (PerlIOValid(f)) {
3515 PerlIO *next = PerlIONext(f);
3517 PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIOBase(next)->tab);
3518 if (tab && tab->Open)
3520 (*tab->Open)(aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
3522 if (!next || (*PerlIOBase(f)->tab->Pushed) (aTHX_ f, mode, PerlIOArg, self) != 0) {
3527 PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIO_default_btm());
3529 if (*mode == IoTYPE_IMPLICIT) {
3535 if (tab && tab->Open)
3536 f = (*tab->Open)(aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
3539 SETERRNO(EINVAL, LIB_INVARG);
3541 if (PerlIO_push(aTHX_ f, self, mode, PerlIOArg) == 0) {
3543 * if push fails during open, open fails. close will pop us.
3548 fd = PerlIO_fileno(f);
3549 if (init && fd == 2) {
3551 * Initial stderr is unbuffered
3553 PerlIOBase(f)->flags |= PERLIO_F_UNBUF;
3555 #ifdef PERLIO_USING_CRLF
3556 # ifdef PERLIO_IS_BINMODE_FD
3557 if (PERLIO_IS_BINMODE_FD(fd))
3558 PerlIO_binmode(aTHX_ f, '<'/*not used*/, O_BINARY, Nullch);
3562 * do something about failing setmode()? --jhi
3564 PerlLIO_setmode(fd, O_BINARY);
3573 * This "flush" is akin to sfio's sync in that it handles files in either
3574 * read or write state. For write state, we put the postponed data through
3575 * the next layers. For read state, we seek() the next layers to the
3576 * offset given by current position in the buffer, and discard the buffer
3577 * state (XXXX supposed to be for seek()able buffers only, but now it is done
3578 * in any case?). Then the pass the stick further in chain.
3581 PerlIOBuf_flush(pTHX_ PerlIO *f)
3583 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3585 PerlIO *n = PerlIONext(f);
3586 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF) {
3588 * write() the buffer
3590 const STDCHAR *buf = b->buf;
3591 const STDCHAR *p = buf;
3592 while (p < b->ptr) {
3593 SSize_t count = PerlIO_write(n, p, b->ptr - p);
3597 else if (count < 0 || PerlIO_error(n)) {
3598 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
3603 b->posn += (p - buf);
3605 else if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
3606 STDCHAR *buf = PerlIO_get_base(f);
3608 * Note position change
3610 b->posn += (b->ptr - buf);
3611 if (b->ptr < b->end) {
3612 /* We did not consume all of it - try and seek downstream to
3613 our logical position
3615 if (PerlIOValid(n) && PerlIO_seek(n, b->posn, SEEK_SET) == 0) {
3616 /* Reload n as some layers may pop themselves on seek */
3617 b->posn = PerlIO_tell(n = PerlIONext(f));
3620 /* Seek failed (e.g. pipe or tty). Do NOT clear buffer or pre-read
3621 data is lost for good - so return saying "ok" having undone
3624 b->posn -= (b->ptr - buf);
3629 b->ptr = b->end = b->buf;
3630 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
3631 /* We check for Valid because of dubious decision to make PerlIO_flush(NULL) flush all */
3632 if (PerlIOValid(n) && PerlIO_flush(n) != 0)
3637 /* This discards the content of the buffer after b->ptr, and rereads
3638 * the buffer from the position off in the layer downstream; here off
3639 * is at offset corresponding to b->ptr - b->buf.
3642 PerlIOBuf_fill(pTHX_ PerlIO *f)
3644 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3645 PerlIO *n = PerlIONext(f);
3648 * Down-stream flush is defined not to loose read data so is harmless.
3649 * we would not normally be fill'ing if there was data left in anycase.
3651 if (PerlIO_flush(f) != 0) /* XXXX Check that its seek() succeeded?! */
3653 if (PerlIOBase(f)->flags & PERLIO_F_TTY)
3654 PerlIOBase_flush_linebuf(aTHX);
3657 PerlIO_get_base(f); /* allocate via vtable */
3659 b->ptr = b->end = b->buf;
3661 if (!PerlIOValid(n)) {
3662 PerlIOBase(f)->flags |= PERLIO_F_EOF;
3666 if (PerlIO_fast_gets(n)) {
3668 * Layer below is also buffered. We do _NOT_ want to call its
3669 * ->Read() because that will loop till it gets what we asked for
3670 * which may hang on a pipe etc. Instead take anything it has to
3671 * hand, or ask it to fill _once_.
3673 avail = PerlIO_get_cnt(n);
3675 avail = PerlIO_fill(n);
3677 avail = PerlIO_get_cnt(n);
3679 if (!PerlIO_error(n) && PerlIO_eof(n))
3684 STDCHAR *ptr = PerlIO_get_ptr(n);
3685 const SSize_t cnt = avail;
3686 if (avail > (SSize_t)b->bufsiz)
3688 Copy(ptr, b->buf, avail, STDCHAR);
3689 PerlIO_set_ptrcnt(n, ptr + avail, cnt - avail);
3693 avail = PerlIO_read(n, b->ptr, b->bufsiz);
3697 PerlIOBase(f)->flags |= PERLIO_F_EOF;
3699 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
3702 b->end = b->buf + avail;
3703 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3708 PerlIOBuf_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
3710 if (PerlIOValid(f)) {
3711 const PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3714 return PerlIOBase_read(aTHX_ f, vbuf, count);
3720 PerlIOBuf_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3722 const STDCHAR *buf = (const STDCHAR *) vbuf + count;
3723 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3726 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
3731 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
3733 * Buffer is already a read buffer, we can overwrite any chars
3734 * which have been read back to buffer start
3736 avail = (b->ptr - b->buf);
3740 * Buffer is idle, set it up so whole buffer is available for
3744 b->end = b->buf + avail;
3746 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3748 * Buffer extends _back_ from where we are now
3750 b->posn -= b->bufsiz;
3752 if (avail > (SSize_t) count) {
3754 * If we have space for more than count, just move count
3762 * In simple stdio-like ungetc() case chars will be already
3765 if (buf != b->ptr) {
3766 Copy(buf, b->ptr, avail, STDCHAR);
3770 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
3774 unread += PerlIOBase_unread(aTHX_ f, vbuf, count);
3780 PerlIOBuf_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3782 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3783 const STDCHAR *buf = (const STDCHAR *) vbuf;
3784 const STDCHAR *flushptr = buf;
3788 if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
3790 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
3791 if (PerlIO_flush(f) != 0) {
3795 if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF) {
3796 flushptr = buf + count;
3797 while (flushptr > buf && *(flushptr - 1) != '\n')
3801 SSize_t avail = b->bufsiz - (b->ptr - b->buf);
3802 if ((SSize_t) count < avail)
3804 if (flushptr > buf && flushptr <= buf + avail)
3805 avail = flushptr - buf;
3806 PerlIOBase(f)->flags |= PERLIO_F_WRBUF;
3808 Copy(buf, b->ptr, avail, STDCHAR);
3813 if (buf == flushptr)
3816 if (b->ptr >= (b->buf + b->bufsiz))
3819 if (PerlIOBase(f)->flags & PERLIO_F_UNBUF)
3825 PerlIOBuf_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
3828 if ((code = PerlIO_flush(f)) == 0) {
3829 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
3830 code = PerlIO_seek(PerlIONext(f), offset, whence);
3832 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3833 b->posn = PerlIO_tell(PerlIONext(f));
3840 PerlIOBuf_tell(pTHX_ PerlIO *f)
3842 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3844 * b->posn is file position where b->buf was read, or will be written
3846 Off_t posn = b->posn;
3847 if ((PerlIOBase(f)->flags & PERLIO_F_APPEND) &&
3848 (PerlIOBase(f)->flags & PERLIO_F_WRBUF)) {
3850 /* As O_APPEND files are normally shared in some sense it is better
3855 /* when file is NOT shared then this is sufficient */
3856 PerlIO_seek(PerlIONext(f),0, SEEK_END);
3858 posn = b->posn = PerlIO_tell(PerlIONext(f));
3862 * If buffer is valid adjust position by amount in buffer
3864 posn += (b->ptr - b->buf);
3870 PerlIOBuf_popped(pTHX_ PerlIO *f)
3872 const IV code = PerlIOBase_popped(aTHX_ f);
3873 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3874 if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
3877 b->ptr = b->end = b->buf = NULL;
3878 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
3883 PerlIOBuf_close(pTHX_ PerlIO *f)
3885 const IV code = PerlIOBase_close(aTHX_ f);
3886 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3887 if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
3890 b->ptr = b->end = b->buf = NULL;
3891 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
3896 PerlIOBuf_get_ptr(pTHX_ PerlIO *f)
3898 const PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3905 PerlIOBuf_get_cnt(pTHX_ PerlIO *f)
3907 const PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3910 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF)
3911 return (b->end - b->ptr);
3916 PerlIOBuf_get_base(pTHX_ PerlIO *f)
3918 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3922 b->buf = Newxz(b->buf,b->bufsiz, STDCHAR);
3924 b->buf = (STDCHAR *) & b->oneword;
3925 b->bufsiz = sizeof(b->oneword);
3927 b->end = b->ptr = b->buf;
3933 PerlIOBuf_bufsiz(pTHX_ PerlIO *f)
3935 const PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3938 return (b->end - b->buf);
3942 PerlIOBuf_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
3944 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3948 if (PerlIO_get_cnt(f) != cnt || b->ptr < b->buf) {
3949 assert(PerlIO_get_cnt(f) == cnt);
3950 assert(b->ptr >= b->buf);
3952 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3956 PerlIOBuf_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
3958 return PerlIOBase_dup(aTHX_ f, o, param, flags);
3963 PERLIO_FUNCS_DECL(PerlIO_perlio) = {
3964 sizeof(PerlIO_funcs),
3967 PERLIO_K_BUFFERED|PERLIO_K_RAW,
3971 PerlIOBase_binmode, /* binmode */
3985 PerlIOBase_clearerr,
3986 PerlIOBase_setlinebuf,
3991 PerlIOBuf_set_ptrcnt,
3994 /*--------------------------------------------------------------------------------------*/
3996 * Temp layer to hold unread chars when cannot do it any other way
4000 PerlIOPending_fill(pTHX_ PerlIO *f)
4003 * Should never happen
4010 PerlIOPending_close(pTHX_ PerlIO *f)
4013 * A tad tricky - flush pops us, then we close new top
4016 return PerlIO_close(f);
4020 PerlIOPending_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
4023 * A tad tricky - flush pops us, then we seek new top
4026 return PerlIO_seek(f, offset, whence);
4031 PerlIOPending_flush(pTHX_ PerlIO *f)
4033 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
4034 if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
4038 PerlIO_pop(aTHX_ f);
4043 PerlIOPending_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
4049 PerlIOBuf_set_ptrcnt(aTHX_ f, ptr, cnt);
4054 PerlIOPending_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
4056 const IV code = PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
4057 PerlIOl * const l = PerlIOBase(f);
4059 * Our PerlIO_fast_gets must match what we are pushed on, or sv_gets()
4060 * etc. get muddled when it changes mid-string when we auto-pop.
4062 l->flags = (l->flags & ~(PERLIO_F_FASTGETS | PERLIO_F_UTF8)) |
4063 (PerlIOBase(PerlIONext(f))->
4064 flags & (PERLIO_F_FASTGETS | PERLIO_F_UTF8));
4069 PerlIOPending_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
4071 SSize_t avail = PerlIO_get_cnt(f);
4073 if ((SSize_t)count < avail)
4076 got = PerlIOBuf_read(aTHX_ f, vbuf, avail);
4077 if (got >= 0 && got < (SSize_t)count) {
4078 const SSize_t more =
4079 PerlIO_read(f, ((STDCHAR *) vbuf) + got, count - got);
4080 if (more >= 0 || got == 0)
4086 PERLIO_FUNCS_DECL(PerlIO_pending) = {
4087 sizeof(PerlIO_funcs),
4090 PERLIO_K_BUFFERED|PERLIO_K_RAW, /* not sure about RAW here */
4091 PerlIOPending_pushed,
4094 PerlIOBase_binmode, /* binmode */
4103 PerlIOPending_close,
4104 PerlIOPending_flush,
4108 PerlIOBase_clearerr,
4109 PerlIOBase_setlinebuf,
4114 PerlIOPending_set_ptrcnt,
4119 /*--------------------------------------------------------------------------------------*/
4121 * crlf - translation On read translate CR,LF to "\n" we do this by
4122 * overriding ptr/cnt entries to hand back a line at a time and keeping a
4123 * record of which nl we "lied" about. On write translate "\n" to CR,LF
4125 * c->nl points on the first byte of CR LF pair when it is temporarily
4126 * replaced by LF, or to the last CR of the buffer. In the former case
4127 * the caller thinks that the buffer ends at c->nl + 1, in the latter
4128 * that it ends at c->nl; these two cases can be distinguished by
4129 * *c->nl. c->nl is set during _getcnt() call, and unset during
4130 * _unread() and _flush() calls.
4131 * It only matters for read operations.
4135 PerlIOBuf base; /* PerlIOBuf stuff */
4136 STDCHAR *nl; /* Position of crlf we "lied" about in the
4141 PerlIOCrlf_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
4144 PerlIOBase(f)->flags |= PERLIO_F_CRLF;
4145 code = PerlIOBuf_pushed(aTHX_ f, mode, arg, tab);
4147 PerlIO_debug("PerlIOCrlf_pushed f=%p %s %s fl=%08" UVxf "\n",
4148 f, PerlIOBase(f)->tab->name, (mode) ? mode : "(Null)",
4149 PerlIOBase(f)->flags);
4152 /* Enable the first CRLF capable layer you can find, but if none
4153 * found, the one we just pushed is fine. This results in at
4154 * any given moment at most one CRLF-capable layer being enabled
4155 * in the whole layer stack. */
4156 PerlIO *g = PerlIONext(f);
4158 PerlIOl *b = PerlIOBase(g);
4159 if (b && b->tab == &PerlIO_crlf) {
4160 if (!(b->flags & PERLIO_F_CRLF))
4161 b->flags |= PERLIO_F_CRLF;
4162 PerlIO_pop(aTHX_ f);
4173 PerlIOCrlf_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
4175 PerlIOCrlf * const c = PerlIOSelf(f, PerlIOCrlf);
4176 if (c->nl) { /* XXXX Shouldn't it be done only if b->ptr > c->nl? */
4180 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF))
4181 return PerlIOBuf_unread(aTHX_ f, vbuf, count);
4183 const STDCHAR *buf = (const STDCHAR *) vbuf + count;
4184 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
4186 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
4191 if (!(PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
4192 b->end = b->ptr = b->buf + b->bufsiz;
4193 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
4194 b->posn -= b->bufsiz;
4196 while (count > 0 && b->ptr > b->buf) {
4197 const int ch = *--buf;
4199 if (b->ptr - 2 >= b->buf) {
4206 /* If b->ptr - 1 == b->buf, we are undoing reading 0xa */
4207 *--(b->ptr) = 0xa; /* Works even if 0xa == '\r' */
4223 /* XXXX This code assumes that buffer size >=2, but does not check it... */
4225 PerlIOCrlf_get_cnt(pTHX_ PerlIO *f)
4227 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
4230 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
4231 PerlIOCrlf * const c = PerlIOSelf(f, PerlIOCrlf);
4232 if ((PerlIOBase(f)->flags & PERLIO_F_CRLF) && (!c->nl || *c->nl == 0xd)) {
4233 STDCHAR *nl = (c->nl) ? c->nl : b->ptr;
4235 while (nl < b->end && *nl != 0xd)
4237 if (nl < b->end && *nl == 0xd) {
4239 if (nl + 1 < b->end) {
4246 * Not CR,LF but just CR
4254 * Blast - found CR as last char in buffer
4259 * They may not care, defer work as long as
4263 return (nl - b->ptr);
4267 b->ptr++; /* say we have read it as far as
4268 * flush() is concerned */
4269 b->buf++; /* Leave space in front of buffer */
4270 /* Note as we have moved buf up flush's
4272 will naturally make posn point at CR
4274 b->bufsiz--; /* Buffer is thus smaller */
4275 code = PerlIO_fill(f); /* Fetch some more */
4276 b->bufsiz++; /* Restore size for next time */
4277 b->buf--; /* Point at space */
4278 b->ptr = nl = b->buf; /* Which is what we hand
4280 *nl = 0xd; /* Fill in the CR */
4282 goto test; /* fill() call worked */
4284 * CR at EOF - just fall through
4286 /* Should we clear EOF though ??? */
4291 return (((c->nl) ? (c->nl + 1) : b->end) - b->ptr);
4297 PerlIOCrlf_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
4299 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
4300 PerlIOCrlf * const c = PerlIOSelf(f, PerlIOCrlf);
4306 if (ptr == b->end && *c->nl == 0xd) {
4307 /* Defered CR at end of buffer case - we lied about count */
4319 * Test code - delete when it works ...
4321 IV flags = PerlIOBase(f)->flags;
4322 STDCHAR *chk = (c->nl) ? (c->nl+1) : b->end;
4323 if (ptr+cnt == c->nl && c->nl+1 == b->end && *c->nl == 0xd) {
4324 /* Defered CR at end of buffer case - we lied about count */
4330 Perl_croak(aTHX_ "ptr wrong %p != %p fl=%08" UVxf
4331 " nl=%p e=%p for %d", ptr, chk, flags, c->nl,
4339 * They have taken what we lied about
4347 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
4351 PerlIOCrlf_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
4353 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF))
4354 return PerlIOBuf_write(aTHX_ f, vbuf, count);
4356 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
4357 const STDCHAR *buf = (const STDCHAR *) vbuf;
4358 const STDCHAR * const ebuf = buf + count;
4361 if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
4363 while (buf < ebuf) {
4364 const STDCHAR * const eptr = b->buf + b->bufsiz;
4365 PerlIOBase(f)->flags |= PERLIO_F_WRBUF;
4366 while (buf < ebuf && b->ptr < eptr) {
4368 if ((b->ptr + 2) > eptr) {
4376 *(b->ptr)++ = 0xd; /* CR */
4377 *(b->ptr)++ = 0xa; /* LF */
4379 if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF) {
4386 *(b->ptr)++ = *buf++;
4388 if (b->ptr >= eptr) {
4394 if (PerlIOBase(f)->flags & PERLIO_F_UNBUF)
4396 return (buf - (STDCHAR *) vbuf);
4401 PerlIOCrlf_flush(pTHX_ PerlIO *f)
4403 PerlIOCrlf * const c = PerlIOSelf(f, PerlIOCrlf);
4408 return PerlIOBuf_flush(aTHX_ f);
4412 PerlIOCrlf_binmode(pTHX_ PerlIO *f)
4414 if ((PerlIOBase(f)->flags & PERLIO_F_CRLF)) {
4415 /* In text mode - flush any pending stuff and flip it */
4416 PerlIOBase(f)->flags &= ~PERLIO_F_CRLF;
4417 #ifndef PERLIO_USING_CRLF
4418 /* CRLF is unusual case - if this is just the :crlf layer pop it */
4419 if (PerlIOBase(f)->tab == &PerlIO_crlf) {
4420 PerlIO_pop(aTHX_ f);
4427 PERLIO_FUNCS_DECL(PerlIO_crlf) = {
4428 sizeof(PerlIO_funcs),
4431 PERLIO_K_BUFFERED | PERLIO_K_CANCRLF | PERLIO_K_RAW,
4433 PerlIOBuf_popped, /* popped */
4435 PerlIOCrlf_binmode, /* binmode */
4439 PerlIOBuf_read, /* generic read works with ptr/cnt lies */
4440 PerlIOCrlf_unread, /* Put CR,LF in buffer for each '\n' */
4441 PerlIOCrlf_write, /* Put CR,LF in buffer for each '\n' */
4449 PerlIOBase_clearerr,
4450 PerlIOBase_setlinebuf,
4455 PerlIOCrlf_set_ptrcnt,
4459 /*--------------------------------------------------------------------------------------*/
4461 * mmap as "buffer" layer
4465 PerlIOBuf base; /* PerlIOBuf stuff */
4466 Mmap_t mptr; /* Mapped address */
4467 Size_t len; /* mapped length */
4468 STDCHAR *bbuf; /* malloced buffer if map fails */
4472 PerlIOMmap_map(pTHX_ PerlIO *f)
4475 PerlIOMmap * const m = PerlIOSelf(f, PerlIOMmap);
4476 const IV flags = PerlIOBase(f)->flags;
4480 if (flags & PERLIO_F_CANREAD) {
4481 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
4482 const int fd = PerlIO_fileno(f);
4484 code = Fstat(fd, &st);
4485 if (code == 0 && S_ISREG(st.st_mode)) {
4486 SSize_t len = st.st_size - b->posn;
4489 if (PL_mmap_page_size <= 0)
4490 Perl_croak(aTHX_ "panic: bad pagesize %" IVdf,
4494 * This is a hack - should never happen - open should
4497 b->posn = PerlIO_tell(PerlIONext(f));
4499 posn = (b->posn / PL_mmap_page_size) * PL_mmap_page_size;
4500 len = st.st_size - posn;
4501 m->mptr = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, posn);
4502 if (m->mptr && m->mptr != (Mmap_t) - 1) {
4503 #if 0 && defined(HAS_MADVISE) && defined(MADV_SEQUENTIAL)
4504 madvise(m->mptr, len, MADV_SEQUENTIAL);
4506 #if 0 && defined(HAS_MADVISE) && defined(MADV_WILLNEED)
4507 madvise(m->mptr, len, MADV_WILLNEED);
4509 PerlIOBase(f)->flags =
4510 (flags & ~PERLIO_F_EOF) | PERLIO_F_RDBUF;
4511 b->end = ((STDCHAR *) m->mptr) + len;
4512 b->buf = ((STDCHAR *) m->mptr) + (b->posn - posn);
4521 PerlIOBase(f)->flags =
4522 flags | PERLIO_F_EOF | PERLIO_F_RDBUF;
4524 b->ptr = b->end = b->ptr;
4533 PerlIOMmap_unmap(pTHX_ PerlIO *f)
4535 PerlIOMmap * const m = PerlIOSelf(f, PerlIOMmap);
4538 PerlIOBuf * const b = &m->base;
4540 code = munmap(m->mptr, m->len);
4544 if (PerlIO_seek(PerlIONext(f), b->posn, SEEK_SET) != 0)
4547 b->ptr = b->end = b->buf;
4548 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
4554 PerlIOMmap_get_base(pTHX_ PerlIO *f)
4556 PerlIOMmap * const m = PerlIOSelf(f, PerlIOMmap);
4557 PerlIOBuf * const b = &m->base;
4558 if (b->buf && (PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
4560 * Already have a readbuffer in progress
4566 * We have a write buffer or flushed PerlIOBuf read buffer
4568 m->bbuf = b->buf; /* save it in case we need it again */
4569 b->buf = NULL; /* Clear to trigger below */
4572 PerlIOMmap_map(aTHX_ f); /* Try and map it */
4575 * Map did not work - recover PerlIOBuf buffer if we have one
4580 b->ptr = b->end = b->buf;
4583 return PerlIOBuf_get_base(aTHX_ f);
4587 PerlIOMmap_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
4589 PerlIOMmap * const m = PerlIOSelf(f, PerlIOMmap);
4590 PerlIOBuf * const b = &m->base;
4591 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
4593 if (b->ptr && (b->ptr - count) >= b->buf
4594 && memEQ(b->ptr - count, vbuf, count)) {
4596 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
4601 * Loose the unwritable mapped buffer
4605 * If flush took the "buffer" see if we have one from before
4607 if (!b->buf && m->bbuf)
4610 PerlIOBuf_get_base(aTHX_ f);
4614 return PerlIOBuf_unread(aTHX_ f, vbuf, count);
4618 PerlIOMmap_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
4620 PerlIOMmap * const m = PerlIOSelf(f, PerlIOMmap);
4621 PerlIOBuf * const b = &m->base;
4623 if (!b->buf || !(PerlIOBase(f)->flags & PERLIO_F_WRBUF)) {
4625 * No, or wrong sort of, buffer
4628 if (PerlIOMmap_unmap(aTHX_ f) != 0)
4632 * If unmap took the "buffer" see if we have one from before
4634 if (!b->buf && m->bbuf)
4637 PerlIOBuf_get_base(aTHX_ f);
4641 return PerlIOBuf_write(aTHX_ f, vbuf, count);
4645 PerlIOMmap_flush(pTHX_ PerlIO *f)
4647 PerlIOMmap * const m = PerlIOSelf(f, PerlIOMmap);
4648 PerlIOBuf * const b = &m->base;
4649 IV code = PerlIOBuf_flush(aTHX_ f);
4651 * Now we are "synced" at PerlIOBuf level
4658 if (PerlIOMmap_unmap(aTHX_ f) != 0)
4663 * We seem to have a PerlIOBuf buffer which was not mapped
4664 * remember it in case we need one later
4673 PerlIOMmap_fill(pTHX_ PerlIO *f)
4675 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
4676 IV code = PerlIO_flush(f);
4677 if (code == 0 && !b->buf) {
4678 code = PerlIOMmap_map(aTHX_ f);
4680 if (code == 0 && !(PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
4681 code = PerlIOBuf_fill(aTHX_ f);
4687 PerlIOMmap_close(pTHX_ PerlIO *f)
4689 PerlIOMmap * const m = PerlIOSelf(f, PerlIOMmap);
4690 PerlIOBuf * const b = &m->base;
4691 IV code = PerlIO_flush(f);
4695 b->ptr = b->end = b->buf;
4697 if (PerlIOBuf_close(aTHX_ f) != 0)
4703 PerlIOMmap_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
4705 return PerlIOBase_dup(aTHX_ f, o, param, flags);
4709 PERLIO_FUNCS_DECL(PerlIO_mmap) = {
4710 sizeof(PerlIO_funcs),
4713 PERLIO_K_BUFFERED|PERLIO_K_RAW,
4717 PerlIOBase_binmode, /* binmode */
4731 PerlIOBase_clearerr,
4732 PerlIOBase_setlinebuf,
4733 PerlIOMmap_get_base,
4737 PerlIOBuf_set_ptrcnt,
4740 #endif /* HAS_MMAP */
4743 Perl_PerlIO_stdin(pTHX)
4747 PerlIO_stdstreams(aTHX);
4749 return &PL_perlio[1];
4753 Perl_PerlIO_stdout(pTHX)
4757 PerlIO_stdstreams(aTHX);
4759 return &PL_perlio[2];
4763 Perl_PerlIO_stderr(pTHX)
4767 PerlIO_stdstreams(aTHX);
4769 return &PL_perlio[3];
4772 /*--------------------------------------------------------------------------------------*/
4775 PerlIO_getname(PerlIO *f, char *buf)
4780 bool exported = FALSE;
4781 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
4783 stdio = PerlIO_exportFILE(f,0);
4787 name = fgetname(stdio, buf);
4788 if (exported) PerlIO_releaseFILE(f,stdio);
4793 PERL_UNUSED_ARG(buf);
4794 Perl_croak(aTHX_ "Don't know how to get file name");
4800 /*--------------------------------------------------------------------------------------*/
4802 * Functions which can be called on any kind of PerlIO implemented in
4806 #undef PerlIO_fdopen
4808 PerlIO_fdopen(int fd, const char *mode)
4811 return PerlIO_openn(aTHX_ Nullch, mode, fd, 0, 0, NULL, 0, NULL);
4816 PerlIO_open(const char *path, const char *mode)
4819 SV *name = sv_2mortal(newSVpv(path, 0));
4820 return PerlIO_openn(aTHX_ Nullch, mode, -1, 0, 0, NULL, 1, &name);
4823 #undef Perlio_reopen
4825 PerlIO_reopen(const char *path, const char *mode, PerlIO *f)
4828 SV *name = sv_2mortal(newSVpv(path,0));
4829 return PerlIO_openn(aTHX_ Nullch, mode, -1, 0, 0, f, 1, &name);
4834 PerlIO_getc(PerlIO *f)
4838 if ( 1 == PerlIO_read(f, buf, 1) ) {
4839 return (unsigned char) buf[0];
4844 #undef PerlIO_ungetc
4846 PerlIO_ungetc(PerlIO *f, int ch)
4851 if (PerlIO_unread(f, &buf, 1) == 1)
4859 PerlIO_putc(PerlIO *f, int ch)
4863 return PerlIO_write(f, &buf, 1);
4868 PerlIO_puts(PerlIO *f, const char *s)
4871 return PerlIO_write(f, s, strlen(s));
4874 #undef PerlIO_rewind
4876 PerlIO_rewind(PerlIO *f)
4879 PerlIO_seek(f, (Off_t) 0, SEEK_SET);
4883 #undef PerlIO_vprintf
4885 PerlIO_vprintf(PerlIO *f, const char *fmt, va_list ap)
4888 SV * const sv = newSVpvs("");
4894 Perl_va_copy(ap, apc);
4895 sv_vcatpvf(sv, fmt, &apc);
4897 sv_vcatpvf(sv, fmt, &ap);
4899 s = SvPV_const(sv, len);
4900 wrote = PerlIO_write(f, s, len);
4905 #undef PerlIO_printf
4907 PerlIO_printf(PerlIO *f, const char *fmt, ...)
4912 result = PerlIO_vprintf(f, fmt, ap);
4917 #undef PerlIO_stdoutf
4919 PerlIO_stdoutf(const char *fmt, ...)
4925 result = PerlIO_vprintf(PerlIO_stdout(), fmt, ap);
4930 #undef PerlIO_tmpfile
4932 PerlIO_tmpfile(void)
4937 const int fd = win32_tmpfd();
4939 f = PerlIO_fdopen(fd, "w+b");
4941 # if defined(HAS_MKSTEMP) && ! defined(VMS) && ! defined(OS2)
4942 SV * const sv = newSVpvs("/tmp/PerlIO_XXXXXX");
4944 * I have no idea how portable mkstemp() is ... NI-S
4946 const int fd = mkstemp(SvPVX(sv));
4948 f = PerlIO_fdopen(fd, "w+");
4950 PerlIOBase(f)->flags |= PERLIO_F_TEMP;
4951 PerlLIO_unlink(SvPVX_const(sv));
4954 # else /* !HAS_MKSTEMP, fallback to stdio tmpfile(). */
4955 FILE * const stdio = PerlSIO_tmpfile();
4958 if ((f = PerlIO_push(aTHX_(PerlIO_allocate(aTHX)),
4959 PERLIO_FUNCS_CAST(&PerlIO_stdio),
4961 PerlIOStdio * const s = PerlIOSelf(f, PerlIOStdio);
4967 # endif /* else HAS_MKSTEMP */
4968 #endif /* else WIN32 */
4975 #endif /* USE_SFIO */
4976 #endif /* PERLIO_IS_STDIO */
4978 /*======================================================================================*/
4980 * Now some functions in terms of above which may be needed even if we are
4981 * not in true PerlIO mode
4985 #undef PerlIO_setpos
4987 PerlIO_setpos(PerlIO *f, SV *pos)
4992 const Off_t * const posn = (Off_t *) SvPV(pos, len);
4993 if (f && len == sizeof(Off_t))
4994 return PerlIO_seek(f, *posn, SEEK_SET);
4996 SETERRNO(EINVAL, SS_IVCHAN);
5000 #undef PerlIO_setpos
5002 PerlIO_setpos(PerlIO *f, SV *pos)
5007 Fpos_t * const fpos = (Fpos_t *) SvPV(pos, len);
5008 if (f && len == sizeof(Fpos_t)) {
5009 #if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64)
5010 return fsetpos64(f, fpos);
5012 return fsetpos(f, fpos);
5016 SETERRNO(EINVAL, SS_IVCHAN);
5022 #undef PerlIO_getpos
5024 PerlIO_getpos(PerlIO *f, SV *pos)
5027 Off_t posn = PerlIO_tell(f);
5028 sv_setpvn(pos, (char *) &posn, sizeof(posn));
5029 return (posn == (Off_t) - 1) ? -1 : 0;
5032 #undef PerlIO_getpos
5034 PerlIO_getpos(PerlIO *f, SV *pos)
5039 #if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64)
5040 code = fgetpos64(f, &fpos);
5042 code = fgetpos(f, &fpos);
5044 sv_setpvn(pos, (char *) &fpos, sizeof(fpos));
5049 #if (defined(PERLIO_IS_STDIO) || !defined(USE_SFIO)) && !defined(HAS_VPRINTF)
5052 vprintf(char *pat, char *args)
5054 _doprnt(pat, args, stdout);
5055 return 0; /* wrong, but perl doesn't use the return
5060 vfprintf(FILE *fd, char *pat, char *args)
5062 _doprnt(pat, args, fd);
5063 return 0; /* wrong, but perl doesn't use the return
5069 #ifndef PerlIO_vsprintf
5071 PerlIO_vsprintf(char *s, int n, const char *fmt, va_list ap)
5074 const int val = vsprintf(s, fmt, ap);
5076 if (strlen(s) >= (STRLEN) n) {
5078 (void) PerlIO_puts(Perl_error_log,
5079 "panic: sprintf overflow - memory corrupted!\n");
5087 #ifndef PerlIO_sprintf
5089 PerlIO_sprintf(char *s, int n, const char *fmt, ...)
5094 result = PerlIO_vsprintf(s, n, fmt, ap);
5102 * c-indentation-style: bsd
5104 * indent-tabs-mode: t
5107 * ex: set ts=8 sts=4 sw=4 noet: