3 * Copyright (c) 1996-2001, Nick Ing-Simmons
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
10 /* If we have ActivePerl-like PERL_IMPLICIT_SYS then we need
11 a dTHX to get at the dispatch tables, even when we do not
12 need it for other reasons.
13 Invent a dSYS macro to abstract this out
15 #ifdef PERL_IMPLICIT_SYS
28 #define PERLIO_NOT_STDIO 0
29 #if !defined(PERLIO_IS_STDIO) && !defined(USE_SFIO)
30 /* #define PerlIO FILE */
33 * This file provides those parts of PerlIO abstraction
34 * which are not #defined in perlio.h.
35 * Which these are depends on various Configure #ifdef's
39 #define PERL_IN_PERLIO_C
42 #undef PerlMemShared_calloc
43 #define PerlMemShared_calloc(x,y) calloc(x,y)
44 #undef PerlMemShared_free
45 #define PerlMemShared_free(x) free(x)
48 perlsio_binmode(FILE *fp, int iotype, int mode)
50 /* This used to be contents of do_binmode in doio.c */
52 # if defined(atarist) || defined(__MINT__)
55 ((FILE*)fp)->_flag |= _IOBIN;
57 ((FILE*)fp)->_flag &= ~ _IOBIN;
63 if (PerlLIO_setmode(fileno(fp), mode) != -1) {
64 # if defined(WIN32) && defined(__BORLANDC__)
65 /* The translation mode of the stream is maintained independent
66 * of the translation mode of the fd in the Borland RTL (heavy
67 * digging through their runtime sources reveal). User has to
68 * set the mode explicitly for the stream (though they don't
69 * document this anywhere). GSAR 97-5-24
75 fp->flags &= ~ _F_BIN;
83 # if defined(USEMYBINMODE)
84 if (my_binmode(fp, iotype, mode) != FALSE)
96 PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names)
98 if (!names || !*names || strEQ(names,":crlf") || strEQ(names,":raw"))
102 Perl_croak(aTHX_ "Cannot apply \"%s\" in non-PerlIO perl",names);
108 PerlIO_destruct(pTHX)
113 PerlIO_binmode(pTHX_ PerlIO *fp, int iotype, int mode, const char *names)
115 return perlsio_binmode(fp,iotype,mode);
118 /* De-mux PerlIO_openn() into fdopen, freopen and fopen type entries */
121 PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd, int imode, int perm, PerlIO *old, int narg, SV **args)
125 if (*args == &PL_sv_undef)
126 return PerlIO_tmpfile();
129 char *name = SvPV_nolen(*args);
132 fd = PerlLIO_open3(name,imode,perm);
134 return PerlIO_fdopen(fd,(char *)mode+1);
138 return PerlIO_reopen(name,mode,old);
142 return PerlIO_open(name,mode);
148 return PerlIO_fdopen(fd,(char *)mode);
156 #ifdef PERLIO_IS_STDIO
161 /* Does nothing (yet) except force this file to be included
162 in perl binary. That allows this file to force inclusion
163 of other functions that may be required by loadable
164 extensions e.g. for FileHandle::tmpfile
168 #undef PerlIO_tmpfile
175 #else /* PERLIO_IS_STDIO */
182 /* This section is just to make sure these functions
183 get pulled in from libsfio.a
186 #undef PerlIO_tmpfile
196 /* Force this file to be included in perl binary. Which allows
197 * this file to force inclusion of other functions that may be
198 * required by loadable extensions e.g. for FileHandle::tmpfile
202 * sfio does its own 'autoflush' on stdout in common cases.
203 * Flush results in a lot of lseek()s to regular files and
204 * lot of small writes to pipes.
206 sfset(sfstdout,SF_SHARE,0);
210 /*======================================================================================*/
211 /* Implement all the PerlIO interface ourselves.
216 /* We _MUST_ have <unistd.h> if we are using lseek() and may have large files */
221 #include <sys/mman.h>
226 void PerlIO_debug(const char *fmt,...) __attribute__((format(__printf__,1,2)));
229 PerlIO_debug(const char *fmt,...)
237 char *s = PerlEnv_getenv("PERLIO_DEBUG");
239 dbg = PerlLIO_open3(s,O_WRONLY|O_CREAT|O_APPEND,0666);
246 SV *sv = newSVpvn("",0);
249 s = CopFILE(PL_curcop);
252 Perl_sv_catpvf(aTHX_ sv, "%s:%"IVdf" ", s, (IV)CopLINE(PL_curcop));
253 Perl_sv_vcatpvf(aTHX_ sv, fmt, &ap);
256 PerlLIO_write(dbg,s,len);
262 /*--------------------------------------------------------------------------------------*/
264 /* Inner level routines */
266 /* Table of pointers to the PerlIO structs (malloc'ed) */
267 PerlIO *_perlio = NULL;
268 #define PERLIO_TABLE_SIZE 64
273 PerlIO_allocate(pTHX)
275 /* Find a free slot in the table, allocating new table as necessary */
282 last = (PerlIO **)(f);
283 for (i=1; i < PERLIO_TABLE_SIZE; i++)
291 f = PerlMemShared_calloc(PERLIO_TABLE_SIZE,sizeof(PerlIO));
301 PerlIO_cleantable(pTHX_ PerlIO **tablep)
303 PerlIO *table = *tablep;
307 PerlIO_cleantable(aTHX_ (PerlIO **) &(table[0]));
308 for (i=PERLIO_TABLE_SIZE-1; i > 0; i--)
316 PerlMemShared_free(table);
325 PerlIO_cleanup_layers(pTHXo_ void *data)
327 PerlIO_layer_hv = Nullhv;
328 PerlIO_layer_av = Nullav;
335 PerlIO_cleantable(aTHX_ &_perlio);
339 PerlIO_destruct(pTHX)
341 PerlIO **table = &_perlio;
346 table = (PerlIO **)(f++);
347 for (i=1; i < PERLIO_TABLE_SIZE; i++)
353 if (l->tab->kind & PERLIO_K_DESTRUCT)
355 PerlIO_debug("Destruct popping %s\n",l->tab->name);
370 PerlIO_pop(pTHX_ PerlIO *f)
375 PerlIO_debug("PerlIO_pop f=%p %s\n",f,l->tab->name);
377 (*l->tab->Popped)(f);
379 PerlMemShared_free(l);
383 /*--------------------------------------------------------------------------------------*/
384 /* XS Interface for perl code */
387 PerlIO_find_layer(pTHX_ const char *name, STRLEN len, int load)
391 if ((SSize_t) len <= 0)
393 svp = hv_fetch(PerlIO_layer_hv,name,len,0);
394 if (!svp && load && PL_subname && PerlIO_layer_av && av_len(PerlIO_layer_av)+1 >= 2)
396 SV *pkgsv = newSVpvn("PerlIO",6);
397 SV *layer = newSVpvn(name,len);
399 /* The two SVs are magically freed by load_module */
400 Perl_load_module(aTHX_ 0, pkgsv, Nullsv, layer, Nullsv);
402 /* Say this is lvalue so we get an 'undef' if still not there */
403 svp = hv_fetch(PerlIO_layer_hv,name,len,1);
405 if (svp && (sv = *svp))
413 #ifdef USE_ATTRIBUTES_FOR_PERLIO
416 perlio_mg_set(pTHX_ SV *sv, MAGIC *mg)
420 IO *io = GvIOn((GV *)SvRV(sv));
421 PerlIO *ifp = IoIFP(io);
422 PerlIO *ofp = IoOFP(io);
423 Perl_warn(aTHX_ "set %"SVf" %p %p %p",sv,io,ifp,ofp);
429 perlio_mg_get(pTHX_ SV *sv, MAGIC *mg)
433 IO *io = GvIOn((GV *)SvRV(sv));
434 PerlIO *ifp = IoIFP(io);
435 PerlIO *ofp = IoOFP(io);
436 Perl_warn(aTHX_ "get %"SVf" %p %p %p",sv,io,ifp,ofp);
442 perlio_mg_clear(pTHX_ SV *sv, MAGIC *mg)
444 Perl_warn(aTHX_ "clear %"SVf,sv);
449 perlio_mg_free(pTHX_ SV *sv, MAGIC *mg)
451 Perl_warn(aTHX_ "free %"SVf,sv);
455 MGVTBL perlio_vtab = {
463 XS(XS_io_MODIFY_SCALAR_ATTRIBUTES)
466 SV *sv = SvRV(ST(1));
471 sv_magic(sv, (SV *)av, PERL_MAGIC_ext, NULL, 0);
473 mg = mg_find(sv, PERL_MAGIC_ext);
474 mg->mg_virtual = &perlio_vtab;
476 Perl_warn(aTHX_ "attrib %"SVf,sv);
477 for (i=2; i < items; i++)
480 const char *name = SvPV(ST(i),len);
481 SV *layer = PerlIO_find_layer(aTHX_ name,len,1);
484 av_push(av,SvREFCNT_inc(layer));
496 #endif /* USE_ATTIBUTES_FOR_PERLIO */
499 PerlIO_tab_sv(pTHX_ PerlIO_funcs *tab)
501 HV *stash = gv_stashpv("PerlIO::Layer", TRUE);
502 SV *sv = sv_bless(newRV_noinc(newSViv(PTR2IV(tab))),stash);
507 PerlIO_define_layer(pTHX_ PerlIO_funcs *tab)
509 if (!PerlIO_layer_hv)
511 PerlIO_layer_hv = get_hv("open::layers",GV_ADD|GV_ADDMULTI);
513 hv_store(PerlIO_layer_hv,tab->name,strlen(tab->name),PerlIO_tab_sv(aTHX_ tab),0);
514 PerlIO_debug("define %s %p\n",tab->name,tab);
518 PerlIO_parse_layers(pTHX_ AV *av, const char *names)
522 const char *s = names;
525 while (isSPACE(*s) || *s == ':')
531 const char *as = Nullch;
535 /* Message is consistent with how attribute lists are passed.
536 Even though this means "foo : : bar" is seen as an invalid separator
538 char q = ((*s == '\'') ? '"' : '\'');
539 Perl_warn(aTHX_ "perlio: invalid separator character %c%c%c in layer specification list", q, *s, q);
545 } while (isALNUM(*e));
563 /* It's a nul terminated string, not allowed to \ the terminating null.
564 Anything other character is passed over. */
572 Perl_warn(aTHX_ "perlio: argument list not closed for layer \"%.*s\"",(int)(e - s),s);
582 SV *layer = PerlIO_find_layer(aTHX_ s,llen,1);
585 av_push(av,SvREFCNT_inc(layer));
586 av_push(av,(as) ? newSVpvn(as,alen) : &PL_sv_undef);
589 Perl_warn(aTHX_ "perlio: unknown layer \"%.*s\"",(int)llen,s);
601 PerlIO_default_buffer(pTHX_ AV *av)
603 PerlIO_funcs *tab = &PerlIO_perlio;
604 if (O_BINARY != O_TEXT)
610 if (PerlIO_stdio.Set_ptrcnt)
615 PerlIO_debug("Pushing %s\n",tab->name);
616 av_push(av,SvREFCNT_inc(PerlIO_find_layer(aTHX_ tab->name,0,0)));
617 av_push(av,&PL_sv_undef);
621 PerlIO_arg_fetch(pTHX_ AV *av,IV n)
623 SV **svp = av_fetch(av,n,FALSE);
624 return (svp) ? *svp : Nullsv;
628 PerlIO_layer_fetch(pTHX_ AV *av,IV n,PerlIO_funcs *def)
630 SV **svp = av_fetch(av,n,FALSE);
632 if (svp && (layer = *svp) && SvROK(layer) && SvIOK((layer = SvRV(layer))))
634 /* PerlIO_debug("Layer %d is %s\n",n/2,tab->name); */
635 return INT2PTR(PerlIO_funcs *, SvIV(layer));
638 Perl_croak(aTHX_ "panic: PerlIO layer array corrupt");
643 PerlIO_default_layers(pTHX)
646 if (!PerlIO_layer_av)
648 const char *s = (PL_tainting) ? Nullch : PerlEnv_getenv("PERLIO");
649 PerlIO_layer_av = get_av("open::layers",GV_ADD|GV_ADDMULTI);
651 #ifdef USE_ATTRIBUTES_FOR_PERLIO
652 newXS("io::MODIFY_SCALAR_ATTRIBUTES",XS_io_MODIFY_SCALAR_ATTRIBUTES,__FILE__);
655 PerlIO_define_layer(aTHX_ &PerlIO_raw);
656 PerlIO_define_layer(aTHX_ &PerlIO_unix);
657 PerlIO_define_layer(aTHX_ &PerlIO_perlio);
658 PerlIO_define_layer(aTHX_ &PerlIO_stdio);
659 PerlIO_define_layer(aTHX_ &PerlIO_crlf);
661 PerlIO_define_layer(aTHX_ &PerlIO_mmap);
663 PerlIO_define_layer(aTHX_ &PerlIO_utf8);
664 PerlIO_define_layer(aTHX_ &PerlIO_byte);
665 av_push(PerlIO_layer_av,SvREFCNT_inc(PerlIO_find_layer(aTHX_ PerlIO_unix.name,0,0)));
666 av_push(PerlIO_layer_av,&PL_sv_undef);
669 PerlIO_parse_layers(aTHX_ PerlIO_layer_av,s);
673 PerlIO_default_buffer(aTHX_ PerlIO_layer_av);
676 len = av_len(PerlIO_layer_av)+1;
679 PerlIO_default_buffer(aTHX_ PerlIO_layer_av);
680 len = av_len(PerlIO_layer_av);
682 return PerlIO_layer_av;
687 PerlIO_default_layer(pTHX_ I32 n)
689 AV *av = PerlIO_default_layers(aTHX);
692 n += av_len(PerlIO_layer_av)+1;
693 return PerlIO_layer_fetch(aTHX_ av,n, &PerlIO_stdio);
696 #define PerlIO_default_top() PerlIO_default_layer(aTHX_ -1)
697 #define PerlIO_default_btm() PerlIO_default_layer(aTHX_ 0)
700 PerlIO_stdstreams(pTHX)
704 PerlIO_allocate(aTHX);
705 PerlIO_fdopen(0,"Ir" PERLIO_STDTEXT);
706 PerlIO_fdopen(1,"Iw" PERLIO_STDTEXT);
707 PerlIO_fdopen(2,"Iw" PERLIO_STDTEXT);
712 PerlIO_push(pTHX_ PerlIO *f,PerlIO_funcs *tab,const char *mode,SV *arg)
715 l = PerlMemShared_calloc(tab->size,sizeof(char));
718 Zero(l,tab->size,char);
722 PerlIO_debug("PerlIO_push f=%p %s %s %p\n",f,tab->name,
723 (mode) ? mode : "(Null)",arg);
724 if ((*l->tab->Pushed)(f,mode,arg) != 0)
734 PerlIOPop_pushed(PerlIO *f, const char *mode, SV *arg)
748 PerlIORaw_pushed(PerlIO *f, const char *mode, SV *arg)
750 /* Remove the dummy layer */
753 /* Pop back to bottom layer */
757 while (!(PerlIOBase(f)->tab->kind & PERLIO_K_RAW))
765 /* Nothing bellow - push unix on top then remove it */
766 if (PerlIO_push(aTHX_ f,PerlIO_default_btm(),mode,arg))
768 PerlIO_pop(aTHX_ PerlIONext(f));
773 PerlIO_debug(":raw f=%p :%s\n",f,PerlIOBase(f)->tab->name);
780 PerlIO_apply_layera(pTHX_ PerlIO *f, const char *mode, AV *layers, IV n)
782 IV max = av_len(layers)+1;
786 PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers,n,NULL);
789 if (!PerlIO_push(aTHX_ f,tab,mode,PerlIOArg))
801 PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names)
806 AV *layers = newAV();
807 code = PerlIO_parse_layers(aTHX_ layers,names);
810 code = PerlIO_apply_layera(aTHX_ f, mode, layers, 0);
812 SvREFCNT_dec((SV *) layers);
818 /*--------------------------------------------------------------------------------------*/
819 /* Given the abstraction above the public API functions */
822 PerlIO_binmode(pTHX_ PerlIO *f, int iotype, int mode, const char *names)
824 PerlIO_debug("PerlIO_binmode f=%p %s %c %x %s\n",
825 f,PerlIOBase(f)->tab->name,iotype,mode, (names) ? names : "(Null)");
826 if (!names && (O_TEXT != O_BINARY && (mode & O_BINARY)))
832 if (PerlIOBase(top)->tab == &PerlIO_crlf)
835 PerlIOBase(top)->flags &= ~PERLIO_F_CRLF;
838 top = PerlIONext(top);
841 return PerlIO_apply_layers(aTHX_ f, NULL, names) == 0 ? TRUE : FALSE;
846 PerlIO__close(PerlIO *f)
849 return (*PerlIOBase(f)->tab->Close)(f);
852 SETERRNO(EBADF,SS$_IVCHAN);
857 #undef PerlIO_fdupopen
859 PerlIO_fdupopen(pTHX_ PerlIO *f)
864 int fd = PerlLIO_dup(PerlIO_fileno(f));
865 PerlIO *new = PerlIO_fdopen(fd,PerlIO_modestr(f,buf));
868 Off_t posn = PerlIO_tell(f);
869 PerlIO_seek(new,posn,SEEK_SET);
875 SETERRNO(EBADF,SS$_IVCHAN);
882 PerlIO_close(PerlIO *f)
888 code = (*PerlIOBase(f)->tab->Close)(f);
899 PerlIO_fileno(PerlIO *f)
902 return (*PerlIOBase(f)->tab->Fileno)(f);
905 SETERRNO(EBADF,SS$_IVCHAN);
911 PerlIO_context_layers(pTHX_ const char *mode)
913 const char *type = NULL;
914 /* Need to supply default layer info from open.pm */
917 SV *layers = PL_curcop->cop_io;
921 type = SvPV(layers,len);
922 if (type && mode[0] != 'r')
924 /* Skip to write part */
925 const char *s = strchr(type,0);
926 if (s && (s-type) < len)
937 PerlIO_layer_from_ref(pTHX_ SV *sv)
939 /* For any scalar type load the handler which is bundled with perl */
940 if (SvTYPE(sv) < SVt_PVAV)
941 return PerlIO_find_layer(aTHX_ "Scalar",6, 1);
943 /* For other types allow if layer is known but don't try and load it */
947 return PerlIO_find_layer(aTHX_ "Array",5, 0);
949 return PerlIO_find_layer(aTHX_ "Hash",4, 0);
951 return PerlIO_find_layer(aTHX_ "Code",4, 0);
953 return PerlIO_find_layer(aTHX_ "Glob",4, 0);
959 PerlIO_resolve_layers(pTHX_ const char *layers,const char *mode,int narg, SV **args)
961 AV *def = PerlIO_default_layers(aTHX);
964 PerlIO_stdstreams(aTHX);
968 /* If it is a reference but not an object see if we have a handler for it */
969 if (SvROK(arg) && !sv_isobject(arg))
971 SV *handler = PerlIO_layer_from_ref(aTHX_ SvRV(arg));
975 av_push(def,SvREFCNT_inc(handler));
976 av_push(def,&PL_sv_undef);
979 /* Don't fail if handler cannot be found
980 * :Via(...) etc. may do something sensible
981 * else we will just stringfy and open resulting string.
986 layers = PerlIO_context_layers(aTHX_ mode);
987 if (layers && *layers)
992 IV n = av_len(def)+1;
996 SV **svp = av_fetch(def,n,0);
997 av_store(av,n,(svp) ? SvREFCNT_inc(*svp) : &PL_sv_undef);
1004 PerlIO_parse_layers(aTHX_ av,layers);
1016 PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd, int imode, int perm, PerlIO *f, int narg, SV **args)
1018 if (!f && narg == 1 && *args == &PL_sv_undef)
1020 if ((f = PerlIO_tmpfile()))
1023 layers = PerlIO_context_layers(aTHX_ mode);
1024 if (layers && *layers)
1025 PerlIO_apply_layers(aTHX_ f,mode,layers);
1032 PerlIO_funcs *tab = NULL;
1035 /* This is "reopen" - it is not tested as perl does not use it yet */
1040 SV *arg = (l->tab->Getarg) ? (*l->tab->Getarg)(&l) : &PL_sv_undef;
1041 av_unshift(layera,2);
1042 av_store(layera,0,PerlIO_tab_sv(aTHX_ l->tab));
1043 av_store(layera,1,arg);
1044 l = *PerlIONext(&l);
1049 layera = PerlIO_resolve_layers(aTHX_ layers, mode, narg, args);
1051 n = av_len(layera)-1;
1054 PerlIO_funcs *t = PerlIO_layer_fetch(aTHX_ layera,n,NULL);
1064 PerlIO_debug("openn(%s,'%s','%s',%d,%x,%o,%p,%d,%p)\n",
1065 tab->name,layers,mode,fd,imode,perm,f,narg,args);
1066 f = (*tab->Open)(aTHX_ tab, layera, n, mode,fd,imode,perm,f,narg,args);
1069 if (n+2 < av_len(layera)+1)
1071 if (PerlIO_apply_layera(aTHX_ f, mode, layera, n+2) != 0)
1078 SvREFCNT_dec(layera);
1084 #undef PerlIO_fdopen
1086 PerlIO_fdopen(int fd, const char *mode)
1089 return PerlIO_openn(aTHX_ Nullch,mode,fd,0,0,NULL,0,NULL);
1094 PerlIO_open(const char *path, const char *mode)
1097 SV *name = sv_2mortal(newSVpvn(path,strlen(path)));
1098 return PerlIO_openn(aTHX_ Nullch,mode,-1,0,0,NULL,1,&name);
1101 #undef PerlIO_reopen
1103 PerlIO_reopen(const char *path, const char *mode, PerlIO *f)
1106 SV *name = sv_2mortal(newSVpvn(path,strlen(path)));
1107 return PerlIO_openn(aTHX_ Nullch,mode,-1,0,0,f,1,&name);
1112 PerlIO_read(PerlIO *f, void *vbuf, Size_t count)
1115 return (*PerlIOBase(f)->tab->Read)(f,vbuf,count);
1118 SETERRNO(EBADF,SS$_IVCHAN);
1123 #undef PerlIO_unread
1125 PerlIO_unread(PerlIO *f, const void *vbuf, Size_t count)
1128 return (*PerlIOBase(f)->tab->Unread)(f,vbuf,count);
1131 SETERRNO(EBADF,SS$_IVCHAN);
1138 PerlIO_write(PerlIO *f, const void *vbuf, Size_t count)
1141 return (*PerlIOBase(f)->tab->Write)(f,vbuf,count);
1144 SETERRNO(EBADF,SS$_IVCHAN);
1151 PerlIO_seek(PerlIO *f, Off_t offset, int whence)
1154 return (*PerlIOBase(f)->tab->Seek)(f,offset,whence);
1157 SETERRNO(EBADF,SS$_IVCHAN);
1164 PerlIO_tell(PerlIO *f)
1167 return (*PerlIOBase(f)->tab->Tell)(f);
1170 SETERRNO(EBADF,SS$_IVCHAN);
1177 PerlIO_flush(PerlIO *f)
1183 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1184 if (tab && tab->Flush)
1186 return (*tab->Flush)(f);
1190 PerlIO_debug("Cannot flush f=%p :%s\n",f,tab->name);
1191 SETERRNO(EBADF,SS$_IVCHAN);
1197 PerlIO_debug("Cannot flush f=%p\n",f);
1198 SETERRNO(EBADF,SS$_IVCHAN);
1204 /* Is it good API design to do flush-all on NULL,
1205 * a potentially errorneous input? Maybe some magical
1206 * value (PerlIO* PERLIO_FLUSH_ALL = (PerlIO*)-1;)?
1207 * Yes, stdio does similar things on fflush(NULL),
1208 * but should we be bound by their design decisions?
1210 PerlIO **table = &_perlio;
1212 while ((f = *table))
1215 table = (PerlIO **)(f++);
1216 for (i=1; i < PERLIO_TABLE_SIZE; i++)
1218 if (*f && PerlIO_flush(f) != 0)
1228 PerlIOBase_flush_linebuf()
1230 PerlIO **table = &_perlio;
1232 while ((f = *table))
1235 table = (PerlIO **)(f++);
1236 for (i=1; i < PERLIO_TABLE_SIZE; i++)
1238 if (*f && (PerlIOBase(f)->flags & (PERLIO_F_LINEBUF|PERLIO_F_CANWRITE))
1239 == (PERLIO_F_LINEBUF|PERLIO_F_CANWRITE))
1248 PerlIO_fill(PerlIO *f)
1251 return (*PerlIOBase(f)->tab->Fill)(f);
1254 SETERRNO(EBADF,SS$_IVCHAN);
1259 #undef PerlIO_isutf8
1261 PerlIO_isutf8(PerlIO *f)
1264 return (PerlIOBase(f)->flags & PERLIO_F_UTF8) != 0;
1267 SETERRNO(EBADF,SS$_IVCHAN);
1274 PerlIO_eof(PerlIO *f)
1277 return (*PerlIOBase(f)->tab->Eof)(f);
1280 SETERRNO(EBADF,SS$_IVCHAN);
1287 PerlIO_error(PerlIO *f)
1290 return (*PerlIOBase(f)->tab->Error)(f);
1293 SETERRNO(EBADF,SS$_IVCHAN);
1298 #undef PerlIO_clearerr
1300 PerlIO_clearerr(PerlIO *f)
1303 (*PerlIOBase(f)->tab->Clearerr)(f);
1305 SETERRNO(EBADF,SS$_IVCHAN);
1308 #undef PerlIO_setlinebuf
1310 PerlIO_setlinebuf(PerlIO *f)
1313 (*PerlIOBase(f)->tab->Setlinebuf)(f);
1315 SETERRNO(EBADF,SS$_IVCHAN);
1318 #undef PerlIO_has_base
1320 PerlIO_has_base(PerlIO *f)
1322 if (f && *f) { return (PerlIOBase(f)->tab->Get_base != NULL); }
1326 #undef PerlIO_fast_gets
1328 PerlIO_fast_gets(PerlIO *f)
1330 if (f && *f && (PerlIOBase(f)->flags & PERLIO_F_FASTGETS))
1332 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1333 return (tab->Set_ptrcnt != NULL);
1338 #undef PerlIO_has_cntptr
1340 PerlIO_has_cntptr(PerlIO *f)
1344 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1345 return (tab->Get_ptr != NULL && tab->Get_cnt != NULL);
1350 #undef PerlIO_canset_cnt
1352 PerlIO_canset_cnt(PerlIO *f)
1356 PerlIOl *l = PerlIOBase(f);
1357 return (l->tab->Set_ptrcnt != NULL);
1362 #undef PerlIO_get_base
1364 PerlIO_get_base(PerlIO *f)
1367 return (*PerlIOBase(f)->tab->Get_base)(f);
1371 #undef PerlIO_get_bufsiz
1373 PerlIO_get_bufsiz(PerlIO *f)
1376 return (*PerlIOBase(f)->tab->Get_bufsiz)(f);
1380 #undef PerlIO_get_ptr
1382 PerlIO_get_ptr(PerlIO *f)
1384 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1385 if (tab->Get_ptr == NULL)
1387 return (*tab->Get_ptr)(f);
1390 #undef PerlIO_get_cnt
1392 PerlIO_get_cnt(PerlIO *f)
1394 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1395 if (tab->Get_cnt == NULL)
1397 return (*tab->Get_cnt)(f);
1400 #undef PerlIO_set_cnt
1402 PerlIO_set_cnt(PerlIO *f,int cnt)
1404 (*PerlIOBase(f)->tab->Set_ptrcnt)(f,NULL,cnt);
1407 #undef PerlIO_set_ptrcnt
1409 PerlIO_set_ptrcnt(PerlIO *f, STDCHAR *ptr, int cnt)
1411 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1412 if (tab->Set_ptrcnt == NULL)
1415 Perl_croak(aTHX_ "PerlIO buffer snooping abuse");
1417 (*PerlIOBase(f)->tab->Set_ptrcnt)(f,ptr,cnt);
1420 /*--------------------------------------------------------------------------------------*/
1421 /* utf8 and raw dummy layers */
1424 PerlIOUtf8_pushed(PerlIO *f, const char *mode, SV *arg)
1429 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1430 PerlIO_pop(aTHX_ f);
1431 if (tab->kind & PERLIO_K_UTF8)
1432 PerlIOBase(f)->flags |= PERLIO_F_UTF8;
1434 PerlIOBase(f)->flags &= ~PERLIO_F_UTF8;
1440 PerlIO_funcs PerlIO_utf8 = {
1443 PERLIO_K_DUMMY|PERLIO_F_UTF8,
1461 NULL, /* get_base */
1462 NULL, /* get_bufsiz */
1465 NULL, /* set_ptrcnt */
1468 PerlIO_funcs PerlIO_byte = {
1489 NULL, /* get_base */
1490 NULL, /* get_bufsiz */
1493 NULL, /* set_ptrcnt */
1497 PerlIORaw_open(pTHX_ PerlIO_funcs *self, AV *layers, IV n,const char *mode, int fd, int imode, int perm, PerlIO *old, int narg, SV **args)
1499 PerlIO_funcs *tab = PerlIO_default_btm();
1500 return (*tab->Open)(aTHX_ tab,layers,n-2,mode,fd,imode,perm,old,narg,args);
1503 PerlIO_funcs PerlIO_raw = {
1524 NULL, /* get_base */
1525 NULL, /* get_bufsiz */
1528 NULL, /* set_ptrcnt */
1530 /*--------------------------------------------------------------------------------------*/
1531 /*--------------------------------------------------------------------------------------*/
1532 /* "Methods" of the "base class" */
1535 PerlIOBase_fileno(PerlIO *f)
1537 return PerlIO_fileno(PerlIONext(f));
1541 PerlIO_modestr(PerlIO *f,char *buf)
1544 IV flags = PerlIOBase(f)->flags;
1545 if (flags & PERLIO_F_APPEND)
1548 if (flags & PERLIO_F_CANREAD)
1553 else if (flags & PERLIO_F_CANREAD)
1556 if (flags & PERLIO_F_CANWRITE)
1559 else if (flags & PERLIO_F_CANWRITE)
1562 if (flags & PERLIO_F_CANREAD)
1567 #if O_TEXT != O_BINARY
1568 if (!(flags & PERLIO_F_CRLF))
1576 PerlIOBase_pushed(PerlIO *f, const char *mode, SV *arg)
1578 PerlIOl *l = PerlIOBase(f);
1580 const char *omode = mode;
1583 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1584 l->flags &= ~(PERLIO_F_CANREAD|PERLIO_F_CANWRITE|
1585 PERLIO_F_TRUNCATE|PERLIO_F_APPEND);
1586 if (tab->Set_ptrcnt != NULL)
1587 l->flags |= PERLIO_F_FASTGETS;
1590 if (*mode == '#' || *mode == 'I')
1595 l->flags |= PERLIO_F_CANREAD;
1598 l->flags |= PERLIO_F_APPEND|PERLIO_F_CANWRITE;
1601 l->flags |= PERLIO_F_TRUNCATE|PERLIO_F_CANWRITE;
1604 SETERRNO(EINVAL,LIB$_INVARG);
1612 l->flags |= PERLIO_F_CANREAD|PERLIO_F_CANWRITE;
1615 l->flags &= ~PERLIO_F_CRLF;
1618 l->flags |= PERLIO_F_CRLF;
1621 SETERRNO(EINVAL,LIB$_INVARG);
1630 l->flags |= l->next->flags &
1631 (PERLIO_F_CANREAD|PERLIO_F_CANWRITE|PERLIO_F_TRUNCATE|PERLIO_F_APPEND);
1635 PerlIO_debug("PerlIOBase_pushed f=%p %s %s fl=%08"UVxf" (%s)\n",
1636 f,PerlIOBase(f)->tab->name,(omode) ? omode : "(Null)",
1637 l->flags,PerlIO_modestr(f,temp));
1643 PerlIOBase_popped(PerlIO *f)
1649 PerlIOBase_unread(PerlIO *f, const void *vbuf, Size_t count)
1652 Off_t old = PerlIO_tell(f);
1654 PerlIO_push(aTHX_ f,&PerlIO_pending,"r",Nullsv);
1655 done = PerlIOBuf_unread(f,vbuf,count);
1656 PerlIOSelf(f,PerlIOBuf)->posn = old - done;
1661 PerlIOBase_read(PerlIO *f, void *vbuf, Size_t count)
1663 STDCHAR *buf = (STDCHAR *) vbuf;
1666 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD))
1670 SSize_t avail = PerlIO_get_cnt(f);
1673 take = (count < avail) ? count : avail;
1676 STDCHAR *ptr = PerlIO_get_ptr(f);
1677 Copy(ptr,buf,take,STDCHAR);
1678 PerlIO_set_ptrcnt(f,ptr+take,(avail -= take));
1682 if (count > 0 && avail <= 0)
1684 if (PerlIO_fill(f) != 0)
1688 return (buf - (STDCHAR *) vbuf);
1694 PerlIOBase_noop_ok(PerlIO *f)
1700 PerlIOBase_noop_fail(PerlIO *f)
1706 PerlIOBase_close(PerlIO *f)
1709 PerlIO *n = PerlIONext(f);
1710 if (PerlIO_flush(f) != 0)
1712 if (n && *n && (*PerlIOBase(n)->tab->Close)(n) != 0)
1714 PerlIOBase(f)->flags &= ~(PERLIO_F_CANREAD|PERLIO_F_CANWRITE|PERLIO_F_OPEN);
1719 PerlIOBase_eof(PerlIO *f)
1723 return (PerlIOBase(f)->flags & PERLIO_F_EOF) != 0;
1729 PerlIOBase_error(PerlIO *f)
1733 return (PerlIOBase(f)->flags & PERLIO_F_ERROR) != 0;
1739 PerlIOBase_clearerr(PerlIO *f)
1743 PerlIO *n = PerlIONext(f);
1744 PerlIOBase(f)->flags &= ~(PERLIO_F_ERROR|PERLIO_F_EOF);
1751 PerlIOBase_setlinebuf(PerlIO *f)
1755 PerlIOBase(f)->flags |= PERLIO_F_LINEBUF;
1759 /*--------------------------------------------------------------------------------------*/
1760 /* Bottom-most level for UNIX-like case */
1764 struct _PerlIO base; /* The generic part */
1765 int fd; /* UNIX like file descriptor */
1766 int oflags; /* open/fcntl flags */
1770 PerlIOUnix_oflags(const char *mode)
1785 oflags = O_CREAT|O_TRUNC;
1796 oflags = O_CREAT|O_APPEND;
1812 else if (*mode == 't')
1815 oflags &= ~O_BINARY;
1818 /* Always open in binary mode */
1820 if (*mode || oflags == -1)
1822 SETERRNO(EINVAL,LIB$_INVARG);
1829 PerlIOUnix_fileno(PerlIO *f)
1831 return PerlIOSelf(f,PerlIOUnix)->fd;
1835 PerlIOUnix_pushed(PerlIO *f, const char *mode, SV *arg)
1837 IV code = PerlIOBase_pushed(f,mode,arg);
1840 PerlIOUnix *s = PerlIOSelf(f,PerlIOUnix);
1841 s->fd = PerlIO_fileno(PerlIONext(f));
1842 s->oflags = PerlIOUnix_oflags(mode);
1844 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
1849 PerlIOUnix_open(pTHX_ PerlIO_funcs *self, AV *layers, IV n, const char *mode, int fd, int imode, int perm, PerlIO *f, int narg, SV **args)
1853 if (PerlIOBase(f)->flags & PERLIO_F_OPEN)
1854 (*PerlIOBase(f)->tab->Close)(f);
1858 char *path = SvPV_nolen(*args);
1863 imode = PerlIOUnix_oflags(mode);
1868 fd = PerlLIO_open3(path,imode,perm);
1878 f = PerlIO_allocate(aTHX);
1879 s = PerlIOSelf(PerlIO_push(aTHX_ f,self,mode,PerlIOArg),PerlIOUnix);
1882 s = PerlIOSelf(f,PerlIOUnix);
1885 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
1892 /* FIXME: pop layers ??? */
1899 PerlIOUnix_read(PerlIO *f, void *vbuf, Size_t count)
1902 int fd = PerlIOSelf(f,PerlIOUnix)->fd;
1903 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD))
1907 SSize_t len = PerlLIO_read(fd,vbuf,count);
1908 if (len >= 0 || errno != EINTR)
1911 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
1912 else if (len == 0 && count != 0)
1913 PerlIOBase(f)->flags |= PERLIO_F_EOF;
1921 PerlIOUnix_write(PerlIO *f, const void *vbuf, Size_t count)
1924 int fd = PerlIOSelf(f,PerlIOUnix)->fd;
1927 SSize_t len = PerlLIO_write(fd,vbuf,count);
1928 if (len >= 0 || errno != EINTR)
1931 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
1939 PerlIOUnix_seek(PerlIO *f, Off_t offset, int whence)
1942 Off_t new = PerlLIO_lseek(PerlIOSelf(f,PerlIOUnix)->fd,offset,whence);
1943 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
1944 return (new == (Off_t) -1) ? -1 : 0;
1948 PerlIOUnix_tell(PerlIO *f)
1951 return PerlLIO_lseek(PerlIOSelf(f,PerlIOUnix)->fd,0,SEEK_CUR);
1955 PerlIOUnix_close(PerlIO *f)
1958 int fd = PerlIOSelf(f,PerlIOUnix)->fd;
1960 while (PerlLIO_close(fd) != 0)
1971 PerlIOBase(f)->flags &= ~PERLIO_F_OPEN;
1976 PerlIO_funcs PerlIO_unix = {
1991 PerlIOBase_noop_ok, /* flush */
1992 PerlIOBase_noop_fail, /* fill */
1995 PerlIOBase_clearerr,
1996 PerlIOBase_setlinebuf,
1997 NULL, /* get_base */
1998 NULL, /* get_bufsiz */
2001 NULL, /* set_ptrcnt */
2004 /*--------------------------------------------------------------------------------------*/
2005 /* stdio as a layer */
2009 struct _PerlIO base;
2010 FILE * stdio; /* The stream */
2014 PerlIOStdio_fileno(PerlIO *f)
2017 return PerlSIO_fileno(PerlIOSelf(f,PerlIOStdio)->stdio);
2021 PerlIOStdio_mode(const char *mode,char *tmode)
2028 if (O_BINARY != O_TEXT)
2036 /* This isn't used yet ... */
2038 PerlIOStdio_pushed(PerlIO *f, const char *mode, SV *arg)
2043 PerlIOStdio *s = PerlIOSelf(f,PerlIOStdio);
2045 FILE *stdio = PerlSIO_fdopen(PerlIO_fileno(PerlIONext(f)),mode = PerlIOStdio_mode(mode,tmode));
2051 return PerlIOBase_pushed(f,mode,arg);
2054 #undef PerlIO_importFILE
2056 PerlIO_importFILE(FILE *stdio, int fl)
2062 PerlIOStdio *s = PerlIOSelf(PerlIO_push(aTHX_ (f = PerlIO_allocate(aTHX)),&PerlIO_stdio,"r+",Nullsv),PerlIOStdio);
2069 PerlIOStdio_open(pTHX_ PerlIO_funcs *self, AV *layers, IV n, const char *mode, int fd, int imode, int perm, PerlIO *f, int narg, SV **args)
2074 char *path = SvPV_nolen(*args);
2075 PerlIOStdio *s = PerlIOSelf(f,PerlIOStdio);
2076 FILE *stdio = PerlSIO_freopen(path,(mode = PerlIOStdio_mode(mode,tmode)),s->stdio);
2086 char *path = SvPV_nolen(*args);
2090 fd = PerlLIO_open3(path,imode,perm);
2094 FILE *stdio = PerlSIO_fopen(path,mode);
2097 PerlIOStdio *s = PerlIOSelf(PerlIO_push(aTHX_ (f = PerlIO_allocate(aTHX)), self,
2098 (mode = PerlIOStdio_mode(mode,tmode)),PerlIOArg),
2119 stdio = PerlSIO_stdin;
2122 stdio = PerlSIO_stdout;
2125 stdio = PerlSIO_stderr;
2131 stdio = PerlSIO_fdopen(fd,mode = PerlIOStdio_mode(mode,tmode));
2135 PerlIOStdio *s = PerlIOSelf(PerlIO_push(aTHX_ (f = PerlIO_allocate(aTHX)),self,mode,PerlIOArg),PerlIOStdio);
2145 PerlIOStdio_read(PerlIO *f, void *vbuf, Size_t count)
2148 FILE *s = PerlIOSelf(f,PerlIOStdio)->stdio;
2152 STDCHAR *buf = (STDCHAR *) vbuf;
2153 /* Perl is expecting PerlIO_getc() to fill the buffer
2154 * Linux's stdio does not do that for fread()
2156 int ch = PerlSIO_fgetc(s);
2164 got = PerlSIO_fread(vbuf,1,count,s);
2169 PerlIOStdio_unread(PerlIO *f, const void *vbuf, Size_t count)
2172 FILE *s = PerlIOSelf(f,PerlIOStdio)->stdio;
2173 STDCHAR *buf = ((STDCHAR *)vbuf)+count-1;
2177 int ch = *buf-- & 0xff;
2178 if (PerlSIO_ungetc(ch,s) != ch)
2187 PerlIOStdio_write(PerlIO *f, const void *vbuf, Size_t count)
2190 return PerlSIO_fwrite(vbuf,1,count,PerlIOSelf(f,PerlIOStdio)->stdio);
2194 PerlIOStdio_seek(PerlIO *f, Off_t offset, int whence)
2197 FILE *stdio = PerlIOSelf(f,PerlIOStdio)->stdio;
2198 return PerlSIO_fseek(stdio,offset,whence);
2202 PerlIOStdio_tell(PerlIO *f)
2205 FILE *stdio = PerlIOSelf(f,PerlIOStdio)->stdio;
2206 return PerlSIO_ftell(stdio);
2210 PerlIOStdio_close(PerlIO *f)
2213 #ifdef SOCKS5_VERSION_NAME
2215 Sock_size_t optlen = sizeof(int);
2217 FILE *stdio = PerlIOSelf(f,PerlIOStdio)->stdio;
2219 #ifdef SOCKS5_VERSION_NAME
2220 (getsockopt(PerlIO_fileno(f), SOL_SOCKET, SO_TYPE, (void *)&optval, &optlen) < 0) ?
2221 PerlSIO_fclose(stdio) :
2222 close(PerlIO_fileno(f))
2224 PerlSIO_fclose(stdio)
2231 PerlIOStdio_flush(PerlIO *f)
2234 FILE *stdio = PerlIOSelf(f,PerlIOStdio)->stdio;
2235 if (PerlIOBase(f)->flags & PERLIO_F_CANWRITE)
2237 return PerlSIO_fflush(stdio);
2242 /* FIXME: This discards ungetc() and pre-read stuff which is
2243 not right if this is just a "sync" from a layer above
2244 Suspect right design is to do _this_ but not have layer above
2245 flush this layer read-to-read
2247 /* Not writeable - sync by attempting a seek */
2249 if (PerlSIO_fseek(stdio,(Off_t) 0, SEEK_CUR) != 0)
2257 PerlIOStdio_fill(PerlIO *f)
2260 FILE *stdio = PerlIOSelf(f,PerlIOStdio)->stdio;
2262 /* fflush()ing read-only streams can cause trouble on some stdio-s */
2263 if ((PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
2265 if (PerlSIO_fflush(stdio) != 0)
2268 c = PerlSIO_fgetc(stdio);
2269 if (c == EOF || PerlSIO_ungetc(c,stdio) != c)
2275 PerlIOStdio_eof(PerlIO *f)
2278 return PerlSIO_feof(PerlIOSelf(f,PerlIOStdio)->stdio);
2282 PerlIOStdio_error(PerlIO *f)
2285 return PerlSIO_ferror(PerlIOSelf(f,PerlIOStdio)->stdio);
2289 PerlIOStdio_clearerr(PerlIO *f)
2292 PerlSIO_clearerr(PerlIOSelf(f,PerlIOStdio)->stdio);
2296 PerlIOStdio_setlinebuf(PerlIO *f)
2299 #ifdef HAS_SETLINEBUF
2300 PerlSIO_setlinebuf(PerlIOSelf(f,PerlIOStdio)->stdio);
2302 PerlSIO_setvbuf(PerlIOSelf(f,PerlIOStdio)->stdio, Nullch, _IOLBF, 0);
2308 PerlIOStdio_get_base(PerlIO *f)
2311 FILE *stdio = PerlIOSelf(f,PerlIOStdio)->stdio;
2312 return PerlSIO_get_base(stdio);
2316 PerlIOStdio_get_bufsiz(PerlIO *f)
2319 FILE *stdio = PerlIOSelf(f,PerlIOStdio)->stdio;
2320 return PerlSIO_get_bufsiz(stdio);
2324 #ifdef USE_STDIO_PTR
2326 PerlIOStdio_get_ptr(PerlIO *f)
2329 FILE *stdio = PerlIOSelf(f,PerlIOStdio)->stdio;
2330 return PerlSIO_get_ptr(stdio);
2334 PerlIOStdio_get_cnt(PerlIO *f)
2337 FILE *stdio = PerlIOSelf(f,PerlIOStdio)->stdio;
2338 return PerlSIO_get_cnt(stdio);
2342 PerlIOStdio_set_ptrcnt(PerlIO *f,STDCHAR *ptr,SSize_t cnt)
2344 FILE *stdio = PerlIOSelf(f,PerlIOStdio)->stdio;
2348 #ifdef STDIO_PTR_LVALUE
2349 PerlSIO_set_ptr(stdio,ptr);
2350 #ifdef STDIO_PTR_LVAL_SETS_CNT
2351 if (PerlSIO_get_cnt(stdio) != (cnt))
2354 assert(PerlSIO_get_cnt(stdio) == (cnt));
2357 #if (!defined(STDIO_PTR_LVAL_NOCHANGE_CNT))
2358 /* Setting ptr _does_ change cnt - we are done */
2361 #else /* STDIO_PTR_LVALUE */
2363 #endif /* STDIO_PTR_LVALUE */
2365 /* Now (or only) set cnt */
2366 #ifdef STDIO_CNT_LVALUE
2367 PerlSIO_set_cnt(stdio,cnt);
2368 #else /* STDIO_CNT_LVALUE */
2369 #if (defined(STDIO_PTR_LVALUE) && defined(STDIO_PTR_LVAL_SETS_CNT))
2370 PerlSIO_set_ptr(stdio,PerlSIO_get_ptr(stdio)+(PerlSIO_get_cnt(stdio)-cnt));
2371 #else /* STDIO_PTR_LVAL_SETS_CNT */
2373 #endif /* STDIO_PTR_LVAL_SETS_CNT */
2374 #endif /* STDIO_CNT_LVALUE */
2379 PerlIO_funcs PerlIO_stdio = {
2381 sizeof(PerlIOStdio),
2398 PerlIOStdio_clearerr,
2399 PerlIOStdio_setlinebuf,
2401 PerlIOStdio_get_base,
2402 PerlIOStdio_get_bufsiz,
2407 #ifdef USE_STDIO_PTR
2408 PerlIOStdio_get_ptr,
2409 PerlIOStdio_get_cnt,
2410 #if (defined(STDIO_PTR_LVALUE) && (defined(STDIO_CNT_LVALUE) || defined(STDIO_PTR_LVAL_SETS_CNT)))
2411 PerlIOStdio_set_ptrcnt
2412 #else /* STDIO_PTR_LVALUE */
2414 #endif /* STDIO_PTR_LVALUE */
2415 #else /* USE_STDIO_PTR */
2419 #endif /* USE_STDIO_PTR */
2422 #undef PerlIO_exportFILE
2424 PerlIO_exportFILE(PerlIO *f, int fl)
2428 stdio = fdopen(PerlIO_fileno(f),"r+");
2432 PerlIOStdio *s = PerlIOSelf(PerlIO_push(aTHX_ f,&PerlIO_stdio,"r+",Nullsv),PerlIOStdio);
2438 #undef PerlIO_findFILE
2440 PerlIO_findFILE(PerlIO *f)
2445 if (l->tab == &PerlIO_stdio)
2447 PerlIOStdio *s = PerlIOSelf(&l,PerlIOStdio);
2450 l = *PerlIONext(&l);
2452 return PerlIO_exportFILE(f,0);
2455 #undef PerlIO_releaseFILE
2457 PerlIO_releaseFILE(PerlIO *p, FILE *f)
2461 /*--------------------------------------------------------------------------------------*/
2462 /* perlio buffer layer */
2465 PerlIOBuf_pushed(PerlIO *f, const char *mode, SV *arg)
2468 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2469 int fd = PerlIO_fileno(f);
2471 if (fd >= 0 && PerlLIO_isatty(fd))
2473 PerlIOBase(f)->flags |= PERLIO_F_LINEBUF|PERLIO_F_TTY;
2475 posn = PerlIO_tell(PerlIONext(f));
2476 if (posn != (Off_t) -1)
2480 return PerlIOBase_pushed(f,mode,arg);
2484 PerlIOBuf_open(pTHX_ PerlIO_funcs *self, AV *layers, IV n, const char *mode, int fd, int imode, int perm, PerlIO *f, int narg, SV **args)
2488 PerlIO *next = PerlIONext(f);
2489 PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n-2, PerlIOBase(next)->tab);
2490 next = (*tab->Open)(aTHX_ tab, layers, n-2, mode,fd,imode,perm,next,narg,args);
2491 if (!next || (*PerlIOBase(f)->tab->Pushed)(f,mode,PerlIOArg) != 0)
2498 PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n-2, PerlIO_default_btm());
2505 f = (*tab->Open)(aTHX_ tab, layers, n-2, mode,fd,imode,perm,NULL,narg,args);
2508 PerlIO_push(aTHX_ f,self,mode,PerlIOArg);
2509 fd = PerlIO_fileno(f);
2510 #if O_BINARY != O_TEXT
2511 /* do something about failing setmode()? --jhi */
2512 PerlLIO_setmode(fd , O_BINARY);
2514 if (init && fd == 2)
2516 /* Initial stderr is unbuffered */
2517 PerlIOBase(f)->flags |= PERLIO_F_UNBUF;
2524 /* This "flush" is akin to sfio's sync in that it handles files in either
2528 PerlIOBuf_flush(PerlIO *f)
2530 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2532 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
2534 /* write() the buffer */
2535 STDCHAR *buf = b->buf;
2537 PerlIO *n = PerlIONext(f);
2540 SSize_t count = PerlIO_write(n,p,b->ptr - p);
2545 else if (count < 0 || PerlIO_error(n))
2547 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2552 b->posn += (p - buf);
2554 else if (PerlIOBase(f)->flags & PERLIO_F_RDBUF)
2556 STDCHAR *buf = PerlIO_get_base(f);
2557 /* Note position change */
2558 b->posn += (b->ptr - buf);
2559 if (b->ptr < b->end)
2561 /* We did not consume all of it */
2562 if (PerlIO_seek(PerlIONext(f),b->posn,SEEK_SET) == 0)
2564 b->posn = PerlIO_tell(PerlIONext(f));
2568 b->ptr = b->end = b->buf;
2569 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF|PERLIO_F_WRBUF);
2570 /* FIXME: Is this right for read case ? */
2571 if (PerlIO_flush(PerlIONext(f)) != 0)
2577 PerlIOBuf_fill(PerlIO *f)
2579 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2580 PerlIO *n = PerlIONext(f);
2582 /* FIXME: doing the down-stream flush is a bad idea if it causes
2583 pre-read data in stdio buffer to be discarded
2584 but this is too simplistic - as it skips _our_ hosekeeping
2585 and breaks tell tests.
2586 if (!(PerlIOBase(f)->flags & PERLIO_F_RDBUF))
2590 if (PerlIO_flush(f) != 0)
2592 if (PerlIOBase(f)->flags & PERLIO_F_TTY)
2593 PerlIOBase_flush_linebuf();
2596 PerlIO_get_base(f); /* allocate via vtable */
2598 b->ptr = b->end = b->buf;
2599 if (PerlIO_fast_gets(n))
2601 /* Layer below is also buffered
2602 * We do _NOT_ want to call its ->Read() because that will loop
2603 * till it gets what we asked for which may hang on a pipe etc.
2604 * Instead take anything it has to hand, or ask it to fill _once_.
2606 avail = PerlIO_get_cnt(n);
2609 avail = PerlIO_fill(n);
2611 avail = PerlIO_get_cnt(n);
2614 if (!PerlIO_error(n) && PerlIO_eof(n))
2620 STDCHAR *ptr = PerlIO_get_ptr(n);
2621 SSize_t cnt = avail;
2622 if (avail > b->bufsiz)
2624 Copy(ptr,b->buf,avail,STDCHAR);
2625 PerlIO_set_ptrcnt(n,ptr+avail,cnt-avail);
2630 avail = PerlIO_read(n,b->ptr,b->bufsiz);
2635 PerlIOBase(f)->flags |= PERLIO_F_EOF;
2637 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2640 b->end = b->buf+avail;
2641 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
2646 PerlIOBuf_read(PerlIO *f, void *vbuf, Size_t count)
2648 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2653 return PerlIOBase_read(f,vbuf,count);
2659 PerlIOBuf_unread(PerlIO *f, const void *vbuf, Size_t count)
2661 const STDCHAR *buf = (const STDCHAR *) vbuf+count;
2662 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2665 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
2671 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF)
2673 avail = (b->ptr - b->buf);
2678 b->end = b->buf + avail;
2680 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
2681 b->posn -= b->bufsiz;
2683 if (avail > (SSize_t) count)
2691 Copy(buf,b->ptr,avail,STDCHAR);
2695 PerlIOBase(f)->flags &= ~ PERLIO_F_EOF;
2702 PerlIOBuf_write(PerlIO *f, const void *vbuf, Size_t count)
2704 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2705 const STDCHAR *buf = (const STDCHAR *) vbuf;
2709 if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
2713 SSize_t avail = b->bufsiz - (b->ptr - b->buf);
2714 if ((SSize_t) count < avail)
2716 PerlIOBase(f)->flags |= PERLIO_F_WRBUF;
2717 if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF)
2737 Copy(buf,b->ptr,avail,STDCHAR);
2744 if (b->ptr >= (b->buf + b->bufsiz))
2747 if (PerlIOBase(f)->flags & PERLIO_F_UNBUF)
2753 PerlIOBuf_seek(PerlIO *f, Off_t offset, int whence)
2756 if ((code = PerlIO_flush(f)) == 0)
2758 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2759 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
2760 code = PerlIO_seek(PerlIONext(f),offset,whence);
2763 b->posn = PerlIO_tell(PerlIONext(f));
2770 PerlIOBuf_tell(PerlIO *f)
2772 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2773 Off_t posn = b->posn;
2775 posn += (b->ptr - b->buf);
2780 PerlIOBuf_close(PerlIO *f)
2782 IV code = PerlIOBase_close(f);
2783 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2784 if (b->buf && b->buf != (STDCHAR *) &b->oneword)
2786 PerlMemShared_free(b->buf);
2789 b->ptr = b->end = b->buf;
2790 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF|PERLIO_F_WRBUF);
2795 PerlIOBuf_get_ptr(PerlIO *f)
2797 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2804 PerlIOBuf_get_cnt(PerlIO *f)
2806 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2809 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF)
2810 return (b->end - b->ptr);
2815 PerlIOBuf_get_base(PerlIO *f)
2817 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2822 b->buf = PerlMemShared_calloc(b->bufsiz,sizeof(STDCHAR));
2825 b->buf = (STDCHAR *)&b->oneword;
2826 b->bufsiz = sizeof(b->oneword);
2835 PerlIOBuf_bufsiz(PerlIO *f)
2837 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2840 return (b->end - b->buf);
2844 PerlIOBuf_set_ptrcnt(PerlIO *f, STDCHAR *ptr, SSize_t cnt)
2846 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2850 if (PerlIO_get_cnt(f) != cnt || b->ptr < b->buf)
2853 assert(PerlIO_get_cnt(f) == cnt);
2854 assert(b->ptr >= b->buf);
2856 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
2859 PerlIO_funcs PerlIO_perlio = {
2878 PerlIOBase_clearerr,
2879 PerlIOBase_setlinebuf,
2884 PerlIOBuf_set_ptrcnt,
2887 /*--------------------------------------------------------------------------------------*/
2888 /* Temp layer to hold unread chars when cannot do it any other way */
2891 PerlIOPending_fill(PerlIO *f)
2893 /* Should never happen */
2899 PerlIOPending_close(PerlIO *f)
2901 /* A tad tricky - flush pops us, then we close new top */
2903 return PerlIO_close(f);
2907 PerlIOPending_seek(PerlIO *f, Off_t offset, int whence)
2909 /* A tad tricky - flush pops us, then we seek new top */
2911 return PerlIO_seek(f,offset,whence);
2916 PerlIOPending_flush(PerlIO *f)
2919 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2920 if (b->buf && b->buf != (STDCHAR *) &b->oneword)
2922 PerlMemShared_free(b->buf);
2925 PerlIO_pop(aTHX_ f);
2930 PerlIOPending_set_ptrcnt(PerlIO *f, STDCHAR *ptr, SSize_t cnt)
2938 PerlIOBuf_set_ptrcnt(f,ptr,cnt);
2943 PerlIOPending_pushed(PerlIO *f,const char *mode,SV *arg)
2945 IV code = PerlIOBase_pushed(f,mode,arg);
2946 PerlIOl *l = PerlIOBase(f);
2947 /* Our PerlIO_fast_gets must match what we are pushed on,
2948 or sv_gets() etc. get muddled when it changes mid-string
2951 l->flags = (l->flags & ~(PERLIO_F_FASTGETS|PERLIO_F_UTF8)) |
2952 (PerlIOBase(PerlIONext(f))->flags & (PERLIO_F_FASTGETS|PERLIO_F_UTF8));
2957 PerlIOPending_read(PerlIO *f, void *vbuf, Size_t count)
2959 SSize_t avail = PerlIO_get_cnt(f);
2964 got = PerlIOBuf_read(f,vbuf,avail);
2965 if (got >= 0 && got < count)
2967 SSize_t more = PerlIO_read(f,((STDCHAR *) vbuf)+got,count-got);
2968 if (more >= 0 || got == 0)
2974 PerlIO_funcs PerlIO_pending = {
2978 PerlIOPending_pushed,
2988 PerlIOPending_close,
2989 PerlIOPending_flush,
2993 PerlIOBase_clearerr,
2994 PerlIOBase_setlinebuf,
2999 PerlIOPending_set_ptrcnt,
3004 /*--------------------------------------------------------------------------------------*/
3005 /* crlf - translation
3006 On read translate CR,LF to "\n" we do this by overriding ptr/cnt entries
3007 to hand back a line at a time and keeping a record of which nl we "lied" about.
3008 On write translate "\n" to CR,LF
3013 PerlIOBuf base; /* PerlIOBuf stuff */
3014 STDCHAR *nl; /* Position of crlf we "lied" about in the buffer */
3018 PerlIOCrlf_pushed(PerlIO *f, const char *mode,SV *arg)
3021 PerlIOBase(f)->flags |= PERLIO_F_CRLF;
3022 code = PerlIOBuf_pushed(f,mode,arg);
3024 PerlIO_debug("PerlIOCrlf_pushed f=%p %s %s fl=%08"UVxf"\n",
3025 f,PerlIOBase(f)->tab->name,(mode) ? mode : "(Null)",
3026 PerlIOBase(f)->flags);
3033 PerlIOCrlf_unread(PerlIO *f, const void *vbuf, Size_t count)
3035 PerlIOCrlf *c = PerlIOSelf(f,PerlIOCrlf);
3041 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF))
3042 return PerlIOBuf_unread(f,vbuf,count);
3045 const STDCHAR *buf = (const STDCHAR *) vbuf+count;
3046 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
3048 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
3054 if (!(PerlIOBase(f)->flags & PERLIO_F_RDBUF))
3056 b->end = b->ptr = b->buf + b->bufsiz;
3057 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3058 b->posn -= b->bufsiz;
3060 while (count > 0 && b->ptr > b->buf)
3065 if (b->ptr - 2 >= b->buf)
3091 PerlIOCrlf_get_cnt(PerlIO *f)
3093 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
3096 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF)
3098 PerlIOCrlf *c = PerlIOSelf(f,PerlIOCrlf);
3099 if ((PerlIOBase(f)->flags & PERLIO_F_CRLF) && !c->nl)
3101 STDCHAR *nl = b->ptr;
3103 while (nl < b->end && *nl != 0xd)
3105 if (nl < b->end && *nl == 0xd)
3117 /* Not CR,LF but just CR */
3124 /* Blast - found CR as last char in buffer */
3127 /* They may not care, defer work as long as possible */
3128 return (nl - b->ptr);
3133 b->ptr++; /* say we have read it as far as flush() is concerned */
3134 b->buf++; /* Leave space an front of buffer */
3135 b->bufsiz--; /* Buffer is thus smaller */
3136 code = PerlIO_fill(f); /* Fetch some more */
3137 b->bufsiz++; /* Restore size for next time */
3138 b->buf--; /* Point at space */
3139 b->ptr = nl = b->buf; /* Which is what we hand off */
3140 b->posn--; /* Buffer starts here */
3141 *nl = 0xd; /* Fill in the CR */
3143 goto test; /* fill() call worked */
3144 /* CR at EOF - just fall through */
3149 return (((c->nl) ? (c->nl+1) : b->end) - b->ptr);
3155 PerlIOCrlf_set_ptrcnt(PerlIO *f, STDCHAR *ptr, SSize_t cnt)
3157 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
3158 PerlIOCrlf *c = PerlIOSelf(f,PerlIOCrlf);
3159 IV flags = PerlIOBase(f)->flags;
3169 if ((flags & PERLIO_F_CRLF) && ptr > b->buf && ptr[-1] == 0xd)
3176 /* Test code - delete when it works ... */
3183 if ((flags & PERLIO_F_CRLF) && chk > b->buf && chk[-1] == 0xd)
3191 Perl_croak(aTHX_ "ptr wrong %p != %p fl=%08"UVxf" nl=%p e=%p for %d",
3192 ptr, chk, flags, c->nl, b->end, cnt);
3199 /* They have taken what we lied about */
3206 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3210 PerlIOCrlf_write(PerlIO *f, const void *vbuf, Size_t count)
3212 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF))
3213 return PerlIOBuf_write(f,vbuf,count);
3216 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
3217 const STDCHAR *buf = (const STDCHAR *) vbuf;
3218 const STDCHAR *ebuf = buf+count;
3221 if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
3225 STDCHAR *eptr = b->buf+b->bufsiz;
3226 PerlIOBase(f)->flags |= PERLIO_F_WRBUF;
3227 while (buf < ebuf && b->ptr < eptr)
3231 if ((b->ptr + 2) > eptr)
3233 /* Not room for both */
3239 *(b->ptr)++ = 0xd; /* CR */
3240 *(b->ptr)++ = 0xa; /* LF */
3242 if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF)
3261 if (PerlIOBase(f)->flags & PERLIO_F_UNBUF)
3263 return (buf - (STDCHAR *) vbuf);
3268 PerlIOCrlf_flush(PerlIO *f)
3270 PerlIOCrlf *c = PerlIOSelf(f,PerlIOCrlf);
3276 return PerlIOBuf_flush(f);
3279 PerlIO_funcs PerlIO_crlf = {
3282 PERLIO_K_BUFFERED|PERLIO_K_CANCRLF,
3284 PerlIOBase_noop_ok, /* popped */
3288 PerlIOBuf_read, /* generic read works with ptr/cnt lies ... */
3289 PerlIOCrlf_unread, /* Put CR,LF in buffer for each '\n' */
3290 PerlIOCrlf_write, /* Put CR,LF in buffer for each '\n' */
3298 PerlIOBase_clearerr,
3299 PerlIOBase_setlinebuf,
3304 PerlIOCrlf_set_ptrcnt,
3308 /*--------------------------------------------------------------------------------------*/
3309 /* mmap as "buffer" layer */
3313 PerlIOBuf base; /* PerlIOBuf stuff */
3314 Mmap_t mptr; /* Mapped address */
3315 Size_t len; /* mapped length */
3316 STDCHAR *bbuf; /* malloced buffer if map fails */
3319 static size_t page_size = 0;
3322 PerlIOMmap_map(PerlIO *f)
3325 PerlIOMmap *m = PerlIOSelf(f,PerlIOMmap);
3326 IV flags = PerlIOBase(f)->flags;
3330 if (flags & PERLIO_F_CANREAD)
3332 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
3333 int fd = PerlIO_fileno(f);
3335 code = fstat(fd,&st);
3336 if (code == 0 && S_ISREG(st.st_mode))
3338 SSize_t len = st.st_size - b->posn;
3343 #if defined(HAS_SYSCONF) && (defined(_SC_PAGESIZE) || defined(_SC_PAGE_SIZE))
3345 SETERRNO(0,SS$_NORMAL);
3346 # ifdef _SC_PAGESIZE
3347 page_size = sysconf(_SC_PAGESIZE);
3349 page_size = sysconf(_SC_PAGE_SIZE);
3351 if ((long)page_size < 0) {
3356 (void)SvUPGRADE(error, SVt_PV);
3357 msg = SvPVx(error, n_a);
3358 Perl_croak(aTHX_ "panic: sysconf: %s", msg);
3361 Perl_croak(aTHX_ "panic: sysconf: pagesize unknown");
3365 # ifdef HAS_GETPAGESIZE
3366 page_size = getpagesize();
3368 # if defined(I_SYS_PARAM) && defined(PAGESIZE)
3369 page_size = PAGESIZE; /* compiletime, bad */
3373 if ((IV)page_size <= 0)
3374 Perl_croak(aTHX_ "panic: bad pagesize %"IVdf, (IV)page_size);
3378 /* This is a hack - should never happen - open should have set it ! */
3379 b->posn = PerlIO_tell(PerlIONext(f));
3381 posn = (b->posn / page_size) * page_size;
3382 len = st.st_size - posn;
3383 m->mptr = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, posn);
3384 if (m->mptr && m->mptr != (Mmap_t) -1)
3386 #if 0 && defined(HAS_MADVISE) && defined(MADV_SEQUENTIAL)
3387 madvise(m->mptr, len, MADV_SEQUENTIAL);
3389 #if 0 && defined(HAS_MADVISE) && defined(MADV_WILLNEED)
3390 madvise(m->mptr, len, MADV_WILLNEED);
3392 PerlIOBase(f)->flags = (flags & ~PERLIO_F_EOF) | PERLIO_F_RDBUF;
3393 b->end = ((STDCHAR *)m->mptr) + len;
3394 b->buf = ((STDCHAR *)m->mptr) + (b->posn - posn);
3405 PerlIOBase(f)->flags = flags | PERLIO_F_EOF | PERLIO_F_RDBUF;
3407 b->ptr = b->end = b->ptr;
3416 PerlIOMmap_unmap(PerlIO *f)
3418 PerlIOMmap *m = PerlIOSelf(f,PerlIOMmap);
3419 PerlIOBuf *b = &m->base;
3425 code = munmap(m->mptr, m->len);
3429 if (PerlIO_seek(PerlIONext(f),b->posn,SEEK_SET) != 0)
3432 b->ptr = b->end = b->buf;
3433 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF|PERLIO_F_WRBUF);
3439 PerlIOMmap_get_base(PerlIO *f)
3441 PerlIOMmap *m = PerlIOSelf(f,PerlIOMmap);
3442 PerlIOBuf *b = &m->base;
3443 if (b->buf && (PerlIOBase(f)->flags & PERLIO_F_RDBUF))
3445 /* Already have a readbuffer in progress */
3450 /* We have a write buffer or flushed PerlIOBuf read buffer */
3451 m->bbuf = b->buf; /* save it in case we need it again */
3452 b->buf = NULL; /* Clear to trigger below */
3456 PerlIOMmap_map(f); /* Try and map it */
3459 /* Map did not work - recover PerlIOBuf buffer if we have one */
3463 b->ptr = b->end = b->buf;
3466 return PerlIOBuf_get_base(f);
3470 PerlIOMmap_unread(PerlIO *f, const void *vbuf, Size_t count)
3472 PerlIOMmap *m = PerlIOSelf(f,PerlIOMmap);
3473 PerlIOBuf *b = &m->base;
3474 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
3476 if (b->ptr && (b->ptr - count) >= b->buf && memEQ(b->ptr - count,vbuf,count))
3479 PerlIOBase(f)->flags &= ~ PERLIO_F_EOF;
3484 /* Loose the unwritable mapped buffer */
3486 /* If flush took the "buffer" see if we have one from before */
3487 if (!b->buf && m->bbuf)
3491 PerlIOBuf_get_base(f);
3495 return PerlIOBuf_unread(f,vbuf,count);
3499 PerlIOMmap_write(PerlIO *f, const void *vbuf, Size_t count)
3501 PerlIOMmap *m = PerlIOSelf(f,PerlIOMmap);
3502 PerlIOBuf *b = &m->base;
3503 if (!b->buf || !(PerlIOBase(f)->flags & PERLIO_F_WRBUF))
3505 /* No, or wrong sort of, buffer */
3508 if (PerlIOMmap_unmap(f) != 0)
3511 /* If unmap took the "buffer" see if we have one from before */
3512 if (!b->buf && m->bbuf)
3516 PerlIOBuf_get_base(f);
3520 return PerlIOBuf_write(f,vbuf,count);
3524 PerlIOMmap_flush(PerlIO *f)
3526 PerlIOMmap *m = PerlIOSelf(f,PerlIOMmap);
3527 PerlIOBuf *b = &m->base;
3528 IV code = PerlIOBuf_flush(f);
3529 /* Now we are "synced" at PerlIOBuf level */
3534 /* Unmap the buffer */
3535 if (PerlIOMmap_unmap(f) != 0)
3540 /* We seem to have a PerlIOBuf buffer which was not mapped
3541 * remember it in case we need one later
3550 PerlIOMmap_fill(PerlIO *f)
3552 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
3553 IV code = PerlIO_flush(f);
3554 if (code == 0 && !b->buf)
3556 code = PerlIOMmap_map(f);
3558 if (code == 0 && !(PerlIOBase(f)->flags & PERLIO_F_RDBUF))
3560 code = PerlIOBuf_fill(f);
3566 PerlIOMmap_close(PerlIO *f)
3568 PerlIOMmap *m = PerlIOSelf(f,PerlIOMmap);
3569 PerlIOBuf *b = &m->base;
3570 IV code = PerlIO_flush(f);
3575 b->ptr = b->end = b->buf;
3577 if (PerlIOBuf_close(f) != 0)
3583 PerlIO_funcs PerlIO_mmap = {
3602 PerlIOBase_clearerr,
3603 PerlIOBase_setlinebuf,
3604 PerlIOMmap_get_base,
3608 PerlIOBuf_set_ptrcnt,
3611 #endif /* HAS_MMAP */
3618 call_atexit(PerlIO_cleanup_layers, NULL);
3623 atexit(&PerlIO_cleanup);
3635 PerlIO_stdstreams(aTHX);
3640 #undef PerlIO_stdout
3647 PerlIO_stdstreams(aTHX);
3652 #undef PerlIO_stderr
3659 PerlIO_stdstreams(aTHX);
3664 /*--------------------------------------------------------------------------------------*/
3666 #undef PerlIO_getname
3668 PerlIO_getname(PerlIO *f, char *buf)
3673 FILE *stdio = PerlIOSelf(f,PerlIOStdio)->stdio;
3674 if (stdio) name = fgetname(stdio, buf);
3676 Perl_croak(aTHX_ "Don't know how to get file name");
3682 /*--------------------------------------------------------------------------------------*/
3683 /* Functions which can be called on any kind of PerlIO implemented
3689 PerlIO_getc(PerlIO *f)
3692 SSize_t count = PerlIO_read(f,buf,1);
3695 return (unsigned char) buf[0];
3700 #undef PerlIO_ungetc
3702 PerlIO_ungetc(PerlIO *f, int ch)
3707 if (PerlIO_unread(f,&buf,1) == 1)
3715 PerlIO_putc(PerlIO *f, int ch)
3718 return PerlIO_write(f,&buf,1);
3723 PerlIO_puts(PerlIO *f, const char *s)
3725 STRLEN len = strlen(s);
3726 return PerlIO_write(f,s,len);
3729 #undef PerlIO_rewind
3731 PerlIO_rewind(PerlIO *f)
3733 PerlIO_seek(f,(Off_t)0,SEEK_SET);
3737 #undef PerlIO_vprintf
3739 PerlIO_vprintf(PerlIO *f, const char *fmt, va_list ap)
3742 SV *sv = newSVpvn("",0);
3748 Perl_va_copy(ap, apc);
3749 sv_vcatpvf(sv, fmt, &apc);
3751 sv_vcatpvf(sv, fmt, &ap);
3754 wrote = PerlIO_write(f,s,len);
3759 #undef PerlIO_printf
3761 PerlIO_printf(PerlIO *f,const char *fmt,...)
3766 result = PerlIO_vprintf(f,fmt,ap);
3771 #undef PerlIO_stdoutf
3773 PerlIO_stdoutf(const char *fmt,...)
3778 result = PerlIO_vprintf(PerlIO_stdout(),fmt,ap);
3783 #undef PerlIO_tmpfile
3785 PerlIO_tmpfile(void)
3787 /* I have no idea how portable mkstemp() is ... */
3788 #if defined(WIN32) || !defined(HAVE_MKSTEMP)
3791 FILE *stdio = PerlSIO_tmpfile();
3794 PerlIOStdio *s = PerlIOSelf(PerlIO_push(aTHX_ (f = PerlIO_allocate(aTHX)),&PerlIO_stdio,"w+",Nullsv),PerlIOStdio);
3800 SV *sv = newSVpv("/tmp/PerlIO_XXXXXX",0);
3801 int fd = mkstemp(SvPVX(sv));
3805 f = PerlIO_fdopen(fd,"w+");
3808 PerlIOBase(f)->flags |= PERLIO_F_TEMP;
3810 PerlLIO_unlink(SvPVX(sv));
3820 #endif /* USE_SFIO */
3821 #endif /* PERLIO_IS_STDIO */
3823 /*======================================================================================*/
3824 /* Now some functions in terms of above which may be needed even if
3825 we are not in true PerlIO mode
3829 #undef PerlIO_setpos
3831 PerlIO_setpos(PerlIO *f, SV *pos)
3837 Off_t *posn = (Off_t *) SvPV(pos,len);
3838 if (f && len == sizeof(Off_t))
3839 return PerlIO_seek(f,*posn,SEEK_SET);
3841 SETERRNO(EINVAL,SS$_IVCHAN);
3845 #undef PerlIO_setpos
3847 PerlIO_setpos(PerlIO *f, SV *pos)
3853 Fpos_t *fpos = (Fpos_t *) SvPV(pos,len);
3854 if (f && len == sizeof(Fpos_t))
3856 #if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64)
3857 return fsetpos64(f, fpos);
3859 return fsetpos(f, fpos);
3863 SETERRNO(EINVAL,SS$_IVCHAN);
3869 #undef PerlIO_getpos
3871 PerlIO_getpos(PerlIO *f, SV *pos)
3874 Off_t posn = PerlIO_tell(f);
3875 sv_setpvn(pos,(char *)&posn,sizeof(posn));
3876 return (posn == (Off_t)-1) ? -1 : 0;
3879 #undef PerlIO_getpos
3881 PerlIO_getpos(PerlIO *f, SV *pos)
3886 #if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64)
3887 code = fgetpos64(f, &fpos);
3889 code = fgetpos(f, &fpos);
3891 sv_setpvn(pos,(char *)&fpos,sizeof(fpos));
3896 #if (defined(PERLIO_IS_STDIO) || !defined(USE_SFIO)) && !defined(HAS_VPRINTF)
3899 vprintf(char *pat, char *args)
3901 _doprnt(pat, args, stdout);
3902 return 0; /* wrong, but perl doesn't use the return value */
3906 vfprintf(FILE *fd, char *pat, char *args)
3908 _doprnt(pat, args, fd);
3909 return 0; /* wrong, but perl doesn't use the return value */
3914 #ifndef PerlIO_vsprintf
3916 PerlIO_vsprintf(char *s, int n, const char *fmt, va_list ap)
3918 int val = vsprintf(s, fmt, ap);
3921 if (strlen(s) >= (STRLEN)n)
3924 (void)PerlIO_puts(Perl_error_log,
3925 "panic: sprintf overflow - memory corrupted!\n");
3933 #ifndef PerlIO_sprintf
3935 PerlIO_sprintf(char *s, int n, const char *fmt,...)
3940 result = PerlIO_vsprintf(s, n, fmt, ap);