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.
17 #define PERLIO_NOT_STDIO 0
18 #if !defined(PERLIO_IS_STDIO) && !defined(USE_SFIO)
19 /* #define PerlIO FILE */
22 * This file provides those parts of PerlIO abstraction
23 * which are not #defined in perlio.h.
24 * Which these are depends on various Configure #ifdef's
28 #define PERL_IN_PERLIO_C
31 #undef PerlMemShared_calloc
32 #define PerlMemShared_calloc(x,y) calloc(x,y)
33 #undef PerlMemShared_free
34 #define PerlMemShared_free(x) free(x)
37 perlsio_binmode(FILE *fp, int iotype, int mode)
39 /* This used to be contents of do_binmode in doio.c */
41 # if defined(atarist) || defined(__MINT__)
44 ((FILE*)fp)->_flag |= _IOBIN;
46 ((FILE*)fp)->_flag &= ~ _IOBIN;
52 if (PerlLIO_setmode(fileno(fp), mode) != -1) {
53 # if defined(WIN32) && defined(__BORLANDC__)
54 /* The translation mode of the stream is maintained independent
55 * of the translation mode of the fd in the Borland RTL (heavy
56 * digging through their runtime sources reveal). User has to
57 * set the mode explicitly for the stream (though they don't
58 * document this anywhere). GSAR 97-5-24
64 fp->flags &= ~ _F_BIN;
72 # if defined(USEMYBINMODE)
73 if (my_binmode(fp, iotype, mode) != FALSE)
85 PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names)
87 if (!names || !*names || strEQ(names,":crlf") || strEQ(names,":raw"))
91 Perl_croak(aTHX_ "Cannot apply \"%s\" in non-PerlIO perl",names);
97 PerlIO_binmode(pTHX_ PerlIO *fp, int iotype, int mode, const char *names)
99 return perlsio_binmode(fp,iotype,mode);
105 #ifdef PERLIO_IS_STDIO
110 /* Does nothing (yet) except force this file to be included
111 in perl binary. That allows this file to force inclusion
112 of other functions that may be required by loadable
113 extensions e.g. for FileHandle::tmpfile
117 #undef PerlIO_tmpfile
124 #else /* PERLIO_IS_STDIO */
131 /* This section is just to make sure these functions
132 get pulled in from libsfio.a
135 #undef PerlIO_tmpfile
145 /* Force this file to be included in perl binary. Which allows
146 * this file to force inclusion of other functions that may be
147 * required by loadable extensions e.g. for FileHandle::tmpfile
151 * sfio does its own 'autoflush' on stdout in common cases.
152 * Flush results in a lot of lseek()s to regular files and
153 * lot of small writes to pipes.
155 sfset(sfstdout,SF_SHARE,0);
159 /*======================================================================================*/
160 /* Implement all the PerlIO interface ourselves.
165 /* We _MUST_ have <unistd.h> if we are using lseek() and may have large files */
170 #include <sys/mman.h>
175 void PerlIO_debug(const char *fmt,...) __attribute__((format(__printf__,1,2)));
178 PerlIO_debug(const char *fmt,...)
186 char *s = PerlEnv_getenv("PERLIO_DEBUG");
188 dbg = PerlLIO_open3(s,O_WRONLY|O_CREAT|O_APPEND,0666);
195 SV *sv = newSVpvn("",0);
198 s = CopFILE(PL_curcop);
201 Perl_sv_catpvf(aTHX_ sv, "%s:%"IVdf" ", s, (IV)CopLINE(PL_curcop));
202 Perl_sv_vcatpvf(aTHX_ sv, fmt, &ap);
205 PerlLIO_write(dbg,s,len);
211 /*--------------------------------------------------------------------------------------*/
213 /* Inner level routines */
215 /* Table of pointers to the PerlIO structs (malloc'ed) */
216 PerlIO *_perlio = NULL;
217 #define PERLIO_TABLE_SIZE 64
220 PerlIO_allocate(pTHX)
222 /* Find a free slot in the table, allocating new table as necessary */
229 last = (PerlIO **)(f);
230 for (i=1; i < PERLIO_TABLE_SIZE; i++)
238 f = PerlMemShared_calloc(PERLIO_TABLE_SIZE,sizeof(PerlIO));
248 PerlIO_cleantable(pTHX_ PerlIO **tablep)
250 PerlIO *table = *tablep;
254 PerlIO_cleantable(aTHX_ (PerlIO **) &(table[0]));
255 for (i=PERLIO_TABLE_SIZE-1; i > 0; i--)
263 PerlMemShared_free(table);
275 PerlIO_cleantable(aTHX_ &_perlio);
279 PerlIO_pop(PerlIO *f)
285 PerlIO_debug("PerlIO_pop f=%p %s\n",f,l->tab->name);
287 (*l->tab->Popped)(f);
289 PerlMemShared_free(l);
293 /*--------------------------------------------------------------------------------------*/
294 /* XS Interface for perl code */
300 char *s = GvNAME(gv);
301 STRLEN l = GvNAMELEN(gv);
302 PerlIO_debug("%.*s\n",(int) l,s);
306 XS(XS_perlio_unimport)
310 char *s = GvNAME(gv);
311 STRLEN l = GvNAMELEN(gv);
312 PerlIO_debug("%.*s\n",(int) l,s);
317 PerlIO_find_layer(const char *name, STRLEN len)
322 if ((SSize_t) len <= 0)
324 svp = hv_fetch(PerlIO_layer_hv,name,len,0);
325 if (svp && (sv = *svp) && SvROK(sv))
332 perlio_mg_set(pTHX_ SV *sv, MAGIC *mg)
336 IO *io = GvIOn((GV *)SvRV(sv));
337 PerlIO *ifp = IoIFP(io);
338 PerlIO *ofp = IoOFP(io);
339 AV *av = (AV *) mg->mg_obj;
340 Perl_warn(aTHX_ "set %"SVf" %p %p %p",sv,io,ifp,ofp);
346 perlio_mg_get(pTHX_ SV *sv, MAGIC *mg)
350 IO *io = GvIOn((GV *)SvRV(sv));
351 PerlIO *ifp = IoIFP(io);
352 PerlIO *ofp = IoOFP(io);
353 AV *av = (AV *) mg->mg_obj;
354 Perl_warn(aTHX_ "get %"SVf" %p %p %p",sv,io,ifp,ofp);
360 perlio_mg_clear(pTHX_ SV *sv, MAGIC *mg)
362 Perl_warn(aTHX_ "clear %"SVf,sv);
367 perlio_mg_free(pTHX_ SV *sv, MAGIC *mg)
369 Perl_warn(aTHX_ "free %"SVf,sv);
373 MGVTBL perlio_vtab = {
381 XS(XS_io_MODIFY_SCALAR_ATTRIBUTES)
384 SV *sv = SvRV(ST(1));
389 sv_magic(sv, (SV *)av, '~', NULL, 0);
391 mg = mg_find(sv,'~');
392 mg->mg_virtual = &perlio_vtab;
394 Perl_warn(aTHX_ "attrib %"SVf,sv);
395 for (i=2; i < items; i++)
398 const char *name = SvPV(ST(i),len);
399 SV *layer = PerlIO_find_layer(name,len);
402 av_push(av,SvREFCNT_inc(layer));
415 PerlIO_define_layer(PerlIO_funcs *tab)
418 HV *stash = gv_stashpv("perlio::Layer", TRUE);
419 SV *sv = sv_bless(newRV_noinc(newSViv(PTR2IV(tab))),stash);
420 if (!PerlIO_layer_hv)
422 PerlIO_layer_hv = get_hv("open::layers",GV_ADD|GV_ADDMULTI);
424 hv_store(PerlIO_layer_hv,tab->name,strlen(tab->name),sv,0);
425 PerlIO_debug("define %s %p\n",tab->name,tab);
429 PerlIO_default_buffer(pTHX)
431 PerlIO_funcs *tab = &PerlIO_perlio;
432 if (O_BINARY != O_TEXT)
438 if (PerlIO_stdio.Set_ptrcnt)
443 PerlIO_debug("Pushing %s\n",tab->name);
444 av_push(PerlIO_layer_av,SvREFCNT_inc(PerlIO_find_layer(tab->name,0)));
449 PerlIO_default_layer(I32 n)
454 PerlIO_funcs *tab = &PerlIO_stdio;
456 if (!PerlIO_layer_av)
458 const char *s = PerlEnv_getenv("PERLIO");
459 PerlIO_layer_av = get_av("open::layers",GV_ADD|GV_ADDMULTI);
460 newXS("perlio::import",XS_perlio_import,__FILE__);
461 newXS("perlio::unimport",XS_perlio_unimport,__FILE__);
463 newXS("io::MODIFY_SCALAR_ATTRIBUTES",XS_io_MODIFY_SCALAR_ATTRIBUTES,__FILE__);
465 PerlIO_define_layer(&PerlIO_raw);
466 PerlIO_define_layer(&PerlIO_unix);
467 PerlIO_define_layer(&PerlIO_perlio);
468 PerlIO_define_layer(&PerlIO_stdio);
469 PerlIO_define_layer(&PerlIO_crlf);
471 PerlIO_define_layer(&PerlIO_mmap);
473 PerlIO_define_layer(&PerlIO_utf8);
474 PerlIO_define_layer(&PerlIO_byte);
475 av_push(PerlIO_layer_av,SvREFCNT_inc(PerlIO_find_layer(PerlIO_unix.name,0)));
481 while (*s && isSPACE((unsigned char)*s))
487 while (*e && !isSPACE((unsigned char)*e))
491 layer = PerlIO_find_layer(s,e-s);
494 PerlIO_funcs *tab = INT2PTR(PerlIO_funcs *, SvIV(SvRV(layer)));
495 if ((tab->kind & PERLIO_K_DUMMY) && (tab->kind & PERLIO_K_BUFFERED))
498 PerlIO_default_buffer(aTHX);
500 PerlIO_debug("Pushing %.*s\n",(e-s),s);
501 av_push(PerlIO_layer_av,SvREFCNT_inc(layer));
502 buffered |= (tab->kind & PERLIO_K_BUFFERED);
505 Perl_warn(aTHX_ "perlio: unknown layer \"%.*s\"",(e-s),s);
511 len = av_len(PerlIO_layer_av);
514 PerlIO_default_buffer(aTHX);
515 len = av_len(PerlIO_layer_av);
519 svp = av_fetch(PerlIO_layer_av,n,0);
520 if (svp && (layer = *svp) && SvROK(layer) && SvIOK((layer = SvRV(layer))))
522 tab = INT2PTR(PerlIO_funcs *, SvIV(layer));
524 /* PerlIO_debug("Layer %d is %s\n",n,tab->name); */
528 #define PerlIO_default_top() PerlIO_default_layer(-1)
529 #define PerlIO_default_btm() PerlIO_default_layer(0)
537 PerlIO_allocate(aTHX);
538 PerlIO_fdopen(0,"Ir" PERLIO_STDTEXT);
539 PerlIO_fdopen(1,"Iw" PERLIO_STDTEXT);
540 PerlIO_fdopen(2,"Iw" PERLIO_STDTEXT);
545 PerlIO_push(PerlIO *f,PerlIO_funcs *tab,const char *mode,const char *arg,STRLEN len)
549 l = PerlMemShared_calloc(tab->size,sizeof(char));
552 Zero(l,tab->size,char);
556 PerlIO_debug("PerlIO_push f=%p %s %s '%.*s'\n",
557 f,tab->name,(mode) ? mode : "(Null)",(int) len,arg);
558 if ((*l->tab->Pushed)(f,mode,arg,len) != 0)
568 PerlIOPop_pushed(PerlIO *f, const char *mode, const char *arg, STRLEN len)
581 PerlIORaw_pushed(PerlIO *f, const char *mode, const char *arg, STRLEN len)
583 /* Remove the dummy layer */
585 /* Pop back to bottom layer */
590 while (!(PerlIOBase(f)->tab->kind & PERLIO_K_RAW))
598 /* Nothing bellow - push unix on top then remove it */
599 if (PerlIO_push(f,PerlIO_default_btm(),mode,arg,len))
601 PerlIO_pop(PerlIONext(f));
606 PerlIO_debug(":raw f=%p :%s\n",f,PerlIOBase(f)->tab->name);
613 PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names)
617 const char *s = names;
620 while (isSPACE(*s) || *s == ':')
626 const char *as = Nullch;
630 /* Message is consistent with how attribute lists are passed.
631 Even though this means "foo : : bar" is seen as an invalid separator
633 char q = ((*s == '\'') ? '"' : '\'');
634 Perl_warn(aTHX_ "perlio: invalid separator character %c%c%c in layer specification list", q, *s, q);
640 } while (isALNUM(*e));
658 /* It's a nul terminated string, not allowed to \ the terminating null.
659 Anything other character is passed over. */
667 Perl_warn(aTHX_ "perlio: argument list not closed for layer \"%.*s\"",(int)(e - s),s);
677 SV *layer = PerlIO_find_layer(s,llen);
680 PerlIO_funcs *tab = INT2PTR(PerlIO_funcs *, SvIV(SvRV(layer)));
683 if (!PerlIO_push(f,tab,mode,as,alen))
688 Perl_warn(aTHX_ "perlio: unknown layer \"%.*s\"",(int)llen,s);
699 /*--------------------------------------------------------------------------------------*/
700 /* Given the abstraction above the public API functions */
703 PerlIO_binmode(pTHX_ PerlIO *f, int iotype, int mode, const char *names)
705 PerlIO_debug("PerlIO_binmode f=%p %s %c %x %s\n",
706 f,PerlIOBase(f)->tab->name,iotype,mode, (names) ? names : "(Null)");
707 if (!names && (O_TEXT != O_BINARY && (mode & O_BINARY)))
713 if (PerlIOBase(top)->tab == &PerlIO_crlf)
716 PerlIOBase(top)->flags &= ~PERLIO_F_CRLF;
719 top = PerlIONext(top);
722 return PerlIO_apply_layers(aTHX_ f, NULL, names) == 0 ? TRUE : FALSE;
727 PerlIO__close(PerlIO *f)
729 return (*PerlIOBase(f)->tab->Close)(f);
732 #undef PerlIO_fdupopen
734 PerlIO_fdupopen(pTHX_ PerlIO *f)
737 int fd = PerlLIO_dup(PerlIO_fileno(f));
738 PerlIO *new = PerlIO_fdopen(fd,PerlIO_modestr(f,buf));
741 Off_t posn = PerlIO_tell(f);
742 PerlIO_seek(new,posn,SEEK_SET);
749 PerlIO_close(PerlIO *f)
751 int code = (*PerlIOBase(f)->tab->Close)(f);
761 PerlIO_fileno(PerlIO *f)
763 return (*PerlIOBase(f)->tab->Fileno)(f);
770 PerlIO_fdopen(int fd, const char *mode)
772 PerlIO_funcs *tab = PerlIO_default_top();
775 return (*tab->Fdopen)(tab,fd,mode);
780 PerlIO_open(const char *path, const char *mode)
782 PerlIO_funcs *tab = PerlIO_default_top();
785 return (*tab->Open)(tab,path,mode);
790 PerlIO_reopen(const char *path, const char *mode, PerlIO *f)
795 if ((*PerlIOBase(f)->tab->Reopen)(path,mode,f) == 0)
797 if ((*PerlIOBase(f)->tab->Pushed)(f,mode,Nullch,0) == 0)
803 return PerlIO_open(path,mode);
808 PerlIO_read(PerlIO *f, void *vbuf, Size_t count)
810 return (*PerlIOBase(f)->tab->Read)(f,vbuf,count);
815 PerlIO_unread(PerlIO *f, const void *vbuf, Size_t count)
817 return (*PerlIOBase(f)->tab->Unread)(f,vbuf,count);
822 PerlIO_write(PerlIO *f, const void *vbuf, Size_t count)
824 return (*PerlIOBase(f)->tab->Write)(f,vbuf,count);
829 PerlIO_seek(PerlIO *f, Off_t offset, int whence)
831 return (*PerlIOBase(f)->tab->Seek)(f,offset,whence);
836 PerlIO_tell(PerlIO *f)
838 return (*PerlIOBase(f)->tab->Tell)(f);
843 PerlIO_flush(PerlIO *f)
847 PerlIO_funcs *tab = PerlIOBase(f)->tab;
848 if (tab && tab->Flush)
850 return (*tab->Flush)(f);
854 PerlIO_debug("Cannot flush f=%p :%s\n",f,tab->name);
861 PerlIO **table = &_perlio;
866 table = (PerlIO **)(f++);
867 for (i=1; i < PERLIO_TABLE_SIZE; i++)
869 if (*f && PerlIO_flush(f) != 0)
880 PerlIO_fill(PerlIO *f)
882 return (*PerlIOBase(f)->tab->Fill)(f);
887 PerlIO_isutf8(PerlIO *f)
889 return (PerlIOBase(f)->flags & PERLIO_F_UTF8) != 0;
894 PerlIO_eof(PerlIO *f)
896 return (*PerlIOBase(f)->tab->Eof)(f);
901 PerlIO_error(PerlIO *f)
903 return (*PerlIOBase(f)->tab->Error)(f);
906 #undef PerlIO_clearerr
908 PerlIO_clearerr(PerlIO *f)
911 (*PerlIOBase(f)->tab->Clearerr)(f);
914 #undef PerlIO_setlinebuf
916 PerlIO_setlinebuf(PerlIO *f)
918 (*PerlIOBase(f)->tab->Setlinebuf)(f);
921 #undef PerlIO_has_base
923 PerlIO_has_base(PerlIO *f)
927 return (PerlIOBase(f)->tab->Get_base != NULL);
932 #undef PerlIO_fast_gets
934 PerlIO_fast_gets(PerlIO *f)
936 if (f && *f && (PerlIOBase(f)->flags & PERLIO_F_FASTGETS))
938 PerlIO_funcs *tab = PerlIOBase(f)->tab;
939 return (tab->Set_ptrcnt != NULL);
944 #undef PerlIO_has_cntptr
946 PerlIO_has_cntptr(PerlIO *f)
950 PerlIO_funcs *tab = PerlIOBase(f)->tab;
951 return (tab->Get_ptr != NULL && tab->Get_cnt != NULL);
956 #undef PerlIO_canset_cnt
958 PerlIO_canset_cnt(PerlIO *f)
962 PerlIOl *l = PerlIOBase(f);
963 return (l->tab->Set_ptrcnt != NULL);
968 #undef PerlIO_get_base
970 PerlIO_get_base(PerlIO *f)
972 return (*PerlIOBase(f)->tab->Get_base)(f);
975 #undef PerlIO_get_bufsiz
977 PerlIO_get_bufsiz(PerlIO *f)
979 return (*PerlIOBase(f)->tab->Get_bufsiz)(f);
982 #undef PerlIO_get_ptr
984 PerlIO_get_ptr(PerlIO *f)
986 PerlIO_funcs *tab = PerlIOBase(f)->tab;
987 if (tab->Get_ptr == NULL)
989 return (*tab->Get_ptr)(f);
992 #undef PerlIO_get_cnt
994 PerlIO_get_cnt(PerlIO *f)
996 PerlIO_funcs *tab = PerlIOBase(f)->tab;
997 if (tab->Get_cnt == NULL)
999 return (*tab->Get_cnt)(f);
1002 #undef PerlIO_set_cnt
1004 PerlIO_set_cnt(PerlIO *f,int cnt)
1006 (*PerlIOBase(f)->tab->Set_ptrcnt)(f,NULL,cnt);
1009 #undef PerlIO_set_ptrcnt
1011 PerlIO_set_ptrcnt(PerlIO *f, STDCHAR *ptr, int cnt)
1013 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1014 if (tab->Set_ptrcnt == NULL)
1017 Perl_croak(aTHX_ "PerlIO buffer snooping abuse");
1019 (*PerlIOBase(f)->tab->Set_ptrcnt)(f,ptr,cnt);
1022 /*--------------------------------------------------------------------------------------*/
1023 /* utf8 and raw dummy layers */
1026 PerlIOUtf8_pushed(PerlIO *f, const char *mode, const char *arg, STRLEN len)
1030 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1032 if (tab->kind & PERLIO_K_UTF8)
1033 PerlIOBase(f)->flags |= PERLIO_F_UTF8;
1035 PerlIOBase(f)->flags &= ~PERLIO_F_UTF8;
1042 PerlIOUtf8_fdopen(PerlIO_funcs *self, int fd,const char *mode)
1044 PerlIO_funcs *tab = PerlIO_default_layer(-2);
1045 PerlIO *f = (*tab->Fdopen)(tab,fd,mode);
1048 PerlIOl *l = PerlIOBase(f);
1049 if (tab->kind & PERLIO_K_UTF8)
1050 l->flags |= PERLIO_F_UTF8;
1052 l->flags &= ~PERLIO_F_UTF8;
1058 PerlIOUtf8_open(PerlIO_funcs *self, const char *path,const char *mode)
1060 PerlIO_funcs *tab = PerlIO_default_layer(-2);
1061 PerlIO *f = (*tab->Open)(tab,path,mode);
1064 PerlIOl *l = PerlIOBase(f);
1065 if (tab->kind & PERLIO_K_UTF8)
1066 l->flags |= PERLIO_F_UTF8;
1068 l->flags &= ~PERLIO_F_UTF8;
1073 PerlIO_funcs PerlIO_utf8 = {
1076 PERLIO_K_DUMMY|PERLIO_F_UTF8,
1095 NULL, /* get_base */
1096 NULL, /* get_bufsiz */
1099 NULL, /* set_ptrcnt */
1102 PerlIO_funcs PerlIO_byte = {
1124 NULL, /* get_base */
1125 NULL, /* get_bufsiz */
1128 NULL, /* set_ptrcnt */
1132 PerlIORaw_fdopen(PerlIO_funcs *self, int fd,const char *mode)
1134 PerlIO_funcs *tab = PerlIO_default_btm();
1135 return (*tab->Fdopen)(tab,fd,mode);
1139 PerlIORaw_open(PerlIO_funcs *self, const char *path,const char *mode)
1141 PerlIO_funcs *tab = PerlIO_default_btm();
1142 return (*tab->Open)(tab,path,mode);
1145 PerlIO_funcs PerlIO_raw = {
1167 NULL, /* get_base */
1168 NULL, /* get_bufsiz */
1171 NULL, /* set_ptrcnt */
1173 /*--------------------------------------------------------------------------------------*/
1174 /*--------------------------------------------------------------------------------------*/
1175 /* "Methods" of the "base class" */
1178 PerlIOBase_fileno(PerlIO *f)
1180 return PerlIO_fileno(PerlIONext(f));
1184 PerlIO_modestr(PerlIO *f,char *buf)
1187 IV flags = PerlIOBase(f)->flags;
1188 if (flags & PERLIO_F_APPEND)
1191 if (flags & PERLIO_F_CANREAD)
1196 else if (flags & PERLIO_F_CANREAD)
1199 if (flags & PERLIO_F_CANWRITE)
1202 else if (flags & PERLIO_F_CANWRITE)
1205 if (flags & PERLIO_F_CANREAD)
1210 #if O_TEXT != O_BINARY
1211 if (!(flags & PERLIO_F_CRLF))
1219 PerlIOBase_pushed(PerlIO *f, const char *mode, const char *arg, STRLEN len)
1221 PerlIOl *l = PerlIOBase(f);
1222 const char *omode = mode;
1224 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1225 l->flags &= ~(PERLIO_F_CANREAD|PERLIO_F_CANWRITE|
1226 PERLIO_F_TRUNCATE|PERLIO_F_APPEND);
1227 if (tab->Set_ptrcnt != NULL)
1228 l->flags |= PERLIO_F_FASTGETS;
1234 l->flags |= PERLIO_F_CANREAD;
1237 l->flags |= PERLIO_F_APPEND|PERLIO_F_CANWRITE;
1240 l->flags |= PERLIO_F_TRUNCATE|PERLIO_F_CANWRITE;
1251 l->flags |= PERLIO_F_CANREAD|PERLIO_F_CANWRITE;
1254 l->flags &= ~PERLIO_F_CRLF;
1257 l->flags |= PERLIO_F_CRLF;
1269 l->flags |= l->next->flags &
1270 (PERLIO_F_CANREAD|PERLIO_F_CANWRITE|PERLIO_F_TRUNCATE|PERLIO_F_APPEND);
1274 PerlIO_debug("PerlIOBase_pushed f=%p %s %s fl=%08"UVxf" (%s)\n",
1275 f,PerlIOBase(f)->tab->name,(omode) ? omode : "(Null)",
1276 l->flags,PerlIO_modestr(f,temp));
1282 PerlIOBase_popped(PerlIO *f)
1288 PerlIOBase_unread(PerlIO *f, const void *vbuf, Size_t count)
1290 Off_t old = PerlIO_tell(f);
1292 PerlIO_push(f,&PerlIO_pending,"r",Nullch,0);
1293 done = PerlIOBuf_unread(f,vbuf,count);
1294 PerlIOSelf(f,PerlIOBuf)->posn = old - done;
1299 PerlIOBase_noop_ok(PerlIO *f)
1305 PerlIOBase_noop_fail(PerlIO *f)
1311 PerlIOBase_close(PerlIO *f)
1314 PerlIO *n = PerlIONext(f);
1315 if (PerlIO_flush(f) != 0)
1317 if (n && (*PerlIOBase(n)->tab->Close)(n) != 0)
1319 PerlIOBase(f)->flags &= ~(PERLIO_F_CANREAD|PERLIO_F_CANWRITE|PERLIO_F_OPEN);
1324 PerlIOBase_eof(PerlIO *f)
1328 return (PerlIOBase(f)->flags & PERLIO_F_EOF) != 0;
1334 PerlIOBase_error(PerlIO *f)
1338 return (PerlIOBase(f)->flags & PERLIO_F_ERROR) != 0;
1344 PerlIOBase_clearerr(PerlIO *f)
1348 PerlIO *n = PerlIONext(f);
1349 PerlIOBase(f)->flags &= ~(PERLIO_F_ERROR|PERLIO_F_EOF);
1356 PerlIOBase_setlinebuf(PerlIO *f)
1361 /*--------------------------------------------------------------------------------------*/
1362 /* Bottom-most level for UNIX-like case */
1366 struct _PerlIO base; /* The generic part */
1367 int fd; /* UNIX like file descriptor */
1368 int oflags; /* open/fcntl flags */
1372 PerlIOUnix_oflags(const char *mode)
1387 oflags = O_CREAT|O_TRUNC;
1398 oflags = O_CREAT|O_APPEND;
1414 else if (*mode == 't')
1417 oflags &= ~O_BINARY;
1420 /* Always open in binary mode */
1422 if (*mode || oflags == -1)
1431 PerlIOUnix_fileno(PerlIO *f)
1433 return PerlIOSelf(f,PerlIOUnix)->fd;
1437 PerlIOUnix_pushed(PerlIO *f, const char *mode, const char *arg, STRLEN len)
1439 IV code = PerlIOBase_pushed(f,mode,arg,len);
1442 PerlIOUnix *s = PerlIOSelf(f,PerlIOUnix);
1443 s->fd = PerlIO_fileno(PerlIONext(f));
1444 s->oflags = PerlIOUnix_oflags(mode);
1446 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
1451 PerlIOUnix_fdopen(PerlIO_funcs *self, int fd,const char *mode)
1459 int oflags = PerlIOUnix_oflags(mode);
1462 PerlIOUnix *s = PerlIOSelf(PerlIO_push(f = PerlIO_allocate(aTHX),self,mode,Nullch,0),PerlIOUnix);
1471 PerlIOUnix_open(PerlIO_funcs *self, const char *path,const char *mode)
1475 int oflags = PerlIOUnix_oflags(mode);
1478 int fd = PerlLIO_open3(path,oflags,0666);
1481 PerlIOUnix *s = PerlIOSelf(PerlIO_push(f = PerlIO_allocate(aTHX),self,mode,Nullch,0),PerlIOUnix);
1484 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
1491 PerlIOUnix_reopen(const char *path, const char *mode, PerlIO *f)
1493 PerlIOUnix *s = PerlIOSelf(f,PerlIOUnix);
1494 int oflags = PerlIOUnix_oflags(mode);
1495 if (PerlIOBase(f)->flags & PERLIO_F_OPEN)
1496 (*PerlIOBase(f)->tab->Close)(f);
1500 int fd = PerlLIO_open3(path,oflags,0666);
1505 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
1513 PerlIOUnix_read(PerlIO *f, void *vbuf, Size_t count)
1516 int fd = PerlIOSelf(f,PerlIOUnix)->fd;
1517 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD))
1521 SSize_t len = PerlLIO_read(fd,vbuf,count);
1522 if (len >= 0 || errno != EINTR)
1525 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
1526 else if (len == 0 && count != 0)
1527 PerlIOBase(f)->flags |= PERLIO_F_EOF;
1535 PerlIOUnix_write(PerlIO *f, const void *vbuf, Size_t count)
1538 int fd = PerlIOSelf(f,PerlIOUnix)->fd;
1541 SSize_t len = PerlLIO_write(fd,vbuf,count);
1542 if (len >= 0 || errno != EINTR)
1545 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
1553 PerlIOUnix_seek(PerlIO *f, Off_t offset, int whence)
1556 Off_t new = PerlLIO_lseek(PerlIOSelf(f,PerlIOUnix)->fd,offset,whence);
1557 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
1558 return (new == (Off_t) -1) ? -1 : 0;
1562 PerlIOUnix_tell(PerlIO *f)
1565 Off_t posn = PerlLIO_lseek(PerlIOSelf(f,PerlIOUnix)->fd,0,SEEK_CUR);
1566 return PerlLIO_lseek(PerlIOSelf(f,PerlIOUnix)->fd,0,SEEK_CUR);
1570 PerlIOUnix_close(PerlIO *f)
1573 int fd = PerlIOSelf(f,PerlIOUnix)->fd;
1575 while (PerlLIO_close(fd) != 0)
1586 PerlIOBase(f)->flags &= ~PERLIO_F_OPEN;
1591 PerlIO_funcs PerlIO_unix = {
1607 PerlIOBase_noop_ok, /* flush */
1608 PerlIOBase_noop_fail, /* fill */
1611 PerlIOBase_clearerr,
1612 PerlIOBase_setlinebuf,
1613 NULL, /* get_base */
1614 NULL, /* get_bufsiz */
1617 NULL, /* set_ptrcnt */
1620 /*--------------------------------------------------------------------------------------*/
1621 /* stdio as a layer */
1625 struct _PerlIO base;
1626 FILE * stdio; /* The stream */
1630 PerlIOStdio_fileno(PerlIO *f)
1633 return PerlSIO_fileno(PerlIOSelf(f,PerlIOStdio)->stdio);
1637 PerlIOStdio_mode(const char *mode,char *tmode)
1644 if (O_BINARY != O_TEXT)
1653 PerlIOStdio_fdopen(PerlIO_funcs *self, int fd,const char *mode)
1672 stdio = PerlSIO_stdin;
1675 stdio = PerlSIO_stdout;
1678 stdio = PerlSIO_stderr;
1684 stdio = PerlSIO_fdopen(fd,mode = PerlIOStdio_mode(mode,tmode));
1688 PerlIOStdio *s = PerlIOSelf(PerlIO_push(f = PerlIO_allocate(aTHX),self,mode,Nullch,0),PerlIOStdio);
1695 /* This isn't used yet ... */
1697 PerlIOStdio_pushed(PerlIO *f, const char *mode, const char *arg, STRLEN len)
1701 PerlIOStdio *s = PerlIOSelf(f,PerlIOStdio);
1703 FILE *stdio = PerlSIO_fdopen(PerlIO_fileno(PerlIONext(f)),mode = PerlIOStdio_mode(mode,tmode));
1709 return PerlIOBase_pushed(f,mode,arg,len);
1712 #undef PerlIO_importFILE
1714 PerlIO_importFILE(FILE *stdio, int fl)
1720 PerlIOStdio *s = PerlIOSelf(PerlIO_push(f = PerlIO_allocate(aTHX),&PerlIO_stdio,"r+",Nullch,0),PerlIOStdio);
1727 PerlIOStdio_open(PerlIO_funcs *self, const char *path,const char *mode)
1731 FILE *stdio = PerlSIO_fopen(path,mode);
1735 PerlIOStdio *s = PerlIOSelf(PerlIO_push(f = PerlIO_allocate(aTHX), self,
1736 (mode = PerlIOStdio_mode(mode,tmode)),Nullch,0),
1744 PerlIOStdio_reopen(const char *path, const char *mode, PerlIO *f)
1747 PerlIOStdio *s = PerlIOSelf(f,PerlIOStdio);
1749 FILE *stdio = PerlSIO_freopen(path,(mode = PerlIOStdio_mode(mode,tmode)),s->stdio);
1757 PerlIOStdio_read(PerlIO *f, void *vbuf, Size_t count)
1760 FILE *s = PerlIOSelf(f,PerlIOStdio)->stdio;
1764 STDCHAR *buf = (STDCHAR *) vbuf;
1765 /* Perl is expecting PerlIO_getc() to fill the buffer
1766 * Linux's stdio does not do that for fread()
1768 int ch = PerlSIO_fgetc(s);
1776 got = PerlSIO_fread(vbuf,1,count,s);
1781 PerlIOStdio_unread(PerlIO *f, const void *vbuf, Size_t count)
1784 FILE *s = PerlIOSelf(f,PerlIOStdio)->stdio;
1785 STDCHAR *buf = ((STDCHAR *)vbuf)+count-1;
1789 int ch = *buf-- & 0xff;
1790 if (PerlSIO_ungetc(ch,s) != ch)
1799 PerlIOStdio_write(PerlIO *f, const void *vbuf, Size_t count)
1802 return PerlSIO_fwrite(vbuf,1,count,PerlIOSelf(f,PerlIOStdio)->stdio);
1806 PerlIOStdio_seek(PerlIO *f, Off_t offset, int whence)
1809 FILE *stdio = PerlIOSelf(f,PerlIOStdio)->stdio;
1810 return PerlSIO_fseek(stdio,offset,whence);
1814 PerlIOStdio_tell(PerlIO *f)
1817 FILE *stdio = PerlIOSelf(f,PerlIOStdio)->stdio;
1818 return PerlSIO_ftell(stdio);
1822 PerlIOStdio_close(PerlIO *f)
1826 int optval, optlen = sizeof(int);
1828 FILE *stdio = PerlIOSelf(f,PerlIOStdio)->stdio;
1831 (getsockopt(PerlIO_fileno(f), SOL_SOCKET, SO_TYPE, (char *)&optval, &optlen) < 0) ?
1832 PerlSIO_fclose(stdio) :
1833 close(PerlIO_fileno(f))
1835 PerlSIO_fclose(stdio)
1842 PerlIOStdio_flush(PerlIO *f)
1845 FILE *stdio = PerlIOSelf(f,PerlIOStdio)->stdio;
1846 if (PerlIOBase(f)->flags & PERLIO_F_CANWRITE)
1848 return PerlSIO_fflush(stdio);
1853 /* FIXME: This discards ungetc() and pre-read stuff which is
1854 not right if this is just a "sync" from a layer above
1855 Suspect right design is to do _this_ but not have layer above
1856 flush this layer read-to-read
1858 /* Not writeable - sync by attempting a seek */
1860 if (PerlSIO_fseek(stdio,(Off_t) 0, SEEK_CUR) != 0)
1868 PerlIOStdio_fill(PerlIO *f)
1871 FILE *stdio = PerlIOSelf(f,PerlIOStdio)->stdio;
1873 /* fflush()ing read-only streams can cause trouble on some stdio-s */
1874 if ((PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
1876 if (PerlSIO_fflush(stdio) != 0)
1879 c = PerlSIO_fgetc(stdio);
1880 if (c == EOF || PerlSIO_ungetc(c,stdio) != c)
1886 PerlIOStdio_eof(PerlIO *f)
1889 return PerlSIO_feof(PerlIOSelf(f,PerlIOStdio)->stdio);
1893 PerlIOStdio_error(PerlIO *f)
1896 return PerlSIO_ferror(PerlIOSelf(f,PerlIOStdio)->stdio);
1900 PerlIOStdio_clearerr(PerlIO *f)
1903 PerlSIO_clearerr(PerlIOSelf(f,PerlIOStdio)->stdio);
1907 PerlIOStdio_setlinebuf(PerlIO *f)
1910 #ifdef HAS_SETLINEBUF
1911 PerlSIO_setlinebuf(PerlIOSelf(f,PerlIOStdio)->stdio);
1913 PerlSIO_setvbuf(PerlIOSelf(f,PerlIOStdio)->stdio, Nullch, _IOLBF, 0);
1919 PerlIOStdio_get_base(PerlIO *f)
1922 FILE *stdio = PerlIOSelf(f,PerlIOStdio)->stdio;
1923 return PerlSIO_get_base(stdio);
1927 PerlIOStdio_get_bufsiz(PerlIO *f)
1930 FILE *stdio = PerlIOSelf(f,PerlIOStdio)->stdio;
1931 return PerlSIO_get_bufsiz(stdio);
1935 #ifdef USE_STDIO_PTR
1937 PerlIOStdio_get_ptr(PerlIO *f)
1940 FILE *stdio = PerlIOSelf(f,PerlIOStdio)->stdio;
1941 return PerlSIO_get_ptr(stdio);
1945 PerlIOStdio_get_cnt(PerlIO *f)
1948 FILE *stdio = PerlIOSelf(f,PerlIOStdio)->stdio;
1949 return PerlSIO_get_cnt(stdio);
1953 PerlIOStdio_set_ptrcnt(PerlIO *f,STDCHAR *ptr,SSize_t cnt)
1956 FILE *stdio = PerlIOSelf(f,PerlIOStdio)->stdio;
1959 #ifdef STDIO_PTR_LVALUE
1960 PerlSIO_set_ptr(stdio,ptr);
1961 #ifdef STDIO_PTR_LVAL_SETS_CNT
1962 if (PerlSIO_get_cnt(stdio) != (cnt))
1965 assert(PerlSIO_get_cnt(stdio) == (cnt));
1968 #if (!defined(STDIO_PTR_LVAL_NOCHANGE_CNT))
1969 /* Setting ptr _does_ change cnt - we are done */
1972 #else /* STDIO_PTR_LVALUE */
1974 #endif /* STDIO_PTR_LVALUE */
1976 /* Now (or only) set cnt */
1977 #ifdef STDIO_CNT_LVALUE
1978 PerlSIO_set_cnt(stdio,cnt);
1979 #else /* STDIO_CNT_LVALUE */
1980 #if (defined(STDIO_PTR_LVALUE) && defined(STDIO_PTR_LVAL_SETS_CNT))
1981 PerlSIO_set_ptr(stdio,PerlSIO_get_ptr(stdio)+(PerlSIO_get_cnt(stdio)-cnt));
1982 #else /* STDIO_PTR_LVAL_SETS_CNT */
1984 #endif /* STDIO_PTR_LVAL_SETS_CNT */
1985 #endif /* STDIO_CNT_LVALUE */
1990 PerlIO_funcs PerlIO_stdio = {
1992 sizeof(PerlIOStdio),
2010 PerlIOStdio_clearerr,
2011 PerlIOStdio_setlinebuf,
2013 PerlIOStdio_get_base,
2014 PerlIOStdio_get_bufsiz,
2019 #ifdef USE_STDIO_PTR
2020 PerlIOStdio_get_ptr,
2021 PerlIOStdio_get_cnt,
2022 #if (defined(STDIO_PTR_LVALUE) && (defined(STDIO_CNT_LVALUE) || defined(STDIO_PTR_LVAL_SETS_CNT)))
2023 PerlIOStdio_set_ptrcnt
2024 #else /* STDIO_PTR_LVALUE */
2026 #endif /* STDIO_PTR_LVALUE */
2027 #else /* USE_STDIO_PTR */
2031 #endif /* USE_STDIO_PTR */
2034 #undef PerlIO_exportFILE
2036 PerlIO_exportFILE(PerlIO *f, int fl)
2040 stdio = fdopen(PerlIO_fileno(f),"r+");
2043 PerlIOStdio *s = PerlIOSelf(PerlIO_push(f,&PerlIO_stdio,"r+",Nullch,0),PerlIOStdio);
2049 #undef PerlIO_findFILE
2051 PerlIO_findFILE(PerlIO *f)
2056 if (l->tab == &PerlIO_stdio)
2058 PerlIOStdio *s = PerlIOSelf(&l,PerlIOStdio);
2061 l = *PerlIONext(&l);
2063 return PerlIO_exportFILE(f,0);
2066 #undef PerlIO_releaseFILE
2068 PerlIO_releaseFILE(PerlIO *p, FILE *f)
2072 /*--------------------------------------------------------------------------------------*/
2073 /* perlio buffer layer */
2076 PerlIOBuf_pushed(PerlIO *f, const char *mode, const char *arg, STRLEN len)
2078 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2079 b->posn = PerlIO_tell(PerlIONext(f));
2080 return PerlIOBase_pushed(f,mode,arg,len);
2084 PerlIOBuf_fdopen(PerlIO_funcs *self, int fd, const char *mode)
2087 PerlIO_funcs *tab = PerlIO_default_btm();
2095 #if O_BINARY != O_TEXT
2096 /* do something about failing setmode()? --jhi */
2097 PerlLIO_setmode(fd, O_BINARY);
2099 f = (*tab->Fdopen)(tab,fd,mode);
2102 PerlIOBuf *b = PerlIOSelf(PerlIO_push(f,self,mode,Nullch,0),PerlIOBuf);
2103 if (init && fd == 2)
2105 /* Initial stderr is unbuffered */
2106 PerlIOBase(f)->flags |= PERLIO_F_UNBUF;
2109 PerlIO_debug("PerlIOBuf_fdopen %s f=%p fd=%d m=%s fl=%08"UVxf"\n",
2110 self->name,f,fd,mode,PerlIOBase(f)->flags);
2117 PerlIOBuf_open(PerlIO_funcs *self, const char *path, const char *mode)
2119 PerlIO_funcs *tab = PerlIO_default_btm();
2120 PerlIO *f = (*tab->Open)(tab,path,mode);
2123 PerlIO_push(f,self,mode,Nullch,0);
2129 PerlIOBuf_reopen(const char *path, const char *mode, PerlIO *f)
2131 PerlIO *next = PerlIONext(f);
2132 int code = (*PerlIOBase(next)->tab->Reopen)(path,mode,next);
2134 code = (*PerlIOBase(f)->tab->Pushed)(f,mode,Nullch,0);
2138 /* This "flush" is akin to sfio's sync in that it handles files in either
2142 PerlIOBuf_flush(PerlIO *f)
2144 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2146 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
2148 /* write() the buffer */
2149 STDCHAR *buf = b->buf;
2151 PerlIO *n = PerlIONext(f);
2154 SSize_t count = PerlIO_write(n,p,b->ptr - p);
2159 else if (count < 0 || PerlIO_error(n))
2161 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2166 b->posn += (p - buf);
2168 else if (PerlIOBase(f)->flags & PERLIO_F_RDBUF)
2170 STDCHAR *buf = PerlIO_get_base(f);
2171 /* Note position change */
2172 b->posn += (b->ptr - buf);
2173 if (b->ptr < b->end)
2175 /* We did not consume all of it */
2176 if (PerlIO_seek(PerlIONext(f),b->posn,SEEK_SET) == 0)
2178 b->posn = PerlIO_tell(PerlIONext(f));
2182 b->ptr = b->end = b->buf;
2183 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF|PERLIO_F_WRBUF);
2184 /* FIXME: Is this right for read case ? */
2185 if (PerlIO_flush(PerlIONext(f)) != 0)
2191 PerlIOBuf_fill(PerlIO *f)
2193 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2194 PerlIO *n = PerlIONext(f);
2196 /* FIXME: doing the down-stream flush is a bad idea if it causes
2197 pre-read data in stdio buffer to be discarded
2198 but this is too simplistic - as it skips _our_ hosekeeping
2199 and breaks tell tests.
2200 if (!(PerlIOBase(f)->flags & PERLIO_F_RDBUF))
2204 if (PerlIO_flush(f) != 0)
2208 PerlIO_get_base(f); /* allocate via vtable */
2210 b->ptr = b->end = b->buf;
2211 if (PerlIO_fast_gets(n))
2213 /* Layer below is also buffered
2214 * We do _NOT_ want to call its ->Read() because that will loop
2215 * till it gets what we asked for which may hang on a pipe etc.
2216 * Instead take anything it has to hand, or ask it to fill _once_.
2218 avail = PerlIO_get_cnt(n);
2221 avail = PerlIO_fill(n);
2223 avail = PerlIO_get_cnt(n);
2226 if (!PerlIO_error(n) && PerlIO_eof(n))
2232 STDCHAR *ptr = PerlIO_get_ptr(n);
2233 SSize_t cnt = avail;
2234 if (avail > b->bufsiz)
2236 Copy(ptr,b->buf,avail,STDCHAR);
2237 PerlIO_set_ptrcnt(n,ptr+avail,cnt-avail);
2242 avail = PerlIO_read(n,b->ptr,b->bufsiz);
2247 PerlIOBase(f)->flags |= PERLIO_F_EOF;
2249 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2252 b->end = b->buf+avail;
2253 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
2258 PerlIOBuf_read(PerlIO *f, void *vbuf, Size_t count)
2260 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2261 STDCHAR *buf = (STDCHAR *) vbuf;
2266 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD))
2270 SSize_t avail = PerlIO_get_cnt(f);
2271 SSize_t take = (count < avail) ? count : avail;
2274 STDCHAR *ptr = PerlIO_get_ptr(f);
2275 Copy(ptr,buf,take,STDCHAR);
2276 PerlIO_set_ptrcnt(f,ptr+take,(avail -= take));
2280 if (count > 0 && avail <= 0)
2282 if (PerlIO_fill(f) != 0)
2286 return (buf - (STDCHAR *) vbuf);
2292 PerlIOBuf_unread(PerlIO *f, const void *vbuf, Size_t count)
2294 const STDCHAR *buf = (const STDCHAR *) vbuf+count;
2295 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2298 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
2304 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF)
2306 avail = (b->ptr - b->buf);
2311 b->end = b->buf + avail;
2313 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
2314 b->posn -= b->bufsiz;
2316 if (avail > (SSize_t) count)
2324 Copy(buf,b->ptr,avail,STDCHAR);
2328 PerlIOBase(f)->flags &= ~ PERLIO_F_EOF;
2335 PerlIOBuf_write(PerlIO *f, const void *vbuf, Size_t count)
2337 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2338 const STDCHAR *buf = (const STDCHAR *) vbuf;
2342 if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
2346 SSize_t avail = b->bufsiz - (b->ptr - b->buf);
2347 if ((SSize_t) count < avail)
2349 PerlIOBase(f)->flags |= PERLIO_F_WRBUF;
2350 if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF)
2370 Copy(buf,b->ptr,avail,STDCHAR);
2377 if (b->ptr >= (b->buf + b->bufsiz))
2380 if (PerlIOBase(f)->flags & PERLIO_F_UNBUF)
2386 PerlIOBuf_seek(PerlIO *f, Off_t offset, int whence)
2389 if ((code = PerlIO_flush(f)) == 0)
2391 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2392 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
2393 code = PerlIO_seek(PerlIONext(f),offset,whence);
2396 b->posn = PerlIO_tell(PerlIONext(f));
2403 PerlIOBuf_tell(PerlIO *f)
2405 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2406 Off_t posn = b->posn;
2408 posn += (b->ptr - b->buf);
2413 PerlIOBuf_close(PerlIO *f)
2416 IV code = PerlIOBase_close(f);
2417 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2418 if (b->buf && b->buf != (STDCHAR *) &b->oneword)
2420 PerlMemShared_free(b->buf);
2423 b->ptr = b->end = b->buf;
2424 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF|PERLIO_F_WRBUF);
2429 PerlIOBuf_setlinebuf(PerlIO *f)
2433 PerlIOBase(f)->flags &= ~PERLIO_F_LINEBUF;
2438 PerlIOBuf_get_ptr(PerlIO *f)
2440 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2447 PerlIOBuf_get_cnt(PerlIO *f)
2449 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2452 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF)
2453 return (b->end - b->ptr);
2458 PerlIOBuf_get_base(PerlIO *f)
2460 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2466 b->buf = PerlMemShared_calloc(b->bufsiz,sizeof(STDCHAR));
2469 b->buf = (STDCHAR *)&b->oneword;
2470 b->bufsiz = sizeof(b->oneword);
2479 PerlIOBuf_bufsiz(PerlIO *f)
2481 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2484 return (b->end - b->buf);
2488 PerlIOBuf_set_ptrcnt(PerlIO *f, STDCHAR *ptr, SSize_t cnt)
2490 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2494 if (PerlIO_get_cnt(f) != cnt || b->ptr < b->buf)
2497 assert(PerlIO_get_cnt(f) == cnt);
2498 assert(b->ptr >= b->buf);
2500 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
2503 PerlIO_funcs PerlIO_perlio = {
2523 PerlIOBase_clearerr,
2524 PerlIOBuf_setlinebuf,
2529 PerlIOBuf_set_ptrcnt,
2532 /*--------------------------------------------------------------------------------------*/
2533 /* Temp layer to hold unread chars when cannot do it any other way */
2536 PerlIOPending_fill(PerlIO *f)
2538 /* Should never happen */
2544 PerlIOPending_close(PerlIO *f)
2546 /* A tad tricky - flush pops us, then we close new top */
2548 return PerlIO_close(f);
2552 PerlIOPending_seek(PerlIO *f, Off_t offset, int whence)
2554 /* A tad tricky - flush pops us, then we seek new top */
2556 return PerlIO_seek(f,offset,whence);
2561 PerlIOPending_flush(PerlIO *f)
2563 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2564 if (b->buf && b->buf != (STDCHAR *) &b->oneword)
2567 PerlMemShared_free(b->buf);
2575 PerlIOPending_set_ptrcnt(PerlIO *f, STDCHAR *ptr, SSize_t cnt)
2583 PerlIOBuf_set_ptrcnt(f,ptr,cnt);
2588 PerlIOPending_pushed(PerlIO *f,const char *mode,const char *arg,STRLEN len)
2590 IV code = PerlIOBase_pushed(f,mode,arg,len);
2591 PerlIOl *l = PerlIOBase(f);
2592 /* Our PerlIO_fast_gets must match what we are pushed on,
2593 or sv_gets() etc. get muddled when it changes mid-string
2596 l->flags = (l->flags & ~(PERLIO_F_FASTGETS|PERLIO_F_UTF8)) |
2597 (PerlIOBase(PerlIONext(f))->flags & (PERLIO_F_FASTGETS|PERLIO_F_UTF8));
2602 PerlIOPending_read(PerlIO *f, void *vbuf, Size_t count)
2604 SSize_t avail = PerlIO_get_cnt(f);
2609 got = PerlIOBuf_read(f,vbuf,avail);
2611 got += PerlIO_read(f,((STDCHAR *) vbuf)+got,count-got);
2616 PerlIO_funcs PerlIO_pending = {
2624 PerlIOPending_pushed,
2631 PerlIOPending_close,
2632 PerlIOPending_flush,
2636 PerlIOBase_clearerr,
2637 PerlIOBuf_setlinebuf,
2642 PerlIOPending_set_ptrcnt,
2647 /*--------------------------------------------------------------------------------------*/
2648 /* crlf - translation
2649 On read translate CR,LF to "\n" we do this by overriding ptr/cnt entries
2650 to hand back a line at a time and keeping a record of which nl we "lied" about.
2651 On write translate "\n" to CR,LF
2656 PerlIOBuf base; /* PerlIOBuf stuff */
2657 STDCHAR *nl; /* Position of crlf we "lied" about in the buffer */
2661 PerlIOCrlf_pushed(PerlIO *f, const char *mode,const char *arg,STRLEN len)
2664 PerlIOBase(f)->flags |= PERLIO_F_CRLF;
2665 code = PerlIOBuf_pushed(f,mode,arg,len);
2667 PerlIO_debug("PerlIOCrlf_pushed f=%p %s %s fl=%08"UVxf"\n",
2668 f,PerlIOBase(f)->tab->name,(mode) ? mode : "(Null)",
2669 PerlIOBase(f)->flags);
2676 PerlIOCrlf_unread(PerlIO *f, const void *vbuf, Size_t count)
2678 PerlIOCrlf *c = PerlIOSelf(f,PerlIOCrlf);
2684 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF))
2685 return PerlIOBuf_unread(f,vbuf,count);
2688 const STDCHAR *buf = (const STDCHAR *) vbuf+count;
2689 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2691 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
2697 if (!(PerlIOBase(f)->flags & PERLIO_F_RDBUF))
2699 b->end = b->ptr = b->buf + b->bufsiz;
2700 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
2701 b->posn -= b->bufsiz;
2703 while (count > 0 && b->ptr > b->buf)
2708 if (b->ptr - 2 >= b->buf)
2734 PerlIOCrlf_get_cnt(PerlIO *f)
2736 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2739 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF)
2741 PerlIOCrlf *c = PerlIOSelf(f,PerlIOCrlf);
2742 if ((PerlIOBase(f)->flags & PERLIO_F_CRLF) && !c->nl)
2744 STDCHAR *nl = b->ptr;
2746 while (nl < b->end && *nl != 0xd)
2748 if (nl < b->end && *nl == 0xd)
2760 /* Not CR,LF but just CR */
2767 /* Blast - found CR as last char in buffer */
2770 /* They may not care, defer work as long as possible */
2771 return (nl - b->ptr);
2777 b->ptr++; /* say we have read it as far as flush() is concerned */
2778 b->buf++; /* Leave space an front of buffer */
2779 b->bufsiz--; /* Buffer is thus smaller */
2780 code = PerlIO_fill(f); /* Fetch some more */
2781 b->bufsiz++; /* Restore size for next time */
2782 b->buf--; /* Point at space */
2783 b->ptr = nl = b->buf; /* Which is what we hand off */
2784 b->posn--; /* Buffer starts here */
2785 *nl = 0xd; /* Fill in the CR */
2787 goto test; /* fill() call worked */
2788 /* CR at EOF - just fall through */
2793 return (((c->nl) ? (c->nl+1) : b->end) - b->ptr);
2799 PerlIOCrlf_set_ptrcnt(PerlIO *f, STDCHAR *ptr, SSize_t cnt)
2801 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2802 PerlIOCrlf *c = PerlIOSelf(f,PerlIOCrlf);
2803 IV flags = PerlIOBase(f)->flags;
2813 if ((flags & PERLIO_F_CRLF) && ptr > b->buf && ptr[-1] == 0xd)
2820 /* Test code - delete when it works ... */
2827 if ((flags & PERLIO_F_CRLF) && chk > b->buf && chk[-1] == 0xd)
2835 Perl_croak(aTHX_ "ptr wrong %p != %p fl=%08"UVxf" nl=%p e=%p for %d",
2836 ptr, chk, flags, c->nl, b->end, cnt);
2843 /* They have taken what we lied about */
2850 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
2854 PerlIOCrlf_write(PerlIO *f, const void *vbuf, Size_t count)
2856 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF))
2857 return PerlIOBuf_write(f,vbuf,count);
2860 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2861 const STDCHAR *buf = (const STDCHAR *) vbuf;
2862 const STDCHAR *ebuf = buf+count;
2865 if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
2869 STDCHAR *eptr = b->buf+b->bufsiz;
2870 PerlIOBase(f)->flags |= PERLIO_F_WRBUF;
2871 while (buf < ebuf && b->ptr < eptr)
2875 if ((b->ptr + 2) > eptr)
2877 /* Not room for both */
2883 *(b->ptr)++ = 0xd; /* CR */
2884 *(b->ptr)++ = 0xa; /* LF */
2886 if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF)
2905 if (PerlIOBase(f)->flags & PERLIO_F_UNBUF)
2907 return (buf - (STDCHAR *) vbuf);
2912 PerlIOCrlf_flush(PerlIO *f)
2914 PerlIOCrlf *c = PerlIOSelf(f,PerlIOCrlf);
2920 return PerlIOBuf_flush(f);
2923 PerlIO_funcs PerlIO_crlf = {
2926 PERLIO_K_BUFFERED|PERLIO_K_CANCRLF,
2932 PerlIOBase_noop_ok, /* popped */
2933 PerlIOBuf_read, /* generic read works with ptr/cnt lies ... */
2934 PerlIOCrlf_unread, /* Put CR,LF in buffer for each '\n' */
2935 PerlIOCrlf_write, /* Put CR,LF in buffer for each '\n' */
2943 PerlIOBase_clearerr,
2944 PerlIOBuf_setlinebuf,
2949 PerlIOCrlf_set_ptrcnt,
2953 /*--------------------------------------------------------------------------------------*/
2954 /* mmap as "buffer" layer */
2958 PerlIOBuf base; /* PerlIOBuf stuff */
2959 Mmap_t mptr; /* Mapped address */
2960 Size_t len; /* mapped length */
2961 STDCHAR *bbuf; /* malloced buffer if map fails */
2964 static size_t page_size = 0;
2967 PerlIOMmap_map(PerlIO *f)
2970 PerlIOMmap *m = PerlIOSelf(f,PerlIOMmap);
2971 PerlIOBuf *b = &m->base;
2972 IV flags = PerlIOBase(f)->flags;
2976 if (flags & PERLIO_F_CANREAD)
2978 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2979 int fd = PerlIO_fileno(f);
2981 code = fstat(fd,&st);
2982 if (code == 0 && S_ISREG(st.st_mode))
2984 SSize_t len = st.st_size - b->posn;
2989 #if defined(HAS_SYSCONF) && (defined(_SC_PAGESIZE) || defined(_SC_PAGE_SIZE))
2991 SETERRNO(0,SS$_NORMAL);
2992 # ifdef _SC_PAGESIZE
2993 page_size = sysconf(_SC_PAGESIZE);
2995 page_size = sysconf(_SC_PAGE_SIZE);
2997 if ((long)page_size < 0) {
3002 (void)SvUPGRADE(error, SVt_PV);
3003 msg = SvPVx(error, n_a);
3004 Perl_croak(aTHX_ "panic: sysconf: %s", msg);
3007 Perl_croak(aTHX_ "panic: sysconf: pagesize unknown");
3011 # ifdef HAS_GETPAGESIZE
3012 page_size = getpagesize();
3014 # if defined(I_SYS_PARAM) && defined(PAGESIZE)
3015 page_size = PAGESIZE; /* compiletime, bad */
3019 if ((IV)page_size <= 0)
3020 Perl_croak(aTHX_ "panic: bad pagesize %"IVdf, (IV)page_size);
3024 /* This is a hack - should never happen - open should have set it ! */
3025 b->posn = PerlIO_tell(PerlIONext(f));
3027 posn = (b->posn / page_size) * page_size;
3028 len = st.st_size - posn;
3029 m->mptr = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, posn);
3030 if (m->mptr && m->mptr != (Mmap_t) -1)
3032 #if 0 && defined(HAS_MADVISE) && defined(MADV_SEQUENTIAL)
3033 madvise(m->mptr, len, MADV_SEQUENTIAL);
3035 #if 0 && defined(HAS_MADVISE) && defined(MADV_WILLNEED)
3036 madvise(m->mptr, len, MADV_WILLNEED);
3038 PerlIOBase(f)->flags = (flags & ~PERLIO_F_EOF) | PERLIO_F_RDBUF;
3039 b->end = ((STDCHAR *)m->mptr) + len;
3040 b->buf = ((STDCHAR *)m->mptr) + (b->posn - posn);
3051 PerlIOBase(f)->flags = flags | PERLIO_F_EOF | PERLIO_F_RDBUF;
3053 b->ptr = b->end = b->ptr;
3062 PerlIOMmap_unmap(PerlIO *f)
3064 PerlIOMmap *m = PerlIOSelf(f,PerlIOMmap);
3065 PerlIOBuf *b = &m->base;
3071 code = munmap(m->mptr, m->len);
3075 if (PerlIO_seek(PerlIONext(f),b->posn,SEEK_SET) != 0)
3078 b->ptr = b->end = b->buf;
3079 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF|PERLIO_F_WRBUF);
3085 PerlIOMmap_get_base(PerlIO *f)
3087 PerlIOMmap *m = PerlIOSelf(f,PerlIOMmap);
3088 PerlIOBuf *b = &m->base;
3089 if (b->buf && (PerlIOBase(f)->flags & PERLIO_F_RDBUF))
3091 /* Already have a readbuffer in progress */
3096 /* We have a write buffer or flushed PerlIOBuf read buffer */
3097 m->bbuf = b->buf; /* save it in case we need it again */
3098 b->buf = NULL; /* Clear to trigger below */
3102 PerlIOMmap_map(f); /* Try and map it */
3105 /* Map did not work - recover PerlIOBuf buffer if we have one */
3109 b->ptr = b->end = b->buf;
3112 return PerlIOBuf_get_base(f);
3116 PerlIOMmap_unread(PerlIO *f, const void *vbuf, Size_t count)
3118 PerlIOMmap *m = PerlIOSelf(f,PerlIOMmap);
3119 PerlIOBuf *b = &m->base;
3120 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
3122 if (b->ptr && (b->ptr - count) >= b->buf && memEQ(b->ptr - count,vbuf,count))
3125 PerlIOBase(f)->flags &= ~ PERLIO_F_EOF;
3130 /* Loose the unwritable mapped buffer */
3132 /* If flush took the "buffer" see if we have one from before */
3133 if (!b->buf && m->bbuf)
3137 PerlIOBuf_get_base(f);
3141 return PerlIOBuf_unread(f,vbuf,count);
3145 PerlIOMmap_write(PerlIO *f, const void *vbuf, Size_t count)
3147 PerlIOMmap *m = PerlIOSelf(f,PerlIOMmap);
3148 PerlIOBuf *b = &m->base;
3149 if (!b->buf || !(PerlIOBase(f)->flags & PERLIO_F_WRBUF))
3151 /* No, or wrong sort of, buffer */
3154 if (PerlIOMmap_unmap(f) != 0)
3157 /* If unmap took the "buffer" see if we have one from before */
3158 if (!b->buf && m->bbuf)
3162 PerlIOBuf_get_base(f);
3166 return PerlIOBuf_write(f,vbuf,count);
3170 PerlIOMmap_flush(PerlIO *f)
3172 PerlIOMmap *m = PerlIOSelf(f,PerlIOMmap);
3173 PerlIOBuf *b = &m->base;
3174 IV code = PerlIOBuf_flush(f);
3175 /* Now we are "synced" at PerlIOBuf level */
3180 /* Unmap the buffer */
3181 if (PerlIOMmap_unmap(f) != 0)
3186 /* We seem to have a PerlIOBuf buffer which was not mapped
3187 * remember it in case we need one later
3196 PerlIOMmap_fill(PerlIO *f)
3198 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
3199 IV code = PerlIO_flush(f);
3200 if (code == 0 && !b->buf)
3202 code = PerlIOMmap_map(f);
3204 if (code == 0 && !(PerlIOBase(f)->flags & PERLIO_F_RDBUF))
3206 code = PerlIOBuf_fill(f);
3212 PerlIOMmap_close(PerlIO *f)
3214 PerlIOMmap *m = PerlIOSelf(f,PerlIOMmap);
3215 PerlIOBuf *b = &m->base;
3216 IV code = PerlIO_flush(f);
3221 b->ptr = b->end = b->buf;
3223 if (PerlIOBuf_close(f) != 0)
3229 PerlIO_funcs PerlIO_mmap = {
3249 PerlIOBase_clearerr,
3250 PerlIOBuf_setlinebuf,
3251 PerlIOMmap_get_base,
3255 PerlIOBuf_set_ptrcnt,
3258 #endif /* HAS_MMAP */
3266 atexit(&PerlIO_cleanup);
3278 PerlIO_stdstreams();
3282 #undef PerlIO_stdout
3287 PerlIO_stdstreams();
3291 #undef PerlIO_stderr
3296 PerlIO_stdstreams();
3300 /*--------------------------------------------------------------------------------------*/
3302 #undef PerlIO_getname
3304 PerlIO_getname(PerlIO *f, char *buf)
3307 Perl_croak(aTHX_ "Don't know how to get file name");
3312 /*--------------------------------------------------------------------------------------*/
3313 /* Functions which can be called on any kind of PerlIO implemented
3319 PerlIO_getc(PerlIO *f)
3322 SSize_t count = PerlIO_read(f,buf,1);
3325 return (unsigned char) buf[0];
3330 #undef PerlIO_ungetc
3332 PerlIO_ungetc(PerlIO *f, int ch)
3337 if (PerlIO_unread(f,&buf,1) == 1)
3345 PerlIO_putc(PerlIO *f, int ch)
3348 return PerlIO_write(f,&buf,1);
3353 PerlIO_puts(PerlIO *f, const char *s)
3355 STRLEN len = strlen(s);
3356 return PerlIO_write(f,s,len);
3359 #undef PerlIO_rewind
3361 PerlIO_rewind(PerlIO *f)
3363 PerlIO_seek(f,(Off_t)0,SEEK_SET);
3367 #undef PerlIO_vprintf
3369 PerlIO_vprintf(PerlIO *f, const char *fmt, va_list ap)
3372 SV *sv = newSVpvn("",0);
3377 Perl_va_copy(ap, apc);
3378 sv_vcatpvf(sv, fmt, &apc);
3380 sv_vcatpvf(sv, fmt, &ap);
3383 return PerlIO_write(f,s,len);
3386 #undef PerlIO_printf
3388 PerlIO_printf(PerlIO *f,const char *fmt,...)
3393 result = PerlIO_vprintf(f,fmt,ap);
3398 #undef PerlIO_stdoutf
3400 PerlIO_stdoutf(const char *fmt,...)
3405 result = PerlIO_vprintf(PerlIO_stdout(),fmt,ap);
3410 #undef PerlIO_tmpfile
3412 PerlIO_tmpfile(void)
3414 /* I have no idea how portable mkstemp() is ... */
3415 #if defined(WIN32) || !defined(HAVE_MKSTEMP)
3418 FILE *stdio = PerlSIO_tmpfile();
3421 PerlIOStdio *s = PerlIOSelf(PerlIO_push(f = PerlIO_allocate(aTHX),&PerlIO_stdio,"w+",Nullch,0),PerlIOStdio);
3427 SV *sv = newSVpv("/tmp/PerlIO_XXXXXX",0);
3428 int fd = mkstemp(SvPVX(sv));
3432 f = PerlIO_fdopen(fd,"w+");
3435 PerlIOBase(f)->flags |= PERLIO_F_TEMP;
3437 PerlLIO_unlink(SvPVX(sv));
3447 #endif /* USE_SFIO */
3448 #endif /* PERLIO_IS_STDIO */
3450 /*======================================================================================*/
3451 /* Now some functions in terms of above which may be needed even if
3452 we are not in true PerlIO mode
3456 #undef PerlIO_setpos
3458 PerlIO_setpos(PerlIO *f, SV *pos)
3464 Off_t *posn = (Off_t *) SvPV(pos,len);
3465 if (f && len == sizeof(Off_t))
3466 return PerlIO_seek(f,*posn,SEEK_SET);
3472 #undef PerlIO_setpos
3474 PerlIO_setpos(PerlIO *f, SV *pos)
3480 Fpos_t *fpos = (Fpos_t *) SvPV(pos,len);
3481 if (f && len == sizeof(Fpos_t))
3483 #if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64)
3484 return fsetpos64(f, fpos);
3486 return fsetpos(f, fpos);
3496 #undef PerlIO_getpos
3498 PerlIO_getpos(PerlIO *f, SV *pos)
3501 Off_t posn = PerlIO_tell(f);
3502 sv_setpvn(pos,(char *)&posn,sizeof(posn));
3503 return (posn == (Off_t)-1) ? -1 : 0;
3506 #undef PerlIO_getpos
3508 PerlIO_getpos(PerlIO *f, SV *pos)
3513 #if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64)
3514 code = fgetpos64(f, &fpos);
3516 code = fgetpos(f, &fpos);
3518 sv_setpvn(pos,(char *)&fpos,sizeof(fpos));
3523 #if (defined(PERLIO_IS_STDIO) || !defined(USE_SFIO)) && !defined(HAS_VPRINTF)
3526 vprintf(char *pat, char *args)
3528 _doprnt(pat, args, stdout);
3529 return 0; /* wrong, but perl doesn't use the return value */
3533 vfprintf(FILE *fd, char *pat, char *args)
3535 _doprnt(pat, args, fd);
3536 return 0; /* wrong, but perl doesn't use the return value */
3541 #ifndef PerlIO_vsprintf
3543 PerlIO_vsprintf(char *s, int n, const char *fmt, va_list ap)
3545 int val = vsprintf(s, fmt, ap);
3548 if (strlen(s) >= (STRLEN)n)
3551 (void)PerlIO_puts(Perl_error_log,
3552 "panic: sprintf overflow - memory corrupted!\n");
3560 #ifndef PerlIO_sprintf
3562 PerlIO_sprintf(char *s, int n, const char *fmt,...)
3567 result = PerlIO_vsprintf(s, n, fmt, ap);