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 * 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)
93 if (my_binmode(fp, iotype, mode) != FALSE)
104 #define O_ACCMODE 3 /* Assume traditional implementation */
108 PerlIO_intmode2str(int rawmode, char *mode, int *writing)
110 int result = rawmode & O_ACCMODE;
115 ptype = IoTYPE_RDONLY;
118 ptype = IoTYPE_WRONLY;
126 *writing = (result != O_RDONLY);
128 if (result == O_RDONLY) {
132 else if (rawmode & O_APPEND) {
134 if (result != O_WRONLY)
139 if (result == O_WRONLY)
146 if (rawmode & O_BINARY)
152 #ifndef PERLIO_LAYERS
154 PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names)
156 if (!names || !*names || strEQ(names, ":crlf") || strEQ(names, ":raw")) {
159 Perl_croak(aTHX_ "Cannot apply \"%s\" in non-PerlIO perl", names);
167 PerlIO_destruct(pTHX)
172 PerlIO_binmode(pTHX_ PerlIO *fp, int iotype, int mode, const char *names)
177 return perlsio_binmode(fp, iotype, mode);
182 PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
186 int fd = PerlLIO_dup(PerlIO_fileno(f));
189 int omode = fcntl(fd, F_GETFL);
191 omode = djgpp_get_stream_mode(f);
193 PerlIO_intmode2str(omode,mode,NULL);
194 /* the r+ is a hack */
195 return PerlIO_fdopen(fd, mode);
200 SETERRNO(EBADF, SS$_IVCHAN);
208 * De-mux PerlIO_openn() into fdopen, freopen and fopen type entries
212 PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd,
213 int imode, int perm, PerlIO *old, int narg, SV **args)
217 Perl_croak(aTHX_ "More than one argument to open");
219 if (*args == &PL_sv_undef)
220 return PerlIO_tmpfile();
222 char *name = SvPV_nolen(*args);
224 fd = PerlLIO_open3(name, imode, perm);
226 return PerlIO_fdopen(fd, (char *) mode + 1);
229 return PerlIO_reopen(name, mode, old);
232 return PerlIO_open(name, mode);
237 return PerlIO_fdopen(fd, (char *) mode);
242 XS(XS_PerlIO__Layer__find)
246 Perl_croak(aTHX_ "Usage class->find(name[,load])");
248 char *name = SvPV_nolen(ST(1));
249 ST(0) = (strEQ(name, "crlf")
250 || strEQ(name, "raw")) ? &PL_sv_yes : &PL_sv_undef;
257 Perl_boot_core_PerlIO(pTHX)
259 newXS("PerlIO::Layer::find", XS_PerlIO__Layer__find, __FILE__);
265 #ifdef PERLIO_IS_STDIO
271 * Does nothing (yet) except force this file to be included in perl
272 * binary. That allows this file to force inclusion of other functions
273 * that may be required by loadable extensions e.g. for
274 * FileHandle::tmpfile
278 #undef PerlIO_tmpfile
285 #else /* PERLIO_IS_STDIO */
293 * This section is just to make sure these functions get pulled in from
297 #undef PerlIO_tmpfile
308 * Force this file to be included in perl binary. Which allows this
309 * file to force inclusion of other functions that may be required by
310 * loadable extensions e.g. for FileHandle::tmpfile
314 * Hack sfio does its own 'autoflush' on stdout in common cases. Flush
315 * results in a lot of lseek()s to regular files and lot of small
318 sfset(sfstdout, SF_SHARE, 0);
322 PerlIO_importFILE(FILE *stdio, int fl)
324 int fd = fileno(stdio);
325 PerlIO *r = PerlIO_fdopen(fd, "r+");
330 PerlIO_findFILE(PerlIO *pio)
332 int fd = PerlIO_fileno(pio);
333 FILE *f = fdopen(fd, "r+");
335 if (!f && errno == EINVAL)
337 if (!f && errno == EINVAL)
344 /*======================================================================================*/
346 * Implement all the PerlIO interface ourselves.
352 * We _MUST_ have <unistd.h> if we are using lseek() and may have large
359 #include <sys/mman.h>
363 void PerlIO_debug(const char *fmt, ...)
364 __attribute__ ((format(__printf__, 1, 2)));
367 PerlIO_debug(const char *fmt, ...)
374 char *s = PerlEnv_getenv("PERLIO_DEBUG");
376 dbg = PerlLIO_open3(s, O_WRONLY | O_CREAT | O_APPEND, 0666);
383 /* Use fixed buffer as sv_catpvf etc. needs SVs */
387 s = CopFILE(PL_curcop);
390 sprintf(buffer, "%s:%" IVdf " ", s, (IV) CopLINE(PL_curcop));
391 len = strlen(buffer);
392 vsprintf(buffer+len, fmt, ap);
393 PerlLIO_write(dbg, buffer, strlen(buffer));
395 SV *sv = newSVpvn("", 0);
398 s = CopFILE(PL_curcop);
401 Perl_sv_catpvf(aTHX_ sv, "%s:%" IVdf " ", s,
402 (IV) CopLINE(PL_curcop));
403 Perl_sv_vcatpvf(aTHX_ sv, fmt, &ap);
406 PerlLIO_write(dbg, s, len);
413 /*--------------------------------------------------------------------------------------*/
416 * Inner level routines
420 * Table of pointers to the PerlIO structs (malloc'ed)
422 #define PERLIO_TABLE_SIZE 64
425 PerlIO_allocate(pTHX)
428 * Find a free slot in the table, allocating new table as necessary
433 while ((f = *last)) {
435 last = (PerlIO **) (f);
436 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
442 Newz('I',f,PERLIO_TABLE_SIZE,PerlIO);
450 #undef PerlIO_fdupopen
452 PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
454 if (PerlIOValid(f)) {
455 PerlIO_funcs *tab = PerlIOBase(f)->tab;
457 PerlIO_debug("fdupopen f=%p param=%p\n",(void*)f,(void*)param);
458 new = (*tab->Dup)(aTHX_ PerlIO_allocate(aTHX),f,param, flags);
462 SETERRNO(EBADF, SS$_IVCHAN);
468 PerlIO_cleantable(pTHX_ PerlIO **tablep)
470 PerlIO *table = *tablep;
473 PerlIO_cleantable(aTHX_(PerlIO **) & (table[0]));
474 for (i = PERLIO_TABLE_SIZE - 1; i > 0; i--) {
475 PerlIO *f = table + i;
487 PerlIO_list_alloc(pTHX)
490 Newz('L', list, 1, PerlIO_list_t);
496 PerlIO_list_free(pTHX_ PerlIO_list_t *list)
499 if (--list->refcnt == 0) {
502 for (i = 0; i < list->cur; i++) {
503 if (list->array[i].arg)
504 SvREFCNT_dec(list->array[i].arg);
506 Safefree(list->array);
514 PerlIO_list_push(pTHX_ PerlIO_list_t *list, PerlIO_funcs *funcs, SV *arg)
517 if (list->cur >= list->len) {
520 Renew(list->array, list->len, PerlIO_pair_t);
522 New('l', list->array, list->len, PerlIO_pair_t);
524 p = &(list->array[list->cur++]);
526 if ((p->arg = arg)) {
532 PerlIO_clone_list(pTHX_ PerlIO_list_t *proto, CLONE_PARAMS *param)
534 PerlIO_list_t *list = (PerlIO_list_t *) NULL;
537 list = PerlIO_list_alloc(aTHX);
538 for (i=0; i < proto->cur; i++) {
540 if (proto->array[i].arg)
541 arg = PerlIO_sv_dup(aTHX_ proto->array[i].arg,param);
542 PerlIO_list_push(aTHX_ list, proto->array[i].funcs, arg);
549 PerlIO_clone(pTHX_ PerlInterpreter *proto, CLONE_PARAMS *param)
552 PerlIO **table = &proto->Iperlio;
555 PL_known_layers = PerlIO_clone_list(aTHX_ proto->Iknown_layers, param);
556 PL_def_layerlist = PerlIO_clone_list(aTHX_ proto->Idef_layerlist, param);
557 PerlIO_allocate(aTHX); /* root slot is never used */
558 PerlIO_debug("Clone %p from %p\n",aTHX,proto);
559 while ((f = *table)) {
561 table = (PerlIO **) (f++);
562 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
564 (void) fp_dup(f, 0, param);
573 PerlIO_destruct(pTHX)
575 PerlIO **table = &PL_perlio;
578 PerlIO_debug("Destruct %p\n",aTHX);
580 while ((f = *table)) {
582 table = (PerlIO **) (f++);
583 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
587 if (l->tab->kind & PERLIO_K_DESTRUCT) {
588 PerlIO_debug("Destruct popping %s\n", l->tab->name);
599 PerlIO_list_free(aTHX_ PL_known_layers);
600 PL_known_layers = NULL;
601 PerlIO_list_free(aTHX_ PL_def_layerlist);
602 PL_def_layerlist = NULL;
606 PerlIO_pop(pTHX_ PerlIO *f)
610 PerlIO_debug("PerlIO_pop f=%p %s\n", (void*)f, l->tab->name);
611 if (l->tab->Popped) {
613 * If popped returns non-zero do not free its layer structure
614 * it has either done so itself, or it is shared and still in
617 if ((*l->tab->Popped) (aTHX_ f) != 0)
625 /*--------------------------------------------------------------------------------------*/
627 * XS Interface for perl code
631 PerlIO_find_layer(pTHX_ const char *name, STRLEN len, int load)
634 if ((SSize_t) len <= 0)
636 for (i = 0; i < PL_known_layers->cur; i++) {
637 PerlIO_funcs *f = PL_known_layers->array[i].funcs;
638 if (memEQ(f->name, name, len)) {
639 PerlIO_debug("%.*s => %p\n", (int) len, name, (void*)f);
643 if (load && PL_subname && PL_def_layerlist
644 && PL_def_layerlist->cur >= 2) {
645 SV *pkgsv = newSVpvn("PerlIO", 6);
646 SV *layer = newSVpvn(name, len);
649 * The two SVs are magically freed by load_module
651 Perl_load_module(aTHX_ 0, pkgsv, Nullsv, layer, Nullsv);
653 return PerlIO_find_layer(aTHX_ name, len, 0);
655 PerlIO_debug("Cannot find %.*s\n", (int) len, name);
659 #ifdef USE_ATTRIBUTES_FOR_PERLIO
662 perlio_mg_set(pTHX_ SV *sv, MAGIC *mg)
665 IO *io = GvIOn((GV *) SvRV(sv));
666 PerlIO *ifp = IoIFP(io);
667 PerlIO *ofp = IoOFP(io);
668 Perl_warn(aTHX_ "set %" SVf " %p %p %p", sv, io, ifp, ofp);
674 perlio_mg_get(pTHX_ SV *sv, MAGIC *mg)
677 IO *io = GvIOn((GV *) SvRV(sv));
678 PerlIO *ifp = IoIFP(io);
679 PerlIO *ofp = IoOFP(io);
680 Perl_warn(aTHX_ "get %" SVf " %p %p %p", sv, io, ifp, ofp);
686 perlio_mg_clear(pTHX_ SV *sv, MAGIC *mg)
688 Perl_warn(aTHX_ "clear %" SVf, sv);
693 perlio_mg_free(pTHX_ SV *sv, MAGIC *mg)
695 Perl_warn(aTHX_ "free %" SVf, sv);
699 MGVTBL perlio_vtab = {
707 XS(XS_io_MODIFY_SCALAR_ATTRIBUTES)
710 SV *sv = SvRV(ST(1));
715 sv_magic(sv, (SV *) av, PERL_MAGIC_ext, NULL, 0);
717 mg = mg_find(sv, PERL_MAGIC_ext);
718 mg->mg_virtual = &perlio_vtab;
720 Perl_warn(aTHX_ "attrib %" SVf, sv);
721 for (i = 2; i < items; i++) {
723 const char *name = SvPV(ST(i), len);
724 SV *layer = PerlIO_find_layer(aTHX_ name, len, 1);
726 av_push(av, SvREFCNT_inc(layer));
737 #endif /* USE_ATTIBUTES_FOR_PERLIO */
740 PerlIO_tab_sv(pTHX_ PerlIO_funcs *tab)
742 HV *stash = gv_stashpv("PerlIO::Layer", TRUE);
743 SV *sv = sv_bless(newRV_noinc(newSViv(PTR2IV(tab))), stash);
747 XS(XS_PerlIO__Layer__find)
751 Perl_croak(aTHX_ "Usage class->find(name[,load])");
754 char *name = SvPV(ST(1), len);
755 bool load = (items > 2) ? SvTRUE(ST(2)) : 0;
756 PerlIO_funcs *layer = PerlIO_find_layer(aTHX_ name, len, load);
758 (layer) ? sv_2mortal(PerlIO_tab_sv(aTHX_ layer)) :
765 PerlIO_define_layer(pTHX_ PerlIO_funcs *tab)
767 if (!PL_known_layers)
768 PL_known_layers = PerlIO_list_alloc(aTHX);
769 PerlIO_list_push(aTHX_ PL_known_layers, tab, Nullsv);
770 PerlIO_debug("define %s %p\n", tab->name, (void*)tab);
774 PerlIO_parse_layers(pTHX_ PerlIO_list_t *av, const char *names)
777 const char *s = names;
779 while (isSPACE(*s) || *s == ':')
784 const char *as = Nullch;
786 if (!isIDFIRST(*s)) {
788 * Message is consistent with how attribute lists are
789 * passed. Even though this means "foo : : bar" is
790 * seen as an invalid separator character.
792 char q = ((*s == '\'') ? '"' : '\'');
794 "perlio: invalid separator character %c%c%c in layer specification list %s",
800 } while (isALNUM(*e));
816 * It's a nul terminated string, not allowed
817 * to \ the terminating null. Anything other
818 * character is passed over.
829 "perlio: argument list not closed for layer \"%.*s\"",
841 PerlIO_funcs *layer =
842 PerlIO_find_layer(aTHX_ s, llen, 1);
844 PerlIO_list_push(aTHX_ av, layer,
850 Perl_warn(aTHX_ "perlio: unknown layer \"%.*s\"",
863 PerlIO_default_buffer(pTHX_ PerlIO_list_t *av)
865 PerlIO_funcs *tab = &PerlIO_perlio;
866 #ifdef PERLIO_USING_CRLF
869 if (PerlIO_stdio.Set_ptrcnt)
872 PerlIO_debug("Pushing %s\n", tab->name);
873 PerlIO_list_push(aTHX_ av, PerlIO_find_layer(aTHX_ tab->name, 0, 0),
878 PerlIO_arg_fetch(PerlIO_list_t *av, IV n)
880 return av->array[n].arg;
884 PerlIO_layer_fetch(pTHX_ PerlIO_list_t *av, IV n, PerlIO_funcs *def)
886 if (n >= 0 && n < av->cur) {
887 PerlIO_debug("Layer %" IVdf " is %s\n", n,
888 av->array[n].funcs->name);
889 return av->array[n].funcs;
892 Perl_croak(aTHX_ "panic: PerlIO layer array corrupt");
897 PerlIO_default_layers(pTHX)
899 if (!PL_def_layerlist) {
900 const char *s = (PL_tainting) ? Nullch : PerlEnv_getenv("PERLIO");
901 PerlIO_funcs *osLayer = &PerlIO_unix;
902 PL_def_layerlist = PerlIO_list_alloc(aTHX);
903 PerlIO_define_layer(aTHX_ & PerlIO_unix);
904 #if defined(WIN32) && !defined(UNDER_CE)
905 PerlIO_define_layer(aTHX_ & PerlIO_win32);
907 osLayer = &PerlIO_win32;
910 PerlIO_define_layer(aTHX_ & PerlIO_raw);
911 PerlIO_define_layer(aTHX_ & PerlIO_perlio);
912 PerlIO_define_layer(aTHX_ & PerlIO_stdio);
913 PerlIO_define_layer(aTHX_ & PerlIO_crlf);
915 PerlIO_define_layer(aTHX_ & PerlIO_mmap);
917 PerlIO_define_layer(aTHX_ & PerlIO_utf8);
918 PerlIO_define_layer(aTHX_ & PerlIO_byte);
919 PerlIO_list_push(aTHX_ PL_def_layerlist,
920 PerlIO_find_layer(aTHX_ osLayer->name, 0, 0),
923 PerlIO_parse_layers(aTHX_ PL_def_layerlist, s);
926 PerlIO_default_buffer(aTHX_ PL_def_layerlist);
929 if (PL_def_layerlist->cur < 2) {
930 PerlIO_default_buffer(aTHX_ PL_def_layerlist);
932 return PL_def_layerlist;
936 Perl_boot_core_PerlIO(pTHX)
938 #ifdef USE_ATTRIBUTES_FOR_PERLIO
939 newXS("io::MODIFY_SCALAR_ATTRIBUTES", XS_io_MODIFY_SCALAR_ATTRIBUTES,
942 newXS("PerlIO::Layer::find", XS_PerlIO__Layer__find, __FILE__);
946 PerlIO_default_layer(pTHX_ I32 n)
948 PerlIO_list_t *av = PerlIO_default_layers(aTHX);
951 return PerlIO_layer_fetch(aTHX_ av, n, &PerlIO_stdio);
954 #define PerlIO_default_top() PerlIO_default_layer(aTHX_ -1)
955 #define PerlIO_default_btm() PerlIO_default_layer(aTHX_ 0)
958 PerlIO_stdstreams(pTHX)
961 PerlIO_allocate(aTHX);
962 PerlIO_fdopen(0, "Ir" PERLIO_STDTEXT);
963 PerlIO_fdopen(1, "Iw" PERLIO_STDTEXT);
964 PerlIO_fdopen(2, "Iw" PERLIO_STDTEXT);
969 PerlIO_push(pTHX_ PerlIO *f, PerlIO_funcs *tab, const char *mode, SV *arg)
972 Newc('L',l,tab->size,char,PerlIOl);
974 Zero(l, tab->size, char);
978 PerlIO_debug("PerlIO_push f=%p %s %s %p\n", (void*)f, tab->name,
979 (mode) ? mode : "(Null)", (void*)arg);
980 if ((*l->tab->Pushed) (aTHX_ f, mode, arg) != 0) {
989 PerlIOPop_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
1001 PerlIORaw_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
1004 * Remove the dummy layer
1006 PerlIO_pop(aTHX_ f);
1008 * Pop back to bottom layer
1010 if (PerlIOValid(f)) {
1012 while (!(PerlIOBase(f)->tab->kind & PERLIO_K_RAW)) {
1013 if (*PerlIONext(f)) {
1014 PerlIO_pop(aTHX_ f);
1018 * Nothing bellow - push unix on top then remove it
1020 if (PerlIO_push(aTHX_ f, PerlIO_default_btm(), mode, arg)) {
1021 PerlIO_pop(aTHX_ PerlIONext(f));
1026 PerlIO_debug(":raw f=%p :%s\n", (void*)f, PerlIOBase(f)->tab->name);
1033 PerlIO_apply_layera(pTHX_ PerlIO *f, const char *mode,
1034 PerlIO_list_t *layers, IV n)
1036 IV max = layers->cur;
1039 PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n, NULL);
1041 if (!PerlIO_push(aTHX_ f, tab, mode, PerlIOArg)) {
1052 PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names)
1056 PerlIO_list_t *layers = PerlIO_list_alloc(aTHX);
1057 code = PerlIO_parse_layers(aTHX_ layers, names);
1059 code = PerlIO_apply_layera(aTHX_ f, mode, layers, 0);
1061 PerlIO_list_free(aTHX_ layers);
1067 /*--------------------------------------------------------------------------------------*/
1069 * Given the abstraction above the public API functions
1073 PerlIO_binmode(pTHX_ PerlIO *f, int iotype, int mode, const char *names)
1075 PerlIO_debug("PerlIO_binmode f=%p %s %c %x %s\n",
1076 (void*)f, PerlIOBase(f)->tab->name, iotype, mode,
1077 (names) ? names : "(Null)");
1079 /* Do not flush etc. if (e.g.) switching encodings.
1080 if a pushed layer knows it needs to flush lower layers
1081 (for example :unix which is never going to call them)
1082 it can do the flush when it is pushed.
1084 return PerlIO_apply_layers(aTHX_ f, NULL, names) == 0 ? TRUE : FALSE;
1087 /* FIXME?: Looking down the layer stack seems wrong,
1088 but is a way of reaching past (say) an encoding layer
1089 to flip CRLF-ness of the layer(s) below
1091 #ifdef PERLIO_USING_CRLF
1092 /* Legacy binmode only has meaning if O_TEXT has a value distinct from
1093 O_BINARY so we can look for it in mode.
1095 if (!(mode & O_BINARY)) {
1098 /* Perhaps we should turn on bottom-most aware layer
1099 e.g. Ilya's idea that UNIX TTY could serve
1101 if (PerlIOBase(f)->tab->kind & PERLIO_K_CANCRLF) {
1102 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF)) {
1103 /* Not in text mode - flush any pending stuff and flip it */
1105 PerlIOBase(f)->flags |= PERLIO_F_CRLF;
1107 /* Only need to turn it on in one layer so we are done */
1112 /* Not finding a CRLF aware layer presumably means we are binary
1113 which is not what was requested - so we failed
1114 We _could_ push :crlf layer but so could caller
1119 /* Either asked for BINMODE or that is normal on this platform
1120 see if any CRLF aware layers are present and turn off the flag
1121 and possibly remove layer.
1124 if (PerlIOBase(f)->tab->kind & PERLIO_K_CANCRLF) {
1125 if ((PerlIOBase(f)->flags & PERLIO_F_CRLF)) {
1126 /* In text mode - flush any pending stuff and flip it */
1128 PerlIOBase(f)->flags &= ~PERLIO_F_CRLF;
1129 #ifndef PERLIO_USING_CRLF
1130 /* CRLF is unusual case - if this is just the :crlf layer pop it */
1131 if (PerlIOBase(f)->tab == &PerlIO_crlf) {
1132 PerlIO_pop(aTHX_ f);
1135 /* Normal case is only one layer doing this, so exit on first
1136 abnormal case can always do multiple binmode calls
1148 PerlIO__close(pTHX_ PerlIO *f)
1151 return (*PerlIOBase(f)->tab->Close) (aTHX_ f);
1153 SETERRNO(EBADF, SS$_IVCHAN);
1159 Perl_PerlIO_close(pTHX_ PerlIO *f)
1162 if (PerlIOValid(f)) {
1163 code = (*PerlIOBase(f)->tab->Close) (aTHX_ f);
1165 PerlIO_pop(aTHX_ f);
1172 Perl_PerlIO_fileno(pTHX_ PerlIO *f)
1175 return (*PerlIOBase(f)->tab->Fileno) (aTHX_ f);
1177 SETERRNO(EBADF, SS$_IVCHAN);
1183 PerlIO_context_layers(pTHX_ const char *mode)
1185 const char *type = NULL;
1187 * Need to supply default layer info from open.pm
1190 SV *layers = PL_curcop->cop_io;
1193 type = SvPV(layers, len);
1194 if (type && mode[0] != 'r') {
1196 * Skip to write part
1198 const char *s = strchr(type, 0);
1199 if (s && (s - type) < len) {
1208 static PerlIO_funcs *
1209 PerlIO_layer_from_ref(pTHX_ SV *sv)
1212 * For any scalar type load the handler which is bundled with perl
1214 if (SvTYPE(sv) < SVt_PVAV)
1215 return PerlIO_find_layer(aTHX_ "Scalar", 6, 1);
1218 * For other types allow if layer is known but don't try and load it
1220 switch (SvTYPE(sv)) {
1222 return PerlIO_find_layer(aTHX_ "Array", 5, 0);
1224 return PerlIO_find_layer(aTHX_ "Hash", 4, 0);
1226 return PerlIO_find_layer(aTHX_ "Code", 4, 0);
1228 return PerlIO_find_layer(aTHX_ "Glob", 4, 0);
1234 PerlIO_resolve_layers(pTHX_ const char *layers,
1235 const char *mode, int narg, SV **args)
1237 PerlIO_list_t *def = PerlIO_default_layers(aTHX);
1240 PerlIO_stdstreams(aTHX);
1244 * If it is a reference but not an object see if we have a handler
1247 if (SvROK(arg) && !sv_isobject(arg)) {
1248 PerlIO_funcs *handler = PerlIO_layer_from_ref(aTHX_ SvRV(arg));
1250 def = PerlIO_list_alloc(aTHX);
1251 PerlIO_list_push(aTHX_ def, handler, &PL_sv_undef);
1255 * Don't fail if handler cannot be found :Via(...) etc. may do
1256 * something sensible else we will just stringfy and open
1262 layers = PerlIO_context_layers(aTHX_ mode);
1263 if (layers && *layers) {
1267 av = PerlIO_list_alloc(aTHX);
1268 for (i = 0; i < def->cur; i++) {
1269 PerlIO_list_push(aTHX_ av, def->array[i].funcs,
1276 PerlIO_parse_layers(aTHX_ av, layers);
1287 PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd,
1288 int imode, int perm, PerlIO *f, int narg, SV **args)
1290 if (!f && narg == 1 && *args == &PL_sv_undef) {
1291 if ((f = PerlIO_tmpfile())) {
1293 layers = PerlIO_context_layers(aTHX_ mode);
1294 if (layers && *layers)
1295 PerlIO_apply_layers(aTHX_ f, mode, layers);
1299 PerlIO_list_t *layera = NULL;
1301 PerlIO_funcs *tab = NULL;
1302 if (PerlIOValid(f)) {
1304 * This is "reopen" - it is not tested as perl does not use it
1308 layera = PerlIO_list_alloc(aTHX);
1310 SV *arg = (l->tab->Getarg)
1311 ? (*l->tab->Getarg) (aTHX_ &l, NULL, 0)
1313 PerlIO_list_push(aTHX_ layera, l->tab, arg);
1314 l = *PerlIONext(&l);
1318 layera = PerlIO_resolve_layers(aTHX_ layers, mode, narg, args);
1321 * Start at "top" of layer stack
1323 n = layera->cur - 1;
1325 PerlIO_funcs *t = PerlIO_layer_fetch(aTHX_ layera, n, NULL);
1334 * Found that layer 'n' can do opens - call it
1336 if (narg > 1 && !(tab->kind & PERLIO_K_MULTIARG)) {
1337 Perl_croak(aTHX_ "More than one argument to open(,':%s')",tab->name);
1339 PerlIO_debug("openn(%s,'%s','%s',%d,%x,%o,%p,%d,%p)\n",
1340 tab->name, layers, mode, fd, imode, perm,
1341 (void*)f, narg, (void*)args);
1342 f = (*tab->Open) (aTHX_ tab, layera, n, mode, fd, imode, perm,
1345 if (n + 1 < layera->cur) {
1347 * More layers above the one that we used to open -
1350 if (PerlIO_apply_layera(aTHX_ f, mode, layera, n + 1)
1357 PerlIO_list_free(aTHX_ layera);
1364 Perl_PerlIO_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
1367 return (*PerlIOBase(f)->tab->Read) (aTHX_ f, vbuf, count);
1369 SETERRNO(EBADF, SS$_IVCHAN);
1375 Perl_PerlIO_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
1378 return (*PerlIOBase(f)->tab->Unread) (aTHX_ f, vbuf, count);
1380 SETERRNO(EBADF, SS$_IVCHAN);
1386 Perl_PerlIO_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
1389 return (*PerlIOBase(f)->tab->Write) (aTHX_ f, vbuf, count);
1391 SETERRNO(EBADF, SS$_IVCHAN);
1397 Perl_PerlIO_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
1400 return (*PerlIOBase(f)->tab->Seek) (aTHX_ f, offset, whence);
1402 SETERRNO(EBADF, SS$_IVCHAN);
1408 Perl_PerlIO_tell(pTHX_ PerlIO *f)
1411 return (*PerlIOBase(f)->tab->Tell) (aTHX_ f);
1413 SETERRNO(EBADF, SS$_IVCHAN);
1419 Perl_PerlIO_flush(pTHX_ PerlIO *f)
1423 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1424 if (tab && tab->Flush) {
1425 return (*tab->Flush) (aTHX_ f);
1428 PerlIO_debug("Cannot flush f=%p :%s\n", (void*)f, tab->name);
1429 SETERRNO(EBADF, SS$_IVCHAN);
1434 PerlIO_debug("Cannot flush f=%p\n", (void*)f);
1435 SETERRNO(EBADF, SS$_IVCHAN);
1441 * Is it good API design to do flush-all on NULL, a potentially
1442 * errorneous input? Maybe some magical value (PerlIO*
1443 * PERLIO_FLUSH_ALL = (PerlIO*)-1;)? Yes, stdio does similar
1444 * things on fflush(NULL), but should we be bound by their design
1447 PerlIO **table = &PL_perlio;
1449 while ((f = *table)) {
1451 table = (PerlIO **) (f++);
1452 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
1453 if (*f && PerlIO_flush(f) != 0)
1463 PerlIOBase_flush_linebuf(pTHX)
1465 PerlIO **table = &PL_perlio;
1467 while ((f = *table)) {
1469 table = (PerlIO **) (f++);
1470 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
1473 flags & (PERLIO_F_LINEBUF | PERLIO_F_CANWRITE))
1474 == (PERLIO_F_LINEBUF | PERLIO_F_CANWRITE))
1482 Perl_PerlIO_fill(pTHX_ PerlIO *f)
1485 return (*PerlIOBase(f)->tab->Fill) (aTHX_ f);
1487 SETERRNO(EBADF, SS$_IVCHAN);
1493 PerlIO_isutf8(PerlIO *f)
1496 return (PerlIOBase(f)->flags & PERLIO_F_UTF8) != 0;
1498 SETERRNO(EBADF, SS$_IVCHAN);
1504 Perl_PerlIO_eof(pTHX_ PerlIO *f)
1507 return (*PerlIOBase(f)->tab->Eof) (aTHX_ f);
1509 SETERRNO(EBADF, SS$_IVCHAN);
1515 Perl_PerlIO_error(pTHX_ PerlIO *f)
1518 return (*PerlIOBase(f)->tab->Error) (aTHX_ f);
1520 SETERRNO(EBADF, SS$_IVCHAN);
1526 Perl_PerlIO_clearerr(pTHX_ PerlIO *f)
1529 (*PerlIOBase(f)->tab->Clearerr) (aTHX_ f);
1531 SETERRNO(EBADF, SS$_IVCHAN);
1535 Perl_PerlIO_setlinebuf(pTHX_ PerlIO *f)
1538 (*PerlIOBase(f)->tab->Setlinebuf) (aTHX_ f);
1540 SETERRNO(EBADF, SS$_IVCHAN);
1544 PerlIO_has_base(PerlIO *f)
1546 if (PerlIOValid(f)) {
1547 return (PerlIOBase(f)->tab->Get_base != NULL);
1553 PerlIO_fast_gets(PerlIO *f)
1555 if (PerlIOValid(f) && (PerlIOBase(f)->flags & PERLIO_F_FASTGETS)) {
1556 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1557 return (tab->Set_ptrcnt != NULL);
1563 PerlIO_has_cntptr(PerlIO *f)
1565 if (PerlIOValid(f)) {
1566 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1567 return (tab->Get_ptr != NULL && tab->Get_cnt != NULL);
1573 PerlIO_canset_cnt(PerlIO *f)
1575 if (PerlIOValid(f)) {
1576 PerlIOl *l = PerlIOBase(f);
1577 return (l->tab->Set_ptrcnt != NULL);
1583 Perl_PerlIO_get_base(pTHX_ PerlIO *f)
1586 return (*PerlIOBase(f)->tab->Get_base) (aTHX_ f);
1591 Perl_PerlIO_get_bufsiz(pTHX_ PerlIO *f)
1594 return (*PerlIOBase(f)->tab->Get_bufsiz) (aTHX_ f);
1599 Perl_PerlIO_get_ptr(pTHX_ PerlIO *f)
1601 if (PerlIOValid(f)) {
1602 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1603 if (tab->Get_ptr == NULL)
1605 return (*tab->Get_ptr) (aTHX_ f);
1611 Perl_PerlIO_get_cnt(pTHX_ PerlIO *f)
1613 if (PerlIOValid(f)) {
1614 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1615 if (tab->Get_cnt == NULL)
1617 return (*tab->Get_cnt) (aTHX_ f);
1623 Perl_PerlIO_set_cnt(pTHX_ PerlIO *f, int cnt)
1625 if (PerlIOValid(f)) {
1626 (*PerlIOBase(f)->tab->Set_ptrcnt) (aTHX_ f, NULL, cnt);
1631 Perl_PerlIO_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, int cnt)
1633 if (PerlIOValid(f)) {
1634 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1635 if (tab->Set_ptrcnt == NULL) {
1636 Perl_croak(aTHX_ "PerlIO buffer snooping abuse");
1638 (*PerlIOBase(f)->tab->Set_ptrcnt) (aTHX_ f, ptr, cnt);
1642 /*--------------------------------------------------------------------------------------*/
1644 * utf8 and raw dummy layers
1648 PerlIOUtf8_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
1650 if (*PerlIONext(f)) {
1651 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1652 PerlIO_pop(aTHX_ f);
1653 if (tab->kind & PERLIO_K_UTF8)
1654 PerlIOBase(f)->flags |= PERLIO_F_UTF8;
1656 PerlIOBase(f)->flags &= ~PERLIO_F_UTF8;
1662 PerlIO_funcs PerlIO_utf8 = {
1665 PERLIO_K_DUMMY | PERLIO_F_UTF8,
1683 NULL, /* get_base */
1684 NULL, /* get_bufsiz */
1687 NULL, /* set_ptrcnt */
1690 PerlIO_funcs PerlIO_byte = {
1711 NULL, /* get_base */
1712 NULL, /* get_bufsiz */
1715 NULL, /* set_ptrcnt */
1719 PerlIORaw_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
1720 IV n, const char *mode, int fd, int imode, int perm,
1721 PerlIO *old, int narg, SV **args)
1723 PerlIO_funcs *tab = PerlIO_default_btm();
1724 return (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
1728 PerlIO_funcs PerlIO_raw = {
1749 NULL, /* get_base */
1750 NULL, /* get_bufsiz */
1753 NULL, /* set_ptrcnt */
1755 /*--------------------------------------------------------------------------------------*/
1756 /*--------------------------------------------------------------------------------------*/
1758 * "Methods" of the "base class"
1762 PerlIOBase_fileno(pTHX_ PerlIO *f)
1764 return PerlIOValid(f) ? PerlIO_fileno(PerlIONext(f)) : -1;
1768 PerlIO_modestr(PerlIO *f, char *buf)
1771 IV flags = PerlIOBase(f)->flags;
1772 if (flags & PERLIO_F_APPEND) {
1774 if (flags & PERLIO_F_CANREAD) {
1778 else if (flags & PERLIO_F_CANREAD) {
1780 if (flags & PERLIO_F_CANWRITE)
1783 else if (flags & PERLIO_F_CANWRITE) {
1785 if (flags & PERLIO_F_CANREAD) {
1789 #ifdef PERLIO_USING_CRLF
1790 if (!(flags & PERLIO_F_CRLF))
1798 PerlIOBase_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
1800 PerlIOl *l = PerlIOBase(f);
1802 const char *omode = mode;
1805 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1806 l->flags &= ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE |
1807 PERLIO_F_TRUNCATE | PERLIO_F_APPEND);
1808 if (tab->Set_ptrcnt != NULL)
1809 l->flags |= PERLIO_F_FASTGETS;
1811 if (*mode == '#' || *mode == 'I')
1815 l->flags |= PERLIO_F_CANREAD;
1818 l->flags |= PERLIO_F_APPEND | PERLIO_F_CANWRITE;
1821 l->flags |= PERLIO_F_TRUNCATE | PERLIO_F_CANWRITE;
1824 SETERRNO(EINVAL, LIB$_INVARG);
1830 l->flags |= PERLIO_F_CANREAD | PERLIO_F_CANWRITE;
1833 l->flags &= ~PERLIO_F_CRLF;
1836 l->flags |= PERLIO_F_CRLF;
1839 SETERRNO(EINVAL, LIB$_INVARG);
1846 l->flags |= l->next->flags &
1847 (PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_TRUNCATE |
1852 PerlIO_debug("PerlIOBase_pushed f=%p %s %s fl=%08" UVxf " (%s)\n",
1853 f, PerlIOBase(f)->tab->name, (omode) ? omode : "(Null)",
1854 l->flags, PerlIO_modestr(f, temp));
1860 PerlIOBase_popped(pTHX_ PerlIO *f)
1866 PerlIOBase_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
1869 * Save the position as current head considers it
1871 Off_t old = PerlIO_tell(f);
1873 PerlIO_push(aTHX_ f, &PerlIO_pending, "r", Nullsv);
1874 PerlIOSelf(f, PerlIOBuf)->posn = old;
1875 done = PerlIOBuf_unread(aTHX_ f, vbuf, count);
1880 PerlIOBase_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
1882 STDCHAR *buf = (STDCHAR *) vbuf;
1884 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD))
1887 SSize_t avail = PerlIO_get_cnt(f);
1890 take = (count < avail) ? count : avail;
1892 STDCHAR *ptr = PerlIO_get_ptr(f);
1893 Copy(ptr, buf, take, STDCHAR);
1894 PerlIO_set_ptrcnt(f, ptr + take, (avail -= take));
1898 if (count > 0 && avail <= 0) {
1899 if (PerlIO_fill(f) != 0)
1903 return (buf - (STDCHAR *) vbuf);
1909 PerlIOBase_noop_ok(pTHX_ PerlIO *f)
1915 PerlIOBase_noop_fail(pTHX_ PerlIO *f)
1921 PerlIOBase_close(pTHX_ PerlIO *f)
1924 PerlIO *n = PerlIONext(f);
1925 if (PerlIO_flush(f) != 0)
1927 if (PerlIOValid(n) && (*PerlIOBase(n)->tab->Close)(aTHX_ n) != 0)
1929 PerlIOBase(f)->flags &=
1930 ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_OPEN);
1935 PerlIOBase_eof(pTHX_ PerlIO *f)
1937 if (PerlIOValid(f)) {
1938 return (PerlIOBase(f)->flags & PERLIO_F_EOF) != 0;
1944 PerlIOBase_error(pTHX_ PerlIO *f)
1946 if (PerlIOValid(f)) {
1947 return (PerlIOBase(f)->flags & PERLIO_F_ERROR) != 0;
1953 PerlIOBase_clearerr(pTHX_ PerlIO *f)
1955 if (PerlIOValid(f)) {
1956 PerlIO *n = PerlIONext(f);
1957 PerlIOBase(f)->flags &= ~(PERLIO_F_ERROR | PERLIO_F_EOF);
1964 PerlIOBase_setlinebuf(pTHX_ PerlIO *f)
1966 if (PerlIOValid(f)) {
1967 PerlIOBase(f)->flags |= PERLIO_F_LINEBUF;
1972 PerlIO_sv_dup(pTHX_ SV *arg, CLONE_PARAMS *param)
1978 return sv_dup(arg, param);
1981 return newSVsv(arg);
1984 return newSVsv(arg);
1989 PerlIOBase_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
1991 PerlIO *nexto = PerlIONext(o);
1992 if (PerlIOValid(nexto)) {
1993 PerlIO_funcs *tab = PerlIOBase(nexto)->tab;
1994 f = (*tab->Dup)(aTHX_ f, nexto, param, flags);
1997 PerlIO_funcs *self = PerlIOBase(o)->tab;
2000 PerlIO_debug("PerlIOBase_dup %s f=%p o=%p param=%p\n",
2001 self->name, (void*)f, (void*)o, (void*)param);
2003 arg = (*self->Getarg)(aTHX_ o,param,flags);
2005 f = PerlIO_push(aTHX_ f, self, PerlIO_modestr(o,buf), arg);
2013 #define PERLIO_MAX_REFCOUNTABLE_FD 2048
2015 perl_mutex PerlIO_mutex;
2017 int PerlIO_fd_refcnt[PERLIO_MAX_REFCOUNTABLE_FD];
2022 /* Place holder for stdstreams call ??? */
2024 MUTEX_INIT(&PerlIO_mutex);
2029 PerlIOUnix_refcnt_inc(int fd)
2031 if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
2033 MUTEX_LOCK(&PerlIO_mutex);
2035 PerlIO_fd_refcnt[fd]++;
2036 PerlIO_debug("fd %d refcnt=%d\n",fd,PerlIO_fd_refcnt[fd]);
2038 MUTEX_UNLOCK(&PerlIO_mutex);
2044 PerlIOUnix_refcnt_dec(int fd)
2047 if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
2049 MUTEX_LOCK(&PerlIO_mutex);
2051 cnt = --PerlIO_fd_refcnt[fd];
2052 PerlIO_debug("fd %d refcnt=%d\n",fd,cnt);
2054 MUTEX_UNLOCK(&PerlIO_mutex);
2061 PerlIO_cleanup(pTHX)
2065 PerlIO_debug("Cleanup %p\n",aTHX);
2067 /* Raise STDIN..STDERR refcount so we don't close them */
2068 for (i=0; i < 3; i++)
2069 PerlIOUnix_refcnt_inc(i);
2070 PerlIO_cleantable(aTHX_ &PL_perlio);
2071 /* Restore STDIN..STDERR refcount */
2072 for (i=0; i < 3; i++)
2073 PerlIOUnix_refcnt_dec(i);
2078 /*--------------------------------------------------------------------------------------*/
2080 * Bottom-most level for UNIX-like case
2084 struct _PerlIO base; /* The generic part */
2085 int fd; /* UNIX like file descriptor */
2086 int oflags; /* open/fcntl flags */
2090 PerlIOUnix_oflags(const char *mode)
2093 if (*mode == 'I' || *mode == '#')
2098 if (*++mode == '+') {
2105 oflags = O_CREAT | O_TRUNC;
2106 if (*++mode == '+') {
2115 oflags = O_CREAT | O_APPEND;
2116 if (*++mode == '+') {
2129 else if (*mode == 't') {
2131 oflags &= ~O_BINARY;
2135 * Always open in binary mode
2138 if (*mode || oflags == -1) {
2139 SETERRNO(EINVAL, LIB$_INVARG);
2146 PerlIOUnix_fileno(pTHX_ PerlIO *f)
2148 return PerlIOSelf(f, PerlIOUnix)->fd;
2152 PerlIOUnix_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
2154 IV code = PerlIOBase_pushed(aTHX_ f, mode, arg);
2155 PerlIOUnix *s = PerlIOSelf(f, PerlIOUnix);
2156 if (*PerlIONext(f)) {
2157 /* We never call down so any pending stuff now */
2158 PerlIO_flush(PerlIONext(f));
2159 s->fd = PerlIO_fileno(PerlIONext(f));
2161 * XXX could (or should) we retrieve the oflags from the open file
2162 * handle rather than believing the "mode" we are passed in? XXX
2163 * Should the value on NULL mode be 0 or -1?
2165 s->oflags = mode ? PerlIOUnix_oflags(mode) : -1;
2167 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
2172 PerlIOUnix_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2173 IV n, const char *mode, int fd, int imode,
2174 int perm, PerlIO *f, int narg, SV **args)
2177 if (PerlIOBase(f)->flags & PERLIO_F_OPEN)
2178 (*PerlIOBase(f)->tab->Close)(aTHX_ f);
2181 char *path = SvPV_nolen(*args);
2185 imode = PerlIOUnix_oflags(mode);
2189 fd = PerlLIO_open3(path, imode, perm);
2197 f = PerlIO_allocate(aTHX);
2198 s = PerlIOSelf(PerlIO_push(aTHX_ f, self, mode, PerlIOArg),
2202 s = PerlIOSelf(f, PerlIOUnix);
2205 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
2206 PerlIOUnix_refcnt_inc(fd);
2212 * FIXME: pop layers ???
2220 PerlIOUnix_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
2222 PerlIOUnix *os = PerlIOSelf(o, PerlIOUnix);
2224 if (flags & PERLIO_DUP_FD) {
2225 fd = PerlLIO_dup(fd);
2227 if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
2228 f = PerlIOBase_dup(aTHX_ f, o, param, flags);
2230 /* If all went well overwrite fd in dup'ed lay with the dup()'ed fd */
2231 PerlIOUnix *s = PerlIOSelf(f, PerlIOUnix);
2233 PerlIOUnix_refcnt_inc(fd);
2242 PerlIOUnix_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
2244 int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2245 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD))
2248 SSize_t len = PerlLIO_read(fd, vbuf, count);
2249 if (len >= 0 || errno != EINTR) {
2251 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2252 else if (len == 0 && count != 0)
2253 PerlIOBase(f)->flags |= PERLIO_F_EOF;
2261 PerlIOUnix_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
2263 int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2265 SSize_t len = PerlLIO_write(fd, vbuf, count);
2266 if (len >= 0 || errno != EINTR) {
2268 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2276 PerlIOUnix_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
2279 PerlLIO_lseek(PerlIOSelf(f, PerlIOUnix)->fd, offset, whence);
2280 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
2281 return (new == (Off_t) - 1) ? -1 : 0;
2285 PerlIOUnix_tell(pTHX_ PerlIO *f)
2287 return PerlLIO_lseek(PerlIOSelf(f, PerlIOUnix)->fd, 0, SEEK_CUR);
2292 PerlIOUnix_close(pTHX_ PerlIO *f)
2294 int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2296 if (PerlIOBase(f)->flags & PERLIO_F_OPEN) {
2297 if (PerlIOUnix_refcnt_dec(fd) > 0) {
2298 PerlIOBase(f)->flags &= ~PERLIO_F_OPEN;
2303 SETERRNO(EBADF,SS$_IVCHAN);
2306 while (PerlLIO_close(fd) != 0) {
2307 if (errno != EINTR) {
2314 PerlIOBase(f)->flags &= ~PERLIO_F_OPEN;
2319 PerlIO_funcs PerlIO_unix = {
2335 PerlIOBase_noop_ok, /* flush */
2336 PerlIOBase_noop_fail, /* fill */
2339 PerlIOBase_clearerr,
2340 PerlIOBase_setlinebuf,
2341 NULL, /* get_base */
2342 NULL, /* get_bufsiz */
2345 NULL, /* set_ptrcnt */
2348 /*--------------------------------------------------------------------------------------*/
2354 struct _PerlIO base;
2355 FILE *stdio; /* The stream */
2359 PerlIOStdio_fileno(pTHX_ PerlIO *f)
2361 return PerlSIO_fileno(PerlIOSelf(f, PerlIOStdio)->stdio);
2365 PerlIOStdio_mode(const char *mode, char *tmode)
2371 #ifdef PERLIO_USING_CRLF
2379 * This isn't used yet ...
2382 PerlIOStdio_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
2384 if (*PerlIONext(f)) {
2385 PerlIOStdio *s = PerlIOSelf(f, PerlIOStdio);
2388 PerlSIO_fdopen(PerlIO_fileno(PerlIONext(f)), mode =
2389 PerlIOStdio_mode(mode, tmode));
2392 /* We never call down so any pending stuff now */
2393 PerlIO_flush(PerlIONext(f));
2398 return PerlIOBase_pushed(aTHX_ f, mode, arg);
2402 PerlIO_importFILE(FILE *stdio, int fl)
2408 PerlIOSelf(PerlIO_push
2409 (aTHX_(f = PerlIO_allocate(aTHX)), &PerlIO_stdio,
2410 "r+", Nullsv), PerlIOStdio);
2417 PerlIOStdio_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2418 IV n, const char *mode, int fd, int imode,
2419 int perm, PerlIO *f, int narg, SV **args)
2423 char *path = SvPV_nolen(*args);
2424 PerlIOStdio *s = PerlIOSelf(f, PerlIOStdio);
2426 PerlIOUnix_refcnt_dec(fileno(s->stdio));
2427 stdio = PerlSIO_freopen(path, (mode = PerlIOStdio_mode(mode, tmode)),
2432 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2437 char *path = SvPV_nolen(*args);
2440 fd = PerlLIO_open3(path, imode, perm);
2443 FILE *stdio = PerlSIO_fopen(path, mode);
2446 PerlIOSelf(PerlIO_push
2447 (aTHX_(f = PerlIO_allocate(aTHX)), self,
2448 (mode = PerlIOStdio_mode(mode, tmode)),
2452 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2467 stdio = PerlSIO_stdin;
2470 stdio = PerlSIO_stdout;
2473 stdio = PerlSIO_stderr;
2478 stdio = PerlSIO_fdopen(fd, mode =
2479 PerlIOStdio_mode(mode, tmode));
2483 PerlIOSelf(PerlIO_push
2484 (aTHX_(f = PerlIO_allocate(aTHX)), self,
2485 mode, PerlIOArg), PerlIOStdio);
2487 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2496 PerlIOStdio_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
2498 /* This assumes no layers underneath - which is what
2499 happens, but is not how I remember it. NI-S 2001/10/16
2501 if ((f = PerlIOBase_dup(aTHX_ f, o, param, flags))) {
2502 FILE *stdio = PerlIOSelf(o, PerlIOStdio)->stdio;
2503 if (flags & PERLIO_DUP_FD) {
2504 int fd = PerlLIO_dup(fileno(stdio));
2507 stdio = fdopen(fd, PerlIO_modestr(o,mode));
2510 /* FIXME: To avoid messy error recovery if dup fails
2511 re-use the existing stdio as though flag was not set
2515 PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
2516 PerlIOUnix_refcnt_inc(fileno(stdio));
2522 PerlIOStdio_close(pTHX_ PerlIO *f)
2524 #ifdef SOCKS5_VERSION_NAME
2526 Sock_size_t optlen = sizeof(int);
2528 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2529 if (PerlIOUnix_refcnt_dec(fileno(stdio)) > 0) {
2530 /* Do not close it but do flush any buffers */
2535 #ifdef SOCKS5_VERSION_NAME
2537 (PerlIO_fileno(f), SOL_SOCKET, SO_TYPE, (void *) &optval,
2539 0) ? PerlSIO_fclose(stdio) : close(PerlIO_fileno(f))
2541 PerlSIO_fclose(stdio)
2550 PerlIOStdio_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
2552 FILE *s = PerlIOSelf(f, PerlIOStdio)->stdio;
2555 STDCHAR *buf = (STDCHAR *) vbuf;
2557 * Perl is expecting PerlIO_getc() to fill the buffer Linux's
2558 * stdio does not do that for fread()
2560 int ch = PerlSIO_fgetc(s);
2567 got = PerlSIO_fread(vbuf, 1, count, s);
2572 PerlIOStdio_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
2574 FILE *s = PerlIOSelf(f, PerlIOStdio)->stdio;
2575 STDCHAR *buf = ((STDCHAR *) vbuf) + count - 1;
2578 int ch = *buf-- & 0xff;
2579 if (PerlSIO_ungetc(ch, s) != ch)
2588 PerlIOStdio_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
2590 return PerlSIO_fwrite(vbuf, 1, count,
2591 PerlIOSelf(f, PerlIOStdio)->stdio);
2595 PerlIOStdio_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
2597 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2598 return PerlSIO_fseek(stdio, offset, whence);
2602 PerlIOStdio_tell(pTHX_ PerlIO *f)
2604 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2605 return PerlSIO_ftell(stdio);
2609 PerlIOStdio_flush(pTHX_ PerlIO *f)
2611 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2612 if (PerlIOBase(f)->flags & PERLIO_F_CANWRITE) {
2613 return PerlSIO_fflush(stdio);
2618 * FIXME: This discards ungetc() and pre-read stuff which is not
2619 * right if this is just a "sync" from a layer above Suspect right
2620 * design is to do _this_ but not have layer above flush this
2621 * layer read-to-read
2624 * Not writeable - sync by attempting a seek
2627 if (PerlSIO_fseek(stdio, (Off_t) 0, SEEK_CUR) != 0)
2635 PerlIOStdio_fill(pTHX_ 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(pTHX_ PerlIO *f)
2655 return PerlSIO_feof(PerlIOSelf(f, PerlIOStdio)->stdio);
2659 PerlIOStdio_error(pTHX_ PerlIO *f)
2661 return PerlSIO_ferror(PerlIOSelf(f, PerlIOStdio)->stdio);
2665 PerlIOStdio_clearerr(pTHX_ PerlIO *f)
2667 PerlSIO_clearerr(PerlIOSelf(f, PerlIOStdio)->stdio);
2671 PerlIOStdio_setlinebuf(pTHX_ PerlIO *f)
2673 #ifdef HAS_SETLINEBUF
2674 PerlSIO_setlinebuf(PerlIOSelf(f, PerlIOStdio)->stdio);
2676 PerlSIO_setvbuf(PerlIOSelf(f, PerlIOStdio)->stdio, Nullch, _IOLBF, 0);
2682 PerlIOStdio_get_base(pTHX_ PerlIO *f)
2684 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2685 return (STDCHAR*)PerlSIO_get_base(stdio);
2689 PerlIOStdio_get_bufsiz(pTHX_ PerlIO *f)
2691 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2692 return PerlSIO_get_bufsiz(stdio);
2696 #ifdef USE_STDIO_PTR
2698 PerlIOStdio_get_ptr(pTHX_ PerlIO *f)
2700 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2701 return (STDCHAR*)PerlSIO_get_ptr(stdio);
2705 PerlIOStdio_get_cnt(pTHX_ PerlIO *f)
2707 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2708 return PerlSIO_get_cnt(stdio);
2712 PerlIOStdio_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
2714 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2716 #ifdef STDIO_PTR_LVALUE
2717 PerlSIO_set_ptr(stdio, (void*)ptr); /* LHS STDCHAR* cast non-portable */
2718 #ifdef STDIO_PTR_LVAL_SETS_CNT
2719 if (PerlSIO_get_cnt(stdio) != (cnt)) {
2720 assert(PerlSIO_get_cnt(stdio) == (cnt));
2723 #if (!defined(STDIO_PTR_LVAL_NOCHANGE_CNT))
2725 * Setting ptr _does_ change cnt - we are done
2729 #else /* STDIO_PTR_LVALUE */
2731 #endif /* STDIO_PTR_LVALUE */
2734 * Now (or only) set cnt
2736 #ifdef STDIO_CNT_LVALUE
2737 PerlSIO_set_cnt(stdio, cnt);
2738 #else /* STDIO_CNT_LVALUE */
2739 #if (defined(STDIO_PTR_LVALUE) && defined(STDIO_PTR_LVAL_SETS_CNT))
2740 PerlSIO_set_ptr(stdio,
2741 PerlSIO_get_ptr(stdio) + (PerlSIO_get_cnt(stdio) -
2743 #else /* STDIO_PTR_LVAL_SETS_CNT */
2745 #endif /* STDIO_PTR_LVAL_SETS_CNT */
2746 #endif /* STDIO_CNT_LVALUE */
2751 PerlIO_funcs PerlIO_stdio = {
2753 sizeof(PerlIOStdio),
2771 PerlIOStdio_clearerr,
2772 PerlIOStdio_setlinebuf,
2774 PerlIOStdio_get_base,
2775 PerlIOStdio_get_bufsiz,
2780 #ifdef USE_STDIO_PTR
2781 PerlIOStdio_get_ptr,
2782 PerlIOStdio_get_cnt,
2783 #if (defined(STDIO_PTR_LVALUE) && (defined(STDIO_CNT_LVALUE) || defined(STDIO_PTR_LVAL_SETS_CNT)))
2784 PerlIOStdio_set_ptrcnt
2785 #else /* STDIO_PTR_LVALUE */
2787 #endif /* STDIO_PTR_LVALUE */
2788 #else /* USE_STDIO_PTR */
2792 #endif /* USE_STDIO_PTR */
2796 PerlIO_exportFILE(PerlIO *f, int fl)
2801 stdio = fdopen(PerlIO_fileno(f), "r+");
2804 PerlIOSelf(PerlIO_push(aTHX_ f, &PerlIO_stdio, "r+", Nullsv),
2812 PerlIO_findFILE(PerlIO *f)
2816 if (l->tab == &PerlIO_stdio) {
2817 PerlIOStdio *s = PerlIOSelf(&l, PerlIOStdio);
2820 l = *PerlIONext(&l);
2822 return PerlIO_exportFILE(f, 0);
2826 PerlIO_releaseFILE(PerlIO *p, FILE *f)
2830 /*--------------------------------------------------------------------------------------*/
2832 * perlio buffer layer
2836 PerlIOBuf_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
2838 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
2839 int fd = PerlIO_fileno(f);
2841 if (fd >= 0 && PerlLIO_isatty(fd)) {
2842 PerlIOBase(f)->flags |= PERLIO_F_LINEBUF | PERLIO_F_TTY;
2844 posn = PerlIO_tell(PerlIONext(f));
2845 if (posn != (Off_t) - 1) {
2848 return PerlIOBase_pushed(aTHX_ f, mode, arg);
2852 PerlIOBuf_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2853 IV n, const char *mode, int fd, int imode, int perm,
2854 PerlIO *f, int narg, SV **args)
2856 if (PerlIOValid(f)) {
2857 PerlIO *next = PerlIONext(f);
2858 PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIOBase(next)->tab);
2859 next = (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
2861 if (!next || (*PerlIOBase(f)->tab->Pushed) (aTHX_ f, mode, PerlIOArg) != 0) {
2866 PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIO_default_btm());
2874 f = (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
2877 if (PerlIO_push(aTHX_ f, self, mode, PerlIOArg) == 0) {
2879 * if push fails during open, open fails. close will pop us.
2884 fd = PerlIO_fileno(f);
2885 #ifdef PERLIO_USING_CRLF
2887 * do something about failing setmode()? --jhi
2889 PerlLIO_setmode(fd, O_BINARY);
2891 if (init && fd == 2) {
2893 * Initial stderr is unbuffered
2895 PerlIOBase(f)->flags |= PERLIO_F_UNBUF;
2904 * This "flush" is akin to sfio's sync in that it handles files in either
2905 * read or write state
2908 PerlIOBuf_flush(pTHX_ PerlIO *f)
2910 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
2912 PerlIO *n = PerlIONext(f);
2913 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF) {
2915 * write() the buffer
2917 STDCHAR *buf = b->buf;
2919 while (p < b->ptr) {
2920 SSize_t count = PerlIO_write(n, p, b->ptr - p);
2924 else if (count < 0 || PerlIO_error(n)) {
2925 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2930 b->posn += (p - buf);
2932 else if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
2933 STDCHAR *buf = PerlIO_get_base(f);
2935 * Note position change
2937 b->posn += (b->ptr - buf);
2938 if (b->ptr < b->end) {
2940 * We did not consume all of it
2942 if (PerlIO_seek(n, b->posn, SEEK_SET) == 0) {
2943 /* Reload n as some layers may pop themselves on seek */
2944 b->posn = PerlIO_tell(n = PerlIONext(f));
2948 b->ptr = b->end = b->buf;
2949 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
2950 /* We check for Valid because of dubious decision to make PerlIO_flush(NULL) flush all */
2951 /* FIXME: Doing downstream flush may be sub-optimal see PerlIOBuf_fill() below */
2952 if (PerlIOValid(n) && PerlIO_flush(n) != 0)
2958 PerlIOBuf_fill(pTHX_ PerlIO *f)
2960 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
2961 PerlIO *n = PerlIONext(f);
2964 * FIXME: doing the down-stream flush maybe sub-optimal if it causes
2965 * pre-read data in stdio buffer to be discarded.
2966 * However, skipping the flush also skips _our_ hosekeeping
2967 * and breaks tell tests. So we do the flush.
2969 if (PerlIO_flush(f) != 0)
2971 if (PerlIOBase(f)->flags & PERLIO_F_TTY)
2972 PerlIOBase_flush_linebuf(aTHX);
2975 PerlIO_get_base(f); /* allocate via vtable */
2977 b->ptr = b->end = b->buf;
2978 if (PerlIO_fast_gets(n)) {
2980 * Layer below is also buffered. We do _NOT_ want to call its
2981 * ->Read() because that will loop till it gets what we asked for
2982 * which may hang on a pipe etc. Instead take anything it has to
2983 * hand, or ask it to fill _once_.
2985 avail = PerlIO_get_cnt(n);
2987 avail = PerlIO_fill(n);
2989 avail = PerlIO_get_cnt(n);
2991 if (!PerlIO_error(n) && PerlIO_eof(n))
2996 STDCHAR *ptr = PerlIO_get_ptr(n);
2997 SSize_t cnt = avail;
2998 if (avail > b->bufsiz)
3000 Copy(ptr, b->buf, avail, STDCHAR);
3001 PerlIO_set_ptrcnt(n, ptr + avail, cnt - avail);
3005 avail = PerlIO_read(n, b->ptr, b->bufsiz);
3009 PerlIOBase(f)->flags |= PERLIO_F_EOF;
3011 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
3014 b->end = b->buf + avail;
3015 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3020 PerlIOBuf_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
3022 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3023 if (PerlIOValid(f)) {
3026 return PerlIOBase_read(aTHX_ f, vbuf, count);
3032 PerlIOBuf_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3034 const STDCHAR *buf = (const STDCHAR *) vbuf + count;
3035 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3038 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
3043 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
3045 * Buffer is already a read buffer, we can overwrite any chars
3046 * which have been read back to buffer start
3048 avail = (b->ptr - b->buf);
3052 * Buffer is idle, set it up so whole buffer is available for
3056 b->end = b->buf + avail;
3058 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3060 * Buffer extends _back_ from where we are now
3062 b->posn -= b->bufsiz;
3064 if (avail > (SSize_t) count) {
3066 * If we have space for more than count, just move count
3074 * In simple stdio-like ungetc() case chars will be already
3077 if (buf != b->ptr) {
3078 Copy(buf, b->ptr, avail, STDCHAR);
3082 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
3089 PerlIOBuf_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3091 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3092 const STDCHAR *buf = (const STDCHAR *) vbuf;
3096 if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
3099 SSize_t avail = b->bufsiz - (b->ptr - b->buf);
3100 if ((SSize_t) count < avail)
3102 PerlIOBase(f)->flags |= PERLIO_F_WRBUF;
3103 if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF) {
3118 Copy(buf, b->ptr, avail, STDCHAR);
3125 if (b->ptr >= (b->buf + b->bufsiz))
3128 if (PerlIOBase(f)->flags & PERLIO_F_UNBUF)
3134 PerlIOBuf_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
3137 if ((code = PerlIO_flush(f)) == 0) {
3138 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3139 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
3140 code = PerlIO_seek(PerlIONext(f), offset, whence);
3142 b->posn = PerlIO_tell(PerlIONext(f));
3149 PerlIOBuf_tell(pTHX_ PerlIO *f)
3151 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3153 * b->posn is file position where b->buf was read, or will be written
3155 Off_t posn = b->posn;
3158 * If buffer is valid adjust position by amount in buffer
3160 posn += (b->ptr - b->buf);
3166 PerlIOBuf_close(pTHX_ PerlIO *f)
3168 IV code = PerlIOBase_close(aTHX_ f);
3169 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3170 if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
3174 b->ptr = b->end = b->buf;
3175 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
3180 PerlIOBuf_get_ptr(pTHX_ PerlIO *f)
3182 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3189 PerlIOBuf_get_cnt(pTHX_ PerlIO *f)
3191 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3194 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF)
3195 return (b->end - b->ptr);
3200 PerlIOBuf_get_base(pTHX_ PerlIO *f)
3202 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3207 Newz('B',b->buf,b->bufsiz, STDCHAR);
3209 b->buf = (STDCHAR *) & b->oneword;
3210 b->bufsiz = sizeof(b->oneword);
3219 PerlIOBuf_bufsiz(pTHX_ PerlIO *f)
3221 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3224 return (b->end - b->buf);
3228 PerlIOBuf_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
3230 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3234 if (PerlIO_get_cnt(f) != cnt || b->ptr < b->buf) {
3235 assert(PerlIO_get_cnt(f) == cnt);
3236 assert(b->ptr >= b->buf);
3238 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3242 PerlIOBuf_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
3244 return PerlIOBase_dup(aTHX_ f, o, param, flags);
3249 PerlIO_funcs PerlIO_perlio = {
3269 PerlIOBase_clearerr,
3270 PerlIOBase_setlinebuf,
3275 PerlIOBuf_set_ptrcnt,
3278 /*--------------------------------------------------------------------------------------*/
3280 * Temp layer to hold unread chars when cannot do it any other way
3284 PerlIOPending_fill(pTHX_ PerlIO *f)
3287 * Should never happen
3294 PerlIOPending_close(pTHX_ PerlIO *f)
3297 * A tad tricky - flush pops us, then we close new top
3300 return PerlIO_close(f);
3304 PerlIOPending_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
3307 * A tad tricky - flush pops us, then we seek new top
3310 return PerlIO_seek(f, offset, whence);
3315 PerlIOPending_flush(pTHX_ PerlIO *f)
3317 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3318 if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
3322 PerlIO_pop(aTHX_ f);
3327 PerlIOPending_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
3333 PerlIOBuf_set_ptrcnt(aTHX_ f, ptr, cnt);
3338 PerlIOPending_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
3340 IV code = PerlIOBase_pushed(aTHX_ f, mode, arg);
3341 PerlIOl *l = PerlIOBase(f);
3343 * Our PerlIO_fast_gets must match what we are pushed on, or sv_gets()
3344 * etc. get muddled when it changes mid-string when we auto-pop.
3346 l->flags = (l->flags & ~(PERLIO_F_FASTGETS | PERLIO_F_UTF8)) |
3347 (PerlIOBase(PerlIONext(f))->
3348 flags & (PERLIO_F_FASTGETS | PERLIO_F_UTF8));
3353 PerlIOPending_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
3355 SSize_t avail = PerlIO_get_cnt(f);
3360 got = PerlIOBuf_read(aTHX_ f, vbuf, avail);
3361 if (got >= 0 && got < count) {
3363 PerlIO_read(f, ((STDCHAR *) vbuf) + got, count - got);
3364 if (more >= 0 || got == 0)
3370 PerlIO_funcs PerlIO_pending = {
3374 PerlIOPending_pushed,
3385 PerlIOPending_close,
3386 PerlIOPending_flush,
3390 PerlIOBase_clearerr,
3391 PerlIOBase_setlinebuf,
3396 PerlIOPending_set_ptrcnt,
3401 /*--------------------------------------------------------------------------------------*/
3403 * crlf - translation On read translate CR,LF to "\n" we do this by
3404 * overriding ptr/cnt entries to hand back a line at a time and keeping a
3405 * record of which nl we "lied" about. On write translate "\n" to CR,LF
3409 PerlIOBuf base; /* PerlIOBuf stuff */
3410 STDCHAR *nl; /* Position of crlf we "lied" about in the
3415 PerlIOCrlf_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
3418 PerlIOBase(f)->flags |= PERLIO_F_CRLF;
3419 code = PerlIOBuf_pushed(aTHX_ f, mode, arg);
3421 PerlIO_debug("PerlIOCrlf_pushed f=%p %s %s fl=%08" UVxf "\n",
3422 f, PerlIOBase(f)->tab->name, (mode) ? mode : "(Null)",
3423 PerlIOBase(f)->flags);
3430 PerlIOCrlf_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3432 PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
3437 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF))
3438 return PerlIOBuf_unread(aTHX_ f, vbuf, count);
3440 const STDCHAR *buf = (const STDCHAR *) vbuf + count;
3441 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3443 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
3448 if (!(PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
3449 b->end = b->ptr = b->buf + b->bufsiz;
3450 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3451 b->posn -= b->bufsiz;
3453 while (count > 0 && b->ptr > b->buf) {
3456 if (b->ptr - 2 >= b->buf) {
3479 PerlIOCrlf_get_cnt(pTHX_ PerlIO *f)
3481 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3484 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
3485 PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
3486 if ((PerlIOBase(f)->flags & PERLIO_F_CRLF) && !c->nl) {
3487 STDCHAR *nl = b->ptr;
3489 while (nl < b->end && *nl != 0xd)
3491 if (nl < b->end && *nl == 0xd) {
3493 if (nl + 1 < b->end) {
3500 * Not CR,LF but just CR
3508 * Blast - found CR as last char in buffer
3513 * They may not care, defer work as long as
3517 return (nl - b->ptr);
3521 b->ptr++; /* say we have read it as far as
3522 * flush() is concerned */
3523 b->buf++; /* Leave space in front of buffer */
3524 b->bufsiz--; /* Buffer is thus smaller */
3525 code = PerlIO_fill(f); /* Fetch some more */
3526 b->bufsiz++; /* Restore size for next time */
3527 b->buf--; /* Point at space */
3528 b->ptr = nl = b->buf; /* Which is what we hand
3530 b->posn--; /* Buffer starts here */
3531 *nl = 0xd; /* Fill in the CR */
3533 goto test; /* fill() call worked */
3535 * CR at EOF - just fall through
3537 /* Should we clear EOF though ??? */
3542 return (((c->nl) ? (c->nl + 1) : b->end) - b->ptr);
3548 PerlIOCrlf_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
3550 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3551 PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
3552 IV flags = PerlIOBase(f)->flags;
3558 if (ptr == b->end && *c->nl == 0xd) {
3559 /* Defered CR at end of buffer case - we lied about count */
3570 * Test code - delete when it works ...
3572 STDCHAR *chk = (c->nl) ? (c->nl+1) : b->end;
3573 if (ptr+cnt == c->nl && c->nl+1 == b->end && *c->nl == 0xd) {
3574 /* Defered CR at end of buffer case - we lied about count */
3580 Perl_warn(aTHX_ "ptr wrong %p != %p fl=%08" UVxf
3581 " nl=%p e=%p for %d", ptr, chk, flags, c->nl,
3588 * They have taken what we lied about
3596 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3600 PerlIOCrlf_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3602 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF))
3603 return PerlIOBuf_write(aTHX_ f, vbuf, count);
3605 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3606 const STDCHAR *buf = (const STDCHAR *) vbuf;
3607 const STDCHAR *ebuf = buf + count;
3610 if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
3612 while (buf < ebuf) {
3613 STDCHAR *eptr = b->buf + b->bufsiz;
3614 PerlIOBase(f)->flags |= PERLIO_F_WRBUF;
3615 while (buf < ebuf && b->ptr < eptr) {
3617 if ((b->ptr + 2) > eptr) {
3625 *(b->ptr)++ = 0xd; /* CR */
3626 *(b->ptr)++ = 0xa; /* LF */
3628 if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF) {
3638 if (b->ptr >= eptr) {
3644 if (PerlIOBase(f)->flags & PERLIO_F_UNBUF)
3646 return (buf - (STDCHAR *) vbuf);
3651 PerlIOCrlf_flush(pTHX_ PerlIO *f)
3653 PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
3658 return PerlIOBuf_flush(aTHX_ f);
3661 PerlIO_funcs PerlIO_crlf = {
3664 PERLIO_K_BUFFERED | PERLIO_K_CANCRLF,
3666 PerlIOBase_noop_ok, /* popped */
3671 PerlIOBuf_read, /* generic read works with ptr/cnt lies
3673 PerlIOCrlf_unread, /* Put CR,LF in buffer for each '\n' */
3674 PerlIOCrlf_write, /* Put CR,LF in buffer for each '\n' */
3682 PerlIOBase_clearerr,
3683 PerlIOBase_setlinebuf,
3688 PerlIOCrlf_set_ptrcnt,
3692 /*--------------------------------------------------------------------------------------*/
3694 * mmap as "buffer" layer
3698 PerlIOBuf base; /* PerlIOBuf stuff */
3699 Mmap_t mptr; /* Mapped address */
3700 Size_t len; /* mapped length */
3701 STDCHAR *bbuf; /* malloced buffer if map fails */
3704 static size_t page_size = 0;
3707 PerlIOMmap_map(pTHX_ PerlIO *f)
3709 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3710 IV flags = PerlIOBase(f)->flags;
3714 if (flags & PERLIO_F_CANREAD) {
3715 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3716 int fd = PerlIO_fileno(f);
3718 code = Fstat(fd, &st);
3719 if (code == 0 && S_ISREG(st.st_mode)) {
3720 SSize_t len = st.st_size - b->posn;
3724 #if defined(HAS_SYSCONF) && (defined(_SC_PAGESIZE) || defined(_SC_PAGE_SIZE))
3726 SETERRNO(0, SS$_NORMAL);
3727 # ifdef _SC_PAGESIZE
3728 page_size = sysconf(_SC_PAGESIZE);
3730 page_size = sysconf(_SC_PAGE_SIZE);
3732 if ((long) page_size < 0) {
3737 (void) SvUPGRADE(error, SVt_PV);
3738 msg = SvPVx(error, n_a);
3739 Perl_croak(aTHX_ "panic: sysconf: %s",
3744 "panic: sysconf: pagesize unknown");
3748 # ifdef HAS_GETPAGESIZE
3749 page_size = getpagesize();
3751 # if defined(I_SYS_PARAM) && defined(PAGESIZE)
3752 page_size = PAGESIZE; /* compiletime, bad */
3756 if ((IV) page_size <= 0)
3757 Perl_croak(aTHX_ "panic: bad pagesize %" IVdf,
3762 * This is a hack - should never happen - open should
3765 b->posn = PerlIO_tell(PerlIONext(f));
3767 posn = (b->posn / page_size) * page_size;
3768 len = st.st_size - posn;
3769 m->mptr = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, posn);
3770 if (m->mptr && m->mptr != (Mmap_t) - 1) {
3771 #if 0 && defined(HAS_MADVISE) && defined(MADV_SEQUENTIAL)
3772 madvise(m->mptr, len, MADV_SEQUENTIAL);
3774 #if 0 && defined(HAS_MADVISE) && defined(MADV_WILLNEED)
3775 madvise(m->mptr, len, MADV_WILLNEED);
3777 PerlIOBase(f)->flags =
3778 (flags & ~PERLIO_F_EOF) | PERLIO_F_RDBUF;
3779 b->end = ((STDCHAR *) m->mptr) + len;
3780 b->buf = ((STDCHAR *) m->mptr) + (b->posn - posn);
3789 PerlIOBase(f)->flags =
3790 flags | PERLIO_F_EOF | PERLIO_F_RDBUF;
3792 b->ptr = b->end = b->ptr;
3801 PerlIOMmap_unmap(pTHX_ PerlIO *f)
3803 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3804 PerlIOBuf *b = &m->base;
3808 code = munmap(m->mptr, m->len);
3812 if (PerlIO_seek(PerlIONext(f), b->posn, SEEK_SET) != 0)
3815 b->ptr = b->end = b->buf;
3816 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
3822 PerlIOMmap_get_base(pTHX_ PerlIO *f)
3824 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3825 PerlIOBuf *b = &m->base;
3826 if (b->buf && (PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
3828 * Already have a readbuffer in progress
3834 * We have a write buffer or flushed PerlIOBuf read buffer
3836 m->bbuf = b->buf; /* save it in case we need it again */
3837 b->buf = NULL; /* Clear to trigger below */
3840 PerlIOMmap_map(aTHX_ f); /* Try and map it */
3843 * Map did not work - recover PerlIOBuf buffer if we have one
3848 b->ptr = b->end = b->buf;
3851 return PerlIOBuf_get_base(aTHX_ f);
3855 PerlIOMmap_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3857 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3858 PerlIOBuf *b = &m->base;
3859 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
3861 if (b->ptr && (b->ptr - count) >= b->buf
3862 && memEQ(b->ptr - count, vbuf, count)) {
3864 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
3869 * Loose the unwritable mapped buffer
3873 * If flush took the "buffer" see if we have one from before
3875 if (!b->buf && m->bbuf)
3878 PerlIOBuf_get_base(aTHX_ f);
3882 return PerlIOBuf_unread(aTHX_ f, vbuf, count);
3886 PerlIOMmap_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3888 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3889 PerlIOBuf *b = &m->base;
3890 if (!b->buf || !(PerlIOBase(f)->flags & PERLIO_F_WRBUF)) {
3892 * No, or wrong sort of, buffer
3895 if (PerlIOMmap_unmap(aTHX_ f) != 0)
3899 * If unmap took the "buffer" see if we have one from before
3901 if (!b->buf && m->bbuf)
3904 PerlIOBuf_get_base(aTHX_ f);
3908 return PerlIOBuf_write(aTHX_ f, vbuf, count);
3912 PerlIOMmap_flush(pTHX_ PerlIO *f)
3914 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3915 PerlIOBuf *b = &m->base;
3916 IV code = PerlIOBuf_flush(aTHX_ f);
3918 * Now we are "synced" at PerlIOBuf level
3925 if (PerlIOMmap_unmap(aTHX_ f) != 0)
3930 * We seem to have a PerlIOBuf buffer which was not mapped
3931 * remember it in case we need one later
3940 PerlIOMmap_fill(pTHX_ PerlIO *f)
3942 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3943 IV code = PerlIO_flush(f);
3944 if (code == 0 && !b->buf) {
3945 code = PerlIOMmap_map(aTHX_ f);
3947 if (code == 0 && !(PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
3948 code = PerlIOBuf_fill(aTHX_ f);
3954 PerlIOMmap_close(pTHX_ PerlIO *f)
3956 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3957 PerlIOBuf *b = &m->base;
3958 IV code = PerlIO_flush(f);
3962 b->ptr = b->end = b->buf;
3964 if (PerlIOBuf_close(aTHX_ f) != 0)
3970 PerlIOMmap_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
3972 return PerlIOBase_dup(aTHX_ f, o, param, flags);
3976 PerlIO_funcs PerlIO_mmap = {
3996 PerlIOBase_clearerr,
3997 PerlIOBase_setlinebuf,
3998 PerlIOMmap_get_base,
4002 PerlIOBuf_set_ptrcnt,
4005 #endif /* HAS_MMAP */
4008 Perl_PerlIO_stdin(pTHX)
4011 PerlIO_stdstreams(aTHX);
4013 return &PL_perlio[1];
4017 Perl_PerlIO_stdout(pTHX)
4020 PerlIO_stdstreams(aTHX);
4022 return &PL_perlio[2];
4026 Perl_PerlIO_stderr(pTHX)
4029 PerlIO_stdstreams(aTHX);
4031 return &PL_perlio[3];
4034 /*--------------------------------------------------------------------------------------*/
4037 PerlIO_getname(PerlIO *f, char *buf)
4042 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
4044 name = fgetname(stdio, buf);
4046 Perl_croak(aTHX_ "Don't know how to get file name");
4052 /*--------------------------------------------------------------------------------------*/
4054 * Functions which can be called on any kind of PerlIO implemented in
4058 #undef PerlIO_fdopen
4060 PerlIO_fdopen(int fd, const char *mode)
4063 return PerlIO_openn(aTHX_ Nullch, mode, fd, 0, 0, NULL, 0, NULL);
4068 PerlIO_open(const char *path, const char *mode)
4071 SV *name = sv_2mortal(newSVpvn(path, strlen(path)));
4072 return PerlIO_openn(aTHX_ Nullch, mode, -1, 0, 0, NULL, 1, &name);
4075 #undef Perlio_reopen
4077 PerlIO_reopen(const char *path, const char *mode, PerlIO *f)
4080 SV *name = sv_2mortal(newSVpvn(path, strlen(path)));
4081 return PerlIO_openn(aTHX_ Nullch, mode, -1, 0, 0, f, 1, &name);
4086 PerlIO_getc(PerlIO *f)
4090 SSize_t count = PerlIO_read(f, buf, 1);
4092 return (unsigned char) buf[0];
4097 #undef PerlIO_ungetc
4099 PerlIO_ungetc(PerlIO *f, int ch)
4104 if (PerlIO_unread(f, &buf, 1) == 1)
4112 PerlIO_putc(PerlIO *f, int ch)
4116 return PerlIO_write(f, &buf, 1);
4121 PerlIO_puts(PerlIO *f, const char *s)
4124 STRLEN len = strlen(s);
4125 return PerlIO_write(f, s, len);
4128 #undef PerlIO_rewind
4130 PerlIO_rewind(PerlIO *f)
4133 PerlIO_seek(f, (Off_t) 0, SEEK_SET);
4137 #undef PerlIO_vprintf
4139 PerlIO_vprintf(PerlIO *f, const char *fmt, va_list ap)
4142 SV *sv = newSVpvn("", 0);
4148 Perl_va_copy(ap, apc);
4149 sv_vcatpvf(sv, fmt, &apc);
4151 sv_vcatpvf(sv, fmt, &ap);
4154 wrote = PerlIO_write(f, s, len);
4159 #undef PerlIO_printf
4161 PerlIO_printf(PerlIO *f, const char *fmt, ...)
4166 result = PerlIO_vprintf(f, fmt, ap);
4171 #undef PerlIO_stdoutf
4173 PerlIO_stdoutf(const char *fmt, ...)
4179 result = PerlIO_vprintf(PerlIO_stdout(), fmt, ap);
4184 #undef PerlIO_tmpfile
4186 PerlIO_tmpfile(void)
4189 * I have no idea how portable mkstemp() is ...
4191 #if defined(WIN32) || !defined(HAVE_MKSTEMP)
4194 FILE *stdio = PerlSIO_tmpfile();
4197 PerlIOSelf(PerlIO_push
4198 (aTHX_(f = PerlIO_allocate(aTHX)), &PerlIO_stdio,
4199 "w+", Nullsv), PerlIOStdio);
4205 SV *sv = newSVpv("/tmp/PerlIO_XXXXXX", 0);
4206 int fd = mkstemp(SvPVX(sv));
4209 f = PerlIO_fdopen(fd, "w+");
4211 PerlIOBase(f)->flags |= PERLIO_F_TEMP;
4213 PerlLIO_unlink(SvPVX(sv));
4223 #endif /* USE_SFIO */
4224 #endif /* PERLIO_IS_STDIO */
4226 /*======================================================================================*/
4228 * Now some functions in terms of above which may be needed even if we are
4229 * not in true PerlIO mode
4233 #undef PerlIO_setpos
4235 PerlIO_setpos(PerlIO *f, SV *pos)
4240 Off_t *posn = (Off_t *) SvPV(pos, len);
4241 if (f && len == sizeof(Off_t))
4242 return PerlIO_seek(f, *posn, SEEK_SET);
4244 SETERRNO(EINVAL, SS$_IVCHAN);
4248 #undef PerlIO_setpos
4250 PerlIO_setpos(PerlIO *f, SV *pos)
4255 Fpos_t *fpos = (Fpos_t *) SvPV(pos, len);
4256 if (f && len == sizeof(Fpos_t)) {
4257 #if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64)
4258 return fsetpos64(f, fpos);
4260 return fsetpos(f, fpos);
4264 SETERRNO(EINVAL, SS$_IVCHAN);
4270 #undef PerlIO_getpos
4272 PerlIO_getpos(PerlIO *f, SV *pos)
4275 Off_t posn = PerlIO_tell(f);
4276 sv_setpvn(pos, (char *) &posn, sizeof(posn));
4277 return (posn == (Off_t) - 1) ? -1 : 0;
4280 #undef PerlIO_getpos
4282 PerlIO_getpos(PerlIO *f, SV *pos)
4287 #if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64)
4288 code = fgetpos64(f, &fpos);
4290 code = fgetpos(f, &fpos);
4292 sv_setpvn(pos, (char *) &fpos, sizeof(fpos));
4297 #if (defined(PERLIO_IS_STDIO) || !defined(USE_SFIO)) && !defined(HAS_VPRINTF)
4300 vprintf(char *pat, char *args)
4302 _doprnt(pat, args, stdout);
4303 return 0; /* wrong, but perl doesn't use the return
4308 vfprintf(FILE *fd, char *pat, char *args)
4310 _doprnt(pat, args, fd);
4311 return 0; /* wrong, but perl doesn't use the return
4317 #ifndef PerlIO_vsprintf
4319 PerlIO_vsprintf(char *s, int n, const char *fmt, va_list ap)
4321 int val = vsprintf(s, fmt, ap);
4323 if (strlen(s) >= (STRLEN) n) {
4325 (void) PerlIO_puts(Perl_error_log,
4326 "panic: sprintf overflow - memory corrupted!\n");
4334 #ifndef PerlIO_sprintf
4336 PerlIO_sprintf(char *s, int n, const char *fmt, ...)
4341 result = PerlIO_vsprintf(s, n, fmt, ap);