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)
{
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;
/*
* 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 *
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;
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),
void
PerlIO_init(pTHX)
{
- /* Place holder for stdstreams call ??? */
-#ifdef USE_THREADS
- MUTEX_INIT(&PerlIO_mutex);
-#else
+ /* MUTEX_INIT(&PL_perlio_mutex) is done in PERL_SYS_INIT3(). */
PERL_UNUSED_CONTEXT;
-#endif
}
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;
}
#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;
+ }
+}
+
/*--------------------------------------------------------------------------------------*/
f->_fileno = -1;
return 1;
# elif defined(__sun__)
+ PERL_UNUSED_ARG(f);
return 0;
# elif defined(__hpux)
f->__fileH = 0xff;
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));
* 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;
}
}
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;