/*
- * perlio.c Copyright (c) 1996-2002, Nick Ing-Simmons You may distribute
+ * perlio.c Copyright (c) 1996-2006, Nick Ing-Simmons You may distribute
* under the terms of either the GNU General Public License or the
* Artistic License, as specified in the README file.
*/
* over passes, and through long dales, and across many streams.
*/
+/* This file contains the functions needed to implement PerlIO, which
+ * is Perl's private replacement for the C stdio library. This is used
+ * by default unless you compile with -Uuseperlio or run with
+ * PERLIO=:stdio (but don't do this unless you know what you're doing)
+ */
+
/*
* If we have ActivePerl-like PERL_IMPLICIT_SYS then we need a dTHX to get
* at the dispatch tables, even when we do not need it for other reasons.
#include "XSUB.h"
+#ifdef __Lynx__
+/* Missing proto on LynxOS */
+int mkstemp(char*);
+#endif
+
/* Call the callback or PerlIOBase, and return failure. */
#define Perl_PerlIO_or_Base(f, callback, base, failure, args) \
if (PerlIOValid(f)) { \
- PerlIO_funcs *tab = PerlIOBase(f)->tab; \
+ const PerlIO_funcs * const tab = PerlIOBase(f)->tab;\
if (tab && tab->callback) \
return (*tab->callback) args; \
else \
/* Call the callback or fail, and return failure. */
#define Perl_PerlIO_or_fail(f, callback, failure, args) \
if (PerlIOValid(f)) { \
- PerlIO_funcs *tab = PerlIOBase(f)->tab; \
+ const PerlIO_funcs * const tab = PerlIOBase(f)->tab;\
if (tab && tab->callback) \
return (*tab->callback) args; \
SETERRNO(EINVAL, LIB_INVARG); \
/* Call the callback or PerlIOBase, and be void. */
#define Perl_PerlIO_or_Base_void(f, callback, base, args) \
if (PerlIOValid(f)) { \
- PerlIO_funcs *tab = PerlIOBase(f)->tab; \
+ const PerlIO_funcs * const tab = PerlIOBase(f)->tab;\
if (tab && tab->callback) \
(*tab->callback) args; \
else \
/* Call the callback or fail, and be void. */
#define Perl_PerlIO_or_fail_void(f, callback, args) \
if (PerlIOValid(f)) { \
- PerlIO_funcs *tab = PerlIOBase(f)->tab; \
+ const PerlIO_funcs * const tab = PerlIOBase(f)->tab;\
if (tab && tab->callback) \
(*tab->callback) args; \
else \
else \
SETERRNO(EBADF, SS_IVCHAN)
+#ifndef USE_SFIO
int
perlsio_binmode(FILE *fp, int iotype, int mode)
{
#ifdef DOSISH
# if defined(atarist) || defined(__MINT__)
if (!fflush(fp)) {
- if (mode & O_BINARY)
- ((FILE *) fp)->_flag |= _IOBIN;
- else
- ((FILE *) fp)->_flag &= ~_IOBIN;
- return 1;
+ if (mode & O_BINARY)
+ ((FILE *) fp)->_flag |= _IOBIN;
+ else
+ ((FILE *) fp)->_flag &= ~_IOBIN;
+ return 1;
}
return 0;
# else
if (PerlLIO_setmode(fileno(fp), mode) != -1) {
#endif
# if defined(WIN32) && defined(__BORLANDC__)
- /*
- * The translation mode of the stream is maintained independent of
- * the translation mode of the fd in the Borland RTL (heavy
- * digging through their runtime sources reveal). User has to set
- * the mode explicitly for the stream (though they don't document
- * this anywhere). GSAR 97-5-24
- */
- fseek(fp, 0L, 0);
- if (mode & O_BINARY)
- fp->flags |= _F_BIN;
- else
- fp->flags &= ~_F_BIN;
+ /*
+ * The translation mode of the stream is maintained independent
+of
+ * the translation mode of the fd in the Borland RTL (heavy
+ * digging through their runtime sources reveal). User has to
+set
+ * the mode explicitly for the stream (though they don't
+document
+ * this anywhere). GSAR 97-5-24
+ */
+ fseek(fp, 0L, 0);
+ if (mode & O_BINARY)
+ fp->flags |= _F_BIN;
+ else
+ fp->flags &= ~_F_BIN;
# endif
- return 1;
+ return 1;
}
else
- return 0;
+ return 0;
# endif
#else
# if defined(USEMYBINMODE)
dTHX;
if (my_binmode(fp, iotype, mode) != FALSE)
- return 1;
+ return 1;
else
- return 0;
+ return 0;
# else
+ PERL_UNUSED_ARG(fp);
+ PERL_UNUSED_ARG(iotype);
+ PERL_UNUSED_ARG(mode);
return 1;
# endif
#endif
}
+#endif /* sfio */
#ifndef O_ACCMODE
#define O_ACCMODE 3 /* Assume traditional implementation */
int
PerlIO_intmode2str(int rawmode, char *mode, int *writing)
{
- int result = rawmode & O_ACCMODE;
+ const int result = rawmode & O_ACCMODE;
int ix = 0;
int ptype;
switch (result) {
PerlIO_binmode(pTHX_ PerlIO *fp, int iotype, int mode, const char *names)
{
#ifdef USE_SFIO
+ PERL_UNUSED_ARG(iotype);
+ PERL_UNUSED_ARG(mode);
+ PERL_UNUSED_ARG(names);
return 1;
#else
return perlsio_binmode(fp, iotype, mode);
PerlIO *
PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
{
-#ifdef PERL_MICRO
+#if defined(PERL_MICRO) || defined(__SYMBIAN32__)
return NULL;
#else
#ifdef PERL_IMPLICIT_SYS
return win32_fdupopen(f);
#else
if (f) {
- int fd = PerlLIO_dup(PerlIO_fileno(f));
+ const int fd = PerlLIO_dup(PerlIO_fileno(f));
if (fd >= 0) {
char mode[8];
- int omode = fcntl(fd, F_GETFL);
#ifdef DJGPP
- omode = djgpp_get_stream_mode(f);
+ const int omode = djgpp_get_stream_mode(f);
+#else
+ const int omode = fcntl(fd, F_GETFL);
#endif
PerlIO_intmode2str(omode,mode,NULL);
/* the r+ is a hack */
if (*args == &PL_sv_undef)
return PerlIO_tmpfile();
else {
- char *name = SvPV_nolen(*args);
- if (*mode == '#') {
+ const char *name = SvPV_nolen_const(*args);
+ if (*mode == IoTYPE_NUMERIC) {
fd = PerlLIO_open3(name, imode, perm);
if (fd >= 0)
- return PerlIO_fdopen(fd, (char *) mode + 1);
+ return PerlIO_fdopen(fd, mode + 1);
}
else if (old) {
return PerlIO_reopen(name, mode, old);
if (items < 2)
Perl_croak(aTHX_ "Usage class->find(name[,load])");
else {
- char *name = SvPV_nolen(ST(1));
+ const char * const name = SvPV_nolen_const(ST(1));
ST(0) = (strEQ(name, "crlf")
|| strEQ(name, "raw")) ? &PL_sv_yes : &PL_sv_undef;
XSRETURN(1);
void
PerlIO_init(pTHX)
{
+ PERL_UNUSED_CONTEXT;
/*
* Does nothing (yet) except force this file to be included in perl
* binary. That allows this file to force inclusion of other functions
void
PerlIO_init(pTHX)
{
+ PERL_UNUSED_CONTEXT;
/*
* Force this file to be included in perl binary. Which allows this
* file to force inclusion of other functions that may be required by
PerlIO *
PerlIO_importFILE(FILE *stdio, const char *mode)
{
- int fd = fileno(stdio);
+ const int fd = fileno(stdio);
if (!mode || !*mode) {
mode = "r+";
}
FILE *
PerlIO_findFILE(PerlIO *pio)
{
- int fd = PerlIO_fileno(pio);
- FILE *f = fdopen(fd, "r+");
+ const int fd = PerlIO_fileno(pio);
+ FILE * const f = fdopen(fd, "r+");
PerlIO_flush(pio);
if (!f && errno == EINVAL)
f = fdopen(fd, "w");
#include <sys/mman.h>
#endif
-/*
- * Why is this here - not in perlio.h? RMB
- */
-void PerlIO_debug(const char *fmt, ...)
- __attribute__format__(__printf__, 1, 2);
-
void
PerlIO_debug(const char *fmt, ...)
{
- static int dbg = 0;
va_list ap;
dSYS;
va_start(ap, fmt);
- if (!dbg) {
- char *s = PerlEnv_getenv("PERLIO_DEBUG");
+ if (!PL_perlio_debug_fd && !PL_tainting && PL_uid == PL_euid && PL_gid == PL_egid) {
+ const char * const s = PerlEnv_getenv("PERLIO_DEBUG");
if (s && *s)
- dbg = PerlLIO_open3(s, O_WRONLY | O_CREAT | O_APPEND, 0666);
+ PL_perlio_debug_fd = PerlLIO_open3(s, O_WRONLY | O_CREAT | O_APPEND, 0666);
else
- dbg = -1;
+ PL_perlio_debug_fd = -1;
}
- if (dbg > 0) {
+ if (PL_perlio_debug_fd > 0) {
dTHX;
#ifdef USE_ITHREADS
+ const char * const s = CopFILE(PL_curcop);
/* Use fixed buffer as sv_catpvf etc. needs SVs */
char buffer[1024];
- char *s;
- STRLEN len;
- s = CopFILE(PL_curcop);
- if (!s)
- s = "(none)";
- sprintf(buffer, "%s:%" IVdf " ", s, (IV) CopLINE(PL_curcop));
- len = strlen(buffer);
- vsprintf(buffer+len, fmt, ap);
- PerlLIO_write(dbg, buffer, strlen(buffer));
+ const STRLEN len = my_sprintf(buffer, "%.40s:%" IVdf " ", s ? s : "(none)", (IV) CopLINE(PL_curcop));
+ const STRLEN len2 = vsprintf(buffer+len, fmt, ap);
+ PerlLIO_write(PL_perlio_debug_fd, buffer, len + len2);
#else
- SV *sv = newSVpvn("", 0);
- char *s;
+ const char *s = CopFILE(PL_curcop);
STRLEN len;
- s = CopFILE(PL_curcop);
- if (!s)
- s = "(none)";
- Perl_sv_catpvf(aTHX_ sv, "%s:%" IVdf " ", s,
+ SV * const sv = newSVpvs("");
+ Perl_sv_catpvf(aTHX_ sv, "%s:%" IVdf " ", s ? s : "(none)",
(IV) CopLINE(PL_curcop));
Perl_sv_vcatpvf(aTHX_ sv, fmt, &ap);
- s = SvPV(sv, len);
- PerlLIO_write(dbg, s, len);
+ s = SvPV_const(sv, len);
+ PerlLIO_write(PL_perlio_debug_fd, s, len);
SvREFCNT_dec(sv);
#endif
}
PerlIO *
PerlIO_allocate(pTHX)
{
+ dVAR;
/*
* Find a free slot in the table, allocating new table as necessary
*/
}
}
}
- Newz('I',f,PERLIO_TABLE_SIZE,PerlIO);
+ Newxz(f,PERLIO_TABLE_SIZE,PerlIO);
if (!f) {
return NULL;
}
PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
{
if (PerlIOValid(f)) {
- PerlIO_funcs *tab = PerlIOBase(f)->tab;
+ const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
PerlIO_debug("fdupopen f=%p param=%p\n",(void*)f,(void*)param);
if (tab && tab->Dup)
return (*tab->Dup)(aTHX_ PerlIO_allocate(aTHX), f, param, flags);
void
PerlIO_cleantable(pTHX_ PerlIO **tablep)
{
- PerlIO *table = *tablep;
+ PerlIO * const table = *tablep;
if (table) {
int i;
PerlIO_cleantable(aTHX_(PerlIO **) & (table[0]));
for (i = PERLIO_TABLE_SIZE - 1; i > 0; i--) {
- PerlIO *f = table + i;
+ PerlIO * const f = table + i;
if (*f) {
PerlIO_close(f);
}
PerlIO_list_alloc(pTHX)
{
PerlIO_list_t *list;
- Newz('L', list, 1, PerlIO_list_t);
+ PERL_UNUSED_CONTEXT;
+ Newxz(list, 1, PerlIO_list_t);
list->refcnt = 1;
return list;
}
void
PerlIO_list_push(pTHX_ PerlIO_list_t *list, PerlIO_funcs *funcs, SV *arg)
{
+ dVAR;
PerlIO_pair_t *p;
+ PERL_UNUSED_CONTEXT;
+
if (list->cur >= list->len) {
list->len += 8;
if (list->array)
Renew(list->array, list->len, PerlIO_pair_t);
else
- New('l', list->array, list->len, PerlIO_pair_t);
+ Newx(list->array, list->len, PerlIO_pair_t);
}
p = &(list->array[list->cur++]);
p->funcs = funcs;
if ((p->arg = arg)) {
- SvREFCNT_inc(arg);
+ SvREFCNT_inc_simple_void_NN(arg);
}
}
PerlIO_list_t *
PerlIO_clone_list(pTHX_ PerlIO_list_t *proto, CLONE_PARAMS *param)
{
- PerlIO_list_t *list = (PerlIO_list_t *) NULL;
+ PerlIO_list_t *list = NULL;
if (proto) {
int i;
list = PerlIO_list_alloc(aTHX);
for (i=0; i < proto->cur; i++) {
- SV *arg = Nullsv;
+ SV *arg = NULL;
if (proto->array[i].arg)
arg = PerlIO_sv_dup(aTHX_ proto->array[i].arg,param);
PerlIO_list_push(aTHX_ list, proto->array[i].funcs, arg);
f++;
}
}
+#else
+ PERL_UNUSED_ARG(proto);
+ PERL_UNUSED_ARG(param);
#endif
}
void
PerlIO_destruct(pTHX)
{
+ dVAR;
PerlIO **table = &PL_perlio;
PerlIO *f;
#ifdef USE_ITHREADS
table = (PerlIO **) (f++);
for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
PerlIO *x = f;
- PerlIOl *l;
+ const PerlIOl *l;
while ((l = *x)) {
if (l->tab->kind & PERLIO_K_DESTRUCT) {
PerlIO_debug("Destruct popping %s\n", l->tab->name);
void
PerlIO_pop(pTHX_ PerlIO *f)
{
- PerlIOl *l = *f;
+ const PerlIOl *l = *f;
if (l) {
PerlIO_debug("PerlIO_pop f=%p %s\n", (void*)f, l->tab->name);
if (l->tab->Popped) {
AV *
PerlIO_get_layers(pTHX_ PerlIO *f)
{
- AV *av = newAV();
+ dVAR;
+ AV * const av = newAV();
- if (PerlIOValid(f)) {
- PerlIOl *l = PerlIOBase(f);
-
- while (l) {
- SV *name = l->tab && l->tab->name ?
- newSVpv(l->tab->name, 0) : &PL_sv_undef;
- SV *arg = l->tab && l->tab->Getarg ?
- (*l->tab->Getarg)(aTHX_ &l, 0, 0) : &PL_sv_undef;
- av_push(av, name);
- av_push(av, arg);
- av_push(av, newSViv((IV)l->flags));
- l = l->next;
- }
- }
+ if (PerlIOValid(f)) {
+ PerlIOl *l = PerlIOBase(f);
+
+ while (l) {
+ SV * const name = l->tab && l->tab->name ?
+ newSVpv(l->tab->name, 0) : &PL_sv_undef;
+ SV * const arg = l->tab && l->tab->Getarg ?
+ (*l->tab->Getarg)(aTHX_ &l, 0, 0) : &PL_sv_undef;
+ av_push(av, name);
+ av_push(av, arg);
+ av_push(av, newSViv((IV)l->flags));
+ l = l->next;
+ }
+ }
- return av;
+ return av;
}
/*--------------------------------------------------------------------------------------*/
PerlIO_funcs *
PerlIO_find_layer(pTHX_ const char *name, STRLEN len, int load)
{
+ dVAR;
IV i;
if ((SSize_t) len <= 0)
len = strlen(name);
for (i = 0; i < PL_known_layers->cur; i++) {
- PerlIO_funcs *f = PL_known_layers->array[i].funcs;
- if (memEQ(f->name, name, len)) {
+ PerlIO_funcs * const f = PL_known_layers->array[i].funcs;
+ if (memEQ(f->name, name, len) && f->name[len] == 0) {
PerlIO_debug("%.*s => %p\n", (int) len, name, (void*)f);
return f;
}
Perl_croak(aTHX_ "Recursive call to Perl_load_module in PerlIO_find_layer");
return NULL;
} else {
- SV *pkgsv = newSVpvn("PerlIO", 6);
- SV *layer = newSVpvn(name, len);
- CV *cv = get_cv("PerlIO::Layer::NoWarnings", FALSE);
- ENTER;
+ SV * const pkgsv = newSVpvs("PerlIO");
+ SV * const layer = newSVpvn(name, len);
+ CV * const cv = get_cv("PerlIO::Layer::NoWarnings", FALSE);
+ ENTER;
SAVEINT(PL_in_load_module);
if (cv) {
- SAVESPTR(PL_warnhook);
- PL_warnhook = (SV *) cv;
+ SAVEGENERICSV(PL_warnhook);
+ PL_warnhook = (SV *) (SvREFCNT_inc_simple_NN(cv));
}
PL_in_load_module++;
/*
* The two SVs are magically freed by load_module
*/
- Perl_load_module(aTHX_ 0, pkgsv, Nullsv, layer, Nullsv);
+ Perl_load_module(aTHX_ 0, pkgsv, NULL, layer, NULL);
PL_in_load_module--;
LEAVE;
return PerlIO_find_layer(aTHX_ name, len, 0);
perlio_mg_set(pTHX_ SV *sv, MAGIC *mg)
{
if (SvROK(sv)) {
- IO *io = GvIOn((GV *) SvRV(sv));
- PerlIO *ifp = IoIFP(io);
- PerlIO *ofp = IoOFP(io);
+ IO * const io = GvIOn((GV *) SvRV(sv));
+ PerlIO * const ifp = IoIFP(io);
+ PerlIO * const ofp = IoOFP(io);
Perl_warn(aTHX_ "set %" SVf " %p %p %p", sv, io, ifp, ofp);
}
return 0;
perlio_mg_get(pTHX_ SV *sv, MAGIC *mg)
{
if (SvROK(sv)) {
- IO *io = GvIOn((GV *) SvRV(sv));
- PerlIO *ifp = IoIFP(io);
- PerlIO *ofp = IoOFP(io);
+ IO * const io = GvIOn((GV *) SvRV(sv));
+ PerlIO * const ifp = IoIFP(io);
+ PerlIO * const ofp = IoOFP(io);
Perl_warn(aTHX_ "get %" SVf " %p %p %p", sv, io, ifp, ofp);
}
return 0;
XS(XS_io_MODIFY_SCALAR_ATTRIBUTES)
{
dXSARGS;
- SV *sv = SvRV(ST(1));
- AV *av = newAV();
+ SV * const sv = SvRV(ST(1));
+ AV * const av = newAV();
MAGIC *mg;
int count = 0;
int i;
Perl_warn(aTHX_ "attrib %" SVf, sv);
for (i = 2; i < items; i++) {
STRLEN len;
- const char *name = SvPV(ST(i), len);
- SV *layer = PerlIO_find_layer(aTHX_ name, len, 1);
+ const char * const name = SvPV_const(ST(i), len);
+ SV * const layer = PerlIO_find_layer(aTHX_ name, len, 1);
if (layer) {
- av_push(av, SvREFCNT_inc(layer));
+ av_push(av, SvREFCNT_inc_simple_NN(layer));
}
else {
ST(count) = ST(i);
SV *
PerlIO_tab_sv(pTHX_ PerlIO_funcs *tab)
{
- HV *stash = gv_stashpv("PerlIO::Layer", TRUE);
- SV *sv = sv_bless(newRV_noinc(newSViv(PTR2IV(tab))), stash);
+ HV * const stash = gv_stashpvs("PerlIO::Layer", TRUE);
+ SV * const sv = sv_bless(newRV_noinc(newSViv(PTR2IV(tab))), stash);
return sv;
}
/* This is used as a %SIG{__WARN__} handler to supress warnings
during loading of layers.
*/
+ dVAR;
dXSARGS;
if (items)
- PerlIO_debug("warning:%s\n",SvPV_nolen(ST(0)));
+ PerlIO_debug("warning:%s\n",SvPV_nolen_const(ST(0)));
XSRETURN(0);
}
XS(XS_PerlIO__Layer__find)
{
+ dVAR;
dXSARGS;
if (items < 2)
Perl_croak(aTHX_ "Usage class->find(name[,load])");
else {
- STRLEN len = 0;
- char *name = SvPV(ST(1), len);
- bool load = (items > 2) ? SvTRUE(ST(2)) : 0;
- PerlIO_funcs *layer = PerlIO_find_layer(aTHX_ name, len, load);
+ STRLEN len;
+ const char * const name = SvPV_const(ST(1), len);
+ const bool load = (items > 2) ? SvTRUE(ST(2)) : 0;
+ PerlIO_funcs * const layer = PerlIO_find_layer(aTHX_ name, len, load);
ST(0) =
(layer) ? sv_2mortal(PerlIO_tab_sv(aTHX_ layer)) :
&PL_sv_undef;
void
PerlIO_define_layer(pTHX_ PerlIO_funcs *tab)
{
+ dVAR;
if (!PL_known_layers)
PL_known_layers = PerlIO_list_alloc(aTHX);
- PerlIO_list_push(aTHX_ PL_known_layers, tab, Nullsv);
+ PerlIO_list_push(aTHX_ PL_known_layers, tab, NULL);
PerlIO_debug("define %s %p\n", tab->name, (void*)tab);
}
int
PerlIO_parse_layers(pTHX_ PerlIO_list_t *av, const char *names)
{
+ dVAR;
if (names) {
const char *s = names;
while (*s) {
if (*s) {
STRLEN llen = 0;
const char *e = s;
- const char *as = Nullch;
+ const char *as = NULL;
STRLEN alen = 0;
if (!isIDFIRST(*s)) {
/*
* passed. Even though this means "foo : : bar" is
* seen as an invalid separator character.
*/
- char q = ((*s == '\'') ? '"' : '\'');
+ const char q = ((*s == '\'') ? '"' : '\'');
if (ckWARN(WARN_LAYER))
Perl_warner(aTHX_ packWARN(WARN_LAYER),
- "perlio: invalid separator character %c%c%c in layer specification list %s",
+ "Invalid separator character %c%c%c in PerlIO layer specification %s",
q, *s, q, s);
SETERRNO(EINVAL, LIB_INVARG);
return -1;
e--;
if (ckWARN(WARN_LAYER))
Perl_warner(aTHX_ packWARN(WARN_LAYER),
- "perlio: argument list not closed for layer \"%.*s\"",
+ "Argument list not closed for PerlIO layer \"%.*s\"",
(int) (e - s), s);
return -1;
default:
}
}
if (e > s) {
- bool warn_layer = ckWARN(WARN_LAYER);
- PerlIO_funcs *layer =
+ PerlIO_funcs * const layer =
PerlIO_find_layer(aTHX_ s, llen, 1);
if (layer) {
PerlIO_list_push(aTHX_ av, layer,
&PL_sv_undef);
}
else {
- if (warn_layer)
- Perl_warner(aTHX_ packWARN(WARN_LAYER), "perlio: unknown layer \"%.*s\"",
+ if (ckWARN(WARN_LAYER))
+ Perl_warner(aTHX_ packWARN(WARN_LAYER), "Unknown PerlIO layer \"%.*s\"",
(int) llen, s);
return -1;
}
void
PerlIO_default_buffer(pTHX_ PerlIO_list_t *av)
{
- PerlIO_funcs *tab = &PerlIO_perlio;
+ dVAR;
+ PERLIO_FUNCS_DECL(*tab) = &PerlIO_perlio;
#ifdef PERLIO_USING_CRLF
tab = &PerlIO_crlf;
#else
IV
PerlIOPop_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
{
+ PERL_UNUSED_ARG(mode);
+ PERL_UNUSED_ARG(arg);
+ PERL_UNUSED_ARG(tab);
if (PerlIOValid(f)) {
PerlIO_flush(f);
PerlIO_pop(aTHX_ f);
return -1;
}
-PerlIO_funcs PerlIO_remove = {
+PERLIO_FUNCS_DECL(PerlIO_remove) = {
sizeof(PerlIO_funcs),
"pop",
0,
NULL,
NULL,
NULL,
+ NULL,
+ NULL,
NULL, /* flush */
NULL, /* fill */
NULL,
PerlIO_list_t *
PerlIO_default_layers(pTHX)
{
+ dVAR;
if (!PL_def_layerlist) {
- const char *s = (PL_tainting) ? Nullch : PerlEnv_getenv("PERLIO");
- PerlIO_funcs *osLayer = &PerlIO_unix;
+ const char * const s = (PL_tainting) ? NULL : PerlEnv_getenv("PERLIO");
+ PERLIO_FUNCS_DECL(*osLayer) = &PerlIO_unix;
PL_def_layerlist = PerlIO_list_alloc(aTHX);
- PerlIO_define_layer(aTHX_ & PerlIO_unix);
-#if defined(WIN32) && !defined(UNDER_CE)
- PerlIO_define_layer(aTHX_ & PerlIO_win32);
+ PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_unix));
+#if defined(WIN32)
+ PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_win32));
#if 0
osLayer = &PerlIO_win32;
#endif
#endif
- PerlIO_define_layer(aTHX_ & PerlIO_raw);
- PerlIO_define_layer(aTHX_ & PerlIO_perlio);
- PerlIO_define_layer(aTHX_ & PerlIO_stdio);
- PerlIO_define_layer(aTHX_ & PerlIO_crlf);
+ PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_raw));
+ PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_perlio));
+ PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_stdio));
+ PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_crlf));
#ifdef HAS_MMAP
- PerlIO_define_layer(aTHX_ & PerlIO_mmap);
+ PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_mmap));
#endif
- PerlIO_define_layer(aTHX_ & PerlIO_utf8);
- PerlIO_define_layer(aTHX_ & PerlIO_remove);
- PerlIO_define_layer(aTHX_ & PerlIO_byte);
+ PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_utf8));
+ PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_remove));
+ PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_byte));
PerlIO_list_push(aTHX_ PL_def_layerlist,
PerlIO_find_layer(aTHX_ osLayer->name, 0, 0),
&PL_sv_undef);
PerlIO_funcs *
PerlIO_default_layer(pTHX_ I32 n)
{
- PerlIO_list_t *av = PerlIO_default_layers(aTHX);
+ dVAR;
+ PerlIO_list_t * const av = PerlIO_default_layers(aTHX);
if (n < 0)
n += av->cur;
- return PerlIO_layer_fetch(aTHX_ av, n, &PerlIO_stdio);
+ return PerlIO_layer_fetch(aTHX_ av, n, PERLIO_FUNCS_CAST(&PerlIO_stdio));
}
#define PerlIO_default_top() PerlIO_default_layer(aTHX_ -1)
void
PerlIO_stdstreams(pTHX)
{
+ dVAR;
if (!PL_perlio) {
PerlIO_allocate(aTHX);
PerlIO_fdopen(0, "Ir" PERLIO_STDTEXT);
}
PerlIO *
-PerlIO_push(pTHX_ PerlIO *f, PerlIO_funcs *tab, const char *mode, SV *arg)
+PerlIO_push(pTHX_ PerlIO *f, PERLIO_FUNCS_DECL(*tab), const char *mode, SV *arg)
{
if (tab->fsize != sizeof(PerlIO_funcs)) {
mismatch:
Perl_croak(aTHX_ "Layer does not match this perl");
}
if (tab->size) {
- PerlIOl *l = NULL;
+ PerlIOl *l;
if (tab->size < sizeof(PerlIOl)) {
goto mismatch;
}
/* Real layer with a data area */
- Newc('L',l,tab->size,char,PerlIOl);
+ Newxc(l,tab->size,char,PerlIOl);
if (l && f) {
Zero(l, tab->size, char);
l->next = *f;
- l->tab = tab;
+ l->tab = (PerlIO_funcs*) tab;
*f = l;
PerlIO_debug("PerlIO_push f=%p %s %s %p\n", (void*)f, tab->name,
(mode) ? mode : "(Null)", (void*)arg);
if (*l->tab->Pushed &&
- (*l->tab->Pushed) (aTHX_ f, mode, arg, tab) != 0) {
+ (*l->tab->Pushed) (aTHX_ f, mode, arg, (PerlIO_funcs*) tab) != 0) {
PerlIO_pop(aTHX_ f);
return NULL;
}
PerlIO_debug("PerlIO_push f=%p %s %s %p\n", (void*)f, tab->name,
(mode) ? mode : "(Null)", (void*)arg);
if (tab->Pushed &&
- (*tab->Pushed) (aTHX_ f, mode, arg, tab) != 0) {
+ (*tab->Pushed) (aTHX_ f, mode, arg, (PerlIO_funcs*) tab) != 0) {
return NULL;
}
}
IV
PerlIORaw_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
{
+ PERL_UNUSED_ARG(mode);
+ PERL_UNUSED_ARG(arg);
+ PERL_UNUSED_ARG(tab);
if (PerlIOValid(f)) {
PerlIO *t;
- PerlIOl *l;
+ const PerlIOl *l;
PerlIO_flush(f);
/*
* Strip all layers that are not suitable for a raw stream
{
int code = 0;
while (n < max) {
- PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n, NULL);
+ PerlIO_funcs * const tab = PerlIO_layer_fetch(aTHX_ layers, n, NULL);
if (tab) {
if (!PerlIO_push(aTHX_ f, tab, mode, PerlIOArg)) {
code = -1;
{
int code = 0;
if (f && names) {
- PerlIO_list_t *layers = PerlIO_list_alloc(aTHX);
+ PerlIO_list_t * const layers = PerlIO_list_alloc(aTHX);
code = PerlIO_parse_layers(aTHX_ layers, names);
if (code == 0) {
code = PerlIO_apply_layera(aTHX_ f, mode, layers, 0, layers->cur);
int
PerlIO_binmode(pTHX_ PerlIO *f, int iotype, int mode, const char *names)
{
- PerlIO_debug("PerlIO_binmode f=%p %s %c %x %s\n",
- (void*)f, PerlIOBase(f)->tab->name, iotype, mode,
- (names) ? names : "(Null)");
+ PerlIO_debug("PerlIO_binmode f=%p %s %c %x %s\n", (void*)f,
+ (PerlIOBase(f)) ? PerlIOBase(f)->tab->name : "(Null)",
+ iotype, mode, (names) ? names : "(Null)");
+
if (names) {
/* Do not flush etc. if (e.g.) switching encodings.
if a pushed layer knows it needs to flush lower layers
/* Legacy binmode is now _defined_ as being equivalent to pushing :raw
So code that used to be here is now in PerlIORaw_pushed().
*/
- return PerlIO_push(aTHX_ f, &PerlIO_raw, Nullch, Nullsv) ? TRUE : FALSE;
+ return PerlIO_push(aTHX_ f, PERLIO_FUNCS_CAST(&PerlIO_raw), NULL, NULL) ? TRUE : FALSE;
}
}
PerlIO__close(pTHX_ PerlIO *f)
{
if (PerlIOValid(f)) {
- PerlIO_funcs *tab = PerlIOBase(f)->tab;
+ PerlIO_funcs * const tab = PerlIOBase(f)->tab;
if (tab && tab->Close)
return (*tab->Close)(aTHX_ f);
else
int
Perl_PerlIO_close(pTHX_ PerlIO *f)
{
- int code = PerlIO__close(aTHX_ f);
+ const int code = PerlIO__close(aTHX_ f);
while (PerlIOValid(f)) {
PerlIO_pop(aTHX_ f);
}
int
Perl_PerlIO_fileno(pTHX_ PerlIO *f)
{
+ dVAR;
Perl_PerlIO_or_Base(f, Fileno, fileno, -1, (aTHX_ f));
}
static const char *
PerlIO_context_layers(pTHX_ const char *mode)
{
+ dVAR;
const char *type = NULL;
/*
* Need to supply default layer info from open.pm
*/
if (PL_curcop) {
- SV *layers = PL_curcop->cop_io;
+ SV * const layers = PL_curcop->cop_io;
if (layers) {
STRLEN len;
- type = SvPV(layers, len);
+ type = SvPV_const(layers, len);
if (type && mode[0] != 'r') {
/*
* Skip to write part
*/
- const char *s = strchr(type, 0);
+ const char * const s = strchr(type, 0);
if (s && (STRLEN)(s - type) < len) {
type = s + 1;
}
static PerlIO_funcs *
PerlIO_layer_from_ref(pTHX_ SV *sv)
{
+ dVAR;
/*
* For any scalar type load the handler which is bundled with perl
*/
if (SvTYPE(sv) < SVt_PVAV)
- return PerlIO_find_layer(aTHX_ "scalar", 6, 1);
+ return PerlIO_find_layer(aTHX_ STR_WITH_LEN("scalar"), 1);
/*
* For other types allow if layer is known but don't try and load it
*/
switch (SvTYPE(sv)) {
case SVt_PVAV:
- return PerlIO_find_layer(aTHX_ "Array", 5, 0);
+ return PerlIO_find_layer(aTHX_ STR_WITH_LEN("Array"), 0);
case SVt_PVHV:
- return PerlIO_find_layer(aTHX_ "Hash", 4, 0);
+ return PerlIO_find_layer(aTHX_ STR_WITH_LEN("Hash"), 0);
case SVt_PVCV:
- return PerlIO_find_layer(aTHX_ "Code", 4, 0);
+ return PerlIO_find_layer(aTHX_ STR_WITH_LEN("Code"), 0);
case SVt_PVGV:
- return PerlIO_find_layer(aTHX_ "Glob", 4, 0);
+ return PerlIO_find_layer(aTHX_ STR_WITH_LEN("Glob"), 0);
}
return NULL;
}
PerlIO_resolve_layers(pTHX_ const char *layers,
const char *mode, int narg, SV **args)
{
+ dVAR;
PerlIO_list_t *def = PerlIO_default_layers(aTHX);
int incdef = 1;
if (!PL_perlio)
PerlIO_stdstreams(aTHX);
if (narg) {
- SV *arg = *args;
+ SV * const arg = *args;
/*
* If it is a reference but not an object see if we have a handler
* for it
*/
if (SvROK(arg) && !sv_isobject(arg)) {
- PerlIO_funcs *handler = PerlIO_layer_from_ref(aTHX_ SvRV(arg));
+ PerlIO_funcs * const handler = PerlIO_layer_from_ref(aTHX_ SvRV(arg));
if (handler) {
def = PerlIO_list_alloc(aTHX);
PerlIO_list_push(aTHX_ def, handler, &PL_sv_undef);
*/
}
}
- if (!layers)
+ if (!layers || !*layers)
layers = PerlIO_context_layers(aTHX_ mode);
if (layers && *layers) {
PerlIO_list_t *av;
if (incdef) {
- IV i = def->cur;
+ IV i;
av = PerlIO_list_alloc(aTHX);
for (i = 0; i < def->cur; i++) {
PerlIO_list_push(aTHX_ av, def->array[i].funcs,
}
else {
PerlIO_list_free(aTHX_ av);
- return (PerlIO_list_t *) NULL;
+ return NULL;
}
}
else {
PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd,
int imode, int perm, PerlIO *f, int narg, SV **args)
{
+ dVAR;
if (!f && narg == 1 && *args == &PL_sv_undef) {
if ((f = PerlIO_tmpfile())) {
- if (!layers)
+ if (!layers || !*layers)
layers = PerlIO_context_layers(aTHX_ mode);
if (layers && *layers)
PerlIO_apply_layers(aTHX_ f, mode, layers);
}
}
else {
- PerlIO_list_t *layera = NULL;
+ PerlIO_list_t *layera;
IV n;
PerlIO_funcs *tab = NULL;
if (PerlIOValid(f)) {
PerlIOl *l = *f;
layera = PerlIO_list_alloc(aTHX);
while (l) {
- SV *arg = (l->tab->Getarg)
+ SV * const arg = (l->tab->Getarg)
? (*l->tab->Getarg) (aTHX_ &l, NULL, 0)
: &PL_sv_undef;
PerlIO_list_push(aTHX_ layera, l->tab, arg);
*/
n = layera->cur - 1;
while (n >= 0) {
- PerlIO_funcs *t = PerlIO_layer_fetch(aTHX_ layera, n, NULL);
+ PerlIO_funcs * const t = PerlIO_layer_fetch(aTHX_ layera, n, NULL);
if (t && t->Open) {
tab = t;
break;
Perl_croak(aTHX_ "More than one argument to open(,':%s')",tab->name);
}
PerlIO_debug("openn(%s,'%s','%s',%d,%x,%o,%p,%d,%p)\n",
- tab->name, layers, mode, fd, imode, perm,
- (void*)f, narg, (void*)args);
+ tab->name, layers ? layers : "(Null)", mode, fd,
+ imode, perm, (void*)f, narg, (void*)args);
if (tab->Open)
f = (*tab->Open) (aTHX_ tab, layera, n, mode, fd, imode, perm,
f, narg, args);
int
Perl_PerlIO_flush(pTHX_ PerlIO *f)
{
+ dVAR;
if (f) {
if (*f) {
- PerlIO_funcs *tab = PerlIOBase(f)->tab;
+ const PerlIO_funcs *tab = PerlIOBase(f)->tab;
if (tab && tab->Flush)
return (*tab->Flush) (aTHX_ f);
void
PerlIOBase_flush_linebuf(pTHX)
{
+ dVAR;
PerlIO **table = &PL_perlio;
PerlIO *f;
while ((f = *table)) {
PerlIO_has_base(PerlIO *f)
{
if (PerlIOValid(f)) {
- PerlIO_funcs *tab = PerlIOBase(f)->tab;
+ const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
if (tab)
return (tab->Get_base != NULL);
PerlIO_fast_gets(PerlIO *f)
{
if (PerlIOValid(f) && (PerlIOBase(f)->flags & PERLIO_F_FASTGETS)) {
- PerlIO_funcs *tab = PerlIOBase(f)->tab;
+ const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
if (tab)
return (tab->Set_ptrcnt != NULL);
PerlIO_has_cntptr(PerlIO *f)
{
if (PerlIOValid(f)) {
- PerlIO_funcs *tab = PerlIOBase(f)->tab;
+ const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
if (tab)
return (tab->Get_ptr != NULL && tab->Get_cnt != NULL);
PerlIO_canset_cnt(PerlIO *f)
{
if (PerlIOValid(f)) {
- PerlIO_funcs *tab = PerlIOBase(f)->tab;
+ const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
if (tab)
return (tab->Set_ptrcnt != NULL);
IV
PerlIOUtf8_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
{
+ PERL_UNUSED_CONTEXT;
+ PERL_UNUSED_ARG(mode);
+ PERL_UNUSED_ARG(arg);
if (PerlIOValid(f)) {
if (tab->kind & PERLIO_K_UTF8)
PerlIOBase(f)->flags |= PERLIO_F_UTF8;
return -1;
}
-PerlIO_funcs PerlIO_utf8 = {
+PERLIO_FUNCS_DECL(PerlIO_utf8) = {
sizeof(PerlIO_funcs),
"utf8",
0,
NULL,
NULL,
NULL,
+ NULL,
+ NULL,
NULL, /* flush */
NULL, /* fill */
NULL,
NULL, /* set_ptrcnt */
};
-PerlIO_funcs PerlIO_byte = {
+PERLIO_FUNCS_DECL(PerlIO_byte) = {
sizeof(PerlIO_funcs),
"bytes",
0,
NULL,
NULL,
NULL,
+ NULL,
+ NULL,
NULL, /* flush */
NULL, /* fill */
NULL,
IV n, const char *mode, int fd, int imode, int perm,
PerlIO *old, int narg, SV **args)
{
- PerlIO_funcs *tab = PerlIO_default_btm();
+ PerlIO_funcs * const tab = PerlIO_default_btm();
+ PERL_UNUSED_ARG(self);
if (tab && tab->Open)
return (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
old, narg, args);
return NULL;
}
-PerlIO_funcs PerlIO_raw = {
+PERLIO_FUNCS_DECL(PerlIO_raw) = {
sizeof(PerlIO_funcs),
"raw",
0,
NULL,
NULL,
NULL,
+ NULL,
+ NULL,
NULL, /* flush */
NULL, /* fill */
NULL,
{
char *s = buf;
if (PerlIOValid(f)) {
- IV flags = PerlIOBase(f)->flags;
+ const IV flags = PerlIOBase(f)->flags;
if (flags & PERLIO_F_APPEND) {
*s++ = 'a';
if (flags & PERLIO_F_CANREAD) {
IV
PerlIOBase_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
{
- PerlIOl *l = PerlIOBase(f);
-#if 0
- const char *omode = mode;
- char temp[8];
-#endif
+ PerlIOl * const l = PerlIOBase(f);
+ PERL_UNUSED_CONTEXT;
+ PERL_UNUSED_ARG(arg);
+
l->flags &= ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE |
PERLIO_F_TRUNCATE | PERLIO_F_APPEND);
if (tab->Set_ptrcnt != NULL)
l->flags |= PERLIO_F_FASTGETS;
if (mode) {
- if (*mode == '#' || *mode == 'I')
+ if (*mode == IoTYPE_NUMERIC || *mode == IoTYPE_IMPLICIT)
mode++;
switch (*mode++) {
case 'r':
IV
PerlIOBase_popped(pTHX_ PerlIO *f)
{
+ PERL_UNUSED_CONTEXT;
+ PERL_UNUSED_ARG(f);
return 0;
}
/*
* Save the position as current head considers it
*/
- Off_t old = PerlIO_tell(f);
- SSize_t done;
- PerlIO_push(aTHX_ f, &PerlIO_pending, "r", Nullsv);
+ const Off_t old = PerlIO_tell(f);
+ PerlIO_push(aTHX_ f, PERLIO_FUNCS_CAST(&PerlIO_pending), "r", NULL);
PerlIOSelf(f, PerlIOBuf)->posn = old;
- done = PerlIOBuf_unread(aTHX_ f, vbuf, count);
- return done;
+ return PerlIOBuf_unread(aTHX_ f, vbuf, count);
}
SSize_t
{
STDCHAR *buf = (STDCHAR *) vbuf;
if (f) {
- if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD))
+ if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD)) {
+ PerlIOBase(f)->flags |= PERLIO_F_ERROR;
+ SETERRNO(EBADF, SS_IVCHAN);
return 0;
+ }
while (count > 0) {
+ get_cnt:
+ {
SSize_t avail = PerlIO_get_cnt(f);
SSize_t take = 0;
if (avail > 0)
PerlIO_set_ptrcnt(f, ptr + take, (avail -= take));
count -= take;
buf += take;
+ if (avail == 0) /* set_ptrcnt could have reset avail */
+ goto get_cnt;
}
if (count > 0 && avail <= 0) {
if (PerlIO_fill(f) != 0)
break;
}
+ }
}
return (buf - (STDCHAR *) vbuf);
}
IV
PerlIOBase_noop_ok(pTHX_ PerlIO *f)
{
+ PERL_UNUSED_CONTEXT;
+ PERL_UNUSED_ARG(f);
return 0;
}
IV
PerlIOBase_noop_fail(pTHX_ PerlIO *f)
{
+ PERL_UNUSED_CONTEXT;
+ PERL_UNUSED_ARG(f);
return -1;
}
PerlIOBase(f)->flags &=
~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_OPEN);
while (PerlIOValid(n)) {
- PerlIO_funcs *tab = PerlIOBase(n)->tab;
+ const PerlIO_funcs * const tab = PerlIOBase(n)->tab;
if (tab && tab->Close) {
if ((*tab->Close)(aTHX_ n) != 0)
code = -1;
IV
PerlIOBase_eof(pTHX_ PerlIO *f)
{
+ PERL_UNUSED_CONTEXT;
if (PerlIOValid(f)) {
return (PerlIOBase(f)->flags & PERLIO_F_EOF) != 0;
}
IV
PerlIOBase_error(pTHX_ PerlIO *f)
{
+ PERL_UNUSED_CONTEXT;
if (PerlIOValid(f)) {
return (PerlIOBase(f)->flags & PERLIO_F_ERROR) != 0;
}
PerlIOBase_clearerr(pTHX_ PerlIO *f)
{
if (PerlIOValid(f)) {
- PerlIO *n = PerlIONext(f);
+ PerlIO * const n = PerlIONext(f);
PerlIOBase(f)->flags &= ~(PERLIO_F_ERROR | PERLIO_F_EOF);
if (PerlIOValid(n))
PerlIO_clearerr(n);
void
PerlIOBase_setlinebuf(pTHX_ PerlIO *f)
{
+ PERL_UNUSED_CONTEXT;
if (PerlIOValid(f)) {
PerlIOBase(f)->flags |= PERLIO_F_LINEBUF;
}
PerlIO_sv_dup(pTHX_ SV *arg, CLONE_PARAMS *param)
{
if (!arg)
- return Nullsv;
+ return NULL;
#ifdef sv_dup
if (param) {
return sv_dup(arg, param);
return newSVsv(arg);
}
#else
+ PERL_UNUSED_ARG(param);
return newSVsv(arg);
#endif
}
PerlIO *
PerlIOBase_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
{
- PerlIO *nexto = PerlIONext(o);
+ PerlIO * const nexto = PerlIONext(o);
if (PerlIOValid(nexto)) {
- PerlIO_funcs *tab = PerlIOBase(nexto)->tab;
+ const PerlIO_funcs * const tab = PerlIOBase(nexto)->tab;
if (tab && tab->Dup)
f = (*tab->Dup)(aTHX_ f, nexto, param, flags);
else
f = PerlIOBase_dup(aTHX_ f, nexto, param, flags);
}
if (f) {
- PerlIO_funcs *self = PerlIOBase(o)->tab;
+ PerlIO_funcs * const self = PerlIOBase(o)->tab;
SV *arg;
char buf[8];
PerlIO_debug("PerlIOBase_dup %s f=%p o=%p param=%p\n",
if (self->Getarg)
arg = (*self->Getarg)(aTHX_ o, param, flags);
else {
- arg = Nullsv;
+ arg = NULL;
}
f = PerlIO_push(aTHX_ f, self, PerlIO_modestr(o,buf), arg);
if (arg) {
return f;
}
-#define PERLIO_MAX_REFCOUNTABLE_FD 2048
#ifdef USE_THREADS
perl_mutex PerlIO_mutex;
#endif
-int PerlIO_fd_refcnt[PERLIO_MAX_REFCOUNTABLE_FD];
+
+/* PL_perlio_fd_refcnt[] is in intrpvar.h */
+
+/* Must be called with PerlIO_mutex locked. */
+static void
+S_more_refcounted_fds(pTHX_ const int new_fd) {
+ const int old_max = PL_perlio_fd_refcnt_size;
+ const int new_max = 16 + (new_fd & ~15);
+ int *new_array;
+
+ PerlIO_debug("More fds - old=%d, need %d, new=%d\n",
+ old_max, new_fd, new_max);
+
+ if (new_fd < old_max) {
+ return;
+ }
+
+ assert (new_max > new_fd);
+
+ new_array
+ = PerlMemShared_realloc(PL_perlio_fd_refcnt, new_max * sizeof(int));
+
+ if (!new_array) {
+#ifdef USE_THREADS
+ MUTEX_UNLOCK(&PerlIO_mutex);
+#endif
+ /* Can't use PerlIO to write as it allocates memory */
+ PerlLIO_write(PerlIO_fileno(Perl_error_log),
+ PL_no_mem, strlen(PL_no_mem));
+ my_exit(1);
+ }
+
+ PL_perlio_fd_refcnt_size = new_max;
+ PL_perlio_fd_refcnt = new_array;
+
+ PerlIO_debug("Zeroing %p, %d\n", new_array + old_max, new_max - old_max);
+
+ Zero(new_array + old_max, new_max - old_max, int);
+}
+
void
PerlIO_init(pTHX)
{
/* Place holder for stdstreams call ??? */
#ifdef USE_THREADS
- MUTEX_INIT(&PerlIO_mutex);
+ MUTEX_INIT(&PerlIO_mutex);
+#else
+ PERL_UNUSED_CONTEXT;
#endif
}
void
PerlIOUnix_refcnt_inc(int fd)
{
- if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
+ dTHX;
+ if (fd >= 0) {
+ dVAR;
+
#ifdef USE_THREADS
MUTEX_LOCK(&PerlIO_mutex);
#endif
- PerlIO_fd_refcnt[fd]++;
- PerlIO_debug("fd %d refcnt=%d\n",fd,PerlIO_fd_refcnt[fd]);
+ if (fd >= PL_perlio_fd_refcnt_size)
+ S_more_refcounted_fds(aTHX_ fd);
+
+ PL_perlio_fd_refcnt[fd]++;
+ PerlIO_debug("fd %d refcnt=%d\n",fd,PL_perlio_fd_refcnt[fd]);
+
#ifdef USE_THREADS
MUTEX_UNLOCK(&PerlIO_mutex);
#endif
int
PerlIOUnix_refcnt_dec(int fd)
{
+ dTHX;
int cnt = 0;
- if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
+ if (fd >= 0) {
+ dVAR;
#ifdef USE_THREADS
MUTEX_LOCK(&PerlIO_mutex);
#endif
- cnt = --PerlIO_fd_refcnt[fd];
+ /* XXX should this be a panic? */
+ if (fd >= PL_perlio_fd_refcnt_size)
+ S_more_refcounted_fds(aTHX_ fd);
+
+ /* XXX should this be a panic if it drops below 0? */
+ cnt = --PL_perlio_fd_refcnt[fd];
PerlIO_debug("fd %d refcnt=%d\n",fd,cnt);
#ifdef USE_THREADS
MUTEX_UNLOCK(&PerlIO_mutex);
void
PerlIO_cleanup(pTHX)
{
+ dVAR;
int i;
#ifdef USE_ITHREADS
PerlIO_debug("Cleanup layers for %p\n",aTHX);
PerlIO_list_free(aTHX_ PL_known_layers);
PL_known_layers = NULL;
}
- if(PL_def_layerlist) {
+ if (PL_def_layerlist) {
PerlIO_list_free(aTHX_ PL_def_layerlist);
PL_def_layerlist = NULL;
}
PerlIOUnix_oflags(const char *mode)
{
int oflags = -1;
- if (*mode == 'I' || *mode == '#')
+ if (*mode == IoTYPE_IMPLICIT || *mode == IoTYPE_NUMERIC)
mode++;
switch (*mode) {
case 'r':
IV
PerlIOUnix_fileno(pTHX_ PerlIO *f)
{
+ PERL_UNUSED_CONTEXT;
return PerlIOSelf(f, PerlIOUnix)->fd;
}
static void
PerlIOUnix_setfd(pTHX_ PerlIO *f, int fd, int imode)
{
- PerlIOUnix *s = PerlIOSelf(f, PerlIOUnix);
+ PerlIOUnix * const s = PerlIOSelf(f, PerlIOUnix);
#if defined(WIN32)
Stat_t st;
if (PerlLIO_fstat(fd, &st) == 0) {
s->fd = fd;
s->oflags = imode;
PerlIOUnix_refcnt_inc(fd);
+ PERL_UNUSED_CONTEXT;
}
IV
return code;
}
+IV
+PerlIOUnix_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
+{
+ const int fd = PerlIOSelf(f, PerlIOUnix)->fd;
+ Off_t new_loc;
+ PERL_UNUSED_CONTEXT;
+ if (PerlIOBase(f)->flags & PERLIO_F_NOTREG) {
+#ifdef ESPIPE
+ SETERRNO(ESPIPE, LIB_INVARG);
+#else
+ SETERRNO(EINVAL, LIB_INVARG);
+#endif
+ return -1;
+ }
+ new_loc = PerlLIO_lseek(fd, offset, whence);
+ if (new_loc == (Off_t) - 1)
+ return -1;
+ PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
+ return 0;
+}
+
PerlIO *
PerlIOUnix_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
IV n, const char *mode, int fd, int imode,
(*PerlIOBase(f)->tab->Close)(aTHX_ f);
}
if (narg > 0) {
- char *path = SvPV_nolen(*args);
- if (*mode == '#')
+ if (*mode == IoTYPE_NUMERIC)
mode++;
else {
imode = PerlIOUnix_oflags(mode);
perm = 0666;
}
if (imode != -1) {
+ const char *path = SvPV_nolen_const(*args);
fd = PerlLIO_open3(path, imode, perm);
}
}
if (fd >= 0) {
- if (*mode == 'I')
+ if (*mode == IoTYPE_IMPLICIT)
mode++;
if (!f) {
f = PerlIO_allocate(aTHX);
}
PerlIOUnix_setfd(aTHX_ f, fd, imode);
PerlIOBase(f)->flags |= PERLIO_F_OPEN;
+ if (*mode == IoTYPE_APPEND)
+ PerlIOUnix_seek(aTHX_ f, 0, SEEK_END);
return f;
}
else {
if (f) {
+ /*EMPTY*/;
/*
* FIXME: pop layers ???
*/
PerlIO *
PerlIOUnix_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
{
- PerlIOUnix *os = PerlIOSelf(o, PerlIOUnix);
+ const PerlIOUnix * const os = PerlIOSelf(o, PerlIOUnix);
int fd = os->fd;
if (flags & PERLIO_DUP_FD) {
fd = PerlLIO_dup(fd);
}
- if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
+ if (fd >= 0) {
f = PerlIOBase_dup(aTHX_ f, o, param, flags);
if (f) {
/* If all went well overwrite fd in dup'ed lay with the dup()'ed fd */
SSize_t
PerlIOUnix_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
{
- int fd = PerlIOSelf(f, PerlIOUnix)->fd;
+ dVAR;
+ const int fd = PerlIOSelf(f, PerlIOUnix)->fd;
+#ifdef PERLIO_STD_SPECIAL
+ if (fd == 0)
+ return PERLIO_STD_IN(fd, vbuf, count);
+#endif
if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD) ||
PerlIOBase(f)->flags & (PERLIO_F_EOF|PERLIO_F_ERROR)) {
return 0;
}
while (1) {
- SSize_t len = PerlLIO_read(fd, vbuf, count);
+ const SSize_t len = PerlLIO_read(fd, vbuf, count);
if (len >= 0 || errno != EINTR) {
if (len < 0) {
if (errno != EAGAIN) {
}
PERL_ASYNC_CHECK();
}
+ /*NOTREACHED*/
}
SSize_t
PerlIOUnix_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
{
- int fd = PerlIOSelf(f, PerlIOUnix)->fd;
+ dVAR;
+ const int fd = PerlIOSelf(f, PerlIOUnix)->fd;
+#ifdef PERLIO_STD_SPECIAL
+ if (fd == 1 || fd == 2)
+ return PERLIO_STD_OUT(fd, vbuf, count);
+#endif
while (1) {
- SSize_t len = PerlLIO_write(fd, vbuf, count);
+ const SSize_t len = PerlLIO_write(fd, vbuf, count);
if (len >= 0 || errno != EINTR) {
if (len < 0) {
if (errno != EAGAIN) {
}
PERL_ASYNC_CHECK();
}
-}
-
-IV
-PerlIOUnix_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
-{
- int fd = PerlIOSelf(f, PerlIOUnix)->fd;
- Off_t new;
- if (PerlIOBase(f)->flags & PERLIO_F_NOTREG) {
-#ifdef ESPIPE
- SETERRNO(ESPIPE, LIB_INVARG);
-#else
- SETERRNO(EINVAL, LIB_INVARG);
-#endif
- return -1;
- }
- new = PerlLIO_lseek(fd, offset, whence);
- if (new == (Off_t) - 1)
- {
- return -1;
- }
- PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
- return 0;
+ /*NOTREACHED*/
}
Off_t
PerlIOUnix_tell(pTHX_ PerlIO *f)
{
+ PERL_UNUSED_CONTEXT;
+
return PerlLIO_lseek(PerlIOSelf(f, PerlIOUnix)->fd, 0, SEEK_CUR);
}
IV
PerlIOUnix_close(pTHX_ PerlIO *f)
{
- int fd = PerlIOSelf(f, PerlIOUnix)->fd;
+ dVAR;
+ const int fd = PerlIOSelf(f, PerlIOUnix)->fd;
int code = 0;
if (PerlIOBase(f)->flags & PERLIO_F_OPEN) {
if (PerlIOUnix_refcnt_dec(fd) > 0) {
return code;
}
-PerlIO_funcs PerlIO_unix = {
+PERLIO_FUNCS_DECL(PerlIO_unix) = {
sizeof(PerlIO_funcs),
"unix",
sizeof(PerlIOUnix),
IV
PerlIOStdio_fileno(pTHX_ PerlIO *f)
{
- FILE *s;
- if (PerlIOValid(f) && (s = PerlIOSelf(f, PerlIOStdio)->stdio)) {
- return PerlSIO_fileno(s);
+ PERL_UNUSED_CONTEXT;
+
+ if (PerlIOValid(f)) {
+ FILE * const s = PerlIOSelf(f, PerlIOStdio)->stdio;
+ if (s)
+ return PerlSIO_fileno(s);
}
errno = EBADF;
return -1;
char *
PerlIOStdio_mode(const char *mode, char *tmode)
{
- char *ret = tmode;
- while (*mode) {
- *tmode++ = *mode++;
+ char * const ret = tmode;
+ if (mode) {
+ while (*mode) {
+ *tmode++ = *mode++;
+ }
}
-#ifdef PERLIO_USING_CRLF
+#if defined(PERLIO_USING_CRLF) || defined(__CYGWIN__)
*tmode++ = 'b';
#endif
*tmode = '\0';
{
PerlIO *n;
if (PerlIOValid(f) && PerlIOValid(n = PerlIONext(f))) {
- PerlIO_funcs *toptab = PerlIOBase(n)->tab;
+ PerlIO_funcs * const toptab = PerlIOBase(n)->tab;
if (toptab == tab) {
/* Top is already stdio - pop self (duplicate) and use original */
PerlIO_pop(aTHX_ f);
return 0;
} else {
- int fd = PerlIO_fileno(n);
+ const int fd = PerlIO_fileno(n);
char tmode[8];
FILE *stdio;
if (fd >= 0 && (stdio = PerlSIO_fdopen(fd,
Note that the errno value set by a failing fdopen
varies between stdio implementations.
*/
- int fd = PerlLIO_dup(fileno(stdio));
+ const int fd = PerlLIO_dup(fileno(stdio));
FILE *f2 = PerlSIO_fdopen(fd, (mode = "r+"));
if (!f2) {
f2 = PerlSIO_fdopen(fd, (mode = "w"));
}
fclose(f2);
}
- if ((f = PerlIO_push(aTHX_(f = PerlIO_allocate(aTHX)), &PerlIO_stdio, mode, Nullsv))) {
+ if ((f = PerlIO_push(aTHX_(f = PerlIO_allocate(aTHX)), PERLIO_FUNCS_CAST(&PerlIO_stdio), mode, NULL))) {
s = PerlIOSelf(f, PerlIOStdio);
s->stdio = stdio;
}
{
char tmode[8];
if (PerlIOValid(f)) {
- char *path = SvPV_nolen(*args);
- PerlIOStdio *s = PerlIOSelf(f, PerlIOStdio);
+ const char * const path = SvPV_nolen_const(*args);
+ PerlIOStdio * const s = PerlIOSelf(f, PerlIOStdio);
FILE *stdio;
PerlIOUnix_refcnt_dec(fileno(s->stdio));
stdio = PerlSIO_freopen(path, (mode = PerlIOStdio_mode(mode, tmode)),
}
else {
if (narg > 0) {
- char *path = SvPV_nolen(*args);
- if (*mode == '#') {
+ const char * const path = SvPV_nolen_const(*args);
+ if (*mode == IoTYPE_NUMERIC) {
mode++;
fd = PerlLIO_open3(path, imode, perm);
}
else {
- FILE *stdio = PerlSIO_fopen(path, mode);
+ FILE *stdio;
+ bool appended = FALSE;
+#ifdef __CYGWIN__
+ /* Cygwin wants its 'b' early. */
+ appended = TRUE;
+ mode = PerlIOStdio_mode(mode, tmode);
+#endif
+ stdio = PerlSIO_fopen(path, mode);
if (stdio) {
- PerlIOStdio *s;
if (!f) {
f = PerlIO_allocate(aTHX);
}
- if ((f = PerlIO_push(aTHX_ f, self,
- (mode = PerlIOStdio_mode(mode, tmode)),
- PerlIOArg))) {
- s = PerlIOSelf(f, PerlIOStdio);
- s->stdio = stdio;
- PerlIOUnix_refcnt_inc(fileno(s->stdio));
+ if (!appended)
+ mode = PerlIOStdio_mode(mode, tmode);
+ f = PerlIO_push(aTHX_ f, self, mode, PerlIOArg);
+ if (f) {
+ PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
+ PerlIOUnix_refcnt_inc(fileno(stdio));
+ } else {
+ PerlSIO_fclose(stdio);
}
return f;
}
if (fd >= 0) {
FILE *stdio = NULL;
int init = 0;
- if (*mode == 'I') {
+ if (*mode == IoTYPE_IMPLICIT) {
init = 1;
mode++;
}
PerlIOStdio_mode(mode, tmode));
}
if (stdio) {
- PerlIOStdio *s;
if (!f) {
f = PerlIO_allocate(aTHX);
}
if ((f = PerlIO_push(aTHX_ f, self, mode, PerlIOArg))) {
- s = PerlIOSelf(f, PerlIOStdio);
+ PerlIOStdio * const s = PerlIOSelf(f, PerlIOStdio);
s->stdio = stdio;
PerlIOUnix_refcnt_inc(fileno(s->stdio));
}
*/
if ((f = PerlIOBase_dup(aTHX_ f, o, param, flags))) {
FILE *stdio = PerlIOSelf(o, PerlIOStdio)->stdio;
- int fd = fileno(stdio);
+ const int fd = fileno(stdio);
char mode[8];
if (flags & PERLIO_DUP_FD) {
- int dfd = PerlLIO_dup(fileno(stdio));
+ const int dfd = PerlLIO_dup(fileno(stdio));
if (dfd >= 0) {
stdio = PerlSIO_fdopen(dfd, PerlIO_modestr(o,mode));
goto set_this;
}
else {
+ /*EMPTY*/;
/* FIXME: To avoid messy error recovery if dup fails
re-use the existing stdio as though flag was not set
*/
static int
PerlIOStdio_invalidate_fileno(pTHX_ FILE *f)
{
+ PERL_UNUSED_CONTEXT;
+
/* XXX this could use PerlIO_canset_fileno() and
* PerlIO_set_fileno() support from Configure
*/
-# if defined(__GLIBC__)
+# if defined(__UCLIBC__)
+ /* uClibc must come before glibc because it defines __GLIBC__ as well. */
+ f->__filedes = -1;
+ return 1;
+# elif defined(__GLIBC__)
/* There may be a better way for GLIBC:
- libio.h defines a flag to not close() on cleanup
*/
f->_fileno = -1;
return 1;
# elif defined(__sun__)
-# if defined(_LP64)
- /* On solaris, if _LP64 is defined, the FILE structure is this:
- *
- * struct FILE {
- * long __pad[16];
- * };
- *
- * It turns out that the fd is stored in the top 32 bits of
- * file->__pad[4]. The lower 32 bits contain flags. file->pad[5] appears
- * to contain a pointer or offset into another structure. All the
- * remaining fields are zero.
- *
- * We set the top bits to -1 (0xFFFFFFFF).
- */
- f->__pad[4] |= 0xffffffff00000000L;
- assert(fileno(f) == 0xffffffff);
-# else /* !defined(_LP64) */
- /* _file is just a unsigned char :-(
- Not clear why we dup() rather than using -1
- even if that would be treated as 0xFF - so will
- a dup fail ...
- */
- f->_file = PerlLIO_dup(fileno(f));
-# endif /* defined(_LP64) */
- return 1;
+ return 0;
# elif defined(__hpux)
f->__fileH = 0xff;
f->__fileL = 0xff;
*/
f->_file = -1;
return 1;
+# elif defined(__OpenBSD__)
+ /* There may be a better way on OpenBSD:
+ - we could insert a dummy func in the _close function entry
+ f->_close = (int (*)(void *)) dummy_close;
+ */
+ f->_file = -1;
+ return 1;
+# elif defined(__EMX__)
+ /* f->_flags &= ~_IOOPEN; */ /* Will leak stream->_buffer */
+ f->_handle = -1;
+ return 1;
# elif defined(__CYGWIN__)
/* There may be a better way on CYGWIN:
- we could insert a dummy func in the _close function entry
*/
# error "Don't know how to set FILE.fileno on your platform"
#endif
+ PERL_UNUSED_ARG(f);
return 0;
# endif
}
IV
PerlIOStdio_close(pTHX_ PerlIO *f)
{
- FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
+ FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
if (!stdio) {
errno = EBADF;
return -1;
}
else {
- int fd = fileno(stdio);
- int socksfd = 0;
+ const int fd = fileno(stdio);
int invalidate = 0;
IV result = 0;
int saveerr = 0;
*/
int optval;
Sock_size_t optlen = sizeof(int);
- if (getsockopt(fd, SOL_SOCKET, SO_TYPE, (void *) &optval, &optlen) == 0) {
- socksfd = 1;
+ if (getsockopt(fd, SOL_SOCKET, SO_TYPE, (void *) &optval, &optlen) == 0)
invalidate = 1;
- }
#endif
- if (PerlIOUnix_refcnt_dec(fd) > 0) {
- /* File descriptor still in use */
+ if (PerlIOUnix_refcnt_dec(fd) > 0) /* File descriptor still in use */
invalidate = 1;
- socksfd = 0;
- }
if (invalidate) {
- /* For STD* handles don't close the stdio at all
- this is because we have shared the FILE * too
- */
- if (stdio == stdin) {
- /* Some stdios are buggy fflush-ing inputs */
- return 0;
- }
- else if (stdio == stdout || stdio == stderr) {
- return PerlIO_flush(f);
- }
+ /* For STD* handles, don't close stdio, since we shared the FILE *, too. */
+ if (stdio == stdin) /* Some stdios are buggy fflush-ing inputs */
+ return 0;
+ if (stdio == stdout || stdio == stderr)
+ return PerlIO_flush(f);
/* Tricky - must fclose(stdio) to free memory but not close(fd)
Use Sarathy's trick from maint-5.6 to invalidate the
fileno slot of the FILE *
*/
result = PerlIO_flush(f);
saveerr = errno;
- if (!(invalidate = PerlIOStdio_invalidate_fileno(aTHX_ stdio))) {
- dupfd = PerlLIO_dup(fd);
- }
+ invalidate = PerlIOStdio_invalidate_fileno(aTHX_ stdio);
+ if (!invalidate)
+ dupfd = PerlLIO_dup(fd);
}
result = PerlSIO_fclose(stdio);
/* We treat error from stdio as success if we invalidated
errno = saveerr;
result = 0;
}
- if (socksfd) {
- /* in SOCKS case let close() determine return value */
- result = close(fd);
- }
+#ifdef SOCKS5_VERSION_NAME
+ /* in SOCKS' case, let close() determine return value */
+ result = close(fd);
+#endif
if (dupfd) {
PerlLIO_dup2(dupfd,fd);
PerlLIO_close(dupfd);
SSize_t
PerlIOStdio_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
{
- FILE *s = PerlIOSelf(f, PerlIOStdio)->stdio;
+ dVAR;
+ FILE * const s = PerlIOSelf(f, PerlIOStdio)->stdio;
SSize_t got = 0;
for (;;) {
if (count == 1) {
* Perl is expecting PerlIO_getc() to fill the buffer Linux's
* stdio does not do that for fread()
*/
- int ch = PerlSIO_fgetc(s);
+ const int ch = PerlSIO_fgetc(s);
if (ch != EOF) {
*buf = ch;
got = 1;
}
else
got = PerlSIO_fread(vbuf, 1, count, s);
+ if (got == 0 && PerlSIO_ferror(s))
+ got = -1;
if (got >= 0 || errno != EINTR)
break;
PERL_ASYNC_CHECK();
PerlIOStdio_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
{
SSize_t unread = 0;
- FILE *s = PerlIOSelf(f, PerlIOStdio)->stdio;
+ FILE * const s = PerlIOSelf(f, PerlIOStdio)->stdio;
#ifdef STDIO_BUFFER_WRITABLE
if (PerlIO_fast_gets(f) && PerlIO_has_base(f)) {
STDCHAR *eptr = (STDCHAR*)PerlSIO_get_ptr(s);
STDCHAR *buf = ((STDCHAR *) vbuf) + count;
while (count > 0) {
- int ch = *--buf & 0xFF;
+ const int ch = *--buf & 0xFF;
if (ungetc(ch,s) != ch) {
/* ungetc did not work */
break;
SSize_t
PerlIOStdio_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
{
+ dVAR;
SSize_t got;
for (;;) {
got = PerlSIO_fwrite(vbuf, 1, count,
IV
PerlIOStdio_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
{
- FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
+ FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
+ PERL_UNUSED_CONTEXT;
+
return PerlSIO_fseek(stdio, offset, whence);
}
Off_t
PerlIOStdio_tell(pTHX_ PerlIO *f)
{
- FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
+ FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
+ PERL_UNUSED_CONTEXT;
+
return PerlSIO_ftell(stdio);
}
IV
PerlIOStdio_flush(pTHX_ PerlIO *f)
{
- FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
+ FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
+ PERL_UNUSED_CONTEXT;
+
if (PerlIOBase(f)->flags & PERLIO_F_CANWRITE) {
return PerlSIO_fflush(stdio);
}
else {
+ /*EMPTY*/;
#if 0
/*
* FIXME: This discards ungetc() and pre-read stuff which is not
/*
* Not writeable - sync by attempting a seek
*/
- int err = errno;
+ const int err = errno;
if (PerlSIO_fseek(stdio, (Off_t) 0, SEEK_CUR) != 0)
errno = err;
#endif
IV
PerlIOStdio_eof(pTHX_ PerlIO *f)
{
+ PERL_UNUSED_CONTEXT;
+
return PerlSIO_feof(PerlIOSelf(f, PerlIOStdio)->stdio);
}
IV
PerlIOStdio_error(pTHX_ PerlIO *f)
{
+ PERL_UNUSED_CONTEXT;
+
return PerlSIO_ferror(PerlIOSelf(f, PerlIOStdio)->stdio);
}
void
PerlIOStdio_clearerr(pTHX_ PerlIO *f)
{
+ PERL_UNUSED_CONTEXT;
+
PerlSIO_clearerr(PerlIOSelf(f, PerlIOStdio)->stdio);
}
void
PerlIOStdio_setlinebuf(pTHX_ PerlIO *f)
{
+ PERL_UNUSED_CONTEXT;
+
#ifdef HAS_SETLINEBUF
PerlSIO_setlinebuf(PerlIOSelf(f, PerlIOStdio)->stdio);
#else
- PerlSIO_setvbuf(PerlIOSelf(f, PerlIOStdio)->stdio, Nullch, _IOLBF, 0);
+ PerlSIO_setvbuf(PerlIOSelf(f, PerlIOStdio)->stdio, NULL, _IOLBF, 0);
#endif
}
STDCHAR *
PerlIOStdio_get_base(pTHX_ PerlIO *f)
{
- FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
+ FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
return (STDCHAR*)PerlSIO_get_base(stdio);
}
Size_t
PerlIOStdio_get_bufsiz(pTHX_ PerlIO *f)
{
- FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
+ FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
return PerlSIO_get_bufsiz(stdio);
}
#endif
STDCHAR *
PerlIOStdio_get_ptr(pTHX_ PerlIO *f)
{
- FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
+ FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
return (STDCHAR*)PerlSIO_get_ptr(stdio);
}
SSize_t
PerlIOStdio_get_cnt(pTHX_ PerlIO *f)
{
- FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
+ FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
return PerlSIO_get_cnt(stdio);
}
void
PerlIOStdio_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
{
- FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
+ FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
if (ptr != NULL) {
#ifdef STDIO_PTR_LVALUE
PerlSIO_set_ptr(stdio, (void*)ptr); /* LHS STDCHAR* cast non-portable */
IV
PerlIOStdio_fill(pTHX_ PerlIO *f)
{
- FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
+ FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
int c;
+ PERL_UNUSED_CONTEXT;
+
/*
* fflush()ing read-only streams can cause trouble on some stdio-s
*/
-PerlIO_funcs PerlIO_stdio = {
+PERLIO_FUNCS_DECL(PerlIO_stdio) = {
sizeof(PerlIO_funcs),
"stdio",
sizeof(PerlIOStdio),
stdio = PerlSIO_fdopen(PerlIO_fileno(f), mode);
if (stdio) {
PerlIOl *l = *f;
+ PerlIO *f2;
/* De-link any lower layers so new :stdio sticks */
*f = NULL;
- if ((f = PerlIO_push(aTHX_ f, &PerlIO_stdio, buf, Nullsv))) {
- PerlIOStdio *s = PerlIOSelf(f, PerlIOStdio);
+ if ((f2 = PerlIO_push(aTHX_ f, PERLIO_FUNCS_CAST(&PerlIO_stdio), buf, NULL))) {
+ PerlIOStdio *s = PerlIOSelf((f = f2), PerlIOStdio);
s->stdio = stdio;
/* Link previous lower layers under new one */
*PerlIONext(f) = l;
l = *PerlIONext(&l);
}
/* Uses fallback "mode" via PerlIO_modestr() in PerlIO_exportFILE */
- return PerlIO_exportFILE(f, Nullch);
+ return PerlIO_exportFILE(f, NULL);
}
/* Use this to reverse PerlIO_exportFILE calls. */
void
PerlIO_releaseFILE(PerlIO *p, FILE *f)
{
+ dVAR;
PerlIOl *l;
while ((l = *p)) {
if (l->tab == &PerlIO_stdio) {
PerlIOBuf_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
{
PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
- int fd = PerlIO_fileno(f);
+ const int fd = PerlIO_fileno(f);
if (fd >= 0 && PerlLIO_isatty(fd)) {
PerlIOBase(f)->flags |= PERLIO_F_LINEBUF | PERLIO_F_TTY;
}
if (*PerlIONext(f)) {
- Off_t posn = PerlIO_tell(PerlIONext(f));
+ const Off_t posn = PerlIO_tell(PerlIONext(f));
if (posn != (Off_t) - 1) {
b->posn = posn;
}
{
if (PerlIOValid(f)) {
PerlIO *next = PerlIONext(f);
- PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIOBase(next)->tab);
- next = (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
- next, narg, args);
+ PerlIO_funcs *tab =
+ PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIOBase(next)->tab);
+ if (tab && tab->Open)
+ next =
+ (*tab->Open)(aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
+ next, narg, args);
if (!next || (*PerlIOBase(f)->tab->Pushed) (aTHX_ f, mode, PerlIOArg, self) != 0) {
return NULL;
}
else {
PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIO_default_btm());
int init = 0;
- if (*mode == 'I') {
+ if (*mode == IoTYPE_IMPLICIT) {
init = 1;
/*
* mode++;
*/
}
- f = (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
- f, narg, args);
+ if (tab && tab->Open)
+ f = (*tab->Open)(aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
+ f, narg, args);
+ else
+ SETERRNO(EINVAL, LIB_INVARG);
if (f) {
if (PerlIO_push(aTHX_ f, self, mode, PerlIOArg) == 0) {
/*
#ifdef PERLIO_USING_CRLF
# ifdef PERLIO_IS_BINMODE_FD
if (PERLIO_IS_BINMODE_FD(fd))
- PerlIO_binmode(aTHX_ f, '<'/*not used*/, O_BINARY, Nullch);
+ PerlIO_binmode(aTHX_ f, '<'/*not used*/, O_BINARY, NULL);
else
# endif
/*
/*
* This "flush" is akin to sfio's sync in that it handles files in either
- * read or write state
+ * read or write state. For write state, we put the postponed data through
+ * the next layers. For read state, we seek() the next layers to the
+ * offset given by current position in the buffer, and discard the buffer
+ * state (XXXX supposed to be for seek()able buffers only, but now it is done
+ * in any case?). Then the pass the stick further in chain.
*/
IV
PerlIOBuf_flush(pTHX_ PerlIO *f)
{
- PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
+ PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
int code = 0;
PerlIO *n = PerlIONext(f);
if (PerlIOBase(f)->flags & PERLIO_F_WRBUF) {
/*
* write() the buffer
*/
- STDCHAR *buf = b->buf;
- STDCHAR *p = buf;
+ const STDCHAR *buf = b->buf;
+ const STDCHAR *p = buf;
while (p < b->ptr) {
SSize_t count = PerlIO_write(n, p, b->ptr - p);
if (count > 0) {
return code;
}
+/* This discards the content of the buffer after b->ptr, and rereads
+ * the buffer from the position off in the layer downstream; here off
+ * is at offset corresponding to b->ptr - b->buf.
+ */
IV
PerlIOBuf_fill(pTHX_ PerlIO *f)
{
- PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
+ PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
PerlIO *n = PerlIONext(f);
SSize_t avail;
/*
* Down-stream flush is defined not to loose read data so is harmless.
* we would not normally be fill'ing if there was data left in anycase.
*/
- if (PerlIO_flush(f) != 0)
+ if (PerlIO_flush(f) != 0) /* XXXX Check that its seek() succeeded?! */
return -1;
if (PerlIOBase(f)->flags & PERLIO_F_TTY)
PerlIOBase_flush_linebuf(aTHX);
if (!b->buf)
PerlIO_get_base(f); /* allocate via vtable */
+ assert(b->buf); /* The b->buf does get allocated via the vtable system. */
+
b->ptr = b->end = b->buf;
if (!PerlIOValid(n)) {
}
if (avail > 0) {
STDCHAR *ptr = PerlIO_get_ptr(n);
- SSize_t cnt = avail;
+ const SSize_t cnt = avail;
if (avail > (SSize_t)b->bufsiz)
avail = b->bufsiz;
Copy(ptr, b->buf, avail, STDCHAR);
SSize_t
PerlIOBuf_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
{
- PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
if (PerlIOValid(f)) {
+ const PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
if (!b->ptr)
PerlIO_get_base(f);
return PerlIOBase_read(aTHX_ f, vbuf, count);
PerlIOBuf_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
{
const STDCHAR *buf = (const STDCHAR *) vbuf + count;
- PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
+ PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
SSize_t unread = 0;
SSize_t avail;
if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
SSize_t
PerlIOBuf_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
{
- PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
+ PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
const STDCHAR *buf = (const STDCHAR *) vbuf;
+ const STDCHAR *flushptr = buf;
Size_t written = 0;
if (!b->buf)
PerlIO_get_base(f);
return 0;
}
}
+ if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF) {
+ flushptr = buf + count;
+ while (flushptr > buf && *(flushptr - 1) != '\n')
+ --flushptr;
+ }
while (count > 0) {
SSize_t avail = b->bufsiz - (b->ptr - b->buf);
if ((SSize_t) count < avail)
avail = count;
+ if (flushptr > buf && flushptr <= buf + avail)
+ avail = flushptr - buf;
PerlIOBase(f)->flags |= PERLIO_F_WRBUF;
- if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF) {
- while (avail > 0) {
- int ch = *buf++;
- *(b->ptr)++ = ch;
- count--;
- avail--;
- written++;
- if (ch == '\n') {
- PerlIO_flush(f);
- break;
- }
- }
- }
- else {
- if (avail) {
- Copy(buf, b->ptr, avail, STDCHAR);
- count -= avail;
- buf += avail;
- written += avail;
- b->ptr += avail;
- }
+ if (avail) {
+ Copy(buf, b->ptr, avail, STDCHAR);
+ count -= avail;
+ buf += avail;
+ written += avail;
+ b->ptr += avail;
+ if (buf == flushptr)
+ PerlIO_flush(f);
}
if (b->ptr >= (b->buf + b->bufsiz))
PerlIO_flush(f);
{
IV code;
if ((code = PerlIO_flush(f)) == 0) {
- PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
code = PerlIO_seek(PerlIONext(f), offset, whence);
if (code == 0) {
+ PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
b->posn = PerlIO_tell(PerlIONext(f));
}
}
Off_t
PerlIOBuf_tell(pTHX_ PerlIO *f)
{
- PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
+ PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
/*
* b->posn is file position where b->buf was read, or will be written
*/
IV
PerlIOBuf_popped(pTHX_ PerlIO *f)
{
- IV code = PerlIOBase_popped(aTHX_ f);
- PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
+ const IV code = PerlIOBase_popped(aTHX_ f);
+ PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
Safefree(b->buf);
}
- b->buf = NULL;
- b->ptr = b->end = b->buf;
+ b->ptr = b->end = b->buf = NULL;
PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
return code;
}
IV
PerlIOBuf_close(pTHX_ PerlIO *f)
{
- IV code = PerlIOBase_close(aTHX_ f);
- PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
+ const IV code = PerlIOBase_close(aTHX_ f);
+ PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
Safefree(b->buf);
}
- b->buf = NULL;
- b->ptr = b->end = b->buf;
+ b->ptr = b->end = b->buf = NULL;
PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
return code;
}
STDCHAR *
PerlIOBuf_get_ptr(pTHX_ PerlIO *f)
{
- PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
+ const PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
if (!b->buf)
PerlIO_get_base(f);
return b->ptr;
SSize_t
PerlIOBuf_get_cnt(pTHX_ PerlIO *f)
{
- PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
+ const PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
if (!b->buf)
PerlIO_get_base(f);
if (PerlIOBase(f)->flags & PERLIO_F_RDBUF)
STDCHAR *
PerlIOBuf_get_base(pTHX_ PerlIO *f)
{
- PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
+ PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
+ PERL_UNUSED_CONTEXT;
+
if (!b->buf) {
if (!b->bufsiz)
b->bufsiz = 4096;
- b->buf =
- Newz('B',b->buf,b->bufsiz, STDCHAR);
+ b->buf = Newxz(b->buf,b->bufsiz, STDCHAR);
if (!b->buf) {
b->buf = (STDCHAR *) & b->oneword;
b->bufsiz = sizeof(b->oneword);
}
- b->ptr = b->buf;
- b->end = b->ptr;
+ b->end = b->ptr = b->buf;
}
return b->buf;
}
Size_t
PerlIOBuf_bufsiz(pTHX_ PerlIO *f)
{
- PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
+ const PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
if (!b->buf)
PerlIO_get_base(f);
return (b->end - b->buf);
void
PerlIOBuf_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
{
- PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
+ PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
if (!b->buf)
PerlIO_get_base(f);
b->ptr = ptr;
-PerlIO_funcs PerlIO_perlio = {
+PERLIO_FUNCS_DECL(PerlIO_perlio) = {
sizeof(PerlIO_funcs),
"perlio",
sizeof(PerlIOBuf),
IV
PerlIOPending_flush(pTHX_ PerlIO *f)
{
- PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
+ PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
Safefree(b->buf);
b->buf = NULL;
IV
PerlIOPending_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
{
- IV code = PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
- PerlIOl *l = PerlIOBase(f);
+ const IV code = PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
+ PerlIOl * const l = PerlIOBase(f);
/*
* Our PerlIO_fast_gets must match what we are pushed on, or sv_gets()
* etc. get muddled when it changes mid-string when we auto-pop.
if (avail > 0)
got = PerlIOBuf_read(aTHX_ f, vbuf, avail);
if (got >= 0 && got < (SSize_t)count) {
- SSize_t more =
+ const SSize_t more =
PerlIO_read(f, ((STDCHAR *) vbuf) + got, count - got);
if (more >= 0 || got == 0)
got += more;
return got;
}
-PerlIO_funcs PerlIO_pending = {
+PERLIO_FUNCS_DECL(PerlIO_pending) = {
sizeof(PerlIO_funcs),
"pending",
sizeof(PerlIOBuf),
* crlf - translation On read translate CR,LF to "\n" we do this by
* overriding ptr/cnt entries to hand back a line at a time and keeping a
* record of which nl we "lied" about. On write translate "\n" to CR,LF
+ *
+ * c->nl points on the first byte of CR LF pair when it is temporarily
+ * replaced by LF, or to the last CR of the buffer. In the former case
+ * the caller thinks that the buffer ends at c->nl + 1, in the latter
+ * that it ends at c->nl; these two cases can be distinguished by
+ * *c->nl. c->nl is set during _getcnt() call, and unset during
+ * _unread() and _flush() calls.
+ * It only matters for read operations.
*/
typedef struct {
f, PerlIOBase(f)->tab->name, (mode) ? mode : "(Null)",
PerlIOBase(f)->flags);
#endif
+ {
+ /* Enable the first CRLF capable layer you can find, but if none
+ * found, the one we just pushed is fine. This results in at
+ * any given moment at most one CRLF-capable layer being enabled
+ * in the whole layer stack. */
+ PerlIO *g = PerlIONext(f);
+ while (g && *g) {
+ PerlIOl *b = PerlIOBase(g);
+ if (b && b->tab == &PerlIO_crlf) {
+ if (!(b->flags & PERLIO_F_CRLF))
+ b->flags |= PERLIO_F_CRLF;
+ PerlIO_pop(aTHX_ f);
+ return code;
+ }
+ g = PerlIONext(g);
+ }
+ }
return code;
}
SSize_t
PerlIOCrlf_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
{
- PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
- if (c->nl) {
+ PerlIOCrlf * const c = PerlIOSelf(f, PerlIOCrlf);
+ if (c->nl) { /* XXXX Shouldn't it be done only if b->ptr > c->nl? */
*(c->nl) = 0xd;
c->nl = NULL;
}
b->posn -= b->bufsiz;
}
while (count > 0 && b->ptr > b->buf) {
- int ch = *--buf;
+ const int ch = *--buf;
if (ch == '\n') {
if (b->ptr - 2 >= b->buf) {
*--(b->ptr) = 0xa;
count--;
}
else {
- buf++;
- break;
+ /* If b->ptr - 1 == b->buf, we are undoing reading 0xa */
+ *--(b->ptr) = 0xa; /* Works even if 0xa == '\r' */
+ unread++;
+ count--;
}
}
else {
}
}
+/* XXXX This code assumes that buffer size >=2, but does not check it... */
SSize_t
PerlIOCrlf_get_cnt(pTHX_ PerlIO *f)
{
- PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
+ PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
if (!b->buf)
PerlIO_get_base(f);
if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
- PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
+ PerlIOCrlf * const c = PerlIOSelf(f, PerlIOCrlf);
if ((PerlIOBase(f)->flags & PERLIO_F_CRLF) && (!c->nl || *c->nl == 0xd)) {
STDCHAR *nl = (c->nl) ? c->nl : b->ptr;
scan:
void
PerlIOCrlf_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
{
- PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
- PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
+ PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
+ PerlIOCrlf * const c = PerlIOSelf(f, PerlIOCrlf);
if (!b->buf)
PerlIO_get_base(f);
if (!ptr) {
ptr -= cnt;
}
else {
+ /*EMPTY*/;
#if 0
/*
* Test code - delete when it works ...
if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF))
return PerlIOBuf_write(aTHX_ f, vbuf, count);
else {
- PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
+ PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
const STDCHAR *buf = (const STDCHAR *) vbuf;
- const STDCHAR *ebuf = buf + count;
+ const STDCHAR * const ebuf = buf + count;
if (!b->buf)
PerlIO_get_base(f);
if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
return 0;
while (buf < ebuf) {
- STDCHAR *eptr = b->buf + b->bufsiz;
+ const STDCHAR * const eptr = b->buf + b->bufsiz;
PerlIOBase(f)->flags |= PERLIO_F_WRBUF;
while (buf < ebuf && b->ptr < eptr) {
if (*buf == '\n') {
}
}
else {
- int ch = *buf++;
- *(b->ptr)++ = ch;
+ *(b->ptr)++ = *buf++;
}
if (b->ptr >= eptr) {
PerlIO_flush(f);
IV
PerlIOCrlf_flush(pTHX_ PerlIO *f)
{
- PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
+ PerlIOCrlf * const c = PerlIOSelf(f, PerlIOCrlf);
if (c->nl) {
*(c->nl) = 0xd;
c->nl = NULL;
return 0;
}
-PerlIO_funcs PerlIO_crlf = {
+PERLIO_FUNCS_DECL(PerlIO_crlf) = {
sizeof(PerlIO_funcs),
"crlf",
sizeof(PerlIOCrlf),
NULL,
PerlIOBase_fileno,
PerlIOBuf_dup,
- PerlIOBuf_read, /* generic read works with ptr/cnt lies
- * ... */
+ PerlIOBuf_read, /* generic read works with ptr/cnt lies */
PerlIOCrlf_unread, /* Put CR,LF in buffer for each '\n' */
PerlIOCrlf_write, /* Put CR,LF in buffer for each '\n' */
PerlIOBuf_seek,
STDCHAR *bbuf; /* malloced buffer if map fails */
} PerlIOMmap;
-static size_t page_size = 0;
-
IV
PerlIOMmap_map(pTHX_ PerlIO *f)
{
- PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
- IV flags = PerlIOBase(f)->flags;
+ dVAR;
+ PerlIOMmap * const m = PerlIOSelf(f, PerlIOMmap);
+ const IV flags = PerlIOBase(f)->flags;
IV code = 0;
if (m->len)
abort();
if (flags & PERLIO_F_CANREAD) {
- PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
- int fd = PerlIO_fileno(f);
+ PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
+ const int fd = PerlIO_fileno(f);
Stat_t st;
code = Fstat(fd, &st);
if (code == 0 && S_ISREG(st.st_mode)) {
SSize_t len = st.st_size - b->posn;
if (len > 0) {
Off_t posn;
- if (!page_size) {
-#if defined(HAS_SYSCONF) && (defined(_SC_PAGESIZE) || defined(_SC_PAGE_SIZE))
- {
- SETERRNO(0, SS_NORMAL);
-# ifdef _SC_PAGESIZE
- page_size = sysconf(_SC_PAGESIZE);
-# else
- page_size = sysconf(_SC_PAGE_SIZE);
-# endif
- if ((long) page_size < 0) {
- if (errno) {
- SV *error = ERRSV;
- char *msg;
- STRLEN n_a;
- (void) SvUPGRADE(error, SVt_PV);
- msg = SvPVx(error, n_a);
- Perl_croak(aTHX_ "panic: sysconf: %s",
- msg);
- }
- else
- Perl_croak(aTHX_
- "panic: sysconf: pagesize unknown");
- }
- }
-#else
-# ifdef HAS_GETPAGESIZE
- page_size = getpagesize();
-# else
-# if defined(I_SYS_PARAM) && defined(PAGESIZE)
- page_size = PAGESIZE; /* compiletime, bad */
-# endif
-# endif
-#endif
- if ((IV) page_size <= 0)
- Perl_croak(aTHX_ "panic: bad pagesize %" IVdf,
- (IV) page_size);
- }
+ if (PL_mmap_page_size <= 0)
+ Perl_croak(aTHX_ "panic: bad pagesize %" IVdf,
+ PL_mmap_page_size);
if (b->posn < 0) {
/*
* This is a hack - should never happen - open should
*/
b->posn = PerlIO_tell(PerlIONext(f));
}
- posn = (b->posn / page_size) * page_size;
+ posn = (b->posn / PL_mmap_page_size) * PL_mmap_page_size;
len = st.st_size - posn;
m->mptr = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, posn);
if (m->mptr && m->mptr != (Mmap_t) - 1) {
IV
PerlIOMmap_unmap(pTHX_ PerlIO *f)
{
- PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
- PerlIOBuf *b = &m->base;
+ PerlIOMmap * const m = PerlIOSelf(f, PerlIOMmap);
IV code = 0;
if (m->len) {
+ PerlIOBuf * const b = &m->base;
if (b->buf) {
code = munmap(m->mptr, m->len);
b->buf = NULL;
STDCHAR *
PerlIOMmap_get_base(pTHX_ PerlIO *f)
{
- PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
- PerlIOBuf *b = &m->base;
+ PerlIOMmap * const m = PerlIOSelf(f, PerlIOMmap);
+ PerlIOBuf * const b = &m->base;
if (b->buf && (PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
/*
* Already have a readbuffer in progress
SSize_t
PerlIOMmap_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
{
- PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
- PerlIOBuf *b = &m->base;
+ PerlIOMmap * const m = PerlIOSelf(f, PerlIOMmap);
+ PerlIOBuf * const b = &m->base;
if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
PerlIO_flush(f);
if (b->ptr && (b->ptr - count) >= b->buf
SSize_t
PerlIOMmap_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
{
- PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
- PerlIOBuf *b = &m->base;
+ PerlIOMmap * const m = PerlIOSelf(f, PerlIOMmap);
+ PerlIOBuf * const b = &m->base;
+
if (!b->buf || !(PerlIOBase(f)->flags & PERLIO_F_WRBUF)) {
/*
* No, or wrong sort of, buffer
IV
PerlIOMmap_flush(pTHX_ PerlIO *f)
{
- PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
- PerlIOBuf *b = &m->base;
+ PerlIOMmap * const m = PerlIOSelf(f, PerlIOMmap);
+ PerlIOBuf * const b = &m->base;
IV code = PerlIOBuf_flush(aTHX_ f);
/*
* Now we are "synced" at PerlIOBuf level
IV
PerlIOMmap_fill(pTHX_ PerlIO *f)
{
- PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
+ PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
IV code = PerlIO_flush(f);
if (code == 0 && !b->buf) {
code = PerlIOMmap_map(aTHX_ f);
IV
PerlIOMmap_close(pTHX_ PerlIO *f)
{
- PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
- PerlIOBuf *b = &m->base;
+ PerlIOMmap * const m = PerlIOSelf(f, PerlIOMmap);
+ PerlIOBuf * const b = &m->base;
IV code = PerlIO_flush(f);
if (m->bbuf) {
b->buf = m->bbuf;
}
-PerlIO_funcs PerlIO_mmap = {
+PERLIO_FUNCS_DECL(PerlIO_mmap) = {
sizeof(PerlIO_funcs),
"mmap",
sizeof(PerlIOMmap),
PerlIO *
Perl_PerlIO_stdin(pTHX)
{
+ dVAR;
if (!PL_perlio) {
PerlIO_stdstreams(aTHX);
}
PerlIO *
Perl_PerlIO_stdout(pTHX)
{
+ dVAR;
if (!PL_perlio) {
PerlIO_stdstreams(aTHX);
}
PerlIO *
Perl_PerlIO_stderr(pTHX)
{
+ dVAR;
if (!PL_perlio) {
PerlIO_stdstreams(aTHX);
}
PerlIO_getname(PerlIO *f, char *buf)
{
dTHX;
- char *name = NULL;
#ifdef VMS
+ char *name = NULL;
+ bool exported = FALSE;
FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
- if (stdio)
+ if (!stdio) {
+ stdio = PerlIO_exportFILE(f,0);
+ exported = TRUE;
+ }
+ if (stdio) {
name = fgetname(stdio, buf);
+ if (exported) PerlIO_releaseFILE(f,stdio);
+ }
+ return name;
#else
+ PERL_UNUSED_ARG(f);
+ PERL_UNUSED_ARG(buf);
Perl_croak(aTHX_ "Don't know how to get file name");
+ return NULL;
#endif
- return name;
}
PerlIO_fdopen(int fd, const char *mode)
{
dTHX;
- return PerlIO_openn(aTHX_ Nullch, mode, fd, 0, 0, NULL, 0, NULL);
+ return PerlIO_openn(aTHX_ NULL, mode, fd, 0, 0, NULL, 0, NULL);
}
#undef PerlIO_open
PerlIO_open(const char *path, const char *mode)
{
dTHX;
- SV *name = sv_2mortal(newSVpvn(path, strlen(path)));
- return PerlIO_openn(aTHX_ Nullch, mode, -1, 0, 0, NULL, 1, &name);
+ SV *name = sv_2mortal(newSVpv(path, 0));
+ return PerlIO_openn(aTHX_ NULL, mode, -1, 0, 0, NULL, 1, &name);
}
#undef Perlio_reopen
PerlIO_reopen(const char *path, const char *mode, PerlIO *f)
{
dTHX;
- SV *name = sv_2mortal(newSVpvn(path, strlen(path)));
- return PerlIO_openn(aTHX_ Nullch, mode, -1, 0, 0, f, 1, &name);
+ SV *name = sv_2mortal(newSVpv(path,0));
+ return PerlIO_openn(aTHX_ NULL, mode, -1, 0, 0, f, 1, &name);
}
#undef PerlIO_getc
{
dTHX;
STDCHAR buf[1];
- SSize_t count = PerlIO_read(f, buf, 1);
- if (count == 1) {
+ if ( 1 == PerlIO_read(f, buf, 1) ) {
return (unsigned char) buf[0];
}
return EOF;
PerlIO_puts(PerlIO *f, const char *s)
{
dTHX;
- STRLEN len = strlen(s);
- return PerlIO_write(f, s, len);
+ return PerlIO_write(f, s, strlen(s));
}
#undef PerlIO_rewind
PerlIO_vprintf(PerlIO *f, const char *fmt, va_list ap)
{
dTHX;
- SV *sv = newSVpvn("", 0);
- char *s;
+ SV * const sv = newSVpvs("");
+ const char *s;
STRLEN len;
SSize_t wrote;
#ifdef NEED_VA_COPY
#else
sv_vcatpvf(sv, fmt, &ap);
#endif
- s = SvPV(sv, len);
+ s = SvPV_const(sv, len);
wrote = PerlIO_write(f, s, len);
SvREFCNT_dec(sv);
return wrote;
PerlIO_tmpfile(void)
{
dTHX;
- PerlIO *f = Perl_my_tmpfp(aTHX);
-
- if (f)
- PerlIOBase(f)->flags |= PERLIO_F_TEMP;
-
+ PerlIO *f = NULL;
+#ifdef WIN32
+ const int fd = win32_tmpfd();
+ if (fd >= 0)
+ f = PerlIO_fdopen(fd, "w+b");
+#else /* WIN32 */
+# if defined(HAS_MKSTEMP) && ! defined(VMS) && ! defined(OS2)
+ SV * const sv = newSVpvs("/tmp/PerlIO_XXXXXX");
+ /*
+ * I have no idea how portable mkstemp() is ... NI-S
+ */
+ const int fd = mkstemp(SvPVX(sv));
+ if (fd >= 0) {
+ f = PerlIO_fdopen(fd, "w+");
+ if (f)
+ PerlIOBase(f)->flags |= PERLIO_F_TEMP;
+ PerlLIO_unlink(SvPVX_const(sv));
+ SvREFCNT_dec(sv);
+ }
+# else /* !HAS_MKSTEMP, fallback to stdio tmpfile(). */
+ FILE * const stdio = PerlSIO_tmpfile();
+
+ if (stdio) {
+ if ((f = PerlIO_push(aTHX_(PerlIO_allocate(aTHX)),
+ PERLIO_FUNCS_CAST(&PerlIO_stdio),
+ "w+", NULL))) {
+ PerlIOStdio * const s = PerlIOSelf(f, PerlIOStdio);
+
+ if (s)
+ s->stdio = stdio;
+ }
+ }
+# endif /* else HAS_MKSTEMP */
+#endif /* else WIN32 */
return f;
}
dTHX;
if (SvOK(pos)) {
STRLEN len;
- Off_t *posn = (Off_t *) SvPV(pos, len);
+ const Off_t * const posn = (Off_t *) SvPV(pos, len);
if (f && len == sizeof(Off_t))
return PerlIO_seek(f, *posn, SEEK_SET);
}
dTHX;
if (SvOK(pos)) {
STRLEN len;
- Fpos_t *fpos = (Fpos_t *) SvPV(pos, len);
+ Fpos_t * const fpos = (Fpos_t *) SvPV(pos, len);
if (f && len == sizeof(Fpos_t)) {
#if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64)
return fsetpos64(f, fpos);
int
PerlIO_vsprintf(char *s, int n, const char *fmt, va_list ap)
{
- int val = vsprintf(s, fmt, ap);
+ dVAR;
+ const int val = vsprintf(s, fmt, ap);
if (n >= 0) {
if (strlen(s) >= (STRLEN) n) {
dTHX;
return result;
}
#endif
+
+/*
+ * Local variables:
+ * c-indentation-style: bsd
+ * c-basic-offset: 4
+ * indent-tabs-mode: t
+ * End:
+ *
+ * ex: set ts=8 sts=4 sw=4 noet:
+ */