2 * perlio.c Copyright (c) 1996-2001, 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 * If we have ActivePerl-like PERL_IMPLICIT_SYS then we need a dTHX to get
9 * at the dispatch tables, even when we do not need it for other reasons.
10 * Invent a dSYS macro to abstract this out
12 #ifdef PERL_IMPLICIT_SYS
25 #define PERLIO_NOT_STDIO 0
26 #if !defined(PERLIO_IS_STDIO) && !defined(USE_SFIO)
32 * This file provides those parts of PerlIO abstraction
33 * which are not #defined in perlio.h.
34 * Which these are depends on various Configure #ifdef's
38 #define PERL_IN_PERLIO_C
41 #ifdef PERL_IMPLICIT_CONTEXT
49 perlsio_binmode(FILE *fp, int iotype, int mode)
52 * This used to be contents of do_binmode in doio.c
55 # if defined(atarist) || defined(__MINT__)
58 ((FILE *) fp)->_flag |= _IOBIN;
60 ((FILE *) fp)->_flag &= ~_IOBIN;
67 if (PerlLIO_setmode(fp, mode) != -1) {
69 if (PerlLIO_setmode(fileno(fp), mode) != -1) {
71 # if defined(WIN32) && defined(__BORLANDC__)
73 * The translation mode of the stream is maintained independent of
74 * the translation mode of the fd in the Borland RTL (heavy
75 * digging through their runtime sources reveal). User has to set
76 * the mode explicitly for the stream (though they don't document
77 * this anywhere). GSAR 97-5-24
91 # if defined(USEMYBINMODE)
92 if (my_binmode(fp, iotype, mode) != FALSE)
103 #define O_ACCMODE 3 /* Assume traditional implementation */
107 PerlIO_intmode2str(int rawmode, char *mode, int *writing)
109 int result = rawmode & O_ACCMODE;
114 ptype = IoTYPE_RDONLY;
117 ptype = IoTYPE_WRONLY;
125 *writing = (result != O_RDONLY);
127 if (result == O_RDONLY) {
131 else if (rawmode & O_APPEND) {
133 if (result != O_WRONLY)
138 if (result == O_WRONLY)
145 if (rawmode & O_BINARY)
151 #ifndef PERLIO_LAYERS
153 PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names)
155 if (!names || !*names || strEQ(names, ":crlf") || strEQ(names, ":raw")) {
158 Perl_croak(aTHX_ "Cannot apply \"%s\" in non-PerlIO perl", names);
166 PerlIO_destruct(pTHX)
171 PerlIO_binmode(pTHX_ PerlIO *fp, int iotype, int mode, const char *names)
176 return perlsio_binmode(fp, iotype, mode);
181 PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
185 int fd = PerlLIO_dup(PerlIO_fileno(f));
188 int omode = fcntl(fd, F_GETFL);
189 PerlIO_intmode2str(omode,mode,NULL);
190 /* the r+ is a hack */
191 return PerlIO_fdopen(fd, mode);
196 SETERRNO(EBADF, SS$_IVCHAN);
204 * De-mux PerlIO_openn() into fdopen, freopen and fopen type entries
208 PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd,
209 int imode, int perm, PerlIO *old, int narg, SV **args)
212 if (*args == &PL_sv_undef)
213 return PerlIO_tmpfile();
215 char *name = SvPV_nolen(*args);
217 fd = PerlLIO_open3(name, imode, perm);
219 return PerlIO_fdopen(fd, (char *) mode + 1);
222 return PerlIO_reopen(name, mode, old);
225 return PerlIO_open(name, mode);
230 return PerlIO_fdopen(fd, (char *) mode);
235 XS(XS_PerlIO__Layer__find)
239 Perl_croak(aTHX_ "Usage class->find(name[,load])");
241 char *name = SvPV_nolen(ST(1));
242 ST(0) = (strEQ(name, "crlf")
243 || strEQ(name, "raw")) ? &PL_sv_yes : &PL_sv_undef;
250 Perl_boot_core_PerlIO(pTHX)
252 newXS("PerlIO::Layer::find", XS_PerlIO__Layer__find, __FILE__);
258 #ifdef PERLIO_IS_STDIO
264 * Does nothing (yet) except force this file to be included in perl
265 * binary. That allows this file to force inclusion of other functions
266 * that may be required by loadable extensions e.g. for
267 * FileHandle::tmpfile
271 #undef PerlIO_tmpfile
278 #else /* PERLIO_IS_STDIO */
286 * This section is just to make sure these functions get pulled in from
290 #undef PerlIO_tmpfile
301 * Force this file to be included in perl binary. Which allows this
302 * file to force inclusion of other functions that may be required by
303 * loadable extensions e.g. for FileHandle::tmpfile
307 * Hack sfio does its own 'autoflush' on stdout in common cases. Flush
308 * results in a lot of lseek()s to regular files and lot of small
311 sfset(sfstdout, SF_SHARE, 0);
315 PerlIO_importFILE(FILE *stdio, int fl)
317 int fd = fileno(stdio);
318 PerlIO *r = PerlIO_fdopen(fd, "r+");
323 PerlIO_findFILE(PerlIO *pio)
325 int fd = PerlIO_fileno(pio);
326 FILE *f = fdopen(fd, "r+");
328 if (!f && errno == EINVAL)
330 if (!f && errno == EINVAL)
337 /*======================================================================================*/
339 * Implement all the PerlIO interface ourselves.
345 * We _MUST_ have <unistd.h> if we are using lseek() and may have large
352 #include <sys/mman.h>
356 void PerlIO_debug(const char *fmt, ...)
357 __attribute__ ((format(__printf__, 1, 2)));
360 PerlIO_debug(const char *fmt, ...)
367 char *s = PerlEnv_getenv("PERLIO_DEBUG");
369 dbg = PerlLIO_open3(s, O_WRONLY | O_CREAT | O_APPEND, 0666);
376 /* Use fixed buffer as sv_catpvf etc. needs SVs */
380 s = CopFILE(PL_curcop);
383 sprintf(buffer, "%s:%" IVdf " ", s, (IV) CopLINE(PL_curcop));
384 len = strlen(buffer);
385 vsprintf(buffer+len, fmt, ap);
386 PerlLIO_write(dbg, buffer, strlen(buffer));
388 SV *sv = newSVpvn("", 0);
391 s = CopFILE(PL_curcop);
394 Perl_sv_catpvf(aTHX_ sv, "%s:%" IVdf " ", s,
395 (IV) CopLINE(PL_curcop));
396 Perl_sv_vcatpvf(aTHX_ sv, fmt, &ap);
399 PerlLIO_write(dbg, s, len);
406 /*--------------------------------------------------------------------------------------*/
409 * Inner level routines
413 * Table of pointers to the PerlIO structs (malloc'ed)
415 #define PERLIO_TABLE_SIZE 64
418 PerlIO_allocate(pTHX)
421 * Find a free slot in the table, allocating new table as necessary
426 while ((f = *last)) {
428 last = (PerlIO **) (f);
429 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
435 Newz('I',f,PERLIO_TABLE_SIZE,PerlIO);
443 #undef PerlIO_fdupopen
445 PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
448 PerlIO_funcs *tab = PerlIOBase(f)->tab;
450 PerlIO_debug("fdupopen f=%p param=%p\n",f,param);
451 new = (*tab->Dup)(aTHX_ PerlIO_allocate(aTHX),f,param, flags);
455 SETERRNO(EBADF, SS$_IVCHAN);
461 PerlIO_cleantable(pTHX_ PerlIO **tablep)
463 PerlIO *table = *tablep;
466 PerlIO_cleantable(aTHX_(PerlIO **) & (table[0]));
467 for (i = PERLIO_TABLE_SIZE - 1; i > 0; i--) {
468 PerlIO *f = table + i;
480 PerlIO_list_alloc(pTHX)
483 Newz('L', list, 1, PerlIO_list_t);
489 PerlIO_list_free(pTHX_ PerlIO_list_t *list)
492 if (--list->refcnt == 0) {
495 for (i = 0; i < list->cur; i++) {
496 if (list->array[i].arg)
497 SvREFCNT_dec(list->array[i].arg);
499 Safefree(list->array);
507 PerlIO_list_push(pTHX_ PerlIO_list_t *list, PerlIO_funcs *funcs, SV *arg)
510 if (list->cur >= list->len) {
513 Renew(list->array, list->len, PerlIO_pair_t);
515 New('l', list->array, list->len, PerlIO_pair_t);
517 p = &(list->array[list->cur++]);
519 if ((p->arg = arg)) {
525 PerlIO_clone_list(pTHX_ PerlIO_list_t *proto, CLONE_PARAMS *param)
527 PerlIO_list_t *list = (PerlIO_list_t *) NULL;
530 list = PerlIO_list_alloc(aTHX);
531 for (i=0; i < proto->cur; i++) {
533 if (proto->array[i].arg)
534 arg = PerlIO_sv_dup(aTHX_ proto->array[i].arg,param);
535 PerlIO_list_push(aTHX_ list, proto->array[i].funcs, arg);
542 PerlIO_clone(pTHX_ PerlInterpreter *proto, CLONE_PARAMS *param)
545 PerlIO **table = &proto->Iperlio;
548 PL_known_layers = PerlIO_clone_list(aTHX_ proto->Iknown_layers, param);
549 PL_def_layerlist = PerlIO_clone_list(aTHX_ proto->Idef_layerlist, param);
550 PerlIO_allocate(aTHX); /* root slot is never used */
551 PerlIO_debug("Clone %p from %p\n",aTHX,proto);
552 while ((f = *table)) {
554 table = (PerlIO **) (f++);
555 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
557 (void) fp_dup(f, 0, param);
566 PerlIO_destruct(pTHX)
568 PerlIO **table = &PL_perlio;
571 PerlIO_debug("Destruct %p\n",aTHX);
573 while ((f = *table)) {
575 table = (PerlIO **) (f++);
576 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
580 if (l->tab->kind & PERLIO_K_DESTRUCT) {
581 PerlIO_debug("Destruct popping %s\n", l->tab->name);
592 PerlIO_list_free(aTHX_ PL_known_layers);
593 PL_known_layers = NULL;
594 PerlIO_list_free(aTHX_ PL_def_layerlist);
595 PL_def_layerlist = NULL;
599 PerlIO_pop(pTHX_ PerlIO *f)
603 PerlIO_debug("PerlIO_pop f=%p %s\n", f, l->tab->name);
604 if (l->tab->Popped) {
606 * If popped returns non-zero do not free its layer structure
607 * it has either done so itself, or it is shared and still in
610 if ((*l->tab->Popped) (f) != 0)
618 /*--------------------------------------------------------------------------------------*/
620 * XS Interface for perl code
624 PerlIO_find_layer(pTHX_ const char *name, STRLEN len, int load)
627 if ((SSize_t) len <= 0)
629 for (i = 0; i < PL_known_layers->cur; i++) {
630 PerlIO_funcs *f = PL_known_layers->array[i].funcs;
631 if (memEQ(f->name, name, len)) {
632 PerlIO_debug("%.*s => %p\n", (int) len, name, f);
636 if (load && PL_subname && PL_def_layerlist
637 && PL_def_layerlist->cur >= 2) {
638 SV *pkgsv = newSVpvn("PerlIO", 6);
639 SV *layer = newSVpvn(name, len);
642 * The two SVs are magically freed by load_module
644 Perl_load_module(aTHX_ 0, pkgsv, Nullsv, layer, Nullsv);
646 return PerlIO_find_layer(aTHX_ name, len, 0);
648 PerlIO_debug("Cannot find %.*s\n", (int) len, name);
652 #ifdef USE_ATTRIBUTES_FOR_PERLIO
655 perlio_mg_set(pTHX_ SV *sv, MAGIC *mg)
658 IO *io = GvIOn((GV *) SvRV(sv));
659 PerlIO *ifp = IoIFP(io);
660 PerlIO *ofp = IoOFP(io);
661 Perl_warn(aTHX_ "set %" SVf " %p %p %p", sv, io, ifp, ofp);
667 perlio_mg_get(pTHX_ SV *sv, MAGIC *mg)
670 IO *io = GvIOn((GV *) SvRV(sv));
671 PerlIO *ifp = IoIFP(io);
672 PerlIO *ofp = IoOFP(io);
673 Perl_warn(aTHX_ "get %" SVf " %p %p %p", sv, io, ifp, ofp);
679 perlio_mg_clear(pTHX_ SV *sv, MAGIC *mg)
681 Perl_warn(aTHX_ "clear %" SVf, sv);
686 perlio_mg_free(pTHX_ SV *sv, MAGIC *mg)
688 Perl_warn(aTHX_ "free %" SVf, sv);
692 MGVTBL perlio_vtab = {
700 XS(XS_io_MODIFY_SCALAR_ATTRIBUTES)
703 SV *sv = SvRV(ST(1));
708 sv_magic(sv, (SV *) av, PERL_MAGIC_ext, NULL, 0);
710 mg = mg_find(sv, PERL_MAGIC_ext);
711 mg->mg_virtual = &perlio_vtab;
713 Perl_warn(aTHX_ "attrib %" SVf, sv);
714 for (i = 2; i < items; i++) {
716 const char *name = SvPV(ST(i), len);
717 SV *layer = PerlIO_find_layer(aTHX_ name, len, 1);
719 av_push(av, SvREFCNT_inc(layer));
730 #endif /* USE_ATTIBUTES_FOR_PERLIO */
733 PerlIO_tab_sv(pTHX_ PerlIO_funcs *tab)
735 HV *stash = gv_stashpv("PerlIO::Layer", TRUE);
736 SV *sv = sv_bless(newRV_noinc(newSViv(PTR2IV(tab))), stash);
740 XS(XS_PerlIO__Layer__find)
744 Perl_croak(aTHX_ "Usage class->find(name[,load])");
747 char *name = SvPV(ST(1), len);
748 bool load = (items > 2) ? SvTRUE(ST(2)) : 0;
749 PerlIO_funcs *layer = PerlIO_find_layer(aTHX_ name, len, load);
751 (layer) ? sv_2mortal(PerlIO_tab_sv(aTHX_ layer)) :
758 PerlIO_define_layer(pTHX_ PerlIO_funcs *tab)
760 if (!PL_known_layers)
761 PL_known_layers = PerlIO_list_alloc(aTHX);
762 PerlIO_list_push(aTHX_ PL_known_layers, tab, Nullsv);
763 PerlIO_debug("define %s %p\n", tab->name, tab);
767 PerlIO_parse_layers(pTHX_ PerlIO_list_t *av, const char *names)
770 const char *s = names;
772 while (isSPACE(*s) || *s == ':')
777 const char *as = Nullch;
779 if (!isIDFIRST(*s)) {
781 * Message is consistent with how attribute lists are
782 * passed. Even though this means "foo : : bar" is
783 * seen as an invalid separator character.
785 char q = ((*s == '\'') ? '"' : '\'');
787 "perlio: invalid separator character %c%c%c in layer specification list %s",
793 } while (isALNUM(*e));
809 * It's a nul terminated string, not allowed
810 * to \ the terminating null. Anything other
811 * character is passed over.
822 "perlio: argument list not closed for layer \"%.*s\"",
834 PerlIO_funcs *layer =
835 PerlIO_find_layer(aTHX_ s, llen, 1);
837 PerlIO_list_push(aTHX_ av, layer,
843 Perl_warn(aTHX_ "perlio: unknown layer \"%.*s\"",
856 PerlIO_default_buffer(pTHX_ PerlIO_list_t *av)
858 PerlIO_funcs *tab = &PerlIO_perlio;
859 if (O_BINARY != O_TEXT) {
863 if (PerlIO_stdio.Set_ptrcnt) {
867 PerlIO_debug("Pushing %s\n", tab->name);
868 PerlIO_list_push(aTHX_ av, PerlIO_find_layer(aTHX_ tab->name, 0, 0),
873 PerlIO_arg_fetch(PerlIO_list_t *av, IV n)
875 return av->array[n].arg;
879 PerlIO_layer_fetch(pTHX_ PerlIO_list_t *av, IV n, PerlIO_funcs *def)
881 if (n >= 0 && n < av->cur) {
882 PerlIO_debug("Layer %" IVdf " is %s\n", n,
883 av->array[n].funcs->name);
884 return av->array[n].funcs;
887 Perl_croak(aTHX_ "panic: PerlIO layer array corrupt");
892 PerlIO_default_layers(pTHX)
894 if (!PL_def_layerlist) {
895 const char *s = (PL_tainting) ? Nullch : PerlEnv_getenv("PERLIO");
896 PerlIO_funcs *osLayer = &PerlIO_unix;
897 PL_def_layerlist = PerlIO_list_alloc(aTHX);
898 PerlIO_define_layer(aTHX_ & PerlIO_unix);
899 #if defined(WIN32) && !defined(UNDER_CE)
900 PerlIO_define_layer(aTHX_ & PerlIO_win32);
902 osLayer = &PerlIO_win32;
905 PerlIO_define_layer(aTHX_ & PerlIO_raw);
906 PerlIO_define_layer(aTHX_ & PerlIO_perlio);
907 PerlIO_define_layer(aTHX_ & PerlIO_stdio);
908 PerlIO_define_layer(aTHX_ & PerlIO_crlf);
910 PerlIO_define_layer(aTHX_ & PerlIO_mmap);
912 PerlIO_define_layer(aTHX_ & PerlIO_utf8);
913 PerlIO_define_layer(aTHX_ & PerlIO_byte);
914 PerlIO_list_push(aTHX_ PL_def_layerlist,
915 PerlIO_find_layer(aTHX_ osLayer->name, 0, 0),
918 PerlIO_parse_layers(aTHX_ PL_def_layerlist, s);
921 PerlIO_default_buffer(aTHX_ PL_def_layerlist);
924 if (PL_def_layerlist->cur < 2) {
925 PerlIO_default_buffer(aTHX_ PL_def_layerlist);
927 return PL_def_layerlist;
931 Perl_boot_core_PerlIO(pTHX)
933 #ifdef USE_ATTRIBUTES_FOR_PERLIO
934 newXS("io::MODIFY_SCALAR_ATTRIBUTES", XS_io_MODIFY_SCALAR_ATTRIBUTES,
937 newXS("PerlIO::Layer::find", XS_PerlIO__Layer__find, __FILE__);
941 PerlIO_default_layer(pTHX_ I32 n)
943 PerlIO_list_t *av = PerlIO_default_layers(aTHX);
946 return PerlIO_layer_fetch(aTHX_ av, n, &PerlIO_stdio);
949 #define PerlIO_default_top() PerlIO_default_layer(aTHX_ -1)
950 #define PerlIO_default_btm() PerlIO_default_layer(aTHX_ 0)
953 PerlIO_stdstreams(pTHX)
956 PerlIO_allocate(aTHX);
957 PerlIO_fdopen(0, "Ir" PERLIO_STDTEXT);
958 PerlIO_fdopen(1, "Iw" PERLIO_STDTEXT);
959 PerlIO_fdopen(2, "Iw" PERLIO_STDTEXT);
964 PerlIO_push(pTHX_ PerlIO *f, PerlIO_funcs *tab, const char *mode, SV *arg)
967 Newc('L',l,tab->size,char,PerlIOl);
969 Zero(l, tab->size, char);
973 PerlIO_debug("PerlIO_push f=%p %s %s %p\n", f, tab->name,
974 (mode) ? mode : "(Null)", arg);
975 if ((*l->tab->Pushed) (f, mode, arg) != 0) {
984 PerlIOPop_pushed(PerlIO *f, const char *mode, SV *arg)
997 PerlIORaw_pushed(PerlIO *f, const char *mode, SV *arg)
1000 * Remove the dummy layer
1003 PerlIO_pop(aTHX_ f);
1005 * Pop back to bottom layer
1009 while (!(PerlIOBase(f)->tab->kind & PERLIO_K_RAW)) {
1010 if (*PerlIONext(f)) {
1011 PerlIO_pop(aTHX_ f);
1015 * Nothing bellow - push unix on top then remove it
1017 if (PerlIO_push(aTHX_ f, PerlIO_default_btm(), mode, arg)) {
1018 PerlIO_pop(aTHX_ PerlIONext(f));
1023 PerlIO_debug(":raw f=%p :%s\n", f, PerlIOBase(f)->tab->name);
1030 PerlIO_apply_layera(pTHX_ PerlIO *f, const char *mode,
1031 PerlIO_list_t *layers, IV n)
1033 IV max = layers->cur;
1036 PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n, NULL);
1038 if (!PerlIO_push(aTHX_ f, tab, mode, PerlIOArg)) {
1049 PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names)
1053 PerlIO_list_t *layers = PerlIO_list_alloc(aTHX);
1054 code = PerlIO_parse_layers(aTHX_ layers, names);
1056 code = PerlIO_apply_layera(aTHX_ f, mode, layers, 0);
1058 PerlIO_list_free(aTHX_ layers);
1064 /*--------------------------------------------------------------------------------------*/
1066 * Given the abstraction above the public API functions
1070 PerlIO_binmode(pTHX_ PerlIO *f, int iotype, int mode, const char *names)
1072 PerlIO_debug("PerlIO_binmode f=%p %s %c %x %s\n",
1073 f, PerlIOBase(f)->tab->name, iotype, mode,
1074 (names) ? names : "(Null)");
1075 /* Can't flush if switching encodings. */
1076 if (!(names && memEQ(names, ":encoding(", 10))) {
1078 if (!names && (O_TEXT != O_BINARY && (mode & O_BINARY))) {
1081 if (PerlIOBase(top)->tab == &PerlIO_crlf) {
1082 PerlIOBase(top)->flags &= ~PERLIO_F_CRLF;
1085 top = PerlIONext(top);
1090 return PerlIO_apply_layers(aTHX_ f, NULL, names) == 0 ? TRUE : FALSE;
1093 #undef PerlIO__close
1095 PerlIO__close(PerlIO *f)
1098 return (*PerlIOBase(f)->tab->Close) (f);
1100 SETERRNO(EBADF, SS$_IVCHAN);
1107 PerlIO_close(PerlIO *f)
1112 code = (*PerlIOBase(f)->tab->Close) (f);
1114 PerlIO_pop(aTHX_ f);
1120 #undef PerlIO_fileno
1122 PerlIO_fileno(PerlIO *f)
1125 return (*PerlIOBase(f)->tab->Fileno) (f);
1127 SETERRNO(EBADF, SS$_IVCHAN);
1133 PerlIO_context_layers(pTHX_ const char *mode)
1135 const char *type = NULL;
1137 * Need to supply default layer info from open.pm
1140 SV *layers = PL_curcop->cop_io;
1143 type = SvPV(layers, len);
1144 if (type && mode[0] != 'r') {
1146 * Skip to write part
1148 const char *s = strchr(type, 0);
1149 if (s && (s - type) < len) {
1158 static PerlIO_funcs *
1159 PerlIO_layer_from_ref(pTHX_ SV *sv)
1162 * For any scalar type load the handler which is bundled with perl
1164 if (SvTYPE(sv) < SVt_PVAV)
1165 return PerlIO_find_layer(aTHX_ "Scalar", 6, 1);
1168 * For other types allow if layer is known but don't try and load it
1170 switch (SvTYPE(sv)) {
1172 return PerlIO_find_layer(aTHX_ "Array", 5, 0);
1174 return PerlIO_find_layer(aTHX_ "Hash", 4, 0);
1176 return PerlIO_find_layer(aTHX_ "Code", 4, 0);
1178 return PerlIO_find_layer(aTHX_ "Glob", 4, 0);
1184 PerlIO_resolve_layers(pTHX_ const char *layers,
1185 const char *mode, int narg, SV **args)
1187 PerlIO_list_t *def = PerlIO_default_layers(aTHX);
1190 PerlIO_stdstreams(aTHX);
1194 * If it is a reference but not an object see if we have a handler
1197 if (SvROK(arg) && !sv_isobject(arg)) {
1198 PerlIO_funcs *handler = PerlIO_layer_from_ref(aTHX_ SvRV(arg));
1200 def = PerlIO_list_alloc(aTHX);
1201 PerlIO_list_push(aTHX_ def, handler, &PL_sv_undef);
1205 * Don't fail if handler cannot be found :Via(...) etc. may do
1206 * something sensible else we will just stringfy and open
1212 layers = PerlIO_context_layers(aTHX_ mode);
1213 if (layers && *layers) {
1217 av = PerlIO_list_alloc(aTHX);
1218 for (i = 0; i < def->cur; i++) {
1219 PerlIO_list_push(aTHX_ av, def->array[i].funcs,
1226 PerlIO_parse_layers(aTHX_ av, layers);
1237 PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd,
1238 int imode, int perm, PerlIO *f, int narg, SV **args)
1240 if (!f && narg == 1 && *args == &PL_sv_undef) {
1241 if ((f = PerlIO_tmpfile())) {
1243 layers = PerlIO_context_layers(aTHX_ mode);
1244 if (layers && *layers)
1245 PerlIO_apply_layers(aTHX_ f, mode, layers);
1249 PerlIO_list_t *layera = NULL;
1251 PerlIO_funcs *tab = NULL;
1254 * This is "reopen" - it is not tested as perl does not use it
1258 layera = PerlIO_list_alloc(aTHX);
1261 (l->tab->Getarg) ? (*l->tab->
1262 Getarg) (aTHX_ &l, NULL, 0) : &PL_sv_undef;
1263 PerlIO_list_push(aTHX_ layera, l->tab, arg);
1264 l = *PerlIONext(&l);
1268 layera = PerlIO_resolve_layers(aTHX_ layers, mode, narg, args);
1271 * Start at "top" of layer stack
1273 n = layera->cur - 1;
1275 PerlIO_funcs *t = PerlIO_layer_fetch(aTHX_ layera, n, NULL);
1284 * Found that layer 'n' can do opens - call it
1286 PerlIO_debug("openn(%s,'%s','%s',%d,%x,%o,%p,%d,%p)\n",
1287 tab->name, layers, mode, fd, imode, perm, f, narg,
1289 f = (*tab->Open) (aTHX_ tab, layera, n, mode, fd, imode, perm,
1292 if (n + 1 < layera->cur) {
1294 * More layers above the one that we used to open -
1297 if (PerlIO_apply_layera(aTHX_ f, mode, layera, n + 1)
1304 PerlIO_list_free(aTHX_ layera);
1310 #undef PerlIO_fdopen
1312 PerlIO_fdopen(int fd, const char *mode)
1315 return PerlIO_openn(aTHX_ Nullch, mode, fd, 0, 0, NULL, 0, NULL);
1320 PerlIO_open(const char *path, const char *mode)
1323 SV *name = sv_2mortal(newSVpvn(path, strlen(path)));
1324 return PerlIO_openn(aTHX_ Nullch, mode, -1, 0, 0, NULL, 1, &name);
1327 #undef PerlIO_reopen
1329 PerlIO_reopen(const char *path, const char *mode, PerlIO *f)
1332 SV *name = sv_2mortal(newSVpvn(path, strlen(path)));
1333 return PerlIO_openn(aTHX_ Nullch, mode, -1, 0, 0, f, 1, &name);
1338 PerlIO_read(PerlIO *f, void *vbuf, Size_t count)
1341 return (*PerlIOBase(f)->tab->Read) (f, vbuf, count);
1343 SETERRNO(EBADF, SS$_IVCHAN);
1348 #undef PerlIO_unread
1350 PerlIO_unread(PerlIO *f, const void *vbuf, Size_t count)
1353 return (*PerlIOBase(f)->tab->Unread) (f, vbuf, count);
1355 SETERRNO(EBADF, SS$_IVCHAN);
1362 PerlIO_write(PerlIO *f, const void *vbuf, Size_t count)
1365 return (*PerlIOBase(f)->tab->Write) (f, vbuf, count);
1367 SETERRNO(EBADF, SS$_IVCHAN);
1374 PerlIO_seek(PerlIO *f, Off_t offset, int whence)
1377 return (*PerlIOBase(f)->tab->Seek) (f, offset, whence);
1379 SETERRNO(EBADF, SS$_IVCHAN);
1386 PerlIO_tell(PerlIO *f)
1389 return (*PerlIOBase(f)->tab->Tell) (f);
1391 SETERRNO(EBADF, SS$_IVCHAN);
1398 PerlIO_flush(PerlIO *f)
1402 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1403 if (tab && tab->Flush) {
1404 return (*tab->Flush) (f);
1407 PerlIO_debug("Cannot flush f=%p :%s\n", f, tab->name);
1408 SETERRNO(EBADF, SS$_IVCHAN);
1413 PerlIO_debug("Cannot flush f=%p\n", f);
1414 SETERRNO(EBADF, SS$_IVCHAN);
1420 * Is it good API design to do flush-all on NULL, a potentially
1421 * errorneous input? Maybe some magical value (PerlIO*
1422 * PERLIO_FLUSH_ALL = (PerlIO*)-1;)? Yes, stdio does similar
1423 * things on fflush(NULL), but should we be bound by their design
1427 PerlIO **table = &PL_perlio;
1429 while ((f = *table)) {
1431 table = (PerlIO **) (f++);
1432 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
1433 if (*f && PerlIO_flush(f) != 0)
1443 PerlIOBase_flush_linebuf()
1446 PerlIO **table = &PL_perlio;
1448 while ((f = *table)) {
1450 table = (PerlIO **) (f++);
1451 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
1454 flags & (PERLIO_F_LINEBUF | PERLIO_F_CANWRITE))
1455 == (PERLIO_F_LINEBUF | PERLIO_F_CANWRITE))
1464 PerlIO_fill(PerlIO *f)
1467 return (*PerlIOBase(f)->tab->Fill) (f);
1469 SETERRNO(EBADF, SS$_IVCHAN);
1474 #undef PerlIO_isutf8
1476 PerlIO_isutf8(PerlIO *f)
1479 return (PerlIOBase(f)->flags & PERLIO_F_UTF8) != 0;
1481 SETERRNO(EBADF, SS$_IVCHAN);
1488 PerlIO_eof(PerlIO *f)
1491 return (*PerlIOBase(f)->tab->Eof) (f);
1493 SETERRNO(EBADF, SS$_IVCHAN);
1500 PerlIO_error(PerlIO *f)
1503 return (*PerlIOBase(f)->tab->Error) (f);
1505 SETERRNO(EBADF, SS$_IVCHAN);
1510 #undef PerlIO_clearerr
1512 PerlIO_clearerr(PerlIO *f)
1515 (*PerlIOBase(f)->tab->Clearerr) (f);
1517 SETERRNO(EBADF, SS$_IVCHAN);
1520 #undef PerlIO_setlinebuf
1522 PerlIO_setlinebuf(PerlIO *f)
1525 (*PerlIOBase(f)->tab->Setlinebuf) (f);
1527 SETERRNO(EBADF, SS$_IVCHAN);
1530 #undef PerlIO_has_base
1532 PerlIO_has_base(PerlIO *f)
1535 return (PerlIOBase(f)->tab->Get_base != NULL);
1540 #undef PerlIO_fast_gets
1542 PerlIO_fast_gets(PerlIO *f)
1544 if (f && *f && (PerlIOBase(f)->flags & PERLIO_F_FASTGETS)) {
1545 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1546 return (tab->Set_ptrcnt != NULL);
1551 #undef PerlIO_has_cntptr
1553 PerlIO_has_cntptr(PerlIO *f)
1556 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1557 return (tab->Get_ptr != NULL && tab->Get_cnt != NULL);
1562 #undef PerlIO_canset_cnt
1564 PerlIO_canset_cnt(PerlIO *f)
1567 PerlIOl *l = PerlIOBase(f);
1568 return (l->tab->Set_ptrcnt != NULL);
1573 #undef PerlIO_get_base
1575 PerlIO_get_base(PerlIO *f)
1578 return (*PerlIOBase(f)->tab->Get_base) (f);
1582 #undef PerlIO_get_bufsiz
1584 PerlIO_get_bufsiz(PerlIO *f)
1587 return (*PerlIOBase(f)->tab->Get_bufsiz) (f);
1591 #undef PerlIO_get_ptr
1593 PerlIO_get_ptr(PerlIO *f)
1595 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1596 if (tab->Get_ptr == NULL)
1598 return (*tab->Get_ptr) (f);
1601 #undef PerlIO_get_cnt
1603 PerlIO_get_cnt(PerlIO *f)
1605 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1606 if (tab->Get_cnt == NULL)
1608 return (*tab->Get_cnt) (f);
1611 #undef PerlIO_set_cnt
1613 PerlIO_set_cnt(PerlIO *f, int cnt)
1615 (*PerlIOBase(f)->tab->Set_ptrcnt) (f, NULL, cnt);
1618 #undef PerlIO_set_ptrcnt
1620 PerlIO_set_ptrcnt(PerlIO *f, STDCHAR * ptr, int cnt)
1622 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1623 if (tab->Set_ptrcnt == NULL) {
1625 Perl_croak(aTHX_ "PerlIO buffer snooping abuse");
1627 (*PerlIOBase(f)->tab->Set_ptrcnt) (f, ptr, cnt);
1630 /*--------------------------------------------------------------------------------------*/
1632 * utf8 and raw dummy layers
1636 PerlIOUtf8_pushed(PerlIO *f, const char *mode, SV *arg)
1638 if (PerlIONext(f)) {
1640 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1641 PerlIO_pop(aTHX_ f);
1642 if (tab->kind & PERLIO_K_UTF8)
1643 PerlIOBase(f)->flags |= PERLIO_F_UTF8;
1645 PerlIOBase(f)->flags &= ~PERLIO_F_UTF8;
1651 PerlIO_funcs PerlIO_utf8 = {
1654 PERLIO_K_DUMMY | PERLIO_F_UTF8,
1672 NULL, /* get_base */
1673 NULL, /* get_bufsiz */
1676 NULL, /* set_ptrcnt */
1679 PerlIO_funcs PerlIO_byte = {
1700 NULL, /* get_base */
1701 NULL, /* get_bufsiz */
1704 NULL, /* set_ptrcnt */
1708 PerlIORaw_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
1709 IV n, const char *mode, int fd, int imode, int perm,
1710 PerlIO *old, int narg, SV **args)
1712 PerlIO_funcs *tab = PerlIO_default_btm();
1713 return (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
1717 PerlIO_funcs PerlIO_raw = {
1738 NULL, /* get_base */
1739 NULL, /* get_bufsiz */
1742 NULL, /* set_ptrcnt */
1744 /*--------------------------------------------------------------------------------------*/
1745 /*--------------------------------------------------------------------------------------*/
1747 * "Methods" of the "base class"
1751 PerlIOBase_fileno(PerlIO *f)
1753 return PerlIO_fileno(PerlIONext(f));
1757 PerlIO_modestr(PerlIO *f, char *buf)
1760 IV flags = PerlIOBase(f)->flags;
1761 if (flags & PERLIO_F_APPEND) {
1763 if (flags & PERLIO_F_CANREAD) {
1767 else if (flags & PERLIO_F_CANREAD) {
1769 if (flags & PERLIO_F_CANWRITE)
1772 else if (flags & PERLIO_F_CANWRITE) {
1774 if (flags & PERLIO_F_CANREAD) {
1778 #if O_TEXT != O_BINARY
1779 if (!(flags & PERLIO_F_CRLF))
1787 PerlIOBase_pushed(PerlIO *f, const char *mode, SV *arg)
1789 PerlIOl *l = PerlIOBase(f);
1791 const char *omode = mode;
1794 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1795 l->flags &= ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE |
1796 PERLIO_F_TRUNCATE | PERLIO_F_APPEND);
1797 if (tab->Set_ptrcnt != NULL)
1798 l->flags |= PERLIO_F_FASTGETS;
1800 if (*mode == '#' || *mode == 'I')
1804 l->flags |= PERLIO_F_CANREAD;
1807 l->flags |= PERLIO_F_APPEND | PERLIO_F_CANWRITE;
1810 l->flags |= PERLIO_F_TRUNCATE | PERLIO_F_CANWRITE;
1813 SETERRNO(EINVAL, LIB$_INVARG);
1819 l->flags |= PERLIO_F_CANREAD | PERLIO_F_CANWRITE;
1822 l->flags &= ~PERLIO_F_CRLF;
1825 l->flags |= PERLIO_F_CRLF;
1828 SETERRNO(EINVAL, LIB$_INVARG);
1835 l->flags |= l->next->flags &
1836 (PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_TRUNCATE |
1841 PerlIO_debug("PerlIOBase_pushed f=%p %s %s fl=%08" UVxf " (%s)\n",
1842 f, PerlIOBase(f)->tab->name, (omode) ? omode : "(Null)",
1843 l->flags, PerlIO_modestr(f, temp));
1849 PerlIOBase_popped(PerlIO *f)
1855 PerlIOBase_unread(PerlIO *f, const void *vbuf, Size_t count)
1859 * Save the position as current head considers it
1861 Off_t old = PerlIO_tell(f);
1863 PerlIO_push(aTHX_ f, &PerlIO_pending, "r", Nullsv);
1864 PerlIOSelf(f, PerlIOBuf)->posn = old;
1865 done = PerlIOBuf_unread(f, vbuf, count);
1870 PerlIOBase_read(PerlIO *f, void *vbuf, Size_t count)
1872 STDCHAR *buf = (STDCHAR *) vbuf;
1874 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD))
1877 SSize_t avail = PerlIO_get_cnt(f);
1880 take = (count < avail) ? count : avail;
1882 STDCHAR *ptr = PerlIO_get_ptr(f);
1883 Copy(ptr, buf, take, STDCHAR);
1884 PerlIO_set_ptrcnt(f, ptr + take, (avail -= take));
1888 if (count > 0 && avail <= 0) {
1889 if (PerlIO_fill(f) != 0)
1893 return (buf - (STDCHAR *) vbuf);
1899 PerlIOBase_noop_ok(PerlIO *f)
1905 PerlIOBase_noop_fail(PerlIO *f)
1911 PerlIOBase_close(PerlIO *f)
1914 PerlIO *n = PerlIONext(f);
1915 if (PerlIO_flush(f) != 0)
1917 if (n && *n && (*PerlIOBase(n)->tab->Close) (n) != 0)
1919 PerlIOBase(f)->flags &=
1920 ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_OPEN);
1925 PerlIOBase_eof(PerlIO *f)
1928 return (PerlIOBase(f)->flags & PERLIO_F_EOF) != 0;
1934 PerlIOBase_error(PerlIO *f)
1937 return (PerlIOBase(f)->flags & PERLIO_F_ERROR) != 0;
1943 PerlIOBase_clearerr(PerlIO *f)
1946 PerlIO *n = PerlIONext(f);
1947 PerlIOBase(f)->flags &= ~(PERLIO_F_ERROR | PERLIO_F_EOF);
1954 PerlIOBase_setlinebuf(PerlIO *f)
1957 PerlIOBase(f)->flags |= PERLIO_F_LINEBUF;
1962 PerlIO_sv_dup(pTHX_ SV *arg, CLONE_PARAMS *param)
1968 return sv_dup(arg, param);
1971 return newSVsv(arg);
1974 return newSVsv(arg);
1979 PerlIOBase_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
1981 PerlIO *nexto = PerlIONext(o);
1983 PerlIO_funcs *tab = PerlIOBase(nexto)->tab;
1984 f = (*tab->Dup)(aTHX_ f, nexto, param, flags);
1987 PerlIO_funcs *self = PerlIOBase(o)->tab;
1990 PerlIO_debug("PerlIOBase_dup %s f=%p o=%p param=%p\n",self->name,f,o,param);
1992 arg = (*self->Getarg)(aTHX_ o,param,flags);
1994 f = PerlIO_push(aTHX_ f, self, PerlIO_modestr(o,buf), arg);
2002 #define PERLIO_MAX_REFCOUNTABLE_FD 2048
2004 perl_mutex PerlIO_mutex;
2006 int PerlIO_fd_refcnt[PERLIO_MAX_REFCOUNTABLE_FD];
2011 /* Place holder for stdstreams call ??? */
2013 MUTEX_INIT(&PerlIO_mutex);
2018 PerlIOUnix_refcnt_inc(int fd)
2020 if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
2022 MUTEX_LOCK(&PerlIO_mutex);
2024 PerlIO_fd_refcnt[fd]++;
2025 PerlIO_debug("fd %d refcnt=%d\n",fd,PerlIO_fd_refcnt[fd]);
2027 MUTEX_UNLOCK(&PerlIO_mutex);
2033 PerlIOUnix_refcnt_dec(int fd)
2036 if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
2038 MUTEX_LOCK(&PerlIO_mutex);
2040 cnt = --PerlIO_fd_refcnt[fd];
2041 PerlIO_debug("fd %d refcnt=%d\n",fd,cnt);
2043 MUTEX_UNLOCK(&PerlIO_mutex);
2050 PerlIO_cleanup(pTHX)
2054 PerlIO_debug("Cleanup %p\n",aTHX);
2056 /* Raise STDIN..STDERR refcount so we don't close them */
2057 for (i=0; i < 3; i++)
2058 PerlIOUnix_refcnt_inc(i);
2059 PerlIO_cleantable(aTHX_ &PL_perlio);
2060 /* Restore STDIN..STDERR refcount */
2061 for (i=0; i < 3; i++)
2062 PerlIOUnix_refcnt_dec(i);
2067 /*--------------------------------------------------------------------------------------*/
2069 * Bottom-most level for UNIX-like case
2073 struct _PerlIO base; /* The generic part */
2074 int fd; /* UNIX like file descriptor */
2075 int oflags; /* open/fcntl flags */
2079 PerlIOUnix_oflags(const char *mode)
2082 if (*mode == 'I' || *mode == '#')
2087 if (*++mode == '+') {
2094 oflags = O_CREAT | O_TRUNC;
2095 if (*++mode == '+') {
2104 oflags = O_CREAT | O_APPEND;
2105 if (*++mode == '+') {
2118 else if (*mode == 't') {
2120 oflags &= ~O_BINARY;
2124 * Always open in binary mode
2127 if (*mode || oflags == -1) {
2128 SETERRNO(EINVAL, LIB$_INVARG);
2135 PerlIOUnix_fileno(PerlIO *f)
2137 return PerlIOSelf(f, PerlIOUnix)->fd;
2141 PerlIOUnix_pushed(PerlIO *f, const char *mode, SV *arg)
2143 IV code = PerlIOBase_pushed(f, mode, arg);
2144 PerlIOUnix *s = PerlIOSelf(f, PerlIOUnix);
2145 if (*PerlIONext(f)) {
2146 s->fd = PerlIO_fileno(PerlIONext(f));
2148 * XXX could (or should) we retrieve the oflags from the open file
2149 * handle rather than believing the "mode" we are passed in? XXX
2150 * Should the value on NULL mode be 0 or -1?
2152 s->oflags = mode ? PerlIOUnix_oflags(mode) : -1;
2154 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
2159 PerlIOUnix_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2160 IV n, const char *mode, int fd, int imode,
2161 int perm, PerlIO *f, int narg, SV **args)
2164 if (PerlIOBase(f)->flags & PERLIO_F_OPEN)
2165 (*PerlIOBase(f)->tab->Close) (f);
2168 char *path = SvPV_nolen(*args);
2172 imode = PerlIOUnix_oflags(mode);
2176 fd = PerlLIO_open3(path, imode, perm);
2184 f = PerlIO_allocate(aTHX);
2185 s = PerlIOSelf(PerlIO_push(aTHX_ f, self, mode, PerlIOArg),
2189 s = PerlIOSelf(f, PerlIOUnix);
2192 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
2193 PerlIOUnix_refcnt_inc(fd);
2199 * FIXME: pop layers ???
2207 PerlIOUnix_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
2209 PerlIOUnix *os = PerlIOSelf(o, PerlIOUnix);
2211 if (flags & PERLIO_DUP_FD) {
2212 fd = PerlLIO_dup(fd);
2214 if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
2215 f = PerlIOBase_dup(aTHX_ f, o, param, flags);
2217 /* If all went well overwrite fd in dup'ed lay with the dup()'ed fd */
2218 PerlIOUnix *s = PerlIOSelf(f, PerlIOUnix);
2220 PerlIOUnix_refcnt_inc(fd);
2229 PerlIOUnix_read(PerlIO *f, void *vbuf, Size_t count)
2232 int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2233 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD))
2236 SSize_t len = PerlLIO_read(fd, vbuf, count);
2237 if (len >= 0 || errno != EINTR) {
2239 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2240 else if (len == 0 && count != 0)
2241 PerlIOBase(f)->flags |= PERLIO_F_EOF;
2249 PerlIOUnix_write(PerlIO *f, const void *vbuf, Size_t count)
2252 int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2254 SSize_t len = PerlLIO_write(fd, vbuf, count);
2255 if (len >= 0 || errno != EINTR) {
2257 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2265 PerlIOUnix_seek(PerlIO *f, Off_t offset, int whence)
2269 PerlLIO_lseek(PerlIOSelf(f, PerlIOUnix)->fd, offset, whence);
2270 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
2271 return (new == (Off_t) - 1) ? -1 : 0;
2275 PerlIOUnix_tell(PerlIO *f)
2278 return PerlLIO_lseek(PerlIOSelf(f, PerlIOUnix)->fd, 0, SEEK_CUR);
2283 PerlIOUnix_close(PerlIO *f)
2286 int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2288 if (PerlIOBase(f)->flags & PERLIO_F_OPEN) {
2289 if (PerlIOUnix_refcnt_dec(fd) > 0) {
2290 PerlIOBase(f)->flags &= ~PERLIO_F_OPEN;
2295 SETERRNO(EBADF,SS$_IVCHAN);
2298 while (PerlLIO_close(fd) != 0) {
2299 if (errno != EINTR) {
2306 PerlIOBase(f)->flags &= ~PERLIO_F_OPEN;
2311 PerlIO_funcs PerlIO_unix = {
2327 PerlIOBase_noop_ok, /* flush */
2328 PerlIOBase_noop_fail, /* fill */
2331 PerlIOBase_clearerr,
2332 PerlIOBase_setlinebuf,
2333 NULL, /* get_base */
2334 NULL, /* get_bufsiz */
2337 NULL, /* set_ptrcnt */
2340 /*--------------------------------------------------------------------------------------*/
2346 struct _PerlIO base;
2347 FILE *stdio; /* The stream */
2351 PerlIOStdio_fileno(PerlIO *f)
2354 return PerlSIO_fileno(PerlIOSelf(f, PerlIOStdio)->stdio);
2358 PerlIOStdio_mode(const char *mode, char *tmode)
2364 if (O_BINARY != O_TEXT) {
2372 * This isn't used yet ...
2375 PerlIOStdio_pushed(PerlIO *f, const char *mode, SV *arg)
2377 if (*PerlIONext(f)) {
2379 PerlIOStdio *s = PerlIOSelf(f, PerlIOStdio);
2382 PerlSIO_fdopen(PerlIO_fileno(PerlIONext(f)), mode =
2383 PerlIOStdio_mode(mode, tmode));
2389 return PerlIOBase_pushed(f, mode, arg);
2392 #undef PerlIO_importFILE
2394 PerlIO_importFILE(FILE *stdio, int fl)
2400 PerlIOSelf(PerlIO_push
2401 (aTHX_(f = PerlIO_allocate(aTHX)), &PerlIO_stdio,
2402 "r+", Nullsv), PerlIOStdio);
2409 PerlIOStdio_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2410 IV n, const char *mode, int fd, int imode,
2411 int perm, PerlIO *f, int narg, SV **args)
2415 char *path = SvPV_nolen(*args);
2416 PerlIOStdio *s = PerlIOSelf(f, PerlIOStdio);
2418 PerlIOUnix_refcnt_dec(fileno(s->stdio));
2419 stdio = PerlSIO_freopen(path, (mode = PerlIOStdio_mode(mode, tmode)),
2424 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2429 char *path = SvPV_nolen(*args);
2432 fd = PerlLIO_open3(path, imode, perm);
2435 FILE *stdio = PerlSIO_fopen(path, mode);
2438 PerlIOSelf(PerlIO_push
2439 (aTHX_(f = PerlIO_allocate(aTHX)), self,
2440 (mode = PerlIOStdio_mode(mode, tmode)),
2444 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2459 stdio = PerlSIO_stdin;
2462 stdio = PerlSIO_stdout;
2465 stdio = PerlSIO_stderr;
2470 stdio = PerlSIO_fdopen(fd, mode =
2471 PerlIOStdio_mode(mode, tmode));
2475 PerlIOSelf(PerlIO_push
2476 (aTHX_(f = PerlIO_allocate(aTHX)), self,
2477 mode, PerlIOArg), PerlIOStdio);
2479 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2488 PerlIOStdio_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
2490 /* This assumes no layers underneath - which is what
2491 happens, but is not how I remember it. NI-S 2001/10/16
2493 if ((f = PerlIOBase_dup(aTHX_ f, o, param, flags))) {
2494 FILE *stdio = PerlIOSelf(o, PerlIOStdio)->stdio;
2495 if (flags & PERLIO_DUP_FD) {
2496 int fd = PerlLIO_dup(fileno(stdio));
2499 stdio = fdopen(fd, PerlIO_modestr(o,mode));
2502 /* FIXME: To avoid messy error recovery if dup fails
2503 re-use the existing stdio as though flag was not set
2507 PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
2508 PerlIOUnix_refcnt_inc(fileno(stdio));
2514 PerlIOStdio_close(PerlIO *f)
2517 #ifdef SOCKS5_VERSION_NAME
2519 Sock_size_t optlen = sizeof(int);
2521 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2522 if (PerlIOUnix_refcnt_dec(fileno(stdio)) > 0) {
2523 /* Do not close it but do flush any buffers */
2528 #ifdef SOCKS5_VERSION_NAME
2530 (PerlIO_fileno(f), SOL_SOCKET, SO_TYPE, (void *) &optval,
2532 0) ? PerlSIO_fclose(stdio) : close(PerlIO_fileno(f))
2534 PerlSIO_fclose(stdio)
2543 PerlIOStdio_read(PerlIO *f, void *vbuf, Size_t count)
2546 FILE *s = PerlIOSelf(f, PerlIOStdio)->stdio;
2549 STDCHAR *buf = (STDCHAR *) vbuf;
2551 * Perl is expecting PerlIO_getc() to fill the buffer Linux's
2552 * stdio does not do that for fread()
2554 int ch = PerlSIO_fgetc(s);
2561 got = PerlSIO_fread(vbuf, 1, count, s);
2566 PerlIOStdio_unread(PerlIO *f, const void *vbuf, Size_t count)
2569 FILE *s = PerlIOSelf(f, PerlIOStdio)->stdio;
2570 STDCHAR *buf = ((STDCHAR *) vbuf) + count - 1;
2573 int ch = *buf-- & 0xff;
2574 if (PerlSIO_ungetc(ch, s) != ch)
2583 PerlIOStdio_write(PerlIO *f, const void *vbuf, Size_t count)
2586 return PerlSIO_fwrite(vbuf, 1, count,
2587 PerlIOSelf(f, PerlIOStdio)->stdio);
2591 PerlIOStdio_seek(PerlIO *f, Off_t offset, int whence)
2594 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2595 return PerlSIO_fseek(stdio, offset, whence);
2599 PerlIOStdio_tell(PerlIO *f)
2602 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2603 return PerlSIO_ftell(stdio);
2607 PerlIOStdio_flush(PerlIO *f)
2610 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2611 if (PerlIOBase(f)->flags & PERLIO_F_CANWRITE) {
2612 return PerlSIO_fflush(stdio);
2617 * FIXME: This discards ungetc() and pre-read stuff which is not
2618 * right if this is just a "sync" from a layer above Suspect right
2619 * design is to do _this_ but not have layer above flush this
2620 * layer read-to-read
2623 * Not writeable - sync by attempting a seek
2626 if (PerlSIO_fseek(stdio, (Off_t) 0, SEEK_CUR) != 0)
2634 PerlIOStdio_fill(PerlIO *f)
2637 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2640 * fflush()ing read-only streams can cause trouble on some stdio-s
2642 if ((PerlIOBase(f)->flags & PERLIO_F_CANWRITE)) {
2643 if (PerlSIO_fflush(stdio) != 0)
2646 c = PerlSIO_fgetc(stdio);
2647 if (c == EOF || PerlSIO_ungetc(c, stdio) != c)
2653 PerlIOStdio_eof(PerlIO *f)
2656 return PerlSIO_feof(PerlIOSelf(f, PerlIOStdio)->stdio);
2660 PerlIOStdio_error(PerlIO *f)
2663 return PerlSIO_ferror(PerlIOSelf(f, PerlIOStdio)->stdio);
2667 PerlIOStdio_clearerr(PerlIO *f)
2670 PerlSIO_clearerr(PerlIOSelf(f, PerlIOStdio)->stdio);
2674 PerlIOStdio_setlinebuf(PerlIO *f)
2677 #ifdef HAS_SETLINEBUF
2678 PerlSIO_setlinebuf(PerlIOSelf(f, PerlIOStdio)->stdio);
2680 PerlSIO_setvbuf(PerlIOSelf(f, PerlIOStdio)->stdio, Nullch, _IOLBF, 0);
2686 PerlIOStdio_get_base(PerlIO *f)
2689 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2690 return PerlSIO_get_base(stdio);
2694 PerlIOStdio_get_bufsiz(PerlIO *f)
2697 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2698 return PerlSIO_get_bufsiz(stdio);
2702 #ifdef USE_STDIO_PTR
2704 PerlIOStdio_get_ptr(PerlIO *f)
2707 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2708 return PerlSIO_get_ptr(stdio);
2712 PerlIOStdio_get_cnt(PerlIO *f)
2715 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2716 return PerlSIO_get_cnt(stdio);
2720 PerlIOStdio_set_ptrcnt(PerlIO *f, STDCHAR * ptr, SSize_t cnt)
2722 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2725 #ifdef STDIO_PTR_LVALUE
2726 PerlSIO_set_ptr(stdio, ptr);
2727 #ifdef STDIO_PTR_LVAL_SETS_CNT
2728 if (PerlSIO_get_cnt(stdio) != (cnt)) {
2730 assert(PerlSIO_get_cnt(stdio) == (cnt));
2733 #if (!defined(STDIO_PTR_LVAL_NOCHANGE_CNT))
2735 * Setting ptr _does_ change cnt - we are done
2739 #else /* STDIO_PTR_LVALUE */
2741 #endif /* STDIO_PTR_LVALUE */
2744 * Now (or only) set cnt
2746 #ifdef STDIO_CNT_LVALUE
2747 PerlSIO_set_cnt(stdio, cnt);
2748 #else /* STDIO_CNT_LVALUE */
2749 #if (defined(STDIO_PTR_LVALUE) && defined(STDIO_PTR_LVAL_SETS_CNT))
2750 PerlSIO_set_ptr(stdio,
2751 PerlSIO_get_ptr(stdio) + (PerlSIO_get_cnt(stdio) -
2753 #else /* STDIO_PTR_LVAL_SETS_CNT */
2755 #endif /* STDIO_PTR_LVAL_SETS_CNT */
2756 #endif /* STDIO_CNT_LVALUE */
2761 PerlIO_funcs PerlIO_stdio = {
2763 sizeof(PerlIOStdio),
2781 PerlIOStdio_clearerr,
2782 PerlIOStdio_setlinebuf,
2784 PerlIOStdio_get_base,
2785 PerlIOStdio_get_bufsiz,
2790 #ifdef USE_STDIO_PTR
2791 PerlIOStdio_get_ptr,
2792 PerlIOStdio_get_cnt,
2793 #if (defined(STDIO_PTR_LVALUE) && (defined(STDIO_CNT_LVALUE) || defined(STDIO_PTR_LVAL_SETS_CNT)))
2794 PerlIOStdio_set_ptrcnt
2795 #else /* STDIO_PTR_LVALUE */
2797 #endif /* STDIO_PTR_LVALUE */
2798 #else /* USE_STDIO_PTR */
2802 #endif /* USE_STDIO_PTR */
2805 #undef PerlIO_exportFILE
2807 PerlIO_exportFILE(PerlIO *f, int fl)
2811 stdio = fdopen(PerlIO_fileno(f), "r+");
2815 PerlIOSelf(PerlIO_push(aTHX_ f, &PerlIO_stdio, "r+", Nullsv),
2822 #undef PerlIO_findFILE
2824 PerlIO_findFILE(PerlIO *f)
2828 if (l->tab == &PerlIO_stdio) {
2829 PerlIOStdio *s = PerlIOSelf(&l, PerlIOStdio);
2832 l = *PerlIONext(&l);
2834 return PerlIO_exportFILE(f, 0);
2837 #undef PerlIO_releaseFILE
2839 PerlIO_releaseFILE(PerlIO *p, FILE *f)
2843 /*--------------------------------------------------------------------------------------*/
2845 * perlio buffer layer
2849 PerlIOBuf_pushed(PerlIO *f, const char *mode, SV *arg)
2852 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
2853 int fd = PerlIO_fileno(f);
2855 if (fd >= 0 && PerlLIO_isatty(fd)) {
2856 PerlIOBase(f)->flags |= PERLIO_F_LINEBUF | PERLIO_F_TTY;
2858 posn = PerlIO_tell(PerlIONext(f));
2859 if (posn != (Off_t) - 1) {
2862 return PerlIOBase_pushed(f, mode, arg);
2866 PerlIOBuf_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2867 IV n, const char *mode, int fd, int imode, int perm,
2868 PerlIO *f, int narg, SV **args)
2871 PerlIO *next = PerlIONext(f);
2873 PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIOBase(next)->tab);
2875 (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
2878 || (*PerlIOBase(f)->tab->Pushed) (f, mode, PerlIOArg) != 0) {
2884 PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIO_default_btm());
2892 f = (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
2895 if (PerlIO_push(aTHX_ f, self, mode, PerlIOArg) == 0) {
2897 * if push fails during open, open fails. close will pop us.
2902 fd = PerlIO_fileno(f);
2903 #if (O_BINARY != O_TEXT) && !defined(__BEOS__)
2905 * do something about failing setmode()? --jhi
2907 PerlLIO_setmode(fd, O_BINARY);
2909 if (init && fd == 2) {
2911 * Initial stderr is unbuffered
2913 PerlIOBase(f)->flags |= PERLIO_F_UNBUF;
2922 * This "flush" is akin to sfio's sync in that it handles files in either
2923 * read or write state
2926 PerlIOBuf_flush(PerlIO *f)
2928 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
2930 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF) {
2932 * write() the buffer
2934 STDCHAR *buf = b->buf;
2936 PerlIO *n = PerlIONext(f);
2937 while (p < b->ptr) {
2938 SSize_t count = PerlIO_write(n, p, b->ptr - p);
2942 else if (count < 0 || PerlIO_error(n)) {
2943 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2948 b->posn += (p - buf);
2950 else if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
2951 STDCHAR *buf = PerlIO_get_base(f);
2953 * Note position change
2955 b->posn += (b->ptr - buf);
2956 if (b->ptr < b->end) {
2958 * We did not consume all of it
2960 if (PerlIO_seek(PerlIONext(f), b->posn, SEEK_SET) == 0) {
2961 b->posn = PerlIO_tell(PerlIONext(f));
2965 b->ptr = b->end = b->buf;
2966 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
2968 * FIXME: Is this right for read case ?
2970 if (PerlIO_flush(PerlIONext(f)) != 0)
2976 PerlIOBuf_fill(PerlIO *f)
2978 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
2979 PerlIO *n = PerlIONext(f);
2982 * FIXME: doing the down-stream flush is a bad idea if it causes
2983 * pre-read data in stdio buffer to be discarded but this is too
2984 * simplistic - as it skips _our_ hosekeeping and breaks tell tests.
2985 * if (!(PerlIOBase(f)->flags & PERLIO_F_RDBUF)) { }
2987 if (PerlIO_flush(f) != 0)
2989 if (PerlIOBase(f)->flags & PERLIO_F_TTY)
2990 PerlIOBase_flush_linebuf();
2993 PerlIO_get_base(f); /* allocate via vtable */
2995 b->ptr = b->end = b->buf;
2996 if (PerlIO_fast_gets(n)) {
2998 * Layer below is also buffered We do _NOT_ want to call its
2999 * ->Read() because that will loop till it gets what we asked for
3000 * which may hang on a pipe etc. Instead take anything it has to
3001 * hand, or ask it to fill _once_.
3003 avail = PerlIO_get_cnt(n);
3005 avail = PerlIO_fill(n);
3007 avail = PerlIO_get_cnt(n);
3009 if (!PerlIO_error(n) && PerlIO_eof(n))
3014 STDCHAR *ptr = PerlIO_get_ptr(n);
3015 SSize_t cnt = avail;
3016 if (avail > b->bufsiz)
3018 Copy(ptr, b->buf, avail, STDCHAR);
3019 PerlIO_set_ptrcnt(n, ptr + avail, cnt - avail);
3023 avail = PerlIO_read(n, b->ptr, b->bufsiz);
3027 PerlIOBase(f)->flags |= PERLIO_F_EOF;
3029 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
3032 b->end = b->buf + avail;
3033 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3038 PerlIOBuf_read(PerlIO *f, void *vbuf, Size_t count)
3040 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3044 return PerlIOBase_read(f, vbuf, count);
3050 PerlIOBuf_unread(PerlIO *f, const void *vbuf, Size_t count)
3052 const STDCHAR *buf = (const STDCHAR *) vbuf + count;
3053 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3056 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
3061 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
3063 * Buffer is already a read buffer, we can overwrite any chars
3064 * which have been read back to buffer start
3066 avail = (b->ptr - b->buf);
3070 * Buffer is idle, set it up so whole buffer is available for
3074 b->end = b->buf + avail;
3076 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3078 * Buffer extends _back_ from where we are now
3080 b->posn -= b->bufsiz;
3082 if (avail > (SSize_t) count) {
3084 * If we have space for more than count, just move count
3092 * In simple stdio-like ungetc() case chars will be already
3095 if (buf != b->ptr) {
3096 Copy(buf, b->ptr, avail, STDCHAR);
3100 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
3107 PerlIOBuf_write(PerlIO *f, const void *vbuf, Size_t count)
3109 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3110 const STDCHAR *buf = (const STDCHAR *) vbuf;
3114 if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
3117 SSize_t avail = b->bufsiz - (b->ptr - b->buf);
3118 if ((SSize_t) count < avail)
3120 PerlIOBase(f)->flags |= PERLIO_F_WRBUF;
3121 if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF) {
3136 Copy(buf, b->ptr, avail, STDCHAR);
3143 if (b->ptr >= (b->buf + b->bufsiz))
3146 if (PerlIOBase(f)->flags & PERLIO_F_UNBUF)
3152 PerlIOBuf_seek(PerlIO *f, Off_t offset, int whence)
3155 if ((code = PerlIO_flush(f)) == 0) {
3156 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3157 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
3158 code = PerlIO_seek(PerlIONext(f), offset, whence);
3160 b->posn = PerlIO_tell(PerlIONext(f));
3167 PerlIOBuf_tell(PerlIO *f)
3169 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3171 * b->posn is file position where b->buf was read, or will be written
3173 Off_t posn = b->posn;
3176 * If buffer is valid adjust position by amount in buffer
3178 posn += (b->ptr - b->buf);
3184 PerlIOBuf_close(PerlIO *f)
3186 IV code = PerlIOBase_close(f);
3187 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3188 if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
3192 b->ptr = b->end = b->buf;
3193 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
3198 PerlIOBuf_get_ptr(PerlIO *f)
3200 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3207 PerlIOBuf_get_cnt(PerlIO *f)
3209 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3212 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF)
3213 return (b->end - b->ptr);
3218 PerlIOBuf_get_base(PerlIO *f)
3220 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3225 Newz('B',b->buf,b->bufsiz, STDCHAR);
3227 b->buf = (STDCHAR *) & b->oneword;
3228 b->bufsiz = sizeof(b->oneword);
3237 PerlIOBuf_bufsiz(PerlIO *f)
3239 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3242 return (b->end - b->buf);
3246 PerlIOBuf_set_ptrcnt(PerlIO *f, STDCHAR * ptr, SSize_t cnt)
3248 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3252 if (PerlIO_get_cnt(f) != cnt || b->ptr < b->buf) {
3254 assert(PerlIO_get_cnt(f) == cnt);
3255 assert(b->ptr >= b->buf);
3257 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3261 PerlIOBuf_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
3263 return PerlIOBase_dup(aTHX_ f, o, param, flags);
3268 PerlIO_funcs PerlIO_perlio = {
3288 PerlIOBase_clearerr,
3289 PerlIOBase_setlinebuf,
3294 PerlIOBuf_set_ptrcnt,
3297 /*--------------------------------------------------------------------------------------*/
3299 * Temp layer to hold unread chars when cannot do it any other way
3303 PerlIOPending_fill(PerlIO *f)
3306 * Should never happen
3313 PerlIOPending_close(PerlIO *f)
3316 * A tad tricky - flush pops us, then we close new top
3319 return PerlIO_close(f);
3323 PerlIOPending_seek(PerlIO *f, Off_t offset, int whence)
3326 * A tad tricky - flush pops us, then we seek new top
3329 return PerlIO_seek(f, offset, whence);
3334 PerlIOPending_flush(PerlIO *f)
3337 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3338 if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
3342 PerlIO_pop(aTHX_ f);
3347 PerlIOPending_set_ptrcnt(PerlIO *f, STDCHAR * ptr, SSize_t cnt)
3353 PerlIOBuf_set_ptrcnt(f, ptr, cnt);
3358 PerlIOPending_pushed(PerlIO *f, const char *mode, SV *arg)
3360 IV code = PerlIOBase_pushed(f, mode, arg);
3361 PerlIOl *l = PerlIOBase(f);
3363 * Our PerlIO_fast_gets must match what we are pushed on, or sv_gets()
3364 * etc. get muddled when it changes mid-string when we auto-pop.
3366 l->flags = (l->flags & ~(PERLIO_F_FASTGETS | PERLIO_F_UTF8)) |
3367 (PerlIOBase(PerlIONext(f))->
3368 flags & (PERLIO_F_FASTGETS | PERLIO_F_UTF8));
3373 PerlIOPending_read(PerlIO *f, void *vbuf, Size_t count)
3375 SSize_t avail = PerlIO_get_cnt(f);
3380 got = PerlIOBuf_read(f, vbuf, avail);
3381 if (got >= 0 && got < count) {
3383 PerlIO_read(f, ((STDCHAR *) vbuf) + got, count - got);
3384 if (more >= 0 || got == 0)
3390 PerlIO_funcs PerlIO_pending = {
3394 PerlIOPending_pushed,
3405 PerlIOPending_close,
3406 PerlIOPending_flush,
3410 PerlIOBase_clearerr,
3411 PerlIOBase_setlinebuf,
3416 PerlIOPending_set_ptrcnt,
3421 /*--------------------------------------------------------------------------------------*/
3423 * crlf - translation On read translate CR,LF to "\n" we do this by
3424 * overriding ptr/cnt entries to hand back a line at a time and keeping a
3425 * record of which nl we "lied" about. On write translate "\n" to CR,LF
3429 PerlIOBuf base; /* PerlIOBuf stuff */
3430 STDCHAR *nl; /* Position of crlf we "lied" about in the
3435 PerlIOCrlf_pushed(PerlIO *f, const char *mode, SV *arg)
3438 PerlIOBase(f)->flags |= PERLIO_F_CRLF;
3439 code = PerlIOBuf_pushed(f, mode, arg);
3441 PerlIO_debug("PerlIOCrlf_pushed f=%p %s %s fl=%08" UVxf "\n",
3442 f, PerlIOBase(f)->tab->name, (mode) ? mode : "(Null)",
3443 PerlIOBase(f)->flags);
3450 PerlIOCrlf_unread(PerlIO *f, const void *vbuf, Size_t count)
3452 PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
3457 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF))
3458 return PerlIOBuf_unread(f, vbuf, count);
3460 const STDCHAR *buf = (const STDCHAR *) vbuf + count;
3461 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3463 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
3468 if (!(PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
3469 b->end = b->ptr = b->buf + b->bufsiz;
3470 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3471 b->posn -= b->bufsiz;
3473 while (count > 0 && b->ptr > b->buf) {
3476 if (b->ptr - 2 >= b->buf) {
3499 PerlIOCrlf_get_cnt(PerlIO *f)
3501 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3504 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
3505 PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
3506 if ((PerlIOBase(f)->flags & PERLIO_F_CRLF) && !c->nl) {
3507 STDCHAR *nl = b->ptr;
3509 while (nl < b->end && *nl != 0xd)
3511 if (nl < b->end && *nl == 0xd) {
3513 if (nl + 1 < b->end) {
3520 * Not CR,LF but just CR
3528 * Blast - found CR as last char in buffer
3532 * They may not care, defer work as long as
3535 return (nl - b->ptr);
3539 b->ptr++; /* say we have read it as far as
3540 * flush() is concerned */
3541 b->buf++; /* Leave space in front of buffer */
3542 b->bufsiz--; /* Buffer is thus smaller */
3543 code = PerlIO_fill(f); /* Fetch some more */
3544 b->bufsiz++; /* Restore size for next time */
3545 b->buf--; /* Point at space */
3546 b->ptr = nl = b->buf; /* Which is what we hand
3548 b->posn--; /* Buffer starts here */
3549 *nl = 0xd; /* Fill in the CR */
3551 goto test; /* fill() call worked */
3553 * CR at EOF - just fall through
3559 return (((c->nl) ? (c->nl + 1) : b->end) - b->ptr);
3565 PerlIOCrlf_set_ptrcnt(PerlIO *f, STDCHAR * ptr, SSize_t cnt)
3567 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3568 PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
3569 IV flags = PerlIOBase(f)->flags;
3577 if ((flags & PERLIO_F_CRLF) && ptr > b->buf && ptr[-1] == 0xd)
3584 * Test code - delete when it works ...
3591 if ((flags & PERLIO_F_CRLF) && chk > b->buf && chk[-1] == 0xd)
3598 Perl_croak(aTHX_ "ptr wrong %p != %p fl=%08" UVxf
3599 " nl=%p e=%p for %d", ptr, chk, flags, c->nl,
3606 * They have taken what we lied about
3614 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3618 PerlIOCrlf_write(PerlIO *f, const void *vbuf, Size_t count)
3620 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF))
3621 return PerlIOBuf_write(f, vbuf, count);
3623 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3624 const STDCHAR *buf = (const STDCHAR *) vbuf;
3625 const STDCHAR *ebuf = buf + count;
3628 if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
3630 while (buf < ebuf) {
3631 STDCHAR *eptr = b->buf + b->bufsiz;
3632 PerlIOBase(f)->flags |= PERLIO_F_WRBUF;
3633 while (buf < ebuf && b->ptr < eptr) {
3635 if ((b->ptr + 2) > eptr) {
3643 *(b->ptr)++ = 0xd; /* CR */
3644 *(b->ptr)++ = 0xa; /* LF */
3646 if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF) {
3656 if (b->ptr >= eptr) {
3662 if (PerlIOBase(f)->flags & PERLIO_F_UNBUF)
3664 return (buf - (STDCHAR *) vbuf);
3669 PerlIOCrlf_flush(PerlIO *f)
3671 PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
3676 return PerlIOBuf_flush(f);
3679 PerlIO_funcs PerlIO_crlf = {
3682 PERLIO_K_BUFFERED | PERLIO_K_CANCRLF,
3684 PerlIOBase_noop_ok, /* popped */
3689 PerlIOBuf_read, /* generic read works with ptr/cnt lies
3691 PerlIOCrlf_unread, /* Put CR,LF in buffer for each '\n' */
3692 PerlIOCrlf_write, /* Put CR,LF in buffer for each '\n' */
3700 PerlIOBase_clearerr,
3701 PerlIOBase_setlinebuf,
3706 PerlIOCrlf_set_ptrcnt,
3710 /*--------------------------------------------------------------------------------------*/
3712 * mmap as "buffer" layer
3716 PerlIOBuf base; /* PerlIOBuf stuff */
3717 Mmap_t mptr; /* Mapped address */
3718 Size_t len; /* mapped length */
3719 STDCHAR *bbuf; /* malloced buffer if map fails */
3722 static size_t page_size = 0;
3725 PerlIOMmap_map(PerlIO *f)
3728 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3729 IV flags = PerlIOBase(f)->flags;
3733 if (flags & PERLIO_F_CANREAD) {
3734 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3735 int fd = PerlIO_fileno(f);
3737 code = Fstat(fd, &st);
3738 if (code == 0 && S_ISREG(st.st_mode)) {
3739 SSize_t len = st.st_size - b->posn;
3743 #if defined(HAS_SYSCONF) && (defined(_SC_PAGESIZE) || defined(_SC_PAGE_SIZE))
3745 SETERRNO(0, SS$_NORMAL);
3746 # ifdef _SC_PAGESIZE
3747 page_size = sysconf(_SC_PAGESIZE);
3749 page_size = sysconf(_SC_PAGE_SIZE);
3751 if ((long) page_size < 0) {
3756 (void) SvUPGRADE(error, SVt_PV);
3757 msg = SvPVx(error, n_a);
3758 Perl_croak(aTHX_ "panic: sysconf: %s",
3763 "panic: sysconf: pagesize unknown");
3767 # ifdef HAS_GETPAGESIZE
3768 page_size = getpagesize();
3770 # if defined(I_SYS_PARAM) && defined(PAGESIZE)
3771 page_size = PAGESIZE; /* compiletime, bad */
3775 if ((IV) page_size <= 0)
3776 Perl_croak(aTHX_ "panic: bad pagesize %" IVdf,
3781 * This is a hack - should never happen - open should
3784 b->posn = PerlIO_tell(PerlIONext(f));
3786 posn = (b->posn / page_size) * page_size;
3787 len = st.st_size - posn;
3788 m->mptr = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, posn);
3789 if (m->mptr && m->mptr != (Mmap_t) - 1) {
3790 #if 0 && defined(HAS_MADVISE) && defined(MADV_SEQUENTIAL)
3791 madvise(m->mptr, len, MADV_SEQUENTIAL);
3793 #if 0 && defined(HAS_MADVISE) && defined(MADV_WILLNEED)
3794 madvise(m->mptr, len, MADV_WILLNEED);
3796 PerlIOBase(f)->flags =
3797 (flags & ~PERLIO_F_EOF) | PERLIO_F_RDBUF;
3798 b->end = ((STDCHAR *) m->mptr) + len;
3799 b->buf = ((STDCHAR *) m->mptr) + (b->posn - posn);
3808 PerlIOBase(f)->flags =
3809 flags | PERLIO_F_EOF | PERLIO_F_RDBUF;
3811 b->ptr = b->end = b->ptr;
3820 PerlIOMmap_unmap(PerlIO *f)
3822 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3823 PerlIOBuf *b = &m->base;
3827 code = munmap(m->mptr, m->len);
3831 if (PerlIO_seek(PerlIONext(f), b->posn, SEEK_SET) != 0)
3834 b->ptr = b->end = b->buf;
3835 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
3841 PerlIOMmap_get_base(PerlIO *f)
3843 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3844 PerlIOBuf *b = &m->base;
3845 if (b->buf && (PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
3847 * Already have a readbuffer in progress
3853 * We have a write buffer or flushed PerlIOBuf read buffer
3855 m->bbuf = b->buf; /* save it in case we need it again */
3856 b->buf = NULL; /* Clear to trigger below */
3859 PerlIOMmap_map(f); /* Try and map it */
3862 * Map did not work - recover PerlIOBuf buffer if we have one
3867 b->ptr = b->end = b->buf;
3870 return PerlIOBuf_get_base(f);
3874 PerlIOMmap_unread(PerlIO *f, const void *vbuf, Size_t count)
3876 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3877 PerlIOBuf *b = &m->base;
3878 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
3880 if (b->ptr && (b->ptr - count) >= b->buf
3881 && memEQ(b->ptr - count, vbuf, count)) {
3883 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
3888 * Loose the unwritable mapped buffer
3892 * If flush took the "buffer" see if we have one from before
3894 if (!b->buf && m->bbuf)
3897 PerlIOBuf_get_base(f);
3901 return PerlIOBuf_unread(f, vbuf, count);
3905 PerlIOMmap_write(PerlIO *f, const void *vbuf, Size_t count)
3907 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3908 PerlIOBuf *b = &m->base;
3909 if (!b->buf || !(PerlIOBase(f)->flags & PERLIO_F_WRBUF)) {
3911 * No, or wrong sort of, buffer
3914 if (PerlIOMmap_unmap(f) != 0)
3918 * If unmap took the "buffer" see if we have one from before
3920 if (!b->buf && m->bbuf)
3923 PerlIOBuf_get_base(f);
3927 return PerlIOBuf_write(f, vbuf, count);
3931 PerlIOMmap_flush(PerlIO *f)
3933 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3934 PerlIOBuf *b = &m->base;
3935 IV code = PerlIOBuf_flush(f);
3937 * Now we are "synced" at PerlIOBuf level
3944 if (PerlIOMmap_unmap(f) != 0)
3949 * We seem to have a PerlIOBuf buffer which was not mapped
3950 * remember it in case we need one later
3959 PerlIOMmap_fill(PerlIO *f)
3961 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3962 IV code = PerlIO_flush(f);
3963 if (code == 0 && !b->buf) {
3964 code = PerlIOMmap_map(f);
3966 if (code == 0 && !(PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
3967 code = PerlIOBuf_fill(f);
3973 PerlIOMmap_close(PerlIO *f)
3975 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3976 PerlIOBuf *b = &m->base;
3977 IV code = PerlIO_flush(f);
3981 b->ptr = b->end = b->buf;
3983 if (PerlIOBuf_close(f) != 0)
3989 PerlIOMmap_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
3991 return PerlIOBase_dup(aTHX_ f, o, param, flags);
3995 PerlIO_funcs PerlIO_mmap = {
4015 PerlIOBase_clearerr,
4016 PerlIOBase_setlinebuf,
4017 PerlIOMmap_get_base,
4021 PerlIOBuf_set_ptrcnt,
4024 #endif /* HAS_MMAP */
4032 PerlIO_stdstreams(aTHX);
4034 return &PL_perlio[1];
4037 #undef PerlIO_stdout
4043 PerlIO_stdstreams(aTHX);
4045 return &PL_perlio[2];
4048 #undef PerlIO_stderr
4054 PerlIO_stdstreams(aTHX);
4056 return &PL_perlio[3];
4059 /*--------------------------------------------------------------------------------------*/
4061 #undef PerlIO_getname
4063 PerlIO_getname(PerlIO *f, char *buf)
4068 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
4070 name = fgetname(stdio, buf);
4072 Perl_croak(aTHX_ "Don't know how to get file name");
4078 /*--------------------------------------------------------------------------------------*/
4080 * Functions which can be called on any kind of PerlIO implemented in
4086 PerlIO_getc(PerlIO *f)
4089 SSize_t count = PerlIO_read(f, buf, 1);
4091 return (unsigned char) buf[0];
4096 #undef PerlIO_ungetc
4098 PerlIO_ungetc(PerlIO *f, int ch)
4102 if (PerlIO_unread(f, &buf, 1) == 1)
4110 PerlIO_putc(PerlIO *f, int ch)
4113 return PerlIO_write(f, &buf, 1);
4118 PerlIO_puts(PerlIO *f, const char *s)
4120 STRLEN len = strlen(s);
4121 return PerlIO_write(f, s, len);
4124 #undef PerlIO_rewind
4126 PerlIO_rewind(PerlIO *f)
4128 PerlIO_seek(f, (Off_t) 0, SEEK_SET);
4132 #undef PerlIO_vprintf
4134 PerlIO_vprintf(PerlIO *f, const char *fmt, va_list ap)
4137 SV *sv = newSVpvn("", 0);
4143 Perl_va_copy(ap, apc);
4144 sv_vcatpvf(sv, fmt, &apc);
4146 sv_vcatpvf(sv, fmt, &ap);
4149 wrote = PerlIO_write(f, s, len);
4154 #undef PerlIO_printf
4156 PerlIO_printf(PerlIO *f, const char *fmt, ...)
4161 result = PerlIO_vprintf(f, fmt, ap);
4166 #undef PerlIO_stdoutf
4168 PerlIO_stdoutf(const char *fmt, ...)
4173 result = PerlIO_vprintf(PerlIO_stdout(), fmt, ap);
4178 #undef PerlIO_tmpfile
4180 PerlIO_tmpfile(void)
4183 * I have no idea how portable mkstemp() is ...
4185 #if defined(WIN32) || !defined(HAVE_MKSTEMP)
4188 FILE *stdio = PerlSIO_tmpfile();
4191 PerlIOSelf(PerlIO_push
4192 (aTHX_(f = PerlIO_allocate(aTHX)), &PerlIO_stdio,
4193 "w+", Nullsv), PerlIOStdio);
4199 SV *sv = newSVpv("/tmp/PerlIO_XXXXXX", 0);
4200 int fd = mkstemp(SvPVX(sv));
4203 f = PerlIO_fdopen(fd, "w+");
4205 PerlIOBase(f)->flags |= PERLIO_F_TEMP;
4207 PerlLIO_unlink(SvPVX(sv));
4217 #endif /* USE_SFIO */
4218 #endif /* PERLIO_IS_STDIO */
4220 /*======================================================================================*/
4222 * Now some functions in terms of above which may be needed even if we are
4223 * not in true PerlIO mode
4227 #undef PerlIO_setpos
4229 PerlIO_setpos(PerlIO *f, SV *pos)
4234 Off_t *posn = (Off_t *) SvPV(pos, len);
4235 if (f && len == sizeof(Off_t))
4236 return PerlIO_seek(f, *posn, SEEK_SET);
4238 SETERRNO(EINVAL, SS$_IVCHAN);
4242 #undef PerlIO_setpos
4244 PerlIO_setpos(PerlIO *f, SV *pos)
4249 Fpos_t *fpos = (Fpos_t *) SvPV(pos, len);
4250 if (f && len == sizeof(Fpos_t)) {
4251 #if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64)
4252 return fsetpos64(f, fpos);
4254 return fsetpos(f, fpos);
4258 SETERRNO(EINVAL, SS$_IVCHAN);
4264 #undef PerlIO_getpos
4266 PerlIO_getpos(PerlIO *f, SV *pos)
4269 Off_t posn = PerlIO_tell(f);
4270 sv_setpvn(pos, (char *) &posn, sizeof(posn));
4271 return (posn == (Off_t) - 1) ? -1 : 0;
4274 #undef PerlIO_getpos
4276 PerlIO_getpos(PerlIO *f, SV *pos)
4281 #if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64)
4282 code = fgetpos64(f, &fpos);
4284 code = fgetpos(f, &fpos);
4286 sv_setpvn(pos, (char *) &fpos, sizeof(fpos));
4291 #if (defined(PERLIO_IS_STDIO) || !defined(USE_SFIO)) && !defined(HAS_VPRINTF)
4294 vprintf(char *pat, char *args)
4296 _doprnt(pat, args, stdout);
4297 return 0; /* wrong, but perl doesn't use the return
4302 vfprintf(FILE *fd, char *pat, char *args)
4304 _doprnt(pat, args, fd);
4305 return 0; /* wrong, but perl doesn't use the return
4311 #ifndef PerlIO_vsprintf
4313 PerlIO_vsprintf(char *s, int n, const char *fmt, va_list ap)
4315 int val = vsprintf(s, fmt, ap);
4317 if (strlen(s) >= (STRLEN) n) {
4319 (void) PerlIO_puts(Perl_error_log,
4320 "panic: sprintf overflow - memory corrupted!\n");
4328 #ifndef PerlIO_sprintf
4330 PerlIO_sprintf(char *s, int n, const char *fmt, ...)
4335 result = PerlIO_vsprintf(s, n, fmt, ap);