2 * perlio.c Copyright (c) 1996-2002, Nick Ing-Simmons You may distribute
3 * under the terms of either the GNU General Public License or the
4 * Artistic License, as specified in the README file.
8 * Hour after hour for nearly three weary days he had jogged up and down,
9 * over passes, and through long dales, and across many streams.
13 * If we have ActivePerl-like PERL_IMPLICIT_SYS then we need a dTHX to get
14 * at the dispatch tables, even when we do not need it for other reasons.
15 * Invent a dSYS macro to abstract this out
17 #ifdef PERL_IMPLICIT_SYS
30 #define PERLIO_NOT_STDIO 0
31 #if !defined(PERLIO_IS_STDIO) && !defined(USE_SFIO)
37 * This file provides those parts of PerlIO abstraction
38 * which are not #defined in perlio.h.
39 * Which these are depends on various Configure #ifdef's
43 #define PERL_IN_PERLIO_C
46 #ifdef PERL_IMPLICIT_CONTEXT
54 perlsio_binmode(FILE *fp, int iotype, int mode)
57 * This used to be contents of do_binmode in doio.c
60 # if defined(atarist) || defined(__MINT__)
63 ((FILE *) fp)->_flag |= _IOBIN;
65 ((FILE *) fp)->_flag &= ~_IOBIN;
72 if (PerlLIO_setmode(fp, mode) != -1) {
74 if (PerlLIO_setmode(fileno(fp), mode) != -1) {
76 # if defined(WIN32) && defined(__BORLANDC__)
78 * The translation mode of the stream is maintained independent of
79 * the translation mode of the fd in the Borland RTL (heavy
80 * digging through their runtime sources reveal). User has to set
81 * the mode explicitly for the stream (though they don't document
82 * this anywhere). GSAR 97-5-24
96 # if defined(USEMYBINMODE)
98 if (my_binmode(fp, iotype, mode) != FALSE)
109 #define O_ACCMODE 3 /* Assume traditional implementation */
113 PerlIO_intmode2str(int rawmode, char *mode, int *writing)
115 int result = rawmode & O_ACCMODE;
120 ptype = IoTYPE_RDONLY;
123 ptype = IoTYPE_WRONLY;
131 *writing = (result != O_RDONLY);
133 if (result == O_RDONLY) {
137 else if (rawmode & O_APPEND) {
139 if (result != O_WRONLY)
144 if (result == O_WRONLY)
151 if (rawmode & O_BINARY)
157 #ifndef PERLIO_LAYERS
159 PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names)
161 if (!names || !*names || strEQ(names, ":crlf") || strEQ(names, ":raw")) {
164 Perl_croak(aTHX_ "Cannot apply \"%s\" in non-PerlIO perl", names);
172 PerlIO_destruct(pTHX)
177 PerlIO_binmode(pTHX_ PerlIO *fp, int iotype, int mode, const char *names)
182 return perlsio_binmode(fp, iotype, mode);
187 PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
191 int fd = PerlLIO_dup(PerlIO_fileno(f));
194 int omode = fcntl(fd, F_GETFL);
196 omode = djgpp_get_stream_mode(f);
198 PerlIO_intmode2str(omode,mode,NULL);
199 /* the r+ is a hack */
200 return PerlIO_fdopen(fd, mode);
205 SETERRNO(EBADF, SS$_IVCHAN);
213 * De-mux PerlIO_openn() into fdopen, freopen and fopen type entries
217 PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd,
218 int imode, int perm, PerlIO *old, int narg, SV **args)
222 Perl_croak(aTHX_ "More than one argument to open");
224 if (*args == &PL_sv_undef)
225 return PerlIO_tmpfile();
227 char *name = SvPV_nolen(*args);
229 fd = PerlLIO_open3(name, imode, perm);
231 return PerlIO_fdopen(fd, (char *) mode + 1);
234 return PerlIO_reopen(name, mode, old);
237 return PerlIO_open(name, mode);
242 return PerlIO_fdopen(fd, (char *) mode);
247 XS(XS_PerlIO__Layer__find)
251 Perl_croak(aTHX_ "Usage class->find(name[,load])");
253 char *name = SvPV_nolen(ST(1));
254 ST(0) = (strEQ(name, "crlf")
255 || strEQ(name, "raw")) ? &PL_sv_yes : &PL_sv_undef;
262 Perl_boot_core_PerlIO(pTHX)
264 newXS("PerlIO::Layer::find", XS_PerlIO__Layer__find, __FILE__);
270 #ifdef PERLIO_IS_STDIO
276 * Does nothing (yet) except force this file to be included in perl
277 * binary. That allows this file to force inclusion of other functions
278 * that may be required by loadable extensions e.g. for
279 * FileHandle::tmpfile
283 #undef PerlIO_tmpfile
290 #else /* PERLIO_IS_STDIO */
298 * This section is just to make sure these functions get pulled in from
302 #undef PerlIO_tmpfile
313 * Force this file to be included in perl binary. Which allows this
314 * file to force inclusion of other functions that may be required by
315 * loadable extensions e.g. for FileHandle::tmpfile
319 * Hack sfio does its own 'autoflush' on stdout in common cases. Flush
320 * results in a lot of lseek()s to regular files and lot of small
323 sfset(sfstdout, SF_SHARE, 0);
327 PerlIO_importFILE(FILE *stdio, int fl)
329 int fd = fileno(stdio);
330 PerlIO *r = PerlIO_fdopen(fd, "r+");
335 PerlIO_findFILE(PerlIO *pio)
337 int fd = PerlIO_fileno(pio);
338 FILE *f = fdopen(fd, "r+");
340 if (!f && errno == EINVAL)
342 if (!f && errno == EINVAL)
349 /*======================================================================================*/
351 * Implement all the PerlIO interface ourselves.
357 * We _MUST_ have <unistd.h> if we are using lseek() and may have large
364 #include <sys/mman.h>
368 void PerlIO_debug(const char *fmt, ...)
369 __attribute__ ((format(__printf__, 1, 2)));
372 PerlIO_debug(const char *fmt, ...)
379 char *s = PerlEnv_getenv("PERLIO_DEBUG");
381 dbg = PerlLIO_open3(s, O_WRONLY | O_CREAT | O_APPEND, 0666);
388 /* Use fixed buffer as sv_catpvf etc. needs SVs */
392 s = CopFILE(PL_curcop);
395 sprintf(buffer, "%s:%" IVdf " ", s, (IV) CopLINE(PL_curcop));
396 len = strlen(buffer);
397 vsprintf(buffer+len, fmt, ap);
398 PerlLIO_write(dbg, buffer, strlen(buffer));
400 SV *sv = newSVpvn("", 0);
403 s = CopFILE(PL_curcop);
406 Perl_sv_catpvf(aTHX_ sv, "%s:%" IVdf " ", s,
407 (IV) CopLINE(PL_curcop));
408 Perl_sv_vcatpvf(aTHX_ sv, fmt, &ap);
411 PerlLIO_write(dbg, s, len);
418 /*--------------------------------------------------------------------------------------*/
421 * Inner level routines
425 * Table of pointers to the PerlIO structs (malloc'ed)
427 #define PERLIO_TABLE_SIZE 64
430 PerlIO_allocate(pTHX)
433 * Find a free slot in the table, allocating new table as necessary
438 while ((f = *last)) {
440 last = (PerlIO **) (f);
441 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
447 Newz('I',f,PERLIO_TABLE_SIZE,PerlIO);
455 #undef PerlIO_fdupopen
457 PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
459 if (PerlIOValid(f)) {
460 PerlIO_funcs *tab = PerlIOBase(f)->tab;
462 PerlIO_debug("fdupopen f=%p param=%p\n",(void*)f,(void*)param);
463 new = (*tab->Dup)(aTHX_ PerlIO_allocate(aTHX),f,param, flags);
467 SETERRNO(EBADF, SS$_IVCHAN);
473 PerlIO_cleantable(pTHX_ PerlIO **tablep)
475 PerlIO *table = *tablep;
478 PerlIO_cleantable(aTHX_(PerlIO **) & (table[0]));
479 for (i = PERLIO_TABLE_SIZE - 1; i > 0; i--) {
480 PerlIO *f = table + i;
492 PerlIO_list_alloc(pTHX)
495 Newz('L', list, 1, PerlIO_list_t);
501 PerlIO_list_free(pTHX_ PerlIO_list_t *list)
504 if (--list->refcnt == 0) {
507 for (i = 0; i < list->cur; i++) {
508 if (list->array[i].arg)
509 SvREFCNT_dec(list->array[i].arg);
511 Safefree(list->array);
519 PerlIO_list_push(pTHX_ PerlIO_list_t *list, PerlIO_funcs *funcs, SV *arg)
522 if (list->cur >= list->len) {
525 Renew(list->array, list->len, PerlIO_pair_t);
527 New('l', list->array, list->len, PerlIO_pair_t);
529 p = &(list->array[list->cur++]);
531 if ((p->arg = arg)) {
537 PerlIO_clone_list(pTHX_ PerlIO_list_t *proto, CLONE_PARAMS *param)
539 PerlIO_list_t *list = (PerlIO_list_t *) NULL;
542 list = PerlIO_list_alloc(aTHX);
543 for (i=0; i < proto->cur; i++) {
545 if (proto->array[i].arg)
546 arg = PerlIO_sv_dup(aTHX_ proto->array[i].arg,param);
547 PerlIO_list_push(aTHX_ list, proto->array[i].funcs, arg);
554 PerlIO_clone(pTHX_ PerlInterpreter *proto, CLONE_PARAMS *param)
557 PerlIO **table = &proto->Iperlio;
560 PL_known_layers = PerlIO_clone_list(aTHX_ proto->Iknown_layers, param);
561 PL_def_layerlist = PerlIO_clone_list(aTHX_ proto->Idef_layerlist, param);
562 PerlIO_allocate(aTHX); /* root slot is never used */
563 PerlIO_debug("Clone %p from %p\n",aTHX,proto);
564 while ((f = *table)) {
566 table = (PerlIO **) (f++);
567 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
569 (void) fp_dup(f, 0, param);
578 PerlIO_destruct(pTHX)
580 PerlIO **table = &PL_perlio;
583 PerlIO_debug("Destruct %p\n",aTHX);
585 while ((f = *table)) {
587 table = (PerlIO **) (f++);
588 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
592 if (l->tab->kind & PERLIO_K_DESTRUCT) {
593 PerlIO_debug("Destruct popping %s\n", l->tab->name);
604 PerlIO_list_free(aTHX_ PL_known_layers);
605 PL_known_layers = NULL;
606 PerlIO_list_free(aTHX_ PL_def_layerlist);
607 PL_def_layerlist = NULL;
611 PerlIO_pop(pTHX_ PerlIO *f)
615 PerlIO_debug("PerlIO_pop f=%p %s\n", (void*)f, l->tab->name);
616 if (l->tab->Popped) {
618 * If popped returns non-zero do not free its layer structure
619 * it has either done so itself, or it is shared and still in
622 if ((*l->tab->Popped) (aTHX_ f) != 0)
630 /*--------------------------------------------------------------------------------------*/
632 * XS Interface for perl code
636 PerlIO_find_layer(pTHX_ const char *name, STRLEN len, int load)
639 if ((SSize_t) len <= 0)
641 for (i = 0; i < PL_known_layers->cur; i++) {
642 PerlIO_funcs *f = PL_known_layers->array[i].funcs;
643 if (memEQ(f->name, name, len)) {
644 PerlIO_debug("%.*s => %p\n", (int) len, name, (void*)f);
648 if (load && PL_subname && PL_def_layerlist
649 && PL_def_layerlist->cur >= 2) {
650 SV *pkgsv = newSVpvn("PerlIO", 6);
651 SV *layer = newSVpvn(name, len);
654 * The two SVs are magically freed by load_module
656 Perl_load_module(aTHX_ 0, pkgsv, Nullsv, layer, Nullsv);
658 return PerlIO_find_layer(aTHX_ name, len, 0);
660 PerlIO_debug("Cannot find %.*s\n", (int) len, name);
664 #ifdef USE_ATTRIBUTES_FOR_PERLIO
667 perlio_mg_set(pTHX_ SV *sv, MAGIC *mg)
670 IO *io = GvIOn((GV *) SvRV(sv));
671 PerlIO *ifp = IoIFP(io);
672 PerlIO *ofp = IoOFP(io);
673 Perl_warn(aTHX_ "set %" SVf " %p %p %p", sv, io, ifp, ofp);
679 perlio_mg_get(pTHX_ SV *sv, MAGIC *mg)
682 IO *io = GvIOn((GV *) SvRV(sv));
683 PerlIO *ifp = IoIFP(io);
684 PerlIO *ofp = IoOFP(io);
685 Perl_warn(aTHX_ "get %" SVf " %p %p %p", sv, io, ifp, ofp);
691 perlio_mg_clear(pTHX_ SV *sv, MAGIC *mg)
693 Perl_warn(aTHX_ "clear %" SVf, sv);
698 perlio_mg_free(pTHX_ SV *sv, MAGIC *mg)
700 Perl_warn(aTHX_ "free %" SVf, sv);
704 MGVTBL perlio_vtab = {
712 XS(XS_io_MODIFY_SCALAR_ATTRIBUTES)
715 SV *sv = SvRV(ST(1));
720 sv_magic(sv, (SV *) av, PERL_MAGIC_ext, NULL, 0);
722 mg = mg_find(sv, PERL_MAGIC_ext);
723 mg->mg_virtual = &perlio_vtab;
725 Perl_warn(aTHX_ "attrib %" SVf, sv);
726 for (i = 2; i < items; i++) {
728 const char *name = SvPV(ST(i), len);
729 SV *layer = PerlIO_find_layer(aTHX_ name, len, 1);
731 av_push(av, SvREFCNT_inc(layer));
742 #endif /* USE_ATTIBUTES_FOR_PERLIO */
745 PerlIO_tab_sv(pTHX_ PerlIO_funcs *tab)
747 HV *stash = gv_stashpv("PerlIO::Layer", TRUE);
748 SV *sv = sv_bless(newRV_noinc(newSViv(PTR2IV(tab))), stash);
752 XS(XS_PerlIO__Layer__find)
756 Perl_croak(aTHX_ "Usage class->find(name[,load])");
759 char *name = SvPV(ST(1), len);
760 bool load = (items > 2) ? SvTRUE(ST(2)) : 0;
761 PerlIO_funcs *layer = PerlIO_find_layer(aTHX_ name, len, load);
763 (layer) ? sv_2mortal(PerlIO_tab_sv(aTHX_ layer)) :
770 PerlIO_define_layer(pTHX_ PerlIO_funcs *tab)
772 if (!PL_known_layers)
773 PL_known_layers = PerlIO_list_alloc(aTHX);
774 PerlIO_list_push(aTHX_ PL_known_layers, tab, Nullsv);
775 PerlIO_debug("define %s %p\n", tab->name, (void*)tab);
779 PerlIO_parse_layers(pTHX_ PerlIO_list_t *av, const char *names)
782 const char *s = names;
784 while (isSPACE(*s) || *s == ':')
789 const char *as = Nullch;
791 if (!isIDFIRST(*s)) {
793 * Message is consistent with how attribute lists are
794 * passed. Even though this means "foo : : bar" is
795 * seen as an invalid separator character.
797 char q = ((*s == '\'') ? '"' : '\'');
799 "perlio: invalid separator character %c%c%c in layer specification list %s",
805 } while (isALNUM(*e));
821 * It's a nul terminated string, not allowed
822 * to \ the terminating null. Anything other
823 * character is passed over.
834 "perlio: argument list not closed for layer \"%.*s\"",
846 PerlIO_funcs *layer =
847 PerlIO_find_layer(aTHX_ s, llen, 1);
849 PerlIO_list_push(aTHX_ av, layer,
855 Perl_warn(aTHX_ "perlio: unknown layer \"%.*s\"",
868 PerlIO_default_buffer(pTHX_ PerlIO_list_t *av)
870 PerlIO_funcs *tab = &PerlIO_perlio;
871 #ifdef PERLIO_USING_CRLF
874 if (PerlIO_stdio.Set_ptrcnt)
877 PerlIO_debug("Pushing %s\n", tab->name);
878 PerlIO_list_push(aTHX_ av, PerlIO_find_layer(aTHX_ tab->name, 0, 0),
883 PerlIO_arg_fetch(PerlIO_list_t *av, IV n)
885 return av->array[n].arg;
889 PerlIO_layer_fetch(pTHX_ PerlIO_list_t *av, IV n, PerlIO_funcs *def)
891 if (n >= 0 && n < av->cur) {
892 PerlIO_debug("Layer %" IVdf " is %s\n", n,
893 av->array[n].funcs->name);
894 return av->array[n].funcs;
897 Perl_croak(aTHX_ "panic: PerlIO layer array corrupt");
902 PerlIO_default_layers(pTHX)
904 if (!PL_def_layerlist) {
905 const char *s = (PL_tainting) ? Nullch : PerlEnv_getenv("PERLIO");
906 PerlIO_funcs *osLayer = &PerlIO_unix;
907 PL_def_layerlist = PerlIO_list_alloc(aTHX);
908 PerlIO_define_layer(aTHX_ & PerlIO_unix);
909 #if defined(WIN32) && !defined(UNDER_CE)
910 PerlIO_define_layer(aTHX_ & PerlIO_win32);
912 osLayer = &PerlIO_win32;
915 PerlIO_define_layer(aTHX_ & PerlIO_raw);
916 PerlIO_define_layer(aTHX_ & PerlIO_perlio);
917 PerlIO_define_layer(aTHX_ & PerlIO_stdio);
918 PerlIO_define_layer(aTHX_ & PerlIO_crlf);
920 PerlIO_define_layer(aTHX_ & PerlIO_mmap);
922 PerlIO_define_layer(aTHX_ & PerlIO_utf8);
923 PerlIO_define_layer(aTHX_ & PerlIO_byte);
924 PerlIO_list_push(aTHX_ PL_def_layerlist,
925 PerlIO_find_layer(aTHX_ osLayer->name, 0, 0),
928 PerlIO_parse_layers(aTHX_ PL_def_layerlist, s);
931 PerlIO_default_buffer(aTHX_ PL_def_layerlist);
934 if (PL_def_layerlist->cur < 2) {
935 PerlIO_default_buffer(aTHX_ PL_def_layerlist);
937 return PL_def_layerlist;
941 Perl_boot_core_PerlIO(pTHX)
943 #ifdef USE_ATTRIBUTES_FOR_PERLIO
944 newXS("io::MODIFY_SCALAR_ATTRIBUTES", XS_io_MODIFY_SCALAR_ATTRIBUTES,
947 newXS("PerlIO::Layer::find", XS_PerlIO__Layer__find, __FILE__);
951 PerlIO_default_layer(pTHX_ I32 n)
953 PerlIO_list_t *av = PerlIO_default_layers(aTHX);
956 return PerlIO_layer_fetch(aTHX_ av, n, &PerlIO_stdio);
959 #define PerlIO_default_top() PerlIO_default_layer(aTHX_ -1)
960 #define PerlIO_default_btm() PerlIO_default_layer(aTHX_ 0)
963 PerlIO_stdstreams(pTHX)
966 PerlIO_allocate(aTHX);
967 PerlIO_fdopen(0, "Ir" PERLIO_STDTEXT);
968 PerlIO_fdopen(1, "Iw" PERLIO_STDTEXT);
969 PerlIO_fdopen(2, "Iw" PERLIO_STDTEXT);
974 PerlIO_push(pTHX_ PerlIO *f, PerlIO_funcs *tab, const char *mode, SV *arg)
977 Newc('L',l,tab->size,char,PerlIOl);
979 Zero(l, tab->size, char);
983 PerlIO_debug("PerlIO_push f=%p %s %s %p\n", (void*)f, tab->name,
984 (mode) ? mode : "(Null)", (void*)arg);
985 if ((*l->tab->Pushed) (aTHX_ f, mode, arg) != 0) {
994 PerlIOPop_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
1006 PerlIORaw_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
1009 * Remove the dummy layer
1011 PerlIO_pop(aTHX_ f);
1013 * Pop back to bottom layer
1015 if (PerlIOValid(f)) {
1017 while (!(PerlIOBase(f)->tab->kind & PERLIO_K_RAW)) {
1018 if (*PerlIONext(f)) {
1019 PerlIO_pop(aTHX_ f);
1023 * Nothing bellow - push unix on top then remove it
1025 if (PerlIO_push(aTHX_ f, PerlIO_default_btm(), mode, arg)) {
1026 PerlIO_pop(aTHX_ PerlIONext(f));
1031 PerlIO_debug(":raw f=%p :%s\n", (void*)f, PerlIOBase(f)->tab->name);
1038 PerlIO_apply_layera(pTHX_ PerlIO *f, const char *mode,
1039 PerlIO_list_t *layers, IV n)
1041 IV max = layers->cur;
1044 PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n, NULL);
1046 if (!PerlIO_push(aTHX_ f, tab, mode, PerlIOArg)) {
1057 PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names)
1061 PerlIO_list_t *layers = PerlIO_list_alloc(aTHX);
1062 code = PerlIO_parse_layers(aTHX_ layers, names);
1064 code = PerlIO_apply_layera(aTHX_ f, mode, layers, 0);
1066 PerlIO_list_free(aTHX_ layers);
1072 /*--------------------------------------------------------------------------------------*/
1074 * Given the abstraction above the public API functions
1078 PerlIO_binmode(pTHX_ PerlIO *f, int iotype, int mode, const char *names)
1080 PerlIO_debug("PerlIO_binmode f=%p %s %c %x %s\n",
1081 (void*)f, PerlIOBase(f)->tab->name, iotype, mode,
1082 (names) ? names : "(Null)");
1084 /* Do not flush etc. if (e.g.) switching encodings.
1085 if a pushed layer knows it needs to flush lower layers
1086 (for example :unix which is never going to call them)
1087 it can do the flush when it is pushed.
1089 return PerlIO_apply_layers(aTHX_ f, NULL, names) == 0 ? TRUE : FALSE;
1092 /* FIXME?: Looking down the layer stack seems wrong,
1093 but is a way of reaching past (say) an encoding layer
1094 to flip CRLF-ness of the layer(s) below
1096 #ifdef PERLIO_USING_CRLF
1097 /* Legacy binmode only has meaning if O_TEXT has a value distinct from
1098 O_BINARY so we can look for it in mode.
1100 if (!(mode & O_BINARY)) {
1103 /* Perhaps we should turn on bottom-most aware layer
1104 e.g. Ilya's idea that UNIX TTY could serve
1106 if (PerlIOBase(f)->tab->kind & PERLIO_K_CANCRLF) {
1107 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF)) {
1108 /* Not in text mode - flush any pending stuff and flip it */
1110 PerlIOBase(f)->flags |= PERLIO_F_CRLF;
1112 /* Only need to turn it on in one layer so we are done */
1117 /* Not finding a CRLF aware layer presumably means we are binary
1118 which is not what was requested - so we failed
1119 We _could_ push :crlf layer but so could caller
1124 /* Either asked for BINMODE or that is normal on this platform
1125 see if any CRLF aware layers are present and turn off the flag
1126 and possibly remove layer.
1129 if (PerlIOBase(f)->tab->kind & PERLIO_K_CANCRLF) {
1130 if ((PerlIOBase(f)->flags & PERLIO_F_CRLF)) {
1131 /* In text mode - flush any pending stuff and flip it */
1133 PerlIOBase(f)->flags &= ~PERLIO_F_CRLF;
1134 #ifndef PERLIO_USING_CRLF
1135 /* CRLF is unusual case - if this is just the :crlf layer pop it */
1136 if (PerlIOBase(f)->tab == &PerlIO_crlf) {
1137 PerlIO_pop(aTHX_ f);
1140 /* Normal case is only one layer doing this, so exit on first
1141 abnormal case can always do multiple binmode calls
1153 PerlIO__close(pTHX_ PerlIO *f)
1156 return (*PerlIOBase(f)->tab->Close) (aTHX_ f);
1158 SETERRNO(EBADF, SS$_IVCHAN);
1164 Perl_PerlIO_close(pTHX_ PerlIO *f)
1167 if (PerlIOValid(f)) {
1168 code = (*PerlIOBase(f)->tab->Close) (aTHX_ f);
1170 PerlIO_pop(aTHX_ f);
1177 Perl_PerlIO_fileno(pTHX_ PerlIO *f)
1180 return (*PerlIOBase(f)->tab->Fileno) (aTHX_ f);
1182 SETERRNO(EBADF, SS$_IVCHAN);
1188 PerlIO_context_layers(pTHX_ const char *mode)
1190 const char *type = NULL;
1192 * Need to supply default layer info from open.pm
1195 SV *layers = PL_curcop->cop_io;
1198 type = SvPV(layers, len);
1199 if (type && mode[0] != 'r') {
1201 * Skip to write part
1203 const char *s = strchr(type, 0);
1204 if (s && (s - type) < len) {
1213 static PerlIO_funcs *
1214 PerlIO_layer_from_ref(pTHX_ SV *sv)
1217 * For any scalar type load the handler which is bundled with perl
1219 if (SvTYPE(sv) < SVt_PVAV)
1220 return PerlIO_find_layer(aTHX_ "Scalar", 6, 1);
1223 * For other types allow if layer is known but don't try and load it
1225 switch (SvTYPE(sv)) {
1227 return PerlIO_find_layer(aTHX_ "Array", 5, 0);
1229 return PerlIO_find_layer(aTHX_ "Hash", 4, 0);
1231 return PerlIO_find_layer(aTHX_ "Code", 4, 0);
1233 return PerlIO_find_layer(aTHX_ "Glob", 4, 0);
1239 PerlIO_resolve_layers(pTHX_ const char *layers,
1240 const char *mode, int narg, SV **args)
1242 PerlIO_list_t *def = PerlIO_default_layers(aTHX);
1245 PerlIO_stdstreams(aTHX);
1249 * If it is a reference but not an object see if we have a handler
1252 if (SvROK(arg) && !sv_isobject(arg)) {
1253 PerlIO_funcs *handler = PerlIO_layer_from_ref(aTHX_ SvRV(arg));
1255 def = PerlIO_list_alloc(aTHX);
1256 PerlIO_list_push(aTHX_ def, handler, &PL_sv_undef);
1260 * Don't fail if handler cannot be found :Via(...) etc. may do
1261 * something sensible else we will just stringfy and open
1267 layers = PerlIO_context_layers(aTHX_ mode);
1268 if (layers && *layers) {
1272 av = PerlIO_list_alloc(aTHX);
1273 for (i = 0; i < def->cur; i++) {
1274 PerlIO_list_push(aTHX_ av, def->array[i].funcs,
1281 PerlIO_parse_layers(aTHX_ av, layers);
1292 PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd,
1293 int imode, int perm, PerlIO *f, int narg, SV **args)
1295 if (!f && narg == 1 && *args == &PL_sv_undef) {
1296 if ((f = PerlIO_tmpfile())) {
1298 layers = PerlIO_context_layers(aTHX_ mode);
1299 if (layers && *layers)
1300 PerlIO_apply_layers(aTHX_ f, mode, layers);
1304 PerlIO_list_t *layera = NULL;
1306 PerlIO_funcs *tab = NULL;
1307 if (PerlIOValid(f)) {
1309 * This is "reopen" - it is not tested as perl does not use it
1313 layera = PerlIO_list_alloc(aTHX);
1315 SV *arg = (l->tab->Getarg)
1316 ? (*l->tab->Getarg) (aTHX_ &l, NULL, 0)
1318 PerlIO_list_push(aTHX_ layera, l->tab, arg);
1319 l = *PerlIONext(&l);
1323 layera = PerlIO_resolve_layers(aTHX_ layers, mode, narg, args);
1326 * Start at "top" of layer stack
1328 n = layera->cur - 1;
1330 PerlIO_funcs *t = PerlIO_layer_fetch(aTHX_ layera, n, NULL);
1339 * Found that layer 'n' can do opens - call it
1341 if (narg > 1 && !(tab->kind & PERLIO_K_MULTIARG)) {
1342 Perl_croak(aTHX_ "More than one argument to open(,':%s')",tab->name);
1344 PerlIO_debug("openn(%s,'%s','%s',%d,%x,%o,%p,%d,%p)\n",
1345 tab->name, layers, mode, fd, imode, perm,
1346 (void*)f, narg, (void*)args);
1347 f = (*tab->Open) (aTHX_ tab, layera, n, mode, fd, imode, perm,
1350 if (n + 1 < layera->cur) {
1352 * More layers above the one that we used to open -
1355 if (PerlIO_apply_layera(aTHX_ f, mode, layera, n + 1)
1362 PerlIO_list_free(aTHX_ layera);
1369 Perl_PerlIO_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
1372 return (*PerlIOBase(f)->tab->Read) (aTHX_ f, vbuf, count);
1374 SETERRNO(EBADF, SS$_IVCHAN);
1380 Perl_PerlIO_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
1383 return (*PerlIOBase(f)->tab->Unread) (aTHX_ f, vbuf, count);
1385 SETERRNO(EBADF, SS$_IVCHAN);
1391 Perl_PerlIO_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
1394 return (*PerlIOBase(f)->tab->Write) (aTHX_ f, vbuf, count);
1396 SETERRNO(EBADF, SS$_IVCHAN);
1402 Perl_PerlIO_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
1405 return (*PerlIOBase(f)->tab->Seek) (aTHX_ f, offset, whence);
1407 SETERRNO(EBADF, SS$_IVCHAN);
1413 Perl_PerlIO_tell(pTHX_ PerlIO *f)
1416 return (*PerlIOBase(f)->tab->Tell) (aTHX_ f);
1418 SETERRNO(EBADF, SS$_IVCHAN);
1424 Perl_PerlIO_flush(pTHX_ PerlIO *f)
1428 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1429 if (tab && tab->Flush) {
1430 return (*tab->Flush) (aTHX_ f);
1433 PerlIO_debug("Cannot flush f=%p :%s\n", (void*)f, tab->name);
1434 SETERRNO(EBADF, SS$_IVCHAN);
1439 PerlIO_debug("Cannot flush f=%p\n", (void*)f);
1440 SETERRNO(EBADF, SS$_IVCHAN);
1446 * Is it good API design to do flush-all on NULL, a potentially
1447 * errorneous input? Maybe some magical value (PerlIO*
1448 * PERLIO_FLUSH_ALL = (PerlIO*)-1;)? Yes, stdio does similar
1449 * things on fflush(NULL), but should we be bound by their design
1452 PerlIO **table = &PL_perlio;
1454 while ((f = *table)) {
1456 table = (PerlIO **) (f++);
1457 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
1458 if (*f && PerlIO_flush(f) != 0)
1468 PerlIOBase_flush_linebuf(pTHX)
1470 PerlIO **table = &PL_perlio;
1472 while ((f = *table)) {
1474 table = (PerlIO **) (f++);
1475 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
1478 flags & (PERLIO_F_LINEBUF | PERLIO_F_CANWRITE))
1479 == (PERLIO_F_LINEBUF | PERLIO_F_CANWRITE))
1487 Perl_PerlIO_fill(pTHX_ PerlIO *f)
1490 return (*PerlIOBase(f)->tab->Fill) (aTHX_ f);
1492 SETERRNO(EBADF, SS$_IVCHAN);
1498 PerlIO_isutf8(PerlIO *f)
1501 return (PerlIOBase(f)->flags & PERLIO_F_UTF8) != 0;
1503 SETERRNO(EBADF, SS$_IVCHAN);
1509 Perl_PerlIO_eof(pTHX_ PerlIO *f)
1512 return (*PerlIOBase(f)->tab->Eof) (aTHX_ f);
1514 SETERRNO(EBADF, SS$_IVCHAN);
1520 Perl_PerlIO_error(pTHX_ PerlIO *f)
1523 return (*PerlIOBase(f)->tab->Error) (aTHX_ f);
1525 SETERRNO(EBADF, SS$_IVCHAN);
1531 Perl_PerlIO_clearerr(pTHX_ PerlIO *f)
1534 (*PerlIOBase(f)->tab->Clearerr) (aTHX_ f);
1536 SETERRNO(EBADF, SS$_IVCHAN);
1540 Perl_PerlIO_setlinebuf(pTHX_ PerlIO *f)
1543 (*PerlIOBase(f)->tab->Setlinebuf) (aTHX_ f);
1545 SETERRNO(EBADF, SS$_IVCHAN);
1549 PerlIO_has_base(PerlIO *f)
1551 if (PerlIOValid(f)) {
1552 return (PerlIOBase(f)->tab->Get_base != NULL);
1558 PerlIO_fast_gets(PerlIO *f)
1560 if (PerlIOValid(f) && (PerlIOBase(f)->flags & PERLIO_F_FASTGETS)) {
1561 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1562 return (tab->Set_ptrcnt != NULL);
1568 PerlIO_has_cntptr(PerlIO *f)
1570 if (PerlIOValid(f)) {
1571 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1572 return (tab->Get_ptr != NULL && tab->Get_cnt != NULL);
1578 PerlIO_canset_cnt(PerlIO *f)
1580 if (PerlIOValid(f)) {
1581 PerlIOl *l = PerlIOBase(f);
1582 return (l->tab->Set_ptrcnt != NULL);
1588 Perl_PerlIO_get_base(pTHX_ PerlIO *f)
1591 return (*PerlIOBase(f)->tab->Get_base) (aTHX_ f);
1596 Perl_PerlIO_get_bufsiz(pTHX_ PerlIO *f)
1599 return (*PerlIOBase(f)->tab->Get_bufsiz) (aTHX_ f);
1604 Perl_PerlIO_get_ptr(pTHX_ PerlIO *f)
1606 if (PerlIOValid(f)) {
1607 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1608 if (tab->Get_ptr == NULL)
1610 return (*tab->Get_ptr) (aTHX_ f);
1616 Perl_PerlIO_get_cnt(pTHX_ PerlIO *f)
1618 if (PerlIOValid(f)) {
1619 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1620 if (tab->Get_cnt == NULL)
1622 return (*tab->Get_cnt) (aTHX_ f);
1628 Perl_PerlIO_set_cnt(pTHX_ PerlIO *f, int cnt)
1630 if (PerlIOValid(f)) {
1631 (*PerlIOBase(f)->tab->Set_ptrcnt) (aTHX_ f, NULL, cnt);
1636 Perl_PerlIO_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, int cnt)
1638 if (PerlIOValid(f)) {
1639 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1640 if (tab->Set_ptrcnt == NULL) {
1641 Perl_croak(aTHX_ "PerlIO buffer snooping abuse");
1643 (*PerlIOBase(f)->tab->Set_ptrcnt) (aTHX_ f, ptr, cnt);
1647 /*--------------------------------------------------------------------------------------*/
1649 * utf8 and raw dummy layers
1653 PerlIOUtf8_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
1655 if (*PerlIONext(f)) {
1656 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1657 PerlIO_pop(aTHX_ f);
1658 if (tab->kind & PERLIO_K_UTF8)
1659 PerlIOBase(f)->flags |= PERLIO_F_UTF8;
1661 PerlIOBase(f)->flags &= ~PERLIO_F_UTF8;
1667 PerlIO_funcs PerlIO_utf8 = {
1670 PERLIO_K_DUMMY | PERLIO_F_UTF8,
1688 NULL, /* get_base */
1689 NULL, /* get_bufsiz */
1692 NULL, /* set_ptrcnt */
1695 PerlIO_funcs PerlIO_byte = {
1716 NULL, /* get_base */
1717 NULL, /* get_bufsiz */
1720 NULL, /* set_ptrcnt */
1724 PerlIORaw_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
1725 IV n, const char *mode, int fd, int imode, int perm,
1726 PerlIO *old, int narg, SV **args)
1728 PerlIO_funcs *tab = PerlIO_default_btm();
1729 return (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
1733 PerlIO_funcs PerlIO_raw = {
1754 NULL, /* get_base */
1755 NULL, /* get_bufsiz */
1758 NULL, /* set_ptrcnt */
1760 /*--------------------------------------------------------------------------------------*/
1761 /*--------------------------------------------------------------------------------------*/
1763 * "Methods" of the "base class"
1767 PerlIOBase_fileno(pTHX_ PerlIO *f)
1769 return PerlIOValid(f) ? PerlIO_fileno(PerlIONext(f)) : -1;
1773 PerlIO_modestr(PerlIO *f, char *buf)
1776 IV flags = PerlIOBase(f)->flags;
1777 if (flags & PERLIO_F_APPEND) {
1779 if (flags & PERLIO_F_CANREAD) {
1783 else if (flags & PERLIO_F_CANREAD) {
1785 if (flags & PERLIO_F_CANWRITE)
1788 else if (flags & PERLIO_F_CANWRITE) {
1790 if (flags & PERLIO_F_CANREAD) {
1794 #ifdef PERLIO_USING_CRLF
1795 if (!(flags & PERLIO_F_CRLF))
1803 PerlIOBase_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
1805 PerlIOl *l = PerlIOBase(f);
1807 const char *omode = mode;
1810 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1811 l->flags &= ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE |
1812 PERLIO_F_TRUNCATE | PERLIO_F_APPEND);
1813 if (tab->Set_ptrcnt != NULL)
1814 l->flags |= PERLIO_F_FASTGETS;
1816 if (*mode == '#' || *mode == 'I')
1820 l->flags |= PERLIO_F_CANREAD;
1823 l->flags |= PERLIO_F_APPEND | PERLIO_F_CANWRITE;
1826 l->flags |= PERLIO_F_TRUNCATE | PERLIO_F_CANWRITE;
1829 SETERRNO(EINVAL, LIB$_INVARG);
1835 l->flags |= PERLIO_F_CANREAD | PERLIO_F_CANWRITE;
1838 l->flags &= ~PERLIO_F_CRLF;
1841 l->flags |= PERLIO_F_CRLF;
1844 SETERRNO(EINVAL, LIB$_INVARG);
1851 l->flags |= l->next->flags &
1852 (PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_TRUNCATE |
1857 PerlIO_debug("PerlIOBase_pushed f=%p %s %s fl=%08" UVxf " (%s)\n",
1858 f, PerlIOBase(f)->tab->name, (omode) ? omode : "(Null)",
1859 l->flags, PerlIO_modestr(f, temp));
1865 PerlIOBase_popped(pTHX_ PerlIO *f)
1871 PerlIOBase_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
1874 * Save the position as current head considers it
1876 Off_t old = PerlIO_tell(f);
1878 PerlIO_push(aTHX_ f, &PerlIO_pending, "r", Nullsv);
1879 PerlIOSelf(f, PerlIOBuf)->posn = old;
1880 done = PerlIOBuf_unread(aTHX_ f, vbuf, count);
1885 PerlIOBase_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
1887 STDCHAR *buf = (STDCHAR *) vbuf;
1889 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD))
1892 SSize_t avail = PerlIO_get_cnt(f);
1895 take = (count < avail) ? count : avail;
1897 STDCHAR *ptr = PerlIO_get_ptr(f);
1898 Copy(ptr, buf, take, STDCHAR);
1899 PerlIO_set_ptrcnt(f, ptr + take, (avail -= take));
1903 if (count > 0 && avail <= 0) {
1904 if (PerlIO_fill(f) != 0)
1908 return (buf - (STDCHAR *) vbuf);
1914 PerlIOBase_noop_ok(pTHX_ PerlIO *f)
1920 PerlIOBase_noop_fail(pTHX_ PerlIO *f)
1926 PerlIOBase_close(pTHX_ PerlIO *f)
1929 PerlIO *n = PerlIONext(f);
1930 if (PerlIO_flush(f) != 0)
1932 if (PerlIOValid(n) && (*PerlIOBase(n)->tab->Close)(aTHX_ n) != 0)
1934 PerlIOBase(f)->flags &=
1935 ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_OPEN);
1940 PerlIOBase_eof(pTHX_ PerlIO *f)
1942 if (PerlIOValid(f)) {
1943 return (PerlIOBase(f)->flags & PERLIO_F_EOF) != 0;
1949 PerlIOBase_error(pTHX_ PerlIO *f)
1951 if (PerlIOValid(f)) {
1952 return (PerlIOBase(f)->flags & PERLIO_F_ERROR) != 0;
1958 PerlIOBase_clearerr(pTHX_ PerlIO *f)
1960 if (PerlIOValid(f)) {
1961 PerlIO *n = PerlIONext(f);
1962 PerlIOBase(f)->flags &= ~(PERLIO_F_ERROR | PERLIO_F_EOF);
1969 PerlIOBase_setlinebuf(pTHX_ PerlIO *f)
1971 if (PerlIOValid(f)) {
1972 PerlIOBase(f)->flags |= PERLIO_F_LINEBUF;
1977 PerlIO_sv_dup(pTHX_ SV *arg, CLONE_PARAMS *param)
1983 return sv_dup(arg, param);
1986 return newSVsv(arg);
1989 return newSVsv(arg);
1994 PerlIOBase_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
1996 PerlIO *nexto = PerlIONext(o);
1997 if (PerlIOValid(nexto)) {
1998 PerlIO_funcs *tab = PerlIOBase(nexto)->tab;
1999 f = (*tab->Dup)(aTHX_ f, nexto, param, flags);
2002 PerlIO_funcs *self = PerlIOBase(o)->tab;
2005 PerlIO_debug("PerlIOBase_dup %s f=%p o=%p param=%p\n",
2006 self->name, (void*)f, (void*)o, (void*)param);
2008 arg = (*self->Getarg)(aTHX_ o,param,flags);
2010 f = PerlIO_push(aTHX_ f, self, PerlIO_modestr(o,buf), arg);
2018 #define PERLIO_MAX_REFCOUNTABLE_FD 2048
2020 perl_mutex PerlIO_mutex;
2022 int PerlIO_fd_refcnt[PERLIO_MAX_REFCOUNTABLE_FD];
2027 /* Place holder for stdstreams call ??? */
2029 MUTEX_INIT(&PerlIO_mutex);
2034 PerlIOUnix_refcnt_inc(int fd)
2036 if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
2038 MUTEX_LOCK(&PerlIO_mutex);
2040 PerlIO_fd_refcnt[fd]++;
2041 PerlIO_debug("fd %d refcnt=%d\n",fd,PerlIO_fd_refcnt[fd]);
2043 MUTEX_UNLOCK(&PerlIO_mutex);
2049 PerlIOUnix_refcnt_dec(int fd)
2052 if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
2054 MUTEX_LOCK(&PerlIO_mutex);
2056 cnt = --PerlIO_fd_refcnt[fd];
2057 PerlIO_debug("fd %d refcnt=%d\n",fd,cnt);
2059 MUTEX_UNLOCK(&PerlIO_mutex);
2066 PerlIO_cleanup(pTHX)
2070 PerlIO_debug("Cleanup %p\n",aTHX);
2072 /* Raise STDIN..STDERR refcount so we don't close them */
2073 for (i=0; i < 3; i++)
2074 PerlIOUnix_refcnt_inc(i);
2075 PerlIO_cleantable(aTHX_ &PL_perlio);
2076 /* Restore STDIN..STDERR refcount */
2077 for (i=0; i < 3; i++)
2078 PerlIOUnix_refcnt_dec(i);
2083 /*--------------------------------------------------------------------------------------*/
2085 * Bottom-most level for UNIX-like case
2089 struct _PerlIO base; /* The generic part */
2090 int fd; /* UNIX like file descriptor */
2091 int oflags; /* open/fcntl flags */
2095 PerlIOUnix_oflags(const char *mode)
2098 if (*mode == 'I' || *mode == '#')
2103 if (*++mode == '+') {
2110 oflags = O_CREAT | O_TRUNC;
2111 if (*++mode == '+') {
2120 oflags = O_CREAT | O_APPEND;
2121 if (*++mode == '+') {
2134 else if (*mode == 't') {
2136 oflags &= ~O_BINARY;
2140 * Always open in binary mode
2143 if (*mode || oflags == -1) {
2144 SETERRNO(EINVAL, LIB$_INVARG);
2151 PerlIOUnix_fileno(pTHX_ PerlIO *f)
2153 return PerlIOSelf(f, PerlIOUnix)->fd;
2157 PerlIOUnix_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
2159 IV code = PerlIOBase_pushed(aTHX_ f, mode, arg);
2160 PerlIOUnix *s = PerlIOSelf(f, PerlIOUnix);
2161 if (*PerlIONext(f)) {
2162 /* We never call down so any pending stuff now */
2163 PerlIO_flush(PerlIONext(f));
2164 s->fd = PerlIO_fileno(PerlIONext(f));
2166 * XXX could (or should) we retrieve the oflags from the open file
2167 * handle rather than believing the "mode" we are passed in? XXX
2168 * Should the value on NULL mode be 0 or -1?
2170 s->oflags = mode ? PerlIOUnix_oflags(mode) : -1;
2172 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
2177 PerlIOUnix_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2178 IV n, const char *mode, int fd, int imode,
2179 int perm, PerlIO *f, int narg, SV **args)
2182 if (PerlIOBase(f)->flags & PERLIO_F_OPEN)
2183 (*PerlIOBase(f)->tab->Close)(aTHX_ f);
2186 char *path = SvPV_nolen(*args);
2190 imode = PerlIOUnix_oflags(mode);
2194 fd = PerlLIO_open3(path, imode, perm);
2202 f = PerlIO_allocate(aTHX);
2203 s = PerlIOSelf(PerlIO_push(aTHX_ f, self, mode, PerlIOArg),
2207 s = PerlIOSelf(f, PerlIOUnix);
2210 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
2211 PerlIOUnix_refcnt_inc(fd);
2217 * FIXME: pop layers ???
2225 PerlIOUnix_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
2227 PerlIOUnix *os = PerlIOSelf(o, PerlIOUnix);
2229 if (flags & PERLIO_DUP_FD) {
2230 fd = PerlLIO_dup(fd);
2232 if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
2233 f = PerlIOBase_dup(aTHX_ f, o, param, flags);
2235 /* If all went well overwrite fd in dup'ed lay with the dup()'ed fd */
2236 PerlIOUnix *s = PerlIOSelf(f, PerlIOUnix);
2238 PerlIOUnix_refcnt_inc(fd);
2247 PerlIOUnix_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
2249 int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2250 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD))
2253 SSize_t len = PerlLIO_read(fd, vbuf, count);
2254 if (len >= 0 || errno != EINTR) {
2256 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2257 else if (len == 0 && count != 0)
2258 PerlIOBase(f)->flags |= PERLIO_F_EOF;
2266 PerlIOUnix_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
2268 int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2270 SSize_t len = PerlLIO_write(fd, vbuf, count);
2271 if (len >= 0 || errno != EINTR) {
2273 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2281 PerlIOUnix_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
2284 PerlLIO_lseek(PerlIOSelf(f, PerlIOUnix)->fd, offset, whence);
2285 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
2286 return (new == (Off_t) - 1) ? -1 : 0;
2290 PerlIOUnix_tell(pTHX_ PerlIO *f)
2292 return PerlLIO_lseek(PerlIOSelf(f, PerlIOUnix)->fd, 0, SEEK_CUR);
2297 PerlIOUnix_close(pTHX_ PerlIO *f)
2299 int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2301 if (PerlIOBase(f)->flags & PERLIO_F_OPEN) {
2302 if (PerlIOUnix_refcnt_dec(fd) > 0) {
2303 PerlIOBase(f)->flags &= ~PERLIO_F_OPEN;
2308 SETERRNO(EBADF,SS$_IVCHAN);
2311 while (PerlLIO_close(fd) != 0) {
2312 if (errno != EINTR) {
2319 PerlIOBase(f)->flags &= ~PERLIO_F_OPEN;
2324 PerlIO_funcs PerlIO_unix = {
2340 PerlIOBase_noop_ok, /* flush */
2341 PerlIOBase_noop_fail, /* fill */
2344 PerlIOBase_clearerr,
2345 PerlIOBase_setlinebuf,
2346 NULL, /* get_base */
2347 NULL, /* get_bufsiz */
2350 NULL, /* set_ptrcnt */
2353 /*--------------------------------------------------------------------------------------*/
2359 struct _PerlIO base;
2360 FILE *stdio; /* The stream */
2364 PerlIOStdio_fileno(pTHX_ PerlIO *f)
2366 return PerlSIO_fileno(PerlIOSelf(f, PerlIOStdio)->stdio);
2370 PerlIOStdio_mode(const char *mode, char *tmode)
2376 #ifdef PERLIO_USING_CRLF
2384 * This isn't used yet ...
2387 PerlIOStdio_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
2389 if (*PerlIONext(f)) {
2390 PerlIOStdio *s = PerlIOSelf(f, PerlIOStdio);
2393 PerlSIO_fdopen(PerlIO_fileno(PerlIONext(f)), mode =
2394 PerlIOStdio_mode(mode, tmode));
2397 /* We never call down so any pending stuff now */
2398 PerlIO_flush(PerlIONext(f));
2403 return PerlIOBase_pushed(aTHX_ f, mode, arg);
2407 PerlIO_importFILE(FILE *stdio, int fl)
2413 PerlIOSelf(PerlIO_push
2414 (aTHX_(f = PerlIO_allocate(aTHX)), &PerlIO_stdio,
2415 "r+", Nullsv), PerlIOStdio);
2422 PerlIOStdio_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2423 IV n, const char *mode, int fd, int imode,
2424 int perm, PerlIO *f, int narg, SV **args)
2428 char *path = SvPV_nolen(*args);
2429 PerlIOStdio *s = PerlIOSelf(f, PerlIOStdio);
2431 PerlIOUnix_refcnt_dec(fileno(s->stdio));
2432 stdio = PerlSIO_freopen(path, (mode = PerlIOStdio_mode(mode, tmode)),
2437 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2442 char *path = SvPV_nolen(*args);
2445 fd = PerlLIO_open3(path, imode, perm);
2448 FILE *stdio = PerlSIO_fopen(path, mode);
2451 PerlIOSelf(PerlIO_push
2452 (aTHX_(f = PerlIO_allocate(aTHX)), self,
2453 (mode = PerlIOStdio_mode(mode, tmode)),
2457 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2472 stdio = PerlSIO_stdin;
2475 stdio = PerlSIO_stdout;
2478 stdio = PerlSIO_stderr;
2483 stdio = PerlSIO_fdopen(fd, mode =
2484 PerlIOStdio_mode(mode, tmode));
2488 PerlIOSelf(PerlIO_push
2489 (aTHX_(f = PerlIO_allocate(aTHX)), self,
2490 mode, PerlIOArg), PerlIOStdio);
2492 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2501 PerlIOStdio_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
2503 /* This assumes no layers underneath - which is what
2504 happens, but is not how I remember it. NI-S 2001/10/16
2506 if ((f = PerlIOBase_dup(aTHX_ f, o, param, flags))) {
2507 FILE *stdio = PerlIOSelf(o, PerlIOStdio)->stdio;
2508 if (flags & PERLIO_DUP_FD) {
2509 int fd = PerlLIO_dup(fileno(stdio));
2512 stdio = fdopen(fd, PerlIO_modestr(o,mode));
2515 /* FIXME: To avoid messy error recovery if dup fails
2516 re-use the existing stdio as though flag was not set
2520 PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
2521 PerlIOUnix_refcnt_inc(fileno(stdio));
2527 PerlIOStdio_close(pTHX_ PerlIO *f)
2529 #ifdef SOCKS5_VERSION_NAME
2531 Sock_size_t optlen = sizeof(int);
2533 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2534 if (PerlIOUnix_refcnt_dec(fileno(stdio)) > 0) {
2535 /* Do not close it but do flush any buffers */
2540 #ifdef SOCKS5_VERSION_NAME
2542 (PerlIO_fileno(f), SOL_SOCKET, SO_TYPE, (void *) &optval,
2544 0) ? PerlSIO_fclose(stdio) : close(PerlIO_fileno(f))
2546 PerlSIO_fclose(stdio)
2555 PerlIOStdio_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
2557 FILE *s = PerlIOSelf(f, PerlIOStdio)->stdio;
2560 STDCHAR *buf = (STDCHAR *) vbuf;
2562 * Perl is expecting PerlIO_getc() to fill the buffer Linux's
2563 * stdio does not do that for fread()
2565 int ch = PerlSIO_fgetc(s);
2572 got = PerlSIO_fread(vbuf, 1, count, s);
2577 PerlIOStdio_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
2579 FILE *s = PerlIOSelf(f, PerlIOStdio)->stdio;
2580 STDCHAR *buf = ((STDCHAR *) vbuf) + count - 1;
2583 int ch = *buf-- & 0xff;
2584 if (PerlSIO_ungetc(ch, s) != ch)
2593 PerlIOStdio_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
2595 return PerlSIO_fwrite(vbuf, 1, count,
2596 PerlIOSelf(f, PerlIOStdio)->stdio);
2600 PerlIOStdio_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
2602 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2603 return PerlSIO_fseek(stdio, offset, whence);
2607 PerlIOStdio_tell(pTHX_ PerlIO *f)
2609 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2610 return PerlSIO_ftell(stdio);
2614 PerlIOStdio_flush(pTHX_ PerlIO *f)
2616 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2617 if (PerlIOBase(f)->flags & PERLIO_F_CANWRITE) {
2618 return PerlSIO_fflush(stdio);
2623 * FIXME: This discards ungetc() and pre-read stuff which is not
2624 * right if this is just a "sync" from a layer above Suspect right
2625 * design is to do _this_ but not have layer above flush this
2626 * layer read-to-read
2629 * Not writeable - sync by attempting a seek
2632 if (PerlSIO_fseek(stdio, (Off_t) 0, SEEK_CUR) != 0)
2640 PerlIOStdio_fill(pTHX_ PerlIO *f)
2642 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2645 * fflush()ing read-only streams can cause trouble on some stdio-s
2647 if ((PerlIOBase(f)->flags & PERLIO_F_CANWRITE)) {
2648 if (PerlSIO_fflush(stdio) != 0)
2651 c = PerlSIO_fgetc(stdio);
2652 if (c == EOF || PerlSIO_ungetc(c, stdio) != c)
2658 PerlIOStdio_eof(pTHX_ PerlIO *f)
2660 return PerlSIO_feof(PerlIOSelf(f, PerlIOStdio)->stdio);
2664 PerlIOStdio_error(pTHX_ PerlIO *f)
2666 return PerlSIO_ferror(PerlIOSelf(f, PerlIOStdio)->stdio);
2670 PerlIOStdio_clearerr(pTHX_ PerlIO *f)
2672 PerlSIO_clearerr(PerlIOSelf(f, PerlIOStdio)->stdio);
2676 PerlIOStdio_setlinebuf(pTHX_ PerlIO *f)
2678 #ifdef HAS_SETLINEBUF
2679 PerlSIO_setlinebuf(PerlIOSelf(f, PerlIOStdio)->stdio);
2681 PerlSIO_setvbuf(PerlIOSelf(f, PerlIOStdio)->stdio, Nullch, _IOLBF, 0);
2687 PerlIOStdio_get_base(pTHX_ PerlIO *f)
2689 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2690 return (STDCHAR*)PerlSIO_get_base(stdio);
2694 PerlIOStdio_get_bufsiz(pTHX_ PerlIO *f)
2696 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2697 return PerlSIO_get_bufsiz(stdio);
2701 #ifdef USE_STDIO_PTR
2703 PerlIOStdio_get_ptr(pTHX_ PerlIO *f)
2705 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2706 return (STDCHAR*)PerlSIO_get_ptr(stdio);
2710 PerlIOStdio_get_cnt(pTHX_ PerlIO *f)
2712 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2713 return PerlSIO_get_cnt(stdio);
2717 PerlIOStdio_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
2719 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2721 #ifdef STDIO_PTR_LVALUE
2722 PerlSIO_set_ptr(stdio, (void*)ptr); /* LHS STDCHAR* cast non-portable */
2723 #ifdef STDIO_PTR_LVAL_SETS_CNT
2724 if (PerlSIO_get_cnt(stdio) != (cnt)) {
2725 assert(PerlSIO_get_cnt(stdio) == (cnt));
2728 #if (!defined(STDIO_PTR_LVAL_NOCHANGE_CNT))
2730 * Setting ptr _does_ change cnt - we are done
2734 #else /* STDIO_PTR_LVALUE */
2736 #endif /* STDIO_PTR_LVALUE */
2739 * Now (or only) set cnt
2741 #ifdef STDIO_CNT_LVALUE
2742 PerlSIO_set_cnt(stdio, cnt);
2743 #else /* STDIO_CNT_LVALUE */
2744 #if (defined(STDIO_PTR_LVALUE) && defined(STDIO_PTR_LVAL_SETS_CNT))
2745 PerlSIO_set_ptr(stdio,
2746 PerlSIO_get_ptr(stdio) + (PerlSIO_get_cnt(stdio) -
2748 #else /* STDIO_PTR_LVAL_SETS_CNT */
2750 #endif /* STDIO_PTR_LVAL_SETS_CNT */
2751 #endif /* STDIO_CNT_LVALUE */
2756 PerlIO_funcs PerlIO_stdio = {
2758 sizeof(PerlIOStdio),
2776 PerlIOStdio_clearerr,
2777 PerlIOStdio_setlinebuf,
2779 PerlIOStdio_get_base,
2780 PerlIOStdio_get_bufsiz,
2785 #ifdef USE_STDIO_PTR
2786 PerlIOStdio_get_ptr,
2787 PerlIOStdio_get_cnt,
2788 #if (defined(STDIO_PTR_LVALUE) && (defined(STDIO_CNT_LVALUE) || defined(STDIO_PTR_LVAL_SETS_CNT)))
2789 PerlIOStdio_set_ptrcnt
2790 #else /* STDIO_PTR_LVALUE */
2792 #endif /* STDIO_PTR_LVALUE */
2793 #else /* USE_STDIO_PTR */
2797 #endif /* USE_STDIO_PTR */
2801 PerlIO_exportFILE(PerlIO *f, int fl)
2806 stdio = fdopen(PerlIO_fileno(f), "r+");
2809 PerlIOSelf(PerlIO_push(aTHX_ f, &PerlIO_stdio, "r+", Nullsv),
2817 PerlIO_findFILE(PerlIO *f)
2821 if (l->tab == &PerlIO_stdio) {
2822 PerlIOStdio *s = PerlIOSelf(&l, PerlIOStdio);
2825 l = *PerlIONext(&l);
2827 return PerlIO_exportFILE(f, 0);
2831 PerlIO_releaseFILE(PerlIO *p, FILE *f)
2835 /*--------------------------------------------------------------------------------------*/
2837 * perlio buffer layer
2841 PerlIOBuf_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
2843 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
2844 int fd = PerlIO_fileno(f);
2846 if (fd >= 0 && PerlLIO_isatty(fd)) {
2847 PerlIOBase(f)->flags |= PERLIO_F_LINEBUF | PERLIO_F_TTY;
2849 posn = PerlIO_tell(PerlIONext(f));
2850 if (posn != (Off_t) - 1) {
2853 return PerlIOBase_pushed(aTHX_ f, mode, arg);
2857 PerlIOBuf_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2858 IV n, const char *mode, int fd, int imode, int perm,
2859 PerlIO *f, int narg, SV **args)
2861 if (PerlIOValid(f)) {
2862 PerlIO *next = PerlIONext(f);
2863 PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIOBase(next)->tab);
2864 next = (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
2866 if (!next || (*PerlIOBase(f)->tab->Pushed) (aTHX_ f, mode, PerlIOArg) != 0) {
2871 PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIO_default_btm());
2879 f = (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
2882 if (PerlIO_push(aTHX_ f, self, mode, PerlIOArg) == 0) {
2884 * if push fails during open, open fails. close will pop us.
2889 fd = PerlIO_fileno(f);
2890 #ifdef PERLIO_USING_CRLF
2892 * do something about failing setmode()? --jhi
2894 PerlLIO_setmode(fd, O_BINARY);
2896 if (init && fd == 2) {
2898 * Initial stderr is unbuffered
2900 PerlIOBase(f)->flags |= PERLIO_F_UNBUF;
2909 * This "flush" is akin to sfio's sync in that it handles files in either
2910 * read or write state
2913 PerlIOBuf_flush(pTHX_ PerlIO *f)
2915 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
2917 PerlIO *n = PerlIONext(f);
2918 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF) {
2920 * write() the buffer
2922 STDCHAR *buf = b->buf;
2924 while (p < b->ptr) {
2925 SSize_t count = PerlIO_write(n, p, b->ptr - p);
2929 else if (count < 0 || PerlIO_error(n)) {
2930 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2935 b->posn += (p - buf);
2937 else if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
2938 STDCHAR *buf = PerlIO_get_base(f);
2940 * Note position change
2942 b->posn += (b->ptr - buf);
2943 if (b->ptr < b->end) {
2945 * We did not consume all of it
2947 if (PerlIO_seek(n, b->posn, SEEK_SET) == 0) {
2948 /* Reload n as some layers may pop themselves on seek */
2949 b->posn = PerlIO_tell(n = PerlIONext(f));
2953 b->ptr = b->end = b->buf;
2954 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
2955 /* We check for Valid because of dubious decision to make PerlIO_flush(NULL) flush all */
2956 /* FIXME: Doing downstream flush may be sub-optimal see PerlIOBuf_fill() below */
2957 if (PerlIOValid(n) && PerlIO_flush(n) != 0)
2963 PerlIOBuf_fill(pTHX_ PerlIO *f)
2965 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
2966 PerlIO *n = PerlIONext(f);
2969 * FIXME: doing the down-stream flush maybe sub-optimal if it causes
2970 * pre-read data in stdio buffer to be discarded.
2971 * However, skipping the flush also skips _our_ hosekeeping
2972 * and breaks tell tests. So we do the flush.
2974 if (PerlIO_flush(f) != 0)
2976 if (PerlIOBase(f)->flags & PERLIO_F_TTY)
2977 PerlIOBase_flush_linebuf(aTHX);
2980 PerlIO_get_base(f); /* allocate via vtable */
2982 b->ptr = b->end = b->buf;
2983 if (PerlIO_fast_gets(n)) {
2985 * Layer below is also buffered. We do _NOT_ want to call its
2986 * ->Read() because that will loop till it gets what we asked for
2987 * which may hang on a pipe etc. Instead take anything it has to
2988 * hand, or ask it to fill _once_.
2990 avail = PerlIO_get_cnt(n);
2992 avail = PerlIO_fill(n);
2994 avail = PerlIO_get_cnt(n);
2996 if (!PerlIO_error(n) && PerlIO_eof(n))
3001 STDCHAR *ptr = PerlIO_get_ptr(n);
3002 SSize_t cnt = avail;
3003 if (avail > b->bufsiz)
3005 Copy(ptr, b->buf, avail, STDCHAR);
3006 PerlIO_set_ptrcnt(n, ptr + avail, cnt - avail);
3010 avail = PerlIO_read(n, b->ptr, b->bufsiz);
3014 PerlIOBase(f)->flags |= PERLIO_F_EOF;
3016 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
3019 b->end = b->buf + avail;
3020 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3025 PerlIOBuf_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
3027 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3028 if (PerlIOValid(f)) {
3031 return PerlIOBase_read(aTHX_ f, vbuf, count);
3037 PerlIOBuf_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3039 const STDCHAR *buf = (const STDCHAR *) vbuf + count;
3040 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3043 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
3048 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
3050 * Buffer is already a read buffer, we can overwrite any chars
3051 * which have been read back to buffer start
3053 avail = (b->ptr - b->buf);
3057 * Buffer is idle, set it up so whole buffer is available for
3061 b->end = b->buf + avail;
3063 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3065 * Buffer extends _back_ from where we are now
3067 b->posn -= b->bufsiz;
3069 if (avail > (SSize_t) count) {
3071 * If we have space for more than count, just move count
3079 * In simple stdio-like ungetc() case chars will be already
3082 if (buf != b->ptr) {
3083 Copy(buf, b->ptr, avail, STDCHAR);
3087 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
3094 PerlIOBuf_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3096 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3097 const STDCHAR *buf = (const STDCHAR *) vbuf;
3101 if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
3104 SSize_t avail = b->bufsiz - (b->ptr - b->buf);
3105 if ((SSize_t) count < avail)
3107 PerlIOBase(f)->flags |= PERLIO_F_WRBUF;
3108 if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF) {
3123 Copy(buf, b->ptr, avail, STDCHAR);
3130 if (b->ptr >= (b->buf + b->bufsiz))
3133 if (PerlIOBase(f)->flags & PERLIO_F_UNBUF)
3139 PerlIOBuf_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
3142 if ((code = PerlIO_flush(f)) == 0) {
3143 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3144 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
3145 code = PerlIO_seek(PerlIONext(f), offset, whence);
3147 b->posn = PerlIO_tell(PerlIONext(f));
3154 PerlIOBuf_tell(pTHX_ PerlIO *f)
3156 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3158 * b->posn is file position where b->buf was read, or will be written
3160 Off_t posn = b->posn;
3163 * If buffer is valid adjust position by amount in buffer
3165 posn += (b->ptr - b->buf);
3171 PerlIOBuf_close(pTHX_ PerlIO *f)
3173 IV code = PerlIOBase_close(aTHX_ f);
3174 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3175 if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
3179 b->ptr = b->end = b->buf;
3180 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
3185 PerlIOBuf_get_ptr(pTHX_ PerlIO *f)
3187 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3194 PerlIOBuf_get_cnt(pTHX_ PerlIO *f)
3196 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3199 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF)
3200 return (b->end - b->ptr);
3205 PerlIOBuf_get_base(pTHX_ PerlIO *f)
3207 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3212 Newz('B',b->buf,b->bufsiz, STDCHAR);
3214 b->buf = (STDCHAR *) & b->oneword;
3215 b->bufsiz = sizeof(b->oneword);
3224 PerlIOBuf_bufsiz(pTHX_ PerlIO *f)
3226 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3229 return (b->end - b->buf);
3233 PerlIOBuf_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
3235 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3239 if (PerlIO_get_cnt(f) != cnt || b->ptr < b->buf) {
3240 assert(PerlIO_get_cnt(f) == cnt);
3241 assert(b->ptr >= b->buf);
3243 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3247 PerlIOBuf_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
3249 return PerlIOBase_dup(aTHX_ f, o, param, flags);
3254 PerlIO_funcs PerlIO_perlio = {
3274 PerlIOBase_clearerr,
3275 PerlIOBase_setlinebuf,
3280 PerlIOBuf_set_ptrcnt,
3283 /*--------------------------------------------------------------------------------------*/
3285 * Temp layer to hold unread chars when cannot do it any other way
3289 PerlIOPending_fill(pTHX_ PerlIO *f)
3292 * Should never happen
3299 PerlIOPending_close(pTHX_ PerlIO *f)
3302 * A tad tricky - flush pops us, then we close new top
3305 return PerlIO_close(f);
3309 PerlIOPending_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
3312 * A tad tricky - flush pops us, then we seek new top
3315 return PerlIO_seek(f, offset, whence);
3320 PerlIOPending_flush(pTHX_ PerlIO *f)
3322 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3323 if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
3327 PerlIO_pop(aTHX_ f);
3332 PerlIOPending_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
3338 PerlIOBuf_set_ptrcnt(aTHX_ f, ptr, cnt);
3343 PerlIOPending_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
3345 IV code = PerlIOBase_pushed(aTHX_ f, mode, arg);
3346 PerlIOl *l = PerlIOBase(f);
3348 * Our PerlIO_fast_gets must match what we are pushed on, or sv_gets()
3349 * etc. get muddled when it changes mid-string when we auto-pop.
3351 l->flags = (l->flags & ~(PERLIO_F_FASTGETS | PERLIO_F_UTF8)) |
3352 (PerlIOBase(PerlIONext(f))->
3353 flags & (PERLIO_F_FASTGETS | PERLIO_F_UTF8));
3358 PerlIOPending_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
3360 SSize_t avail = PerlIO_get_cnt(f);
3365 got = PerlIOBuf_read(aTHX_ f, vbuf, avail);
3366 if (got >= 0 && got < count) {
3368 PerlIO_read(f, ((STDCHAR *) vbuf) + got, count - got);
3369 if (more >= 0 || got == 0)
3375 PerlIO_funcs PerlIO_pending = {
3379 PerlIOPending_pushed,
3390 PerlIOPending_close,
3391 PerlIOPending_flush,
3395 PerlIOBase_clearerr,
3396 PerlIOBase_setlinebuf,
3401 PerlIOPending_set_ptrcnt,
3406 /*--------------------------------------------------------------------------------------*/
3408 * crlf - translation On read translate CR,LF to "\n" we do this by
3409 * overriding ptr/cnt entries to hand back a line at a time and keeping a
3410 * record of which nl we "lied" about. On write translate "\n" to CR,LF
3414 PerlIOBuf base; /* PerlIOBuf stuff */
3415 STDCHAR *nl; /* Position of crlf we "lied" about in the
3420 PerlIOCrlf_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
3423 PerlIOBase(f)->flags |= PERLIO_F_CRLF;
3424 code = PerlIOBuf_pushed(aTHX_ f, mode, arg);
3426 PerlIO_debug("PerlIOCrlf_pushed f=%p %s %s fl=%08" UVxf "\n",
3427 f, PerlIOBase(f)->tab->name, (mode) ? mode : "(Null)",
3428 PerlIOBase(f)->flags);
3435 PerlIOCrlf_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3437 PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
3442 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF))
3443 return PerlIOBuf_unread(aTHX_ f, vbuf, count);
3445 const STDCHAR *buf = (const STDCHAR *) vbuf + count;
3446 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3448 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
3453 if (!(PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
3454 b->end = b->ptr = b->buf + b->bufsiz;
3455 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3456 b->posn -= b->bufsiz;
3458 while (count > 0 && b->ptr > b->buf) {
3461 if (b->ptr - 2 >= b->buf) {
3484 PerlIOCrlf_get_cnt(pTHX_ PerlIO *f)
3486 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3489 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
3490 PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
3491 if ((PerlIOBase(f)->flags & PERLIO_F_CRLF) && !c->nl) {
3492 STDCHAR *nl = b->ptr;
3494 while (nl < b->end && *nl != 0xd)
3496 if (nl < b->end && *nl == 0xd) {
3498 if (nl + 1 < b->end) {
3505 * Not CR,LF but just CR
3513 * Blast - found CR as last char in buffer
3518 * They may not care, defer work as long as
3522 return (nl - b->ptr);
3526 b->ptr++; /* say we have read it as far as
3527 * flush() is concerned */
3528 b->buf++; /* Leave space in front of buffer */
3529 b->bufsiz--; /* Buffer is thus smaller */
3530 code = PerlIO_fill(f); /* Fetch some more */
3531 b->bufsiz++; /* Restore size for next time */
3532 b->buf--; /* Point at space */
3533 b->ptr = nl = b->buf; /* Which is what we hand
3535 b->posn--; /* Buffer starts here */
3536 *nl = 0xd; /* Fill in the CR */
3538 goto test; /* fill() call worked */
3540 * CR at EOF - just fall through
3542 /* Should we clear EOF though ??? */
3547 return (((c->nl) ? (c->nl + 1) : b->end) - b->ptr);
3553 PerlIOCrlf_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
3555 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3556 PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
3557 IV flags = PerlIOBase(f)->flags;
3563 if (ptr == b->end && *c->nl == 0xd) {
3564 /* Defered CR at end of buffer case - we lied about count */
3575 * Test code - delete when it works ...
3577 STDCHAR *chk = (c->nl) ? (c->nl+1) : b->end;
3578 if (ptr+cnt == c->nl && c->nl+1 == b->end && *c->nl == 0xd) {
3579 /* Defered CR at end of buffer case - we lied about count */
3585 Perl_warn(aTHX_ "ptr wrong %p != %p fl=%08" UVxf
3586 " nl=%p e=%p for %d", ptr, chk, flags, c->nl,
3593 * They have taken what we lied about
3601 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3605 PerlIOCrlf_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3607 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF))
3608 return PerlIOBuf_write(aTHX_ f, vbuf, count);
3610 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3611 const STDCHAR *buf = (const STDCHAR *) vbuf;
3612 const STDCHAR *ebuf = buf + count;
3615 if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
3617 while (buf < ebuf) {
3618 STDCHAR *eptr = b->buf + b->bufsiz;
3619 PerlIOBase(f)->flags |= PERLIO_F_WRBUF;
3620 while (buf < ebuf && b->ptr < eptr) {
3622 if ((b->ptr + 2) > eptr) {
3630 *(b->ptr)++ = 0xd; /* CR */
3631 *(b->ptr)++ = 0xa; /* LF */
3633 if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF) {
3643 if (b->ptr >= eptr) {
3649 if (PerlIOBase(f)->flags & PERLIO_F_UNBUF)
3651 return (buf - (STDCHAR *) vbuf);
3656 PerlIOCrlf_flush(pTHX_ PerlIO *f)
3658 PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
3663 return PerlIOBuf_flush(aTHX_ f);
3666 PerlIO_funcs PerlIO_crlf = {
3669 PERLIO_K_BUFFERED | PERLIO_K_CANCRLF,
3671 PerlIOBase_noop_ok, /* popped */
3676 PerlIOBuf_read, /* generic read works with ptr/cnt lies
3678 PerlIOCrlf_unread, /* Put CR,LF in buffer for each '\n' */
3679 PerlIOCrlf_write, /* Put CR,LF in buffer for each '\n' */
3687 PerlIOBase_clearerr,
3688 PerlIOBase_setlinebuf,
3693 PerlIOCrlf_set_ptrcnt,
3697 /*--------------------------------------------------------------------------------------*/
3699 * mmap as "buffer" layer
3703 PerlIOBuf base; /* PerlIOBuf stuff */
3704 Mmap_t mptr; /* Mapped address */
3705 Size_t len; /* mapped length */
3706 STDCHAR *bbuf; /* malloced buffer if map fails */
3709 static size_t page_size = 0;
3712 PerlIOMmap_map(pTHX_ PerlIO *f)
3714 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3715 IV flags = PerlIOBase(f)->flags;
3719 if (flags & PERLIO_F_CANREAD) {
3720 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3721 int fd = PerlIO_fileno(f);
3723 code = Fstat(fd, &st);
3724 if (code == 0 && S_ISREG(st.st_mode)) {
3725 SSize_t len = st.st_size - b->posn;
3729 #if defined(HAS_SYSCONF) && (defined(_SC_PAGESIZE) || defined(_SC_PAGE_SIZE))
3731 SETERRNO(0, SS$_NORMAL);
3732 # ifdef _SC_PAGESIZE
3733 page_size = sysconf(_SC_PAGESIZE);
3735 page_size = sysconf(_SC_PAGE_SIZE);
3737 if ((long) page_size < 0) {
3742 (void) SvUPGRADE(error, SVt_PV);
3743 msg = SvPVx(error, n_a);
3744 Perl_croak(aTHX_ "panic: sysconf: %s",
3749 "panic: sysconf: pagesize unknown");
3753 # ifdef HAS_GETPAGESIZE
3754 page_size = getpagesize();
3756 # if defined(I_SYS_PARAM) && defined(PAGESIZE)
3757 page_size = PAGESIZE; /* compiletime, bad */
3761 if ((IV) page_size <= 0)
3762 Perl_croak(aTHX_ "panic: bad pagesize %" IVdf,
3767 * This is a hack - should never happen - open should
3770 b->posn = PerlIO_tell(PerlIONext(f));
3772 posn = (b->posn / page_size) * page_size;
3773 len = st.st_size - posn;
3774 m->mptr = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, posn);
3775 if (m->mptr && m->mptr != (Mmap_t) - 1) {
3776 #if 0 && defined(HAS_MADVISE) && defined(MADV_SEQUENTIAL)
3777 madvise(m->mptr, len, MADV_SEQUENTIAL);
3779 #if 0 && defined(HAS_MADVISE) && defined(MADV_WILLNEED)
3780 madvise(m->mptr, len, MADV_WILLNEED);
3782 PerlIOBase(f)->flags =
3783 (flags & ~PERLIO_F_EOF) | PERLIO_F_RDBUF;
3784 b->end = ((STDCHAR *) m->mptr) + len;
3785 b->buf = ((STDCHAR *) m->mptr) + (b->posn - posn);
3794 PerlIOBase(f)->flags =
3795 flags | PERLIO_F_EOF | PERLIO_F_RDBUF;
3797 b->ptr = b->end = b->ptr;
3806 PerlIOMmap_unmap(pTHX_ PerlIO *f)
3808 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3809 PerlIOBuf *b = &m->base;
3813 code = munmap(m->mptr, m->len);
3817 if (PerlIO_seek(PerlIONext(f), b->posn, SEEK_SET) != 0)
3820 b->ptr = b->end = b->buf;
3821 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
3827 PerlIOMmap_get_base(pTHX_ PerlIO *f)
3829 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3830 PerlIOBuf *b = &m->base;
3831 if (b->buf && (PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
3833 * Already have a readbuffer in progress
3839 * We have a write buffer or flushed PerlIOBuf read buffer
3841 m->bbuf = b->buf; /* save it in case we need it again */
3842 b->buf = NULL; /* Clear to trigger below */
3845 PerlIOMmap_map(aTHX_ f); /* Try and map it */
3848 * Map did not work - recover PerlIOBuf buffer if we have one
3853 b->ptr = b->end = b->buf;
3856 return PerlIOBuf_get_base(aTHX_ f);
3860 PerlIOMmap_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3862 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3863 PerlIOBuf *b = &m->base;
3864 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
3866 if (b->ptr && (b->ptr - count) >= b->buf
3867 && memEQ(b->ptr - count, vbuf, count)) {
3869 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
3874 * Loose the unwritable mapped buffer
3878 * If flush took the "buffer" see if we have one from before
3880 if (!b->buf && m->bbuf)
3883 PerlIOBuf_get_base(aTHX_ f);
3887 return PerlIOBuf_unread(aTHX_ f, vbuf, count);
3891 PerlIOMmap_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3893 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3894 PerlIOBuf *b = &m->base;
3895 if (!b->buf || !(PerlIOBase(f)->flags & PERLIO_F_WRBUF)) {
3897 * No, or wrong sort of, buffer
3900 if (PerlIOMmap_unmap(aTHX_ f) != 0)
3904 * If unmap took the "buffer" see if we have one from before
3906 if (!b->buf && m->bbuf)
3909 PerlIOBuf_get_base(aTHX_ f);
3913 return PerlIOBuf_write(aTHX_ f, vbuf, count);
3917 PerlIOMmap_flush(pTHX_ PerlIO *f)
3919 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3920 PerlIOBuf *b = &m->base;
3921 IV code = PerlIOBuf_flush(aTHX_ f);
3923 * Now we are "synced" at PerlIOBuf level
3930 if (PerlIOMmap_unmap(aTHX_ f) != 0)
3935 * We seem to have a PerlIOBuf buffer which was not mapped
3936 * remember it in case we need one later
3945 PerlIOMmap_fill(pTHX_ PerlIO *f)
3947 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3948 IV code = PerlIO_flush(f);
3949 if (code == 0 && !b->buf) {
3950 code = PerlIOMmap_map(aTHX_ f);
3952 if (code == 0 && !(PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
3953 code = PerlIOBuf_fill(aTHX_ f);
3959 PerlIOMmap_close(pTHX_ PerlIO *f)
3961 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3962 PerlIOBuf *b = &m->base;
3963 IV code = PerlIO_flush(f);
3967 b->ptr = b->end = b->buf;
3969 if (PerlIOBuf_close(aTHX_ f) != 0)
3975 PerlIOMmap_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
3977 return PerlIOBase_dup(aTHX_ f, o, param, flags);
3981 PerlIO_funcs PerlIO_mmap = {
4001 PerlIOBase_clearerr,
4002 PerlIOBase_setlinebuf,
4003 PerlIOMmap_get_base,
4007 PerlIOBuf_set_ptrcnt,
4010 #endif /* HAS_MMAP */
4013 Perl_PerlIO_stdin(pTHX)
4016 PerlIO_stdstreams(aTHX);
4018 return &PL_perlio[1];
4022 Perl_PerlIO_stdout(pTHX)
4025 PerlIO_stdstreams(aTHX);
4027 return &PL_perlio[2];
4031 Perl_PerlIO_stderr(pTHX)
4034 PerlIO_stdstreams(aTHX);
4036 return &PL_perlio[3];
4039 /*--------------------------------------------------------------------------------------*/
4042 PerlIO_getname(PerlIO *f, char *buf)
4047 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
4049 name = fgetname(stdio, buf);
4051 Perl_croak(aTHX_ "Don't know how to get file name");
4057 /*--------------------------------------------------------------------------------------*/
4059 * Functions which can be called on any kind of PerlIO implemented in
4063 #undef PerlIO_fdopen
4065 PerlIO_fdopen(int fd, const char *mode)
4068 return PerlIO_openn(aTHX_ Nullch, mode, fd, 0, 0, NULL, 0, NULL);
4073 PerlIO_open(const char *path, const char *mode)
4076 SV *name = sv_2mortal(newSVpvn(path, strlen(path)));
4077 return PerlIO_openn(aTHX_ Nullch, mode, -1, 0, 0, NULL, 1, &name);
4080 #undef Perlio_reopen
4082 PerlIO_reopen(const char *path, const char *mode, PerlIO *f)
4085 SV *name = sv_2mortal(newSVpvn(path, strlen(path)));
4086 return PerlIO_openn(aTHX_ Nullch, mode, -1, 0, 0, f, 1, &name);
4091 PerlIO_getc(PerlIO *f)
4095 SSize_t count = PerlIO_read(f, buf, 1);
4097 return (unsigned char) buf[0];
4102 #undef PerlIO_ungetc
4104 PerlIO_ungetc(PerlIO *f, int ch)
4109 if (PerlIO_unread(f, &buf, 1) == 1)
4117 PerlIO_putc(PerlIO *f, int ch)
4121 return PerlIO_write(f, &buf, 1);
4126 PerlIO_puts(PerlIO *f, const char *s)
4129 STRLEN len = strlen(s);
4130 return PerlIO_write(f, s, len);
4133 #undef PerlIO_rewind
4135 PerlIO_rewind(PerlIO *f)
4138 PerlIO_seek(f, (Off_t) 0, SEEK_SET);
4142 #undef PerlIO_vprintf
4144 PerlIO_vprintf(PerlIO *f, const char *fmt, va_list ap)
4147 SV *sv = newSVpvn("", 0);
4153 Perl_va_copy(ap, apc);
4154 sv_vcatpvf(sv, fmt, &apc);
4156 sv_vcatpvf(sv, fmt, &ap);
4159 wrote = PerlIO_write(f, s, len);
4164 #undef PerlIO_printf
4166 PerlIO_printf(PerlIO *f, const char *fmt, ...)
4171 result = PerlIO_vprintf(f, fmt, ap);
4176 #undef PerlIO_stdoutf
4178 PerlIO_stdoutf(const char *fmt, ...)
4184 result = PerlIO_vprintf(PerlIO_stdout(), fmt, ap);
4189 #undef PerlIO_tmpfile
4191 PerlIO_tmpfile(void)
4194 * I have no idea how portable mkstemp() is ...
4196 #if defined(WIN32) || !defined(HAVE_MKSTEMP)
4199 FILE *stdio = PerlSIO_tmpfile();
4202 PerlIOSelf(PerlIO_push
4203 (aTHX_(f = PerlIO_allocate(aTHX)), &PerlIO_stdio,
4204 "w+", Nullsv), PerlIOStdio);
4210 SV *sv = newSVpv("/tmp/PerlIO_XXXXXX", 0);
4211 int fd = mkstemp(SvPVX(sv));
4214 f = PerlIO_fdopen(fd, "w+");
4216 PerlIOBase(f)->flags |= PERLIO_F_TEMP;
4218 PerlLIO_unlink(SvPVX(sv));
4228 #endif /* USE_SFIO */
4229 #endif /* PERLIO_IS_STDIO */
4231 /*======================================================================================*/
4233 * Now some functions in terms of above which may be needed even if we are
4234 * not in true PerlIO mode
4238 #undef PerlIO_setpos
4240 PerlIO_setpos(PerlIO *f, SV *pos)
4245 Off_t *posn = (Off_t *) SvPV(pos, len);
4246 if (f && len == sizeof(Off_t))
4247 return PerlIO_seek(f, *posn, SEEK_SET);
4249 SETERRNO(EINVAL, SS$_IVCHAN);
4253 #undef PerlIO_setpos
4255 PerlIO_setpos(PerlIO *f, SV *pos)
4260 Fpos_t *fpos = (Fpos_t *) SvPV(pos, len);
4261 if (f && len == sizeof(Fpos_t)) {
4262 #if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64)
4263 return fsetpos64(f, fpos);
4265 return fsetpos(f, fpos);
4269 SETERRNO(EINVAL, SS$_IVCHAN);
4275 #undef PerlIO_getpos
4277 PerlIO_getpos(PerlIO *f, SV *pos)
4280 Off_t posn = PerlIO_tell(f);
4281 sv_setpvn(pos, (char *) &posn, sizeof(posn));
4282 return (posn == (Off_t) - 1) ? -1 : 0;
4285 #undef PerlIO_getpos
4287 PerlIO_getpos(PerlIO *f, SV *pos)
4292 #if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64)
4293 code = fgetpos64(f, &fpos);
4295 code = fgetpos(f, &fpos);
4297 sv_setpvn(pos, (char *) &fpos, sizeof(fpos));
4302 #if (defined(PERLIO_IS_STDIO) || !defined(USE_SFIO)) && !defined(HAS_VPRINTF)
4305 vprintf(char *pat, char *args)
4307 _doprnt(pat, args, stdout);
4308 return 0; /* wrong, but perl doesn't use the return
4313 vfprintf(FILE *fd, char *pat, char *args)
4315 _doprnt(pat, args, fd);
4316 return 0; /* wrong, but perl doesn't use the return
4322 #ifndef PerlIO_vsprintf
4324 PerlIO_vsprintf(char *s, int n, const char *fmt, va_list ap)
4326 int val = vsprintf(s, fmt, ap);
4328 if (strlen(s) >= (STRLEN) n) {
4330 (void) PerlIO_puts(Perl_error_log,
4331 "panic: sprintf overflow - memory corrupted!\n");
4339 #ifndef PerlIO_sprintf
4341 PerlIO_sprintf(char *s, int n, const char *fmt, ...)
4346 result = PerlIO_vsprintf(s, n, fmt, ap);