3 * Copyright (c) 1996-2000, 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);
286 (*l->tab->Popped)(f);
288 PerlMemShared_free(l);
292 /*--------------------------------------------------------------------------------------*/
293 /* XS Interface for perl code */
299 char *s = GvNAME(gv);
300 STRLEN l = GvNAMELEN(gv);
301 PerlIO_debug("%.*s\n",(int) l,s);
305 XS(XS_perlio_unimport)
309 char *s = GvNAME(gv);
310 STRLEN l = GvNAMELEN(gv);
311 PerlIO_debug("%.*s\n",(int) l,s);
316 PerlIO_find_layer(const char *name, STRLEN len)
321 if ((SSize_t) len <= 0)
323 svp = hv_fetch(PerlIO_layer_hv,name,len,0);
324 if (svp && (sv = *svp) && SvROK(sv))
331 perlio_mg_set(pTHX_ SV *sv, MAGIC *mg)
335 IO *io = GvIOn((GV *)SvRV(sv));
336 PerlIO *ifp = IoIFP(io);
337 PerlIO *ofp = IoOFP(io);
338 AV *av = (AV *) mg->mg_obj;
339 Perl_warn(aTHX_ "set %"SVf" %p %p %p",sv,io,ifp,ofp);
345 perlio_mg_get(pTHX_ SV *sv, MAGIC *mg)
349 IO *io = GvIOn((GV *)SvRV(sv));
350 PerlIO *ifp = IoIFP(io);
351 PerlIO *ofp = IoOFP(io);
352 AV *av = (AV *) mg->mg_obj;
353 Perl_warn(aTHX_ "get %"SVf" %p %p %p",sv,io,ifp,ofp);
359 perlio_mg_clear(pTHX_ SV *sv, MAGIC *mg)
361 Perl_warn(aTHX_ "clear %"SVf,sv);
366 perlio_mg_free(pTHX_ SV *sv, MAGIC *mg)
368 Perl_warn(aTHX_ "free %"SVf,sv);
372 MGVTBL perlio_vtab = {
380 XS(XS_io_MODIFY_SCALAR_ATTRIBUTES)
383 SV *sv = SvRV(ST(1));
388 sv_magic(sv, (SV *)av, '~', NULL, 0);
390 mg = mg_find(sv,'~');
391 mg->mg_virtual = &perlio_vtab;
393 Perl_warn(aTHX_ "attrib %"SVf,sv);
394 for (i=2; i < items; i++)
397 const char *name = SvPV(ST(i),len);
398 SV *layer = PerlIO_find_layer(name,len);
401 av_push(av,SvREFCNT_inc(layer));
414 PerlIO_define_layer(PerlIO_funcs *tab)
417 HV *stash = gv_stashpv("perlio::Layer", TRUE);
418 SV *sv = sv_bless(newRV_noinc(newSViv(PTR2IV(tab))),stash);
419 hv_store(PerlIO_layer_hv,tab->name,strlen(tab->name),sv,0);
420 PerlIO_debug("define %s %p\n",tab->name,tab);
424 PerlIO_default_buffer(pTHX)
426 PerlIO_funcs *tab = &PerlIO_perlio;
427 if (O_BINARY != O_TEXT)
433 if (PerlIO_stdio.Set_ptrcnt)
438 PerlIO_debug("Pushing %s\n",tab->name);
439 av_push(PerlIO_layer_av,SvREFCNT_inc(PerlIO_find_layer(tab->name,0)));
445 PerlIO_default_layer(I32 n)
450 PerlIO_funcs *tab = &PerlIO_stdio;
452 if (!PerlIO_layer_hv)
454 const char *s = PerlEnv_getenv("PERLIO");
455 newXS("perlio::import",XS_perlio_import,__FILE__);
456 newXS("perlio::unimport",XS_perlio_unimport,__FILE__);
458 newXS("io::MODIFY_SCALAR_ATTRIBUTES",XS_io_MODIFY_SCALAR_ATTRIBUTES,__FILE__);
460 PerlIO_layer_hv = get_hv("open::layers",GV_ADD|GV_ADDMULTI);
461 PerlIO_layer_av = get_av("open::layers",GV_ADD|GV_ADDMULTI);
462 PerlIO_define_layer(&PerlIO_raw);
463 PerlIO_define_layer(&PerlIO_unix);
464 PerlIO_define_layer(&PerlIO_perlio);
465 PerlIO_define_layer(&PerlIO_stdio);
466 PerlIO_define_layer(&PerlIO_crlf);
468 PerlIO_define_layer(&PerlIO_mmap);
470 PerlIO_define_layer(&PerlIO_utf8);
471 av_push(PerlIO_layer_av,SvREFCNT_inc(PerlIO_find_layer(PerlIO_unix.name,0)));
477 while (*s && isSPACE((unsigned char)*s))
483 while (*e && !isSPACE((unsigned char)*e))
487 layer = PerlIO_find_layer(s,e-s);
490 PerlIO_funcs *tab = INT2PTR(PerlIO_funcs *, SvIV(SvRV(layer)));
491 if ((tab->kind & PERLIO_K_DUMMY) && (tab->kind & PERLIO_K_BUFFERED))
494 PerlIO_default_buffer(aTHX);
496 PerlIO_debug("Pushing %.*s\n",(e-s),s);
497 av_push(PerlIO_layer_av,SvREFCNT_inc(layer));
498 buffered |= (tab->kind & PERLIO_K_BUFFERED);
501 Perl_warn(aTHX_ "perlio: unknown layer \"%.*s\"",(e-s),s);
507 len = av_len(PerlIO_layer_av);
510 PerlIO_default_buffer(aTHX);
511 len = av_len(PerlIO_layer_av);
515 svp = av_fetch(PerlIO_layer_av,n,0);
516 if (svp && (layer = *svp) && SvROK(layer) && SvIOK((layer = SvRV(layer))))
518 tab = INT2PTR(PerlIO_funcs *, SvIV(layer));
520 /* PerlIO_debug("Layer %d is %s\n",n,tab->name); */
524 #define PerlIO_default_top() PerlIO_default_layer(-1)
525 #define PerlIO_default_btm() PerlIO_default_layer(0)
533 PerlIO_allocate(aTHX);
534 PerlIO_fdopen(0,"Ir" PERLIO_STDTEXT);
535 PerlIO_fdopen(1,"Iw" PERLIO_STDTEXT);
536 PerlIO_fdopen(2,"Iw" PERLIO_STDTEXT);
541 PerlIO_push(PerlIO *f,PerlIO_funcs *tab,const char *mode,const char *arg,STRLEN len)
545 l = PerlMemShared_calloc(tab->size,sizeof(char));
548 Zero(l,tab->size,char);
552 PerlIO_debug("PerlIO_push f=%p %s %s\n",f,tab->name,(mode) ? mode : "(Null)");
553 if ((*l->tab->Pushed)(f,mode,arg,len) != 0)
563 PerlIOUtf8_pushed(PerlIO *f, const char *mode, const char *arg, STRLEN len)
568 PerlIOBase(f)->flags |= PERLIO_F_UTF8;
575 PerlIORaw_pushed(PerlIO *f, const char *mode, const char *arg, STRLEN len)
577 /* Pop back to bottom layer */
581 while (PerlIONext(f))
591 PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names)
595 const char *s = names;
605 const char *as = Nullch;
606 const char *ae = Nullch;
608 while (*e && *e != ':' && !isSPACE(*e))
618 if (as && --count == 0)
625 if ((e - s) == 3 && strncmp(s,"raw",3) == 0)
627 /* Pop back to bottom layer */
631 while (PerlIONext(f))
637 else if ((e - s) == 4 && strncmp(s,"utf8",4) == 0)
639 PerlIOBase(f)->flags |= PERLIO_F_UTF8;
641 else if ((e - s) == 5 && strncmp(s,"bytes",5) == 0)
643 PerlIOBase(f)->flags &= ~PERLIO_F_UTF8;
647 STRLEN len = ((as) ? as : e)-s;
648 SV *layer = PerlIO_find_layer(s,len);
651 PerlIO_funcs *tab = INT2PTR(PerlIO_funcs *, SvIV(SvRV(layer)));
654 if (as && (ae == Nullch)) {
656 Perl_warn(aTHX_ "perlio: argument list not closed for layer \"%.*s\"",(int)(e - s),s);
658 len = (as) ? (ae-(as++)-1) : 0;
659 if (!PerlIO_push(f,tab,mode,as,len))
664 Perl_warn(aTHX_ "perlio: unknown layer \"%.*s\"",(int)len,s);
676 /*--------------------------------------------------------------------------------------*/
677 /* Given the abstraction above the public API functions */
680 PerlIO_binmode(pTHX_ PerlIO *f, int iotype, int mode, const char *names)
682 PerlIO_debug("PerlIO_binmode f=%p %s %c %x %s\n",
683 f,PerlIOBase(f)->tab->name,iotype,mode, (names) ? names : "(Null)");
684 if (!names && (O_TEXT != O_BINARY && (mode & O_BINARY)))
690 if (PerlIOBase(top)->tab == &PerlIO_crlf)
693 PerlIOBase(top)->flags &= ~PERLIO_F_CRLF;
696 top = PerlIONext(top);
699 return PerlIO_apply_layers(aTHX_ f, NULL, names) == 0 ? TRUE : FALSE;
704 PerlIO__close(PerlIO *f)
706 return (*PerlIOBase(f)->tab->Close)(f);
709 #undef PerlIO_fdupopen
711 PerlIO_fdupopen(pTHX_ PerlIO *f)
714 int fd = PerlLIO_dup(PerlIO_fileno(f));
715 PerlIO *new = PerlIO_fdopen(fd,PerlIO_modestr(f,buf));
718 Off_t posn = PerlIO_tell(f);
719 PerlIO_seek(new,posn,SEEK_SET);
726 PerlIO_close(PerlIO *f)
728 int code = (*PerlIOBase(f)->tab->Close)(f);
738 PerlIO_fileno(PerlIO *f)
740 return (*PerlIOBase(f)->tab->Fileno)(f);
747 PerlIO_fdopen(int fd, const char *mode)
749 PerlIO_funcs *tab = PerlIO_default_top();
752 return (*tab->Fdopen)(tab,fd,mode);
757 PerlIO_open(const char *path, const char *mode)
759 PerlIO_funcs *tab = PerlIO_default_top();
762 return (*tab->Open)(tab,path,mode);
767 PerlIO_reopen(const char *path, const char *mode, PerlIO *f)
772 if ((*PerlIOBase(f)->tab->Reopen)(path,mode,f) == 0)
774 if ((*PerlIOBase(f)->tab->Pushed)(f,mode,Nullch,0) == 0)
780 return PerlIO_open(path,mode);
785 PerlIO_read(PerlIO *f, void *vbuf, Size_t count)
787 return (*PerlIOBase(f)->tab->Read)(f,vbuf,count);
792 PerlIO_unread(PerlIO *f, const void *vbuf, Size_t count)
794 return (*PerlIOBase(f)->tab->Unread)(f,vbuf,count);
799 PerlIO_write(PerlIO *f, const void *vbuf, Size_t count)
801 return (*PerlIOBase(f)->tab->Write)(f,vbuf,count);
806 PerlIO_seek(PerlIO *f, Off_t offset, int whence)
808 return (*PerlIOBase(f)->tab->Seek)(f,offset,whence);
813 PerlIO_tell(PerlIO *f)
815 return (*PerlIOBase(f)->tab->Tell)(f);
820 PerlIO_flush(PerlIO *f)
824 return (*PerlIOBase(f)->tab->Flush)(f);
828 PerlIO **table = &_perlio;
833 table = (PerlIO **)(f++);
834 for (i=1; i < PERLIO_TABLE_SIZE; i++)
836 if (*f && PerlIO_flush(f) != 0)
847 PerlIO_fill(PerlIO *f)
849 return (*PerlIOBase(f)->tab->Fill)(f);
854 PerlIO_isutf8(PerlIO *f)
856 return (PerlIOBase(f)->flags & PERLIO_F_UTF8) != 0;
861 PerlIO_eof(PerlIO *f)
863 return (*PerlIOBase(f)->tab->Eof)(f);
868 PerlIO_error(PerlIO *f)
870 return (*PerlIOBase(f)->tab->Error)(f);
873 #undef PerlIO_clearerr
875 PerlIO_clearerr(PerlIO *f)
878 (*PerlIOBase(f)->tab->Clearerr)(f);
881 #undef PerlIO_setlinebuf
883 PerlIO_setlinebuf(PerlIO *f)
885 (*PerlIOBase(f)->tab->Setlinebuf)(f);
888 #undef PerlIO_has_base
890 PerlIO_has_base(PerlIO *f)
894 return (PerlIOBase(f)->tab->Get_base != NULL);
899 #undef PerlIO_fast_gets
901 PerlIO_fast_gets(PerlIO *f)
903 if (f && *f && (PerlIOBase(f)->flags & PERLIO_F_FASTGETS))
905 PerlIO_funcs *tab = PerlIOBase(f)->tab;
906 return (tab->Set_ptrcnt != NULL);
911 #undef PerlIO_has_cntptr
913 PerlIO_has_cntptr(PerlIO *f)
917 PerlIO_funcs *tab = PerlIOBase(f)->tab;
918 return (tab->Get_ptr != NULL && tab->Get_cnt != NULL);
923 #undef PerlIO_canset_cnt
925 PerlIO_canset_cnt(PerlIO *f)
929 PerlIOl *l = PerlIOBase(f);
930 return (l->tab->Set_ptrcnt != NULL);
935 #undef PerlIO_get_base
937 PerlIO_get_base(PerlIO *f)
939 return (*PerlIOBase(f)->tab->Get_base)(f);
942 #undef PerlIO_get_bufsiz
944 PerlIO_get_bufsiz(PerlIO *f)
946 return (*PerlIOBase(f)->tab->Get_bufsiz)(f);
949 #undef PerlIO_get_ptr
951 PerlIO_get_ptr(PerlIO *f)
953 PerlIO_funcs *tab = PerlIOBase(f)->tab;
954 if (tab->Get_ptr == NULL)
956 return (*tab->Get_ptr)(f);
959 #undef PerlIO_get_cnt
961 PerlIO_get_cnt(PerlIO *f)
963 PerlIO_funcs *tab = PerlIOBase(f)->tab;
964 if (tab->Get_cnt == NULL)
966 return (*tab->Get_cnt)(f);
969 #undef PerlIO_set_cnt
971 PerlIO_set_cnt(PerlIO *f,int cnt)
973 (*PerlIOBase(f)->tab->Set_ptrcnt)(f,NULL,cnt);
976 #undef PerlIO_set_ptrcnt
978 PerlIO_set_ptrcnt(PerlIO *f, STDCHAR *ptr, int cnt)
980 PerlIO_funcs *tab = PerlIOBase(f)->tab;
981 if (tab->Set_ptrcnt == NULL)
984 Perl_croak(aTHX_ "PerlIO buffer snooping abuse");
986 (*PerlIOBase(f)->tab->Set_ptrcnt)(f,ptr,cnt);
989 /*--------------------------------------------------------------------------------------*/
990 /* utf8 and raw dummy layers */
993 PerlIOUtf8_fdopen(PerlIO_funcs *self, int fd,const char *mode)
995 PerlIO_funcs *tab = PerlIO_default_layer(-2);
996 PerlIO *f = (*tab->Fdopen)(tab,fd,mode);
999 PerlIOBase(f)->flags |= PERLIO_F_UTF8;
1005 PerlIOUtf8_open(PerlIO_funcs *self, const char *path,const char *mode)
1007 PerlIO_funcs *tab = PerlIO_default_layer(-2);
1008 PerlIO *f = (*tab->Open)(tab,path,mode);
1011 PerlIOBase(f)->flags |= PERLIO_F_UTF8;
1016 PerlIO_funcs PerlIO_utf8 = {
1019 PERLIO_K_DUMMY|PERLIO_K_BUFFERED,
1038 NULL, /* get_base */
1039 NULL, /* get_bufsiz */
1042 NULL, /* set_ptrcnt */
1046 PerlIORaw_fdopen(PerlIO_funcs *self, int fd,const char *mode)
1048 PerlIO_funcs *tab = PerlIO_default_layer(0);
1049 return (*tab->Fdopen)(tab,fd,mode);
1053 PerlIORaw_open(PerlIO_funcs *self, const char *path,const char *mode)
1055 PerlIO_funcs *tab = PerlIO_default_layer(0);
1056 return (*tab->Open)(tab,path,mode);
1059 PerlIO_funcs PerlIO_raw = {
1062 PERLIO_K_DUMMY|PERLIO_K_RAW,
1081 NULL, /* get_base */
1082 NULL, /* get_bufsiz */
1085 NULL, /* set_ptrcnt */
1087 /*--------------------------------------------------------------------------------------*/
1088 /*--------------------------------------------------------------------------------------*/
1089 /* "Methods" of the "base class" */
1092 PerlIOBase_fileno(PerlIO *f)
1094 return PerlIO_fileno(PerlIONext(f));
1098 PerlIO_modestr(PerlIO *f,char *buf)
1101 IV flags = PerlIOBase(f)->flags;
1102 if (flags & PERLIO_F_APPEND)
1105 if (flags & PERLIO_F_CANREAD)
1110 else if (flags & PERLIO_F_CANREAD)
1113 if (flags & PERLIO_F_CANWRITE)
1116 else if (flags & PERLIO_F_CANWRITE)
1119 if (flags & PERLIO_F_CANREAD)
1124 #if O_TEXT != O_BINARY
1125 if (!(flags & PERLIO_F_CRLF))
1133 PerlIOBase_pushed(PerlIO *f, const char *mode, const char *arg, STRLEN len)
1135 PerlIOl *l = PerlIOBase(f);
1136 const char *omode = mode;
1138 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1139 l->flags &= ~(PERLIO_F_CANREAD|PERLIO_F_CANWRITE|
1140 PERLIO_F_TRUNCATE|PERLIO_F_APPEND);
1141 if (tab->Set_ptrcnt != NULL)
1142 l->flags |= PERLIO_F_FASTGETS;
1148 l->flags |= PERLIO_F_CANREAD;
1151 l->flags |= PERLIO_F_APPEND|PERLIO_F_CANWRITE;
1154 l->flags |= PERLIO_F_TRUNCATE|PERLIO_F_CANWRITE;
1165 l->flags |= PERLIO_F_CANREAD|PERLIO_F_CANWRITE;
1168 l->flags &= ~PERLIO_F_CRLF;
1171 l->flags |= PERLIO_F_CRLF;
1183 l->flags |= l->next->flags &
1184 (PERLIO_F_CANREAD|PERLIO_F_CANWRITE|PERLIO_F_TRUNCATE|PERLIO_F_APPEND);
1188 PerlIO_debug("PerlIOBase_pushed f=%p %s %s fl=%08"UVxf" (%s)\n",
1189 f,PerlIOBase(f)->tab->name,(omode) ? omode : "(Null)",
1190 l->flags,PerlIO_modestr(f,temp));
1196 PerlIOBase_popped(PerlIO *f)
1202 PerlIOBase_unread(PerlIO *f, const void *vbuf, Size_t count)
1204 Off_t old = PerlIO_tell(f);
1206 PerlIO_push(f,&PerlIO_pending,"r",Nullch,0);
1207 done = PerlIOBuf_unread(f,vbuf,count);
1208 PerlIOSelf(f,PerlIOBuf)->posn = old - done;
1213 PerlIOBase_noop_ok(PerlIO *f)
1219 PerlIOBase_noop_fail(PerlIO *f)
1225 PerlIOBase_close(PerlIO *f)
1228 PerlIO *n = PerlIONext(f);
1229 if (PerlIO_flush(f) != 0)
1231 if (n && (*PerlIOBase(n)->tab->Close)(n) != 0)
1233 PerlIOBase(f)->flags &= ~(PERLIO_F_CANREAD|PERLIO_F_CANWRITE|PERLIO_F_OPEN);
1238 PerlIOBase_eof(PerlIO *f)
1242 return (PerlIOBase(f)->flags & PERLIO_F_EOF) != 0;
1248 PerlIOBase_error(PerlIO *f)
1252 return (PerlIOBase(f)->flags & PERLIO_F_ERROR) != 0;
1258 PerlIOBase_clearerr(PerlIO *f)
1262 PerlIO *n = PerlIONext(f);
1263 PerlIOBase(f)->flags &= ~(PERLIO_F_ERROR|PERLIO_F_EOF);
1270 PerlIOBase_setlinebuf(PerlIO *f)
1275 /*--------------------------------------------------------------------------------------*/
1276 /* Bottom-most level for UNIX-like case */
1280 struct _PerlIO base; /* The generic part */
1281 int fd; /* UNIX like file descriptor */
1282 int oflags; /* open/fcntl flags */
1286 PerlIOUnix_oflags(const char *mode)
1301 oflags = O_CREAT|O_TRUNC;
1312 oflags = O_CREAT|O_APPEND;
1328 else if (*mode == 't')
1331 oflags &= ~O_BINARY;
1334 /* Always open in binary mode */
1336 if (*mode || oflags == -1)
1345 PerlIOUnix_fileno(PerlIO *f)
1347 return PerlIOSelf(f,PerlIOUnix)->fd;
1351 PerlIOUnix_fdopen(PerlIO_funcs *self, int fd,const char *mode)
1359 int oflags = PerlIOUnix_oflags(mode);
1362 PerlIOUnix *s = PerlIOSelf(PerlIO_push(f = PerlIO_allocate(aTHX),self,mode,Nullch,0),PerlIOUnix);
1365 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
1372 PerlIOUnix_open(PerlIO_funcs *self, const char *path,const char *mode)
1376 int oflags = PerlIOUnix_oflags(mode);
1379 int fd = PerlLIO_open3(path,oflags,0666);
1382 PerlIOUnix *s = PerlIOSelf(PerlIO_push(f = PerlIO_allocate(aTHX),self,mode,Nullch,0),PerlIOUnix);
1385 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
1392 PerlIOUnix_reopen(const char *path, const char *mode, PerlIO *f)
1394 PerlIOUnix *s = PerlIOSelf(f,PerlIOUnix);
1395 int oflags = PerlIOUnix_oflags(mode);
1396 if (PerlIOBase(f)->flags & PERLIO_F_OPEN)
1397 (*PerlIOBase(f)->tab->Close)(f);
1401 int fd = PerlLIO_open3(path,oflags,0666);
1406 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
1414 PerlIOUnix_read(PerlIO *f, void *vbuf, Size_t count)
1417 int fd = PerlIOSelf(f,PerlIOUnix)->fd;
1418 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD))
1422 SSize_t len = PerlLIO_read(fd,vbuf,count);
1423 if (len >= 0 || errno != EINTR)
1426 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
1427 else if (len == 0 && count != 0)
1428 PerlIOBase(f)->flags |= PERLIO_F_EOF;
1436 PerlIOUnix_write(PerlIO *f, const void *vbuf, Size_t count)
1439 int fd = PerlIOSelf(f,PerlIOUnix)->fd;
1442 SSize_t len = PerlLIO_write(fd,vbuf,count);
1443 if (len >= 0 || errno != EINTR)
1446 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
1454 PerlIOUnix_seek(PerlIO *f, Off_t offset, int whence)
1457 Off_t new = PerlLIO_lseek(PerlIOSelf(f,PerlIOUnix)->fd,offset,whence);
1458 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
1459 return (new == (Off_t) -1) ? -1 : 0;
1463 PerlIOUnix_tell(PerlIO *f)
1466 Off_t posn = PerlLIO_lseek(PerlIOSelf(f,PerlIOUnix)->fd,0,SEEK_CUR);
1467 return PerlLIO_lseek(PerlIOSelf(f,PerlIOUnix)->fd,0,SEEK_CUR);
1471 PerlIOUnix_close(PerlIO *f)
1474 int fd = PerlIOSelf(f,PerlIOUnix)->fd;
1476 while (PerlLIO_close(fd) != 0)
1487 PerlIOBase(f)->flags &= ~PERLIO_F_OPEN;
1492 PerlIO_funcs PerlIO_unix = {
1508 PerlIOBase_noop_ok, /* flush */
1509 PerlIOBase_noop_fail, /* fill */
1512 PerlIOBase_clearerr,
1513 PerlIOBase_setlinebuf,
1514 NULL, /* get_base */
1515 NULL, /* get_bufsiz */
1518 NULL, /* set_ptrcnt */
1521 /*--------------------------------------------------------------------------------------*/
1522 /* stdio as a layer */
1526 struct _PerlIO base;
1527 FILE * stdio; /* The stream */
1531 PerlIOStdio_fileno(PerlIO *f)
1534 return PerlSIO_fileno(PerlIOSelf(f,PerlIOStdio)->stdio);
1538 PerlIOStdio_mode(const char *mode,char *tmode)
1545 if (O_BINARY != O_TEXT)
1554 PerlIOStdio_fdopen(PerlIO_funcs *self, int fd,const char *mode)
1573 stdio = PerlSIO_stdin;
1576 stdio = PerlSIO_stdout;
1579 stdio = PerlSIO_stderr;
1585 stdio = PerlSIO_fdopen(fd,mode = PerlIOStdio_mode(mode,tmode));
1589 PerlIOStdio *s = PerlIOSelf(PerlIO_push(f = PerlIO_allocate(aTHX),self,mode,Nullch,0),PerlIOStdio);
1596 #undef PerlIO_importFILE
1598 PerlIO_importFILE(FILE *stdio, int fl)
1604 PerlIOStdio *s = PerlIOSelf(PerlIO_push(f = PerlIO_allocate(aTHX),&PerlIO_stdio,"r+",Nullch,0),PerlIOStdio);
1611 PerlIOStdio_open(PerlIO_funcs *self, const char *path,const char *mode)
1615 FILE *stdio = PerlSIO_fopen(path,mode);
1619 PerlIOStdio *s = PerlIOSelf(PerlIO_push(f = PerlIO_allocate(aTHX), self,
1620 (mode = PerlIOStdio_mode(mode,tmode)),Nullch,0),
1628 PerlIOStdio_reopen(const char *path, const char *mode, PerlIO *f)
1631 PerlIOStdio *s = PerlIOSelf(f,PerlIOStdio);
1633 FILE *stdio = PerlSIO_freopen(path,(mode = PerlIOStdio_mode(mode,tmode)),s->stdio);
1641 PerlIOStdio_read(PerlIO *f, void *vbuf, Size_t count)
1644 FILE *s = PerlIOSelf(f,PerlIOStdio)->stdio;
1648 STDCHAR *buf = (STDCHAR *) vbuf;
1649 /* Perl is expecting PerlIO_getc() to fill the buffer
1650 * Linux's stdio does not do that for fread()
1652 int ch = PerlSIO_fgetc(s);
1660 got = PerlSIO_fread(vbuf,1,count,s);
1665 PerlIOStdio_unread(PerlIO *f, const void *vbuf, Size_t count)
1668 FILE *s = PerlIOSelf(f,PerlIOStdio)->stdio;
1669 STDCHAR *buf = ((STDCHAR *)vbuf)+count-1;
1673 int ch = *buf-- & 0xff;
1674 if (PerlSIO_ungetc(ch,s) != ch)
1683 PerlIOStdio_write(PerlIO *f, const void *vbuf, Size_t count)
1686 return PerlSIO_fwrite(vbuf,1,count,PerlIOSelf(f,PerlIOStdio)->stdio);
1690 PerlIOStdio_seek(PerlIO *f, Off_t offset, int whence)
1693 FILE *stdio = PerlIOSelf(f,PerlIOStdio)->stdio;
1694 return PerlSIO_fseek(stdio,offset,whence);
1698 PerlIOStdio_tell(PerlIO *f)
1701 FILE *stdio = PerlIOSelf(f,PerlIOStdio)->stdio;
1702 return PerlSIO_ftell(stdio);
1706 PerlIOStdio_close(PerlIO *f)
1710 int optval, optlen = sizeof(int);
1712 FILE *stdio = PerlIOSelf(f,PerlIOStdio)->stdio;
1715 (getsockopt(PerlIO_fileno(f), SOL_SOCKET, SO_TYPE, (char *)&optval, &optlen) < 0) ?
1716 PerlSIO_fclose(stdio) :
1717 close(PerlIO_fileno(f))
1719 PerlSIO_fclose(stdio)
1726 PerlIOStdio_flush(PerlIO *f)
1729 FILE *stdio = PerlIOSelf(f,PerlIOStdio)->stdio;
1730 if (PerlIOBase(f)->flags & PERLIO_F_CANWRITE)
1732 return PerlSIO_fflush(stdio);
1737 /* FIXME: This discards ungetc() and pre-read stuff which is
1738 not right if this is just a "sync" from a layer above
1739 Suspect right design is to do _this_ but not have layer above
1740 flush this layer read-to-read
1742 /* Not writeable - sync by attempting a seek */
1744 if (PerlSIO_fseek(stdio,(Off_t) 0, SEEK_CUR) != 0)
1752 PerlIOStdio_fill(PerlIO *f)
1755 FILE *stdio = PerlIOSelf(f,PerlIOStdio)->stdio;
1757 /* fflush()ing read-only streams can cause trouble on some stdio-s */
1758 if ((PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
1760 if (PerlSIO_fflush(stdio) != 0)
1763 c = PerlSIO_fgetc(stdio);
1764 if (c == EOF || PerlSIO_ungetc(c,stdio) != c)
1770 PerlIOStdio_eof(PerlIO *f)
1773 return PerlSIO_feof(PerlIOSelf(f,PerlIOStdio)->stdio);
1777 PerlIOStdio_error(PerlIO *f)
1780 return PerlSIO_ferror(PerlIOSelf(f,PerlIOStdio)->stdio);
1784 PerlIOStdio_clearerr(PerlIO *f)
1787 PerlSIO_clearerr(PerlIOSelf(f,PerlIOStdio)->stdio);
1791 PerlIOStdio_setlinebuf(PerlIO *f)
1794 #ifdef HAS_SETLINEBUF
1795 PerlSIO_setlinebuf(PerlIOSelf(f,PerlIOStdio)->stdio);
1797 PerlSIO_setvbuf(PerlIOSelf(f,PerlIOStdio)->stdio, Nullch, _IOLBF, 0);
1803 PerlIOStdio_get_base(PerlIO *f)
1806 FILE *stdio = PerlIOSelf(f,PerlIOStdio)->stdio;
1807 return PerlSIO_get_base(stdio);
1811 PerlIOStdio_get_bufsiz(PerlIO *f)
1814 FILE *stdio = PerlIOSelf(f,PerlIOStdio)->stdio;
1815 return PerlSIO_get_bufsiz(stdio);
1819 #ifdef USE_STDIO_PTR
1821 PerlIOStdio_get_ptr(PerlIO *f)
1824 FILE *stdio = PerlIOSelf(f,PerlIOStdio)->stdio;
1825 return PerlSIO_get_ptr(stdio);
1829 PerlIOStdio_get_cnt(PerlIO *f)
1832 FILE *stdio = PerlIOSelf(f,PerlIOStdio)->stdio;
1833 return PerlSIO_get_cnt(stdio);
1837 PerlIOStdio_set_ptrcnt(PerlIO *f,STDCHAR *ptr,SSize_t cnt)
1840 FILE *stdio = PerlIOSelf(f,PerlIOStdio)->stdio;
1843 #ifdef STDIO_PTR_LVALUE
1844 PerlSIO_set_ptr(stdio,ptr);
1845 #ifdef STDIO_PTR_LVAL_SETS_CNT
1846 if (PerlSIO_get_cnt(stdio) != (cnt))
1849 assert(PerlSIO_get_cnt(stdio) == (cnt));
1852 #if (!defined(STDIO_PTR_LVAL_NOCHANGE_CNT))
1853 /* Setting ptr _does_ change cnt - we are done */
1856 #else /* STDIO_PTR_LVALUE */
1858 #endif /* STDIO_PTR_LVALUE */
1860 /* Now (or only) set cnt */
1861 #ifdef STDIO_CNT_LVALUE
1862 PerlSIO_set_cnt(stdio,cnt);
1863 #else /* STDIO_CNT_LVALUE */
1864 #if (defined(STDIO_PTR_LVALUE) && defined(STDIO_PTR_LVAL_SETS_CNT))
1865 PerlSIO_set_ptr(stdio,PerlSIO_get_ptr(stdio)+(PerlSIO_get_cnt(stdio)-cnt));
1866 #else /* STDIO_PTR_LVAL_SETS_CNT */
1868 #endif /* STDIO_PTR_LVAL_SETS_CNT */
1869 #endif /* STDIO_CNT_LVALUE */
1874 PerlIO_funcs PerlIO_stdio = {
1876 sizeof(PerlIOStdio),
1894 PerlIOStdio_clearerr,
1895 PerlIOStdio_setlinebuf,
1897 PerlIOStdio_get_base,
1898 PerlIOStdio_get_bufsiz,
1903 #ifdef USE_STDIO_PTR
1904 PerlIOStdio_get_ptr,
1905 PerlIOStdio_get_cnt,
1906 #if (defined(STDIO_PTR_LVALUE) && (defined(STDIO_CNT_LVALUE) || defined(STDIO_PTR_LVAL_SETS_CNT)))
1907 PerlIOStdio_set_ptrcnt
1908 #else /* STDIO_PTR_LVALUE */
1910 #endif /* STDIO_PTR_LVALUE */
1911 #else /* USE_STDIO_PTR */
1915 #endif /* USE_STDIO_PTR */
1918 #undef PerlIO_exportFILE
1920 PerlIO_exportFILE(PerlIO *f, int fl)
1924 stdio = fdopen(PerlIO_fileno(f),"r+");
1927 PerlIOStdio *s = PerlIOSelf(PerlIO_push(f,&PerlIO_stdio,"r+",Nullch,0),PerlIOStdio);
1933 #undef PerlIO_findFILE
1935 PerlIO_findFILE(PerlIO *f)
1940 if (l->tab == &PerlIO_stdio)
1942 PerlIOStdio *s = PerlIOSelf(&l,PerlIOStdio);
1945 l = *PerlIONext(&l);
1947 return PerlIO_exportFILE(f,0);
1950 #undef PerlIO_releaseFILE
1952 PerlIO_releaseFILE(PerlIO *p, FILE *f)
1956 /*--------------------------------------------------------------------------------------*/
1957 /* perlio buffer layer */
1960 PerlIOBuf_pushed(PerlIO *f, const char *mode, const char *arg, STRLEN len)
1962 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
1963 b->posn = PerlIO_tell(PerlIONext(f));
1964 return PerlIOBase_pushed(f,mode,arg,len);
1968 PerlIOBuf_fdopen(PerlIO_funcs *self, int fd, const char *mode)
1971 PerlIO_funcs *tab = PerlIO_default_btm();
1979 #if O_BINARY != O_TEXT
1980 /* do something about failing setmode()? --jhi */
1981 PerlLIO_setmode(fd, O_BINARY);
1983 f = (*tab->Fdopen)(tab,fd,mode);
1986 PerlIOBuf *b = PerlIOSelf(PerlIO_push(f,self,mode,Nullch,0),PerlIOBuf);
1987 if (init && fd == 2)
1989 /* Initial stderr is unbuffered */
1990 PerlIOBase(f)->flags |= PERLIO_F_UNBUF;
1993 PerlIO_debug("PerlIOBuf_fdopen %s f=%p fd=%d m=%s fl=%08"UVxf"\n",
1994 self->name,f,fd,mode,PerlIOBase(f)->flags);
2001 PerlIOBuf_open(PerlIO_funcs *self, const char *path, const char *mode)
2003 PerlIO_funcs *tab = PerlIO_default_btm();
2004 PerlIO *f = (*tab->Open)(tab,path,mode);
2007 PerlIO_push(f,self,mode,Nullch,0);
2013 PerlIOBuf_reopen(const char *path, const char *mode, PerlIO *f)
2015 PerlIO *next = PerlIONext(f);
2016 int code = (*PerlIOBase(next)->tab->Reopen)(path,mode,next);
2018 code = (*PerlIOBase(f)->tab->Pushed)(f,mode,Nullch,0);
2022 /* This "flush" is akin to sfio's sync in that it handles files in either
2026 PerlIOBuf_flush(PerlIO *f)
2028 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2030 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
2032 /* write() the buffer */
2033 STDCHAR *buf = b->buf;
2036 PerlIO *n = PerlIONext(f);
2039 count = PerlIO_write(n,p,b->ptr - p);
2044 else if (count < 0 || PerlIO_error(n))
2046 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2051 b->posn += (p - buf);
2053 else if (PerlIOBase(f)->flags & PERLIO_F_RDBUF)
2055 STDCHAR *buf = PerlIO_get_base(f);
2056 /* Note position change */
2057 b->posn += (b->ptr - buf);
2058 if (b->ptr < b->end)
2060 /* We did not consume all of it */
2061 if (PerlIO_seek(PerlIONext(f),b->posn,SEEK_SET) == 0)
2063 b->posn = PerlIO_tell(PerlIONext(f));
2067 b->ptr = b->end = b->buf;
2068 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF|PERLIO_F_WRBUF);
2069 /* FIXME: Is this right for read case ? */
2070 if (PerlIO_flush(PerlIONext(f)) != 0)
2076 PerlIOBuf_fill(PerlIO *f)
2078 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2079 PerlIO *n = PerlIONext(f);
2081 /* FIXME: doing the down-stream flush is a bad idea if it causes
2082 pre-read data in stdio buffer to be discarded
2083 but this is too simplistic - as it skips _our_ hosekeeping
2084 and breaks tell tests.
2085 if (!(PerlIOBase(f)->flags & PERLIO_F_RDBUF))
2089 if (PerlIO_flush(f) != 0)
2093 PerlIO_get_base(f); /* allocate via vtable */
2095 b->ptr = b->end = b->buf;
2096 if (PerlIO_fast_gets(n))
2098 /* Layer below is also buffered
2099 * We do _NOT_ want to call its ->Read() because that will loop
2100 * till it gets what we asked for which may hang on a pipe etc.
2101 * Instead take anything it has to hand, or ask it to fill _once_.
2103 avail = PerlIO_get_cnt(n);
2106 avail = PerlIO_fill(n);
2108 avail = PerlIO_get_cnt(n);
2111 if (!PerlIO_error(n) && PerlIO_eof(n))
2117 STDCHAR *ptr = PerlIO_get_ptr(n);
2118 SSize_t cnt = avail;
2119 if (avail > b->bufsiz)
2121 Copy(ptr,b->buf,avail,STDCHAR);
2122 PerlIO_set_ptrcnt(n,ptr+avail,cnt-avail);
2127 avail = PerlIO_read(n,b->ptr,b->bufsiz);
2132 PerlIOBase(f)->flags |= PERLIO_F_EOF;
2134 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2137 b->end = b->buf+avail;
2138 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
2143 PerlIOBuf_read(PerlIO *f, void *vbuf, Size_t count)
2145 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2146 STDCHAR *buf = (STDCHAR *) vbuf;
2151 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD))
2155 SSize_t avail = PerlIO_get_cnt(f);
2156 SSize_t take = (count < avail) ? count : avail;
2159 STDCHAR *ptr = PerlIO_get_ptr(f);
2160 Copy(ptr,buf,take,STDCHAR);
2161 PerlIO_set_ptrcnt(f,ptr+take,(avail -= take));
2165 if (count > 0 && avail <= 0)
2167 if (PerlIO_fill(f) != 0)
2171 return (buf - (STDCHAR *) vbuf);
2177 PerlIOBuf_unread(PerlIO *f, const void *vbuf, Size_t count)
2179 const STDCHAR *buf = (const STDCHAR *) vbuf+count;
2180 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2183 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
2189 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF)
2191 avail = (b->ptr - b->buf);
2196 b->end = b->buf + avail;
2198 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
2199 b->posn -= b->bufsiz;
2201 if (avail > (SSize_t) count)
2209 Copy(buf,b->ptr,avail,STDCHAR);
2213 PerlIOBase(f)->flags &= ~ PERLIO_F_EOF;
2220 PerlIOBuf_write(PerlIO *f, const void *vbuf, Size_t count)
2222 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2223 const STDCHAR *buf = (const STDCHAR *) vbuf;
2227 if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
2231 SSize_t avail = b->bufsiz - (b->ptr - b->buf);
2232 if ((SSize_t) count < avail)
2234 PerlIOBase(f)->flags |= PERLIO_F_WRBUF;
2235 if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF)
2255 Copy(buf,b->ptr,avail,STDCHAR);
2262 if (b->ptr >= (b->buf + b->bufsiz))
2265 if (PerlIOBase(f)->flags & PERLIO_F_UNBUF)
2271 PerlIOBuf_seek(PerlIO *f, Off_t offset, int whence)
2274 if ((code = PerlIO_flush(f)) == 0)
2276 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2277 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
2278 code = PerlIO_seek(PerlIONext(f),offset,whence);
2281 b->posn = PerlIO_tell(PerlIONext(f));
2288 PerlIOBuf_tell(PerlIO *f)
2290 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2291 Off_t posn = b->posn;
2293 posn += (b->ptr - b->buf);
2298 PerlIOBuf_close(PerlIO *f)
2301 IV code = PerlIOBase_close(f);
2302 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2303 if (b->buf && b->buf != (STDCHAR *) &b->oneword)
2305 PerlMemShared_free(b->buf);
2308 b->ptr = b->end = b->buf;
2309 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF|PERLIO_F_WRBUF);
2314 PerlIOBuf_setlinebuf(PerlIO *f)
2318 PerlIOBase(f)->flags &= ~PERLIO_F_LINEBUF;
2323 PerlIOBuf_get_ptr(PerlIO *f)
2325 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2332 PerlIOBuf_get_cnt(PerlIO *f)
2334 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2337 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF)
2338 return (b->end - b->ptr);
2343 PerlIOBuf_get_base(PerlIO *f)
2345 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2351 b->buf = PerlMemShared_calloc(b->bufsiz,sizeof(STDCHAR));
2354 b->buf = (STDCHAR *)&b->oneword;
2355 b->bufsiz = sizeof(b->oneword);
2364 PerlIOBuf_bufsiz(PerlIO *f)
2366 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2369 return (b->end - b->buf);
2373 PerlIOBuf_set_ptrcnt(PerlIO *f, STDCHAR *ptr, SSize_t cnt)
2375 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2379 if (PerlIO_get_cnt(f) != cnt || b->ptr < b->buf)
2382 assert(PerlIO_get_cnt(f) == cnt);
2383 assert(b->ptr >= b->buf);
2385 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
2388 PerlIO_funcs PerlIO_perlio = {
2408 PerlIOBase_clearerr,
2409 PerlIOBuf_setlinebuf,
2414 PerlIOBuf_set_ptrcnt,
2417 /*--------------------------------------------------------------------------------------*/
2418 /* Temp layer to hold unread chars when cannot do it any other way */
2421 PerlIOPending_fill(PerlIO *f)
2423 /* Should never happen */
2429 PerlIOPending_close(PerlIO *f)
2431 /* A tad tricky - flush pops us, then we close new top */
2433 return PerlIO_close(f);
2437 PerlIOPending_seek(PerlIO *f, Off_t offset, int whence)
2439 /* A tad tricky - flush pops us, then we seek new top */
2441 return PerlIO_seek(f,offset,whence);
2446 PerlIOPending_flush(PerlIO *f)
2448 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2449 if (b->buf && b->buf != (STDCHAR *) &b->oneword)
2452 PerlMemShared_free(b->buf);
2460 PerlIOPending_set_ptrcnt(PerlIO *f, STDCHAR *ptr, SSize_t cnt)
2468 PerlIOBuf_set_ptrcnt(f,ptr,cnt);
2473 PerlIOPending_pushed(PerlIO *f,const char *mode,const char *arg,STRLEN len)
2475 IV code = PerlIOBase_pushed(f,mode,arg,len);
2476 PerlIOl *l = PerlIOBase(f);
2477 /* Our PerlIO_fast_gets must match what we are pushed on,
2478 or sv_gets() etc. get muddled when it changes mid-string
2481 l->flags = (l->flags & ~(PERLIO_F_FASTGETS|PERLIO_F_UTF8)) |
2482 (PerlIOBase(PerlIONext(f))->flags & (PERLIO_F_FASTGETS|PERLIO_F_UTF8));
2487 PerlIOPending_read(PerlIO *f, void *vbuf, Size_t count)
2489 SSize_t avail = PerlIO_get_cnt(f);
2494 got = PerlIOBuf_read(f,vbuf,avail);
2496 got += PerlIO_read(f,((STDCHAR *) vbuf)+got,count-got);
2501 PerlIO_funcs PerlIO_pending = {
2509 PerlIOPending_pushed,
2516 PerlIOPending_close,
2517 PerlIOPending_flush,
2521 PerlIOBase_clearerr,
2522 PerlIOBuf_setlinebuf,
2527 PerlIOPending_set_ptrcnt,
2532 /*--------------------------------------------------------------------------------------*/
2533 /* crlf - translation
2534 On read translate CR,LF to "\n" we do this by overriding ptr/cnt entries
2535 to hand back a line at a time and keeping a record of which nl we "lied" about.
2536 On write translate "\n" to CR,LF
2541 PerlIOBuf base; /* PerlIOBuf stuff */
2542 STDCHAR *nl; /* Position of crlf we "lied" about in the buffer */
2546 PerlIOCrlf_pushed(PerlIO *f, const char *mode,const char *arg,STRLEN len)
2549 PerlIOBase(f)->flags |= PERLIO_F_CRLF;
2550 code = PerlIOBuf_pushed(f,mode,arg,len);
2552 PerlIO_debug("PerlIOCrlf_pushed f=%p %s %s fl=%08"UVxf"\n",
2553 f,PerlIOBase(f)->tab->name,(mode) ? mode : "(Null)",
2554 PerlIOBase(f)->flags);
2561 PerlIOCrlf_unread(PerlIO *f, const void *vbuf, Size_t count)
2563 PerlIOCrlf *c = PerlIOSelf(f,PerlIOCrlf);
2569 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF))
2570 return PerlIOBuf_unread(f,vbuf,count);
2573 const STDCHAR *buf = (const STDCHAR *) vbuf+count;
2574 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2576 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
2582 if (!(PerlIOBase(f)->flags & PERLIO_F_RDBUF))
2584 b->end = b->ptr = b->buf + b->bufsiz;
2585 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
2586 b->posn -= b->bufsiz;
2588 while (count > 0 && b->ptr > b->buf)
2593 if (b->ptr - 2 >= b->buf)
2619 PerlIOCrlf_get_cnt(PerlIO *f)
2621 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2624 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF)
2626 PerlIOCrlf *c = PerlIOSelf(f,PerlIOCrlf);
2627 if ((PerlIOBase(f)->flags & PERLIO_F_CRLF) && !c->nl)
2629 STDCHAR *nl = b->ptr;
2631 while (nl < b->end && *nl != 0xd)
2633 if (nl < b->end && *nl == 0xd)
2645 /* Not CR,LF but just CR */
2652 /* Blast - found CR as last char in buffer */
2655 /* They may not care, defer work as long as possible */
2656 return (nl - b->ptr);
2662 b->ptr++; /* say we have read it as far as flush() is concerned */
2663 b->buf++; /* Leave space an front of buffer */
2664 b->bufsiz--; /* Buffer is thus smaller */
2665 code = PerlIO_fill(f); /* Fetch some more */
2666 b->bufsiz++; /* Restore size for next time */
2667 b->buf--; /* Point at space */
2668 b->ptr = nl = b->buf; /* Which is what we hand off */
2669 b->posn--; /* Buffer starts here */
2670 *nl = 0xd; /* Fill in the CR */
2672 goto test; /* fill() call worked */
2673 /* CR at EOF - just fall through */
2678 return (((c->nl) ? (c->nl+1) : b->end) - b->ptr);
2684 PerlIOCrlf_set_ptrcnt(PerlIO *f, STDCHAR *ptr, SSize_t cnt)
2686 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2687 PerlIOCrlf *c = PerlIOSelf(f,PerlIOCrlf);
2688 IV flags = PerlIOBase(f)->flags;
2698 if ((flags & PERLIO_F_CRLF) && ptr > b->buf && ptr[-1] == 0xd)
2705 /* Test code - delete when it works ... */
2712 if ((flags & PERLIO_F_CRLF) && chk > b->buf && chk[-1] == 0xd)
2720 Perl_croak(aTHX_ "ptr wrong %p != %p fl=%08"UVxf" nl=%p e=%p for %d",
2721 ptr, chk, flags, c->nl, b->end, cnt);
2728 /* They have taken what we lied about */
2735 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
2739 PerlIOCrlf_write(PerlIO *f, const void *vbuf, Size_t count)
2741 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF))
2742 return PerlIOBuf_write(f,vbuf,count);
2745 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2746 const STDCHAR *buf = (const STDCHAR *) vbuf;
2747 const STDCHAR *ebuf = buf+count;
2750 if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
2754 STDCHAR *eptr = b->buf+b->bufsiz;
2755 PerlIOBase(f)->flags |= PERLIO_F_WRBUF;
2756 while (buf < ebuf && b->ptr < eptr)
2760 if ((b->ptr + 2) > eptr)
2762 /* Not room for both */
2768 *(b->ptr)++ = 0xd; /* CR */
2769 *(b->ptr)++ = 0xa; /* LF */
2771 if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF)
2790 if (PerlIOBase(f)->flags & PERLIO_F_UNBUF)
2792 return (buf - (STDCHAR *) vbuf);
2797 PerlIOCrlf_flush(PerlIO *f)
2799 PerlIOCrlf *c = PerlIOSelf(f,PerlIOCrlf);
2805 return PerlIOBuf_flush(f);
2808 PerlIO_funcs PerlIO_crlf = {
2811 PERLIO_K_BUFFERED|PERLIO_K_CANCRLF,
2817 PerlIOBase_noop_ok, /* popped */
2818 PerlIOBuf_read, /* generic read works with ptr/cnt lies ... */
2819 PerlIOCrlf_unread, /* Put CR,LF in buffer for each '\n' */
2820 PerlIOCrlf_write, /* Put CR,LF in buffer for each '\n' */
2828 PerlIOBase_clearerr,
2829 PerlIOBuf_setlinebuf,
2834 PerlIOCrlf_set_ptrcnt,
2838 /*--------------------------------------------------------------------------------------*/
2839 /* mmap as "buffer" layer */
2843 PerlIOBuf base; /* PerlIOBuf stuff */
2844 Mmap_t mptr; /* Mapped address */
2845 Size_t len; /* mapped length */
2846 STDCHAR *bbuf; /* malloced buffer if map fails */
2849 static size_t page_size = 0;
2852 PerlIOMmap_map(PerlIO *f)
2855 PerlIOMmap *m = PerlIOSelf(f,PerlIOMmap);
2856 PerlIOBuf *b = &m->base;
2857 IV flags = PerlIOBase(f)->flags;
2861 if (flags & PERLIO_F_CANREAD)
2863 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
2864 int fd = PerlIO_fileno(f);
2866 code = fstat(fd,&st);
2867 if (code == 0 && S_ISREG(st.st_mode))
2869 SSize_t len = st.st_size - b->posn;
2874 #if defined(HAS_SYSCONF) && (defined(_SC_PAGESIZE) || defined(_SC_PAGE_SIZE))
2876 SETERRNO(0,SS$_NORMAL);
2877 # ifdef _SC_PAGESIZE
2878 page_size = sysconf(_SC_PAGESIZE);
2880 page_size = sysconf(_SC_PAGE_SIZE);
2882 if ((long)page_size < 0) {
2887 (void)SvUPGRADE(error, SVt_PV);
2888 msg = SvPVx(error, n_a);
2889 Perl_croak(aTHX_ "panic: sysconf: %s", msg);
2892 Perl_croak(aTHX_ "panic: sysconf: pagesize unknown");
2896 # ifdef HAS_GETPAGESIZE
2897 page_size = getpagesize();
2899 # if defined(I_SYS_PARAM) && defined(PAGESIZE)
2900 page_size = PAGESIZE; /* compiletime, bad */
2904 if ((IV)page_size <= 0)
2905 Perl_croak(aTHX_ "panic: bad pagesize %"IVdf, (IV)page_size);
2909 /* This is a hack - should never happen - open should have set it ! */
2910 b->posn = PerlIO_tell(PerlIONext(f));
2912 posn = (b->posn / page_size) * page_size;
2913 len = st.st_size - posn;
2914 m->mptr = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, posn);
2915 if (m->mptr && m->mptr != (Mmap_t) -1)
2917 #if 0 && defined(HAS_MADVISE) && defined(MADV_SEQUENTIAL)
2918 madvise(m->mptr, len, MADV_SEQUENTIAL);
2920 #if 0 && defined(HAS_MADVISE) && defined(MADV_WILLNEED)
2921 madvise(m->mptr, len, MADV_WILLNEED);
2923 PerlIOBase(f)->flags = (flags & ~PERLIO_F_EOF) | PERLIO_F_RDBUF;
2924 b->end = ((STDCHAR *)m->mptr) + len;
2925 b->buf = ((STDCHAR *)m->mptr) + (b->posn - posn);
2936 PerlIOBase(f)->flags = flags | PERLIO_F_EOF | PERLIO_F_RDBUF;
2938 b->ptr = b->end = b->ptr;
2947 PerlIOMmap_unmap(PerlIO *f)
2949 PerlIOMmap *m = PerlIOSelf(f,PerlIOMmap);
2950 PerlIOBuf *b = &m->base;
2956 code = munmap(m->mptr, m->len);
2960 if (PerlIO_seek(PerlIONext(f),b->posn,SEEK_SET) != 0)
2963 b->ptr = b->end = b->buf;
2964 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF|PERLIO_F_WRBUF);
2970 PerlIOMmap_get_base(PerlIO *f)
2972 PerlIOMmap *m = PerlIOSelf(f,PerlIOMmap);
2973 PerlIOBuf *b = &m->base;
2974 if (b->buf && (PerlIOBase(f)->flags & PERLIO_F_RDBUF))
2976 /* Already have a readbuffer in progress */
2981 /* We have a write buffer or flushed PerlIOBuf read buffer */
2982 m->bbuf = b->buf; /* save it in case we need it again */
2983 b->buf = NULL; /* Clear to trigger below */
2987 PerlIOMmap_map(f); /* Try and map it */
2990 /* Map did not work - recover PerlIOBuf buffer if we have one */
2994 b->ptr = b->end = b->buf;
2997 return PerlIOBuf_get_base(f);
3001 PerlIOMmap_unread(PerlIO *f, const void *vbuf, Size_t count)
3003 PerlIOMmap *m = PerlIOSelf(f,PerlIOMmap);
3004 PerlIOBuf *b = &m->base;
3005 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
3007 if (b->ptr && (b->ptr - count) >= b->buf && memEQ(b->ptr - count,vbuf,count))
3010 PerlIOBase(f)->flags &= ~ PERLIO_F_EOF;
3015 /* Loose the unwritable mapped buffer */
3017 /* If flush took the "buffer" see if we have one from before */
3018 if (!b->buf && m->bbuf)
3022 PerlIOBuf_get_base(f);
3026 return PerlIOBuf_unread(f,vbuf,count);
3030 PerlIOMmap_write(PerlIO *f, const void *vbuf, Size_t count)
3032 PerlIOMmap *m = PerlIOSelf(f,PerlIOMmap);
3033 PerlIOBuf *b = &m->base;
3034 if (!b->buf || !(PerlIOBase(f)->flags & PERLIO_F_WRBUF))
3036 /* No, or wrong sort of, buffer */
3039 if (PerlIOMmap_unmap(f) != 0)
3042 /* If unmap took the "buffer" see if we have one from before */
3043 if (!b->buf && m->bbuf)
3047 PerlIOBuf_get_base(f);
3051 return PerlIOBuf_write(f,vbuf,count);
3055 PerlIOMmap_flush(PerlIO *f)
3057 PerlIOMmap *m = PerlIOSelf(f,PerlIOMmap);
3058 PerlIOBuf *b = &m->base;
3059 IV code = PerlIOBuf_flush(f);
3060 /* Now we are "synced" at PerlIOBuf level */
3065 /* Unmap the buffer */
3066 if (PerlIOMmap_unmap(f) != 0)
3071 /* We seem to have a PerlIOBuf buffer which was not mapped
3072 * remember it in case we need one later
3081 PerlIOMmap_fill(PerlIO *f)
3083 PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
3084 IV code = PerlIO_flush(f);
3085 if (code == 0 && !b->buf)
3087 code = PerlIOMmap_map(f);
3089 if (code == 0 && !(PerlIOBase(f)->flags & PERLIO_F_RDBUF))
3091 code = PerlIOBuf_fill(f);
3097 PerlIOMmap_close(PerlIO *f)
3099 PerlIOMmap *m = PerlIOSelf(f,PerlIOMmap);
3100 PerlIOBuf *b = &m->base;
3101 IV code = PerlIO_flush(f);
3106 b->ptr = b->end = b->buf;
3108 if (PerlIOBuf_close(f) != 0)
3114 PerlIO_funcs PerlIO_mmap = {
3134 PerlIOBase_clearerr,
3135 PerlIOBuf_setlinebuf,
3136 PerlIOMmap_get_base,
3140 PerlIOBuf_set_ptrcnt,
3143 #endif /* HAS_MMAP */
3151 atexit(&PerlIO_cleanup);
3163 PerlIO_stdstreams();
3167 #undef PerlIO_stdout
3172 PerlIO_stdstreams();
3176 #undef PerlIO_stderr
3181 PerlIO_stdstreams();
3185 /*--------------------------------------------------------------------------------------*/
3187 #undef PerlIO_getname
3189 PerlIO_getname(PerlIO *f, char *buf)
3192 Perl_croak(aTHX_ "Don't know how to get file name");
3197 /*--------------------------------------------------------------------------------------*/
3198 /* Functions which can be called on any kind of PerlIO implemented
3204 PerlIO_getc(PerlIO *f)
3207 SSize_t count = PerlIO_read(f,buf,1);
3210 return (unsigned char) buf[0];
3215 #undef PerlIO_ungetc
3217 PerlIO_ungetc(PerlIO *f, int ch)
3222 if (PerlIO_unread(f,&buf,1) == 1)
3230 PerlIO_putc(PerlIO *f, int ch)
3233 return PerlIO_write(f,&buf,1);
3238 PerlIO_puts(PerlIO *f, const char *s)
3240 STRLEN len = strlen(s);
3241 return PerlIO_write(f,s,len);
3244 #undef PerlIO_rewind
3246 PerlIO_rewind(PerlIO *f)
3248 PerlIO_seek(f,(Off_t)0,SEEK_SET);
3252 #undef PerlIO_vprintf
3254 PerlIO_vprintf(PerlIO *f, const char *fmt, va_list ap)
3257 SV *sv = newSVpvn("",0);
3262 Perl_va_copy(ap, apc);
3263 sv_vcatpvf(sv, fmt, &apc);
3265 sv_vcatpvf(sv, fmt, &ap);
3268 return PerlIO_write(f,s,len);
3271 #undef PerlIO_printf
3273 PerlIO_printf(PerlIO *f,const char *fmt,...)
3278 result = PerlIO_vprintf(f,fmt,ap);
3283 #undef PerlIO_stdoutf
3285 PerlIO_stdoutf(const char *fmt,...)
3290 result = PerlIO_vprintf(PerlIO_stdout(),fmt,ap);
3295 #undef PerlIO_tmpfile
3297 PerlIO_tmpfile(void)
3299 /* I have no idea how portable mkstemp() is ... */
3300 #if defined(WIN32) || !defined(HAVE_MKSTEMP)
3303 FILE *stdio = PerlSIO_tmpfile();
3306 PerlIOStdio *s = PerlIOSelf(PerlIO_push(f = PerlIO_allocate(aTHX),&PerlIO_stdio,"w+",Nullch,0),PerlIOStdio);
3312 SV *sv = newSVpv("/tmp/PerlIO_XXXXXX",0);
3313 int fd = mkstemp(SvPVX(sv));
3317 f = PerlIO_fdopen(fd,"w+");
3320 PerlIOBase(f)->flags |= PERLIO_F_TEMP;
3322 PerlLIO_unlink(SvPVX(sv));
3332 #endif /* USE_SFIO */
3333 #endif /* PERLIO_IS_STDIO */
3335 /*======================================================================================*/
3336 /* Now some functions in terms of above which may be needed even if
3337 we are not in true PerlIO mode
3341 #undef PerlIO_setpos
3343 PerlIO_setpos(PerlIO *f, SV *pos)
3349 Off_t *posn = (Off_t *) SvPV(pos,len);
3350 if (f && len == sizeof(Off_t))
3351 return PerlIO_seek(f,*posn,SEEK_SET);
3357 #undef PerlIO_setpos
3359 PerlIO_setpos(PerlIO *f, SV *pos)
3365 Fpos_t *fpos = (Fpos_t *) SvPV(pos,len);
3366 if (f && len == sizeof(Fpos_t))
3368 #if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64)
3369 return fsetpos64(f, fpos);
3371 return fsetpos(f, fpos);
3381 #undef PerlIO_getpos
3383 PerlIO_getpos(PerlIO *f, SV *pos)
3386 Off_t posn = PerlIO_tell(f);
3387 sv_setpvn(pos,(char *)&posn,sizeof(posn));
3388 return (posn == (Off_t)-1) ? -1 : 0;
3391 #undef PerlIO_getpos
3393 PerlIO_getpos(PerlIO *f, SV *pos)
3398 #if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64)
3399 code = fgetpos64(f, &fpos);
3401 code = fgetpos(f, &fpos);
3403 sv_setpvn(pos,(char *)&fpos,sizeof(fpos));
3408 #if (defined(PERLIO_IS_STDIO) || !defined(USE_SFIO)) && !defined(HAS_VPRINTF)
3411 vprintf(char *pat, char *args)
3413 _doprnt(pat, args, stdout);
3414 return 0; /* wrong, but perl doesn't use the return value */
3418 vfprintf(FILE *fd, char *pat, char *args)
3420 _doprnt(pat, args, fd);
3421 return 0; /* wrong, but perl doesn't use the return value */
3426 #ifndef PerlIO_vsprintf
3428 PerlIO_vsprintf(char *s, int n, const char *fmt, va_list ap)
3430 int val = vsprintf(s, fmt, ap);
3433 if (strlen(s) >= (STRLEN)n)
3436 (void)PerlIO_puts(Perl_error_log,
3437 "panic: sprintf overflow - memory corrupted!\n");
3445 #ifndef PerlIO_sprintf
3447 PerlIO_sprintf(char *s, int n, const char *fmt,...)
3452 result = PerlIO_vsprintf(s, n, fmt, ap);