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)
191 int fd = PerlLIO_dup(PerlIO_fileno(f));
194 int omode = fcntl(fd, F_GETFL);
196 omode = djgpp_get_stream_mode(f);
198 PerlIO_intmode2str(omode,mode,NULL);
199 /* the r+ is a hack */
200 return PerlIO_fdopen(fd, mode);
205 SETERRNO(EBADF, SS$_IVCHAN);
213 * De-mux PerlIO_openn() into fdopen, freopen and fopen type entries
217 PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd,
218 int imode, int perm, PerlIO *old, int narg, SV **args)
222 Perl_croak(aTHX_ "More than one argument to open");
224 if (*args == &PL_sv_undef)
225 return PerlIO_tmpfile();
227 char *name = SvPV_nolen(*args);
229 fd = PerlLIO_open3(name, imode, perm);
231 return PerlIO_fdopen(fd, (char *) mode + 1);
234 return PerlIO_reopen(name, mode, old);
237 return PerlIO_open(name, mode);
242 return PerlIO_fdopen(fd, (char *) mode);
247 XS(XS_PerlIO__Layer__find)
251 Perl_croak(aTHX_ "Usage class->find(name[,load])");
253 char *name = SvPV_nolen(ST(1));
254 ST(0) = (strEQ(name, "crlf")
255 || strEQ(name, "raw")) ? &PL_sv_yes : &PL_sv_undef;
262 Perl_boot_core_PerlIO(pTHX)
264 newXS("PerlIO::Layer::find", XS_PerlIO__Layer__find, __FILE__);
270 #ifdef PERLIO_IS_STDIO
276 * Does nothing (yet) except force this file to be included in perl
277 * binary. That allows this file to force inclusion of other functions
278 * that may be required by loadable extensions e.g. for
279 * FileHandle::tmpfile
283 #undef PerlIO_tmpfile
290 #else /* PERLIO_IS_STDIO */
298 * This section is just to make sure these functions get pulled in from
302 #undef PerlIO_tmpfile
313 * Force this file to be included in perl binary. Which allows this
314 * file to force inclusion of other functions that may be required by
315 * loadable extensions e.g. for FileHandle::tmpfile
319 * Hack sfio does its own 'autoflush' on stdout in common cases. Flush
320 * results in a lot of lseek()s to regular files and lot of small
323 sfset(sfstdout, SF_SHARE, 0);
327 PerlIO_importFILE(FILE *stdio, int fl)
329 int fd = fileno(stdio);
330 PerlIO *r = PerlIO_fdopen(fd, "r+");
335 PerlIO_findFILE(PerlIO *pio)
337 int fd = PerlIO_fileno(pio);
338 FILE *f = fdopen(fd, "r+");
340 if (!f && errno == EINVAL)
342 if (!f && errno == EINVAL)
349 /*======================================================================================*/
351 * Implement all the PerlIO interface ourselves.
357 * We _MUST_ have <unistd.h> if we are using lseek() and may have large
364 #include <sys/mman.h>
368 void PerlIO_debug(const char *fmt, ...)
369 __attribute__ ((format(__printf__, 1, 2)));
372 PerlIO_debug(const char *fmt, ...)
379 char *s = PerlEnv_getenv("PERLIO_DEBUG");
381 dbg = PerlLIO_open3(s, O_WRONLY | O_CREAT | O_APPEND, 0666);
388 /* Use fixed buffer as sv_catpvf etc. needs SVs */
392 s = CopFILE(PL_curcop);
395 sprintf(buffer, "%s:%" IVdf " ", s, (IV) CopLINE(PL_curcop));
396 len = strlen(buffer);
397 vsprintf(buffer+len, fmt, ap);
398 PerlLIO_write(dbg, buffer, strlen(buffer));
400 SV *sv = newSVpvn("", 0);
403 s = CopFILE(PL_curcop);
406 Perl_sv_catpvf(aTHX_ sv, "%s:%" IVdf " ", s,
407 (IV) CopLINE(PL_curcop));
408 Perl_sv_vcatpvf(aTHX_ sv, fmt, &ap);
411 PerlLIO_write(dbg, s, len);
418 /*--------------------------------------------------------------------------------------*/
421 * Inner level routines
425 * Table of pointers to the PerlIO structs (malloc'ed)
427 #define PERLIO_TABLE_SIZE 64
430 PerlIO_allocate(pTHX)
433 * Find a free slot in the table, allocating new table as necessary
438 while ((f = *last)) {
440 last = (PerlIO **) (f);
441 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
447 Newz('I',f,PERLIO_TABLE_SIZE,PerlIO);
455 #undef PerlIO_fdupopen
457 PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
459 if (PerlIOValid(f)) {
460 PerlIO_funcs *tab = PerlIOBase(f)->tab;
462 PerlIO_debug("fdupopen f=%p param=%p\n",(void*)f,(void*)param);
463 new = (*tab->Dup)(aTHX_ PerlIO_allocate(aTHX),f,param, flags);
467 SETERRNO(EBADF, SS$_IVCHAN);
473 PerlIO_cleantable(pTHX_ PerlIO **tablep)
475 PerlIO *table = *tablep;
478 PerlIO_cleantable(aTHX_(PerlIO **) & (table[0]));
479 for (i = PERLIO_TABLE_SIZE - 1; i > 0; i--) {
480 PerlIO *f = table + i;
492 PerlIO_list_alloc(pTHX)
495 Newz('L', list, 1, PerlIO_list_t);
501 PerlIO_list_free(pTHX_ PerlIO_list_t *list)
504 if (--list->refcnt == 0) {
507 for (i = 0; i < list->cur; i++) {
508 if (list->array[i].arg)
509 SvREFCNT_dec(list->array[i].arg);
511 Safefree(list->array);
519 PerlIO_list_push(pTHX_ PerlIO_list_t *list, PerlIO_funcs *funcs, SV *arg)
522 if (list->cur >= list->len) {
525 Renew(list->array, list->len, PerlIO_pair_t);
527 New('l', list->array, list->len, PerlIO_pair_t);
529 p = &(list->array[list->cur++]);
531 if ((p->arg = arg)) {
537 PerlIO_clone_list(pTHX_ PerlIO_list_t *proto, CLONE_PARAMS *param)
539 PerlIO_list_t *list = (PerlIO_list_t *) NULL;
542 list = PerlIO_list_alloc(aTHX);
543 for (i=0; i < proto->cur; i++) {
545 if (proto->array[i].arg)
546 arg = PerlIO_sv_dup(aTHX_ proto->array[i].arg,param);
547 PerlIO_list_push(aTHX_ list, proto->array[i].funcs, arg);
554 PerlIO_clone(pTHX_ PerlInterpreter *proto, CLONE_PARAMS *param)
557 PerlIO **table = &proto->Iperlio;
560 PL_known_layers = PerlIO_clone_list(aTHX_ proto->Iknown_layers, param);
561 PL_def_layerlist = PerlIO_clone_list(aTHX_ proto->Idef_layerlist, param);
562 PerlIO_allocate(aTHX); /* root slot is never used */
563 PerlIO_debug("Clone %p from %p\n",aTHX,proto);
564 while ((f = *table)) {
566 table = (PerlIO **) (f++);
567 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
569 (void) fp_dup(f, 0, param);
578 PerlIO_destruct(pTHX)
580 PerlIO **table = &PL_perlio;
583 PerlIO_debug("Destruct %p\n",aTHX);
585 while ((f = *table)) {
587 table = (PerlIO **) (f++);
588 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
592 if (l->tab->kind & PERLIO_K_DESTRUCT) {
593 PerlIO_debug("Destruct popping %s\n", l->tab->name);
604 PerlIO_list_free(aTHX_ PL_known_layers);
605 PL_known_layers = NULL;
606 PerlIO_list_free(aTHX_ PL_def_layerlist);
607 PL_def_layerlist = NULL;
611 PerlIO_pop(pTHX_ PerlIO *f)
615 PerlIO_debug("PerlIO_pop f=%p %s\n", (void*)f, l->tab->name);
616 if (l->tab->Popped) {
618 * If popped returns non-zero do not free its layer structure
619 * it has either done so itself, or it is shared and still in
622 if ((*l->tab->Popped) (aTHX_ f) != 0)
630 /*--------------------------------------------------------------------------------------*/
632 * XS Interface for perl code
636 PerlIO_find_layer(pTHX_ const char *name, STRLEN len, int load)
639 if ((SSize_t) len <= 0)
641 for (i = 0; i < PL_known_layers->cur; i++) {
642 PerlIO_funcs *f = PL_known_layers->array[i].funcs;
643 if (memEQ(f->name, name, len)) {
644 PerlIO_debug("%.*s => %p\n", (int) len, name, (void*)f);
648 if (load && PL_subname && PL_def_layerlist
649 && PL_def_layerlist->cur >= 2) {
650 SV *pkgsv = newSVpvn("PerlIO", 6);
651 SV *layer = newSVpvn(name, len);
654 * The two SVs are magically freed by load_module
656 Perl_load_module(aTHX_ 0, pkgsv, Nullsv, layer, Nullsv);
658 return PerlIO_find_layer(aTHX_ name, len, 0);
660 PerlIO_debug("Cannot find %.*s\n", (int) len, name);
664 #ifdef USE_ATTRIBUTES_FOR_PERLIO
667 perlio_mg_set(pTHX_ SV *sv, MAGIC *mg)
670 IO *io = GvIOn((GV *) SvRV(sv));
671 PerlIO *ifp = IoIFP(io);
672 PerlIO *ofp = IoOFP(io);
673 Perl_warn(aTHX_ "set %" SVf " %p %p %p", sv, io, ifp, ofp);
679 perlio_mg_get(pTHX_ SV *sv, MAGIC *mg)
682 IO *io = GvIOn((GV *) SvRV(sv));
683 PerlIO *ifp = IoIFP(io);
684 PerlIO *ofp = IoOFP(io);
685 Perl_warn(aTHX_ "get %" SVf " %p %p %p", sv, io, ifp, ofp);
691 perlio_mg_clear(pTHX_ SV *sv, MAGIC *mg)
693 Perl_warn(aTHX_ "clear %" SVf, sv);
698 perlio_mg_free(pTHX_ SV *sv, MAGIC *mg)
700 Perl_warn(aTHX_ "free %" SVf, sv);
704 MGVTBL perlio_vtab = {
712 XS(XS_io_MODIFY_SCALAR_ATTRIBUTES)
715 SV *sv = SvRV(ST(1));
720 sv_magic(sv, (SV *) av, PERL_MAGIC_ext, NULL, 0);
722 mg = mg_find(sv, PERL_MAGIC_ext);
723 mg->mg_virtual = &perlio_vtab;
725 Perl_warn(aTHX_ "attrib %" SVf, sv);
726 for (i = 2; i < items; i++) {
728 const char *name = SvPV(ST(i), len);
729 SV *layer = PerlIO_find_layer(aTHX_ name, len, 1);
731 av_push(av, SvREFCNT_inc(layer));
742 #endif /* USE_ATTIBUTES_FOR_PERLIO */
745 PerlIO_tab_sv(pTHX_ PerlIO_funcs *tab)
747 HV *stash = gv_stashpv("PerlIO::Layer", TRUE);
748 SV *sv = sv_bless(newRV_noinc(newSViv(PTR2IV(tab))), stash);
752 XS(XS_PerlIO__Layer__find)
756 Perl_croak(aTHX_ "Usage class->find(name[,load])");
759 char *name = SvPV(ST(1), len);
760 bool load = (items > 2) ? SvTRUE(ST(2)) : 0;
761 PerlIO_funcs *layer = PerlIO_find_layer(aTHX_ name, len, load);
763 (layer) ? sv_2mortal(PerlIO_tab_sv(aTHX_ layer)) :
770 PerlIO_define_layer(pTHX_ PerlIO_funcs *tab)
772 if (!PL_known_layers)
773 PL_known_layers = PerlIO_list_alloc(aTHX);
774 PerlIO_list_push(aTHX_ PL_known_layers, tab, Nullsv);
775 PerlIO_debug("define %s %p\n", tab->name, (void*)tab);
779 PerlIO_parse_layers(pTHX_ PerlIO_list_t *av, const char *names)
782 const char *s = names;
784 while (isSPACE(*s) || *s == ':')
789 const char *as = Nullch;
791 if (!isIDFIRST(*s)) {
793 * Message is consistent with how attribute lists are
794 * passed. Even though this means "foo : : bar" is
795 * seen as an invalid separator character.
797 char q = ((*s == '\'') ? '"' : '\'');
798 if (ckWARN(WARN_LAYER))
799 Perl_warner(aTHX_ packWARN(WARN_LAYER),
800 "perlio: invalid separator character %c%c%c in layer specification list %s",
806 } while (isALNUM(*e));
822 * It's a nul terminated string, not allowed
823 * to \ the terminating null. Anything other
824 * character is passed over.
834 if (ckWARN(WARN_LAYER))
835 Perl_warner(aTHX_ packWARN(WARN_LAYER),
836 "perlio: argument list not closed for layer \"%.*s\"",
848 bool warn_layer = ckWARN(WARN_LAYER);
849 PerlIO_funcs *layer =
850 PerlIO_find_layer(aTHX_ s, llen, 1);
852 PerlIO_list_push(aTHX_ av, layer,
859 Perl_warner(aTHX_ packWARN(WARN_LAYER), "perlio: unknown layer \"%.*s\"",
872 PerlIO_default_buffer(pTHX_ PerlIO_list_t *av)
874 PerlIO_funcs *tab = &PerlIO_perlio;
875 #ifdef PERLIO_USING_CRLF
878 if (PerlIO_stdio.Set_ptrcnt)
881 PerlIO_debug("Pushing %s\n", tab->name);
882 PerlIO_list_push(aTHX_ av, PerlIO_find_layer(aTHX_ tab->name, 0, 0),
887 PerlIO_arg_fetch(PerlIO_list_t *av, IV n)
889 return av->array[n].arg;
893 PerlIO_layer_fetch(pTHX_ PerlIO_list_t *av, IV n, PerlIO_funcs *def)
895 if (n >= 0 && n < av->cur) {
896 PerlIO_debug("Layer %" IVdf " is %s\n", n,
897 av->array[n].funcs->name);
898 return av->array[n].funcs;
901 Perl_croak(aTHX_ "panic: PerlIO layer array corrupt");
906 PerlIO_default_layers(pTHX)
908 if (!PL_def_layerlist) {
909 const char *s = (PL_tainting) ? Nullch : PerlEnv_getenv("PERLIO");
910 PerlIO_funcs *osLayer = &PerlIO_unix;
911 PL_def_layerlist = PerlIO_list_alloc(aTHX);
912 PerlIO_define_layer(aTHX_ & PerlIO_unix);
913 #if defined(WIN32) && !defined(UNDER_CE)
914 PerlIO_define_layer(aTHX_ & PerlIO_win32);
916 osLayer = &PerlIO_win32;
919 PerlIO_define_layer(aTHX_ & PerlIO_raw);
920 PerlIO_define_layer(aTHX_ & PerlIO_perlio);
921 PerlIO_define_layer(aTHX_ & PerlIO_stdio);
922 PerlIO_define_layer(aTHX_ & PerlIO_crlf);
924 PerlIO_define_layer(aTHX_ & PerlIO_mmap);
926 PerlIO_define_layer(aTHX_ & PerlIO_utf8);
927 PerlIO_define_layer(aTHX_ & PerlIO_byte);
928 PerlIO_list_push(aTHX_ PL_def_layerlist,
929 PerlIO_find_layer(aTHX_ osLayer->name, 0, 0),
932 PerlIO_parse_layers(aTHX_ PL_def_layerlist, s);
935 PerlIO_default_buffer(aTHX_ PL_def_layerlist);
938 if (PL_def_layerlist->cur < 2) {
939 PerlIO_default_buffer(aTHX_ PL_def_layerlist);
941 return PL_def_layerlist;
945 Perl_boot_core_PerlIO(pTHX)
947 #ifdef USE_ATTRIBUTES_FOR_PERLIO
948 newXS("io::MODIFY_SCALAR_ATTRIBUTES", XS_io_MODIFY_SCALAR_ATTRIBUTES,
951 newXS("PerlIO::Layer::find", XS_PerlIO__Layer__find, __FILE__);
955 PerlIO_default_layer(pTHX_ I32 n)
957 PerlIO_list_t *av = PerlIO_default_layers(aTHX);
960 return PerlIO_layer_fetch(aTHX_ av, n, &PerlIO_stdio);
963 #define PerlIO_default_top() PerlIO_default_layer(aTHX_ -1)
964 #define PerlIO_default_btm() PerlIO_default_layer(aTHX_ 0)
967 PerlIO_stdstreams(pTHX)
970 PerlIO_allocate(aTHX);
971 PerlIO_fdopen(0, "Ir" PERLIO_STDTEXT);
972 PerlIO_fdopen(1, "Iw" PERLIO_STDTEXT);
973 PerlIO_fdopen(2, "Iw" PERLIO_STDTEXT);
978 PerlIO_push(pTHX_ PerlIO *f, PerlIO_funcs *tab, const char *mode, SV *arg)
981 Newc('L',l,tab->size,char,PerlIOl);
983 Zero(l, tab->size, char);
987 PerlIO_debug("PerlIO_push f=%p %s %s %p\n", (void*)f, tab->name,
988 (mode) ? mode : "(Null)", (void*)arg);
989 if ((*l->tab->Pushed) (aTHX_ f, mode, arg) != 0) {
998 PerlIOPop_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
1000 PerlIO_pop(aTHX_ f);
1003 PerlIO_pop(aTHX_ f);
1010 PerlIORaw_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
1013 * Remove the dummy layer
1015 PerlIO_pop(aTHX_ f);
1017 * Pop back to bottom layer
1019 if (PerlIOValid(f)) {
1021 while (!(PerlIOBase(f)->tab->kind & PERLIO_K_RAW)) {
1022 if (*PerlIONext(f)) {
1023 PerlIO_pop(aTHX_ f);
1027 * Nothing bellow - push unix on top then remove it
1029 if (PerlIO_push(aTHX_ f, PerlIO_default_btm(), mode, arg)) {
1030 PerlIO_pop(aTHX_ PerlIONext(f));
1035 PerlIO_debug(":raw f=%p :%s\n", (void*)f, PerlIOBase(f)->tab->name);
1042 PerlIO_apply_layera(pTHX_ PerlIO *f, const char *mode,
1043 PerlIO_list_t *layers, IV n)
1045 IV max = layers->cur;
1048 PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n, NULL);
1050 if (!PerlIO_push(aTHX_ f, tab, mode, PerlIOArg)) {
1061 PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names)
1065 PerlIO_list_t *layers = PerlIO_list_alloc(aTHX);
1066 code = PerlIO_parse_layers(aTHX_ layers, names);
1068 code = PerlIO_apply_layera(aTHX_ f, mode, layers, 0);
1070 PerlIO_list_free(aTHX_ layers);
1076 /*--------------------------------------------------------------------------------------*/
1078 * Given the abstraction above the public API functions
1082 PerlIO_binmode(pTHX_ PerlIO *f, int iotype, int mode, const char *names)
1084 PerlIO_debug("PerlIO_binmode f=%p %s %c %x %s\n",
1085 (void*)f, PerlIOBase(f)->tab->name, iotype, mode,
1086 (names) ? names : "(Null)");
1088 /* Do not flush etc. if (e.g.) switching encodings.
1089 if a pushed layer knows it needs to flush lower layers
1090 (for example :unix which is never going to call them)
1091 it can do the flush when it is pushed.
1093 return PerlIO_apply_layers(aTHX_ f, NULL, names) == 0 ? TRUE : FALSE;
1096 /* FIXME?: Looking down the layer stack seems wrong,
1097 but is a way of reaching past (say) an encoding layer
1098 to flip CRLF-ness of the layer(s) below
1100 #ifdef PERLIO_USING_CRLF
1101 /* Legacy binmode only has meaning if O_TEXT has a value distinct from
1102 O_BINARY so we can look for it in mode.
1104 if (!(mode & O_BINARY)) {
1107 /* Perhaps we should turn on bottom-most aware layer
1108 e.g. Ilya's idea that UNIX TTY could serve
1110 if (PerlIOBase(f)->tab->kind & PERLIO_K_CANCRLF) {
1111 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF)) {
1112 /* Not in text mode - flush any pending stuff and flip it */
1114 PerlIOBase(f)->flags |= PERLIO_F_CRLF;
1116 /* Only need to turn it on in one layer so we are done */
1121 /* Not finding a CRLF aware layer presumably means we are binary
1122 which is not what was requested - so we failed
1123 We _could_ push :crlf layer but so could caller
1128 /* Either asked for BINMODE or that is normal on this platform
1129 see if any CRLF aware layers are present and turn off the flag
1130 and possibly remove layer.
1133 if (PerlIOBase(f)->tab->kind & PERLIO_K_CANCRLF) {
1134 if ((PerlIOBase(f)->flags & PERLIO_F_CRLF)) {
1135 /* In text mode - flush any pending stuff and flip it */
1137 PerlIOBase(f)->flags &= ~PERLIO_F_CRLF;
1138 #ifndef PERLIO_USING_CRLF
1139 /* CRLF is unusual case - if this is just the :crlf layer pop it */
1140 if (PerlIOBase(f)->tab == &PerlIO_crlf) {
1141 PerlIO_pop(aTHX_ f);
1144 /* Normal case is only one layer doing this, so exit on first
1145 abnormal case can always do multiple binmode calls
1157 PerlIO__close(pTHX_ PerlIO *f)
1160 return (*PerlIOBase(f)->tab->Close) (aTHX_ f);
1162 SETERRNO(EBADF, SS$_IVCHAN);
1168 Perl_PerlIO_close(pTHX_ PerlIO *f)
1171 if (PerlIOValid(f)) {
1172 code = (*PerlIOBase(f)->tab->Close) (aTHX_ f);
1174 PerlIO_pop(aTHX_ f);
1181 Perl_PerlIO_fileno(pTHX_ PerlIO *f)
1184 return (*PerlIOBase(f)->tab->Fileno) (aTHX_ f);
1186 SETERRNO(EBADF, SS$_IVCHAN);
1192 PerlIO_context_layers(pTHX_ const char *mode)
1194 const char *type = NULL;
1196 * Need to supply default layer info from open.pm
1199 SV *layers = PL_curcop->cop_io;
1202 type = SvPV(layers, len);
1203 if (type && mode[0] != 'r') {
1205 * Skip to write part
1207 const char *s = strchr(type, 0);
1208 if (s && (STRLEN)(s - type) < len) {
1217 static PerlIO_funcs *
1218 PerlIO_layer_from_ref(pTHX_ SV *sv)
1221 * For any scalar type load the handler which is bundled with perl
1223 if (SvTYPE(sv) < SVt_PVAV)
1224 return PerlIO_find_layer(aTHX_ "Scalar", 6, 1);
1227 * For other types allow if layer is known but don't try and load it
1229 switch (SvTYPE(sv)) {
1231 return PerlIO_find_layer(aTHX_ "Array", 5, 0);
1233 return PerlIO_find_layer(aTHX_ "Hash", 4, 0);
1235 return PerlIO_find_layer(aTHX_ "Code", 4, 0);
1237 return PerlIO_find_layer(aTHX_ "Glob", 4, 0);
1243 PerlIO_resolve_layers(pTHX_ const char *layers,
1244 const char *mode, int narg, SV **args)
1246 PerlIO_list_t *def = PerlIO_default_layers(aTHX);
1249 PerlIO_stdstreams(aTHX);
1253 * If it is a reference but not an object see if we have a handler
1256 if (SvROK(arg) && !sv_isobject(arg)) {
1257 PerlIO_funcs *handler = PerlIO_layer_from_ref(aTHX_ SvRV(arg));
1259 def = PerlIO_list_alloc(aTHX);
1260 PerlIO_list_push(aTHX_ def, handler, &PL_sv_undef);
1264 * Don't fail if handler cannot be found :Via(...) etc. may do
1265 * something sensible else we will just stringfy and open
1271 layers = PerlIO_context_layers(aTHX_ mode);
1272 if (layers && *layers) {
1276 av = PerlIO_list_alloc(aTHX);
1277 for (i = 0; i < def->cur; i++) {
1278 PerlIO_list_push(aTHX_ av, def->array[i].funcs,
1285 PerlIO_parse_layers(aTHX_ av, layers);
1296 PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd,
1297 int imode, int perm, PerlIO *f, int narg, SV **args)
1299 if (!f && narg == 1 && *args == &PL_sv_undef) {
1300 if ((f = PerlIO_tmpfile())) {
1302 layers = PerlIO_context_layers(aTHX_ mode);
1303 if (layers && *layers)
1304 PerlIO_apply_layers(aTHX_ f, mode, layers);
1308 PerlIO_list_t *layera = NULL;
1310 PerlIO_funcs *tab = NULL;
1311 if (PerlIOValid(f)) {
1313 * This is "reopen" - it is not tested as perl does not use it
1317 layera = PerlIO_list_alloc(aTHX);
1319 SV *arg = (l->tab->Getarg)
1320 ? (*l->tab->Getarg) (aTHX_ &l, NULL, 0)
1322 PerlIO_list_push(aTHX_ layera, l->tab, arg);
1323 l = *PerlIONext(&l);
1327 layera = PerlIO_resolve_layers(aTHX_ layers, mode, narg, args);
1330 * Start at "top" of layer stack
1332 n = layera->cur - 1;
1334 PerlIO_funcs *t = PerlIO_layer_fetch(aTHX_ layera, n, NULL);
1343 * Found that layer 'n' can do opens - call it
1345 if (narg > 1 && !(tab->kind & PERLIO_K_MULTIARG)) {
1346 Perl_croak(aTHX_ "More than one argument to open(,':%s')",tab->name);
1348 PerlIO_debug("openn(%s,'%s','%s',%d,%x,%o,%p,%d,%p)\n",
1349 tab->name, layers, mode, fd, imode, perm,
1350 (void*)f, narg, (void*)args);
1351 f = (*tab->Open) (aTHX_ tab, layera, n, mode, fd, imode, perm,
1354 if (n + 1 < layera->cur) {
1356 * More layers above the one that we used to open -
1359 if (PerlIO_apply_layera(aTHX_ f, mode, layera, n + 1)
1366 PerlIO_list_free(aTHX_ layera);
1373 Perl_PerlIO_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
1376 return (*PerlIOBase(f)->tab->Read) (aTHX_ f, vbuf, count);
1378 SETERRNO(EBADF, SS$_IVCHAN);
1384 Perl_PerlIO_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
1387 return (*PerlIOBase(f)->tab->Unread) (aTHX_ f, vbuf, count);
1389 SETERRNO(EBADF, SS$_IVCHAN);
1395 Perl_PerlIO_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
1398 return (*PerlIOBase(f)->tab->Write) (aTHX_ f, vbuf, count);
1400 SETERRNO(EBADF, SS$_IVCHAN);
1406 Perl_PerlIO_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
1409 return (*PerlIOBase(f)->tab->Seek) (aTHX_ f, offset, whence);
1411 SETERRNO(EBADF, SS$_IVCHAN);
1417 Perl_PerlIO_tell(pTHX_ PerlIO *f)
1420 return (*PerlIOBase(f)->tab->Tell) (aTHX_ f);
1422 SETERRNO(EBADF, SS$_IVCHAN);
1428 Perl_PerlIO_flush(pTHX_ PerlIO *f)
1432 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1433 if (tab && tab->Flush) {
1434 return (*tab->Flush) (aTHX_ f);
1437 PerlIO_debug("Cannot flush f=%p :%s\n", (void*)f, tab->name);
1438 SETERRNO(EBADF, SS$_IVCHAN);
1443 PerlIO_debug("Cannot flush f=%p\n", (void*)f);
1444 SETERRNO(EBADF, SS$_IVCHAN);
1450 * Is it good API design to do flush-all on NULL, a potentially
1451 * errorneous input? Maybe some magical value (PerlIO*
1452 * PERLIO_FLUSH_ALL = (PerlIO*)-1;)? Yes, stdio does similar
1453 * things on fflush(NULL), but should we be bound by their design
1456 PerlIO **table = &PL_perlio;
1458 while ((f = *table)) {
1460 table = (PerlIO **) (f++);
1461 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
1462 if (*f && PerlIO_flush(f) != 0)
1472 PerlIOBase_flush_linebuf(pTHX)
1474 PerlIO **table = &PL_perlio;
1476 while ((f = *table)) {
1478 table = (PerlIO **) (f++);
1479 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
1482 flags & (PERLIO_F_LINEBUF | PERLIO_F_CANWRITE))
1483 == (PERLIO_F_LINEBUF | PERLIO_F_CANWRITE))
1491 Perl_PerlIO_fill(pTHX_ PerlIO *f)
1494 return (*PerlIOBase(f)->tab->Fill) (aTHX_ f);
1496 SETERRNO(EBADF, SS$_IVCHAN);
1502 PerlIO_isutf8(PerlIO *f)
1505 return (PerlIOBase(f)->flags & PERLIO_F_UTF8) != 0;
1507 SETERRNO(EBADF, SS$_IVCHAN);
1513 Perl_PerlIO_eof(pTHX_ PerlIO *f)
1516 return (*PerlIOBase(f)->tab->Eof) (aTHX_ f);
1518 SETERRNO(EBADF, SS$_IVCHAN);
1524 Perl_PerlIO_error(pTHX_ PerlIO *f)
1527 return (*PerlIOBase(f)->tab->Error) (aTHX_ f);
1529 SETERRNO(EBADF, SS$_IVCHAN);
1535 Perl_PerlIO_clearerr(pTHX_ PerlIO *f)
1538 (*PerlIOBase(f)->tab->Clearerr) (aTHX_ f);
1540 SETERRNO(EBADF, SS$_IVCHAN);
1544 Perl_PerlIO_setlinebuf(pTHX_ PerlIO *f)
1547 (*PerlIOBase(f)->tab->Setlinebuf) (aTHX_ f);
1549 SETERRNO(EBADF, SS$_IVCHAN);
1553 PerlIO_has_base(PerlIO *f)
1555 if (PerlIOValid(f)) {
1556 return (PerlIOBase(f)->tab->Get_base != NULL);
1562 PerlIO_fast_gets(PerlIO *f)
1564 if (PerlIOValid(f) && (PerlIOBase(f)->flags & PERLIO_F_FASTGETS)) {
1565 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1566 return (tab->Set_ptrcnt != NULL);
1572 PerlIO_has_cntptr(PerlIO *f)
1574 if (PerlIOValid(f)) {
1575 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1576 return (tab->Get_ptr != NULL && tab->Get_cnt != NULL);
1582 PerlIO_canset_cnt(PerlIO *f)
1584 if (PerlIOValid(f)) {
1585 PerlIOl *l = PerlIOBase(f);
1586 return (l->tab->Set_ptrcnt != NULL);
1592 Perl_PerlIO_get_base(pTHX_ PerlIO *f)
1595 return (*PerlIOBase(f)->tab->Get_base) (aTHX_ f);
1600 Perl_PerlIO_get_bufsiz(pTHX_ PerlIO *f)
1603 return (*PerlIOBase(f)->tab->Get_bufsiz) (aTHX_ f);
1608 Perl_PerlIO_get_ptr(pTHX_ PerlIO *f)
1610 if (PerlIOValid(f)) {
1611 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1612 if (tab->Get_ptr == NULL)
1614 return (*tab->Get_ptr) (aTHX_ f);
1620 Perl_PerlIO_get_cnt(pTHX_ PerlIO *f)
1622 if (PerlIOValid(f)) {
1623 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1624 if (tab->Get_cnt == NULL)
1626 return (*tab->Get_cnt) (aTHX_ f);
1632 Perl_PerlIO_set_cnt(pTHX_ PerlIO *f, int cnt)
1634 if (PerlIOValid(f)) {
1635 (*PerlIOBase(f)->tab->Set_ptrcnt) (aTHX_ f, NULL, cnt);
1640 Perl_PerlIO_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, int cnt)
1642 if (PerlIOValid(f)) {
1643 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1644 if (tab->Set_ptrcnt == NULL) {
1645 Perl_croak(aTHX_ "PerlIO buffer snooping abuse");
1647 (*PerlIOBase(f)->tab->Set_ptrcnt) (aTHX_ f, ptr, cnt);
1651 /*--------------------------------------------------------------------------------------*/
1653 * utf8 and raw dummy layers
1657 PerlIOUtf8_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
1659 if (*PerlIONext(f)) {
1660 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1661 PerlIO_pop(aTHX_ f);
1662 if (tab->kind & PERLIO_K_UTF8)
1663 PerlIOBase(f)->flags |= PERLIO_F_UTF8;
1665 PerlIOBase(f)->flags &= ~PERLIO_F_UTF8;
1671 PerlIO_funcs PerlIO_utf8 = {
1674 PERLIO_K_DUMMY | PERLIO_F_UTF8,
1692 NULL, /* get_base */
1693 NULL, /* get_bufsiz */
1696 NULL, /* set_ptrcnt */
1699 PerlIO_funcs PerlIO_byte = {
1720 NULL, /* get_base */
1721 NULL, /* get_bufsiz */
1724 NULL, /* set_ptrcnt */
1728 PerlIORaw_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
1729 IV n, const char *mode, int fd, int imode, int perm,
1730 PerlIO *old, int narg, SV **args)
1732 PerlIO_funcs *tab = PerlIO_default_btm();
1733 return (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
1737 PerlIO_funcs PerlIO_raw = {
1758 NULL, /* get_base */
1759 NULL, /* get_bufsiz */
1762 NULL, /* set_ptrcnt */
1764 /*--------------------------------------------------------------------------------------*/
1765 /*--------------------------------------------------------------------------------------*/
1767 * "Methods" of the "base class"
1771 PerlIOBase_fileno(pTHX_ PerlIO *f)
1773 return PerlIOValid(f) ? PerlIO_fileno(PerlIONext(f)) : -1;
1777 PerlIO_modestr(PerlIO *f, char *buf)
1780 IV flags = PerlIOBase(f)->flags;
1781 if (flags & PERLIO_F_APPEND) {
1783 if (flags & PERLIO_F_CANREAD) {
1787 else if (flags & PERLIO_F_CANREAD) {
1789 if (flags & PERLIO_F_CANWRITE)
1792 else if (flags & PERLIO_F_CANWRITE) {
1794 if (flags & PERLIO_F_CANREAD) {
1798 #ifdef PERLIO_USING_CRLF
1799 if (!(flags & PERLIO_F_CRLF))
1807 PerlIOBase_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
1809 PerlIOl *l = PerlIOBase(f);
1811 const char *omode = mode;
1814 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1815 l->flags &= ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE |
1816 PERLIO_F_TRUNCATE | PERLIO_F_APPEND);
1817 if (tab->Set_ptrcnt != NULL)
1818 l->flags |= PERLIO_F_FASTGETS;
1820 if (*mode == '#' || *mode == 'I')
1824 l->flags |= PERLIO_F_CANREAD;
1827 l->flags |= PERLIO_F_APPEND | PERLIO_F_CANWRITE;
1830 l->flags |= PERLIO_F_TRUNCATE | PERLIO_F_CANWRITE;
1833 SETERRNO(EINVAL, LIB$_INVARG);
1839 l->flags |= PERLIO_F_CANREAD | PERLIO_F_CANWRITE;
1842 l->flags &= ~PERLIO_F_CRLF;
1845 l->flags |= PERLIO_F_CRLF;
1848 SETERRNO(EINVAL, LIB$_INVARG);
1855 l->flags |= l->next->flags &
1856 (PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_TRUNCATE |
1861 PerlIO_debug("PerlIOBase_pushed f=%p %s %s fl=%08" UVxf " (%s)\n",
1862 f, PerlIOBase(f)->tab->name, (omode) ? omode : "(Null)",
1863 l->flags, PerlIO_modestr(f, temp));
1869 PerlIOBase_popped(pTHX_ PerlIO *f)
1875 PerlIOBase_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
1878 * Save the position as current head considers it
1880 Off_t old = PerlIO_tell(f);
1882 PerlIO_push(aTHX_ f, &PerlIO_pending, "r", Nullsv);
1883 PerlIOSelf(f, PerlIOBuf)->posn = old;
1884 done = PerlIOBuf_unread(aTHX_ f, vbuf, count);
1889 PerlIOBase_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
1891 STDCHAR *buf = (STDCHAR *) vbuf;
1893 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD))
1896 SSize_t avail = PerlIO_get_cnt(f);
1899 take = ((SSize_t)count < avail) ? count : avail;
1901 STDCHAR *ptr = PerlIO_get_ptr(f);
1902 Copy(ptr, buf, take, STDCHAR);
1903 PerlIO_set_ptrcnt(f, ptr + take, (avail -= take));
1907 if (count > 0 && avail <= 0) {
1908 if (PerlIO_fill(f) != 0)
1912 return (buf - (STDCHAR *) vbuf);
1918 PerlIOBase_noop_ok(pTHX_ PerlIO *f)
1924 PerlIOBase_noop_fail(pTHX_ PerlIO *f)
1930 PerlIOBase_close(pTHX_ PerlIO *f)
1933 PerlIO *n = PerlIONext(f);
1934 if (PerlIO_flush(f) != 0)
1936 if (PerlIOValid(n) && (*PerlIOBase(n)->tab->Close)(aTHX_ n) != 0)
1938 PerlIOBase(f)->flags &=
1939 ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_OPEN);
1944 PerlIOBase_eof(pTHX_ PerlIO *f)
1946 if (PerlIOValid(f)) {
1947 return (PerlIOBase(f)->flags & PERLIO_F_EOF) != 0;
1953 PerlIOBase_error(pTHX_ PerlIO *f)
1955 if (PerlIOValid(f)) {
1956 return (PerlIOBase(f)->flags & PERLIO_F_ERROR) != 0;
1962 PerlIOBase_clearerr(pTHX_ PerlIO *f)
1964 if (PerlIOValid(f)) {
1965 PerlIO *n = PerlIONext(f);
1966 PerlIOBase(f)->flags &= ~(PERLIO_F_ERROR | PERLIO_F_EOF);
1973 PerlIOBase_setlinebuf(pTHX_ PerlIO *f)
1975 if (PerlIOValid(f)) {
1976 PerlIOBase(f)->flags |= PERLIO_F_LINEBUF;
1981 PerlIO_sv_dup(pTHX_ SV *arg, CLONE_PARAMS *param)
1987 return sv_dup(arg, param);
1990 return newSVsv(arg);
1993 return newSVsv(arg);
1998 PerlIOBase_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
2000 PerlIO *nexto = PerlIONext(o);
2001 if (PerlIOValid(nexto)) {
2002 PerlIO_funcs *tab = PerlIOBase(nexto)->tab;
2003 f = (*tab->Dup)(aTHX_ f, nexto, param, flags);
2006 PerlIO_funcs *self = PerlIOBase(o)->tab;
2009 PerlIO_debug("PerlIOBase_dup %s f=%p o=%p param=%p\n",
2010 self->name, (void*)f, (void*)o, (void*)param);
2012 arg = (*self->Getarg)(aTHX_ o,param,flags);
2014 f = PerlIO_push(aTHX_ f, self, PerlIO_modestr(o,buf), arg);
2022 #define PERLIO_MAX_REFCOUNTABLE_FD 2048
2024 perl_mutex PerlIO_mutex;
2026 int PerlIO_fd_refcnt[PERLIO_MAX_REFCOUNTABLE_FD];
2031 /* Place holder for stdstreams call ??? */
2033 MUTEX_INIT(&PerlIO_mutex);
2038 PerlIOUnix_refcnt_inc(int fd)
2040 if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
2042 MUTEX_LOCK(&PerlIO_mutex);
2044 PerlIO_fd_refcnt[fd]++;
2045 PerlIO_debug("fd %d refcnt=%d\n",fd,PerlIO_fd_refcnt[fd]);
2047 MUTEX_UNLOCK(&PerlIO_mutex);
2053 PerlIOUnix_refcnt_dec(int fd)
2056 if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
2058 MUTEX_LOCK(&PerlIO_mutex);
2060 cnt = --PerlIO_fd_refcnt[fd];
2061 PerlIO_debug("fd %d refcnt=%d\n",fd,cnt);
2063 MUTEX_UNLOCK(&PerlIO_mutex);
2070 PerlIO_cleanup(pTHX)
2074 PerlIO_debug("Cleanup %p\n",aTHX);
2076 /* Raise STDIN..STDERR refcount so we don't close them */
2077 for (i=0; i < 3; i++)
2078 PerlIOUnix_refcnt_inc(i);
2079 PerlIO_cleantable(aTHX_ &PL_perlio);
2080 /* Restore STDIN..STDERR refcount */
2081 for (i=0; i < 3; i++)
2082 PerlIOUnix_refcnt_dec(i);
2087 /*--------------------------------------------------------------------------------------*/
2089 * Bottom-most level for UNIX-like case
2093 struct _PerlIO base; /* The generic part */
2094 int fd; /* UNIX like file descriptor */
2095 int oflags; /* open/fcntl flags */
2099 PerlIOUnix_oflags(const char *mode)
2102 if (*mode == 'I' || *mode == '#')
2107 if (*++mode == '+') {
2114 oflags = O_CREAT | O_TRUNC;
2115 if (*++mode == '+') {
2124 oflags = O_CREAT | O_APPEND;
2125 if (*++mode == '+') {
2138 else if (*mode == 't') {
2140 oflags &= ~O_BINARY;
2144 * Always open in binary mode
2147 if (*mode || oflags == -1) {
2148 SETERRNO(EINVAL, LIB$_INVARG);
2155 PerlIOUnix_fileno(pTHX_ PerlIO *f)
2157 return PerlIOSelf(f, PerlIOUnix)->fd;
2161 PerlIOUnix_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
2163 IV code = PerlIOBase_pushed(aTHX_ f, mode, arg);
2164 PerlIOUnix *s = PerlIOSelf(f, PerlIOUnix);
2165 if (*PerlIONext(f)) {
2166 /* We never call down so any pending stuff now */
2167 PerlIO_flush(PerlIONext(f));
2168 s->fd = PerlIO_fileno(PerlIONext(f));
2170 * XXX could (or should) we retrieve the oflags from the open file
2171 * handle rather than believing the "mode" we are passed in? XXX
2172 * Should the value on NULL mode be 0 or -1?
2174 s->oflags = mode ? PerlIOUnix_oflags(mode) : -1;
2176 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
2181 PerlIOUnix_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2182 IV n, const char *mode, int fd, int imode,
2183 int perm, PerlIO *f, int narg, SV **args)
2186 if (PerlIOBase(f)->flags & PERLIO_F_OPEN)
2187 (*PerlIOBase(f)->tab->Close)(aTHX_ f);
2190 char *path = SvPV_nolen(*args);
2194 imode = PerlIOUnix_oflags(mode);
2198 fd = PerlLIO_open3(path, imode, perm);
2206 f = PerlIO_allocate(aTHX);
2207 s = PerlIOSelf(PerlIO_push(aTHX_ f, self, mode, PerlIOArg),
2211 s = PerlIOSelf(f, PerlIOUnix);
2214 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
2215 PerlIOUnix_refcnt_inc(fd);
2221 * FIXME: pop layers ???
2229 PerlIOUnix_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
2231 PerlIOUnix *os = PerlIOSelf(o, PerlIOUnix);
2233 if (flags & PERLIO_DUP_FD) {
2234 fd = PerlLIO_dup(fd);
2236 if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
2237 f = PerlIOBase_dup(aTHX_ f, o, param, flags);
2239 /* If all went well overwrite fd in dup'ed lay with the dup()'ed fd */
2240 PerlIOUnix *s = PerlIOSelf(f, PerlIOUnix);
2242 PerlIOUnix_refcnt_inc(fd);
2251 PerlIOUnix_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
2253 int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2254 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD))
2257 SSize_t len = PerlLIO_read(fd, vbuf, count);
2258 if (len >= 0 || errno != EINTR) {
2260 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2261 else if (len == 0 && count != 0)
2262 PerlIOBase(f)->flags |= PERLIO_F_EOF;
2270 PerlIOUnix_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
2272 int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2274 SSize_t len = PerlLIO_write(fd, vbuf, count);
2275 if (len >= 0 || errno != EINTR) {
2277 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2285 PerlIOUnix_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
2288 PerlLIO_lseek(PerlIOSelf(f, PerlIOUnix)->fd, offset, whence);
2289 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
2290 return (new == (Off_t) - 1) ? -1 : 0;
2294 PerlIOUnix_tell(pTHX_ PerlIO *f)
2296 return PerlLIO_lseek(PerlIOSelf(f, PerlIOUnix)->fd, 0, SEEK_CUR);
2301 PerlIOUnix_close(pTHX_ PerlIO *f)
2303 int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2305 if (PerlIOBase(f)->flags & PERLIO_F_OPEN) {
2306 if (PerlIOUnix_refcnt_dec(fd) > 0) {
2307 PerlIOBase(f)->flags &= ~PERLIO_F_OPEN;
2312 SETERRNO(EBADF,SS$_IVCHAN);
2315 while (PerlLIO_close(fd) != 0) {
2316 if (errno != EINTR) {
2323 PerlIOBase(f)->flags &= ~PERLIO_F_OPEN;
2328 PerlIO_funcs PerlIO_unix = {
2344 PerlIOBase_noop_ok, /* flush */
2345 PerlIOBase_noop_fail, /* fill */
2348 PerlIOBase_clearerr,
2349 PerlIOBase_setlinebuf,
2350 NULL, /* get_base */
2351 NULL, /* get_bufsiz */
2354 NULL, /* set_ptrcnt */
2357 /*--------------------------------------------------------------------------------------*/
2363 struct _PerlIO base;
2364 FILE *stdio; /* The stream */
2368 PerlIOStdio_fileno(pTHX_ PerlIO *f)
2370 return PerlSIO_fileno(PerlIOSelf(f, PerlIOStdio)->stdio);
2374 PerlIOStdio_mode(const char *mode, char *tmode)
2380 #ifdef PERLIO_USING_CRLF
2388 * This isn't used yet ...
2391 PerlIOStdio_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
2393 if (*PerlIONext(f)) {
2394 PerlIOStdio *s = PerlIOSelf(f, PerlIOStdio);
2397 PerlSIO_fdopen(PerlIO_fileno(PerlIONext(f)), mode =
2398 PerlIOStdio_mode(mode, tmode));
2401 /* We never call down so any pending stuff now */
2402 PerlIO_flush(PerlIONext(f));
2407 return PerlIOBase_pushed(aTHX_ f, mode, arg);
2411 PerlIO_importFILE(FILE *stdio, int fl)
2417 PerlIOSelf(PerlIO_push
2418 (aTHX_(f = PerlIO_allocate(aTHX)), &PerlIO_stdio,
2419 "r+", Nullsv), PerlIOStdio);
2426 PerlIOStdio_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2427 IV n, const char *mode, int fd, int imode,
2428 int perm, PerlIO *f, int narg, SV **args)
2432 char *path = SvPV_nolen(*args);
2433 PerlIOStdio *s = PerlIOSelf(f, PerlIOStdio);
2435 PerlIOUnix_refcnt_dec(fileno(s->stdio));
2436 stdio = PerlSIO_freopen(path, (mode = PerlIOStdio_mode(mode, tmode)),
2441 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2446 char *path = SvPV_nolen(*args);
2449 fd = PerlLIO_open3(path, imode, perm);
2452 FILE *stdio = PerlSIO_fopen(path, mode);
2455 PerlIOSelf(PerlIO_push
2456 (aTHX_(f = PerlIO_allocate(aTHX)), self,
2457 (mode = PerlIOStdio_mode(mode, tmode)),
2461 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2476 stdio = PerlSIO_stdin;
2479 stdio = PerlSIO_stdout;
2482 stdio = PerlSIO_stderr;
2487 stdio = PerlSIO_fdopen(fd, mode =
2488 PerlIOStdio_mode(mode, tmode));
2492 PerlIOSelf(PerlIO_push
2493 (aTHX_(f = PerlIO_allocate(aTHX)), self,
2494 mode, PerlIOArg), PerlIOStdio);
2496 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2505 PerlIOStdio_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
2507 /* This assumes no layers underneath - which is what
2508 happens, but is not how I remember it. NI-S 2001/10/16
2510 if ((f = PerlIOBase_dup(aTHX_ f, o, param, flags))) {
2511 FILE *stdio = PerlIOSelf(o, PerlIOStdio)->stdio;
2512 if (flags & PERLIO_DUP_FD) {
2513 int fd = PerlLIO_dup(fileno(stdio));
2516 stdio = fdopen(fd, PerlIO_modestr(o,mode));
2519 /* FIXME: To avoid messy error recovery if dup fails
2520 re-use the existing stdio as though flag was not set
2524 PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
2525 PerlIOUnix_refcnt_inc(fileno(stdio));
2531 PerlIOStdio_close(pTHX_ PerlIO *f)
2533 #ifdef SOCKS5_VERSION_NAME
2535 Sock_size_t optlen = sizeof(int);
2537 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2538 if (PerlIOUnix_refcnt_dec(fileno(stdio)) > 0) {
2539 /* Do not close it but do flush any buffers */
2540 return PerlIO_flush(f);
2543 #ifdef SOCKS5_VERSION_NAME
2545 (PerlIO_fileno(f), SOL_SOCKET, SO_TYPE, (void *) &optval,
2547 0) ? PerlSIO_fclose(stdio) : close(PerlIO_fileno(f))
2549 PerlSIO_fclose(stdio)
2558 PerlIOStdio_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
2560 FILE *s = PerlIOSelf(f, PerlIOStdio)->stdio;
2563 STDCHAR *buf = (STDCHAR *) vbuf;
2565 * Perl is expecting PerlIO_getc() to fill the buffer Linux's
2566 * stdio does not do that for fread()
2568 int ch = PerlSIO_fgetc(s);
2575 got = PerlSIO_fread(vbuf, 1, count, s);
2580 PerlIOStdio_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
2582 FILE *s = PerlIOSelf(f, PerlIOStdio)->stdio;
2583 STDCHAR *buf = ((STDCHAR *) vbuf) + count - 1;
2586 int ch = *buf-- & 0xff;
2587 if (PerlSIO_ungetc(ch, s) != ch)
2596 PerlIOStdio_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
2598 return PerlSIO_fwrite(vbuf, 1, count,
2599 PerlIOSelf(f, PerlIOStdio)->stdio);
2603 PerlIOStdio_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
2605 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2606 return PerlSIO_fseek(stdio, offset, whence);
2610 PerlIOStdio_tell(pTHX_ PerlIO *f)
2612 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2613 return PerlSIO_ftell(stdio);
2617 PerlIOStdio_flush(pTHX_ PerlIO *f)
2619 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2620 if (PerlIOBase(f)->flags & PERLIO_F_CANWRITE) {
2621 return PerlSIO_fflush(stdio);
2626 * FIXME: This discards ungetc() and pre-read stuff which is not
2627 * right if this is just a "sync" from a layer above Suspect right
2628 * design is to do _this_ but not have layer above flush this
2629 * layer read-to-read
2632 * Not writeable - sync by attempting a seek
2635 if (PerlSIO_fseek(stdio, (Off_t) 0, SEEK_CUR) != 0)
2643 PerlIOStdio_fill(pTHX_ PerlIO *f)
2645 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2648 * fflush()ing read-only streams can cause trouble on some stdio-s
2650 if ((PerlIOBase(f)->flags & PERLIO_F_CANWRITE)) {
2651 if (PerlSIO_fflush(stdio) != 0)
2654 c = PerlSIO_fgetc(stdio);
2655 if (c == EOF || PerlSIO_ungetc(c, stdio) != c)
2661 PerlIOStdio_eof(pTHX_ PerlIO *f)
2663 return PerlSIO_feof(PerlIOSelf(f, PerlIOStdio)->stdio);
2667 PerlIOStdio_error(pTHX_ PerlIO *f)
2669 return PerlSIO_ferror(PerlIOSelf(f, PerlIOStdio)->stdio);
2673 PerlIOStdio_clearerr(pTHX_ PerlIO *f)
2675 PerlSIO_clearerr(PerlIOSelf(f, PerlIOStdio)->stdio);
2679 PerlIOStdio_setlinebuf(pTHX_ PerlIO *f)
2681 #ifdef HAS_SETLINEBUF
2682 PerlSIO_setlinebuf(PerlIOSelf(f, PerlIOStdio)->stdio);
2684 PerlSIO_setvbuf(PerlIOSelf(f, PerlIOStdio)->stdio, Nullch, _IOLBF, 0);
2690 PerlIOStdio_get_base(pTHX_ PerlIO *f)
2692 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2693 return (STDCHAR*)PerlSIO_get_base(stdio);
2697 PerlIOStdio_get_bufsiz(pTHX_ PerlIO *f)
2699 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2700 return PerlSIO_get_bufsiz(stdio);
2704 #ifdef USE_STDIO_PTR
2706 PerlIOStdio_get_ptr(pTHX_ PerlIO *f)
2708 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2709 return (STDCHAR*)PerlSIO_get_ptr(stdio);
2713 PerlIOStdio_get_cnt(pTHX_ PerlIO *f)
2715 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2716 return PerlSIO_get_cnt(stdio);
2720 PerlIOStdio_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
2722 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2724 #ifdef STDIO_PTR_LVALUE
2725 PerlSIO_set_ptr(stdio, (void*)ptr); /* LHS STDCHAR* cast non-portable */
2726 #ifdef STDIO_PTR_LVAL_SETS_CNT
2727 if (PerlSIO_get_cnt(stdio) != (cnt)) {
2728 assert(PerlSIO_get_cnt(stdio) == (cnt));
2731 #if (!defined(STDIO_PTR_LVAL_NOCHANGE_CNT))
2733 * Setting ptr _does_ change cnt - we are done
2737 #else /* STDIO_PTR_LVALUE */
2739 #endif /* STDIO_PTR_LVALUE */
2742 * Now (or only) set cnt
2744 #ifdef STDIO_CNT_LVALUE
2745 PerlSIO_set_cnt(stdio, cnt);
2746 #else /* STDIO_CNT_LVALUE */
2747 #if (defined(STDIO_PTR_LVALUE) && defined(STDIO_PTR_LVAL_SETS_CNT))
2748 PerlSIO_set_ptr(stdio,
2749 PerlSIO_get_ptr(stdio) + (PerlSIO_get_cnt(stdio) -
2751 #else /* STDIO_PTR_LVAL_SETS_CNT */
2753 #endif /* STDIO_PTR_LVAL_SETS_CNT */
2754 #endif /* STDIO_CNT_LVALUE */
2759 PerlIO_funcs PerlIO_stdio = {
2761 sizeof(PerlIOStdio),
2779 PerlIOStdio_clearerr,
2780 PerlIOStdio_setlinebuf,
2782 PerlIOStdio_get_base,
2783 PerlIOStdio_get_bufsiz,
2788 #ifdef USE_STDIO_PTR
2789 PerlIOStdio_get_ptr,
2790 PerlIOStdio_get_cnt,
2791 #if (defined(STDIO_PTR_LVALUE) && (defined(STDIO_CNT_LVALUE) || defined(STDIO_PTR_LVAL_SETS_CNT)))
2792 PerlIOStdio_set_ptrcnt
2793 #else /* STDIO_PTR_LVALUE */
2795 #endif /* STDIO_PTR_LVALUE */
2796 #else /* USE_STDIO_PTR */
2800 #endif /* USE_STDIO_PTR */
2804 PerlIO_exportFILE(PerlIO *f, int fl)
2809 stdio = fdopen(PerlIO_fileno(f), "r+");
2812 PerlIOSelf(PerlIO_push(aTHX_ f, &PerlIO_stdio, "r+", Nullsv),
2820 PerlIO_findFILE(PerlIO *f)
2824 if (l->tab == &PerlIO_stdio) {
2825 PerlIOStdio *s = PerlIOSelf(&l, PerlIOStdio);
2828 l = *PerlIONext(&l);
2830 return PerlIO_exportFILE(f, 0);
2834 PerlIO_releaseFILE(PerlIO *p, FILE *f)
2838 /*--------------------------------------------------------------------------------------*/
2840 * perlio buffer layer
2844 PerlIOBuf_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
2846 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
2847 int fd = PerlIO_fileno(f);
2849 if (fd >= 0 && PerlLIO_isatty(fd)) {
2850 PerlIOBase(f)->flags |= PERLIO_F_LINEBUF | PERLIO_F_TTY;
2852 posn = PerlIO_tell(PerlIONext(f));
2853 if (posn != (Off_t) - 1) {
2856 return PerlIOBase_pushed(aTHX_ f, mode, arg);
2860 PerlIOBuf_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2861 IV n, const char *mode, int fd, int imode, int perm,
2862 PerlIO *f, int narg, SV **args)
2864 if (PerlIOValid(f)) {
2865 PerlIO *next = PerlIONext(f);
2866 PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIOBase(next)->tab);
2867 next = (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
2869 if (!next || (*PerlIOBase(f)->tab->Pushed) (aTHX_ f, mode, PerlIOArg) != 0) {
2874 PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIO_default_btm());
2882 f = (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
2885 if (PerlIO_push(aTHX_ f, self, mode, PerlIOArg) == 0) {
2887 * if push fails during open, open fails. close will pop us.
2892 fd = PerlIO_fileno(f);
2893 if (init && fd == 2) {
2895 * Initial stderr is unbuffered
2897 PerlIOBase(f)->flags |= PERLIO_F_UNBUF;
2899 #ifdef PERLIO_USING_CRLF
2900 # ifdef PERLIO_IS_BINMODE_FD
2901 if (PERLIO_IS_BINMODE_FD(fd))
2902 PerlIO_binmode(f, '<'/*not used*/, O_BINARY, Nullch);
2906 * do something about failing setmode()? --jhi
2908 PerlLIO_setmode(fd, O_BINARY);
2917 * This "flush" is akin to sfio's sync in that it handles files in either
2918 * read or write state
2921 PerlIOBuf_flush(pTHX_ PerlIO *f)
2923 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
2925 PerlIO *n = PerlIONext(f);
2926 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF) {
2928 * write() the buffer
2930 STDCHAR *buf = b->buf;
2932 while (p < b->ptr) {
2933 SSize_t count = PerlIO_write(n, p, b->ptr - p);
2937 else if (count < 0 || PerlIO_error(n)) {
2938 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2943 b->posn += (p - buf);
2945 else if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
2946 STDCHAR *buf = PerlIO_get_base(f);
2948 * Note position change
2950 b->posn += (b->ptr - buf);
2951 if (b->ptr < b->end) {
2953 * We did not consume all of it
2955 if (PerlIO_seek(n, b->posn, SEEK_SET) == 0) {
2956 /* Reload n as some layers may pop themselves on seek */
2957 b->posn = PerlIO_tell(n = PerlIONext(f));
2961 b->ptr = b->end = b->buf;
2962 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
2963 /* We check for Valid because of dubious decision to make PerlIO_flush(NULL) flush all */
2964 /* FIXME: Doing downstream flush may be sub-optimal see PerlIOBuf_fill() below */
2965 if (PerlIOValid(n) && PerlIO_flush(n) != 0)
2971 PerlIOBuf_fill(pTHX_ PerlIO *f)
2973 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
2974 PerlIO *n = PerlIONext(f);
2977 * FIXME: doing the down-stream flush maybe sub-optimal if it causes
2978 * pre-read data in stdio buffer to be discarded.
2979 * However, skipping the flush also skips _our_ hosekeeping
2980 * and breaks tell tests. So we do the flush.
2982 if (PerlIO_flush(f) != 0)
2984 if (PerlIOBase(f)->flags & PERLIO_F_TTY)
2985 PerlIOBase_flush_linebuf(aTHX);
2988 PerlIO_get_base(f); /* allocate via vtable */
2990 b->ptr = b->end = b->buf;
2991 if (PerlIO_fast_gets(n)) {
2993 * Layer below is also buffered. We do _NOT_ want to call its
2994 * ->Read() because that will loop till it gets what we asked for
2995 * which may hang on a pipe etc. Instead take anything it has to
2996 * hand, or ask it to fill _once_.
2998 avail = PerlIO_get_cnt(n);
3000 avail = PerlIO_fill(n);
3002 avail = PerlIO_get_cnt(n);
3004 if (!PerlIO_error(n) && PerlIO_eof(n))
3009 STDCHAR *ptr = PerlIO_get_ptr(n);
3010 SSize_t cnt = avail;
3011 if (avail > (SSize_t)b->bufsiz)
3013 Copy(ptr, b->buf, avail, STDCHAR);
3014 PerlIO_set_ptrcnt(n, ptr + avail, cnt - avail);
3018 avail = PerlIO_read(n, b->ptr, b->bufsiz);
3022 PerlIOBase(f)->flags |= PERLIO_F_EOF;
3024 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
3027 b->end = b->buf + avail;
3028 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3033 PerlIOBuf_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
3035 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3036 if (PerlIOValid(f)) {
3039 return PerlIOBase_read(aTHX_ f, vbuf, count);
3045 PerlIOBuf_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3047 const STDCHAR *buf = (const STDCHAR *) vbuf + count;
3048 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3051 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
3056 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
3058 * Buffer is already a read buffer, we can overwrite any chars
3059 * which have been read back to buffer start
3061 avail = (b->ptr - b->buf);
3065 * Buffer is idle, set it up so whole buffer is available for
3069 b->end = b->buf + avail;
3071 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3073 * Buffer extends _back_ from where we are now
3075 b->posn -= b->bufsiz;
3077 if (avail > (SSize_t) count) {
3079 * If we have space for more than count, just move count
3087 * In simple stdio-like ungetc() case chars will be already
3090 if (buf != b->ptr) {
3091 Copy(buf, b->ptr, avail, STDCHAR);
3095 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
3102 PerlIOBuf_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3104 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3105 const STDCHAR *buf = (const STDCHAR *) vbuf;
3109 if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
3112 SSize_t avail = b->bufsiz - (b->ptr - b->buf);
3113 if ((SSize_t) count < avail)
3115 PerlIOBase(f)->flags |= PERLIO_F_WRBUF;
3116 if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF) {
3131 Copy(buf, b->ptr, avail, STDCHAR);
3138 if (b->ptr >= (b->buf + b->bufsiz))
3141 if (PerlIOBase(f)->flags & PERLIO_F_UNBUF)
3147 PerlIOBuf_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
3150 if ((code = PerlIO_flush(f)) == 0) {
3151 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3152 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
3153 code = PerlIO_seek(PerlIONext(f), offset, whence);
3155 b->posn = PerlIO_tell(PerlIONext(f));
3162 PerlIOBuf_tell(pTHX_ PerlIO *f)
3164 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3166 * b->posn is file position where b->buf was read, or will be written
3168 Off_t posn = b->posn;
3171 * If buffer is valid adjust position by amount in buffer
3173 posn += (b->ptr - b->buf);
3179 PerlIOBuf_close(pTHX_ PerlIO *f)
3181 IV code = PerlIOBase_close(aTHX_ f);
3182 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3183 if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
3187 b->ptr = b->end = b->buf;
3188 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
3193 PerlIOBuf_get_ptr(pTHX_ PerlIO *f)
3195 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3202 PerlIOBuf_get_cnt(pTHX_ PerlIO *f)
3204 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3207 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF)
3208 return (b->end - b->ptr);
3213 PerlIOBuf_get_base(pTHX_ PerlIO *f)
3215 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3220 Newz('B',b->buf,b->bufsiz, STDCHAR);
3222 b->buf = (STDCHAR *) & b->oneword;
3223 b->bufsiz = sizeof(b->oneword);
3232 PerlIOBuf_bufsiz(pTHX_ PerlIO *f)
3234 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3237 return (b->end - b->buf);
3241 PerlIOBuf_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
3243 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3247 if (PerlIO_get_cnt(f) != cnt || b->ptr < b->buf) {
3248 assert(PerlIO_get_cnt(f) == cnt);
3249 assert(b->ptr >= b->buf);
3251 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3255 PerlIOBuf_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
3257 return PerlIOBase_dup(aTHX_ f, o, param, flags);
3262 PerlIO_funcs PerlIO_perlio = {
3282 PerlIOBase_clearerr,
3283 PerlIOBase_setlinebuf,
3288 PerlIOBuf_set_ptrcnt,
3291 /*--------------------------------------------------------------------------------------*/
3293 * Temp layer to hold unread chars when cannot do it any other way
3297 PerlIOPending_fill(pTHX_ PerlIO *f)
3300 * Should never happen
3307 PerlIOPending_close(pTHX_ PerlIO *f)
3310 * A tad tricky - flush pops us, then we close new top
3313 return PerlIO_close(f);
3317 PerlIOPending_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
3320 * A tad tricky - flush pops us, then we seek new top
3323 return PerlIO_seek(f, offset, whence);
3328 PerlIOPending_flush(pTHX_ PerlIO *f)
3330 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3331 if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
3335 PerlIO_pop(aTHX_ f);
3340 PerlIOPending_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
3346 PerlIOBuf_set_ptrcnt(aTHX_ f, ptr, cnt);
3351 PerlIOPending_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
3353 IV code = PerlIOBase_pushed(aTHX_ f, mode, arg);
3354 PerlIOl *l = PerlIOBase(f);
3356 * Our PerlIO_fast_gets must match what we are pushed on, or sv_gets()
3357 * etc. get muddled when it changes mid-string when we auto-pop.
3359 l->flags = (l->flags & ~(PERLIO_F_FASTGETS | PERLIO_F_UTF8)) |
3360 (PerlIOBase(PerlIONext(f))->
3361 flags & (PERLIO_F_FASTGETS | PERLIO_F_UTF8));
3366 PerlIOPending_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
3368 SSize_t avail = PerlIO_get_cnt(f);
3370 if ((SSize_t)count < avail)
3373 got = PerlIOBuf_read(aTHX_ f, vbuf, avail);
3374 if (got >= 0 && got < (SSize_t)count) {
3376 PerlIO_read(f, ((STDCHAR *) vbuf) + got, count - got);
3377 if (more >= 0 || got == 0)
3383 PerlIO_funcs PerlIO_pending = {
3387 PerlIOPending_pushed,
3398 PerlIOPending_close,
3399 PerlIOPending_flush,
3403 PerlIOBase_clearerr,
3404 PerlIOBase_setlinebuf,
3409 PerlIOPending_set_ptrcnt,
3414 /*--------------------------------------------------------------------------------------*/
3416 * crlf - translation On read translate CR,LF to "\n" we do this by
3417 * overriding ptr/cnt entries to hand back a line at a time and keeping a
3418 * record of which nl we "lied" about. On write translate "\n" to CR,LF
3422 PerlIOBuf base; /* PerlIOBuf stuff */
3423 STDCHAR *nl; /* Position of crlf we "lied" about in the
3428 PerlIOCrlf_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
3431 PerlIOBase(f)->flags |= PERLIO_F_CRLF;
3432 code = PerlIOBuf_pushed(aTHX_ f, mode, arg);
3434 PerlIO_debug("PerlIOCrlf_pushed f=%p %s %s fl=%08" UVxf "\n",
3435 f, PerlIOBase(f)->tab->name, (mode) ? mode : "(Null)",
3436 PerlIOBase(f)->flags);
3443 PerlIOCrlf_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3445 PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
3450 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF))
3451 return PerlIOBuf_unread(aTHX_ f, vbuf, count);
3453 const STDCHAR *buf = (const STDCHAR *) vbuf + count;
3454 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3456 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
3461 if (!(PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
3462 b->end = b->ptr = b->buf + b->bufsiz;
3463 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3464 b->posn -= b->bufsiz;
3466 while (count > 0 && b->ptr > b->buf) {
3469 if (b->ptr - 2 >= b->buf) {
3492 PerlIOCrlf_get_cnt(pTHX_ PerlIO *f)
3494 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3497 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
3498 PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
3499 if ((PerlIOBase(f)->flags & PERLIO_F_CRLF) && (!c->nl || *c->nl == 0xd)) {
3500 STDCHAR *nl = (c->nl) ? c->nl : b->ptr;
3502 while (nl < b->end && *nl != 0xd)
3504 if (nl < b->end && *nl == 0xd) {
3506 if (nl + 1 < b->end) {
3513 * Not CR,LF but just CR
3521 * Blast - found CR as last char in buffer
3526 * They may not care, defer work as long as
3530 return (nl - b->ptr);
3534 b->ptr++; /* say we have read it as far as
3535 * flush() is concerned */
3536 b->buf++; /* Leave space in front of buffer */
3537 b->bufsiz--; /* Buffer is thus smaller */
3538 code = PerlIO_fill(f); /* Fetch some more */
3539 b->bufsiz++; /* Restore size for next time */
3540 b->buf--; /* Point at space */
3541 b->ptr = nl = b->buf; /* Which is what we hand
3543 b->posn--; /* Buffer starts here */
3544 *nl = 0xd; /* Fill in the CR */
3546 goto test; /* fill() call worked */
3548 * CR at EOF - just fall through
3550 /* Should we clear EOF though ??? */
3555 return (((c->nl) ? (c->nl + 1) : b->end) - b->ptr);
3561 PerlIOCrlf_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
3563 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3564 PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
3570 if (ptr == b->end && *c->nl == 0xd) {
3571 /* Defered CR at end of buffer case - we lied about count */
3583 * Test code - delete when it works ...
3585 IV flags = PerlIOBase(f)->flags;
3586 STDCHAR *chk = (c->nl) ? (c->nl+1) : b->end;
3587 if (ptr+cnt == c->nl && c->nl+1 == b->end && *c->nl == 0xd) {
3588 /* Defered CR at end of buffer case - we lied about count */
3594 Perl_croak(aTHX_ "ptr wrong %p != %p fl=%08" UVxf
3595 " nl=%p e=%p for %d", ptr, chk, flags, c->nl,
3603 * They have taken what we lied about
3611 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3615 PerlIOCrlf_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3617 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF))
3618 return PerlIOBuf_write(aTHX_ f, vbuf, count);
3620 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3621 const STDCHAR *buf = (const STDCHAR *) vbuf;
3622 const STDCHAR *ebuf = buf + count;
3625 if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
3627 while (buf < ebuf) {
3628 STDCHAR *eptr = b->buf + b->bufsiz;
3629 PerlIOBase(f)->flags |= PERLIO_F_WRBUF;
3630 while (buf < ebuf && b->ptr < eptr) {
3632 if ((b->ptr + 2) > eptr) {
3640 *(b->ptr)++ = 0xd; /* CR */
3641 *(b->ptr)++ = 0xa; /* LF */
3643 if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF) {
3653 if (b->ptr >= eptr) {
3659 if (PerlIOBase(f)->flags & PERLIO_F_UNBUF)
3661 return (buf - (STDCHAR *) vbuf);
3666 PerlIOCrlf_flush(pTHX_ PerlIO *f)
3668 PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
3673 return PerlIOBuf_flush(aTHX_ f);
3676 PerlIO_funcs PerlIO_crlf = {
3679 PERLIO_K_BUFFERED | PERLIO_K_CANCRLF,
3681 PerlIOBase_noop_ok, /* popped */
3686 PerlIOBuf_read, /* generic read works with ptr/cnt lies
3688 PerlIOCrlf_unread, /* Put CR,LF in buffer for each '\n' */
3689 PerlIOCrlf_write, /* Put CR,LF in buffer for each '\n' */
3697 PerlIOBase_clearerr,
3698 PerlIOBase_setlinebuf,
3703 PerlIOCrlf_set_ptrcnt,
3707 /*--------------------------------------------------------------------------------------*/
3709 * mmap as "buffer" layer
3713 PerlIOBuf base; /* PerlIOBuf stuff */
3714 Mmap_t mptr; /* Mapped address */
3715 Size_t len; /* mapped length */
3716 STDCHAR *bbuf; /* malloced buffer if map fails */
3719 static size_t page_size = 0;
3722 PerlIOMmap_map(pTHX_ PerlIO *f)
3724 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3725 IV flags = PerlIOBase(f)->flags;
3729 if (flags & PERLIO_F_CANREAD) {
3730 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3731 int fd = PerlIO_fileno(f);
3733 code = Fstat(fd, &st);
3734 if (code == 0 && S_ISREG(st.st_mode)) {
3735 SSize_t len = st.st_size - b->posn;
3739 #if defined(HAS_SYSCONF) && (defined(_SC_PAGESIZE) || defined(_SC_PAGE_SIZE))
3741 SETERRNO(0, SS$_NORMAL);
3742 # ifdef _SC_PAGESIZE
3743 page_size = sysconf(_SC_PAGESIZE);
3745 page_size = sysconf(_SC_PAGE_SIZE);
3747 if ((long) page_size < 0) {
3752 (void) SvUPGRADE(error, SVt_PV);
3753 msg = SvPVx(error, n_a);
3754 Perl_croak(aTHX_ "panic: sysconf: %s",
3759 "panic: sysconf: pagesize unknown");
3763 # ifdef HAS_GETPAGESIZE
3764 page_size = getpagesize();
3766 # if defined(I_SYS_PARAM) && defined(PAGESIZE)
3767 page_size = PAGESIZE; /* compiletime, bad */
3771 if ((IV) page_size <= 0)
3772 Perl_croak(aTHX_ "panic: bad pagesize %" IVdf,
3777 * This is a hack - should never happen - open should
3780 b->posn = PerlIO_tell(PerlIONext(f));
3782 posn = (b->posn / page_size) * page_size;
3783 len = st.st_size - posn;
3784 m->mptr = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, posn);
3785 if (m->mptr && m->mptr != (Mmap_t) - 1) {
3786 #if 0 && defined(HAS_MADVISE) && defined(MADV_SEQUENTIAL)
3787 madvise(m->mptr, len, MADV_SEQUENTIAL);
3789 #if 0 && defined(HAS_MADVISE) && defined(MADV_WILLNEED)
3790 madvise(m->mptr, len, MADV_WILLNEED);
3792 PerlIOBase(f)->flags =
3793 (flags & ~PERLIO_F_EOF) | PERLIO_F_RDBUF;
3794 b->end = ((STDCHAR *) m->mptr) + len;
3795 b->buf = ((STDCHAR *) m->mptr) + (b->posn - posn);
3804 PerlIOBase(f)->flags =
3805 flags | PERLIO_F_EOF | PERLIO_F_RDBUF;
3807 b->ptr = b->end = b->ptr;
3816 PerlIOMmap_unmap(pTHX_ PerlIO *f)
3818 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3819 PerlIOBuf *b = &m->base;
3823 code = munmap(m->mptr, m->len);
3827 if (PerlIO_seek(PerlIONext(f), b->posn, SEEK_SET) != 0)
3830 b->ptr = b->end = b->buf;
3831 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
3837 PerlIOMmap_get_base(pTHX_ PerlIO *f)
3839 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3840 PerlIOBuf *b = &m->base;
3841 if (b->buf && (PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
3843 * Already have a readbuffer in progress
3849 * We have a write buffer or flushed PerlIOBuf read buffer
3851 m->bbuf = b->buf; /* save it in case we need it again */
3852 b->buf = NULL; /* Clear to trigger below */
3855 PerlIOMmap_map(aTHX_ f); /* Try and map it */
3858 * Map did not work - recover PerlIOBuf buffer if we have one
3863 b->ptr = b->end = b->buf;
3866 return PerlIOBuf_get_base(aTHX_ f);
3870 PerlIOMmap_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3872 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3873 PerlIOBuf *b = &m->base;
3874 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
3876 if (b->ptr && (b->ptr - count) >= b->buf
3877 && memEQ(b->ptr - count, vbuf, count)) {
3879 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
3884 * Loose the unwritable mapped buffer
3888 * If flush took the "buffer" see if we have one from before
3890 if (!b->buf && m->bbuf)
3893 PerlIOBuf_get_base(aTHX_ f);
3897 return PerlIOBuf_unread(aTHX_ f, vbuf, count);
3901 PerlIOMmap_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3903 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3904 PerlIOBuf *b = &m->base;
3905 if (!b->buf || !(PerlIOBase(f)->flags & PERLIO_F_WRBUF)) {
3907 * No, or wrong sort of, buffer
3910 if (PerlIOMmap_unmap(aTHX_ f) != 0)
3914 * If unmap took the "buffer" see if we have one from before
3916 if (!b->buf && m->bbuf)
3919 PerlIOBuf_get_base(aTHX_ f);
3923 return PerlIOBuf_write(aTHX_ f, vbuf, count);
3927 PerlIOMmap_flush(pTHX_ PerlIO *f)
3929 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3930 PerlIOBuf *b = &m->base;
3931 IV code = PerlIOBuf_flush(aTHX_ f);
3933 * Now we are "synced" at PerlIOBuf level
3940 if (PerlIOMmap_unmap(aTHX_ f) != 0)
3945 * We seem to have a PerlIOBuf buffer which was not mapped
3946 * remember it in case we need one later
3955 PerlIOMmap_fill(pTHX_ PerlIO *f)
3957 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3958 IV code = PerlIO_flush(f);
3959 if (code == 0 && !b->buf) {
3960 code = PerlIOMmap_map(aTHX_ f);
3962 if (code == 0 && !(PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
3963 code = PerlIOBuf_fill(aTHX_ f);
3969 PerlIOMmap_close(pTHX_ PerlIO *f)
3971 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3972 PerlIOBuf *b = &m->base;
3973 IV code = PerlIO_flush(f);
3977 b->ptr = b->end = b->buf;
3979 if (PerlIOBuf_close(aTHX_ f) != 0)
3985 PerlIOMmap_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
3987 return PerlIOBase_dup(aTHX_ f, o, param, flags);
3991 PerlIO_funcs PerlIO_mmap = {
4011 PerlIOBase_clearerr,
4012 PerlIOBase_setlinebuf,
4013 PerlIOMmap_get_base,
4017 PerlIOBuf_set_ptrcnt,
4020 #endif /* HAS_MMAP */
4023 Perl_PerlIO_stdin(pTHX)
4026 PerlIO_stdstreams(aTHX);
4028 return &PL_perlio[1];
4032 Perl_PerlIO_stdout(pTHX)
4035 PerlIO_stdstreams(aTHX);
4037 return &PL_perlio[2];
4041 Perl_PerlIO_stderr(pTHX)
4044 PerlIO_stdstreams(aTHX);
4046 return &PL_perlio[3];
4049 /*--------------------------------------------------------------------------------------*/
4052 PerlIO_getname(PerlIO *f, char *buf)
4057 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
4059 name = fgetname(stdio, buf);
4061 Perl_croak(aTHX_ "Don't know how to get file name");
4067 /*--------------------------------------------------------------------------------------*/
4069 * Functions which can be called on any kind of PerlIO implemented in
4073 #undef PerlIO_fdopen
4075 PerlIO_fdopen(int fd, const char *mode)
4078 return PerlIO_openn(aTHX_ Nullch, mode, fd, 0, 0, NULL, 0, NULL);
4083 PerlIO_open(const char *path, const char *mode)
4086 SV *name = sv_2mortal(newSVpvn(path, strlen(path)));
4087 return PerlIO_openn(aTHX_ Nullch, mode, -1, 0, 0, NULL, 1, &name);
4090 #undef Perlio_reopen
4092 PerlIO_reopen(const char *path, const char *mode, PerlIO *f)
4095 SV *name = sv_2mortal(newSVpvn(path, strlen(path)));
4096 return PerlIO_openn(aTHX_ Nullch, mode, -1, 0, 0, f, 1, &name);
4101 PerlIO_getc(PerlIO *f)
4105 SSize_t count = PerlIO_read(f, buf, 1);
4107 return (unsigned char) buf[0];
4112 #undef PerlIO_ungetc
4114 PerlIO_ungetc(PerlIO *f, int ch)
4119 if (PerlIO_unread(f, &buf, 1) == 1)
4127 PerlIO_putc(PerlIO *f, int ch)
4131 return PerlIO_write(f, &buf, 1);
4136 PerlIO_puts(PerlIO *f, const char *s)
4139 STRLEN len = strlen(s);
4140 return PerlIO_write(f, s, len);
4143 #undef PerlIO_rewind
4145 PerlIO_rewind(PerlIO *f)
4148 PerlIO_seek(f, (Off_t) 0, SEEK_SET);
4152 #undef PerlIO_vprintf
4154 PerlIO_vprintf(PerlIO *f, const char *fmt, va_list ap)
4157 SV *sv = newSVpvn("", 0);
4163 Perl_va_copy(ap, apc);
4164 sv_vcatpvf(sv, fmt, &apc);
4166 sv_vcatpvf(sv, fmt, &ap);
4169 wrote = PerlIO_write(f, s, len);
4174 #undef PerlIO_printf
4176 PerlIO_printf(PerlIO *f, const char *fmt, ...)
4181 result = PerlIO_vprintf(f, fmt, ap);
4186 #undef PerlIO_stdoutf
4188 PerlIO_stdoutf(const char *fmt, ...)
4194 result = PerlIO_vprintf(PerlIO_stdout(), fmt, ap);
4199 #undef PerlIO_tmpfile
4201 PerlIO_tmpfile(void)
4204 * I have no idea how portable mkstemp() is ...
4206 #if defined(WIN32) || !defined(HAVE_MKSTEMP)
4209 FILE *stdio = PerlSIO_tmpfile();
4212 PerlIOSelf(PerlIO_push
4213 (aTHX_(f = PerlIO_allocate(aTHX)), &PerlIO_stdio,
4214 "w+", Nullsv), PerlIOStdio);
4220 SV *sv = newSVpv("/tmp/PerlIO_XXXXXX", 0);
4221 int fd = mkstemp(SvPVX(sv));
4224 f = PerlIO_fdopen(fd, "w+");
4226 PerlIOBase(f)->flags |= PERLIO_F_TEMP;
4228 PerlLIO_unlink(SvPVX(sv));
4238 #endif /* USE_SFIO */
4239 #endif /* PERLIO_IS_STDIO */
4241 /*======================================================================================*/
4243 * Now some functions in terms of above which may be needed even if we are
4244 * not in true PerlIO mode
4248 #undef PerlIO_setpos
4250 PerlIO_setpos(PerlIO *f, SV *pos)
4255 Off_t *posn = (Off_t *) SvPV(pos, len);
4256 if (f && len == sizeof(Off_t))
4257 return PerlIO_seek(f, *posn, SEEK_SET);
4259 SETERRNO(EINVAL, SS$_IVCHAN);
4263 #undef PerlIO_setpos
4265 PerlIO_setpos(PerlIO *f, SV *pos)
4270 Fpos_t *fpos = (Fpos_t *) SvPV(pos, len);
4271 if (f && len == sizeof(Fpos_t)) {
4272 #if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64)
4273 return fsetpos64(f, fpos);
4275 return fsetpos(f, fpos);
4279 SETERRNO(EINVAL, SS$_IVCHAN);
4285 #undef PerlIO_getpos
4287 PerlIO_getpos(PerlIO *f, SV *pos)
4290 Off_t posn = PerlIO_tell(f);
4291 sv_setpvn(pos, (char *) &posn, sizeof(posn));
4292 return (posn == (Off_t) - 1) ? -1 : 0;
4295 #undef PerlIO_getpos
4297 PerlIO_getpos(PerlIO *f, SV *pos)
4302 #if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64)
4303 code = fgetpos64(f, &fpos);
4305 code = fgetpos(f, &fpos);
4307 sv_setpvn(pos, (char *) &fpos, sizeof(fpos));
4312 #if (defined(PERLIO_IS_STDIO) || !defined(USE_SFIO)) && !defined(HAS_VPRINTF)
4315 vprintf(char *pat, char *args)
4317 _doprnt(pat, args, stdout);
4318 return 0; /* wrong, but perl doesn't use the return
4323 vfprintf(FILE *fd, char *pat, char *args)
4325 _doprnt(pat, args, fd);
4326 return 0; /* wrong, but perl doesn't use the return
4332 #ifndef PerlIO_vsprintf
4334 PerlIO_vsprintf(char *s, int n, const char *fmt, va_list ap)
4336 int val = vsprintf(s, fmt, ap);
4338 if (strlen(s) >= (STRLEN) n) {
4340 (void) PerlIO_puts(Perl_error_log,
4341 "panic: sprintf overflow - memory corrupted!\n");
4349 #ifndef PerlIO_sprintf
4351 PerlIO_sprintf(char *s, int n, const char *fmt, ...)
4356 result = PerlIO_vsprintf(s, n, fmt, ap);