2 * perlio.c Copyright (c) 1996-2002, Nick Ing-Simmons You may distribute
3 * under the terms of either the GNU General Public License or the
4 * Artistic License, as specified in the README file.
8 * Hour after hour for nearly three weary days he had jogged up and down,
9 * over passes, and through long dales, and across many streams.
13 * If we have ActivePerl-like PERL_IMPLICIT_SYS then we need a dTHX to get
14 * at the dispatch tables, even when we do not need it for other reasons.
15 * Invent a dSYS macro to abstract this out
17 #ifdef PERL_IMPLICIT_SYS
30 #define PERLIO_NOT_STDIO 0
31 #if !defined(PERLIO_IS_STDIO) && !defined(USE_SFIO)
37 * This file provides those parts of PerlIO abstraction
38 * which are not #defined in perlio.h.
39 * Which these are depends on various Configure #ifdef's
43 #define PERL_IN_PERLIO_C
46 #ifdef PERL_IMPLICIT_CONTEXT
54 perlsio_binmode(FILE *fp, int iotype, int mode)
57 * This used to be contents of do_binmode in doio.c
60 # if defined(atarist) || defined(__MINT__)
63 ((FILE *) fp)->_flag |= _IOBIN;
65 ((FILE *) fp)->_flag &= ~_IOBIN;
72 if (PerlLIO_setmode(fp, mode) != -1) {
74 if (PerlLIO_setmode(fileno(fp), mode) != -1) {
76 # if defined(WIN32) && defined(__BORLANDC__)
78 * The translation mode of the stream is maintained independent of
79 * the translation mode of the fd in the Borland RTL (heavy
80 * digging through their runtime sources reveal). User has to set
81 * the mode explicitly for the stream (though they don't document
82 * this anywhere). GSAR 97-5-24
96 # if defined(USEMYBINMODE)
98 if (my_binmode(fp, iotype, mode) != FALSE)
109 #define O_ACCMODE 3 /* Assume traditional implementation */
113 PerlIO_intmode2str(int rawmode, char *mode, int *writing)
115 int result = rawmode & O_ACCMODE;
120 ptype = IoTYPE_RDONLY;
123 ptype = IoTYPE_WRONLY;
131 *writing = (result != O_RDONLY);
133 if (result == O_RDONLY) {
137 else if (rawmode & O_APPEND) {
139 if (result != O_WRONLY)
144 if (result == O_WRONLY)
151 if (rawmode & O_BINARY)
157 #ifndef PERLIO_LAYERS
159 PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names)
161 if (!names || !*names || strEQ(names, ":crlf") || strEQ(names, ":raw")) {
164 Perl_croak(aTHX_ "Cannot apply \"%s\" in non-PerlIO perl", names);
172 PerlIO_destruct(pTHX)
177 PerlIO_binmode(pTHX_ PerlIO *fp, int iotype, int mode, const char *names)
182 return perlsio_binmode(fp, iotype, mode);
187 PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
192 #ifdef PERL_IMPLICIT_SYS
193 return PerlSIO_fdupopen(f);
196 return win32_fdupopen(f);
199 int fd = PerlLIO_dup(PerlIO_fileno(f));
202 int omode = fcntl(fd, F_GETFL);
204 omode = djgpp_get_stream_mode(f);
206 PerlIO_intmode2str(omode,mode,NULL);
207 /* the r+ is a hack */
208 return PerlIO_fdopen(fd, mode);
213 SETERRNO(EBADF, SS$_IVCHAN);
223 * De-mux PerlIO_openn() into fdopen, freopen and fopen type entries
227 PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd,
228 int imode, int perm, PerlIO *old, int narg, SV **args)
232 Perl_croak(aTHX_ "More than one argument to open");
234 if (*args == &PL_sv_undef)
235 return PerlIO_tmpfile();
237 char *name = SvPV_nolen(*args);
239 fd = PerlLIO_open3(name, imode, perm);
241 return PerlIO_fdopen(fd, (char *) mode + 1);
244 return PerlIO_reopen(name, mode, old);
247 return PerlIO_open(name, mode);
252 return PerlIO_fdopen(fd, (char *) mode);
257 XS(XS_PerlIO__Layer__find)
261 Perl_croak(aTHX_ "Usage class->find(name[,load])");
263 char *name = SvPV_nolen(ST(1));
264 ST(0) = (strEQ(name, "crlf")
265 || strEQ(name, "raw")) ? &PL_sv_yes : &PL_sv_undef;
272 Perl_boot_core_PerlIO(pTHX)
274 newXS("PerlIO::Layer::find", XS_PerlIO__Layer__find, __FILE__);
280 #ifdef PERLIO_IS_STDIO
286 * Does nothing (yet) except force this file to be included in perl
287 * binary. That allows this file to force inclusion of other functions
288 * that may be required by loadable extensions e.g. for
289 * FileHandle::tmpfile
293 #undef PerlIO_tmpfile
300 #else /* PERLIO_IS_STDIO */
308 * This section is just to make sure these functions get pulled in from
312 #undef PerlIO_tmpfile
323 * Force this file to be included in perl binary. Which allows this
324 * file to force inclusion of other functions that may be required by
325 * loadable extensions e.g. for FileHandle::tmpfile
329 * Hack sfio does its own 'autoflush' on stdout in common cases. Flush
330 * results in a lot of lseek()s to regular files and lot of small
333 sfset(sfstdout, SF_SHARE, 0);
337 PerlIO_importFILE(FILE *stdio, int fl)
339 int fd = fileno(stdio);
340 PerlIO *r = PerlIO_fdopen(fd, "r+");
345 PerlIO_findFILE(PerlIO *pio)
347 int fd = PerlIO_fileno(pio);
348 FILE *f = fdopen(fd, "r+");
350 if (!f && errno == EINVAL)
352 if (!f && errno == EINVAL)
359 /*======================================================================================*/
361 * Implement all the PerlIO interface ourselves.
367 * We _MUST_ have <unistd.h> if we are using lseek() and may have large
374 #include <sys/mman.h>
378 void PerlIO_debug(const char *fmt, ...)
379 __attribute__ ((format(__printf__, 1, 2)));
382 PerlIO_debug(const char *fmt, ...)
389 char *s = PerlEnv_getenv("PERLIO_DEBUG");
391 dbg = PerlLIO_open3(s, O_WRONLY | O_CREAT | O_APPEND, 0666);
398 /* Use fixed buffer as sv_catpvf etc. needs SVs */
402 s = CopFILE(PL_curcop);
405 sprintf(buffer, "%s:%" IVdf " ", s, (IV) CopLINE(PL_curcop));
406 len = strlen(buffer);
407 vsprintf(buffer+len, fmt, ap);
408 PerlLIO_write(dbg, buffer, strlen(buffer));
410 SV *sv = newSVpvn("", 0);
413 s = CopFILE(PL_curcop);
416 Perl_sv_catpvf(aTHX_ sv, "%s:%" IVdf " ", s,
417 (IV) CopLINE(PL_curcop));
418 Perl_sv_vcatpvf(aTHX_ sv, fmt, &ap);
421 PerlLIO_write(dbg, s, len);
428 /*--------------------------------------------------------------------------------------*/
431 * Inner level routines
435 * Table of pointers to the PerlIO structs (malloc'ed)
437 #define PERLIO_TABLE_SIZE 64
440 PerlIO_allocate(pTHX)
443 * Find a free slot in the table, allocating new table as necessary
448 while ((f = *last)) {
450 last = (PerlIO **) (f);
451 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
457 Newz('I',f,PERLIO_TABLE_SIZE,PerlIO);
465 #undef PerlIO_fdupopen
467 PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
469 if (PerlIOValid(f)) {
470 PerlIO_funcs *tab = PerlIOBase(f)->tab;
472 PerlIO_debug("fdupopen f=%p param=%p\n",(void*)f,(void*)param);
473 new = (*tab->Dup)(aTHX_ PerlIO_allocate(aTHX),f,param, flags);
477 SETERRNO(EBADF, SS$_IVCHAN);
483 PerlIO_cleantable(pTHX_ PerlIO **tablep)
485 PerlIO *table = *tablep;
488 PerlIO_cleantable(aTHX_(PerlIO **) & (table[0]));
489 for (i = PERLIO_TABLE_SIZE - 1; i > 0; i--) {
490 PerlIO *f = table + i;
502 PerlIO_list_alloc(pTHX)
505 Newz('L', list, 1, PerlIO_list_t);
511 PerlIO_list_free(pTHX_ PerlIO_list_t *list)
514 if (--list->refcnt == 0) {
517 for (i = 0; i < list->cur; i++) {
518 if (list->array[i].arg)
519 SvREFCNT_dec(list->array[i].arg);
521 Safefree(list->array);
529 PerlIO_list_push(pTHX_ PerlIO_list_t *list, PerlIO_funcs *funcs, SV *arg)
532 if (list->cur >= list->len) {
535 Renew(list->array, list->len, PerlIO_pair_t);
537 New('l', list->array, list->len, PerlIO_pair_t);
539 p = &(list->array[list->cur++]);
541 if ((p->arg = arg)) {
547 PerlIO_clone_list(pTHX_ PerlIO_list_t *proto, CLONE_PARAMS *param)
549 PerlIO_list_t *list = (PerlIO_list_t *) NULL;
552 list = PerlIO_list_alloc(aTHX);
553 for (i=0; i < proto->cur; i++) {
555 if (proto->array[i].arg)
556 arg = PerlIO_sv_dup(aTHX_ proto->array[i].arg,param);
557 PerlIO_list_push(aTHX_ list, proto->array[i].funcs, arg);
564 PerlIO_clone(pTHX_ PerlInterpreter *proto, CLONE_PARAMS *param)
567 PerlIO **table = &proto->Iperlio;
570 PL_known_layers = PerlIO_clone_list(aTHX_ proto->Iknown_layers, param);
571 PL_def_layerlist = PerlIO_clone_list(aTHX_ proto->Idef_layerlist, param);
572 PerlIO_allocate(aTHX); /* root slot is never used */
573 PerlIO_debug("Clone %p from %p\n",aTHX,proto);
574 while ((f = *table)) {
576 table = (PerlIO **) (f++);
577 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
579 (void) fp_dup(f, 0, param);
588 PerlIO_destruct(pTHX)
590 PerlIO **table = &PL_perlio;
593 PerlIO_debug("Destruct %p\n",aTHX);
595 while ((f = *table)) {
597 table = (PerlIO **) (f++);
598 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
602 if (l->tab->kind & PERLIO_K_DESTRUCT) {
603 PerlIO_debug("Destruct popping %s\n", l->tab->name);
617 PerlIO_pop(pTHX_ PerlIO *f)
621 PerlIO_debug("PerlIO_pop f=%p %s\n", (void*)f, l->tab->name);
622 if (l->tab->Popped) {
624 * If popped returns non-zero do not free its layer structure
625 * it has either done so itself, or it is shared and still in
628 if ((*l->tab->Popped) (aTHX_ f) != 0)
636 /*--------------------------------------------------------------------------------------*/
638 * XS Interface for perl code
642 PerlIO_find_layer(pTHX_ const char *name, STRLEN len, int load)
645 if ((SSize_t) len <= 0)
647 for (i = 0; i < PL_known_layers->cur; i++) {
648 PerlIO_funcs *f = PL_known_layers->array[i].funcs;
649 if (memEQ(f->name, name, len)) {
650 PerlIO_debug("%.*s => %p\n", (int) len, name, (void*)f);
654 if (load && PL_subname && PL_def_layerlist
655 && PL_def_layerlist->cur >= 2) {
656 SV *pkgsv = newSVpvn("PerlIO", 6);
657 SV *layer = newSVpvn(name, len);
660 * The two SVs are magically freed by load_module
662 Perl_load_module(aTHX_ 0, pkgsv, Nullsv, layer, Nullsv);
664 return PerlIO_find_layer(aTHX_ name, len, 0);
666 PerlIO_debug("Cannot find %.*s\n", (int) len, name);
670 #ifdef USE_ATTRIBUTES_FOR_PERLIO
673 perlio_mg_set(pTHX_ SV *sv, MAGIC *mg)
676 IO *io = GvIOn((GV *) SvRV(sv));
677 PerlIO *ifp = IoIFP(io);
678 PerlIO *ofp = IoOFP(io);
679 Perl_warn(aTHX_ "set %" SVf " %p %p %p", sv, io, ifp, ofp);
685 perlio_mg_get(pTHX_ SV *sv, MAGIC *mg)
688 IO *io = GvIOn((GV *) SvRV(sv));
689 PerlIO *ifp = IoIFP(io);
690 PerlIO *ofp = IoOFP(io);
691 Perl_warn(aTHX_ "get %" SVf " %p %p %p", sv, io, ifp, ofp);
697 perlio_mg_clear(pTHX_ SV *sv, MAGIC *mg)
699 Perl_warn(aTHX_ "clear %" SVf, sv);
704 perlio_mg_free(pTHX_ SV *sv, MAGIC *mg)
706 Perl_warn(aTHX_ "free %" SVf, sv);
710 MGVTBL perlio_vtab = {
718 XS(XS_io_MODIFY_SCALAR_ATTRIBUTES)
721 SV *sv = SvRV(ST(1));
726 sv_magic(sv, (SV *) av, PERL_MAGIC_ext, NULL, 0);
728 mg = mg_find(sv, PERL_MAGIC_ext);
729 mg->mg_virtual = &perlio_vtab;
731 Perl_warn(aTHX_ "attrib %" SVf, sv);
732 for (i = 2; i < items; i++) {
734 const char *name = SvPV(ST(i), len);
735 SV *layer = PerlIO_find_layer(aTHX_ name, len, 1);
737 av_push(av, SvREFCNT_inc(layer));
748 #endif /* USE_ATTIBUTES_FOR_PERLIO */
751 PerlIO_tab_sv(pTHX_ PerlIO_funcs *tab)
753 HV *stash = gv_stashpv("PerlIO::Layer", TRUE);
754 SV *sv = sv_bless(newRV_noinc(newSViv(PTR2IV(tab))), stash);
758 XS(XS_PerlIO__Layer__find)
762 Perl_croak(aTHX_ "Usage class->find(name[,load])");
765 char *name = SvPV(ST(1), len);
766 bool load = (items > 2) ? SvTRUE(ST(2)) : 0;
767 PerlIO_funcs *layer = PerlIO_find_layer(aTHX_ name, len, load);
769 (layer) ? sv_2mortal(PerlIO_tab_sv(aTHX_ layer)) :
776 PerlIO_define_layer(pTHX_ PerlIO_funcs *tab)
778 if (!PL_known_layers)
779 PL_known_layers = PerlIO_list_alloc(aTHX);
780 PerlIO_list_push(aTHX_ PL_known_layers, tab, Nullsv);
781 PerlIO_debug("define %s %p\n", tab->name, (void*)tab);
785 PerlIO_parse_layers(pTHX_ PerlIO_list_t *av, const char *names)
788 const char *s = names;
790 while (isSPACE(*s) || *s == ':')
795 const char *as = Nullch;
797 if (!isIDFIRST(*s)) {
799 * Message is consistent with how attribute lists are
800 * passed. Even though this means "foo : : bar" is
801 * seen as an invalid separator character.
803 char q = ((*s == '\'') ? '"' : '\'');
804 if (ckWARN(WARN_LAYER))
805 Perl_warner(aTHX_ packWARN(WARN_LAYER),
806 "perlio: invalid separator character %c%c%c in layer specification list %s",
812 } while (isALNUM(*e));
828 * It's a nul terminated string, not allowed
829 * to \ the terminating null. Anything other
830 * character is passed over.
840 if (ckWARN(WARN_LAYER))
841 Perl_warner(aTHX_ packWARN(WARN_LAYER),
842 "perlio: argument list not closed for layer \"%.*s\"",
854 bool warn_layer = ckWARN(WARN_LAYER);
855 PerlIO_funcs *layer =
856 PerlIO_find_layer(aTHX_ s, llen, 1);
858 PerlIO_list_push(aTHX_ av, layer,
865 Perl_warner(aTHX_ packWARN(WARN_LAYER), "perlio: unknown layer \"%.*s\"",
878 PerlIO_default_buffer(pTHX_ PerlIO_list_t *av)
880 PerlIO_funcs *tab = &PerlIO_perlio;
881 #ifdef PERLIO_USING_CRLF
884 if (PerlIO_stdio.Set_ptrcnt)
887 PerlIO_debug("Pushing %s\n", tab->name);
888 PerlIO_list_push(aTHX_ av, PerlIO_find_layer(aTHX_ tab->name, 0, 0),
893 PerlIO_arg_fetch(PerlIO_list_t *av, IV n)
895 return av->array[n].arg;
899 PerlIO_layer_fetch(pTHX_ PerlIO_list_t *av, IV n, PerlIO_funcs *def)
901 if (n >= 0 && n < av->cur) {
902 PerlIO_debug("Layer %" IVdf " is %s\n", n,
903 av->array[n].funcs->name);
904 return av->array[n].funcs;
907 Perl_croak(aTHX_ "panic: PerlIO layer array corrupt");
912 PerlIO_default_layers(pTHX)
914 if (!PL_def_layerlist) {
915 const char *s = (PL_tainting) ? Nullch : PerlEnv_getenv("PERLIO");
916 PerlIO_funcs *osLayer = &PerlIO_unix;
917 PL_def_layerlist = PerlIO_list_alloc(aTHX);
918 PerlIO_define_layer(aTHX_ & PerlIO_unix);
919 #if defined(WIN32) && !defined(UNDER_CE)
920 PerlIO_define_layer(aTHX_ & PerlIO_win32);
922 osLayer = &PerlIO_win32;
925 PerlIO_define_layer(aTHX_ & PerlIO_raw);
926 PerlIO_define_layer(aTHX_ & PerlIO_perlio);
927 PerlIO_define_layer(aTHX_ & PerlIO_stdio);
928 PerlIO_define_layer(aTHX_ & PerlIO_crlf);
930 PerlIO_define_layer(aTHX_ & PerlIO_mmap);
932 PerlIO_define_layer(aTHX_ & PerlIO_utf8);
933 PerlIO_define_layer(aTHX_ & PerlIO_byte);
934 PerlIO_list_push(aTHX_ PL_def_layerlist,
935 PerlIO_find_layer(aTHX_ osLayer->name, 0, 0),
938 PerlIO_parse_layers(aTHX_ PL_def_layerlist, s);
941 PerlIO_default_buffer(aTHX_ PL_def_layerlist);
944 if (PL_def_layerlist->cur < 2) {
945 PerlIO_default_buffer(aTHX_ PL_def_layerlist);
947 return PL_def_layerlist;
951 Perl_boot_core_PerlIO(pTHX)
953 #ifdef USE_ATTRIBUTES_FOR_PERLIO
954 newXS("io::MODIFY_SCALAR_ATTRIBUTES", XS_io_MODIFY_SCALAR_ATTRIBUTES,
957 newXS("PerlIO::Layer::find", XS_PerlIO__Layer__find, __FILE__);
961 PerlIO_default_layer(pTHX_ I32 n)
963 PerlIO_list_t *av = PerlIO_default_layers(aTHX);
966 return PerlIO_layer_fetch(aTHX_ av, n, &PerlIO_stdio);
969 #define PerlIO_default_top() PerlIO_default_layer(aTHX_ -1)
970 #define PerlIO_default_btm() PerlIO_default_layer(aTHX_ 0)
973 PerlIO_stdstreams(pTHX)
976 PerlIO_allocate(aTHX);
977 PerlIO_fdopen(0, "Ir" PERLIO_STDTEXT);
978 PerlIO_fdopen(1, "Iw" PERLIO_STDTEXT);
979 PerlIO_fdopen(2, "Iw" PERLIO_STDTEXT);
984 PerlIO_push(pTHX_ PerlIO *f, PerlIO_funcs *tab, const char *mode, SV *arg)
987 Newc('L',l,tab->size,char,PerlIOl);
989 Zero(l, tab->size, char);
993 PerlIO_debug("PerlIO_push f=%p %s %s %p\n", (void*)f, tab->name,
994 (mode) ? mode : "(Null)", (void*)arg);
995 if ((*l->tab->Pushed) (aTHX_ f, mode, arg) != 0) {
1004 PerlIOPop_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
1006 PerlIO_pop(aTHX_ f);
1009 PerlIO_pop(aTHX_ f);
1016 PerlIORaw_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
1019 * Remove the dummy layer
1021 PerlIO_pop(aTHX_ f);
1023 * Pop back to bottom layer
1025 if (PerlIOValid(f)) {
1027 while (!(PerlIOBase(f)->tab->kind & PERLIO_K_RAW)) {
1028 if (*PerlIONext(f)) {
1029 PerlIO_pop(aTHX_ f);
1033 * Nothing bellow - push unix on top then remove it
1035 if (PerlIO_push(aTHX_ f, PerlIO_default_btm(), mode, arg)) {
1036 PerlIO_pop(aTHX_ PerlIONext(f));
1041 PerlIO_debug(":raw f=%p :%s\n", (void*)f, PerlIOBase(f)->tab->name);
1048 PerlIO_apply_layera(pTHX_ PerlIO *f, const char *mode,
1049 PerlIO_list_t *layers, IV n, IV max)
1053 PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n, NULL);
1055 if (!PerlIO_push(aTHX_ f, tab, mode, PerlIOArg)) {
1066 PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names)
1070 PerlIO_list_t *layers = PerlIO_list_alloc(aTHX);
1071 code = PerlIO_parse_layers(aTHX_ layers, names);
1073 code = PerlIO_apply_layera(aTHX_ f, mode, layers, 0, layers->cur);
1075 PerlIO_list_free(aTHX_ layers);
1081 /*--------------------------------------------------------------------------------------*/
1083 * Given the abstraction above the public API functions
1087 PerlIO_binmode(pTHX_ PerlIO *f, int iotype, int mode, const char *names)
1089 PerlIO_debug("PerlIO_binmode f=%p %s %c %x %s\n",
1090 (void*)f, PerlIOBase(f)->tab->name, iotype, mode,
1091 (names) ? names : "(Null)");
1093 /* Do not flush etc. if (e.g.) switching encodings.
1094 if a pushed layer knows it needs to flush lower layers
1095 (for example :unix which is never going to call them)
1096 it can do the flush when it is pushed.
1098 return PerlIO_apply_layers(aTHX_ f, NULL, names) == 0 ? TRUE : FALSE;
1101 /* FIXME?: Looking down the layer stack seems wrong,
1102 but is a way of reaching past (say) an encoding layer
1103 to flip CRLF-ness of the layer(s) below
1105 #ifdef PERLIO_USING_CRLF
1106 /* Legacy binmode only has meaning if O_TEXT has a value distinct from
1107 O_BINARY so we can look for it in mode.
1109 if (!(mode & O_BINARY)) {
1112 /* Perhaps we should turn on bottom-most aware layer
1113 e.g. Ilya's idea that UNIX TTY could serve
1115 if (PerlIOBase(f)->tab->kind & PERLIO_K_CANCRLF) {
1116 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF)) {
1117 /* Not in text mode - flush any pending stuff and flip it */
1119 PerlIOBase(f)->flags |= PERLIO_F_CRLF;
1121 /* Only need to turn it on in one layer so we are done */
1126 /* Not finding a CRLF aware layer presumably means we are binary
1127 which is not what was requested - so we failed
1128 We _could_ push :crlf layer but so could caller
1133 /* Either asked for BINMODE or that is normal on this platform
1134 see if any CRLF aware layers are present and turn off the flag
1135 and possibly remove layer.
1138 if (PerlIOBase(f)->tab->kind & PERLIO_K_CANCRLF) {
1139 if ((PerlIOBase(f)->flags & PERLIO_F_CRLF)) {
1140 /* In text mode - flush any pending stuff and flip it */
1142 PerlIOBase(f)->flags &= ~PERLIO_F_CRLF;
1143 #ifndef PERLIO_USING_CRLF
1144 /* CRLF is unusual case - if this is just the :crlf layer pop it */
1145 if (PerlIOBase(f)->tab == &PerlIO_crlf) {
1146 PerlIO_pop(aTHX_ f);
1149 /* Normal case is only one layer doing this, so exit on first
1150 abnormal case can always do multiple binmode calls
1162 PerlIO__close(pTHX_ PerlIO *f)
1165 return (*PerlIOBase(f)->tab->Close) (aTHX_ f);
1167 SETERRNO(EBADF, SS$_IVCHAN);
1173 Perl_PerlIO_close(pTHX_ PerlIO *f)
1176 if (PerlIOValid(f)) {
1177 code = (*PerlIOBase(f)->tab->Close) (aTHX_ f);
1179 PerlIO_pop(aTHX_ f);
1186 Perl_PerlIO_fileno(pTHX_ PerlIO *f)
1189 return (*PerlIOBase(f)->tab->Fileno) (aTHX_ f);
1191 SETERRNO(EBADF, SS$_IVCHAN);
1197 PerlIO_context_layers(pTHX_ const char *mode)
1199 const char *type = NULL;
1201 * Need to supply default layer info from open.pm
1204 SV *layers = PL_curcop->cop_io;
1207 type = SvPV(layers, len);
1208 if (type && mode[0] != 'r') {
1210 * Skip to write part
1212 const char *s = strchr(type, 0);
1213 if (s && (STRLEN)(s - type) < len) {
1222 static PerlIO_funcs *
1223 PerlIO_layer_from_ref(pTHX_ SV *sv)
1226 * For any scalar type load the handler which is bundled with perl
1228 if (SvTYPE(sv) < SVt_PVAV)
1229 return PerlIO_find_layer(aTHX_ "Scalar", 6, 1);
1232 * For other types allow if layer is known but don't try and load it
1234 switch (SvTYPE(sv)) {
1236 return PerlIO_find_layer(aTHX_ "Array", 5, 0);
1238 return PerlIO_find_layer(aTHX_ "Hash", 4, 0);
1240 return PerlIO_find_layer(aTHX_ "Code", 4, 0);
1242 return PerlIO_find_layer(aTHX_ "Glob", 4, 0);
1248 PerlIO_resolve_layers(pTHX_ const char *layers,
1249 const char *mode, int narg, SV **args)
1251 PerlIO_list_t *def = PerlIO_default_layers(aTHX);
1254 PerlIO_stdstreams(aTHX);
1258 * If it is a reference but not an object see if we have a handler
1261 if (SvROK(arg) && !sv_isobject(arg)) {
1262 PerlIO_funcs *handler = PerlIO_layer_from_ref(aTHX_ SvRV(arg));
1264 def = PerlIO_list_alloc(aTHX);
1265 PerlIO_list_push(aTHX_ def, handler, &PL_sv_undef);
1269 * Don't fail if handler cannot be found :Via(...) etc. may do
1270 * something sensible else we will just stringfy and open
1276 layers = PerlIO_context_layers(aTHX_ mode);
1277 if (layers && *layers) {
1281 av = PerlIO_list_alloc(aTHX);
1282 for (i = 0; i < def->cur; i++) {
1283 PerlIO_list_push(aTHX_ av, def->array[i].funcs,
1290 PerlIO_parse_layers(aTHX_ av, layers);
1301 PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd,
1302 int imode, int perm, PerlIO *f, int narg, SV **args)
1304 if (!f && narg == 1 && *args == &PL_sv_undef) {
1305 if ((f = PerlIO_tmpfile())) {
1307 layers = PerlIO_context_layers(aTHX_ mode);
1308 if (layers && *layers)
1309 PerlIO_apply_layers(aTHX_ f, mode, layers);
1313 PerlIO_list_t *layera = NULL;
1315 PerlIO_funcs *tab = NULL;
1316 if (PerlIOValid(f)) {
1318 * This is "reopen" - it is not tested as perl does not use it
1322 layera = PerlIO_list_alloc(aTHX);
1324 SV *arg = (l->tab->Getarg)
1325 ? (*l->tab->Getarg) (aTHX_ &l, NULL, 0)
1327 PerlIO_list_push(aTHX_ layera, l->tab, arg);
1328 l = *PerlIONext(&l);
1332 layera = PerlIO_resolve_layers(aTHX_ layers, mode, narg, args);
1335 * Start at "top" of layer stack
1337 n = layera->cur - 1;
1339 PerlIO_funcs *t = PerlIO_layer_fetch(aTHX_ layera, n, NULL);
1348 * Found that layer 'n' can do opens - call it
1350 if (narg > 1 && !(tab->kind & PERLIO_K_MULTIARG)) {
1351 Perl_croak(aTHX_ "More than one argument to open(,':%s')",tab->name);
1353 PerlIO_debug("openn(%s,'%s','%s',%d,%x,%o,%p,%d,%p)\n",
1354 tab->name, layers, mode, fd, imode, perm,
1355 (void*)f, narg, (void*)args);
1356 f = (*tab->Open) (aTHX_ tab, layera, n, mode, fd, imode, perm,
1359 if (n + 1 < layera->cur) {
1361 * More layers above the one that we used to open -
1364 if (PerlIO_apply_layera(aTHX_ f, mode, layera, n + 1, layera->cur) != 0) {
1365 /* If pushing layers fails close the file */
1372 PerlIO_list_free(aTHX_ layera);
1379 Perl_PerlIO_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
1382 return (*PerlIOBase(f)->tab->Read) (aTHX_ f, vbuf, count);
1384 SETERRNO(EBADF, SS$_IVCHAN);
1390 Perl_PerlIO_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
1393 return (*PerlIOBase(f)->tab->Unread) (aTHX_ f, vbuf, count);
1395 SETERRNO(EBADF, SS$_IVCHAN);
1401 Perl_PerlIO_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
1404 return (*PerlIOBase(f)->tab->Write) (aTHX_ f, vbuf, count);
1406 SETERRNO(EBADF, SS$_IVCHAN);
1412 Perl_PerlIO_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
1415 return (*PerlIOBase(f)->tab->Seek) (aTHX_ f, offset, whence);
1417 SETERRNO(EBADF, SS$_IVCHAN);
1423 Perl_PerlIO_tell(pTHX_ PerlIO *f)
1426 return (*PerlIOBase(f)->tab->Tell) (aTHX_ f);
1428 SETERRNO(EBADF, SS$_IVCHAN);
1434 Perl_PerlIO_flush(pTHX_ PerlIO *f)
1438 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1439 if (tab && tab->Flush) {
1440 return (*tab->Flush) (aTHX_ f);
1443 PerlIO_debug("Cannot flush f=%p :%s\n", (void*)f, tab->name);
1444 SETERRNO(EBADF, SS$_IVCHAN);
1449 PerlIO_debug("Cannot flush f=%p\n", (void*)f);
1450 SETERRNO(EBADF, SS$_IVCHAN);
1456 * Is it good API design to do flush-all on NULL, a potentially
1457 * errorneous input? Maybe some magical value (PerlIO*
1458 * PERLIO_FLUSH_ALL = (PerlIO*)-1;)? Yes, stdio does similar
1459 * things on fflush(NULL), but should we be bound by their design
1462 PerlIO **table = &PL_perlio;
1464 while ((f = *table)) {
1466 table = (PerlIO **) (f++);
1467 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
1468 if (*f && PerlIO_flush(f) != 0)
1478 PerlIOBase_flush_linebuf(pTHX)
1480 PerlIO **table = &PL_perlio;
1482 while ((f = *table)) {
1484 table = (PerlIO **) (f++);
1485 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
1488 flags & (PERLIO_F_LINEBUF | PERLIO_F_CANWRITE))
1489 == (PERLIO_F_LINEBUF | PERLIO_F_CANWRITE))
1497 Perl_PerlIO_fill(pTHX_ PerlIO *f)
1500 return (*PerlIOBase(f)->tab->Fill) (aTHX_ f);
1502 SETERRNO(EBADF, SS$_IVCHAN);
1508 PerlIO_isutf8(PerlIO *f)
1511 return (PerlIOBase(f)->flags & PERLIO_F_UTF8) != 0;
1513 SETERRNO(EBADF, SS$_IVCHAN);
1519 Perl_PerlIO_eof(pTHX_ PerlIO *f)
1522 return (*PerlIOBase(f)->tab->Eof) (aTHX_ f);
1524 SETERRNO(EBADF, SS$_IVCHAN);
1530 Perl_PerlIO_error(pTHX_ PerlIO *f)
1533 return (*PerlIOBase(f)->tab->Error) (aTHX_ f);
1535 SETERRNO(EBADF, SS$_IVCHAN);
1541 Perl_PerlIO_clearerr(pTHX_ PerlIO *f)
1544 (*PerlIOBase(f)->tab->Clearerr) (aTHX_ f);
1546 SETERRNO(EBADF, SS$_IVCHAN);
1550 Perl_PerlIO_setlinebuf(pTHX_ PerlIO *f)
1553 (*PerlIOBase(f)->tab->Setlinebuf) (aTHX_ f);
1555 SETERRNO(EBADF, SS$_IVCHAN);
1559 PerlIO_has_base(PerlIO *f)
1561 if (PerlIOValid(f)) {
1562 return (PerlIOBase(f)->tab->Get_base != NULL);
1568 PerlIO_fast_gets(PerlIO *f)
1570 if (PerlIOValid(f) && (PerlIOBase(f)->flags & PERLIO_F_FASTGETS)) {
1571 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1572 return (tab->Set_ptrcnt != NULL);
1578 PerlIO_has_cntptr(PerlIO *f)
1580 if (PerlIOValid(f)) {
1581 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1582 return (tab->Get_ptr != NULL && tab->Get_cnt != NULL);
1588 PerlIO_canset_cnt(PerlIO *f)
1590 if (PerlIOValid(f)) {
1591 PerlIOl *l = PerlIOBase(f);
1592 return (l->tab->Set_ptrcnt != NULL);
1598 Perl_PerlIO_get_base(pTHX_ PerlIO *f)
1601 return (*PerlIOBase(f)->tab->Get_base) (aTHX_ f);
1606 Perl_PerlIO_get_bufsiz(pTHX_ PerlIO *f)
1609 return (*PerlIOBase(f)->tab->Get_bufsiz) (aTHX_ f);
1614 Perl_PerlIO_get_ptr(pTHX_ PerlIO *f)
1616 if (PerlIOValid(f)) {
1617 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1618 if (tab->Get_ptr == NULL)
1620 return (*tab->Get_ptr) (aTHX_ f);
1626 Perl_PerlIO_get_cnt(pTHX_ PerlIO *f)
1628 if (PerlIOValid(f)) {
1629 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1630 if (tab->Get_cnt == NULL)
1632 return (*tab->Get_cnt) (aTHX_ f);
1638 Perl_PerlIO_set_cnt(pTHX_ PerlIO *f, int cnt)
1640 if (PerlIOValid(f)) {
1641 (*PerlIOBase(f)->tab->Set_ptrcnt) (aTHX_ f, NULL, cnt);
1646 Perl_PerlIO_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, int cnt)
1648 if (PerlIOValid(f)) {
1649 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1650 if (tab->Set_ptrcnt == NULL) {
1651 Perl_croak(aTHX_ "PerlIO buffer snooping abuse");
1653 (*PerlIOBase(f)->tab->Set_ptrcnt) (aTHX_ f, ptr, cnt);
1657 /*--------------------------------------------------------------------------------------*/
1659 * utf8 and raw dummy layers
1663 PerlIOUtf8_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
1665 if (*PerlIONext(f)) {
1666 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1667 PerlIO_pop(aTHX_ f);
1668 if (tab->kind & PERLIO_K_UTF8)
1669 PerlIOBase(f)->flags |= PERLIO_F_UTF8;
1671 PerlIOBase(f)->flags &= ~PERLIO_F_UTF8;
1677 PerlIO_funcs PerlIO_utf8 = {
1680 PERLIO_K_DUMMY | PERLIO_F_UTF8,
1698 NULL, /* get_base */
1699 NULL, /* get_bufsiz */
1702 NULL, /* set_ptrcnt */
1705 PerlIO_funcs PerlIO_byte = {
1726 NULL, /* get_base */
1727 NULL, /* get_bufsiz */
1730 NULL, /* set_ptrcnt */
1734 PerlIORaw_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
1735 IV n, const char *mode, int fd, int imode, int perm,
1736 PerlIO *old, int narg, SV **args)
1738 PerlIO_funcs *tab = PerlIO_default_btm();
1739 return (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
1743 PerlIO_funcs PerlIO_raw = {
1764 NULL, /* get_base */
1765 NULL, /* get_bufsiz */
1768 NULL, /* set_ptrcnt */
1770 /*--------------------------------------------------------------------------------------*/
1771 /*--------------------------------------------------------------------------------------*/
1773 * "Methods" of the "base class"
1777 PerlIOBase_fileno(pTHX_ PerlIO *f)
1779 return PerlIOValid(f) ? PerlIO_fileno(PerlIONext(f)) : -1;
1783 PerlIO_modestr(PerlIO *f, char *buf)
1786 IV flags = PerlIOBase(f)->flags;
1787 if (flags & PERLIO_F_APPEND) {
1789 if (flags & PERLIO_F_CANREAD) {
1793 else if (flags & PERLIO_F_CANREAD) {
1795 if (flags & PERLIO_F_CANWRITE)
1798 else if (flags & PERLIO_F_CANWRITE) {
1800 if (flags & PERLIO_F_CANREAD) {
1804 #ifdef PERLIO_USING_CRLF
1805 if (!(flags & PERLIO_F_CRLF))
1813 PerlIOBase_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
1815 PerlIOl *l = PerlIOBase(f);
1817 const char *omode = mode;
1820 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1821 l->flags &= ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE |
1822 PERLIO_F_TRUNCATE | PERLIO_F_APPEND);
1823 if (tab->Set_ptrcnt != NULL)
1824 l->flags |= PERLIO_F_FASTGETS;
1826 if (*mode == '#' || *mode == 'I')
1830 l->flags |= PERLIO_F_CANREAD;
1833 l->flags |= PERLIO_F_APPEND | PERLIO_F_CANWRITE;
1836 l->flags |= PERLIO_F_TRUNCATE | PERLIO_F_CANWRITE;
1839 SETERRNO(EINVAL, LIB$_INVARG);
1845 l->flags |= PERLIO_F_CANREAD | PERLIO_F_CANWRITE;
1848 l->flags &= ~PERLIO_F_CRLF;
1851 l->flags |= PERLIO_F_CRLF;
1854 SETERRNO(EINVAL, LIB$_INVARG);
1861 l->flags |= l->next->flags &
1862 (PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_TRUNCATE |
1867 PerlIO_debug("PerlIOBase_pushed f=%p %s %s fl=%08" UVxf " (%s)\n",
1868 f, PerlIOBase(f)->tab->name, (omode) ? omode : "(Null)",
1869 l->flags, PerlIO_modestr(f, temp));
1875 PerlIOBase_popped(pTHX_ PerlIO *f)
1881 PerlIOBase_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
1884 * Save the position as current head considers it
1886 Off_t old = PerlIO_tell(f);
1888 PerlIO_push(aTHX_ f, &PerlIO_pending, "r", Nullsv);
1889 PerlIOSelf(f, PerlIOBuf)->posn = old;
1890 done = PerlIOBuf_unread(aTHX_ f, vbuf, count);
1895 PerlIOBase_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
1897 STDCHAR *buf = (STDCHAR *) vbuf;
1899 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD))
1902 SSize_t avail = PerlIO_get_cnt(f);
1905 take = ((SSize_t)count < avail) ? count : avail;
1907 STDCHAR *ptr = PerlIO_get_ptr(f);
1908 Copy(ptr, buf, take, STDCHAR);
1909 PerlIO_set_ptrcnt(f, ptr + take, (avail -= take));
1913 if (count > 0 && avail <= 0) {
1914 if (PerlIO_fill(f) != 0)
1918 return (buf - (STDCHAR *) vbuf);
1924 PerlIOBase_noop_ok(pTHX_ PerlIO *f)
1930 PerlIOBase_noop_fail(pTHX_ PerlIO *f)
1936 PerlIOBase_close(pTHX_ PerlIO *f)
1939 PerlIO *n = PerlIONext(f);
1940 if (PerlIO_flush(f) != 0)
1942 if (PerlIOValid(n) && (*PerlIOBase(n)->tab->Close)(aTHX_ n) != 0)
1944 PerlIOBase(f)->flags &=
1945 ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_OPEN);
1950 PerlIOBase_eof(pTHX_ PerlIO *f)
1952 if (PerlIOValid(f)) {
1953 return (PerlIOBase(f)->flags & PERLIO_F_EOF) != 0;
1959 PerlIOBase_error(pTHX_ PerlIO *f)
1961 if (PerlIOValid(f)) {
1962 return (PerlIOBase(f)->flags & PERLIO_F_ERROR) != 0;
1968 PerlIOBase_clearerr(pTHX_ PerlIO *f)
1970 if (PerlIOValid(f)) {
1971 PerlIO *n = PerlIONext(f);
1972 PerlIOBase(f)->flags &= ~(PERLIO_F_ERROR | PERLIO_F_EOF);
1979 PerlIOBase_setlinebuf(pTHX_ PerlIO *f)
1981 if (PerlIOValid(f)) {
1982 PerlIOBase(f)->flags |= PERLIO_F_LINEBUF;
1987 PerlIO_sv_dup(pTHX_ SV *arg, CLONE_PARAMS *param)
1993 return sv_dup(arg, param);
1996 return newSVsv(arg);
1999 return newSVsv(arg);
2004 PerlIOBase_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
2006 PerlIO *nexto = PerlIONext(o);
2007 if (PerlIOValid(nexto)) {
2008 PerlIO_funcs *tab = PerlIOBase(nexto)->tab;
2009 f = (*tab->Dup)(aTHX_ f, nexto, param, flags);
2012 PerlIO_funcs *self = PerlIOBase(o)->tab;
2015 PerlIO_debug("PerlIOBase_dup %s f=%p o=%p param=%p\n",
2016 self->name, (void*)f, (void*)o, (void*)param);
2018 arg = (*self->Getarg)(aTHX_ o,param,flags);
2020 f = PerlIO_push(aTHX_ f, self, PerlIO_modestr(o,buf), arg);
2028 #define PERLIO_MAX_REFCOUNTABLE_FD 2048
2030 perl_mutex PerlIO_mutex;
2032 int PerlIO_fd_refcnt[PERLIO_MAX_REFCOUNTABLE_FD];
2037 /* Place holder for stdstreams call ??? */
2039 MUTEX_INIT(&PerlIO_mutex);
2044 PerlIOUnix_refcnt_inc(int fd)
2046 if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
2048 MUTEX_LOCK(&PerlIO_mutex);
2050 PerlIO_fd_refcnt[fd]++;
2051 PerlIO_debug("fd %d refcnt=%d\n",fd,PerlIO_fd_refcnt[fd]);
2053 MUTEX_UNLOCK(&PerlIO_mutex);
2059 PerlIOUnix_refcnt_dec(int fd)
2062 if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
2064 MUTEX_LOCK(&PerlIO_mutex);
2066 cnt = --PerlIO_fd_refcnt[fd];
2067 PerlIO_debug("fd %d refcnt=%d\n",fd,cnt);
2069 MUTEX_UNLOCK(&PerlIO_mutex);
2076 PerlIO_cleanup(pTHX)
2080 PerlIO_debug("Cleanup layers for %p\n",aTHX);
2082 PerlIO_debug("Cleanup layers\n");
2084 /* Raise STDIN..STDERR refcount so we don't close them */
2085 for (i=0; i < 3; i++)
2086 PerlIOUnix_refcnt_inc(i);
2087 PerlIO_cleantable(aTHX_ &PL_perlio);
2088 /* Restore STDIN..STDERR refcount */
2089 for (i=0; i < 3; i++)
2090 PerlIOUnix_refcnt_dec(i);
2092 if (PL_known_layers) {
2093 PerlIO_list_free(aTHX_ PL_known_layers);
2094 PL_known_layers = NULL;
2096 if(PL_def_layerlist) {
2097 PerlIO_list_free(aTHX_ PL_def_layerlist);
2098 PL_def_layerlist = NULL;
2104 /*--------------------------------------------------------------------------------------*/
2106 * Bottom-most level for UNIX-like case
2110 struct _PerlIO base; /* The generic part */
2111 int fd; /* UNIX like file descriptor */
2112 int oflags; /* open/fcntl flags */
2116 PerlIOUnix_oflags(const char *mode)
2119 if (*mode == 'I' || *mode == '#')
2124 if (*++mode == '+') {
2131 oflags = O_CREAT | O_TRUNC;
2132 if (*++mode == '+') {
2141 oflags = O_CREAT | O_APPEND;
2142 if (*++mode == '+') {
2155 else if (*mode == 't') {
2157 oflags &= ~O_BINARY;
2161 * Always open in binary mode
2164 if (*mode || oflags == -1) {
2165 SETERRNO(EINVAL, LIB$_INVARG);
2172 PerlIOUnix_fileno(pTHX_ PerlIO *f)
2174 return PerlIOSelf(f, PerlIOUnix)->fd;
2178 PerlIOUnix_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
2180 IV code = PerlIOBase_pushed(aTHX_ f, mode, arg);
2181 PerlIOUnix *s = PerlIOSelf(f, PerlIOUnix);
2182 if (*PerlIONext(f)) {
2183 /* We never call down so any pending stuff now */
2184 PerlIO_flush(PerlIONext(f));
2185 s->fd = PerlIO_fileno(PerlIONext(f));
2187 * XXX could (or should) we retrieve the oflags from the open file
2188 * handle rather than believing the "mode" we are passed in? XXX
2189 * Should the value on NULL mode be 0 or -1?
2191 s->oflags = mode ? PerlIOUnix_oflags(mode) : -1;
2193 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
2198 PerlIOUnix_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2199 IV n, const char *mode, int fd, int imode,
2200 int perm, PerlIO *f, int narg, SV **args)
2202 if (PerlIOValid(f)) {
2203 if (PerlIOBase(f)->flags & PERLIO_F_OPEN)
2204 (*PerlIOBase(f)->tab->Close)(aTHX_ f);
2207 char *path = SvPV_nolen(*args);
2211 imode = PerlIOUnix_oflags(mode);
2215 fd = PerlLIO_open3(path, imode, perm);
2223 f = PerlIO_allocate(aTHX);
2225 if (!PerlIOValid(f)) {
2226 s = PerlIOSelf(PerlIO_push(aTHX_ f, self, mode, PerlIOArg),
2230 s = PerlIOSelf(f, PerlIOUnix);
2234 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
2235 PerlIOUnix_refcnt_inc(fd);
2241 * FIXME: pop layers ???
2249 PerlIOUnix_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
2251 PerlIOUnix *os = PerlIOSelf(o, PerlIOUnix);
2253 if (flags & PERLIO_DUP_FD) {
2254 fd = PerlLIO_dup(fd);
2256 if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
2257 f = PerlIOBase_dup(aTHX_ f, o, param, flags);
2259 /* If all went well overwrite fd in dup'ed lay with the dup()'ed fd */
2260 PerlIOUnix *s = PerlIOSelf(f, PerlIOUnix);
2262 PerlIOUnix_refcnt_inc(fd);
2271 PerlIOUnix_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
2273 int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2274 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD))
2277 SSize_t len = PerlLIO_read(fd, vbuf, count);
2278 if (len >= 0 || errno != EINTR) {
2280 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2281 else if (len == 0 && count != 0)
2282 PerlIOBase(f)->flags |= PERLIO_F_EOF;
2290 PerlIOUnix_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
2292 int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2294 SSize_t len = PerlLIO_write(fd, vbuf, count);
2295 if (len >= 0 || errno != EINTR) {
2297 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2305 PerlIOUnix_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
2308 PerlLIO_lseek(PerlIOSelf(f, PerlIOUnix)->fd, offset, whence);
2309 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
2310 return (new == (Off_t) - 1) ? -1 : 0;
2314 PerlIOUnix_tell(pTHX_ PerlIO *f)
2316 return PerlLIO_lseek(PerlIOSelf(f, PerlIOUnix)->fd, 0, SEEK_CUR);
2321 PerlIOUnix_close(pTHX_ PerlIO *f)
2323 int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2325 if (PerlIOBase(f)->flags & PERLIO_F_OPEN) {
2326 if (PerlIOUnix_refcnt_dec(fd) > 0) {
2327 PerlIOBase(f)->flags &= ~PERLIO_F_OPEN;
2332 SETERRNO(EBADF,SS$_IVCHAN);
2335 while (PerlLIO_close(fd) != 0) {
2336 if (errno != EINTR) {
2343 PerlIOBase(f)->flags &= ~PERLIO_F_OPEN;
2348 PerlIO_funcs PerlIO_unix = {
2364 PerlIOBase_noop_ok, /* flush */
2365 PerlIOBase_noop_fail, /* fill */
2368 PerlIOBase_clearerr,
2369 PerlIOBase_setlinebuf,
2370 NULL, /* get_base */
2371 NULL, /* get_bufsiz */
2374 NULL, /* set_ptrcnt */
2377 /*--------------------------------------------------------------------------------------*/
2383 struct _PerlIO base;
2384 FILE *stdio; /* The stream */
2388 PerlIOStdio_fileno(pTHX_ PerlIO *f)
2390 return PerlSIO_fileno(PerlIOSelf(f, PerlIOStdio)->stdio);
2394 PerlIOStdio_mode(const char *mode, char *tmode)
2400 #ifdef PERLIO_USING_CRLF
2408 * This isn't used yet ...
2411 PerlIOStdio_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
2413 if (*PerlIONext(f)) {
2414 PerlIOStdio *s = PerlIOSelf(f, PerlIOStdio);
2417 PerlSIO_fdopen(PerlIO_fileno(PerlIONext(f)), mode =
2418 PerlIOStdio_mode(mode, tmode));
2421 /* We never call down so any pending stuff now */
2422 PerlIO_flush(PerlIONext(f));
2427 return PerlIOBase_pushed(aTHX_ f, mode, arg);
2431 PerlIO_importFILE(FILE *stdio, int fl)
2437 PerlIOSelf(PerlIO_push
2438 (aTHX_(f = PerlIO_allocate(aTHX)), &PerlIO_stdio,
2439 "r+", Nullsv), PerlIOStdio);
2446 PerlIOStdio_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2447 IV n, const char *mode, int fd, int imode,
2448 int perm, PerlIO *f, int narg, SV **args)
2451 if (PerlIOValid(f)) {
2452 char *path = SvPV_nolen(*args);
2453 PerlIOStdio *s = PerlIOSelf(f, PerlIOStdio);
2455 PerlIOUnix_refcnt_dec(fileno(s->stdio));
2456 stdio = PerlSIO_freopen(path, (mode = PerlIOStdio_mode(mode, tmode)),
2461 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2466 char *path = SvPV_nolen(*args);
2469 fd = PerlLIO_open3(path, imode, perm);
2472 FILE *stdio = PerlSIO_fopen(path, mode);
2476 f = PerlIO_allocate(aTHX);
2478 s = PerlIOSelf(PerlIO_push(aTHX_ f, self,
2479 (mode = PerlIOStdio_mode(mode, tmode)),
2483 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2498 stdio = PerlSIO_stdin;
2501 stdio = PerlSIO_stdout;
2504 stdio = PerlSIO_stderr;
2509 stdio = PerlSIO_fdopen(fd, mode =
2510 PerlIOStdio_mode(mode, tmode));
2515 f = PerlIO_allocate(aTHX);
2517 s = PerlIOSelf(PerlIO_push(aTHX_ f, self, mode, PerlIOArg), PerlIOStdio);
2519 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2528 PerlIOStdio_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
2530 /* This assumes no layers underneath - which is what
2531 happens, but is not how I remember it. NI-S 2001/10/16
2533 if ((f = PerlIOBase_dup(aTHX_ f, o, param, flags))) {
2534 FILE *stdio = PerlIOSelf(o, PerlIOStdio)->stdio;
2535 if (flags & PERLIO_DUP_FD) {
2536 int fd = PerlLIO_dup(fileno(stdio));
2539 stdio = fdopen(fd, PerlIO_modestr(o,mode));
2542 /* FIXME: To avoid messy error recovery if dup fails
2543 re-use the existing stdio as though flag was not set
2547 PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
2548 PerlIOUnix_refcnt_inc(fileno(stdio));
2554 PerlIOStdio_close(pTHX_ PerlIO *f)
2556 #ifdef SOCKS5_VERSION_NAME
2558 Sock_size_t optlen = sizeof(int);
2560 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2561 if (PerlIOUnix_refcnt_dec(fileno(stdio)) > 0) {
2562 /* Do not close it but do flush any buffers */
2563 return PerlIO_flush(f);
2566 #ifdef SOCKS5_VERSION_NAME
2568 (PerlIO_fileno(f), SOL_SOCKET, SO_TYPE, (void *) &optval,
2570 0) ? PerlSIO_fclose(stdio) : close(PerlIO_fileno(f))
2572 PerlSIO_fclose(stdio)
2581 PerlIOStdio_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
2583 FILE *s = PerlIOSelf(f, PerlIOStdio)->stdio;
2586 STDCHAR *buf = (STDCHAR *) vbuf;
2588 * Perl is expecting PerlIO_getc() to fill the buffer Linux's
2589 * stdio does not do that for fread()
2591 int ch = PerlSIO_fgetc(s);
2598 got = PerlSIO_fread(vbuf, 1, count, s);
2603 PerlIOStdio_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
2605 FILE *s = PerlIOSelf(f, PerlIOStdio)->stdio;
2606 STDCHAR *buf = ((STDCHAR *) vbuf) + count - 1;
2609 int ch = *buf-- & 0xff;
2610 if (PerlSIO_ungetc(ch, s) != ch)
2619 PerlIOStdio_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
2621 return PerlSIO_fwrite(vbuf, 1, count,
2622 PerlIOSelf(f, PerlIOStdio)->stdio);
2626 PerlIOStdio_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
2628 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2629 return PerlSIO_fseek(stdio, offset, whence);
2633 PerlIOStdio_tell(pTHX_ PerlIO *f)
2635 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2636 return PerlSIO_ftell(stdio);
2640 PerlIOStdio_flush(pTHX_ PerlIO *f)
2642 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2643 if (PerlIOBase(f)->flags & PERLIO_F_CANWRITE) {
2644 return PerlSIO_fflush(stdio);
2649 * FIXME: This discards ungetc() and pre-read stuff which is not
2650 * right if this is just a "sync" from a layer above Suspect right
2651 * design is to do _this_ but not have layer above flush this
2652 * layer read-to-read
2655 * Not writeable - sync by attempting a seek
2658 if (PerlSIO_fseek(stdio, (Off_t) 0, SEEK_CUR) != 0)
2666 PerlIOStdio_fill(pTHX_ PerlIO *f)
2668 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2671 * fflush()ing read-only streams can cause trouble on some stdio-s
2673 if ((PerlIOBase(f)->flags & PERLIO_F_CANWRITE)) {
2674 if (PerlSIO_fflush(stdio) != 0)
2677 c = PerlSIO_fgetc(stdio);
2678 if (c == EOF || PerlSIO_ungetc(c, stdio) != c)
2684 PerlIOStdio_eof(pTHX_ PerlIO *f)
2686 return PerlSIO_feof(PerlIOSelf(f, PerlIOStdio)->stdio);
2690 PerlIOStdio_error(pTHX_ PerlIO *f)
2692 return PerlSIO_ferror(PerlIOSelf(f, PerlIOStdio)->stdio);
2696 PerlIOStdio_clearerr(pTHX_ PerlIO *f)
2698 PerlSIO_clearerr(PerlIOSelf(f, PerlIOStdio)->stdio);
2702 PerlIOStdio_setlinebuf(pTHX_ PerlIO *f)
2704 #ifdef HAS_SETLINEBUF
2705 PerlSIO_setlinebuf(PerlIOSelf(f, PerlIOStdio)->stdio);
2707 PerlSIO_setvbuf(PerlIOSelf(f, PerlIOStdio)->stdio, Nullch, _IOLBF, 0);
2713 PerlIOStdio_get_base(pTHX_ PerlIO *f)
2715 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2716 return (STDCHAR*)PerlSIO_get_base(stdio);
2720 PerlIOStdio_get_bufsiz(pTHX_ PerlIO *f)
2722 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2723 return PerlSIO_get_bufsiz(stdio);
2727 #ifdef USE_STDIO_PTR
2729 PerlIOStdio_get_ptr(pTHX_ PerlIO *f)
2731 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2732 return (STDCHAR*)PerlSIO_get_ptr(stdio);
2736 PerlIOStdio_get_cnt(pTHX_ PerlIO *f)
2738 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2739 return PerlSIO_get_cnt(stdio);
2743 PerlIOStdio_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
2745 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2747 #ifdef STDIO_PTR_LVALUE
2748 PerlSIO_set_ptr(stdio, (void*)ptr); /* LHS STDCHAR* cast non-portable */
2749 #ifdef STDIO_PTR_LVAL_SETS_CNT
2750 if (PerlSIO_get_cnt(stdio) != (cnt)) {
2751 assert(PerlSIO_get_cnt(stdio) == (cnt));
2754 #if (!defined(STDIO_PTR_LVAL_NOCHANGE_CNT))
2756 * Setting ptr _does_ change cnt - we are done
2760 #else /* STDIO_PTR_LVALUE */
2762 #endif /* STDIO_PTR_LVALUE */
2765 * Now (or only) set cnt
2767 #ifdef STDIO_CNT_LVALUE
2768 PerlSIO_set_cnt(stdio, cnt);
2769 #else /* STDIO_CNT_LVALUE */
2770 #if (defined(STDIO_PTR_LVALUE) && defined(STDIO_PTR_LVAL_SETS_CNT))
2771 PerlSIO_set_ptr(stdio,
2772 PerlSIO_get_ptr(stdio) + (PerlSIO_get_cnt(stdio) -
2774 #else /* STDIO_PTR_LVAL_SETS_CNT */
2776 #endif /* STDIO_PTR_LVAL_SETS_CNT */
2777 #endif /* STDIO_CNT_LVALUE */
2782 PerlIO_funcs PerlIO_stdio = {
2784 sizeof(PerlIOStdio),
2802 PerlIOStdio_clearerr,
2803 PerlIOStdio_setlinebuf,
2805 PerlIOStdio_get_base,
2806 PerlIOStdio_get_bufsiz,
2811 #ifdef USE_STDIO_PTR
2812 PerlIOStdio_get_ptr,
2813 PerlIOStdio_get_cnt,
2814 #if (defined(STDIO_PTR_LVALUE) && (defined(STDIO_CNT_LVALUE) || defined(STDIO_PTR_LVAL_SETS_CNT)))
2815 PerlIOStdio_set_ptrcnt
2816 #else /* STDIO_PTR_LVALUE */
2818 #endif /* STDIO_PTR_LVALUE */
2819 #else /* USE_STDIO_PTR */
2823 #endif /* USE_STDIO_PTR */
2827 PerlIO_exportFILE(PerlIO *f, int fl)
2832 stdio = fdopen(PerlIO_fileno(f), "r+");
2835 PerlIOSelf(PerlIO_push(aTHX_ f, &PerlIO_stdio, "r+", Nullsv),
2843 PerlIO_findFILE(PerlIO *f)
2847 if (l->tab == &PerlIO_stdio) {
2848 PerlIOStdio *s = PerlIOSelf(&l, PerlIOStdio);
2851 l = *PerlIONext(&l);
2853 return PerlIO_exportFILE(f, 0);
2857 PerlIO_releaseFILE(PerlIO *p, FILE *f)
2861 /*--------------------------------------------------------------------------------------*/
2863 * perlio buffer layer
2867 PerlIOBuf_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
2869 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
2870 int fd = PerlIO_fileno(f);
2872 if (fd >= 0 && PerlLIO_isatty(fd)) {
2873 PerlIOBase(f)->flags |= PERLIO_F_LINEBUF | PERLIO_F_TTY;
2875 posn = PerlIO_tell(PerlIONext(f));
2876 if (posn != (Off_t) - 1) {
2879 return PerlIOBase_pushed(aTHX_ f, mode, arg);
2883 PerlIOBuf_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2884 IV n, const char *mode, int fd, int imode, int perm,
2885 PerlIO *f, int narg, SV **args)
2887 if (PerlIOValid(f)) {
2888 PerlIO *next = PerlIONext(f);
2889 PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIOBase(next)->tab);
2890 next = (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
2892 if (!next || (*PerlIOBase(f)->tab->Pushed) (aTHX_ f, mode, PerlIOArg) != 0) {
2897 PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIO_default_btm());
2905 f = (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
2908 if (PerlIO_push(aTHX_ f, self, mode, PerlIOArg) == 0) {
2910 * if push fails during open, open fails. close will pop us.
2915 fd = PerlIO_fileno(f);
2916 if (init && fd == 2) {
2918 * Initial stderr is unbuffered
2920 PerlIOBase(f)->flags |= PERLIO_F_UNBUF;
2922 #ifdef PERLIO_USING_CRLF
2923 # ifdef PERLIO_IS_BINMODE_FD
2924 if (PERLIO_IS_BINMODE_FD(fd))
2925 PerlIO_binmode(f, '<'/*not used*/, O_BINARY, Nullch);
2929 * do something about failing setmode()? --jhi
2931 PerlLIO_setmode(fd, O_BINARY);
2940 * This "flush" is akin to sfio's sync in that it handles files in either
2941 * read or write state
2944 PerlIOBuf_flush(pTHX_ PerlIO *f)
2946 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
2948 PerlIO *n = PerlIONext(f);
2949 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF) {
2951 * write() the buffer
2953 STDCHAR *buf = b->buf;
2955 while (p < b->ptr) {
2956 SSize_t count = PerlIO_write(n, p, b->ptr - p);
2960 else if (count < 0 || PerlIO_error(n)) {
2961 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2966 b->posn += (p - buf);
2968 else if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
2969 STDCHAR *buf = PerlIO_get_base(f);
2971 * Note position change
2973 b->posn += (b->ptr - buf);
2974 if (b->ptr < b->end) {
2976 * We did not consume all of it
2978 if (PerlIO_seek(n, b->posn, SEEK_SET) == 0) {
2979 /* Reload n as some layers may pop themselves on seek */
2980 b->posn = PerlIO_tell(n = PerlIONext(f));
2984 b->ptr = b->end = b->buf;
2985 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
2986 /* We check for Valid because of dubious decision to make PerlIO_flush(NULL) flush all */
2987 /* FIXME: Doing downstream flush may be sub-optimal see PerlIOBuf_fill() below */
2988 if (PerlIOValid(n) && PerlIO_flush(n) != 0)
2994 PerlIOBuf_fill(pTHX_ PerlIO *f)
2996 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
2997 PerlIO *n = PerlIONext(f);
3000 * FIXME: doing the down-stream flush maybe sub-optimal if it causes
3001 * pre-read data in stdio buffer to be discarded.
3002 * However, skipping the flush also skips _our_ hosekeeping
3003 * and breaks tell tests. So we do the flush.
3005 if (PerlIO_flush(f) != 0)
3007 if (PerlIOBase(f)->flags & PERLIO_F_TTY)
3008 PerlIOBase_flush_linebuf(aTHX);
3011 PerlIO_get_base(f); /* allocate via vtable */
3013 b->ptr = b->end = b->buf;
3014 if (PerlIO_fast_gets(n)) {
3016 * Layer below is also buffered. We do _NOT_ want to call its
3017 * ->Read() because that will loop till it gets what we asked for
3018 * which may hang on a pipe etc. Instead take anything it has to
3019 * hand, or ask it to fill _once_.
3021 avail = PerlIO_get_cnt(n);
3023 avail = PerlIO_fill(n);
3025 avail = PerlIO_get_cnt(n);
3027 if (!PerlIO_error(n) && PerlIO_eof(n))
3032 STDCHAR *ptr = PerlIO_get_ptr(n);
3033 SSize_t cnt = avail;
3034 if (avail > (SSize_t)b->bufsiz)
3036 Copy(ptr, b->buf, avail, STDCHAR);
3037 PerlIO_set_ptrcnt(n, ptr + avail, cnt - avail);
3041 avail = PerlIO_read(n, b->ptr, b->bufsiz);
3045 PerlIOBase(f)->flags |= PERLIO_F_EOF;
3047 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
3050 b->end = b->buf + avail;
3051 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3056 PerlIOBuf_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
3058 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3059 if (PerlIOValid(f)) {
3062 return PerlIOBase_read(aTHX_ f, vbuf, count);
3068 PerlIOBuf_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3070 const STDCHAR *buf = (const STDCHAR *) vbuf + count;
3071 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3074 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
3079 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
3081 * Buffer is already a read buffer, we can overwrite any chars
3082 * which have been read back to buffer start
3084 avail = (b->ptr - b->buf);
3088 * Buffer is idle, set it up so whole buffer is available for
3092 b->end = b->buf + avail;
3094 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3096 * Buffer extends _back_ from where we are now
3098 b->posn -= b->bufsiz;
3100 if (avail > (SSize_t) count) {
3102 * If we have space for more than count, just move count
3110 * In simple stdio-like ungetc() case chars will be already
3113 if (buf != b->ptr) {
3114 Copy(buf, b->ptr, avail, STDCHAR);
3118 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
3125 PerlIOBuf_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3127 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3128 const STDCHAR *buf = (const STDCHAR *) vbuf;
3132 if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
3135 SSize_t avail = b->bufsiz - (b->ptr - b->buf);
3136 if ((SSize_t) count < avail)
3138 PerlIOBase(f)->flags |= PERLIO_F_WRBUF;
3139 if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF) {
3154 Copy(buf, b->ptr, avail, STDCHAR);
3161 if (b->ptr >= (b->buf + b->bufsiz))
3164 if (PerlIOBase(f)->flags & PERLIO_F_UNBUF)
3170 PerlIOBuf_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
3173 if ((code = PerlIO_flush(f)) == 0) {
3174 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3175 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
3176 code = PerlIO_seek(PerlIONext(f), offset, whence);
3178 b->posn = PerlIO_tell(PerlIONext(f));
3185 PerlIOBuf_tell(pTHX_ PerlIO *f)
3187 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3189 * b->posn is file position where b->buf was read, or will be written
3191 Off_t posn = b->posn;
3194 * If buffer is valid adjust position by amount in buffer
3196 posn += (b->ptr - b->buf);
3202 PerlIOBuf_close(pTHX_ PerlIO *f)
3204 IV code = PerlIOBase_close(aTHX_ f);
3205 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3206 if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
3210 b->ptr = b->end = b->buf;
3211 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
3216 PerlIOBuf_get_ptr(pTHX_ PerlIO *f)
3218 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3225 PerlIOBuf_get_cnt(pTHX_ PerlIO *f)
3227 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3230 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF)
3231 return (b->end - b->ptr);
3236 PerlIOBuf_get_base(pTHX_ PerlIO *f)
3238 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3243 Newz('B',b->buf,b->bufsiz, STDCHAR);
3245 b->buf = (STDCHAR *) & b->oneword;
3246 b->bufsiz = sizeof(b->oneword);
3255 PerlIOBuf_bufsiz(pTHX_ PerlIO *f)
3257 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3260 return (b->end - b->buf);
3264 PerlIOBuf_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
3266 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3270 if (PerlIO_get_cnt(f) != cnt || b->ptr < b->buf) {
3271 assert(PerlIO_get_cnt(f) == cnt);
3272 assert(b->ptr >= b->buf);
3274 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3278 PerlIOBuf_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
3280 return PerlIOBase_dup(aTHX_ f, o, param, flags);
3285 PerlIO_funcs PerlIO_perlio = {
3305 PerlIOBase_clearerr,
3306 PerlIOBase_setlinebuf,
3311 PerlIOBuf_set_ptrcnt,
3314 /*--------------------------------------------------------------------------------------*/
3316 * Temp layer to hold unread chars when cannot do it any other way
3320 PerlIOPending_fill(pTHX_ PerlIO *f)
3323 * Should never happen
3330 PerlIOPending_close(pTHX_ PerlIO *f)
3333 * A tad tricky - flush pops us, then we close new top
3336 return PerlIO_close(f);
3340 PerlIOPending_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
3343 * A tad tricky - flush pops us, then we seek new top
3346 return PerlIO_seek(f, offset, whence);
3351 PerlIOPending_flush(pTHX_ PerlIO *f)
3353 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3354 if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
3358 PerlIO_pop(aTHX_ f);
3363 PerlIOPending_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
3369 PerlIOBuf_set_ptrcnt(aTHX_ f, ptr, cnt);
3374 PerlIOPending_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
3376 IV code = PerlIOBase_pushed(aTHX_ f, mode, arg);
3377 PerlIOl *l = PerlIOBase(f);
3379 * Our PerlIO_fast_gets must match what we are pushed on, or sv_gets()
3380 * etc. get muddled when it changes mid-string when we auto-pop.
3382 l->flags = (l->flags & ~(PERLIO_F_FASTGETS | PERLIO_F_UTF8)) |
3383 (PerlIOBase(PerlIONext(f))->
3384 flags & (PERLIO_F_FASTGETS | PERLIO_F_UTF8));
3389 PerlIOPending_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
3391 SSize_t avail = PerlIO_get_cnt(f);
3393 if ((SSize_t)count < avail)
3396 got = PerlIOBuf_read(aTHX_ f, vbuf, avail);
3397 if (got >= 0 && got < (SSize_t)count) {
3399 PerlIO_read(f, ((STDCHAR *) vbuf) + got, count - got);
3400 if (more >= 0 || got == 0)
3406 PerlIO_funcs PerlIO_pending = {
3410 PerlIOPending_pushed,
3421 PerlIOPending_close,
3422 PerlIOPending_flush,
3426 PerlIOBase_clearerr,
3427 PerlIOBase_setlinebuf,
3432 PerlIOPending_set_ptrcnt,
3437 /*--------------------------------------------------------------------------------------*/
3439 * crlf - translation On read translate CR,LF to "\n" we do this by
3440 * overriding ptr/cnt entries to hand back a line at a time and keeping a
3441 * record of which nl we "lied" about. On write translate "\n" to CR,LF
3445 PerlIOBuf base; /* PerlIOBuf stuff */
3446 STDCHAR *nl; /* Position of crlf we "lied" about in the
3451 PerlIOCrlf_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
3454 PerlIOBase(f)->flags |= PERLIO_F_CRLF;
3455 code = PerlIOBuf_pushed(aTHX_ f, mode, arg);
3457 PerlIO_debug("PerlIOCrlf_pushed f=%p %s %s fl=%08" UVxf "\n",
3458 f, PerlIOBase(f)->tab->name, (mode) ? mode : "(Null)",
3459 PerlIOBase(f)->flags);
3466 PerlIOCrlf_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3468 PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
3473 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF))
3474 return PerlIOBuf_unread(aTHX_ f, vbuf, count);
3476 const STDCHAR *buf = (const STDCHAR *) vbuf + count;
3477 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3479 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
3484 if (!(PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
3485 b->end = b->ptr = b->buf + b->bufsiz;
3486 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3487 b->posn -= b->bufsiz;
3489 while (count > 0 && b->ptr > b->buf) {
3492 if (b->ptr - 2 >= b->buf) {
3515 PerlIOCrlf_get_cnt(pTHX_ PerlIO *f)
3517 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3520 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
3521 PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
3522 if ((PerlIOBase(f)->flags & PERLIO_F_CRLF) && (!c->nl || *c->nl == 0xd)) {
3523 STDCHAR *nl = (c->nl) ? c->nl : b->ptr;
3525 while (nl < b->end && *nl != 0xd)
3527 if (nl < b->end && *nl == 0xd) {
3529 if (nl + 1 < b->end) {
3536 * Not CR,LF but just CR
3544 * Blast - found CR as last char in buffer
3549 * They may not care, defer work as long as
3553 return (nl - b->ptr);
3557 b->ptr++; /* say we have read it as far as
3558 * flush() is concerned */
3559 b->buf++; /* Leave space in front of buffer */
3560 b->bufsiz--; /* Buffer is thus smaller */
3561 code = PerlIO_fill(f); /* Fetch some more */
3562 b->bufsiz++; /* Restore size for next time */
3563 b->buf--; /* Point at space */
3564 b->ptr = nl = b->buf; /* Which is what we hand
3566 b->posn--; /* Buffer starts here */
3567 *nl = 0xd; /* Fill in the CR */
3569 goto test; /* fill() call worked */
3571 * CR at EOF - just fall through
3573 /* Should we clear EOF though ??? */
3578 return (((c->nl) ? (c->nl + 1) : b->end) - b->ptr);
3584 PerlIOCrlf_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
3586 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3587 PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
3593 if (ptr == b->end && *c->nl == 0xd) {
3594 /* Defered CR at end of buffer case - we lied about count */
3606 * Test code - delete when it works ...
3608 IV flags = PerlIOBase(f)->flags;
3609 STDCHAR *chk = (c->nl) ? (c->nl+1) : b->end;
3610 if (ptr+cnt == c->nl && c->nl+1 == b->end && *c->nl == 0xd) {
3611 /* Defered CR at end of buffer case - we lied about count */
3617 Perl_croak(aTHX_ "ptr wrong %p != %p fl=%08" UVxf
3618 " nl=%p e=%p for %d", ptr, chk, flags, c->nl,
3626 * They have taken what we lied about
3634 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3638 PerlIOCrlf_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3640 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF))
3641 return PerlIOBuf_write(aTHX_ f, vbuf, count);
3643 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3644 const STDCHAR *buf = (const STDCHAR *) vbuf;
3645 const STDCHAR *ebuf = buf + count;
3648 if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
3650 while (buf < ebuf) {
3651 STDCHAR *eptr = b->buf + b->bufsiz;
3652 PerlIOBase(f)->flags |= PERLIO_F_WRBUF;
3653 while (buf < ebuf && b->ptr < eptr) {
3655 if ((b->ptr + 2) > eptr) {
3663 *(b->ptr)++ = 0xd; /* CR */
3664 *(b->ptr)++ = 0xa; /* LF */
3666 if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF) {
3676 if (b->ptr >= eptr) {
3682 if (PerlIOBase(f)->flags & PERLIO_F_UNBUF)
3684 return (buf - (STDCHAR *) vbuf);
3689 PerlIOCrlf_flush(pTHX_ PerlIO *f)
3691 PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
3696 return PerlIOBuf_flush(aTHX_ f);
3699 PerlIO_funcs PerlIO_crlf = {
3702 PERLIO_K_BUFFERED | PERLIO_K_CANCRLF,
3704 PerlIOBase_noop_ok, /* popped */
3709 PerlIOBuf_read, /* generic read works with ptr/cnt lies
3711 PerlIOCrlf_unread, /* Put CR,LF in buffer for each '\n' */
3712 PerlIOCrlf_write, /* Put CR,LF in buffer for each '\n' */
3720 PerlIOBase_clearerr,
3721 PerlIOBase_setlinebuf,
3726 PerlIOCrlf_set_ptrcnt,
3730 /*--------------------------------------------------------------------------------------*/
3732 * mmap as "buffer" layer
3736 PerlIOBuf base; /* PerlIOBuf stuff */
3737 Mmap_t mptr; /* Mapped address */
3738 Size_t len; /* mapped length */
3739 STDCHAR *bbuf; /* malloced buffer if map fails */
3742 static size_t page_size = 0;
3745 PerlIOMmap_map(pTHX_ PerlIO *f)
3747 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3748 IV flags = PerlIOBase(f)->flags;
3752 if (flags & PERLIO_F_CANREAD) {
3753 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3754 int fd = PerlIO_fileno(f);
3756 code = Fstat(fd, &st);
3757 if (code == 0 && S_ISREG(st.st_mode)) {
3758 SSize_t len = st.st_size - b->posn;
3762 #if defined(HAS_SYSCONF) && (defined(_SC_PAGESIZE) || defined(_SC_PAGE_SIZE))
3764 SETERRNO(0, SS$_NORMAL);
3765 # ifdef _SC_PAGESIZE
3766 page_size = sysconf(_SC_PAGESIZE);
3768 page_size = sysconf(_SC_PAGE_SIZE);
3770 if ((long) page_size < 0) {
3775 (void) SvUPGRADE(error, SVt_PV);
3776 msg = SvPVx(error, n_a);
3777 Perl_croak(aTHX_ "panic: sysconf: %s",
3782 "panic: sysconf: pagesize unknown");
3786 # ifdef HAS_GETPAGESIZE
3787 page_size = getpagesize();
3789 # if defined(I_SYS_PARAM) && defined(PAGESIZE)
3790 page_size = PAGESIZE; /* compiletime, bad */
3794 if ((IV) page_size <= 0)
3795 Perl_croak(aTHX_ "panic: bad pagesize %" IVdf,
3800 * This is a hack - should never happen - open should
3803 b->posn = PerlIO_tell(PerlIONext(f));
3805 posn = (b->posn / page_size) * page_size;
3806 len = st.st_size - posn;
3807 m->mptr = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, posn);
3808 if (m->mptr && m->mptr != (Mmap_t) - 1) {
3809 #if 0 && defined(HAS_MADVISE) && defined(MADV_SEQUENTIAL)
3810 madvise(m->mptr, len, MADV_SEQUENTIAL);
3812 #if 0 && defined(HAS_MADVISE) && defined(MADV_WILLNEED)
3813 madvise(m->mptr, len, MADV_WILLNEED);
3815 PerlIOBase(f)->flags =
3816 (flags & ~PERLIO_F_EOF) | PERLIO_F_RDBUF;
3817 b->end = ((STDCHAR *) m->mptr) + len;
3818 b->buf = ((STDCHAR *) m->mptr) + (b->posn - posn);
3827 PerlIOBase(f)->flags =
3828 flags | PERLIO_F_EOF | PERLIO_F_RDBUF;
3830 b->ptr = b->end = b->ptr;
3839 PerlIOMmap_unmap(pTHX_ PerlIO *f)
3841 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3842 PerlIOBuf *b = &m->base;
3846 code = munmap(m->mptr, m->len);
3850 if (PerlIO_seek(PerlIONext(f), b->posn, SEEK_SET) != 0)
3853 b->ptr = b->end = b->buf;
3854 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
3860 PerlIOMmap_get_base(pTHX_ PerlIO *f)
3862 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3863 PerlIOBuf *b = &m->base;
3864 if (b->buf && (PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
3866 * Already have a readbuffer in progress
3872 * We have a write buffer or flushed PerlIOBuf read buffer
3874 m->bbuf = b->buf; /* save it in case we need it again */
3875 b->buf = NULL; /* Clear to trigger below */
3878 PerlIOMmap_map(aTHX_ f); /* Try and map it */
3881 * Map did not work - recover PerlIOBuf buffer if we have one
3886 b->ptr = b->end = b->buf;
3889 return PerlIOBuf_get_base(aTHX_ f);
3893 PerlIOMmap_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3895 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3896 PerlIOBuf *b = &m->base;
3897 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
3899 if (b->ptr && (b->ptr - count) >= b->buf
3900 && memEQ(b->ptr - count, vbuf, count)) {
3902 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
3907 * Loose the unwritable mapped buffer
3911 * If flush took the "buffer" see if we have one from before
3913 if (!b->buf && m->bbuf)
3916 PerlIOBuf_get_base(aTHX_ f);
3920 return PerlIOBuf_unread(aTHX_ f, vbuf, count);
3924 PerlIOMmap_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3926 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3927 PerlIOBuf *b = &m->base;
3928 if (!b->buf || !(PerlIOBase(f)->flags & PERLIO_F_WRBUF)) {
3930 * No, or wrong sort of, buffer
3933 if (PerlIOMmap_unmap(aTHX_ f) != 0)
3937 * If unmap took the "buffer" see if we have one from before
3939 if (!b->buf && m->bbuf)
3942 PerlIOBuf_get_base(aTHX_ f);
3946 return PerlIOBuf_write(aTHX_ f, vbuf, count);
3950 PerlIOMmap_flush(pTHX_ PerlIO *f)
3952 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3953 PerlIOBuf *b = &m->base;
3954 IV code = PerlIOBuf_flush(aTHX_ f);
3956 * Now we are "synced" at PerlIOBuf level
3963 if (PerlIOMmap_unmap(aTHX_ f) != 0)
3968 * We seem to have a PerlIOBuf buffer which was not mapped
3969 * remember it in case we need one later
3978 PerlIOMmap_fill(pTHX_ PerlIO *f)
3980 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3981 IV code = PerlIO_flush(f);
3982 if (code == 0 && !b->buf) {
3983 code = PerlIOMmap_map(aTHX_ f);
3985 if (code == 0 && !(PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
3986 code = PerlIOBuf_fill(aTHX_ f);
3992 PerlIOMmap_close(pTHX_ PerlIO *f)
3994 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3995 PerlIOBuf *b = &m->base;
3996 IV code = PerlIO_flush(f);
4000 b->ptr = b->end = b->buf;
4002 if (PerlIOBuf_close(aTHX_ f) != 0)
4008 PerlIOMmap_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
4010 return PerlIOBase_dup(aTHX_ f, o, param, flags);
4014 PerlIO_funcs PerlIO_mmap = {
4034 PerlIOBase_clearerr,
4035 PerlIOBase_setlinebuf,
4036 PerlIOMmap_get_base,
4040 PerlIOBuf_set_ptrcnt,
4043 #endif /* HAS_MMAP */
4046 Perl_PerlIO_stdin(pTHX)
4049 PerlIO_stdstreams(aTHX);
4051 return &PL_perlio[1];
4055 Perl_PerlIO_stdout(pTHX)
4058 PerlIO_stdstreams(aTHX);
4060 return &PL_perlio[2];
4064 Perl_PerlIO_stderr(pTHX)
4067 PerlIO_stdstreams(aTHX);
4069 return &PL_perlio[3];
4072 /*--------------------------------------------------------------------------------------*/
4075 PerlIO_getname(PerlIO *f, char *buf)
4080 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
4082 name = fgetname(stdio, buf);
4084 Perl_croak(aTHX_ "Don't know how to get file name");
4090 /*--------------------------------------------------------------------------------------*/
4092 * Functions which can be called on any kind of PerlIO implemented in
4096 #undef PerlIO_fdopen
4098 PerlIO_fdopen(int fd, const char *mode)
4101 return PerlIO_openn(aTHX_ Nullch, mode, fd, 0, 0, NULL, 0, NULL);
4106 PerlIO_open(const char *path, const char *mode)
4109 SV *name = sv_2mortal(newSVpvn(path, strlen(path)));
4110 return PerlIO_openn(aTHX_ Nullch, mode, -1, 0, 0, NULL, 1, &name);
4113 #undef Perlio_reopen
4115 PerlIO_reopen(const char *path, const char *mode, PerlIO *f)
4118 SV *name = sv_2mortal(newSVpvn(path, strlen(path)));
4119 return PerlIO_openn(aTHX_ Nullch, mode, -1, 0, 0, f, 1, &name);
4124 PerlIO_getc(PerlIO *f)
4128 SSize_t count = PerlIO_read(f, buf, 1);
4130 return (unsigned char) buf[0];
4135 #undef PerlIO_ungetc
4137 PerlIO_ungetc(PerlIO *f, int ch)
4142 if (PerlIO_unread(f, &buf, 1) == 1)
4150 PerlIO_putc(PerlIO *f, int ch)
4154 return PerlIO_write(f, &buf, 1);
4159 PerlIO_puts(PerlIO *f, const char *s)
4162 STRLEN len = strlen(s);
4163 return PerlIO_write(f, s, len);
4166 #undef PerlIO_rewind
4168 PerlIO_rewind(PerlIO *f)
4171 PerlIO_seek(f, (Off_t) 0, SEEK_SET);
4175 #undef PerlIO_vprintf
4177 PerlIO_vprintf(PerlIO *f, const char *fmt, va_list ap)
4180 SV *sv = newSVpvn("", 0);
4186 Perl_va_copy(ap, apc);
4187 sv_vcatpvf(sv, fmt, &apc);
4189 sv_vcatpvf(sv, fmt, &ap);
4192 wrote = PerlIO_write(f, s, len);
4197 #undef PerlIO_printf
4199 PerlIO_printf(PerlIO *f, const char *fmt, ...)
4204 result = PerlIO_vprintf(f, fmt, ap);
4209 #undef PerlIO_stdoutf
4211 PerlIO_stdoutf(const char *fmt, ...)
4217 result = PerlIO_vprintf(PerlIO_stdout(), fmt, ap);
4222 #undef PerlIO_tmpfile
4224 PerlIO_tmpfile(void)
4227 * I have no idea how portable mkstemp() is ...
4229 #if defined(WIN32) || !defined(HAVE_MKSTEMP)
4232 FILE *stdio = PerlSIO_tmpfile();
4235 PerlIOSelf(PerlIO_push
4236 (aTHX_(f = PerlIO_allocate(aTHX)), &PerlIO_stdio,
4237 "w+", Nullsv), PerlIOStdio);
4243 SV *sv = newSVpv("/tmp/PerlIO_XXXXXX", 0);
4244 int fd = mkstemp(SvPVX(sv));
4247 f = PerlIO_fdopen(fd, "w+");
4249 PerlIOBase(f)->flags |= PERLIO_F_TEMP;
4251 PerlLIO_unlink(SvPVX(sv));
4261 #endif /* USE_SFIO */
4262 #endif /* PERLIO_IS_STDIO */
4264 /*======================================================================================*/
4266 * Now some functions in terms of above which may be needed even if we are
4267 * not in true PerlIO mode
4271 #undef PerlIO_setpos
4273 PerlIO_setpos(PerlIO *f, SV *pos)
4278 Off_t *posn = (Off_t *) SvPV(pos, len);
4279 if (f && len == sizeof(Off_t))
4280 return PerlIO_seek(f, *posn, SEEK_SET);
4282 SETERRNO(EINVAL, SS$_IVCHAN);
4286 #undef PerlIO_setpos
4288 PerlIO_setpos(PerlIO *f, SV *pos)
4293 Fpos_t *fpos = (Fpos_t *) SvPV(pos, len);
4294 if (f && len == sizeof(Fpos_t)) {
4295 #if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64)
4296 return fsetpos64(f, fpos);
4298 return fsetpos(f, fpos);
4302 SETERRNO(EINVAL, SS$_IVCHAN);
4308 #undef PerlIO_getpos
4310 PerlIO_getpos(PerlIO *f, SV *pos)
4313 Off_t posn = PerlIO_tell(f);
4314 sv_setpvn(pos, (char *) &posn, sizeof(posn));
4315 return (posn == (Off_t) - 1) ? -1 : 0;
4318 #undef PerlIO_getpos
4320 PerlIO_getpos(PerlIO *f, SV *pos)
4325 #if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64)
4326 code = fgetpos64(f, &fpos);
4328 code = fgetpos(f, &fpos);
4330 sv_setpvn(pos, (char *) &fpos, sizeof(fpos));
4335 #if (defined(PERLIO_IS_STDIO) || !defined(USE_SFIO)) && !defined(HAS_VPRINTF)
4338 vprintf(char *pat, char *args)
4340 _doprnt(pat, args, stdout);
4341 return 0; /* wrong, but perl doesn't use the return
4346 vfprintf(FILE *fd, char *pat, char *args)
4348 _doprnt(pat, args, fd);
4349 return 0; /* wrong, but perl doesn't use the return
4355 #ifndef PerlIO_vsprintf
4357 PerlIO_vsprintf(char *s, int n, const char *fmt, va_list ap)
4359 int val = vsprintf(s, fmt, ap);
4361 if (strlen(s) >= (STRLEN) n) {
4363 (void) PerlIO_puts(Perl_error_log,
4364 "panic: sprintf overflow - memory corrupted!\n");
4372 #ifndef PerlIO_sprintf
4374 PerlIO_sprintf(char *s, int n, const char *fmt, ...)
4379 result = PerlIO_vsprintf(s, n, fmt, ap);