2 * perlio.c Copyright (c) 1996-2001, Nick Ing-Simmons You may distribute
3 * under the terms of either the GNU General Public License or the
4 * Artistic License, as specified in the README file.
8 * If we have ActivePerl-like PERL_IMPLICIT_SYS then we need a dTHX to get
9 * at the dispatch tables, even when we do not need it for other reasons.
10 * Invent a dSYS macro to abstract this out
12 #ifdef PERL_IMPLICIT_SYS
25 #define PERLIO_NOT_STDIO 0
26 #if !defined(PERLIO_IS_STDIO) && !defined(USE_SFIO)
32 * This file provides those parts of PerlIO abstraction
33 * which are not #defined in perlio.h.
34 * Which these are depends on various Configure #ifdef's
38 #define PERL_IN_PERLIO_C
41 #ifdef PERL_IMPLICIT_CONTEXT
49 perlsio_binmode(FILE *fp, int iotype, int mode)
52 * This used to be contents of do_binmode in doio.c
55 # if defined(atarist) || defined(__MINT__)
58 ((FILE *) fp)->_flag |= _IOBIN;
60 ((FILE *) fp)->_flag &= ~_IOBIN;
67 if (PerlLIO_setmode(fp, mode) != -1) {
69 if (PerlLIO_setmode(fileno(fp), mode) != -1) {
71 # if defined(WIN32) && defined(__BORLANDC__)
73 * The translation mode of the stream is maintained independent of
74 * the translation mode of the fd in the Borland RTL (heavy
75 * digging through their runtime sources reveal). User has to set
76 * the mode explicitly for the stream (though they don't document
77 * this anywhere). GSAR 97-5-24
91 # if defined(USEMYBINMODE)
92 if (my_binmode(fp, iotype, mode) != FALSE)
103 #define O_ACCMODE 3 /* Assume traditional implementation */
107 PerlIO_intmode2str(int rawmode, char *mode, int *writing)
109 int result = rawmode & O_ACCMODE;
114 ptype = IoTYPE_RDONLY;
117 ptype = IoTYPE_WRONLY;
125 *writing = (result != O_RDONLY);
127 if (result == O_RDONLY) {
131 else if (rawmode & O_APPEND) {
133 if (result != O_WRONLY)
138 if (result == O_WRONLY)
145 if (rawmode & O_BINARY)
151 #ifndef PERLIO_LAYERS
153 PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names)
155 if (!names || !*names || strEQ(names, ":crlf") || strEQ(names, ":raw")) {
158 Perl_croak(aTHX_ "Cannot apply \"%s\" in non-PerlIO perl", names);
166 PerlIO_destruct(pTHX)
171 PerlIO_binmode(pTHX_ PerlIO *fp, int iotype, int mode, const char *names)
176 return perlsio_binmode(fp, iotype, mode);
181 PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
185 int fd = PerlLIO_dup(PerlIO_fileno(f));
188 int omode = fcntl(fd, F_GETFL);
189 PerlIO_intmode2str(omode,mode,NULL);
190 /* the r+ is a hack */
191 return PerlIO_fdopen(fd, mode);
196 SETERRNO(EBADF, SS$_IVCHAN);
204 * De-mux PerlIO_openn() into fdopen, freopen and fopen type entries
208 PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd,
209 int imode, int perm, PerlIO *old, int narg, SV **args)
213 Perl_croak(aTHX_ "More than one argument to open");
215 if (*args == &PL_sv_undef)
216 return PerlIO_tmpfile();
218 char *name = SvPV_nolen(*args);
220 fd = PerlLIO_open3(name, imode, perm);
222 return PerlIO_fdopen(fd, (char *) mode + 1);
225 return PerlIO_reopen(name, mode, old);
228 return PerlIO_open(name, mode);
233 return PerlIO_fdopen(fd, (char *) mode);
238 XS(XS_PerlIO__Layer__find)
242 Perl_croak(aTHX_ "Usage class->find(name[,load])");
244 char *name = SvPV_nolen(ST(1));
245 ST(0) = (strEQ(name, "crlf")
246 || strEQ(name, "raw")) ? &PL_sv_yes : &PL_sv_undef;
253 Perl_boot_core_PerlIO(pTHX)
255 newXS("PerlIO::Layer::find", XS_PerlIO__Layer__find, __FILE__);
261 #ifdef PERLIO_IS_STDIO
267 * Does nothing (yet) except force this file to be included in perl
268 * binary. That allows this file to force inclusion of other functions
269 * that may be required by loadable extensions e.g. for
270 * FileHandle::tmpfile
274 #undef PerlIO_tmpfile
281 #else /* PERLIO_IS_STDIO */
289 * This section is just to make sure these functions get pulled in from
293 #undef PerlIO_tmpfile
304 * Force this file to be included in perl binary. Which allows this
305 * file to force inclusion of other functions that may be required by
306 * loadable extensions e.g. for FileHandle::tmpfile
310 * Hack sfio does its own 'autoflush' on stdout in common cases. Flush
311 * results in a lot of lseek()s to regular files and lot of small
314 sfset(sfstdout, SF_SHARE, 0);
318 PerlIO_importFILE(FILE *stdio, int fl)
320 int fd = fileno(stdio);
321 PerlIO *r = PerlIO_fdopen(fd, "r+");
326 PerlIO_findFILE(PerlIO *pio)
328 int fd = PerlIO_fileno(pio);
329 FILE *f = fdopen(fd, "r+");
331 if (!f && errno == EINVAL)
333 if (!f && errno == EINVAL)
340 /*======================================================================================*/
342 * Implement all the PerlIO interface ourselves.
348 * We _MUST_ have <unistd.h> if we are using lseek() and may have large
355 #include <sys/mman.h>
359 void PerlIO_debug(const char *fmt, ...)
360 __attribute__ ((format(__printf__, 1, 2)));
363 PerlIO_debug(const char *fmt, ...)
370 char *s = PerlEnv_getenv("PERLIO_DEBUG");
372 dbg = PerlLIO_open3(s, O_WRONLY | O_CREAT | O_APPEND, 0666);
379 /* Use fixed buffer as sv_catpvf etc. needs SVs */
383 s = CopFILE(PL_curcop);
386 sprintf(buffer, "%s:%" IVdf " ", s, (IV) CopLINE(PL_curcop));
387 len = strlen(buffer);
388 vsprintf(buffer+len, fmt, ap);
389 PerlLIO_write(dbg, buffer, strlen(buffer));
391 SV *sv = newSVpvn("", 0);
394 s = CopFILE(PL_curcop);
397 Perl_sv_catpvf(aTHX_ sv, "%s:%" IVdf " ", s,
398 (IV) CopLINE(PL_curcop));
399 Perl_sv_vcatpvf(aTHX_ sv, fmt, &ap);
402 PerlLIO_write(dbg, s, len);
409 /*--------------------------------------------------------------------------------------*/
412 * Inner level routines
416 * Table of pointers to the PerlIO structs (malloc'ed)
418 #define PERLIO_TABLE_SIZE 64
421 PerlIO_allocate(pTHX)
424 * Find a free slot in the table, allocating new table as necessary
429 while ((f = *last)) {
431 last = (PerlIO **) (f);
432 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
438 Newz('I',f,PERLIO_TABLE_SIZE,PerlIO);
446 #undef PerlIO_fdupopen
448 PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
451 PerlIO_funcs *tab = PerlIOBase(f)->tab;
453 PerlIO_debug("fdupopen f=%p param=%p\n",f,param);
454 new = (*tab->Dup)(aTHX_ PerlIO_allocate(aTHX),f,param, flags);
458 SETERRNO(EBADF, SS$_IVCHAN);
464 PerlIO_cleantable(pTHX_ PerlIO **tablep)
466 PerlIO *table = *tablep;
469 PerlIO_cleantable(aTHX_(PerlIO **) & (table[0]));
470 for (i = PERLIO_TABLE_SIZE - 1; i > 0; i--) {
471 PerlIO *f = table + i;
483 PerlIO_list_alloc(pTHX)
486 Newz('L', list, 1, PerlIO_list_t);
492 PerlIO_list_free(pTHX_ PerlIO_list_t *list)
495 if (--list->refcnt == 0) {
498 for (i = 0; i < list->cur; i++) {
499 if (list->array[i].arg)
500 SvREFCNT_dec(list->array[i].arg);
502 Safefree(list->array);
510 PerlIO_list_push(pTHX_ PerlIO_list_t *list, PerlIO_funcs *funcs, SV *arg)
513 if (list->cur >= list->len) {
516 Renew(list->array, list->len, PerlIO_pair_t);
518 New('l', list->array, list->len, PerlIO_pair_t);
520 p = &(list->array[list->cur++]);
522 if ((p->arg = arg)) {
528 PerlIO_clone_list(pTHX_ PerlIO_list_t *proto, CLONE_PARAMS *param)
530 PerlIO_list_t *list = (PerlIO_list_t *) NULL;
533 list = PerlIO_list_alloc(aTHX);
534 for (i=0; i < proto->cur; i++) {
536 if (proto->array[i].arg)
537 arg = PerlIO_sv_dup(aTHX_ proto->array[i].arg,param);
538 PerlIO_list_push(aTHX_ list, proto->array[i].funcs, arg);
545 PerlIO_clone(pTHX_ PerlInterpreter *proto, CLONE_PARAMS *param)
548 PerlIO **table = &proto->Iperlio;
551 PL_known_layers = PerlIO_clone_list(aTHX_ proto->Iknown_layers, param);
552 PL_def_layerlist = PerlIO_clone_list(aTHX_ proto->Idef_layerlist, param);
553 PerlIO_allocate(aTHX); /* root slot is never used */
554 PerlIO_debug("Clone %p from %p\n",aTHX,proto);
555 while ((f = *table)) {
557 table = (PerlIO **) (f++);
558 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
560 (void) fp_dup(f, 0, param);
569 PerlIO_destruct(pTHX)
571 PerlIO **table = &PL_perlio;
574 PerlIO_debug("Destruct %p\n",aTHX);
576 while ((f = *table)) {
578 table = (PerlIO **) (f++);
579 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
583 if (l->tab->kind & PERLIO_K_DESTRUCT) {
584 PerlIO_debug("Destruct popping %s\n", l->tab->name);
595 PerlIO_list_free(aTHX_ PL_known_layers);
596 PL_known_layers = NULL;
597 PerlIO_list_free(aTHX_ PL_def_layerlist);
598 PL_def_layerlist = NULL;
602 PerlIO_pop(pTHX_ PerlIO *f)
606 PerlIO_debug("PerlIO_pop f=%p %s\n", f, l->tab->name);
607 if (l->tab->Popped) {
609 * If popped returns non-zero do not free its layer structure
610 * it has either done so itself, or it is shared and still in
613 if ((*l->tab->Popped) (f) != 0)
621 /*--------------------------------------------------------------------------------------*/
623 * XS Interface for perl code
627 PerlIO_find_layer(pTHX_ const char *name, STRLEN len, int load)
630 if ((SSize_t) len <= 0)
632 for (i = 0; i < PL_known_layers->cur; i++) {
633 PerlIO_funcs *f = PL_known_layers->array[i].funcs;
634 if (memEQ(f->name, name, len)) {
635 PerlIO_debug("%.*s => %p\n", (int) len, name, f);
639 if (load && PL_subname && PL_def_layerlist
640 && PL_def_layerlist->cur >= 2) {
641 SV *pkgsv = newSVpvn("PerlIO", 6);
642 SV *layer = newSVpvn(name, len);
645 * The two SVs are magically freed by load_module
647 Perl_load_module(aTHX_ 0, pkgsv, Nullsv, layer, Nullsv);
649 return PerlIO_find_layer(aTHX_ name, len, 0);
651 PerlIO_debug("Cannot find %.*s\n", (int) len, name);
655 #ifdef USE_ATTRIBUTES_FOR_PERLIO
658 perlio_mg_set(pTHX_ SV *sv, MAGIC *mg)
661 IO *io = GvIOn((GV *) SvRV(sv));
662 PerlIO *ifp = IoIFP(io);
663 PerlIO *ofp = IoOFP(io);
664 Perl_warn(aTHX_ "set %" SVf " %p %p %p", sv, io, ifp, ofp);
670 perlio_mg_get(pTHX_ SV *sv, MAGIC *mg)
673 IO *io = GvIOn((GV *) SvRV(sv));
674 PerlIO *ifp = IoIFP(io);
675 PerlIO *ofp = IoOFP(io);
676 Perl_warn(aTHX_ "get %" SVf " %p %p %p", sv, io, ifp, ofp);
682 perlio_mg_clear(pTHX_ SV *sv, MAGIC *mg)
684 Perl_warn(aTHX_ "clear %" SVf, sv);
689 perlio_mg_free(pTHX_ SV *sv, MAGIC *mg)
691 Perl_warn(aTHX_ "free %" SVf, sv);
695 MGVTBL perlio_vtab = {
703 XS(XS_io_MODIFY_SCALAR_ATTRIBUTES)
706 SV *sv = SvRV(ST(1));
711 sv_magic(sv, (SV *) av, PERL_MAGIC_ext, NULL, 0);
713 mg = mg_find(sv, PERL_MAGIC_ext);
714 mg->mg_virtual = &perlio_vtab;
716 Perl_warn(aTHX_ "attrib %" SVf, sv);
717 for (i = 2; i < items; i++) {
719 const char *name = SvPV(ST(i), len);
720 SV *layer = PerlIO_find_layer(aTHX_ name, len, 1);
722 av_push(av, SvREFCNT_inc(layer));
733 #endif /* USE_ATTIBUTES_FOR_PERLIO */
736 PerlIO_tab_sv(pTHX_ PerlIO_funcs *tab)
738 HV *stash = gv_stashpv("PerlIO::Layer", TRUE);
739 SV *sv = sv_bless(newRV_noinc(newSViv(PTR2IV(tab))), stash);
743 XS(XS_PerlIO__Layer__find)
747 Perl_croak(aTHX_ "Usage class->find(name[,load])");
750 char *name = SvPV(ST(1), len);
751 bool load = (items > 2) ? SvTRUE(ST(2)) : 0;
752 PerlIO_funcs *layer = PerlIO_find_layer(aTHX_ name, len, load);
754 (layer) ? sv_2mortal(PerlIO_tab_sv(aTHX_ layer)) :
761 PerlIO_define_layer(pTHX_ PerlIO_funcs *tab)
763 if (!PL_known_layers)
764 PL_known_layers = PerlIO_list_alloc(aTHX);
765 PerlIO_list_push(aTHX_ PL_known_layers, tab, Nullsv);
766 PerlIO_debug("define %s %p\n", tab->name, tab);
770 PerlIO_parse_layers(pTHX_ PerlIO_list_t *av, const char *names)
773 const char *s = names;
775 while (isSPACE(*s) || *s == ':')
780 const char *as = Nullch;
782 if (!isIDFIRST(*s)) {
784 * Message is consistent with how attribute lists are
785 * passed. Even though this means "foo : : bar" is
786 * seen as an invalid separator character.
788 char q = ((*s == '\'') ? '"' : '\'');
790 "perlio: invalid separator character %c%c%c in layer specification list %s",
796 } while (isALNUM(*e));
812 * It's a nul terminated string, not allowed
813 * to \ the terminating null. Anything other
814 * character is passed over.
825 "perlio: argument list not closed for layer \"%.*s\"",
837 PerlIO_funcs *layer =
838 PerlIO_find_layer(aTHX_ s, llen, 1);
840 PerlIO_list_push(aTHX_ av, layer,
846 Perl_warn(aTHX_ "perlio: unknown layer \"%.*s\"",
859 PerlIO_default_buffer(pTHX_ PerlIO_list_t *av)
861 PerlIO_funcs *tab = &PerlIO_perlio;
862 #ifdef PERLIO_USING_CRLF
865 if (PerlIO_stdio.Set_ptrcnt)
868 PerlIO_debug("Pushing %s\n", tab->name);
869 PerlIO_list_push(aTHX_ av, PerlIO_find_layer(aTHX_ tab->name, 0, 0),
874 PerlIO_arg_fetch(PerlIO_list_t *av, IV n)
876 return av->array[n].arg;
880 PerlIO_layer_fetch(pTHX_ PerlIO_list_t *av, IV n, PerlIO_funcs *def)
882 if (n >= 0 && n < av->cur) {
883 PerlIO_debug("Layer %" IVdf " is %s\n", n,
884 av->array[n].funcs->name);
885 return av->array[n].funcs;
888 Perl_croak(aTHX_ "panic: PerlIO layer array corrupt");
893 PerlIO_default_layers(pTHX)
895 if (!PL_def_layerlist) {
896 const char *s = (PL_tainting) ? Nullch : PerlEnv_getenv("PERLIO");
897 PerlIO_funcs *osLayer = &PerlIO_unix;
898 PL_def_layerlist = PerlIO_list_alloc(aTHX);
899 PerlIO_define_layer(aTHX_ & PerlIO_unix);
900 #if defined(WIN32) && !defined(UNDER_CE)
901 PerlIO_define_layer(aTHX_ & PerlIO_win32);
903 osLayer = &PerlIO_win32;
906 PerlIO_define_layer(aTHX_ & PerlIO_raw);
907 PerlIO_define_layer(aTHX_ & PerlIO_perlio);
908 PerlIO_define_layer(aTHX_ & PerlIO_stdio);
909 PerlIO_define_layer(aTHX_ & PerlIO_crlf);
911 PerlIO_define_layer(aTHX_ & PerlIO_mmap);
913 PerlIO_define_layer(aTHX_ & PerlIO_utf8);
914 PerlIO_define_layer(aTHX_ & PerlIO_byte);
915 PerlIO_list_push(aTHX_ PL_def_layerlist,
916 PerlIO_find_layer(aTHX_ osLayer->name, 0, 0),
919 PerlIO_parse_layers(aTHX_ PL_def_layerlist, s);
922 PerlIO_default_buffer(aTHX_ PL_def_layerlist);
925 if (PL_def_layerlist->cur < 2) {
926 PerlIO_default_buffer(aTHX_ PL_def_layerlist);
928 return PL_def_layerlist;
932 Perl_boot_core_PerlIO(pTHX)
934 #ifdef USE_ATTRIBUTES_FOR_PERLIO
935 newXS("io::MODIFY_SCALAR_ATTRIBUTES", XS_io_MODIFY_SCALAR_ATTRIBUTES,
938 newXS("PerlIO::Layer::find", XS_PerlIO__Layer__find, __FILE__);
942 PerlIO_default_layer(pTHX_ I32 n)
944 PerlIO_list_t *av = PerlIO_default_layers(aTHX);
947 return PerlIO_layer_fetch(aTHX_ av, n, &PerlIO_stdio);
950 #define PerlIO_default_top() PerlIO_default_layer(aTHX_ -1)
951 #define PerlIO_default_btm() PerlIO_default_layer(aTHX_ 0)
954 PerlIO_stdstreams(pTHX)
957 PerlIO_allocate(aTHX);
958 PerlIO_fdopen(0, "Ir" PERLIO_STDTEXT);
959 PerlIO_fdopen(1, "Iw" PERLIO_STDTEXT);
960 PerlIO_fdopen(2, "Iw" PERLIO_STDTEXT);
965 PerlIO_push(pTHX_ PerlIO *f, PerlIO_funcs *tab, const char *mode, SV *arg)
968 Newc('L',l,tab->size,char,PerlIOl);
970 Zero(l, tab->size, char);
974 PerlIO_debug("PerlIO_push f=%p %s %s %p\n", f, tab->name,
975 (mode) ? mode : "(Null)", arg);
976 if ((*l->tab->Pushed) (f, mode, arg) != 0) {
985 PerlIOPop_pushed(PerlIO *f, const char *mode, SV *arg)
998 PerlIORaw_pushed(PerlIO *f, const char *mode, SV *arg)
1001 * Remove the dummy layer
1004 PerlIO_pop(aTHX_ f);
1006 * Pop back to bottom layer
1010 while (!(PerlIOBase(f)->tab->kind & PERLIO_K_RAW)) {
1011 if (*PerlIONext(f)) {
1012 PerlIO_pop(aTHX_ f);
1016 * Nothing bellow - push unix on top then remove it
1018 if (PerlIO_push(aTHX_ f, PerlIO_default_btm(), mode, arg)) {
1019 PerlIO_pop(aTHX_ PerlIONext(f));
1024 PerlIO_debug(":raw f=%p :%s\n", f, PerlIOBase(f)->tab->name);
1031 PerlIO_apply_layera(pTHX_ PerlIO *f, const char *mode,
1032 PerlIO_list_t *layers, IV n)
1034 IV max = layers->cur;
1037 PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n, NULL);
1039 if (!PerlIO_push(aTHX_ f, tab, mode, PerlIOArg)) {
1050 PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names)
1054 PerlIO_list_t *layers = PerlIO_list_alloc(aTHX);
1055 code = PerlIO_parse_layers(aTHX_ layers, names);
1057 code = PerlIO_apply_layera(aTHX_ f, mode, layers, 0);
1059 PerlIO_list_free(aTHX_ layers);
1065 /*--------------------------------------------------------------------------------------*/
1067 * Given the abstraction above the public API functions
1071 PerlIO_binmode(pTHX_ PerlIO *f, int iotype, int mode, const char *names)
1073 PerlIO_debug("PerlIO_binmode f=%p %s %c %x %s\n",
1074 f, PerlIOBase(f)->tab->name, iotype, mode,
1075 (names) ? names : "(Null)");
1076 /* Can't flush if switching encodings. */
1077 if (!(names && memEQ(names, ":encoding(", 10))) {
1079 #ifdef PERLIO_USING_CRLF
1080 if (!names && (mode & O_BINARY)) {
1083 if (PerlIOBase(top)->tab == &PerlIO_crlf) {
1084 PerlIOBase(top)->flags &= ~PERLIO_F_CRLF;
1087 top = PerlIONext(top);
1093 return PerlIO_apply_layers(aTHX_ f, NULL, names) == 0 ? TRUE : FALSE;
1096 #undef PerlIO__close
1098 PerlIO__close(PerlIO *f)
1101 return (*PerlIOBase(f)->tab->Close) (f);
1103 SETERRNO(EBADF, SS$_IVCHAN);
1110 PerlIO_close(PerlIO *f)
1115 code = (*PerlIOBase(f)->tab->Close) (f);
1117 PerlIO_pop(aTHX_ f);
1123 #undef PerlIO_fileno
1125 PerlIO_fileno(PerlIO *f)
1128 return (*PerlIOBase(f)->tab->Fileno) (f);
1130 SETERRNO(EBADF, SS$_IVCHAN);
1136 PerlIO_context_layers(pTHX_ const char *mode)
1138 const char *type = NULL;
1140 * Need to supply default layer info from open.pm
1143 SV *layers = PL_curcop->cop_io;
1146 type = SvPV(layers, len);
1147 if (type && mode[0] != 'r') {
1149 * Skip to write part
1151 const char *s = strchr(type, 0);
1152 if (s && (s - type) < len) {
1161 static PerlIO_funcs *
1162 PerlIO_layer_from_ref(pTHX_ SV *sv)
1165 * For any scalar type load the handler which is bundled with perl
1167 if (SvTYPE(sv) < SVt_PVAV)
1168 return PerlIO_find_layer(aTHX_ "Scalar", 6, 1);
1171 * For other types allow if layer is known but don't try and load it
1173 switch (SvTYPE(sv)) {
1175 return PerlIO_find_layer(aTHX_ "Array", 5, 0);
1177 return PerlIO_find_layer(aTHX_ "Hash", 4, 0);
1179 return PerlIO_find_layer(aTHX_ "Code", 4, 0);
1181 return PerlIO_find_layer(aTHX_ "Glob", 4, 0);
1187 PerlIO_resolve_layers(pTHX_ const char *layers,
1188 const char *mode, int narg, SV **args)
1190 PerlIO_list_t *def = PerlIO_default_layers(aTHX);
1193 PerlIO_stdstreams(aTHX);
1197 * If it is a reference but not an object see if we have a handler
1200 if (SvROK(arg) && !sv_isobject(arg)) {
1201 PerlIO_funcs *handler = PerlIO_layer_from_ref(aTHX_ SvRV(arg));
1203 def = PerlIO_list_alloc(aTHX);
1204 PerlIO_list_push(aTHX_ def, handler, &PL_sv_undef);
1208 * Don't fail if handler cannot be found :Via(...) etc. may do
1209 * something sensible else we will just stringfy and open
1215 layers = PerlIO_context_layers(aTHX_ mode);
1216 if (layers && *layers) {
1220 av = PerlIO_list_alloc(aTHX);
1221 for (i = 0; i < def->cur; i++) {
1222 PerlIO_list_push(aTHX_ av, def->array[i].funcs,
1229 PerlIO_parse_layers(aTHX_ av, layers);
1240 PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd,
1241 int imode, int perm, PerlIO *f, int narg, SV **args)
1243 if (!f && narg == 1 && *args == &PL_sv_undef) {
1244 if ((f = PerlIO_tmpfile())) {
1246 layers = PerlIO_context_layers(aTHX_ mode);
1247 if (layers && *layers)
1248 PerlIO_apply_layers(aTHX_ f, mode, layers);
1252 PerlIO_list_t *layera = NULL;
1254 PerlIO_funcs *tab = NULL;
1257 * This is "reopen" - it is not tested as perl does not use it
1261 layera = PerlIO_list_alloc(aTHX);
1264 (l->tab->Getarg) ? (*l->tab->
1265 Getarg) (aTHX_ &l, NULL, 0) : &PL_sv_undef;
1266 PerlIO_list_push(aTHX_ layera, l->tab, arg);
1267 l = *PerlIONext(&l);
1271 layera = PerlIO_resolve_layers(aTHX_ layers, mode, narg, args);
1274 * Start at "top" of layer stack
1276 n = layera->cur - 1;
1278 PerlIO_funcs *t = PerlIO_layer_fetch(aTHX_ layera, n, NULL);
1287 * Found that layer 'n' can do opens - call it
1289 if (narg > 1 && !(tab->kind & PERLIO_K_MULTIARG)) {
1290 Perl_croak(aTHX_ "More than one argument to open(,':%s')",tab->name);
1292 PerlIO_debug("openn(%s,'%s','%s',%d,%x,%o,%p,%d,%p)\n",
1293 tab->name, layers, mode, fd, imode, perm, f, narg,
1295 f = (*tab->Open) (aTHX_ tab, layera, n, mode, fd, imode, perm,
1298 if (n + 1 < layera->cur) {
1300 * More layers above the one that we used to open -
1303 if (PerlIO_apply_layera(aTHX_ f, mode, layera, n + 1)
1310 PerlIO_list_free(aTHX_ layera);
1316 #undef PerlIO_fdopen
1318 PerlIO_fdopen(int fd, const char *mode)
1321 return PerlIO_openn(aTHX_ Nullch, mode, fd, 0, 0, NULL, 0, NULL);
1326 PerlIO_open(const char *path, const char *mode)
1329 SV *name = sv_2mortal(newSVpvn(path, strlen(path)));
1330 return PerlIO_openn(aTHX_ Nullch, mode, -1, 0, 0, NULL, 1, &name);
1333 #undef PerlIO_reopen
1335 PerlIO_reopen(const char *path, const char *mode, PerlIO *f)
1338 SV *name = sv_2mortal(newSVpvn(path, strlen(path)));
1339 return PerlIO_openn(aTHX_ Nullch, mode, -1, 0, 0, f, 1, &name);
1344 PerlIO_read(PerlIO *f, void *vbuf, Size_t count)
1347 return (*PerlIOBase(f)->tab->Read) (f, vbuf, count);
1349 SETERRNO(EBADF, SS$_IVCHAN);
1354 #undef PerlIO_unread
1356 PerlIO_unread(PerlIO *f, const void *vbuf, Size_t count)
1359 return (*PerlIOBase(f)->tab->Unread) (f, vbuf, count);
1361 SETERRNO(EBADF, SS$_IVCHAN);
1368 PerlIO_write(PerlIO *f, const void *vbuf, Size_t count)
1371 return (*PerlIOBase(f)->tab->Write) (f, vbuf, count);
1373 SETERRNO(EBADF, SS$_IVCHAN);
1380 PerlIO_seek(PerlIO *f, Off_t offset, int whence)
1383 return (*PerlIOBase(f)->tab->Seek) (f, offset, whence);
1385 SETERRNO(EBADF, SS$_IVCHAN);
1392 PerlIO_tell(PerlIO *f)
1395 return (*PerlIOBase(f)->tab->Tell) (f);
1397 SETERRNO(EBADF, SS$_IVCHAN);
1404 PerlIO_flush(PerlIO *f)
1408 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1409 if (tab && tab->Flush) {
1410 return (*tab->Flush) (f);
1413 PerlIO_debug("Cannot flush f=%p :%s\n", f, tab->name);
1414 SETERRNO(EBADF, SS$_IVCHAN);
1419 PerlIO_debug("Cannot flush f=%p\n", f);
1420 SETERRNO(EBADF, SS$_IVCHAN);
1426 * Is it good API design to do flush-all on NULL, a potentially
1427 * errorneous input? Maybe some magical value (PerlIO*
1428 * PERLIO_FLUSH_ALL = (PerlIO*)-1;)? Yes, stdio does similar
1429 * things on fflush(NULL), but should we be bound by their design
1433 PerlIO **table = &PL_perlio;
1435 while ((f = *table)) {
1437 table = (PerlIO **) (f++);
1438 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
1439 if (*f && PerlIO_flush(f) != 0)
1449 PerlIOBase_flush_linebuf()
1452 PerlIO **table = &PL_perlio;
1454 while ((f = *table)) {
1456 table = (PerlIO **) (f++);
1457 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
1460 flags & (PERLIO_F_LINEBUF | PERLIO_F_CANWRITE))
1461 == (PERLIO_F_LINEBUF | PERLIO_F_CANWRITE))
1470 PerlIO_fill(PerlIO *f)
1473 return (*PerlIOBase(f)->tab->Fill) (f);
1475 SETERRNO(EBADF, SS$_IVCHAN);
1480 #undef PerlIO_isutf8
1482 PerlIO_isutf8(PerlIO *f)
1485 return (PerlIOBase(f)->flags & PERLIO_F_UTF8) != 0;
1487 SETERRNO(EBADF, SS$_IVCHAN);
1494 PerlIO_eof(PerlIO *f)
1497 return (*PerlIOBase(f)->tab->Eof) (f);
1499 SETERRNO(EBADF, SS$_IVCHAN);
1506 PerlIO_error(PerlIO *f)
1509 return (*PerlIOBase(f)->tab->Error) (f);
1511 SETERRNO(EBADF, SS$_IVCHAN);
1516 #undef PerlIO_clearerr
1518 PerlIO_clearerr(PerlIO *f)
1521 (*PerlIOBase(f)->tab->Clearerr) (f);
1523 SETERRNO(EBADF, SS$_IVCHAN);
1526 #undef PerlIO_setlinebuf
1528 PerlIO_setlinebuf(PerlIO *f)
1531 (*PerlIOBase(f)->tab->Setlinebuf) (f);
1533 SETERRNO(EBADF, SS$_IVCHAN);
1536 #undef PerlIO_has_base
1538 PerlIO_has_base(PerlIO *f)
1541 return (PerlIOBase(f)->tab->Get_base != NULL);
1546 #undef PerlIO_fast_gets
1548 PerlIO_fast_gets(PerlIO *f)
1550 if (f && *f && (PerlIOBase(f)->flags & PERLIO_F_FASTGETS)) {
1551 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1552 return (tab->Set_ptrcnt != NULL);
1557 #undef PerlIO_has_cntptr
1559 PerlIO_has_cntptr(PerlIO *f)
1562 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1563 return (tab->Get_ptr != NULL && tab->Get_cnt != NULL);
1568 #undef PerlIO_canset_cnt
1570 PerlIO_canset_cnt(PerlIO *f)
1573 PerlIOl *l = PerlIOBase(f);
1574 return (l->tab->Set_ptrcnt != NULL);
1579 #undef PerlIO_get_base
1581 PerlIO_get_base(PerlIO *f)
1584 return (*PerlIOBase(f)->tab->Get_base) (f);
1588 #undef PerlIO_get_bufsiz
1590 PerlIO_get_bufsiz(PerlIO *f)
1593 return (*PerlIOBase(f)->tab->Get_bufsiz) (f);
1597 #undef PerlIO_get_ptr
1599 PerlIO_get_ptr(PerlIO *f)
1601 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1602 if (tab->Get_ptr == NULL)
1604 return (*tab->Get_ptr) (f);
1607 #undef PerlIO_get_cnt
1609 PerlIO_get_cnt(PerlIO *f)
1611 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1612 if (tab->Get_cnt == NULL)
1614 return (*tab->Get_cnt) (f);
1617 #undef PerlIO_set_cnt
1619 PerlIO_set_cnt(PerlIO *f, int cnt)
1621 (*PerlIOBase(f)->tab->Set_ptrcnt) (f, NULL, cnt);
1624 #undef PerlIO_set_ptrcnt
1626 PerlIO_set_ptrcnt(PerlIO *f, STDCHAR * ptr, int cnt)
1628 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1629 if (tab->Set_ptrcnt == NULL) {
1631 Perl_croak(aTHX_ "PerlIO buffer snooping abuse");
1633 (*PerlIOBase(f)->tab->Set_ptrcnt) (f, ptr, cnt);
1636 /*--------------------------------------------------------------------------------------*/
1638 * utf8 and raw dummy layers
1642 PerlIOUtf8_pushed(PerlIO *f, const char *mode, SV *arg)
1644 if (PerlIONext(f)) {
1646 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1647 PerlIO_pop(aTHX_ f);
1648 if (tab->kind & PERLIO_K_UTF8)
1649 PerlIOBase(f)->flags |= PERLIO_F_UTF8;
1651 PerlIOBase(f)->flags &= ~PERLIO_F_UTF8;
1657 PerlIO_funcs PerlIO_utf8 = {
1660 PERLIO_K_DUMMY | PERLIO_F_UTF8,
1678 NULL, /* get_base */
1679 NULL, /* get_bufsiz */
1682 NULL, /* set_ptrcnt */
1685 PerlIO_funcs PerlIO_byte = {
1706 NULL, /* get_base */
1707 NULL, /* get_bufsiz */
1710 NULL, /* set_ptrcnt */
1714 PerlIORaw_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
1715 IV n, const char *mode, int fd, int imode, int perm,
1716 PerlIO *old, int narg, SV **args)
1718 PerlIO_funcs *tab = PerlIO_default_btm();
1719 return (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
1723 PerlIO_funcs PerlIO_raw = {
1744 NULL, /* get_base */
1745 NULL, /* get_bufsiz */
1748 NULL, /* set_ptrcnt */
1750 /*--------------------------------------------------------------------------------------*/
1751 /*--------------------------------------------------------------------------------------*/
1753 * "Methods" of the "base class"
1757 PerlIOBase_fileno(PerlIO *f)
1759 return PerlIO_fileno(PerlIONext(f));
1763 PerlIO_modestr(PerlIO *f, char *buf)
1766 IV flags = PerlIOBase(f)->flags;
1767 if (flags & PERLIO_F_APPEND) {
1769 if (flags & PERLIO_F_CANREAD) {
1773 else if (flags & PERLIO_F_CANREAD) {
1775 if (flags & PERLIO_F_CANWRITE)
1778 else if (flags & PERLIO_F_CANWRITE) {
1780 if (flags & PERLIO_F_CANREAD) {
1784 #ifdef PERLIO_USING_CRLF
1785 if (!(flags & PERLIO_F_CRLF))
1793 PerlIOBase_pushed(PerlIO *f, const char *mode, SV *arg)
1795 PerlIOl *l = PerlIOBase(f);
1797 const char *omode = mode;
1800 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1801 l->flags &= ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE |
1802 PERLIO_F_TRUNCATE | PERLIO_F_APPEND);
1803 if (tab->Set_ptrcnt != NULL)
1804 l->flags |= PERLIO_F_FASTGETS;
1806 if (*mode == '#' || *mode == 'I')
1810 l->flags |= PERLIO_F_CANREAD;
1813 l->flags |= PERLIO_F_APPEND | PERLIO_F_CANWRITE;
1816 l->flags |= PERLIO_F_TRUNCATE | PERLIO_F_CANWRITE;
1819 SETERRNO(EINVAL, LIB$_INVARG);
1825 l->flags |= PERLIO_F_CANREAD | PERLIO_F_CANWRITE;
1828 l->flags &= ~PERLIO_F_CRLF;
1831 l->flags |= PERLIO_F_CRLF;
1834 SETERRNO(EINVAL, LIB$_INVARG);
1841 l->flags |= l->next->flags &
1842 (PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_TRUNCATE |
1847 PerlIO_debug("PerlIOBase_pushed f=%p %s %s fl=%08" UVxf " (%s)\n",
1848 f, PerlIOBase(f)->tab->name, (omode) ? omode : "(Null)",
1849 l->flags, PerlIO_modestr(f, temp));
1855 PerlIOBase_popped(PerlIO *f)
1861 PerlIOBase_unread(PerlIO *f, const void *vbuf, Size_t count)
1865 * Save the position as current head considers it
1867 Off_t old = PerlIO_tell(f);
1869 PerlIO_push(aTHX_ f, &PerlIO_pending, "r", Nullsv);
1870 PerlIOSelf(f, PerlIOBuf)->posn = old;
1871 done = PerlIOBuf_unread(f, vbuf, count);
1876 PerlIOBase_read(PerlIO *f, void *vbuf, Size_t count)
1878 STDCHAR *buf = (STDCHAR *) vbuf;
1880 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD))
1883 SSize_t avail = PerlIO_get_cnt(f);
1886 take = (count < avail) ? count : avail;
1888 STDCHAR *ptr = PerlIO_get_ptr(f);
1889 Copy(ptr, buf, take, STDCHAR);
1890 PerlIO_set_ptrcnt(f, ptr + take, (avail -= take));
1894 if (count > 0 && avail <= 0) {
1895 if (PerlIO_fill(f) != 0)
1899 return (buf - (STDCHAR *) vbuf);
1905 PerlIOBase_noop_ok(PerlIO *f)
1911 PerlIOBase_noop_fail(PerlIO *f)
1917 PerlIOBase_close(PerlIO *f)
1920 PerlIO *n = PerlIONext(f);
1921 if (PerlIO_flush(f) != 0)
1923 if (n && *n && (*PerlIOBase(n)->tab->Close) (n) != 0)
1925 PerlIOBase(f)->flags &=
1926 ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_OPEN);
1931 PerlIOBase_eof(PerlIO *f)
1934 return (PerlIOBase(f)->flags & PERLIO_F_EOF) != 0;
1940 PerlIOBase_error(PerlIO *f)
1943 return (PerlIOBase(f)->flags & PERLIO_F_ERROR) != 0;
1949 PerlIOBase_clearerr(PerlIO *f)
1952 PerlIO *n = PerlIONext(f);
1953 PerlIOBase(f)->flags &= ~(PERLIO_F_ERROR | PERLIO_F_EOF);
1960 PerlIOBase_setlinebuf(PerlIO *f)
1963 PerlIOBase(f)->flags |= PERLIO_F_LINEBUF;
1968 PerlIO_sv_dup(pTHX_ SV *arg, CLONE_PARAMS *param)
1974 return sv_dup(arg, param);
1977 return newSVsv(arg);
1980 return newSVsv(arg);
1985 PerlIOBase_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
1987 PerlIO *nexto = PerlIONext(o);
1989 PerlIO_funcs *tab = PerlIOBase(nexto)->tab;
1990 f = (*tab->Dup)(aTHX_ f, nexto, param, flags);
1993 PerlIO_funcs *self = PerlIOBase(o)->tab;
1996 PerlIO_debug("PerlIOBase_dup %s f=%p o=%p param=%p\n",self->name,f,o,param);
1998 arg = (*self->Getarg)(aTHX_ o,param,flags);
2000 f = PerlIO_push(aTHX_ f, self, PerlIO_modestr(o,buf), arg);
2008 #define PERLIO_MAX_REFCOUNTABLE_FD 2048
2010 perl_mutex PerlIO_mutex;
2012 int PerlIO_fd_refcnt[PERLIO_MAX_REFCOUNTABLE_FD];
2017 /* Place holder for stdstreams call ??? */
2019 MUTEX_INIT(&PerlIO_mutex);
2024 PerlIOUnix_refcnt_inc(int fd)
2026 if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
2028 MUTEX_LOCK(&PerlIO_mutex);
2030 PerlIO_fd_refcnt[fd]++;
2031 PerlIO_debug("fd %d refcnt=%d\n",fd,PerlIO_fd_refcnt[fd]);
2033 MUTEX_UNLOCK(&PerlIO_mutex);
2039 PerlIOUnix_refcnt_dec(int fd)
2042 if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
2044 MUTEX_LOCK(&PerlIO_mutex);
2046 cnt = --PerlIO_fd_refcnt[fd];
2047 PerlIO_debug("fd %d refcnt=%d\n",fd,cnt);
2049 MUTEX_UNLOCK(&PerlIO_mutex);
2056 PerlIO_cleanup(pTHX)
2060 PerlIO_debug("Cleanup %p\n",aTHX);
2062 /* Raise STDIN..STDERR refcount so we don't close them */
2063 for (i=0; i < 3; i++)
2064 PerlIOUnix_refcnt_inc(i);
2065 PerlIO_cleantable(aTHX_ &PL_perlio);
2066 /* Restore STDIN..STDERR refcount */
2067 for (i=0; i < 3; i++)
2068 PerlIOUnix_refcnt_dec(i);
2073 /*--------------------------------------------------------------------------------------*/
2075 * Bottom-most level for UNIX-like case
2079 struct _PerlIO base; /* The generic part */
2080 int fd; /* UNIX like file descriptor */
2081 int oflags; /* open/fcntl flags */
2085 PerlIOUnix_oflags(const char *mode)
2088 if (*mode == 'I' || *mode == '#')
2093 if (*++mode == '+') {
2100 oflags = O_CREAT | O_TRUNC;
2101 if (*++mode == '+') {
2110 oflags = O_CREAT | O_APPEND;
2111 if (*++mode == '+') {
2124 else if (*mode == 't') {
2126 oflags &= ~O_BINARY;
2130 * Always open in binary mode
2133 if (*mode || oflags == -1) {
2134 SETERRNO(EINVAL, LIB$_INVARG);
2141 PerlIOUnix_fileno(PerlIO *f)
2143 return PerlIOSelf(f, PerlIOUnix)->fd;
2147 PerlIOUnix_pushed(PerlIO *f, const char *mode, SV *arg)
2149 IV code = PerlIOBase_pushed(f, mode, arg);
2150 PerlIOUnix *s = PerlIOSelf(f, PerlIOUnix);
2151 if (*PerlIONext(f)) {
2152 s->fd = PerlIO_fileno(PerlIONext(f));
2154 * XXX could (or should) we retrieve the oflags from the open file
2155 * handle rather than believing the "mode" we are passed in? XXX
2156 * Should the value on NULL mode be 0 or -1?
2158 s->oflags = mode ? PerlIOUnix_oflags(mode) : -1;
2160 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
2165 PerlIOUnix_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2166 IV n, const char *mode, int fd, int imode,
2167 int perm, PerlIO *f, int narg, SV **args)
2170 if (PerlIOBase(f)->flags & PERLIO_F_OPEN)
2171 (*PerlIOBase(f)->tab->Close) (f);
2174 char *path = SvPV_nolen(*args);
2178 imode = PerlIOUnix_oflags(mode);
2182 fd = PerlLIO_open3(path, imode, perm);
2190 f = PerlIO_allocate(aTHX);
2191 s = PerlIOSelf(PerlIO_push(aTHX_ f, self, mode, PerlIOArg),
2195 s = PerlIOSelf(f, PerlIOUnix);
2198 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
2199 PerlIOUnix_refcnt_inc(fd);
2205 * FIXME: pop layers ???
2213 PerlIOUnix_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
2215 PerlIOUnix *os = PerlIOSelf(o, PerlIOUnix);
2217 if (flags & PERLIO_DUP_FD) {
2218 fd = PerlLIO_dup(fd);
2220 if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
2221 f = PerlIOBase_dup(aTHX_ f, o, param, flags);
2223 /* If all went well overwrite fd in dup'ed lay with the dup()'ed fd */
2224 PerlIOUnix *s = PerlIOSelf(f, PerlIOUnix);
2226 PerlIOUnix_refcnt_inc(fd);
2235 PerlIOUnix_read(PerlIO *f, void *vbuf, Size_t count)
2238 int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2239 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD))
2242 SSize_t len = PerlLIO_read(fd, vbuf, count);
2243 if (len >= 0 || errno != EINTR) {
2245 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2246 else if (len == 0 && count != 0)
2247 PerlIOBase(f)->flags |= PERLIO_F_EOF;
2255 PerlIOUnix_write(PerlIO *f, const void *vbuf, Size_t count)
2258 int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2260 SSize_t len = PerlLIO_write(fd, vbuf, count);
2261 if (len >= 0 || errno != EINTR) {
2263 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2271 PerlIOUnix_seek(PerlIO *f, Off_t offset, int whence)
2275 PerlLIO_lseek(PerlIOSelf(f, PerlIOUnix)->fd, offset, whence);
2276 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
2277 return (new == (Off_t) - 1) ? -1 : 0;
2281 PerlIOUnix_tell(PerlIO *f)
2284 return PerlLIO_lseek(PerlIOSelf(f, PerlIOUnix)->fd, 0, SEEK_CUR);
2289 PerlIOUnix_close(PerlIO *f)
2292 int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2294 if (PerlIOBase(f)->flags & PERLIO_F_OPEN) {
2295 if (PerlIOUnix_refcnt_dec(fd) > 0) {
2296 PerlIOBase(f)->flags &= ~PERLIO_F_OPEN;
2301 SETERRNO(EBADF,SS$_IVCHAN);
2304 while (PerlLIO_close(fd) != 0) {
2305 if (errno != EINTR) {
2312 PerlIOBase(f)->flags &= ~PERLIO_F_OPEN;
2317 PerlIO_funcs PerlIO_unix = {
2333 PerlIOBase_noop_ok, /* flush */
2334 PerlIOBase_noop_fail, /* fill */
2337 PerlIOBase_clearerr,
2338 PerlIOBase_setlinebuf,
2339 NULL, /* get_base */
2340 NULL, /* get_bufsiz */
2343 NULL, /* set_ptrcnt */
2346 /*--------------------------------------------------------------------------------------*/
2352 struct _PerlIO base;
2353 FILE *stdio; /* The stream */
2357 PerlIOStdio_fileno(PerlIO *f)
2360 return PerlSIO_fileno(PerlIOSelf(f, PerlIOStdio)->stdio);
2364 PerlIOStdio_mode(const char *mode, char *tmode)
2370 #ifdef PERLIO_USING_CRLF
2378 * This isn't used yet ...
2381 PerlIOStdio_pushed(PerlIO *f, const char *mode, SV *arg)
2383 if (*PerlIONext(f)) {
2385 PerlIOStdio *s = PerlIOSelf(f, PerlIOStdio);
2388 PerlSIO_fdopen(PerlIO_fileno(PerlIONext(f)), mode =
2389 PerlIOStdio_mode(mode, tmode));
2395 return PerlIOBase_pushed(f, mode, arg);
2398 #undef PerlIO_importFILE
2400 PerlIO_importFILE(FILE *stdio, int fl)
2406 PerlIOSelf(PerlIO_push
2407 (aTHX_(f = PerlIO_allocate(aTHX)), &PerlIO_stdio,
2408 "r+", Nullsv), PerlIOStdio);
2415 PerlIOStdio_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2416 IV n, const char *mode, int fd, int imode,
2417 int perm, PerlIO *f, int narg, SV **args)
2421 char *path = SvPV_nolen(*args);
2422 PerlIOStdio *s = PerlIOSelf(f, PerlIOStdio);
2424 PerlIOUnix_refcnt_dec(fileno(s->stdio));
2425 stdio = PerlSIO_freopen(path, (mode = PerlIOStdio_mode(mode, tmode)),
2430 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2435 char *path = SvPV_nolen(*args);
2438 fd = PerlLIO_open3(path, imode, perm);
2441 FILE *stdio = PerlSIO_fopen(path, mode);
2444 PerlIOSelf(PerlIO_push
2445 (aTHX_(f = PerlIO_allocate(aTHX)), self,
2446 (mode = PerlIOStdio_mode(mode, tmode)),
2450 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2465 stdio = PerlSIO_stdin;
2468 stdio = PerlSIO_stdout;
2471 stdio = PerlSIO_stderr;
2476 stdio = PerlSIO_fdopen(fd, mode =
2477 PerlIOStdio_mode(mode, tmode));
2481 PerlIOSelf(PerlIO_push
2482 (aTHX_(f = PerlIO_allocate(aTHX)), self,
2483 mode, PerlIOArg), PerlIOStdio);
2485 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2494 PerlIOStdio_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
2496 /* This assumes no layers underneath - which is what
2497 happens, but is not how I remember it. NI-S 2001/10/16
2499 if ((f = PerlIOBase_dup(aTHX_ f, o, param, flags))) {
2500 FILE *stdio = PerlIOSelf(o, PerlIOStdio)->stdio;
2501 if (flags & PERLIO_DUP_FD) {
2502 int fd = PerlLIO_dup(fileno(stdio));
2505 stdio = fdopen(fd, PerlIO_modestr(o,mode));
2508 /* FIXME: To avoid messy error recovery if dup fails
2509 re-use the existing stdio as though flag was not set
2513 PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
2514 PerlIOUnix_refcnt_inc(fileno(stdio));
2520 PerlIOStdio_close(PerlIO *f)
2523 #ifdef SOCKS5_VERSION_NAME
2525 Sock_size_t optlen = sizeof(int);
2527 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2528 if (PerlIOUnix_refcnt_dec(fileno(stdio)) > 0) {
2529 /* Do not close it but do flush any buffers */
2534 #ifdef SOCKS5_VERSION_NAME
2536 (PerlIO_fileno(f), SOL_SOCKET, SO_TYPE, (void *) &optval,
2538 0) ? PerlSIO_fclose(stdio) : close(PerlIO_fileno(f))
2540 PerlSIO_fclose(stdio)
2549 PerlIOStdio_read(PerlIO *f, void *vbuf, Size_t count)
2552 FILE *s = PerlIOSelf(f, PerlIOStdio)->stdio;
2555 STDCHAR *buf = (STDCHAR *) vbuf;
2557 * Perl is expecting PerlIO_getc() to fill the buffer Linux's
2558 * stdio does not do that for fread()
2560 int ch = PerlSIO_fgetc(s);
2567 got = PerlSIO_fread(vbuf, 1, count, s);
2572 PerlIOStdio_unread(PerlIO *f, const void *vbuf, Size_t count)
2575 FILE *s = PerlIOSelf(f, PerlIOStdio)->stdio;
2576 STDCHAR *buf = ((STDCHAR *) vbuf) + count - 1;
2579 int ch = *buf-- & 0xff;
2580 if (PerlSIO_ungetc(ch, s) != ch)
2589 PerlIOStdio_write(PerlIO *f, const void *vbuf, Size_t count)
2592 return PerlSIO_fwrite(vbuf, 1, count,
2593 PerlIOSelf(f, PerlIOStdio)->stdio);
2597 PerlIOStdio_seek(PerlIO *f, Off_t offset, int whence)
2600 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2601 return PerlSIO_fseek(stdio, offset, whence);
2605 PerlIOStdio_tell(PerlIO *f)
2608 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2609 return PerlSIO_ftell(stdio);
2613 PerlIOStdio_flush(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(PerlIO *f)
2643 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2646 * fflush()ing read-only streams can cause trouble on some stdio-s
2648 if ((PerlIOBase(f)->flags & PERLIO_F_CANWRITE)) {
2649 if (PerlSIO_fflush(stdio) != 0)
2652 c = PerlSIO_fgetc(stdio);
2653 if (c == EOF || PerlSIO_ungetc(c, stdio) != c)
2659 PerlIOStdio_eof(PerlIO *f)
2662 return PerlSIO_feof(PerlIOSelf(f, PerlIOStdio)->stdio);
2666 PerlIOStdio_error(PerlIO *f)
2669 return PerlSIO_ferror(PerlIOSelf(f, PerlIOStdio)->stdio);
2673 PerlIOStdio_clearerr(PerlIO *f)
2676 PerlSIO_clearerr(PerlIOSelf(f, PerlIOStdio)->stdio);
2680 PerlIOStdio_setlinebuf(PerlIO *f)
2683 #ifdef HAS_SETLINEBUF
2684 PerlSIO_setlinebuf(PerlIOSelf(f, PerlIOStdio)->stdio);
2686 PerlSIO_setvbuf(PerlIOSelf(f, PerlIOStdio)->stdio, Nullch, _IOLBF, 0);
2692 PerlIOStdio_get_base(PerlIO *f)
2695 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2696 return PerlSIO_get_base(stdio);
2700 PerlIOStdio_get_bufsiz(PerlIO *f)
2703 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2704 return PerlSIO_get_bufsiz(stdio);
2708 #ifdef USE_STDIO_PTR
2710 PerlIOStdio_get_ptr(PerlIO *f)
2713 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2714 return PerlSIO_get_ptr(stdio);
2718 PerlIOStdio_get_cnt(PerlIO *f)
2721 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2722 return PerlSIO_get_cnt(stdio);
2726 PerlIOStdio_set_ptrcnt(PerlIO *f, STDCHAR * ptr, SSize_t cnt)
2728 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2731 #ifdef STDIO_PTR_LVALUE
2732 PerlSIO_set_ptr(stdio, ptr);
2733 #ifdef STDIO_PTR_LVAL_SETS_CNT
2734 if (PerlSIO_get_cnt(stdio) != (cnt)) {
2736 assert(PerlSIO_get_cnt(stdio) == (cnt));
2739 #if (!defined(STDIO_PTR_LVAL_NOCHANGE_CNT))
2741 * Setting ptr _does_ change cnt - we are done
2745 #else /* STDIO_PTR_LVALUE */
2747 #endif /* STDIO_PTR_LVALUE */
2750 * Now (or only) set cnt
2752 #ifdef STDIO_CNT_LVALUE
2753 PerlSIO_set_cnt(stdio, cnt);
2754 #else /* STDIO_CNT_LVALUE */
2755 #if (defined(STDIO_PTR_LVALUE) && defined(STDIO_PTR_LVAL_SETS_CNT))
2756 PerlSIO_set_ptr(stdio,
2757 PerlSIO_get_ptr(stdio) + (PerlSIO_get_cnt(stdio) -
2759 #else /* STDIO_PTR_LVAL_SETS_CNT */
2761 #endif /* STDIO_PTR_LVAL_SETS_CNT */
2762 #endif /* STDIO_CNT_LVALUE */
2767 PerlIO_funcs PerlIO_stdio = {
2769 sizeof(PerlIOStdio),
2787 PerlIOStdio_clearerr,
2788 PerlIOStdio_setlinebuf,
2790 PerlIOStdio_get_base,
2791 PerlIOStdio_get_bufsiz,
2796 #ifdef USE_STDIO_PTR
2797 PerlIOStdio_get_ptr,
2798 PerlIOStdio_get_cnt,
2799 #if (defined(STDIO_PTR_LVALUE) && (defined(STDIO_CNT_LVALUE) || defined(STDIO_PTR_LVAL_SETS_CNT)))
2800 PerlIOStdio_set_ptrcnt
2801 #else /* STDIO_PTR_LVALUE */
2803 #endif /* STDIO_PTR_LVALUE */
2804 #else /* USE_STDIO_PTR */
2808 #endif /* USE_STDIO_PTR */
2811 #undef PerlIO_exportFILE
2813 PerlIO_exportFILE(PerlIO *f, int fl)
2817 stdio = fdopen(PerlIO_fileno(f), "r+");
2821 PerlIOSelf(PerlIO_push(aTHX_ f, &PerlIO_stdio, "r+", Nullsv),
2828 #undef PerlIO_findFILE
2830 PerlIO_findFILE(PerlIO *f)
2834 if (l->tab == &PerlIO_stdio) {
2835 PerlIOStdio *s = PerlIOSelf(&l, PerlIOStdio);
2838 l = *PerlIONext(&l);
2840 return PerlIO_exportFILE(f, 0);
2843 #undef PerlIO_releaseFILE
2845 PerlIO_releaseFILE(PerlIO *p, FILE *f)
2849 /*--------------------------------------------------------------------------------------*/
2851 * perlio buffer layer
2855 PerlIOBuf_pushed(PerlIO *f, const char *mode, SV *arg)
2858 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
2859 int fd = PerlIO_fileno(f);
2861 if (fd >= 0 && PerlLIO_isatty(fd)) {
2862 PerlIOBase(f)->flags |= PERLIO_F_LINEBUF | PERLIO_F_TTY;
2864 posn = PerlIO_tell(PerlIONext(f));
2865 if (posn != (Off_t) - 1) {
2868 return PerlIOBase_pushed(f, mode, arg);
2872 PerlIOBuf_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2873 IV n, const char *mode, int fd, int imode, int perm,
2874 PerlIO *f, int narg, SV **args)
2877 PerlIO *next = PerlIONext(f);
2879 PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIOBase(next)->tab);
2881 (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
2884 || (*PerlIOBase(f)->tab->Pushed) (f, mode, PerlIOArg) != 0) {
2890 PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIO_default_btm());
2898 f = (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
2901 if (PerlIO_push(aTHX_ f, self, mode, PerlIOArg) == 0) {
2903 * if push fails during open, open fails. close will pop us.
2908 fd = PerlIO_fileno(f);
2909 #ifdef PERLIO_USING_CRLF
2911 * do something about failing setmode()? --jhi
2913 PerlLIO_setmode(fd, O_BINARY);
2915 if (init && fd == 2) {
2917 * Initial stderr is unbuffered
2919 PerlIOBase(f)->flags |= PERLIO_F_UNBUF;
2928 * This "flush" is akin to sfio's sync in that it handles files in either
2929 * read or write state
2932 PerlIOBuf_flush(PerlIO *f)
2934 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
2936 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF) {
2938 * write() the buffer
2940 STDCHAR *buf = b->buf;
2942 PerlIO *n = PerlIONext(f);
2943 while (p < b->ptr) {
2944 SSize_t count = PerlIO_write(n, p, b->ptr - p);
2948 else if (count < 0 || PerlIO_error(n)) {
2949 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2954 b->posn += (p - buf);
2956 else if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
2957 STDCHAR *buf = PerlIO_get_base(f);
2959 * Note position change
2961 b->posn += (b->ptr - buf);
2962 if (b->ptr < b->end) {
2964 * We did not consume all of it
2966 if (PerlIO_seek(PerlIONext(f), b->posn, SEEK_SET) == 0) {
2967 b->posn = PerlIO_tell(PerlIONext(f));
2971 b->ptr = b->end = b->buf;
2972 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
2974 * FIXME: Is this right for read case ?
2976 if (PerlIO_flush(PerlIONext(f)) != 0)
2982 PerlIOBuf_fill(PerlIO *f)
2984 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
2985 PerlIO *n = PerlIONext(f);
2988 * FIXME: doing the down-stream flush is a bad idea if it causes
2989 * pre-read data in stdio buffer to be discarded but this is too
2990 * simplistic - as it skips _our_ hosekeeping and breaks tell tests.
2991 * if (!(PerlIOBase(f)->flags & PERLIO_F_RDBUF)) { }
2993 if (PerlIO_flush(f) != 0)
2995 if (PerlIOBase(f)->flags & PERLIO_F_TTY)
2996 PerlIOBase_flush_linebuf();
2999 PerlIO_get_base(f); /* allocate via vtable */
3001 b->ptr = b->end = b->buf;
3002 if (PerlIO_fast_gets(n)) {
3004 * Layer below is also buffered We do _NOT_ want to call its
3005 * ->Read() because that will loop till it gets what we asked for
3006 * which may hang on a pipe etc. Instead take anything it has to
3007 * hand, or ask it to fill _once_.
3009 avail = PerlIO_get_cnt(n);
3011 avail = PerlIO_fill(n);
3013 avail = PerlIO_get_cnt(n);
3015 if (!PerlIO_error(n) && PerlIO_eof(n))
3020 STDCHAR *ptr = PerlIO_get_ptr(n);
3021 SSize_t cnt = avail;
3022 if (avail > b->bufsiz)
3024 Copy(ptr, b->buf, avail, STDCHAR);
3025 PerlIO_set_ptrcnt(n, ptr + avail, cnt - avail);
3029 avail = PerlIO_read(n, b->ptr, b->bufsiz);
3033 PerlIOBase(f)->flags |= PERLIO_F_EOF;
3035 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
3038 b->end = b->buf + avail;
3039 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3044 PerlIOBuf_read(PerlIO *f, void *vbuf, Size_t count)
3046 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3050 return PerlIOBase_read(f, vbuf, count);
3056 PerlIOBuf_unread(PerlIO *f, const void *vbuf, Size_t count)
3058 const STDCHAR *buf = (const STDCHAR *) vbuf + count;
3059 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3062 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
3067 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
3069 * Buffer is already a read buffer, we can overwrite any chars
3070 * which have been read back to buffer start
3072 avail = (b->ptr - b->buf);
3076 * Buffer is idle, set it up so whole buffer is available for
3080 b->end = b->buf + avail;
3082 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3084 * Buffer extends _back_ from where we are now
3086 b->posn -= b->bufsiz;
3088 if (avail > (SSize_t) count) {
3090 * If we have space for more than count, just move count
3098 * In simple stdio-like ungetc() case chars will be already
3101 if (buf != b->ptr) {
3102 Copy(buf, b->ptr, avail, STDCHAR);
3106 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
3113 PerlIOBuf_write(PerlIO *f, const void *vbuf, Size_t count)
3115 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3116 const STDCHAR *buf = (const STDCHAR *) vbuf;
3120 if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
3123 SSize_t avail = b->bufsiz - (b->ptr - b->buf);
3124 if ((SSize_t) count < avail)
3126 PerlIOBase(f)->flags |= PERLIO_F_WRBUF;
3127 if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF) {
3142 Copy(buf, b->ptr, avail, STDCHAR);
3149 if (b->ptr >= (b->buf + b->bufsiz))
3152 if (PerlIOBase(f)->flags & PERLIO_F_UNBUF)
3158 PerlIOBuf_seek(PerlIO *f, Off_t offset, int whence)
3161 if ((code = PerlIO_flush(f)) == 0) {
3162 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3163 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
3164 code = PerlIO_seek(PerlIONext(f), offset, whence);
3166 b->posn = PerlIO_tell(PerlIONext(f));
3173 PerlIOBuf_tell(PerlIO *f)
3175 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3177 * b->posn is file position where b->buf was read, or will be written
3179 Off_t posn = b->posn;
3182 * If buffer is valid adjust position by amount in buffer
3184 posn += (b->ptr - b->buf);
3190 PerlIOBuf_close(PerlIO *f)
3192 IV code = PerlIOBase_close(f);
3193 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3194 if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
3198 b->ptr = b->end = b->buf;
3199 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
3204 PerlIOBuf_get_ptr(PerlIO *f)
3206 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3213 PerlIOBuf_get_cnt(PerlIO *f)
3215 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3218 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF)
3219 return (b->end - b->ptr);
3224 PerlIOBuf_get_base(PerlIO *f)
3226 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3231 Newz('B',b->buf,b->bufsiz, STDCHAR);
3233 b->buf = (STDCHAR *) & b->oneword;
3234 b->bufsiz = sizeof(b->oneword);
3243 PerlIOBuf_bufsiz(PerlIO *f)
3245 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3248 return (b->end - b->buf);
3252 PerlIOBuf_set_ptrcnt(PerlIO *f, STDCHAR * ptr, SSize_t cnt)
3254 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3258 if (PerlIO_get_cnt(f) != cnt || b->ptr < b->buf) {
3260 assert(PerlIO_get_cnt(f) == cnt);
3261 assert(b->ptr >= b->buf);
3263 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3267 PerlIOBuf_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
3269 return PerlIOBase_dup(aTHX_ f, o, param, flags);
3274 PerlIO_funcs PerlIO_perlio = {
3294 PerlIOBase_clearerr,
3295 PerlIOBase_setlinebuf,
3300 PerlIOBuf_set_ptrcnt,
3303 /*--------------------------------------------------------------------------------------*/
3305 * Temp layer to hold unread chars when cannot do it any other way
3309 PerlIOPending_fill(PerlIO *f)
3312 * Should never happen
3319 PerlIOPending_close(PerlIO *f)
3322 * A tad tricky - flush pops us, then we close new top
3325 return PerlIO_close(f);
3329 PerlIOPending_seek(PerlIO *f, Off_t offset, int whence)
3332 * A tad tricky - flush pops us, then we seek new top
3335 return PerlIO_seek(f, offset, whence);
3340 PerlIOPending_flush(PerlIO *f)
3343 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3344 if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
3348 PerlIO_pop(aTHX_ f);
3353 PerlIOPending_set_ptrcnt(PerlIO *f, STDCHAR * ptr, SSize_t cnt)
3359 PerlIOBuf_set_ptrcnt(f, ptr, cnt);
3364 PerlIOPending_pushed(PerlIO *f, const char *mode, SV *arg)
3366 IV code = PerlIOBase_pushed(f, mode, arg);
3367 PerlIOl *l = PerlIOBase(f);
3369 * Our PerlIO_fast_gets must match what we are pushed on, or sv_gets()
3370 * etc. get muddled when it changes mid-string when we auto-pop.
3372 l->flags = (l->flags & ~(PERLIO_F_FASTGETS | PERLIO_F_UTF8)) |
3373 (PerlIOBase(PerlIONext(f))->
3374 flags & (PERLIO_F_FASTGETS | PERLIO_F_UTF8));
3379 PerlIOPending_read(PerlIO *f, void *vbuf, Size_t count)
3381 SSize_t avail = PerlIO_get_cnt(f);
3386 got = PerlIOBuf_read(f, vbuf, avail);
3387 if (got >= 0 && got < count) {
3389 PerlIO_read(f, ((STDCHAR *) vbuf) + got, count - got);
3390 if (more >= 0 || got == 0)
3396 PerlIO_funcs PerlIO_pending = {
3400 PerlIOPending_pushed,
3411 PerlIOPending_close,
3412 PerlIOPending_flush,
3416 PerlIOBase_clearerr,
3417 PerlIOBase_setlinebuf,
3422 PerlIOPending_set_ptrcnt,
3427 /*--------------------------------------------------------------------------------------*/
3429 * crlf - translation On read translate CR,LF to "\n" we do this by
3430 * overriding ptr/cnt entries to hand back a line at a time and keeping a
3431 * record of which nl we "lied" about. On write translate "\n" to CR,LF
3435 PerlIOBuf base; /* PerlIOBuf stuff */
3436 STDCHAR *nl; /* Position of crlf we "lied" about in the
3441 PerlIOCrlf_pushed(PerlIO *f, const char *mode, SV *arg)
3444 PerlIOBase(f)->flags |= PERLIO_F_CRLF;
3445 code = PerlIOBuf_pushed(f, mode, arg);
3447 PerlIO_debug("PerlIOCrlf_pushed f=%p %s %s fl=%08" UVxf "\n",
3448 f, PerlIOBase(f)->tab->name, (mode) ? mode : "(Null)",
3449 PerlIOBase(f)->flags);
3456 PerlIOCrlf_unread(PerlIO *f, const void *vbuf, Size_t count)
3458 PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
3463 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF))
3464 return PerlIOBuf_unread(f, vbuf, count);
3466 const STDCHAR *buf = (const STDCHAR *) vbuf + count;
3467 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3469 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
3474 if (!(PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
3475 b->end = b->ptr = b->buf + b->bufsiz;
3476 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3477 b->posn -= b->bufsiz;
3479 while (count > 0 && b->ptr > b->buf) {
3482 if (b->ptr - 2 >= b->buf) {
3505 PerlIOCrlf_get_cnt(PerlIO *f)
3507 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3510 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
3511 PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
3512 if ((PerlIOBase(f)->flags & PERLIO_F_CRLF) && !c->nl) {
3513 STDCHAR *nl = b->ptr;
3515 while (nl < b->end && *nl != 0xd)
3517 if (nl < b->end && *nl == 0xd) {
3519 if (nl + 1 < b->end) {
3526 * Not CR,LF but just CR
3534 * Blast - found CR as last char in buffer
3538 * They may not care, defer work as long as
3541 return (nl - b->ptr);
3545 b->ptr++; /* say we have read it as far as
3546 * flush() is concerned */
3547 b->buf++; /* Leave space in front of buffer */
3548 b->bufsiz--; /* Buffer is thus smaller */
3549 code = PerlIO_fill(f); /* Fetch some more */
3550 b->bufsiz++; /* Restore size for next time */
3551 b->buf--; /* Point at space */
3552 b->ptr = nl = b->buf; /* Which is what we hand
3554 b->posn--; /* Buffer starts here */
3555 *nl = 0xd; /* Fill in the CR */
3557 goto test; /* fill() call worked */
3559 * CR at EOF - just fall through
3565 return (((c->nl) ? (c->nl + 1) : b->end) - b->ptr);
3571 PerlIOCrlf_set_ptrcnt(PerlIO *f, STDCHAR * ptr, SSize_t cnt)
3573 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3574 PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
3575 IV flags = PerlIOBase(f)->flags;
3583 if ((flags & PERLIO_F_CRLF) && ptr > b->buf && ptr[-1] == 0xd)
3590 * Test code - delete when it works ...
3597 if ((flags & PERLIO_F_CRLF) && chk > b->buf && chk[-1] == 0xd)
3604 Perl_croak(aTHX_ "ptr wrong %p != %p fl=%08" UVxf
3605 " nl=%p e=%p for %d", ptr, chk, flags, c->nl,
3612 * They have taken what we lied about
3620 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3624 PerlIOCrlf_write(PerlIO *f, const void *vbuf, Size_t count)
3626 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF))
3627 return PerlIOBuf_write(f, vbuf, count);
3629 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3630 const STDCHAR *buf = (const STDCHAR *) vbuf;
3631 const STDCHAR *ebuf = buf + count;
3634 if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
3636 while (buf < ebuf) {
3637 STDCHAR *eptr = b->buf + b->bufsiz;
3638 PerlIOBase(f)->flags |= PERLIO_F_WRBUF;
3639 while (buf < ebuf && b->ptr < eptr) {
3641 if ((b->ptr + 2) > eptr) {
3649 *(b->ptr)++ = 0xd; /* CR */
3650 *(b->ptr)++ = 0xa; /* LF */
3652 if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF) {
3662 if (b->ptr >= eptr) {
3668 if (PerlIOBase(f)->flags & PERLIO_F_UNBUF)
3670 return (buf - (STDCHAR *) vbuf);
3675 PerlIOCrlf_flush(PerlIO *f)
3677 PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
3682 return PerlIOBuf_flush(f);
3685 PerlIO_funcs PerlIO_crlf = {
3688 PERLIO_K_BUFFERED | PERLIO_K_CANCRLF,
3690 PerlIOBase_noop_ok, /* popped */
3695 PerlIOBuf_read, /* generic read works with ptr/cnt lies
3697 PerlIOCrlf_unread, /* Put CR,LF in buffer for each '\n' */
3698 PerlIOCrlf_write, /* Put CR,LF in buffer for each '\n' */
3706 PerlIOBase_clearerr,
3707 PerlIOBase_setlinebuf,
3712 PerlIOCrlf_set_ptrcnt,
3716 /*--------------------------------------------------------------------------------------*/
3718 * mmap as "buffer" layer
3722 PerlIOBuf base; /* PerlIOBuf stuff */
3723 Mmap_t mptr; /* Mapped address */
3724 Size_t len; /* mapped length */
3725 STDCHAR *bbuf; /* malloced buffer if map fails */
3728 static size_t page_size = 0;
3731 PerlIOMmap_map(PerlIO *f)
3734 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3735 IV flags = PerlIOBase(f)->flags;
3739 if (flags & PERLIO_F_CANREAD) {
3740 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3741 int fd = PerlIO_fileno(f);
3743 code = Fstat(fd, &st);
3744 if (code == 0 && S_ISREG(st.st_mode)) {
3745 SSize_t len = st.st_size - b->posn;
3749 #if defined(HAS_SYSCONF) && (defined(_SC_PAGESIZE) || defined(_SC_PAGE_SIZE))
3751 SETERRNO(0, SS$_NORMAL);
3752 # ifdef _SC_PAGESIZE
3753 page_size = sysconf(_SC_PAGESIZE);
3755 page_size = sysconf(_SC_PAGE_SIZE);
3757 if ((long) page_size < 0) {
3762 (void) SvUPGRADE(error, SVt_PV);
3763 msg = SvPVx(error, n_a);
3764 Perl_croak(aTHX_ "panic: sysconf: %s",
3769 "panic: sysconf: pagesize unknown");
3773 # ifdef HAS_GETPAGESIZE
3774 page_size = getpagesize();
3776 # if defined(I_SYS_PARAM) && defined(PAGESIZE)
3777 page_size = PAGESIZE; /* compiletime, bad */
3781 if ((IV) page_size <= 0)
3782 Perl_croak(aTHX_ "panic: bad pagesize %" IVdf,
3787 * This is a hack - should never happen - open should
3790 b->posn = PerlIO_tell(PerlIONext(f));
3792 posn = (b->posn / page_size) * page_size;
3793 len = st.st_size - posn;
3794 m->mptr = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, posn);
3795 if (m->mptr && m->mptr != (Mmap_t) - 1) {
3796 #if 0 && defined(HAS_MADVISE) && defined(MADV_SEQUENTIAL)
3797 madvise(m->mptr, len, MADV_SEQUENTIAL);
3799 #if 0 && defined(HAS_MADVISE) && defined(MADV_WILLNEED)
3800 madvise(m->mptr, len, MADV_WILLNEED);
3802 PerlIOBase(f)->flags =
3803 (flags & ~PERLIO_F_EOF) | PERLIO_F_RDBUF;
3804 b->end = ((STDCHAR *) m->mptr) + len;
3805 b->buf = ((STDCHAR *) m->mptr) + (b->posn - posn);
3814 PerlIOBase(f)->flags =
3815 flags | PERLIO_F_EOF | PERLIO_F_RDBUF;
3817 b->ptr = b->end = b->ptr;
3826 PerlIOMmap_unmap(PerlIO *f)
3828 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3829 PerlIOBuf *b = &m->base;
3833 code = munmap(m->mptr, m->len);
3837 if (PerlIO_seek(PerlIONext(f), b->posn, SEEK_SET) != 0)
3840 b->ptr = b->end = b->buf;
3841 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
3847 PerlIOMmap_get_base(PerlIO *f)
3849 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3850 PerlIOBuf *b = &m->base;
3851 if (b->buf && (PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
3853 * Already have a readbuffer in progress
3859 * We have a write buffer or flushed PerlIOBuf read buffer
3861 m->bbuf = b->buf; /* save it in case we need it again */
3862 b->buf = NULL; /* Clear to trigger below */
3865 PerlIOMmap_map(f); /* Try and map it */
3868 * Map did not work - recover PerlIOBuf buffer if we have one
3873 b->ptr = b->end = b->buf;
3876 return PerlIOBuf_get_base(f);
3880 PerlIOMmap_unread(PerlIO *f, const void *vbuf, Size_t count)
3882 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3883 PerlIOBuf *b = &m->base;
3884 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
3886 if (b->ptr && (b->ptr - count) >= b->buf
3887 && memEQ(b->ptr - count, vbuf, count)) {
3889 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
3894 * Loose the unwritable mapped buffer
3898 * If flush took the "buffer" see if we have one from before
3900 if (!b->buf && m->bbuf)
3903 PerlIOBuf_get_base(f);
3907 return PerlIOBuf_unread(f, vbuf, count);
3911 PerlIOMmap_write(PerlIO *f, const void *vbuf, Size_t count)
3913 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3914 PerlIOBuf *b = &m->base;
3915 if (!b->buf || !(PerlIOBase(f)->flags & PERLIO_F_WRBUF)) {
3917 * No, or wrong sort of, buffer
3920 if (PerlIOMmap_unmap(f) != 0)
3924 * If unmap took the "buffer" see if we have one from before
3926 if (!b->buf && m->bbuf)
3929 PerlIOBuf_get_base(f);
3933 return PerlIOBuf_write(f, vbuf, count);
3937 PerlIOMmap_flush(PerlIO *f)
3939 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3940 PerlIOBuf *b = &m->base;
3941 IV code = PerlIOBuf_flush(f);
3943 * Now we are "synced" at PerlIOBuf level
3950 if (PerlIOMmap_unmap(f) != 0)
3955 * We seem to have a PerlIOBuf buffer which was not mapped
3956 * remember it in case we need one later
3965 PerlIOMmap_fill(PerlIO *f)
3967 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3968 IV code = PerlIO_flush(f);
3969 if (code == 0 && !b->buf) {
3970 code = PerlIOMmap_map(f);
3972 if (code == 0 && !(PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
3973 code = PerlIOBuf_fill(f);
3979 PerlIOMmap_close(PerlIO *f)
3981 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3982 PerlIOBuf *b = &m->base;
3983 IV code = PerlIO_flush(f);
3987 b->ptr = b->end = b->buf;
3989 if (PerlIOBuf_close(f) != 0)
3995 PerlIOMmap_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
3997 return PerlIOBase_dup(aTHX_ f, o, param, flags);
4001 PerlIO_funcs PerlIO_mmap = {
4021 PerlIOBase_clearerr,
4022 PerlIOBase_setlinebuf,
4023 PerlIOMmap_get_base,
4027 PerlIOBuf_set_ptrcnt,
4030 #endif /* HAS_MMAP */
4038 PerlIO_stdstreams(aTHX);
4040 return &PL_perlio[1];
4043 #undef PerlIO_stdout
4049 PerlIO_stdstreams(aTHX);
4051 return &PL_perlio[2];
4054 #undef PerlIO_stderr
4060 PerlIO_stdstreams(aTHX);
4062 return &PL_perlio[3];
4065 /*--------------------------------------------------------------------------------------*/
4067 #undef PerlIO_getname
4069 PerlIO_getname(PerlIO *f, char *buf)
4074 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
4076 name = fgetname(stdio, buf);
4078 Perl_croak(aTHX_ "Don't know how to get file name");
4084 /*--------------------------------------------------------------------------------------*/
4086 * Functions which can be called on any kind of PerlIO implemented in
4092 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)
4108 if (PerlIO_unread(f, &buf, 1) == 1)
4116 PerlIO_putc(PerlIO *f, int ch)
4119 return PerlIO_write(f, &buf, 1);
4124 PerlIO_puts(PerlIO *f, const char *s)
4126 STRLEN len = strlen(s);
4127 return PerlIO_write(f, s, len);
4130 #undef PerlIO_rewind
4132 PerlIO_rewind(PerlIO *f)
4134 PerlIO_seek(f, (Off_t) 0, SEEK_SET);
4138 #undef PerlIO_vprintf
4140 PerlIO_vprintf(PerlIO *f, const char *fmt, va_list ap)
4143 SV *sv = newSVpvn("", 0);
4149 Perl_va_copy(ap, apc);
4150 sv_vcatpvf(sv, fmt, &apc);
4152 sv_vcatpvf(sv, fmt, &ap);
4155 wrote = PerlIO_write(f, s, len);
4160 #undef PerlIO_printf
4162 PerlIO_printf(PerlIO *f, const char *fmt, ...)
4167 result = PerlIO_vprintf(f, fmt, ap);
4172 #undef PerlIO_stdoutf
4174 PerlIO_stdoutf(const char *fmt, ...)
4179 result = PerlIO_vprintf(PerlIO_stdout(), fmt, ap);
4184 #undef PerlIO_tmpfile
4186 PerlIO_tmpfile(void)
4189 * I have no idea how portable mkstemp() is ...
4191 #if defined(WIN32) || !defined(HAVE_MKSTEMP)
4194 FILE *stdio = PerlSIO_tmpfile();
4197 PerlIOSelf(PerlIO_push
4198 (aTHX_(f = PerlIO_allocate(aTHX)), &PerlIO_stdio,
4199 "w+", Nullsv), PerlIOStdio);
4205 SV *sv = newSVpv("/tmp/PerlIO_XXXXXX", 0);
4206 int fd = mkstemp(SvPVX(sv));
4209 f = PerlIO_fdopen(fd, "w+");
4211 PerlIOBase(f)->flags |= PERLIO_F_TEMP;
4213 PerlLIO_unlink(SvPVX(sv));
4223 #endif /* USE_SFIO */
4224 #endif /* PERLIO_IS_STDIO */
4226 /*======================================================================================*/
4228 * Now some functions in terms of above which may be needed even if we are
4229 * not in true PerlIO mode
4233 #undef PerlIO_setpos
4235 PerlIO_setpos(PerlIO *f, SV *pos)
4240 Off_t *posn = (Off_t *) SvPV(pos, len);
4241 if (f && len == sizeof(Off_t))
4242 return PerlIO_seek(f, *posn, SEEK_SET);
4244 SETERRNO(EINVAL, SS$_IVCHAN);
4248 #undef PerlIO_setpos
4250 PerlIO_setpos(PerlIO *f, SV *pos)
4255 Fpos_t *fpos = (Fpos_t *) SvPV(pos, len);
4256 if (f && len == sizeof(Fpos_t)) {
4257 #if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64)
4258 return fsetpos64(f, fpos);
4260 return fsetpos(f, fpos);
4264 SETERRNO(EINVAL, SS$_IVCHAN);
4270 #undef PerlIO_getpos
4272 PerlIO_getpos(PerlIO *f, SV *pos)
4275 Off_t posn = PerlIO_tell(f);
4276 sv_setpvn(pos, (char *) &posn, sizeof(posn));
4277 return (posn == (Off_t) - 1) ? -1 : 0;
4280 #undef PerlIO_getpos
4282 PerlIO_getpos(PerlIO *f, SV *pos)
4287 #if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64)
4288 code = fgetpos64(f, &fpos);
4290 code = fgetpos(f, &fpos);
4292 sv_setpvn(pos, (char *) &fpos, sizeof(fpos));
4297 #if (defined(PERLIO_IS_STDIO) || !defined(USE_SFIO)) && !defined(HAS_VPRINTF)
4300 vprintf(char *pat, char *args)
4302 _doprnt(pat, args, stdout);
4303 return 0; /* wrong, but perl doesn't use the return
4308 vfprintf(FILE *fd, char *pat, char *args)
4310 _doprnt(pat, args, fd);
4311 return 0; /* wrong, but perl doesn't use the return
4317 #ifndef PerlIO_vsprintf
4319 PerlIO_vsprintf(char *s, int n, const char *fmt, va_list ap)
4321 int val = vsprintf(s, fmt, ap);
4323 if (strlen(s) >= (STRLEN) n) {
4325 (void) PerlIO_puts(Perl_error_log,
4326 "panic: sprintf overflow - memory corrupted!\n");
4334 #ifndef PerlIO_sprintf
4336 PerlIO_sprintf(char *s, int n, const char *fmt, ...)
4341 result = PerlIO_vsprintf(s, n, fmt, ap);