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 int fd = PerlLIO_dup(PerlIO_fileno(f));
199 int omode = fcntl(fd, F_GETFL);
201 omode = djgpp_get_stream_mode(f);
203 PerlIO_intmode2str(omode,mode,NULL);
204 /* the r+ is a hack */
205 return PerlIO_fdopen(fd, mode);
210 SETERRNO(EBADF, SS$_IVCHAN);
219 * De-mux PerlIO_openn() into fdopen, freopen and fopen type entries
223 PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd,
224 int imode, int perm, PerlIO *old, int narg, SV **args)
228 Perl_croak(aTHX_ "More than one argument to open");
230 if (*args == &PL_sv_undef)
231 return PerlIO_tmpfile();
233 char *name = SvPV_nolen(*args);
235 fd = PerlLIO_open3(name, imode, perm);
237 return PerlIO_fdopen(fd, (char *) mode + 1);
240 return PerlIO_reopen(name, mode, old);
243 return PerlIO_open(name, mode);
248 return PerlIO_fdopen(fd, (char *) mode);
253 XS(XS_PerlIO__Layer__find)
257 Perl_croak(aTHX_ "Usage class->find(name[,load])");
259 char *name = SvPV_nolen(ST(1));
260 ST(0) = (strEQ(name, "crlf")
261 || strEQ(name, "raw")) ? &PL_sv_yes : &PL_sv_undef;
268 Perl_boot_core_PerlIO(pTHX)
270 newXS("PerlIO::Layer::find", XS_PerlIO__Layer__find, __FILE__);
276 #ifdef PERLIO_IS_STDIO
282 * Does nothing (yet) except force this file to be included in perl
283 * binary. That allows this file to force inclusion of other functions
284 * that may be required by loadable extensions e.g. for
285 * FileHandle::tmpfile
289 #undef PerlIO_tmpfile
296 #else /* PERLIO_IS_STDIO */
304 * This section is just to make sure these functions get pulled in from
308 #undef PerlIO_tmpfile
319 * Force this file to be included in perl binary. Which allows this
320 * file to force inclusion of other functions that may be required by
321 * loadable extensions e.g. for FileHandle::tmpfile
325 * Hack sfio does its own 'autoflush' on stdout in common cases. Flush
326 * results in a lot of lseek()s to regular files and lot of small
329 sfset(sfstdout, SF_SHARE, 0);
333 PerlIO_importFILE(FILE *stdio, int fl)
335 int fd = fileno(stdio);
336 PerlIO *r = PerlIO_fdopen(fd, "r+");
341 PerlIO_findFILE(PerlIO *pio)
343 int fd = PerlIO_fileno(pio);
344 FILE *f = fdopen(fd, "r+");
346 if (!f && errno == EINVAL)
348 if (!f && errno == EINVAL)
355 /*======================================================================================*/
357 * Implement all the PerlIO interface ourselves.
363 * We _MUST_ have <unistd.h> if we are using lseek() and may have large
370 #include <sys/mman.h>
374 void PerlIO_debug(const char *fmt, ...)
375 __attribute__ ((format(__printf__, 1, 2)));
378 PerlIO_debug(const char *fmt, ...)
385 char *s = PerlEnv_getenv("PERLIO_DEBUG");
387 dbg = PerlLIO_open3(s, O_WRONLY | O_CREAT | O_APPEND, 0666);
394 /* Use fixed buffer as sv_catpvf etc. needs SVs */
398 s = CopFILE(PL_curcop);
401 sprintf(buffer, "%s:%" IVdf " ", s, (IV) CopLINE(PL_curcop));
402 len = strlen(buffer);
403 vsprintf(buffer+len, fmt, ap);
404 PerlLIO_write(dbg, buffer, strlen(buffer));
406 SV *sv = newSVpvn("", 0);
409 s = CopFILE(PL_curcop);
412 Perl_sv_catpvf(aTHX_ sv, "%s:%" IVdf " ", s,
413 (IV) CopLINE(PL_curcop));
414 Perl_sv_vcatpvf(aTHX_ sv, fmt, &ap);
417 PerlLIO_write(dbg, s, len);
424 /*--------------------------------------------------------------------------------------*/
427 * Inner level routines
431 * Table of pointers to the PerlIO structs (malloc'ed)
433 #define PERLIO_TABLE_SIZE 64
436 PerlIO_allocate(pTHX)
439 * Find a free slot in the table, allocating new table as necessary
444 while ((f = *last)) {
446 last = (PerlIO **) (f);
447 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
453 Newz('I',f,PERLIO_TABLE_SIZE,PerlIO);
461 #undef PerlIO_fdupopen
463 PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
465 if (PerlIOValid(f)) {
466 PerlIO_funcs *tab = PerlIOBase(f)->tab;
468 PerlIO_debug("fdupopen f=%p param=%p\n",(void*)f,(void*)param);
469 new = (*tab->Dup)(aTHX_ PerlIO_allocate(aTHX),f,param, flags);
473 SETERRNO(EBADF, SS$_IVCHAN);
479 PerlIO_cleantable(pTHX_ PerlIO **tablep)
481 PerlIO *table = *tablep;
484 PerlIO_cleantable(aTHX_(PerlIO **) & (table[0]));
485 for (i = PERLIO_TABLE_SIZE - 1; i > 0; i--) {
486 PerlIO *f = table + i;
498 PerlIO_list_alloc(pTHX)
501 Newz('L', list, 1, PerlIO_list_t);
507 PerlIO_list_free(pTHX_ PerlIO_list_t *list)
510 if (--list->refcnt == 0) {
513 for (i = 0; i < list->cur; i++) {
514 if (list->array[i].arg)
515 SvREFCNT_dec(list->array[i].arg);
517 Safefree(list->array);
525 PerlIO_list_push(pTHX_ PerlIO_list_t *list, PerlIO_funcs *funcs, SV *arg)
528 if (list->cur >= list->len) {
531 Renew(list->array, list->len, PerlIO_pair_t);
533 New('l', list->array, list->len, PerlIO_pair_t);
535 p = &(list->array[list->cur++]);
537 if ((p->arg = arg)) {
543 PerlIO_clone_list(pTHX_ PerlIO_list_t *proto, CLONE_PARAMS *param)
545 PerlIO_list_t *list = (PerlIO_list_t *) NULL;
548 list = PerlIO_list_alloc(aTHX);
549 for (i=0; i < proto->cur; i++) {
551 if (proto->array[i].arg)
552 arg = PerlIO_sv_dup(aTHX_ proto->array[i].arg,param);
553 PerlIO_list_push(aTHX_ list, proto->array[i].funcs, arg);
560 PerlIO_clone(pTHX_ PerlInterpreter *proto, CLONE_PARAMS *param)
563 PerlIO **table = &proto->Iperlio;
566 PL_known_layers = PerlIO_clone_list(aTHX_ proto->Iknown_layers, param);
567 PL_def_layerlist = PerlIO_clone_list(aTHX_ proto->Idef_layerlist, param);
568 PerlIO_allocate(aTHX); /* root slot is never used */
569 PerlIO_debug("Clone %p from %p\n",aTHX,proto);
570 while ((f = *table)) {
572 table = (PerlIO **) (f++);
573 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
575 (void) fp_dup(f, 0, param);
584 PerlIO_destruct(pTHX)
586 PerlIO **table = &PL_perlio;
589 PerlIO_debug("Destruct %p\n",aTHX);
591 while ((f = *table)) {
593 table = (PerlIO **) (f++);
594 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
598 if (l->tab->kind & PERLIO_K_DESTRUCT) {
599 PerlIO_debug("Destruct popping %s\n", l->tab->name);
613 PerlIO_pop(pTHX_ PerlIO *f)
617 PerlIO_debug("PerlIO_pop f=%p %s\n", (void*)f, l->tab->name);
618 if (l->tab->Popped) {
620 * If popped returns non-zero do not free its layer structure
621 * it has either done so itself, or it is shared and still in
624 if ((*l->tab->Popped) (aTHX_ f) != 0)
632 /*--------------------------------------------------------------------------------------*/
634 * XS Interface for perl code
638 PerlIO_find_layer(pTHX_ const char *name, STRLEN len, int load)
641 if ((SSize_t) len <= 0)
643 for (i = 0; i < PL_known_layers->cur; i++) {
644 PerlIO_funcs *f = PL_known_layers->array[i].funcs;
645 if (memEQ(f->name, name, len)) {
646 PerlIO_debug("%.*s => %p\n", (int) len, name, (void*)f);
650 if (load && PL_subname && PL_def_layerlist
651 && PL_def_layerlist->cur >= 2) {
652 SV *pkgsv = newSVpvn("PerlIO", 6);
653 SV *layer = newSVpvn(name, len);
656 * The two SVs are magically freed by load_module
658 Perl_load_module(aTHX_ 0, pkgsv, Nullsv, layer, Nullsv);
660 return PerlIO_find_layer(aTHX_ name, len, 0);
662 PerlIO_debug("Cannot find %.*s\n", (int) len, name);
666 #ifdef USE_ATTRIBUTES_FOR_PERLIO
669 perlio_mg_set(pTHX_ SV *sv, MAGIC *mg)
672 IO *io = GvIOn((GV *) SvRV(sv));
673 PerlIO *ifp = IoIFP(io);
674 PerlIO *ofp = IoOFP(io);
675 Perl_warn(aTHX_ "set %" SVf " %p %p %p", sv, io, ifp, ofp);
681 perlio_mg_get(pTHX_ SV *sv, MAGIC *mg)
684 IO *io = GvIOn((GV *) SvRV(sv));
685 PerlIO *ifp = IoIFP(io);
686 PerlIO *ofp = IoOFP(io);
687 Perl_warn(aTHX_ "get %" SVf " %p %p %p", sv, io, ifp, ofp);
693 perlio_mg_clear(pTHX_ SV *sv, MAGIC *mg)
695 Perl_warn(aTHX_ "clear %" SVf, sv);
700 perlio_mg_free(pTHX_ SV *sv, MAGIC *mg)
702 Perl_warn(aTHX_ "free %" SVf, sv);
706 MGVTBL perlio_vtab = {
714 XS(XS_io_MODIFY_SCALAR_ATTRIBUTES)
717 SV *sv = SvRV(ST(1));
722 sv_magic(sv, (SV *) av, PERL_MAGIC_ext, NULL, 0);
724 mg = mg_find(sv, PERL_MAGIC_ext);
725 mg->mg_virtual = &perlio_vtab;
727 Perl_warn(aTHX_ "attrib %" SVf, sv);
728 for (i = 2; i < items; i++) {
730 const char *name = SvPV(ST(i), len);
731 SV *layer = PerlIO_find_layer(aTHX_ name, len, 1);
733 av_push(av, SvREFCNT_inc(layer));
744 #endif /* USE_ATTIBUTES_FOR_PERLIO */
747 PerlIO_tab_sv(pTHX_ PerlIO_funcs *tab)
749 HV *stash = gv_stashpv("PerlIO::Layer", TRUE);
750 SV *sv = sv_bless(newRV_noinc(newSViv(PTR2IV(tab))), stash);
754 XS(XS_PerlIO__Layer__find)
758 Perl_croak(aTHX_ "Usage class->find(name[,load])");
761 char *name = SvPV(ST(1), len);
762 bool load = (items > 2) ? SvTRUE(ST(2)) : 0;
763 PerlIO_funcs *layer = PerlIO_find_layer(aTHX_ name, len, load);
765 (layer) ? sv_2mortal(PerlIO_tab_sv(aTHX_ layer)) :
772 PerlIO_define_layer(pTHX_ PerlIO_funcs *tab)
774 if (!PL_known_layers)
775 PL_known_layers = PerlIO_list_alloc(aTHX);
776 PerlIO_list_push(aTHX_ PL_known_layers, tab, Nullsv);
777 PerlIO_debug("define %s %p\n", tab->name, (void*)tab);
781 PerlIO_parse_layers(pTHX_ PerlIO_list_t *av, const char *names)
784 const char *s = names;
786 while (isSPACE(*s) || *s == ':')
791 const char *as = Nullch;
793 if (!isIDFIRST(*s)) {
795 * Message is consistent with how attribute lists are
796 * passed. Even though this means "foo : : bar" is
797 * seen as an invalid separator character.
799 char q = ((*s == '\'') ? '"' : '\'');
800 if (ckWARN(WARN_LAYER))
801 Perl_warner(aTHX_ packWARN(WARN_LAYER),
802 "perlio: invalid separator character %c%c%c in layer specification list %s",
808 } while (isALNUM(*e));
824 * It's a nul terminated string, not allowed
825 * to \ the terminating null. Anything other
826 * character is passed over.
836 if (ckWARN(WARN_LAYER))
837 Perl_warner(aTHX_ packWARN(WARN_LAYER),
838 "perlio: argument list not closed for layer \"%.*s\"",
850 bool warn_layer = ckWARN(WARN_LAYER);
851 PerlIO_funcs *layer =
852 PerlIO_find_layer(aTHX_ s, llen, 1);
854 PerlIO_list_push(aTHX_ av, layer,
861 Perl_warner(aTHX_ packWARN(WARN_LAYER), "perlio: unknown layer \"%.*s\"",
874 PerlIO_default_buffer(pTHX_ PerlIO_list_t *av)
876 PerlIO_funcs *tab = &PerlIO_perlio;
877 #ifdef PERLIO_USING_CRLF
880 if (PerlIO_stdio.Set_ptrcnt)
883 PerlIO_debug("Pushing %s\n", tab->name);
884 PerlIO_list_push(aTHX_ av, PerlIO_find_layer(aTHX_ tab->name, 0, 0),
889 PerlIO_arg_fetch(PerlIO_list_t *av, IV n)
891 return av->array[n].arg;
895 PerlIO_layer_fetch(pTHX_ PerlIO_list_t *av, IV n, PerlIO_funcs *def)
897 if (n >= 0 && n < av->cur) {
898 PerlIO_debug("Layer %" IVdf " is %s\n", n,
899 av->array[n].funcs->name);
900 return av->array[n].funcs;
903 Perl_croak(aTHX_ "panic: PerlIO layer array corrupt");
908 PerlIO_default_layers(pTHX)
910 if (!PL_def_layerlist) {
911 const char *s = (PL_tainting) ? Nullch : PerlEnv_getenv("PERLIO");
912 PerlIO_funcs *osLayer = &PerlIO_unix;
913 PL_def_layerlist = PerlIO_list_alloc(aTHX);
914 PerlIO_define_layer(aTHX_ & PerlIO_unix);
915 #if defined(WIN32) && !defined(UNDER_CE)
916 PerlIO_define_layer(aTHX_ & PerlIO_win32);
918 osLayer = &PerlIO_win32;
921 PerlIO_define_layer(aTHX_ & PerlIO_raw);
922 PerlIO_define_layer(aTHX_ & PerlIO_perlio);
923 PerlIO_define_layer(aTHX_ & PerlIO_stdio);
924 PerlIO_define_layer(aTHX_ & PerlIO_crlf);
926 PerlIO_define_layer(aTHX_ & PerlIO_mmap);
928 PerlIO_define_layer(aTHX_ & PerlIO_utf8);
929 PerlIO_define_layer(aTHX_ & PerlIO_byte);
930 PerlIO_list_push(aTHX_ PL_def_layerlist,
931 PerlIO_find_layer(aTHX_ osLayer->name, 0, 0),
934 PerlIO_parse_layers(aTHX_ PL_def_layerlist, s);
937 PerlIO_default_buffer(aTHX_ PL_def_layerlist);
940 if (PL_def_layerlist->cur < 2) {
941 PerlIO_default_buffer(aTHX_ PL_def_layerlist);
943 return PL_def_layerlist;
947 Perl_boot_core_PerlIO(pTHX)
949 #ifdef USE_ATTRIBUTES_FOR_PERLIO
950 newXS("io::MODIFY_SCALAR_ATTRIBUTES", XS_io_MODIFY_SCALAR_ATTRIBUTES,
953 newXS("PerlIO::Layer::find", XS_PerlIO__Layer__find, __FILE__);
957 PerlIO_default_layer(pTHX_ I32 n)
959 PerlIO_list_t *av = PerlIO_default_layers(aTHX);
962 return PerlIO_layer_fetch(aTHX_ av, n, &PerlIO_stdio);
965 #define PerlIO_default_top() PerlIO_default_layer(aTHX_ -1)
966 #define PerlIO_default_btm() PerlIO_default_layer(aTHX_ 0)
969 PerlIO_stdstreams(pTHX)
972 PerlIO_allocate(aTHX);
973 PerlIO_fdopen(0, "Ir" PERLIO_STDTEXT);
974 PerlIO_fdopen(1, "Iw" PERLIO_STDTEXT);
975 PerlIO_fdopen(2, "Iw" PERLIO_STDTEXT);
980 PerlIO_push(pTHX_ PerlIO *f, PerlIO_funcs *tab, const char *mode, SV *arg)
983 Newc('L',l,tab->size,char,PerlIOl);
985 Zero(l, tab->size, char);
989 PerlIO_debug("PerlIO_push f=%p %s %s %p\n", (void*)f, tab->name,
990 (mode) ? mode : "(Null)", (void*)arg);
991 if ((*l->tab->Pushed) (aTHX_ f, mode, arg) != 0) {
1000 PerlIOPop_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
1002 PerlIO_pop(aTHX_ f);
1005 PerlIO_pop(aTHX_ f);
1012 PerlIORaw_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
1015 * Remove the dummy layer
1017 PerlIO_pop(aTHX_ f);
1019 * Pop back to bottom layer
1021 if (PerlIOValid(f)) {
1023 while (!(PerlIOBase(f)->tab->kind & PERLIO_K_RAW)) {
1024 if (*PerlIONext(f)) {
1025 PerlIO_pop(aTHX_ f);
1029 * Nothing bellow - push unix on top then remove it
1031 if (PerlIO_push(aTHX_ f, PerlIO_default_btm(), mode, arg)) {
1032 PerlIO_pop(aTHX_ PerlIONext(f));
1037 PerlIO_debug(":raw f=%p :%s\n", (void*)f, PerlIOBase(f)->tab->name);
1044 PerlIO_apply_layera(pTHX_ PerlIO *f, const char *mode,
1045 PerlIO_list_t *layers, IV n, IV max)
1049 PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n, NULL);
1051 if (!PerlIO_push(aTHX_ f, tab, mode, PerlIOArg)) {
1062 PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names)
1066 PerlIO_list_t *layers = PerlIO_list_alloc(aTHX);
1067 code = PerlIO_parse_layers(aTHX_ layers, names);
1069 code = PerlIO_apply_layera(aTHX_ f, mode, layers, 0, layers->cur);
1071 PerlIO_list_free(aTHX_ layers);
1077 /*--------------------------------------------------------------------------------------*/
1079 * Given the abstraction above the public API functions
1083 PerlIO_binmode(pTHX_ PerlIO *f, int iotype, int mode, const char *names)
1085 PerlIO_debug("PerlIO_binmode f=%p %s %c %x %s\n",
1086 (void*)f, PerlIOBase(f)->tab->name, iotype, mode,
1087 (names) ? names : "(Null)");
1089 /* Do not flush etc. if (e.g.) switching encodings.
1090 if a pushed layer knows it needs to flush lower layers
1091 (for example :unix which is never going to call them)
1092 it can do the flush when it is pushed.
1094 return PerlIO_apply_layers(aTHX_ f, NULL, names) == 0 ? TRUE : FALSE;
1097 /* FIXME?: Looking down the layer stack seems wrong,
1098 but is a way of reaching past (say) an encoding layer
1099 to flip CRLF-ness of the layer(s) below
1101 #ifdef PERLIO_USING_CRLF
1102 /* Legacy binmode only has meaning if O_TEXT has a value distinct from
1103 O_BINARY so we can look for it in mode.
1105 if (!(mode & O_BINARY)) {
1108 /* Perhaps we should turn on bottom-most aware layer
1109 e.g. Ilya's idea that UNIX TTY could serve
1111 if (PerlIOBase(f)->tab->kind & PERLIO_K_CANCRLF) {
1112 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF)) {
1113 /* Not in text mode - flush any pending stuff and flip it */
1115 PerlIOBase(f)->flags |= PERLIO_F_CRLF;
1117 /* Only need to turn it on in one layer so we are done */
1122 /* Not finding a CRLF aware layer presumably means we are binary
1123 which is not what was requested - so we failed
1124 We _could_ push :crlf layer but so could caller
1129 /* Either asked for BINMODE or that is normal on this platform
1130 see if any CRLF aware layers are present and turn off the flag
1131 and possibly remove layer.
1134 if (PerlIOBase(f)->tab->kind & PERLIO_K_CANCRLF) {
1135 if ((PerlIOBase(f)->flags & PERLIO_F_CRLF)) {
1136 /* In text mode - flush any pending stuff and flip it */
1138 PerlIOBase(f)->flags &= ~PERLIO_F_CRLF;
1139 #ifndef PERLIO_USING_CRLF
1140 /* CRLF is unusual case - if this is just the :crlf layer pop it */
1141 if (PerlIOBase(f)->tab == &PerlIO_crlf) {
1142 PerlIO_pop(aTHX_ f);
1145 /* Normal case is only one layer doing this, so exit on first
1146 abnormal case can always do multiple binmode calls
1158 PerlIO__close(pTHX_ PerlIO *f)
1161 return (*PerlIOBase(f)->tab->Close) (aTHX_ f);
1163 SETERRNO(EBADF, SS$_IVCHAN);
1169 Perl_PerlIO_close(pTHX_ PerlIO *f)
1172 if (PerlIOValid(f)) {
1173 code = (*PerlIOBase(f)->tab->Close) (aTHX_ f);
1175 PerlIO_pop(aTHX_ f);
1182 Perl_PerlIO_fileno(pTHX_ PerlIO *f)
1185 return (*PerlIOBase(f)->tab->Fileno) (aTHX_ f);
1187 SETERRNO(EBADF, SS$_IVCHAN);
1193 PerlIO_context_layers(pTHX_ const char *mode)
1195 const char *type = NULL;
1197 * Need to supply default layer info from open.pm
1200 SV *layers = PL_curcop->cop_io;
1203 type = SvPV(layers, len);
1204 if (type && mode[0] != 'r') {
1206 * Skip to write part
1208 const char *s = strchr(type, 0);
1209 if (s && (STRLEN)(s - type) < len) {
1218 static PerlIO_funcs *
1219 PerlIO_layer_from_ref(pTHX_ SV *sv)
1222 * For any scalar type load the handler which is bundled with perl
1224 if (SvTYPE(sv) < SVt_PVAV)
1225 return PerlIO_find_layer(aTHX_ "Scalar", 6, 1);
1228 * For other types allow if layer is known but don't try and load it
1230 switch (SvTYPE(sv)) {
1232 return PerlIO_find_layer(aTHX_ "Array", 5, 0);
1234 return PerlIO_find_layer(aTHX_ "Hash", 4, 0);
1236 return PerlIO_find_layer(aTHX_ "Code", 4, 0);
1238 return PerlIO_find_layer(aTHX_ "Glob", 4, 0);
1244 PerlIO_resolve_layers(pTHX_ const char *layers,
1245 const char *mode, int narg, SV **args)
1247 PerlIO_list_t *def = PerlIO_default_layers(aTHX);
1250 PerlIO_stdstreams(aTHX);
1254 * If it is a reference but not an object see if we have a handler
1257 if (SvROK(arg) && !sv_isobject(arg)) {
1258 PerlIO_funcs *handler = PerlIO_layer_from_ref(aTHX_ SvRV(arg));
1260 def = PerlIO_list_alloc(aTHX);
1261 PerlIO_list_push(aTHX_ def, handler, &PL_sv_undef);
1265 * Don't fail if handler cannot be found :Via(...) etc. may do
1266 * something sensible else we will just stringfy and open
1272 layers = PerlIO_context_layers(aTHX_ mode);
1273 if (layers && *layers) {
1277 av = PerlIO_list_alloc(aTHX);
1278 for (i = 0; i < def->cur; i++) {
1279 PerlIO_list_push(aTHX_ av, def->array[i].funcs,
1286 PerlIO_parse_layers(aTHX_ av, layers);
1297 PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd,
1298 int imode, int perm, PerlIO *f, int narg, SV **args)
1300 if (!f && narg == 1 && *args == &PL_sv_undef) {
1301 if ((f = PerlIO_tmpfile())) {
1303 layers = PerlIO_context_layers(aTHX_ mode);
1304 if (layers && *layers)
1305 PerlIO_apply_layers(aTHX_ f, mode, layers);
1309 PerlIO_list_t *layera = NULL;
1311 PerlIO_funcs *tab = NULL;
1312 if (PerlIOValid(f)) {
1314 * This is "reopen" - it is not tested as perl does not use it
1318 layera = PerlIO_list_alloc(aTHX);
1320 SV *arg = (l->tab->Getarg)
1321 ? (*l->tab->Getarg) (aTHX_ &l, NULL, 0)
1323 PerlIO_list_push(aTHX_ layera, l->tab, arg);
1324 l = *PerlIONext(&l);
1328 layera = PerlIO_resolve_layers(aTHX_ layers, mode, narg, args);
1331 * Start at "top" of layer stack
1333 n = layera->cur - 1;
1335 PerlIO_funcs *t = PerlIO_layer_fetch(aTHX_ layera, n, NULL);
1344 * Found that layer 'n' can do opens - call it
1346 if (narg > 1 && !(tab->kind & PERLIO_K_MULTIARG)) {
1347 Perl_croak(aTHX_ "More than one argument to open(,':%s')",tab->name);
1349 PerlIO_debug("openn(%s,'%s','%s',%d,%x,%o,%p,%d,%p)\n",
1350 tab->name, layers, mode, fd, imode, perm,
1351 (void*)f, narg, (void*)args);
1352 f = (*tab->Open) (aTHX_ tab, layera, n, mode, fd, imode, perm,
1355 if (n + 1 < layera->cur) {
1357 * More layers above the one that we used to open -
1360 if (PerlIO_apply_layera(aTHX_ f, mode, layera, n + 1, layera->cur) != 0) {
1361 /* If pushing layers fails close the file */
1368 PerlIO_list_free(aTHX_ layera);
1375 Perl_PerlIO_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
1378 return (*PerlIOBase(f)->tab->Read) (aTHX_ f, vbuf, count);
1380 SETERRNO(EBADF, SS$_IVCHAN);
1386 Perl_PerlIO_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
1389 return (*PerlIOBase(f)->tab->Unread) (aTHX_ f, vbuf, count);
1391 SETERRNO(EBADF, SS$_IVCHAN);
1397 Perl_PerlIO_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
1400 return (*PerlIOBase(f)->tab->Write) (aTHX_ f, vbuf, count);
1402 SETERRNO(EBADF, SS$_IVCHAN);
1408 Perl_PerlIO_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
1411 return (*PerlIOBase(f)->tab->Seek) (aTHX_ f, offset, whence);
1413 SETERRNO(EBADF, SS$_IVCHAN);
1419 Perl_PerlIO_tell(pTHX_ PerlIO *f)
1422 return (*PerlIOBase(f)->tab->Tell) (aTHX_ f);
1424 SETERRNO(EBADF, SS$_IVCHAN);
1430 Perl_PerlIO_flush(pTHX_ PerlIO *f)
1434 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1435 if (tab && tab->Flush) {
1436 return (*tab->Flush) (aTHX_ f);
1439 PerlIO_debug("Cannot flush f=%p :%s\n", (void*)f, tab->name);
1440 SETERRNO(EBADF, SS$_IVCHAN);
1445 PerlIO_debug("Cannot flush f=%p\n", (void*)f);
1446 SETERRNO(EBADF, SS$_IVCHAN);
1452 * Is it good API design to do flush-all on NULL, a potentially
1453 * errorneous input? Maybe some magical value (PerlIO*
1454 * PERLIO_FLUSH_ALL = (PerlIO*)-1;)? Yes, stdio does similar
1455 * things on fflush(NULL), but should we be bound by their design
1458 PerlIO **table = &PL_perlio;
1460 while ((f = *table)) {
1462 table = (PerlIO **) (f++);
1463 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
1464 if (*f && PerlIO_flush(f) != 0)
1474 PerlIOBase_flush_linebuf(pTHX)
1476 PerlIO **table = &PL_perlio;
1478 while ((f = *table)) {
1480 table = (PerlIO **) (f++);
1481 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
1484 flags & (PERLIO_F_LINEBUF | PERLIO_F_CANWRITE))
1485 == (PERLIO_F_LINEBUF | PERLIO_F_CANWRITE))
1493 Perl_PerlIO_fill(pTHX_ PerlIO *f)
1496 return (*PerlIOBase(f)->tab->Fill) (aTHX_ f);
1498 SETERRNO(EBADF, SS$_IVCHAN);
1504 PerlIO_isutf8(PerlIO *f)
1507 return (PerlIOBase(f)->flags & PERLIO_F_UTF8) != 0;
1509 SETERRNO(EBADF, SS$_IVCHAN);
1515 Perl_PerlIO_eof(pTHX_ PerlIO *f)
1518 return (*PerlIOBase(f)->tab->Eof) (aTHX_ f);
1520 SETERRNO(EBADF, SS$_IVCHAN);
1526 Perl_PerlIO_error(pTHX_ PerlIO *f)
1529 return (*PerlIOBase(f)->tab->Error) (aTHX_ f);
1531 SETERRNO(EBADF, SS$_IVCHAN);
1537 Perl_PerlIO_clearerr(pTHX_ PerlIO *f)
1540 (*PerlIOBase(f)->tab->Clearerr) (aTHX_ f);
1542 SETERRNO(EBADF, SS$_IVCHAN);
1546 Perl_PerlIO_setlinebuf(pTHX_ PerlIO *f)
1549 (*PerlIOBase(f)->tab->Setlinebuf) (aTHX_ f);
1551 SETERRNO(EBADF, SS$_IVCHAN);
1555 PerlIO_has_base(PerlIO *f)
1557 if (PerlIOValid(f)) {
1558 return (PerlIOBase(f)->tab->Get_base != NULL);
1564 PerlIO_fast_gets(PerlIO *f)
1566 if (PerlIOValid(f) && (PerlIOBase(f)->flags & PERLIO_F_FASTGETS)) {
1567 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1568 return (tab->Set_ptrcnt != NULL);
1574 PerlIO_has_cntptr(PerlIO *f)
1576 if (PerlIOValid(f)) {
1577 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1578 return (tab->Get_ptr != NULL && tab->Get_cnt != NULL);
1584 PerlIO_canset_cnt(PerlIO *f)
1586 if (PerlIOValid(f)) {
1587 PerlIOl *l = PerlIOBase(f);
1588 return (l->tab->Set_ptrcnt != NULL);
1594 Perl_PerlIO_get_base(pTHX_ PerlIO *f)
1597 return (*PerlIOBase(f)->tab->Get_base) (aTHX_ f);
1602 Perl_PerlIO_get_bufsiz(pTHX_ PerlIO *f)
1605 return (*PerlIOBase(f)->tab->Get_bufsiz) (aTHX_ f);
1610 Perl_PerlIO_get_ptr(pTHX_ PerlIO *f)
1612 if (PerlIOValid(f)) {
1613 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1614 if (tab->Get_ptr == NULL)
1616 return (*tab->Get_ptr) (aTHX_ f);
1622 Perl_PerlIO_get_cnt(pTHX_ PerlIO *f)
1624 if (PerlIOValid(f)) {
1625 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1626 if (tab->Get_cnt == NULL)
1628 return (*tab->Get_cnt) (aTHX_ f);
1634 Perl_PerlIO_set_cnt(pTHX_ PerlIO *f, int cnt)
1636 if (PerlIOValid(f)) {
1637 (*PerlIOBase(f)->tab->Set_ptrcnt) (aTHX_ f, NULL, cnt);
1642 Perl_PerlIO_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, int cnt)
1644 if (PerlIOValid(f)) {
1645 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1646 if (tab->Set_ptrcnt == NULL) {
1647 Perl_croak(aTHX_ "PerlIO buffer snooping abuse");
1649 (*PerlIOBase(f)->tab->Set_ptrcnt) (aTHX_ f, ptr, cnt);
1653 /*--------------------------------------------------------------------------------------*/
1655 * utf8 and raw dummy layers
1659 PerlIOUtf8_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
1661 if (*PerlIONext(f)) {
1662 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1663 PerlIO_pop(aTHX_ f);
1664 if (tab->kind & PERLIO_K_UTF8)
1665 PerlIOBase(f)->flags |= PERLIO_F_UTF8;
1667 PerlIOBase(f)->flags &= ~PERLIO_F_UTF8;
1673 PerlIO_funcs PerlIO_utf8 = {
1676 PERLIO_K_DUMMY | PERLIO_F_UTF8,
1694 NULL, /* get_base */
1695 NULL, /* get_bufsiz */
1698 NULL, /* set_ptrcnt */
1701 PerlIO_funcs PerlIO_byte = {
1722 NULL, /* get_base */
1723 NULL, /* get_bufsiz */
1726 NULL, /* set_ptrcnt */
1730 PerlIORaw_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
1731 IV n, const char *mode, int fd, int imode, int perm,
1732 PerlIO *old, int narg, SV **args)
1734 PerlIO_funcs *tab = PerlIO_default_btm();
1735 return (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
1739 PerlIO_funcs PerlIO_raw = {
1760 NULL, /* get_base */
1761 NULL, /* get_bufsiz */
1764 NULL, /* set_ptrcnt */
1766 /*--------------------------------------------------------------------------------------*/
1767 /*--------------------------------------------------------------------------------------*/
1769 * "Methods" of the "base class"
1773 PerlIOBase_fileno(pTHX_ PerlIO *f)
1775 return PerlIOValid(f) ? PerlIO_fileno(PerlIONext(f)) : -1;
1779 PerlIO_modestr(PerlIO *f, char *buf)
1782 IV flags = PerlIOBase(f)->flags;
1783 if (flags & PERLIO_F_APPEND) {
1785 if (flags & PERLIO_F_CANREAD) {
1789 else if (flags & PERLIO_F_CANREAD) {
1791 if (flags & PERLIO_F_CANWRITE)
1794 else if (flags & PERLIO_F_CANWRITE) {
1796 if (flags & PERLIO_F_CANREAD) {
1800 #ifdef PERLIO_USING_CRLF
1801 if (!(flags & PERLIO_F_CRLF))
1809 PerlIOBase_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
1811 PerlIOl *l = PerlIOBase(f);
1813 const char *omode = mode;
1816 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1817 l->flags &= ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE |
1818 PERLIO_F_TRUNCATE | PERLIO_F_APPEND);
1819 if (tab->Set_ptrcnt != NULL)
1820 l->flags |= PERLIO_F_FASTGETS;
1822 if (*mode == '#' || *mode == 'I')
1826 l->flags |= PERLIO_F_CANREAD;
1829 l->flags |= PERLIO_F_APPEND | PERLIO_F_CANWRITE;
1832 l->flags |= PERLIO_F_TRUNCATE | PERLIO_F_CANWRITE;
1835 SETERRNO(EINVAL, LIB$_INVARG);
1841 l->flags |= PERLIO_F_CANREAD | PERLIO_F_CANWRITE;
1844 l->flags &= ~PERLIO_F_CRLF;
1847 l->flags |= PERLIO_F_CRLF;
1850 SETERRNO(EINVAL, LIB$_INVARG);
1857 l->flags |= l->next->flags &
1858 (PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_TRUNCATE |
1863 PerlIO_debug("PerlIOBase_pushed f=%p %s %s fl=%08" UVxf " (%s)\n",
1864 f, PerlIOBase(f)->tab->name, (omode) ? omode : "(Null)",
1865 l->flags, PerlIO_modestr(f, temp));
1871 PerlIOBase_popped(pTHX_ PerlIO *f)
1877 PerlIOBase_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
1880 * Save the position as current head considers it
1882 Off_t old = PerlIO_tell(f);
1884 PerlIO_push(aTHX_ f, &PerlIO_pending, "r", Nullsv);
1885 PerlIOSelf(f, PerlIOBuf)->posn = old;
1886 done = PerlIOBuf_unread(aTHX_ f, vbuf, count);
1891 PerlIOBase_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
1893 STDCHAR *buf = (STDCHAR *) vbuf;
1895 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD))
1898 SSize_t avail = PerlIO_get_cnt(f);
1901 take = ((SSize_t)count < avail) ? count : avail;
1903 STDCHAR *ptr = PerlIO_get_ptr(f);
1904 Copy(ptr, buf, take, STDCHAR);
1905 PerlIO_set_ptrcnt(f, ptr + take, (avail -= take));
1909 if (count > 0 && avail <= 0) {
1910 if (PerlIO_fill(f) != 0)
1914 return (buf - (STDCHAR *) vbuf);
1920 PerlIOBase_noop_ok(pTHX_ PerlIO *f)
1926 PerlIOBase_noop_fail(pTHX_ PerlIO *f)
1932 PerlIOBase_close(pTHX_ PerlIO *f)
1935 PerlIO *n = PerlIONext(f);
1936 if (PerlIO_flush(f) != 0)
1938 if (PerlIOValid(n) && (*PerlIOBase(n)->tab->Close)(aTHX_ n) != 0)
1940 PerlIOBase(f)->flags &=
1941 ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_OPEN);
1946 PerlIOBase_eof(pTHX_ PerlIO *f)
1948 if (PerlIOValid(f)) {
1949 return (PerlIOBase(f)->flags & PERLIO_F_EOF) != 0;
1955 PerlIOBase_error(pTHX_ PerlIO *f)
1957 if (PerlIOValid(f)) {
1958 return (PerlIOBase(f)->flags & PERLIO_F_ERROR) != 0;
1964 PerlIOBase_clearerr(pTHX_ PerlIO *f)
1966 if (PerlIOValid(f)) {
1967 PerlIO *n = PerlIONext(f);
1968 PerlIOBase(f)->flags &= ~(PERLIO_F_ERROR | PERLIO_F_EOF);
1975 PerlIOBase_setlinebuf(pTHX_ PerlIO *f)
1977 if (PerlIOValid(f)) {
1978 PerlIOBase(f)->flags |= PERLIO_F_LINEBUF;
1983 PerlIO_sv_dup(pTHX_ SV *arg, CLONE_PARAMS *param)
1989 return sv_dup(arg, param);
1992 return newSVsv(arg);
1995 return newSVsv(arg);
2000 PerlIOBase_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
2002 PerlIO *nexto = PerlIONext(o);
2003 if (PerlIOValid(nexto)) {
2004 PerlIO_funcs *tab = PerlIOBase(nexto)->tab;
2005 f = (*tab->Dup)(aTHX_ f, nexto, param, flags);
2008 PerlIO_funcs *self = PerlIOBase(o)->tab;
2011 PerlIO_debug("PerlIOBase_dup %s f=%p o=%p param=%p\n",
2012 self->name, (void*)f, (void*)o, (void*)param);
2014 arg = (*self->Getarg)(aTHX_ o,param,flags);
2016 f = PerlIO_push(aTHX_ f, self, PerlIO_modestr(o,buf), arg);
2024 #define PERLIO_MAX_REFCOUNTABLE_FD 2048
2026 perl_mutex PerlIO_mutex;
2028 int PerlIO_fd_refcnt[PERLIO_MAX_REFCOUNTABLE_FD];
2033 /* Place holder for stdstreams call ??? */
2035 MUTEX_INIT(&PerlIO_mutex);
2040 PerlIOUnix_refcnt_inc(int fd)
2042 if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
2044 MUTEX_LOCK(&PerlIO_mutex);
2046 PerlIO_fd_refcnt[fd]++;
2047 PerlIO_debug("fd %d refcnt=%d\n",fd,PerlIO_fd_refcnt[fd]);
2049 MUTEX_UNLOCK(&PerlIO_mutex);
2055 PerlIOUnix_refcnt_dec(int fd)
2058 if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
2060 MUTEX_LOCK(&PerlIO_mutex);
2062 cnt = --PerlIO_fd_refcnt[fd];
2063 PerlIO_debug("fd %d refcnt=%d\n",fd,cnt);
2065 MUTEX_UNLOCK(&PerlIO_mutex);
2072 PerlIO_cleanup(pTHX)
2076 PerlIO_debug("Cleanup layers for %p\n",aTHX);
2078 PerlIO_debug("Cleanup layers\n");
2080 /* Raise STDIN..STDERR refcount so we don't close them */
2081 for (i=0; i < 3; i++)
2082 PerlIOUnix_refcnt_inc(i);
2083 PerlIO_cleantable(aTHX_ &PL_perlio);
2084 /* Restore STDIN..STDERR refcount */
2085 for (i=0; i < 3; i++)
2086 PerlIOUnix_refcnt_dec(i);
2088 if (PL_known_layers) {
2089 PerlIO_list_free(aTHX_ PL_known_layers);
2090 PL_known_layers = NULL;
2092 if(PL_def_layerlist) {
2093 PerlIO_list_free(aTHX_ PL_def_layerlist);
2094 PL_def_layerlist = NULL;
2100 /*--------------------------------------------------------------------------------------*/
2102 * Bottom-most level for UNIX-like case
2106 struct _PerlIO base; /* The generic part */
2107 int fd; /* UNIX like file descriptor */
2108 int oflags; /* open/fcntl flags */
2112 PerlIOUnix_oflags(const char *mode)
2115 if (*mode == 'I' || *mode == '#')
2120 if (*++mode == '+') {
2127 oflags = O_CREAT | O_TRUNC;
2128 if (*++mode == '+') {
2137 oflags = O_CREAT | O_APPEND;
2138 if (*++mode == '+') {
2151 else if (*mode == 't') {
2153 oflags &= ~O_BINARY;
2157 * Always open in binary mode
2160 if (*mode || oflags == -1) {
2161 SETERRNO(EINVAL, LIB$_INVARG);
2168 PerlIOUnix_fileno(pTHX_ PerlIO *f)
2170 return PerlIOSelf(f, PerlIOUnix)->fd;
2174 PerlIOUnix_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
2176 IV code = PerlIOBase_pushed(aTHX_ f, mode, arg);
2177 PerlIOUnix *s = PerlIOSelf(f, PerlIOUnix);
2178 if (*PerlIONext(f)) {
2179 /* We never call down so any pending stuff now */
2180 PerlIO_flush(PerlIONext(f));
2181 s->fd = PerlIO_fileno(PerlIONext(f));
2183 * XXX could (or should) we retrieve the oflags from the open file
2184 * handle rather than believing the "mode" we are passed in? XXX
2185 * Should the value on NULL mode be 0 or -1?
2187 s->oflags = mode ? PerlIOUnix_oflags(mode) : -1;
2189 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
2194 PerlIOUnix_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2195 IV n, const char *mode, int fd, int imode,
2196 int perm, PerlIO *f, int narg, SV **args)
2198 if (PerlIOValid(f)) {
2199 if (PerlIOBase(f)->flags & PERLIO_F_OPEN)
2200 (*PerlIOBase(f)->tab->Close)(aTHX_ f);
2203 char *path = SvPV_nolen(*args);
2207 imode = PerlIOUnix_oflags(mode);
2211 fd = PerlLIO_open3(path, imode, perm);
2219 f = PerlIO_allocate(aTHX);
2221 if (!PerlIOValid(f)) {
2222 s = PerlIOSelf(PerlIO_push(aTHX_ f, self, mode, PerlIOArg),
2226 s = PerlIOSelf(f, PerlIOUnix);
2230 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
2231 PerlIOUnix_refcnt_inc(fd);
2237 * FIXME: pop layers ???
2245 PerlIOUnix_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
2247 PerlIOUnix *os = PerlIOSelf(o, PerlIOUnix);
2249 if (flags & PERLIO_DUP_FD) {
2250 fd = PerlLIO_dup(fd);
2252 if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
2253 f = PerlIOBase_dup(aTHX_ f, o, param, flags);
2255 /* If all went well overwrite fd in dup'ed lay with the dup()'ed fd */
2256 PerlIOUnix *s = PerlIOSelf(f, PerlIOUnix);
2258 PerlIOUnix_refcnt_inc(fd);
2267 PerlIOUnix_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
2269 int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2270 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD))
2273 SSize_t len = PerlLIO_read(fd, vbuf, count);
2274 if (len >= 0 || errno != EINTR) {
2276 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2277 else if (len == 0 && count != 0)
2278 PerlIOBase(f)->flags |= PERLIO_F_EOF;
2286 PerlIOUnix_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
2288 int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2290 SSize_t len = PerlLIO_write(fd, vbuf, count);
2291 if (len >= 0 || errno != EINTR) {
2293 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2301 PerlIOUnix_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
2304 PerlLIO_lseek(PerlIOSelf(f, PerlIOUnix)->fd, offset, whence);
2305 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
2306 return (new == (Off_t) - 1) ? -1 : 0;
2310 PerlIOUnix_tell(pTHX_ PerlIO *f)
2312 return PerlLIO_lseek(PerlIOSelf(f, PerlIOUnix)->fd, 0, SEEK_CUR);
2317 PerlIOUnix_close(pTHX_ PerlIO *f)
2319 int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2321 if (PerlIOBase(f)->flags & PERLIO_F_OPEN) {
2322 if (PerlIOUnix_refcnt_dec(fd) > 0) {
2323 PerlIOBase(f)->flags &= ~PERLIO_F_OPEN;
2328 SETERRNO(EBADF,SS$_IVCHAN);
2331 while (PerlLIO_close(fd) != 0) {
2332 if (errno != EINTR) {
2339 PerlIOBase(f)->flags &= ~PERLIO_F_OPEN;
2344 PerlIO_funcs PerlIO_unix = {
2360 PerlIOBase_noop_ok, /* flush */
2361 PerlIOBase_noop_fail, /* fill */
2364 PerlIOBase_clearerr,
2365 PerlIOBase_setlinebuf,
2366 NULL, /* get_base */
2367 NULL, /* get_bufsiz */
2370 NULL, /* set_ptrcnt */
2373 /*--------------------------------------------------------------------------------------*/
2379 struct _PerlIO base;
2380 FILE *stdio; /* The stream */
2384 PerlIOStdio_fileno(pTHX_ PerlIO *f)
2386 return PerlSIO_fileno(PerlIOSelf(f, PerlIOStdio)->stdio);
2390 PerlIOStdio_mode(const char *mode, char *tmode)
2396 #ifdef PERLIO_USING_CRLF
2404 * This isn't used yet ...
2407 PerlIOStdio_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
2409 if (*PerlIONext(f)) {
2410 PerlIOStdio *s = PerlIOSelf(f, PerlIOStdio);
2413 PerlSIO_fdopen(PerlIO_fileno(PerlIONext(f)), mode =
2414 PerlIOStdio_mode(mode, tmode));
2417 /* We never call down so any pending stuff now */
2418 PerlIO_flush(PerlIONext(f));
2423 return PerlIOBase_pushed(aTHX_ f, mode, arg);
2427 PerlIO_importFILE(FILE *stdio, int fl)
2433 PerlIOSelf(PerlIO_push
2434 (aTHX_(f = PerlIO_allocate(aTHX)), &PerlIO_stdio,
2435 "r+", Nullsv), PerlIOStdio);
2442 PerlIOStdio_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2443 IV n, const char *mode, int fd, int imode,
2444 int perm, PerlIO *f, int narg, SV **args)
2447 if (PerlIOValid(f)) {
2448 char *path = SvPV_nolen(*args);
2449 PerlIOStdio *s = PerlIOSelf(f, PerlIOStdio);
2451 PerlIOUnix_refcnt_dec(fileno(s->stdio));
2452 stdio = PerlSIO_freopen(path, (mode = PerlIOStdio_mode(mode, tmode)),
2457 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2462 char *path = SvPV_nolen(*args);
2465 fd = PerlLIO_open3(path, imode, perm);
2468 FILE *stdio = PerlSIO_fopen(path, mode);
2472 f = PerlIO_allocate(aTHX);
2474 s = PerlIOSelf(PerlIO_push(aTHX_ f, self,
2475 (mode = PerlIOStdio_mode(mode, tmode)),
2479 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2494 stdio = PerlSIO_stdin;
2497 stdio = PerlSIO_stdout;
2500 stdio = PerlSIO_stderr;
2505 stdio = PerlSIO_fdopen(fd, mode =
2506 PerlIOStdio_mode(mode, tmode));
2511 f = PerlIO_allocate(aTHX);
2513 s = PerlIOSelf(PerlIO_push(aTHX_ f, self, mode, PerlIOArg), PerlIOStdio);
2515 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2524 PerlIOStdio_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
2526 /* This assumes no layers underneath - which is what
2527 happens, but is not how I remember it. NI-S 2001/10/16
2529 if ((f = PerlIOBase_dup(aTHX_ f, o, param, flags))) {
2530 FILE *stdio = PerlIOSelf(o, PerlIOStdio)->stdio;
2531 if (flags & PERLIO_DUP_FD) {
2532 int fd = PerlLIO_dup(fileno(stdio));
2535 stdio = fdopen(fd, PerlIO_modestr(o,mode));
2538 /* FIXME: To avoid messy error recovery if dup fails
2539 re-use the existing stdio as though flag was not set
2543 PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
2544 PerlIOUnix_refcnt_inc(fileno(stdio));
2550 PerlIOStdio_close(pTHX_ PerlIO *f)
2552 #ifdef SOCKS5_VERSION_NAME
2554 Sock_size_t optlen = sizeof(int);
2556 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2557 if (PerlIOUnix_refcnt_dec(fileno(stdio)) > 0) {
2558 /* Do not close it but do flush any buffers */
2559 return PerlIO_flush(f);
2562 #ifdef SOCKS5_VERSION_NAME
2564 (PerlIO_fileno(f), SOL_SOCKET, SO_TYPE, (void *) &optval,
2566 0) ? PerlSIO_fclose(stdio) : close(PerlIO_fileno(f))
2568 PerlSIO_fclose(stdio)
2577 PerlIOStdio_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
2579 FILE *s = PerlIOSelf(f, PerlIOStdio)->stdio;
2582 STDCHAR *buf = (STDCHAR *) vbuf;
2584 * Perl is expecting PerlIO_getc() to fill the buffer Linux's
2585 * stdio does not do that for fread()
2587 int ch = PerlSIO_fgetc(s);
2594 got = PerlSIO_fread(vbuf, 1, count, s);
2599 PerlIOStdio_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
2601 FILE *s = PerlIOSelf(f, PerlIOStdio)->stdio;
2602 STDCHAR *buf = ((STDCHAR *) vbuf) + count - 1;
2605 int ch = *buf-- & 0xff;
2606 if (PerlSIO_ungetc(ch, s) != ch)
2615 PerlIOStdio_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
2617 return PerlSIO_fwrite(vbuf, 1, count,
2618 PerlIOSelf(f, PerlIOStdio)->stdio);
2622 PerlIOStdio_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
2624 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2625 return PerlSIO_fseek(stdio, offset, whence);
2629 PerlIOStdio_tell(pTHX_ PerlIO *f)
2631 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2632 return PerlSIO_ftell(stdio);
2636 PerlIOStdio_flush(pTHX_ PerlIO *f)
2638 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2639 if (PerlIOBase(f)->flags & PERLIO_F_CANWRITE) {
2640 return PerlSIO_fflush(stdio);
2645 * FIXME: This discards ungetc() and pre-read stuff which is not
2646 * right if this is just a "sync" from a layer above Suspect right
2647 * design is to do _this_ but not have layer above flush this
2648 * layer read-to-read
2651 * Not writeable - sync by attempting a seek
2654 if (PerlSIO_fseek(stdio, (Off_t) 0, SEEK_CUR) != 0)
2662 PerlIOStdio_fill(pTHX_ PerlIO *f)
2664 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2667 * fflush()ing read-only streams can cause trouble on some stdio-s
2669 if ((PerlIOBase(f)->flags & PERLIO_F_CANWRITE)) {
2670 if (PerlSIO_fflush(stdio) != 0)
2673 c = PerlSIO_fgetc(stdio);
2674 if (c == EOF || PerlSIO_ungetc(c, stdio) != c)
2680 PerlIOStdio_eof(pTHX_ PerlIO *f)
2682 return PerlSIO_feof(PerlIOSelf(f, PerlIOStdio)->stdio);
2686 PerlIOStdio_error(pTHX_ PerlIO *f)
2688 return PerlSIO_ferror(PerlIOSelf(f, PerlIOStdio)->stdio);
2692 PerlIOStdio_clearerr(pTHX_ PerlIO *f)
2694 PerlSIO_clearerr(PerlIOSelf(f, PerlIOStdio)->stdio);
2698 PerlIOStdio_setlinebuf(pTHX_ PerlIO *f)
2700 #ifdef HAS_SETLINEBUF
2701 PerlSIO_setlinebuf(PerlIOSelf(f, PerlIOStdio)->stdio);
2703 PerlSIO_setvbuf(PerlIOSelf(f, PerlIOStdio)->stdio, Nullch, _IOLBF, 0);
2709 PerlIOStdio_get_base(pTHX_ PerlIO *f)
2711 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2712 return (STDCHAR*)PerlSIO_get_base(stdio);
2716 PerlIOStdio_get_bufsiz(pTHX_ PerlIO *f)
2718 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2719 return PerlSIO_get_bufsiz(stdio);
2723 #ifdef USE_STDIO_PTR
2725 PerlIOStdio_get_ptr(pTHX_ PerlIO *f)
2727 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2728 return (STDCHAR*)PerlSIO_get_ptr(stdio);
2732 PerlIOStdio_get_cnt(pTHX_ PerlIO *f)
2734 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2735 return PerlSIO_get_cnt(stdio);
2739 PerlIOStdio_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
2741 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2743 #ifdef STDIO_PTR_LVALUE
2744 PerlSIO_set_ptr(stdio, (void*)ptr); /* LHS STDCHAR* cast non-portable */
2745 #ifdef STDIO_PTR_LVAL_SETS_CNT
2746 if (PerlSIO_get_cnt(stdio) != (cnt)) {
2747 assert(PerlSIO_get_cnt(stdio) == (cnt));
2750 #if (!defined(STDIO_PTR_LVAL_NOCHANGE_CNT))
2752 * Setting ptr _does_ change cnt - we are done
2756 #else /* STDIO_PTR_LVALUE */
2758 #endif /* STDIO_PTR_LVALUE */
2761 * Now (or only) set cnt
2763 #ifdef STDIO_CNT_LVALUE
2764 PerlSIO_set_cnt(stdio, cnt);
2765 #else /* STDIO_CNT_LVALUE */
2766 #if (defined(STDIO_PTR_LVALUE) && defined(STDIO_PTR_LVAL_SETS_CNT))
2767 PerlSIO_set_ptr(stdio,
2768 PerlSIO_get_ptr(stdio) + (PerlSIO_get_cnt(stdio) -
2770 #else /* STDIO_PTR_LVAL_SETS_CNT */
2772 #endif /* STDIO_PTR_LVAL_SETS_CNT */
2773 #endif /* STDIO_CNT_LVALUE */
2778 PerlIO_funcs PerlIO_stdio = {
2780 sizeof(PerlIOStdio),
2798 PerlIOStdio_clearerr,
2799 PerlIOStdio_setlinebuf,
2801 PerlIOStdio_get_base,
2802 PerlIOStdio_get_bufsiz,
2807 #ifdef USE_STDIO_PTR
2808 PerlIOStdio_get_ptr,
2809 PerlIOStdio_get_cnt,
2810 #if (defined(STDIO_PTR_LVALUE) && (defined(STDIO_CNT_LVALUE) || defined(STDIO_PTR_LVAL_SETS_CNT)))
2811 PerlIOStdio_set_ptrcnt
2812 #else /* STDIO_PTR_LVALUE */
2814 #endif /* STDIO_PTR_LVALUE */
2815 #else /* USE_STDIO_PTR */
2819 #endif /* USE_STDIO_PTR */
2823 PerlIO_exportFILE(PerlIO *f, int fl)
2828 stdio = fdopen(PerlIO_fileno(f), "r+");
2831 PerlIOSelf(PerlIO_push(aTHX_ f, &PerlIO_stdio, "r+", Nullsv),
2839 PerlIO_findFILE(PerlIO *f)
2843 if (l->tab == &PerlIO_stdio) {
2844 PerlIOStdio *s = PerlIOSelf(&l, PerlIOStdio);
2847 l = *PerlIONext(&l);
2849 return PerlIO_exportFILE(f, 0);
2853 PerlIO_releaseFILE(PerlIO *p, FILE *f)
2857 /*--------------------------------------------------------------------------------------*/
2859 * perlio buffer layer
2863 PerlIOBuf_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
2865 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
2866 int fd = PerlIO_fileno(f);
2868 if (fd >= 0 && PerlLIO_isatty(fd)) {
2869 PerlIOBase(f)->flags |= PERLIO_F_LINEBUF | PERLIO_F_TTY;
2871 posn = PerlIO_tell(PerlIONext(f));
2872 if (posn != (Off_t) - 1) {
2875 return PerlIOBase_pushed(aTHX_ f, mode, arg);
2879 PerlIOBuf_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2880 IV n, const char *mode, int fd, int imode, int perm,
2881 PerlIO *f, int narg, SV **args)
2883 if (PerlIOValid(f)) {
2884 PerlIO *next = PerlIONext(f);
2885 PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIOBase(next)->tab);
2886 next = (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
2888 if (!next || (*PerlIOBase(f)->tab->Pushed) (aTHX_ f, mode, PerlIOArg) != 0) {
2893 PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIO_default_btm());
2901 f = (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
2904 if (PerlIO_push(aTHX_ f, self, mode, PerlIOArg) == 0) {
2906 * if push fails during open, open fails. close will pop us.
2911 fd = PerlIO_fileno(f);
2912 if (init && fd == 2) {
2914 * Initial stderr is unbuffered
2916 PerlIOBase(f)->flags |= PERLIO_F_UNBUF;
2918 #ifdef PERLIO_USING_CRLF
2919 # ifdef PERLIO_IS_BINMODE_FD
2920 if (PERLIO_IS_BINMODE_FD(fd))
2921 PerlIO_binmode(f, '<'/*not used*/, O_BINARY, Nullch);
2925 * do something about failing setmode()? --jhi
2927 PerlLIO_setmode(fd, O_BINARY);
2936 * This "flush" is akin to sfio's sync in that it handles files in either
2937 * read or write state
2940 PerlIOBuf_flush(pTHX_ PerlIO *f)
2942 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
2944 PerlIO *n = PerlIONext(f);
2945 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF) {
2947 * write() the buffer
2949 STDCHAR *buf = b->buf;
2951 while (p < b->ptr) {
2952 SSize_t count = PerlIO_write(n, p, b->ptr - p);
2956 else if (count < 0 || PerlIO_error(n)) {
2957 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2962 b->posn += (p - buf);
2964 else if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
2965 STDCHAR *buf = PerlIO_get_base(f);
2967 * Note position change
2969 b->posn += (b->ptr - buf);
2970 if (b->ptr < b->end) {
2972 * We did not consume all of it
2974 if (PerlIO_seek(n, b->posn, SEEK_SET) == 0) {
2975 /* Reload n as some layers may pop themselves on seek */
2976 b->posn = PerlIO_tell(n = PerlIONext(f));
2980 b->ptr = b->end = b->buf;
2981 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
2982 /* We check for Valid because of dubious decision to make PerlIO_flush(NULL) flush all */
2983 /* FIXME: Doing downstream flush may be sub-optimal see PerlIOBuf_fill() below */
2984 if (PerlIOValid(n) && PerlIO_flush(n) != 0)
2990 PerlIOBuf_fill(pTHX_ PerlIO *f)
2992 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
2993 PerlIO *n = PerlIONext(f);
2996 * FIXME: doing the down-stream flush maybe sub-optimal if it causes
2997 * pre-read data in stdio buffer to be discarded.
2998 * However, skipping the flush also skips _our_ hosekeeping
2999 * and breaks tell tests. So we do the flush.
3001 if (PerlIO_flush(f) != 0)
3003 if (PerlIOBase(f)->flags & PERLIO_F_TTY)
3004 PerlIOBase_flush_linebuf(aTHX);
3007 PerlIO_get_base(f); /* allocate via vtable */
3009 b->ptr = b->end = b->buf;
3010 if (PerlIO_fast_gets(n)) {
3012 * Layer below is also buffered. We do _NOT_ want to call its
3013 * ->Read() because that will loop till it gets what we asked for
3014 * which may hang on a pipe etc. Instead take anything it has to
3015 * hand, or ask it to fill _once_.
3017 avail = PerlIO_get_cnt(n);
3019 avail = PerlIO_fill(n);
3021 avail = PerlIO_get_cnt(n);
3023 if (!PerlIO_error(n) && PerlIO_eof(n))
3028 STDCHAR *ptr = PerlIO_get_ptr(n);
3029 SSize_t cnt = avail;
3030 if (avail > (SSize_t)b->bufsiz)
3032 Copy(ptr, b->buf, avail, STDCHAR);
3033 PerlIO_set_ptrcnt(n, ptr + avail, cnt - avail);
3037 avail = PerlIO_read(n, b->ptr, b->bufsiz);
3041 PerlIOBase(f)->flags |= PERLIO_F_EOF;
3043 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
3046 b->end = b->buf + avail;
3047 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3052 PerlIOBuf_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
3054 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3055 if (PerlIOValid(f)) {
3058 return PerlIOBase_read(aTHX_ f, vbuf, count);
3064 PerlIOBuf_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3066 const STDCHAR *buf = (const STDCHAR *) vbuf + count;
3067 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3070 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
3075 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
3077 * Buffer is already a read buffer, we can overwrite any chars
3078 * which have been read back to buffer start
3080 avail = (b->ptr - b->buf);
3084 * Buffer is idle, set it up so whole buffer is available for
3088 b->end = b->buf + avail;
3090 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3092 * Buffer extends _back_ from where we are now
3094 b->posn -= b->bufsiz;
3096 if (avail > (SSize_t) count) {
3098 * If we have space for more than count, just move count
3106 * In simple stdio-like ungetc() case chars will be already
3109 if (buf != b->ptr) {
3110 Copy(buf, b->ptr, avail, STDCHAR);
3114 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
3121 PerlIOBuf_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3123 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3124 const STDCHAR *buf = (const STDCHAR *) vbuf;
3128 if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
3131 SSize_t avail = b->bufsiz - (b->ptr - b->buf);
3132 if ((SSize_t) count < avail)
3134 PerlIOBase(f)->flags |= PERLIO_F_WRBUF;
3135 if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF) {
3150 Copy(buf, b->ptr, avail, STDCHAR);
3157 if (b->ptr >= (b->buf + b->bufsiz))
3160 if (PerlIOBase(f)->flags & PERLIO_F_UNBUF)
3166 PerlIOBuf_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
3169 if ((code = PerlIO_flush(f)) == 0) {
3170 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3171 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
3172 code = PerlIO_seek(PerlIONext(f), offset, whence);
3174 b->posn = PerlIO_tell(PerlIONext(f));
3181 PerlIOBuf_tell(pTHX_ PerlIO *f)
3183 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3185 * b->posn is file position where b->buf was read, or will be written
3187 Off_t posn = b->posn;
3190 * If buffer is valid adjust position by amount in buffer
3192 posn += (b->ptr - b->buf);
3198 PerlIOBuf_close(pTHX_ PerlIO *f)
3200 IV code = PerlIOBase_close(aTHX_ f);
3201 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3202 if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
3206 b->ptr = b->end = b->buf;
3207 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
3212 PerlIOBuf_get_ptr(pTHX_ PerlIO *f)
3214 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3221 PerlIOBuf_get_cnt(pTHX_ PerlIO *f)
3223 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3226 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF)
3227 return (b->end - b->ptr);
3232 PerlIOBuf_get_base(pTHX_ PerlIO *f)
3234 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3239 Newz('B',b->buf,b->bufsiz, STDCHAR);
3241 b->buf = (STDCHAR *) & b->oneword;
3242 b->bufsiz = sizeof(b->oneword);
3251 PerlIOBuf_bufsiz(pTHX_ PerlIO *f)
3253 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3256 return (b->end - b->buf);
3260 PerlIOBuf_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
3262 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3266 if (PerlIO_get_cnt(f) != cnt || b->ptr < b->buf) {
3267 assert(PerlIO_get_cnt(f) == cnt);
3268 assert(b->ptr >= b->buf);
3270 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3274 PerlIOBuf_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
3276 return PerlIOBase_dup(aTHX_ f, o, param, flags);
3281 PerlIO_funcs PerlIO_perlio = {
3301 PerlIOBase_clearerr,
3302 PerlIOBase_setlinebuf,
3307 PerlIOBuf_set_ptrcnt,
3310 /*--------------------------------------------------------------------------------------*/
3312 * Temp layer to hold unread chars when cannot do it any other way
3316 PerlIOPending_fill(pTHX_ PerlIO *f)
3319 * Should never happen
3326 PerlIOPending_close(pTHX_ PerlIO *f)
3329 * A tad tricky - flush pops us, then we close new top
3332 return PerlIO_close(f);
3336 PerlIOPending_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
3339 * A tad tricky - flush pops us, then we seek new top
3342 return PerlIO_seek(f, offset, whence);
3347 PerlIOPending_flush(pTHX_ PerlIO *f)
3349 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3350 if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
3354 PerlIO_pop(aTHX_ f);
3359 PerlIOPending_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
3365 PerlIOBuf_set_ptrcnt(aTHX_ f, ptr, cnt);
3370 PerlIOPending_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
3372 IV code = PerlIOBase_pushed(aTHX_ f, mode, arg);
3373 PerlIOl *l = PerlIOBase(f);
3375 * Our PerlIO_fast_gets must match what we are pushed on, or sv_gets()
3376 * etc. get muddled when it changes mid-string when we auto-pop.
3378 l->flags = (l->flags & ~(PERLIO_F_FASTGETS | PERLIO_F_UTF8)) |
3379 (PerlIOBase(PerlIONext(f))->
3380 flags & (PERLIO_F_FASTGETS | PERLIO_F_UTF8));
3385 PerlIOPending_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
3387 SSize_t avail = PerlIO_get_cnt(f);
3389 if ((SSize_t)count < avail)
3392 got = PerlIOBuf_read(aTHX_ f, vbuf, avail);
3393 if (got >= 0 && got < (SSize_t)count) {
3395 PerlIO_read(f, ((STDCHAR *) vbuf) + got, count - got);
3396 if (more >= 0 || got == 0)
3402 PerlIO_funcs PerlIO_pending = {
3406 PerlIOPending_pushed,
3417 PerlIOPending_close,
3418 PerlIOPending_flush,
3422 PerlIOBase_clearerr,
3423 PerlIOBase_setlinebuf,
3428 PerlIOPending_set_ptrcnt,
3433 /*--------------------------------------------------------------------------------------*/
3435 * crlf - translation On read translate CR,LF to "\n" we do this by
3436 * overriding ptr/cnt entries to hand back a line at a time and keeping a
3437 * record of which nl we "lied" about. On write translate "\n" to CR,LF
3441 PerlIOBuf base; /* PerlIOBuf stuff */
3442 STDCHAR *nl; /* Position of crlf we "lied" about in the
3447 PerlIOCrlf_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
3450 PerlIOBase(f)->flags |= PERLIO_F_CRLF;
3451 code = PerlIOBuf_pushed(aTHX_ f, mode, arg);
3453 PerlIO_debug("PerlIOCrlf_pushed f=%p %s %s fl=%08" UVxf "\n",
3454 f, PerlIOBase(f)->tab->name, (mode) ? mode : "(Null)",
3455 PerlIOBase(f)->flags);
3462 PerlIOCrlf_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3464 PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
3469 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF))
3470 return PerlIOBuf_unread(aTHX_ f, vbuf, count);
3472 const STDCHAR *buf = (const STDCHAR *) vbuf + count;
3473 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3475 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
3480 if (!(PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
3481 b->end = b->ptr = b->buf + b->bufsiz;
3482 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3483 b->posn -= b->bufsiz;
3485 while (count > 0 && b->ptr > b->buf) {
3488 if (b->ptr - 2 >= b->buf) {
3511 PerlIOCrlf_get_cnt(pTHX_ PerlIO *f)
3513 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3516 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
3517 PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
3518 if ((PerlIOBase(f)->flags & PERLIO_F_CRLF) && (!c->nl || *c->nl == 0xd)) {
3519 STDCHAR *nl = (c->nl) ? c->nl : b->ptr;
3521 while (nl < b->end && *nl != 0xd)
3523 if (nl < b->end && *nl == 0xd) {
3525 if (nl + 1 < b->end) {
3532 * Not CR,LF but just CR
3540 * Blast - found CR as last char in buffer
3545 * They may not care, defer work as long as
3549 return (nl - b->ptr);
3553 b->ptr++; /* say we have read it as far as
3554 * flush() is concerned */
3555 b->buf++; /* Leave space in front of buffer */
3556 b->bufsiz--; /* Buffer is thus smaller */
3557 code = PerlIO_fill(f); /* Fetch some more */
3558 b->bufsiz++; /* Restore size for next time */
3559 b->buf--; /* Point at space */
3560 b->ptr = nl = b->buf; /* Which is what we hand
3562 b->posn--; /* Buffer starts here */
3563 *nl = 0xd; /* Fill in the CR */
3565 goto test; /* fill() call worked */
3567 * CR at EOF - just fall through
3569 /* Should we clear EOF though ??? */
3574 return (((c->nl) ? (c->nl + 1) : b->end) - b->ptr);
3580 PerlIOCrlf_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
3582 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3583 PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
3589 if (ptr == b->end && *c->nl == 0xd) {
3590 /* Defered CR at end of buffer case - we lied about count */
3602 * Test code - delete when it works ...
3604 IV flags = PerlIOBase(f)->flags;
3605 STDCHAR *chk = (c->nl) ? (c->nl+1) : b->end;
3606 if (ptr+cnt == c->nl && c->nl+1 == b->end && *c->nl == 0xd) {
3607 /* Defered CR at end of buffer case - we lied about count */
3613 Perl_croak(aTHX_ "ptr wrong %p != %p fl=%08" UVxf
3614 " nl=%p e=%p for %d", ptr, chk, flags, c->nl,
3622 * They have taken what we lied about
3630 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3634 PerlIOCrlf_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3636 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF))
3637 return PerlIOBuf_write(aTHX_ f, vbuf, count);
3639 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3640 const STDCHAR *buf = (const STDCHAR *) vbuf;
3641 const STDCHAR *ebuf = buf + count;
3644 if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
3646 while (buf < ebuf) {
3647 STDCHAR *eptr = b->buf + b->bufsiz;
3648 PerlIOBase(f)->flags |= PERLIO_F_WRBUF;
3649 while (buf < ebuf && b->ptr < eptr) {
3651 if ((b->ptr + 2) > eptr) {
3659 *(b->ptr)++ = 0xd; /* CR */
3660 *(b->ptr)++ = 0xa; /* LF */
3662 if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF) {
3672 if (b->ptr >= eptr) {
3678 if (PerlIOBase(f)->flags & PERLIO_F_UNBUF)
3680 return (buf - (STDCHAR *) vbuf);
3685 PerlIOCrlf_flush(pTHX_ PerlIO *f)
3687 PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
3692 return PerlIOBuf_flush(aTHX_ f);
3695 PerlIO_funcs PerlIO_crlf = {
3698 PERLIO_K_BUFFERED | PERLIO_K_CANCRLF,
3700 PerlIOBase_noop_ok, /* popped */
3705 PerlIOBuf_read, /* generic read works with ptr/cnt lies
3707 PerlIOCrlf_unread, /* Put CR,LF in buffer for each '\n' */
3708 PerlIOCrlf_write, /* Put CR,LF in buffer for each '\n' */
3716 PerlIOBase_clearerr,
3717 PerlIOBase_setlinebuf,
3722 PerlIOCrlf_set_ptrcnt,
3726 /*--------------------------------------------------------------------------------------*/
3728 * mmap as "buffer" layer
3732 PerlIOBuf base; /* PerlIOBuf stuff */
3733 Mmap_t mptr; /* Mapped address */
3734 Size_t len; /* mapped length */
3735 STDCHAR *bbuf; /* malloced buffer if map fails */
3738 static size_t page_size = 0;
3741 PerlIOMmap_map(pTHX_ PerlIO *f)
3743 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3744 IV flags = PerlIOBase(f)->flags;
3748 if (flags & PERLIO_F_CANREAD) {
3749 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3750 int fd = PerlIO_fileno(f);
3752 code = Fstat(fd, &st);
3753 if (code == 0 && S_ISREG(st.st_mode)) {
3754 SSize_t len = st.st_size - b->posn;
3758 #if defined(HAS_SYSCONF) && (defined(_SC_PAGESIZE) || defined(_SC_PAGE_SIZE))
3760 SETERRNO(0, SS$_NORMAL);
3761 # ifdef _SC_PAGESIZE
3762 page_size = sysconf(_SC_PAGESIZE);
3764 page_size = sysconf(_SC_PAGE_SIZE);
3766 if ((long) page_size < 0) {
3771 (void) SvUPGRADE(error, SVt_PV);
3772 msg = SvPVx(error, n_a);
3773 Perl_croak(aTHX_ "panic: sysconf: %s",
3778 "panic: sysconf: pagesize unknown");
3782 # ifdef HAS_GETPAGESIZE
3783 page_size = getpagesize();
3785 # if defined(I_SYS_PARAM) && defined(PAGESIZE)
3786 page_size = PAGESIZE; /* compiletime, bad */
3790 if ((IV) page_size <= 0)
3791 Perl_croak(aTHX_ "panic: bad pagesize %" IVdf,
3796 * This is a hack - should never happen - open should
3799 b->posn = PerlIO_tell(PerlIONext(f));
3801 posn = (b->posn / page_size) * page_size;
3802 len = st.st_size - posn;
3803 m->mptr = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, posn);
3804 if (m->mptr && m->mptr != (Mmap_t) - 1) {
3805 #if 0 && defined(HAS_MADVISE) && defined(MADV_SEQUENTIAL)
3806 madvise(m->mptr, len, MADV_SEQUENTIAL);
3808 #if 0 && defined(HAS_MADVISE) && defined(MADV_WILLNEED)
3809 madvise(m->mptr, len, MADV_WILLNEED);
3811 PerlIOBase(f)->flags =
3812 (flags & ~PERLIO_F_EOF) | PERLIO_F_RDBUF;
3813 b->end = ((STDCHAR *) m->mptr) + len;
3814 b->buf = ((STDCHAR *) m->mptr) + (b->posn - posn);
3823 PerlIOBase(f)->flags =
3824 flags | PERLIO_F_EOF | PERLIO_F_RDBUF;
3826 b->ptr = b->end = b->ptr;
3835 PerlIOMmap_unmap(pTHX_ PerlIO *f)
3837 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3838 PerlIOBuf *b = &m->base;
3842 code = munmap(m->mptr, m->len);
3846 if (PerlIO_seek(PerlIONext(f), b->posn, SEEK_SET) != 0)
3849 b->ptr = b->end = b->buf;
3850 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
3856 PerlIOMmap_get_base(pTHX_ PerlIO *f)
3858 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3859 PerlIOBuf *b = &m->base;
3860 if (b->buf && (PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
3862 * Already have a readbuffer in progress
3868 * We have a write buffer or flushed PerlIOBuf read buffer
3870 m->bbuf = b->buf; /* save it in case we need it again */
3871 b->buf = NULL; /* Clear to trigger below */
3874 PerlIOMmap_map(aTHX_ f); /* Try and map it */
3877 * Map did not work - recover PerlIOBuf buffer if we have one
3882 b->ptr = b->end = b->buf;
3885 return PerlIOBuf_get_base(aTHX_ f);
3889 PerlIOMmap_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3891 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3892 PerlIOBuf *b = &m->base;
3893 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
3895 if (b->ptr && (b->ptr - count) >= b->buf
3896 && memEQ(b->ptr - count, vbuf, count)) {
3898 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
3903 * Loose the unwritable mapped buffer
3907 * If flush took the "buffer" see if we have one from before
3909 if (!b->buf && m->bbuf)
3912 PerlIOBuf_get_base(aTHX_ f);
3916 return PerlIOBuf_unread(aTHX_ f, vbuf, count);
3920 PerlIOMmap_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3922 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3923 PerlIOBuf *b = &m->base;
3924 if (!b->buf || !(PerlIOBase(f)->flags & PERLIO_F_WRBUF)) {
3926 * No, or wrong sort of, buffer
3929 if (PerlIOMmap_unmap(aTHX_ f) != 0)
3933 * If unmap took the "buffer" see if we have one from before
3935 if (!b->buf && m->bbuf)
3938 PerlIOBuf_get_base(aTHX_ f);
3942 return PerlIOBuf_write(aTHX_ f, vbuf, count);
3946 PerlIOMmap_flush(pTHX_ PerlIO *f)
3948 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3949 PerlIOBuf *b = &m->base;
3950 IV code = PerlIOBuf_flush(aTHX_ f);
3952 * Now we are "synced" at PerlIOBuf level
3959 if (PerlIOMmap_unmap(aTHX_ f) != 0)
3964 * We seem to have a PerlIOBuf buffer which was not mapped
3965 * remember it in case we need one later
3974 PerlIOMmap_fill(pTHX_ PerlIO *f)
3976 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3977 IV code = PerlIO_flush(f);
3978 if (code == 0 && !b->buf) {
3979 code = PerlIOMmap_map(aTHX_ f);
3981 if (code == 0 && !(PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
3982 code = PerlIOBuf_fill(aTHX_ f);
3988 PerlIOMmap_close(pTHX_ PerlIO *f)
3990 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3991 PerlIOBuf *b = &m->base;
3992 IV code = PerlIO_flush(f);
3996 b->ptr = b->end = b->buf;
3998 if (PerlIOBuf_close(aTHX_ f) != 0)
4004 PerlIOMmap_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
4006 return PerlIOBase_dup(aTHX_ f, o, param, flags);
4010 PerlIO_funcs PerlIO_mmap = {
4030 PerlIOBase_clearerr,
4031 PerlIOBase_setlinebuf,
4032 PerlIOMmap_get_base,
4036 PerlIOBuf_set_ptrcnt,
4039 #endif /* HAS_MMAP */
4042 Perl_PerlIO_stdin(pTHX)
4045 PerlIO_stdstreams(aTHX);
4047 return &PL_perlio[1];
4051 Perl_PerlIO_stdout(pTHX)
4054 PerlIO_stdstreams(aTHX);
4056 return &PL_perlio[2];
4060 Perl_PerlIO_stderr(pTHX)
4063 PerlIO_stdstreams(aTHX);
4065 return &PL_perlio[3];
4068 /*--------------------------------------------------------------------------------------*/
4071 PerlIO_getname(PerlIO *f, char *buf)
4076 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
4078 name = fgetname(stdio, buf);
4080 Perl_croak(aTHX_ "Don't know how to get file name");
4086 /*--------------------------------------------------------------------------------------*/
4088 * Functions which can be called on any kind of PerlIO implemented in
4092 #undef PerlIO_fdopen
4094 PerlIO_fdopen(int fd, const char *mode)
4097 return PerlIO_openn(aTHX_ Nullch, mode, fd, 0, 0, NULL, 0, NULL);
4102 PerlIO_open(const char *path, const char *mode)
4105 SV *name = sv_2mortal(newSVpvn(path, strlen(path)));
4106 return PerlIO_openn(aTHX_ Nullch, mode, -1, 0, 0, NULL, 1, &name);
4109 #undef Perlio_reopen
4111 PerlIO_reopen(const char *path, const char *mode, PerlIO *f)
4114 SV *name = sv_2mortal(newSVpvn(path, strlen(path)));
4115 return PerlIO_openn(aTHX_ Nullch, mode, -1, 0, 0, f, 1, &name);
4120 PerlIO_getc(PerlIO *f)
4124 SSize_t count = PerlIO_read(f, buf, 1);
4126 return (unsigned char) buf[0];
4131 #undef PerlIO_ungetc
4133 PerlIO_ungetc(PerlIO *f, int ch)
4138 if (PerlIO_unread(f, &buf, 1) == 1)
4146 PerlIO_putc(PerlIO *f, int ch)
4150 return PerlIO_write(f, &buf, 1);
4155 PerlIO_puts(PerlIO *f, const char *s)
4158 STRLEN len = strlen(s);
4159 return PerlIO_write(f, s, len);
4162 #undef PerlIO_rewind
4164 PerlIO_rewind(PerlIO *f)
4167 PerlIO_seek(f, (Off_t) 0, SEEK_SET);
4171 #undef PerlIO_vprintf
4173 PerlIO_vprintf(PerlIO *f, const char *fmt, va_list ap)
4176 SV *sv = newSVpvn("", 0);
4182 Perl_va_copy(ap, apc);
4183 sv_vcatpvf(sv, fmt, &apc);
4185 sv_vcatpvf(sv, fmt, &ap);
4188 wrote = PerlIO_write(f, s, len);
4193 #undef PerlIO_printf
4195 PerlIO_printf(PerlIO *f, const char *fmt, ...)
4200 result = PerlIO_vprintf(f, fmt, ap);
4205 #undef PerlIO_stdoutf
4207 PerlIO_stdoutf(const char *fmt, ...)
4213 result = PerlIO_vprintf(PerlIO_stdout(), fmt, ap);
4218 #undef PerlIO_tmpfile
4220 PerlIO_tmpfile(void)
4223 * I have no idea how portable mkstemp() is ...
4225 #if defined(WIN32) || !defined(HAVE_MKSTEMP)
4228 FILE *stdio = PerlSIO_tmpfile();
4231 PerlIOSelf(PerlIO_push
4232 (aTHX_(f = PerlIO_allocate(aTHX)), &PerlIO_stdio,
4233 "w+", Nullsv), PerlIOStdio);
4239 SV *sv = newSVpv("/tmp/PerlIO_XXXXXX", 0);
4240 int fd = mkstemp(SvPVX(sv));
4243 f = PerlIO_fdopen(fd, "w+");
4245 PerlIOBase(f)->flags |= PERLIO_F_TEMP;
4247 PerlLIO_unlink(SvPVX(sv));
4257 #endif /* USE_SFIO */
4258 #endif /* PERLIO_IS_STDIO */
4260 /*======================================================================================*/
4262 * Now some functions in terms of above which may be needed even if we are
4263 * not in true PerlIO mode
4267 #undef PerlIO_setpos
4269 PerlIO_setpos(PerlIO *f, SV *pos)
4274 Off_t *posn = (Off_t *) SvPV(pos, len);
4275 if (f && len == sizeof(Off_t))
4276 return PerlIO_seek(f, *posn, SEEK_SET);
4278 SETERRNO(EINVAL, SS$_IVCHAN);
4282 #undef PerlIO_setpos
4284 PerlIO_setpos(PerlIO *f, SV *pos)
4289 Fpos_t *fpos = (Fpos_t *) SvPV(pos, len);
4290 if (f && len == sizeof(Fpos_t)) {
4291 #if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64)
4292 return fsetpos64(f, fpos);
4294 return fsetpos(f, fpos);
4298 SETERRNO(EINVAL, SS$_IVCHAN);
4304 #undef PerlIO_getpos
4306 PerlIO_getpos(PerlIO *f, SV *pos)
4309 Off_t posn = PerlIO_tell(f);
4310 sv_setpvn(pos, (char *) &posn, sizeof(posn));
4311 return (posn == (Off_t) - 1) ? -1 : 0;
4314 #undef PerlIO_getpos
4316 PerlIO_getpos(PerlIO *f, SV *pos)
4321 #if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64)
4322 code = fgetpos64(f, &fpos);
4324 code = fgetpos(f, &fpos);
4326 sv_setpvn(pos, (char *) &fpos, sizeof(fpos));
4331 #if (defined(PERLIO_IS_STDIO) || !defined(USE_SFIO)) && !defined(HAS_VPRINTF)
4334 vprintf(char *pat, char *args)
4336 _doprnt(pat, args, stdout);
4337 return 0; /* wrong, but perl doesn't use the return
4342 vfprintf(FILE *fd, char *pat, char *args)
4344 _doprnt(pat, args, fd);
4345 return 0; /* wrong, but perl doesn't use the return
4351 #ifndef PerlIO_vsprintf
4353 PerlIO_vsprintf(char *s, int n, const char *fmt, va_list ap)
4355 int val = vsprintf(s, fmt, ap);
4357 if (strlen(s) >= (STRLEN) n) {
4359 (void) PerlIO_puts(Perl_error_log,
4360 "panic: sprintf overflow - memory corrupted!\n");
4368 #ifndef PerlIO_sprintf
4370 PerlIO_sprintf(char *s, int n, const char *fmt, ...)
4375 result = PerlIO_vsprintf(s, n, fmt, ap);