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);
190 PerlIO_intmode2str(omode,mode,NULL);
191 /* the r+ is a hack */
192 return PerlIO_fdopen(fd, mode);
197 SETERRNO(EBADF, SS$_IVCHAN);
205 * De-mux PerlIO_openn() into fdopen, freopen and fopen type entries
209 PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd,
210 int imode, int perm, PerlIO *old, int narg, SV **args)
214 Perl_croak(aTHX_ "More than one argument to open");
216 if (*args == &PL_sv_undef)
217 return PerlIO_tmpfile();
219 char *name = SvPV_nolen(*args);
221 fd = PerlLIO_open3(name, imode, perm);
223 return PerlIO_fdopen(fd, (char *) mode + 1);
226 return PerlIO_reopen(name, mode, old);
229 return PerlIO_open(name, mode);
234 return PerlIO_fdopen(fd, (char *) mode);
239 XS(XS_PerlIO__Layer__find)
243 Perl_croak(aTHX_ "Usage class->find(name[,load])");
245 char *name = SvPV_nolen(ST(1));
246 ST(0) = (strEQ(name, "crlf")
247 || strEQ(name, "raw")) ? &PL_sv_yes : &PL_sv_undef;
254 Perl_boot_core_PerlIO(pTHX)
256 newXS("PerlIO::Layer::find", XS_PerlIO__Layer__find, __FILE__);
262 #ifdef PERLIO_IS_STDIO
268 * Does nothing (yet) except force this file to be included in perl
269 * binary. That allows this file to force inclusion of other functions
270 * that may be required by loadable extensions e.g. for
271 * FileHandle::tmpfile
275 #undef PerlIO_tmpfile
282 #else /* PERLIO_IS_STDIO */
290 * This section is just to make sure these functions get pulled in from
294 #undef PerlIO_tmpfile
305 * Force this file to be included in perl binary. Which allows this
306 * file to force inclusion of other functions that may be required by
307 * loadable extensions e.g. for FileHandle::tmpfile
311 * Hack sfio does its own 'autoflush' on stdout in common cases. Flush
312 * results in a lot of lseek()s to regular files and lot of small
315 sfset(sfstdout, SF_SHARE, 0);
319 PerlIO_importFILE(FILE *stdio, int fl)
321 int fd = fileno(stdio);
322 PerlIO *r = PerlIO_fdopen(fd, "r+");
327 PerlIO_findFILE(PerlIO *pio)
329 int fd = PerlIO_fileno(pio);
330 FILE *f = fdopen(fd, "r+");
332 if (!f && errno == EINVAL)
334 if (!f && errno == EINVAL)
341 /*======================================================================================*/
343 * Implement all the PerlIO interface ourselves.
349 * We _MUST_ have <unistd.h> if we are using lseek() and may have large
356 #include <sys/mman.h>
360 void PerlIO_debug(const char *fmt, ...)
361 __attribute__ ((format(__printf__, 1, 2)));
364 PerlIO_debug(const char *fmt, ...)
371 char *s = PerlEnv_getenv("PERLIO_DEBUG");
373 dbg = PerlLIO_open3(s, O_WRONLY | O_CREAT | O_APPEND, 0666);
380 /* Use fixed buffer as sv_catpvf etc. needs SVs */
384 s = CopFILE(PL_curcop);
387 sprintf(buffer, "%s:%" IVdf " ", s, (IV) CopLINE(PL_curcop));
388 len = strlen(buffer);
389 vsprintf(buffer+len, fmt, ap);
390 PerlLIO_write(dbg, buffer, strlen(buffer));
392 SV *sv = newSVpvn("", 0);
395 s = CopFILE(PL_curcop);
398 Perl_sv_catpvf(aTHX_ sv, "%s:%" IVdf " ", s,
399 (IV) CopLINE(PL_curcop));
400 Perl_sv_vcatpvf(aTHX_ sv, fmt, &ap);
403 PerlLIO_write(dbg, s, len);
410 /*--------------------------------------------------------------------------------------*/
413 * Inner level routines
417 * Table of pointers to the PerlIO structs (malloc'ed)
419 #define PERLIO_TABLE_SIZE 64
422 PerlIO_allocate(pTHX)
425 * Find a free slot in the table, allocating new table as necessary
430 while ((f = *last)) {
432 last = (PerlIO **) (f);
433 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
439 Newz('I',f,PERLIO_TABLE_SIZE,PerlIO);
447 #undef PerlIO_fdupopen
449 PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
451 if (PerlIOValid(f)) {
452 PerlIO_funcs *tab = PerlIOBase(f)->tab;
454 PerlIO_debug("fdupopen f=%p param=%p\n",(void*)f,(void*)param);
455 new = (*tab->Dup)(aTHX_ PerlIO_allocate(aTHX),f,param, flags);
459 SETERRNO(EBADF, SS$_IVCHAN);
465 PerlIO_cleantable(pTHX_ PerlIO **tablep)
467 PerlIO *table = *tablep;
470 PerlIO_cleantable(aTHX_(PerlIO **) & (table[0]));
471 for (i = PERLIO_TABLE_SIZE - 1; i > 0; i--) {
472 PerlIO *f = table + i;
484 PerlIO_list_alloc(pTHX)
487 Newz('L', list, 1, PerlIO_list_t);
493 PerlIO_list_free(pTHX_ PerlIO_list_t *list)
496 if (--list->refcnt == 0) {
499 for (i = 0; i < list->cur; i++) {
500 if (list->array[i].arg)
501 SvREFCNT_dec(list->array[i].arg);
503 Safefree(list->array);
511 PerlIO_list_push(pTHX_ PerlIO_list_t *list, PerlIO_funcs *funcs, SV *arg)
514 if (list->cur >= list->len) {
517 Renew(list->array, list->len, PerlIO_pair_t);
519 New('l', list->array, list->len, PerlIO_pair_t);
521 p = &(list->array[list->cur++]);
523 if ((p->arg = arg)) {
529 PerlIO_clone_list(pTHX_ PerlIO_list_t *proto, CLONE_PARAMS *param)
531 PerlIO_list_t *list = (PerlIO_list_t *) NULL;
534 list = PerlIO_list_alloc(aTHX);
535 for (i=0; i < proto->cur; i++) {
537 if (proto->array[i].arg)
538 arg = PerlIO_sv_dup(aTHX_ proto->array[i].arg,param);
539 PerlIO_list_push(aTHX_ list, proto->array[i].funcs, arg);
546 PerlIO_clone(pTHX_ PerlInterpreter *proto, CLONE_PARAMS *param)
549 PerlIO **table = &proto->Iperlio;
552 PL_known_layers = PerlIO_clone_list(aTHX_ proto->Iknown_layers, param);
553 PL_def_layerlist = PerlIO_clone_list(aTHX_ proto->Idef_layerlist, param);
554 PerlIO_allocate(aTHX); /* root slot is never used */
555 PerlIO_debug("Clone %p from %p\n",aTHX,proto);
556 while ((f = *table)) {
558 table = (PerlIO **) (f++);
559 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
561 (void) fp_dup(f, 0, param);
570 PerlIO_destruct(pTHX)
572 PerlIO **table = &PL_perlio;
575 PerlIO_debug("Destruct %p\n",aTHX);
577 while ((f = *table)) {
579 table = (PerlIO **) (f++);
580 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
584 if (l->tab->kind & PERLIO_K_DESTRUCT) {
585 PerlIO_debug("Destruct popping %s\n", l->tab->name);
596 PerlIO_list_free(aTHX_ PL_known_layers);
597 PL_known_layers = NULL;
598 PerlIO_list_free(aTHX_ PL_def_layerlist);
599 PL_def_layerlist = NULL;
603 PerlIO_pop(pTHX_ PerlIO *f)
607 PerlIO_debug("PerlIO_pop f=%p %s\n", (void*)f, l->tab->name);
608 if (l->tab->Popped) {
610 * If popped returns non-zero do not free its layer structure
611 * it has either done so itself, or it is shared and still in
614 if ((*l->tab->Popped) (aTHX_ f) != 0)
622 /*--------------------------------------------------------------------------------------*/
624 * XS Interface for perl code
628 PerlIO_find_layer(pTHX_ const char *name, STRLEN len, int load)
631 if ((SSize_t) len <= 0)
633 for (i = 0; i < PL_known_layers->cur; i++) {
634 PerlIO_funcs *f = PL_known_layers->array[i].funcs;
635 if (memEQ(f->name, name, len)) {
636 PerlIO_debug("%.*s => %p\n", (int) len, name, (void*)f);
640 if (load && PL_subname && PL_def_layerlist
641 && PL_def_layerlist->cur >= 2) {
642 SV *pkgsv = newSVpvn("PerlIO", 6);
643 SV *layer = newSVpvn(name, len);
646 * The two SVs are magically freed by load_module
648 Perl_load_module(aTHX_ 0, pkgsv, Nullsv, layer, Nullsv);
650 return PerlIO_find_layer(aTHX_ name, len, 0);
652 PerlIO_debug("Cannot find %.*s\n", (int) len, name);
656 #ifdef USE_ATTRIBUTES_FOR_PERLIO
659 perlio_mg_set(pTHX_ SV *sv, MAGIC *mg)
662 IO *io = GvIOn((GV *) SvRV(sv));
663 PerlIO *ifp = IoIFP(io);
664 PerlIO *ofp = IoOFP(io);
665 Perl_warn(aTHX_ "set %" SVf " %p %p %p", sv, io, ifp, ofp);
671 perlio_mg_get(pTHX_ SV *sv, MAGIC *mg)
674 IO *io = GvIOn((GV *) SvRV(sv));
675 PerlIO *ifp = IoIFP(io);
676 PerlIO *ofp = IoOFP(io);
677 Perl_warn(aTHX_ "get %" SVf " %p %p %p", sv, io, ifp, ofp);
683 perlio_mg_clear(pTHX_ SV *sv, MAGIC *mg)
685 Perl_warn(aTHX_ "clear %" SVf, sv);
690 perlio_mg_free(pTHX_ SV *sv, MAGIC *mg)
692 Perl_warn(aTHX_ "free %" SVf, sv);
696 MGVTBL perlio_vtab = {
704 XS(XS_io_MODIFY_SCALAR_ATTRIBUTES)
707 SV *sv = SvRV(ST(1));
712 sv_magic(sv, (SV *) av, PERL_MAGIC_ext, NULL, 0);
714 mg = mg_find(sv, PERL_MAGIC_ext);
715 mg->mg_virtual = &perlio_vtab;
717 Perl_warn(aTHX_ "attrib %" SVf, sv);
718 for (i = 2; i < items; i++) {
720 const char *name = SvPV(ST(i), len);
721 SV *layer = PerlIO_find_layer(aTHX_ name, len, 1);
723 av_push(av, SvREFCNT_inc(layer));
734 #endif /* USE_ATTIBUTES_FOR_PERLIO */
737 PerlIO_tab_sv(pTHX_ PerlIO_funcs *tab)
739 HV *stash = gv_stashpv("PerlIO::Layer", TRUE);
740 SV *sv = sv_bless(newRV_noinc(newSViv(PTR2IV(tab))), stash);
744 XS(XS_PerlIO__Layer__find)
748 Perl_croak(aTHX_ "Usage class->find(name[,load])");
751 char *name = SvPV(ST(1), len);
752 bool load = (items > 2) ? SvTRUE(ST(2)) : 0;
753 PerlIO_funcs *layer = PerlIO_find_layer(aTHX_ name, len, load);
755 (layer) ? sv_2mortal(PerlIO_tab_sv(aTHX_ layer)) :
762 PerlIO_define_layer(pTHX_ PerlIO_funcs *tab)
764 if (!PL_known_layers)
765 PL_known_layers = PerlIO_list_alloc(aTHX);
766 PerlIO_list_push(aTHX_ PL_known_layers, tab, Nullsv);
767 PerlIO_debug("define %s %p\n", tab->name, (void*)tab);
771 PerlIO_parse_layers(pTHX_ PerlIO_list_t *av, const char *names)
774 const char *s = names;
776 while (isSPACE(*s) || *s == ':')
781 const char *as = Nullch;
783 if (!isIDFIRST(*s)) {
785 * Message is consistent with how attribute lists are
786 * passed. Even though this means "foo : : bar" is
787 * seen as an invalid separator character.
789 char q = ((*s == '\'') ? '"' : '\'');
791 "perlio: invalid separator character %c%c%c in layer specification list %s",
797 } while (isALNUM(*e));
813 * It's a nul terminated string, not allowed
814 * to \ the terminating null. Anything other
815 * character is passed over.
826 "perlio: argument list not closed for layer \"%.*s\"",
838 PerlIO_funcs *layer =
839 PerlIO_find_layer(aTHX_ s, llen, 1);
841 PerlIO_list_push(aTHX_ av, layer,
847 Perl_warn(aTHX_ "perlio: unknown layer \"%.*s\"",
860 PerlIO_default_buffer(pTHX_ PerlIO_list_t *av)
862 PerlIO_funcs *tab = &PerlIO_perlio;
863 #ifdef PERLIO_USING_CRLF
866 if (PerlIO_stdio.Set_ptrcnt)
869 PerlIO_debug("Pushing %s\n", tab->name);
870 PerlIO_list_push(aTHX_ av, PerlIO_find_layer(aTHX_ tab->name, 0, 0),
875 PerlIO_arg_fetch(PerlIO_list_t *av, IV n)
877 return av->array[n].arg;
881 PerlIO_layer_fetch(pTHX_ PerlIO_list_t *av, IV n, PerlIO_funcs *def)
883 if (n >= 0 && n < av->cur) {
884 PerlIO_debug("Layer %" IVdf " is %s\n", n,
885 av->array[n].funcs->name);
886 return av->array[n].funcs;
889 Perl_croak(aTHX_ "panic: PerlIO layer array corrupt");
894 PerlIO_default_layers(pTHX)
896 if (!PL_def_layerlist) {
897 const char *s = (PL_tainting) ? Nullch : PerlEnv_getenv("PERLIO");
898 PerlIO_funcs *osLayer = &PerlIO_unix;
899 PL_def_layerlist = PerlIO_list_alloc(aTHX);
900 PerlIO_define_layer(aTHX_ & PerlIO_unix);
901 #if defined(WIN32) && !defined(UNDER_CE)
902 PerlIO_define_layer(aTHX_ & PerlIO_win32);
904 osLayer = &PerlIO_win32;
907 PerlIO_define_layer(aTHX_ & PerlIO_raw);
908 PerlIO_define_layer(aTHX_ & PerlIO_perlio);
909 PerlIO_define_layer(aTHX_ & PerlIO_stdio);
910 PerlIO_define_layer(aTHX_ & PerlIO_crlf);
912 PerlIO_define_layer(aTHX_ & PerlIO_mmap);
914 PerlIO_define_layer(aTHX_ & PerlIO_utf8);
915 PerlIO_define_layer(aTHX_ & PerlIO_byte);
916 PerlIO_list_push(aTHX_ PL_def_layerlist,
917 PerlIO_find_layer(aTHX_ osLayer->name, 0, 0),
920 PerlIO_parse_layers(aTHX_ PL_def_layerlist, s);
923 PerlIO_default_buffer(aTHX_ PL_def_layerlist);
926 if (PL_def_layerlist->cur < 2) {
927 PerlIO_default_buffer(aTHX_ PL_def_layerlist);
929 return PL_def_layerlist;
933 Perl_boot_core_PerlIO(pTHX)
935 #ifdef USE_ATTRIBUTES_FOR_PERLIO
936 newXS("io::MODIFY_SCALAR_ATTRIBUTES", XS_io_MODIFY_SCALAR_ATTRIBUTES,
939 newXS("PerlIO::Layer::find", XS_PerlIO__Layer__find, __FILE__);
943 PerlIO_default_layer(pTHX_ I32 n)
945 PerlIO_list_t *av = PerlIO_default_layers(aTHX);
948 return PerlIO_layer_fetch(aTHX_ av, n, &PerlIO_stdio);
951 #define PerlIO_default_top() PerlIO_default_layer(aTHX_ -1)
952 #define PerlIO_default_btm() PerlIO_default_layer(aTHX_ 0)
955 PerlIO_stdstreams(pTHX)
958 PerlIO_allocate(aTHX);
959 PerlIO_fdopen(0, "Ir" PERLIO_STDTEXT);
960 PerlIO_fdopen(1, "Iw" PERLIO_STDTEXT);
961 PerlIO_fdopen(2, "Iw" PERLIO_STDTEXT);
966 PerlIO_push(pTHX_ PerlIO *f, PerlIO_funcs *tab, const char *mode, SV *arg)
969 Newc('L',l,tab->size,char,PerlIOl);
971 Zero(l, tab->size, char);
975 PerlIO_debug("PerlIO_push f=%p %s %s %p\n", (void*)f, tab->name,
976 (mode) ? mode : "(Null)", (void*)arg);
977 if ((*l->tab->Pushed) (aTHX_ f, mode, arg) != 0) {
986 PerlIOPop_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
998 PerlIORaw_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
1001 * Remove the dummy layer
1003 PerlIO_pop(aTHX_ f);
1005 * Pop back to bottom layer
1007 if (PerlIOValid(f)) {
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", (void*)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 (void*)f, PerlIOBase(f)->tab->name, iotype, mode,
1074 (names) ? names : "(Null)");
1076 /* Do not flush etc. if (e.g.) switching encodings.
1077 if a pushed layer knows it needs to flush lower layers
1078 (for example :unix which is never going to call them)
1079 it can do the flush when it is pushed.
1081 return PerlIO_apply_layers(aTHX_ f, NULL, names) == 0 ? TRUE : FALSE;
1084 /* FIXME?: Looking down the layer stack seems wrong,
1085 but is a way of reaching past (say) an encoding layer
1086 to flip CRLF-ness of the layer(s) below
1088 #ifdef PERLIO_USING_CRLF
1089 /* Legacy binmode only has meaning if O_TEXT has a value distinct from
1090 O_BINARY so we can look for it in mode.
1092 if (!(mode & O_BINARY)) {
1095 /* Perhaps we should turn on bottom-most aware layer
1096 e.g. Ilya's idea that UNIX TTY could serve
1098 if (PerlIOBase(f)->tab->kind & PERLIO_K_CANCRLF) {
1099 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF)) {
1100 /* Not in text mode - flush any pending stuff and flip it */
1102 PerlIOBase(f)->flags |= PERLIO_F_CRLF;
1104 /* Only need to turn it on in one layer so we are done */
1109 /* Not finding a CRLF aware layer presumably means we are binary
1110 which is not what was requested - so we failed
1111 We _could_ push :crlf layer but so could caller
1116 /* Either asked for BINMODE or that is normal on this platform
1117 see if any CRLF aware layers are present and turn off the flag
1118 and possibly remove layer.
1121 if (PerlIOBase(f)->tab->kind & PERLIO_K_CANCRLF) {
1122 if ((PerlIOBase(f)->flags & PERLIO_F_CRLF)) {
1123 /* In text mode - flush any pending stuff and flip it */
1125 PerlIOBase(f)->flags &= ~PERLIO_F_CRLF;
1126 #ifndef PERLIO_USING_CRLF
1127 /* CRLF is unusual case - if this is just the :crlf layer pop it */
1128 if (PerlIOBase(f)->tab == &PerlIO_crlf) {
1129 PerlIO_pop(aTHX_ f);
1132 /* Normal case is only one layer doing this, so exit on first
1133 abnormal case can always do multiple binmode calls
1145 PerlIO__close(pTHX_ PerlIO *f)
1148 return (*PerlIOBase(f)->tab->Close) (aTHX_ f);
1150 SETERRNO(EBADF, SS$_IVCHAN);
1156 Perl_PerlIO_close(pTHX_ PerlIO *f)
1159 if (PerlIOValid(f)) {
1160 code = (*PerlIOBase(f)->tab->Close) (aTHX_ f);
1162 PerlIO_pop(aTHX_ f);
1169 Perl_PerlIO_fileno(pTHX_ PerlIO *f)
1172 return (*PerlIOBase(f)->tab->Fileno) (aTHX_ f);
1174 SETERRNO(EBADF, SS$_IVCHAN);
1180 PerlIO_context_layers(pTHX_ const char *mode)
1182 const char *type = NULL;
1184 * Need to supply default layer info from open.pm
1187 SV *layers = PL_curcop->cop_io;
1190 type = SvPV(layers, len);
1191 if (type && mode[0] != 'r') {
1193 * Skip to write part
1195 const char *s = strchr(type, 0);
1196 if (s && (s - type) < len) {
1205 static PerlIO_funcs *
1206 PerlIO_layer_from_ref(pTHX_ SV *sv)
1209 * For any scalar type load the handler which is bundled with perl
1211 if (SvTYPE(sv) < SVt_PVAV)
1212 return PerlIO_find_layer(aTHX_ "Scalar", 6, 1);
1215 * For other types allow if layer is known but don't try and load it
1217 switch (SvTYPE(sv)) {
1219 return PerlIO_find_layer(aTHX_ "Array", 5, 0);
1221 return PerlIO_find_layer(aTHX_ "Hash", 4, 0);
1223 return PerlIO_find_layer(aTHX_ "Code", 4, 0);
1225 return PerlIO_find_layer(aTHX_ "Glob", 4, 0);
1231 PerlIO_resolve_layers(pTHX_ const char *layers,
1232 const char *mode, int narg, SV **args)
1234 PerlIO_list_t *def = PerlIO_default_layers(aTHX);
1237 PerlIO_stdstreams(aTHX);
1241 * If it is a reference but not an object see if we have a handler
1244 if (SvROK(arg) && !sv_isobject(arg)) {
1245 PerlIO_funcs *handler = PerlIO_layer_from_ref(aTHX_ SvRV(arg));
1247 def = PerlIO_list_alloc(aTHX);
1248 PerlIO_list_push(aTHX_ def, handler, &PL_sv_undef);
1252 * Don't fail if handler cannot be found :Via(...) etc. may do
1253 * something sensible else we will just stringfy and open
1259 layers = PerlIO_context_layers(aTHX_ mode);
1260 if (layers && *layers) {
1264 av = PerlIO_list_alloc(aTHX);
1265 for (i = 0; i < def->cur; i++) {
1266 PerlIO_list_push(aTHX_ av, def->array[i].funcs,
1273 PerlIO_parse_layers(aTHX_ av, layers);
1284 PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd,
1285 int imode, int perm, PerlIO *f, int narg, SV **args)
1287 if (!f && narg == 1 && *args == &PL_sv_undef) {
1288 if ((f = PerlIO_tmpfile())) {
1290 layers = PerlIO_context_layers(aTHX_ mode);
1291 if (layers && *layers)
1292 PerlIO_apply_layers(aTHX_ f, mode, layers);
1296 PerlIO_list_t *layera = NULL;
1298 PerlIO_funcs *tab = NULL;
1299 if (PerlIOValid(f)) {
1301 * This is "reopen" - it is not tested as perl does not use it
1305 layera = PerlIO_list_alloc(aTHX);
1307 SV *arg = (l->tab->Getarg)
1308 ? (*l->tab->Getarg) (aTHX_ &l, NULL, 0)
1310 PerlIO_list_push(aTHX_ layera, l->tab, arg);
1311 l = *PerlIONext(&l);
1315 layera = PerlIO_resolve_layers(aTHX_ layers, mode, narg, args);
1318 * Start at "top" of layer stack
1320 n = layera->cur - 1;
1322 PerlIO_funcs *t = PerlIO_layer_fetch(aTHX_ layera, n, NULL);
1331 * Found that layer 'n' can do opens - call it
1333 if (narg > 1 && !(tab->kind & PERLIO_K_MULTIARG)) {
1334 Perl_croak(aTHX_ "More than one argument to open(,':%s')",tab->name);
1336 PerlIO_debug("openn(%s,'%s','%s',%d,%x,%o,%p,%d,%p)\n",
1337 tab->name, layers, mode, fd, imode, perm,
1338 (void*)f, narg, (void*)args);
1339 f = (*tab->Open) (aTHX_ tab, layera, n, mode, fd, imode, perm,
1342 if (n + 1 < layera->cur) {
1344 * More layers above the one that we used to open -
1347 if (PerlIO_apply_layera(aTHX_ f, mode, layera, n + 1)
1354 PerlIO_list_free(aTHX_ layera);
1361 Perl_PerlIO_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
1364 return (*PerlIOBase(f)->tab->Read) (aTHX_ f, vbuf, count);
1366 SETERRNO(EBADF, SS$_IVCHAN);
1372 Perl_PerlIO_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
1375 return (*PerlIOBase(f)->tab->Unread) (aTHX_ f, vbuf, count);
1377 SETERRNO(EBADF, SS$_IVCHAN);
1383 Perl_PerlIO_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
1386 return (*PerlIOBase(f)->tab->Write) (aTHX_ f, vbuf, count);
1388 SETERRNO(EBADF, SS$_IVCHAN);
1394 Perl_PerlIO_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
1397 return (*PerlIOBase(f)->tab->Seek) (aTHX_ f, offset, whence);
1399 SETERRNO(EBADF, SS$_IVCHAN);
1405 Perl_PerlIO_tell(pTHX_ PerlIO *f)
1408 return (*PerlIOBase(f)->tab->Tell) (aTHX_ f);
1410 SETERRNO(EBADF, SS$_IVCHAN);
1416 Perl_PerlIO_flush(pTHX_ PerlIO *f)
1420 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1421 if (tab && tab->Flush) {
1422 return (*tab->Flush) (aTHX_ f);
1425 PerlIO_debug("Cannot flush f=%p :%s\n", (void*)f, tab->name);
1426 SETERRNO(EBADF, SS$_IVCHAN);
1431 PerlIO_debug("Cannot flush f=%p\n", (void*)f);
1432 SETERRNO(EBADF, SS$_IVCHAN);
1438 * Is it good API design to do flush-all on NULL, a potentially
1439 * errorneous input? Maybe some magical value (PerlIO*
1440 * PERLIO_FLUSH_ALL = (PerlIO*)-1;)? Yes, stdio does similar
1441 * things on fflush(NULL), but should we be bound by their design
1444 PerlIO **table = &PL_perlio;
1446 while ((f = *table)) {
1448 table = (PerlIO **) (f++);
1449 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
1450 if (*f && PerlIO_flush(f) != 0)
1460 PerlIOBase_flush_linebuf(pTHX)
1462 PerlIO **table = &PL_perlio;
1464 while ((f = *table)) {
1466 table = (PerlIO **) (f++);
1467 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
1470 flags & (PERLIO_F_LINEBUF | PERLIO_F_CANWRITE))
1471 == (PERLIO_F_LINEBUF | PERLIO_F_CANWRITE))
1479 Perl_PerlIO_fill(pTHX_ PerlIO *f)
1482 return (*PerlIOBase(f)->tab->Fill) (aTHX_ f);
1484 SETERRNO(EBADF, SS$_IVCHAN);
1490 PerlIO_isutf8(PerlIO *f)
1493 return (PerlIOBase(f)->flags & PERLIO_F_UTF8) != 0;
1495 SETERRNO(EBADF, SS$_IVCHAN);
1501 Perl_PerlIO_eof(pTHX_ PerlIO *f)
1504 return (*PerlIOBase(f)->tab->Eof) (aTHX_ f);
1506 SETERRNO(EBADF, SS$_IVCHAN);
1512 Perl_PerlIO_error(pTHX_ PerlIO *f)
1515 return (*PerlIOBase(f)->tab->Error) (aTHX_ f);
1517 SETERRNO(EBADF, SS$_IVCHAN);
1523 Perl_PerlIO_clearerr(pTHX_ PerlIO *f)
1526 (*PerlIOBase(f)->tab->Clearerr) (aTHX_ f);
1528 SETERRNO(EBADF, SS$_IVCHAN);
1532 Perl_PerlIO_setlinebuf(pTHX_ PerlIO *f)
1535 (*PerlIOBase(f)->tab->Setlinebuf) (aTHX_ f);
1537 SETERRNO(EBADF, SS$_IVCHAN);
1541 PerlIO_has_base(PerlIO *f)
1543 if (PerlIOValid(f)) {
1544 return (PerlIOBase(f)->tab->Get_base != NULL);
1550 PerlIO_fast_gets(PerlIO *f)
1552 if (PerlIOValid(f) && (PerlIOBase(f)->flags & PERLIO_F_FASTGETS)) {
1553 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1554 return (tab->Set_ptrcnt != NULL);
1560 PerlIO_has_cntptr(PerlIO *f)
1562 if (PerlIOValid(f)) {
1563 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1564 return (tab->Get_ptr != NULL && tab->Get_cnt != NULL);
1570 PerlIO_canset_cnt(PerlIO *f)
1572 if (PerlIOValid(f)) {
1573 PerlIOl *l = PerlIOBase(f);
1574 return (l->tab->Set_ptrcnt != NULL);
1580 Perl_PerlIO_get_base(pTHX_ PerlIO *f)
1583 return (*PerlIOBase(f)->tab->Get_base) (aTHX_ f);
1588 Perl_PerlIO_get_bufsiz(pTHX_ PerlIO *f)
1591 return (*PerlIOBase(f)->tab->Get_bufsiz) (aTHX_ f);
1596 Perl_PerlIO_get_ptr(pTHX_ PerlIO *f)
1598 if (PerlIOValid(f)) {
1599 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1600 if (tab->Get_ptr == NULL)
1602 return (*tab->Get_ptr) (aTHX_ f);
1608 Perl_PerlIO_get_cnt(pTHX_ PerlIO *f)
1610 if (PerlIOValid(f)) {
1611 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1612 if (tab->Get_cnt == NULL)
1614 return (*tab->Get_cnt) (aTHX_ f);
1620 Perl_PerlIO_set_cnt(pTHX_ PerlIO *f, int cnt)
1622 if (PerlIOValid(f)) {
1623 (*PerlIOBase(f)->tab->Set_ptrcnt) (aTHX_ f, NULL, cnt);
1628 Perl_PerlIO_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, int cnt)
1630 if (PerlIOValid(f)) {
1631 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1632 if (tab->Set_ptrcnt == NULL) {
1633 Perl_croak(aTHX_ "PerlIO buffer snooping abuse");
1635 (*PerlIOBase(f)->tab->Set_ptrcnt) (aTHX_ f, ptr, cnt);
1639 /*--------------------------------------------------------------------------------------*/
1641 * utf8 and raw dummy layers
1645 PerlIOUtf8_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
1647 if (*PerlIONext(f)) {
1648 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1649 PerlIO_pop(aTHX_ f);
1650 if (tab->kind & PERLIO_K_UTF8)
1651 PerlIOBase(f)->flags |= PERLIO_F_UTF8;
1653 PerlIOBase(f)->flags &= ~PERLIO_F_UTF8;
1659 PerlIO_funcs PerlIO_utf8 = {
1662 PERLIO_K_DUMMY | PERLIO_F_UTF8,
1680 NULL, /* get_base */
1681 NULL, /* get_bufsiz */
1684 NULL, /* set_ptrcnt */
1687 PerlIO_funcs PerlIO_byte = {
1708 NULL, /* get_base */
1709 NULL, /* get_bufsiz */
1712 NULL, /* set_ptrcnt */
1716 PerlIORaw_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
1717 IV n, const char *mode, int fd, int imode, int perm,
1718 PerlIO *old, int narg, SV **args)
1720 PerlIO_funcs *tab = PerlIO_default_btm();
1721 return (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
1725 PerlIO_funcs PerlIO_raw = {
1746 NULL, /* get_base */
1747 NULL, /* get_bufsiz */
1750 NULL, /* set_ptrcnt */
1752 /*--------------------------------------------------------------------------------------*/
1753 /*--------------------------------------------------------------------------------------*/
1755 * "Methods" of the "base class"
1759 PerlIOBase_fileno(pTHX_ PerlIO *f)
1761 return PerlIOValid(f) ? PerlIO_fileno(PerlIONext(f)) : -1;
1765 PerlIO_modestr(PerlIO *f, char *buf)
1768 IV flags = PerlIOBase(f)->flags;
1769 if (flags & PERLIO_F_APPEND) {
1771 if (flags & PERLIO_F_CANREAD) {
1775 else if (flags & PERLIO_F_CANREAD) {
1777 if (flags & PERLIO_F_CANWRITE)
1780 else if (flags & PERLIO_F_CANWRITE) {
1782 if (flags & PERLIO_F_CANREAD) {
1786 #ifdef PERLIO_USING_CRLF
1787 if (!(flags & PERLIO_F_CRLF))
1795 PerlIOBase_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
1797 PerlIOl *l = PerlIOBase(f);
1799 const char *omode = mode;
1802 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1803 l->flags &= ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE |
1804 PERLIO_F_TRUNCATE | PERLIO_F_APPEND);
1805 if (tab->Set_ptrcnt != NULL)
1806 l->flags |= PERLIO_F_FASTGETS;
1808 if (*mode == '#' || *mode == 'I')
1812 l->flags |= PERLIO_F_CANREAD;
1815 l->flags |= PERLIO_F_APPEND | PERLIO_F_CANWRITE;
1818 l->flags |= PERLIO_F_TRUNCATE | PERLIO_F_CANWRITE;
1821 SETERRNO(EINVAL, LIB$_INVARG);
1827 l->flags |= PERLIO_F_CANREAD | PERLIO_F_CANWRITE;
1830 l->flags &= ~PERLIO_F_CRLF;
1833 l->flags |= PERLIO_F_CRLF;
1836 SETERRNO(EINVAL, LIB$_INVARG);
1843 l->flags |= l->next->flags &
1844 (PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_TRUNCATE |
1849 PerlIO_debug("PerlIOBase_pushed f=%p %s %s fl=%08" UVxf " (%s)\n",
1850 f, PerlIOBase(f)->tab->name, (omode) ? omode : "(Null)",
1851 l->flags, PerlIO_modestr(f, temp));
1857 PerlIOBase_popped(pTHX_ PerlIO *f)
1863 PerlIOBase_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
1866 * Save the position as current head considers it
1868 Off_t old = PerlIO_tell(f);
1870 PerlIO_push(aTHX_ f, &PerlIO_pending, "r", Nullsv);
1871 PerlIOSelf(f, PerlIOBuf)->posn = old;
1872 done = PerlIOBuf_unread(aTHX_ f, vbuf, count);
1877 PerlIOBase_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
1879 STDCHAR *buf = (STDCHAR *) vbuf;
1881 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD))
1884 SSize_t avail = PerlIO_get_cnt(f);
1887 take = (count < avail) ? count : avail;
1889 STDCHAR *ptr = PerlIO_get_ptr(f);
1890 Copy(ptr, buf, take, STDCHAR);
1891 PerlIO_set_ptrcnt(f, ptr + take, (avail -= take));
1895 if (count > 0 && avail <= 0) {
1896 if (PerlIO_fill(f) != 0)
1900 return (buf - (STDCHAR *) vbuf);
1906 PerlIOBase_noop_ok(pTHX_ PerlIO *f)
1912 PerlIOBase_noop_fail(pTHX_ PerlIO *f)
1918 PerlIOBase_close(pTHX_ PerlIO *f)
1921 PerlIO *n = PerlIONext(f);
1922 if (PerlIO_flush(f) != 0)
1924 if (PerlIOValid(n) && (*PerlIOBase(n)->tab->Close)(aTHX_ n) != 0)
1926 PerlIOBase(f)->flags &=
1927 ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_OPEN);
1932 PerlIOBase_eof(pTHX_ PerlIO *f)
1934 if (PerlIOValid(f)) {
1935 return (PerlIOBase(f)->flags & PERLIO_F_EOF) != 0;
1941 PerlIOBase_error(pTHX_ PerlIO *f)
1943 if (PerlIOValid(f)) {
1944 return (PerlIOBase(f)->flags & PERLIO_F_ERROR) != 0;
1950 PerlIOBase_clearerr(pTHX_ PerlIO *f)
1952 if (PerlIOValid(f)) {
1953 PerlIO *n = PerlIONext(f);
1954 PerlIOBase(f)->flags &= ~(PERLIO_F_ERROR | PERLIO_F_EOF);
1961 PerlIOBase_setlinebuf(pTHX_ PerlIO *f)
1963 if (PerlIOValid(f)) {
1964 PerlIOBase(f)->flags |= PERLIO_F_LINEBUF;
1969 PerlIO_sv_dup(pTHX_ SV *arg, CLONE_PARAMS *param)
1975 return sv_dup(arg, param);
1978 return newSVsv(arg);
1981 return newSVsv(arg);
1986 PerlIOBase_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
1988 PerlIO *nexto = PerlIONext(o);
1989 if (PerlIOValid(nexto)) {
1990 PerlIO_funcs *tab = PerlIOBase(nexto)->tab;
1991 f = (*tab->Dup)(aTHX_ f, nexto, param, flags);
1994 PerlIO_funcs *self = PerlIOBase(o)->tab;
1997 PerlIO_debug("PerlIOBase_dup %s f=%p o=%p param=%p\n",
1998 self->name, (void*)f, (void*)o, (void*)param);
2000 arg = (*self->Getarg)(aTHX_ o,param,flags);
2002 f = PerlIO_push(aTHX_ f, self, PerlIO_modestr(o,buf), arg);
2010 #define PERLIO_MAX_REFCOUNTABLE_FD 2048
2012 perl_mutex PerlIO_mutex;
2014 int PerlIO_fd_refcnt[PERLIO_MAX_REFCOUNTABLE_FD];
2019 /* Place holder for stdstreams call ??? */
2021 MUTEX_INIT(&PerlIO_mutex);
2026 PerlIOUnix_refcnt_inc(int fd)
2028 if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
2030 MUTEX_LOCK(&PerlIO_mutex);
2032 PerlIO_fd_refcnt[fd]++;
2033 PerlIO_debug("fd %d refcnt=%d\n",fd,PerlIO_fd_refcnt[fd]);
2035 MUTEX_UNLOCK(&PerlIO_mutex);
2041 PerlIOUnix_refcnt_dec(int fd)
2044 if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
2046 MUTEX_LOCK(&PerlIO_mutex);
2048 cnt = --PerlIO_fd_refcnt[fd];
2049 PerlIO_debug("fd %d refcnt=%d\n",fd,cnt);
2051 MUTEX_UNLOCK(&PerlIO_mutex);
2058 PerlIO_cleanup(pTHX)
2062 PerlIO_debug("Cleanup %p\n",aTHX);
2064 /* Raise STDIN..STDERR refcount so we don't close them */
2065 for (i=0; i < 3; i++)
2066 PerlIOUnix_refcnt_inc(i);
2067 PerlIO_cleantable(aTHX_ &PL_perlio);
2068 /* Restore STDIN..STDERR refcount */
2069 for (i=0; i < 3; i++)
2070 PerlIOUnix_refcnt_dec(i);
2075 /*--------------------------------------------------------------------------------------*/
2077 * Bottom-most level for UNIX-like case
2081 struct _PerlIO base; /* The generic part */
2082 int fd; /* UNIX like file descriptor */
2083 int oflags; /* open/fcntl flags */
2087 PerlIOUnix_oflags(const char *mode)
2090 if (*mode == 'I' || *mode == '#')
2095 if (*++mode == '+') {
2102 oflags = O_CREAT | O_TRUNC;
2103 if (*++mode == '+') {
2112 oflags = O_CREAT | O_APPEND;
2113 if (*++mode == '+') {
2126 else if (*mode == 't') {
2128 oflags &= ~O_BINARY;
2132 * Always open in binary mode
2135 if (*mode || oflags == -1) {
2136 SETERRNO(EINVAL, LIB$_INVARG);
2143 PerlIOUnix_fileno(pTHX_ PerlIO *f)
2145 return PerlIOSelf(f, PerlIOUnix)->fd;
2149 PerlIOUnix_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
2151 IV code = PerlIOBase_pushed(aTHX_ f, mode, arg);
2152 PerlIOUnix *s = PerlIOSelf(f, PerlIOUnix);
2153 if (*PerlIONext(f)) {
2154 /* We never call down so any pending stuff now */
2155 PerlIO_flush(PerlIONext(f));
2156 s->fd = PerlIO_fileno(PerlIONext(f));
2158 * XXX could (or should) we retrieve the oflags from the open file
2159 * handle rather than believing the "mode" we are passed in? XXX
2160 * Should the value on NULL mode be 0 or -1?
2162 s->oflags = mode ? PerlIOUnix_oflags(mode) : -1;
2164 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
2169 PerlIOUnix_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2170 IV n, const char *mode, int fd, int imode,
2171 int perm, PerlIO *f, int narg, SV **args)
2174 if (PerlIOBase(f)->flags & PERLIO_F_OPEN)
2175 (*PerlIOBase(f)->tab->Close)(aTHX_ f);
2178 char *path = SvPV_nolen(*args);
2182 imode = PerlIOUnix_oflags(mode);
2186 fd = PerlLIO_open3(path, imode, perm);
2194 f = PerlIO_allocate(aTHX);
2195 s = PerlIOSelf(PerlIO_push(aTHX_ f, self, mode, PerlIOArg),
2199 s = PerlIOSelf(f, PerlIOUnix);
2202 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
2203 PerlIOUnix_refcnt_inc(fd);
2209 * FIXME: pop layers ???
2217 PerlIOUnix_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
2219 PerlIOUnix *os = PerlIOSelf(o, PerlIOUnix);
2221 if (flags & PERLIO_DUP_FD) {
2222 fd = PerlLIO_dup(fd);
2224 if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
2225 f = PerlIOBase_dup(aTHX_ f, o, param, flags);
2227 /* If all went well overwrite fd in dup'ed lay with the dup()'ed fd */
2228 PerlIOUnix *s = PerlIOSelf(f, PerlIOUnix);
2230 PerlIOUnix_refcnt_inc(fd);
2239 PerlIOUnix_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
2241 int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2242 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD))
2245 SSize_t len = PerlLIO_read(fd, vbuf, count);
2246 if (len >= 0 || errno != EINTR) {
2248 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2249 else if (len == 0 && count != 0)
2250 PerlIOBase(f)->flags |= PERLIO_F_EOF;
2258 PerlIOUnix_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
2260 int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2262 SSize_t len = PerlLIO_write(fd, vbuf, count);
2263 if (len >= 0 || errno != EINTR) {
2265 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2273 PerlIOUnix_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
2276 PerlLIO_lseek(PerlIOSelf(f, PerlIOUnix)->fd, offset, whence);
2277 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
2278 return (new == (Off_t) - 1) ? -1 : 0;
2282 PerlIOUnix_tell(pTHX_ PerlIO *f)
2284 return PerlLIO_lseek(PerlIOSelf(f, PerlIOUnix)->fd, 0, SEEK_CUR);
2289 PerlIOUnix_close(pTHX_ PerlIO *f)
2291 int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2293 if (PerlIOBase(f)->flags & PERLIO_F_OPEN) {
2294 if (PerlIOUnix_refcnt_dec(fd) > 0) {
2295 PerlIOBase(f)->flags &= ~PERLIO_F_OPEN;
2300 SETERRNO(EBADF,SS$_IVCHAN);
2303 while (PerlLIO_close(fd) != 0) {
2304 if (errno != EINTR) {
2311 PerlIOBase(f)->flags &= ~PERLIO_F_OPEN;
2316 PerlIO_funcs PerlIO_unix = {
2332 PerlIOBase_noop_ok, /* flush */
2333 PerlIOBase_noop_fail, /* fill */
2336 PerlIOBase_clearerr,
2337 PerlIOBase_setlinebuf,
2338 NULL, /* get_base */
2339 NULL, /* get_bufsiz */
2342 NULL, /* set_ptrcnt */
2345 /*--------------------------------------------------------------------------------------*/
2351 struct _PerlIO base;
2352 FILE *stdio; /* The stream */
2356 PerlIOStdio_fileno(pTHX_ PerlIO *f)
2358 return PerlSIO_fileno(PerlIOSelf(f, PerlIOStdio)->stdio);
2362 PerlIOStdio_mode(const char *mode, char *tmode)
2368 #ifdef PERLIO_USING_CRLF
2376 * This isn't used yet ...
2379 PerlIOStdio_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
2381 if (*PerlIONext(f)) {
2382 PerlIOStdio *s = PerlIOSelf(f, PerlIOStdio);
2385 PerlSIO_fdopen(PerlIO_fileno(PerlIONext(f)), mode =
2386 PerlIOStdio_mode(mode, tmode));
2389 /* We never call down so any pending stuff now */
2390 PerlIO_flush(PerlIONext(f));
2395 return PerlIOBase_pushed(aTHX_ f, mode, arg);
2399 PerlIO_importFILE(FILE *stdio, int fl)
2405 PerlIOSelf(PerlIO_push
2406 (aTHX_(f = PerlIO_allocate(aTHX)), &PerlIO_stdio,
2407 "r+", Nullsv), PerlIOStdio);
2414 PerlIOStdio_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2415 IV n, const char *mode, int fd, int imode,
2416 int perm, PerlIO *f, int narg, SV **args)
2420 char *path = SvPV_nolen(*args);
2421 PerlIOStdio *s = PerlIOSelf(f, PerlIOStdio);
2423 PerlIOUnix_refcnt_dec(fileno(s->stdio));
2424 stdio = PerlSIO_freopen(path, (mode = PerlIOStdio_mode(mode, tmode)),
2429 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2434 char *path = SvPV_nolen(*args);
2437 fd = PerlLIO_open3(path, imode, perm);
2440 FILE *stdio = PerlSIO_fopen(path, mode);
2443 PerlIOSelf(PerlIO_push
2444 (aTHX_(f = PerlIO_allocate(aTHX)), self,
2445 (mode = PerlIOStdio_mode(mode, tmode)),
2449 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2464 stdio = PerlSIO_stdin;
2467 stdio = PerlSIO_stdout;
2470 stdio = PerlSIO_stderr;
2475 stdio = PerlSIO_fdopen(fd, mode =
2476 PerlIOStdio_mode(mode, tmode));
2480 PerlIOSelf(PerlIO_push
2481 (aTHX_(f = PerlIO_allocate(aTHX)), self,
2482 mode, PerlIOArg), PerlIOStdio);
2484 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2493 PerlIOStdio_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
2495 /* This assumes no layers underneath - which is what
2496 happens, but is not how I remember it. NI-S 2001/10/16
2498 if ((f = PerlIOBase_dup(aTHX_ f, o, param, flags))) {
2499 FILE *stdio = PerlIOSelf(o, PerlIOStdio)->stdio;
2500 if (flags & PERLIO_DUP_FD) {
2501 int fd = PerlLIO_dup(fileno(stdio));
2504 stdio = fdopen(fd, PerlIO_modestr(o,mode));
2507 /* FIXME: To avoid messy error recovery if dup fails
2508 re-use the existing stdio as though flag was not set
2512 PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
2513 PerlIOUnix_refcnt_inc(fileno(stdio));
2519 PerlIOStdio_close(pTHX_ PerlIO *f)
2521 #ifdef SOCKS5_VERSION_NAME
2523 Sock_size_t optlen = sizeof(int);
2525 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2526 if (PerlIOUnix_refcnt_dec(fileno(stdio)) > 0) {
2527 /* Do not close it but do flush any buffers */
2532 #ifdef SOCKS5_VERSION_NAME
2534 (PerlIO_fileno(f), SOL_SOCKET, SO_TYPE, (void *) &optval,
2536 0) ? PerlSIO_fclose(stdio) : close(PerlIO_fileno(f))
2538 PerlSIO_fclose(stdio)
2547 PerlIOStdio_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
2549 FILE *s = PerlIOSelf(f, PerlIOStdio)->stdio;
2552 STDCHAR *buf = (STDCHAR *) vbuf;
2554 * Perl is expecting PerlIO_getc() to fill the buffer Linux's
2555 * stdio does not do that for fread()
2557 int ch = PerlSIO_fgetc(s);
2564 got = PerlSIO_fread(vbuf, 1, count, s);
2569 PerlIOStdio_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
2571 FILE *s = PerlIOSelf(f, PerlIOStdio)->stdio;
2572 STDCHAR *buf = ((STDCHAR *) vbuf) + count - 1;
2575 int ch = *buf-- & 0xff;
2576 if (PerlSIO_ungetc(ch, s) != ch)
2585 PerlIOStdio_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
2587 return PerlSIO_fwrite(vbuf, 1, count,
2588 PerlIOSelf(f, PerlIOStdio)->stdio);
2592 PerlIOStdio_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
2594 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2595 return PerlSIO_fseek(stdio, offset, whence);
2599 PerlIOStdio_tell(pTHX_ PerlIO *f)
2601 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2602 return PerlSIO_ftell(stdio);
2606 PerlIOStdio_flush(pTHX_ PerlIO *f)
2608 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2609 if (PerlIOBase(f)->flags & PERLIO_F_CANWRITE) {
2610 return PerlSIO_fflush(stdio);
2615 * FIXME: This discards ungetc() and pre-read stuff which is not
2616 * right if this is just a "sync" from a layer above Suspect right
2617 * design is to do _this_ but not have layer above flush this
2618 * layer read-to-read
2621 * Not writeable - sync by attempting a seek
2624 if (PerlSIO_fseek(stdio, (Off_t) 0, SEEK_CUR) != 0)
2632 PerlIOStdio_fill(pTHX_ PerlIO *f)
2634 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2637 * fflush()ing read-only streams can cause trouble on some stdio-s
2639 if ((PerlIOBase(f)->flags & PERLIO_F_CANWRITE)) {
2640 if (PerlSIO_fflush(stdio) != 0)
2643 c = PerlSIO_fgetc(stdio);
2644 if (c == EOF || PerlSIO_ungetc(c, stdio) != c)
2650 PerlIOStdio_eof(pTHX_ PerlIO *f)
2652 return PerlSIO_feof(PerlIOSelf(f, PerlIOStdio)->stdio);
2656 PerlIOStdio_error(pTHX_ PerlIO *f)
2658 return PerlSIO_ferror(PerlIOSelf(f, PerlIOStdio)->stdio);
2662 PerlIOStdio_clearerr(pTHX_ PerlIO *f)
2664 PerlSIO_clearerr(PerlIOSelf(f, PerlIOStdio)->stdio);
2668 PerlIOStdio_setlinebuf(pTHX_ PerlIO *f)
2670 #ifdef HAS_SETLINEBUF
2671 PerlSIO_setlinebuf(PerlIOSelf(f, PerlIOStdio)->stdio);
2673 PerlSIO_setvbuf(PerlIOSelf(f, PerlIOStdio)->stdio, Nullch, _IOLBF, 0);
2679 PerlIOStdio_get_base(pTHX_ PerlIO *f)
2681 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2682 return (STDCHAR*)PerlSIO_get_base(stdio);
2686 PerlIOStdio_get_bufsiz(pTHX_ PerlIO *f)
2688 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2689 return PerlSIO_get_bufsiz(stdio);
2693 #ifdef USE_STDIO_PTR
2695 PerlIOStdio_get_ptr(pTHX_ PerlIO *f)
2697 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2698 return (STDCHAR*)PerlSIO_get_ptr(stdio);
2702 PerlIOStdio_get_cnt(pTHX_ PerlIO *f)
2704 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2705 return PerlSIO_get_cnt(stdio);
2709 PerlIOStdio_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
2711 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2713 #ifdef STDIO_PTR_LVALUE
2714 PerlSIO_set_ptr(stdio, (void*)ptr); /* LHS STDCHAR* cast non-portable */
2715 #ifdef STDIO_PTR_LVAL_SETS_CNT
2716 if (PerlSIO_get_cnt(stdio) != (cnt)) {
2717 assert(PerlSIO_get_cnt(stdio) == (cnt));
2720 #if (!defined(STDIO_PTR_LVAL_NOCHANGE_CNT))
2722 * Setting ptr _does_ change cnt - we are done
2726 #else /* STDIO_PTR_LVALUE */
2728 #endif /* STDIO_PTR_LVALUE */
2731 * Now (or only) set cnt
2733 #ifdef STDIO_CNT_LVALUE
2734 PerlSIO_set_cnt(stdio, cnt);
2735 #else /* STDIO_CNT_LVALUE */
2736 #if (defined(STDIO_PTR_LVALUE) && defined(STDIO_PTR_LVAL_SETS_CNT))
2737 PerlSIO_set_ptr(stdio,
2738 PerlSIO_get_ptr(stdio) + (PerlSIO_get_cnt(stdio) -
2740 #else /* STDIO_PTR_LVAL_SETS_CNT */
2742 #endif /* STDIO_PTR_LVAL_SETS_CNT */
2743 #endif /* STDIO_CNT_LVALUE */
2748 PerlIO_funcs PerlIO_stdio = {
2750 sizeof(PerlIOStdio),
2768 PerlIOStdio_clearerr,
2769 PerlIOStdio_setlinebuf,
2771 PerlIOStdio_get_base,
2772 PerlIOStdio_get_bufsiz,
2777 #ifdef USE_STDIO_PTR
2778 PerlIOStdio_get_ptr,
2779 PerlIOStdio_get_cnt,
2780 #if (defined(STDIO_PTR_LVALUE) && (defined(STDIO_CNT_LVALUE) || defined(STDIO_PTR_LVAL_SETS_CNT)))
2781 PerlIOStdio_set_ptrcnt
2782 #else /* STDIO_PTR_LVALUE */
2784 #endif /* STDIO_PTR_LVALUE */
2785 #else /* USE_STDIO_PTR */
2789 #endif /* USE_STDIO_PTR */
2793 PerlIO_exportFILE(PerlIO *f, int fl)
2798 stdio = fdopen(PerlIO_fileno(f), "r+");
2801 PerlIOSelf(PerlIO_push(aTHX_ f, &PerlIO_stdio, "r+", Nullsv),
2809 PerlIO_findFILE(PerlIO *f)
2813 if (l->tab == &PerlIO_stdio) {
2814 PerlIOStdio *s = PerlIOSelf(&l, PerlIOStdio);
2817 l = *PerlIONext(&l);
2819 return PerlIO_exportFILE(f, 0);
2823 PerlIO_releaseFILE(PerlIO *p, FILE *f)
2827 /*--------------------------------------------------------------------------------------*/
2829 * perlio buffer layer
2833 PerlIOBuf_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
2835 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
2836 int fd = PerlIO_fileno(f);
2838 if (fd >= 0 && PerlLIO_isatty(fd)) {
2839 PerlIOBase(f)->flags |= PERLIO_F_LINEBUF | PERLIO_F_TTY;
2841 posn = PerlIO_tell(PerlIONext(f));
2842 if (posn != (Off_t) - 1) {
2845 return PerlIOBase_pushed(aTHX_ f, mode, arg);
2849 PerlIOBuf_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2850 IV n, const char *mode, int fd, int imode, int perm,
2851 PerlIO *f, int narg, SV **args)
2853 if (PerlIOValid(f)) {
2854 PerlIO *next = PerlIONext(f);
2855 PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIOBase(next)->tab);
2856 next = (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
2858 if (!next || (*PerlIOBase(f)->tab->Pushed) (aTHX_ f, mode, PerlIOArg) != 0) {
2863 PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIO_default_btm());
2871 f = (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
2874 if (PerlIO_push(aTHX_ f, self, mode, PerlIOArg) == 0) {
2876 * if push fails during open, open fails. close will pop us.
2881 fd = PerlIO_fileno(f);
2882 #ifdef PERLIO_USING_CRLF
2884 * do something about failing setmode()? --jhi
2886 PerlLIO_setmode(fd, O_BINARY);
2888 if (init && fd == 2) {
2890 * Initial stderr is unbuffered
2892 PerlIOBase(f)->flags |= PERLIO_F_UNBUF;
2901 * This "flush" is akin to sfio's sync in that it handles files in either
2902 * read or write state
2905 PerlIOBuf_flush(pTHX_ PerlIO *f)
2907 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
2909 PerlIO *n = PerlIONext(f);
2910 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF) {
2912 * write() the buffer
2914 STDCHAR *buf = b->buf;
2916 while (p < b->ptr) {
2917 SSize_t count = PerlIO_write(n, p, b->ptr - p);
2921 else if (count < 0 || PerlIO_error(n)) {
2922 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2927 b->posn += (p - buf);
2929 else if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
2930 STDCHAR *buf = PerlIO_get_base(f);
2932 * Note position change
2934 b->posn += (b->ptr - buf);
2935 if (b->ptr < b->end) {
2937 * We did not consume all of it
2939 if (PerlIO_seek(n, b->posn, SEEK_SET) == 0) {
2940 /* Reload n as some layers may pop themselves on seek */
2941 b->posn = PerlIO_tell(n = PerlIONext(f));
2945 b->ptr = b->end = b->buf;
2946 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
2947 /* We check for Valid because of dubious decision to make PerlIO_flush(NULL) flush all */
2948 /* FIXME: Doing downstream flush may be sub-optimal see PerlIOBuf_fill() below */
2949 if (PerlIOValid(n) && PerlIO_flush(n) != 0)
2955 PerlIOBuf_fill(pTHX_ PerlIO *f)
2957 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
2958 PerlIO *n = PerlIONext(f);
2961 * FIXME: doing the down-stream flush maybe sub-optimal if it causes
2962 * pre-read data in stdio buffer to be discarded.
2963 * However, skipping the flush also skips _our_ hosekeeping
2964 * and breaks tell tests. So we do the flush.
2966 if (PerlIO_flush(f) != 0)
2968 if (PerlIOBase(f)->flags & PERLIO_F_TTY)
2969 PerlIOBase_flush_linebuf(aTHX);
2972 PerlIO_get_base(f); /* allocate via vtable */
2974 b->ptr = b->end = b->buf;
2975 if (PerlIO_fast_gets(n)) {
2977 * Layer below is also buffered. We do _NOT_ want to call its
2978 * ->Read() because that will loop till it gets what we asked for
2979 * which may hang on a pipe etc. Instead take anything it has to
2980 * hand, or ask it to fill _once_.
2982 avail = PerlIO_get_cnt(n);
2984 avail = PerlIO_fill(n);
2986 avail = PerlIO_get_cnt(n);
2988 if (!PerlIO_error(n) && PerlIO_eof(n))
2993 STDCHAR *ptr = PerlIO_get_ptr(n);
2994 SSize_t cnt = avail;
2995 if (avail > b->bufsiz)
2997 Copy(ptr, b->buf, avail, STDCHAR);
2998 PerlIO_set_ptrcnt(n, ptr + avail, cnt - avail);
3002 avail = PerlIO_read(n, b->ptr, b->bufsiz);
3006 PerlIOBase(f)->flags |= PERLIO_F_EOF;
3008 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
3011 b->end = b->buf + avail;
3012 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3017 PerlIOBuf_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
3019 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3020 if (PerlIOValid(f)) {
3023 return PerlIOBase_read(aTHX_ f, vbuf, count);
3029 PerlIOBuf_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3031 const STDCHAR *buf = (const STDCHAR *) vbuf + count;
3032 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3035 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
3040 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
3042 * Buffer is already a read buffer, we can overwrite any chars
3043 * which have been read back to buffer start
3045 avail = (b->ptr - b->buf);
3049 * Buffer is idle, set it up so whole buffer is available for
3053 b->end = b->buf + avail;
3055 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3057 * Buffer extends _back_ from where we are now
3059 b->posn -= b->bufsiz;
3061 if (avail > (SSize_t) count) {
3063 * If we have space for more than count, just move count
3071 * In simple stdio-like ungetc() case chars will be already
3074 if (buf != b->ptr) {
3075 Copy(buf, b->ptr, avail, STDCHAR);
3079 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
3086 PerlIOBuf_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3088 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3089 const STDCHAR *buf = (const STDCHAR *) vbuf;
3093 if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
3096 SSize_t avail = b->bufsiz - (b->ptr - b->buf);
3097 if ((SSize_t) count < avail)
3099 PerlIOBase(f)->flags |= PERLIO_F_WRBUF;
3100 if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF) {
3115 Copy(buf, b->ptr, avail, STDCHAR);
3122 if (b->ptr >= (b->buf + b->bufsiz))
3125 if (PerlIOBase(f)->flags & PERLIO_F_UNBUF)
3131 PerlIOBuf_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
3134 if ((code = PerlIO_flush(f)) == 0) {
3135 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3136 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
3137 code = PerlIO_seek(PerlIONext(f), offset, whence);
3139 b->posn = PerlIO_tell(PerlIONext(f));
3146 PerlIOBuf_tell(pTHX_ PerlIO *f)
3148 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3150 * b->posn is file position where b->buf was read, or will be written
3152 Off_t posn = b->posn;
3155 * If buffer is valid adjust position by amount in buffer
3157 posn += (b->ptr - b->buf);
3163 PerlIOBuf_close(pTHX_ PerlIO *f)
3165 IV code = PerlIOBase_close(aTHX_ f);
3166 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3167 if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
3171 b->ptr = b->end = b->buf;
3172 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
3177 PerlIOBuf_get_ptr(pTHX_ PerlIO *f)
3179 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3186 PerlIOBuf_get_cnt(pTHX_ PerlIO *f)
3188 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3191 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF)
3192 return (b->end - b->ptr);
3197 PerlIOBuf_get_base(pTHX_ PerlIO *f)
3199 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3204 Newz('B',b->buf,b->bufsiz, STDCHAR);
3206 b->buf = (STDCHAR *) & b->oneword;
3207 b->bufsiz = sizeof(b->oneword);
3216 PerlIOBuf_bufsiz(pTHX_ PerlIO *f)
3218 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3221 return (b->end - b->buf);
3225 PerlIOBuf_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
3227 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3231 if (PerlIO_get_cnt(f) != cnt || b->ptr < b->buf) {
3232 assert(PerlIO_get_cnt(f) == cnt);
3233 assert(b->ptr >= b->buf);
3235 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3239 PerlIOBuf_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
3241 return PerlIOBase_dup(aTHX_ f, o, param, flags);
3246 PerlIO_funcs PerlIO_perlio = {
3266 PerlIOBase_clearerr,
3267 PerlIOBase_setlinebuf,
3272 PerlIOBuf_set_ptrcnt,
3275 /*--------------------------------------------------------------------------------------*/
3277 * Temp layer to hold unread chars when cannot do it any other way
3281 PerlIOPending_fill(pTHX_ PerlIO *f)
3284 * Should never happen
3291 PerlIOPending_close(pTHX_ PerlIO *f)
3294 * A tad tricky - flush pops us, then we close new top
3297 return PerlIO_close(f);
3301 PerlIOPending_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
3304 * A tad tricky - flush pops us, then we seek new top
3307 return PerlIO_seek(f, offset, whence);
3312 PerlIOPending_flush(pTHX_ PerlIO *f)
3314 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3315 if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
3319 PerlIO_pop(aTHX_ f);
3324 PerlIOPending_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
3330 PerlIOBuf_set_ptrcnt(aTHX_ f, ptr, cnt);
3335 PerlIOPending_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
3337 IV code = PerlIOBase_pushed(aTHX_ f, mode, arg);
3338 PerlIOl *l = PerlIOBase(f);
3340 * Our PerlIO_fast_gets must match what we are pushed on, or sv_gets()
3341 * etc. get muddled when it changes mid-string when we auto-pop.
3343 l->flags = (l->flags & ~(PERLIO_F_FASTGETS | PERLIO_F_UTF8)) |
3344 (PerlIOBase(PerlIONext(f))->
3345 flags & (PERLIO_F_FASTGETS | PERLIO_F_UTF8));
3350 PerlIOPending_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
3352 SSize_t avail = PerlIO_get_cnt(f);
3357 got = PerlIOBuf_read(aTHX_ f, vbuf, avail);
3358 if (got >= 0 && got < count) {
3360 PerlIO_read(f, ((STDCHAR *) vbuf) + got, count - got);
3361 if (more >= 0 || got == 0)
3367 PerlIO_funcs PerlIO_pending = {
3371 PerlIOPending_pushed,
3382 PerlIOPending_close,
3383 PerlIOPending_flush,
3387 PerlIOBase_clearerr,
3388 PerlIOBase_setlinebuf,
3393 PerlIOPending_set_ptrcnt,
3398 /*--------------------------------------------------------------------------------------*/
3400 * crlf - translation On read translate CR,LF to "\n" we do this by
3401 * overriding ptr/cnt entries to hand back a line at a time and keeping a
3402 * record of which nl we "lied" about. On write translate "\n" to CR,LF
3406 PerlIOBuf base; /* PerlIOBuf stuff */
3407 STDCHAR *nl; /* Position of crlf we "lied" about in the
3412 PerlIOCrlf_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
3415 PerlIOBase(f)->flags |= PERLIO_F_CRLF;
3416 code = PerlIOBuf_pushed(aTHX_ f, mode, arg);
3418 PerlIO_debug("PerlIOCrlf_pushed f=%p %s %s fl=%08" UVxf "\n",
3419 f, PerlIOBase(f)->tab->name, (mode) ? mode : "(Null)",
3420 PerlIOBase(f)->flags);
3427 PerlIOCrlf_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3429 PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
3434 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF))
3435 return PerlIOBuf_unread(aTHX_ f, vbuf, count);
3437 const STDCHAR *buf = (const STDCHAR *) vbuf + count;
3438 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3440 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
3445 if (!(PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
3446 b->end = b->ptr = b->buf + b->bufsiz;
3447 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3448 b->posn -= b->bufsiz;
3450 while (count > 0 && b->ptr > b->buf) {
3453 if (b->ptr - 2 >= b->buf) {
3476 PerlIOCrlf_get_cnt(pTHX_ PerlIO *f)
3478 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3481 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
3482 PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
3483 if ((PerlIOBase(f)->flags & PERLIO_F_CRLF) && !c->nl) {
3484 STDCHAR *nl = b->ptr;
3486 while (nl < b->end && *nl != 0xd)
3488 if (nl < b->end && *nl == 0xd) {
3490 if (nl + 1 < b->end) {
3497 * Not CR,LF but just CR
3505 * Blast - found CR as last char in buffer
3510 * They may not care, defer work as long as
3514 return (nl - b->ptr);
3518 b->ptr++; /* say we have read it as far as
3519 * flush() is concerned */
3520 b->buf++; /* Leave space in front of buffer */
3521 b->bufsiz--; /* Buffer is thus smaller */
3522 code = PerlIO_fill(f); /* Fetch some more */
3523 b->bufsiz++; /* Restore size for next time */
3524 b->buf--; /* Point at space */
3525 b->ptr = nl = b->buf; /* Which is what we hand
3527 b->posn--; /* Buffer starts here */
3528 *nl = 0xd; /* Fill in the CR */
3530 goto test; /* fill() call worked */
3532 * CR at EOF - just fall through
3534 /* Should we clear EOF though ??? */
3539 return (((c->nl) ? (c->nl + 1) : b->end) - b->ptr);
3545 PerlIOCrlf_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
3547 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3548 PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
3549 IV flags = PerlIOBase(f)->flags;
3555 if (ptr == b->end && *c->nl == 0xd) {
3556 /* Defered CR at end of buffer case - we lied about count */
3567 * Test code - delete when it works ...
3569 STDCHAR *chk = (c->nl) ? (c->nl+1) : b->end;
3570 if (ptr+cnt == c->nl && c->nl+1 == b->end && *c->nl == 0xd) {
3571 /* Defered CR at end of buffer case - we lied about count */
3577 Perl_warn(aTHX_ "ptr wrong %p != %p fl=%08" UVxf
3578 " nl=%p e=%p for %d", ptr, chk, flags, c->nl,
3585 * They have taken what we lied about
3593 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3597 PerlIOCrlf_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3599 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF))
3600 return PerlIOBuf_write(aTHX_ f, vbuf, count);
3602 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3603 const STDCHAR *buf = (const STDCHAR *) vbuf;
3604 const STDCHAR *ebuf = buf + count;
3607 if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
3609 while (buf < ebuf) {
3610 STDCHAR *eptr = b->buf + b->bufsiz;
3611 PerlIOBase(f)->flags |= PERLIO_F_WRBUF;
3612 while (buf < ebuf && b->ptr < eptr) {
3614 if ((b->ptr + 2) > eptr) {
3622 *(b->ptr)++ = 0xd; /* CR */
3623 *(b->ptr)++ = 0xa; /* LF */
3625 if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF) {
3635 if (b->ptr >= eptr) {
3641 if (PerlIOBase(f)->flags & PERLIO_F_UNBUF)
3643 return (buf - (STDCHAR *) vbuf);
3648 PerlIOCrlf_flush(pTHX_ PerlIO *f)
3650 PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
3655 return PerlIOBuf_flush(aTHX_ f);
3658 PerlIO_funcs PerlIO_crlf = {
3661 PERLIO_K_BUFFERED | PERLIO_K_CANCRLF,
3663 PerlIOBase_noop_ok, /* popped */
3668 PerlIOBuf_read, /* generic read works with ptr/cnt lies
3670 PerlIOCrlf_unread, /* Put CR,LF in buffer for each '\n' */
3671 PerlIOCrlf_write, /* Put CR,LF in buffer for each '\n' */
3679 PerlIOBase_clearerr,
3680 PerlIOBase_setlinebuf,
3685 PerlIOCrlf_set_ptrcnt,
3689 /*--------------------------------------------------------------------------------------*/
3691 * mmap as "buffer" layer
3695 PerlIOBuf base; /* PerlIOBuf stuff */
3696 Mmap_t mptr; /* Mapped address */
3697 Size_t len; /* mapped length */
3698 STDCHAR *bbuf; /* malloced buffer if map fails */
3701 static size_t page_size = 0;
3704 PerlIOMmap_map(pTHX_ PerlIO *f)
3706 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3707 IV flags = PerlIOBase(f)->flags;
3711 if (flags & PERLIO_F_CANREAD) {
3712 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3713 int fd = PerlIO_fileno(f);
3715 code = Fstat(fd, &st);
3716 if (code == 0 && S_ISREG(st.st_mode)) {
3717 SSize_t len = st.st_size - b->posn;
3721 #if defined(HAS_SYSCONF) && (defined(_SC_PAGESIZE) || defined(_SC_PAGE_SIZE))
3723 SETERRNO(0, SS$_NORMAL);
3724 # ifdef _SC_PAGESIZE
3725 page_size = sysconf(_SC_PAGESIZE);
3727 page_size = sysconf(_SC_PAGE_SIZE);
3729 if ((long) page_size < 0) {
3734 (void) SvUPGRADE(error, SVt_PV);
3735 msg = SvPVx(error, n_a);
3736 Perl_croak(aTHX_ "panic: sysconf: %s",
3741 "panic: sysconf: pagesize unknown");
3745 # ifdef HAS_GETPAGESIZE
3746 page_size = getpagesize();
3748 # if defined(I_SYS_PARAM) && defined(PAGESIZE)
3749 page_size = PAGESIZE; /* compiletime, bad */
3753 if ((IV) page_size <= 0)
3754 Perl_croak(aTHX_ "panic: bad pagesize %" IVdf,
3759 * This is a hack - should never happen - open should
3762 b->posn = PerlIO_tell(PerlIONext(f));
3764 posn = (b->posn / page_size) * page_size;
3765 len = st.st_size - posn;
3766 m->mptr = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, posn);
3767 if (m->mptr && m->mptr != (Mmap_t) - 1) {
3768 #if 0 && defined(HAS_MADVISE) && defined(MADV_SEQUENTIAL)
3769 madvise(m->mptr, len, MADV_SEQUENTIAL);
3771 #if 0 && defined(HAS_MADVISE) && defined(MADV_WILLNEED)
3772 madvise(m->mptr, len, MADV_WILLNEED);
3774 PerlIOBase(f)->flags =
3775 (flags & ~PERLIO_F_EOF) | PERLIO_F_RDBUF;
3776 b->end = ((STDCHAR *) m->mptr) + len;
3777 b->buf = ((STDCHAR *) m->mptr) + (b->posn - posn);
3786 PerlIOBase(f)->flags =
3787 flags | PERLIO_F_EOF | PERLIO_F_RDBUF;
3789 b->ptr = b->end = b->ptr;
3798 PerlIOMmap_unmap(pTHX_ PerlIO *f)
3800 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3801 PerlIOBuf *b = &m->base;
3805 code = munmap(m->mptr, m->len);
3809 if (PerlIO_seek(PerlIONext(f), b->posn, SEEK_SET) != 0)
3812 b->ptr = b->end = b->buf;
3813 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
3819 PerlIOMmap_get_base(pTHX_ PerlIO *f)
3821 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3822 PerlIOBuf *b = &m->base;
3823 if (b->buf && (PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
3825 * Already have a readbuffer in progress
3831 * We have a write buffer or flushed PerlIOBuf read buffer
3833 m->bbuf = b->buf; /* save it in case we need it again */
3834 b->buf = NULL; /* Clear to trigger below */
3837 PerlIOMmap_map(aTHX_ f); /* Try and map it */
3840 * Map did not work - recover PerlIOBuf buffer if we have one
3845 b->ptr = b->end = b->buf;
3848 return PerlIOBuf_get_base(aTHX_ f);
3852 PerlIOMmap_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3854 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3855 PerlIOBuf *b = &m->base;
3856 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
3858 if (b->ptr && (b->ptr - count) >= b->buf
3859 && memEQ(b->ptr - count, vbuf, count)) {
3861 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
3866 * Loose the unwritable mapped buffer
3870 * If flush took the "buffer" see if we have one from before
3872 if (!b->buf && m->bbuf)
3875 PerlIOBuf_get_base(aTHX_ f);
3879 return PerlIOBuf_unread(aTHX_ f, vbuf, count);
3883 PerlIOMmap_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3885 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3886 PerlIOBuf *b = &m->base;
3887 if (!b->buf || !(PerlIOBase(f)->flags & PERLIO_F_WRBUF)) {
3889 * No, or wrong sort of, buffer
3892 if (PerlIOMmap_unmap(aTHX_ f) != 0)
3896 * If unmap took the "buffer" see if we have one from before
3898 if (!b->buf && m->bbuf)
3901 PerlIOBuf_get_base(aTHX_ f);
3905 return PerlIOBuf_write(aTHX_ f, vbuf, count);
3909 PerlIOMmap_flush(pTHX_ PerlIO *f)
3911 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3912 PerlIOBuf *b = &m->base;
3913 IV code = PerlIOBuf_flush(aTHX_ f);
3915 * Now we are "synced" at PerlIOBuf level
3922 if (PerlIOMmap_unmap(aTHX_ f) != 0)
3927 * We seem to have a PerlIOBuf buffer which was not mapped
3928 * remember it in case we need one later
3937 PerlIOMmap_fill(pTHX_ PerlIO *f)
3939 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3940 IV code = PerlIO_flush(f);
3941 if (code == 0 && !b->buf) {
3942 code = PerlIOMmap_map(aTHX_ f);
3944 if (code == 0 && !(PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
3945 code = PerlIOBuf_fill(aTHX_ f);
3951 PerlIOMmap_close(pTHX_ PerlIO *f)
3953 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3954 PerlIOBuf *b = &m->base;
3955 IV code = PerlIO_flush(f);
3959 b->ptr = b->end = b->buf;
3961 if (PerlIOBuf_close(aTHX_ f) != 0)
3967 PerlIOMmap_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
3969 return PerlIOBase_dup(aTHX_ f, o, param, flags);
3973 PerlIO_funcs PerlIO_mmap = {
3993 PerlIOBase_clearerr,
3994 PerlIOBase_setlinebuf,
3995 PerlIOMmap_get_base,
3999 PerlIOBuf_set_ptrcnt,
4002 #endif /* HAS_MMAP */
4005 Perl_PerlIO_stdin(pTHX)
4008 PerlIO_stdstreams(aTHX);
4010 return &PL_perlio[1];
4014 Perl_PerlIO_stdout(pTHX)
4017 PerlIO_stdstreams(aTHX);
4019 return &PL_perlio[2];
4023 Perl_PerlIO_stderr(pTHX)
4026 PerlIO_stdstreams(aTHX);
4028 return &PL_perlio[3];
4031 /*--------------------------------------------------------------------------------------*/
4034 PerlIO_getname(PerlIO *f, char *buf)
4039 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
4041 name = fgetname(stdio, buf);
4043 Perl_croak(aTHX_ "Don't know how to get file name");
4049 /*--------------------------------------------------------------------------------------*/
4051 * Functions which can be called on any kind of PerlIO implemented in
4055 #undef PerlIO_fdopen
4057 PerlIO_fdopen(int fd, const char *mode)
4060 return PerlIO_openn(aTHX_ Nullch, mode, fd, 0, 0, NULL, 0, NULL);
4065 PerlIO_open(const char *path, const char *mode)
4068 SV *name = sv_2mortal(newSVpvn(path, strlen(path)));
4069 return PerlIO_openn(aTHX_ Nullch, mode, -1, 0, 0, NULL, 1, &name);
4072 #undef Perlio_reopen
4074 PerlIO_reopen(const char *path, const char *mode, PerlIO *f)
4077 SV *name = sv_2mortal(newSVpvn(path, strlen(path)));
4078 return PerlIO_openn(aTHX_ Nullch, mode, -1, 0, 0, f, 1, &name);
4083 PerlIO_getc(PerlIO *f)
4087 SSize_t count = PerlIO_read(f, buf, 1);
4089 return (unsigned char) buf[0];
4094 #undef PerlIO_ungetc
4096 PerlIO_ungetc(PerlIO *f, int ch)
4101 if (PerlIO_unread(f, &buf, 1) == 1)
4109 PerlIO_putc(PerlIO *f, int ch)
4113 return PerlIO_write(f, &buf, 1);
4118 PerlIO_puts(PerlIO *f, const char *s)
4121 STRLEN len = strlen(s);
4122 return PerlIO_write(f, s, len);
4125 #undef PerlIO_rewind
4127 PerlIO_rewind(PerlIO *f)
4130 PerlIO_seek(f, (Off_t) 0, SEEK_SET);
4134 #undef PerlIO_vprintf
4136 PerlIO_vprintf(PerlIO *f, const char *fmt, va_list ap)
4139 SV *sv = newSVpvn("", 0);
4145 Perl_va_copy(ap, apc);
4146 sv_vcatpvf(sv, fmt, &apc);
4148 sv_vcatpvf(sv, fmt, &ap);
4151 wrote = PerlIO_write(f, s, len);
4156 #undef PerlIO_printf
4158 PerlIO_printf(PerlIO *f, const char *fmt, ...)
4163 result = PerlIO_vprintf(f, fmt, ap);
4168 #undef PerlIO_stdoutf
4170 PerlIO_stdoutf(const char *fmt, ...)
4176 result = PerlIO_vprintf(PerlIO_stdout(), fmt, ap);
4181 #undef PerlIO_tmpfile
4183 PerlIO_tmpfile(void)
4186 * I have no idea how portable mkstemp() is ...
4188 #if defined(WIN32) || !defined(HAVE_MKSTEMP)
4191 FILE *stdio = PerlSIO_tmpfile();
4194 PerlIOSelf(PerlIO_push
4195 (aTHX_(f = PerlIO_allocate(aTHX)), &PerlIO_stdio,
4196 "w+", Nullsv), PerlIOStdio);
4202 SV *sv = newSVpv("/tmp/PerlIO_XXXXXX", 0);
4203 int fd = mkstemp(SvPVX(sv));
4206 f = PerlIO_fdopen(fd, "w+");
4208 PerlIOBase(f)->flags |= PERLIO_F_TEMP;
4210 PerlLIO_unlink(SvPVX(sv));
4220 #endif /* USE_SFIO */
4221 #endif /* PERLIO_IS_STDIO */
4223 /*======================================================================================*/
4225 * Now some functions in terms of above which may be needed even if we are
4226 * not in true PerlIO mode
4230 #undef PerlIO_setpos
4232 PerlIO_setpos(PerlIO *f, SV *pos)
4237 Off_t *posn = (Off_t *) SvPV(pos, len);
4238 if (f && len == sizeof(Off_t))
4239 return PerlIO_seek(f, *posn, SEEK_SET);
4241 SETERRNO(EINVAL, SS$_IVCHAN);
4245 #undef PerlIO_setpos
4247 PerlIO_setpos(PerlIO *f, SV *pos)
4252 Fpos_t *fpos = (Fpos_t *) SvPV(pos, len);
4253 if (f && len == sizeof(Fpos_t)) {
4254 #if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64)
4255 return fsetpos64(f, fpos);
4257 return fsetpos(f, fpos);
4261 SETERRNO(EINVAL, SS$_IVCHAN);
4267 #undef PerlIO_getpos
4269 PerlIO_getpos(PerlIO *f, SV *pos)
4272 Off_t posn = PerlIO_tell(f);
4273 sv_setpvn(pos, (char *) &posn, sizeof(posn));
4274 return (posn == (Off_t) - 1) ? -1 : 0;
4277 #undef PerlIO_getpos
4279 PerlIO_getpos(PerlIO *f, SV *pos)
4284 #if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64)
4285 code = fgetpos64(f, &fpos);
4287 code = fgetpos(f, &fpos);
4289 sv_setpvn(pos, (char *) &fpos, sizeof(fpos));
4294 #if (defined(PERLIO_IS_STDIO) || !defined(USE_SFIO)) && !defined(HAS_VPRINTF)
4297 vprintf(char *pat, char *args)
4299 _doprnt(pat, args, stdout);
4300 return 0; /* wrong, but perl doesn't use the return
4305 vfprintf(FILE *fd, char *pat, char *args)
4307 _doprnt(pat, args, fd);
4308 return 0; /* wrong, but perl doesn't use the return
4314 #ifndef PerlIO_vsprintf
4316 PerlIO_vsprintf(char *s, int n, const char *fmt, va_list ap)
4318 int val = vsprintf(s, fmt, ap);
4320 if (strlen(s) >= (STRLEN) n) {
4322 (void) PerlIO_puts(Perl_error_log,
4323 "panic: sprintf overflow - memory corrupted!\n");
4331 #ifndef PerlIO_sprintf
4333 PerlIO_sprintf(char *s, int n, const char *fmt, ...)
4338 result = PerlIO_vsprintf(s, n, fmt, ap);