#ifdef PERL_MICRO
# include "uconfig.h"
#else
-# include "config.h"
+# ifndef USE_CROSS_COMPILE
+# include "config.h"
+# else
+# include "xconfig.h"
+# endif
#endif
#define PERLIO_NOT_STDIO 0
else \
SETERRNO(EBADF, SS_IVCHAN)
+#if defined(__osf__) && _XOPEN_SOURCE < 500
+extern int fseeko(FILE *, off_t, int);
+extern off_t ftello(FILE *);
+#endif
+
#ifndef USE_SFIO
+
+EXTERN_C int perlsio_binmode(FILE *fp, int iotype, int mode);
+
int
perlsio_binmode(FILE *fp, int iotype, int mode)
{
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
va_list ap;
dSYS;
va_start(ap, fmt);
- 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)
- PL_perlio_debug_fd = PerlLIO_open3(s, O_WRONLY | O_CREAT | O_APPEND, 0666);
- else
+ if (!PL_perlio_debug_fd) {
+ if (!PL_tainting && PL_uid == PL_euid && PL_gid == PL_egid) {
+ const char * const s = PerlEnv_getenv("PERLIO_DEBUG");
+ if (s && *s)
+ PL_perlio_debug_fd
+ = PerlLIO_open3(s, O_WRONLY | O_CREAT | O_APPEND, 0666);
+ else
+ PL_perlio_debug_fd = -1;
+ } else {
+ /* tainting or set*id, so ignore the environment, and ensure we
+ skip these tests next time through. */
PL_perlio_debug_fd = -1;
+ }
}
if (PL_perlio_debug_fd > 0) {
dTHX;
const char * const s = CopFILE(PL_curcop);
/* Use fixed buffer as sv_catpvf etc. needs SVs */
char buffer[1024];
- 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);
+ const STRLEN len1 = my_snprintf(buffer, sizeof(buffer), "%.40s:%" IVdf " ", s ? s : "(none)", (IV) CopLINE(PL_curcop));
+ const STRLEN len2 = my_vsnprintf(buffer + len1, sizeof(buffer) - len1, fmt, ap);
+ PerlLIO_write(PL_perlio_debug_fd, buffer, len1 + len2);
#else
const char *s = CopFILE(PL_curcop);
STRLEN len;
PerlIO_list_alloc(pTHX)
{
PerlIO_list_t *list;
+ PERL_UNUSED_CONTEXT;
Newxz(list, 1, PerlIO_list_t);
list->refcnt = 1;
return list;
{
dVAR;
PerlIO_pair_t *p;
+ PERL_UNUSED_CONTEXT;
+
if (list->cur >= list->len) {
list->len += 8;
if (list->array)
p = &(list->array[list->cur++]);
p->funcs = funcs;
if ((p->arg = arg)) {
- (void)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);
PL_known_layers = PerlIO_clone_list(aTHX_ proto->Iknown_layers, param);
PL_def_layerlist = PerlIO_clone_list(aTHX_ proto->Idef_layerlist, param);
PerlIO_allocate(aTHX); /* root slot is never used */
- PerlIO_debug("Clone %p from %p\n",aTHX,proto);
+ PerlIO_debug("Clone %p from %p\n",(void*)aTHX,(void*)proto);
while ((f = *table)) {
int i;
table = (PerlIO **) (f++);
}
}
#else
+ PERL_UNUSED_CONTEXT;
PERL_UNUSED_ARG(proto);
PERL_UNUSED_ARG(param);
#endif
PerlIO **table = &PL_perlio;
PerlIO *f;
#ifdef USE_ITHREADS
- PerlIO_debug("Destruct %p\n",aTHX);
+ PerlIO_debug("Destruct %p\n",(void*)aTHX);
#endif
while ((f = *table)) {
int i;
SAVEINT(PL_in_load_module);
if (cv) {
SAVEGENERICSV(PL_warnhook);
- (void)SvREFCNT_inc(cv);
- PL_warnhook = (SV *) cv;
+ PL_warnhook = (SV *) (SvREFCNT_inc_simple_NN(cv));
}
PL_in_load_module++;
/*
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);
goto mismatch;
}
/* Real layer with a data area */
- Newxc(l,tab->size,char,PerlIOl);
- if (l && f) {
- Zero(l, tab->size, char);
- l->next = *f;
- 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, (PerlIO_funcs*) tab) != 0) {
- PerlIO_pop(aTHX_ f);
- return NULL;
+ if (f) {
+ char *temp;
+ Newxz(temp, tab->size, char);
+ l = (PerlIOl*)temp;
+ if (l) {
+ l->next = *f;
+ 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, (PerlIO_funcs*) tab) != 0) {
+ PerlIO_pop(aTHX_ f);
+ return NULL;
+ }
}
+ else
+ return NULL;
}
}
else if (f) {
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 * const layers = PL_curcop->cop_io;
- if (layers) {
- STRLEN len;
- type = SvPV_const(layers, len);
- if (type && mode[0] != 'r') {
- /*
- * Skip to write part
- */
- const char * const s = strchr(type, 0);
- if (s && (STRLEN)(s - type) < len) {
- type = s + 1;
- }
- }
- }
- }
- return type;
-}
static PerlIO_funcs *
PerlIO_layer_from_ref(pTHX_ SV *sv)
/*
* For any scalar type load the handler which is bundled with perl
*/
- if (SvTYPE(sv) < SVt_PVAV)
- return PerlIO_find_layer(aTHX_ STR_WITH_LEN("scalar"), 1);
+ if (SvTYPE(sv) < SVt_PVAV) {
+ PerlIO_funcs *f = PerlIO_find_layer(aTHX_ STR_WITH_LEN("scalar"), 1);
+ /* This isn't supposed to happen, since PerlIO::scalar is core,
+ * but could happen anyway in smaller installs or with PAR */
+ if (!f && ckWARN(WARN_LAYER))
+ Perl_warner(aTHX_ packWARN(WARN_LAYER), "Unknown PerlIO layer \"scalar\"");
+ return f;
+ }
/*
* For other types allow if layer is known but don't try and load it
return PerlIO_find_layer(aTHX_ STR_WITH_LEN("Code"), 0);
case SVt_PVGV:
return PerlIO_find_layer(aTHX_ STR_WITH_LEN("Glob"), 0);
+ default:
+ return NULL;
}
- return NULL;
}
PerlIO_list_t *
}
}
if (!layers || !*layers)
- layers = PerlIO_context_layers(aTHX_ mode);
+ layers = Perl_PerlIO_context_layers(aTHX_ mode);
if (layers && *layers) {
PerlIO_list_t *av;
if (incdef) {
}
else {
PerlIO_list_free(aTHX_ av);
- return (PerlIO_list_t *) NULL;
+ return NULL;
}
}
else {
if (!f && narg == 1 && *args == &PL_sv_undef) {
if ((f = PerlIO_tmpfile())) {
if (!layers || !*layers)
- layers = PerlIO_context_layers(aTHX_ mode);
+ layers = Perl_PerlIO_context_layers(aTHX_ mode);
if (layers && *layers)
PerlIO_apply_layers(aTHX_ f, mode, layers);
}
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)) {
PerlIOBase_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
{
PerlIOl * const l = PerlIOBase(f);
+ PERL_UNUSED_CONTEXT;
PERL_UNUSED_ARG(arg);
l->flags &= ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE |
IV
PerlIOBase_popped(pTHX_ PerlIO *f)
{
+ PERL_UNUSED_CONTEXT;
PERL_UNUSED_ARG(f);
return 0;
}
SSize_t avail = PerlIO_get_cnt(f);
SSize_t take = 0;
if (avail > 0)
- take = ((SSize_t)count < avail) ? count : avail;
+ take = ((SSize_t)count < avail) ? (SSize_t)count : avail;
if (take > 0) {
STDCHAR *ptr = PerlIO_get_ptr(f);
Copy(ptr, buf, take, STDCHAR);
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;
}
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;
}
void
PerlIOBase_setlinebuf(pTHX_ PerlIO *f)
{
+ PERL_UNUSED_CONTEXT;
if (PerlIOValid(f)) {
PerlIOBase(f)->flags |= PERLIO_F_LINEBUF;
}
return f;
}
-#ifdef USE_THREADS
-perl_mutex PerlIO_mutex;
-#endif
-
/* PL_perlio_fd_refcnt[] is in intrpvar.h */
-/* Must be called with PerlIO_mutex locked. */
+/* Must be called with PL_perlio_mutex locked. */
static void
S_more_refcounted_fds(pTHX_ const int new_fd) {
+ dVAR;
const int old_max = PL_perlio_fd_refcnt_size;
const int new_max = 16 + (new_fd & ~15);
int *new_array;
assert (new_max > new_fd);
- new_array
- = PerlMemShared_realloc(PL_perlio_fd_refcnt, new_max * sizeof(int));
+ /* Use plain realloc() since we need this memory to be really
+ * global and visible to all the interpreters and/or threads. */
+ new_array = (int*) realloc(PL_perlio_fd_refcnt, new_max * sizeof(int));
if (!new_array) {
-#ifdef USE_THREADS
- MUTEX_UNLOCK(&PerlIO_mutex);
+#ifdef USE_ITHREADS
+ MUTEX_UNLOCK(&PL_perlio_mutex);
#endif
/* Can't use PerlIO to write as it allocates memory */
PerlLIO_write(PerlIO_fileno(Perl_error_log),
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);
+ PerlIO_debug("Zeroing %p, %d\n",
+ (void*)(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);
-#endif
+ /* MUTEX_INIT(&PL_perlio_mutex) is done in PERL_SYS_INIT3(). */
+ PERL_UNUSED_CONTEXT;
}
void
if (fd >= 0) {
dVAR;
-#ifdef USE_THREADS
- MUTEX_LOCK(&PerlIO_mutex);
+#ifdef USE_ITHREADS
+ MUTEX_LOCK(&PL_perlio_mutex);
#endif
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]);
+ if (PL_perlio_fd_refcnt[fd] <= 0) {
+ Perl_croak(aTHX_ "refcnt_inc: fd %d: %d <= 0\n",
+ fd, PL_perlio_fd_refcnt[fd]);
+ }
+ PerlIO_debug("refcnt_inc: fd %d refcnt=%d\n",
+ fd, PL_perlio_fd_refcnt[fd]);
-#ifdef USE_THREADS
- MUTEX_UNLOCK(&PerlIO_mutex);
+#ifdef USE_ITHREADS
+ MUTEX_UNLOCK(&PL_perlio_mutex);
#endif
+ } else {
+ Perl_croak(aTHX_ "refcnt_inc: fd %d < 0\n", fd);
}
}
int cnt = 0;
if (fd >= 0) {
dVAR;
-#ifdef USE_THREADS
- MUTEX_LOCK(&PerlIO_mutex);
+#ifdef USE_ITHREADS
+ MUTEX_LOCK(&PL_perlio_mutex);
#endif
- /* 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? */
+ if (fd >= PL_perlio_fd_refcnt_size) {
+ Perl_croak(aTHX_ "refcnt_dec: fd %d >= refcnt_size %d\n",
+ fd, PL_perlio_fd_refcnt_size);
+ }
+ if (PL_perlio_fd_refcnt[fd] <= 0) {
+ Perl_croak(aTHX_ "refcnt_dec: fd %d: %d <= 0\n",
+ fd, PL_perlio_fd_refcnt[fd]);
+ }
cnt = --PL_perlio_fd_refcnt[fd];
- PerlIO_debug("fd %d refcnt=%d\n",fd,cnt);
-#ifdef USE_THREADS
- MUTEX_UNLOCK(&PerlIO_mutex);
+ PerlIO_debug("refcnt_dec: fd %d refcnt=%d\n", fd, cnt);
+#ifdef USE_ITHREADS
+ MUTEX_UNLOCK(&PL_perlio_mutex);
#endif
+ } else {
+ Perl_croak(aTHX_ "refcnt_dec: fd %d < 0\n", fd);
}
return cnt;
}
dVAR;
int i;
#ifdef USE_ITHREADS
- PerlIO_debug("Cleanup layers for %p\n",aTHX);
+ PerlIO_debug("Cleanup layers for %p\n",(void*)aTHX);
#else
PerlIO_debug("Cleanup layers\n");
#endif
+
/* Raise STDIN..STDERR refcount so we don't close them */
for (i=0; i < 3; i++)
PerlIOUnix_refcnt_inc(i);
}
}
+void PerlIO_teardown(pTHX) /* Call only from PERL_SYS_TERM(). */
+{
+
+#ifdef DEBUGGING
+ {
+ /* By now all filehandles should have been closed, so any
+ * stray (non-STD-)filehandles indicate *possible* (PerlIO)
+ * errors. */
+ int i;
+ for (i = 3; i < PL_perlio_fd_refcnt_size; i++) {
+ if (PL_perlio_fd_refcnt[i])
+ PerlIO_debug("PerlIO_cleanup: fd %d refcnt=%d\n",
+ i, PL_perlio_fd_refcnt[i]);
+ }
+ }
+#endif
+ /* Not bothering with PL_perlio_mutex since by now
+ * all the interpreters are gone. */
+ if (PL_perlio_fd_refcnt_size /* Assuming initial size of zero. */
+ && PL_perlio_fd_refcnt) {
+ free(PL_perlio_fd_refcnt); /* To match realloc() in S_more_refcounted_fds(). */
+ PL_perlio_fd_refcnt = NULL;
+ PL_perlio_fd_refcnt_size = 0;
+ }
+}
+
/*--------------------------------------------------------------------------------------*/
IV
PerlIOUnix_fileno(pTHX_ PerlIO *f)
{
+ PERL_UNUSED_CONTEXT;
return PerlIOSelf(f, PerlIOUnix)->fd;
}
s->fd = fd;
s->oflags = imode;
PerlIOUnix_refcnt_inc(fd);
+ PERL_UNUSED_CONTEXT;
}
IV
{
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 {
if (f) {
- /*EMPTY*/;
+ NOOP;
/*
* FIXME: pop layers ???
*/
Off_t
PerlIOUnix_tell(pTHX_ PerlIO *f)
{
+ PERL_UNUSED_CONTEXT;
+
return PerlLIO_lseek(PerlIOSelf(f, PerlIOUnix)->fd, 0, SEEK_CUR);
}
IV
PerlIOStdio_fileno(pTHX_ PerlIO *f)
{
+ PERL_UNUSED_CONTEXT;
+
if (PerlIOValid(f)) {
FILE * const s = PerlIOSelf(f, PerlIOStdio)->stdio;
if (s)
#endif
stdio = PerlSIO_fopen(path, mode);
if (stdio) {
- PerlIOStdio *s;
if (!f) {
f = PerlIO_allocate(aTHX);
}
mode = PerlIOStdio_mode(mode, tmode);
f = PerlIO_push(aTHX_ f, self, mode, PerlIOArg);
if (f) {
- s = PerlIOSelf(f, PerlIOStdio);
- s->stdio = stdio;
- PerlIOUnix_refcnt_inc(fileno(s->stdio));
+ PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
+ PerlIOUnix_refcnt_inc(fileno(stdio));
+ } else {
+ PerlSIO_fclose(stdio);
}
return f;
}
f = PerlIO_allocate(aTHX);
}
if ((f = PerlIO_push(aTHX_ f, self, mode, PerlIOArg))) {
- PerlIOStdio * const s = PerlIOSelf(f, PerlIOStdio);
- s->stdio = stdio;
- PerlIOUnix_refcnt_inc(fileno(s->stdio));
+ PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
+ PerlIOUnix_refcnt_inc(fileno(stdio));
}
return f;
}
goto set_this;
}
else {
- /*EMPTY*/;
+ NOOP;
/* 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
*/
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;
+ PERL_UNUSED_ARG(f);
+ return 0;
# elif defined(__hpux)
f->__fileH = 0xff;
f->__fileL = 0xff;
}
else {
const int fd = fileno(stdio);
- int socksfd = 0;
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);
PerlIOStdio_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
{
FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
+ PERL_UNUSED_CONTEXT;
+
return PerlSIO_fseek(stdio, offset, whence);
}
PerlIOStdio_tell(pTHX_ PerlIO *f)
{
FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
+ PERL_UNUSED_CONTEXT;
+
return PerlSIO_ftell(stdio);
}
PerlIOStdio_flush(pTHX_ PerlIO *f)
{
FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
+ PERL_UNUSED_CONTEXT;
+
if (PerlIOBase(f)->flags & PERLIO_F_CANWRITE) {
return PerlSIO_fflush(stdio);
}
else {
- /*EMPTY*/;
+ NOOP;
#if 0
/*
* FIXME: This discards ungetc() and pre-read stuff which is not
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
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 */
+ PerlSIO_set_ptr(stdio, ptr); /* LHS STDCHAR* cast non-portable */
#ifdef STDIO_PTR_LVAL_SETS_CNT
if (PerlSIO_get_cnt(stdio) != (cnt)) {
assert(PerlSIO_get_cnt(stdio) == (cnt));
{
FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
int c;
+ PERL_UNUSED_CONTEXT;
+
/*
* fflush()ing read-only streams can cause trouble on some stdio-s
*/
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)) {
PerlIOBuf_get_base(pTHX_ PerlIO *f)
{
PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
+ PERL_UNUSED_CONTEXT;
+
if (!b->buf) {
if (!b->bufsiz)
b->bufsiz = 4096;
* buffer */
} PerlIOCrlf;
+/* Inherit the PERLIO_F_UTF8 flag from previous layer.
+ * Otherwise the :crlf layer would always revert back to
+ * raw mode.
+ */
+static void
+S_inherit_utf8_flag(PerlIO *f)
+{
+ PerlIO *g = PerlIONext(f);
+ if (PerlIOValid(g)) {
+ if (PerlIOBase(g)->flags & PERLIO_F_UTF8) {
+ PerlIOBase(f)->flags |= PERLIO_F_UTF8;
+ }
+ }
+}
+
IV
PerlIOCrlf_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
{
* any given moment at most one CRLF-capable layer being enabled
* in the whole layer stack. */
PerlIO *g = PerlIONext(f);
- while (g && *g) {
+ while (PerlIOValid(g)) {
PerlIOl *b = PerlIOBase(g);
if (b && b->tab == &PerlIO_crlf) {
if (!(b->flags & PERLIO_F_CRLF))
b->flags |= PERLIO_F_CRLF;
+ S_inherit_utf8_flag(g);
PerlIO_pop(aTHX_ f);
return code;
}
g = PerlIONext(g);
}
}
+ S_inherit_utf8_flag(f);
return code;
}
ptr -= cnt;
}
else {
- /*EMPTY*/;
+ NOOP;
#if 0
/*
* Test code - delete when it works ...
}
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);
+ m->mptr = (Mmap_t)mmap(NULL, len, PROT_READ, MAP_SHARED, fd, posn);
if (m->mptr && m->mptr != (Mmap_t) - 1) {
#if 0 && defined(HAS_MADVISE) && defined(MADV_SEQUENTIAL)
madvise(m->mptr, len, MADV_SEQUENTIAL);
if (m->len) {
PerlIOBuf * const b = &m->base;
if (b->buf) {
- code = munmap(m->mptr, m->len);
+ /* The munmap address argument is tricky: depending on the
+ * standard it is either "void *" or "caddr_t" (which is
+ * usually "char *" (signed or unsigned). If we cast it
+ * to "void *", those that have it caddr_t and an uptight
+ * C++ compiler, will freak out. But casting it as char*
+ * should work. Maybe. (Using Mmap_t figured out by
+ * Configure doesn't always work, apparently.) */
+ code = munmap((char*)m->mptr, m->len);
b->buf = NULL;
m->len = 0;
m->mptr = NULL;
# 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 (stdio)
+ f = PerlIO_fdopen(fileno(stdio), "w+");
- if (s)
- s->stdio = stdio;
- }
- }
# endif /* else HAS_MKSTEMP */
#endif /* else WIN32 */
return f;
* Now some functions in terms of above which may be needed even if we are
* not in true PerlIO mode
*/
+const char *
+Perl_PerlIO_context_layers(pTHX_ const char *mode)
+{
+ dVAR;
+ const char *type = NULL;
+ /*
+ * Need to supply default layer info from open.pm
+ */
+ if (PL_curcop && PL_curcop->cop_hints & HINT_LEXICAL_IO) {
+ SV * const layers
+ = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash, 0,
+ "open", 4, 0, 0);
+ assert(layers);
+ if (SvOK(layers)) {
+ STRLEN len;
+ type = SvPV_const(layers, len);
+ if (type && mode && mode[0] != 'r') {
+ /*
+ * Skip to write part, which is separated by a '\0'
+ */
+ STRLEN read_len = strlen(type);
+ if (read_len < len) {
+ type += read_len + 1;
+ }
+ }
+ }
+ }
+ return type;
+}
+
#ifndef HAS_FSETPOS
#undef PerlIO_setpos
int
PerlIO_vsprintf(char *s, int n, const char *fmt, va_list ap)
{
- dVAR;
- const int val = vsprintf(s, fmt, ap);
- if (n >= 0) {
- if (strlen(s) >= (STRLEN) n) {
- dTHX;
- (void) PerlIO_puts(Perl_error_log,
- "panic: sprintf overflow - memory corrupted!\n");
- my_exit(1);
- }
+ dTHX;
+ const int val = my_vsnprintf(s, n > 0 ? n : 0, fmt, ap);
+ PERL_UNUSED_CONTEXT;
+
+#ifndef PERL_MY_VSNPRINTF_GUARDED
+ if (val < 0 || (n > 0 ? val >= n : 0)) {
+ Perl_croak(aTHX_ "panic: my_vsnprintf overflow in PerlIO_vsprintf\n");
}
+#endif
return val;
}
#endif