3 * Copyright (c) 1996-2006, Nick Ing-Simmons
4 * Copyright (c) 2006, 2007, 2008, 2009, 2010, 2011 Larry Wall and others
6 * You may distribute under the terms of either the GNU General Public License
7 * or the Artistic License, as specified in the README file.
11 * Hour after hour for nearly three weary days he had jogged up and down,
12 * over passes, and through long dales, and across many streams.
14 * [pp.791-792 of _The Lord of the Rings_, V/iii: "The Muster of Rohan"]
17 /* This file contains the functions needed to implement PerlIO, which
18 * is Perl's private replacement for the C stdio library. This is used
19 * by default unless you compile with -Uuseperlio or run with
20 * PERLIO=:stdio (but don't do this unless you know what you're doing)
24 * If we have ActivePerl-like PERL_IMPLICIT_SYS then we need a dTHX to get
25 * at the dispatch tables, even when we do not need it for other reasons.
26 * Invent a dSYS macro to abstract this out
28 #ifdef PERL_IMPLICIT_SYS
34 #define PERLIO_NOT_STDIO 0
36 * This file provides those parts of PerlIO abstraction
37 * which are not #defined in perlio.h.
38 * Which these are depends on various Configure #ifdef's
42 #define PERL_IN_PERLIO_C
56 #define PerlIO_lockcnt(f) (((PerlIOl*)(void*)(f))->head->flags)
58 /* Call the callback or PerlIOBase, and return failure. */
59 #define Perl_PerlIO_or_Base(f, callback, base, failure, args) \
60 if (PerlIOValid(f)) { \
61 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;\
62 if (tab && tab->callback) \
63 return (*tab->callback) args; \
65 return PerlIOBase_ ## base args; \
68 SETERRNO(EBADF, SS_IVCHAN); \
71 /* Call the callback or fail, and return failure. */
72 #define Perl_PerlIO_or_fail(f, callback, failure, args) \
73 if (PerlIOValid(f)) { \
74 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;\
75 if (tab && tab->callback) \
76 return (*tab->callback) args; \
77 SETERRNO(EINVAL, LIB_INVARG); \
80 SETERRNO(EBADF, SS_IVCHAN); \
83 /* Call the callback or PerlIOBase, and be void. */
84 #define Perl_PerlIO_or_Base_void(f, callback, base, args) \
85 if (PerlIOValid(f)) { \
86 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;\
87 if (tab && tab->callback) \
88 (*tab->callback) args; \
90 PerlIOBase_ ## base args; \
93 SETERRNO(EBADF, SS_IVCHAN)
95 /* Call the callback or fail, and be void. */
96 #define Perl_PerlIO_or_fail_void(f, callback, args) \
97 if (PerlIOValid(f)) { \
98 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;\
99 if (tab && tab->callback) \
100 (*tab->callback) args; \
102 SETERRNO(EINVAL, LIB_INVARG); \
105 SETERRNO(EBADF, SS_IVCHAN)
107 #if defined(__osf__) && _XOPEN_SOURCE < 500
108 extern int fseeko(FILE *, off_t, int);
109 extern off_t ftello(FILE *);
112 #define NATIVE_0xd CR_NATIVE
113 #define NATIVE_0xa LF_NATIVE
115 EXTERN_C int perlsio_binmode(FILE *fp, int iotype, int mode);
118 perlsio_binmode(FILE *fp, int iotype, int mode)
121 * This used to be contents of do_binmode in doio.c
125 PERL_UNUSED_ARG(iotype);
126 if (PerlLIO_setmode(fileno(fp), mode) != -1) {
132 # if defined(USEMYBINMODE)
134 # if defined(__CYGWIN__)
135 PERL_UNUSED_ARG(iotype);
137 if (my_binmode(fp, iotype, mode) != FALSE)
143 PERL_UNUSED_ARG(iotype);
144 PERL_UNUSED_ARG(mode);
151 # define O_ACCMODE 3 /* Assume traditional implementation */
155 PerlIO_intmode2str(int rawmode, char *mode, int *writing)
157 const int result = rawmode & O_ACCMODE;
162 ptype = IoTYPE_RDONLY;
165 ptype = IoTYPE_WRONLY;
173 *writing = (result != O_RDONLY);
175 if (result == O_RDONLY) {
179 else if (rawmode & O_APPEND) {
181 if (result != O_WRONLY)
186 if (result == O_WRONLY)
194 /* Unless O_BINARY is different from zero, bit-and:ing
195 * with it won't do much good. */
196 if (rawmode & O_BINARY)
203 #ifndef PERLIO_LAYERS
205 PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names)
207 if (!names || !*names
208 || strEQ(names, ":crlf")
209 || strEQ(names, ":raw")
210 || strEQ(names, ":bytes")
214 Perl_croak(aTHX_ "Cannot apply \"%s\" in non-PerlIO perl", names);
222 PerlIO_destruct(pTHX)
227 PerlIO_binmode(pTHX_ PerlIO *fp, int iotype, int mode, const char *names)
229 return perlsio_binmode(fp, iotype, mode);
233 PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
235 #if defined(PERL_IMPLICIT_SYS)
236 return PerlSIO_fdupopen(f);
239 return win32_fdupopen(f);
242 const int fd = PerlLIO_dup_cloexec(PerlIO_fileno(f));
245 const int omode = fcntl(fd, F_GETFL);
246 PerlIO_intmode2str(omode,mode,NULL);
247 /* the r+ is a hack */
248 return PerlIO_fdopen(fd, mode);
253 SETERRNO(EBADF, SS_IVCHAN);
262 * De-mux PerlIO_openn() into fdopen, freopen and fopen type entries
266 PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd,
267 int imode, int perm, PerlIO *old, int narg, SV **args)
271 Perl_croak(aTHX_ "More than one argument to open");
273 if (*args == &PL_sv_undef)
274 return PerlIO_tmpfile();
277 const char *name = SvPV_const(*args, len);
278 if (!IS_SAFE_PATHNAME(name, len, "open"))
281 if (*mode == IoTYPE_NUMERIC) {
282 fd = PerlLIO_open3_cloexec(name, imode, perm);
284 return PerlIO_fdopen(fd, mode + 1);
287 return PerlIO_reopen(name, mode, old);
290 return PerlIO_open(name, mode);
295 return PerlIO_fdopen(fd, mode);
300 XS(XS_PerlIO__Layer__find); /* prototype to pass -Wmissing-prototypes */
301 XS(XS_PerlIO__Layer__find)
305 Perl_croak(aTHX_ "Usage class->find(name[,load])");
307 const char * const name = SvPV_nolen_const(ST(1));
308 ST(0) = (strEQ(name, "crlf")
309 || strEQ(name, "raw")) ? &PL_sv_yes : &PL_sv_undef;
316 Perl_boot_core_PerlIO(pTHX)
318 newXS("PerlIO::Layer::find", XS_PerlIO__Layer__find, __FILE__);
324 /*======================================================================================*/
326 * Implement all the PerlIO interface ourselves.
332 PerlIO_debug(const char *fmt, ...)
342 if (!PL_perlio_debug_fd) {
344 PerlProc_getuid() == PerlProc_geteuid() &&
345 PerlProc_getgid() == PerlProc_getegid()) {
346 const char * const s = PerlEnv_getenv("PERLIO_DEBUG");
348 PL_perlio_debug_fd = PerlLIO_open3_cloexec(s,
349 O_WRONLY | O_CREAT | O_APPEND, 0666);
351 PL_perlio_debug_fd = PerlLIO_dup_cloexec(2); /* stderr */
353 /* tainting or set*id, so ignore the environment and send the
354 debug output to stderr, like other -D switches. */
355 PL_perlio_debug_fd = PerlLIO_dup_cloexec(2); /* stderr */
358 if (PL_perlio_debug_fd > 0) {
360 const char * const s = CopFILE(PL_curcop);
361 /* Use fixed buffer as sv_catpvf etc. needs SVs */
363 const STRLEN len1 = my_snprintf(buffer, sizeof(buffer), "%.40s:%" LINE_Tf " ", s ? s : "(none)", CopLINE(PL_curcop));
365 # ifdef HAS_VSNPRINTF
366 /* my_vsnprintf() isn't available with quadmath, but the native vsnprintf()
367 should be, otherwise the system isn't likely to support quadmath.
368 Nothing should be calling PerlIO_debug() with floating point anyway.
370 DECLARATION_FOR_LC_NUMERIC_MANIPULATION;
371 STORE_LC_NUMERIC_SET_TO_NEEDED();
372 const STRLEN len2 = vsnprintf(buffer + len1, sizeof(buffer) - len1, fmt, ap);
373 RESTORE_LC_NUMERIC();
375 STATIC_ASSERT_STMT(0);
378 const STRLEN len2 = my_vsnprintf(buffer + len1, sizeof(buffer) - len1, fmt, ap);
380 PERL_UNUSED_RESULT(PerlLIO_write(PL_perlio_debug_fd, buffer, len1 + len2));
382 const char *s = CopFILE(PL_curcop);
384 SV * const sv = Perl_newSVpvf(aTHX_ "%s:%" LINE_Tf " ",
385 s ? s : "(none)", CopLINE(PL_curcop));
386 Perl_sv_vcatpvf(aTHX_ sv, fmt, &ap);
388 s = SvPV_const(sv, len);
389 PERL_UNUSED_RESULT(PerlLIO_write(PL_perlio_debug_fd, s, len));
396 /*--------------------------------------------------------------------------------------*/
399 * Inner level routines
402 /* check that the head field of each layer points back to the head */
405 # define VERIFY_HEAD(f) PerlIO_verify_head(aTHX_ f)
407 PerlIO_verify_head(pTHX_ PerlIO *f)
411 # ifndef PERL_IMPLICIT_SYS
416 p = head = PerlIOBase(f)->head;
419 assert(p->head == head);
427 # define VERIFY_HEAD(f)
432 * Table of pointers to the PerlIO structs (malloc'ed)
434 #define PERLIO_TABLE_SIZE 64
437 PerlIO_init_table(pTHX)
441 Newxz(PL_perlio, PERLIO_TABLE_SIZE, PerlIOl);
447 PerlIO_allocate(pTHX)
450 * Find a free slot in the table, allocating new tables as necessary
455 while ((f = *last)) {
458 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
459 if (!((++f)->next)) {
464 Newxz(f,PERLIO_TABLE_SIZE,PerlIOl);
471 f->flags = 0; /* lockcnt */
477 #undef PerlIO_fdupopen
479 PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
481 if (PerlIOValid(f)) {
482 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
483 DEBUG_i( PerlIO_debug("fdupopen f=%p param=%p\n",(void*)f,(void*)param) );
485 return (*tab->Dup)(aTHX_ PerlIO_allocate(aTHX), f, param, flags);
487 return PerlIOBase_dup(aTHX_ PerlIO_allocate(aTHX), f, param, flags);
491 SETERRNO(EBADF, SS_IVCHAN);
497 PerlIO_cleantable(pTHX_ PerlIOl **tablep)
499 PerlIOl * const table = *tablep;
502 PerlIO_cleantable(aTHX_ &table[0].next);
503 for (i = PERLIO_TABLE_SIZE - 1; i > 0; i--) {
504 PerlIOl * const f = table + i;
506 PerlIO_close(&(f->next));
516 PerlIO_list_alloc(pTHX)
520 Newxz(list, 1, PerlIO_list_t);
526 PerlIO_list_free(pTHX_ PerlIO_list_t *list)
529 if (--list->refcnt == 0) {
532 for (i = 0; i < list->cur; i++)
533 SvREFCNT_dec(list->array[i].arg);
534 Safefree(list->array);
542 PerlIO_list_push(pTHX_ PerlIO_list_t *list, PerlIO_funcs *funcs, SV *arg)
547 if (list->cur >= list->len) {
548 const IV new_len = list->len + 8;
550 Renew(list->array, new_len, PerlIO_pair_t);
552 Newx(list->array, new_len, PerlIO_pair_t);
555 p = &(list->array[list->cur++]);
557 if ((p->arg = arg)) {
558 SvREFCNT_inc_simple_void_NN(arg);
563 PerlIO_clone_list(pTHX_ PerlIO_list_t *proto, CLONE_PARAMS *param)
565 PerlIO_list_t *list = NULL;
568 list = PerlIO_list_alloc(aTHX);
569 for (i=0; i < proto->cur; i++) {
570 SV *arg = proto->array[i].arg;
573 arg = sv_dup(arg, param);
575 PERL_UNUSED_ARG(param);
577 PerlIO_list_push(aTHX_ list, proto->array[i].funcs, arg);
584 PerlIO_clone(pTHX_ PerlInterpreter *proto, CLONE_PARAMS *param)
587 PerlIOl **table = &proto->Iperlio;
590 PL_known_layers = PerlIO_clone_list(aTHX_ proto->Iknown_layers, param);
591 PL_def_layerlist = PerlIO_clone_list(aTHX_ proto->Idef_layerlist, param);
592 PerlIO_init_table(aTHX);
593 DEBUG_i( PerlIO_debug("Clone %p from %p\n",(void*)aTHX,(void*)proto) );
594 while ((f = *table)) {
598 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
600 (void) fp_dup(&(f->next), 0, param);
607 PERL_UNUSED_ARG(proto);
608 PERL_UNUSED_ARG(param);
613 PerlIO_destruct(pTHX)
615 PerlIOl **table = &PL_perlio;
618 DEBUG_i( PerlIO_debug("Destruct %p\n",(void*)aTHX) );
620 while ((f = *table)) {
624 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
625 PerlIO *x = &(f->next);
628 if (l->tab && l->tab->kind & PERLIO_K_DESTRUCT) {
629 DEBUG_i( PerlIO_debug("Destruct popping %s\n", l->tab->name) );
643 PerlIO_pop(pTHX_ PerlIO *f)
645 const PerlIOl *l = *f;
648 DEBUG_i( PerlIO_debug("PerlIO_pop f=%p %s\n", (void*)f,
649 l->tab ? l->tab->name : "(Null)") );
650 if (l->tab && l->tab->Popped) {
652 * If popped returns non-zero do not free its layer structure
653 * it has either done so itself, or it is shared and still in
656 if ((*l->tab->Popped) (aTHX_ f) != 0)
659 if (PerlIO_lockcnt(f)) {
660 /* we're in use; defer freeing the structure */
661 PerlIOBase(f)->flags = PERLIO_F_CLEARED;
662 PerlIOBase(f)->tab = NULL;
672 /* Return as an array the stack of layers on a filehandle. Note that
673 * the stack is returned top-first in the array, and there are three
674 * times as many array elements as there are layers in the stack: the
675 * first element of a layer triplet is the name, the second one is the
676 * arguments, and the third one is the flags. */
679 PerlIO_get_layers(pTHX_ PerlIO *f)
681 AV * const av = newAV();
683 if (PerlIOValid(f)) {
684 PerlIOl *l = PerlIOBase(f);
687 /* There is some collusion in the implementation of
688 XS_PerlIO_get_layers - it knows that name and flags are
689 generated as fresh SVs here, and takes advantage of that to
690 "copy" them by taking a reference. If it changes here, it needs
691 to change there too. */
692 SV * const name = l->tab && l->tab->name ?
693 newSVpv(l->tab->name, 0) : &PL_sv_undef;
694 SV * const arg = l->tab && l->tab->Getarg ?
695 (*l->tab->Getarg)(aTHX_ &l, 0, 0) : &PL_sv_undef;
696 av_push_simple(av, name);
697 av_push_simple(av, arg);
698 av_push_simple(av, newSViv((IV)l->flags));
706 /*--------------------------------------------------------------------------------------*/
708 * XS Interface for perl code
712 PerlIO_find_layer(pTHX_ const char *name, STRLEN len, int load)
716 if ((SSize_t) len <= 0)
718 for (i = 0; i < PL_known_layers->cur; i++) {
719 PerlIO_funcs * const f = PL_known_layers->array[i].funcs;
720 const STRLEN this_len = strlen(f->name);
721 if (this_len == len && memEQ(f->name, name, len)) {
722 DEBUG_i( PerlIO_debug("%.*s => %p\n", (int) len, name, (void*)f) );
726 if (load && PL_subname && PL_def_layerlist
727 && PL_def_layerlist->cur >= 2) {
728 if (PL_in_load_module) {
729 Perl_croak(aTHX_ "Recursive call to Perl_load_module in PerlIO_find_layer");
732 SV * const pkgsv = newSVpvs("PerlIO");
733 SV * const layer = newSVpvn(name, len);
734 CV * const cv = get_cvs("PerlIO::Layer::NoWarnings", 0);
736 SAVEBOOL(PL_in_load_module);
738 SAVEGENERICSV(PL_warnhook);
739 PL_warnhook = MUTABLE_SV((SvREFCNT_inc_simple_NN(cv)));
741 PL_in_load_module = TRUE;
743 * The two SVs are magically freed by load_module
745 Perl_load_module(aTHX_ 0, pkgsv, NULL, layer, NULL);
747 return PerlIO_find_layer(aTHX_ name, len, 0);
750 DEBUG_i( PerlIO_debug("Cannot find %.*s\n", (int) len, name) );
754 #ifdef USE_ATTRIBUTES_FOR_PERLIO
757 perlio_mg_set(pTHX_ SV *sv, MAGIC *mg)
760 IO * const io = GvIOn(MUTABLE_GV(SvRV(sv)));
761 PerlIO * const ifp = IoIFP(io);
762 PerlIO * const ofp = IoOFP(io);
763 Perl_warn(aTHX_ "set %" SVf " %p %p %p",
764 SVfARG(sv), (void*)io, (void*)ifp, (void*)ofp);
770 perlio_mg_get(pTHX_ SV *sv, MAGIC *mg)
773 IO * const io = GvIOn(MUTABLE_GV(SvRV(sv)));
774 PerlIO * const ifp = IoIFP(io);
775 PerlIO * const ofp = IoOFP(io);
776 Perl_warn(aTHX_ "get %" SVf " %p %p %p",
777 SVfARG(sv), (void*)io, (void*)ifp, (void*)ofp);
783 perlio_mg_clear(pTHX_ SV *sv, MAGIC *mg)
785 Perl_warn(aTHX_ "clear %" SVf, SVfARG(sv));
790 perlio_mg_free(pTHX_ SV *sv, MAGIC *mg)
792 Perl_warn(aTHX_ "free %" SVf, SVfARG(sv));
796 MGVTBL perlio_vtab = {
804 XS(XS_io_MODIFY_SCALAR_ATTRIBUTES); /* prototype to pass -Wmissing-prototypes */
805 XS(XS_io_MODIFY_SCALAR_ATTRIBUTES)
808 SV * const sv = SvRV(ST(1));
809 AV * const av = newAV();
813 sv_magic(sv, MUTABLE_SV(av), PERL_MAGIC_ext, NULL, 0);
815 mg = mg_find(sv, PERL_MAGIC_ext);
816 mg->mg_virtual = &perlio_vtab;
818 Perl_warn(aTHX_ "attrib %" SVf, SVfARG(sv));
819 for (i = 2; i < items; i++) {
821 const char * const name = SvPV_const(ST(i), len);
822 SV * const layer = PerlIO_find_layer(aTHX_ name, len, 1);
824 av_push_simple(av, SvREFCNT_inc_simple_NN(layer));
835 #endif /* USE_ATTRIBUTES_FOR_PERLIO */
838 PerlIO_tab_sv(pTHX_ PerlIO_funcs *tab)
840 HV * const stash = gv_stashpvs("PerlIO::Layer", GV_ADD);
841 SV * const sv = sv_bless(newRV_noinc(newSViv(PTR2IV(tab))), stash);
845 XS(XS_PerlIO__Layer__NoWarnings); /* prototype to pass -Wmissing-prototypes */
846 XS(XS_PerlIO__Layer__NoWarnings)
848 /* This is used as a %SIG{__WARN__} handler to suppress warnings
849 during loading of layers.
852 PERL_UNUSED_VAR(items);
855 PerlIO_debug("warning:%s\n",SvPV_nolen_const(ST(0))) );
859 XS(XS_PerlIO__Layer__find); /* prototype to pass -Wmissing-prototypes */
860 XS(XS_PerlIO__Layer__find)
864 Perl_croak(aTHX_ "Usage class->find(name[,load])");
867 const char * const name = SvPV_const(ST(1), len);
868 const bool load = (items > 2) ? SvTRUE_NN(ST(2)) : 0;
869 PerlIO_funcs * const layer = PerlIO_find_layer(aTHX_ name, len, load);
871 (layer) ? sv_2mortal(PerlIO_tab_sv(aTHX_ layer)) :
878 PerlIO_define_layer(pTHX_ PerlIO_funcs *tab)
880 if (!PL_known_layers)
881 PL_known_layers = PerlIO_list_alloc(aTHX);
882 PerlIO_list_push(aTHX_ PL_known_layers, tab, NULL);
883 DEBUG_i( PerlIO_debug("define %s %p\n", tab->name, (void*)tab) );
887 PerlIO_parse_layers(pTHX_ PerlIO_list_t *av, const char *names)
890 const char *s = names;
892 while (isSPACE(*s) || *s == ':')
897 const char *as = NULL;
899 if (!isIDFIRST(*s)) {
901 * Message is consistent with how attribute lists are
902 * passed. Even though this means "foo : : bar" is
903 * seen as an invalid separator character.
905 const char q = ((*s == '\'') ? '"' : '\'');
906 Perl_ck_warner(aTHX_ packWARN(WARN_LAYER),
907 "Invalid separator character %c%c%c in PerlIO layer specification %s",
909 SETERRNO(EINVAL, LIB_INVARG);
914 } while (isWORDCHAR(*e));
930 * It's a nul terminated string, not allowed
931 * to \ the terminating null. Anything other
932 * character is passed over.
940 Perl_ck_warner(aTHX_ packWARN(WARN_LAYER),
941 "Argument list not closed for PerlIO layer \"%.*s\"",
953 PerlIO_funcs * const layer =
954 PerlIO_find_layer(aTHX_ s, llen, 1);
958 arg = newSVpvn(as, alen);
959 PerlIO_list_push(aTHX_ av, layer,
960 (arg) ? arg : &PL_sv_undef);
964 Perl_ck_warner(aTHX_ packWARN(WARN_LAYER), "Unknown PerlIO layer \"%.*s\"",
977 PerlIO_default_buffer(pTHX_ PerlIO_list_t *av)
979 PERLIO_FUNCS_DECL(*tab) = &PerlIO_perlio;
980 #ifdef PERLIO_USING_CRLF
983 if (PerlIO_stdio.Set_ptrcnt)
986 DEBUG_i( PerlIO_debug("Pushing %s\n", tab->name) );
987 PerlIO_list_push(aTHX_ av, (PerlIO_funcs *)tab, &PL_sv_undef);
991 PerlIO_arg_fetch(PerlIO_list_t *av, IV n)
993 return av->array[n].arg;
997 PerlIO_layer_fetch(pTHX_ PerlIO_list_t *av, IV n, PerlIO_funcs *def)
999 if (n >= 0 && n < av->cur) {
1000 DEBUG_i( PerlIO_debug("Layer %" IVdf " is %s\n", n,
1001 av->array[n].funcs->name) );
1002 return av->array[n].funcs;
1005 Perl_croak(aTHX_ "panic: PerlIO layer array corrupt");
1010 PerlIOPop_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
1012 PERL_UNUSED_ARG(mode);
1013 PERL_UNUSED_ARG(arg);
1014 PERL_UNUSED_ARG(tab);
1015 if (PerlIOValid(f)) {
1017 PerlIO_pop(aTHX_ f);
1023 PERLIO_FUNCS_DECL(PerlIO_remove) = {
1024 sizeof(PerlIO_funcs),
1027 PERLIO_K_DUMMY | PERLIO_K_UTF8,
1047 NULL, /* get_base */
1048 NULL, /* get_bufsiz */
1051 NULL, /* set_ptrcnt */
1054 static const char code_point_warning[] =
1055 "Strings with code points over 0xFF may not be mapped into in-memory file handles\n";
1058 struct _PerlIO base; /* Base "class" info */
1064 PerlIOScalar_eof(pTHX_ PerlIO * f)
1066 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
1067 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
1069 (void)SvPV(s->var, len);
1070 return len - (STRLEN)(s->posn) <= 0;
1076 PerlIOScalar_pushed(pTHX_ PerlIO * f, const char *mode, SV * arg,
1080 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
1081 /* If called (normally) via open() then arg is ref to scalar we are
1082 * using, otherwise arg (from binmode presumably) is either NULL
1083 * or the _name_ of the scalar
1085 if (arg && SvOK(arg)) {
1087 if (SvREADONLY(SvRV(arg)) && !SvIsCOW(SvRV(arg))
1088 && mode && *mode != 'r') {
1089 if (ckWARN(WARN_LAYER))
1090 Perl_warner(aTHX_ packWARN(WARN_LAYER), "%s", PL_no_modify);
1091 SETERRNO(EACCES, RMS_PRV);
1094 s->var = SvREFCNT_inc(SvRV(arg));
1096 if (!SvPOK(s->var) && SvOK(s->var))
1097 (void)SvPV_nomg_const_nolen(s->var);
1102 (SvPV_nolen(arg), GV_ADD | GV_ADDMULTI));
1104 SvUPGRADE(s->var, SVt_PV);
1107 s->var = newSVpvs("");
1110 code = PerlIOBase_pushed(aTHX_ f, mode, NULL, tab);
1111 if (!SvOK(s->var) || (PerlIOBase(f)->flags) & PERLIO_F_TRUNCATE)
1113 sv_force_normal(s->var);
1114 SvCUR_set(s->var, 0);
1115 if (SvPOK(s->var)) *SvPVX(s->var) = 0;
1117 if (SvUTF8(s->var) && !sv_utf8_downgrade(s->var, TRUE)) {
1118 if (ckWARN(WARN_UTF8))
1119 Perl_warner(aTHX_ packWARN(WARN_UTF8), code_point_warning);
1120 SETERRNO(EINVAL, SS_IVCHAN);
1121 SvREFCNT_dec(s->var);
1125 if ((PerlIOBase(f)->flags) & PERLIO_F_APPEND)
1126 s->posn = SvOK(s->var) ? sv_len(s->var) : 0;
1134 PerlIOScalar_popped(pTHX_ PerlIO * f)
1136 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
1138 SvREFCNT_dec(s->var);
1145 PerlIOScalar_close(pTHX_ PerlIO * f)
1147 IV code = PerlIOBase_close(aTHX_ f);
1148 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
1153 PerlIOScalar_fileno(pTHX_ PerlIO * f)
1160 PerlIOScalar_seek(pTHX_ PerlIO * f, Off_t offset, int whence)
1162 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
1170 new_posn = offset + s->posn;
1175 (void)SvPV(s->var, oldcur);
1176 new_posn = offset + oldcur;
1180 SETERRNO(EINVAL, SS_IVCHAN);
1184 if (ckWARN(WARN_LAYER))
1185 Perl_warner(aTHX_ packWARN(WARN_LAYER), "Offset outside string");
1186 SETERRNO(EINVAL, SS_IVCHAN);
1194 PerlIOScalar_tell(pTHX_ PerlIO * f)
1196 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
1202 PerlIOScalar_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
1206 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD)) {
1207 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
1208 SETERRNO(EBADF, SS_IVCHAN);
1209 Perl_PerlIO_save_errno(aTHX_ f);
1213 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
1220 if (sv_utf8_downgrade(sv, TRUE)) {
1221 p = SvPV_nomg(sv, len);
1224 if (ckWARN(WARN_UTF8))
1225 Perl_warner(aTHX_ packWARN(WARN_UTF8), code_point_warning);
1226 SETERRNO(EINVAL, SS_IVCHAN);
1230 /* I assume that Off_t is at least as large as len (which
1231 * seems safe) and that the size of the buffer in our SV is
1232 * always less than half the size of the address space
1234 * Which turns out not to be the case on 64-bit Windows, since
1235 * a build with USE_LARGE_FILES=undef defines Off_t as long,
1236 * which is 32-bits on 64-bit Windows. This doesn't appear to
1237 * be the case on other 64-bit platforms.
1239 #if Off_t_size >= Size_t_size
1240 assert(len < ((~(STRLEN)0) >> 1));
1241 if ((Off_t)len <= s->posn)
1244 if (len <= (STRLEN)s->posn)
1247 got = len - (STRLEN)(s->posn);
1248 if ((STRLEN)got > (STRLEN)count)
1249 got = (STRLEN)count;
1250 Copy(p + (STRLEN)(s->posn), vbuf, got, STDCHAR);
1251 s->posn += (Off_t)got;
1252 return (SSize_t)got;
1257 PerlIOScalar_write(pTHX_ PerlIO * f, const void *vbuf, Size_t count)
1259 if (PerlIOBase(f)->flags & PERLIO_F_CANWRITE) {
1261 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
1265 if (!SvROK(sv)) sv_force_normal(sv);
1266 if (SvOK(sv)) SvPV_force_nomg_nolen(sv);
1267 if (SvUTF8(sv) && !sv_utf8_downgrade(sv, TRUE)) {
1268 if (ckWARN(WARN_UTF8))
1269 Perl_warner(aTHX_ packWARN(WARN_UTF8), code_point_warning);
1270 SETERRNO(EINVAL, SS_IVCHAN);
1273 if ((PerlIOBase(f)->flags) & PERLIO_F_APPEND) {
1274 dst = SvGROW(sv, SvCUR(sv) + count + 1);
1276 s->posn = offset + count;
1279 STRLEN const cur = SvCUR(sv);
1281 /* ensure we don't try to create ridiculously large
1282 * SVs on small platforms
1284 #if Size_t_size < Off_t_size
1285 if (s->posn > SSize_t_MAX) {
1287 SETERRNO(EFBIG, SS_BUFFEROVF);
1289 SETERRNO(ENOSPC, SS_BUFFEROVF);
1295 if ((STRLEN)s->posn > cur) {
1296 dst = SvGROW(sv, (STRLEN)s->posn + count + 1);
1297 Zero(SvPVX(sv) + cur, (STRLEN)s->posn - cur, char);
1299 else if ((s->posn + count) >= cur)
1300 dst = SvGROW(sv, (STRLEN)s->posn + count + 1);
1306 Move(vbuf, dst + offset, count, char);
1307 if ((STRLEN) s->posn > SvCUR(sv)) {
1308 SvCUR_set(sv, (STRLEN)s->posn);
1309 dst[(STRLEN) s->posn] = 0;
1320 PerlIOScalar_fill(pTHX_ PerlIO * f)
1327 PerlIOScalar_flush(pTHX_ PerlIO * f)
1334 PerlIOScalar_get_base(pTHX_ PerlIO * f)
1336 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
1337 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
1339 return (STDCHAR *) SvPV_nolen(s->var);
1341 return (STDCHAR *) NULL;
1345 PerlIOScalar_get_ptr(pTHX_ PerlIO * f)
1347 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
1348 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
1349 return PerlIOScalar_get_base(aTHX_ f) + s->posn;
1351 return (STDCHAR *) NULL;
1355 PerlIOScalar_get_cnt(pTHX_ PerlIO * f)
1357 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
1358 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
1360 (void)SvPV(s->var,len);
1361 if ((Off_t)len > s->posn)
1362 return len - (STRLEN)s->posn;
1370 PerlIOScalar_bufsiz(pTHX_ PerlIO * f)
1372 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
1373 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
1375 return SvCUR(s->var);
1381 PerlIOScalar_set_ptrcnt(pTHX_ PerlIO * f, STDCHAR * ptr, SSize_t cnt)
1383 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
1385 PERL_UNUSED_ARG(ptr);
1386 (void)SvPV(s->var,len);
1387 s->posn = len - cnt;
1391 PerlIOScalar_open(pTHX_ PerlIO_funcs * self, PerlIO_list_t * layers, IV n,
1392 const char *mode, int fd, int imode, int perm,
1393 PerlIO * f, int narg, SV ** args)
1395 SV *arg = (narg > 0) ? *args : PerlIOArg;
1396 PERL_UNUSED_ARG(fd);
1397 PERL_UNUSED_ARG(imode);
1398 PERL_UNUSED_ARG(perm);
1399 if (SvROK(arg) || SvPOK(arg)) {
1401 f = PerlIO_allocate(aTHX);
1403 if ( (f = PerlIO_push(aTHX_ f, self, mode, arg)) ) {
1404 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
1412 PerlIOScalar_arg(pTHX_ PerlIO * f, CLONE_PARAMS * param, int flags)
1414 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
1416 if (flags & PERLIO_DUP_CLONE)
1417 var = PerlIO_sv_dup(aTHX_ var, param);
1418 else if (flags & PERLIO_DUP_FD) {
1419 /* Equivalent (guesses NI-S) of dup() is to create a new scalar */
1423 var = SvREFCNT_inc(var);
1425 return newRV_noinc(var);
1429 PerlIOScalar_dup(pTHX_ PerlIO * f, PerlIO * o, CLONE_PARAMS * param,
1432 /* Duplication causes the scalar layer to be pushed on to clone, caus-
1433 ing the cloned scalar to be set to the empty string by
1434 PerlIOScalar_pushed. So set aside our scalar temporarily. */
1435 PerlIOScalar * const os = PerlIOSelf(o, PerlIOScalar);
1436 PerlIOScalar *fs = NULL; /* avoid "may be used uninitialized" warning */
1437 SV * const var = os->var;
1438 os->var = newSVpvs("");
1439 if ((f = PerlIOBase_dup(aTHX_ f, o, param, flags))) {
1440 fs = PerlIOSelf(f, PerlIOScalar);
1441 /* var has been set by implicit push, so replace it */
1442 SvREFCNT_dec(fs->var);
1444 SvREFCNT_dec(os->var);
1447 SV * const rv = PerlIOScalar_arg(aTHX_ o, param, flags);
1448 fs->var = SvREFCNT_inc(SvRV(rv));
1450 fs->posn = os->posn;
1455 static PERLIO_FUNCS_DECL(PerlIO_scalar) = {
1456 sizeof(PerlIO_funcs),
1458 sizeof(PerlIOScalar),
1459 PERLIO_K_BUFFERED | PERLIO_K_RAW,
1460 PerlIOScalar_pushed,
1461 PerlIOScalar_popped,
1465 PerlIOScalar_fileno,
1477 PerlIOBase_clearerr,
1478 PerlIOBase_setlinebuf,
1479 PerlIOScalar_get_base,
1480 PerlIOScalar_bufsiz,
1481 PerlIOScalar_get_ptr,
1482 PerlIOScalar_get_cnt,
1483 PerlIOScalar_set_ptrcnt,
1488 PerlIO_default_layers(pTHX)
1490 if (!PL_def_layerlist) {
1491 const char * const s = TAINTING_get ? NULL : PerlEnv_getenv("PERLIO");
1492 PERLIO_FUNCS_DECL(*osLayer) = &PerlIO_unix;
1493 PL_def_layerlist = PerlIO_list_alloc(aTHX);
1494 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_unix));
1495 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_raw));
1496 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_perlio));
1497 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_stdio));
1498 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_crlf));
1499 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_utf8));
1500 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_remove));
1501 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_byte));
1502 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_scalar));
1503 PerlIO_list_push(aTHX_ PL_def_layerlist, (PerlIO_funcs *)osLayer,
1506 PerlIO_parse_layers(aTHX_ PL_def_layerlist, s);
1509 PerlIO_default_buffer(aTHX_ PL_def_layerlist);
1512 if (PL_def_layerlist->cur < 2) {
1513 PerlIO_default_buffer(aTHX_ PL_def_layerlist);
1515 return PL_def_layerlist;
1519 Perl_boot_core_PerlIO(pTHX)
1521 #ifdef USE_ATTRIBUTES_FOR_PERLIO
1522 newXS("io::MODIFY_SCALAR_ATTRIBUTES", XS_io_MODIFY_SCALAR_ATTRIBUTES,
1525 newXS("PerlIO::Layer::find", XS_PerlIO__Layer__find, __FILE__);
1526 newXS("PerlIO::Layer::NoWarnings", XS_PerlIO__Layer__NoWarnings, __FILE__);
1530 PerlIO_default_layer(pTHX_ I32 n)
1532 PerlIO_list_t * const av = PerlIO_default_layers(aTHX);
1535 return PerlIO_layer_fetch(aTHX_ av, n, PERLIO_FUNCS_CAST(&PerlIO_stdio));
1538 #define PerlIO_default_top() PerlIO_default_layer(aTHX_ -1)
1539 #define PerlIO_default_btm() PerlIO_default_layer(aTHX_ 0)
1542 PerlIO_stdstreams(pTHX)
1545 PerlIO_init_table(aTHX);
1546 PerlIO_fdopen(0, "Ir" PERLIO_STDTEXT);
1547 PerlIO_fdopen(1, "Iw" PERLIO_STDTEXT);
1548 PerlIO_fdopen(2, "Iw" PERLIO_STDTEXT);
1553 PerlIO_push(pTHX_ PerlIO *f, PERLIO_FUNCS_DECL(*tab), const char *mode, SV *arg)
1556 if (tab->fsize != sizeof(PerlIO_funcs)) {
1558 "%s (%" UVuf ") does not match %s (%" UVuf ")",
1559 "PerlIO layer function table size", (UV)tab->fsize,
1560 "size expected by this perl", (UV)sizeof(PerlIO_funcs) );
1564 if (tab->size < sizeof(PerlIOl)) {
1566 "%s (%" UVuf ") smaller than %s (%" UVuf ")",
1567 "PerlIO layer instance size", (UV)tab->size,
1568 "size expected by this perl", (UV)sizeof(PerlIOl) );
1570 /* Real layer with a data area */
1573 Newxz(temp, tab->size, char);
1577 l->tab = (PerlIO_funcs*) tab;
1578 l->head = ((PerlIOl*)f)->head;
1580 DEBUG_i( PerlIO_debug("PerlIO_push f=%p %s %s %p\n",
1581 (void*)f, tab->name,
1582 (mode) ? mode : "(Null)", (void*)arg) );
1583 if (*l->tab->Pushed &&
1585 (aTHX_ f, mode, arg, (PerlIO_funcs*) tab) != 0) {
1586 PerlIO_pop(aTHX_ f);
1595 /* Pseudo-layer where push does its own stack adjust */
1596 DEBUG_i( PerlIO_debug("PerlIO_push f=%p %s %s %p\n", (void*)f, tab->name,
1597 (mode) ? mode : "(Null)", (void*)arg) );
1599 (*tab->Pushed) (aTHX_ f, mode, arg, (PerlIO_funcs*) tab) != 0) {
1607 PerlIOBase_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
1608 IV n, const char *mode, int fd, int imode, int perm,
1609 PerlIO *old, int narg, SV **args)
1611 PerlIO_funcs * const tab = PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIO_default_layer(aTHX_ 0));
1612 if (tab && tab->Open) {
1613 PerlIO* ret = (*tab->Open)(aTHX_ tab, layers, n - 1, mode, fd, imode, perm, old, narg, args);
1614 if (ret && PerlIO_push(aTHX_ ret, self, mode, PerlIOArg) == NULL) {
1620 SETERRNO(EINVAL, LIB_INVARG);
1625 PerlIOBase_binmode(pTHX_ PerlIO *f)
1627 if (PerlIOValid(f)) {
1628 /* Is layer suitable for raw stream ? */
1629 if (PerlIOBase(f)->tab && PerlIOBase(f)->tab->kind & PERLIO_K_RAW) {
1630 /* Yes - turn off UTF-8-ness, to undo UTF-8 locale effects */
1631 PerlIOBase(f)->flags &= ~PERLIO_F_UTF8;
1634 /* Not suitable - pop it */
1635 PerlIO_pop(aTHX_ f);
1643 PerlIORaw_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
1645 PERL_UNUSED_ARG(mode);
1646 PERL_UNUSED_ARG(arg);
1647 PERL_UNUSED_ARG(tab);
1649 if (PerlIOValid(f)) {
1654 * Strip all layers that are not suitable for a raw stream
1657 while (t && (l = *t)) {
1658 if (l->tab && l->tab->Binmode) {
1659 /* Has a handler - normal case */
1660 if ((*l->tab->Binmode)(aTHX_ t) == 0) {
1662 /* Layer still there - move down a layer */
1671 /* No handler - pop it */
1672 PerlIO_pop(aTHX_ t);
1675 if (PerlIOValid(f)) {
1676 DEBUG_i( PerlIO_debug(":raw f=%p :%s\n", (void*)f,
1677 PerlIOBase(f)->tab ? PerlIOBase(f)->tab->name : "(Null)") );
1685 PerlIO_apply_layera(pTHX_ PerlIO *f, const char *mode,
1686 PerlIO_list_t *layers, IV n, IV max)
1690 PerlIO_funcs * const tab = PerlIO_layer_fetch(aTHX_ layers, n, NULL);
1692 if (!PerlIO_push(aTHX_ f, tab, mode, PerlIOArg)) {
1703 PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names)
1707 save_scalar(PL_errgv);
1709 PerlIO_list_t * const layers = PerlIO_list_alloc(aTHX);
1710 code = PerlIO_parse_layers(aTHX_ layers, names);
1712 code = PerlIO_apply_layera(aTHX_ f, mode, layers, 0, layers->cur);
1714 PerlIO_list_free(aTHX_ layers);
1721 /*--------------------------------------------------------------------------------------*/
1723 * Given the abstraction above the public API functions
1727 PerlIO_binmode(pTHX_ PerlIO *f, int iotype, int mode, const char *names)
1729 PERL_UNUSED_ARG(iotype);
1730 PERL_UNUSED_ARG(mode);
1733 PerlIO_debug("PerlIO_binmode f=%p %s %c %x %s\n", (void*)f,
1734 (PerlIOBase(f) && PerlIOBase(f)->tab) ?
1735 PerlIOBase(f)->tab->name : "(Null)",
1736 iotype, mode, (names) ? names : "(Null)") );
1739 /* Do not flush etc. if (e.g.) switching encodings.
1740 if a pushed layer knows it needs to flush lower layers
1741 (for example :unix which is never going to call them)
1742 it can do the flush when it is pushed.
1744 return cBOOL(PerlIO_apply_layers(aTHX_ f, NULL, names) == 0);
1747 /* Fake 5.6 legacy of using this call to turn ON O_TEXT */
1748 #ifdef PERLIO_USING_CRLF
1749 /* Legacy binmode only has meaning if O_TEXT has a value distinct from
1750 O_BINARY so we can look for it in mode.
1752 if (!(mode & O_BINARY)) {
1754 /* FIXME?: Looking down the layer stack seems wrong,
1755 but is a way of reaching past (say) an encoding layer
1756 to flip CRLF-ness of the layer(s) below
1759 /* Perhaps we should turn on bottom-most aware layer
1760 e.g. Ilya's idea that UNIX TTY could serve
1762 if (PerlIOBase(f)->tab &&
1763 PerlIOBase(f)->tab->kind & PERLIO_K_CANCRLF)
1765 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF)) {
1766 /* Not in text mode - flush any pending stuff and flip it */
1768 PerlIOBase(f)->flags |= PERLIO_F_CRLF;
1770 /* Only need to turn it on in one layer so we are done */
1775 /* Not finding a CRLF aware layer presumably means we are binary
1776 which is not what was requested - so we failed
1777 We _could_ push :crlf layer but so could caller
1782 /* Legacy binmode is now _defined_ as being equivalent to pushing :raw
1783 So code that used to be here is now in PerlIORaw_pushed().
1785 return cBOOL(PerlIO_push(aTHX_ f, PERLIO_FUNCS_CAST(&PerlIO_raw), NULL, NULL));
1790 PerlIO__close(pTHX_ PerlIO *f)
1792 if (PerlIOValid(f)) {
1793 PerlIO_funcs * const tab = PerlIOBase(f)->tab;
1794 if (tab && tab->Close)
1795 return (*tab->Close)(aTHX_ f);
1797 return PerlIOBase_close(aTHX_ f);
1800 SETERRNO(EBADF, SS_IVCHAN);
1806 Perl_PerlIO_close(pTHX_ PerlIO *f)
1808 const int code = PerlIO__close(aTHX_ f);
1809 while (PerlIOValid(f)) {
1810 PerlIO_pop(aTHX_ f);
1811 if (PerlIO_lockcnt(f))
1812 /* we're in use; the 'pop' deferred freeing the structure */
1819 Perl_PerlIO_fileno(pTHX_ PerlIO *f)
1821 Perl_PerlIO_or_Base(f, Fileno, fileno, -1, (aTHX_ f));
1825 static PerlIO_funcs *
1826 PerlIO_layer_from_ref(pTHX_ SV *sv)
1829 * For any scalar type load the handler which is bundled with perl
1831 if (SvTYPE(sv) < SVt_PVAV && (!isGV_with_GP(sv) || SvFAKE(sv)))
1832 return (PerlIO_funcs*) &PerlIO_scalar;
1835 * For other types allow if layer is known but don't try and load it
1837 switch (SvTYPE(sv)) {
1839 return PerlIO_find_layer(aTHX_ STR_WITH_LEN("Array"), 0);
1841 return PerlIO_find_layer(aTHX_ STR_WITH_LEN("Hash"), 0);
1843 return PerlIO_find_layer(aTHX_ STR_WITH_LEN("Code"), 0);
1845 return PerlIO_find_layer(aTHX_ STR_WITH_LEN("Glob"), 0);
1852 PerlIO_resolve_layers(pTHX_ const char *layers,
1853 const char *mode, int narg, SV **args)
1855 PerlIO_list_t *def = PerlIO_default_layers(aTHX);
1858 PerlIO_stdstreams(aTHX);
1860 SV * const arg = *args;
1862 * If it is a reference but not an object see if we have a handler
1865 if (SvROK(arg) && !SvOBJECT(SvRV(arg))) {
1866 PerlIO_funcs * const handler = PerlIO_layer_from_ref(aTHX_ SvRV(arg));
1868 def = PerlIO_list_alloc(aTHX);
1869 PerlIO_list_push(aTHX_ def, handler, &PL_sv_undef);
1873 * Don't fail if handler cannot be found :via(...) etc. may do
1874 * something sensible else we will just stringify and open
1879 if (!layers || !*layers)
1880 layers = Perl_PerlIO_context_layers(aTHX_ mode);
1881 if (layers && *layers) {
1884 av = PerlIO_clone_list(aTHX_ def, NULL);
1889 if (PerlIO_parse_layers(aTHX_ av, layers) == 0) {
1893 PerlIO_list_free(aTHX_ av);
1905 PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd,
1906 int imode, int perm, PerlIO *f, int narg, SV **args)
1908 if (!f && narg == 1 && *args == &PL_sv_undef) {
1909 imode = PerlIOUnix_oflags(mode);
1911 if (imode != -1 && (f = PerlIO_tmpfile_flags(imode))) {
1912 if (!layers || !*layers)
1913 layers = Perl_PerlIO_context_layers(aTHX_ mode);
1914 if (layers && *layers)
1915 PerlIO_apply_layers(aTHX_ f, mode, layers);
1919 PerlIO_list_t *layera;
1921 PerlIO_funcs *tab = NULL;
1922 if (PerlIOValid(f)) {
1924 * This is "reopen" - it is not tested as perl does not use it
1928 layera = PerlIO_list_alloc(aTHX);
1931 if (l->tab && l->tab->Getarg)
1932 arg = (*l->tab->Getarg) (aTHX_ &l, NULL, 0);
1933 PerlIO_list_push(aTHX_ layera, l->tab,
1934 (arg) ? arg : &PL_sv_undef);
1936 l = *PerlIONext(&l);
1940 layera = PerlIO_resolve_layers(aTHX_ layers, mode, narg, args);
1946 * Start at "top" of layer stack
1948 n = layera->cur - 1;
1950 PerlIO_funcs * const t = PerlIO_layer_fetch(aTHX_ layera, n, NULL);
1959 * Found that layer 'n' can do opens - call it
1961 if (narg > 1 && !(tab->kind & PERLIO_K_MULTIARG)) {
1962 Perl_croak(aTHX_ "More than one argument to open(,':%s')",tab->name);
1964 DEBUG_i( PerlIO_debug("openn(%s,'%s','%s',%d,%x,%o,%p,%d,%p)\n",
1965 tab->name, layers ? layers : "(Null)", mode, fd,
1966 imode, perm, (void*)f, narg, (void*)args) );
1968 f = (*tab->Open) (aTHX_ tab, layera, n, mode, fd, imode, perm,
1971 SETERRNO(EINVAL, LIB_INVARG);
1975 if (n + 1 < layera->cur) {
1977 * More layers above the one that we used to open -
1980 if (PerlIO_apply_layera(aTHX_ f, mode, layera, n + 1, layera->cur) != 0) {
1981 /* If pushing layers fails close the file */
1988 PerlIO_list_free(aTHX_ layera);
1995 Perl_PerlIO_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
1997 PERL_ARGS_ASSERT_PERLIO_READ;
1999 Perl_PerlIO_or_Base(f, Read, read, -1, (aTHX_ f, vbuf, count));
2003 Perl_PerlIO_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
2005 PERL_ARGS_ASSERT_PERLIO_UNREAD;
2007 Perl_PerlIO_or_Base(f, Unread, unread, -1, (aTHX_ f, vbuf, count));
2011 Perl_PerlIO_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
2013 PERL_ARGS_ASSERT_PERLIO_WRITE;
2015 Perl_PerlIO_or_fail(f, Write, -1, (aTHX_ f, vbuf, count));
2019 Perl_PerlIO_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
2021 Perl_PerlIO_or_fail(f, Seek, -1, (aTHX_ f, offset, whence));
2025 Perl_PerlIO_tell(pTHX_ PerlIO *f)
2027 Perl_PerlIO_or_fail(f, Tell, -1, (aTHX_ f));
2031 Perl_PerlIO_flush(pTHX_ PerlIO *f)
2035 const PerlIO_funcs *tab = PerlIOBase(f)->tab;
2037 if (tab && tab->Flush)
2038 return (*tab->Flush) (aTHX_ f);
2040 return 0; /* If no Flush defined, silently succeed. */
2043 DEBUG_i( PerlIO_debug("Cannot flush f=%p\n", (void*)f) );
2044 SETERRNO(EBADF, SS_IVCHAN);
2050 * Is it good API design to do flush-all on NULL, a potentially
2051 * erroneous input? Maybe some magical value (PerlIO*
2052 * PERLIO_FLUSH_ALL = (PerlIO*)-1;)? Yes, stdio does similar
2053 * things on fflush(NULL), but should we be bound by their design
2056 PerlIOl **table = &PL_perlio;
2059 while ((ff = *table)) {
2063 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
2064 if (ff->next && PerlIO_flush(&(ff->next)) != 0)
2074 PerlIOBase_flush_linebuf(pTHX)
2076 PerlIOl **table = &PL_perlio;
2078 while ((f = *table)) {
2082 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
2084 && (PerlIOBase(&(f->next))->
2085 flags & (PERLIO_F_LINEBUF | PERLIO_F_CANWRITE))
2086 == (PERLIO_F_LINEBUF | PERLIO_F_CANWRITE))
2087 PerlIO_flush(&(f->next));
2094 Perl_PerlIO_fill(pTHX_ PerlIO *f)
2096 Perl_PerlIO_or_fail(f, Fill, -1, (aTHX_ f));
2100 PerlIO_isutf8(PerlIO *f)
2103 return (PerlIOBase(f)->flags & PERLIO_F_UTF8) != 0;
2105 SETERRNO(EBADF, SS_IVCHAN);
2111 Perl_PerlIO_eof(pTHX_ PerlIO *f)
2113 Perl_PerlIO_or_Base(f, Eof, eof, -1, (aTHX_ f));
2117 Perl_PerlIO_error(pTHX_ PerlIO *f)
2119 Perl_PerlIO_or_Base(f, Error, error, -1, (aTHX_ f));
2123 Perl_PerlIO_clearerr(pTHX_ PerlIO *f)
2125 Perl_PerlIO_or_Base_void(f, Clearerr, clearerr, (aTHX_ f));
2129 Perl_PerlIO_setlinebuf(pTHX_ PerlIO *f)
2131 Perl_PerlIO_or_Base_void(f, Setlinebuf, setlinebuf, (aTHX_ f));
2135 PerlIO_has_base(PerlIO *f)
2137 if (PerlIOValid(f)) {
2138 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
2141 return (tab->Get_base != NULL);
2148 PerlIO_fast_gets(PerlIO *f)
2150 if (PerlIOValid(f)) {
2151 if (PerlIOBase(f)->flags & PERLIO_F_FASTGETS) {
2152 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
2155 return (tab->Set_ptrcnt != NULL);
2163 PerlIO_has_cntptr(PerlIO *f)
2165 if (PerlIOValid(f)) {
2166 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
2169 return (tab->Get_ptr != NULL && tab->Get_cnt != NULL);
2176 PerlIO_canset_cnt(PerlIO *f)
2178 if (PerlIOValid(f)) {
2179 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
2182 return (tab->Set_ptrcnt != NULL);
2189 Perl_PerlIO_get_base(pTHX_ PerlIO *f)
2191 Perl_PerlIO_or_fail(f, Get_base, NULL, (aTHX_ f));
2195 Perl_PerlIO_get_bufsiz(pTHX_ PerlIO *f)
2197 /* Note that Get_bufsiz returns a Size_t */
2198 Perl_PerlIO_or_fail(f, Get_bufsiz, -1, (aTHX_ f));
2202 Perl_PerlIO_get_ptr(pTHX_ PerlIO *f)
2204 Perl_PerlIO_or_fail(f, Get_ptr, NULL, (aTHX_ f));
2208 Perl_PerlIO_get_cnt(pTHX_ PerlIO *f)
2210 Perl_PerlIO_or_fail(f, Get_cnt, -1, (aTHX_ f));
2214 Perl_PerlIO_set_cnt(pTHX_ PerlIO *f, SSize_t cnt)
2216 Perl_PerlIO_or_fail_void(f, Set_ptrcnt, (aTHX_ f, NULL, cnt));
2220 Perl_PerlIO_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
2222 Perl_PerlIO_or_fail_void(f, Set_ptrcnt, (aTHX_ f, ptr, cnt));
2226 /*--------------------------------------------------------------------------------------*/
2228 * utf8 and raw dummy layers
2232 PerlIOUtf8_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
2234 PERL_UNUSED_CONTEXT;
2235 PERL_UNUSED_ARG(mode);
2236 PERL_UNUSED_ARG(arg);
2237 if (PerlIOValid(f)) {
2238 if (tab && tab->kind & PERLIO_K_UTF8)
2239 PerlIOBase(f)->flags |= PERLIO_F_UTF8;
2241 PerlIOBase(f)->flags &= ~PERLIO_F_UTF8;
2247 PERLIO_FUNCS_DECL(PerlIO_utf8) = {
2248 sizeof(PerlIO_funcs),
2251 PERLIO_K_DUMMY | PERLIO_K_UTF8 | PERLIO_K_MULTIARG,
2271 NULL, /* get_base */
2272 NULL, /* get_bufsiz */
2275 NULL, /* set_ptrcnt */
2278 PERLIO_FUNCS_DECL(PerlIO_byte) = {
2279 sizeof(PerlIO_funcs),
2282 PERLIO_K_DUMMY | PERLIO_K_MULTIARG,
2302 NULL, /* get_base */
2303 NULL, /* get_bufsiz */
2306 NULL, /* set_ptrcnt */
2309 PERLIO_FUNCS_DECL(PerlIO_raw) = {
2310 sizeof(PerlIO_funcs),
2333 NULL, /* get_base */
2334 NULL, /* get_bufsiz */
2337 NULL, /* set_ptrcnt */
2339 /*--------------------------------------------------------------------------------------*/
2340 /*--------------------------------------------------------------------------------------*/
2342 * "Methods" of the "base class"
2346 PerlIOBase_fileno(pTHX_ PerlIO *f)
2348 return PerlIOValid(f) ? PerlIO_fileno(PerlIONext(f)) : -1;
2352 PerlIO_modestr(PerlIO * f, char *buf)
2355 if (PerlIOValid(f)) {
2356 const IV flags = PerlIOBase(f)->flags;
2357 if (flags & PERLIO_F_APPEND) {
2359 if (flags & PERLIO_F_CANREAD) {
2363 else if (flags & PERLIO_F_CANREAD) {
2365 if (flags & PERLIO_F_CANWRITE)
2368 else if (flags & PERLIO_F_CANWRITE) {
2370 if (flags & PERLIO_F_CANREAD) {
2374 #ifdef PERLIO_USING_CRLF
2375 if (!(flags & PERLIO_F_CRLF))
2385 PerlIOBase_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
2387 PerlIOl * const l = PerlIOBase(f);
2388 PERL_UNUSED_CONTEXT;
2389 PERL_UNUSED_ARG(arg);
2391 l->flags &= ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE |
2392 PERLIO_F_TRUNCATE | PERLIO_F_APPEND);
2393 if (tab && tab->Set_ptrcnt != NULL)
2394 l->flags |= PERLIO_F_FASTGETS;
2396 if (*mode == IoTYPE_NUMERIC || *mode == IoTYPE_IMPLICIT)
2400 l->flags |= PERLIO_F_CANREAD;
2403 l->flags |= PERLIO_F_APPEND | PERLIO_F_CANWRITE;
2406 l->flags |= PERLIO_F_TRUNCATE | PERLIO_F_CANWRITE;
2409 SETERRNO(EINVAL, LIB_INVARG);
2412 #ifdef __MVS__ /* XXX Perhaps should be be OEMVS instead of __MVS__ */
2414 /* The mode variable contains one positional parameter followed by
2415 * optional keyword parameters. The positional parameters must be
2416 * passed as lowercase characters. The keyword parameters can be
2417 * passed in mixed case. They must be separated by commas. Only one
2418 * instance of a keyword can be specified. */
2424 l->flags |= PERLIO_F_CANREAD | PERLIO_F_CANWRITE;
2428 l->flags &= ~PERLIO_F_CRLF;
2432 l->flags |= PERLIO_F_CRLF;
2446 l->flags |= PERLIO_F_CANREAD | PERLIO_F_CANWRITE;
2449 l->flags &= ~PERLIO_F_CRLF;
2452 l->flags |= PERLIO_F_CRLF;
2455 SETERRNO(EINVAL, LIB_INVARG);
2463 l->flags |= l->next->flags &
2464 (PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_TRUNCATE |
2470 PerlIO_debug("PerlIOBase_pushed f=%p %s %s fl=%08" UVxf " (%s)\n",
2471 (void*)f, PerlIOBase(f)->tab->name, (omode) ? omode : "(Null)",
2472 l->flags, PerlIO_modestr(f, temp));
2479 PerlIOBase_popped(pTHX_ PerlIO *f)
2481 PERL_UNUSED_CONTEXT;
2487 PerlIOBase_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
2490 * Save the position as current head considers it
2492 const Off_t old = PerlIO_tell(f);
2493 PerlIO_push(aTHX_ f, PERLIO_FUNCS_CAST(&PerlIO_pending), "r", NULL);
2494 PerlIOSelf(f, PerlIOBuf)->posn = old;
2495 return PerlIOBuf_unread(aTHX_ f, vbuf, count);
2499 PerlIOBase_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
2501 STDCHAR *buf = (STDCHAR *) vbuf;
2503 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD)) {
2504 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2505 SETERRNO(EBADF, SS_IVCHAN);
2506 PerlIO_save_errno(f);
2512 SSize_t avail = PerlIO_get_cnt(f);
2515 take = (((SSize_t) count >= 0) && ((SSize_t)count < avail)) ? (SSize_t)count : avail;
2517 STDCHAR *ptr = PerlIO_get_ptr(f);
2518 Copy(ptr, buf, take, STDCHAR);
2519 PerlIO_set_ptrcnt(f, ptr + take, (avail -= take));
2522 if (avail == 0) /* set_ptrcnt could have reset avail */
2525 if (count > 0 && avail <= 0) {
2526 if (PerlIO_fill(f) != 0)
2531 return (buf - (STDCHAR *) vbuf);
2537 PerlIOBase_noop_ok(pTHX_ PerlIO *f)
2539 PERL_UNUSED_CONTEXT;
2545 PerlIOBase_noop_fail(pTHX_ PerlIO *f)
2547 PERL_UNUSED_CONTEXT;
2553 PerlIOBase_close(pTHX_ PerlIO *f)
2556 if (PerlIOValid(f)) {
2557 PerlIO *n = PerlIONext(f);
2558 code = PerlIO_flush(f);
2559 PerlIOBase(f)->flags &=
2560 ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_OPEN);
2561 while (PerlIOValid(n)) {
2562 const PerlIO_funcs * const tab = PerlIOBase(n)->tab;
2563 if (tab && tab->Close) {
2564 if ((*tab->Close)(aTHX_ n) != 0)
2569 PerlIOBase(n)->flags &=
2570 ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_OPEN);
2576 SETERRNO(EBADF, SS_IVCHAN);
2582 PerlIOBase_eof(pTHX_ PerlIO *f)
2584 PERL_UNUSED_CONTEXT;
2585 if (PerlIOValid(f)) {
2586 return (PerlIOBase(f)->flags & PERLIO_F_EOF) != 0;
2592 PerlIOBase_error(pTHX_ PerlIO *f)
2594 PERL_UNUSED_CONTEXT;
2595 if (PerlIOValid(f)) {
2596 return (PerlIOBase(f)->flags & PERLIO_F_ERROR) != 0;
2602 PerlIOBase_clearerr(pTHX_ PerlIO *f)
2604 if (PerlIOValid(f)) {
2605 PerlIO * const n = PerlIONext(f);
2606 PerlIOBase(f)->flags &= ~(PERLIO_F_ERROR | PERLIO_F_EOF);
2613 PerlIOBase_setlinebuf(pTHX_ PerlIO *f)
2615 PERL_UNUSED_CONTEXT;
2616 if (PerlIOValid(f)) {
2617 PerlIOBase(f)->flags |= PERLIO_F_LINEBUF;
2622 PerlIO_sv_dup(pTHX_ SV *arg, CLONE_PARAMS *param)
2628 arg = sv_dup(arg, param);
2629 SvREFCNT_inc_simple_void_NN(arg);
2633 return newSVsv(arg);
2636 PERL_UNUSED_ARG(param);
2637 return newSVsv(arg);
2642 PerlIOBase_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
2644 PerlIO * const nexto = PerlIONext(o);
2645 if (PerlIOValid(nexto)) {
2646 const PerlIO_funcs * const tab = PerlIOBase(nexto)->tab;
2647 if (tab && tab->Dup)
2648 f = (*tab->Dup)(aTHX_ f, nexto, param, flags);
2650 f = PerlIOBase_dup(aTHX_ f, nexto, param, flags);
2653 PerlIO_funcs * const self = PerlIOBase(o)->tab;
2657 DEBUG_i(PerlIO_debug("PerlIOBase_dup %s f=%p o=%p param=%p\n",
2659 (void*)f, (void*)o, (void*)param) );
2661 arg = (*self->Getarg)(aTHX_ o, param, flags);
2662 f = PerlIO_push(aTHX_ f, self, PerlIO_modestr(o,buf), arg);
2663 if (f && PerlIOBase(o)->flags & PERLIO_F_UTF8)
2664 PerlIOBase(f)->flags |= PERLIO_F_UTF8;
2670 /* PL_perlio_fd_refcnt[] is in intrpvar.h */
2672 /* Must be called with PL_perlio_mutex locked. */
2674 S_more_refcounted_fds(pTHX_ const int new_fd)
2675 PERL_TSA_REQUIRES(PL_perlio_mutex)
2677 const int old_max = PL_perlio_fd_refcnt_size;
2678 const int new_max = 16 + (new_fd & ~15);
2681 #ifndef PERL_IMPLICIT_SYS
2682 PERL_UNUSED_CONTEXT;
2685 DEBUG_i( PerlIO_debug("More fds - old=%d, need %d, new=%d\n",
2686 old_max, new_fd, new_max) );
2688 if (new_fd < old_max) {
2692 assert (new_max > new_fd);
2694 /* Use plain realloc() since we need this memory to be really
2695 * global and visible to all the interpreters and/or threads. */
2696 new_array = (int*) realloc(PL_perlio_fd_refcnt, new_max * sizeof(int));
2699 MUTEX_UNLOCK(&PL_perlio_mutex);
2703 PL_perlio_fd_refcnt_size = new_max;
2704 PL_perlio_fd_refcnt = new_array;
2706 DEBUG_i( PerlIO_debug("Zeroing %p, %d\n",
2707 (void*)(new_array + old_max),
2708 new_max - old_max) );
2710 Zero(new_array + old_max, new_max - old_max, int);
2717 /* MUTEX_INIT(&PL_perlio_mutex) is done in PERL_SYS_INIT3(). */
2718 PERL_UNUSED_CONTEXT;
2722 PerlIOUnix_refcnt_inc(int fd)
2727 MUTEX_LOCK(&PL_perlio_mutex);
2728 if (fd >= PL_perlio_fd_refcnt_size)
2729 S_more_refcounted_fds(aTHX_ fd);
2731 PL_perlio_fd_refcnt[fd]++;
2732 if (PL_perlio_fd_refcnt[fd] <= 0) {
2733 /* diag_listed_as: refcnt_inc: fd %d%s */
2734 Perl_croak(aTHX_ "refcnt_inc: fd %d: %d <= 0\n",
2735 fd, PL_perlio_fd_refcnt[fd]);
2737 DEBUG_i( PerlIO_debug("refcnt_inc: fd %d refcnt=%d\n",
2738 fd, PL_perlio_fd_refcnt[fd]) );
2740 MUTEX_UNLOCK(&PL_perlio_mutex);
2742 /* diag_listed_as: refcnt_inc: fd %d%s */
2743 Perl_croak(aTHX_ "refcnt_inc: fd %d < 0\n", fd);
2748 PerlIOUnix_refcnt_dec(int fd)
2755 MUTEX_LOCK(&PL_perlio_mutex);
2756 if (fd >= PL_perlio_fd_refcnt_size) {
2757 /* diag_listed_as: refcnt_dec: fd %d%s */
2758 Perl_croak_nocontext("refcnt_dec: fd %d >= refcnt_size %d\n",
2759 fd, PL_perlio_fd_refcnt_size);
2761 if (PL_perlio_fd_refcnt[fd] <= 0) {
2762 /* diag_listed_as: refcnt_dec: fd %d%s */
2763 Perl_croak_nocontext("refcnt_dec: fd %d: %d <= 0\n",
2764 fd, PL_perlio_fd_refcnt[fd]);
2766 cnt = --PL_perlio_fd_refcnt[fd];
2767 DEBUG_i( PerlIO_debug("refcnt_dec: fd %d refcnt=%d\n", fd, cnt) );
2768 MUTEX_UNLOCK(&PL_perlio_mutex);
2770 /* diag_listed_as: refcnt_dec: fd %d%s */
2771 Perl_croak_nocontext("refcnt_dec: fd %d < 0\n", fd);
2777 PerlIOUnix_refcnt(int fd)
2782 MUTEX_LOCK(&PL_perlio_mutex);
2783 if (fd >= PL_perlio_fd_refcnt_size) {
2784 /* diag_listed_as: refcnt: fd %d%s */
2785 Perl_croak(aTHX_ "refcnt: fd %d >= refcnt_size %d\n",
2786 fd, PL_perlio_fd_refcnt_size);
2788 if (PL_perlio_fd_refcnt[fd] <= 0) {
2789 /* diag_listed_as: refcnt: fd %d%s */
2790 Perl_croak(aTHX_ "refcnt: fd %d: %d <= 0\n",
2791 fd, PL_perlio_fd_refcnt[fd]);
2793 cnt = PL_perlio_fd_refcnt[fd];
2794 MUTEX_UNLOCK(&PL_perlio_mutex);
2796 /* diag_listed_as: refcnt: fd %d%s */
2797 Perl_croak(aTHX_ "refcnt: fd %d < 0\n", fd);
2803 PerlIO_cleanup(pTHX)
2807 DEBUG_i( PerlIO_debug("Cleanup layers for %p\n",(void*)aTHX) );
2809 DEBUG_i( PerlIO_debug("Cleanup layers\n") );
2812 /* Raise STDIN..STDERR refcount so we don't close them */
2813 for (i=0; i < 3; i++)
2814 PerlIOUnix_refcnt_inc(i);
2815 PerlIO_cleantable(aTHX_ &PL_perlio);
2816 /* Restore STDIN..STDERR refcount */
2817 for (i=0; i < 3; i++)
2818 PerlIOUnix_refcnt_dec(i);
2820 if (PL_known_layers) {
2821 PerlIO_list_free(aTHX_ PL_known_layers);
2822 PL_known_layers = NULL;
2824 if (PL_def_layerlist) {
2825 PerlIO_list_free(aTHX_ PL_def_layerlist);
2826 PL_def_layerlist = NULL;
2830 void PerlIO_teardown(void) /* Call only from PERL_SYS_TERM(). */
2833 /* XXX we can't rely on an interpreter being present at this late stage,
2834 XXX so we can't use a function like PerlLIO_write that relies on one
2835 being present (at least in win32) :-(.
2840 /* By now all filehandles should have been closed, so any
2841 * stray (non-STD-)filehandles indicate *possible* (PerlIO)
2843 #define PERLIO_TEARDOWN_MESSAGE_BUF_SIZE 64
2844 #define PERLIO_TEARDOWN_MESSAGE_FD 2
2845 char buf[PERLIO_TEARDOWN_MESSAGE_BUF_SIZE];
2847 for (i = 3; i < PL_perlio_fd_refcnt_size; i++) {
2848 if (PL_perlio_fd_refcnt[i]) {
2850 my_snprintf(buf, sizeof(buf),
2851 "PerlIO_teardown: fd %d refcnt=%d\n",
2852 i, PL_perlio_fd_refcnt[i]);
2853 PerlLIO_write(PERLIO_TEARDOWN_MESSAGE_FD, buf, len);
2859 /* Not bothering with PL_perlio_mutex since by now
2860 * all the interpreters are gone. */
2861 if (PL_perlio_fd_refcnt_size /* Assuming initial size of zero. */
2862 && PL_perlio_fd_refcnt) {
2863 free(PL_perlio_fd_refcnt); /* To match realloc() in S_more_refcounted_fds(). */
2864 PL_perlio_fd_refcnt = NULL;
2865 PL_perlio_fd_refcnt_size = 0;
2869 /*--------------------------------------------------------------------------------------*/
2871 * Bottom-most level for UNIX-like case
2875 struct _PerlIO base; /* The generic part */
2876 int fd; /* UNIX like file descriptor */
2877 int oflags; /* open/fcntl flags */
2881 S_lockcnt_dec(pTHX_ const void* f)
2883 #ifndef PERL_IMPLICIT_SYS
2884 PERL_UNUSED_CONTEXT;
2886 PerlIO_lockcnt((PerlIO*)f)--;
2890 /* call the signal handler, and if that handler happens to clear
2891 * this handle, free what we can and return true */
2894 S_perlio_async_run(pTHX_ PerlIO* f) {
2896 SAVEDESTRUCTOR_X(S_lockcnt_dec, (void*)f);
2897 PerlIO_lockcnt(f)++;
2899 if ( !(PerlIOBase(f)->flags & PERLIO_F_CLEARED) ) {
2903 /* we've just run some perl-level code that could have done
2904 * anything, including closing the file or clearing this layer.
2905 * If so, free any lower layers that have already been
2906 * cleared, then return an error. */
2907 while (PerlIOValid(f) &&
2908 (PerlIOBase(f)->flags & PERLIO_F_CLEARED))
2910 const PerlIOl *l = *f;
2919 PerlIOUnix_oflags(const char *mode)
2922 if (*mode == IoTYPE_IMPLICIT || *mode == IoTYPE_NUMERIC)
2927 if (*++mode == '+') {
2934 oflags = O_CREAT | O_TRUNC;
2935 if (*++mode == '+') {
2944 oflags = O_CREAT | O_APPEND;
2945 if (*++mode == '+') {
2954 /* XXX TODO: PerlIO_open() test that exercises 'rb' and 'rt'. */
2956 /* Unless O_BINARY is different from O_TEXT, first bit-or:ing one
2957 * of them in, and then bit-and-masking the other them away, won't
2958 * have much of an effect. */
2961 #if O_TEXT != O_BINARY
2968 #if O_TEXT != O_BINARY
2970 oflags &= ~O_BINARY;
2976 /* bit-or:ing with zero O_BINARY would be useless. */
2978 * If neither "t" nor "b" was specified, open the file
2981 * Note that if something else than the zero byte was seen
2982 * here (e.g. bogus mode "rx"), just few lines later we will
2983 * set the errno and invalidate the flags.
2989 if (*mode || oflags == -1) {
2990 SETERRNO(EINVAL, LIB_INVARG);
2997 PerlIOUnix_fileno(pTHX_ PerlIO *f)
2999 PERL_UNUSED_CONTEXT;
3000 return PerlIOSelf(f, PerlIOUnix)->fd;
3004 PerlIOUnix_setfd(pTHX_ PerlIO *f, int fd, int imode)
3006 PerlIOUnix * const s = PerlIOSelf(f, PerlIOUnix);
3009 if (PerlLIO_fstat(fd, &st) == 0) {
3010 if (!S_ISREG(st.st_mode)) {
3011 DEBUG_i( PerlIO_debug("%d is not regular file\n",fd) );
3012 PerlIOBase(f)->flags |= PERLIO_F_NOTREG;
3015 DEBUG_i( PerlIO_debug("%d _is_ a regular file\n",fd) );
3021 PerlIOUnix_refcnt_inc(fd);
3022 PERL_UNUSED_CONTEXT;
3026 PerlIOUnix_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
3028 IV code = PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
3029 if (*PerlIONext(f)) {
3030 /* We never call down so do any pending stuff now */
3031 PerlIO_flush(PerlIONext(f));
3033 * XXX could (or should) we retrieve the oflags from the open file
3034 * handle rather than believing the "mode" we are passed in? XXX
3035 * Should the value on NULL mode be 0 or -1?
3037 PerlIOUnix_setfd(aTHX_ f, PerlIO_fileno(PerlIONext(f)),
3038 mode ? PerlIOUnix_oflags(mode) : -1);
3040 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
3046 PerlIOUnix_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
3048 const int fd = PerlIOSelf(f, PerlIOUnix)->fd;
3050 PERL_UNUSED_CONTEXT;
3051 if (PerlIOBase(f)->flags & PERLIO_F_NOTREG) {
3053 SETERRNO(ESPIPE, LIB_INVARG);
3055 SETERRNO(EINVAL, LIB_INVARG);
3059 new_loc = PerlLIO_lseek(fd, offset, whence);
3060 if (new_loc == (Off_t) - 1)
3062 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
3067 PerlIOUnix_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
3068 IV n, const char *mode, int fd, int imode,
3069 int perm, PerlIO *f, int narg, SV **args)
3071 bool known_cloexec = 0;
3072 if (PerlIOValid(f)) {
3073 if (PerlIOBase(f)->tab && PerlIOBase(f)->flags & PERLIO_F_OPEN)
3074 (*PerlIOBase(f)->tab->Close)(aTHX_ f);
3077 if (*mode == IoTYPE_NUMERIC)
3080 imode = PerlIOUnix_oflags(mode);
3082 perm = 0777; /* preserve RMS defaults, ACL inheritance, etc. */
3089 const char *path = SvPV_const(*args, len);
3090 if (!IS_SAFE_PATHNAME(path, len, "open"))
3092 fd = PerlLIO_open3_cloexec(path, imode, perm);
3098 setfd_inhexec_for_sysfd(fd);
3100 setfd_cloexec_or_inhexec_by_sysfdness(fd);
3101 if (*mode == IoTYPE_IMPLICIT)
3104 f = PerlIO_allocate(aTHX);
3106 if (!PerlIOValid(f)) {
3107 if (!(f = PerlIO_push(aTHX_ f, self, mode, PerlIOArg))) {
3112 PerlIOUnix_setfd(aTHX_ f, fd, imode);
3113 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
3114 if (*mode == IoTYPE_APPEND)
3115 PerlIOUnix_seek(aTHX_ f, 0, SEEK_END);
3122 * FIXME: pop layers ???
3130 PerlIOUnix_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
3132 const PerlIOUnix * const os = PerlIOSelf(o, PerlIOUnix);
3134 if (flags & PERLIO_DUP_FD) {
3135 fd = PerlLIO_dup_cloexec(fd);
3137 setfd_inhexec_for_sysfd(fd);
3140 f = PerlIOBase_dup(aTHX_ f, o, param, flags);
3142 /* If all went well overwrite fd in dup'ed lay with the dup()'ed fd */
3143 PerlIOUnix_setfd(aTHX_ f, fd, os->oflags);
3153 PerlIOUnix_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
3156 if (PerlIO_lockcnt(f)) /* in use: abort ungracefully */
3158 fd = PerlIOSelf(f, PerlIOUnix)->fd;
3159 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD) ||
3160 PerlIOBase(f)->flags & (PERLIO_F_EOF|PERLIO_F_ERROR)) {
3164 const SSize_t len = PerlLIO_read(fd, vbuf, count);
3165 if (len >= 0 || errno != EINTR) {
3167 if (errno != EAGAIN) {
3168 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
3169 PerlIO_save_errno(f);
3172 else if (len == 0 && count != 0) {
3173 PerlIOBase(f)->flags |= PERLIO_F_EOF;
3179 if (PL_sig_pending && S_perlio_async_run(aTHX_ f))
3182 NOT_REACHED; /*NOTREACHED*/
3186 PerlIOUnix_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3189 if (PerlIO_lockcnt(f)) /* in use: abort ungracefully */
3191 fd = PerlIOSelf(f, PerlIOUnix)->fd;
3193 const SSize_t len = PerlLIO_write(fd, vbuf, count);
3194 if (len >= 0 || errno != EINTR) {
3196 if (errno != EAGAIN) {
3197 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
3198 PerlIO_save_errno(f);
3204 if (PL_sig_pending && S_perlio_async_run(aTHX_ f))
3207 NOT_REACHED; /*NOTREACHED*/
3211 PerlIOUnix_tell(pTHX_ PerlIO *f)
3213 PERL_UNUSED_CONTEXT;
3215 return PerlLIO_lseek(PerlIOSelf(f, PerlIOUnix)->fd, 0, SEEK_CUR);
3220 PerlIOUnix_close(pTHX_ PerlIO *f)
3222 const int fd = PerlIOSelf(f, PerlIOUnix)->fd;
3224 if (PerlIOBase(f)->flags & PERLIO_F_OPEN) {
3225 code = PerlIOBase_close(aTHX_ f);
3226 if (PerlIOUnix_refcnt_dec(fd) > 0) {
3227 PerlIOBase(f)->flags &= ~PERLIO_F_OPEN;
3232 SETERRNO(EBADF,SS_IVCHAN);
3235 while (PerlLIO_close(fd) != 0) {
3236 if (errno != EINTR) {
3241 if (PL_sig_pending && S_perlio_async_run(aTHX_ f))
3245 PerlIOBase(f)->flags &= ~PERLIO_F_OPEN;
3250 PERLIO_FUNCS_DECL(PerlIO_unix) = {
3251 sizeof(PerlIO_funcs),
3258 PerlIOBase_binmode, /* binmode */
3268 PerlIOBase_noop_ok, /* flush */
3269 PerlIOBase_noop_fail, /* fill */
3272 PerlIOBase_clearerr,
3273 PerlIOBase_setlinebuf,
3274 NULL, /* get_base */
3275 NULL, /* get_bufsiz */
3278 NULL, /* set_ptrcnt */
3281 /*--------------------------------------------------------------------------------------*/
3286 #if defined(VMS) && !defined(STDIO_BUFFER_WRITABLE)
3287 /* perl5.8 - This ensures the last minute VMS ungetc fix is not
3288 broken by the last second glibc 2.3 fix
3290 # define STDIO_BUFFER_WRITABLE
3295 struct _PerlIO base;
3296 FILE *stdio; /* The stream */
3300 PerlIOStdio_fileno(pTHX_ PerlIO *f)
3302 PERL_UNUSED_CONTEXT;
3304 if (PerlIOValid(f)) {
3305 FILE * const s = PerlIOSelf(f, PerlIOStdio)->stdio;
3307 return PerlSIO_fileno(s);
3314 PerlIOStdio_mode(const char *mode, char *tmode)
3316 char * const ret = tmode;
3322 #if defined(PERLIO_USING_CRLF) || defined(__CYGWIN__)
3330 PerlIOStdio_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
3333 if (PerlIOValid(f) && PerlIOValid(n = PerlIONext(f))) {
3334 PerlIO_funcs * const toptab = PerlIOBase(n)->tab;
3335 if (toptab == tab) {
3336 /* Top is already stdio - pop self (duplicate) and use original */
3337 PerlIO_pop(aTHX_ f);
3340 const int fd = PerlIO_fileno(n);
3343 if (fd >= 0 && (stdio = PerlSIO_fdopen(fd,
3344 mode = PerlIOStdio_mode(mode, tmode)))) {
3345 PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
3346 /* We never call down so do any pending stuff now */
3347 PerlIO_flush(PerlIONext(f));
3348 return PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
3355 return PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
3360 PerlIO_importFILE(FILE *stdio, const char *mode)
3366 char filename[FILENAME_MAX];
3371 int fd0 = fileno(stdio);
3374 rc = fldata(stdio,filename,&fileinfo);
3378 if(fileinfo.__dsorgHFS){
3381 /*This MVS dataset , OK!*/
3386 if (!mode || !*mode) {
3387 /* We need to probe to see how we can open the stream
3388 so start with read/write and then try write and read
3389 we dup() so that we can fclose without loosing the fd.
3391 Note that the errno value set by a failing fdopen
3392 varies between stdio implementations.
3394 const int fd = PerlLIO_dup_cloexec(fd0);
3399 f2 = PerlSIO_fdopen(fd, (mode = "r+"));
3401 f2 = PerlSIO_fdopen(fd, (mode = "w"));
3404 f2 = PerlSIO_fdopen(fd, (mode = "r"));
3407 /* Don't seem to be able to open */
3413 if ((f = PerlIO_push(aTHX_(PerlIO_allocate(aTHX)), PERLIO_FUNCS_CAST(&PerlIO_stdio), mode, NULL))) {
3414 s = PerlIOSelf(f, PerlIOStdio);
3416 fd0 = fileno(stdio);
3418 PerlIOUnix_refcnt_inc(fd0);
3419 setfd_cloexec_or_inhexec_by_sysfdness(fd0);
3423 rc = fldata(stdio,filename,&fileinfo);
3425 PerlIOUnix_refcnt_inc(fd0);
3427 if(fileinfo.__dsorgHFS){
3428 PerlIOUnix_refcnt_inc(fd0);
3430 /*This MVS dataset , OK!*/
3439 PerlIOStdio_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
3440 IV n, const char *mode, int fd, int imode,
3441 int perm, PerlIO *f, int narg, SV **args)
3444 if (PerlIOValid(f)) {
3446 const char * const path = SvPV_const(*args, len);
3447 PerlIOStdio * const s = PerlIOSelf(f, PerlIOStdio);
3449 if (!IS_SAFE_PATHNAME(path, len, "open"))
3451 PerlIOUnix_refcnt_dec(fileno(s->stdio));
3452 stdio = PerlSIO_freopen(path, PerlIOStdio_mode(mode, tmode),
3458 PerlIOUnix_refcnt_inc(fd);
3459 setfd_cloexec_or_inhexec_by_sysfdness(fd);
3465 const char * const path = SvPV_const(*args, len);
3466 if (!IS_SAFE_PATHNAME(path, len, "open"))
3468 if (*mode == IoTYPE_NUMERIC) {
3470 fd = PerlLIO_open3_cloexec(path, imode, perm);
3474 bool appended = FALSE;
3476 /* Cygwin wants its 'b' early. */
3478 mode = PerlIOStdio_mode(mode, tmode);
3480 stdio = PerlSIO_fopen(path, mode);
3483 f = PerlIO_allocate(aTHX);
3486 mode = PerlIOStdio_mode(mode, tmode);
3487 f = PerlIO_push(aTHX_ f, self, mode, PerlIOArg);
3489 PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
3491 PerlIOUnix_refcnt_inc(fd);
3492 setfd_cloexec_or_inhexec_by_sysfdness(fd);
3494 PerlSIO_fclose(stdio);
3506 if (*mode == IoTYPE_IMPLICIT) {
3513 stdio = PerlSIO_stdin;
3516 stdio = PerlSIO_stdout;
3519 stdio = PerlSIO_stderr;
3524 stdio = PerlSIO_fdopen(fd, mode =
3525 PerlIOStdio_mode(mode, tmode));
3529 f = PerlIO_allocate(aTHX);
3531 if ((f = PerlIO_push(aTHX_ f, self, mode, PerlIOArg))) {
3532 PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
3534 PerlIOUnix_refcnt_inc(fd);
3535 setfd_cloexec_or_inhexec_by_sysfdness(fd);
3546 PerlIOStdio_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
3548 /* This assumes no layers underneath - which is what
3549 happens, but is not how I remember it. NI-S 2001/10/16
3551 if ((f = PerlIOBase_dup(aTHX_ f, o, param, flags))) {
3552 FILE *stdio = PerlIOSelf(o, PerlIOStdio)->stdio;
3553 const int fd = fileno(stdio);
3555 if (flags & PERLIO_DUP_FD) {
3556 const int dfd = PerlLIO_dup_cloexec(fileno(stdio));
3558 stdio = PerlSIO_fdopen(dfd, PerlIO_modestr(o,mode));
3563 /* FIXME: To avoid messy error recovery if dup fails
3564 re-use the existing stdio as though flag was not set
3568 stdio = PerlSIO_fdopen(fd, PerlIO_modestr(o,mode));
3570 PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
3572 int fd = fileno(stdio);
3573 PerlIOUnix_refcnt_inc(fd);
3574 setfd_cloexec_or_inhexec_by_sysfdness(fd);
3581 PerlIOStdio_invalidate_fileno(pTHX_ FILE *f)
3583 PERL_UNUSED_CONTEXT;
3585 /* XXX this could use PerlIO_canset_fileno() and
3586 * PerlIO_set_fileno() support from Configure
3588 #if defined(HAS_FDCLOSE)
3589 return fdclose(f, NULL) == 0 ? 1 : 0;
3590 #elif defined(__UCLIBC__)
3591 /* uClibc must come before glibc because it defines __GLIBC__ as well. */
3594 #elif defined(__GLIBC__)
3595 /* There may be a better way for GLIBC:
3596 - libio.h defines a flag to not close() on cleanup
3600 #elif defined(__sun)
3603 #elif defined(__hpux)
3607 /* Next one ->_file seems to be a reasonable fallback, i.e. if
3608 your platform does not have special entry try this one.
3609 [For OSF only have confirmation for Tru64 (alpha)
3610 but assume other OSFs will be similar.]
3612 #elif defined(_AIX) || defined(__osf__) || defined(__irix__)
3615 #elif defined(__FreeBSD__)
3616 /* There may be a better way on FreeBSD:
3617 - we could insert a dummy func in the _close function entry
3618 f->_close = (int (*)(void *)) dummy_close;
3622 #elif defined(__OpenBSD__)
3623 /* There may be a better way on OpenBSD:
3624 - we could insert a dummy func in the _close function entry
3625 f->_close = (int (*)(void *)) dummy_close;
3629 #elif defined(__EMX__)
3630 /* f->_flags &= ~_IOOPEN; */ /* Will leak stream->_buffer */
3633 #elif defined(__CYGWIN__)
3634 /* There may be a better way on CYGWIN:
3635 - we could insert a dummy func in the _close function entry
3636 f->_close = (int (*)(void *)) dummy_close;
3640 #elif defined(WIN32)
3641 PERLIO_FILE_file(f) = -1;
3645 /* Sarathy's code did this - we fall back to a dup/dup2 hack
3646 (which isn't thread safe) instead
3648 # error "Don't know how to set FILE.fileno on your platform"
3656 PerlIOStdio_close(pTHX_ PerlIO *f)
3658 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3664 const int fd = fileno(stdio);
3669 #ifdef SOCKS5_VERSION_NAME
3670 /* Socks lib overrides close() but stdio isn't linked to
3671 that library (though we are) - so we must call close()
3672 on sockets on stdio's behalf.
3675 Sock_size_t optlen = sizeof(int);
3676 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, (void *) &optval, &optlen) == 0)
3679 /* Test for -1, as *BSD stdio (at least) on fclose sets the FILE* such
3680 that a subsequent fileno() on it returns -1. Don't want to croak()
3681 from within PerlIOUnix_refcnt_dec() if some buggy caller code is
3682 trying to close an already closed handle which somehow it still has
3683 a reference to. (via.xs, I'm looking at you). */
3684 if (fd != -1 && PerlIOUnix_refcnt_dec(fd) > 0) {
3685 /* File descriptor still in use */
3689 /* For STD* handles, don't close stdio, since we shared the FILE *, too. */
3690 if (stdio == stdin) /* Some stdios are buggy fflush-ing inputs */
3692 if (stdio == stdout || stdio == stderr)
3693 return PerlIO_flush(f);
3695 MUTEX_LOCK(&PL_perlio_mutex);
3696 /* Right. We need a mutex here because for a brief while we
3697 will have the situation that fd is actually closed. Hence if
3698 a second thread were to get into this block, its dup() would
3699 likely return our fd as its dupfd. (after all, it is closed)
3700 Then if we get to the dup2() first, we blat the fd back
3701 (messing up its temporary as a side effect) only for it to
3702 then close its dupfd (== our fd) in its close(dupfd) */
3704 /* There is, of course, a race condition, that any other thread
3705 trying to input/output/whatever on this fd will be stuffed
3706 for the duration of this little manoeuvrer. Perhaps we
3707 should hold an IO mutex for the duration of every IO
3708 operation if we know that invalidate doesn't work on this
3709 platform, but that would suck, and could kill performance.
3711 Except that correctness trumps speed.
3712 Advice from klortho #11912. */
3714 /* Tricky - must fclose(stdio) to free memory but not close(fd)
3715 Use Sarathy's trick from maint-5.6 to invalidate the
3716 fileno slot of the FILE *
3718 result = PerlIO_flush(f);
3720 invalidate = PerlIOStdio_invalidate_fileno(aTHX_ stdio);
3722 dupfd = PerlLIO_dup_cloexec(fd);
3725 /* Oh cXap. This isn't going to go well. Not sure if we can
3726 recover from here, or if closing this particular FILE *
3727 is a good idea now. */
3732 SAVE_ERRNO; /* This is here only to silence compiler warnings */
3734 result = PerlSIO_fclose(stdio);
3735 /* We treat error from stdio as success if we invalidated
3736 errno may NOT be expected EBADF
3738 if (invalidate && result != 0) {
3742 #ifdef SOCKS5_VERSION_NAME
3743 /* in SOCKS' case, let close() determine return value */
3747 PerlLIO_dup2_cloexec(dupfd, fd);
3748 setfd_inhexec_for_sysfd(fd);
3749 PerlLIO_close(dupfd);
3751 MUTEX_UNLOCK(&PL_perlio_mutex);
3757 PerlIOStdio_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
3761 if (PerlIO_lockcnt(f)) /* in use: abort ungracefully */
3763 s = PerlIOSelf(f, PerlIOStdio)->stdio;
3766 STDCHAR *buf = (STDCHAR *) vbuf;
3768 * Perl is expecting PerlIO_getc() to fill the buffer Linux's
3769 * stdio does not do that for fread()
3771 const int ch = PerlSIO_fgetc(s);
3778 got = PerlSIO_fread(vbuf, 1, count, s);
3779 if (got == 0 && PerlSIO_ferror(s))
3781 if (got >= 0 || errno != EINTR)
3783 if (PL_sig_pending && S_perlio_async_run(aTHX_ f))
3785 SETERRNO(0,0); /* just in case */
3788 /* Under some circumstances IRIX stdio fgetc() and fread()
3789 * set the errno to ENOENT, which makes no sense according
3790 * to either IRIX or POSIX. [rt.perl.org #123977] */
3791 if (errno == ENOENT) SETERRNO(0,0);
3797 PerlIOStdio_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3800 FILE * const s = PerlIOSelf(f, PerlIOStdio)->stdio;
3802 #ifdef STDIO_BUFFER_WRITABLE
3803 if (PerlIO_fast_gets(f) && PerlIO_has_base(f)) {
3804 STDCHAR *buf = ((STDCHAR *) vbuf) + count;
3805 STDCHAR *base = PerlIO_get_base(f);
3806 SSize_t cnt = PerlIO_get_cnt(f);
3807 STDCHAR *ptr = PerlIO_get_ptr(f);
3808 SSize_t avail = ptr - base;
3810 if (avail > count) {
3814 Move(buf-avail,ptr,avail,STDCHAR);
3817 PerlIO_set_ptrcnt(f,ptr,cnt+avail);
3818 if (PerlSIO_feof(s) && unread >= 0)
3819 PerlSIO_clearerr(s);
3824 if (PerlIO_has_cntptr(f)) {
3825 /* We can get pointer to buffer but not its base
3826 Do ungetc() but check chars are ending up in the
3829 STDCHAR *eptr = (STDCHAR*)PerlSIO_get_ptr(s);
3830 STDCHAR *buf = ((STDCHAR *) vbuf) + count;
3832 const int ch = (U8) *--buf;
3833 if (ungetc(ch,s) != ch) {
3834 /* ungetc did not work */
3837 if ((STDCHAR*)PerlSIO_get_ptr(s) != --eptr || (((U8) *eptr) != ch)) {
3838 /* Did not change pointer as expected */
3839 if (fgetc(s) != EOF) /* get char back again */
3849 unread += PerlIOBase_unread(aTHX_ f, vbuf, count);
3855 PerlIOStdio_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
3858 if (PerlIO_lockcnt(f)) /* in use: abort ungracefully */
3861 got = PerlSIO_fwrite(vbuf, 1, count,
3862 PerlIOSelf(f, PerlIOStdio)->stdio);
3863 if (got >= 0 || errno != EINTR)
3865 if (PL_sig_pending && S_perlio_async_run(aTHX_ f))
3867 SETERRNO(0,0); /* just in case */
3873 PerlIOStdio_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
3875 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3876 PERL_UNUSED_CONTEXT;
3878 return PerlSIO_fseek(stdio, offset, whence);
3882 PerlIOStdio_tell(pTHX_ PerlIO *f)
3884 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3885 PERL_UNUSED_CONTEXT;
3887 return PerlSIO_ftell(stdio);
3891 PerlIOStdio_flush(pTHX_ PerlIO *f)
3893 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3894 PERL_UNUSED_CONTEXT;
3896 if (PerlIOBase(f)->flags & PERLIO_F_CANWRITE) {
3897 return PerlSIO_fflush(stdio);
3903 * FIXME: This discards ungetc() and pre-read stuff which is not
3904 * right if this is just a "sync" from a layer above Suspect right
3905 * design is to do _this_ but not have layer above flush this
3906 * layer read-to-read
3909 * Not writeable - sync by attempting a seek
3912 if (PerlSIO_fseek(stdio, (Off_t) 0, SEEK_CUR) != 0)
3920 PerlIOStdio_eof(pTHX_ PerlIO *f)
3922 PERL_UNUSED_CONTEXT;
3924 return PerlSIO_feof(PerlIOSelf(f, PerlIOStdio)->stdio);
3928 PerlIOStdio_error(pTHX_ PerlIO *f)
3930 PERL_UNUSED_CONTEXT;
3932 return PerlSIO_ferror(PerlIOSelf(f, PerlIOStdio)->stdio);
3936 PerlIOStdio_clearerr(pTHX_ PerlIO *f)
3938 PERL_UNUSED_CONTEXT;
3940 PerlSIO_clearerr(PerlIOSelf(f, PerlIOStdio)->stdio);
3944 PerlIOStdio_setlinebuf(pTHX_ PerlIO *f)
3946 PERL_UNUSED_CONTEXT;
3948 #ifdef HAS_SETLINEBUF
3949 PerlSIO_setlinebuf(PerlIOSelf(f, PerlIOStdio)->stdio);
3951 PerlSIO_setvbuf(PerlIOSelf(f, PerlIOStdio)->stdio, NULL, _IOLBF, 0);
3957 PerlIOStdio_get_base(pTHX_ PerlIO *f)
3959 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3960 PERL_UNUSED_CONTEXT;
3961 return (STDCHAR*)PerlSIO_get_base(stdio);
3965 PerlIOStdio_get_bufsiz(pTHX_ PerlIO *f)
3967 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3968 PERL_UNUSED_CONTEXT;
3969 return PerlSIO_get_bufsiz(stdio);
3973 #ifdef USE_STDIO_PTR
3975 PerlIOStdio_get_ptr(pTHX_ PerlIO *f)
3977 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3978 PERL_UNUSED_CONTEXT;
3979 return (STDCHAR*)PerlSIO_get_ptr(stdio);
3983 PerlIOStdio_get_cnt(pTHX_ PerlIO *f)
3985 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3986 PERL_UNUSED_CONTEXT;
3987 return PerlSIO_get_cnt(stdio);
3991 PerlIOStdio_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
3993 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3994 PERL_UNUSED_CONTEXT;
3996 # ifdef STDIO_PTR_LVALUE
3997 /* This is a long-standing infamous mess. The root of the
3998 * problem is that one cannot know the signedness of char, and
3999 * more precisely the signedness of FILE._ptr. The following
4000 * things have been tried, and they have all failed (across
4001 * different compilers (remember that core needs to build
4002 * also with c++) and compiler options:
4004 * - casting the RHS to (void*) -- works in *some* places
4005 * - casting the LHS to (void*) -- totally unportable
4007 * So let's try silencing the warning at least for gcc. */
4008 GCC_DIAG_IGNORE_STMT(-Wpointer-sign);
4009 PerlSIO_set_ptr(stdio, ptr); /* LHS STDCHAR* cast non-portable */
4010 GCC_DIAG_RESTORE_STMT;
4011 # ifdef STDIO_PTR_LVAL_SETS_CNT
4012 assert(PerlSIO_get_cnt(stdio) == (cnt));
4014 # if (!defined(STDIO_PTR_LVAL_NOCHANGE_CNT))
4016 * Setting ptr _does_ change cnt - we are done
4020 # else /* STDIO_PTR_LVALUE */
4022 # endif /* STDIO_PTR_LVALUE */
4025 * Now (or only) set cnt
4027 # ifdef STDIO_CNT_LVALUE
4028 PerlSIO_set_cnt(stdio, cnt);
4029 # elif (defined(STDIO_PTR_LVALUE) && defined(STDIO_PTR_LVAL_SETS_CNT))
4030 PerlSIO_set_ptr(stdio,
4031 PerlSIO_get_ptr(stdio) + (PerlSIO_get_cnt(stdio) -
4033 # else /* STDIO_PTR_LVAL_SETS_CNT */
4035 # endif /* STDIO_CNT_LVALUE */
4042 PerlIOStdio_fill(pTHX_ PerlIO *f)
4046 PERL_UNUSED_CONTEXT;
4047 if (PerlIO_lockcnt(f)) /* in use: abort ungracefully */
4049 stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
4052 * fflush()ing read-only streams can cause trouble on some stdio-s
4054 if ((PerlIOBase(f)->flags & PERLIO_F_CANWRITE)) {
4055 if (PerlSIO_fflush(stdio) != 0)
4059 c = PerlSIO_fgetc(stdio);
4062 if (! PerlSIO_ferror(stdio) || errno != EINTR)
4064 if (PL_sig_pending && S_perlio_async_run(aTHX_ f))
4069 #if (defined(STDIO_PTR_LVALUE) && (defined(STDIO_CNT_LVALUE) || defined(STDIO_PTR_LVAL_SETS_CNT)))
4071 # ifdef STDIO_BUFFER_WRITABLE
4072 if (PerlIO_fast_gets(f) && PerlIO_has_base(f)) {
4073 /* Fake ungetc() to the real buffer in case system's ungetc
4076 STDCHAR *base = (STDCHAR*)PerlSIO_get_base(stdio);
4077 SSize_t cnt = PerlSIO_get_cnt(stdio);
4078 STDCHAR *ptr = (STDCHAR*)PerlSIO_get_ptr(stdio);
4079 if (ptr == base+1) {
4080 *--ptr = (STDCHAR) c;
4081 PerlIOStdio_set_ptrcnt(aTHX_ f,ptr,cnt+1);
4082 if (PerlSIO_feof(stdio))
4083 PerlSIO_clearerr(stdio);
4089 if (PerlIO_has_cntptr(f)) {
4091 if (PerlIOStdio_unread(aTHX_ f,&ch,1) == 1) {
4097 /* If buffer snoop scheme above fails fall back to
4100 if (PerlSIO_ungetc(c, stdio) != c)
4108 PERLIO_FUNCS_DECL(PerlIO_stdio) = {
4109 sizeof(PerlIO_funcs),
4111 sizeof(PerlIOStdio),
4112 PERLIO_K_BUFFERED|PERLIO_K_RAW,
4116 PerlIOBase_binmode, /* binmode */
4130 PerlIOStdio_clearerr,
4131 PerlIOStdio_setlinebuf,
4133 PerlIOStdio_get_base,
4134 PerlIOStdio_get_bufsiz,
4139 #ifdef USE_STDIO_PTR
4140 PerlIOStdio_get_ptr,
4141 PerlIOStdio_get_cnt,
4142 # if defined(HAS_FAST_STDIO) && defined(USE_FAST_STDIO)
4143 PerlIOStdio_set_ptrcnt,
4146 # endif /* HAS_FAST_STDIO && USE_FAST_STDIO */
4151 #endif /* USE_STDIO_PTR */
4154 /* Note that calls to PerlIO_exportFILE() are reversed using
4155 * PerlIO_releaseFILE(), not importFILE. */
4157 PerlIO_exportFILE(PerlIO * f, const char *mode)
4161 if (PerlIOValid(f)) {
4163 int fd = PerlIO_fileno(f);
4168 if (!mode || !*mode) {
4169 mode = PerlIO_modestr(f, buf);
4171 stdio = PerlSIO_fdopen(PerlIO_fileno(f), mode);
4175 /* De-link any lower layers so new :stdio sticks */
4177 if ((f2 = PerlIO_push(aTHX_ f, PERLIO_FUNCS_CAST(&PerlIO_stdio), buf, NULL))) {
4178 PerlIOStdio *s = PerlIOSelf((f = f2), PerlIOStdio);
4180 PerlIOUnix_refcnt_inc(fileno(stdio));
4181 /* Link previous lower layers under new one */
4185 /* restore layers list */
4195 PerlIO_findFILE(PerlIO *f)
4200 if (l->tab == &PerlIO_stdio) {
4201 PerlIOStdio *s = PerlIOSelf(&l, PerlIOStdio);
4204 l = *PerlIONext(&l);
4206 /* Uses fallback "mode" via PerlIO_modestr() in PerlIO_exportFILE */
4207 /* However, we're not really exporting a FILE * to someone else (who
4208 becomes responsible for closing it, or calling PerlIO_releaseFILE())
4209 So we need to undo its reference count increase on the underlying file
4210 descriptor. We have to do this, because if the loop above returns you
4211 the FILE *, then *it* didn't increase any reference count. So there's
4212 only one way to be consistent. */
4213 stdio = PerlIO_exportFILE(f, NULL);
4215 const int fd = fileno(stdio);
4217 PerlIOUnix_refcnt_dec(fd);
4222 /* Use this to reverse PerlIO_exportFILE calls. */
4224 PerlIO_releaseFILE(PerlIO *p, FILE *f)
4228 if (l->tab == &PerlIO_stdio) {
4229 PerlIOStdio *s = PerlIOSelf(&l, PerlIOStdio);
4230 if (s->stdio == f) { /* not in a loop */
4231 const int fd = fileno(f);
4233 PerlIOUnix_refcnt_dec(fd);
4236 PerlIO_pop(aTHX_ p);
4246 /*--------------------------------------------------------------------------------------*/
4248 * perlio buffer layer
4252 PerlIOBuf_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
4254 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
4255 const int fd = PerlIO_fileno(f);
4256 if (fd >= 0 && PerlLIO_isatty(fd)) {
4257 PerlIOBase(f)->flags |= PERLIO_F_LINEBUF | PERLIO_F_TTY;
4259 if (*PerlIONext(f)) {
4260 const Off_t posn = PerlIO_tell(PerlIONext(f));
4261 if (posn != (Off_t) - 1) {
4265 return PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
4269 PerlIOBuf_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
4270 IV n, const char *mode, int fd, int imode, int perm,
4271 PerlIO *f, int narg, SV **args)
4273 if (PerlIOValid(f)) {
4274 PerlIO *next = PerlIONext(f);
4276 PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIOBase(next)->tab);
4277 if (tab && tab->Open)
4279 (*tab->Open)(aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
4281 if (!next || (*PerlIOBase(f)->tab->Pushed) (aTHX_ f, mode, PerlIOArg, self) != 0) {
4286 PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIO_default_btm());
4288 if (*mode == IoTYPE_IMPLICIT) {
4294 if (tab && tab->Open)
4295 f = (*tab->Open)(aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
4298 SETERRNO(EINVAL, LIB_INVARG);
4300 if (PerlIO_push(aTHX_ f, self, mode, PerlIOArg) == 0) {
4302 * if push fails during open, open fails. close will pop us.
4307 fd = PerlIO_fileno(f);
4308 if (init && fd == 2) {
4310 * Initial stderr is unbuffered
4312 PerlIOBase(f)->flags |= PERLIO_F_UNBUF;
4314 #ifdef PERLIO_USING_CRLF
4315 # ifdef PERLIO_IS_BINMODE_FD
4316 if (PERLIO_IS_BINMODE_FD(fd))
4317 PerlIO_binmode(aTHX_ f, '<'/*not used*/, O_BINARY, NULL);
4321 * do something about failing setmode()? --jhi
4323 PerlLIO_setmode(fd, O_BINARY);
4326 /* Enable line buffering with record-oriented regular files
4327 * so we don't introduce an extraneous record boundary when
4328 * the buffer fills up.
4330 if (PerlIOBase(f)->flags & PERLIO_F_CANWRITE) {
4332 if (PerlLIO_fstat(fd, &st) == 0
4333 && S_ISREG(st.st_mode)
4334 && (st.st_fab_rfm == FAB$C_VAR
4335 || st.st_fab_rfm == FAB$C_VFC)) {
4336 PerlIOBase(f)->flags |= PERLIO_F_LINEBUF;
4347 * This "flush" is akin to sfio's sync in that it handles files in either
4348 * read or write state. For write state, we put the postponed data through
4349 * the next layers. For read state, we seek() the next layers to the
4350 * offset given by current position in the buffer, and discard the buffer
4351 * state (XXXX supposed to be for seek()able buffers only, but now it is done
4352 * in any case?). Then the pass the stick further in chain.
4355 PerlIOBuf_flush(pTHX_ PerlIO *f)
4357 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
4359 PerlIO *n = PerlIONext(f);
4360 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF) {
4362 * write() the buffer
4364 const STDCHAR *buf = b->buf;
4365 const STDCHAR *p = buf;
4366 while (p < b->ptr) {
4367 SSize_t count = PerlIO_write(n, p, b->ptr - p);
4371 else if (count < 0 || PerlIO_error(n)) {
4372 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
4373 PerlIO_save_errno(f);
4378 b->posn += (p - buf);
4380 else if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
4381 STDCHAR *buf = PerlIO_get_base(f);
4383 * Note position change
4385 b->posn += (b->ptr - buf);
4386 if (b->ptr < b->end) {
4387 /* We did not consume all of it - try and seek downstream to
4388 our logical position
4390 if (PerlIOValid(n) && PerlIO_seek(n, b->posn, SEEK_SET) == 0) {
4391 /* Reload n as some layers may pop themselves on seek */
4392 b->posn = PerlIO_tell(n = PerlIONext(f));
4395 /* Seek failed (e.g. pipe or tty). Do NOT clear buffer or pre-read
4396 data is lost for good - so return saying "ok" having undone
4399 b->posn -= (b->ptr - buf);
4404 b->ptr = b->end = b->buf;
4405 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
4406 /* We check for Valid because of dubious decision to make PerlIO_flush(NULL) flush all */
4407 if (PerlIOValid(n) && PerlIO_flush(n) != 0)
4412 /* This discards the content of the buffer after b->ptr, and rereads
4413 * the buffer from the position off in the layer downstream; here off
4414 * is at offset corresponding to b->ptr - b->buf.
4417 PerlIOBuf_fill(pTHX_ PerlIO *f)
4419 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
4420 PerlIO *n = PerlIONext(f);
4423 * Down-stream flush is defined not to loose read data so is harmless.
4424 * we would not normally be fill'ing if there was data left in anycase.
4426 if (PerlIO_flush(f) != 0) /* XXXX Check that its seek() succeeded?! */
4428 if (PerlIOBase(f)->flags & PERLIO_F_TTY)
4429 PerlIOBase_flush_linebuf(aTHX);
4432 PerlIO_get_base(f); /* allocate via vtable */
4434 assert(b->buf); /* The b->buf does get allocated via the vtable system. */
4436 b->ptr = b->end = b->buf;
4438 if (!PerlIOValid(n)) {
4439 PerlIOBase(f)->flags |= PERLIO_F_EOF;
4443 if (PerlIO_fast_gets(n)) {
4445 * Layer below is also buffered. We do _NOT_ want to call its
4446 * ->Read() because that will loop till it gets what we asked for
4447 * which may hang on a pipe etc. Instead take anything it has to
4448 * hand, or ask it to fill _once_.
4450 avail = PerlIO_get_cnt(n);
4452 avail = PerlIO_fill(n);
4454 avail = PerlIO_get_cnt(n);
4456 if (!PerlIO_error(n) && PerlIO_eof(n))
4461 STDCHAR *ptr = PerlIO_get_ptr(n);
4462 const SSize_t cnt = avail;
4463 if (avail > (SSize_t)b->bufsiz)
4465 Copy(ptr, b->buf, avail, STDCHAR);
4466 PerlIO_set_ptrcnt(n, ptr + avail, cnt - avail);
4470 avail = PerlIO_read(n, b->ptr, b->bufsiz);
4474 PerlIOBase(f)->flags |= PERLIO_F_EOF;
4477 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
4478 PerlIO_save_errno(f);
4482 b->end = b->buf + avail;
4483 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
4488 PerlIOBuf_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
4490 if (PerlIOValid(f)) {
4491 const PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
4494 return PerlIOBase_read(aTHX_ f, vbuf, count);
4500 PerlIOBuf_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
4502 const STDCHAR *buf = (const STDCHAR *) vbuf + count;
4503 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
4506 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
4511 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
4513 * Buffer is already a read buffer, we can overwrite any chars
4514 * which have been read back to buffer start
4516 avail = (b->ptr - b->buf);
4520 * Buffer is idle, set it up so whole buffer is available for
4524 b->end = b->buf + avail;
4526 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
4528 * Buffer extends _back_ from where we are now
4530 b->posn -= b->bufsiz;
4532 if ((SSize_t) count >= 0 && avail > (SSize_t) count) {
4534 * If we have space for more than count, just move count
4542 * In simple stdio-like ungetc() case chars will be already
4545 if (buf != b->ptr) {
4546 Copy(buf, b->ptr, avail, STDCHAR);
4550 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
4554 unread += PerlIOBase_unread(aTHX_ f, vbuf, count);
4560 PerlIOBuf_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
4562 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
4563 const STDCHAR *buf = (const STDCHAR *) vbuf;
4564 const STDCHAR *flushptr = buf;
4568 if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
4570 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
4571 if (PerlIO_flush(f) != 0) {
4575 if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF) {
4576 flushptr = buf + count;
4577 while (flushptr > buf && *(flushptr - 1) != '\n')
4581 SSize_t avail = b->bufsiz - (b->ptr - b->buf);
4582 if ((SSize_t) count >= 0 && (SSize_t) count < avail)
4584 if (flushptr > buf && flushptr <= buf + avail)
4585 avail = flushptr - buf;
4586 PerlIOBase(f)->flags |= PERLIO_F_WRBUF;
4588 Copy(buf, b->ptr, avail, STDCHAR);
4593 if (buf == flushptr)
4596 if (b->ptr >= (b->buf + b->bufsiz))
4597 if (PerlIO_flush(f) == -1)
4600 if (PerlIOBase(f)->flags & PERLIO_F_UNBUF)
4606 PerlIOBuf_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
4609 if ((code = PerlIO_flush(f)) == 0) {
4610 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
4611 code = PerlIO_seek(PerlIONext(f), offset, whence);
4613 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
4614 b->posn = PerlIO_tell(PerlIONext(f));
4621 PerlIOBuf_tell(pTHX_ PerlIO *f)
4623 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
4625 * b->posn is file position where b->buf was read, or will be written
4627 Off_t posn = b->posn;
4628 if ((PerlIOBase(f)->flags & PERLIO_F_APPEND) &&
4629 (PerlIOBase(f)->flags & PERLIO_F_WRBUF)) {
4631 /* As O_APPEND files are normally shared in some sense it is better
4636 /* when file is NOT shared then this is sufficient */
4637 PerlIO_seek(PerlIONext(f),0, SEEK_END);
4639 posn = b->posn = PerlIO_tell(PerlIONext(f));
4643 * If buffer is valid adjust position by amount in buffer
4645 posn += (b->ptr - b->buf);
4651 PerlIOBuf_popped(pTHX_ PerlIO *f)
4653 const IV code = PerlIOBase_popped(aTHX_ f);
4654 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
4655 if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
4658 b->ptr = b->end = b->buf = NULL;
4659 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
4664 PerlIOBuf_close(pTHX_ PerlIO *f)
4666 const IV code = PerlIOBase_close(aTHX_ f);
4667 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
4668 if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
4671 b->ptr = b->end = b->buf = NULL;
4672 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
4677 PerlIOBuf_get_ptr(pTHX_ PerlIO *f)
4679 const PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
4686 PerlIOBuf_get_cnt(pTHX_ PerlIO *f)
4688 const PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
4691 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF)
4692 return (b->end - b->ptr);
4697 PerlIOBuf_get_base(pTHX_ PerlIO *f)
4699 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
4700 PERL_UNUSED_CONTEXT;
4704 b->bufsiz = PERLIOBUF_DEFAULT_BUFSIZ;
4705 Newx(b->buf,b->bufsiz, STDCHAR);
4707 b->buf = (STDCHAR *) & b->oneword;
4708 b->bufsiz = sizeof(b->oneword);
4710 b->end = b->ptr = b->buf;
4716 PerlIOBuf_bufsiz(pTHX_ PerlIO *f)
4718 const PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
4721 return (b->end - b->buf);
4725 PerlIOBuf_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
4727 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
4729 PERL_UNUSED_ARG(cnt);
4734 assert(PerlIO_get_cnt(f) == cnt);
4735 assert(b->ptr >= b->buf);
4736 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
4740 PerlIOBuf_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
4742 return PerlIOBase_dup(aTHX_ f, o, param, flags);
4747 PERLIO_FUNCS_DECL(PerlIO_perlio) = {
4748 sizeof(PerlIO_funcs),
4751 PERLIO_K_BUFFERED|PERLIO_K_RAW,
4755 PerlIOBase_binmode, /* binmode */
4769 PerlIOBase_clearerr,
4770 PerlIOBase_setlinebuf,
4775 PerlIOBuf_set_ptrcnt,
4778 /*--------------------------------------------------------------------------------------*/
4780 * Temp layer to hold unread chars when cannot do it any other way
4784 PerlIOPending_fill(pTHX_ PerlIO *f)
4787 * Should never happen
4794 PerlIOPending_close(pTHX_ PerlIO *f)
4797 * A tad tricky - flush pops us, then we close new top
4800 return PerlIO_close(f);
4804 PerlIOPending_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
4807 * A tad tricky - flush pops us, then we seek new top
4810 return PerlIO_seek(f, offset, whence);
4815 PerlIOPending_flush(pTHX_ PerlIO *f)
4817 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
4818 if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
4822 PerlIO_pop(aTHX_ f);
4827 PerlIOPending_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
4833 PerlIOBuf_set_ptrcnt(aTHX_ f, ptr, cnt);
4838 PerlIOPending_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
4840 const IV code = PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
4841 PerlIOl * const l = PerlIOBase(f);
4843 * Our PerlIO_fast_gets must match what we are pushed on, or sv_gets()
4844 * etc. get muddled when it changes mid-string when we auto-pop.
4846 l->flags = (l->flags & ~(PERLIO_F_FASTGETS | PERLIO_F_UTF8)) |
4847 (PerlIOBase(PerlIONext(f))->
4848 flags & (PERLIO_F_FASTGETS | PERLIO_F_UTF8));
4853 PerlIOPending_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
4855 SSize_t avail = PerlIO_get_cnt(f);
4857 if ((SSize_t) count >= 0 && (SSize_t)count < avail)
4860 got = PerlIOBuf_read(aTHX_ f, vbuf, avail);
4861 if (got >= 0 && got < (SSize_t)count) {
4862 const SSize_t more =
4863 PerlIO_read(f, ((STDCHAR *) vbuf) + got, count - got);
4864 if (more >= 0 || got == 0)
4870 PERLIO_FUNCS_DECL(PerlIO_pending) = {
4871 sizeof(PerlIO_funcs),
4874 PERLIO_K_BUFFERED|PERLIO_K_RAW, /* not sure about RAW here */
4875 PerlIOPending_pushed,
4878 PerlIOBase_binmode, /* binmode */
4887 PerlIOPending_close,
4888 PerlIOPending_flush,
4892 PerlIOBase_clearerr,
4893 PerlIOBase_setlinebuf,
4898 PerlIOPending_set_ptrcnt,
4903 /*--------------------------------------------------------------------------------------*/
4905 * crlf - translation On read translate CR,LF to "\n" we do this by
4906 * overriding ptr/cnt entries to hand back a line at a time and keeping a
4907 * record of which nl we "lied" about. On write translate "\n" to CR,LF
4909 * c->nl points on the first byte of CR LF pair when it is temporarily
4910 * replaced by LF, or to the last CR of the buffer. In the former case
4911 * the caller thinks that the buffer ends at c->nl + 1, in the latter
4912 * that it ends at c->nl; these two cases can be distinguished by
4913 * *c->nl. c->nl is set during _getcnt() call, and unset during
4914 * _unread() and _flush() calls.
4915 * It only matters for read operations.
4919 PerlIOBuf base; /* PerlIOBuf stuff */
4920 STDCHAR *nl; /* Position of crlf we "lied" about in the
4924 /* Inherit the PERLIO_F_UTF8 flag from previous layer.
4925 * Otherwise the :crlf layer would always revert back to
4929 S_inherit_utf8_flag(PerlIO *f)
4931 PerlIO *g = PerlIONext(f);
4932 if (PerlIOValid(g)) {
4933 if (PerlIOBase(g)->flags & PERLIO_F_UTF8) {
4934 PerlIOBase(f)->flags |= PERLIO_F_UTF8;
4940 PerlIOCrlf_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
4943 PerlIOBase(f)->flags |= PERLIO_F_CRLF;
4944 code = PerlIOBuf_pushed(aTHX_ f, mode, arg, tab);
4947 PerlIO_debug("PerlIOCrlf_pushed f=%p %s %s fl=%08" UVxf "\n",
4948 (void*)f, PerlIOBase(f)->tab->name, (mode) ? mode : "(Null)",
4949 PerlIOBase(f)->flags);
4953 /* If the old top layer is a CRLF layer, reactivate it (if
4954 * necessary) and remove this new layer from the stack */
4955 PerlIO *g = PerlIONext(f);
4956 if (PerlIOValid(g)) {
4957 PerlIOl *b = PerlIOBase(g);
4958 if (b && b->tab == &PerlIO_crlf) {
4959 if (!(b->flags & PERLIO_F_CRLF))
4960 b->flags |= PERLIO_F_CRLF;
4961 S_inherit_utf8_flag(g);
4962 PerlIO_pop(aTHX_ f);
4967 S_inherit_utf8_flag(f);
4973 PerlIOCrlf_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
4975 PerlIOCrlf * const c = PerlIOSelf(f, PerlIOCrlf);
4976 if (c->nl) { /* XXXX Shouldn't it be done only if b->ptr > c->nl? */
4977 *(c->nl) = NATIVE_0xd;
4980 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF))
4981 return PerlIOBuf_unread(aTHX_ f, vbuf, count);
4983 const STDCHAR *buf = (const STDCHAR *) vbuf + count;
4984 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
4986 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
4991 if (!(PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
4992 b->end = b->ptr = b->buf + b->bufsiz;
4993 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
4994 b->posn -= b->bufsiz;
4996 while (count > 0 && b->ptr > b->buf) {
4997 const int ch = *--buf;
4999 if (b->ptr - 2 >= b->buf) {
5000 *--(b->ptr) = NATIVE_0xa;
5001 *--(b->ptr) = NATIVE_0xd;
5006 /* If b->ptr - 1 == b->buf, we are undoing reading 0xa */
5007 *--(b->ptr) = NATIVE_0xa; /* Works even if 0xa ==
5021 unread += PerlIOBase_unread(aTHX_ f, (const STDCHAR *) vbuf + unread, count);
5026 /* XXXX This code assumes that buffer size >=2, but does not check it... */
5028 PerlIOCrlf_get_cnt(pTHX_ PerlIO *f)
5030 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
5033 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
5034 PerlIOCrlf * const c = PerlIOSelf(f, PerlIOCrlf);
5035 if ((PerlIOBase(f)->flags & PERLIO_F_CRLF) && (!c->nl || *c->nl == NATIVE_0xd)) {
5036 STDCHAR *nl = (c->nl) ? c->nl : b->ptr;
5038 while (nl < b->end && *nl != NATIVE_0xd)
5040 if (nl < b->end && *nl == NATIVE_0xd) {
5042 if (nl + 1 < b->end) {
5043 if (nl[1] == NATIVE_0xa) {
5049 * Not CR,LF but just CR
5057 * Blast - found CR as last char in buffer
5062 * They may not care, defer work as long as
5066 return (nl - b->ptr);
5070 b->ptr++; /* say we have read it as far as
5071 * flush() is concerned */
5072 b->buf++; /* Leave space in front of buffer */
5073 /* Note as we have moved buf up flush's
5075 will naturally make posn point at CR
5077 b->bufsiz--; /* Buffer is thus smaller */
5078 code = PerlIO_fill(f); /* Fetch some more */
5079 b->bufsiz++; /* Restore size for next time */
5080 b->buf--; /* Point at space */
5081 b->ptr = nl = b->buf; /* Which is what we hand
5083 *nl = NATIVE_0xd; /* Fill in the CR */
5085 goto test; /* fill() call worked */
5087 * CR at EOF - just fall through
5089 /* Should we clear EOF though ??? */
5094 return (((c->nl) ? (c->nl + 1) : b->end) - b->ptr);
5100 PerlIOCrlf_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
5102 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
5103 PerlIOCrlf * const c = PerlIOSelf(f, PerlIOCrlf);
5109 if (ptr == b->end && *c->nl == NATIVE_0xd) {
5110 /* Deferred CR at end of buffer case - we lied about count */
5123 * Test code - delete when it works ...
5125 IV flags = PerlIOBase(f)->flags;
5126 STDCHAR *chk = (c->nl) ? (c->nl+1) : b->end;
5127 if (ptr+cnt == c->nl && c->nl+1 == b->end && *c->nl == NATIVE_0xd) {
5128 /* Deferred CR at end of buffer case - we lied about count */
5134 Perl_croak(aTHX_ "ptr wrong %p != %p fl=%08" UVxf
5135 " nl=%p e=%p for %d", (void*)ptr, (void*)chk,
5136 flags, c->nl, b->end, cnt);
5143 * They have taken what we lied about
5145 *(c->nl) = NATIVE_0xd;
5151 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
5155 PerlIOCrlf_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
5157 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF))
5158 return PerlIOBuf_write(aTHX_ f, vbuf, count);
5160 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
5161 const STDCHAR *buf = (const STDCHAR *) vbuf;
5162 const STDCHAR * const ebuf = buf + count;
5165 if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
5167 while (buf < ebuf) {
5168 const STDCHAR * const eptr = b->buf + b->bufsiz;
5169 PerlIOBase(f)->flags |= PERLIO_F_WRBUF;
5170 while (buf < ebuf && b->ptr < eptr) {
5172 if ((b->ptr + 2) > eptr) {
5180 *(b->ptr)++ = NATIVE_0xd; /* CR */
5181 *(b->ptr)++ = NATIVE_0xa; /* LF */
5183 if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF) {
5190 *(b->ptr)++ = *buf++;
5192 if (b->ptr >= eptr) {
5198 if (PerlIOBase(f)->flags & PERLIO_F_UNBUF)
5200 return (buf - (STDCHAR *) vbuf);
5205 PerlIOCrlf_flush(pTHX_ PerlIO *f)
5207 PerlIOCrlf * const c = PerlIOSelf(f, PerlIOCrlf);
5209 *(c->nl) = NATIVE_0xd;
5212 return PerlIOBuf_flush(aTHX_ f);
5216 PerlIOCrlf_binmode(pTHX_ PerlIO *f)
5218 if ((PerlIOBase(f)->flags & PERLIO_F_CRLF)) {
5219 /* In text mode - flush any pending stuff and flip it */
5220 PerlIOBase(f)->flags &= ~PERLIO_F_CRLF;
5221 #ifndef PERLIO_USING_CRLF
5222 /* CRLF is unusual case - if this is just the :crlf layer pop it */
5223 PerlIO_pop(aTHX_ f);
5226 return PerlIOBase_binmode(aTHX_ f);
5229 PERLIO_FUNCS_DECL(PerlIO_crlf) = {
5230 sizeof(PerlIO_funcs),
5233 PERLIO_K_BUFFERED | PERLIO_K_CANCRLF | PERLIO_K_RAW,
5235 PerlIOBuf_popped, /* popped */
5237 PerlIOCrlf_binmode, /* binmode */
5241 PerlIOBuf_read, /* generic read works with ptr/cnt lies */
5242 PerlIOCrlf_unread, /* Put CR,LF in buffer for each '\n' */
5243 PerlIOCrlf_write, /* Put CR,LF in buffer for each '\n' */
5251 PerlIOBase_clearerr,
5252 PerlIOBase_setlinebuf,
5257 PerlIOCrlf_set_ptrcnt,
5261 Perl_PerlIO_stdin(pTHX)
5264 PerlIO_stdstreams(aTHX);
5266 return &PL_perlio[1].next;
5270 Perl_PerlIO_stdout(pTHX)
5273 PerlIO_stdstreams(aTHX);
5275 return &PL_perlio[2].next;
5279 Perl_PerlIO_stderr(pTHX)
5282 PerlIO_stdstreams(aTHX);
5284 return &PL_perlio[3].next;
5287 /*--------------------------------------------------------------------------------------*/
5290 PerlIO_getname(PerlIO *f, char *buf)
5295 bool exported = FALSE;
5296 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
5298 stdio = PerlIO_exportFILE(f,0);
5302 name = fgetname(stdio, buf);
5303 if (exported) PerlIO_releaseFILE(f,stdio);
5308 PERL_UNUSED_ARG(buf);
5309 Perl_croak_nocontext("Don't know how to get file name");
5315 /*--------------------------------------------------------------------------------------*/
5317 * Functions which can be called on any kind of PerlIO implemented in
5321 #undef PerlIO_fdopen
5323 PerlIO_fdopen(int fd, const char *mode)
5326 return PerlIO_openn(aTHX_ NULL, mode, fd, 0, 0, NULL, 0, NULL);
5331 PerlIO_open(const char *path, const char *mode)
5334 SV *name = newSVpvn_flags(path, path == NULL ? 0 : strlen(path), SVs_TEMP);
5335 return PerlIO_openn(aTHX_ NULL, mode, -1, 0, 0, NULL, 1, &name);
5338 #undef Perlio_reopen
5340 PerlIO_reopen(const char *path, const char *mode, PerlIO *f)
5343 SV *name = newSVpvn_flags(path, path == NULL ? 0 : strlen(path), SVs_TEMP);
5344 return PerlIO_openn(aTHX_ NULL, mode, -1, 0, 0, f, 1, &name);
5349 PerlIO_getc(PerlIO *f)
5353 if ( 1 == PerlIO_read(f, buf, 1) ) {
5354 return (unsigned char) buf[0];
5359 #undef PerlIO_ungetc
5361 PerlIO_ungetc(PerlIO *f, int ch)
5366 if (PerlIO_unread(f, &buf, 1) == 1)
5374 PerlIO_putc(PerlIO *f, int ch)
5378 return PerlIO_write(f, &buf, 1);
5383 PerlIO_puts(PerlIO *f, const char *s)
5386 return PerlIO_write(f, s, strlen(s));
5389 #undef PerlIO_rewind
5391 PerlIO_rewind(PerlIO *f)
5394 PerlIO_seek(f, (Off_t) 0, SEEK_SET);
5398 #undef PerlIO_vprintf
5400 PerlIO_vprintf(PerlIO *f, const char *fmt, va_list ap)
5409 Perl_va_copy(ap, apc);
5410 sv = vnewSVpvf(fmt, &apc);
5413 sv = vnewSVpvf(fmt, &ap);
5415 s = SvPV_const(sv, len);
5416 wrote = PerlIO_write(f, s, len);
5421 #undef PerlIO_printf
5423 PerlIO_printf(PerlIO *f, const char *fmt, ...)
5428 result = PerlIO_vprintf(f, fmt, ap);
5433 #undef PerlIO_stdoutf
5435 PerlIO_stdoutf(const char *fmt, ...)
5441 result = PerlIO_vprintf(PerlIO_stdout(), fmt, ap);
5446 #undef PerlIO_tmpfile
5448 PerlIO_tmpfile(void)
5450 return PerlIO_tmpfile_flags(0);
5453 #define MKOSTEMP_MODES ( O_RDWR | O_CREAT | O_EXCL )
5454 #define MKOSTEMP_MODE_MASK ( O_ACCMODE | O_CREAT | O_EXCL | O_TRUNC )
5457 PerlIO_tmpfile_flags(int imode)
5464 const int fd = win32_tmpfd_mode(imode);
5466 f = PerlIO_fdopen(fd, "w+b");
5467 #elif ! defined(OS2)
5469 char tempname[] = "/tmp/PerlIO_XXXXXX";
5470 const char * const tmpdir = TAINTING_get ? NULL : PerlEnv_getenv("TMPDIR");
5472 int old_umask = umask(0177);
5473 imode &= ~MKOSTEMP_MODE_MASK;
5474 if (tmpdir && *tmpdir) {
5475 /* if TMPDIR is set and not empty, we try that first */
5476 sv = newSVpv(tmpdir, 0);
5477 sv_catpv(sv, tempname + 4);
5478 fd = Perl_my_mkostemp_cloexec(SvPVX(sv), imode | O_VMS_DELETEONCLOSE);
5483 /* else we try /tmp */
5484 fd = Perl_my_mkostemp_cloexec(tempname, imode | O_VMS_DELETEONCLOSE);
5489 sv_catpv(sv, tempname + 4);
5490 fd = Perl_my_mkostemp_cloexec(SvPVX(sv), imode | O_VMS_DELETEONCLOSE);
5494 /* fdopen() with a numeric mode */
5497 (void)PerlIO_intmode2str(imode | MKOSTEMP_MODES, mode, &writing);
5498 f = PerlIO_fdopen(fd, mode);
5500 PerlIOBase(f)->flags |= PERLIO_F_TEMP;
5502 PerlLIO_unlink(sv ? SvPVX_const(sv) : tempname);
5506 #else /* !HAS_MKSTEMP, fallback to stdio tmpfile(). */
5507 FILE * const stdio = PerlSIO_tmpfile();
5510 f = PerlIO_fdopen(fileno(stdio), "w+");
5512 #endif /* else WIN32 */
5517 Perl_PerlIO_save_errno(pTHX_ PerlIO *f)
5519 PERL_UNUSED_CONTEXT;
5520 if (!PerlIOValid(f))
5522 PerlIOBase(f)->err = errno;
5524 PerlIOBase(f)->os_err = vaxc$errno;
5526 PerlIOBase(f)->os_err = Perl_rc;
5527 #elif defined(WIN32)
5528 PerlIOBase(f)->os_err = GetLastError();
5533 Perl_PerlIO_restore_errno(pTHX_ PerlIO *f)
5535 PERL_UNUSED_CONTEXT;
5536 if (!PerlIOValid(f))
5538 SETERRNO(PerlIOBase(f)->err, PerlIOBase(f)->os_err);
5540 Perl_rc = PerlIOBase(f)->os_err);
5541 #elif defined(WIN32)
5542 SetLastError(PerlIOBase(f)->os_err);
5550 /*======================================================================================*/
5552 * Now some functions in terms of above which may be needed even if we are
5553 * not in true PerlIO mode
5556 Perl_PerlIO_context_layers(pTHX_ const char *mode)
5558 /* Returns the layers set by "use open" */
5560 const char *direction = NULL;
5563 * Need to supply default layer info from open.pm
5569 if (mode && mode[0] != 'r') {
5570 if (PL_curcop->cop_hints & HINT_LEXICAL_IO_OUT)
5571 direction = "open>";
5573 if (PL_curcop->cop_hints & HINT_LEXICAL_IO_IN)
5574 direction = "open<";
5579 layers = cop_hints_fetch_pvn(PL_curcop, direction, 5, 0, 0);
5582 return SvOK(layers) ? SvPV_nolen_const(layers) : NULL;
5587 # undef PerlIO_setpos
5589 PerlIO_setpos(PerlIO *f, SV *pos)
5595 const Off_t * const posn = (Off_t *) SvPV(pos, len);
5596 if(len == sizeof(Off_t))
5597 return PerlIO_seek(f, *posn, SEEK_SET);
5600 SETERRNO(EINVAL, SS_IVCHAN);
5604 # undef PerlIO_setpos
5606 PerlIO_setpos(PerlIO *f, SV *pos)
5612 Fpos_t * const fpos = (Fpos_t *) SvPV(pos, len);
5613 if(len == sizeof(Fpos_t))
5614 # if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64)
5615 return fsetpos64(f, fpos);
5617 return fsetpos(f, fpos);
5621 SETERRNO(EINVAL, SS_IVCHAN);
5627 # undef PerlIO_getpos
5629 PerlIO_getpos(PerlIO *f, SV *pos)
5632 Off_t posn = PerlIO_tell(f);
5633 sv_setpvn(pos, (char *) &posn, sizeof(posn));
5634 return (posn == (Off_t) - 1) ? -1 : 0;
5637 # undef PerlIO_getpos
5639 PerlIO_getpos(PerlIO *f, SV *pos)
5644 # if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64)
5645 code = fgetpos64(f, &fpos);
5647 code = fgetpos(f, &fpos);
5649 sv_setpvn(pos, (char *) &fpos, sizeof(fpos));
5654 /* print a failure format string message to stderr and fail exit the process
5655 using only libc without depending on any perl data structures being
5660 Perl_noperl_die(const char* pat, ...)
5663 PERL_ARGS_ASSERT_NOPERL_DIE;
5664 va_start(arglist, pat);
5665 vfprintf(stderr, pat, arglist);
5671 * ex: set ts=8 sts=4 sw=4 et: