2 * perlio.c Copyright (c) 1996-2002, Nick Ing-Simmons You may distribute
3 * under the terms of either the GNU General Public License or the
4 * Artistic License, as specified in the README file.
8 * Hour after hour for nearly three weary days he had jogged up and down,
9 * over passes, and through long dales, and across many streams.
13 * If we have ActivePerl-like PERL_IMPLICIT_SYS then we need a dTHX to get
14 * at the dispatch tables, even when we do not need it for other reasons.
15 * Invent a dSYS macro to abstract this out
17 #ifdef PERL_IMPLICIT_SYS
30 #define PERLIO_NOT_STDIO 0
31 #if !defined(PERLIO_IS_STDIO) && !defined(USE_SFIO)
37 * This file provides those parts of PerlIO abstraction
38 * which are not #defined in perlio.h.
39 * Which these are depends on various Configure #ifdef's
43 #define PERL_IN_PERLIO_C
46 #ifdef PERL_IMPLICIT_CONTEXT
54 perlsio_binmode(FILE *fp, int iotype, int mode)
57 * This used to be contents of do_binmode in doio.c
60 # if defined(atarist) || defined(__MINT__)
63 ((FILE *) fp)->_flag |= _IOBIN;
65 ((FILE *) fp)->_flag &= ~_IOBIN;
72 if (PerlLIO_setmode(fp, mode) != -1) {
74 if (PerlLIO_setmode(fileno(fp), mode) != -1) {
76 # if defined(WIN32) && defined(__BORLANDC__)
78 * The translation mode of the stream is maintained independent of
79 * the translation mode of the fd in the Borland RTL (heavy
80 * digging through their runtime sources reveal). User has to set
81 * the mode explicitly for the stream (though they don't document
82 * this anywhere). GSAR 97-5-24
96 # if defined(USEMYBINMODE)
98 if (my_binmode(fp, iotype, mode) != FALSE)
109 #define O_ACCMODE 3 /* Assume traditional implementation */
113 PerlIO_intmode2str(int rawmode, char *mode, int *writing)
115 int result = rawmode & O_ACCMODE;
120 ptype = IoTYPE_RDONLY;
123 ptype = IoTYPE_WRONLY;
131 *writing = (result != O_RDONLY);
133 if (result == O_RDONLY) {
137 else if (rawmode & O_APPEND) {
139 if (result != O_WRONLY)
144 if (result == O_WRONLY)
151 if (rawmode & O_BINARY)
157 #ifndef PERLIO_LAYERS
159 PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names)
161 if (!names || !*names
162 || strEQ(names, ":crlf")
163 || strEQ(names, ":raw")
164 || strEQ(names, ":bytes")
168 Perl_croak(aTHX_ "Cannot apply \"%s\" in non-PerlIO perl", names);
176 PerlIO_destruct(pTHX)
181 PerlIO_binmode(pTHX_ PerlIO *fp, int iotype, int mode, const char *names)
186 return perlsio_binmode(fp, iotype, mode);
191 PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
196 #ifdef PERL_IMPLICIT_SYS
197 return PerlSIO_fdupopen(f);
200 return win32_fdupopen(f);
203 int fd = PerlLIO_dup(PerlIO_fileno(f));
206 int omode = fcntl(fd, F_GETFL);
208 omode = djgpp_get_stream_mode(f);
210 PerlIO_intmode2str(omode,mode,NULL);
211 /* the r+ is a hack */
212 return PerlIO_fdopen(fd, mode);
217 SETERRNO(EBADF, SS_IVCHAN);
227 * De-mux PerlIO_openn() into fdopen, freopen and fopen type entries
231 PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd,
232 int imode, int perm, PerlIO *old, int narg, SV **args)
236 Perl_croak(aTHX_ "More than one argument to open");
238 if (*args == &PL_sv_undef)
239 return PerlIO_tmpfile();
241 char *name = SvPV_nolen(*args);
243 fd = PerlLIO_open3(name, imode, perm);
245 return PerlIO_fdopen(fd, (char *) mode + 1);
248 return PerlIO_reopen(name, mode, old);
251 return PerlIO_open(name, mode);
256 return PerlIO_fdopen(fd, (char *) mode);
261 XS(XS_PerlIO__Layer__find)
265 Perl_croak(aTHX_ "Usage class->find(name[,load])");
267 char *name = SvPV_nolen(ST(1));
268 ST(0) = (strEQ(name, "crlf")
269 || strEQ(name, "raw")) ? &PL_sv_yes : &PL_sv_undef;
276 Perl_boot_core_PerlIO(pTHX)
278 newXS("PerlIO::Layer::find", XS_PerlIO__Layer__find, __FILE__);
284 #ifdef PERLIO_IS_STDIO
290 * Does nothing (yet) except force this file to be included in perl
291 * binary. That allows this file to force inclusion of other functions
292 * that may be required by loadable extensions e.g. for
293 * FileHandle::tmpfile
297 #undef PerlIO_tmpfile
304 #else /* PERLIO_IS_STDIO */
312 * This section is just to make sure these functions get pulled in from
316 #undef PerlIO_tmpfile
327 * Force this file to be included in perl binary. Which allows this
328 * file to force inclusion of other functions that may be required by
329 * loadable extensions e.g. for FileHandle::tmpfile
333 * Hack sfio does its own 'autoflush' on stdout in common cases. Flush
334 * results in a lot of lseek()s to regular files and lot of small
337 sfset(sfstdout, SF_SHARE, 0);
340 /* This is not the reverse of PerlIO_exportFILE(), PerlIO_releaseFILE() is. */
342 PerlIO_importFILE(FILE *stdio, const char *mode)
344 int fd = fileno(stdio);
345 if (!mode || !*mode) {
348 return PerlIO_fdopen(fd, mode);
352 PerlIO_findFILE(PerlIO *pio)
354 int fd = PerlIO_fileno(pio);
355 FILE *f = fdopen(fd, "r+");
357 if (!f && errno == EINVAL)
359 if (!f && errno == EINVAL)
366 /*======================================================================================*/
368 * Implement all the PerlIO interface ourselves.
374 * We _MUST_ have <unistd.h> if we are using lseek() and may have large
381 #include <sys/mman.h>
385 void PerlIO_debug(const char *fmt, ...)
386 __attribute__ ((format(__printf__, 1, 2)));
389 PerlIO_debug(const char *fmt, ...)
396 char *s = PerlEnv_getenv("PERLIO_DEBUG");
398 dbg = PerlLIO_open3(s, O_WRONLY | O_CREAT | O_APPEND, 0666);
405 /* Use fixed buffer as sv_catpvf etc. needs SVs */
409 s = CopFILE(PL_curcop);
412 sprintf(buffer, "%s:%" IVdf " ", s, (IV) CopLINE(PL_curcop));
413 len = strlen(buffer);
414 vsprintf(buffer+len, fmt, ap);
415 PerlLIO_write(dbg, buffer, strlen(buffer));
417 SV *sv = newSVpvn("", 0);
420 s = CopFILE(PL_curcop);
423 Perl_sv_catpvf(aTHX_ sv, "%s:%" IVdf " ", s,
424 (IV) CopLINE(PL_curcop));
425 Perl_sv_vcatpvf(aTHX_ sv, fmt, &ap);
428 PerlLIO_write(dbg, s, len);
435 /*--------------------------------------------------------------------------------------*/
438 * Inner level routines
442 * Table of pointers to the PerlIO structs (malloc'ed)
444 #define PERLIO_TABLE_SIZE 64
447 PerlIO_allocate(pTHX)
450 * Find a free slot in the table, allocating new table as necessary
455 while ((f = *last)) {
457 last = (PerlIO **) (f);
458 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
464 Newz('I',f,PERLIO_TABLE_SIZE,PerlIO);
472 #undef PerlIO_fdupopen
474 PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
476 if (PerlIOValid(f)) {
477 PerlIO_funcs *tab = PerlIOBase(f)->tab;
479 PerlIO_debug("fdupopen f=%p param=%p\n",(void*)f,(void*)param);
480 new = (*tab->Dup)(aTHX_ PerlIO_allocate(aTHX),f,param, flags);
484 SETERRNO(EBADF, SS_IVCHAN);
490 PerlIO_cleantable(pTHX_ PerlIO **tablep)
492 PerlIO *table = *tablep;
495 PerlIO_cleantable(aTHX_(PerlIO **) & (table[0]));
496 for (i = PERLIO_TABLE_SIZE - 1; i > 0; i--) {
497 PerlIO *f = table + i;
509 PerlIO_list_alloc(pTHX)
512 Newz('L', list, 1, PerlIO_list_t);
518 PerlIO_list_free(pTHX_ PerlIO_list_t *list)
521 if (--list->refcnt == 0) {
524 for (i = 0; i < list->cur; i++) {
525 if (list->array[i].arg)
526 SvREFCNT_dec(list->array[i].arg);
528 Safefree(list->array);
536 PerlIO_list_push(pTHX_ PerlIO_list_t *list, PerlIO_funcs *funcs, SV *arg)
539 if (list->cur >= list->len) {
542 Renew(list->array, list->len, PerlIO_pair_t);
544 New('l', list->array, list->len, PerlIO_pair_t);
546 p = &(list->array[list->cur++]);
548 if ((p->arg = arg)) {
554 PerlIO_clone_list(pTHX_ PerlIO_list_t *proto, CLONE_PARAMS *param)
556 PerlIO_list_t *list = (PerlIO_list_t *) NULL;
559 list = PerlIO_list_alloc(aTHX);
560 for (i=0; i < proto->cur; i++) {
562 if (proto->array[i].arg)
563 arg = PerlIO_sv_dup(aTHX_ proto->array[i].arg,param);
564 PerlIO_list_push(aTHX_ list, proto->array[i].funcs, arg);
571 PerlIO_clone(pTHX_ PerlInterpreter *proto, CLONE_PARAMS *param)
574 PerlIO **table = &proto->Iperlio;
577 PL_known_layers = PerlIO_clone_list(aTHX_ proto->Iknown_layers, param);
578 PL_def_layerlist = PerlIO_clone_list(aTHX_ proto->Idef_layerlist, param);
579 PerlIO_allocate(aTHX); /* root slot is never used */
580 PerlIO_debug("Clone %p from %p\n",aTHX,proto);
581 while ((f = *table)) {
583 table = (PerlIO **) (f++);
584 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
586 (void) fp_dup(f, 0, param);
595 PerlIO_destruct(pTHX)
597 PerlIO **table = &PL_perlio;
600 PerlIO_debug("Destruct %p\n",aTHX);
602 while ((f = *table)) {
604 table = (PerlIO **) (f++);
605 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
609 if (l->tab->kind & PERLIO_K_DESTRUCT) {
610 PerlIO_debug("Destruct popping %s\n", l->tab->name);
624 PerlIO_pop(pTHX_ PerlIO *f)
628 PerlIO_debug("PerlIO_pop f=%p %s\n", (void*)f, l->tab->name);
629 if (l->tab->Popped) {
631 * If popped returns non-zero do not free its layer structure
632 * it has either done so itself, or it is shared and still in
635 if ((*l->tab->Popped) (aTHX_ f) != 0)
643 /*--------------------------------------------------------------------------------------*/
645 * XS Interface for perl code
649 PerlIO_find_layer(pTHX_ const char *name, STRLEN len, int load)
652 if ((SSize_t) len <= 0)
654 for (i = 0; i < PL_known_layers->cur; i++) {
655 PerlIO_funcs *f = PL_known_layers->array[i].funcs;
656 if (memEQ(f->name, name, len)) {
657 PerlIO_debug("%.*s => %p\n", (int) len, name, (void*)f);
661 if (load && PL_subname && PL_def_layerlist
662 && PL_def_layerlist->cur >= 2) {
663 if (PL_in_load_module) {
664 Perl_croak(aTHX_ "Recursive call to Perl_load_module in PerlIO_find_layer");
667 SV *pkgsv = newSVpvn("PerlIO", 6);
668 SV *layer = newSVpvn(name, len);
669 CV *cv = get_cv("PerlIO::Layer::NoWarnings", FALSE);
671 SAVEINT(PL_in_load_module);
673 SAVESPTR(PL_warnhook);
674 PL_warnhook = (SV *) cv;
678 * The two SVs are magically freed by load_module
680 Perl_load_module(aTHX_ 0, pkgsv, Nullsv, layer, Nullsv);
683 return PerlIO_find_layer(aTHX_ name, len, 0);
686 PerlIO_debug("Cannot find %.*s\n", (int) len, name);
690 #ifdef USE_ATTRIBUTES_FOR_PERLIO
693 perlio_mg_set(pTHX_ SV *sv, MAGIC *mg)
696 IO *io = GvIOn((GV *) SvRV(sv));
697 PerlIO *ifp = IoIFP(io);
698 PerlIO *ofp = IoOFP(io);
699 Perl_warn(aTHX_ "set %" SVf " %p %p %p", sv, io, ifp, ofp);
705 perlio_mg_get(pTHX_ SV *sv, MAGIC *mg)
708 IO *io = GvIOn((GV *) SvRV(sv));
709 PerlIO *ifp = IoIFP(io);
710 PerlIO *ofp = IoOFP(io);
711 Perl_warn(aTHX_ "get %" SVf " %p %p %p", sv, io, ifp, ofp);
717 perlio_mg_clear(pTHX_ SV *sv, MAGIC *mg)
719 Perl_warn(aTHX_ "clear %" SVf, sv);
724 perlio_mg_free(pTHX_ SV *sv, MAGIC *mg)
726 Perl_warn(aTHX_ "free %" SVf, sv);
730 MGVTBL perlio_vtab = {
738 XS(XS_io_MODIFY_SCALAR_ATTRIBUTES)
741 SV *sv = SvRV(ST(1));
746 sv_magic(sv, (SV *) av, PERL_MAGIC_ext, NULL, 0);
748 mg = mg_find(sv, PERL_MAGIC_ext);
749 mg->mg_virtual = &perlio_vtab;
751 Perl_warn(aTHX_ "attrib %" SVf, sv);
752 for (i = 2; i < items; i++) {
754 const char *name = SvPV(ST(i), len);
755 SV *layer = PerlIO_find_layer(aTHX_ name, len, 1);
757 av_push(av, SvREFCNT_inc(layer));
768 #endif /* USE_ATTIBUTES_FOR_PERLIO */
771 PerlIO_tab_sv(pTHX_ PerlIO_funcs *tab)
773 HV *stash = gv_stashpv("PerlIO::Layer", TRUE);
774 SV *sv = sv_bless(newRV_noinc(newSViv(PTR2IV(tab))), stash);
778 XS(XS_PerlIO__Layer__NoWarnings)
780 /* This is used as a %SIG{__WARN__} handler to supress warnings
781 during loading of layers.
785 PerlIO_debug("warning:%s\n",SvPV_nolen(ST(0)));
789 XS(XS_PerlIO__Layer__find)
793 Perl_croak(aTHX_ "Usage class->find(name[,load])");
796 char *name = SvPV(ST(1), len);
797 bool load = (items > 2) ? SvTRUE(ST(2)) : 0;
798 PerlIO_funcs *layer = PerlIO_find_layer(aTHX_ name, len, load);
800 (layer) ? sv_2mortal(PerlIO_tab_sv(aTHX_ layer)) :
807 PerlIO_define_layer(pTHX_ PerlIO_funcs *tab)
809 if (!PL_known_layers)
810 PL_known_layers = PerlIO_list_alloc(aTHX);
811 PerlIO_list_push(aTHX_ PL_known_layers, tab, Nullsv);
812 PerlIO_debug("define %s %p\n", tab->name, (void*)tab);
816 PerlIO_parse_layers(pTHX_ PerlIO_list_t *av, const char *names)
819 const char *s = names;
821 while (isSPACE(*s) || *s == ':')
826 const char *as = Nullch;
828 if (!isIDFIRST(*s)) {
830 * Message is consistent with how attribute lists are
831 * passed. Even though this means "foo : : bar" is
832 * seen as an invalid separator character.
834 char q = ((*s == '\'') ? '"' : '\'');
835 if (ckWARN(WARN_LAYER))
836 Perl_warner(aTHX_ packWARN(WARN_LAYER),
837 "perlio: invalid separator character %c%c%c in layer specification list %s",
839 SETERRNO(EINVAL, LIB_INVARG);
844 } while (isALNUM(*e));
860 * It's a nul terminated string, not allowed
861 * to \ the terminating null. Anything other
862 * character is passed over.
872 if (ckWARN(WARN_LAYER))
873 Perl_warner(aTHX_ packWARN(WARN_LAYER),
874 "perlio: argument list not closed for layer \"%.*s\"",
886 bool warn_layer = ckWARN(WARN_LAYER);
887 PerlIO_funcs *layer =
888 PerlIO_find_layer(aTHX_ s, llen, 1);
890 PerlIO_list_push(aTHX_ av, layer,
897 Perl_warner(aTHX_ packWARN(WARN_LAYER), "perlio: unknown layer \"%.*s\"",
910 PerlIO_default_buffer(pTHX_ PerlIO_list_t *av)
912 PerlIO_funcs *tab = &PerlIO_perlio;
913 #ifdef PERLIO_USING_CRLF
916 if (PerlIO_stdio.Set_ptrcnt)
919 PerlIO_debug("Pushing %s\n", tab->name);
920 PerlIO_list_push(aTHX_ av, PerlIO_find_layer(aTHX_ tab->name, 0, 0),
925 PerlIO_arg_fetch(PerlIO_list_t *av, IV n)
927 return av->array[n].arg;
931 PerlIO_layer_fetch(pTHX_ PerlIO_list_t *av, IV n, PerlIO_funcs *def)
933 if (n >= 0 && n < av->cur) {
934 PerlIO_debug("Layer %" IVdf " is %s\n", n,
935 av->array[n].funcs->name);
936 return av->array[n].funcs;
939 Perl_croak(aTHX_ "panic: PerlIO layer array corrupt");
944 PerlIOPop_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
946 if (PerlIOValid(f)) {
954 PerlIO_funcs PerlIO_remove = {
955 sizeof(PerlIO_funcs),
958 PERLIO_K_DUMMY | PERLIO_K_UTF8,
977 NULL, /* get_bufsiz */
980 NULL, /* set_ptrcnt */
984 PerlIO_default_layers(pTHX)
986 if (!PL_def_layerlist) {
987 const char *s = (PL_tainting) ? Nullch : PerlEnv_getenv("PERLIO");
988 PerlIO_funcs *osLayer = &PerlIO_unix;
989 PL_def_layerlist = PerlIO_list_alloc(aTHX);
990 PerlIO_define_layer(aTHX_ & PerlIO_unix);
991 #if defined(WIN32) && !defined(UNDER_CE)
992 PerlIO_define_layer(aTHX_ & PerlIO_win32);
994 osLayer = &PerlIO_win32;
997 PerlIO_define_layer(aTHX_ & PerlIO_raw);
998 PerlIO_define_layer(aTHX_ & PerlIO_perlio);
999 PerlIO_define_layer(aTHX_ & PerlIO_stdio);
1000 PerlIO_define_layer(aTHX_ & PerlIO_crlf);
1002 PerlIO_define_layer(aTHX_ & PerlIO_mmap);
1004 PerlIO_define_layer(aTHX_ & PerlIO_utf8);
1005 PerlIO_define_layer(aTHX_ & PerlIO_remove);
1006 PerlIO_define_layer(aTHX_ & PerlIO_byte);
1007 PerlIO_list_push(aTHX_ PL_def_layerlist,
1008 PerlIO_find_layer(aTHX_ osLayer->name, 0, 0),
1011 PerlIO_parse_layers(aTHX_ PL_def_layerlist, s);
1014 PerlIO_default_buffer(aTHX_ PL_def_layerlist);
1017 if (PL_def_layerlist->cur < 2) {
1018 PerlIO_default_buffer(aTHX_ PL_def_layerlist);
1020 return PL_def_layerlist;
1024 Perl_boot_core_PerlIO(pTHX)
1026 #ifdef USE_ATTRIBUTES_FOR_PERLIO
1027 newXS("io::MODIFY_SCALAR_ATTRIBUTES", XS_io_MODIFY_SCALAR_ATTRIBUTES,
1030 newXS("PerlIO::Layer::find", XS_PerlIO__Layer__find, __FILE__);
1031 newXS("PerlIO::Layer::NoWarnings", XS_PerlIO__Layer__NoWarnings, __FILE__);
1035 PerlIO_default_layer(pTHX_ I32 n)
1037 PerlIO_list_t *av = PerlIO_default_layers(aTHX);
1040 return PerlIO_layer_fetch(aTHX_ av, n, &PerlIO_stdio);
1043 #define PerlIO_default_top() PerlIO_default_layer(aTHX_ -1)
1044 #define PerlIO_default_btm() PerlIO_default_layer(aTHX_ 0)
1047 PerlIO_stdstreams(pTHX)
1050 PerlIO_allocate(aTHX);
1051 PerlIO_fdopen(0, "Ir" PERLIO_STDTEXT);
1052 PerlIO_fdopen(1, "Iw" PERLIO_STDTEXT);
1053 PerlIO_fdopen(2, "Iw" PERLIO_STDTEXT);
1058 PerlIO_push(pTHX_ PerlIO *f, PerlIO_funcs *tab, const char *mode, SV *arg)
1060 if (tab->fsize != sizeof(PerlIO_funcs)) {
1062 Perl_croak(aTHX_ "Layer does not match this perl");
1066 if (tab->size < sizeof(PerlIOl)) {
1069 /* Real layer with a data area */
1070 Newc('L',l,tab->size,char,PerlIOl);
1072 Zero(l, tab->size, char);
1076 PerlIO_debug("PerlIO_push f=%p %s %s %p\n", (void*)f, tab->name,
1077 (mode) ? mode : "(Null)", (void*)arg);
1078 if ((*l->tab->Pushed) (aTHX_ f, mode, arg, tab) != 0) {
1079 PerlIO_pop(aTHX_ f);
1085 /* Pseudo-layer where push does its own stack adjust */
1086 PerlIO_debug("PerlIO_push f=%p %s %s %p\n", (void*)f, tab->name,
1087 (mode) ? mode : "(Null)", (void*)arg);
1088 if ((*tab->Pushed) (aTHX_ f, mode, arg, tab) != 0) {
1096 PerlIOBase_binmode(pTHX_ PerlIO *f)
1098 if (PerlIOValid(f)) {
1099 /* Is layer suitable for raw stream ? */
1100 if (PerlIOBase(f)->tab->kind & PERLIO_K_RAW) {
1101 /* Yes - turn off UTF-8-ness, to undo UTF-8 locale effects */
1102 PerlIOBase(f)->flags &= ~PERLIO_F_UTF8;
1105 /* Not suitable - pop it */
1106 PerlIO_pop(aTHX_ f);
1114 PerlIORaw_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
1117 if (PerlIOValid(f)) {
1122 * Strip all layers that are not suitable for a raw stream
1125 while (t && (l = *t)) {
1126 if (l->tab->Binmode) {
1127 /* Has a handler - normal case */
1128 if ((*l->tab->Binmode)(aTHX_ f) == 0) {
1130 /* Layer still there - move down a layer */
1139 /* No handler - pop it */
1140 PerlIO_pop(aTHX_ t);
1143 if (PerlIOValid(f)) {
1144 PerlIO_debug(":raw f=%p :%s\n", (void*)f, PerlIOBase(f)->tab->name);
1152 PerlIO_apply_layera(pTHX_ PerlIO *f, const char *mode,
1153 PerlIO_list_t *layers, IV n, IV max)
1157 PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n, NULL);
1159 if (!PerlIO_push(aTHX_ f, tab, mode, PerlIOArg)) {
1170 PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names)
1174 PerlIO_list_t *layers = PerlIO_list_alloc(aTHX);
1175 code = PerlIO_parse_layers(aTHX_ layers, names);
1177 code = PerlIO_apply_layera(aTHX_ f, mode, layers, 0, layers->cur);
1179 PerlIO_list_free(aTHX_ layers);
1185 /*--------------------------------------------------------------------------------------*/
1187 * Given the abstraction above the public API functions
1191 PerlIO_binmode(pTHX_ PerlIO *f, int iotype, int mode, const char *names)
1193 PerlIO_debug("PerlIO_binmode f=%p %s %c %x %s\n",
1194 (void*)f, PerlIOBase(f)->tab->name, iotype, mode,
1195 (names) ? names : "(Null)");
1197 /* Do not flush etc. if (e.g.) switching encodings.
1198 if a pushed layer knows it needs to flush lower layers
1199 (for example :unix which is never going to call them)
1200 it can do the flush when it is pushed.
1202 return PerlIO_apply_layers(aTHX_ f, NULL, names) == 0 ? TRUE : FALSE;
1205 /* Fake 5.6 legacy of using this call to turn ON O_TEXT */
1206 #ifdef PERLIO_USING_CRLF
1207 /* Legacy binmode only has meaning if O_TEXT has a value distinct from
1208 O_BINARY so we can look for it in mode.
1210 if (!(mode & O_BINARY)) {
1212 /* FIXME?: Looking down the layer stack seems wrong,
1213 but is a way of reaching past (say) an encoding layer
1214 to flip CRLF-ness of the layer(s) below
1217 /* Perhaps we should turn on bottom-most aware layer
1218 e.g. Ilya's idea that UNIX TTY could serve
1220 if (PerlIOBase(f)->tab->kind & PERLIO_K_CANCRLF) {
1221 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF)) {
1222 /* Not in text mode - flush any pending stuff and flip it */
1224 PerlIOBase(f)->flags |= PERLIO_F_CRLF;
1226 /* Only need to turn it on in one layer so we are done */
1231 /* Not finding a CRLF aware layer presumably means we are binary
1232 which is not what was requested - so we failed
1233 We _could_ push :crlf layer but so could caller
1238 /* Legacy binmode is now _defined_ as being equivalent to pushing :raw
1239 So code that used to be here is now in PerlIORaw_pushed().
1241 return PerlIO_push(aTHX_ f, &PerlIO_raw, Nullch, Nullsv) ? TRUE : FALSE;
1246 PerlIO__close(pTHX_ PerlIO *f)
1249 return (*PerlIOBase(f)->tab->Close) (aTHX_ f);
1251 SETERRNO(EBADF, SS_IVCHAN);
1257 Perl_PerlIO_close(pTHX_ PerlIO *f)
1260 if (PerlIOValid(f)) {
1261 code = (*PerlIOBase(f)->tab->Close) (aTHX_ f);
1263 PerlIO_pop(aTHX_ f);
1270 Perl_PerlIO_fileno(pTHX_ PerlIO *f)
1273 return (*PerlIOBase(f)->tab->Fileno) (aTHX_ f);
1275 SETERRNO(EBADF, SS_IVCHAN);
1281 PerlIO_context_layers(pTHX_ const char *mode)
1283 const char *type = NULL;
1285 * Need to supply default layer info from open.pm
1288 SV *layers = PL_curcop->cop_io;
1291 type = SvPV(layers, len);
1292 if (type && mode[0] != 'r') {
1294 * Skip to write part
1296 const char *s = strchr(type, 0);
1297 if (s && (STRLEN)(s - type) < len) {
1306 static PerlIO_funcs *
1307 PerlIO_layer_from_ref(pTHX_ SV *sv)
1310 * For any scalar type load the handler which is bundled with perl
1312 if (SvTYPE(sv) < SVt_PVAV)
1313 return PerlIO_find_layer(aTHX_ "scalar", 6, 1);
1316 * For other types allow if layer is known but don't try and load it
1318 switch (SvTYPE(sv)) {
1320 return PerlIO_find_layer(aTHX_ "Array", 5, 0);
1322 return PerlIO_find_layer(aTHX_ "Hash", 4, 0);
1324 return PerlIO_find_layer(aTHX_ "Code", 4, 0);
1326 return PerlIO_find_layer(aTHX_ "Glob", 4, 0);
1332 PerlIO_resolve_layers(pTHX_ const char *layers,
1333 const char *mode, int narg, SV **args)
1335 PerlIO_list_t *def = PerlIO_default_layers(aTHX);
1338 PerlIO_stdstreams(aTHX);
1342 * If it is a reference but not an object see if we have a handler
1345 if (SvROK(arg) && !sv_isobject(arg)) {
1346 PerlIO_funcs *handler = PerlIO_layer_from_ref(aTHX_ SvRV(arg));
1348 def = PerlIO_list_alloc(aTHX);
1349 PerlIO_list_push(aTHX_ def, handler, &PL_sv_undef);
1353 * Don't fail if handler cannot be found :via(...) etc. may do
1354 * something sensible else we will just stringfy and open
1360 layers = PerlIO_context_layers(aTHX_ mode);
1361 if (layers && *layers) {
1365 av = PerlIO_list_alloc(aTHX);
1366 for (i = 0; i < def->cur; i++) {
1367 PerlIO_list_push(aTHX_ av, def->array[i].funcs,
1374 if (PerlIO_parse_layers(aTHX_ av, layers) == 0) {
1378 PerlIO_list_free(aTHX_ av);
1379 return (PerlIO_list_t *) NULL;
1390 PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd,
1391 int imode, int perm, PerlIO *f, int narg, SV **args)
1393 if (!f && narg == 1 && *args == &PL_sv_undef) {
1394 if ((f = PerlIO_tmpfile())) {
1396 layers = PerlIO_context_layers(aTHX_ mode);
1397 if (layers && *layers)
1398 PerlIO_apply_layers(aTHX_ f, mode, layers);
1402 PerlIO_list_t *layera = NULL;
1404 PerlIO_funcs *tab = NULL;
1405 if (PerlIOValid(f)) {
1407 * This is "reopen" - it is not tested as perl does not use it
1411 layera = PerlIO_list_alloc(aTHX);
1413 SV *arg = (l->tab->Getarg)
1414 ? (*l->tab->Getarg) (aTHX_ &l, NULL, 0)
1416 PerlIO_list_push(aTHX_ layera, l->tab, arg);
1417 l = *PerlIONext(&l);
1421 layera = PerlIO_resolve_layers(aTHX_ layers, mode, narg, args);
1427 * Start at "top" of layer stack
1429 n = layera->cur - 1;
1431 PerlIO_funcs *t = PerlIO_layer_fetch(aTHX_ layera, n, NULL);
1440 * Found that layer 'n' can do opens - call it
1442 if (narg > 1 && !(tab->kind & PERLIO_K_MULTIARG)) {
1443 Perl_croak(aTHX_ "More than one argument to open(,':%s')",tab->name);
1445 PerlIO_debug("openn(%s,'%s','%s',%d,%x,%o,%p,%d,%p)\n",
1446 tab->name, layers, mode, fd, imode, perm,
1447 (void*)f, narg, (void*)args);
1448 f = (*tab->Open) (aTHX_ tab, layera, n, mode, fd, imode, perm,
1451 if (n + 1 < layera->cur) {
1453 * More layers above the one that we used to open -
1456 if (PerlIO_apply_layera(aTHX_ f, mode, layera, n + 1, layera->cur) != 0) {
1457 /* If pushing layers fails close the file */
1464 PerlIO_list_free(aTHX_ layera);
1471 Perl_PerlIO_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
1474 return (*PerlIOBase(f)->tab->Read) (aTHX_ f, vbuf, count);
1476 SETERRNO(EBADF, SS_IVCHAN);
1482 Perl_PerlIO_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
1485 return (*PerlIOBase(f)->tab->Unread) (aTHX_ f, vbuf, count);
1487 SETERRNO(EBADF, SS_IVCHAN);
1493 Perl_PerlIO_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
1496 return (*PerlIOBase(f)->tab->Write) (aTHX_ f, vbuf, count);
1498 SETERRNO(EBADF, SS_IVCHAN);
1504 Perl_PerlIO_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
1507 return (*PerlIOBase(f)->tab->Seek) (aTHX_ f, offset, whence);
1509 SETERRNO(EBADF, SS_IVCHAN);
1515 Perl_PerlIO_tell(pTHX_ PerlIO *f)
1518 return (*PerlIOBase(f)->tab->Tell) (aTHX_ f);
1520 SETERRNO(EBADF, SS_IVCHAN);
1526 Perl_PerlIO_flush(pTHX_ PerlIO *f)
1530 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1531 if (tab && tab->Flush) {
1532 return (*tab->Flush) (aTHX_ f);
1535 PerlIO_debug("Cannot flush f=%p :%s\n", (void*)f, tab->name);
1536 SETERRNO(EBADF, SS_IVCHAN);
1541 PerlIO_debug("Cannot flush f=%p\n", (void*)f);
1542 SETERRNO(EBADF, SS_IVCHAN);
1548 * Is it good API design to do flush-all on NULL, a potentially
1549 * errorneous input? Maybe some magical value (PerlIO*
1550 * PERLIO_FLUSH_ALL = (PerlIO*)-1;)? Yes, stdio does similar
1551 * things on fflush(NULL), but should we be bound by their design
1554 PerlIO **table = &PL_perlio;
1556 while ((f = *table)) {
1558 table = (PerlIO **) (f++);
1559 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
1560 if (*f && PerlIO_flush(f) != 0)
1570 PerlIOBase_flush_linebuf(pTHX)
1572 PerlIO **table = &PL_perlio;
1574 while ((f = *table)) {
1576 table = (PerlIO **) (f++);
1577 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
1580 flags & (PERLIO_F_LINEBUF | PERLIO_F_CANWRITE))
1581 == (PERLIO_F_LINEBUF | PERLIO_F_CANWRITE))
1589 Perl_PerlIO_fill(pTHX_ PerlIO *f)
1592 return (*PerlIOBase(f)->tab->Fill) (aTHX_ f);
1594 SETERRNO(EBADF, SS_IVCHAN);
1600 PerlIO_isutf8(PerlIO *f)
1603 return (PerlIOBase(f)->flags & PERLIO_F_UTF8) != 0;
1605 SETERRNO(EBADF, SS_IVCHAN);
1611 Perl_PerlIO_eof(pTHX_ PerlIO *f)
1614 return (*PerlIOBase(f)->tab->Eof) (aTHX_ f);
1616 SETERRNO(EBADF, SS_IVCHAN);
1622 Perl_PerlIO_error(pTHX_ PerlIO *f)
1625 return (*PerlIOBase(f)->tab->Error) (aTHX_ f);
1627 SETERRNO(EBADF, SS_IVCHAN);
1633 Perl_PerlIO_clearerr(pTHX_ PerlIO *f)
1636 (*PerlIOBase(f)->tab->Clearerr) (aTHX_ f);
1638 SETERRNO(EBADF, SS_IVCHAN);
1642 Perl_PerlIO_setlinebuf(pTHX_ PerlIO *f)
1645 (*PerlIOBase(f)->tab->Setlinebuf) (aTHX_ f);
1647 SETERRNO(EBADF, SS_IVCHAN);
1651 PerlIO_has_base(PerlIO *f)
1653 if (PerlIOValid(f)) {
1654 return (PerlIOBase(f)->tab->Get_base != NULL);
1660 PerlIO_fast_gets(PerlIO *f)
1662 if (PerlIOValid(f) && (PerlIOBase(f)->flags & PERLIO_F_FASTGETS)) {
1663 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1664 return (tab->Set_ptrcnt != NULL);
1670 PerlIO_has_cntptr(PerlIO *f)
1672 if (PerlIOValid(f)) {
1673 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1674 return (tab->Get_ptr != NULL && tab->Get_cnt != NULL);
1680 PerlIO_canset_cnt(PerlIO *f)
1682 if (PerlIOValid(f)) {
1683 PerlIOl *l = PerlIOBase(f);
1684 return (l->tab->Set_ptrcnt != NULL);
1690 Perl_PerlIO_get_base(pTHX_ PerlIO *f)
1693 return (*PerlIOBase(f)->tab->Get_base) (aTHX_ f);
1698 Perl_PerlIO_get_bufsiz(pTHX_ PerlIO *f)
1701 return (*PerlIOBase(f)->tab->Get_bufsiz) (aTHX_ f);
1706 Perl_PerlIO_get_ptr(pTHX_ PerlIO *f)
1708 if (PerlIOValid(f)) {
1709 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1710 if (tab->Get_ptr == NULL)
1712 return (*tab->Get_ptr) (aTHX_ f);
1718 Perl_PerlIO_get_cnt(pTHX_ PerlIO *f)
1720 if (PerlIOValid(f)) {
1721 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1722 if (tab->Get_cnt == NULL)
1724 return (*tab->Get_cnt) (aTHX_ f);
1730 Perl_PerlIO_set_cnt(pTHX_ PerlIO *f, int cnt)
1732 if (PerlIOValid(f)) {
1733 (*PerlIOBase(f)->tab->Set_ptrcnt) (aTHX_ f, NULL, cnt);
1738 Perl_PerlIO_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, int cnt)
1740 if (PerlIOValid(f)) {
1741 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1742 if (tab->Set_ptrcnt == NULL) {
1743 Perl_croak(aTHX_ "PerlIO buffer snooping abuse");
1745 (*PerlIOBase(f)->tab->Set_ptrcnt) (aTHX_ f, ptr, cnt);
1750 /*--------------------------------------------------------------------------------------*/
1752 * utf8 and raw dummy layers
1756 PerlIOUtf8_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
1758 if (PerlIOValid(f)) {
1759 if (tab->kind & PERLIO_K_UTF8)
1760 PerlIOBase(f)->flags |= PERLIO_F_UTF8;
1762 PerlIOBase(f)->flags &= ~PERLIO_F_UTF8;
1768 PerlIO_funcs PerlIO_utf8 = {
1769 sizeof(PerlIO_funcs),
1772 PERLIO_K_DUMMY | PERLIO_K_UTF8,
1790 NULL, /* get_base */
1791 NULL, /* get_bufsiz */
1794 NULL, /* set_ptrcnt */
1797 PerlIO_funcs PerlIO_byte = {
1798 sizeof(PerlIO_funcs),
1819 NULL, /* get_base */
1820 NULL, /* get_bufsiz */
1823 NULL, /* set_ptrcnt */
1827 PerlIORaw_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
1828 IV n, const char *mode, int fd, int imode, int perm,
1829 PerlIO *old, int narg, SV **args)
1831 PerlIO_funcs *tab = PerlIO_default_btm();
1832 return (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
1836 PerlIO_funcs PerlIO_raw = {
1837 sizeof(PerlIO_funcs),
1858 NULL, /* get_base */
1859 NULL, /* get_bufsiz */
1862 NULL, /* set_ptrcnt */
1864 /*--------------------------------------------------------------------------------------*/
1865 /*--------------------------------------------------------------------------------------*/
1867 * "Methods" of the "base class"
1871 PerlIOBase_fileno(pTHX_ PerlIO *f)
1873 return PerlIOValid(f) ? PerlIO_fileno(PerlIONext(f)) : -1;
1877 PerlIO_modestr(PerlIO * f, char *buf)
1880 if (PerlIOValid(f)) {
1881 IV flags = PerlIOBase(f)->flags;
1882 if (flags & PERLIO_F_APPEND) {
1884 if (flags & PERLIO_F_CANREAD) {
1888 else if (flags & PERLIO_F_CANREAD) {
1890 if (flags & PERLIO_F_CANWRITE)
1893 else if (flags & PERLIO_F_CANWRITE) {
1895 if (flags & PERLIO_F_CANREAD) {
1899 #ifdef PERLIO_USING_CRLF
1900 if (!(flags & PERLIO_F_CRLF))
1910 PerlIOBase_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
1912 PerlIOl *l = PerlIOBase(f);
1914 const char *omode = mode;
1917 l->flags &= ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE |
1918 PERLIO_F_TRUNCATE | PERLIO_F_APPEND);
1919 if (tab->Set_ptrcnt != NULL)
1920 l->flags |= PERLIO_F_FASTGETS;
1922 if (*mode == '#' || *mode == 'I')
1926 l->flags |= PERLIO_F_CANREAD;
1929 l->flags |= PERLIO_F_APPEND | PERLIO_F_CANWRITE;
1932 l->flags |= PERLIO_F_TRUNCATE | PERLIO_F_CANWRITE;
1935 SETERRNO(EINVAL, LIB_INVARG);
1941 l->flags |= PERLIO_F_CANREAD | PERLIO_F_CANWRITE;
1944 l->flags &= ~PERLIO_F_CRLF;
1947 l->flags |= PERLIO_F_CRLF;
1950 SETERRNO(EINVAL, LIB_INVARG);
1957 l->flags |= l->next->flags &
1958 (PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_TRUNCATE |
1963 PerlIO_debug("PerlIOBase_pushed f=%p %s %s fl=%08" UVxf " (%s)\n",
1964 f, PerlIOBase(f)->tab->name, (omode) ? omode : "(Null)",
1965 l->flags, PerlIO_modestr(f, temp));
1971 PerlIOBase_popped(pTHX_ PerlIO *f)
1977 PerlIOBase_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
1980 * Save the position as current head considers it
1982 Off_t old = PerlIO_tell(f);
1984 PerlIO_push(aTHX_ f, &PerlIO_pending, "r", Nullsv);
1985 PerlIOSelf(f, PerlIOBuf)->posn = old;
1986 done = PerlIOBuf_unread(aTHX_ f, vbuf, count);
1991 PerlIOBase_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
1993 STDCHAR *buf = (STDCHAR *) vbuf;
1995 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD))
1998 SSize_t avail = PerlIO_get_cnt(f);
2001 take = ((SSize_t)count < avail) ? count : avail;
2003 STDCHAR *ptr = PerlIO_get_ptr(f);
2004 Copy(ptr, buf, take, STDCHAR);
2005 PerlIO_set_ptrcnt(f, ptr + take, (avail -= take));
2009 if (count > 0 && avail <= 0) {
2010 if (PerlIO_fill(f) != 0)
2014 return (buf - (STDCHAR *) vbuf);
2020 PerlIOBase_noop_ok(pTHX_ PerlIO *f)
2026 PerlIOBase_noop_fail(pTHX_ PerlIO *f)
2032 PerlIOBase_close(pTHX_ PerlIO *f)
2035 PerlIO *n = PerlIONext(f);
2036 if (PerlIO_flush(f) != 0)
2038 if (PerlIOValid(n) && (*PerlIOBase(n)->tab->Close)(aTHX_ n) != 0)
2040 PerlIOBase(f)->flags &=
2041 ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_OPEN);
2046 PerlIOBase_eof(pTHX_ PerlIO *f)
2048 if (PerlIOValid(f)) {
2049 return (PerlIOBase(f)->flags & PERLIO_F_EOF) != 0;
2055 PerlIOBase_error(pTHX_ PerlIO *f)
2057 if (PerlIOValid(f)) {
2058 return (PerlIOBase(f)->flags & PERLIO_F_ERROR) != 0;
2064 PerlIOBase_clearerr(pTHX_ PerlIO *f)
2066 if (PerlIOValid(f)) {
2067 PerlIO *n = PerlIONext(f);
2068 PerlIOBase(f)->flags &= ~(PERLIO_F_ERROR | PERLIO_F_EOF);
2075 PerlIOBase_setlinebuf(pTHX_ PerlIO *f)
2077 if (PerlIOValid(f)) {
2078 PerlIOBase(f)->flags |= PERLIO_F_LINEBUF;
2083 PerlIO_sv_dup(pTHX_ SV *arg, CLONE_PARAMS *param)
2089 return sv_dup(arg, param);
2092 return newSVsv(arg);
2095 return newSVsv(arg);
2100 PerlIOBase_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
2102 PerlIO *nexto = PerlIONext(o);
2103 if (PerlIOValid(nexto)) {
2104 PerlIO_funcs *tab = PerlIOBase(nexto)->tab;
2105 f = (*tab->Dup)(aTHX_ f, nexto, param, flags);
2108 PerlIO_funcs *self = PerlIOBase(o)->tab;
2111 PerlIO_debug("PerlIOBase_dup %s f=%p o=%p param=%p\n",
2112 self->name, (void*)f, (void*)o, (void*)param);
2114 arg = (*self->Getarg)(aTHX_ o,param,flags);
2116 f = PerlIO_push(aTHX_ f, self, PerlIO_modestr(o,buf), arg);
2124 #define PERLIO_MAX_REFCOUNTABLE_FD 2048
2126 perl_mutex PerlIO_mutex;
2128 int PerlIO_fd_refcnt[PERLIO_MAX_REFCOUNTABLE_FD];
2133 /* Place holder for stdstreams call ??? */
2135 MUTEX_INIT(&PerlIO_mutex);
2140 PerlIOUnix_refcnt_inc(int fd)
2142 if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
2144 MUTEX_LOCK(&PerlIO_mutex);
2146 PerlIO_fd_refcnt[fd]++;
2147 PerlIO_debug("fd %d refcnt=%d\n",fd,PerlIO_fd_refcnt[fd]);
2149 MUTEX_UNLOCK(&PerlIO_mutex);
2155 PerlIOUnix_refcnt_dec(int fd)
2158 if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
2160 MUTEX_LOCK(&PerlIO_mutex);
2162 cnt = --PerlIO_fd_refcnt[fd];
2163 PerlIO_debug("fd %d refcnt=%d\n",fd,cnt);
2165 MUTEX_UNLOCK(&PerlIO_mutex);
2172 PerlIO_cleanup(pTHX)
2176 PerlIO_debug("Cleanup layers for %p\n",aTHX);
2178 PerlIO_debug("Cleanup layers\n");
2180 /* Raise STDIN..STDERR refcount so we don't close them */
2181 for (i=0; i < 3; i++)
2182 PerlIOUnix_refcnt_inc(i);
2183 PerlIO_cleantable(aTHX_ &PL_perlio);
2184 /* Restore STDIN..STDERR refcount */
2185 for (i=0; i < 3; i++)
2186 PerlIOUnix_refcnt_dec(i);
2188 if (PL_known_layers) {
2189 PerlIO_list_free(aTHX_ PL_known_layers);
2190 PL_known_layers = NULL;
2192 if(PL_def_layerlist) {
2193 PerlIO_list_free(aTHX_ PL_def_layerlist);
2194 PL_def_layerlist = NULL;
2200 /*--------------------------------------------------------------------------------------*/
2202 * Bottom-most level for UNIX-like case
2206 struct _PerlIO base; /* The generic part */
2207 int fd; /* UNIX like file descriptor */
2208 int oflags; /* open/fcntl flags */
2212 PerlIOUnix_oflags(const char *mode)
2215 if (*mode == 'I' || *mode == '#')
2220 if (*++mode == '+') {
2227 oflags = O_CREAT | O_TRUNC;
2228 if (*++mode == '+') {
2237 oflags = O_CREAT | O_APPEND;
2238 if (*++mode == '+') {
2251 else if (*mode == 't') {
2253 oflags &= ~O_BINARY;
2257 * Always open in binary mode
2260 if (*mode || oflags == -1) {
2261 SETERRNO(EINVAL, LIB_INVARG);
2268 PerlIOUnix_fileno(pTHX_ PerlIO *f)
2270 return PerlIOSelf(f, PerlIOUnix)->fd;
2274 PerlIOUnix_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
2276 IV code = PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
2277 PerlIOUnix *s = PerlIOSelf(f, PerlIOUnix);
2278 if (*PerlIONext(f)) {
2279 /* We never call down so do any pending stuff now */
2280 PerlIO_flush(PerlIONext(f));
2281 s->fd = PerlIO_fileno(PerlIONext(f));
2283 * XXX could (or should) we retrieve the oflags from the open file
2284 * handle rather than believing the "mode" we are passed in? XXX
2285 * Should the value on NULL mode be 0 or -1?
2287 s->oflags = mode ? PerlIOUnix_oflags(mode) : -1;
2289 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
2294 PerlIOUnix_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2295 IV n, const char *mode, int fd, int imode,
2296 int perm, PerlIO *f, int narg, SV **args)
2298 if (PerlIOValid(f)) {
2299 if (PerlIOBase(f)->flags & PERLIO_F_OPEN)
2300 (*PerlIOBase(f)->tab->Close)(aTHX_ f);
2303 char *path = SvPV_nolen(*args);
2307 imode = PerlIOUnix_oflags(mode);
2311 fd = PerlLIO_open3(path, imode, perm);
2319 f = PerlIO_allocate(aTHX);
2321 if (!PerlIOValid(f)) {
2322 if (!(f = PerlIO_push(aTHX_ f, self, mode, PerlIOArg))) {
2326 s = PerlIOSelf(f, PerlIOUnix);
2329 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
2330 PerlIOUnix_refcnt_inc(fd);
2336 * FIXME: pop layers ???
2344 PerlIOUnix_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
2346 PerlIOUnix *os = PerlIOSelf(o, PerlIOUnix);
2348 if (flags & PERLIO_DUP_FD) {
2349 fd = PerlLIO_dup(fd);
2351 if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
2352 f = PerlIOBase_dup(aTHX_ f, o, param, flags);
2354 /* If all went well overwrite fd in dup'ed lay with the dup()'ed fd */
2355 PerlIOUnix *s = PerlIOSelf(f, PerlIOUnix);
2357 PerlIOUnix_refcnt_inc(fd);
2366 PerlIOUnix_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
2368 int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2369 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD) ||
2370 PerlIOBase(f)->flags & (PERLIO_F_EOF|PERLIO_F_ERROR)) {
2374 SSize_t len = PerlLIO_read(fd, vbuf, count);
2375 if (len >= 0 || errno != EINTR) {
2377 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2378 else if (len == 0 && count != 0)
2379 PerlIOBase(f)->flags |= PERLIO_F_EOF;
2387 PerlIOUnix_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
2389 int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2391 SSize_t len = PerlLIO_write(fd, vbuf, count);
2392 if (len >= 0 || errno != EINTR) {
2394 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2402 PerlIOUnix_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
2405 PerlLIO_lseek(PerlIOSelf(f, PerlIOUnix)->fd, offset, whence);
2406 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
2407 return (new == (Off_t) - 1) ? -1 : 0;
2411 PerlIOUnix_tell(pTHX_ PerlIO *f)
2413 return PerlLIO_lseek(PerlIOSelf(f, PerlIOUnix)->fd, 0, SEEK_CUR);
2418 PerlIOUnix_close(pTHX_ PerlIO *f)
2420 int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2422 if (PerlIOBase(f)->flags & PERLIO_F_OPEN) {
2423 if (PerlIOUnix_refcnt_dec(fd) > 0) {
2424 PerlIOBase(f)->flags &= ~PERLIO_F_OPEN;
2429 SETERRNO(EBADF,SS_IVCHAN);
2432 while (PerlLIO_close(fd) != 0) {
2433 if (errno != EINTR) {
2440 PerlIOBase(f)->flags &= ~PERLIO_F_OPEN;
2445 PerlIO_funcs PerlIO_unix = {
2446 sizeof(PerlIO_funcs),
2453 PerlIOBase_binmode, /* binmode */
2463 PerlIOBase_noop_ok, /* flush */
2464 PerlIOBase_noop_fail, /* fill */
2467 PerlIOBase_clearerr,
2468 PerlIOBase_setlinebuf,
2469 NULL, /* get_base */
2470 NULL, /* get_bufsiz */
2473 NULL, /* set_ptrcnt */
2476 /*--------------------------------------------------------------------------------------*/
2481 #if defined(VMS) && !defined(STDIO_BUFFER_WRITABLE)
2482 /* perl5.8 - This ensures the last minute VMS ungetc fix is not
2483 broken by the last second glibc 2.3 fix
2485 #define STDIO_BUFFER_WRITABLE
2490 struct _PerlIO base;
2491 FILE *stdio; /* The stream */
2495 PerlIOStdio_fileno(pTHX_ PerlIO *f)
2498 if (PerlIOValid(f) && (s = PerlIOSelf(f, PerlIOStdio)->stdio)) {
2499 return PerlSIO_fileno(s);
2506 PerlIOStdio_mode(const char *mode, char *tmode)
2512 #ifdef PERLIO_USING_CRLF
2520 PerlIOStdio_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
2523 if (PerlIOValid(f) && PerlIOValid(n = PerlIONext(f))) {
2524 PerlIO_funcs *toptab = PerlIOBase(n)->tab;
2525 if (toptab == tab) {
2526 /* Top is already stdio - pop self (duplicate) and use original */
2527 PerlIO_pop(aTHX_ f);
2530 int fd = PerlIO_fileno(n);
2533 if (fd >= 0 && (stdio = PerlSIO_fdopen(fd,
2534 mode = PerlIOStdio_mode(mode, tmode)))) {
2535 PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
2536 /* We never call down so do any pending stuff now */
2537 PerlIO_flush(PerlIONext(f));
2544 return PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
2549 PerlIO_importFILE(FILE *stdio, const char *mode)
2555 if (!mode || !*mode) {
2556 /* We need to probe to see how we can open the stream
2557 so start with read/write and then try write and read
2558 we dup() so that we can fclose without loosing the fd.
2560 Note that the errno value set by a failing fdopen
2561 varies between stdio implementations.
2563 int fd = PerlLIO_dup(fileno(stdio));
2564 FILE *f2 = PerlSIO_fdopen(fd, (mode = "r+"));
2566 f2 = PerlSIO_fdopen(fd, (mode = "w"));
2569 f2 = PerlSIO_fdopen(fd, (mode = "r"));
2572 /* Don't seem to be able to open */
2578 if ((f = PerlIO_push(aTHX_(f = PerlIO_allocate(aTHX)), &PerlIO_stdio, mode, Nullsv))) {
2579 s = PerlIOSelf(f, PerlIOStdio);
2587 PerlIOStdio_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2588 IV n, const char *mode, int fd, int imode,
2589 int perm, PerlIO *f, int narg, SV **args)
2592 if (PerlIOValid(f)) {
2593 char *path = SvPV_nolen(*args);
2594 PerlIOStdio *s = PerlIOSelf(f, PerlIOStdio);
2596 PerlIOUnix_refcnt_dec(fileno(s->stdio));
2597 stdio = PerlSIO_freopen(path, (mode = PerlIOStdio_mode(mode, tmode)),
2602 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2607 char *path = SvPV_nolen(*args);
2610 fd = PerlLIO_open3(path, imode, perm);
2613 FILE *stdio = PerlSIO_fopen(path, mode);
2617 f = PerlIO_allocate(aTHX);
2619 if ((f = PerlIO_push(aTHX_ f, self,
2620 (mode = PerlIOStdio_mode(mode, tmode)),
2622 s = PerlIOSelf(f, PerlIOStdio);
2624 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2643 stdio = PerlSIO_stdin;
2646 stdio = PerlSIO_stdout;
2649 stdio = PerlSIO_stderr;
2654 stdio = PerlSIO_fdopen(fd, mode =
2655 PerlIOStdio_mode(mode, tmode));
2660 f = PerlIO_allocate(aTHX);
2662 if ((f = PerlIO_push(aTHX_ f, self, mode, PerlIOArg))) {
2663 s = PerlIOSelf(f, PerlIOStdio);
2665 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2675 PerlIOStdio_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
2677 /* This assumes no layers underneath - which is what
2678 happens, but is not how I remember it. NI-S 2001/10/16
2680 if ((f = PerlIOBase_dup(aTHX_ f, o, param, flags))) {
2681 FILE *stdio = PerlIOSelf(o, PerlIOStdio)->stdio;
2682 int fd = fileno(stdio);
2684 if (flags & PERLIO_DUP_FD) {
2685 int dfd = PerlLIO_dup(fileno(stdio));
2687 stdio = PerlSIO_fdopen(dfd, PerlIO_modestr(o,mode));
2691 /* FIXME: To avoid messy error recovery if dup fails
2692 re-use the existing stdio as though flag was not set
2696 stdio = PerlSIO_fdopen(fd, PerlIO_modestr(o,mode));
2698 PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
2699 PerlIOUnix_refcnt_inc(fileno(stdio));
2705 PerlIOStdio_invalidate_fileno(pTHX_ FILE *f)
2707 /* XXX this could use PerlIO_canset_fileno() and
2708 * PerlIO_set_fileno() support from Configure
2710 # if defined(__GLIBC__)
2711 /* There may be a better way for GLIBC:
2712 - libio.h defines a flag to not close() on cleanup
2716 # elif defined(__sun__)
2718 /* On solaris, if _LP64 is defined, the FILE structure is this:
2724 * It turns out that the fd is stored in the top 32 bits of
2725 * file->__pad[4]. The lower 32 bits contain flags. file->pad[5] appears
2726 * to contain a pointer or offset into another structure. All the
2727 * remaining fields are zero.
2729 * We set the top bits to -1 (0xFFFFFFFF).
2731 f->__pad[4] |= 0xffffffff00000000L;
2732 assert(fileno(f) == 0xffffffff);
2733 # else /* !defined(_LP64) */
2734 /* _file is just a unsigned char :-(
2735 Not clear why we dup() rather than using -1
2736 even if that would be treated as 0xFF - so will
2739 f->_file = PerlLIO_dup(fileno(f));
2740 # endif /* defined(_LP64) */
2742 # elif defined(__hpux)
2746 /* Next one ->_file seems to be a reasonable fallback, i.e. if
2747 your platform does not have special entry try this one.
2748 [For OSF only have confirmation for Tru64 (alpha)
2749 but assume other OSFs will be similar.]
2751 # elif defined(_AIX) || defined(__osf__) || defined(__irix__)
2754 # elif defined(__FreeBSD__)
2755 /* There may be a better way on FreeBSD:
2756 - we could insert a dummy func in the _close function entry
2757 f->_close = (int (*)(void *)) dummy_close;
2761 # elif defined(__CYGWIN__)
2762 /* There may be a better way on CYGWIN:
2763 - we could insert a dummy func in the _close function entry
2764 f->_close = (int (*)(void *)) dummy_close;
2768 # elif defined(WIN32)
2769 # if defined(__BORLANDC__)
2770 f->fd = PerlLIO_dup(fileno(f));
2777 /* Sarathy's code did this - we fall back to a dup/dup2 hack
2778 (which isn't thread safe) instead
2780 # error "Don't know how to set FILE.fileno on your platform"
2787 PerlIOStdio_close(pTHX_ PerlIO *f)
2789 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2795 int fd = fileno(stdio);
2801 #ifdef SOCKS5_VERSION_NAME
2802 /* Socks lib overrides close() but stdio isn't linked to
2803 that library (though we are) - so we must call close()
2804 on sockets on stdio's behalf.
2807 Sock_size_t optlen = sizeof(int);
2808 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, (void *) &optval, &optlen) == 0) {
2813 if (PerlIOUnix_refcnt_dec(fd) > 0) {
2814 /* File descriptor still in use */
2819 /* For STD* handles don't close the stdio at all
2820 this is because we have shared the FILE * too
2822 if (stdio == stdin) {
2823 /* Some stdios are buggy fflush-ing inputs */
2826 else if (stdio == stdout || stdio == stderr) {
2827 return PerlIO_flush(f);
2829 /* Tricky - must fclose(stdio) to free memory but not close(fd)
2830 Use Sarathy's trick from maint-5.6 to invalidate the
2831 fileno slot of the FILE *
2833 result = PerlIO_flush(f);
2835 if (!(invalidate = PerlIOStdio_invalidate_fileno(aTHX_ stdio))) {
2836 dupfd = PerlLIO_dup(fd);
2839 result = PerlSIO_fclose(stdio);
2840 /* We treat error from stdio as success if we invalidated
2841 errno may NOT be expected EBADF
2843 if (invalidate && result != 0) {
2848 /* in SOCKS case let close() determine return value */
2852 PerlLIO_dup2(dupfd,fd);
2860 PerlIOStdio_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
2862 FILE *s = PerlIOSelf(f, PerlIOStdio)->stdio;
2866 STDCHAR *buf = (STDCHAR *) vbuf;
2868 * Perl is expecting PerlIO_getc() to fill the buffer Linux's
2869 * stdio does not do that for fread()
2871 int ch = PerlSIO_fgetc(s);
2878 got = PerlSIO_fread(vbuf, 1, count, s);
2879 if (got || errno != EINTR)
2882 errno = 0; /* just in case */
2888 PerlIOStdio_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
2891 FILE *s = PerlIOSelf(f, PerlIOStdio)->stdio;
2893 #ifdef STDIO_BUFFER_WRITABLE
2894 if (PerlIO_fast_gets(f) && PerlIO_has_base(f)) {
2895 STDCHAR *buf = ((STDCHAR *) vbuf) + count;
2896 STDCHAR *base = PerlIO_get_base(f);
2897 SSize_t cnt = PerlIO_get_cnt(f);
2898 STDCHAR *ptr = PerlIO_get_ptr(f);
2899 SSize_t avail = ptr - base;
2901 if (avail > count) {
2905 Move(buf-avail,ptr,avail,STDCHAR);
2908 PerlIO_set_ptrcnt(f,ptr,cnt+avail);
2909 if (PerlSIO_feof(s) && unread >= 0)
2910 PerlSIO_clearerr(s);
2915 if (PerlIO_has_cntptr(f)) {
2916 /* We can get pointer to buffer but not its base
2917 Do ungetc() but check chars are ending up in the
2920 STDCHAR *eptr = (STDCHAR*)PerlSIO_get_ptr(s);
2921 STDCHAR *buf = ((STDCHAR *) vbuf) + count;
2923 int ch = *--buf & 0xFF;
2924 if (ungetc(ch,s) != ch) {
2925 /* ungetc did not work */
2928 if ((STDCHAR*)PerlSIO_get_ptr(s) != --eptr || ((*eptr & 0xFF) != ch)) {
2929 /* Did not change pointer as expected */
2930 fgetc(s); /* get char back again */
2940 unread += PerlIOBase_unread(aTHX_ f, vbuf, count);
2946 PerlIOStdio_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
2950 got = PerlSIO_fwrite(vbuf, 1, count,
2951 PerlIOSelf(f, PerlIOStdio)->stdio);
2952 if (got || errno != EINTR)
2955 errno = 0; /* just in case */
2961 PerlIOStdio_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
2963 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2964 return PerlSIO_fseek(stdio, offset, whence);
2968 PerlIOStdio_tell(pTHX_ PerlIO *f)
2970 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2971 return PerlSIO_ftell(stdio);
2975 PerlIOStdio_flush(pTHX_ PerlIO *f)
2977 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2978 if (PerlIOBase(f)->flags & PERLIO_F_CANWRITE) {
2979 return PerlSIO_fflush(stdio);
2984 * FIXME: This discards ungetc() and pre-read stuff which is not
2985 * right if this is just a "sync" from a layer above Suspect right
2986 * design is to do _this_ but not have layer above flush this
2987 * layer read-to-read
2990 * Not writeable - sync by attempting a seek
2993 if (PerlSIO_fseek(stdio, (Off_t) 0, SEEK_CUR) != 0)
3001 PerlIOStdio_eof(pTHX_ PerlIO *f)
3003 return PerlSIO_feof(PerlIOSelf(f, PerlIOStdio)->stdio);
3007 PerlIOStdio_error(pTHX_ PerlIO *f)
3009 return PerlSIO_ferror(PerlIOSelf(f, PerlIOStdio)->stdio);
3013 PerlIOStdio_clearerr(pTHX_ PerlIO *f)
3015 PerlSIO_clearerr(PerlIOSelf(f, PerlIOStdio)->stdio);
3019 PerlIOStdio_setlinebuf(pTHX_ PerlIO *f)
3021 #ifdef HAS_SETLINEBUF
3022 PerlSIO_setlinebuf(PerlIOSelf(f, PerlIOStdio)->stdio);
3024 PerlSIO_setvbuf(PerlIOSelf(f, PerlIOStdio)->stdio, Nullch, _IOLBF, 0);
3030 PerlIOStdio_get_base(pTHX_ PerlIO *f)
3032 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3033 return (STDCHAR*)PerlSIO_get_base(stdio);
3037 PerlIOStdio_get_bufsiz(pTHX_ PerlIO *f)
3039 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3040 return PerlSIO_get_bufsiz(stdio);
3044 #ifdef USE_STDIO_PTR
3046 PerlIOStdio_get_ptr(pTHX_ PerlIO *f)
3048 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3049 return (STDCHAR*)PerlSIO_get_ptr(stdio);
3053 PerlIOStdio_get_cnt(pTHX_ PerlIO *f)
3055 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3056 return PerlSIO_get_cnt(stdio);
3060 PerlIOStdio_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
3062 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3064 #ifdef STDIO_PTR_LVALUE
3065 PerlSIO_set_ptr(stdio, (void*)ptr); /* LHS STDCHAR* cast non-portable */
3066 #ifdef STDIO_PTR_LVAL_SETS_CNT
3067 if (PerlSIO_get_cnt(stdio) != (cnt)) {
3068 assert(PerlSIO_get_cnt(stdio) == (cnt));
3071 #if (!defined(STDIO_PTR_LVAL_NOCHANGE_CNT))
3073 * Setting ptr _does_ change cnt - we are done
3077 #else /* STDIO_PTR_LVALUE */
3079 #endif /* STDIO_PTR_LVALUE */
3082 * Now (or only) set cnt
3084 #ifdef STDIO_CNT_LVALUE
3085 PerlSIO_set_cnt(stdio, cnt);
3086 #else /* STDIO_CNT_LVALUE */
3087 #if (defined(STDIO_PTR_LVALUE) && defined(STDIO_PTR_LVAL_SETS_CNT))
3088 PerlSIO_set_ptr(stdio,
3089 PerlSIO_get_ptr(stdio) + (PerlSIO_get_cnt(stdio) -
3091 #else /* STDIO_PTR_LVAL_SETS_CNT */
3093 #endif /* STDIO_PTR_LVAL_SETS_CNT */
3094 #endif /* STDIO_CNT_LVALUE */
3101 PerlIOStdio_fill(pTHX_ PerlIO *f)
3103 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3106 * fflush()ing read-only streams can cause trouble on some stdio-s
3108 if ((PerlIOBase(f)->flags & PERLIO_F_CANWRITE)) {
3109 if (PerlSIO_fflush(stdio) != 0)
3112 c = PerlSIO_fgetc(stdio);
3116 #if (defined(STDIO_PTR_LVALUE) && (defined(STDIO_CNT_LVALUE) || defined(STDIO_PTR_LVAL_SETS_CNT)))
3118 #ifdef STDIO_BUFFER_WRITABLE
3119 if (PerlIO_fast_gets(f) && PerlIO_has_base(f)) {
3120 /* Fake ungetc() to the real buffer in case system's ungetc
3123 STDCHAR *base = (STDCHAR*)PerlSIO_get_base(stdio);
3124 SSize_t cnt = PerlSIO_get_cnt(stdio);
3125 STDCHAR *ptr = (STDCHAR*)PerlSIO_get_ptr(stdio);
3126 if (ptr == base+1) {
3127 *--ptr = (STDCHAR) c;
3128 PerlIOStdio_set_ptrcnt(aTHX_ f,ptr,cnt+1);
3129 if (PerlSIO_feof(stdio))
3130 PerlSIO_clearerr(stdio);
3136 if (PerlIO_has_cntptr(f)) {
3138 if (PerlIOStdio_unread(aTHX_ f,&ch,1) == 1) {
3145 /* An ungetc()d char is handled separately from the regular
3146 * buffer, so we stuff it in the buffer ourselves.
3147 * Should never get called as should hit code above
3149 *(--((*stdio)->_ptr)) = (unsigned char) c;
3152 /* If buffer snoop scheme above fails fall back to
3155 if (PerlSIO_ungetc(c, stdio) != c)
3163 PerlIO_funcs PerlIO_stdio = {
3164 sizeof(PerlIO_funcs),
3166 sizeof(PerlIOStdio),
3167 PERLIO_K_BUFFERED|PERLIO_K_RAW,
3171 PerlIOBase_binmode, /* binmode */
3185 PerlIOStdio_clearerr,
3186 PerlIOStdio_setlinebuf,
3188 PerlIOStdio_get_base,
3189 PerlIOStdio_get_bufsiz,
3194 #ifdef USE_STDIO_PTR
3195 PerlIOStdio_get_ptr,
3196 PerlIOStdio_get_cnt,
3197 #if (defined(STDIO_PTR_LVALUE) && (defined(STDIO_CNT_LVALUE) || defined(STDIO_PTR_LVAL_SETS_CNT)))
3198 PerlIOStdio_set_ptrcnt
3199 #else /* STDIO_PTR_LVALUE */
3201 #endif /* STDIO_PTR_LVALUE */
3202 #else /* USE_STDIO_PTR */
3206 #endif /* USE_STDIO_PTR */
3209 /* Note that calls to PerlIO_exportFILE() are reversed using
3210 * PerlIO_releaseFILE(), not importFILE. */
3212 PerlIO_exportFILE(PerlIO * f, const char *mode)
3216 if (PerlIOValid(f)) {
3219 if (!mode || !*mode) {
3220 mode = PerlIO_modestr(f, buf);
3222 stdio = PerlSIO_fdopen(PerlIO_fileno(f), mode);
3225 /* De-link any lower layers so new :stdio sticks */
3227 if ((f = PerlIO_push(aTHX_ f, &PerlIO_stdio, buf, Nullsv))) {
3228 PerlIOStdio *s = PerlIOSelf(f, PerlIOStdio);
3230 /* Link previous lower layers under new one */
3234 /* restore layers list */
3244 PerlIO_findFILE(PerlIO *f)
3248 if (l->tab == &PerlIO_stdio) {
3249 PerlIOStdio *s = PerlIOSelf(&l, PerlIOStdio);
3252 l = *PerlIONext(&l);
3254 /* Uses fallback "mode" via PerlIO_modestr() in PerlIO_exportFILE */
3255 return PerlIO_exportFILE(f, Nullch);
3258 /* Use this to reverse PerlIO_exportFILE calls. */
3260 PerlIO_releaseFILE(PerlIO *p, FILE *f)
3264 if (l->tab == &PerlIO_stdio) {
3265 PerlIOStdio *s = PerlIOSelf(&l, PerlIOStdio);
3266 if (s->stdio == f) {
3268 PerlIO_pop(aTHX_ p);
3277 /*--------------------------------------------------------------------------------------*/
3279 * perlio buffer layer
3283 PerlIOBuf_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
3285 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3286 int fd = PerlIO_fileno(f);
3287 if (fd >= 0 && PerlLIO_isatty(fd)) {
3288 PerlIOBase(f)->flags |= PERLIO_F_LINEBUF | PERLIO_F_TTY;
3290 if (*PerlIONext(f)) {
3291 Off_t posn = PerlIO_tell(PerlIONext(f));
3292 if (posn != (Off_t) - 1) {
3296 return PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
3300 PerlIOBuf_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
3301 IV n, const char *mode, int fd, int imode, int perm,
3302 PerlIO *f, int narg, SV **args)
3304 if (PerlIOValid(f)) {
3305 PerlIO *next = PerlIONext(f);
3306 PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIOBase(next)->tab);
3307 next = (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
3309 if (!next || (*PerlIOBase(f)->tab->Pushed) (aTHX_ f, mode, PerlIOArg, self) != 0) {
3314 PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIO_default_btm());
3322 f = (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
3325 if (PerlIO_push(aTHX_ f, self, mode, PerlIOArg) == 0) {
3327 * if push fails during open, open fails. close will pop us.
3332 fd = PerlIO_fileno(f);
3333 if (init && fd == 2) {
3335 * Initial stderr is unbuffered
3337 PerlIOBase(f)->flags |= PERLIO_F_UNBUF;
3339 #ifdef PERLIO_USING_CRLF
3340 # ifdef PERLIO_IS_BINMODE_FD
3341 if (PERLIO_IS_BINMODE_FD(fd))
3342 PerlIO_binmode(f, '<'/*not used*/, O_BINARY, Nullch);
3346 * do something about failing setmode()? --jhi
3348 PerlLIO_setmode(fd, O_BINARY);
3357 * This "flush" is akin to sfio's sync in that it handles files in either
3358 * read or write state
3361 PerlIOBuf_flush(pTHX_ PerlIO *f)
3363 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3365 PerlIO *n = PerlIONext(f);
3366 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF) {
3368 * write() the buffer
3370 STDCHAR *buf = b->buf;
3372 while (p < b->ptr) {
3373 SSize_t count = PerlIO_write(n, p, b->ptr - p);
3377 else if (count < 0 || PerlIO_error(n)) {
3378 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
3383 b->posn += (p - buf);
3385 else if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
3386 STDCHAR *buf = PerlIO_get_base(f);
3388 * Note position change
3390 b->posn += (b->ptr - buf);
3391 if (b->ptr < b->end) {
3392 /* We did not consume all of it - try and seek downstream to
3393 our logical position
3395 if (PerlIOValid(n) && PerlIO_seek(n, b->posn, SEEK_SET) == 0) {
3396 /* Reload n as some layers may pop themselves on seek */
3397 b->posn = PerlIO_tell(n = PerlIONext(f));
3400 /* Seek failed (e.g. pipe or tty). Do NOT clear buffer or pre-read
3401 data is lost for good - so return saying "ok" having undone
3404 b->posn -= (b->ptr - buf);
3409 b->ptr = b->end = b->buf;
3410 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
3411 /* We check for Valid because of dubious decision to make PerlIO_flush(NULL) flush all */
3412 if (PerlIOValid(n) && PerlIO_flush(n) != 0)
3418 PerlIOBuf_fill(pTHX_ PerlIO *f)
3420 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3421 PerlIO *n = PerlIONext(f);
3424 * Down-stream flush is defined not to loose read data so is harmless.
3425 * we would not normally be fill'ing if there was data left in anycase.
3427 if (PerlIO_flush(f) != 0)
3429 if (PerlIOBase(f)->flags & PERLIO_F_TTY)
3430 PerlIOBase_flush_linebuf(aTHX);
3433 PerlIO_get_base(f); /* allocate via vtable */
3435 b->ptr = b->end = b->buf;
3437 if (!PerlIOValid(n)) {
3438 PerlIOBase(f)->flags |= PERLIO_F_EOF;
3442 if (PerlIO_fast_gets(n)) {
3444 * Layer below is also buffered. We do _NOT_ want to call its
3445 * ->Read() because that will loop till it gets what we asked for
3446 * which may hang on a pipe etc. Instead take anything it has to
3447 * hand, or ask it to fill _once_.
3449 avail = PerlIO_get_cnt(n);
3451 avail = PerlIO_fill(n);
3453 avail = PerlIO_get_cnt(n);
3455 if (!PerlIO_error(n) && PerlIO_eof(n))
3460 STDCHAR *ptr = PerlIO_get_ptr(n);
3461 SSize_t cnt = avail;
3462 if (avail > (SSize_t)b->bufsiz)
3464 Copy(ptr, b->buf, avail, STDCHAR);
3465 PerlIO_set_ptrcnt(n, ptr + avail, cnt - avail);
3469 avail = PerlIO_read(n, b->ptr, b->bufsiz);
3473 PerlIOBase(f)->flags |= PERLIO_F_EOF;
3475 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
3478 b->end = b->buf + avail;
3479 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3484 PerlIOBuf_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
3486 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3487 if (PerlIOValid(f)) {
3490 return PerlIOBase_read(aTHX_ f, vbuf, count);
3496 PerlIOBuf_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3498 const STDCHAR *buf = (const STDCHAR *) vbuf + count;
3499 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3502 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
3507 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
3509 * Buffer is already a read buffer, we can overwrite any chars
3510 * which have been read back to buffer start
3512 avail = (b->ptr - b->buf);
3516 * Buffer is idle, set it up so whole buffer is available for
3520 b->end = b->buf + avail;
3522 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3524 * Buffer extends _back_ from where we are now
3526 b->posn -= b->bufsiz;
3528 if (avail > (SSize_t) count) {
3530 * If we have space for more than count, just move count
3538 * In simple stdio-like ungetc() case chars will be already
3541 if (buf != b->ptr) {
3542 Copy(buf, b->ptr, avail, STDCHAR);
3546 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
3550 unread += PerlIOBase_unread(aTHX_ f, vbuf, count);
3556 PerlIOBuf_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3558 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3559 const STDCHAR *buf = (const STDCHAR *) vbuf;
3563 if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
3565 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
3566 if (PerlIO_flush(f) != 0) {
3571 SSize_t avail = b->bufsiz - (b->ptr - b->buf);
3572 if ((SSize_t) count < avail)
3574 PerlIOBase(f)->flags |= PERLIO_F_WRBUF;
3575 if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF) {
3590 Copy(buf, b->ptr, avail, STDCHAR);
3597 if (b->ptr >= (b->buf + b->bufsiz))
3600 if (PerlIOBase(f)->flags & PERLIO_F_UNBUF)
3606 PerlIOBuf_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
3609 if ((code = PerlIO_flush(f)) == 0) {
3610 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3611 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
3612 code = PerlIO_seek(PerlIONext(f), offset, whence);
3614 b->posn = PerlIO_tell(PerlIONext(f));
3621 PerlIOBuf_tell(pTHX_ PerlIO *f)
3623 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3625 * b->posn is file position where b->buf was read, or will be written
3627 Off_t posn = b->posn;
3628 if ((PerlIOBase(f)->flags & PERLIO_F_APPEND) &&
3629 (PerlIOBase(f)->flags & PERLIO_F_WRBUF)) {
3631 /* As O_APPEND files are normally shared in some sense it is better
3636 /* when file is NOT shared then this is sufficient */
3637 PerlIO_seek(PerlIONext(f),0, SEEK_END);
3639 posn = b->posn = PerlIO_tell(PerlIONext(f));
3643 * If buffer is valid adjust position by amount in buffer
3645 posn += (b->ptr - b->buf);
3651 PerlIOBuf_popped(pTHX_ PerlIO *f)
3653 IV code = PerlIOBase_popped(aTHX_ f);
3654 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3655 if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
3659 b->ptr = b->end = b->buf;
3660 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
3665 PerlIOBuf_close(pTHX_ PerlIO *f)
3667 IV code = PerlIOBase_close(aTHX_ f);
3668 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3669 if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
3673 b->ptr = b->end = b->buf;
3674 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
3679 PerlIOBuf_get_ptr(pTHX_ PerlIO *f)
3681 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3688 PerlIOBuf_get_cnt(pTHX_ PerlIO *f)
3690 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3693 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF)
3694 return (b->end - b->ptr);
3699 PerlIOBuf_get_base(pTHX_ PerlIO *f)
3701 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3706 Newz('B',b->buf,b->bufsiz, STDCHAR);
3708 b->buf = (STDCHAR *) & b->oneword;
3709 b->bufsiz = sizeof(b->oneword);
3718 PerlIOBuf_bufsiz(pTHX_ PerlIO *f)
3720 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3723 return (b->end - b->buf);
3727 PerlIOBuf_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
3729 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3733 if (PerlIO_get_cnt(f) != cnt || b->ptr < b->buf) {
3734 assert(PerlIO_get_cnt(f) == cnt);
3735 assert(b->ptr >= b->buf);
3737 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3741 PerlIOBuf_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
3743 return PerlIOBase_dup(aTHX_ f, o, param, flags);
3748 PerlIO_funcs PerlIO_perlio = {
3749 sizeof(PerlIO_funcs),
3752 PERLIO_K_BUFFERED|PERLIO_K_RAW,
3756 PerlIOBase_binmode, /* binmode */
3770 PerlIOBase_clearerr,
3771 PerlIOBase_setlinebuf,
3776 PerlIOBuf_set_ptrcnt,
3779 /*--------------------------------------------------------------------------------------*/
3781 * Temp layer to hold unread chars when cannot do it any other way
3785 PerlIOPending_fill(pTHX_ PerlIO *f)
3788 * Should never happen
3795 PerlIOPending_close(pTHX_ PerlIO *f)
3798 * A tad tricky - flush pops us, then we close new top
3801 return PerlIO_close(f);
3805 PerlIOPending_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
3808 * A tad tricky - flush pops us, then we seek new top
3811 return PerlIO_seek(f, offset, whence);
3816 PerlIOPending_flush(pTHX_ PerlIO *f)
3818 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3819 if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
3823 PerlIO_pop(aTHX_ f);
3828 PerlIOPending_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
3834 PerlIOBuf_set_ptrcnt(aTHX_ f, ptr, cnt);
3839 PerlIOPending_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
3841 IV code = PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
3842 PerlIOl *l = PerlIOBase(f);
3844 * Our PerlIO_fast_gets must match what we are pushed on, or sv_gets()
3845 * etc. get muddled when it changes mid-string when we auto-pop.
3847 l->flags = (l->flags & ~(PERLIO_F_FASTGETS | PERLIO_F_UTF8)) |
3848 (PerlIOBase(PerlIONext(f))->
3849 flags & (PERLIO_F_FASTGETS | PERLIO_F_UTF8));
3854 PerlIOPending_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
3856 SSize_t avail = PerlIO_get_cnt(f);
3858 if ((SSize_t)count < avail)
3861 got = PerlIOBuf_read(aTHX_ f, vbuf, avail);
3862 if (got >= 0 && got < (SSize_t)count) {
3864 PerlIO_read(f, ((STDCHAR *) vbuf) + got, count - got);
3865 if (more >= 0 || got == 0)
3871 PerlIO_funcs PerlIO_pending = {
3872 sizeof(PerlIO_funcs),
3875 PERLIO_K_BUFFERED|PERLIO_K_RAW, /* not sure about RAW here */
3876 PerlIOPending_pushed,
3879 PerlIOBase_binmode, /* binmode */
3888 PerlIOPending_close,
3889 PerlIOPending_flush,
3893 PerlIOBase_clearerr,
3894 PerlIOBase_setlinebuf,
3899 PerlIOPending_set_ptrcnt,
3904 /*--------------------------------------------------------------------------------------*/
3906 * crlf - translation On read translate CR,LF to "\n" we do this by
3907 * overriding ptr/cnt entries to hand back a line at a time and keeping a
3908 * record of which nl we "lied" about. On write translate "\n" to CR,LF
3912 PerlIOBuf base; /* PerlIOBuf stuff */
3913 STDCHAR *nl; /* Position of crlf we "lied" about in the
3918 PerlIOCrlf_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
3921 PerlIOBase(f)->flags |= PERLIO_F_CRLF;
3922 code = PerlIOBuf_pushed(aTHX_ f, mode, arg, tab);
3924 PerlIO_debug("PerlIOCrlf_pushed f=%p %s %s fl=%08" UVxf "\n",
3925 f, PerlIOBase(f)->tab->name, (mode) ? mode : "(Null)",
3926 PerlIOBase(f)->flags);
3933 PerlIOCrlf_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3935 PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
3940 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF))
3941 return PerlIOBuf_unread(aTHX_ f, vbuf, count);
3943 const STDCHAR *buf = (const STDCHAR *) vbuf + count;
3944 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3946 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
3951 if (!(PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
3952 b->end = b->ptr = b->buf + b->bufsiz;
3953 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3954 b->posn -= b->bufsiz;
3956 while (count > 0 && b->ptr > b->buf) {
3959 if (b->ptr - 2 >= b->buf) {
3982 PerlIOCrlf_get_cnt(pTHX_ PerlIO *f)
3984 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3987 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
3988 PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
3989 if ((PerlIOBase(f)->flags & PERLIO_F_CRLF) && (!c->nl || *c->nl == 0xd)) {
3990 STDCHAR *nl = (c->nl) ? c->nl : b->ptr;
3992 while (nl < b->end && *nl != 0xd)
3994 if (nl < b->end && *nl == 0xd) {
3996 if (nl + 1 < b->end) {
4003 * Not CR,LF but just CR
4011 * Blast - found CR as last char in buffer
4016 * They may not care, defer work as long as
4020 return (nl - b->ptr);
4024 b->ptr++; /* say we have read it as far as
4025 * flush() is concerned */
4026 b->buf++; /* Leave space in front of buffer */
4027 /* Note as we have moved buf up flush's
4029 will naturally make posn point at CR
4031 b->bufsiz--; /* Buffer is thus smaller */
4032 code = PerlIO_fill(f); /* Fetch some more */
4033 b->bufsiz++; /* Restore size for next time */
4034 b->buf--; /* Point at space */
4035 b->ptr = nl = b->buf; /* Which is what we hand
4037 *nl = 0xd; /* Fill in the CR */
4039 goto test; /* fill() call worked */
4041 * CR at EOF - just fall through
4043 /* Should we clear EOF though ??? */
4048 return (((c->nl) ? (c->nl + 1) : b->end) - b->ptr);
4054 PerlIOCrlf_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
4056 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
4057 PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
4063 if (ptr == b->end && *c->nl == 0xd) {
4064 /* Defered CR at end of buffer case - we lied about count */
4076 * Test code - delete when it works ...
4078 IV flags = PerlIOBase(f)->flags;
4079 STDCHAR *chk = (c->nl) ? (c->nl+1) : b->end;
4080 if (ptr+cnt == c->nl && c->nl+1 == b->end && *c->nl == 0xd) {
4081 /* Defered CR at end of buffer case - we lied about count */
4087 Perl_croak(aTHX_ "ptr wrong %p != %p fl=%08" UVxf
4088 " nl=%p e=%p for %d", ptr, chk, flags, c->nl,
4096 * They have taken what we lied about
4104 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
4108 PerlIOCrlf_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
4110 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF))
4111 return PerlIOBuf_write(aTHX_ f, vbuf, count);
4113 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
4114 const STDCHAR *buf = (const STDCHAR *) vbuf;
4115 const STDCHAR *ebuf = buf + count;
4118 if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
4120 while (buf < ebuf) {
4121 STDCHAR *eptr = b->buf + b->bufsiz;
4122 PerlIOBase(f)->flags |= PERLIO_F_WRBUF;
4123 while (buf < ebuf && b->ptr < eptr) {
4125 if ((b->ptr + 2) > eptr) {
4133 *(b->ptr)++ = 0xd; /* CR */
4134 *(b->ptr)++ = 0xa; /* LF */
4136 if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF) {
4146 if (b->ptr >= eptr) {
4152 if (PerlIOBase(f)->flags & PERLIO_F_UNBUF)
4154 return (buf - (STDCHAR *) vbuf);
4159 PerlIOCrlf_flush(pTHX_ PerlIO *f)
4161 PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
4166 return PerlIOBuf_flush(aTHX_ f);
4170 PerlIOCrlf_binmode(pTHX_ PerlIO *f)
4172 if ((PerlIOBase(f)->flags & PERLIO_F_CRLF)) {
4173 /* In text mode - flush any pending stuff and flip it */
4174 PerlIOBase(f)->flags &= ~PERLIO_F_CRLF;
4175 #ifndef PERLIO_USING_CRLF
4176 /* CRLF is unusual case - if this is just the :crlf layer pop it */
4177 if (PerlIOBase(f)->tab == &PerlIO_crlf) {
4178 PerlIO_pop(aTHX_ f);
4185 PerlIO_funcs PerlIO_crlf = {
4186 sizeof(PerlIO_funcs),
4189 PERLIO_K_BUFFERED | PERLIO_K_CANCRLF | PERLIO_K_RAW,
4191 PerlIOBuf_popped, /* popped */
4193 PerlIOCrlf_binmode, /* binmode */
4197 PerlIOBuf_read, /* generic read works with ptr/cnt lies
4199 PerlIOCrlf_unread, /* Put CR,LF in buffer for each '\n' */
4200 PerlIOCrlf_write, /* Put CR,LF in buffer for each '\n' */
4208 PerlIOBase_clearerr,
4209 PerlIOBase_setlinebuf,
4214 PerlIOCrlf_set_ptrcnt,
4218 /*--------------------------------------------------------------------------------------*/
4220 * mmap as "buffer" layer
4224 PerlIOBuf base; /* PerlIOBuf stuff */
4225 Mmap_t mptr; /* Mapped address */
4226 Size_t len; /* mapped length */
4227 STDCHAR *bbuf; /* malloced buffer if map fails */
4230 static size_t page_size = 0;
4233 PerlIOMmap_map(pTHX_ PerlIO *f)
4235 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
4236 IV flags = PerlIOBase(f)->flags;
4240 if (flags & PERLIO_F_CANREAD) {
4241 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
4242 int fd = PerlIO_fileno(f);
4244 code = Fstat(fd, &st);
4245 if (code == 0 && S_ISREG(st.st_mode)) {
4246 SSize_t len = st.st_size - b->posn;
4250 #if defined(HAS_SYSCONF) && (defined(_SC_PAGESIZE) || defined(_SC_PAGE_SIZE))
4252 SETERRNO(0, SS_NORMAL);
4253 # ifdef _SC_PAGESIZE
4254 page_size = sysconf(_SC_PAGESIZE);
4256 page_size = sysconf(_SC_PAGE_SIZE);
4258 if ((long) page_size < 0) {
4263 (void) SvUPGRADE(error, SVt_PV);
4264 msg = SvPVx(error, n_a);
4265 Perl_croak(aTHX_ "panic: sysconf: %s",
4270 "panic: sysconf: pagesize unknown");
4274 # ifdef HAS_GETPAGESIZE
4275 page_size = getpagesize();
4277 # if defined(I_SYS_PARAM) && defined(PAGESIZE)
4278 page_size = PAGESIZE; /* compiletime, bad */
4282 if ((IV) page_size <= 0)
4283 Perl_croak(aTHX_ "panic: bad pagesize %" IVdf,
4288 * This is a hack - should never happen - open should
4291 b->posn = PerlIO_tell(PerlIONext(f));
4293 posn = (b->posn / page_size) * page_size;
4294 len = st.st_size - posn;
4295 m->mptr = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, posn);
4296 if (m->mptr && m->mptr != (Mmap_t) - 1) {
4297 #if 0 && defined(HAS_MADVISE) && defined(MADV_SEQUENTIAL)
4298 madvise(m->mptr, len, MADV_SEQUENTIAL);
4300 #if 0 && defined(HAS_MADVISE) && defined(MADV_WILLNEED)
4301 madvise(m->mptr, len, MADV_WILLNEED);
4303 PerlIOBase(f)->flags =
4304 (flags & ~PERLIO_F_EOF) | PERLIO_F_RDBUF;
4305 b->end = ((STDCHAR *) m->mptr) + len;
4306 b->buf = ((STDCHAR *) m->mptr) + (b->posn - posn);
4315 PerlIOBase(f)->flags =
4316 flags | PERLIO_F_EOF | PERLIO_F_RDBUF;
4318 b->ptr = b->end = b->ptr;
4327 PerlIOMmap_unmap(pTHX_ PerlIO *f)
4329 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
4330 PerlIOBuf *b = &m->base;
4334 code = munmap(m->mptr, m->len);
4338 if (PerlIO_seek(PerlIONext(f), b->posn, SEEK_SET) != 0)
4341 b->ptr = b->end = b->buf;
4342 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
4348 PerlIOMmap_get_base(pTHX_ PerlIO *f)
4350 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
4351 PerlIOBuf *b = &m->base;
4352 if (b->buf && (PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
4354 * Already have a readbuffer in progress
4360 * We have a write buffer or flushed PerlIOBuf read buffer
4362 m->bbuf = b->buf; /* save it in case we need it again */
4363 b->buf = NULL; /* Clear to trigger below */
4366 PerlIOMmap_map(aTHX_ f); /* Try and map it */
4369 * Map did not work - recover PerlIOBuf buffer if we have one
4374 b->ptr = b->end = b->buf;
4377 return PerlIOBuf_get_base(aTHX_ f);
4381 PerlIOMmap_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
4383 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
4384 PerlIOBuf *b = &m->base;
4385 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
4387 if (b->ptr && (b->ptr - count) >= b->buf
4388 && memEQ(b->ptr - count, vbuf, count)) {
4390 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
4395 * Loose the unwritable mapped buffer
4399 * If flush took the "buffer" see if we have one from before
4401 if (!b->buf && m->bbuf)
4404 PerlIOBuf_get_base(aTHX_ f);
4408 return PerlIOBuf_unread(aTHX_ f, vbuf, count);
4412 PerlIOMmap_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
4414 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
4415 PerlIOBuf *b = &m->base;
4416 if (!b->buf || !(PerlIOBase(f)->flags & PERLIO_F_WRBUF)) {
4418 * No, or wrong sort of, buffer
4421 if (PerlIOMmap_unmap(aTHX_ f) != 0)
4425 * If unmap took the "buffer" see if we have one from before
4427 if (!b->buf && m->bbuf)
4430 PerlIOBuf_get_base(aTHX_ f);
4434 return PerlIOBuf_write(aTHX_ f, vbuf, count);
4438 PerlIOMmap_flush(pTHX_ PerlIO *f)
4440 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
4441 PerlIOBuf *b = &m->base;
4442 IV code = PerlIOBuf_flush(aTHX_ f);
4444 * Now we are "synced" at PerlIOBuf level
4451 if (PerlIOMmap_unmap(aTHX_ f) != 0)
4456 * We seem to have a PerlIOBuf buffer which was not mapped
4457 * remember it in case we need one later
4466 PerlIOMmap_fill(pTHX_ PerlIO *f)
4468 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
4469 IV code = PerlIO_flush(f);
4470 if (code == 0 && !b->buf) {
4471 code = PerlIOMmap_map(aTHX_ f);
4473 if (code == 0 && !(PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
4474 code = PerlIOBuf_fill(aTHX_ f);
4480 PerlIOMmap_close(pTHX_ PerlIO *f)
4482 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
4483 PerlIOBuf *b = &m->base;
4484 IV code = PerlIO_flush(f);
4488 b->ptr = b->end = b->buf;
4490 if (PerlIOBuf_close(aTHX_ f) != 0)
4496 PerlIOMmap_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
4498 return PerlIOBase_dup(aTHX_ f, o, param, flags);
4502 PerlIO_funcs PerlIO_mmap = {
4503 sizeof(PerlIO_funcs),
4506 PERLIO_K_BUFFERED|PERLIO_K_RAW,
4510 PerlIOBase_binmode, /* binmode */
4524 PerlIOBase_clearerr,
4525 PerlIOBase_setlinebuf,
4526 PerlIOMmap_get_base,
4530 PerlIOBuf_set_ptrcnt,
4533 #endif /* HAS_MMAP */
4536 Perl_PerlIO_stdin(pTHX)
4539 PerlIO_stdstreams(aTHX);
4541 return &PL_perlio[1];
4545 Perl_PerlIO_stdout(pTHX)
4548 PerlIO_stdstreams(aTHX);
4550 return &PL_perlio[2];
4554 Perl_PerlIO_stderr(pTHX)
4557 PerlIO_stdstreams(aTHX);
4559 return &PL_perlio[3];
4562 /*--------------------------------------------------------------------------------------*/
4565 PerlIO_getname(PerlIO *f, char *buf)
4570 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
4572 name = fgetname(stdio, buf);
4574 Perl_croak(aTHX_ "Don't know how to get file name");
4580 /*--------------------------------------------------------------------------------------*/
4582 * Functions which can be called on any kind of PerlIO implemented in
4586 #undef PerlIO_fdopen
4588 PerlIO_fdopen(int fd, const char *mode)
4591 return PerlIO_openn(aTHX_ Nullch, mode, fd, 0, 0, NULL, 0, NULL);
4596 PerlIO_open(const char *path, const char *mode)
4599 SV *name = sv_2mortal(newSVpvn(path, strlen(path)));
4600 return PerlIO_openn(aTHX_ Nullch, mode, -1, 0, 0, NULL, 1, &name);
4603 #undef Perlio_reopen
4605 PerlIO_reopen(const char *path, const char *mode, PerlIO *f)
4608 SV *name = sv_2mortal(newSVpvn(path, strlen(path)));
4609 return PerlIO_openn(aTHX_ Nullch, mode, -1, 0, 0, f, 1, &name);
4614 PerlIO_getc(PerlIO *f)
4618 SSize_t count = PerlIO_read(f, buf, 1);
4620 return (unsigned char) buf[0];
4625 #undef PerlIO_ungetc
4627 PerlIO_ungetc(PerlIO *f, int ch)
4632 if (PerlIO_unread(f, &buf, 1) == 1)
4640 PerlIO_putc(PerlIO *f, int ch)
4644 return PerlIO_write(f, &buf, 1);
4649 PerlIO_puts(PerlIO *f, const char *s)
4652 STRLEN len = strlen(s);
4653 return PerlIO_write(f, s, len);
4656 #undef PerlIO_rewind
4658 PerlIO_rewind(PerlIO *f)
4661 PerlIO_seek(f, (Off_t) 0, SEEK_SET);
4665 #undef PerlIO_vprintf
4667 PerlIO_vprintf(PerlIO *f, const char *fmt, va_list ap)
4670 SV *sv = newSVpvn("", 0);
4676 Perl_va_copy(ap, apc);
4677 sv_vcatpvf(sv, fmt, &apc);
4679 sv_vcatpvf(sv, fmt, &ap);
4682 wrote = PerlIO_write(f, s, len);
4687 #undef PerlIO_printf
4689 PerlIO_printf(PerlIO *f, const char *fmt, ...)
4694 result = PerlIO_vprintf(f, fmt, ap);
4699 #undef PerlIO_stdoutf
4701 PerlIO_stdoutf(const char *fmt, ...)
4707 result = PerlIO_vprintf(PerlIO_stdout(), fmt, ap);
4712 #undef PerlIO_tmpfile
4714 PerlIO_tmpfile(void)
4717 * I have no idea how portable mkstemp() is ...
4719 #if defined(WIN32) || !defined(HAVE_MKSTEMP)
4722 FILE *stdio = PerlSIO_tmpfile();
4724 if ((f = PerlIO_push(aTHX_(PerlIO_allocate(aTHX)), &PerlIO_stdio, "w+", Nullsv))) {
4725 PerlIOStdio *s = PerlIOSelf(f, PerlIOStdio);
4732 SV *sv = newSVpv("/tmp/PerlIO_XXXXXX", 0);
4733 int fd = mkstemp(SvPVX(sv));
4736 f = PerlIO_fdopen(fd, "w+");
4738 PerlIOBase(f)->flags |= PERLIO_F_TEMP;
4740 PerlLIO_unlink(SvPVX(sv));
4750 #endif /* USE_SFIO */
4751 #endif /* PERLIO_IS_STDIO */
4753 /*======================================================================================*/
4755 * Now some functions in terms of above which may be needed even if we are
4756 * not in true PerlIO mode
4760 #undef PerlIO_setpos
4762 PerlIO_setpos(PerlIO *f, SV *pos)
4767 Off_t *posn = (Off_t *) SvPV(pos, len);
4768 if (f && len == sizeof(Off_t))
4769 return PerlIO_seek(f, *posn, SEEK_SET);
4771 SETERRNO(EINVAL, SS_IVCHAN);
4775 #undef PerlIO_setpos
4777 PerlIO_setpos(PerlIO *f, SV *pos)
4782 Fpos_t *fpos = (Fpos_t *) SvPV(pos, len);
4783 if (f && len == sizeof(Fpos_t)) {
4784 #if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64)
4785 return fsetpos64(f, fpos);
4787 return fsetpos(f, fpos);
4791 SETERRNO(EINVAL, SS_IVCHAN);
4797 #undef PerlIO_getpos
4799 PerlIO_getpos(PerlIO *f, SV *pos)
4802 Off_t posn = PerlIO_tell(f);
4803 sv_setpvn(pos, (char *) &posn, sizeof(posn));
4804 return (posn == (Off_t) - 1) ? -1 : 0;
4807 #undef PerlIO_getpos
4809 PerlIO_getpos(PerlIO *f, SV *pos)
4814 #if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64)
4815 code = fgetpos64(f, &fpos);
4817 code = fgetpos(f, &fpos);
4819 sv_setpvn(pos, (char *) &fpos, sizeof(fpos));
4824 #if (defined(PERLIO_IS_STDIO) || !defined(USE_SFIO)) && !defined(HAS_VPRINTF)
4827 vprintf(char *pat, char *args)
4829 _doprnt(pat, args, stdout);
4830 return 0; /* wrong, but perl doesn't use the return
4835 vfprintf(FILE *fd, char *pat, char *args)
4837 _doprnt(pat, args, fd);
4838 return 0; /* wrong, but perl doesn't use the return
4844 #ifndef PerlIO_vsprintf
4846 PerlIO_vsprintf(char *s, int n, const char *fmt, va_list ap)
4848 int val = vsprintf(s, fmt, ap);
4850 if (strlen(s) >= (STRLEN) n) {
4852 (void) PerlIO_puts(Perl_error_log,
4853 "panic: sprintf overflow - memory corrupted!\n");
4861 #ifndef PerlIO_sprintf
4863 PerlIO_sprintf(char *s, int n, const char *fmt, ...)
4868 result = PerlIO_vsprintf(s, n, fmt, ap);