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
363 * Does nothing (yet) except force this file to be included in perl
364 * binary. That allows this file to force inclusion of other functions
365 * that may be required by loadable extensions e.g. for
366 * FileHandle::tmpfile
370 #undef PerlIO_tmpfile
377 #else /* PERLIO_IS_STDIO */
385 * This section is just to make sure these functions get pulled in from
389 #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)
577 Newxz(list, 1, PerlIO_list_t);
583 PerlIO_list_free(pTHX_ PerlIO_list_t *list)
586 if (--list->refcnt == 0) {
589 for (i = 0; i < list->cur; i++) {
590 if (list->array[i].arg)
591 SvREFCNT_dec(list->array[i].arg);
593 Safefree(list->array);
601 PerlIO_list_push(pTHX_ PerlIO_list_t *list, PerlIO_funcs *funcs, SV *arg)
607 if (list->cur >= list->len) {
610 Renew(list->array, list->len, PerlIO_pair_t);
612 Newx(list->array, list->len, PerlIO_pair_t);
614 p = &(list->array[list->cur++]);
616 if ((p->arg = arg)) {
617 SvREFCNT_inc_simple_void_NN(arg);
622 PerlIO_clone_list(pTHX_ PerlIO_list_t *proto, CLONE_PARAMS *param)
624 PerlIO_list_t *list = NULL;
627 list = PerlIO_list_alloc(aTHX);
628 for (i=0; i < proto->cur; i++) {
630 if (proto->array[i].arg)
631 arg = PerlIO_sv_dup(aTHX_ proto->array[i].arg,param);
632 PerlIO_list_push(aTHX_ list, proto->array[i].funcs, arg);
639 PerlIO_clone(pTHX_ PerlInterpreter *proto, CLONE_PARAMS *param)
642 PerlIO **table = &proto->Iperlio;
645 PL_known_layers = PerlIO_clone_list(aTHX_ proto->Iknown_layers, param);
646 PL_def_layerlist = PerlIO_clone_list(aTHX_ proto->Idef_layerlist, param);
647 PerlIO_allocate(aTHX); /* root slot is never used */
648 PerlIO_debug("Clone %p from %p\n",aTHX,proto);
649 while ((f = *table)) {
651 table = (PerlIO **) (f++);
652 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
654 (void) fp_dup(f, 0, param);
660 PERL_UNUSED_ARG(proto);
661 PERL_UNUSED_ARG(param);
666 PerlIO_destruct(pTHX)
669 PerlIO **table = &PL_perlio;
672 PerlIO_debug("Destruct %p\n",aTHX);
674 while ((f = *table)) {
676 table = (PerlIO **) (f++);
677 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
681 if (l->tab->kind & PERLIO_K_DESTRUCT) {
682 PerlIO_debug("Destruct popping %s\n", l->tab->name);
696 PerlIO_pop(pTHX_ PerlIO *f)
698 const PerlIOl *l = *f;
700 PerlIO_debug("PerlIO_pop f=%p %s\n", (void*)f, l->tab->name);
701 if (l->tab->Popped) {
703 * If popped returns non-zero do not free its layer structure
704 * it has either done so itself, or it is shared and still in
707 if ((*l->tab->Popped) (aTHX_ f) != 0)
715 /* Return as an array the stack of layers on a filehandle. Note that
716 * the stack is returned top-first in the array, and there are three
717 * times as many array elements as there are layers in the stack: the
718 * first element of a layer triplet is the name, the second one is the
719 * arguments, and the third one is the flags. */
722 PerlIO_get_layers(pTHX_ PerlIO *f)
725 AV * const av = newAV();
727 if (PerlIOValid(f)) {
728 PerlIOl *l = PerlIOBase(f);
731 SV * const name = l->tab && l->tab->name ?
732 newSVpv(l->tab->name, 0) : &PL_sv_undef;
733 SV * const arg = l->tab && l->tab->Getarg ?
734 (*l->tab->Getarg)(aTHX_ &l, 0, 0) : &PL_sv_undef;
737 av_push(av, newSViv((IV)l->flags));
745 /*--------------------------------------------------------------------------------------*/
747 * XS Interface for perl code
751 PerlIO_find_layer(pTHX_ const char *name, STRLEN len, int load)
755 if ((SSize_t) len <= 0)
757 for (i = 0; i < PL_known_layers->cur; i++) {
758 PerlIO_funcs * const f = PL_known_layers->array[i].funcs;
759 if (memEQ(f->name, name, len) && f->name[len] == 0) {
760 PerlIO_debug("%.*s => %p\n", (int) len, name, (void*)f);
764 if (load && PL_subname && PL_def_layerlist
765 && PL_def_layerlist->cur >= 2) {
766 if (PL_in_load_module) {
767 Perl_croak(aTHX_ "Recursive call to Perl_load_module in PerlIO_find_layer");
770 SV * const pkgsv = newSVpvs("PerlIO");
771 SV * const layer = newSVpvn(name, len);
772 CV * const cv = get_cv("PerlIO::Layer::NoWarnings", FALSE);
774 SAVEINT(PL_in_load_module);
776 SAVEGENERICSV(PL_warnhook);
777 PL_warnhook = (SV *) (SvREFCNT_inc_simple_NN(cv));
781 * The two SVs are magically freed by load_module
783 Perl_load_module(aTHX_ 0, pkgsv, NULL, layer, NULL);
786 return PerlIO_find_layer(aTHX_ name, len, 0);
789 PerlIO_debug("Cannot find %.*s\n", (int) len, name);
793 #ifdef USE_ATTRIBUTES_FOR_PERLIO
796 perlio_mg_set(pTHX_ SV *sv, MAGIC *mg)
799 IO * const io = GvIOn((GV *) SvRV(sv));
800 PerlIO * const ifp = IoIFP(io);
801 PerlIO * const ofp = IoOFP(io);
802 Perl_warn(aTHX_ "set %" SVf " %p %p %p", sv, io, ifp, ofp);
808 perlio_mg_get(pTHX_ SV *sv, MAGIC *mg)
811 IO * const io = GvIOn((GV *) SvRV(sv));
812 PerlIO * const ifp = IoIFP(io);
813 PerlIO * const ofp = IoOFP(io);
814 Perl_warn(aTHX_ "get %" SVf " %p %p %p", sv, io, ifp, ofp);
820 perlio_mg_clear(pTHX_ SV *sv, MAGIC *mg)
822 Perl_warn(aTHX_ "clear %" SVf, sv);
827 perlio_mg_free(pTHX_ SV *sv, MAGIC *mg)
829 Perl_warn(aTHX_ "free %" SVf, sv);
833 MGVTBL perlio_vtab = {
841 XS(XS_io_MODIFY_SCALAR_ATTRIBUTES)
844 SV * const sv = SvRV(ST(1));
845 AV * const av = newAV();
849 sv_magic(sv, (SV *) av, PERL_MAGIC_ext, NULL, 0);
851 mg = mg_find(sv, PERL_MAGIC_ext);
852 mg->mg_virtual = &perlio_vtab;
854 Perl_warn(aTHX_ "attrib %" SVf, sv);
855 for (i = 2; i < items; i++) {
857 const char * const name = SvPV_const(ST(i), len);
858 SV * const layer = PerlIO_find_layer(aTHX_ name, len, 1);
860 av_push(av, SvREFCNT_inc_simple_NN(layer));
871 #endif /* USE_ATTIBUTES_FOR_PERLIO */
874 PerlIO_tab_sv(pTHX_ PerlIO_funcs *tab)
876 HV * const stash = gv_stashpvs("PerlIO::Layer", TRUE);
877 SV * const sv = sv_bless(newRV_noinc(newSViv(PTR2IV(tab))), stash);
881 XS(XS_PerlIO__Layer__NoWarnings)
883 /* This is used as a %SIG{__WARN__} handler to supress warnings
884 during loading of layers.
889 PerlIO_debug("warning:%s\n",SvPV_nolen_const(ST(0)));
893 XS(XS_PerlIO__Layer__find)
898 Perl_croak(aTHX_ "Usage class->find(name[,load])");
901 const char * const name = SvPV_const(ST(1), len);
902 const bool load = (items > 2) ? SvTRUE(ST(2)) : 0;
903 PerlIO_funcs * const layer = PerlIO_find_layer(aTHX_ name, len, load);
905 (layer) ? sv_2mortal(PerlIO_tab_sv(aTHX_ layer)) :
912 PerlIO_define_layer(pTHX_ PerlIO_funcs *tab)
915 if (!PL_known_layers)
916 PL_known_layers = PerlIO_list_alloc(aTHX);
917 PerlIO_list_push(aTHX_ PL_known_layers, tab, NULL);
918 PerlIO_debug("define %s %p\n", tab->name, (void*)tab);
922 PerlIO_parse_layers(pTHX_ PerlIO_list_t *av, const char *names)
926 const char *s = names;
928 while (isSPACE(*s) || *s == ':')
933 const char *as = NULL;
935 if (!isIDFIRST(*s)) {
937 * Message is consistent with how attribute lists are
938 * passed. Even though this means "foo : : bar" is
939 * seen as an invalid separator character.
941 const char q = ((*s == '\'') ? '"' : '\'');
942 if (ckWARN(WARN_LAYER))
943 Perl_warner(aTHX_ packWARN(WARN_LAYER),
944 "Invalid separator character %c%c%c in PerlIO layer specification %s",
946 SETERRNO(EINVAL, LIB_INVARG);
951 } while (isALNUM(*e));
967 * It's a nul terminated string, not allowed
968 * to \ the terminating null. Anything other
969 * character is passed over.
979 if (ckWARN(WARN_LAYER))
980 Perl_warner(aTHX_ packWARN(WARN_LAYER),
981 "Argument list not closed for PerlIO layer \"%.*s\"",
993 PerlIO_funcs * const layer =
994 PerlIO_find_layer(aTHX_ s, llen, 1);
996 PerlIO_list_push(aTHX_ av, layer,
1002 if (ckWARN(WARN_LAYER))
1003 Perl_warner(aTHX_ packWARN(WARN_LAYER), "Unknown PerlIO layer \"%.*s\"",
1016 PerlIO_default_buffer(pTHX_ PerlIO_list_t *av)
1019 PERLIO_FUNCS_DECL(*tab) = &PerlIO_perlio;
1020 #ifdef PERLIO_USING_CRLF
1023 if (PerlIO_stdio.Set_ptrcnt)
1024 tab = &PerlIO_stdio;
1026 PerlIO_debug("Pushing %s\n", tab->name);
1027 PerlIO_list_push(aTHX_ av, PerlIO_find_layer(aTHX_ tab->name, 0, 0),
1032 PerlIO_arg_fetch(PerlIO_list_t *av, IV n)
1034 return av->array[n].arg;
1038 PerlIO_layer_fetch(pTHX_ PerlIO_list_t *av, IV n, PerlIO_funcs *def)
1040 if (n >= 0 && n < av->cur) {
1041 PerlIO_debug("Layer %" IVdf " is %s\n", n,
1042 av->array[n].funcs->name);
1043 return av->array[n].funcs;
1046 Perl_croak(aTHX_ "panic: PerlIO layer array corrupt");
1051 PerlIOPop_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
1053 PERL_UNUSED_ARG(mode);
1054 PERL_UNUSED_ARG(arg);
1055 PERL_UNUSED_ARG(tab);
1056 if (PerlIOValid(f)) {
1058 PerlIO_pop(aTHX_ f);
1064 PERLIO_FUNCS_DECL(PerlIO_remove) = {
1065 sizeof(PerlIO_funcs),
1068 PERLIO_K_DUMMY | PERLIO_K_UTF8,
1088 NULL, /* get_base */
1089 NULL, /* get_bufsiz */
1092 NULL, /* set_ptrcnt */
1096 PerlIO_default_layers(pTHX)
1099 if (!PL_def_layerlist) {
1100 const char * const s = (PL_tainting) ? NULL : PerlEnv_getenv("PERLIO");
1101 PERLIO_FUNCS_DECL(*osLayer) = &PerlIO_unix;
1102 PL_def_layerlist = PerlIO_list_alloc(aTHX);
1103 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_unix));
1105 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_win32));
1107 osLayer = &PerlIO_win32;
1110 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_raw));
1111 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_perlio));
1112 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_stdio));
1113 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_crlf));
1115 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_mmap));
1117 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_utf8));
1118 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_remove));
1119 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_byte));
1120 PerlIO_list_push(aTHX_ PL_def_layerlist,
1121 PerlIO_find_layer(aTHX_ osLayer->name, 0, 0),
1124 PerlIO_parse_layers(aTHX_ PL_def_layerlist, s);
1127 PerlIO_default_buffer(aTHX_ PL_def_layerlist);
1130 if (PL_def_layerlist->cur < 2) {
1131 PerlIO_default_buffer(aTHX_ PL_def_layerlist);
1133 return PL_def_layerlist;
1137 Perl_boot_core_PerlIO(pTHX)
1139 #ifdef USE_ATTRIBUTES_FOR_PERLIO
1140 newXS("io::MODIFY_SCALAR_ATTRIBUTES", XS_io_MODIFY_SCALAR_ATTRIBUTES,
1143 newXS("PerlIO::Layer::find", XS_PerlIO__Layer__find, __FILE__);
1144 newXS("PerlIO::Layer::NoWarnings", XS_PerlIO__Layer__NoWarnings, __FILE__);
1148 PerlIO_default_layer(pTHX_ I32 n)
1151 PerlIO_list_t * const av = PerlIO_default_layers(aTHX);
1154 return PerlIO_layer_fetch(aTHX_ av, n, PERLIO_FUNCS_CAST(&PerlIO_stdio));
1157 #define PerlIO_default_top() PerlIO_default_layer(aTHX_ -1)
1158 #define PerlIO_default_btm() PerlIO_default_layer(aTHX_ 0)
1161 PerlIO_stdstreams(pTHX)
1165 PerlIO_allocate(aTHX);
1166 PerlIO_fdopen(0, "Ir" PERLIO_STDTEXT);
1167 PerlIO_fdopen(1, "Iw" PERLIO_STDTEXT);
1168 PerlIO_fdopen(2, "Iw" PERLIO_STDTEXT);
1173 PerlIO_push(pTHX_ PerlIO *f, PERLIO_FUNCS_DECL(*tab), const char *mode, SV *arg)
1175 if (tab->fsize != sizeof(PerlIO_funcs)) {
1177 Perl_croak(aTHX_ "Layer does not match this perl");
1181 if (tab->size < sizeof(PerlIOl)) {
1184 /* Real layer with a data area */
1185 Newxc(l,tab->size,char,PerlIOl);
1187 Zero(l, tab->size, char);
1189 l->tab = (PerlIO_funcs*) tab;
1191 PerlIO_debug("PerlIO_push f=%p %s %s %p\n", (void*)f, tab->name,
1192 (mode) ? mode : "(Null)", (void*)arg);
1193 if (*l->tab->Pushed &&
1194 (*l->tab->Pushed) (aTHX_ f, mode, arg, (PerlIO_funcs*) tab) != 0) {
1195 PerlIO_pop(aTHX_ f);
1201 /* Pseudo-layer where push does its own stack adjust */
1202 PerlIO_debug("PerlIO_push f=%p %s %s %p\n", (void*)f, tab->name,
1203 (mode) ? mode : "(Null)", (void*)arg);
1205 (*tab->Pushed) (aTHX_ f, mode, arg, (PerlIO_funcs*) tab) != 0) {
1213 PerlIOBase_binmode(pTHX_ PerlIO *f)
1215 if (PerlIOValid(f)) {
1216 /* Is layer suitable for raw stream ? */
1217 if (PerlIOBase(f)->tab->kind & PERLIO_K_RAW) {
1218 /* Yes - turn off UTF-8-ness, to undo UTF-8 locale effects */
1219 PerlIOBase(f)->flags &= ~PERLIO_F_UTF8;
1222 /* Not suitable - pop it */
1223 PerlIO_pop(aTHX_ f);
1231 PerlIORaw_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
1233 PERL_UNUSED_ARG(mode);
1234 PERL_UNUSED_ARG(arg);
1235 PERL_UNUSED_ARG(tab);
1237 if (PerlIOValid(f)) {
1242 * Strip all layers that are not suitable for a raw stream
1245 while (t && (l = *t)) {
1246 if (l->tab->Binmode) {
1247 /* Has a handler - normal case */
1248 if ((*l->tab->Binmode)(aTHX_ f) == 0) {
1250 /* Layer still there - move down a layer */
1259 /* No handler - pop it */
1260 PerlIO_pop(aTHX_ t);
1263 if (PerlIOValid(f)) {
1264 PerlIO_debug(":raw f=%p :%s\n", (void*)f, PerlIOBase(f)->tab->name);
1272 PerlIO_apply_layera(pTHX_ PerlIO *f, const char *mode,
1273 PerlIO_list_t *layers, IV n, IV max)
1277 PerlIO_funcs * const tab = PerlIO_layer_fetch(aTHX_ layers, n, NULL);
1279 if (!PerlIO_push(aTHX_ f, tab, mode, PerlIOArg)) {
1290 PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names)
1294 PerlIO_list_t * const layers = PerlIO_list_alloc(aTHX);
1295 code = PerlIO_parse_layers(aTHX_ layers, names);
1297 code = PerlIO_apply_layera(aTHX_ f, mode, layers, 0, layers->cur);
1299 PerlIO_list_free(aTHX_ layers);
1305 /*--------------------------------------------------------------------------------------*/
1307 * Given the abstraction above the public API functions
1311 PerlIO_binmode(pTHX_ PerlIO *f, int iotype, int mode, const char *names)
1313 PerlIO_debug("PerlIO_binmode f=%p %s %c %x %s\n", (void*)f,
1314 (PerlIOBase(f)) ? PerlIOBase(f)->tab->name : "(Null)",
1315 iotype, mode, (names) ? names : "(Null)");
1318 /* Do not flush etc. if (e.g.) switching encodings.
1319 if a pushed layer knows it needs to flush lower layers
1320 (for example :unix which is never going to call them)
1321 it can do the flush when it is pushed.
1323 return PerlIO_apply_layers(aTHX_ f, NULL, names) == 0 ? TRUE : FALSE;
1326 /* Fake 5.6 legacy of using this call to turn ON O_TEXT */
1327 #ifdef PERLIO_USING_CRLF
1328 /* Legacy binmode only has meaning if O_TEXT has a value distinct from
1329 O_BINARY so we can look for it in mode.
1331 if (!(mode & O_BINARY)) {
1333 /* FIXME?: Looking down the layer stack seems wrong,
1334 but is a way of reaching past (say) an encoding layer
1335 to flip CRLF-ness of the layer(s) below
1338 /* Perhaps we should turn on bottom-most aware layer
1339 e.g. Ilya's idea that UNIX TTY could serve
1341 if (PerlIOBase(f)->tab->kind & PERLIO_K_CANCRLF) {
1342 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF)) {
1343 /* Not in text mode - flush any pending stuff and flip it */
1345 PerlIOBase(f)->flags |= PERLIO_F_CRLF;
1347 /* Only need to turn it on in one layer so we are done */
1352 /* Not finding a CRLF aware layer presumably means we are binary
1353 which is not what was requested - so we failed
1354 We _could_ push :crlf layer but so could caller
1359 /* Legacy binmode is now _defined_ as being equivalent to pushing :raw
1360 So code that used to be here is now in PerlIORaw_pushed().
1362 return PerlIO_push(aTHX_ f, PERLIO_FUNCS_CAST(&PerlIO_raw), NULL, NULL) ? TRUE : FALSE;
1367 PerlIO__close(pTHX_ PerlIO *f)
1369 if (PerlIOValid(f)) {
1370 PerlIO_funcs * const tab = PerlIOBase(f)->tab;
1371 if (tab && tab->Close)
1372 return (*tab->Close)(aTHX_ f);
1374 return PerlIOBase_close(aTHX_ f);
1377 SETERRNO(EBADF, SS_IVCHAN);
1383 Perl_PerlIO_close(pTHX_ PerlIO *f)
1385 const int code = PerlIO__close(aTHX_ f);
1386 while (PerlIOValid(f)) {
1387 PerlIO_pop(aTHX_ f);
1393 Perl_PerlIO_fileno(pTHX_ PerlIO *f)
1396 Perl_PerlIO_or_Base(f, Fileno, fileno, -1, (aTHX_ f));
1400 PerlIO_context_layers(pTHX_ const char *mode)
1403 const char *type = NULL;
1405 * Need to supply default layer info from open.pm
1408 SV * const layers = PL_curcop->cop_io;
1411 type = SvPV_const(layers, len);
1412 if (type && mode[0] != 'r') {
1414 * Skip to write part
1416 const char * const s = strchr(type, 0);
1417 if (s && (STRLEN)(s - type) < len) {
1426 static PerlIO_funcs *
1427 PerlIO_layer_from_ref(pTHX_ SV *sv)
1431 * For any scalar type load the handler which is bundled with perl
1433 if (SvTYPE(sv) < SVt_PVAV)
1434 return PerlIO_find_layer(aTHX_ STR_WITH_LEN("scalar"), 1);
1437 * For other types allow if layer is known but don't try and load it
1439 switch (SvTYPE(sv)) {
1441 return PerlIO_find_layer(aTHX_ STR_WITH_LEN("Array"), 0);
1443 return PerlIO_find_layer(aTHX_ STR_WITH_LEN("Hash"), 0);
1445 return PerlIO_find_layer(aTHX_ STR_WITH_LEN("Code"), 0);
1447 return PerlIO_find_layer(aTHX_ STR_WITH_LEN("Glob"), 0);
1453 PerlIO_resolve_layers(pTHX_ const char *layers,
1454 const char *mode, int narg, SV **args)
1457 PerlIO_list_t *def = PerlIO_default_layers(aTHX);
1460 PerlIO_stdstreams(aTHX);
1462 SV * const arg = *args;
1464 * If it is a reference but not an object see if we have a handler
1467 if (SvROK(arg) && !sv_isobject(arg)) {
1468 PerlIO_funcs * const handler = PerlIO_layer_from_ref(aTHX_ SvRV(arg));
1470 def = PerlIO_list_alloc(aTHX);
1471 PerlIO_list_push(aTHX_ def, handler, &PL_sv_undef);
1475 * Don't fail if handler cannot be found :via(...) etc. may do
1476 * something sensible else we will just stringfy and open
1481 if (!layers || !*layers)
1482 layers = PerlIO_context_layers(aTHX_ mode);
1483 if (layers && *layers) {
1487 av = PerlIO_list_alloc(aTHX);
1488 for (i = 0; i < def->cur; i++) {
1489 PerlIO_list_push(aTHX_ av, def->array[i].funcs,
1496 if (PerlIO_parse_layers(aTHX_ av, layers) == 0) {
1500 PerlIO_list_free(aTHX_ av);
1512 PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd,
1513 int imode, int perm, PerlIO *f, int narg, SV **args)
1516 if (!f && narg == 1 && *args == &PL_sv_undef) {
1517 if ((f = PerlIO_tmpfile())) {
1518 if (!layers || !*layers)
1519 layers = PerlIO_context_layers(aTHX_ mode);
1520 if (layers && *layers)
1521 PerlIO_apply_layers(aTHX_ f, mode, layers);
1525 PerlIO_list_t *layera;
1527 PerlIO_funcs *tab = NULL;
1528 if (PerlIOValid(f)) {
1530 * This is "reopen" - it is not tested as perl does not use it
1534 layera = PerlIO_list_alloc(aTHX);
1536 SV * const arg = (l->tab->Getarg)
1537 ? (*l->tab->Getarg) (aTHX_ &l, NULL, 0)
1539 PerlIO_list_push(aTHX_ layera, l->tab, arg);
1540 l = *PerlIONext(&l);
1544 layera = PerlIO_resolve_layers(aTHX_ layers, mode, narg, args);
1550 * Start at "top" of layer stack
1552 n = layera->cur - 1;
1554 PerlIO_funcs * const t = PerlIO_layer_fetch(aTHX_ layera, n, NULL);
1563 * Found that layer 'n' can do opens - call it
1565 if (narg > 1 && !(tab->kind & PERLIO_K_MULTIARG)) {
1566 Perl_croak(aTHX_ "More than one argument to open(,':%s')",tab->name);
1568 PerlIO_debug("openn(%s,'%s','%s',%d,%x,%o,%p,%d,%p)\n",
1569 tab->name, layers ? layers : "(Null)", mode, fd,
1570 imode, perm, (void*)f, narg, (void*)args);
1572 f = (*tab->Open) (aTHX_ tab, layera, n, mode, fd, imode, perm,
1575 SETERRNO(EINVAL, LIB_INVARG);
1579 if (n + 1 < layera->cur) {
1581 * More layers above the one that we used to open -
1584 if (PerlIO_apply_layera(aTHX_ f, mode, layera, n + 1, layera->cur) != 0) {
1585 /* If pushing layers fails close the file */
1592 PerlIO_list_free(aTHX_ layera);
1599 Perl_PerlIO_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
1601 Perl_PerlIO_or_Base(f, Read, read, -1, (aTHX_ f, vbuf, count));
1605 Perl_PerlIO_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
1607 Perl_PerlIO_or_Base(f, Unread, unread, -1, (aTHX_ f, vbuf, count));
1611 Perl_PerlIO_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
1613 Perl_PerlIO_or_fail(f, Write, -1, (aTHX_ f, vbuf, count));
1617 Perl_PerlIO_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
1619 Perl_PerlIO_or_fail(f, Seek, -1, (aTHX_ f, offset, whence));
1623 Perl_PerlIO_tell(pTHX_ PerlIO *f)
1625 Perl_PerlIO_or_fail(f, Tell, -1, (aTHX_ f));
1629 Perl_PerlIO_flush(pTHX_ PerlIO *f)
1634 const PerlIO_funcs *tab = PerlIOBase(f)->tab;
1636 if (tab && tab->Flush)
1637 return (*tab->Flush) (aTHX_ f);
1639 return 0; /* If no Flush defined, silently succeed. */
1642 PerlIO_debug("Cannot flush f=%p\n", (void*)f);
1643 SETERRNO(EBADF, SS_IVCHAN);
1649 * Is it good API design to do flush-all on NULL, a potentially
1650 * errorneous input? Maybe some magical value (PerlIO*
1651 * PERLIO_FLUSH_ALL = (PerlIO*)-1;)? Yes, stdio does similar
1652 * things on fflush(NULL), but should we be bound by their design
1655 PerlIO **table = &PL_perlio;
1657 while ((f = *table)) {
1659 table = (PerlIO **) (f++);
1660 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
1661 if (*f && PerlIO_flush(f) != 0)
1671 PerlIOBase_flush_linebuf(pTHX)
1674 PerlIO **table = &PL_perlio;
1676 while ((f = *table)) {
1678 table = (PerlIO **) (f++);
1679 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
1682 flags & (PERLIO_F_LINEBUF | PERLIO_F_CANWRITE))
1683 == (PERLIO_F_LINEBUF | PERLIO_F_CANWRITE))
1691 Perl_PerlIO_fill(pTHX_ PerlIO *f)
1693 Perl_PerlIO_or_fail(f, Fill, -1, (aTHX_ f));
1697 PerlIO_isutf8(PerlIO *f)
1700 return (PerlIOBase(f)->flags & PERLIO_F_UTF8) != 0;
1702 SETERRNO(EBADF, SS_IVCHAN);
1708 Perl_PerlIO_eof(pTHX_ PerlIO *f)
1710 Perl_PerlIO_or_Base(f, Eof, eof, -1, (aTHX_ f));
1714 Perl_PerlIO_error(pTHX_ PerlIO *f)
1716 Perl_PerlIO_or_Base(f, Error, error, -1, (aTHX_ f));
1720 Perl_PerlIO_clearerr(pTHX_ PerlIO *f)
1722 Perl_PerlIO_or_Base_void(f, Clearerr, clearerr, (aTHX_ f));
1726 Perl_PerlIO_setlinebuf(pTHX_ PerlIO *f)
1728 Perl_PerlIO_or_Base_void(f, Setlinebuf, setlinebuf, (aTHX_ f));
1732 PerlIO_has_base(PerlIO *f)
1734 if (PerlIOValid(f)) {
1735 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
1738 return (tab->Get_base != NULL);
1739 SETERRNO(EINVAL, LIB_INVARG);
1742 SETERRNO(EBADF, SS_IVCHAN);
1748 PerlIO_fast_gets(PerlIO *f)
1750 if (PerlIOValid(f) && (PerlIOBase(f)->flags & PERLIO_F_FASTGETS)) {
1751 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
1754 return (tab->Set_ptrcnt != NULL);
1755 SETERRNO(EINVAL, LIB_INVARG);
1758 SETERRNO(EBADF, SS_IVCHAN);
1764 PerlIO_has_cntptr(PerlIO *f)
1766 if (PerlIOValid(f)) {
1767 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
1770 return (tab->Get_ptr != NULL && tab->Get_cnt != NULL);
1771 SETERRNO(EINVAL, LIB_INVARG);
1774 SETERRNO(EBADF, SS_IVCHAN);
1780 PerlIO_canset_cnt(PerlIO *f)
1782 if (PerlIOValid(f)) {
1783 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
1786 return (tab->Set_ptrcnt != NULL);
1787 SETERRNO(EINVAL, LIB_INVARG);
1790 SETERRNO(EBADF, SS_IVCHAN);
1796 Perl_PerlIO_get_base(pTHX_ PerlIO *f)
1798 Perl_PerlIO_or_fail(f, Get_base, NULL, (aTHX_ f));
1802 Perl_PerlIO_get_bufsiz(pTHX_ PerlIO *f)
1804 Perl_PerlIO_or_fail(f, Get_bufsiz, -1, (aTHX_ f));
1808 Perl_PerlIO_get_ptr(pTHX_ PerlIO *f)
1810 Perl_PerlIO_or_fail(f, Get_ptr, NULL, (aTHX_ f));
1814 Perl_PerlIO_get_cnt(pTHX_ PerlIO *f)
1816 Perl_PerlIO_or_fail(f, Get_cnt, -1, (aTHX_ f));
1820 Perl_PerlIO_set_cnt(pTHX_ PerlIO *f, int cnt)
1822 Perl_PerlIO_or_fail_void(f, Set_ptrcnt, (aTHX_ f, NULL, cnt));
1826 Perl_PerlIO_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, int cnt)
1828 Perl_PerlIO_or_fail_void(f, Set_ptrcnt, (aTHX_ f, ptr, cnt));
1832 /*--------------------------------------------------------------------------------------*/
1834 * utf8 and raw dummy layers
1838 PerlIOUtf8_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
1840 PERL_UNUSED_CONTEXT;
1841 PERL_UNUSED_ARG(mode);
1842 PERL_UNUSED_ARG(arg);
1843 if (PerlIOValid(f)) {
1844 if (tab->kind & PERLIO_K_UTF8)
1845 PerlIOBase(f)->flags |= PERLIO_F_UTF8;
1847 PerlIOBase(f)->flags &= ~PERLIO_F_UTF8;
1853 PERLIO_FUNCS_DECL(PerlIO_utf8) = {
1854 sizeof(PerlIO_funcs),
1857 PERLIO_K_DUMMY | PERLIO_K_UTF8,
1877 NULL, /* get_base */
1878 NULL, /* get_bufsiz */
1881 NULL, /* set_ptrcnt */
1884 PERLIO_FUNCS_DECL(PerlIO_byte) = {
1885 sizeof(PerlIO_funcs),
1908 NULL, /* get_base */
1909 NULL, /* get_bufsiz */
1912 NULL, /* set_ptrcnt */
1916 PerlIORaw_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
1917 IV n, const char *mode, int fd, int imode, int perm,
1918 PerlIO *old, int narg, SV **args)
1920 PerlIO_funcs * const tab = PerlIO_default_btm();
1921 PERL_UNUSED_ARG(self);
1922 if (tab && tab->Open)
1923 return (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
1925 SETERRNO(EINVAL, LIB_INVARG);
1929 PERLIO_FUNCS_DECL(PerlIO_raw) = {
1930 sizeof(PerlIO_funcs),
1953 NULL, /* get_base */
1954 NULL, /* get_bufsiz */
1957 NULL, /* set_ptrcnt */
1959 /*--------------------------------------------------------------------------------------*/
1960 /*--------------------------------------------------------------------------------------*/
1962 * "Methods" of the "base class"
1966 PerlIOBase_fileno(pTHX_ PerlIO *f)
1968 return PerlIOValid(f) ? PerlIO_fileno(PerlIONext(f)) : -1;
1972 PerlIO_modestr(PerlIO * f, char *buf)
1975 if (PerlIOValid(f)) {
1976 const IV flags = PerlIOBase(f)->flags;
1977 if (flags & PERLIO_F_APPEND) {
1979 if (flags & PERLIO_F_CANREAD) {
1983 else if (flags & PERLIO_F_CANREAD) {
1985 if (flags & PERLIO_F_CANWRITE)
1988 else if (flags & PERLIO_F_CANWRITE) {
1990 if (flags & PERLIO_F_CANREAD) {
1994 #ifdef PERLIO_USING_CRLF
1995 if (!(flags & PERLIO_F_CRLF))
2005 PerlIOBase_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
2007 PerlIOl * const l = PerlIOBase(f);
2008 PERL_UNUSED_CONTEXT;
2009 PERL_UNUSED_ARG(arg);
2011 l->flags &= ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE |
2012 PERLIO_F_TRUNCATE | PERLIO_F_APPEND);
2013 if (tab->Set_ptrcnt != NULL)
2014 l->flags |= PERLIO_F_FASTGETS;
2016 if (*mode == IoTYPE_NUMERIC || *mode == IoTYPE_IMPLICIT)
2020 l->flags |= PERLIO_F_CANREAD;
2023 l->flags |= PERLIO_F_APPEND | PERLIO_F_CANWRITE;
2026 l->flags |= PERLIO_F_TRUNCATE | PERLIO_F_CANWRITE;
2029 SETERRNO(EINVAL, LIB_INVARG);
2035 l->flags |= PERLIO_F_CANREAD | PERLIO_F_CANWRITE;
2038 l->flags &= ~PERLIO_F_CRLF;
2041 l->flags |= PERLIO_F_CRLF;
2044 SETERRNO(EINVAL, LIB_INVARG);
2051 l->flags |= l->next->flags &
2052 (PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_TRUNCATE |
2057 PerlIO_debug("PerlIOBase_pushed f=%p %s %s fl=%08" UVxf " (%s)\n",
2058 f, PerlIOBase(f)->tab->name, (omode) ? omode : "(Null)",
2059 l->flags, PerlIO_modestr(f, temp));
2065 PerlIOBase_popped(pTHX_ PerlIO *f)
2067 PERL_UNUSED_CONTEXT;
2073 PerlIOBase_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
2076 * Save the position as current head considers it
2078 const Off_t old = PerlIO_tell(f);
2079 PerlIO_push(aTHX_ f, PERLIO_FUNCS_CAST(&PerlIO_pending), "r", NULL);
2080 PerlIOSelf(f, PerlIOBuf)->posn = old;
2081 return PerlIOBuf_unread(aTHX_ f, vbuf, count);
2085 PerlIOBase_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
2087 STDCHAR *buf = (STDCHAR *) vbuf;
2089 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD)) {
2090 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2091 SETERRNO(EBADF, SS_IVCHAN);
2097 SSize_t avail = PerlIO_get_cnt(f);
2100 take = ((SSize_t)count < avail) ? count : avail;
2102 STDCHAR *ptr = PerlIO_get_ptr(f);
2103 Copy(ptr, buf, take, STDCHAR);
2104 PerlIO_set_ptrcnt(f, ptr + take, (avail -= take));
2107 if (avail == 0) /* set_ptrcnt could have reset avail */
2110 if (count > 0 && avail <= 0) {
2111 if (PerlIO_fill(f) != 0)
2116 return (buf - (STDCHAR *) vbuf);
2122 PerlIOBase_noop_ok(pTHX_ PerlIO *f)
2124 PERL_UNUSED_CONTEXT;
2130 PerlIOBase_noop_fail(pTHX_ PerlIO *f)
2132 PERL_UNUSED_CONTEXT;
2138 PerlIOBase_close(pTHX_ PerlIO *f)
2141 if (PerlIOValid(f)) {
2142 PerlIO *n = PerlIONext(f);
2143 code = PerlIO_flush(f);
2144 PerlIOBase(f)->flags &=
2145 ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_OPEN);
2146 while (PerlIOValid(n)) {
2147 const PerlIO_funcs * const tab = PerlIOBase(n)->tab;
2148 if (tab && tab->Close) {
2149 if ((*tab->Close)(aTHX_ n) != 0)
2154 PerlIOBase(n)->flags &=
2155 ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_OPEN);
2161 SETERRNO(EBADF, SS_IVCHAN);
2167 PerlIOBase_eof(pTHX_ PerlIO *f)
2169 PERL_UNUSED_CONTEXT;
2170 if (PerlIOValid(f)) {
2171 return (PerlIOBase(f)->flags & PERLIO_F_EOF) != 0;
2177 PerlIOBase_error(pTHX_ PerlIO *f)
2179 PERL_UNUSED_CONTEXT;
2180 if (PerlIOValid(f)) {
2181 return (PerlIOBase(f)->flags & PERLIO_F_ERROR) != 0;
2187 PerlIOBase_clearerr(pTHX_ PerlIO *f)
2189 if (PerlIOValid(f)) {
2190 PerlIO * const n = PerlIONext(f);
2191 PerlIOBase(f)->flags &= ~(PERLIO_F_ERROR | PERLIO_F_EOF);
2198 PerlIOBase_setlinebuf(pTHX_ PerlIO *f)
2200 PERL_UNUSED_CONTEXT;
2201 if (PerlIOValid(f)) {
2202 PerlIOBase(f)->flags |= PERLIO_F_LINEBUF;
2207 PerlIO_sv_dup(pTHX_ SV *arg, CLONE_PARAMS *param)
2213 return sv_dup(arg, param);
2216 return newSVsv(arg);
2219 PERL_UNUSED_ARG(param);
2220 return newSVsv(arg);
2225 PerlIOBase_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
2227 PerlIO * const nexto = PerlIONext(o);
2228 if (PerlIOValid(nexto)) {
2229 const PerlIO_funcs * const tab = PerlIOBase(nexto)->tab;
2230 if (tab && tab->Dup)
2231 f = (*tab->Dup)(aTHX_ f, nexto, param, flags);
2233 f = PerlIOBase_dup(aTHX_ f, nexto, param, flags);
2236 PerlIO_funcs * const self = PerlIOBase(o)->tab;
2239 PerlIO_debug("PerlIOBase_dup %s f=%p o=%p param=%p\n",
2240 self->name, (void*)f, (void*)o, (void*)param);
2242 arg = (*self->Getarg)(aTHX_ o, param, flags);
2246 f = PerlIO_push(aTHX_ f, self, PerlIO_modestr(o,buf), arg);
2255 perl_mutex PerlIO_mutex;
2258 /* PL_perlio_fd_refcnt[] is in intrpvar.h */
2260 /* Must be called with PerlIO_mutex locked. */
2262 S_more_refcounted_fds(pTHX_ const int new_fd) {
2263 const int old_max = PL_perlio_fd_refcnt_size;
2264 const int new_max = 16 + (new_fd & ~15);
2267 PerlIO_debug("More fds - old=%d, need %d, new=%d\n",
2268 old_max, new_fd, new_max);
2270 if (new_fd < old_max) {
2274 assert (new_max > new_fd);
2277 = PerlMemShared_realloc(PL_perlio_fd_refcnt, new_max * sizeof(int));
2281 MUTEX_UNLOCK(&PerlIO_mutex);
2283 /* Can't use PerlIO to write as it allocates memory */
2284 PerlLIO_write(PerlIO_fileno(Perl_error_log),
2285 PL_no_mem, strlen(PL_no_mem));
2289 PL_perlio_fd_refcnt_size = new_max;
2290 PL_perlio_fd_refcnt = new_array;
2292 PerlIO_debug("Zeroing %p, %d\n", new_array + old_max, new_max - old_max);
2294 Zero(new_array + old_max, new_max - old_max, int);
2301 /* Place holder for stdstreams call ??? */
2303 MUTEX_INIT(&PerlIO_mutex);
2305 PERL_UNUSED_CONTEXT;
2310 PerlIOUnix_refcnt_inc(int fd)
2317 MUTEX_LOCK(&PerlIO_mutex);
2319 if (fd >= PL_perlio_fd_refcnt_size)
2320 S_more_refcounted_fds(aTHX_ fd);
2322 PL_perlio_fd_refcnt[fd]++;
2323 PerlIO_debug("fd %d refcnt=%d\n",fd,PL_perlio_fd_refcnt[fd]);
2326 MUTEX_UNLOCK(&PerlIO_mutex);
2332 PerlIOUnix_refcnt_dec(int fd)
2339 MUTEX_LOCK(&PerlIO_mutex);
2341 /* XXX should this be a panic? */
2342 if (fd >= PL_perlio_fd_refcnt_size)
2343 S_more_refcounted_fds(aTHX_ fd);
2345 /* XXX should this be a panic if it drops below 0? */
2346 cnt = --PL_perlio_fd_refcnt[fd];
2347 PerlIO_debug("fd %d refcnt=%d\n",fd,cnt);
2349 MUTEX_UNLOCK(&PerlIO_mutex);
2356 PerlIO_cleanup(pTHX)
2361 PerlIO_debug("Cleanup layers for %p\n",aTHX);
2363 PerlIO_debug("Cleanup layers\n");
2365 /* Raise STDIN..STDERR refcount so we don't close them */
2366 for (i=0; i < 3; i++)
2367 PerlIOUnix_refcnt_inc(i);
2368 PerlIO_cleantable(aTHX_ &PL_perlio);
2369 /* Restore STDIN..STDERR refcount */
2370 for (i=0; i < 3; i++)
2371 PerlIOUnix_refcnt_dec(i);
2373 if (PL_known_layers) {
2374 PerlIO_list_free(aTHX_ PL_known_layers);
2375 PL_known_layers = NULL;
2377 if (PL_def_layerlist) {
2378 PerlIO_list_free(aTHX_ PL_def_layerlist);
2379 PL_def_layerlist = NULL;
2385 /*--------------------------------------------------------------------------------------*/
2387 * Bottom-most level for UNIX-like case
2391 struct _PerlIO base; /* The generic part */
2392 int fd; /* UNIX like file descriptor */
2393 int oflags; /* open/fcntl flags */
2397 PerlIOUnix_oflags(const char *mode)
2400 if (*mode == IoTYPE_IMPLICIT || *mode == IoTYPE_NUMERIC)
2405 if (*++mode == '+') {
2412 oflags = O_CREAT | O_TRUNC;
2413 if (*++mode == '+') {
2422 oflags = O_CREAT | O_APPEND;
2423 if (*++mode == '+') {
2436 else if (*mode == 't') {
2438 oflags &= ~O_BINARY;
2442 * Always open in binary mode
2445 if (*mode || oflags == -1) {
2446 SETERRNO(EINVAL, LIB_INVARG);
2453 PerlIOUnix_fileno(pTHX_ PerlIO *f)
2455 PERL_UNUSED_CONTEXT;
2456 return PerlIOSelf(f, PerlIOUnix)->fd;
2460 PerlIOUnix_setfd(pTHX_ PerlIO *f, int fd, int imode)
2462 PerlIOUnix * const s = PerlIOSelf(f, PerlIOUnix);
2465 if (PerlLIO_fstat(fd, &st) == 0) {
2466 if (!S_ISREG(st.st_mode)) {
2467 PerlIO_debug("%d is not regular file\n",fd);
2468 PerlIOBase(f)->flags |= PERLIO_F_NOTREG;
2471 PerlIO_debug("%d _is_ a regular file\n",fd);
2477 PerlIOUnix_refcnt_inc(fd);
2478 PERL_UNUSED_CONTEXT;
2482 PerlIOUnix_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
2484 IV code = PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
2485 if (*PerlIONext(f)) {
2486 /* We never call down so do any pending stuff now */
2487 PerlIO_flush(PerlIONext(f));
2489 * XXX could (or should) we retrieve the oflags from the open file
2490 * handle rather than believing the "mode" we are passed in? XXX
2491 * Should the value on NULL mode be 0 or -1?
2493 PerlIOUnix_setfd(aTHX_ f, PerlIO_fileno(PerlIONext(f)),
2494 mode ? PerlIOUnix_oflags(mode) : -1);
2496 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
2502 PerlIOUnix_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
2504 const int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2506 PERL_UNUSED_CONTEXT;
2507 if (PerlIOBase(f)->flags & PERLIO_F_NOTREG) {
2509 SETERRNO(ESPIPE, LIB_INVARG);
2511 SETERRNO(EINVAL, LIB_INVARG);
2515 new_loc = PerlLIO_lseek(fd, offset, whence);
2516 if (new_loc == (Off_t) - 1)
2518 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
2523 PerlIOUnix_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2524 IV n, const char *mode, int fd, int imode,
2525 int perm, PerlIO *f, int narg, SV **args)
2527 if (PerlIOValid(f)) {
2528 if (PerlIOBase(f)->flags & PERLIO_F_OPEN)
2529 (*PerlIOBase(f)->tab->Close)(aTHX_ f);
2532 if (*mode == IoTYPE_NUMERIC)
2535 imode = PerlIOUnix_oflags(mode);
2539 const char *path = SvPV_nolen_const(*args);
2540 fd = PerlLIO_open3(path, imode, perm);
2544 if (*mode == IoTYPE_IMPLICIT)
2547 f = PerlIO_allocate(aTHX);
2549 if (!PerlIOValid(f)) {
2550 if (!(f = PerlIO_push(aTHX_ f, self, mode, PerlIOArg))) {
2554 PerlIOUnix_setfd(aTHX_ f, fd, imode);
2555 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
2556 if (*mode == IoTYPE_APPEND)
2557 PerlIOUnix_seek(aTHX_ f, 0, SEEK_END);
2564 * FIXME: pop layers ???
2572 PerlIOUnix_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
2574 const PerlIOUnix * const os = PerlIOSelf(o, PerlIOUnix);
2576 if (flags & PERLIO_DUP_FD) {
2577 fd = PerlLIO_dup(fd);
2580 f = PerlIOBase_dup(aTHX_ f, o, param, flags);
2582 /* If all went well overwrite fd in dup'ed lay with the dup()'ed fd */
2583 PerlIOUnix_setfd(aTHX_ f, fd, os->oflags);
2592 PerlIOUnix_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
2595 const int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2596 #ifdef PERLIO_STD_SPECIAL
2598 return PERLIO_STD_IN(fd, vbuf, count);
2600 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD) ||
2601 PerlIOBase(f)->flags & (PERLIO_F_EOF|PERLIO_F_ERROR)) {
2605 const SSize_t len = PerlLIO_read(fd, vbuf, count);
2606 if (len >= 0 || errno != EINTR) {
2608 if (errno != EAGAIN) {
2609 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2612 else if (len == 0 && count != 0) {
2613 PerlIOBase(f)->flags |= PERLIO_F_EOF;
2624 PerlIOUnix_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
2627 const int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2628 #ifdef PERLIO_STD_SPECIAL
2629 if (fd == 1 || fd == 2)
2630 return PERLIO_STD_OUT(fd, vbuf, count);
2633 const SSize_t len = PerlLIO_write(fd, vbuf, count);
2634 if (len >= 0 || errno != EINTR) {
2636 if (errno != EAGAIN) {
2637 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2648 PerlIOUnix_tell(pTHX_ PerlIO *f)
2650 PERL_UNUSED_CONTEXT;
2652 return PerlLIO_lseek(PerlIOSelf(f, PerlIOUnix)->fd, 0, SEEK_CUR);
2657 PerlIOUnix_close(pTHX_ PerlIO *f)
2660 const int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2662 if (PerlIOBase(f)->flags & PERLIO_F_OPEN) {
2663 if (PerlIOUnix_refcnt_dec(fd) > 0) {
2664 PerlIOBase(f)->flags &= ~PERLIO_F_OPEN;
2669 SETERRNO(EBADF,SS_IVCHAN);
2672 while (PerlLIO_close(fd) != 0) {
2673 if (errno != EINTR) {
2680 PerlIOBase(f)->flags &= ~PERLIO_F_OPEN;
2685 PERLIO_FUNCS_DECL(PerlIO_unix) = {
2686 sizeof(PerlIO_funcs),
2693 PerlIOBase_binmode, /* binmode */
2703 PerlIOBase_noop_ok, /* flush */
2704 PerlIOBase_noop_fail, /* fill */
2707 PerlIOBase_clearerr,
2708 PerlIOBase_setlinebuf,
2709 NULL, /* get_base */
2710 NULL, /* get_bufsiz */
2713 NULL, /* set_ptrcnt */
2716 /*--------------------------------------------------------------------------------------*/
2721 #if defined(VMS) && !defined(STDIO_BUFFER_WRITABLE)
2722 /* perl5.8 - This ensures the last minute VMS ungetc fix is not
2723 broken by the last second glibc 2.3 fix
2725 #define STDIO_BUFFER_WRITABLE
2730 struct _PerlIO base;
2731 FILE *stdio; /* The stream */
2735 PerlIOStdio_fileno(pTHX_ PerlIO *f)
2737 PERL_UNUSED_CONTEXT;
2739 if (PerlIOValid(f)) {
2740 FILE * const s = PerlIOSelf(f, PerlIOStdio)->stdio;
2742 return PerlSIO_fileno(s);
2749 PerlIOStdio_mode(const char *mode, char *tmode)
2751 char * const ret = tmode;
2757 #if defined(PERLIO_USING_CRLF) || defined(__CYGWIN__)
2765 PerlIOStdio_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
2768 if (PerlIOValid(f) && PerlIOValid(n = PerlIONext(f))) {
2769 PerlIO_funcs * const toptab = PerlIOBase(n)->tab;
2770 if (toptab == tab) {
2771 /* Top is already stdio - pop self (duplicate) and use original */
2772 PerlIO_pop(aTHX_ f);
2775 const int fd = PerlIO_fileno(n);
2778 if (fd >= 0 && (stdio = PerlSIO_fdopen(fd,
2779 mode = PerlIOStdio_mode(mode, tmode)))) {
2780 PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
2781 /* We never call down so do any pending stuff now */
2782 PerlIO_flush(PerlIONext(f));
2789 return PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
2794 PerlIO_importFILE(FILE *stdio, const char *mode)
2800 if (!mode || !*mode) {
2801 /* We need to probe to see how we can open the stream
2802 so start with read/write and then try write and read
2803 we dup() so that we can fclose without loosing the fd.
2805 Note that the errno value set by a failing fdopen
2806 varies between stdio implementations.
2808 const int fd = PerlLIO_dup(fileno(stdio));
2809 FILE *f2 = PerlSIO_fdopen(fd, (mode = "r+"));
2811 f2 = PerlSIO_fdopen(fd, (mode = "w"));
2814 f2 = PerlSIO_fdopen(fd, (mode = "r"));
2817 /* Don't seem to be able to open */
2823 if ((f = PerlIO_push(aTHX_(f = PerlIO_allocate(aTHX)), PERLIO_FUNCS_CAST(&PerlIO_stdio), mode, NULL))) {
2824 s = PerlIOSelf(f, PerlIOStdio);
2832 PerlIOStdio_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2833 IV n, const char *mode, int fd, int imode,
2834 int perm, PerlIO *f, int narg, SV **args)
2837 if (PerlIOValid(f)) {
2838 const char * const path = SvPV_nolen_const(*args);
2839 PerlIOStdio * const s = PerlIOSelf(f, PerlIOStdio);
2841 PerlIOUnix_refcnt_dec(fileno(s->stdio));
2842 stdio = PerlSIO_freopen(path, (mode = PerlIOStdio_mode(mode, tmode)),
2847 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2852 const char * const path = SvPV_nolen_const(*args);
2853 if (*mode == IoTYPE_NUMERIC) {
2855 fd = PerlLIO_open3(path, imode, perm);
2859 bool appended = FALSE;
2861 /* Cygwin wants its 'b' early. */
2863 mode = PerlIOStdio_mode(mode, tmode);
2865 stdio = PerlSIO_fopen(path, mode);
2869 f = PerlIO_allocate(aTHX);
2872 mode = PerlIOStdio_mode(mode, tmode);
2873 f = PerlIO_push(aTHX_ f, self, mode, PerlIOArg);
2875 s = PerlIOSelf(f, PerlIOStdio);
2877 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2889 if (*mode == IoTYPE_IMPLICIT) {
2896 stdio = PerlSIO_stdin;
2899 stdio = PerlSIO_stdout;
2902 stdio = PerlSIO_stderr;
2907 stdio = PerlSIO_fdopen(fd, mode =
2908 PerlIOStdio_mode(mode, tmode));
2912 f = PerlIO_allocate(aTHX);
2914 if ((f = PerlIO_push(aTHX_ f, self, mode, PerlIOArg))) {
2915 PerlIOStdio * const s = PerlIOSelf(f, PerlIOStdio);
2917 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2927 PerlIOStdio_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
2929 /* This assumes no layers underneath - which is what
2930 happens, but is not how I remember it. NI-S 2001/10/16
2932 if ((f = PerlIOBase_dup(aTHX_ f, o, param, flags))) {
2933 FILE *stdio = PerlIOSelf(o, PerlIOStdio)->stdio;
2934 const int fd = fileno(stdio);
2936 if (flags & PERLIO_DUP_FD) {
2937 const int dfd = PerlLIO_dup(fileno(stdio));
2939 stdio = PerlSIO_fdopen(dfd, PerlIO_modestr(o,mode));
2944 /* FIXME: To avoid messy error recovery if dup fails
2945 re-use the existing stdio as though flag was not set
2949 stdio = PerlSIO_fdopen(fd, PerlIO_modestr(o,mode));
2951 PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
2952 PerlIOUnix_refcnt_inc(fileno(stdio));
2958 PerlIOStdio_invalidate_fileno(pTHX_ FILE *f)
2960 PERL_UNUSED_CONTEXT;
2962 /* XXX this could use PerlIO_canset_fileno() and
2963 * PerlIO_set_fileno() support from Configure
2965 # if defined(__UCLIBC__)
2966 /* uClibc must come before glibc because it defines __GLIBC__ as well. */
2969 # elif defined(__GLIBC__)
2970 /* There may be a better way for GLIBC:
2971 - libio.h defines a flag to not close() on cleanup
2975 # elif defined(__sun__)
2977 # elif defined(__hpux)
2981 /* Next one ->_file seems to be a reasonable fallback, i.e. if
2982 your platform does not have special entry try this one.
2983 [For OSF only have confirmation for Tru64 (alpha)
2984 but assume other OSFs will be similar.]
2986 # elif defined(_AIX) || defined(__osf__) || defined(__irix__)
2989 # elif defined(__FreeBSD__)
2990 /* There may be a better way on FreeBSD:
2991 - we could insert a dummy func in the _close function entry
2992 f->_close = (int (*)(void *)) dummy_close;
2996 # elif defined(__OpenBSD__)
2997 /* There may be a better way on OpenBSD:
2998 - we could insert a dummy func in the _close function entry
2999 f->_close = (int (*)(void *)) dummy_close;
3003 # elif defined(__EMX__)
3004 /* f->_flags &= ~_IOOPEN; */ /* Will leak stream->_buffer */
3007 # elif defined(__CYGWIN__)
3008 /* There may be a better way on CYGWIN:
3009 - we could insert a dummy func in the _close function entry
3010 f->_close = (int (*)(void *)) dummy_close;
3014 # elif defined(WIN32)
3015 # if defined(__BORLANDC__)
3016 f->fd = PerlLIO_dup(fileno(f));
3017 # elif defined(UNDER_CE)
3018 /* WIN_CE does not have access to FILE internals, it hardly has FILE
3027 /* Sarathy's code did this - we fall back to a dup/dup2 hack
3028 (which isn't thread safe) instead
3030 # error "Don't know how to set FILE.fileno on your platform"
3038 PerlIOStdio_close(pTHX_ PerlIO *f)
3040 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3046 const int fd = fileno(stdio);
3051 #ifdef SOCKS5_VERSION_NAME
3052 /* Socks lib overrides close() but stdio isn't linked to
3053 that library (though we are) - so we must call close()
3054 on sockets on stdio's behalf.
3057 Sock_size_t optlen = sizeof(int);
3058 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, (void *) &optval, &optlen) == 0)
3061 if (PerlIOUnix_refcnt_dec(fd) > 0) /* File descriptor still in use */
3064 /* For STD* handles, don't close stdio, since we shared the FILE *, too. */
3065 if (stdio == stdin) /* Some stdios are buggy fflush-ing inputs */
3067 if (stdio == stdout || stdio == stderr)
3068 return PerlIO_flush(f);
3069 /* Tricky - must fclose(stdio) to free memory but not close(fd)
3070 Use Sarathy's trick from maint-5.6 to invalidate the
3071 fileno slot of the FILE *
3073 result = PerlIO_flush(f);
3075 invalidate = PerlIOStdio_invalidate_fileno(aTHX_ stdio);
3077 dupfd = PerlLIO_dup(fd);
3079 result = PerlSIO_fclose(stdio);
3080 /* We treat error from stdio as success if we invalidated
3081 errno may NOT be expected EBADF
3083 if (invalidate && result != 0) {
3087 #ifdef SOCKS5_VERSION_NAME
3088 /* in SOCKS' case, let close() determine return value */
3092 PerlLIO_dup2(dupfd,fd);
3093 PerlLIO_close(dupfd);
3100 PerlIOStdio_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
3103 FILE * const s = PerlIOSelf(f, PerlIOStdio)->stdio;
3107 STDCHAR *buf = (STDCHAR *) vbuf;
3109 * Perl is expecting PerlIO_getc() to fill the buffer Linux's
3110 * stdio does not do that for fread()
3112 const int ch = PerlSIO_fgetc(s);
3119 got = PerlSIO_fread(vbuf, 1, count, s);
3120 if (got == 0 && PerlSIO_ferror(s))
3122 if (got >= 0 || errno != EINTR)
3125 SETERRNO(0,0); /* just in case */
3131 PerlIOStdio_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3134 FILE * const s = PerlIOSelf(f, PerlIOStdio)->stdio;
3136 #ifdef STDIO_BUFFER_WRITABLE
3137 if (PerlIO_fast_gets(f) && PerlIO_has_base(f)) {
3138 STDCHAR *buf = ((STDCHAR *) vbuf) + count;
3139 STDCHAR *base = PerlIO_get_base(f);
3140 SSize_t cnt = PerlIO_get_cnt(f);
3141 STDCHAR *ptr = PerlIO_get_ptr(f);
3142 SSize_t avail = ptr - base;
3144 if (avail > count) {
3148 Move(buf-avail,ptr,avail,STDCHAR);
3151 PerlIO_set_ptrcnt(f,ptr,cnt+avail);
3152 if (PerlSIO_feof(s) && unread >= 0)
3153 PerlSIO_clearerr(s);
3158 if (PerlIO_has_cntptr(f)) {
3159 /* We can get pointer to buffer but not its base
3160 Do ungetc() but check chars are ending up in the
3163 STDCHAR *eptr = (STDCHAR*)PerlSIO_get_ptr(s);
3164 STDCHAR *buf = ((STDCHAR *) vbuf) + count;
3166 const int ch = *--buf & 0xFF;
3167 if (ungetc(ch,s) != ch) {
3168 /* ungetc did not work */
3171 if ((STDCHAR*)PerlSIO_get_ptr(s) != --eptr || ((*eptr & 0xFF) != ch)) {
3172 /* Did not change pointer as expected */
3173 fgetc(s); /* get char back again */
3183 unread += PerlIOBase_unread(aTHX_ f, vbuf, count);
3189 PerlIOStdio_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3194 got = PerlSIO_fwrite(vbuf, 1, count,
3195 PerlIOSelf(f, PerlIOStdio)->stdio);
3196 if (got >= 0 || errno != EINTR)
3199 SETERRNO(0,0); /* just in case */
3205 PerlIOStdio_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
3207 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3208 PERL_UNUSED_CONTEXT;
3210 return PerlSIO_fseek(stdio, offset, whence);
3214 PerlIOStdio_tell(pTHX_ PerlIO *f)
3216 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3217 PERL_UNUSED_CONTEXT;
3219 return PerlSIO_ftell(stdio);
3223 PerlIOStdio_flush(pTHX_ PerlIO *f)
3225 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3226 PERL_UNUSED_CONTEXT;
3228 if (PerlIOBase(f)->flags & PERLIO_F_CANWRITE) {
3229 return PerlSIO_fflush(stdio);
3235 * FIXME: This discards ungetc() and pre-read stuff which is not
3236 * right if this is just a "sync" from a layer above Suspect right
3237 * design is to do _this_ but not have layer above flush this
3238 * layer read-to-read
3241 * Not writeable - sync by attempting a seek
3243 const int err = errno;
3244 if (PerlSIO_fseek(stdio, (Off_t) 0, SEEK_CUR) != 0)
3252 PerlIOStdio_eof(pTHX_ PerlIO *f)
3254 PERL_UNUSED_CONTEXT;
3256 return PerlSIO_feof(PerlIOSelf(f, PerlIOStdio)->stdio);
3260 PerlIOStdio_error(pTHX_ PerlIO *f)
3262 PERL_UNUSED_CONTEXT;
3264 return PerlSIO_ferror(PerlIOSelf(f, PerlIOStdio)->stdio);
3268 PerlIOStdio_clearerr(pTHX_ PerlIO *f)
3270 PERL_UNUSED_CONTEXT;
3272 PerlSIO_clearerr(PerlIOSelf(f, PerlIOStdio)->stdio);
3276 PerlIOStdio_setlinebuf(pTHX_ PerlIO *f)
3278 PERL_UNUSED_CONTEXT;
3280 #ifdef HAS_SETLINEBUF
3281 PerlSIO_setlinebuf(PerlIOSelf(f, PerlIOStdio)->stdio);
3283 PerlSIO_setvbuf(PerlIOSelf(f, PerlIOStdio)->stdio, NULL, _IOLBF, 0);
3289 PerlIOStdio_get_base(pTHX_ PerlIO *f)
3291 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3292 return (STDCHAR*)PerlSIO_get_base(stdio);
3296 PerlIOStdio_get_bufsiz(pTHX_ PerlIO *f)
3298 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3299 return PerlSIO_get_bufsiz(stdio);
3303 #ifdef USE_STDIO_PTR
3305 PerlIOStdio_get_ptr(pTHX_ PerlIO *f)
3307 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3308 return (STDCHAR*)PerlSIO_get_ptr(stdio);
3312 PerlIOStdio_get_cnt(pTHX_ PerlIO *f)
3314 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3315 return PerlSIO_get_cnt(stdio);
3319 PerlIOStdio_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
3321 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3323 #ifdef STDIO_PTR_LVALUE
3324 PerlSIO_set_ptr(stdio, (void*)ptr); /* LHS STDCHAR* cast non-portable */
3325 #ifdef STDIO_PTR_LVAL_SETS_CNT
3326 if (PerlSIO_get_cnt(stdio) != (cnt)) {
3327 assert(PerlSIO_get_cnt(stdio) == (cnt));
3330 #if (!defined(STDIO_PTR_LVAL_NOCHANGE_CNT))
3332 * Setting ptr _does_ change cnt - we are done
3336 #else /* STDIO_PTR_LVALUE */
3338 #endif /* STDIO_PTR_LVALUE */
3341 * Now (or only) set cnt
3343 #ifdef STDIO_CNT_LVALUE
3344 PerlSIO_set_cnt(stdio, cnt);
3345 #else /* STDIO_CNT_LVALUE */
3346 #if (defined(STDIO_PTR_LVALUE) && defined(STDIO_PTR_LVAL_SETS_CNT))
3347 PerlSIO_set_ptr(stdio,
3348 PerlSIO_get_ptr(stdio) + (PerlSIO_get_cnt(stdio) -
3350 #else /* STDIO_PTR_LVAL_SETS_CNT */
3352 #endif /* STDIO_PTR_LVAL_SETS_CNT */
3353 #endif /* STDIO_CNT_LVALUE */
3360 PerlIOStdio_fill(pTHX_ PerlIO *f)
3362 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3364 PERL_UNUSED_CONTEXT;
3367 * fflush()ing read-only streams can cause trouble on some stdio-s
3369 if ((PerlIOBase(f)->flags & PERLIO_F_CANWRITE)) {
3370 if (PerlSIO_fflush(stdio) != 0)
3373 c = PerlSIO_fgetc(stdio);
3377 #if (defined(STDIO_PTR_LVALUE) && (defined(STDIO_CNT_LVALUE) || defined(STDIO_PTR_LVAL_SETS_CNT)))
3379 #ifdef STDIO_BUFFER_WRITABLE
3380 if (PerlIO_fast_gets(f) && PerlIO_has_base(f)) {
3381 /* Fake ungetc() to the real buffer in case system's ungetc
3384 STDCHAR *base = (STDCHAR*)PerlSIO_get_base(stdio);
3385 SSize_t cnt = PerlSIO_get_cnt(stdio);
3386 STDCHAR *ptr = (STDCHAR*)PerlSIO_get_ptr(stdio);
3387 if (ptr == base+1) {
3388 *--ptr = (STDCHAR) c;
3389 PerlIOStdio_set_ptrcnt(aTHX_ f,ptr,cnt+1);
3390 if (PerlSIO_feof(stdio))
3391 PerlSIO_clearerr(stdio);
3397 if (PerlIO_has_cntptr(f)) {
3399 if (PerlIOStdio_unread(aTHX_ f,&ch,1) == 1) {
3406 /* An ungetc()d char is handled separately from the regular
3407 * buffer, so we stuff it in the buffer ourselves.
3408 * Should never get called as should hit code above
3410 *(--((*stdio)->_ptr)) = (unsigned char) c;
3413 /* If buffer snoop scheme above fails fall back to
3416 if (PerlSIO_ungetc(c, stdio) != c)
3424 PERLIO_FUNCS_DECL(PerlIO_stdio) = {
3425 sizeof(PerlIO_funcs),
3427 sizeof(PerlIOStdio),
3428 PERLIO_K_BUFFERED|PERLIO_K_RAW,
3432 PerlIOBase_binmode, /* binmode */
3446 PerlIOStdio_clearerr,
3447 PerlIOStdio_setlinebuf,
3449 PerlIOStdio_get_base,
3450 PerlIOStdio_get_bufsiz,
3455 #ifdef USE_STDIO_PTR
3456 PerlIOStdio_get_ptr,
3457 PerlIOStdio_get_cnt,
3458 # if defined(HAS_FAST_STDIO) && defined(USE_FAST_STDIO)
3459 PerlIOStdio_set_ptrcnt,
3462 # endif /* HAS_FAST_STDIO && USE_FAST_STDIO */
3467 #endif /* USE_STDIO_PTR */
3470 /* Note that calls to PerlIO_exportFILE() are reversed using
3471 * PerlIO_releaseFILE(), not importFILE. */
3473 PerlIO_exportFILE(PerlIO * f, const char *mode)
3477 if (PerlIOValid(f)) {
3480 if (!mode || !*mode) {
3481 mode = PerlIO_modestr(f, buf);
3483 stdio = PerlSIO_fdopen(PerlIO_fileno(f), mode);
3487 /* De-link any lower layers so new :stdio sticks */
3489 if ((f2 = PerlIO_push(aTHX_ f, PERLIO_FUNCS_CAST(&PerlIO_stdio), buf, NULL))) {
3490 PerlIOStdio *s = PerlIOSelf((f = f2), PerlIOStdio);
3492 /* Link previous lower layers under new one */
3496 /* restore layers list */
3506 PerlIO_findFILE(PerlIO *f)
3510 if (l->tab == &PerlIO_stdio) {
3511 PerlIOStdio *s = PerlIOSelf(&l, PerlIOStdio);
3514 l = *PerlIONext(&l);
3516 /* Uses fallback "mode" via PerlIO_modestr() in PerlIO_exportFILE */
3517 return PerlIO_exportFILE(f, NULL);
3520 /* Use this to reverse PerlIO_exportFILE calls. */
3522 PerlIO_releaseFILE(PerlIO *p, FILE *f)
3527 if (l->tab == &PerlIO_stdio) {
3528 PerlIOStdio *s = PerlIOSelf(&l, PerlIOStdio);
3529 if (s->stdio == f) {
3531 PerlIO_pop(aTHX_ p);
3540 /*--------------------------------------------------------------------------------------*/
3542 * perlio buffer layer
3546 PerlIOBuf_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
3548 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3549 const int fd = PerlIO_fileno(f);
3550 if (fd >= 0 && PerlLIO_isatty(fd)) {
3551 PerlIOBase(f)->flags |= PERLIO_F_LINEBUF | PERLIO_F_TTY;
3553 if (*PerlIONext(f)) {
3554 const Off_t posn = PerlIO_tell(PerlIONext(f));
3555 if (posn != (Off_t) - 1) {
3559 return PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
3563 PerlIOBuf_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
3564 IV n, const char *mode, int fd, int imode, int perm,
3565 PerlIO *f, int narg, SV **args)
3567 if (PerlIOValid(f)) {
3568 PerlIO *next = PerlIONext(f);
3570 PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIOBase(next)->tab);
3571 if (tab && tab->Open)
3573 (*tab->Open)(aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
3575 if (!next || (*PerlIOBase(f)->tab->Pushed) (aTHX_ f, mode, PerlIOArg, self) != 0) {
3580 PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIO_default_btm());
3582 if (*mode == IoTYPE_IMPLICIT) {
3588 if (tab && tab->Open)
3589 f = (*tab->Open)(aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
3592 SETERRNO(EINVAL, LIB_INVARG);
3594 if (PerlIO_push(aTHX_ f, self, mode, PerlIOArg) == 0) {
3596 * if push fails during open, open fails. close will pop us.
3601 fd = PerlIO_fileno(f);
3602 if (init && fd == 2) {
3604 * Initial stderr is unbuffered
3606 PerlIOBase(f)->flags |= PERLIO_F_UNBUF;
3608 #ifdef PERLIO_USING_CRLF
3609 # ifdef PERLIO_IS_BINMODE_FD
3610 if (PERLIO_IS_BINMODE_FD(fd))
3611 PerlIO_binmode(aTHX_ f, '<'/*not used*/, O_BINARY, NULL);
3615 * do something about failing setmode()? --jhi
3617 PerlLIO_setmode(fd, O_BINARY);
3626 * This "flush" is akin to sfio's sync in that it handles files in either
3627 * read or write state. For write state, we put the postponed data through
3628 * the next layers. For read state, we seek() the next layers to the
3629 * offset given by current position in the buffer, and discard the buffer
3630 * state (XXXX supposed to be for seek()able buffers only, but now it is done
3631 * in any case?). Then the pass the stick further in chain.
3634 PerlIOBuf_flush(pTHX_ PerlIO *f)
3636 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3638 PerlIO *n = PerlIONext(f);
3639 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF) {
3641 * write() the buffer
3643 const STDCHAR *buf = b->buf;
3644 const STDCHAR *p = buf;
3645 while (p < b->ptr) {
3646 SSize_t count = PerlIO_write(n, p, b->ptr - p);
3650 else if (count < 0 || PerlIO_error(n)) {
3651 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
3656 b->posn += (p - buf);
3658 else if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
3659 STDCHAR *buf = PerlIO_get_base(f);
3661 * Note position change
3663 b->posn += (b->ptr - buf);
3664 if (b->ptr < b->end) {
3665 /* We did not consume all of it - try and seek downstream to
3666 our logical position
3668 if (PerlIOValid(n) && PerlIO_seek(n, b->posn, SEEK_SET) == 0) {
3669 /* Reload n as some layers may pop themselves on seek */
3670 b->posn = PerlIO_tell(n = PerlIONext(f));
3673 /* Seek failed (e.g. pipe or tty). Do NOT clear buffer or pre-read
3674 data is lost for good - so return saying "ok" having undone
3677 b->posn -= (b->ptr - buf);
3682 b->ptr = b->end = b->buf;
3683 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
3684 /* We check for Valid because of dubious decision to make PerlIO_flush(NULL) flush all */
3685 if (PerlIOValid(n) && PerlIO_flush(n) != 0)
3690 /* This discards the content of the buffer after b->ptr, and rereads
3691 * the buffer from the position off in the layer downstream; here off
3692 * is at offset corresponding to b->ptr - b->buf.
3695 PerlIOBuf_fill(pTHX_ PerlIO *f)
3697 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3698 PerlIO *n = PerlIONext(f);
3701 * Down-stream flush is defined not to loose read data so is harmless.
3702 * we would not normally be fill'ing if there was data left in anycase.
3704 if (PerlIO_flush(f) != 0) /* XXXX Check that its seek() succeeded?! */
3706 if (PerlIOBase(f)->flags & PERLIO_F_TTY)
3707 PerlIOBase_flush_linebuf(aTHX);
3710 PerlIO_get_base(f); /* allocate via vtable */
3714 b->ptr = b->end = b->buf;
3716 if (!PerlIOValid(n)) {
3717 PerlIOBase(f)->flags |= PERLIO_F_EOF;
3721 if (PerlIO_fast_gets(n)) {
3723 * Layer below is also buffered. We do _NOT_ want to call its
3724 * ->Read() because that will loop till it gets what we asked for
3725 * which may hang on a pipe etc. Instead take anything it has to
3726 * hand, or ask it to fill _once_.
3728 avail = PerlIO_get_cnt(n);
3730 avail = PerlIO_fill(n);
3732 avail = PerlIO_get_cnt(n);
3734 if (!PerlIO_error(n) && PerlIO_eof(n))
3739 STDCHAR *ptr = PerlIO_get_ptr(n);
3740 const SSize_t cnt = avail;
3741 if (avail > (SSize_t)b->bufsiz)
3743 Copy(ptr, b->buf, avail, STDCHAR);
3744 PerlIO_set_ptrcnt(n, ptr + avail, cnt - avail);
3748 avail = PerlIO_read(n, b->ptr, b->bufsiz);
3752 PerlIOBase(f)->flags |= PERLIO_F_EOF;
3754 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
3757 b->end = b->buf + avail;
3758 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3763 PerlIOBuf_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
3765 if (PerlIOValid(f)) {
3766 const PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3769 return PerlIOBase_read(aTHX_ f, vbuf, count);
3775 PerlIOBuf_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3777 const STDCHAR *buf = (const STDCHAR *) vbuf + count;
3778 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3781 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
3786 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
3788 * Buffer is already a read buffer, we can overwrite any chars
3789 * which have been read back to buffer start
3791 avail = (b->ptr - b->buf);
3795 * Buffer is idle, set it up so whole buffer is available for
3799 b->end = b->buf + avail;
3801 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3803 * Buffer extends _back_ from where we are now
3805 b->posn -= b->bufsiz;
3807 if (avail > (SSize_t) count) {
3809 * If we have space for more than count, just move count
3817 * In simple stdio-like ungetc() case chars will be already
3820 if (buf != b->ptr) {
3821 Copy(buf, b->ptr, avail, STDCHAR);
3825 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
3829 unread += PerlIOBase_unread(aTHX_ f, vbuf, count);
3835 PerlIOBuf_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3837 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3838 const STDCHAR *buf = (const STDCHAR *) vbuf;
3839 const STDCHAR *flushptr = buf;
3843 if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
3845 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
3846 if (PerlIO_flush(f) != 0) {
3850 if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF) {
3851 flushptr = buf + count;
3852 while (flushptr > buf && *(flushptr - 1) != '\n')
3856 SSize_t avail = b->bufsiz - (b->ptr - b->buf);
3857 if ((SSize_t) count < avail)
3859 if (flushptr > buf && flushptr <= buf + avail)
3860 avail = flushptr - buf;
3861 PerlIOBase(f)->flags |= PERLIO_F_WRBUF;
3863 Copy(buf, b->ptr, avail, STDCHAR);
3868 if (buf == flushptr)
3871 if (b->ptr >= (b->buf + b->bufsiz))
3874 if (PerlIOBase(f)->flags & PERLIO_F_UNBUF)
3880 PerlIOBuf_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
3883 if ((code = PerlIO_flush(f)) == 0) {
3884 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
3885 code = PerlIO_seek(PerlIONext(f), offset, whence);
3887 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3888 b->posn = PerlIO_tell(PerlIONext(f));
3895 PerlIOBuf_tell(pTHX_ PerlIO *f)
3897 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3899 * b->posn is file position where b->buf was read, or will be written
3901 Off_t posn = b->posn;
3902 if ((PerlIOBase(f)->flags & PERLIO_F_APPEND) &&
3903 (PerlIOBase(f)->flags & PERLIO_F_WRBUF)) {
3905 /* As O_APPEND files are normally shared in some sense it is better
3910 /* when file is NOT shared then this is sufficient */
3911 PerlIO_seek(PerlIONext(f),0, SEEK_END);
3913 posn = b->posn = PerlIO_tell(PerlIONext(f));
3917 * If buffer is valid adjust position by amount in buffer
3919 posn += (b->ptr - b->buf);
3925 PerlIOBuf_popped(pTHX_ PerlIO *f)
3927 const IV code = PerlIOBase_popped(aTHX_ f);
3928 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3929 if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
3932 b->ptr = b->end = b->buf = NULL;
3933 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
3938 PerlIOBuf_close(pTHX_ PerlIO *f)
3940 const IV code = PerlIOBase_close(aTHX_ f);
3941 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3942 if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
3945 b->ptr = b->end = b->buf = NULL;
3946 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
3951 PerlIOBuf_get_ptr(pTHX_ PerlIO *f)
3953 const PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3960 PerlIOBuf_get_cnt(pTHX_ PerlIO *f)
3962 const PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3965 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF)
3966 return (b->end - b->ptr);
3971 PerlIOBuf_get_base(pTHX_ PerlIO *f)
3973 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3974 PERL_UNUSED_CONTEXT;
3979 b->buf = Newxz(b->buf,b->bufsiz, STDCHAR);
3981 b->buf = (STDCHAR *) & b->oneword;
3982 b->bufsiz = sizeof(b->oneword);
3984 b->end = b->ptr = b->buf;
3990 PerlIOBuf_bufsiz(pTHX_ PerlIO *f)
3992 const PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
3995 return (b->end - b->buf);
3999 PerlIOBuf_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
4001 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
4005 if (PerlIO_get_cnt(f) != cnt || b->ptr < b->buf) {
4006 assert(PerlIO_get_cnt(f) == cnt);
4007 assert(b->ptr >= b->buf);
4009 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
4013 PerlIOBuf_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
4015 return PerlIOBase_dup(aTHX_ f, o, param, flags);
4020 PERLIO_FUNCS_DECL(PerlIO_perlio) = {
4021 sizeof(PerlIO_funcs),
4024 PERLIO_K_BUFFERED|PERLIO_K_RAW,
4028 PerlIOBase_binmode, /* binmode */
4042 PerlIOBase_clearerr,
4043 PerlIOBase_setlinebuf,
4048 PerlIOBuf_set_ptrcnt,
4051 /*--------------------------------------------------------------------------------------*/
4053 * Temp layer to hold unread chars when cannot do it any other way
4057 PerlIOPending_fill(pTHX_ PerlIO *f)
4060 * Should never happen
4067 PerlIOPending_close(pTHX_ PerlIO *f)
4070 * A tad tricky - flush pops us, then we close new top
4073 return PerlIO_close(f);
4077 PerlIOPending_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
4080 * A tad tricky - flush pops us, then we seek new top
4083 return PerlIO_seek(f, offset, whence);
4088 PerlIOPending_flush(pTHX_ PerlIO *f)
4090 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
4091 if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
4095 PerlIO_pop(aTHX_ f);
4100 PerlIOPending_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
4106 PerlIOBuf_set_ptrcnt(aTHX_ f, ptr, cnt);
4111 PerlIOPending_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
4113 const IV code = PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
4114 PerlIOl * const l = PerlIOBase(f);
4116 * Our PerlIO_fast_gets must match what we are pushed on, or sv_gets()
4117 * etc. get muddled when it changes mid-string when we auto-pop.
4119 l->flags = (l->flags & ~(PERLIO_F_FASTGETS | PERLIO_F_UTF8)) |
4120 (PerlIOBase(PerlIONext(f))->
4121 flags & (PERLIO_F_FASTGETS | PERLIO_F_UTF8));
4126 PerlIOPending_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
4128 SSize_t avail = PerlIO_get_cnt(f);
4130 if ((SSize_t)count < avail)
4133 got = PerlIOBuf_read(aTHX_ f, vbuf, avail);
4134 if (got >= 0 && got < (SSize_t)count) {
4135 const SSize_t more =
4136 PerlIO_read(f, ((STDCHAR *) vbuf) + got, count - got);
4137 if (more >= 0 || got == 0)
4143 PERLIO_FUNCS_DECL(PerlIO_pending) = {
4144 sizeof(PerlIO_funcs),
4147 PERLIO_K_BUFFERED|PERLIO_K_RAW, /* not sure about RAW here */
4148 PerlIOPending_pushed,
4151 PerlIOBase_binmode, /* binmode */
4160 PerlIOPending_close,
4161 PerlIOPending_flush,
4165 PerlIOBase_clearerr,
4166 PerlIOBase_setlinebuf,
4171 PerlIOPending_set_ptrcnt,
4176 /*--------------------------------------------------------------------------------------*/
4178 * crlf - translation On read translate CR,LF to "\n" we do this by
4179 * overriding ptr/cnt entries to hand back a line at a time and keeping a
4180 * record of which nl we "lied" about. On write translate "\n" to CR,LF
4182 * c->nl points on the first byte of CR LF pair when it is temporarily
4183 * replaced by LF, or to the last CR of the buffer. In the former case
4184 * the caller thinks that the buffer ends at c->nl + 1, in the latter
4185 * that it ends at c->nl; these two cases can be distinguished by
4186 * *c->nl. c->nl is set during _getcnt() call, and unset during
4187 * _unread() and _flush() calls.
4188 * It only matters for read operations.
4192 PerlIOBuf base; /* PerlIOBuf stuff */
4193 STDCHAR *nl; /* Position of crlf we "lied" about in the
4198 PerlIOCrlf_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
4201 PerlIOBase(f)->flags |= PERLIO_F_CRLF;
4202 code = PerlIOBuf_pushed(aTHX_ f, mode, arg, tab);
4204 PerlIO_debug("PerlIOCrlf_pushed f=%p %s %s fl=%08" UVxf "\n",
4205 f, PerlIOBase(f)->tab->name, (mode) ? mode : "(Null)",
4206 PerlIOBase(f)->flags);
4209 /* Enable the first CRLF capable layer you can find, but if none
4210 * found, the one we just pushed is fine. This results in at
4211 * any given moment at most one CRLF-capable layer being enabled
4212 * in the whole layer stack. */
4213 PerlIO *g = PerlIONext(f);
4215 PerlIOl *b = PerlIOBase(g);
4216 if (b && b->tab == &PerlIO_crlf) {
4217 if (!(b->flags & PERLIO_F_CRLF))
4218 b->flags |= PERLIO_F_CRLF;
4219 PerlIO_pop(aTHX_ f);
4230 PerlIOCrlf_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
4232 PerlIOCrlf * const c = PerlIOSelf(f, PerlIOCrlf);
4233 if (c->nl) { /* XXXX Shouldn't it be done only if b->ptr > c->nl? */
4237 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF))
4238 return PerlIOBuf_unread(aTHX_ f, vbuf, count);
4240 const STDCHAR *buf = (const STDCHAR *) vbuf + count;
4241 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
4243 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
4248 if (!(PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
4249 b->end = b->ptr = b->buf + b->bufsiz;
4250 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
4251 b->posn -= b->bufsiz;
4253 while (count > 0 && b->ptr > b->buf) {
4254 const int ch = *--buf;
4256 if (b->ptr - 2 >= b->buf) {
4263 /* If b->ptr - 1 == b->buf, we are undoing reading 0xa */
4264 *--(b->ptr) = 0xa; /* Works even if 0xa == '\r' */
4280 /* XXXX This code assumes that buffer size >=2, but does not check it... */
4282 PerlIOCrlf_get_cnt(pTHX_ PerlIO *f)
4284 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
4287 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
4288 PerlIOCrlf * const c = PerlIOSelf(f, PerlIOCrlf);
4289 if ((PerlIOBase(f)->flags & PERLIO_F_CRLF) && (!c->nl || *c->nl == 0xd)) {
4290 STDCHAR *nl = (c->nl) ? c->nl : b->ptr;
4292 while (nl < b->end && *nl != 0xd)
4294 if (nl < b->end && *nl == 0xd) {
4296 if (nl + 1 < b->end) {
4303 * Not CR,LF but just CR
4311 * Blast - found CR as last char in buffer
4316 * They may not care, defer work as long as
4320 return (nl - b->ptr);
4324 b->ptr++; /* say we have read it as far as
4325 * flush() is concerned */
4326 b->buf++; /* Leave space in front of buffer */
4327 /* Note as we have moved buf up flush's
4329 will naturally make posn point at CR
4331 b->bufsiz--; /* Buffer is thus smaller */
4332 code = PerlIO_fill(f); /* Fetch some more */
4333 b->bufsiz++; /* Restore size for next time */
4334 b->buf--; /* Point at space */
4335 b->ptr = nl = b->buf; /* Which is what we hand
4337 *nl = 0xd; /* Fill in the CR */
4339 goto test; /* fill() call worked */
4341 * CR at EOF - just fall through
4343 /* Should we clear EOF though ??? */
4348 return (((c->nl) ? (c->nl + 1) : b->end) - b->ptr);
4354 PerlIOCrlf_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
4356 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
4357 PerlIOCrlf * const c = PerlIOSelf(f, PerlIOCrlf);
4363 if (ptr == b->end && *c->nl == 0xd) {
4364 /* Defered CR at end of buffer case - we lied about count */
4377 * Test code - delete when it works ...
4379 IV flags = PerlIOBase(f)->flags;
4380 STDCHAR *chk = (c->nl) ? (c->nl+1) : b->end;
4381 if (ptr+cnt == c->nl && c->nl+1 == b->end && *c->nl == 0xd) {
4382 /* Defered CR at end of buffer case - we lied about count */
4388 Perl_croak(aTHX_ "ptr wrong %p != %p fl=%08" UVxf
4389 " nl=%p e=%p for %d", ptr, chk, flags, c->nl,
4397 * They have taken what we lied about
4405 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
4409 PerlIOCrlf_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
4411 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF))
4412 return PerlIOBuf_write(aTHX_ f, vbuf, count);
4414 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
4415 const STDCHAR *buf = (const STDCHAR *) vbuf;
4416 const STDCHAR * const ebuf = buf + count;
4419 if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
4421 while (buf < ebuf) {
4422 const STDCHAR * const eptr = b->buf + b->bufsiz;
4423 PerlIOBase(f)->flags |= PERLIO_F_WRBUF;
4424 while (buf < ebuf && b->ptr < eptr) {
4426 if ((b->ptr + 2) > eptr) {
4434 *(b->ptr)++ = 0xd; /* CR */
4435 *(b->ptr)++ = 0xa; /* LF */
4437 if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF) {
4444 *(b->ptr)++ = *buf++;
4446 if (b->ptr >= eptr) {
4452 if (PerlIOBase(f)->flags & PERLIO_F_UNBUF)
4454 return (buf - (STDCHAR *) vbuf);
4459 PerlIOCrlf_flush(pTHX_ PerlIO *f)
4461 PerlIOCrlf * const c = PerlIOSelf(f, PerlIOCrlf);
4466 return PerlIOBuf_flush(aTHX_ f);
4470 PerlIOCrlf_binmode(pTHX_ PerlIO *f)
4472 if ((PerlIOBase(f)->flags & PERLIO_F_CRLF)) {
4473 /* In text mode - flush any pending stuff and flip it */
4474 PerlIOBase(f)->flags &= ~PERLIO_F_CRLF;
4475 #ifndef PERLIO_USING_CRLF
4476 /* CRLF is unusual case - if this is just the :crlf layer pop it */
4477 if (PerlIOBase(f)->tab == &PerlIO_crlf) {
4478 PerlIO_pop(aTHX_ f);
4485 PERLIO_FUNCS_DECL(PerlIO_crlf) = {
4486 sizeof(PerlIO_funcs),
4489 PERLIO_K_BUFFERED | PERLIO_K_CANCRLF | PERLIO_K_RAW,
4491 PerlIOBuf_popped, /* popped */
4493 PerlIOCrlf_binmode, /* binmode */
4497 PerlIOBuf_read, /* generic read works with ptr/cnt lies */
4498 PerlIOCrlf_unread, /* Put CR,LF in buffer for each '\n' */
4499 PerlIOCrlf_write, /* Put CR,LF in buffer for each '\n' */
4507 PerlIOBase_clearerr,
4508 PerlIOBase_setlinebuf,
4513 PerlIOCrlf_set_ptrcnt,
4517 /*--------------------------------------------------------------------------------------*/
4519 * mmap as "buffer" layer
4523 PerlIOBuf base; /* PerlIOBuf stuff */
4524 Mmap_t mptr; /* Mapped address */
4525 Size_t len; /* mapped length */
4526 STDCHAR *bbuf; /* malloced buffer if map fails */
4530 PerlIOMmap_map(pTHX_ PerlIO *f)
4533 PerlIOMmap * const m = PerlIOSelf(f, PerlIOMmap);
4534 const IV flags = PerlIOBase(f)->flags;
4538 if (flags & PERLIO_F_CANREAD) {
4539 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
4540 const int fd = PerlIO_fileno(f);
4542 code = Fstat(fd, &st);
4543 if (code == 0 && S_ISREG(st.st_mode)) {
4544 SSize_t len = st.st_size - b->posn;
4547 if (PL_mmap_page_size <= 0)
4548 Perl_croak(aTHX_ "panic: bad pagesize %" IVdf,
4552 * This is a hack - should never happen - open should
4555 b->posn = PerlIO_tell(PerlIONext(f));
4557 posn = (b->posn / PL_mmap_page_size) * PL_mmap_page_size;
4558 len = st.st_size - posn;
4559 m->mptr = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, posn);
4560 if (m->mptr && m->mptr != (Mmap_t) - 1) {
4561 #if 0 && defined(HAS_MADVISE) && defined(MADV_SEQUENTIAL)
4562 madvise(m->mptr, len, MADV_SEQUENTIAL);
4564 #if 0 && defined(HAS_MADVISE) && defined(MADV_WILLNEED)
4565 madvise(m->mptr, len, MADV_WILLNEED);
4567 PerlIOBase(f)->flags =
4568 (flags & ~PERLIO_F_EOF) | PERLIO_F_RDBUF;
4569 b->end = ((STDCHAR *) m->mptr) + len;
4570 b->buf = ((STDCHAR *) m->mptr) + (b->posn - posn);
4579 PerlIOBase(f)->flags =
4580 flags | PERLIO_F_EOF | PERLIO_F_RDBUF;
4582 b->ptr = b->end = b->ptr;
4591 PerlIOMmap_unmap(pTHX_ PerlIO *f)
4593 PerlIOMmap * const m = PerlIOSelf(f, PerlIOMmap);
4596 PerlIOBuf * const b = &m->base;
4598 code = munmap(m->mptr, m->len);
4602 if (PerlIO_seek(PerlIONext(f), b->posn, SEEK_SET) != 0)
4605 b->ptr = b->end = b->buf;
4606 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
4612 PerlIOMmap_get_base(pTHX_ PerlIO *f)
4614 PerlIOMmap * const m = PerlIOSelf(f, PerlIOMmap);
4615 PerlIOBuf * const b = &m->base;
4616 if (b->buf && (PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
4618 * Already have a readbuffer in progress
4624 * We have a write buffer or flushed PerlIOBuf read buffer
4626 m->bbuf = b->buf; /* save it in case we need it again */
4627 b->buf = NULL; /* Clear to trigger below */
4630 PerlIOMmap_map(aTHX_ f); /* Try and map it */
4633 * Map did not work - recover PerlIOBuf buffer if we have one
4638 b->ptr = b->end = b->buf;
4641 return PerlIOBuf_get_base(aTHX_ f);
4645 PerlIOMmap_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
4647 PerlIOMmap * const m = PerlIOSelf(f, PerlIOMmap);
4648 PerlIOBuf * const b = &m->base;
4649 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
4651 if (b->ptr && (b->ptr - count) >= b->buf
4652 && memEQ(b->ptr - count, vbuf, count)) {
4654 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
4659 * Loose the unwritable mapped buffer
4663 * If flush took the "buffer" see if we have one from before
4665 if (!b->buf && m->bbuf)
4668 PerlIOBuf_get_base(aTHX_ f);
4672 return PerlIOBuf_unread(aTHX_ f, vbuf, count);
4676 PerlIOMmap_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
4678 PerlIOMmap * const m = PerlIOSelf(f, PerlIOMmap);
4679 PerlIOBuf * const b = &m->base;
4681 if (!b->buf || !(PerlIOBase(f)->flags & PERLIO_F_WRBUF)) {
4683 * No, or wrong sort of, buffer
4686 if (PerlIOMmap_unmap(aTHX_ f) != 0)
4690 * If unmap took the "buffer" see if we have one from before
4692 if (!b->buf && m->bbuf)
4695 PerlIOBuf_get_base(aTHX_ f);
4699 return PerlIOBuf_write(aTHX_ f, vbuf, count);
4703 PerlIOMmap_flush(pTHX_ PerlIO *f)
4705 PerlIOMmap * const m = PerlIOSelf(f, PerlIOMmap);
4706 PerlIOBuf * const b = &m->base;
4707 IV code = PerlIOBuf_flush(aTHX_ f);
4709 * Now we are "synced" at PerlIOBuf level
4716 if (PerlIOMmap_unmap(aTHX_ f) != 0)
4721 * We seem to have a PerlIOBuf buffer which was not mapped
4722 * remember it in case we need one later
4731 PerlIOMmap_fill(pTHX_ PerlIO *f)
4733 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
4734 IV code = PerlIO_flush(f);
4735 if (code == 0 && !b->buf) {
4736 code = PerlIOMmap_map(aTHX_ f);
4738 if (code == 0 && !(PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
4739 code = PerlIOBuf_fill(aTHX_ f);
4745 PerlIOMmap_close(pTHX_ PerlIO *f)
4747 PerlIOMmap * const m = PerlIOSelf(f, PerlIOMmap);
4748 PerlIOBuf * const b = &m->base;
4749 IV code = PerlIO_flush(f);
4753 b->ptr = b->end = b->buf;
4755 if (PerlIOBuf_close(aTHX_ f) != 0)
4761 PerlIOMmap_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
4763 return PerlIOBase_dup(aTHX_ f, o, param, flags);
4767 PERLIO_FUNCS_DECL(PerlIO_mmap) = {
4768 sizeof(PerlIO_funcs),
4771 PERLIO_K_BUFFERED|PERLIO_K_RAW,
4775 PerlIOBase_binmode, /* binmode */
4789 PerlIOBase_clearerr,
4790 PerlIOBase_setlinebuf,
4791 PerlIOMmap_get_base,
4795 PerlIOBuf_set_ptrcnt,
4798 #endif /* HAS_MMAP */
4801 Perl_PerlIO_stdin(pTHX)
4805 PerlIO_stdstreams(aTHX);
4807 return &PL_perlio[1];
4811 Perl_PerlIO_stdout(pTHX)
4815 PerlIO_stdstreams(aTHX);
4817 return &PL_perlio[2];
4821 Perl_PerlIO_stderr(pTHX)
4825 PerlIO_stdstreams(aTHX);
4827 return &PL_perlio[3];
4830 /*--------------------------------------------------------------------------------------*/
4833 PerlIO_getname(PerlIO *f, char *buf)
4838 bool exported = FALSE;
4839 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
4841 stdio = PerlIO_exportFILE(f,0);
4845 name = fgetname(stdio, buf);
4846 if (exported) PerlIO_releaseFILE(f,stdio);
4851 PERL_UNUSED_ARG(buf);
4852 Perl_croak(aTHX_ "Don't know how to get file name");
4858 /*--------------------------------------------------------------------------------------*/
4860 * Functions which can be called on any kind of PerlIO implemented in
4864 #undef PerlIO_fdopen
4866 PerlIO_fdopen(int fd, const char *mode)
4869 return PerlIO_openn(aTHX_ NULL, mode, fd, 0, 0, NULL, 0, NULL);
4874 PerlIO_open(const char *path, const char *mode)
4877 SV *name = sv_2mortal(newSVpv(path, 0));
4878 return PerlIO_openn(aTHX_ NULL, mode, -1, 0, 0, NULL, 1, &name);
4881 #undef Perlio_reopen
4883 PerlIO_reopen(const char *path, const char *mode, PerlIO *f)
4886 SV *name = sv_2mortal(newSVpv(path,0));
4887 return PerlIO_openn(aTHX_ NULL, mode, -1, 0, 0, f, 1, &name);
4892 PerlIO_getc(PerlIO *f)
4896 if ( 1 == PerlIO_read(f, buf, 1) ) {
4897 return (unsigned char) buf[0];
4902 #undef PerlIO_ungetc
4904 PerlIO_ungetc(PerlIO *f, int ch)
4909 if (PerlIO_unread(f, &buf, 1) == 1)
4917 PerlIO_putc(PerlIO *f, int ch)
4921 return PerlIO_write(f, &buf, 1);
4926 PerlIO_puts(PerlIO *f, const char *s)
4929 return PerlIO_write(f, s, strlen(s));
4932 #undef PerlIO_rewind
4934 PerlIO_rewind(PerlIO *f)
4937 PerlIO_seek(f, (Off_t) 0, SEEK_SET);
4941 #undef PerlIO_vprintf
4943 PerlIO_vprintf(PerlIO *f, const char *fmt, va_list ap)
4946 SV * const sv = newSVpvs("");
4952 Perl_va_copy(ap, apc);
4953 sv_vcatpvf(sv, fmt, &apc);
4955 sv_vcatpvf(sv, fmt, &ap);
4957 s = SvPV_const(sv, len);
4958 wrote = PerlIO_write(f, s, len);
4963 #undef PerlIO_printf
4965 PerlIO_printf(PerlIO *f, const char *fmt, ...)
4970 result = PerlIO_vprintf(f, fmt, ap);
4975 #undef PerlIO_stdoutf
4977 PerlIO_stdoutf(const char *fmt, ...)
4983 result = PerlIO_vprintf(PerlIO_stdout(), fmt, ap);
4988 #undef PerlIO_tmpfile
4990 PerlIO_tmpfile(void)
4995 const int fd = win32_tmpfd();
4997 f = PerlIO_fdopen(fd, "w+b");
4999 # if defined(HAS_MKSTEMP) && ! defined(VMS) && ! defined(OS2)
5000 SV * const sv = newSVpvs("/tmp/PerlIO_XXXXXX");
5002 * I have no idea how portable mkstemp() is ... NI-S
5004 const int fd = mkstemp(SvPVX(sv));
5006 f = PerlIO_fdopen(fd, "w+");
5008 PerlIOBase(f)->flags |= PERLIO_F_TEMP;
5009 PerlLIO_unlink(SvPVX_const(sv));
5012 # else /* !HAS_MKSTEMP, fallback to stdio tmpfile(). */
5013 FILE * const stdio = PerlSIO_tmpfile();
5016 if ((f = PerlIO_push(aTHX_(PerlIO_allocate(aTHX)),
5017 PERLIO_FUNCS_CAST(&PerlIO_stdio),
5019 PerlIOStdio * const s = PerlIOSelf(f, PerlIOStdio);
5025 # endif /* else HAS_MKSTEMP */
5026 #endif /* else WIN32 */
5033 #endif /* USE_SFIO */
5034 #endif /* PERLIO_IS_STDIO */
5036 /*======================================================================================*/
5038 * Now some functions in terms of above which may be needed even if we are
5039 * not in true PerlIO mode
5043 #undef PerlIO_setpos
5045 PerlIO_setpos(PerlIO *f, SV *pos)
5050 const Off_t * const posn = (Off_t *) SvPV(pos, len);
5051 if (f && len == sizeof(Off_t))
5052 return PerlIO_seek(f, *posn, SEEK_SET);
5054 SETERRNO(EINVAL, SS_IVCHAN);
5058 #undef PerlIO_setpos
5060 PerlIO_setpos(PerlIO *f, SV *pos)
5065 Fpos_t * const fpos = (Fpos_t *) SvPV(pos, len);
5066 if (f && len == sizeof(Fpos_t)) {
5067 #if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64)
5068 return fsetpos64(f, fpos);
5070 return fsetpos(f, fpos);
5074 SETERRNO(EINVAL, SS_IVCHAN);
5080 #undef PerlIO_getpos
5082 PerlIO_getpos(PerlIO *f, SV *pos)
5085 Off_t posn = PerlIO_tell(f);
5086 sv_setpvn(pos, (char *) &posn, sizeof(posn));
5087 return (posn == (Off_t) - 1) ? -1 : 0;
5090 #undef PerlIO_getpos
5092 PerlIO_getpos(PerlIO *f, SV *pos)
5097 #if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64)
5098 code = fgetpos64(f, &fpos);
5100 code = fgetpos(f, &fpos);
5102 sv_setpvn(pos, (char *) &fpos, sizeof(fpos));
5107 #if (defined(PERLIO_IS_STDIO) || !defined(USE_SFIO)) && !defined(HAS_VPRINTF)
5110 vprintf(char *pat, char *args)
5112 _doprnt(pat, args, stdout);
5113 return 0; /* wrong, but perl doesn't use the return
5118 vfprintf(FILE *fd, char *pat, char *args)
5120 _doprnt(pat, args, fd);
5121 return 0; /* wrong, but perl doesn't use the return
5127 #ifndef PerlIO_vsprintf
5129 PerlIO_vsprintf(char *s, int n, const char *fmt, va_list ap)
5132 const int val = vsprintf(s, fmt, ap);
5134 if (strlen(s) >= (STRLEN) n) {
5136 (void) PerlIO_puts(Perl_error_log,
5137 "panic: sprintf overflow - memory corrupted!\n");
5145 #ifndef PerlIO_sprintf
5147 PerlIO_sprintf(char *s, int n, const char *fmt, ...)
5152 result = PerlIO_vsprintf(s, n, fmt, ap);
5160 * c-indentation-style: bsd
5162 * indent-tabs-mode: t
5165 * ex: set ts=8 sts=4 sw=4 noet: