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
60 /* Missing proto on LynxOS */
64 /* Call the callback or PerlIOBase, and return failure. */
65 #define Perl_PerlIO_or_Base(f, callback, base, failure, args) \
66 if (PerlIOValid(f)) { \
67 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;\
68 if (tab && tab->callback) \
69 return (*tab->callback) args; \
71 return PerlIOBase_ ## base args; \
74 SETERRNO(EBADF, SS_IVCHAN); \
77 /* Call the callback or fail, and return failure. */
78 #define Perl_PerlIO_or_fail(f, callback, failure, args) \
79 if (PerlIOValid(f)) { \
80 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;\
81 if (tab && tab->callback) \
82 return (*tab->callback) args; \
83 SETERRNO(EINVAL, LIB_INVARG); \
86 SETERRNO(EBADF, SS_IVCHAN); \
89 /* Call the callback or PerlIOBase, and be void. */
90 #define Perl_PerlIO_or_Base_void(f, callback, base, args) \
91 if (PerlIOValid(f)) { \
92 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;\
93 if (tab && tab->callback) \
94 (*tab->callback) args; \
96 PerlIOBase_ ## base args; \
99 SETERRNO(EBADF, SS_IVCHAN)
101 /* Call the callback or fail, and be void. */
102 #define Perl_PerlIO_or_fail_void(f, callback, args) \
103 if (PerlIOValid(f)) { \
104 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;\
105 if (tab && tab->callback) \
106 (*tab->callback) args; \
108 SETERRNO(EINVAL, LIB_INVARG); \
111 SETERRNO(EBADF, SS_IVCHAN)
115 perlsio_binmode(FILE *fp, int iotype, int mode)
118 * This used to be contents of do_binmode in doio.c
121 # if defined(atarist) || defined(__MINT__)
124 ((FILE *) fp)->_flag |= _IOBIN;
126 ((FILE *) fp)->_flag &= ~_IOBIN;
133 if (PerlLIO_setmode(fp, mode) != -1) {
135 if (PerlLIO_setmode(fileno(fp), mode) != -1) {
137 # if defined(WIN32) && defined(__BORLANDC__)
139 * The translation mode of the stream is maintained independent
141 * the translation mode of the fd in the Borland RTL (heavy
142 * digging through their runtime sources reveal). User has to
144 * the mode explicitly for the stream (though they don't
146 * this anywhere). GSAR 97-5-24
152 fp->flags &= ~_F_BIN;
160 # if defined(USEMYBINMODE)
162 if (my_binmode(fp, iotype, mode) != FALSE)
168 PERL_UNUSED_ARG(iotype);
169 PERL_UNUSED_ARG(mode);
177 #define O_ACCMODE 3 /* Assume traditional implementation */
181 PerlIO_intmode2str(int rawmode, char *mode, int *writing)
183 const int result = rawmode & O_ACCMODE;
188 ptype = IoTYPE_RDONLY;
191 ptype = IoTYPE_WRONLY;
199 *writing = (result != O_RDONLY);
201 if (result == O_RDONLY) {
205 else if (rawmode & O_APPEND) {
207 if (result != O_WRONLY)
212 if (result == O_WRONLY)
219 if (rawmode & O_BINARY)
225 #ifndef PERLIO_LAYERS
227 PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names)
229 if (!names || !*names
230 || strEQ(names, ":crlf")
231 || strEQ(names, ":raw")
232 || strEQ(names, ":bytes")
236 Perl_croak(aTHX_ "Cannot apply \"%s\" in non-PerlIO perl", names);
244 PerlIO_destruct(pTHX)
249 PerlIO_binmode(pTHX_ PerlIO *fp, int iotype, int mode, const char *names)
252 PERL_UNUSED_ARG(iotype);
253 PERL_UNUSED_ARG(mode);
254 PERL_UNUSED_ARG(names);
257 return perlsio_binmode(fp, iotype, mode);
262 PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
264 #if defined(PERL_MICRO) || defined(__SYMBIAN32__)
267 #ifdef PERL_IMPLICIT_SYS
268 return PerlSIO_fdupopen(f);
271 return win32_fdupopen(f);
274 const int fd = PerlLIO_dup(PerlIO_fileno(f));
278 const int omode = djgpp_get_stream_mode(f);
280 const int omode = fcntl(fd, F_GETFL);
282 PerlIO_intmode2str(omode,mode,NULL);
283 /* the r+ is a hack */
284 return PerlIO_fdopen(fd, mode);
289 SETERRNO(EBADF, SS_IVCHAN);
299 * De-mux PerlIO_openn() into fdopen, freopen and fopen type entries
303 PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd,
304 int imode, int perm, PerlIO *old, int narg, SV **args)
308 Perl_croak(aTHX_ "More than one argument to open");
310 if (*args == &PL_sv_undef)
311 return PerlIO_tmpfile();
313 const char *name = SvPV_nolen_const(*args);
314 if (*mode == IoTYPE_NUMERIC) {
315 fd = PerlLIO_open3(name, imode, perm);
317 return PerlIO_fdopen(fd, mode + 1);
320 return PerlIO_reopen(name, mode, old);
323 return PerlIO_open(name, mode);
328 return PerlIO_fdopen(fd, (char *) mode);
333 XS(XS_PerlIO__Layer__find)
337 Perl_croak(aTHX_ "Usage class->find(name[,load])");
339 const char * const name = SvPV_nolen_const(ST(1));
340 ST(0) = (strEQ(name, "crlf")
341 || strEQ(name, "raw")) ? &PL_sv_yes : &PL_sv_undef;
348 Perl_boot_core_PerlIO(pTHX)
350 newXS("PerlIO::Layer::find", XS_PerlIO__Layer__find, __FILE__);
356 #ifdef PERLIO_IS_STDIO
362 * Does nothing (yet) except force this file to be included in perl
363 * binary. That allows this file to force inclusion of other functions
364 * that may be required by loadable extensions e.g. for
365 * FileHandle::tmpfile
369 #undef PerlIO_tmpfile
376 #else /* PERLIO_IS_STDIO */
384 * This section is just to make sure these functions get pulled in from
388 #undef PerlIO_tmpfile
399 * Force this file to be included in perl binary. Which allows this
400 * file to force inclusion of other functions that may be required by
401 * loadable extensions e.g. for FileHandle::tmpfile
405 * Hack sfio does its own 'autoflush' on stdout in common cases. Flush
406 * results in a lot of lseek()s to regular files and lot of small
409 sfset(sfstdout, SF_SHARE, 0);
412 /* This is not the reverse of PerlIO_exportFILE(), PerlIO_releaseFILE() is. */
414 PerlIO_importFILE(FILE *stdio, const char *mode)
416 const int fd = fileno(stdio);
417 if (!mode || !*mode) {
420 return PerlIO_fdopen(fd, mode);
424 PerlIO_findFILE(PerlIO *pio)
426 const int fd = PerlIO_fileno(pio);
427 FILE * const f = fdopen(fd, "r+");
429 if (!f && errno == EINVAL)
431 if (!f && errno == EINVAL)
438 /*======================================================================================*/
440 * Implement all the PerlIO interface ourselves.
446 * We _MUST_ have <unistd.h> if we are using lseek() and may have large
453 #include <sys/mman.h>
457 PerlIO_debug(const char *fmt, ...)
462 if (!PL_perlio_debug_fd && !PL_tainting && PL_uid == PL_euid && PL_gid == PL_egid) {
463 const char * const s = PerlEnv_getenv("PERLIO_DEBUG");
465 PL_perlio_debug_fd = PerlLIO_open3(s, O_WRONLY | O_CREAT | O_APPEND, 0666);
467 PL_perlio_debug_fd = -1;
469 if (PL_perlio_debug_fd > 0) {
472 const char * const s = CopFILE(PL_curcop);
473 /* Use fixed buffer as sv_catpvf etc. needs SVs */
475 const STRLEN len = my_sprintf(buffer, "%.40s:%" IVdf " ", s ? s : "(none)", (IV) CopLINE(PL_curcop));
476 const STRLEN len2 = vsprintf(buffer+len, fmt, ap);
477 PerlLIO_write(PL_perlio_debug_fd, buffer, len + len2);
479 const char *s = CopFILE(PL_curcop);
481 SV * const sv = newSVpvs("");
482 Perl_sv_catpvf(aTHX_ sv, "%s:%" IVdf " ", s ? s : "(none)",
483 (IV) CopLINE(PL_curcop));
484 Perl_sv_vcatpvf(aTHX_ sv, fmt, &ap);
486 s = SvPV_const(sv, len);
487 PerlLIO_write(PL_perlio_debug_fd, s, len);
494 /*--------------------------------------------------------------------------------------*/
497 * Inner level routines
501 * Table of pointers to the PerlIO structs (malloc'ed)
503 #define PERLIO_TABLE_SIZE 64
506 PerlIO_allocate(pTHX)
510 * Find a free slot in the table, allocating new table as necessary
515 while ((f = *last)) {
517 last = (PerlIO **) (f);
518 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
524 Newxz(f,PERLIO_TABLE_SIZE,PerlIO);
532 #undef PerlIO_fdupopen
534 PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
536 if (PerlIOValid(f)) {
537 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
538 PerlIO_debug("fdupopen f=%p param=%p\n",(void*)f,(void*)param);
540 return (*tab->Dup)(aTHX_ PerlIO_allocate(aTHX), f, param, flags);
542 return PerlIOBase_dup(aTHX_ PerlIO_allocate(aTHX), f, param, flags);
546 SETERRNO(EBADF, SS_IVCHAN);
552 PerlIO_cleantable(pTHX_ PerlIO **tablep)
554 PerlIO * const table = *tablep;
557 PerlIO_cleantable(aTHX_(PerlIO **) & (table[0]));
558 for (i = PERLIO_TABLE_SIZE - 1; i > 0; i--) {
559 PerlIO * const f = table + i;
571 PerlIO_list_alloc(pTHX)
574 Newxz(list, 1, PerlIO_list_t);
580 PerlIO_list_free(pTHX_ PerlIO_list_t *list)
583 if (--list->refcnt == 0) {
586 for (i = 0; i < list->cur; i++) {
587 if (list->array[i].arg)
588 SvREFCNT_dec(list->array[i].arg);
590 Safefree(list->array);
598 PerlIO_list_push(pTHX_ PerlIO_list_t *list, PerlIO_funcs *funcs, SV *arg)
602 if (list->cur >= list->len) {
605 Renew(list->array, list->len, PerlIO_pair_t);
607 Newx(list->array, list->len, PerlIO_pair_t);
609 p = &(list->array[list->cur++]);
611 if ((p->arg = arg)) {
612 (void)SvREFCNT_inc(arg);
617 PerlIO_clone_list(pTHX_ PerlIO_list_t *proto, CLONE_PARAMS *param)
619 PerlIO_list_t *list = (PerlIO_list_t *) NULL;
622 list = PerlIO_list_alloc(aTHX);
623 for (i=0; i < proto->cur; i++) {
625 if (proto->array[i].arg)
626 arg = PerlIO_sv_dup(aTHX_ proto->array[i].arg,param);
627 PerlIO_list_push(aTHX_ list, proto->array[i].funcs, arg);
634 PerlIO_clone(pTHX_ PerlInterpreter *proto, CLONE_PARAMS *param)
637 PerlIO **table = &proto->Iperlio;
640 PL_known_layers = PerlIO_clone_list(aTHX_ proto->Iknown_layers, param);
641 PL_def_layerlist = PerlIO_clone_list(aTHX_ proto->Idef_layerlist, param);
642 PerlIO_allocate(aTHX); /* root slot is never used */
643 PerlIO_debug("Clone %p from %p\n",aTHX,proto);
644 while ((f = *table)) {
646 table = (PerlIO **) (f++);
647 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
649 (void) fp_dup(f, 0, param);
655 PERL_UNUSED_ARG(proto);
656 PERL_UNUSED_ARG(param);
661 PerlIO_destruct(pTHX)
664 PerlIO **table = &PL_perlio;
667 PerlIO_debug("Destruct %p\n",aTHX);
669 while ((f = *table)) {
671 table = (PerlIO **) (f++);
672 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
676 if (l->tab->kind & PERLIO_K_DESTRUCT) {
677 PerlIO_debug("Destruct popping %s\n", l->tab->name);
691 PerlIO_pop(pTHX_ PerlIO *f)
693 const PerlIOl *l = *f;
695 PerlIO_debug("PerlIO_pop f=%p %s\n", (void*)f, l->tab->name);
696 if (l->tab->Popped) {
698 * If popped returns non-zero do not free its layer structure
699 * it has either done so itself, or it is shared and still in
702 if ((*l->tab->Popped) (aTHX_ f) != 0)
710 /* Return as an array the stack of layers on a filehandle. Note that
711 * the stack is returned top-first in the array, and there are three
712 * times as many array elements as there are layers in the stack: the
713 * first element of a layer triplet is the name, the second one is the
714 * arguments, and the third one is the flags. */
717 PerlIO_get_layers(pTHX_ PerlIO *f)
720 AV * const av = newAV();
722 if (PerlIOValid(f)) {
723 PerlIOl *l = PerlIOBase(f);
726 SV * const name = l->tab && l->tab->name ?
727 newSVpv(l->tab->name, 0) : &PL_sv_undef;
728 SV * const arg = l->tab && l->tab->Getarg ?
729 (*l->tab->Getarg)(aTHX_ &l, 0, 0) : &PL_sv_undef;
732 av_push(av, newSViv((IV)l->flags));
740 /*--------------------------------------------------------------------------------------*/
742 * XS Interface for perl code
746 PerlIO_find_layer(pTHX_ const char *name, STRLEN len, int load)
750 if ((SSize_t) len <= 0)
752 for (i = 0; i < PL_known_layers->cur; i++) {
753 PerlIO_funcs * const f = PL_known_layers->array[i].funcs;
754 if (memEQ(f->name, name, len) && f->name[len] == 0) {
755 PerlIO_debug("%.*s => %p\n", (int) len, name, (void*)f);
759 if (load && PL_subname && PL_def_layerlist
760 && PL_def_layerlist->cur >= 2) {
761 if (PL_in_load_module) {
762 Perl_croak(aTHX_ "Recursive call to Perl_load_module in PerlIO_find_layer");
765 SV * const pkgsv = newSVpvs("PerlIO");
766 SV * const layer = newSVpvn(name, len);
767 CV * const cv = get_cv("PerlIO::Layer::NoWarnings", FALSE);
769 SAVEINT(PL_in_load_module);
771 SAVEGENERICSV(PL_warnhook);
772 (void)SvREFCNT_inc(cv);
773 PL_warnhook = (SV *) cv;
777 * The two SVs are magically freed by load_module
779 Perl_load_module(aTHX_ 0, pkgsv, NULL, layer, NULL);
782 return PerlIO_find_layer(aTHX_ name, len, 0);
785 PerlIO_debug("Cannot find %.*s\n", (int) len, name);
789 #ifdef USE_ATTRIBUTES_FOR_PERLIO
792 perlio_mg_set(pTHX_ SV *sv, MAGIC *mg)
795 IO * const io = GvIOn((GV *) SvRV(sv));
796 PerlIO * const ifp = IoIFP(io);
797 PerlIO * const ofp = IoOFP(io);
798 Perl_warn(aTHX_ "set %" SVf " %p %p %p", sv, io, ifp, ofp);
804 perlio_mg_get(pTHX_ SV *sv, MAGIC *mg)
807 IO * const io = GvIOn((GV *) SvRV(sv));
808 PerlIO * const ifp = IoIFP(io);
809 PerlIO * const ofp = IoOFP(io);
810 Perl_warn(aTHX_ "get %" SVf " %p %p %p", sv, io, ifp, ofp);
816 perlio_mg_clear(pTHX_ SV *sv, MAGIC *mg)
818 Perl_warn(aTHX_ "clear %" SVf, sv);
823 perlio_mg_free(pTHX_ SV *sv, MAGIC *mg)
825 Perl_warn(aTHX_ "free %" SVf, sv);
829 MGVTBL perlio_vtab = {
837 XS(XS_io_MODIFY_SCALAR_ATTRIBUTES)
840 SV * const sv = SvRV(ST(1));
841 AV * const av = newAV();
845 sv_magic(sv, (SV *) av, PERL_MAGIC_ext, NULL, 0);
847 mg = mg_find(sv, PERL_MAGIC_ext);
848 mg->mg_virtual = &perlio_vtab;
850 Perl_warn(aTHX_ "attrib %" SVf, sv);
851 for (i = 2; i < items; i++) {
853 const char * const name = SvPV_const(ST(i), len);
854 SV * const layer = PerlIO_find_layer(aTHX_ name, len, 1);
856 av_push(av, SvREFCNT_inc(layer));
867 #endif /* USE_ATTIBUTES_FOR_PERLIO */
870 PerlIO_tab_sv(pTHX_ PerlIO_funcs *tab)
872 HV * const stash = gv_stashpvs("PerlIO::Layer", TRUE);
873 SV * const sv = sv_bless(newRV_noinc(newSViv(PTR2IV(tab))), stash);
877 XS(XS_PerlIO__Layer__NoWarnings)
879 /* This is used as a %SIG{__WARN__} handler to supress warnings
880 during loading of layers.
885 PerlIO_debug("warning:%s\n",SvPV_nolen_const(ST(0)));
889 XS(XS_PerlIO__Layer__find)
894 Perl_croak(aTHX_ "Usage class->find(name[,load])");
897 const char * const name = SvPV_const(ST(1), len);
898 const bool load = (items > 2) ? SvTRUE(ST(2)) : 0;
899 PerlIO_funcs * const layer = PerlIO_find_layer(aTHX_ name, len, load);
901 (layer) ? sv_2mortal(PerlIO_tab_sv(aTHX_ layer)) :
908 PerlIO_define_layer(pTHX_ PerlIO_funcs *tab)
911 if (!PL_known_layers)
912 PL_known_layers = PerlIO_list_alloc(aTHX);
913 PerlIO_list_push(aTHX_ PL_known_layers, tab, NULL);
914 PerlIO_debug("define %s %p\n", tab->name, (void*)tab);
918 PerlIO_parse_layers(pTHX_ PerlIO_list_t *av, const char *names)
922 const char *s = names;
924 while (isSPACE(*s) || *s == ':')
929 const char *as = NULL;
931 if (!isIDFIRST(*s)) {
933 * Message is consistent with how attribute lists are
934 * passed. Even though this means "foo : : bar" is
935 * seen as an invalid separator character.
937 const char q = ((*s == '\'') ? '"' : '\'');
938 if (ckWARN(WARN_LAYER))
939 Perl_warner(aTHX_ packWARN(WARN_LAYER),
940 "Invalid separator character %c%c%c in PerlIO layer specification %s",
942 SETERRNO(EINVAL, LIB_INVARG);
947 } while (isALNUM(*e));
963 * It's a nul terminated string, not allowed
964 * to \ the terminating null. Anything other
965 * character is passed over.
975 if (ckWARN(WARN_LAYER))
976 Perl_warner(aTHX_ packWARN(WARN_LAYER),
977 "Argument list not closed for PerlIO layer \"%.*s\"",
989 PerlIO_funcs * const layer =
990 PerlIO_find_layer(aTHX_ s, llen, 1);
992 PerlIO_list_push(aTHX_ av, layer,
998 if (ckWARN(WARN_LAYER))
999 Perl_warner(aTHX_ packWARN(WARN_LAYER), "Unknown PerlIO layer \"%.*s\"",
1012 PerlIO_default_buffer(pTHX_ PerlIO_list_t *av)
1015 PERLIO_FUNCS_DECL(*tab) = &PerlIO_perlio;
1016 #ifdef PERLIO_USING_CRLF
1019 if (PerlIO_stdio.Set_ptrcnt)
1020 tab = &PerlIO_stdio;
1022 PerlIO_debug("Pushing %s\n", tab->name);
1023 PerlIO_list_push(aTHX_ av, PerlIO_find_layer(aTHX_ tab->name, 0, 0),
1028 PerlIO_arg_fetch(PerlIO_list_t *av, IV n)
1030 return av->array[n].arg;
1034 PerlIO_layer_fetch(pTHX_ PerlIO_list_t *av, IV n, PerlIO_funcs *def)
1036 if (n >= 0 && n < av->cur) {
1037 PerlIO_debug("Layer %" IVdf " is %s\n", n,
1038 av->array[n].funcs->name);
1039 return av->array[n].funcs;
1042 Perl_croak(aTHX_ "panic: PerlIO layer array corrupt");
1047 PerlIOPop_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
1049 PERL_UNUSED_ARG(mode);
1050 PERL_UNUSED_ARG(arg);
1051 PERL_UNUSED_ARG(tab);
1052 if (PerlIOValid(f)) {
1054 PerlIO_pop(aTHX_ f);
1060 PERLIO_FUNCS_DECL(PerlIO_remove) = {
1061 sizeof(PerlIO_funcs),
1064 PERLIO_K_DUMMY | PERLIO_K_UTF8,
1084 NULL, /* get_base */
1085 NULL, /* get_bufsiz */
1088 NULL, /* set_ptrcnt */
1092 PerlIO_default_layers(pTHX)
1095 if (!PL_def_layerlist) {
1096 const char * const s = (PL_tainting) ? NULL : PerlEnv_getenv("PERLIO");
1097 PERLIO_FUNCS_DECL(*osLayer) = &PerlIO_unix;
1098 PL_def_layerlist = PerlIO_list_alloc(aTHX);
1099 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_unix));
1101 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_win32));
1103 osLayer = &PerlIO_win32;
1106 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_raw));
1107 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_perlio));
1108 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_stdio));
1109 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_crlf));
1111 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_mmap));
1113 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_utf8));
1114 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_remove));
1115 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_byte));
1116 PerlIO_list_push(aTHX_ PL_def_layerlist,
1117 PerlIO_find_layer(aTHX_ osLayer->name, 0, 0),
1120 PerlIO_parse_layers(aTHX_ PL_def_layerlist, s);
1123 PerlIO_default_buffer(aTHX_ PL_def_layerlist);
1126 if (PL_def_layerlist->cur < 2) {
1127 PerlIO_default_buffer(aTHX_ PL_def_layerlist);
1129 return PL_def_layerlist;
1133 Perl_boot_core_PerlIO(pTHX)
1135 #ifdef USE_ATTRIBUTES_FOR_PERLIO
1136 newXS("io::MODIFY_SCALAR_ATTRIBUTES", XS_io_MODIFY_SCALAR_ATTRIBUTES,
1139 newXS("PerlIO::Layer::find", XS_PerlIO__Layer__find, __FILE__);
1140 newXS("PerlIO::Layer::NoWarnings", XS_PerlIO__Layer__NoWarnings, __FILE__);
1144 PerlIO_default_layer(pTHX_ I32 n)
1147 PerlIO_list_t * const av = PerlIO_default_layers(aTHX);
1150 return PerlIO_layer_fetch(aTHX_ av, n, PERLIO_FUNCS_CAST(&PerlIO_stdio));
1153 #define PerlIO_default_top() PerlIO_default_layer(aTHX_ -1)
1154 #define PerlIO_default_btm() PerlIO_default_layer(aTHX_ 0)
1157 PerlIO_stdstreams(pTHX)
1161 PerlIO_allocate(aTHX);
1162 PerlIO_fdopen(0, "Ir" PERLIO_STDTEXT);
1163 PerlIO_fdopen(1, "Iw" PERLIO_STDTEXT);
1164 PerlIO_fdopen(2, "Iw" PERLIO_STDTEXT);
1169 PerlIO_push(pTHX_ PerlIO *f, PERLIO_FUNCS_DECL(*tab), const char *mode, SV *arg)
1171 if (tab->fsize != sizeof(PerlIO_funcs)) {
1173 Perl_croak(aTHX_ "Layer does not match this perl");
1177 if (tab->size < sizeof(PerlIOl)) {
1180 /* Real layer with a data area */
1181 Newxc(l,tab->size,char,PerlIOl);
1183 Zero(l, tab->size, char);
1185 l->tab = (PerlIO_funcs*) tab;
1187 PerlIO_debug("PerlIO_push f=%p %s %s %p\n", (void*)f, tab->name,
1188 (mode) ? mode : "(Null)", (void*)arg);
1189 if (*l->tab->Pushed &&
1190 (*l->tab->Pushed) (aTHX_ f, mode, arg, (PerlIO_funcs*) tab) != 0) {
1191 PerlIO_pop(aTHX_ f);
1197 /* Pseudo-layer where push does its own stack adjust */
1198 PerlIO_debug("PerlIO_push f=%p %s %s %p\n", (void*)f, tab->name,
1199 (mode) ? mode : "(Null)", (void*)arg);
1201 (*tab->Pushed) (aTHX_ f, mode, arg, (PerlIO_funcs*) tab) != 0) {
1209 PerlIOBase_binmode(pTHX_ PerlIO *f)
1211 if (PerlIOValid(f)) {
1212 /* Is layer suitable for raw stream ? */
1213 if (PerlIOBase(f)->tab->kind & PERLIO_K_RAW) {
1214 /* Yes - turn off UTF-8-ness, to undo UTF-8 locale effects */
1215 PerlIOBase(f)->flags &= ~PERLIO_F_UTF8;
1218 /* Not suitable - pop it */
1219 PerlIO_pop(aTHX_ f);
1227 PerlIORaw_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
1229 PERL_UNUSED_ARG(mode);
1230 PERL_UNUSED_ARG(arg);
1231 PERL_UNUSED_ARG(tab);
1233 if (PerlIOValid(f)) {
1238 * Strip all layers that are not suitable for a raw stream
1241 while (t && (l = *t)) {
1242 if (l->tab->Binmode) {
1243 /* Has a handler - normal case */
1244 if ((*l->tab->Binmode)(aTHX_ f) == 0) {
1246 /* Layer still there - move down a layer */
1255 /* No handler - pop it */
1256 PerlIO_pop(aTHX_ t);
1259 if (PerlIOValid(f)) {
1260 PerlIO_debug(":raw f=%p :%s\n", (void*)f, PerlIOBase(f)->tab->name);
1268 PerlIO_apply_layera(pTHX_ PerlIO *f, const char *mode,
1269 PerlIO_list_t *layers, IV n, IV max)
1273 PerlIO_funcs * const tab = PerlIO_layer_fetch(aTHX_ layers, n, NULL);
1275 if (!PerlIO_push(aTHX_ f, tab, mode, PerlIOArg)) {
1286 PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names)
1290 PerlIO_list_t * const layers = PerlIO_list_alloc(aTHX);
1291 code = PerlIO_parse_layers(aTHX_ layers, names);
1293 code = PerlIO_apply_layera(aTHX_ f, mode, layers, 0, layers->cur);
1295 PerlIO_list_free(aTHX_ layers);
1301 /*--------------------------------------------------------------------------------------*/
1303 * Given the abstraction above the public API functions
1307 PerlIO_binmode(pTHX_ PerlIO *f, int iotype, int mode, const char *names)
1309 PerlIO_debug("PerlIO_binmode f=%p %s %c %x %s\n", (void*)f,
1310 (PerlIOBase(f)) ? PerlIOBase(f)->tab->name : "(Null)",
1311 iotype, mode, (names) ? names : "(Null)");
1314 /* Do not flush etc. if (e.g.) switching encodings.
1315 if a pushed layer knows it needs to flush lower layers
1316 (for example :unix which is never going to call them)
1317 it can do the flush when it is pushed.
1319 return PerlIO_apply_layers(aTHX_ f, NULL, names) == 0 ? TRUE : FALSE;
1322 /* Fake 5.6 legacy of using this call to turn ON O_TEXT */
1323 #ifdef PERLIO_USING_CRLF
1324 /* Legacy binmode only has meaning if O_TEXT has a value distinct from
1325 O_BINARY so we can look for it in mode.
1327 if (!(mode & O_BINARY)) {
1329 /* FIXME?: Looking down the layer stack seems wrong,
1330 but is a way of reaching past (say) an encoding layer
1331 to flip CRLF-ness of the layer(s) below
1334 /* Perhaps we should turn on bottom-most aware layer
1335 e.g. Ilya's idea that UNIX TTY could serve
1337 if (PerlIOBase(f)->tab->kind & PERLIO_K_CANCRLF) {
1338 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF)) {
1339 /* Not in text mode - flush any pending stuff and flip it */
1341 PerlIOBase(f)->flags |= PERLIO_F_CRLF;
1343 /* Only need to turn it on in one layer so we are done */
1348 /* Not finding a CRLF aware layer presumably means we are binary
1349 which is not what was requested - so we failed
1350 We _could_ push :crlf layer but so could caller
1355 /* Legacy binmode is now _defined_ as being equivalent to pushing :raw
1356 So code that used to be here is now in PerlIORaw_pushed().
1358 return PerlIO_push(aTHX_ f, PERLIO_FUNCS_CAST(&PerlIO_raw), NULL, NULL) ? TRUE : FALSE;
1363 PerlIO__close(pTHX_ PerlIO *f)
1365 if (PerlIOValid(f)) {
1366 PerlIO_funcs * const tab = PerlIOBase(f)->tab;
1367 if (tab && tab->Close)
1368 return (*tab->Close)(aTHX_ f);
1370 return PerlIOBase_close(aTHX_ f);
1373 SETERRNO(EBADF, SS_IVCHAN);
1379 Perl_PerlIO_close(pTHX_ PerlIO *f)
1381 const int code = PerlIO__close(aTHX_ f);
1382 while (PerlIOValid(f)) {
1383 PerlIO_pop(aTHX_ f);
1389 Perl_PerlIO_fileno(pTHX_ PerlIO *f)
1392 Perl_PerlIO_or_Base(f, Fileno, fileno, -1, (aTHX_ f));
1396 PerlIO_context_layers(pTHX_ const char *mode)
1399 const char *type = NULL;
1401 * Need to supply default layer info from open.pm
1404 SV * const layers = PL_curcop->cop_io;
1407 type = SvPV_const(layers, len);
1408 if (type && mode[0] != 'r') {
1410 * Skip to write part
1412 const char * const s = strchr(type, 0);
1413 if (s && (STRLEN)(s - type) < len) {
1422 static PerlIO_funcs *
1423 PerlIO_layer_from_ref(pTHX_ SV *sv)
1427 * For any scalar type load the handler which is bundled with perl
1429 if (SvTYPE(sv) < SVt_PVAV)
1430 return PerlIO_find_layer(aTHX_ STR_WITH_LEN("scalar"), 1);
1433 * For other types allow if layer is known but don't try and load it
1435 switch (SvTYPE(sv)) {
1437 return PerlIO_find_layer(aTHX_ STR_WITH_LEN("Array"), 0);
1439 return PerlIO_find_layer(aTHX_ STR_WITH_LEN("Hash"), 0);
1441 return PerlIO_find_layer(aTHX_ STR_WITH_LEN("Code"), 0);
1443 return PerlIO_find_layer(aTHX_ STR_WITH_LEN("Glob"), 0);
1449 PerlIO_resolve_layers(pTHX_ const char *layers,
1450 const char *mode, int narg, SV **args)
1453 PerlIO_list_t *def = PerlIO_default_layers(aTHX);
1456 PerlIO_stdstreams(aTHX);
1458 SV * const arg = *args;
1460 * If it is a reference but not an object see if we have a handler
1463 if (SvROK(arg) && !sv_isobject(arg)) {
1464 PerlIO_funcs * const handler = PerlIO_layer_from_ref(aTHX_ SvRV(arg));
1466 def = PerlIO_list_alloc(aTHX);
1467 PerlIO_list_push(aTHX_ def, handler, &PL_sv_undef);
1471 * Don't fail if handler cannot be found :via(...) etc. may do
1472 * something sensible else we will just stringfy and open
1477 if (!layers || !*layers)
1478 layers = PerlIO_context_layers(aTHX_ mode);
1479 if (layers && *layers) {
1483 av = PerlIO_list_alloc(aTHX);
1484 for (i = 0; i < def->cur; i++) {
1485 PerlIO_list_push(aTHX_ av, def->array[i].funcs,
1492 if (PerlIO_parse_layers(aTHX_ av, layers) == 0) {
1496 PerlIO_list_free(aTHX_ av);
1497 return (PerlIO_list_t *) NULL;
1508 PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd,
1509 int imode, int perm, PerlIO *f, int narg, SV **args)
1512 if (!f && narg == 1 && *args == &PL_sv_undef) {
1513 if ((f = PerlIO_tmpfile())) {
1514 if (!layers || !*layers)
1515 layers = PerlIO_context_layers(aTHX_ mode);
1516 if (layers && *layers)
1517 PerlIO_apply_layers(aTHX_ f, mode, layers);
1521 PerlIO_list_t *layera;
1523 PerlIO_funcs *tab = NULL;
1524 if (PerlIOValid(f)) {
1526 * This is "reopen" - it is not tested as perl does not use it
1530 layera = PerlIO_list_alloc(aTHX);
1532 SV * const arg = (l->tab->Getarg)
1533 ? (*l->tab->Getarg) (aTHX_ &l, NULL, 0)
1535 PerlIO_list_push(aTHX_ layera, l->tab, arg);
1536 l = *PerlIONext(&l);
1540 layera = PerlIO_resolve_layers(aTHX_ layers, mode, narg, args);
1546 * Start at "top" of layer stack
1548 n = layera->cur - 1;
1550 PerlIO_funcs * const t = PerlIO_layer_fetch(aTHX_ layera, n, NULL);
1559 * Found that layer 'n' can do opens - call it
1561 if (narg > 1 && !(tab->kind & PERLIO_K_MULTIARG)) {
1562 Perl_croak(aTHX_ "More than one argument to open(,':%s')",tab->name);
1564 PerlIO_debug("openn(%s,'%s','%s',%d,%x,%o,%p,%d,%p)\n",
1565 tab->name, layers ? layers : "(Null)", mode, fd,
1566 imode, perm, (void*)f, narg, (void*)args);
1568 f = (*tab->Open) (aTHX_ tab, layera, n, mode, fd, imode, perm,
1571 SETERRNO(EINVAL, LIB_INVARG);
1575 if (n + 1 < layera->cur) {
1577 * More layers above the one that we used to open -
1580 if (PerlIO_apply_layera(aTHX_ f, mode, layera, n + 1, layera->cur) != 0) {
1581 /* If pushing layers fails close the file */
1588 PerlIO_list_free(aTHX_ layera);
1595 Perl_PerlIO_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
1597 Perl_PerlIO_or_Base(f, Read, read, -1, (aTHX_ f, vbuf, count));
1601 Perl_PerlIO_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
1603 Perl_PerlIO_or_Base(f, Unread, unread, -1, (aTHX_ f, vbuf, count));
1607 Perl_PerlIO_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
1609 Perl_PerlIO_or_fail(f, Write, -1, (aTHX_ f, vbuf, count));
1613 Perl_PerlIO_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
1615 Perl_PerlIO_or_fail(f, Seek, -1, (aTHX_ f, offset, whence));
1619 Perl_PerlIO_tell(pTHX_ PerlIO *f)
1621 Perl_PerlIO_or_fail(f, Tell, -1, (aTHX_ f));
1625 Perl_PerlIO_flush(pTHX_ PerlIO *f)
1630 const PerlIO_funcs *tab = PerlIOBase(f)->tab;
1632 if (tab && tab->Flush)
1633 return (*tab->Flush) (aTHX_ f);
1635 return 0; /* If no Flush defined, silently succeed. */
1638 PerlIO_debug("Cannot flush f=%p\n", (void*)f);
1639 SETERRNO(EBADF, SS_IVCHAN);
1645 * Is it good API design to do flush-all on NULL, a potentially
1646 * errorneous input? Maybe some magical value (PerlIO*
1647 * PERLIO_FLUSH_ALL = (PerlIO*)-1;)? Yes, stdio does similar
1648 * things on fflush(NULL), but should we be bound by their design
1651 PerlIO **table = &PL_perlio;
1653 while ((f = *table)) {
1655 table = (PerlIO **) (f++);
1656 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
1657 if (*f && PerlIO_flush(f) != 0)
1667 PerlIOBase_flush_linebuf(pTHX)
1670 PerlIO **table = &PL_perlio;
1672 while ((f = *table)) {
1674 table = (PerlIO **) (f++);
1675 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
1678 flags & (PERLIO_F_LINEBUF | PERLIO_F_CANWRITE))
1679 == (PERLIO_F_LINEBUF | PERLIO_F_CANWRITE))
1687 Perl_PerlIO_fill(pTHX_ PerlIO *f)
1689 Perl_PerlIO_or_fail(f, Fill, -1, (aTHX_ f));
1693 PerlIO_isutf8(PerlIO *f)
1696 return (PerlIOBase(f)->flags & PERLIO_F_UTF8) != 0;
1698 SETERRNO(EBADF, SS_IVCHAN);
1704 Perl_PerlIO_eof(pTHX_ PerlIO *f)
1706 Perl_PerlIO_or_Base(f, Eof, eof, -1, (aTHX_ f));
1710 Perl_PerlIO_error(pTHX_ PerlIO *f)
1712 Perl_PerlIO_or_Base(f, Error, error, -1, (aTHX_ f));
1716 Perl_PerlIO_clearerr(pTHX_ PerlIO *f)
1718 Perl_PerlIO_or_Base_void(f, Clearerr, clearerr, (aTHX_ f));
1722 Perl_PerlIO_setlinebuf(pTHX_ PerlIO *f)
1724 Perl_PerlIO_or_Base_void(f, Setlinebuf, setlinebuf, (aTHX_ f));
1728 PerlIO_has_base(PerlIO *f)
1730 if (PerlIOValid(f)) {
1731 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
1734 return (tab->Get_base != NULL);
1735 SETERRNO(EINVAL, LIB_INVARG);
1738 SETERRNO(EBADF, SS_IVCHAN);
1744 PerlIO_fast_gets(PerlIO *f)
1746 if (PerlIOValid(f) && (PerlIOBase(f)->flags & PERLIO_F_FASTGETS)) {
1747 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
1750 return (tab->Set_ptrcnt != NULL);
1751 SETERRNO(EINVAL, LIB_INVARG);
1754 SETERRNO(EBADF, SS_IVCHAN);
1760 PerlIO_has_cntptr(PerlIO *f)
1762 if (PerlIOValid(f)) {
1763 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
1766 return (tab->Get_ptr != NULL && tab->Get_cnt != NULL);
1767 SETERRNO(EINVAL, LIB_INVARG);
1770 SETERRNO(EBADF, SS_IVCHAN);
1776 PerlIO_canset_cnt(PerlIO *f)
1778 if (PerlIOValid(f)) {
1779 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
1782 return (tab->Set_ptrcnt != NULL);
1783 SETERRNO(EINVAL, LIB_INVARG);
1786 SETERRNO(EBADF, SS_IVCHAN);
1792 Perl_PerlIO_get_base(pTHX_ PerlIO *f)
1794 Perl_PerlIO_or_fail(f, Get_base, NULL, (aTHX_ f));
1798 Perl_PerlIO_get_bufsiz(pTHX_ PerlIO *f)
1800 Perl_PerlIO_or_fail(f, Get_bufsiz, -1, (aTHX_ f));
1804 Perl_PerlIO_get_ptr(pTHX_ PerlIO *f)
1806 Perl_PerlIO_or_fail(f, Get_ptr, NULL, (aTHX_ f));
1810 Perl_PerlIO_get_cnt(pTHX_ PerlIO *f)
1812 Perl_PerlIO_or_fail(f, Get_cnt, -1, (aTHX_ f));
1816 Perl_PerlIO_set_cnt(pTHX_ PerlIO *f, int cnt)
1818 Perl_PerlIO_or_fail_void(f, Set_ptrcnt, (aTHX_ f, NULL, cnt));
1822 Perl_PerlIO_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, int cnt)
1824 Perl_PerlIO_or_fail_void(f, Set_ptrcnt, (aTHX_ f, ptr, cnt));
1828 /*--------------------------------------------------------------------------------------*/
1830 * utf8 and raw dummy layers
1834 PerlIOUtf8_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
1836 PERL_UNUSED_ARG(mode);
1837 PERL_UNUSED_ARG(arg);
1838 if (PerlIOValid(f)) {
1839 if (tab->kind & PERLIO_K_UTF8)
1840 PerlIOBase(f)->flags |= PERLIO_F_UTF8;
1842 PerlIOBase(f)->flags &= ~PERLIO_F_UTF8;
1848 PERLIO_FUNCS_DECL(PerlIO_utf8) = {
1849 sizeof(PerlIO_funcs),
1852 PERLIO_K_DUMMY | PERLIO_K_UTF8,
1872 NULL, /* get_base */
1873 NULL, /* get_bufsiz */
1876 NULL, /* set_ptrcnt */
1879 PERLIO_FUNCS_DECL(PerlIO_byte) = {
1880 sizeof(PerlIO_funcs),
1903 NULL, /* get_base */
1904 NULL, /* get_bufsiz */
1907 NULL, /* set_ptrcnt */
1911 PerlIORaw_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
1912 IV n, const char *mode, int fd, int imode, int perm,
1913 PerlIO *old, int narg, SV **args)
1915 PerlIO_funcs * const tab = PerlIO_default_btm();
1916 PERL_UNUSED_ARG(self);
1917 if (tab && tab->Open)
1918 return (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
1920 SETERRNO(EINVAL, LIB_INVARG);
1924 PERLIO_FUNCS_DECL(PerlIO_raw) = {
1925 sizeof(PerlIO_funcs),
1948 NULL, /* get_base */
1949 NULL, /* get_bufsiz */
1952 NULL, /* set_ptrcnt */
1954 /*--------------------------------------------------------------------------------------*/
1955 /*--------------------------------------------------------------------------------------*/
1957 * "Methods" of the "base class"
1961 PerlIOBase_fileno(pTHX_ PerlIO *f)
1963 return PerlIOValid(f) ? PerlIO_fileno(PerlIONext(f)) : -1;
1967 PerlIO_modestr(PerlIO * f, char *buf)
1970 if (PerlIOValid(f)) {
1971 const IV flags = PerlIOBase(f)->flags;
1972 if (flags & PERLIO_F_APPEND) {
1974 if (flags & PERLIO_F_CANREAD) {
1978 else if (flags & PERLIO_F_CANREAD) {
1980 if (flags & PERLIO_F_CANWRITE)
1983 else if (flags & PERLIO_F_CANWRITE) {
1985 if (flags & PERLIO_F_CANREAD) {
1989 #ifdef PERLIO_USING_CRLF
1990 if (!(flags & PERLIO_F_CRLF))
2000 PerlIOBase_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
2002 PerlIOl * const l = PerlIOBase(f);
2003 PERL_UNUSED_ARG(arg);
2005 l->flags &= ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE |
2006 PERLIO_F_TRUNCATE | PERLIO_F_APPEND);
2007 if (tab->Set_ptrcnt != NULL)
2008 l->flags |= PERLIO_F_FASTGETS;
2010 if (*mode == IoTYPE_NUMERIC || *mode == IoTYPE_IMPLICIT)
2014 l->flags |= PERLIO_F_CANREAD;
2017 l->flags |= PERLIO_F_APPEND | PERLIO_F_CANWRITE;
2020 l->flags |= PERLIO_F_TRUNCATE | PERLIO_F_CANWRITE;
2023 SETERRNO(EINVAL, LIB_INVARG);
2029 l->flags |= PERLIO_F_CANREAD | PERLIO_F_CANWRITE;
2032 l->flags &= ~PERLIO_F_CRLF;
2035 l->flags |= PERLIO_F_CRLF;
2038 SETERRNO(EINVAL, LIB_INVARG);
2045 l->flags |= l->next->flags &
2046 (PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_TRUNCATE |
2051 PerlIO_debug("PerlIOBase_pushed f=%p %s %s fl=%08" UVxf " (%s)\n",
2052 f, PerlIOBase(f)->tab->name, (omode) ? omode : "(Null)",
2053 l->flags, PerlIO_modestr(f, temp));
2059 PerlIOBase_popped(pTHX_ PerlIO *f)
2066 PerlIOBase_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
2069 * Save the position as current head considers it
2071 const Off_t old = PerlIO_tell(f);
2072 PerlIO_push(aTHX_ f, PERLIO_FUNCS_CAST(&PerlIO_pending), "r", NULL);
2073 PerlIOSelf(f, PerlIOBuf)->posn = old;
2074 return PerlIOBuf_unread(aTHX_ f, vbuf, count);
2078 PerlIOBase_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
2080 STDCHAR *buf = (STDCHAR *) vbuf;
2082 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD)) {
2083 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2084 SETERRNO(EBADF, SS_IVCHAN);
2090 SSize_t avail = PerlIO_get_cnt(f);
2093 take = ((SSize_t)count < avail) ? count : avail;
2095 STDCHAR *ptr = PerlIO_get_ptr(f);
2096 Copy(ptr, buf, take, STDCHAR);
2097 PerlIO_set_ptrcnt(f, ptr + take, (avail -= take));
2100 if (avail == 0) /* set_ptrcnt could have reset avail */
2103 if (count > 0 && avail <= 0) {
2104 if (PerlIO_fill(f) != 0)
2109 return (buf - (STDCHAR *) vbuf);
2115 PerlIOBase_noop_ok(pTHX_ PerlIO *f)
2122 PerlIOBase_noop_fail(pTHX_ PerlIO *f)
2129 PerlIOBase_close(pTHX_ PerlIO *f)
2132 if (PerlIOValid(f)) {
2133 PerlIO *n = PerlIONext(f);
2134 code = PerlIO_flush(f);
2135 PerlIOBase(f)->flags &=
2136 ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_OPEN);
2137 while (PerlIOValid(n)) {
2138 const PerlIO_funcs * const tab = PerlIOBase(n)->tab;
2139 if (tab && tab->Close) {
2140 if ((*tab->Close)(aTHX_ n) != 0)
2145 PerlIOBase(n)->flags &=
2146 ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_OPEN);
2152 SETERRNO(EBADF, SS_IVCHAN);
2158 PerlIOBase_eof(pTHX_ PerlIO *f)
2160 if (PerlIOValid(f)) {
2161 return (PerlIOBase(f)->flags & PERLIO_F_EOF) != 0;
2167 PerlIOBase_error(pTHX_ PerlIO *f)
2169 if (PerlIOValid(f)) {
2170 return (PerlIOBase(f)->flags & PERLIO_F_ERROR) != 0;
2176 PerlIOBase_clearerr(pTHX_ PerlIO *f)
2178 if (PerlIOValid(f)) {
2179 PerlIO * const n = PerlIONext(f);
2180 PerlIOBase(f)->flags &= ~(PERLIO_F_ERROR | PERLIO_F_EOF);
2187 PerlIOBase_setlinebuf(pTHX_ PerlIO *f)
2189 if (PerlIOValid(f)) {
2190 PerlIOBase(f)->flags |= PERLIO_F_LINEBUF;
2195 PerlIO_sv_dup(pTHX_ SV *arg, CLONE_PARAMS *param)
2201 return sv_dup(arg, param);
2204 return newSVsv(arg);
2207 PERL_UNUSED_ARG(param);
2208 return newSVsv(arg);
2213 PerlIOBase_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
2215 PerlIO * const nexto = PerlIONext(o);
2216 if (PerlIOValid(nexto)) {
2217 const PerlIO_funcs * const tab = PerlIOBase(nexto)->tab;
2218 if (tab && tab->Dup)
2219 f = (*tab->Dup)(aTHX_ f, nexto, param, flags);
2221 f = PerlIOBase_dup(aTHX_ f, nexto, param, flags);
2224 PerlIO_funcs * const self = PerlIOBase(o)->tab;
2227 PerlIO_debug("PerlIOBase_dup %s f=%p o=%p param=%p\n",
2228 self->name, (void*)f, (void*)o, (void*)param);
2230 arg = (*self->Getarg)(aTHX_ o, param, flags);
2234 f = PerlIO_push(aTHX_ f, self, PerlIO_modestr(o,buf), arg);
2243 perl_mutex PerlIO_mutex;
2246 /* PL_perlio_fd_refcnt[] is in intrpvar.h */
2248 /* Must be called with PerlIO_mutex locked. */
2250 S_more_refcounted_fds(pTHX_ const int new_fd) {
2251 const int old_max = PL_perlio_fd_refcnt_size;
2252 const int new_max = 16 + (new_fd & ~15);
2255 PerlIO_debug("More fds - old=%d, need %d, new=%d\n",
2256 old_max, new_fd, new_max);
2258 if (new_fd < old_max) {
2262 assert (new_max > new_fd);
2265 = PerlMemShared_realloc(PL_perlio_fd_refcnt, new_max * sizeof(int));
2269 MUTEX_UNLOCK(&PerlIO_mutex);
2271 /* Can't use PerlIO to write as it allocates memory */
2272 PerlLIO_write(PerlIO_fileno(Perl_error_log),
2273 PL_no_mem, strlen(PL_no_mem));
2277 PL_perlio_fd_refcnt_size = new_max;
2278 PL_perlio_fd_refcnt = new_array;
2280 PerlIO_debug("Zeroing %p, %d\n", new_array + old_max, new_max - old_max);
2282 Zero(new_array + old_max, new_max - old_max, int);
2289 /* Place holder for stdstreams call ??? */
2291 MUTEX_INIT(&PerlIO_mutex);
2296 PerlIOUnix_refcnt_inc(int fd)
2303 MUTEX_LOCK(&PerlIO_mutex);
2305 if (fd >= PL_perlio_fd_refcnt_size)
2306 S_more_refcounted_fds(aTHX_ fd);
2308 PL_perlio_fd_refcnt[fd]++;
2309 PerlIO_debug("fd %d refcnt=%d\n",fd,PL_perlio_fd_refcnt[fd]);
2312 MUTEX_UNLOCK(&PerlIO_mutex);
2318 PerlIOUnix_refcnt_dec(int fd)
2325 MUTEX_LOCK(&PerlIO_mutex);
2327 /* XXX should this be a panic? */
2328 if (fd >= PL_perlio_fd_refcnt_size)
2329 S_more_refcounted_fds(aTHX_ fd);
2331 /* XXX should this be a panic if it drops below 0? */
2332 cnt = --PL_perlio_fd_refcnt[fd];
2333 PerlIO_debug("fd %d refcnt=%d\n",fd,cnt);
2335 MUTEX_UNLOCK(&PerlIO_mutex);
2342 PerlIO_cleanup(pTHX)
2347 PerlIO_debug("Cleanup layers for %p\n",aTHX);
2349 PerlIO_debug("Cleanup layers\n");
2351 /* Raise STDIN..STDERR refcount so we don't close them */
2352 for (i=0; i < 3; i++)
2353 PerlIOUnix_refcnt_inc(i);
2354 PerlIO_cleantable(aTHX_ &PL_perlio);
2355 /* Restore STDIN..STDERR refcount */
2356 for (i=0; i < 3; i++)
2357 PerlIOUnix_refcnt_dec(i);
2359 if (PL_known_layers) {
2360 PerlIO_list_free(aTHX_ PL_known_layers);
2361 PL_known_layers = NULL;
2363 if (PL_def_layerlist) {
2364 PerlIO_list_free(aTHX_ PL_def_layerlist);
2365 PL_def_layerlist = NULL;
2371 /*--------------------------------------------------------------------------------------*/
2373 * Bottom-most level for UNIX-like case
2377 struct _PerlIO base; /* The generic part */
2378 int fd; /* UNIX like file descriptor */
2379 int oflags; /* open/fcntl flags */
2383 PerlIOUnix_oflags(const char *mode)
2386 if (*mode == IoTYPE_IMPLICIT || *mode == IoTYPE_NUMERIC)
2391 if (*++mode == '+') {
2398 oflags = O_CREAT | O_TRUNC;
2399 if (*++mode == '+') {
2408 oflags = O_CREAT | O_APPEND;
2409 if (*++mode == '+') {
2422 else if (*mode == 't') {
2424 oflags &= ~O_BINARY;
2428 * Always open in binary mode
2431 if (*mode || oflags == -1) {
2432 SETERRNO(EINVAL, LIB_INVARG);
2439 PerlIOUnix_fileno(pTHX_ PerlIO *f)
2441 return PerlIOSelf(f, PerlIOUnix)->fd;
2445 PerlIOUnix_setfd(pTHX_ PerlIO *f, int fd, int imode)
2447 PerlIOUnix * const s = PerlIOSelf(f, PerlIOUnix);
2450 if (PerlLIO_fstat(fd, &st) == 0) {
2451 if (!S_ISREG(st.st_mode)) {
2452 PerlIO_debug("%d is not regular file\n",fd);
2453 PerlIOBase(f)->flags |= PERLIO_F_NOTREG;
2456 PerlIO_debug("%d _is_ a regular file\n",fd);
2462 PerlIOUnix_refcnt_inc(fd);
2466 PerlIOUnix_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
2468 IV code = PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
2469 if (*PerlIONext(f)) {
2470 /* We never call down so do any pending stuff now */
2471 PerlIO_flush(PerlIONext(f));
2473 * XXX could (or should) we retrieve the oflags from the open file
2474 * handle rather than believing the "mode" we are passed in? XXX
2475 * Should the value on NULL mode be 0 or -1?
2477 PerlIOUnix_setfd(aTHX_ f, PerlIO_fileno(PerlIONext(f)),
2478 mode ? PerlIOUnix_oflags(mode) : -1);
2480 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
2486 PerlIOUnix_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
2488 const int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2490 if (PerlIOBase(f)->flags & PERLIO_F_NOTREG) {
2492 SETERRNO(ESPIPE, LIB_INVARG);
2494 SETERRNO(EINVAL, LIB_INVARG);
2498 new_loc = PerlLIO_lseek(fd, offset, whence);
2499 if (new_loc == (Off_t) - 1)
2501 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
2506 PerlIOUnix_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2507 IV n, const char *mode, int fd, int imode,
2508 int perm, PerlIO *f, int narg, SV **args)
2510 if (PerlIOValid(f)) {
2511 if (PerlIOBase(f)->flags & PERLIO_F_OPEN)
2512 (*PerlIOBase(f)->tab->Close)(aTHX_ f);
2515 if (*mode == IoTYPE_NUMERIC)
2518 imode = PerlIOUnix_oflags(mode);
2522 const char *path = SvPV_nolen_const(*args);
2523 fd = PerlLIO_open3(path, imode, perm);
2527 if (*mode == IoTYPE_IMPLICIT)
2530 f = PerlIO_allocate(aTHX);
2532 if (!PerlIOValid(f)) {
2533 if (!(f = PerlIO_push(aTHX_ f, self, mode, PerlIOArg))) {
2537 PerlIOUnix_setfd(aTHX_ f, fd, imode);
2538 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
2539 if (*mode == IoTYPE_APPEND)
2540 PerlIOUnix_seek(aTHX_ f, 0, SEEK_END);
2547 * FIXME: pop layers ???
2555 PerlIOUnix_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
2557 const PerlIOUnix * const os = PerlIOSelf(o, PerlIOUnix);
2559 if (flags & PERLIO_DUP_FD) {
2560 fd = PerlLIO_dup(fd);
2563 f = PerlIOBase_dup(aTHX_ f, o, param, flags);
2565 /* If all went well overwrite fd in dup'ed lay with the dup()'ed fd */
2566 PerlIOUnix_setfd(aTHX_ f, fd, os->oflags);
2575 PerlIOUnix_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
2578 const int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2579 #ifdef PERLIO_STD_SPECIAL
2581 return PERLIO_STD_IN(fd, vbuf, count);
2583 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD) ||
2584 PerlIOBase(f)->flags & (PERLIO_F_EOF|PERLIO_F_ERROR)) {
2588 const SSize_t len = PerlLIO_read(fd, vbuf, count);
2589 if (len >= 0 || errno != EINTR) {
2591 if (errno != EAGAIN) {
2592 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2595 else if (len == 0 && count != 0) {
2596 PerlIOBase(f)->flags |= PERLIO_F_EOF;
2607 PerlIOUnix_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
2610 const int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2611 #ifdef PERLIO_STD_SPECIAL
2612 if (fd == 1 || fd == 2)
2613 return PERLIO_STD_OUT(fd, vbuf, count);
2616 const SSize_t len = PerlLIO_write(fd, vbuf, count);
2617 if (len >= 0 || errno != EINTR) {
2619 if (errno != EAGAIN) {
2620 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2631 PerlIOUnix_tell(pTHX_ PerlIO *f)
2633 return PerlLIO_lseek(PerlIOSelf(f, PerlIOUnix)->fd, 0, SEEK_CUR);
2638 PerlIOUnix_close(pTHX_ PerlIO *f)
2641 const int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2643 if (PerlIOBase(f)->flags & PERLIO_F_OPEN) {
2644 if (PerlIOUnix_refcnt_dec(fd) > 0) {
2645 PerlIOBase(f)->flags &= ~PERLIO_F_OPEN;
2650 SETERRNO(EBADF,SS_IVCHAN);
2653 while (PerlLIO_close(fd) != 0) {
2654 if (errno != EINTR) {
2661 PerlIOBase(f)->flags &= ~PERLIO_F_OPEN;
2666 PERLIO_FUNCS_DECL(PerlIO_unix) = {
2667 sizeof(PerlIO_funcs),
2674 PerlIOBase_binmode, /* binmode */
2684 PerlIOBase_noop_ok, /* flush */
2685 PerlIOBase_noop_fail, /* fill */
2688 PerlIOBase_clearerr,
2689 PerlIOBase_setlinebuf,
2690 NULL, /* get_base */
2691 NULL, /* get_bufsiz */
2694 NULL, /* set_ptrcnt */
2697 /*--------------------------------------------------------------------------------------*/
2702 #if defined(VMS) && !defined(STDIO_BUFFER_WRITABLE)
2703 /* perl5.8 - This ensures the last minute VMS ungetc fix is not
2704 broken by the last second glibc 2.3 fix
2706 #define STDIO_BUFFER_WRITABLE
2711 struct _PerlIO base;
2712 FILE *stdio; /* The stream */
2716 PerlIOStdio_fileno(pTHX_ PerlIO *f)
2718 if (PerlIOValid(f)) {
2719 FILE * const s = PerlIOSelf(f, PerlIOStdio)->stdio;
2721 return PerlSIO_fileno(s);
2728 PerlIOStdio_mode(const char *mode, char *tmode)
2730 char * const ret = tmode;
2736 #if defined(PERLIO_USING_CRLF) || defined(__CYGWIN__)
2744 PerlIOStdio_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
2747 if (PerlIOValid(f) && PerlIOValid(n = PerlIONext(f))) {
2748 PerlIO_funcs * const toptab = PerlIOBase(n)->tab;
2749 if (toptab == tab) {
2750 /* Top is already stdio - pop self (duplicate) and use original */
2751 PerlIO_pop(aTHX_ f);
2754 const int fd = PerlIO_fileno(n);
2757 if (fd >= 0 && (stdio = PerlSIO_fdopen(fd,
2758 mode = PerlIOStdio_mode(mode, tmode)))) {
2759 PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
2760 /* We never call down so do any pending stuff now */
2761 PerlIO_flush(PerlIONext(f));
2768 return PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
2773 PerlIO_importFILE(FILE *stdio, const char *mode)
2779 if (!mode || !*mode) {
2780 /* We need to probe to see how we can open the stream
2781 so start with read/write and then try write and read
2782 we dup() so that we can fclose without loosing the fd.
2784 Note that the errno value set by a failing fdopen
2785 varies between stdio implementations.
2787 const int fd = PerlLIO_dup(fileno(stdio));
2788 FILE *f2 = PerlSIO_fdopen(fd, (mode = "r+"));
2790 f2 = PerlSIO_fdopen(fd, (mode = "w"));
2793 f2 = PerlSIO_fdopen(fd, (mode = "r"));
2796 /* Don't seem to be able to open */
2802 if ((f = PerlIO_push(aTHX_(f = PerlIO_allocate(aTHX)), PERLIO_FUNCS_CAST(&PerlIO_stdio), mode, NULL))) {
2803 s = PerlIOSelf(f, PerlIOStdio);
2811 PerlIOStdio_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2812 IV n, const char *mode, int fd, int imode,
2813 int perm, PerlIO *f, int narg, SV **args)
2816 if (PerlIOValid(f)) {
2817 const char * const path = SvPV_nolen_const(*args);
2818 PerlIOStdio * const s = PerlIOSelf(f, PerlIOStdio);
2820 PerlIOUnix_refcnt_dec(fileno(s->stdio));
2821 stdio = PerlSIO_freopen(path, (mode = PerlIOStdio_mode(mode, tmode)),
2826 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2831 const char * const path = SvPV_nolen_const(*args);
2832 if (*mode == IoTYPE_NUMERIC) {
2834 fd = PerlLIO_open3(path, imode, perm);
2838 bool appended = FALSE;
2840 /* Cygwin wants its 'b' early. */
2842 mode = PerlIOStdio_mode(mode, tmode);
2844 stdio = PerlSIO_fopen(path, mode);
2848 f = PerlIO_allocate(aTHX);
2851 mode = PerlIOStdio_mode(mode, tmode);
2852 f = PerlIO_push(aTHX_ f, self, mode, PerlIOArg);
2854 s = PerlIOSelf(f, PerlIOStdio);
2856 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2868 if (*mode == IoTYPE_IMPLICIT) {
2875 stdio = PerlSIO_stdin;
2878 stdio = PerlSIO_stdout;
2881 stdio = PerlSIO_stderr;
2886 stdio = PerlSIO_fdopen(fd, mode =
2887 PerlIOStdio_mode(mode, tmode));
2891 f = PerlIO_allocate(aTHX);
2893 if ((f = PerlIO_push(aTHX_ f, self, mode, PerlIOArg))) {
2894 PerlIOStdio * const s = PerlIOSelf(f, PerlIOStdio);
2896 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2906 PerlIOStdio_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
2908 /* This assumes no layers underneath - which is what
2909 happens, but is not how I remember it. NI-S 2001/10/16
2911 if ((f = PerlIOBase_dup(aTHX_ f, o, param, flags))) {
2912 FILE *stdio = PerlIOSelf(o, PerlIOStdio)->stdio;
2913 const int fd = fileno(stdio);
2915 if (flags & PERLIO_DUP_FD) {
2916 const int dfd = PerlLIO_dup(fileno(stdio));
2918 stdio = PerlSIO_fdopen(dfd, PerlIO_modestr(o,mode));
2923 /* FIXME: To avoid messy error recovery if dup fails
2924 re-use the existing stdio as though flag was not set
2928 stdio = PerlSIO_fdopen(fd, PerlIO_modestr(o,mode));
2930 PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
2931 PerlIOUnix_refcnt_inc(fileno(stdio));
2937 PerlIOStdio_invalidate_fileno(pTHX_ FILE *f)
2939 /* XXX this could use PerlIO_canset_fileno() and
2940 * PerlIO_set_fileno() support from Configure
2942 # if defined(__UCLIBC__)
2943 /* uClibc must come before glibc because it defines __GLIBC__ as well. */
2946 # elif defined(__GLIBC__)
2947 /* There may be a better way for GLIBC:
2948 - libio.h defines a flag to not close() on cleanup
2952 # elif defined(__sun__)
2954 /* On solaris, if _LP64 is defined, the FILE structure is this:
2960 * It turns out that the fd is stored in the top 32 bits of
2961 * file->__pad[4]. The lower 32 bits contain flags. file->pad[5] appears
2962 * to contain a pointer or offset into another structure. All the
2963 * remaining fields are zero.
2965 * We set the top bits to -1 (0xFFFFFFFF).
2967 f->__pad[4] |= 0xffffffff00000000L;
2968 assert(fileno(f) == 0xffffffff);
2969 # else /* !defined(_LP64) */
2970 /* _file is just a unsigned char :-(
2971 Not clear why we dup() rather than using -1
2972 even if that would be treated as 0xFF - so will
2975 f->_file = PerlLIO_dup(fileno(f));
2976 # endif /* defined(_LP64) */
2978 # elif defined(__hpux)
2982 /* Next one ->_file seems to be a reasonable fallback, i.e. if
2983 your platform does not have special entry try this one.
2984 [For OSF only have confirmation for Tru64 (alpha)
2985 but assume other OSFs will be similar.]
2987 # elif defined(_AIX) || defined(__osf__) || defined(__irix__)
2990 # elif defined(__FreeBSD__)
2991 /* There may be a better way on FreeBSD:
2992 - we could insert a dummy func in the _close function entry
2993 f->_close = (int (*)(void *)) dummy_close;
2997 # elif defined(__OpenBSD__)
2998 /* There may be a better way on OpenBSD:
2999 - we could insert a dummy func in the _close function entry
3000 f->_close = (int (*)(void *)) dummy_close;
3004 # elif defined(__EMX__)
3005 /* f->_flags &= ~_IOOPEN; */ /* Will leak stream->_buffer */
3008 # elif defined(__CYGWIN__)
3009 /* There may be a better way on CYGWIN:
3010 - we could insert a dummy func in the _close function entry
3011 f->_close = (int (*)(void *)) dummy_close;
3015 # elif defined(WIN32)
3016 # if defined(__BORLANDC__)
3017 f->fd = PerlLIO_dup(fileno(f));
3018 # elif defined(UNDER_CE)
3019 /* WIN_CE does not have access to FILE internals, it hardly has FILE
3028 /* Sarathy's code did this - we fall back to a dup/dup2 hack
3029 (which isn't thread safe) instead
3031 # error "Don't know how to set FILE.fileno on your platform"
3039 PerlIOStdio_close(pTHX_ PerlIO *f)
3041 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3047 const int fd = fileno(stdio);
3053 #ifdef SOCKS5_VERSION_NAME
3054 /* Socks lib overrides close() but stdio isn't linked to
3055 that library (though we are) - so we must call close()
3056 on sockets on stdio's behalf.
3059 Sock_size_t optlen = sizeof(int);
3060 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, (void *) &optval, &optlen) == 0) {
3065 if (PerlIOUnix_refcnt_dec(fd) > 0) {
3066 /* File descriptor still in use */
3071 /* For STD* handles don't close the stdio at all
3072 this is because we have shared the FILE * too
3074 if (stdio == stdin) {
3075 /* Some stdios are buggy fflush-ing inputs */
3078 else if (stdio == stdout || stdio == stderr) {
3079 return PerlIO_flush(f);
3081 /* Tricky - must fclose(stdio) to free memory but not close(fd)
3082 Use Sarathy's trick from maint-5.6 to invalidate the
3083 fileno slot of the FILE *
3085 result = PerlIO_flush(f);
3087 if (!(invalidate = PerlIOStdio_invalidate_fileno(aTHX_ stdio))) {
3088 dupfd = PerlLIO_dup(fd);
3091 result = PerlSIO_fclose(stdio);
3092 /* We treat error from stdio as success if we invalidated
3093 errno may NOT be expected EBADF
3095 if (invalidate && result != 0) {
3100 /* in SOCKS case let close() determine return value */
3104 PerlLIO_dup2(dupfd,fd);
3105 PerlLIO_close(dupfd);
3112 PerlIOStdio_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
3115 FILE * const s = PerlIOSelf(f, PerlIOStdio)->stdio;
3119 STDCHAR *buf = (STDCHAR *) vbuf;
3121 * Perl is expecting PerlIO_getc() to fill the buffer Linux's
3122 * stdio does not do that for fread()
3124 const int ch = PerlSIO_fgetc(s);
3131 got = PerlSIO_fread(vbuf, 1, count, s);
3132 if (got == 0 && PerlSIO_ferror(s))
3134 if (got >= 0 || errno != EINTR)
3137 SETERRNO(0,0); /* just in case */
3143 PerlIOStdio_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3146 FILE * const s = PerlIOSelf(f, PerlIOStdio)->stdio;
3148 #ifdef STDIO_BUFFER_WRITABLE
3149 if (PerlIO_fast_gets(f) && PerlIO_has_base(f)) {
3150 STDCHAR *buf = ((STDCHAR *) vbuf) + count;
3151 STDCHAR *base = PerlIO_get_base(f);
3152 SSize_t cnt = PerlIO_get_cnt(f);
3153 STDCHAR *ptr = PerlIO_get_ptr(f);
3154 SSize_t avail = ptr - base;
3156 if (avail > count) {
3160 Move(buf-avail,ptr,avail,STDCHAR);
3163 PerlIO_set_ptrcnt(f,ptr,cnt+avail);
3164 if (PerlSIO_feof(s) && unread >= 0)
3165 PerlSIO_clearerr(s);
3170 if (PerlIO_has_cntptr(f)) {
3171 /* We can get pointer to buffer but not its base
3172 Do ungetc() but check chars are ending up in the
3175 STDCHAR *eptr = (STDCHAR*)PerlSIO_get_ptr(s);
3176 STDCHAR *buf = ((STDCHAR *) vbuf) + count;
3178 const int ch = *--buf & 0xFF;
3179 if (ungetc(ch,s) != ch) {
3180 /* ungetc did not work */
3183 if ((STDCHAR*)PerlSIO_get_ptr(s) != --eptr || ((*eptr & 0xFF) != ch)) {
3184 /* Did not change pointer as expected */
3185 fgetc(s); /* get char back again */
3195 unread += PerlIOBase_unread(aTHX_ f, vbuf, count);
3201 PerlIOStdio_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3206 got = PerlSIO_fwrite(vbuf, 1, count,
3207 PerlIOSelf(f, PerlIOStdio)->stdio);
3208 if (got >= 0 || errno != EINTR)
3211 SETERRNO(0,0); /* just in case */
3217 PerlIOStdio_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
3219 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3220 return PerlSIO_fseek(stdio, offset, whence);
3224 PerlIOStdio_tell(pTHX_ PerlIO *f)
3226 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3227 return PerlSIO_ftell(stdio);
3231 PerlIOStdio_flush(pTHX_ PerlIO *f)
3233 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3234 if (PerlIOBase(f)->flags & PERLIO_F_CANWRITE) {
3235 return PerlSIO_fflush(stdio);
3241 * FIXME: This discards ungetc() and pre-read stuff which is not
3242 * right if this is just a "sync" from a layer above Suspect right
3243 * design is to do _this_ but not have layer above flush this
3244 * layer read-to-read
3247 * Not writeable - sync by attempting a seek
3249 const int err = errno;
3250 if (PerlSIO_fseek(stdio, (Off_t) 0, SEEK_CUR) != 0)
3258 PerlIOStdio_eof(pTHX_ PerlIO *f)
3260 return PerlSIO_feof(PerlIOSelf(f, PerlIOStdio)->stdio);
3264 PerlIOStdio_error(pTHX_ PerlIO *f)
3266 return PerlSIO_ferror(PerlIOSelf(f, PerlIOStdio)->stdio);
3270 PerlIOStdio_clearerr(pTHX_ PerlIO *f)
3272 PerlSIO_clearerr(PerlIOSelf(f, PerlIOStdio)->stdio);
3276 PerlIOStdio_setlinebuf(pTHX_ PerlIO *f)
3278 #ifdef HAS_SETLINEBUF
3279 PerlSIO_setlinebuf(PerlIOSelf(f, PerlIOStdio)->stdio);
3281 PerlSIO_setvbuf(PerlIOSelf(f, PerlIOStdio)->stdio, NULL, _IOLBF, 0);
3287 PerlIOStdio_get_base(pTHX_ PerlIO *f)
3289 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3290 return (STDCHAR*)PerlSIO_get_base(stdio);
3294 PerlIOStdio_get_bufsiz(pTHX_ PerlIO *f)
3296 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3297 return PerlSIO_get_bufsiz(stdio);
3301 #ifdef USE_STDIO_PTR
3303 PerlIOStdio_get_ptr(pTHX_ PerlIO *f)
3305 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3306 return (STDCHAR*)PerlSIO_get_ptr(stdio);
3310 PerlIOStdio_get_cnt(pTHX_ PerlIO *f)
3312 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3313 return PerlSIO_get_cnt(stdio);
3317 PerlIOStdio_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
3319 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3321 #ifdef STDIO_PTR_LVALUE
3322 PerlSIO_set_ptr(stdio, (void*)ptr); /* LHS STDCHAR* cast non-portable */
3323 #ifdef STDIO_PTR_LVAL_SETS_CNT
3324 if (PerlSIO_get_cnt(stdio) != (cnt)) {
3325 assert(PerlSIO_get_cnt(stdio) == (cnt));
3328 #if (!defined(STDIO_PTR_LVAL_NOCHANGE_CNT))
3330 * Setting ptr _does_ change cnt - we are done
3334 #else /* STDIO_PTR_LVALUE */
3336 #endif /* STDIO_PTR_LVALUE */
3339 * Now (or only) set cnt
3341 #ifdef STDIO_CNT_LVALUE
3342 PerlSIO_set_cnt(stdio, cnt);
3343 #else /* STDIO_CNT_LVALUE */
3344 #if (defined(STDIO_PTR_LVALUE) && defined(STDIO_PTR_LVAL_SETS_CNT))
3345 PerlSIO_set_ptr(stdio,
3346 PerlSIO_get_ptr(stdio) + (PerlSIO_get_cnt(stdio) -
3348 #else /* STDIO_PTR_LVAL_SETS_CNT */
3350 #endif /* STDIO_PTR_LVAL_SETS_CNT */
3351 #endif /* STDIO_CNT_LVALUE */
3358 PerlIOStdio_fill(pTHX_ PerlIO *f)
3360 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3363 * fflush()ing read-only streams can cause trouble on some stdio-s
3365 if ((PerlIOBase(f)->flags & PERLIO_F_CANWRITE)) {
3366 if (PerlSIO_fflush(stdio) != 0)
3369 c = PerlSIO_fgetc(stdio);
3373 #if (defined(STDIO_PTR_LVALUE) && (defined(STDIO_CNT_LVALUE) || defined(STDIO_PTR_LVAL_SETS_CNT)))
3375 #ifdef STDIO_BUFFER_WRITABLE
3376 if (PerlIO_fast_gets(f) && PerlIO_has_base(f)) {
3377 /* Fake ungetc() to the real buffer in case system's ungetc
3380 STDCHAR *base = (STDCHAR*)PerlSIO_get_base(stdio);
3381 SSize_t cnt = PerlSIO_get_cnt(stdio);
3382 STDCHAR *ptr = (STDCHAR*)PerlSIO_get_ptr(stdio);
3383 if (ptr == base+1) {
3384 *--ptr = (STDCHAR) c;
3385 PerlIOStdio_set_ptrcnt(aTHX_ f,ptr,cnt+1);
3386 if (PerlSIO_feof(stdio))
3387 PerlSIO_clearerr(stdio);
3393 if (PerlIO_has_cntptr(f)) {
3395 if (PerlIOStdio_unread(aTHX_ f,&ch,1) == 1) {
3402 /* An ungetc()d char is handled separately from the regular
3403 * buffer, so we stuff it in the buffer ourselves.
3404 * Should never get called as should hit code above
3406 *(--((*stdio)->_ptr)) = (unsigned char) c;
3409 /* If buffer snoop scheme above fails fall back to
3412 if (PerlSIO_ungetc(c, stdio) != c)
3420 PERLIO_FUNCS_DECL(PerlIO_stdio) = {
3421 sizeof(PerlIO_funcs),
3423 sizeof(PerlIOStdio),
3424 PERLIO_K_BUFFERED|PERLIO_K_RAW,
3428 PerlIOBase_binmode, /* binmode */
3442 PerlIOStdio_clearerr,
3443 PerlIOStdio_setlinebuf,
3445 PerlIOStdio_get_base,
3446 PerlIOStdio_get_bufsiz,
3451 #ifdef USE_STDIO_PTR
3452 PerlIOStdio_get_ptr,
3453 PerlIOStdio_get_cnt,
3454 # if defined(HAS_FAST_STDIO) && defined(USE_FAST_STDIO)
3455 PerlIOStdio_set_ptrcnt,
3458 # endif /* HAS_FAST_STDIO && USE_FAST_STDIO */
3463 #endif /* USE_STDIO_PTR */
3466 /* Note that calls to PerlIO_exportFILE() are reversed using
3467 * PerlIO_releaseFILE(), not importFILE. */
3469 PerlIO_exportFILE(PerlIO * f, const char *mode)
3473 if (PerlIOValid(f)) {
3476 if (!mode || !*mode) {
3477 mode = PerlIO_modestr(f, buf);
3479 stdio = PerlSIO_fdopen(PerlIO_fileno(f), mode);
3483 /* De-link any lower layers so new :stdio sticks */
3485 if ((f2 = PerlIO_push(aTHX_ f, PERLIO_FUNCS_CAST(&PerlIO_stdio), buf, NULL))) {
3486 PerlIOStdio *s = PerlIOSelf((f = f2), PerlIOStdio);
3488 /* Link previous lower layers under new one */
3492 /* restore layers list */
3502 PerlIO_findFILE(PerlIO *f)
3506 if (l->tab == &PerlIO_stdio) {
3507 PerlIOStdio *s = PerlIOSelf(&l, PerlIOStdio);
3510 l = *PerlIONext(&l);
3512 /* Uses fallback "mode" via PerlIO_modestr() in PerlIO_exportFILE */
3513 return PerlIO_exportFILE(f, NULL);
3516 /* Use this to reverse PerlIO_exportFILE calls. */
3518 PerlIO_releaseFILE(PerlIO *p, FILE *f)
3523 if (l->tab == &PerlIO_stdio) {
3524 PerlIOStdio *s = PerlIOSelf(&l, PerlIOStdio);
3525 if (s->stdio == f) {
3527 PerlIO_pop(aTHX_ p);
3536 /*--------------------------------------------------------------------------------------*/
3538 * perlio buffer layer
3542 PerlIOBuf_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
3544 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3545 const int fd = PerlIO_fileno(f);
3546 if (fd >= 0 && PerlLIO_isatty(fd)) {
3547 PerlIOBase(f)->flags |= PERLIO_F_LINEBUF | PERLIO_F_TTY;
3549 if (*PerlIONext(f)) {
3550 const Off_t posn = PerlIO_tell(PerlIONext(f));
3551 if (posn != (Off_t) - 1) {
3555 return PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
3559 PerlIOBuf_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
3560 IV n, const char *mode, int fd, int imode, int perm,
3561 PerlIO *f, int narg, SV **args)
3563 if (PerlIOValid(f)) {
3564 PerlIO *next = PerlIONext(f);
3566 PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIOBase(next)->tab);
3567 if (tab && tab->Open)
3569 (*tab->Open)(aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
3571 if (!next || (*PerlIOBase(f)->tab->Pushed) (aTHX_ f, mode, PerlIOArg, self) != 0) {
3576 PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIO_default_btm());
3578 if (*mode == IoTYPE_IMPLICIT) {
3584 if (tab && tab->Open)
3585 f = (*tab->Open)(aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
3588 SETERRNO(EINVAL, LIB_INVARG);
3590 if (PerlIO_push(aTHX_ f, self, mode, PerlIOArg) == 0) {
3592 * if push fails during open, open fails. close will pop us.
3597 fd = PerlIO_fileno(f);
3598 if (init && fd == 2) {
3600 * Initial stderr is unbuffered
3602 PerlIOBase(f)->flags |= PERLIO_F_UNBUF;
3604 #ifdef PERLIO_USING_CRLF
3605 # ifdef PERLIO_IS_BINMODE_FD
3606 if (PERLIO_IS_BINMODE_FD(fd))
3607 PerlIO_binmode(aTHX_ f, '<'/*not used*/, O_BINARY, NULL);
3611 * do something about failing setmode()? --jhi
3613 PerlLIO_setmode(fd, O_BINARY);
3622 * This "flush" is akin to sfio's sync in that it handles files in either
3623 * read or write state. For write state, we put the postponed data through
3624 * the next layers. For read state, we seek() the next layers to the
3625 * offset given by current position in the buffer, and discard the buffer
3626 * state (XXXX supposed to be for seek()able buffers only, but now it is done
3627 * in any case?). Then the pass the stick further in chain.
3630 PerlIOBuf_flush(pTHX_ PerlIO *f)
3632 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3634 PerlIO *n = PerlIONext(f);
3635 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF) {
3637 * write() the buffer
3639 const STDCHAR *buf = b->buf;
3640 const STDCHAR *p = buf;
3641 while (p < b->ptr) {
3642 SSize_t count = PerlIO_write(n, p, b->ptr - p);
3646 else if (count < 0 || PerlIO_error(n)) {
3647 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
3652 b->posn += (p - buf);
3654 else if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
3655 STDCHAR *buf = PerlIO_get_base(f);
3657 * Note position change
3659 b->posn += (b->ptr - buf);
3660 if (b->ptr < b->end) {
3661 /* We did not consume all of it - try and seek downstream to
3662 our logical position
3664 if (PerlIOValid(n) && PerlIO_seek(n, b->posn, SEEK_SET) == 0) {
3665 /* Reload n as some layers may pop themselves on seek */
3666 b->posn = PerlIO_tell(n = PerlIONext(f));
3669 /* Seek failed (e.g. pipe or tty). Do NOT clear buffer or pre-read
3670 data is lost for good - so return saying "ok" having undone
3673 b->posn -= (b->ptr - buf);
3678 b->ptr = b->end = b->buf;
3679 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
3680 /* We check for Valid because of dubious decision to make PerlIO_flush(NULL) flush all */
3681 if (PerlIOValid(n) && PerlIO_flush(n) != 0)
3686 /* This discards the content of the buffer after b->ptr, and rereads
3687 * the buffer from the position off in the layer downstream; here off
3688 * is at offset corresponding to b->ptr - b->buf.
3691 PerlIOBuf_fill(pTHX_ PerlIO *f)
3693 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3694 PerlIO *n = PerlIONext(f);
3697 * Down-stream flush is defined not to loose read data so is harmless.
3698 * we would not normally be fill'ing if there was data left in anycase.
3700 if (PerlIO_flush(f) != 0) /* XXXX Check that its seek() succeeded?! */
3702 if (PerlIOBase(f)->flags & PERLIO_F_TTY)
3703 PerlIOBase_flush_linebuf(aTHX);
3706 PerlIO_get_base(f); /* allocate via vtable */
3708 b->ptr = b->end = b->buf;
3710 if (!PerlIOValid(n)) {
3711 PerlIOBase(f)->flags |= PERLIO_F_EOF;
3715 if (PerlIO_fast_gets(n)) {
3717 * Layer below is also buffered. We do _NOT_ want to call its
3718 * ->Read() because that will loop till it gets what we asked for
3719 * which may hang on a pipe etc. Instead take anything it has to
3720 * hand, or ask it to fill _once_.
3722 avail = PerlIO_get_cnt(n);
3724 avail = PerlIO_fill(n);
3726 avail = PerlIO_get_cnt(n);
3728 if (!PerlIO_error(n) && PerlIO_eof(n))
3733 STDCHAR *ptr = PerlIO_get_ptr(n);
3734 const SSize_t cnt = avail;
3735 if (avail > (SSize_t)b->bufsiz)
3737 Copy(ptr, b->buf, avail, STDCHAR);
3738 PerlIO_set_ptrcnt(n, ptr + avail, cnt - avail);
3742 avail = PerlIO_read(n, b->ptr, b->bufsiz);
3746 PerlIOBase(f)->flags |= PERLIO_F_EOF;
3748 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
3751 b->end = b->buf + avail;
3752 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3757 PerlIOBuf_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
3759 if (PerlIOValid(f)) {
3760 const PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3763 return PerlIOBase_read(aTHX_ f, vbuf, count);
3769 PerlIOBuf_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3771 const STDCHAR *buf = (const STDCHAR *) vbuf + count;
3772 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3775 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
3780 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
3782 * Buffer is already a read buffer, we can overwrite any chars
3783 * which have been read back to buffer start
3785 avail = (b->ptr - b->buf);
3789 * Buffer is idle, set it up so whole buffer is available for
3793 b->end = b->buf + avail;
3795 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3797 * Buffer extends _back_ from where we are now
3799 b->posn -= b->bufsiz;
3801 if (avail > (SSize_t) count) {
3803 * If we have space for more than count, just move count
3811 * In simple stdio-like ungetc() case chars will be already
3814 if (buf != b->ptr) {
3815 Copy(buf, b->ptr, avail, STDCHAR);
3819 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
3823 unread += PerlIOBase_unread(aTHX_ f, vbuf, count);
3829 PerlIOBuf_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3831 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3832 const STDCHAR *buf = (const STDCHAR *) vbuf;
3833 const STDCHAR *flushptr = buf;
3837 if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
3839 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
3840 if (PerlIO_flush(f) != 0) {
3844 if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF) {
3845 flushptr = buf + count;
3846 while (flushptr > buf && *(flushptr - 1) != '\n')
3850 SSize_t avail = b->bufsiz - (b->ptr - b->buf);
3851 if ((SSize_t) count < avail)
3853 if (flushptr > buf && flushptr <= buf + avail)
3854 avail = flushptr - buf;
3855 PerlIOBase(f)->flags |= PERLIO_F_WRBUF;
3857 Copy(buf, b->ptr, avail, STDCHAR);
3862 if (buf == flushptr)
3865 if (b->ptr >= (b->buf + b->bufsiz))
3868 if (PerlIOBase(f)->flags & PERLIO_F_UNBUF)
3874 PerlIOBuf_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
3877 if ((code = PerlIO_flush(f)) == 0) {
3878 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
3879 code = PerlIO_seek(PerlIONext(f), offset, whence);
3881 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3882 b->posn = PerlIO_tell(PerlIONext(f));
3889 PerlIOBuf_tell(pTHX_ PerlIO *f)
3891 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3893 * b->posn is file position where b->buf was read, or will be written
3895 Off_t posn = b->posn;
3896 if ((PerlIOBase(f)->flags & PERLIO_F_APPEND) &&
3897 (PerlIOBase(f)->flags & PERLIO_F_WRBUF)) {
3899 /* As O_APPEND files are normally shared in some sense it is better
3904 /* when file is NOT shared then this is sufficient */
3905 PerlIO_seek(PerlIONext(f),0, SEEK_END);
3907 posn = b->posn = PerlIO_tell(PerlIONext(f));
3911 * If buffer is valid adjust position by amount in buffer
3913 posn += (b->ptr - b->buf);
3919 PerlIOBuf_popped(pTHX_ PerlIO *f)
3921 const IV code = PerlIOBase_popped(aTHX_ f);
3922 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3923 if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
3926 b->ptr = b->end = b->buf = NULL;
3927 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
3932 PerlIOBuf_close(pTHX_ PerlIO *f)
3934 const IV code = PerlIOBase_close(aTHX_ f);
3935 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3936 if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
3939 b->ptr = b->end = b->buf = NULL;
3940 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
3945 PerlIOBuf_get_ptr(pTHX_ PerlIO *f)
3947 const PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3954 PerlIOBuf_get_cnt(pTHX_ PerlIO *f)
3956 const PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3959 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF)
3960 return (b->end - b->ptr);
3965 PerlIOBuf_get_base(pTHX_ PerlIO *f)
3967 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3971 b->buf = Newxz(b->buf,b->bufsiz, STDCHAR);
3973 b->buf = (STDCHAR *) & b->oneword;
3974 b->bufsiz = sizeof(b->oneword);
3976 b->end = b->ptr = b->buf;
3982 PerlIOBuf_bufsiz(pTHX_ PerlIO *f)
3984 const PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3987 return (b->end - b->buf);
3991 PerlIOBuf_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
3993 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3997 if (PerlIO_get_cnt(f) != cnt || b->ptr < b->buf) {
3998 assert(PerlIO_get_cnt(f) == cnt);
3999 assert(b->ptr >= b->buf);
4001 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
4005 PerlIOBuf_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
4007 return PerlIOBase_dup(aTHX_ f, o, param, flags);
4012 PERLIO_FUNCS_DECL(PerlIO_perlio) = {
4013 sizeof(PerlIO_funcs),
4016 PERLIO_K_BUFFERED|PERLIO_K_RAW,
4020 PerlIOBase_binmode, /* binmode */
4034 PerlIOBase_clearerr,
4035 PerlIOBase_setlinebuf,
4040 PerlIOBuf_set_ptrcnt,
4043 /*--------------------------------------------------------------------------------------*/
4045 * Temp layer to hold unread chars when cannot do it any other way
4049 PerlIOPending_fill(pTHX_ PerlIO *f)
4052 * Should never happen
4059 PerlIOPending_close(pTHX_ PerlIO *f)
4062 * A tad tricky - flush pops us, then we close new top
4065 return PerlIO_close(f);
4069 PerlIOPending_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
4072 * A tad tricky - flush pops us, then we seek new top
4075 return PerlIO_seek(f, offset, whence);
4080 PerlIOPending_flush(pTHX_ PerlIO *f)
4082 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
4083 if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
4087 PerlIO_pop(aTHX_ f);
4092 PerlIOPending_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
4098 PerlIOBuf_set_ptrcnt(aTHX_ f, ptr, cnt);
4103 PerlIOPending_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
4105 const IV code = PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
4106 PerlIOl * const l = PerlIOBase(f);
4108 * Our PerlIO_fast_gets must match what we are pushed on, or sv_gets()
4109 * etc. get muddled when it changes mid-string when we auto-pop.
4111 l->flags = (l->flags & ~(PERLIO_F_FASTGETS | PERLIO_F_UTF8)) |
4112 (PerlIOBase(PerlIONext(f))->
4113 flags & (PERLIO_F_FASTGETS | PERLIO_F_UTF8));
4118 PerlIOPending_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
4120 SSize_t avail = PerlIO_get_cnt(f);
4122 if ((SSize_t)count < avail)
4125 got = PerlIOBuf_read(aTHX_ f, vbuf, avail);
4126 if (got >= 0 && got < (SSize_t)count) {
4127 const SSize_t more =
4128 PerlIO_read(f, ((STDCHAR *) vbuf) + got, count - got);
4129 if (more >= 0 || got == 0)
4135 PERLIO_FUNCS_DECL(PerlIO_pending) = {
4136 sizeof(PerlIO_funcs),
4139 PERLIO_K_BUFFERED|PERLIO_K_RAW, /* not sure about RAW here */
4140 PerlIOPending_pushed,
4143 PerlIOBase_binmode, /* binmode */
4152 PerlIOPending_close,
4153 PerlIOPending_flush,
4157 PerlIOBase_clearerr,
4158 PerlIOBase_setlinebuf,
4163 PerlIOPending_set_ptrcnt,
4168 /*--------------------------------------------------------------------------------------*/
4170 * crlf - translation On read translate CR,LF to "\n" we do this by
4171 * overriding ptr/cnt entries to hand back a line at a time and keeping a
4172 * record of which nl we "lied" about. On write translate "\n" to CR,LF
4174 * c->nl points on the first byte of CR LF pair when it is temporarily
4175 * replaced by LF, or to the last CR of the buffer. In the former case
4176 * the caller thinks that the buffer ends at c->nl + 1, in the latter
4177 * that it ends at c->nl; these two cases can be distinguished by
4178 * *c->nl. c->nl is set during _getcnt() call, and unset during
4179 * _unread() and _flush() calls.
4180 * It only matters for read operations.
4184 PerlIOBuf base; /* PerlIOBuf stuff */
4185 STDCHAR *nl; /* Position of crlf we "lied" about in the
4190 PerlIOCrlf_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
4193 PerlIOBase(f)->flags |= PERLIO_F_CRLF;
4194 code = PerlIOBuf_pushed(aTHX_ f, mode, arg, tab);
4196 PerlIO_debug("PerlIOCrlf_pushed f=%p %s %s fl=%08" UVxf "\n",
4197 f, PerlIOBase(f)->tab->name, (mode) ? mode : "(Null)",
4198 PerlIOBase(f)->flags);
4201 /* Enable the first CRLF capable layer you can find, but if none
4202 * found, the one we just pushed is fine. This results in at
4203 * any given moment at most one CRLF-capable layer being enabled
4204 * in the whole layer stack. */
4205 PerlIO *g = PerlIONext(f);
4207 PerlIOl *b = PerlIOBase(g);
4208 if (b && b->tab == &PerlIO_crlf) {
4209 if (!(b->flags & PERLIO_F_CRLF))
4210 b->flags |= PERLIO_F_CRLF;
4211 PerlIO_pop(aTHX_ f);
4222 PerlIOCrlf_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
4224 PerlIOCrlf * const c = PerlIOSelf(f, PerlIOCrlf);
4225 if (c->nl) { /* XXXX Shouldn't it be done only if b->ptr > c->nl? */
4229 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF))
4230 return PerlIOBuf_unread(aTHX_ f, vbuf, count);
4232 const STDCHAR *buf = (const STDCHAR *) vbuf + count;
4233 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
4235 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
4240 if (!(PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
4241 b->end = b->ptr = b->buf + b->bufsiz;
4242 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
4243 b->posn -= b->bufsiz;
4245 while (count > 0 && b->ptr > b->buf) {
4246 const int ch = *--buf;
4248 if (b->ptr - 2 >= b->buf) {
4255 /* If b->ptr - 1 == b->buf, we are undoing reading 0xa */
4256 *--(b->ptr) = 0xa; /* Works even if 0xa == '\r' */
4272 /* XXXX This code assumes that buffer size >=2, but does not check it... */
4274 PerlIOCrlf_get_cnt(pTHX_ PerlIO *f)
4276 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
4279 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
4280 PerlIOCrlf * const c = PerlIOSelf(f, PerlIOCrlf);
4281 if ((PerlIOBase(f)->flags & PERLIO_F_CRLF) && (!c->nl || *c->nl == 0xd)) {
4282 STDCHAR *nl = (c->nl) ? c->nl : b->ptr;
4284 while (nl < b->end && *nl != 0xd)
4286 if (nl < b->end && *nl == 0xd) {
4288 if (nl + 1 < b->end) {
4295 * Not CR,LF but just CR
4303 * Blast - found CR as last char in buffer
4308 * They may not care, defer work as long as
4312 return (nl - b->ptr);
4316 b->ptr++; /* say we have read it as far as
4317 * flush() is concerned */
4318 b->buf++; /* Leave space in front of buffer */
4319 /* Note as we have moved buf up flush's
4321 will naturally make posn point at CR
4323 b->bufsiz--; /* Buffer is thus smaller */
4324 code = PerlIO_fill(f); /* Fetch some more */
4325 b->bufsiz++; /* Restore size for next time */
4326 b->buf--; /* Point at space */
4327 b->ptr = nl = b->buf; /* Which is what we hand
4329 *nl = 0xd; /* Fill in the CR */
4331 goto test; /* fill() call worked */
4333 * CR at EOF - just fall through
4335 /* Should we clear EOF though ??? */
4340 return (((c->nl) ? (c->nl + 1) : b->end) - b->ptr);
4346 PerlIOCrlf_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
4348 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
4349 PerlIOCrlf * const c = PerlIOSelf(f, PerlIOCrlf);
4355 if (ptr == b->end && *c->nl == 0xd) {
4356 /* Defered CR at end of buffer case - we lied about count */
4369 * Test code - delete when it works ...
4371 IV flags = PerlIOBase(f)->flags;
4372 STDCHAR *chk = (c->nl) ? (c->nl+1) : b->end;
4373 if (ptr+cnt == c->nl && c->nl+1 == b->end && *c->nl == 0xd) {
4374 /* Defered CR at end of buffer case - we lied about count */
4380 Perl_croak(aTHX_ "ptr wrong %p != %p fl=%08" UVxf
4381 " nl=%p e=%p for %d", ptr, chk, flags, c->nl,
4389 * They have taken what we lied about
4397 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
4401 PerlIOCrlf_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
4403 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF))
4404 return PerlIOBuf_write(aTHX_ f, vbuf, count);
4406 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
4407 const STDCHAR *buf = (const STDCHAR *) vbuf;
4408 const STDCHAR * const ebuf = buf + count;
4411 if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
4413 while (buf < ebuf) {
4414 const STDCHAR * const eptr = b->buf + b->bufsiz;
4415 PerlIOBase(f)->flags |= PERLIO_F_WRBUF;
4416 while (buf < ebuf && b->ptr < eptr) {
4418 if ((b->ptr + 2) > eptr) {
4426 *(b->ptr)++ = 0xd; /* CR */
4427 *(b->ptr)++ = 0xa; /* LF */
4429 if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF) {
4436 *(b->ptr)++ = *buf++;
4438 if (b->ptr >= eptr) {
4444 if (PerlIOBase(f)->flags & PERLIO_F_UNBUF)
4446 return (buf - (STDCHAR *) vbuf);
4451 PerlIOCrlf_flush(pTHX_ PerlIO *f)
4453 PerlIOCrlf * const c = PerlIOSelf(f, PerlIOCrlf);
4458 return PerlIOBuf_flush(aTHX_ f);
4462 PerlIOCrlf_binmode(pTHX_ PerlIO *f)
4464 if ((PerlIOBase(f)->flags & PERLIO_F_CRLF)) {
4465 /* In text mode - flush any pending stuff and flip it */
4466 PerlIOBase(f)->flags &= ~PERLIO_F_CRLF;
4467 #ifndef PERLIO_USING_CRLF
4468 /* CRLF is unusual case - if this is just the :crlf layer pop it */
4469 if (PerlIOBase(f)->tab == &PerlIO_crlf) {
4470 PerlIO_pop(aTHX_ f);
4477 PERLIO_FUNCS_DECL(PerlIO_crlf) = {
4478 sizeof(PerlIO_funcs),
4481 PERLIO_K_BUFFERED | PERLIO_K_CANCRLF | PERLIO_K_RAW,
4483 PerlIOBuf_popped, /* popped */
4485 PerlIOCrlf_binmode, /* binmode */
4489 PerlIOBuf_read, /* generic read works with ptr/cnt lies */
4490 PerlIOCrlf_unread, /* Put CR,LF in buffer for each '\n' */
4491 PerlIOCrlf_write, /* Put CR,LF in buffer for each '\n' */
4499 PerlIOBase_clearerr,
4500 PerlIOBase_setlinebuf,
4505 PerlIOCrlf_set_ptrcnt,
4509 /*--------------------------------------------------------------------------------------*/
4511 * mmap as "buffer" layer
4515 PerlIOBuf base; /* PerlIOBuf stuff */
4516 Mmap_t mptr; /* Mapped address */
4517 Size_t len; /* mapped length */
4518 STDCHAR *bbuf; /* malloced buffer if map fails */
4522 PerlIOMmap_map(pTHX_ PerlIO *f)
4525 PerlIOMmap * const m = PerlIOSelf(f, PerlIOMmap);
4526 const IV flags = PerlIOBase(f)->flags;
4530 if (flags & PERLIO_F_CANREAD) {
4531 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
4532 const int fd = PerlIO_fileno(f);
4534 code = Fstat(fd, &st);
4535 if (code == 0 && S_ISREG(st.st_mode)) {
4536 SSize_t len = st.st_size - b->posn;
4539 if (PL_mmap_page_size <= 0)
4540 Perl_croak(aTHX_ "panic: bad pagesize %" IVdf,
4544 * This is a hack - should never happen - open should
4547 b->posn = PerlIO_tell(PerlIONext(f));
4549 posn = (b->posn / PL_mmap_page_size) * PL_mmap_page_size;
4550 len = st.st_size - posn;
4551 m->mptr = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, posn);
4552 if (m->mptr && m->mptr != (Mmap_t) - 1) {
4553 #if 0 && defined(HAS_MADVISE) && defined(MADV_SEQUENTIAL)
4554 madvise(m->mptr, len, MADV_SEQUENTIAL);
4556 #if 0 && defined(HAS_MADVISE) && defined(MADV_WILLNEED)
4557 madvise(m->mptr, len, MADV_WILLNEED);
4559 PerlIOBase(f)->flags =
4560 (flags & ~PERLIO_F_EOF) | PERLIO_F_RDBUF;
4561 b->end = ((STDCHAR *) m->mptr) + len;
4562 b->buf = ((STDCHAR *) m->mptr) + (b->posn - posn);
4571 PerlIOBase(f)->flags =
4572 flags | PERLIO_F_EOF | PERLIO_F_RDBUF;
4574 b->ptr = b->end = b->ptr;
4583 PerlIOMmap_unmap(pTHX_ PerlIO *f)
4585 PerlIOMmap * const m = PerlIOSelf(f, PerlIOMmap);
4588 PerlIOBuf * const b = &m->base;
4590 code = munmap(m->mptr, m->len);
4594 if (PerlIO_seek(PerlIONext(f), b->posn, SEEK_SET) != 0)
4597 b->ptr = b->end = b->buf;
4598 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
4604 PerlIOMmap_get_base(pTHX_ PerlIO *f)
4606 PerlIOMmap * const m = PerlIOSelf(f, PerlIOMmap);
4607 PerlIOBuf * const b = &m->base;
4608 if (b->buf && (PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
4610 * Already have a readbuffer in progress
4616 * We have a write buffer or flushed PerlIOBuf read buffer
4618 m->bbuf = b->buf; /* save it in case we need it again */
4619 b->buf = NULL; /* Clear to trigger below */
4622 PerlIOMmap_map(aTHX_ f); /* Try and map it */
4625 * Map did not work - recover PerlIOBuf buffer if we have one
4630 b->ptr = b->end = b->buf;
4633 return PerlIOBuf_get_base(aTHX_ f);
4637 PerlIOMmap_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
4639 PerlIOMmap * const m = PerlIOSelf(f, PerlIOMmap);
4640 PerlIOBuf * const b = &m->base;
4641 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
4643 if (b->ptr && (b->ptr - count) >= b->buf
4644 && memEQ(b->ptr - count, vbuf, count)) {
4646 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
4651 * Loose the unwritable mapped buffer
4655 * If flush took the "buffer" see if we have one from before
4657 if (!b->buf && m->bbuf)
4660 PerlIOBuf_get_base(aTHX_ f);
4664 return PerlIOBuf_unread(aTHX_ f, vbuf, count);
4668 PerlIOMmap_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
4670 PerlIOMmap * const m = PerlIOSelf(f, PerlIOMmap);
4671 PerlIOBuf * const b = &m->base;
4673 if (!b->buf || !(PerlIOBase(f)->flags & PERLIO_F_WRBUF)) {
4675 * No, or wrong sort of, buffer
4678 if (PerlIOMmap_unmap(aTHX_ f) != 0)
4682 * If unmap took the "buffer" see if we have one from before
4684 if (!b->buf && m->bbuf)
4687 PerlIOBuf_get_base(aTHX_ f);
4691 return PerlIOBuf_write(aTHX_ f, vbuf, count);
4695 PerlIOMmap_flush(pTHX_ PerlIO *f)
4697 PerlIOMmap * const m = PerlIOSelf(f, PerlIOMmap);
4698 PerlIOBuf * const b = &m->base;
4699 IV code = PerlIOBuf_flush(aTHX_ f);
4701 * Now we are "synced" at PerlIOBuf level
4708 if (PerlIOMmap_unmap(aTHX_ f) != 0)
4713 * We seem to have a PerlIOBuf buffer which was not mapped
4714 * remember it in case we need one later
4723 PerlIOMmap_fill(pTHX_ PerlIO *f)
4725 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
4726 IV code = PerlIO_flush(f);
4727 if (code == 0 && !b->buf) {
4728 code = PerlIOMmap_map(aTHX_ f);
4730 if (code == 0 && !(PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
4731 code = PerlIOBuf_fill(aTHX_ f);
4737 PerlIOMmap_close(pTHX_ PerlIO *f)
4739 PerlIOMmap * const m = PerlIOSelf(f, PerlIOMmap);
4740 PerlIOBuf * const b = &m->base;
4741 IV code = PerlIO_flush(f);
4745 b->ptr = b->end = b->buf;
4747 if (PerlIOBuf_close(aTHX_ f) != 0)
4753 PerlIOMmap_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
4755 return PerlIOBase_dup(aTHX_ f, o, param, flags);
4759 PERLIO_FUNCS_DECL(PerlIO_mmap) = {
4760 sizeof(PerlIO_funcs),
4763 PERLIO_K_BUFFERED|PERLIO_K_RAW,
4767 PerlIOBase_binmode, /* binmode */
4781 PerlIOBase_clearerr,
4782 PerlIOBase_setlinebuf,
4783 PerlIOMmap_get_base,
4787 PerlIOBuf_set_ptrcnt,
4790 #endif /* HAS_MMAP */
4793 Perl_PerlIO_stdin(pTHX)
4797 PerlIO_stdstreams(aTHX);
4799 return &PL_perlio[1];
4803 Perl_PerlIO_stdout(pTHX)
4807 PerlIO_stdstreams(aTHX);
4809 return &PL_perlio[2];
4813 Perl_PerlIO_stderr(pTHX)
4817 PerlIO_stdstreams(aTHX);
4819 return &PL_perlio[3];
4822 /*--------------------------------------------------------------------------------------*/
4825 PerlIO_getname(PerlIO *f, char *buf)
4830 bool exported = FALSE;
4831 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
4833 stdio = PerlIO_exportFILE(f,0);
4837 name = fgetname(stdio, buf);
4838 if (exported) PerlIO_releaseFILE(f,stdio);
4843 PERL_UNUSED_ARG(buf);
4844 Perl_croak(aTHX_ "Don't know how to get file name");
4850 /*--------------------------------------------------------------------------------------*/
4852 * Functions which can be called on any kind of PerlIO implemented in
4856 #undef PerlIO_fdopen
4858 PerlIO_fdopen(int fd, const char *mode)
4861 return PerlIO_openn(aTHX_ NULL, mode, fd, 0, 0, NULL, 0, NULL);
4866 PerlIO_open(const char *path, const char *mode)
4869 SV *name = sv_2mortal(newSVpv(path, 0));
4870 return PerlIO_openn(aTHX_ NULL, mode, -1, 0, 0, NULL, 1, &name);
4873 #undef Perlio_reopen
4875 PerlIO_reopen(const char *path, const char *mode, PerlIO *f)
4878 SV *name = sv_2mortal(newSVpv(path,0));
4879 return PerlIO_openn(aTHX_ NULL, mode, -1, 0, 0, f, 1, &name);
4884 PerlIO_getc(PerlIO *f)
4888 if ( 1 == PerlIO_read(f, buf, 1) ) {
4889 return (unsigned char) buf[0];
4894 #undef PerlIO_ungetc
4896 PerlIO_ungetc(PerlIO *f, int ch)
4901 if (PerlIO_unread(f, &buf, 1) == 1)
4909 PerlIO_putc(PerlIO *f, int ch)
4913 return PerlIO_write(f, &buf, 1);
4918 PerlIO_puts(PerlIO *f, const char *s)
4921 return PerlIO_write(f, s, strlen(s));
4924 #undef PerlIO_rewind
4926 PerlIO_rewind(PerlIO *f)
4929 PerlIO_seek(f, (Off_t) 0, SEEK_SET);
4933 #undef PerlIO_vprintf
4935 PerlIO_vprintf(PerlIO *f, const char *fmt, va_list ap)
4938 SV * const sv = newSVpvs("");
4944 Perl_va_copy(ap, apc);
4945 sv_vcatpvf(sv, fmt, &apc);
4947 sv_vcatpvf(sv, fmt, &ap);
4949 s = SvPV_const(sv, len);
4950 wrote = PerlIO_write(f, s, len);
4955 #undef PerlIO_printf
4957 PerlIO_printf(PerlIO *f, const char *fmt, ...)
4962 result = PerlIO_vprintf(f, fmt, ap);
4967 #undef PerlIO_stdoutf
4969 PerlIO_stdoutf(const char *fmt, ...)
4975 result = PerlIO_vprintf(PerlIO_stdout(), fmt, ap);
4980 #undef PerlIO_tmpfile
4982 PerlIO_tmpfile(void)
4987 const int fd = win32_tmpfd();
4989 f = PerlIO_fdopen(fd, "w+b");
4991 # if defined(HAS_MKSTEMP) && ! defined(VMS) && ! defined(OS2)
4992 SV * const sv = newSVpvs("/tmp/PerlIO_XXXXXX");
4994 * I have no idea how portable mkstemp() is ... NI-S
4996 const int fd = mkstemp(SvPVX(sv));
4998 f = PerlIO_fdopen(fd, "w+");
5000 PerlIOBase(f)->flags |= PERLIO_F_TEMP;
5001 PerlLIO_unlink(SvPVX_const(sv));
5004 # else /* !HAS_MKSTEMP, fallback to stdio tmpfile(). */
5005 FILE * const stdio = PerlSIO_tmpfile();
5008 if ((f = PerlIO_push(aTHX_(PerlIO_allocate(aTHX)),
5009 PERLIO_FUNCS_CAST(&PerlIO_stdio),
5011 PerlIOStdio * const s = PerlIOSelf(f, PerlIOStdio);
5017 # endif /* else HAS_MKSTEMP */
5018 #endif /* else WIN32 */
5025 #endif /* USE_SFIO */
5026 #endif /* PERLIO_IS_STDIO */
5028 /*======================================================================================*/
5030 * Now some functions in terms of above which may be needed even if we are
5031 * not in true PerlIO mode
5035 #undef PerlIO_setpos
5037 PerlIO_setpos(PerlIO *f, SV *pos)
5042 const Off_t * const posn = (Off_t *) SvPV(pos, len);
5043 if (f && len == sizeof(Off_t))
5044 return PerlIO_seek(f, *posn, SEEK_SET);
5046 SETERRNO(EINVAL, SS_IVCHAN);
5050 #undef PerlIO_setpos
5052 PerlIO_setpos(PerlIO *f, SV *pos)
5057 Fpos_t * const fpos = (Fpos_t *) SvPV(pos, len);
5058 if (f && len == sizeof(Fpos_t)) {
5059 #if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64)
5060 return fsetpos64(f, fpos);
5062 return fsetpos(f, fpos);
5066 SETERRNO(EINVAL, SS_IVCHAN);
5072 #undef PerlIO_getpos
5074 PerlIO_getpos(PerlIO *f, SV *pos)
5077 Off_t posn = PerlIO_tell(f);
5078 sv_setpvn(pos, (char *) &posn, sizeof(posn));
5079 return (posn == (Off_t) - 1) ? -1 : 0;
5082 #undef PerlIO_getpos
5084 PerlIO_getpos(PerlIO *f, SV *pos)
5089 #if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64)
5090 code = fgetpos64(f, &fpos);
5092 code = fgetpos(f, &fpos);
5094 sv_setpvn(pos, (char *) &fpos, sizeof(fpos));
5099 #if (defined(PERLIO_IS_STDIO) || !defined(USE_SFIO)) && !defined(HAS_VPRINTF)
5102 vprintf(char *pat, char *args)
5104 _doprnt(pat, args, stdout);
5105 return 0; /* wrong, but perl doesn't use the return
5110 vfprintf(FILE *fd, char *pat, char *args)
5112 _doprnt(pat, args, fd);
5113 return 0; /* wrong, but perl doesn't use the return
5119 #ifndef PerlIO_vsprintf
5121 PerlIO_vsprintf(char *s, int n, const char *fmt, va_list ap)
5124 const int val = vsprintf(s, fmt, ap);
5126 if (strlen(s) >= (STRLEN) n) {
5128 (void) PerlIO_puts(Perl_error_log,
5129 "panic: sprintf overflow - memory corrupted!\n");
5137 #ifndef PerlIO_sprintf
5139 PerlIO_sprintf(char *s, int n, const char *fmt, ...)
5144 result = PerlIO_vsprintf(s, n, fmt, ap);
5152 * c-indentation-style: bsd
5154 * indent-tabs-mode: t
5157 * ex: set ts=8 sts=4 sw=4 noet: