2 * perlio.c Copyright (c) 1996-2002, 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.
13 * If we have ActivePerl-like PERL_IMPLICIT_SYS then we need a dTHX to get
14 * at the dispatch tables, even when we do not need it for other reasons.
15 * Invent a dSYS macro to abstract this out
17 #ifdef PERL_IMPLICIT_SYS
30 #define PERLIO_NOT_STDIO 0
31 #if !defined(PERLIO_IS_STDIO) && !defined(USE_SFIO)
37 * This file provides those parts of PerlIO abstraction
38 * which are not #defined in perlio.h.
39 * Which these are depends on various Configure #ifdef's
43 #define PERL_IN_PERLIO_C
46 #ifdef PERL_IMPLICIT_CONTEXT
54 perlsio_binmode(FILE *fp, int iotype, int mode)
57 * This used to be contents of do_binmode in doio.c
60 # if defined(atarist) || defined(__MINT__)
63 ((FILE *) fp)->_flag |= _IOBIN;
65 ((FILE *) fp)->_flag &= ~_IOBIN;
72 if (PerlLIO_setmode(fp, mode) != -1) {
74 if (PerlLIO_setmode(fileno(fp), mode) != -1) {
76 # if defined(WIN32) && defined(__BORLANDC__)
78 * The translation mode of the stream is maintained independent of
79 * the translation mode of the fd in the Borland RTL (heavy
80 * digging through their runtime sources reveal). User has to set
81 * the mode explicitly for the stream (though they don't document
82 * this anywhere). GSAR 97-5-24
96 # if defined(USEMYBINMODE)
98 if (my_binmode(fp, iotype, mode) != FALSE)
109 #define O_ACCMODE 3 /* Assume traditional implementation */
113 PerlIO_intmode2str(int rawmode, char *mode, int *writing)
115 int result = rawmode & O_ACCMODE;
120 ptype = IoTYPE_RDONLY;
123 ptype = IoTYPE_WRONLY;
131 *writing = (result != O_RDONLY);
133 if (result == O_RDONLY) {
137 else if (rawmode & O_APPEND) {
139 if (result != O_WRONLY)
144 if (result == O_WRONLY)
151 if (rawmode & O_BINARY)
157 #ifndef PERLIO_LAYERS
159 PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names)
161 if (!names || !*names
162 || strEQ(names, ":crlf")
163 || strEQ(names, ":raw")
164 || strEQ(names, ":bytes")
168 Perl_croak(aTHX_ "Cannot apply \"%s\" in non-PerlIO perl", names);
176 PerlIO_destruct(pTHX)
181 PerlIO_binmode(pTHX_ PerlIO *fp, int iotype, int mode, const char *names)
186 return perlsio_binmode(fp, iotype, mode);
191 PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
196 #ifdef PERL_IMPLICIT_SYS
197 return PerlSIO_fdupopen(f);
200 return win32_fdupopen(f);
203 int fd = PerlLIO_dup(PerlIO_fileno(f));
206 int omode = fcntl(fd, F_GETFL);
208 omode = djgpp_get_stream_mode(f);
210 PerlIO_intmode2str(omode,mode,NULL);
211 /* the r+ is a hack */
212 return PerlIO_fdopen(fd, mode);
217 SETERRNO(EBADF, SS_IVCHAN);
227 * De-mux PerlIO_openn() into fdopen, freopen and fopen type entries
231 PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd,
232 int imode, int perm, PerlIO *old, int narg, SV **args)
236 Perl_croak(aTHX_ "More than one argument to open");
238 if (*args == &PL_sv_undef)
239 return PerlIO_tmpfile();
241 char *name = SvPV_nolen(*args);
243 fd = PerlLIO_open3(name, imode, perm);
245 return PerlIO_fdopen(fd, (char *) mode + 1);
248 return PerlIO_reopen(name, mode, old);
251 return PerlIO_open(name, mode);
256 return PerlIO_fdopen(fd, (char *) mode);
261 XS(XS_PerlIO__Layer__find)
265 Perl_croak(aTHX_ "Usage class->find(name[,load])");
267 char *name = SvPV_nolen(ST(1));
268 ST(0) = (strEQ(name, "crlf")
269 || strEQ(name, "raw")) ? &PL_sv_yes : &PL_sv_undef;
276 Perl_boot_core_PerlIO(pTHX)
278 newXS("PerlIO::Layer::find", XS_PerlIO__Layer__find, __FILE__);
284 #ifdef PERLIO_IS_STDIO
290 * Does nothing (yet) except force this file to be included in perl
291 * binary. That allows this file to force inclusion of other functions
292 * that may be required by loadable extensions e.g. for
293 * FileHandle::tmpfile
297 #undef PerlIO_tmpfile
304 #else /* PERLIO_IS_STDIO */
312 * This section is just to make sure these functions get pulled in from
316 #undef PerlIO_tmpfile
327 * Force this file to be included in perl binary. Which allows this
328 * file to force inclusion of other functions that may be required by
329 * loadable extensions e.g. for FileHandle::tmpfile
333 * Hack sfio does its own 'autoflush' on stdout in common cases. Flush
334 * results in a lot of lseek()s to regular files and lot of small
337 sfset(sfstdout, SF_SHARE, 0);
340 /* This is not the reverse of PerlIO_exportFILE(), PerlIO_releaseFILE() is. */
342 PerlIO_importFILE(FILE *stdio, const char *mode)
344 int fd = fileno(stdio);
345 if (!mode || !*mode) {
348 return PerlIO_fdopen(fd, mode);
352 PerlIO_findFILE(PerlIO *pio)
354 int fd = PerlIO_fileno(pio);
355 FILE *f = fdopen(fd, "r+");
357 if (!f && errno == EINVAL)
359 if (!f && errno == EINVAL)
366 /*======================================================================================*/
368 * Implement all the PerlIO interface ourselves.
374 * We _MUST_ have <unistd.h> if we are using lseek() and may have large
381 #include <sys/mman.h>
385 void PerlIO_debug(const char *fmt, ...)
386 __attribute__ ((format(__printf__, 1, 2)));
389 PerlIO_debug(const char *fmt, ...)
396 char *s = PerlEnv_getenv("PERLIO_DEBUG");
398 dbg = PerlLIO_open3(s, O_WRONLY | O_CREAT | O_APPEND, 0666);
405 /* Use fixed buffer as sv_catpvf etc. needs SVs */
409 s = CopFILE(PL_curcop);
412 sprintf(buffer, "%s:%" IVdf " ", s, (IV) CopLINE(PL_curcop));
413 len = strlen(buffer);
414 vsprintf(buffer+len, fmt, ap);
415 PerlLIO_write(dbg, buffer, strlen(buffer));
417 SV *sv = newSVpvn("", 0);
420 s = CopFILE(PL_curcop);
423 Perl_sv_catpvf(aTHX_ sv, "%s:%" IVdf " ", s,
424 (IV) CopLINE(PL_curcop));
425 Perl_sv_vcatpvf(aTHX_ sv, fmt, &ap);
428 PerlLIO_write(dbg, s, len);
435 /*--------------------------------------------------------------------------------------*/
438 * Inner level routines
442 * Table of pointers to the PerlIO structs (malloc'ed)
444 #define PERLIO_TABLE_SIZE 64
447 PerlIO_allocate(pTHX)
450 * Find a free slot in the table, allocating new table as necessary
455 while ((f = *last)) {
457 last = (PerlIO **) (f);
458 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
464 Newz('I',f,PERLIO_TABLE_SIZE,PerlIO);
472 #undef PerlIO_fdupopen
474 PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
476 if (PerlIOValid(f)) {
477 PerlIO_funcs *tab = PerlIOBase(f)->tab;
479 PerlIO_debug("fdupopen f=%p param=%p\n",(void*)f,(void*)param);
480 new = (*tab->Dup)(aTHX_ PerlIO_allocate(aTHX),f,param, flags);
484 SETERRNO(EBADF, SS_IVCHAN);
490 PerlIO_cleantable(pTHX_ PerlIO **tablep)
492 PerlIO *table = *tablep;
495 PerlIO_cleantable(aTHX_(PerlIO **) & (table[0]));
496 for (i = PERLIO_TABLE_SIZE - 1; i > 0; i--) {
497 PerlIO *f = table + i;
509 PerlIO_list_alloc(pTHX)
512 Newz('L', list, 1, PerlIO_list_t);
518 PerlIO_list_free(pTHX_ PerlIO_list_t *list)
521 if (--list->refcnt == 0) {
524 for (i = 0; i < list->cur; i++) {
525 if (list->array[i].arg)
526 SvREFCNT_dec(list->array[i].arg);
528 Safefree(list->array);
536 PerlIO_list_push(pTHX_ PerlIO_list_t *list, PerlIO_funcs *funcs, SV *arg)
539 if (list->cur >= list->len) {
542 Renew(list->array, list->len, PerlIO_pair_t);
544 New('l', list->array, list->len, PerlIO_pair_t);
546 p = &(list->array[list->cur++]);
548 if ((p->arg = arg)) {
554 PerlIO_clone_list(pTHX_ PerlIO_list_t *proto, CLONE_PARAMS *param)
556 PerlIO_list_t *list = (PerlIO_list_t *) NULL;
559 list = PerlIO_list_alloc(aTHX);
560 for (i=0; i < proto->cur; i++) {
562 if (proto->array[i].arg)
563 arg = PerlIO_sv_dup(aTHX_ proto->array[i].arg,param);
564 PerlIO_list_push(aTHX_ list, proto->array[i].funcs, arg);
571 PerlIO_clone(pTHX_ PerlInterpreter *proto, CLONE_PARAMS *param)
574 PerlIO **table = &proto->Iperlio;
577 PL_known_layers = PerlIO_clone_list(aTHX_ proto->Iknown_layers, param);
578 PL_def_layerlist = PerlIO_clone_list(aTHX_ proto->Idef_layerlist, param);
579 PerlIO_allocate(aTHX); /* root slot is never used */
580 PerlIO_debug("Clone %p from %p\n",aTHX,proto);
581 while ((f = *table)) {
583 table = (PerlIO **) (f++);
584 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
586 (void) fp_dup(f, 0, param);
595 PerlIO_destruct(pTHX)
597 PerlIO **table = &PL_perlio;
600 PerlIO_debug("Destruct %p\n",aTHX);
602 while ((f = *table)) {
604 table = (PerlIO **) (f++);
605 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
609 if (l->tab->kind & PERLIO_K_DESTRUCT) {
610 PerlIO_debug("Destruct popping %s\n", l->tab->name);
624 PerlIO_pop(pTHX_ PerlIO *f)
628 PerlIO_debug("PerlIO_pop f=%p %s\n", (void*)f, l->tab->name);
629 if (l->tab->Popped) {
631 * If popped returns non-zero do not free its layer structure
632 * it has either done so itself, or it is shared and still in
635 if ((*l->tab->Popped) (aTHX_ f) != 0)
643 /*--------------------------------------------------------------------------------------*/
645 * XS Interface for perl code
649 PerlIO_find_layer(pTHX_ const char *name, STRLEN len, int load)
652 if ((SSize_t) len <= 0)
654 for (i = 0; i < PL_known_layers->cur; i++) {
655 PerlIO_funcs *f = PL_known_layers->array[i].funcs;
656 if (memEQ(f->name, name, len)) {
657 PerlIO_debug("%.*s => %p\n", (int) len, name, (void*)f);
661 if (load && PL_subname && PL_def_layerlist
662 && PL_def_layerlist->cur >= 2) {
663 SV *pkgsv = newSVpvn("PerlIO", 6);
664 SV *layer = newSVpvn(name, len);
667 * The two SVs are magically freed by load_module
669 Perl_load_module(aTHX_ 0, pkgsv, Nullsv, layer, Nullsv);
671 return PerlIO_find_layer(aTHX_ name, len, 0);
673 PerlIO_debug("Cannot find %.*s\n", (int) len, name);
677 #ifdef USE_ATTRIBUTES_FOR_PERLIO
680 perlio_mg_set(pTHX_ SV *sv, MAGIC *mg)
683 IO *io = GvIOn((GV *) SvRV(sv));
684 PerlIO *ifp = IoIFP(io);
685 PerlIO *ofp = IoOFP(io);
686 Perl_warn(aTHX_ "set %" SVf " %p %p %p", sv, io, ifp, ofp);
692 perlio_mg_get(pTHX_ SV *sv, MAGIC *mg)
695 IO *io = GvIOn((GV *) SvRV(sv));
696 PerlIO *ifp = IoIFP(io);
697 PerlIO *ofp = IoOFP(io);
698 Perl_warn(aTHX_ "get %" SVf " %p %p %p", sv, io, ifp, ofp);
704 perlio_mg_clear(pTHX_ SV *sv, MAGIC *mg)
706 Perl_warn(aTHX_ "clear %" SVf, sv);
711 perlio_mg_free(pTHX_ SV *sv, MAGIC *mg)
713 Perl_warn(aTHX_ "free %" SVf, sv);
717 MGVTBL perlio_vtab = {
725 XS(XS_io_MODIFY_SCALAR_ATTRIBUTES)
728 SV *sv = SvRV(ST(1));
733 sv_magic(sv, (SV *) av, PERL_MAGIC_ext, NULL, 0);
735 mg = mg_find(sv, PERL_MAGIC_ext);
736 mg->mg_virtual = &perlio_vtab;
738 Perl_warn(aTHX_ "attrib %" SVf, sv);
739 for (i = 2; i < items; i++) {
741 const char *name = SvPV(ST(i), len);
742 SV *layer = PerlIO_find_layer(aTHX_ name, len, 1);
744 av_push(av, SvREFCNT_inc(layer));
755 #endif /* USE_ATTIBUTES_FOR_PERLIO */
758 PerlIO_tab_sv(pTHX_ PerlIO_funcs *tab)
760 HV *stash = gv_stashpv("PerlIO::Layer", TRUE);
761 SV *sv = sv_bless(newRV_noinc(newSViv(PTR2IV(tab))), stash);
765 XS(XS_PerlIO__Layer__find)
769 Perl_croak(aTHX_ "Usage class->find(name[,load])");
772 char *name = SvPV(ST(1), len);
773 bool load = (items > 2) ? SvTRUE(ST(2)) : 0;
774 PerlIO_funcs *layer = PerlIO_find_layer(aTHX_ name, len, load);
776 (layer) ? sv_2mortal(PerlIO_tab_sv(aTHX_ layer)) :
783 PerlIO_define_layer(pTHX_ PerlIO_funcs *tab)
785 if (!PL_known_layers)
786 PL_known_layers = PerlIO_list_alloc(aTHX);
787 PerlIO_list_push(aTHX_ PL_known_layers, tab, Nullsv);
788 PerlIO_debug("define %s %p\n", tab->name, (void*)tab);
792 PerlIO_parse_layers(pTHX_ PerlIO_list_t *av, const char *names)
795 const char *s = names;
797 while (isSPACE(*s) || *s == ':')
802 const char *as = Nullch;
804 if (!isIDFIRST(*s)) {
806 * Message is consistent with how attribute lists are
807 * passed. Even though this means "foo : : bar" is
808 * seen as an invalid separator character.
810 char q = ((*s == '\'') ? '"' : '\'');
811 if (ckWARN(WARN_LAYER))
812 Perl_warner(aTHX_ packWARN(WARN_LAYER),
813 "perlio: invalid separator character %c%c%c in layer specification list %s",
815 SETERRNO(EINVAL, LIB_INVARG);
820 } while (isALNUM(*e));
836 * It's a nul terminated string, not allowed
837 * to \ the terminating null. Anything other
838 * character is passed over.
848 if (ckWARN(WARN_LAYER))
849 Perl_warner(aTHX_ packWARN(WARN_LAYER),
850 "perlio: argument list not closed for layer \"%.*s\"",
862 bool warn_layer = ckWARN(WARN_LAYER);
863 PerlIO_funcs *layer =
864 PerlIO_find_layer(aTHX_ s, llen, 1);
866 PerlIO_list_push(aTHX_ av, layer,
873 Perl_warner(aTHX_ packWARN(WARN_LAYER), "perlio: unknown layer \"%.*s\"",
886 PerlIO_default_buffer(pTHX_ PerlIO_list_t *av)
888 PerlIO_funcs *tab = &PerlIO_perlio;
889 #ifdef PERLIO_USING_CRLF
892 if (PerlIO_stdio.Set_ptrcnt)
895 PerlIO_debug("Pushing %s\n", tab->name);
896 PerlIO_list_push(aTHX_ av, PerlIO_find_layer(aTHX_ tab->name, 0, 0),
901 PerlIO_arg_fetch(PerlIO_list_t *av, IV n)
903 return av->array[n].arg;
907 PerlIO_layer_fetch(pTHX_ PerlIO_list_t *av, IV n, PerlIO_funcs *def)
909 if (n >= 0 && n < av->cur) {
910 PerlIO_debug("Layer %" IVdf " is %s\n", n,
911 av->array[n].funcs->name);
912 return av->array[n].funcs;
915 Perl_croak(aTHX_ "panic: PerlIO layer array corrupt");
920 PerlIOPop_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
922 if (PerlIOValid(f)) {
930 PerlIO_funcs PerlIO_remove = {
931 sizeof(PerlIO_funcs),
934 PERLIO_K_DUMMY | PERLIO_K_UTF8,
953 NULL, /* get_bufsiz */
956 NULL, /* set_ptrcnt */
960 PerlIO_default_layers(pTHX)
962 if (!PL_def_layerlist) {
963 const char *s = (PL_tainting) ? Nullch : PerlEnv_getenv("PERLIO");
964 PerlIO_funcs *osLayer = &PerlIO_unix;
965 PL_def_layerlist = PerlIO_list_alloc(aTHX);
966 PerlIO_define_layer(aTHX_ & PerlIO_unix);
967 #if defined(WIN32) && !defined(UNDER_CE)
968 PerlIO_define_layer(aTHX_ & PerlIO_win32);
970 osLayer = &PerlIO_win32;
973 PerlIO_define_layer(aTHX_ & PerlIO_raw);
974 PerlIO_define_layer(aTHX_ & PerlIO_perlio);
975 PerlIO_define_layer(aTHX_ & PerlIO_stdio);
976 PerlIO_define_layer(aTHX_ & PerlIO_crlf);
978 PerlIO_define_layer(aTHX_ & PerlIO_mmap);
980 PerlIO_define_layer(aTHX_ & PerlIO_utf8);
981 PerlIO_define_layer(aTHX_ & PerlIO_remove);
982 PerlIO_define_layer(aTHX_ & PerlIO_byte);
983 PerlIO_list_push(aTHX_ PL_def_layerlist,
984 PerlIO_find_layer(aTHX_ osLayer->name, 0, 0),
987 PerlIO_parse_layers(aTHX_ PL_def_layerlist, s);
990 PerlIO_default_buffer(aTHX_ PL_def_layerlist);
993 if (PL_def_layerlist->cur < 2) {
994 PerlIO_default_buffer(aTHX_ PL_def_layerlist);
996 return PL_def_layerlist;
1000 Perl_boot_core_PerlIO(pTHX)
1002 #ifdef USE_ATTRIBUTES_FOR_PERLIO
1003 newXS("io::MODIFY_SCALAR_ATTRIBUTES", XS_io_MODIFY_SCALAR_ATTRIBUTES,
1006 newXS("PerlIO::Layer::find", XS_PerlIO__Layer__find, __FILE__);
1010 PerlIO_default_layer(pTHX_ I32 n)
1012 PerlIO_list_t *av = PerlIO_default_layers(aTHX);
1015 return PerlIO_layer_fetch(aTHX_ av, n, &PerlIO_stdio);
1018 #define PerlIO_default_top() PerlIO_default_layer(aTHX_ -1)
1019 #define PerlIO_default_btm() PerlIO_default_layer(aTHX_ 0)
1022 PerlIO_stdstreams(pTHX)
1025 PerlIO_allocate(aTHX);
1026 PerlIO_fdopen(0, "Ir" PERLIO_STDTEXT);
1027 PerlIO_fdopen(1, "Iw" PERLIO_STDTEXT);
1028 PerlIO_fdopen(2, "Iw" PERLIO_STDTEXT);
1033 PerlIO_push(pTHX_ PerlIO *f, PerlIO_funcs *tab, const char *mode, SV *arg)
1035 if (tab->fsize != sizeof(PerlIO_funcs)) {
1037 Perl_croak(aTHX_ "Layer does not match this perl");
1041 if (tab->size < sizeof(PerlIOl)) {
1044 /* Real layer with a data area */
1045 Newc('L',l,tab->size,char,PerlIOl);
1047 Zero(l, tab->size, char);
1051 PerlIO_debug("PerlIO_push f=%p %s %s %p\n", (void*)f, tab->name,
1052 (mode) ? mode : "(Null)", (void*)arg);
1053 if ((*l->tab->Pushed) (aTHX_ f, mode, arg, tab) != 0) {
1054 PerlIO_pop(aTHX_ f);
1060 /* Pseudo-layer where push does its own stack adjust */
1061 PerlIO_debug("PerlIO_push f=%p %s %s %p\n", (void*)f, tab->name,
1062 (mode) ? mode : "(Null)", (void*)arg);
1063 if ((*tab->Pushed) (aTHX_ f, mode, arg, tab) != 0) {
1071 PerlIOBase_binmode(pTHX_ PerlIO *f)
1073 if (PerlIOValid(f)) {
1074 /* Is layer suitable for raw stream ? */
1075 if (PerlIOBase(f)->tab->kind & PERLIO_K_RAW) {
1076 /* Yes - turn off UTF-8-ness, to undo UTF-8 locale effects */
1077 PerlIOBase(f)->flags &= ~PERLIO_F_UTF8;
1080 /* Not suitable - pop it */
1081 PerlIO_pop(aTHX_ f);
1089 PerlIORaw_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
1092 if (PerlIOValid(f)) {
1097 * Strip all layers that are not suitable for a raw stream
1100 while (t && (l = *t)) {
1101 if (l->tab->Binmode) {
1102 /* Has a handler - normal case */
1103 if ((*l->tab->Binmode)(aTHX_ f) == 0) {
1105 /* Layer still there - move down a layer */
1114 /* No handler - pop it */
1115 PerlIO_pop(aTHX_ t);
1118 if (PerlIOValid(f)) {
1119 PerlIO_debug(":raw f=%p :%s\n", (void*)f, PerlIOBase(f)->tab->name);
1127 PerlIO_apply_layera(pTHX_ PerlIO *f, const char *mode,
1128 PerlIO_list_t *layers, IV n, IV max)
1132 PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n, NULL);
1134 if (!PerlIO_push(aTHX_ f, tab, mode, PerlIOArg)) {
1145 PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names)
1149 PerlIO_list_t *layers = PerlIO_list_alloc(aTHX);
1150 code = PerlIO_parse_layers(aTHX_ layers, names);
1152 code = PerlIO_apply_layera(aTHX_ f, mode, layers, 0, layers->cur);
1154 PerlIO_list_free(aTHX_ layers);
1160 /*--------------------------------------------------------------------------------------*/
1162 * Given the abstraction above the public API functions
1166 PerlIO_binmode(pTHX_ PerlIO *f, int iotype, int mode, const char *names)
1168 PerlIO_debug("PerlIO_binmode f=%p %s %c %x %s\n",
1169 (void*)f, PerlIOBase(f)->tab->name, iotype, mode,
1170 (names) ? names : "(Null)");
1172 /* Do not flush etc. if (e.g.) switching encodings.
1173 if a pushed layer knows it needs to flush lower layers
1174 (for example :unix which is never going to call them)
1175 it can do the flush when it is pushed.
1177 return PerlIO_apply_layers(aTHX_ f, NULL, names) == 0 ? TRUE : FALSE;
1180 /* Fake 5.6 legacy of using this call to turn ON O_TEXT */
1181 #ifdef PERLIO_USING_CRLF
1182 /* Legacy binmode only has meaning if O_TEXT has a value distinct from
1183 O_BINARY so we can look for it in mode.
1185 if (!(mode & O_BINARY)) {
1187 /* FIXME?: Looking down the layer stack seems wrong,
1188 but is a way of reaching past (say) an encoding layer
1189 to flip CRLF-ness of the layer(s) below
1192 /* Perhaps we should turn on bottom-most aware layer
1193 e.g. Ilya's idea that UNIX TTY could serve
1195 if (PerlIOBase(f)->tab->kind & PERLIO_K_CANCRLF) {
1196 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF)) {
1197 /* Not in text mode - flush any pending stuff and flip it */
1199 PerlIOBase(f)->flags |= PERLIO_F_CRLF;
1201 /* Only need to turn it on in one layer so we are done */
1206 /* Not finding a CRLF aware layer presumably means we are binary
1207 which is not what was requested - so we failed
1208 We _could_ push :crlf layer but so could caller
1213 /* Legacy binmode is now _defined_ as being equivalent to pushing :raw
1214 So code that used to be here is now in PerlIORaw_pushed().
1216 return PerlIO_push(aTHX_ f, &PerlIO_raw, Nullch, Nullsv) ? TRUE : FALSE;
1221 PerlIO__close(pTHX_ PerlIO *f)
1224 return (*PerlIOBase(f)->tab->Close) (aTHX_ f);
1226 SETERRNO(EBADF, SS_IVCHAN);
1232 Perl_PerlIO_close(pTHX_ PerlIO *f)
1235 if (PerlIOValid(f)) {
1236 code = (*PerlIOBase(f)->tab->Close) (aTHX_ f);
1238 PerlIO_pop(aTHX_ f);
1245 Perl_PerlIO_fileno(pTHX_ PerlIO *f)
1248 return (*PerlIOBase(f)->tab->Fileno) (aTHX_ f);
1250 SETERRNO(EBADF, SS_IVCHAN);
1256 PerlIO_context_layers(pTHX_ const char *mode)
1258 const char *type = NULL;
1260 * Need to supply default layer info from open.pm
1263 SV *layers = PL_curcop->cop_io;
1266 type = SvPV(layers, len);
1267 if (type && mode[0] != 'r') {
1269 * Skip to write part
1271 const char *s = strchr(type, 0);
1272 if (s && (STRLEN)(s - type) < len) {
1281 static PerlIO_funcs *
1282 PerlIO_layer_from_ref(pTHX_ SV *sv)
1285 * For any scalar type load the handler which is bundled with perl
1287 if (SvTYPE(sv) < SVt_PVAV)
1288 return PerlIO_find_layer(aTHX_ "scalar", 6, 1);
1291 * For other types allow if layer is known but don't try and load it
1293 switch (SvTYPE(sv)) {
1295 return PerlIO_find_layer(aTHX_ "Array", 5, 0);
1297 return PerlIO_find_layer(aTHX_ "Hash", 4, 0);
1299 return PerlIO_find_layer(aTHX_ "Code", 4, 0);
1301 return PerlIO_find_layer(aTHX_ "Glob", 4, 0);
1307 PerlIO_resolve_layers(pTHX_ const char *layers,
1308 const char *mode, int narg, SV **args)
1310 PerlIO_list_t *def = PerlIO_default_layers(aTHX);
1313 PerlIO_stdstreams(aTHX);
1317 * If it is a reference but not an object see if we have a handler
1320 if (SvROK(arg) && !sv_isobject(arg)) {
1321 PerlIO_funcs *handler = PerlIO_layer_from_ref(aTHX_ SvRV(arg));
1323 def = PerlIO_list_alloc(aTHX);
1324 PerlIO_list_push(aTHX_ def, handler, &PL_sv_undef);
1328 * Don't fail if handler cannot be found :via(...) etc. may do
1329 * something sensible else we will just stringfy and open
1335 layers = PerlIO_context_layers(aTHX_ mode);
1336 if (layers && *layers) {
1340 av = PerlIO_list_alloc(aTHX);
1341 for (i = 0; i < def->cur; i++) {
1342 PerlIO_list_push(aTHX_ av, def->array[i].funcs,
1349 if (PerlIO_parse_layers(aTHX_ av, layers) == 0) {
1353 PerlIO_list_free(aTHX_ av);
1354 return (PerlIO_list_t *) NULL;
1365 PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd,
1366 int imode, int perm, PerlIO *f, int narg, SV **args)
1368 if (!f && narg == 1 && *args == &PL_sv_undef) {
1369 if ((f = PerlIO_tmpfile())) {
1371 layers = PerlIO_context_layers(aTHX_ mode);
1372 if (layers && *layers)
1373 PerlIO_apply_layers(aTHX_ f, mode, layers);
1377 PerlIO_list_t *layera = NULL;
1379 PerlIO_funcs *tab = NULL;
1380 if (PerlIOValid(f)) {
1382 * This is "reopen" - it is not tested as perl does not use it
1386 layera = PerlIO_list_alloc(aTHX);
1388 SV *arg = (l->tab->Getarg)
1389 ? (*l->tab->Getarg) (aTHX_ &l, NULL, 0)
1391 PerlIO_list_push(aTHX_ layera, l->tab, arg);
1392 l = *PerlIONext(&l);
1396 layera = PerlIO_resolve_layers(aTHX_ layers, mode, narg, args);
1402 * Start at "top" of layer stack
1404 n = layera->cur - 1;
1406 PerlIO_funcs *t = PerlIO_layer_fetch(aTHX_ layera, n, NULL);
1415 * Found that layer 'n' can do opens - call it
1417 if (narg > 1 && !(tab->kind & PERLIO_K_MULTIARG)) {
1418 Perl_croak(aTHX_ "More than one argument to open(,':%s')",tab->name);
1420 PerlIO_debug("openn(%s,'%s','%s',%d,%x,%o,%p,%d,%p)\n",
1421 tab->name, layers, mode, fd, imode, perm,
1422 (void*)f, narg, (void*)args);
1423 f = (*tab->Open) (aTHX_ tab, layera, n, mode, fd, imode, perm,
1426 if (n + 1 < layera->cur) {
1428 * More layers above the one that we used to open -
1431 if (PerlIO_apply_layera(aTHX_ f, mode, layera, n + 1, layera->cur) != 0) {
1432 /* If pushing layers fails close the file */
1439 PerlIO_list_free(aTHX_ layera);
1446 Perl_PerlIO_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
1449 return (*PerlIOBase(f)->tab->Read) (aTHX_ f, vbuf, count);
1451 SETERRNO(EBADF, SS_IVCHAN);
1457 Perl_PerlIO_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
1460 return (*PerlIOBase(f)->tab->Unread) (aTHX_ f, vbuf, count);
1462 SETERRNO(EBADF, SS_IVCHAN);
1468 Perl_PerlIO_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
1471 return (*PerlIOBase(f)->tab->Write) (aTHX_ f, vbuf, count);
1473 SETERRNO(EBADF, SS_IVCHAN);
1479 Perl_PerlIO_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
1482 return (*PerlIOBase(f)->tab->Seek) (aTHX_ f, offset, whence);
1484 SETERRNO(EBADF, SS_IVCHAN);
1490 Perl_PerlIO_tell(pTHX_ PerlIO *f)
1493 return (*PerlIOBase(f)->tab->Tell) (aTHX_ f);
1495 SETERRNO(EBADF, SS_IVCHAN);
1501 Perl_PerlIO_flush(pTHX_ PerlIO *f)
1505 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1506 if (tab && tab->Flush) {
1507 return (*tab->Flush) (aTHX_ f);
1510 PerlIO_debug("Cannot flush f=%p :%s\n", (void*)f, tab->name);
1511 SETERRNO(EBADF, SS_IVCHAN);
1516 PerlIO_debug("Cannot flush f=%p\n", (void*)f);
1517 SETERRNO(EBADF, SS_IVCHAN);
1523 * Is it good API design to do flush-all on NULL, a potentially
1524 * errorneous input? Maybe some magical value (PerlIO*
1525 * PERLIO_FLUSH_ALL = (PerlIO*)-1;)? Yes, stdio does similar
1526 * things on fflush(NULL), but should we be bound by their design
1529 PerlIO **table = &PL_perlio;
1531 while ((f = *table)) {
1533 table = (PerlIO **) (f++);
1534 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
1535 if (*f && PerlIO_flush(f) != 0)
1545 PerlIOBase_flush_linebuf(pTHX)
1547 PerlIO **table = &PL_perlio;
1549 while ((f = *table)) {
1551 table = (PerlIO **) (f++);
1552 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
1555 flags & (PERLIO_F_LINEBUF | PERLIO_F_CANWRITE))
1556 == (PERLIO_F_LINEBUF | PERLIO_F_CANWRITE))
1564 Perl_PerlIO_fill(pTHX_ PerlIO *f)
1567 return (*PerlIOBase(f)->tab->Fill) (aTHX_ f);
1569 SETERRNO(EBADF, SS_IVCHAN);
1575 PerlIO_isutf8(PerlIO *f)
1578 return (PerlIOBase(f)->flags & PERLIO_F_UTF8) != 0;
1580 SETERRNO(EBADF, SS_IVCHAN);
1586 Perl_PerlIO_eof(pTHX_ PerlIO *f)
1589 return (*PerlIOBase(f)->tab->Eof) (aTHX_ f);
1591 SETERRNO(EBADF, SS_IVCHAN);
1597 Perl_PerlIO_error(pTHX_ PerlIO *f)
1600 return (*PerlIOBase(f)->tab->Error) (aTHX_ f);
1602 SETERRNO(EBADF, SS_IVCHAN);
1608 Perl_PerlIO_clearerr(pTHX_ PerlIO *f)
1611 (*PerlIOBase(f)->tab->Clearerr) (aTHX_ f);
1613 SETERRNO(EBADF, SS_IVCHAN);
1617 Perl_PerlIO_setlinebuf(pTHX_ PerlIO *f)
1620 (*PerlIOBase(f)->tab->Setlinebuf) (aTHX_ f);
1622 SETERRNO(EBADF, SS_IVCHAN);
1626 PerlIO_has_base(PerlIO *f)
1628 if (PerlIOValid(f)) {
1629 return (PerlIOBase(f)->tab->Get_base != NULL);
1635 PerlIO_fast_gets(PerlIO *f)
1637 if (PerlIOValid(f) && (PerlIOBase(f)->flags & PERLIO_F_FASTGETS)) {
1638 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1639 return (tab->Set_ptrcnt != NULL);
1645 PerlIO_has_cntptr(PerlIO *f)
1647 if (PerlIOValid(f)) {
1648 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1649 return (tab->Get_ptr != NULL && tab->Get_cnt != NULL);
1655 PerlIO_canset_cnt(PerlIO *f)
1657 if (PerlIOValid(f)) {
1658 PerlIOl *l = PerlIOBase(f);
1659 return (l->tab->Set_ptrcnt != NULL);
1665 Perl_PerlIO_get_base(pTHX_ PerlIO *f)
1668 return (*PerlIOBase(f)->tab->Get_base) (aTHX_ f);
1673 Perl_PerlIO_get_bufsiz(pTHX_ PerlIO *f)
1676 return (*PerlIOBase(f)->tab->Get_bufsiz) (aTHX_ f);
1681 Perl_PerlIO_get_ptr(pTHX_ PerlIO *f)
1683 if (PerlIOValid(f)) {
1684 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1685 if (tab->Get_ptr == NULL)
1687 return (*tab->Get_ptr) (aTHX_ f);
1693 Perl_PerlIO_get_cnt(pTHX_ PerlIO *f)
1695 if (PerlIOValid(f)) {
1696 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1697 if (tab->Get_cnt == NULL)
1699 return (*tab->Get_cnt) (aTHX_ f);
1705 Perl_PerlIO_set_cnt(pTHX_ PerlIO *f, int cnt)
1707 if (PerlIOValid(f)) {
1708 (*PerlIOBase(f)->tab->Set_ptrcnt) (aTHX_ f, NULL, cnt);
1713 Perl_PerlIO_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, int cnt)
1715 if (PerlIOValid(f)) {
1716 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1717 if (tab->Set_ptrcnt == NULL) {
1718 Perl_croak(aTHX_ "PerlIO buffer snooping abuse");
1720 (*PerlIOBase(f)->tab->Set_ptrcnt) (aTHX_ f, ptr, cnt);
1725 /*--------------------------------------------------------------------------------------*/
1727 * utf8 and raw dummy layers
1731 PerlIOUtf8_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
1733 if (PerlIOValid(f)) {
1734 if (tab->kind & PERLIO_K_UTF8)
1735 PerlIOBase(f)->flags |= PERLIO_F_UTF8;
1737 PerlIOBase(f)->flags &= ~PERLIO_F_UTF8;
1743 PerlIO_funcs PerlIO_utf8 = {
1744 sizeof(PerlIO_funcs),
1747 PERLIO_K_DUMMY | PERLIO_K_UTF8,
1765 NULL, /* get_base */
1766 NULL, /* get_bufsiz */
1769 NULL, /* set_ptrcnt */
1772 PerlIO_funcs PerlIO_byte = {
1773 sizeof(PerlIO_funcs),
1794 NULL, /* get_base */
1795 NULL, /* get_bufsiz */
1798 NULL, /* set_ptrcnt */
1802 PerlIORaw_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
1803 IV n, const char *mode, int fd, int imode, int perm,
1804 PerlIO *old, int narg, SV **args)
1806 PerlIO_funcs *tab = PerlIO_default_btm();
1807 return (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
1811 PerlIO_funcs PerlIO_raw = {
1812 sizeof(PerlIO_funcs),
1833 NULL, /* get_base */
1834 NULL, /* get_bufsiz */
1837 NULL, /* set_ptrcnt */
1839 /*--------------------------------------------------------------------------------------*/
1840 /*--------------------------------------------------------------------------------------*/
1842 * "Methods" of the "base class"
1846 PerlIOBase_fileno(pTHX_ PerlIO *f)
1848 return PerlIOValid(f) ? PerlIO_fileno(PerlIONext(f)) : -1;
1852 PerlIO_modestr(PerlIO * f, char *buf)
1855 if (PerlIOValid(f)) {
1856 IV flags = PerlIOBase(f)->flags;
1857 if (flags & PERLIO_F_APPEND) {
1859 if (flags & PERLIO_F_CANREAD) {
1863 else if (flags & PERLIO_F_CANREAD) {
1865 if (flags & PERLIO_F_CANWRITE)
1868 else if (flags & PERLIO_F_CANWRITE) {
1870 if (flags & PERLIO_F_CANREAD) {
1874 #ifdef PERLIO_USING_CRLF
1875 if (!(flags & PERLIO_F_CRLF))
1885 PerlIOBase_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
1887 PerlIOl *l = PerlIOBase(f);
1889 const char *omode = mode;
1892 l->flags &= ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE |
1893 PERLIO_F_TRUNCATE | PERLIO_F_APPEND);
1894 if (tab->Set_ptrcnt != NULL)
1895 l->flags |= PERLIO_F_FASTGETS;
1897 if (*mode == '#' || *mode == 'I')
1901 l->flags |= PERLIO_F_CANREAD;
1904 l->flags |= PERLIO_F_APPEND | PERLIO_F_CANWRITE;
1907 l->flags |= PERLIO_F_TRUNCATE | PERLIO_F_CANWRITE;
1910 SETERRNO(EINVAL, LIB_INVARG);
1916 l->flags |= PERLIO_F_CANREAD | PERLIO_F_CANWRITE;
1919 l->flags &= ~PERLIO_F_CRLF;
1922 l->flags |= PERLIO_F_CRLF;
1925 SETERRNO(EINVAL, LIB_INVARG);
1932 l->flags |= l->next->flags &
1933 (PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_TRUNCATE |
1938 PerlIO_debug("PerlIOBase_pushed f=%p %s %s fl=%08" UVxf " (%s)\n",
1939 f, PerlIOBase(f)->tab->name, (omode) ? omode : "(Null)",
1940 l->flags, PerlIO_modestr(f, temp));
1946 PerlIOBase_popped(pTHX_ PerlIO *f)
1952 PerlIOBase_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
1955 * Save the position as current head considers it
1957 Off_t old = PerlIO_tell(f);
1959 PerlIO_push(aTHX_ f, &PerlIO_pending, "r", Nullsv);
1960 PerlIOSelf(f, PerlIOBuf)->posn = old;
1961 done = PerlIOBuf_unread(aTHX_ f, vbuf, count);
1966 PerlIOBase_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
1968 STDCHAR *buf = (STDCHAR *) vbuf;
1970 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD))
1973 SSize_t avail = PerlIO_get_cnt(f);
1976 take = ((SSize_t)count < avail) ? count : avail;
1978 STDCHAR *ptr = PerlIO_get_ptr(f);
1979 Copy(ptr, buf, take, STDCHAR);
1980 PerlIO_set_ptrcnt(f, ptr + take, (avail -= take));
1984 if (count > 0 && avail <= 0) {
1985 if (PerlIO_fill(f) != 0)
1989 return (buf - (STDCHAR *) vbuf);
1995 PerlIOBase_noop_ok(pTHX_ PerlIO *f)
2001 PerlIOBase_noop_fail(pTHX_ PerlIO *f)
2007 PerlIOBase_close(pTHX_ PerlIO *f)
2010 PerlIO *n = PerlIONext(f);
2011 if (PerlIO_flush(f) != 0)
2013 if (PerlIOValid(n) && (*PerlIOBase(n)->tab->Close)(aTHX_ n) != 0)
2015 PerlIOBase(f)->flags &=
2016 ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_OPEN);
2021 PerlIOBase_eof(pTHX_ PerlIO *f)
2023 if (PerlIOValid(f)) {
2024 return (PerlIOBase(f)->flags & PERLIO_F_EOF) != 0;
2030 PerlIOBase_error(pTHX_ PerlIO *f)
2032 if (PerlIOValid(f)) {
2033 return (PerlIOBase(f)->flags & PERLIO_F_ERROR) != 0;
2039 PerlIOBase_clearerr(pTHX_ PerlIO *f)
2041 if (PerlIOValid(f)) {
2042 PerlIO *n = PerlIONext(f);
2043 PerlIOBase(f)->flags &= ~(PERLIO_F_ERROR | PERLIO_F_EOF);
2050 PerlIOBase_setlinebuf(pTHX_ PerlIO *f)
2052 if (PerlIOValid(f)) {
2053 PerlIOBase(f)->flags |= PERLIO_F_LINEBUF;
2058 PerlIO_sv_dup(pTHX_ SV *arg, CLONE_PARAMS *param)
2064 return sv_dup(arg, param);
2067 return newSVsv(arg);
2070 return newSVsv(arg);
2075 PerlIOBase_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
2077 PerlIO *nexto = PerlIONext(o);
2078 if (PerlIOValid(nexto)) {
2079 PerlIO_funcs *tab = PerlIOBase(nexto)->tab;
2080 f = (*tab->Dup)(aTHX_ f, nexto, param, flags);
2083 PerlIO_funcs *self = PerlIOBase(o)->tab;
2086 PerlIO_debug("PerlIOBase_dup %s f=%p o=%p param=%p\n",
2087 self->name, (void*)f, (void*)o, (void*)param);
2089 arg = (*self->Getarg)(aTHX_ o,param,flags);
2091 f = PerlIO_push(aTHX_ f, self, PerlIO_modestr(o,buf), arg);
2099 #define PERLIO_MAX_REFCOUNTABLE_FD 2048
2101 perl_mutex PerlIO_mutex;
2103 int PerlIO_fd_refcnt[PERLIO_MAX_REFCOUNTABLE_FD];
2108 /* Place holder for stdstreams call ??? */
2110 MUTEX_INIT(&PerlIO_mutex);
2115 PerlIOUnix_refcnt_inc(int fd)
2117 if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
2119 MUTEX_LOCK(&PerlIO_mutex);
2121 PerlIO_fd_refcnt[fd]++;
2122 PerlIO_debug("fd %d refcnt=%d\n",fd,PerlIO_fd_refcnt[fd]);
2124 MUTEX_UNLOCK(&PerlIO_mutex);
2130 PerlIOUnix_refcnt_dec(int fd)
2133 if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
2135 MUTEX_LOCK(&PerlIO_mutex);
2137 cnt = --PerlIO_fd_refcnt[fd];
2138 PerlIO_debug("fd %d refcnt=%d\n",fd,cnt);
2140 MUTEX_UNLOCK(&PerlIO_mutex);
2147 PerlIO_cleanup(pTHX)
2151 PerlIO_debug("Cleanup layers for %p\n",aTHX);
2153 PerlIO_debug("Cleanup layers\n");
2155 /* Raise STDIN..STDERR refcount so we don't close them */
2156 for (i=0; i < 3; i++)
2157 PerlIOUnix_refcnt_inc(i);
2158 PerlIO_cleantable(aTHX_ &PL_perlio);
2159 /* Restore STDIN..STDERR refcount */
2160 for (i=0; i < 3; i++)
2161 PerlIOUnix_refcnt_dec(i);
2163 if (PL_known_layers) {
2164 PerlIO_list_free(aTHX_ PL_known_layers);
2165 PL_known_layers = NULL;
2167 if(PL_def_layerlist) {
2168 PerlIO_list_free(aTHX_ PL_def_layerlist);
2169 PL_def_layerlist = NULL;
2175 /*--------------------------------------------------------------------------------------*/
2177 * Bottom-most level for UNIX-like case
2181 struct _PerlIO base; /* The generic part */
2182 int fd; /* UNIX like file descriptor */
2183 int oflags; /* open/fcntl flags */
2187 PerlIOUnix_oflags(const char *mode)
2190 if (*mode == 'I' || *mode == '#')
2195 if (*++mode == '+') {
2202 oflags = O_CREAT | O_TRUNC;
2203 if (*++mode == '+') {
2212 oflags = O_CREAT | O_APPEND;
2213 if (*++mode == '+') {
2226 else if (*mode == 't') {
2228 oflags &= ~O_BINARY;
2232 * Always open in binary mode
2235 if (*mode || oflags == -1) {
2236 SETERRNO(EINVAL, LIB_INVARG);
2243 PerlIOUnix_fileno(pTHX_ PerlIO *f)
2245 return PerlIOSelf(f, PerlIOUnix)->fd;
2249 PerlIOUnix_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
2251 IV code = PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
2252 PerlIOUnix *s = PerlIOSelf(f, PerlIOUnix);
2253 if (*PerlIONext(f)) {
2254 /* We never call down so do any pending stuff now */
2255 PerlIO_flush(PerlIONext(f));
2256 s->fd = PerlIO_fileno(PerlIONext(f));
2258 * XXX could (or should) we retrieve the oflags from the open file
2259 * handle rather than believing the "mode" we are passed in? XXX
2260 * Should the value on NULL mode be 0 or -1?
2262 s->oflags = mode ? PerlIOUnix_oflags(mode) : -1;
2264 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
2269 PerlIOUnix_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2270 IV n, const char *mode, int fd, int imode,
2271 int perm, PerlIO *f, int narg, SV **args)
2273 if (PerlIOValid(f)) {
2274 if (PerlIOBase(f)->flags & PERLIO_F_OPEN)
2275 (*PerlIOBase(f)->tab->Close)(aTHX_ f);
2278 char *path = SvPV_nolen(*args);
2282 imode = PerlIOUnix_oflags(mode);
2286 fd = PerlLIO_open3(path, imode, perm);
2294 f = PerlIO_allocate(aTHX);
2296 if (!PerlIOValid(f)) {
2297 if (!(f = PerlIO_push(aTHX_ f, self, mode, PerlIOArg))) {
2301 s = PerlIOSelf(f, PerlIOUnix);
2304 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
2305 PerlIOUnix_refcnt_inc(fd);
2311 * FIXME: pop layers ???
2319 PerlIOUnix_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
2321 PerlIOUnix *os = PerlIOSelf(o, PerlIOUnix);
2323 if (flags & PERLIO_DUP_FD) {
2324 fd = PerlLIO_dup(fd);
2326 if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
2327 f = PerlIOBase_dup(aTHX_ f, o, param, flags);
2329 /* If all went well overwrite fd in dup'ed lay with the dup()'ed fd */
2330 PerlIOUnix *s = PerlIOSelf(f, PerlIOUnix);
2332 PerlIOUnix_refcnt_inc(fd);
2341 PerlIOUnix_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
2343 int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2344 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD) ||
2345 PerlIOBase(f)->flags & (PERLIO_F_EOF|PERLIO_F_ERROR)) {
2349 SSize_t len = PerlLIO_read(fd, vbuf, count);
2350 if (len >= 0 || errno != EINTR) {
2352 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2353 else if (len == 0 && count != 0)
2354 PerlIOBase(f)->flags |= PERLIO_F_EOF;
2362 PerlIOUnix_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
2364 int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2366 SSize_t len = PerlLIO_write(fd, vbuf, count);
2367 if (len >= 0 || errno != EINTR) {
2369 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2377 PerlIOUnix_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
2380 PerlLIO_lseek(PerlIOSelf(f, PerlIOUnix)->fd, offset, whence);
2381 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
2382 return (new == (Off_t) - 1) ? -1 : 0;
2386 PerlIOUnix_tell(pTHX_ PerlIO *f)
2388 return PerlLIO_lseek(PerlIOSelf(f, PerlIOUnix)->fd, 0, SEEK_CUR);
2393 PerlIOUnix_close(pTHX_ PerlIO *f)
2395 int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2397 if (PerlIOBase(f)->flags & PERLIO_F_OPEN) {
2398 if (PerlIOUnix_refcnt_dec(fd) > 0) {
2399 PerlIOBase(f)->flags &= ~PERLIO_F_OPEN;
2404 SETERRNO(EBADF,SS_IVCHAN);
2407 while (PerlLIO_close(fd) != 0) {
2408 if (errno != EINTR) {
2415 PerlIOBase(f)->flags &= ~PERLIO_F_OPEN;
2420 PerlIO_funcs PerlIO_unix = {
2421 sizeof(PerlIO_funcs),
2428 PerlIOBase_binmode, /* binmode */
2438 PerlIOBase_noop_ok, /* flush */
2439 PerlIOBase_noop_fail, /* fill */
2442 PerlIOBase_clearerr,
2443 PerlIOBase_setlinebuf,
2444 NULL, /* get_base */
2445 NULL, /* get_bufsiz */
2448 NULL, /* set_ptrcnt */
2451 /*--------------------------------------------------------------------------------------*/
2456 #if defined(VMS) && !defined(STDIO_BUFFER_WRITABLE)
2457 /* perl5.8 - This ensures the last minute VMS ungetc fix is not
2458 broken by the last second glibc 2.3 fix
2460 #define STDIO_BUFFER_WRITABLE
2465 struct _PerlIO base;
2466 FILE *stdio; /* The stream */
2470 PerlIOStdio_fileno(pTHX_ PerlIO *f)
2473 if (PerlIOValid(f) && (s = PerlIOSelf(f, PerlIOStdio)->stdio)) {
2474 return PerlSIO_fileno(s);
2481 PerlIOStdio_mode(const char *mode, char *tmode)
2487 #ifdef PERLIO_USING_CRLF
2495 PerlIOStdio_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
2498 if (PerlIOValid(f) && PerlIOValid(n = PerlIONext(f))) {
2499 PerlIO_funcs *toptab = PerlIOBase(n)->tab;
2500 if (toptab == tab) {
2501 /* Top is already stdio - pop self (duplicate) and use original */
2502 PerlIO_pop(aTHX_ f);
2505 int fd = PerlIO_fileno(n);
2508 if (fd >= 0 && (stdio = PerlSIO_fdopen(fd,
2509 mode = PerlIOStdio_mode(mode, tmode)))) {
2510 PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
2511 /* We never call down so do any pending stuff now */
2512 PerlIO_flush(PerlIONext(f));
2519 return PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
2524 PerlIO_importFILE(FILE *stdio, const char *mode)
2530 if (!mode || !*mode) {
2531 /* We need to probe to see how we can open the stream
2532 so start with read/write and then try write and read
2533 we dup() so that we can fclose without loosing the fd.
2535 Note that the errno value set by a failing fdopen
2536 varies between stdio implementations.
2538 int fd = PerlLIO_dup(fileno(stdio));
2539 FILE *f2 = PerlSIO_fdopen(fd, (mode = "r+"));
2541 f2 = PerlSIO_fdopen(fd, (mode = "w"));
2544 f2 = PerlSIO_fdopen(fd, (mode = "r"));
2547 /* Don't seem to be able to open */
2553 if ((f = PerlIO_push(aTHX_(f = PerlIO_allocate(aTHX)), &PerlIO_stdio, mode, Nullsv))) {
2554 s = PerlIOSelf(f, PerlIOStdio);
2562 PerlIOStdio_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2563 IV n, const char *mode, int fd, int imode,
2564 int perm, PerlIO *f, int narg, SV **args)
2567 if (PerlIOValid(f)) {
2568 char *path = SvPV_nolen(*args);
2569 PerlIOStdio *s = PerlIOSelf(f, PerlIOStdio);
2571 PerlIOUnix_refcnt_dec(fileno(s->stdio));
2572 stdio = PerlSIO_freopen(path, (mode = PerlIOStdio_mode(mode, tmode)),
2577 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2582 char *path = SvPV_nolen(*args);
2585 fd = PerlLIO_open3(path, imode, perm);
2588 FILE *stdio = PerlSIO_fopen(path, mode);
2592 f = PerlIO_allocate(aTHX);
2594 if ((f = PerlIO_push(aTHX_ f, self,
2595 (mode = PerlIOStdio_mode(mode, tmode)),
2597 s = PerlIOSelf(f, PerlIOStdio);
2599 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2618 stdio = PerlSIO_stdin;
2621 stdio = PerlSIO_stdout;
2624 stdio = PerlSIO_stderr;
2629 stdio = PerlSIO_fdopen(fd, mode =
2630 PerlIOStdio_mode(mode, tmode));
2635 f = PerlIO_allocate(aTHX);
2637 if ((f = PerlIO_push(aTHX_ f, self, mode, PerlIOArg))) {
2638 s = PerlIOSelf(f, PerlIOStdio);
2640 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2650 PerlIOStdio_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
2652 /* This assumes no layers underneath - which is what
2653 happens, but is not how I remember it. NI-S 2001/10/16
2655 if ((f = PerlIOBase_dup(aTHX_ f, o, param, flags))) {
2656 FILE *stdio = PerlIOSelf(o, PerlIOStdio)->stdio;
2657 if (flags & PERLIO_DUP_FD) {
2658 int fd = PerlLIO_dup(fileno(stdio));
2661 stdio = PerlSIO_fdopen(fd, PerlIO_modestr(o,mode));
2664 /* FIXME: To avoid messy error recovery if dup fails
2665 re-use the existing stdio as though flag was not set
2669 PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
2670 PerlIOUnix_refcnt_inc(fileno(stdio));
2676 PerlIOStdio_close(pTHX_ PerlIO *f)
2678 #ifdef SOCKS5_VERSION_NAME
2680 Sock_size_t optlen = sizeof(int);
2682 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2687 if (PerlIOUnix_refcnt_dec(fileno(stdio)) > 0) {
2688 /* Do not close it but do flush any buffers */
2689 return PerlIO_flush(f);
2692 #ifdef SOCKS5_VERSION_NAME
2694 (PerlIO_fileno(f), SOL_SOCKET, SO_TYPE, (void *) &optval,
2696 0) ? PerlSIO_fclose(stdio) : close(PerlIO_fileno(f))
2698 PerlSIO_fclose(stdio)
2707 PerlIOStdio_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
2709 FILE *s = PerlIOSelf(f, PerlIOStdio)->stdio;
2712 STDCHAR *buf = (STDCHAR *) vbuf;
2714 * Perl is expecting PerlIO_getc() to fill the buffer Linux's
2715 * stdio does not do that for fread()
2717 int ch = PerlSIO_fgetc(s);
2724 got = PerlSIO_fread(vbuf, 1, count, s);
2729 PerlIOStdio_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
2732 FILE *s = PerlIOSelf(f, PerlIOStdio)->stdio;
2734 #ifdef STDIO_BUFFER_WRITABLE
2735 if (PerlIO_fast_gets(f) && PerlIO_has_base(f)) {
2736 STDCHAR *buf = ((STDCHAR *) vbuf) + count;
2737 STDCHAR *base = PerlIO_get_base(f);
2738 SSize_t cnt = PerlIO_get_cnt(f);
2739 STDCHAR *ptr = PerlIO_get_ptr(f);
2740 SSize_t avail = ptr - base;
2742 if (avail > count) {
2746 Move(buf-avail,ptr,avail,STDCHAR);
2749 PerlIO_set_ptrcnt(f,ptr,cnt+avail);
2750 if (PerlSIO_feof(s) && unread >= 0)
2751 PerlSIO_clearerr(s);
2756 if (PerlIO_has_cntptr(f)) {
2757 /* We can get pointer to buffer but not its base
2758 Do ungetc() but check chars are ending up in the
2761 STDCHAR *eptr = (STDCHAR*)PerlSIO_get_ptr(s);
2762 STDCHAR *buf = ((STDCHAR *) vbuf) + count;
2764 int ch = *--buf & 0xFF;
2765 if (ungetc(ch,s) != ch) {
2766 /* ungetc did not work */
2769 if ((STDCHAR*)PerlSIO_get_ptr(s) != --eptr || ((*eptr & 0xFF) != ch)) {
2770 /* Did not change pointer as expected */
2771 fgetc(s); /* get char back again */
2781 unread += PerlIOBase_unread(aTHX_ f, vbuf, count);
2787 PerlIOStdio_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
2789 return PerlSIO_fwrite(vbuf, 1, count,
2790 PerlIOSelf(f, PerlIOStdio)->stdio);
2794 PerlIOStdio_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
2796 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2797 return PerlSIO_fseek(stdio, offset, whence);
2801 PerlIOStdio_tell(pTHX_ PerlIO *f)
2803 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2804 return PerlSIO_ftell(stdio);
2808 PerlIOStdio_flush(pTHX_ PerlIO *f)
2810 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2811 if (PerlIOBase(f)->flags & PERLIO_F_CANWRITE) {
2812 return PerlSIO_fflush(stdio);
2817 * FIXME: This discards ungetc() and pre-read stuff which is not
2818 * right if this is just a "sync" from a layer above Suspect right
2819 * design is to do _this_ but not have layer above flush this
2820 * layer read-to-read
2823 * Not writeable - sync by attempting a seek
2826 if (PerlSIO_fseek(stdio, (Off_t) 0, SEEK_CUR) != 0)
2834 PerlIOStdio_eof(pTHX_ PerlIO *f)
2836 return PerlSIO_feof(PerlIOSelf(f, PerlIOStdio)->stdio);
2840 PerlIOStdio_error(pTHX_ PerlIO *f)
2842 return PerlSIO_ferror(PerlIOSelf(f, PerlIOStdio)->stdio);
2846 PerlIOStdio_clearerr(pTHX_ PerlIO *f)
2848 PerlSIO_clearerr(PerlIOSelf(f, PerlIOStdio)->stdio);
2852 PerlIOStdio_setlinebuf(pTHX_ PerlIO *f)
2854 #ifdef HAS_SETLINEBUF
2855 PerlSIO_setlinebuf(PerlIOSelf(f, PerlIOStdio)->stdio);
2857 PerlSIO_setvbuf(PerlIOSelf(f, PerlIOStdio)->stdio, Nullch, _IOLBF, 0);
2863 PerlIOStdio_get_base(pTHX_ PerlIO *f)
2865 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2866 return (STDCHAR*)PerlSIO_get_base(stdio);
2870 PerlIOStdio_get_bufsiz(pTHX_ PerlIO *f)
2872 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2873 return PerlSIO_get_bufsiz(stdio);
2877 #ifdef USE_STDIO_PTR
2879 PerlIOStdio_get_ptr(pTHX_ PerlIO *f)
2881 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2882 return (STDCHAR*)PerlSIO_get_ptr(stdio);
2886 PerlIOStdio_get_cnt(pTHX_ PerlIO *f)
2888 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2889 return PerlSIO_get_cnt(stdio);
2893 PerlIOStdio_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
2895 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2897 #ifdef STDIO_PTR_LVALUE
2898 PerlSIO_set_ptr(stdio, (void*)ptr); /* LHS STDCHAR* cast non-portable */
2899 #ifdef STDIO_PTR_LVAL_SETS_CNT
2900 if (PerlSIO_get_cnt(stdio) != (cnt)) {
2901 assert(PerlSIO_get_cnt(stdio) == (cnt));
2904 #if (!defined(STDIO_PTR_LVAL_NOCHANGE_CNT))
2906 * Setting ptr _does_ change cnt - we are done
2910 #else /* STDIO_PTR_LVALUE */
2912 #endif /* STDIO_PTR_LVALUE */
2915 * Now (or only) set cnt
2917 #ifdef STDIO_CNT_LVALUE
2918 PerlSIO_set_cnt(stdio, cnt);
2919 #else /* STDIO_CNT_LVALUE */
2920 #if (defined(STDIO_PTR_LVALUE) && defined(STDIO_PTR_LVAL_SETS_CNT))
2921 PerlSIO_set_ptr(stdio,
2922 PerlSIO_get_ptr(stdio) + (PerlSIO_get_cnt(stdio) -
2924 #else /* STDIO_PTR_LVAL_SETS_CNT */
2926 #endif /* STDIO_PTR_LVAL_SETS_CNT */
2927 #endif /* STDIO_CNT_LVALUE */
2934 PerlIOStdio_fill(pTHX_ PerlIO *f)
2936 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2939 * fflush()ing read-only streams can cause trouble on some stdio-s
2941 if ((PerlIOBase(f)->flags & PERLIO_F_CANWRITE)) {
2942 if (PerlSIO_fflush(stdio) != 0)
2945 c = PerlSIO_fgetc(stdio);
2949 #if (defined(STDIO_PTR_LVALUE) && (defined(STDIO_CNT_LVALUE) || defined(STDIO_PTR_LVAL_SETS_CNT)))
2951 #ifdef STDIO_BUFFER_WRITABLE
2952 if (PerlIO_fast_gets(f) && PerlIO_has_base(f)) {
2953 /* Fake ungetc() to the real buffer in case system's ungetc
2956 STDCHAR *base = (STDCHAR*)PerlSIO_get_base(stdio);
2957 SSize_t cnt = PerlSIO_get_cnt(stdio);
2958 STDCHAR *ptr = (STDCHAR*)PerlSIO_get_ptr(stdio);
2959 if (ptr == base+1) {
2960 *--ptr = (STDCHAR) c;
2961 PerlIOStdio_set_ptrcnt(aTHX_ f,ptr,cnt+1);
2962 if (PerlSIO_feof(stdio))
2963 PerlSIO_clearerr(stdio);
2969 if (PerlIO_has_cntptr(f)) {
2971 if (PerlIOStdio_unread(aTHX_ f,&ch,1) == 1) {
2978 /* An ungetc()d char is handled separately from the regular
2979 * buffer, so we stuff it in the buffer ourselves.
2980 * Should never get called as should hit code above
2982 *(--((*stdio)->_ptr)) = (unsigned char) c;
2985 /* If buffer snoop scheme above fails fall back to
2988 if (PerlSIO_ungetc(c, stdio) != c)
2996 PerlIO_funcs PerlIO_stdio = {
2997 sizeof(PerlIO_funcs),
2999 sizeof(PerlIOStdio),
3000 PERLIO_K_BUFFERED|PERLIO_K_RAW,
3004 PerlIOBase_binmode, /* binmode */
3018 PerlIOStdio_clearerr,
3019 PerlIOStdio_setlinebuf,
3021 PerlIOStdio_get_base,
3022 PerlIOStdio_get_bufsiz,
3027 #ifdef USE_STDIO_PTR
3028 PerlIOStdio_get_ptr,
3029 PerlIOStdio_get_cnt,
3030 #if (defined(STDIO_PTR_LVALUE) && (defined(STDIO_CNT_LVALUE) || defined(STDIO_PTR_LVAL_SETS_CNT)))
3031 PerlIOStdio_set_ptrcnt
3032 #else /* STDIO_PTR_LVALUE */
3034 #endif /* STDIO_PTR_LVALUE */
3035 #else /* USE_STDIO_PTR */
3039 #endif /* USE_STDIO_PTR */
3042 /* Note that calls to PerlIO_exportFILE() are reversed using
3043 * PerlIO_releaseFILE(), not importFILE. */
3045 PerlIO_exportFILE(PerlIO * f, const char *mode)
3049 if (PerlIOValid(f)) {
3052 if (!mode || !*mode) {
3053 mode = PerlIO_modestr(f, buf);
3055 stdio = PerlSIO_fdopen(PerlIO_fileno(f), mode);
3058 /* De-link any lower layers so new :stdio sticks */
3060 if ((f = PerlIO_push(aTHX_ f, &PerlIO_stdio, buf, Nullsv))) {
3061 PerlIOStdio *s = PerlIOSelf(f, PerlIOStdio);
3063 /* Link previous lower layers under new one */
3067 /* restore layers list */
3077 PerlIO_findFILE(PerlIO *f)
3081 if (l->tab == &PerlIO_stdio) {
3082 PerlIOStdio *s = PerlIOSelf(&l, PerlIOStdio);
3085 l = *PerlIONext(&l);
3087 /* Uses fallback "mode" via PerlIO_modestr() in PerlIO_exportFILE */
3088 return PerlIO_exportFILE(f, Nullch);
3091 /* Use this to reverse PerlIO_exportFILE calls. */
3093 PerlIO_releaseFILE(PerlIO *p, FILE *f)
3097 if (l->tab == &PerlIO_stdio) {
3098 PerlIOStdio *s = PerlIOSelf(&l, PerlIOStdio);
3099 if (s->stdio == f) {
3101 PerlIO_pop(aTHX_ p);
3110 /*--------------------------------------------------------------------------------------*/
3112 * perlio buffer layer
3116 PerlIOBuf_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
3118 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3119 int fd = PerlIO_fileno(f);
3120 if (fd >= 0 && PerlLIO_isatty(fd)) {
3121 PerlIOBase(f)->flags |= PERLIO_F_LINEBUF | PERLIO_F_TTY;
3123 if (*PerlIONext(f)) {
3124 Off_t posn = PerlIO_tell(PerlIONext(f));
3125 if (posn != (Off_t) - 1) {
3129 return PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
3133 PerlIOBuf_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
3134 IV n, const char *mode, int fd, int imode, int perm,
3135 PerlIO *f, int narg, SV **args)
3137 if (PerlIOValid(f)) {
3138 PerlIO *next = PerlIONext(f);
3139 PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIOBase(next)->tab);
3140 next = (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
3142 if (!next || (*PerlIOBase(f)->tab->Pushed) (aTHX_ f, mode, PerlIOArg, self) != 0) {
3147 PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIO_default_btm());
3155 f = (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
3158 if (PerlIO_push(aTHX_ f, self, mode, PerlIOArg) == 0) {
3160 * if push fails during open, open fails. close will pop us.
3165 fd = PerlIO_fileno(f);
3166 if (init && fd == 2) {
3168 * Initial stderr is unbuffered
3170 PerlIOBase(f)->flags |= PERLIO_F_UNBUF;
3172 #ifdef PERLIO_USING_CRLF
3173 # ifdef PERLIO_IS_BINMODE_FD
3174 if (PERLIO_IS_BINMODE_FD(fd))
3175 PerlIO_binmode(f, '<'/*not used*/, O_BINARY, Nullch);
3179 * do something about failing setmode()? --jhi
3181 PerlLIO_setmode(fd, O_BINARY);
3190 * This "flush" is akin to sfio's sync in that it handles files in either
3191 * read or write state
3194 PerlIOBuf_flush(pTHX_ PerlIO *f)
3196 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3198 PerlIO *n = PerlIONext(f);
3199 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF) {
3201 * write() the buffer
3203 STDCHAR *buf = b->buf;
3205 while (p < b->ptr) {
3206 SSize_t count = PerlIO_write(n, p, b->ptr - p);
3210 else if (count < 0 || PerlIO_error(n)) {
3211 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
3216 b->posn += (p - buf);
3218 else if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
3219 STDCHAR *buf = PerlIO_get_base(f);
3221 * Note position change
3223 b->posn += (b->ptr - buf);
3224 if (b->ptr < b->end) {
3225 /* We did not consume all of it - try and seek downstream to
3226 our logical position
3228 if (PerlIOValid(n) && PerlIO_seek(n, b->posn, SEEK_SET) == 0) {
3229 /* Reload n as some layers may pop themselves on seek */
3230 b->posn = PerlIO_tell(n = PerlIONext(f));
3233 /* Seek failed (e.g. pipe or tty). Do NOT clear buffer or pre-read
3234 data is lost for good - so return saying "ok" having undone
3237 b->posn -= (b->ptr - buf);
3242 b->ptr = b->end = b->buf;
3243 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
3244 /* We check for Valid because of dubious decision to make PerlIO_flush(NULL) flush all */
3245 if (PerlIOValid(n) && PerlIO_flush(n) != 0)
3251 PerlIOBuf_fill(pTHX_ PerlIO *f)
3253 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3254 PerlIO *n = PerlIONext(f);
3257 * Down-stream flush is defined not to loose read data so is harmless.
3258 * we would not normally be fill'ing if there was data left in anycase.
3260 if (PerlIO_flush(f) != 0)
3262 if (PerlIOBase(f)->flags & PERLIO_F_TTY)
3263 PerlIOBase_flush_linebuf(aTHX);
3266 PerlIO_get_base(f); /* allocate via vtable */
3268 b->ptr = b->end = b->buf;
3270 if (!PerlIOValid(n)) {
3271 PerlIOBase(f)->flags |= PERLIO_F_EOF;
3275 if (PerlIO_fast_gets(n)) {
3277 * Layer below is also buffered. We do _NOT_ want to call its
3278 * ->Read() because that will loop till it gets what we asked for
3279 * which may hang on a pipe etc. Instead take anything it has to
3280 * hand, or ask it to fill _once_.
3282 avail = PerlIO_get_cnt(n);
3284 avail = PerlIO_fill(n);
3286 avail = PerlIO_get_cnt(n);
3288 if (!PerlIO_error(n) && PerlIO_eof(n))
3293 STDCHAR *ptr = PerlIO_get_ptr(n);
3294 SSize_t cnt = avail;
3295 if (avail > (SSize_t)b->bufsiz)
3297 Copy(ptr, b->buf, avail, STDCHAR);
3298 PerlIO_set_ptrcnt(n, ptr + avail, cnt - avail);
3302 avail = PerlIO_read(n, b->ptr, b->bufsiz);
3306 PerlIOBase(f)->flags |= PERLIO_F_EOF;
3308 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
3311 b->end = b->buf + avail;
3312 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3317 PerlIOBuf_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
3319 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3320 if (PerlIOValid(f)) {
3323 return PerlIOBase_read(aTHX_ f, vbuf, count);
3329 PerlIOBuf_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3331 const STDCHAR *buf = (const STDCHAR *) vbuf + count;
3332 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3335 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
3340 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
3342 * Buffer is already a read buffer, we can overwrite any chars
3343 * which have been read back to buffer start
3345 avail = (b->ptr - b->buf);
3349 * Buffer is idle, set it up so whole buffer is available for
3353 b->end = b->buf + avail;
3355 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3357 * Buffer extends _back_ from where we are now
3359 b->posn -= b->bufsiz;
3361 if (avail > (SSize_t) count) {
3363 * If we have space for more than count, just move count
3371 * In simple stdio-like ungetc() case chars will be already
3374 if (buf != b->ptr) {
3375 Copy(buf, b->ptr, avail, STDCHAR);
3379 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
3383 unread += PerlIOBase_unread(aTHX_ f, vbuf, count);
3389 PerlIOBuf_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3391 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3392 const STDCHAR *buf = (const STDCHAR *) vbuf;
3396 if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
3399 SSize_t avail = b->bufsiz - (b->ptr - b->buf);
3400 if ((SSize_t) count < avail)
3402 PerlIOBase(f)->flags |= PERLIO_F_WRBUF;
3403 if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF) {
3418 Copy(buf, b->ptr, avail, STDCHAR);
3425 if (b->ptr >= (b->buf + b->bufsiz))
3428 if (PerlIOBase(f)->flags & PERLIO_F_UNBUF)
3434 PerlIOBuf_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
3437 if ((code = PerlIO_flush(f)) == 0) {
3438 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3439 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
3440 code = PerlIO_seek(PerlIONext(f), offset, whence);
3442 b->posn = PerlIO_tell(PerlIONext(f));
3449 PerlIOBuf_tell(pTHX_ PerlIO *f)
3451 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3453 * b->posn is file position where b->buf was read, or will be written
3455 Off_t posn = b->posn;
3458 * If buffer is valid adjust position by amount in buffer
3460 posn += (b->ptr - b->buf);
3466 PerlIOBuf_popped(pTHX_ PerlIO *f)
3468 IV code = PerlIOBase_popped(aTHX_ f);
3469 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3470 if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
3474 b->ptr = b->end = b->buf;
3475 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
3480 PerlIOBuf_close(pTHX_ PerlIO *f)
3482 IV code = PerlIOBase_close(aTHX_ f);
3483 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3484 if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
3488 b->ptr = b->end = b->buf;
3489 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
3494 PerlIOBuf_get_ptr(pTHX_ PerlIO *f)
3496 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3503 PerlIOBuf_get_cnt(pTHX_ PerlIO *f)
3505 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3508 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF)
3509 return (b->end - b->ptr);
3514 PerlIOBuf_get_base(pTHX_ PerlIO *f)
3516 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3521 Newz('B',b->buf,b->bufsiz, STDCHAR);
3523 b->buf = (STDCHAR *) & b->oneword;
3524 b->bufsiz = sizeof(b->oneword);
3533 PerlIOBuf_bufsiz(pTHX_ PerlIO *f)
3535 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3538 return (b->end - b->buf);
3542 PerlIOBuf_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
3544 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3548 if (PerlIO_get_cnt(f) != cnt || b->ptr < b->buf) {
3549 assert(PerlIO_get_cnt(f) == cnt);
3550 assert(b->ptr >= b->buf);
3552 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3556 PerlIOBuf_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
3558 return PerlIOBase_dup(aTHX_ f, o, param, flags);
3563 PerlIO_funcs PerlIO_perlio = {
3564 sizeof(PerlIO_funcs),
3567 PERLIO_K_BUFFERED|PERLIO_K_RAW,
3571 PerlIOBase_binmode, /* binmode */
3585 PerlIOBase_clearerr,
3586 PerlIOBase_setlinebuf,
3591 PerlIOBuf_set_ptrcnt,
3594 /*--------------------------------------------------------------------------------------*/
3596 * Temp layer to hold unread chars when cannot do it any other way
3600 PerlIOPending_fill(pTHX_ PerlIO *f)
3603 * Should never happen
3610 PerlIOPending_close(pTHX_ PerlIO *f)
3613 * A tad tricky - flush pops us, then we close new top
3616 return PerlIO_close(f);
3620 PerlIOPending_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
3623 * A tad tricky - flush pops us, then we seek new top
3626 return PerlIO_seek(f, offset, whence);
3631 PerlIOPending_flush(pTHX_ PerlIO *f)
3633 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3634 if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
3638 PerlIO_pop(aTHX_ f);
3643 PerlIOPending_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
3649 PerlIOBuf_set_ptrcnt(aTHX_ f, ptr, cnt);
3654 PerlIOPending_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
3656 IV code = PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
3657 PerlIOl *l = PerlIOBase(f);
3659 * Our PerlIO_fast_gets must match what we are pushed on, or sv_gets()
3660 * etc. get muddled when it changes mid-string when we auto-pop.
3662 l->flags = (l->flags & ~(PERLIO_F_FASTGETS | PERLIO_F_UTF8)) |
3663 (PerlIOBase(PerlIONext(f))->
3664 flags & (PERLIO_F_FASTGETS | PERLIO_F_UTF8));
3669 PerlIOPending_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
3671 SSize_t avail = PerlIO_get_cnt(f);
3673 if ((SSize_t)count < avail)
3676 got = PerlIOBuf_read(aTHX_ f, vbuf, avail);
3677 if (got >= 0 && got < (SSize_t)count) {
3679 PerlIO_read(f, ((STDCHAR *) vbuf) + got, count - got);
3680 if (more >= 0 || got == 0)
3686 PerlIO_funcs PerlIO_pending = {
3687 sizeof(PerlIO_funcs),
3690 PERLIO_K_BUFFERED|PERLIO_K_RAW, /* not sure about RAW here */
3691 PerlIOPending_pushed,
3694 PerlIOBase_binmode, /* binmode */
3703 PerlIOPending_close,
3704 PerlIOPending_flush,
3708 PerlIOBase_clearerr,
3709 PerlIOBase_setlinebuf,
3714 PerlIOPending_set_ptrcnt,
3719 /*--------------------------------------------------------------------------------------*/
3721 * crlf - translation On read translate CR,LF to "\n" we do this by
3722 * overriding ptr/cnt entries to hand back a line at a time and keeping a
3723 * record of which nl we "lied" about. On write translate "\n" to CR,LF
3727 PerlIOBuf base; /* PerlIOBuf stuff */
3728 STDCHAR *nl; /* Position of crlf we "lied" about in the
3733 PerlIOCrlf_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
3736 PerlIOBase(f)->flags |= PERLIO_F_CRLF;
3737 code = PerlIOBuf_pushed(aTHX_ f, mode, arg, tab);
3739 PerlIO_debug("PerlIOCrlf_pushed f=%p %s %s fl=%08" UVxf "\n",
3740 f, PerlIOBase(f)->tab->name, (mode) ? mode : "(Null)",
3741 PerlIOBase(f)->flags);
3748 PerlIOCrlf_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3750 PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
3755 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF))
3756 return PerlIOBuf_unread(aTHX_ f, vbuf, count);
3758 const STDCHAR *buf = (const STDCHAR *) vbuf + count;
3759 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3761 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
3766 if (!(PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
3767 b->end = b->ptr = b->buf + b->bufsiz;
3768 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3769 b->posn -= b->bufsiz;
3771 while (count > 0 && b->ptr > b->buf) {
3774 if (b->ptr - 2 >= b->buf) {
3797 PerlIOCrlf_get_cnt(pTHX_ PerlIO *f)
3799 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3802 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
3803 PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
3804 if ((PerlIOBase(f)->flags & PERLIO_F_CRLF) && (!c->nl || *c->nl == 0xd)) {
3805 STDCHAR *nl = (c->nl) ? c->nl : b->ptr;
3807 while (nl < b->end && *nl != 0xd)
3809 if (nl < b->end && *nl == 0xd) {
3811 if (nl + 1 < b->end) {
3818 * Not CR,LF but just CR
3826 * Blast - found CR as last char in buffer
3831 * They may not care, defer work as long as
3835 return (nl - b->ptr);
3839 b->ptr++; /* say we have read it as far as
3840 * flush() is concerned */
3841 b->buf++; /* Leave space in front of buffer */
3842 b->bufsiz--; /* Buffer is thus smaller */
3843 code = PerlIO_fill(f); /* Fetch some more */
3844 b->bufsiz++; /* Restore size for next time */
3845 b->buf--; /* Point at space */
3846 b->ptr = nl = b->buf; /* Which is what we hand
3848 b->posn--; /* Buffer starts here */
3849 *nl = 0xd; /* Fill in the CR */
3851 goto test; /* fill() call worked */
3853 * CR at EOF - just fall through
3855 /* Should we clear EOF though ??? */
3860 return (((c->nl) ? (c->nl + 1) : b->end) - b->ptr);
3866 PerlIOCrlf_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
3868 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3869 PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
3875 if (ptr == b->end && *c->nl == 0xd) {
3876 /* Defered CR at end of buffer case - we lied about count */
3888 * Test code - delete when it works ...
3890 IV flags = PerlIOBase(f)->flags;
3891 STDCHAR *chk = (c->nl) ? (c->nl+1) : b->end;
3892 if (ptr+cnt == c->nl && c->nl+1 == b->end && *c->nl == 0xd) {
3893 /* Defered CR at end of buffer case - we lied about count */
3899 Perl_croak(aTHX_ "ptr wrong %p != %p fl=%08" UVxf
3900 " nl=%p e=%p for %d", ptr, chk, flags, c->nl,
3908 * They have taken what we lied about
3916 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3920 PerlIOCrlf_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3922 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF))
3923 return PerlIOBuf_write(aTHX_ f, vbuf, count);
3925 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3926 const STDCHAR *buf = (const STDCHAR *) vbuf;
3927 const STDCHAR *ebuf = buf + count;
3930 if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
3932 while (buf < ebuf) {
3933 STDCHAR *eptr = b->buf + b->bufsiz;
3934 PerlIOBase(f)->flags |= PERLIO_F_WRBUF;
3935 while (buf < ebuf && b->ptr < eptr) {
3937 if ((b->ptr + 2) > eptr) {
3945 *(b->ptr)++ = 0xd; /* CR */
3946 *(b->ptr)++ = 0xa; /* LF */
3948 if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF) {
3958 if (b->ptr >= eptr) {
3964 if (PerlIOBase(f)->flags & PERLIO_F_UNBUF)
3966 return (buf - (STDCHAR *) vbuf);
3971 PerlIOCrlf_flush(pTHX_ PerlIO *f)
3973 PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
3978 return PerlIOBuf_flush(aTHX_ f);
3982 PerlIOCrlf_binmode(pTHX_ PerlIO *f)
3984 if ((PerlIOBase(f)->flags & PERLIO_F_CRLF)) {
3985 /* In text mode - flush any pending stuff and flip it */
3986 PerlIOBase(f)->flags &= ~PERLIO_F_CRLF;
3987 #ifndef PERLIO_USING_CRLF
3988 /* CRLF is unusual case - if this is just the :crlf layer pop it */
3989 if (PerlIOBase(f)->tab == &PerlIO_crlf) {
3990 PerlIO_pop(aTHX_ f);
3997 PerlIO_funcs PerlIO_crlf = {
3998 sizeof(PerlIO_funcs),
4001 PERLIO_K_BUFFERED | PERLIO_K_CANCRLF | PERLIO_K_RAW,
4003 PerlIOBuf_popped, /* popped */
4005 PerlIOCrlf_binmode, /* binmode */
4009 PerlIOBuf_read, /* generic read works with ptr/cnt lies
4011 PerlIOCrlf_unread, /* Put CR,LF in buffer for each '\n' */
4012 PerlIOCrlf_write, /* Put CR,LF in buffer for each '\n' */
4020 PerlIOBase_clearerr,
4021 PerlIOBase_setlinebuf,
4026 PerlIOCrlf_set_ptrcnt,
4030 /*--------------------------------------------------------------------------------------*/
4032 * mmap as "buffer" layer
4036 PerlIOBuf base; /* PerlIOBuf stuff */
4037 Mmap_t mptr; /* Mapped address */
4038 Size_t len; /* mapped length */
4039 STDCHAR *bbuf; /* malloced buffer if map fails */
4042 static size_t page_size = 0;
4045 PerlIOMmap_map(pTHX_ PerlIO *f)
4047 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
4048 IV flags = PerlIOBase(f)->flags;
4052 if (flags & PERLIO_F_CANREAD) {
4053 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
4054 int fd = PerlIO_fileno(f);
4056 code = Fstat(fd, &st);
4057 if (code == 0 && S_ISREG(st.st_mode)) {
4058 SSize_t len = st.st_size - b->posn;
4062 #if defined(HAS_SYSCONF) && (defined(_SC_PAGESIZE) || defined(_SC_PAGE_SIZE))
4064 SETERRNO(0, SS_NORMAL);
4065 # ifdef _SC_PAGESIZE
4066 page_size = sysconf(_SC_PAGESIZE);
4068 page_size = sysconf(_SC_PAGE_SIZE);
4070 if ((long) page_size < 0) {
4075 (void) SvUPGRADE(error, SVt_PV);
4076 msg = SvPVx(error, n_a);
4077 Perl_croak(aTHX_ "panic: sysconf: %s",
4082 "panic: sysconf: pagesize unknown");
4086 # ifdef HAS_GETPAGESIZE
4087 page_size = getpagesize();
4089 # if defined(I_SYS_PARAM) && defined(PAGESIZE)
4090 page_size = PAGESIZE; /* compiletime, bad */
4094 if ((IV) page_size <= 0)
4095 Perl_croak(aTHX_ "panic: bad pagesize %" IVdf,
4100 * This is a hack - should never happen - open should
4103 b->posn = PerlIO_tell(PerlIONext(f));
4105 posn = (b->posn / page_size) * page_size;
4106 len = st.st_size - posn;
4107 m->mptr = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, posn);
4108 if (m->mptr && m->mptr != (Mmap_t) - 1) {
4109 #if 0 && defined(HAS_MADVISE) && defined(MADV_SEQUENTIAL)
4110 madvise(m->mptr, len, MADV_SEQUENTIAL);
4112 #if 0 && defined(HAS_MADVISE) && defined(MADV_WILLNEED)
4113 madvise(m->mptr, len, MADV_WILLNEED);
4115 PerlIOBase(f)->flags =
4116 (flags & ~PERLIO_F_EOF) | PERLIO_F_RDBUF;
4117 b->end = ((STDCHAR *) m->mptr) + len;
4118 b->buf = ((STDCHAR *) m->mptr) + (b->posn - posn);
4127 PerlIOBase(f)->flags =
4128 flags | PERLIO_F_EOF | PERLIO_F_RDBUF;
4130 b->ptr = b->end = b->ptr;
4139 PerlIOMmap_unmap(pTHX_ PerlIO *f)
4141 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
4142 PerlIOBuf *b = &m->base;
4146 code = munmap(m->mptr, m->len);
4150 if (PerlIO_seek(PerlIONext(f), b->posn, SEEK_SET) != 0)
4153 b->ptr = b->end = b->buf;
4154 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
4160 PerlIOMmap_get_base(pTHX_ PerlIO *f)
4162 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
4163 PerlIOBuf *b = &m->base;
4164 if (b->buf && (PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
4166 * Already have a readbuffer in progress
4172 * We have a write buffer or flushed PerlIOBuf read buffer
4174 m->bbuf = b->buf; /* save it in case we need it again */
4175 b->buf = NULL; /* Clear to trigger below */
4178 PerlIOMmap_map(aTHX_ f); /* Try and map it */
4181 * Map did not work - recover PerlIOBuf buffer if we have one
4186 b->ptr = b->end = b->buf;
4189 return PerlIOBuf_get_base(aTHX_ f);
4193 PerlIOMmap_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
4195 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
4196 PerlIOBuf *b = &m->base;
4197 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
4199 if (b->ptr && (b->ptr - count) >= b->buf
4200 && memEQ(b->ptr - count, vbuf, count)) {
4202 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
4207 * Loose the unwritable mapped buffer
4211 * If flush took the "buffer" see if we have one from before
4213 if (!b->buf && m->bbuf)
4216 PerlIOBuf_get_base(aTHX_ f);
4220 return PerlIOBuf_unread(aTHX_ f, vbuf, count);
4224 PerlIOMmap_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
4226 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
4227 PerlIOBuf *b = &m->base;
4228 if (!b->buf || !(PerlIOBase(f)->flags & PERLIO_F_WRBUF)) {
4230 * No, or wrong sort of, buffer
4233 if (PerlIOMmap_unmap(aTHX_ f) != 0)
4237 * If unmap took the "buffer" see if we have one from before
4239 if (!b->buf && m->bbuf)
4242 PerlIOBuf_get_base(aTHX_ f);
4246 return PerlIOBuf_write(aTHX_ f, vbuf, count);
4250 PerlIOMmap_flush(pTHX_ PerlIO *f)
4252 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
4253 PerlIOBuf *b = &m->base;
4254 IV code = PerlIOBuf_flush(aTHX_ f);
4256 * Now we are "synced" at PerlIOBuf level
4263 if (PerlIOMmap_unmap(aTHX_ f) != 0)
4268 * We seem to have a PerlIOBuf buffer which was not mapped
4269 * remember it in case we need one later
4278 PerlIOMmap_fill(pTHX_ PerlIO *f)
4280 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
4281 IV code = PerlIO_flush(f);
4282 if (code == 0 && !b->buf) {
4283 code = PerlIOMmap_map(aTHX_ f);
4285 if (code == 0 && !(PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
4286 code = PerlIOBuf_fill(aTHX_ f);
4292 PerlIOMmap_close(pTHX_ PerlIO *f)
4294 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
4295 PerlIOBuf *b = &m->base;
4296 IV code = PerlIO_flush(f);
4300 b->ptr = b->end = b->buf;
4302 if (PerlIOBuf_close(aTHX_ f) != 0)
4308 PerlIOMmap_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
4310 return PerlIOBase_dup(aTHX_ f, o, param, flags);
4314 PerlIO_funcs PerlIO_mmap = {
4315 sizeof(PerlIO_funcs),
4318 PERLIO_K_BUFFERED|PERLIO_K_RAW,
4322 PerlIOBase_binmode, /* binmode */
4336 PerlIOBase_clearerr,
4337 PerlIOBase_setlinebuf,
4338 PerlIOMmap_get_base,
4342 PerlIOBuf_set_ptrcnt,
4345 #endif /* HAS_MMAP */
4348 Perl_PerlIO_stdin(pTHX)
4351 PerlIO_stdstreams(aTHX);
4353 return &PL_perlio[1];
4357 Perl_PerlIO_stdout(pTHX)
4360 PerlIO_stdstreams(aTHX);
4362 return &PL_perlio[2];
4366 Perl_PerlIO_stderr(pTHX)
4369 PerlIO_stdstreams(aTHX);
4371 return &PL_perlio[3];
4374 /*--------------------------------------------------------------------------------------*/
4377 PerlIO_getname(PerlIO *f, char *buf)
4382 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
4384 name = fgetname(stdio, buf);
4386 Perl_croak(aTHX_ "Don't know how to get file name");
4392 /*--------------------------------------------------------------------------------------*/
4394 * Functions which can be called on any kind of PerlIO implemented in
4398 #undef PerlIO_fdopen
4400 PerlIO_fdopen(int fd, const char *mode)
4403 return PerlIO_openn(aTHX_ Nullch, mode, fd, 0, 0, NULL, 0, NULL);
4408 PerlIO_open(const char *path, const char *mode)
4411 SV *name = sv_2mortal(newSVpvn(path, strlen(path)));
4412 return PerlIO_openn(aTHX_ Nullch, mode, -1, 0, 0, NULL, 1, &name);
4415 #undef Perlio_reopen
4417 PerlIO_reopen(const char *path, const char *mode, PerlIO *f)
4420 SV *name = sv_2mortal(newSVpvn(path, strlen(path)));
4421 return PerlIO_openn(aTHX_ Nullch, mode, -1, 0, 0, f, 1, &name);
4426 PerlIO_getc(PerlIO *f)
4430 SSize_t count = PerlIO_read(f, buf, 1);
4432 return (unsigned char) buf[0];
4437 #undef PerlIO_ungetc
4439 PerlIO_ungetc(PerlIO *f, int ch)
4444 if (PerlIO_unread(f, &buf, 1) == 1)
4452 PerlIO_putc(PerlIO *f, int ch)
4456 return PerlIO_write(f, &buf, 1);
4461 PerlIO_puts(PerlIO *f, const char *s)
4464 STRLEN len = strlen(s);
4465 return PerlIO_write(f, s, len);
4468 #undef PerlIO_rewind
4470 PerlIO_rewind(PerlIO *f)
4473 PerlIO_seek(f, (Off_t) 0, SEEK_SET);
4477 #undef PerlIO_vprintf
4479 PerlIO_vprintf(PerlIO *f, const char *fmt, va_list ap)
4482 SV *sv = newSVpvn("", 0);
4488 Perl_va_copy(ap, apc);
4489 sv_vcatpvf(sv, fmt, &apc);
4491 sv_vcatpvf(sv, fmt, &ap);
4494 wrote = PerlIO_write(f, s, len);
4499 #undef PerlIO_printf
4501 PerlIO_printf(PerlIO *f, const char *fmt, ...)
4506 result = PerlIO_vprintf(f, fmt, ap);
4511 #undef PerlIO_stdoutf
4513 PerlIO_stdoutf(const char *fmt, ...)
4519 result = PerlIO_vprintf(PerlIO_stdout(), fmt, ap);
4524 #undef PerlIO_tmpfile
4526 PerlIO_tmpfile(void)
4529 * I have no idea how portable mkstemp() is ...
4531 #if defined(WIN32) || !defined(HAVE_MKSTEMP)
4534 FILE *stdio = PerlSIO_tmpfile();
4536 if ((f = PerlIO_push(aTHX_(PerlIO_allocate(aTHX)), &PerlIO_stdio, "w+", Nullsv))) {
4537 PerlIOStdio *s = PerlIOSelf(f, PerlIOStdio);
4544 SV *sv = newSVpv("/tmp/PerlIO_XXXXXX", 0);
4545 int fd = mkstemp(SvPVX(sv));
4548 f = PerlIO_fdopen(fd, "w+");
4550 PerlIOBase(f)->flags |= PERLIO_F_TEMP;
4552 PerlLIO_unlink(SvPVX(sv));
4562 #endif /* USE_SFIO */
4563 #endif /* PERLIO_IS_STDIO */
4565 /*======================================================================================*/
4567 * Now some functions in terms of above which may be needed even if we are
4568 * not in true PerlIO mode
4572 #undef PerlIO_setpos
4574 PerlIO_setpos(PerlIO *f, SV *pos)
4579 Off_t *posn = (Off_t *) SvPV(pos, len);
4580 if (f && len == sizeof(Off_t))
4581 return PerlIO_seek(f, *posn, SEEK_SET);
4583 SETERRNO(EINVAL, SS_IVCHAN);
4587 #undef PerlIO_setpos
4589 PerlIO_setpos(PerlIO *f, SV *pos)
4594 Fpos_t *fpos = (Fpos_t *) SvPV(pos, len);
4595 if (f && len == sizeof(Fpos_t)) {
4596 #if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64)
4597 return fsetpos64(f, fpos);
4599 return fsetpos(f, fpos);
4603 SETERRNO(EINVAL, SS_IVCHAN);
4609 #undef PerlIO_getpos
4611 PerlIO_getpos(PerlIO *f, SV *pos)
4614 Off_t posn = PerlIO_tell(f);
4615 sv_setpvn(pos, (char *) &posn, sizeof(posn));
4616 return (posn == (Off_t) - 1) ? -1 : 0;
4619 #undef PerlIO_getpos
4621 PerlIO_getpos(PerlIO *f, SV *pos)
4626 #if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64)
4627 code = fgetpos64(f, &fpos);
4629 code = fgetpos(f, &fpos);
4631 sv_setpvn(pos, (char *) &fpos, sizeof(fpos));
4636 #if (defined(PERLIO_IS_STDIO) || !defined(USE_SFIO)) && !defined(HAS_VPRINTF)
4639 vprintf(char *pat, char *args)
4641 _doprnt(pat, args, stdout);
4642 return 0; /* wrong, but perl doesn't use the return
4647 vfprintf(FILE *fd, char *pat, char *args)
4649 _doprnt(pat, args, fd);
4650 return 0; /* wrong, but perl doesn't use the return
4656 #ifndef PerlIO_vsprintf
4658 PerlIO_vsprintf(char *s, int n, const char *fmt, va_list ap)
4660 int val = vsprintf(s, fmt, ap);
4662 if (strlen(s) >= (STRLEN) n) {
4664 (void) PerlIO_puts(Perl_error_log,
4665 "panic: sprintf overflow - memory corrupted!\n");
4673 #ifndef PerlIO_sprintf
4675 PerlIO_sprintf(char *s, int n, const char *fmt, ...)
4680 result = PerlIO_vsprintf(s, n, fmt, ap);