return NULL;
}
else {
- SETERRNO(EBADF, SS$_IVCHAN);
+ SETERRNO(EBADF, SS_IVCHAN);
}
#endif
return NULL;
sfset(sfstdout, SF_SHARE, 0);
}
+/* This is not the reverse of PerlIO_exportFILE(), PerlIO_releaseFILE() is. */
PerlIO *
PerlIO_importFILE(FILE *stdio, const char *mode)
{
int fd = fileno(stdio);
if (!mode || !*mode) {
- mmode = "r+";
+ mode = "r+";
}
return PerlIO_fdopen(fd, mode);
}
return new;
}
else {
- SETERRNO(EBADF, SS$_IVCHAN);
+ SETERRNO(EBADF, SS_IVCHAN);
return NULL;
}
}
}
}
+/* Return as an array the stack of layers on a filehandle. Note that
+ * the stack is returned top-first in the array, and there are three
+ * times as many array elements as there are layers in the stack: the
+ * first element of a layer triplet is the name, the second one is the
+ * arguments, and the third one is the flags. */
+
+AV *
+PerlIO_get_layers(pTHX_ PerlIO *f)
+{
+ AV *av = newAV();
+
+ if (PerlIOValid(f)) {
+ dSP;
+ 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;
+ }
+ }
+
+ return av;
+}
+
/*--------------------------------------------------------------------------------------*/
/*
* XS Interface for perl code
}
if (load && PL_subname && PL_def_layerlist
&& PL_def_layerlist->cur >= 2) {
- SV *pkgsv = newSVpvn("PerlIO", 6);
- SV *layer = newSVpvn(name, len);
- ENTER;
- /*
- * The two SVs are magically freed by load_module
- */
- Perl_load_module(aTHX_ 0, pkgsv, Nullsv, layer, Nullsv);
- LEAVE;
- return PerlIO_find_layer(aTHX_ name, len, 0);
+ if (PL_in_load_module) {
+ 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;
+ SAVEINT(PL_in_load_module);
+ if (cv) {
+ SAVESPTR(PL_warnhook);
+ PL_warnhook = (SV *) cv;
+ }
+ PL_in_load_module++;
+ /*
+ * The two SVs are magically freed by load_module
+ */
+ Perl_load_module(aTHX_ 0, pkgsv, Nullsv, layer, Nullsv);
+ PL_in_load_module--;
+ LEAVE;
+ return PerlIO_find_layer(aTHX_ name, len, 0);
+ }
}
PerlIO_debug("Cannot find %.*s\n", (int) len, name);
return NULL;
return sv;
}
+XS(XS_PerlIO__Layer__NoWarnings)
+{
+ /* This is used as a %SIG{__WARN__} handler to supress warnings
+ during loading of layers.
+ */
+ dXSARGS;
+ if (items)
+ PerlIO_debug("warning:%s\n",SvPV_nolen(ST(0)));
+ XSRETURN(0);
+}
+
XS(XS_PerlIO__Layer__find)
{
dXSARGS;
Perl_warner(aTHX_ packWARN(WARN_LAYER),
"perlio: invalid separator character %c%c%c in layer specification list %s",
q, *s, q, s);
- SETERRNO(EINVAL, LIB$_INVARG);
+ SETERRNO(EINVAL, LIB_INVARG);
return -1;
}
do {
return def;
}
+IV
+PerlIOPop_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
+{
+ if (PerlIOValid(f)) {
+ PerlIO_flush(f);
+ PerlIO_pop(aTHX_ f);
+ return 0;
+ }
+ return -1;
+}
+
+PerlIO_funcs PerlIO_remove = {
+ sizeof(PerlIO_funcs),
+ "pop",
+ 0,
+ PERLIO_K_DUMMY | PERLIO_K_UTF8,
+ PerlIOPop_pushed,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ NULL, /* flush */
+ NULL, /* fill */
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ NULL, /* get_base */
+ NULL, /* get_bufsiz */
+ NULL, /* get_ptr */
+ NULL, /* get_cnt */
+ NULL, /* set_ptrcnt */
+};
+
PerlIO_list_t *
PerlIO_default_layers(pTHX)
{
PerlIO_define_layer(aTHX_ & PerlIO_mmap);
#endif
PerlIO_define_layer(aTHX_ & PerlIO_utf8);
+ PerlIO_define_layer(aTHX_ & PerlIO_remove);
PerlIO_define_layer(aTHX_ & PerlIO_byte);
PerlIO_list_push(aTHX_ PL_def_layerlist,
PerlIO_find_layer(aTHX_ osLayer->name, 0, 0),
__FILE__);
#endif
newXS("PerlIO::Layer::find", XS_PerlIO__Layer__find, __FILE__);
+ newXS("PerlIO::Layer::NoWarnings", XS_PerlIO__Layer__NoWarnings, __FILE__);
}
PerlIO_funcs *
PerlIO *
PerlIO_push(pTHX_ PerlIO *f, PerlIO_funcs *tab, const char *mode, SV *arg)
{
- PerlIOl *l = NULL;
- Newc('L',l,tab->size,char,PerlIOl);
- if (l && f) {
- Zero(l, tab->size, char);
- l->next = *f;
- l->tab = tab;
- *f = l;
+ if (tab->fsize != sizeof(PerlIO_funcs)) {
+ mismatch:
+ Perl_croak(aTHX_ "Layer does not match this perl");
+ }
+ if (tab->size) {
+ PerlIOl *l = NULL;
+ if (tab->size < sizeof(PerlIOl)) {
+ goto mismatch;
+ }
+ /* Real layer with a data area */
+ Newc('L',l,tab->size,char,PerlIOl);
+ if (l && f) {
+ Zero(l, tab->size, char);
+ l->next = *f;
+ l->tab = 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) (aTHX_ f, mode, arg, tab) != 0) {
+ PerlIO_pop(aTHX_ f);
+ return NULL;
+ }
+ }
+ }
+ else if (f) {
+ /* Pseudo-layer where push does its own stack adjust */
PerlIO_debug("PerlIO_push f=%p %s %s %p\n", (void*)f, tab->name,
(mode) ? mode : "(Null)", (void*)arg);
- if ((*l->tab->Pushed) (aTHX_ f, mode, arg) != 0) {
- PerlIO_pop(aTHX_ f);
+ if ((*tab->Pushed) (aTHX_ f, mode, arg, tab) != 0) {
return NULL;
}
}
}
IV
-PerlIOPop_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
-{
- PerlIO_pop(aTHX_ f);
- if (*f) {
- PerlIO_flush(f);
- PerlIO_pop(aTHX_ f);
- return 0;
- }
- return -1;
-}
-
-IV
PerlIOBase_binmode(pTHX_ PerlIO *f)
{
if (PerlIOValid(f)) {
}
IV
-PerlIORaw_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
+PerlIORaw_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
{
if (PerlIOValid(f)) {
PerlIO *t;
PerlIOl *l;
- PerlIO_pop(aTHX_ f); /* Remove the dummy layer */
PerlIO_flush(f);
/*
* Strip all layers that are not suitable for a raw stream
if (PerlIOValid(f))
return (*PerlIOBase(f)->tab->Close) (aTHX_ f);
else {
- SETERRNO(EBADF, SS$_IVCHAN);
+ SETERRNO(EBADF, SS_IVCHAN);
return -1;
}
}
if (PerlIOValid(f))
return (*PerlIOBase(f)->tab->Fileno) (aTHX_ f);
else {
- SETERRNO(EBADF, SS$_IVCHAN);
+ SETERRNO(EBADF, SS_IVCHAN);
return -1;
}
}
* 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_ "scalar", 6, 1);
/*
* For other types allow if layer is known but don't try and load it
incdef = 0;
}
/*
- * Don't fail if handler cannot be found :Via(...) etc. may do
+ * Don't fail if handler cannot be found :via(...) etc. may do
* something sensible else we will just stringfy and open
* resulting string.
*/
if (PerlIOValid(f))
return (*PerlIOBase(f)->tab->Read) (aTHX_ f, vbuf, count);
else {
- SETERRNO(EBADF, SS$_IVCHAN);
+ SETERRNO(EBADF, SS_IVCHAN);
return -1;
}
}
if (PerlIOValid(f))
return (*PerlIOBase(f)->tab->Unread) (aTHX_ f, vbuf, count);
else {
- SETERRNO(EBADF, SS$_IVCHAN);
+ SETERRNO(EBADF, SS_IVCHAN);
return -1;
}
}
if (PerlIOValid(f))
return (*PerlIOBase(f)->tab->Write) (aTHX_ f, vbuf, count);
else {
- SETERRNO(EBADF, SS$_IVCHAN);
+ SETERRNO(EBADF, SS_IVCHAN);
return -1;
}
}
if (PerlIOValid(f))
return (*PerlIOBase(f)->tab->Seek) (aTHX_ f, offset, whence);
else {
- SETERRNO(EBADF, SS$_IVCHAN);
+ SETERRNO(EBADF, SS_IVCHAN);
return -1;
}
}
if (PerlIOValid(f))
return (*PerlIOBase(f)->tab->Tell) (aTHX_ f);
else {
- SETERRNO(EBADF, SS$_IVCHAN);
+ SETERRNO(EBADF, SS_IVCHAN);
return -1;
}
}
}
else {
PerlIO_debug("Cannot flush f=%p :%s\n", (void*)f, tab->name);
- SETERRNO(EBADF, SS$_IVCHAN);
+ SETERRNO(EBADF, SS_IVCHAN);
return -1;
}
}
else {
PerlIO_debug("Cannot flush f=%p\n", (void*)f);
- SETERRNO(EBADF, SS$_IVCHAN);
+ SETERRNO(EBADF, SS_IVCHAN);
return -1;
}
}
if (PerlIOValid(f))
return (*PerlIOBase(f)->tab->Fill) (aTHX_ f);
else {
- SETERRNO(EBADF, SS$_IVCHAN);
+ SETERRNO(EBADF, SS_IVCHAN);
return -1;
}
}
if (PerlIOValid(f))
return (PerlIOBase(f)->flags & PERLIO_F_UTF8) != 0;
else {
- SETERRNO(EBADF, SS$_IVCHAN);
+ SETERRNO(EBADF, SS_IVCHAN);
return -1;
}
}
if (PerlIOValid(f))
return (*PerlIOBase(f)->tab->Eof) (aTHX_ f);
else {
- SETERRNO(EBADF, SS$_IVCHAN);
+ SETERRNO(EBADF, SS_IVCHAN);
return -1;
}
}
if (PerlIOValid(f))
return (*PerlIOBase(f)->tab->Error) (aTHX_ f);
else {
- SETERRNO(EBADF, SS$_IVCHAN);
+ SETERRNO(EBADF, SS_IVCHAN);
return -1;
}
}
if (PerlIOValid(f))
(*PerlIOBase(f)->tab->Clearerr) (aTHX_ f);
else
- SETERRNO(EBADF, SS$_IVCHAN);
+ SETERRNO(EBADF, SS_IVCHAN);
}
void
if (PerlIOValid(f))
(*PerlIOBase(f)->tab->Setlinebuf) (aTHX_ f);
else
- SETERRNO(EBADF, SS$_IVCHAN);
+ SETERRNO(EBADF, SS_IVCHAN);
}
int
}
}
+
/*--------------------------------------------------------------------------------------*/
/*
* utf8 and raw dummy layers
*/
IV
-PerlIOUtf8_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
+PerlIOUtf8_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
{
- if (*PerlIONext(f)) {
- PerlIO_funcs *tab = PerlIOBase(f)->tab;
- PerlIO_pop(aTHX_ f);
+ if (PerlIOValid(f)) {
if (tab->kind & PERLIO_K_UTF8)
PerlIOBase(f)->flags |= PERLIO_F_UTF8;
else
}
PerlIO_funcs PerlIO_utf8 = {
+ sizeof(PerlIO_funcs),
"utf8",
- sizeof(PerlIOl),
+ 0,
PERLIO_K_DUMMY | PERLIO_K_UTF8,
PerlIOUtf8_pushed,
NULL,
};
PerlIO_funcs PerlIO_byte = {
+ sizeof(PerlIO_funcs),
"bytes",
- sizeof(PerlIOl),
+ 0,
PERLIO_K_DUMMY,
PerlIOUtf8_pushed,
NULL,
}
PerlIO_funcs PerlIO_raw = {
+ sizeof(PerlIO_funcs),
"raw",
- sizeof(PerlIOl),
+ 0,
PERLIO_K_DUMMY,
PerlIORaw_pushed,
PerlIOBase_popped,
}
char *
-PerlIO_modestr(PerlIO *f, char *buf)
+PerlIO_modestr(PerlIO * f, char *buf)
{
char *s = buf;
- IV flags = PerlIOBase(f)->flags;
- if (flags & PERLIO_F_APPEND) {
- *s++ = 'a';
- if (flags & PERLIO_F_CANREAD) {
- *s++ = '+';
+ if (PerlIOValid(f)) {
+ IV flags = PerlIOBase(f)->flags;
+ if (flags & PERLIO_F_APPEND) {
+ *s++ = 'a';
+ if (flags & PERLIO_F_CANREAD) {
+ *s++ = '+';
+ }
}
- }
- else if (flags & PERLIO_F_CANREAD) {
- *s++ = 'r';
- if (flags & PERLIO_F_CANWRITE)
- *s++ = '+';
- }
- else if (flags & PERLIO_F_CANWRITE) {
- *s++ = 'w';
- if (flags & PERLIO_F_CANREAD) {
- *s++ = '+';
+ else if (flags & PERLIO_F_CANREAD) {
+ *s++ = 'r';
+ if (flags & PERLIO_F_CANWRITE)
+ *s++ = '+';
+ }
+ else if (flags & PERLIO_F_CANWRITE) {
+ *s++ = 'w';
+ if (flags & PERLIO_F_CANREAD) {
+ *s++ = '+';
+ }
}
- }
#ifdef PERLIO_USING_CRLF
- if (!(flags & PERLIO_F_CRLF))
- *s++ = 'b';
+ if (!(flags & PERLIO_F_CRLF))
+ *s++ = 'b';
#endif
+ }
*s = '\0';
return buf;
}
+
IV
-PerlIOBase_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
+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
- PerlIO_funcs *tab = PerlIOBase(f)->tab;
l->flags &= ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE |
PERLIO_F_TRUNCATE | PERLIO_F_APPEND);
if (tab->Set_ptrcnt != NULL)
l->flags |= PERLIO_F_TRUNCATE | PERLIO_F_CANWRITE;
break;
default:
- SETERRNO(EINVAL, LIB$_INVARG);
+ SETERRNO(EINVAL, LIB_INVARG);
return -1;
}
while (*mode) {
l->flags |= PERLIO_F_CRLF;
break;
default:
- SETERRNO(EINVAL, LIB$_INVARG);
+ SETERRNO(EINVAL, LIB_INVARG);
return -1;
}
}
*/
oflags |= O_BINARY;
if (*mode || oflags == -1) {
- SETERRNO(EINVAL, LIB$_INVARG);
+ SETERRNO(EINVAL, LIB_INVARG);
oflags = -1;
}
return oflags;
}
IV
-PerlIOUnix_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
+PerlIOUnix_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
{
- IV code = PerlIOBase_pushed(aTHX_ f, mode, arg);
+ IV code = PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
PerlIOUnix *s = PerlIOSelf(f, PerlIOUnix);
if (*PerlIONext(f)) {
/* We never call down so do any pending stuff now */
PerlIOUnix_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
{
int fd = PerlIOSelf(f, PerlIOUnix)->fd;
- if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD))
+ 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);
if (len >= 0 || errno != EINTR) {
}
}
else {
- SETERRNO(EBADF,SS$_IVCHAN);
+ SETERRNO(EBADF,SS_IVCHAN);
return -1;
}
while (PerlLIO_close(fd) != 0) {
}
PerlIO_funcs PerlIO_unix = {
+ sizeof(PerlIO_funcs),
"unix",
sizeof(PerlIOUnix),
PERLIO_K_RAW,
IV
PerlIOStdio_fileno(pTHX_ PerlIO *f)
{
- return PerlSIO_fileno(PerlIOSelf(f, PerlIOStdio)->stdio);
+ FILE *s;
+ if (PerlIOValid(f) && (s = PerlIOSelf(f, PerlIOStdio)->stdio)) {
+ return PerlSIO_fileno(s);
+ }
+ errno = EBADF;
+ return -1;
}
char *
return ret;
}
-/*
- * This isn't used yet ...
- */
IV
-PerlIOStdio_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
+PerlIOStdio_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
{
- if (*PerlIONext(f)) {
- PerlIOStdio *s = PerlIOSelf(f, PerlIOStdio);
- char tmode[8];
- FILE *stdio =
- PerlSIO_fdopen(PerlIO_fileno(PerlIONext(f)), mode =
- PerlIOStdio_mode(mode, tmode));
- if (stdio) {
- s->stdio = stdio;
- /* We never call down so do any pending stuff now */
- PerlIO_flush(PerlIONext(f));
- }
- else
- return -1;
+ PerlIO *n;
+ if (PerlIOValid(f) && PerlIOValid(n = PerlIONext(f))) {
+ PerlIO_funcs *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);
+ char tmode[8];
+ FILE *stdio;
+ if (fd >= 0 && (stdio = PerlSIO_fdopen(fd,
+ mode = PerlIOStdio_mode(mode, tmode)))) {
+ PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
+ /* We never call down so do any pending stuff now */
+ PerlIO_flush(PerlIONext(f));
+ }
+ else {
+ return -1;
+ }
+ }
}
- return PerlIOBase_pushed(aTHX_ f, mode, arg);
+ return PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
}
s->stdio = stdio;
PerlIOUnix_refcnt_inc(fileno(s->stdio));
}
+ return f;
+ }
+ else {
+ return NULL;
}
- return f;
}
}
if (fd >= 0) {
*/
if ((f = PerlIOBase_dup(aTHX_ f, o, param, flags))) {
FILE *stdio = PerlIOSelf(o, PerlIOStdio)->stdio;
+ int fd = fileno(stdio);
+ char mode[8];
if (flags & PERLIO_DUP_FD) {
- int fd = PerlLIO_dup(fileno(stdio));
- if (fd >= 0) {
- char mode[8];
- stdio = PerlSIO_fdopen(fd, PerlIO_modestr(o,mode));
+ int dfd = PerlLIO_dup(fileno(stdio));
+ if (dfd >= 0) {
+ stdio = PerlSIO_fdopen(dfd, PerlIO_modestr(o,mode));
+ goto set_this;
}
else {
/* FIXME: To avoid messy error recovery if dup fails
*/
}
}
+ stdio = PerlSIO_fdopen(fd, PerlIO_modestr(o,mode));
+ set_this:
PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
PerlIOUnix_refcnt_inc(fileno(stdio));
}
return f;
}
+static int
+PerlIOStdio_invalidate_fileno(pTHX_ FILE *f)
+{
+ /* XXX this could use PerlIO_canset_fileno() and
+ * PerlIO_set_fileno() support from Configure
+ */
+# if 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;
+# elif defined(__hpux)
+ f->__fileH = 0xff;
+ f->__fileL = 0xff;
+ return 1;
+ /* Next one ->_file seems to be a reasonable fallback, i.e. if
+ your platform does not have special entry try this one.
+ [For OSF only have confirmation for Tru64 (alpha)
+ but assume other OSFs will be similar.]
+ */
+# elif defined(_AIX) || defined(__osf__) || defined(__irix__)
+ f->_file = -1;
+ return 1;
+# elif defined(__FreeBSD__)
+ /* There may be a better way on FreeBSD:
+ - we could insert a dummy func in the _close function entry
+ f->_close = (int (*)(void *)) dummy_close;
+ */
+ f->_file = -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
+ f->_close = (int (*)(void *)) dummy_close;
+ */
+ f->_file = -1;
+ return 1;
+# elif defined(WIN32)
+# if defined(__BORLANDC__)
+ f->fd = PerlLIO_dup(fileno(f));
+# elif defined(UNDER_CE)
+ /* WIN_CE does not have access to FILE internals, it hardly has FILE
+ structure at all
+ */
+# else
+ f->_file = -1;
+# endif
+ return 1;
+# else
+#if 0
+ /* Sarathy's code did this - we fall back to a dup/dup2 hack
+ (which isn't thread safe) instead
+ */
+# error "Don't know how to set FILE.fileno on your platform"
+#endif
+ return 0;
+# endif
+}
+
IV
PerlIOStdio_close(pTHX_ PerlIO *f)
{
-#ifdef SOCKS5_VERSION_NAME
- int optval;
- Sock_size_t optlen = sizeof(int);
-#endif
FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
- if (PerlIOUnix_refcnt_dec(fileno(stdio)) > 0) {
- /* Do not close it but do flush any buffers */
- return PerlIO_flush(f);
+ if (!stdio) {
+ errno = EBADF;
+ return -1;
}
- return (
+ else {
+ int fd = fileno(stdio);
+ int socksfd = 0;
+ int invalidate = 0;
+ IV result = 0;
+ int saveerr = 0;
+ int dupfd = 0;
#ifdef SOCKS5_VERSION_NAME
- (getsockopt
- (PerlIO_fileno(f), SOL_SOCKET, SO_TYPE, (void *) &optval,
- &optlen) <
- 0) ? PerlSIO_fclose(stdio) : close(PerlIO_fileno(f))
-#else
- PerlSIO_fclose(stdio)
+ /* Socks lib overrides close() but stdio isn't linked to
+ that library (though we are) - so we must call close()
+ on sockets on stdio's behalf.
+ */
+ int optval;
+ Sock_size_t optlen = sizeof(int);
+ if (getsockopt(fd, SOL_SOCKET, SO_TYPE, (void *) &optval, &optlen) == 0) {
+ socksfd = 1;
+ invalidate = 1;
+ }
#endif
- );
-
+ 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);
+ }
+ /* 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);
+ }
+ }
+ result = PerlSIO_fclose(stdio);
+ /* We treat error from stdio as success if we invalidated
+ errno may NOT be expected EBADF
+ */
+ if (invalidate && result != 0) {
+ errno = saveerr;
+ result = 0;
+ }
+ if (socksfd) {
+ /* in SOCKS case let close() determine return value */
+ result = close(fd);
+ }
+ if (dupfd) {
+ PerlLIO_dup2(dupfd,fd);
+ close(dupfd);
+ }
+ return result;
+ }
}
-
-
SSize_t
PerlIOStdio_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
{
FILE *s = PerlIOSelf(f, PerlIOStdio)->stdio;
SSize_t got = 0;
- if (count == 1) {
- STDCHAR *buf = (STDCHAR *) vbuf;
- /*
- * Perl is expecting PerlIO_getc() to fill the buffer Linux's
- * stdio does not do that for fread()
- */
- int ch = PerlSIO_fgetc(s);
- if (ch != EOF) {
- *buf = ch;
- got = 1;
+ for (;;) {
+ if (count == 1) {
+ STDCHAR *buf = (STDCHAR *) vbuf;
+ /*
+ * Perl is expecting PerlIO_getc() to fill the buffer Linux's
+ * stdio does not do that for fread()
+ */
+ int ch = PerlSIO_fgetc(s);
+ if (ch != EOF) {
+ *buf = ch;
+ got = 1;
+ }
}
+ else
+ got = PerlSIO_fread(vbuf, 1, count, s);
+ if (got || errno != EINTR)
+ break;
+ PERL_ASYNC_CHECK();
+ errno = 0; /* just in case */
}
- else
- got = PerlSIO_fread(vbuf, 1, count, s);
return got;
}
SSize_t
PerlIOStdio_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
{
- return PerlSIO_fwrite(vbuf, 1, count,
- PerlIOSelf(f, PerlIOStdio)->stdio);
+ SSize_t got;
+ for (;;) {
+ got = PerlSIO_fwrite(vbuf, 1, count,
+ PerlIOSelf(f, PerlIOStdio)->stdio);
+ if (got || errno != EINTR)
+ break;
+ PERL_ASYNC_CHECK();
+ errno = 0; /* just in case */
+ }
+ return got;
}
IV
PerlIO_funcs PerlIO_stdio = {
+ sizeof(PerlIO_funcs),
"stdio",
sizeof(PerlIOStdio),
PERLIO_K_BUFFERED|PERLIO_K_RAW,
- PerlIOBase_pushed,
+ PerlIOStdio_pushed,
PerlIOBase_popped,
PerlIOStdio_open,
PerlIOBase_binmode, /* binmode */
#endif /* USE_STDIO_PTR */
};
+/* Note that calls to PerlIO_exportFILE() are reversed using
+ * PerlIO_releaseFILE(), not importFILE. */
FILE *
-PerlIO_exportFILE(PerlIO *f, const char *mode)
+PerlIO_exportFILE(PerlIO * f, const char *mode)
{
dTHX;
- FILE *stdio;
- char buf[8];
- PerlIO_flush(f);
- if (!mode || !*mode) {
- mode = PerlIO_modestr(f,buf);
- }
- stdio = PerlSIO_fdopen(PerlIO_fileno(f), mode);
- if (stdio) {
- if ((f = PerlIO_push(aTHX_ f, &PerlIO_stdio, buf, Nullsv))) {
- PerlIOStdio *s = PerlIOSelf(f,PerlIOStdio);
- s->stdio = stdio;
+ FILE *stdio = NULL;
+ if (PerlIOValid(f)) {
+ char buf[8];
+ PerlIO_flush(f);
+ if (!mode || !*mode) {
+ mode = PerlIO_modestr(f, buf);
+ }
+ stdio = PerlSIO_fdopen(PerlIO_fileno(f), mode);
+ if (stdio) {
+ PerlIOl *l = *f;
+ /* 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);
+ s->stdio = stdio;
+ /* Link previous lower layers under new one */
+ *PerlIONext(f) = l;
+ }
+ else {
+ /* restore layers list */
+ *f = l;
+ }
}
}
return stdio;
}
+
FILE *
PerlIO_findFILE(PerlIO *f)
{
return PerlIO_exportFILE(f, Nullch);
}
+/* Use this to reverse PerlIO_exportFILE calls. */
void
PerlIO_releaseFILE(PerlIO *p, FILE *f)
{
*/
IV
-PerlIOBuf_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
+PerlIOBuf_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
{
PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
int fd = PerlIO_fileno(f);
b->posn = posn;
}
}
- return PerlIOBase_pushed(aTHX_ f, mode, arg);
+ return PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
}
PerlIO *
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);
- if (!next || (*PerlIOBase(f)->tab->Pushed) (aTHX_ f, mode, PerlIOArg) != 0) {
+ if (!next || (*PerlIOBase(f)->tab->Pushed) (aTHX_ f, mode, PerlIOArg, self) != 0) {
return NULL;
}
}
#ifdef PERLIO_USING_CRLF
# ifdef PERLIO_IS_BINMODE_FD
if (PERLIO_IS_BINMODE_FD(fd))
- PerlIO_binmode(f, '<'/*not used*/, O_BINARY, Nullch);
+ PerlIO_binmode(aTHX_ f, '<'/*not used*/, O_BINARY, Nullch);
else
# endif
/*
PerlIO_get_base(f);
if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
return 0;
+ if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
+ if (PerlIO_flush(f) != 0) {
+ return 0;
+ }
+ }
while (count > 0) {
SSize_t avail = b->bufsiz - (b->ptr - b->buf);
if ((SSize_t) count < avail)
* b->posn is file position where b->buf was read, or will be written
*/
Off_t posn = b->posn;
+ if ((PerlIOBase(f)->flags & PERLIO_F_APPEND) &&
+ (PerlIOBase(f)->flags & PERLIO_F_WRBUF)) {
+#if 1
+ /* As O_APPEND files are normally shared in some sense it is better
+ to flush :
+ */
+ PerlIO_flush(f);
+#else
+ /* when file is NOT shared then this is sufficient */
+ PerlIO_seek(PerlIONext(f),0, SEEK_END);
+#endif
+ posn = b->posn = PerlIO_tell(PerlIONext(f));
+ }
if (b->buf) {
/*
* If buffer is valid adjust position by amount in buffer
PerlIO_funcs PerlIO_perlio = {
+ sizeof(PerlIO_funcs),
"perlio",
sizeof(PerlIOBuf),
PERLIO_K_BUFFERED|PERLIO_K_RAW,
}
IV
-PerlIOPending_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
+PerlIOPending_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
{
- IV code = PerlIOBase_pushed(aTHX_ f, mode, arg);
+ IV code = PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
PerlIOl *l = PerlIOBase(f);
/*
* Our PerlIO_fast_gets must match what we are pushed on, or sv_gets()
}
PerlIO_funcs PerlIO_pending = {
+ sizeof(PerlIO_funcs),
"pending",
sizeof(PerlIOBuf),
PERLIO_K_BUFFERED|PERLIO_K_RAW, /* not sure about RAW here */
} PerlIOCrlf;
IV
-PerlIOCrlf_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
+PerlIOCrlf_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
{
IV code;
PerlIOBase(f)->flags |= PERLIO_F_CRLF;
- code = PerlIOBuf_pushed(aTHX_ f, mode, arg);
+ code = PerlIOBuf_pushed(aTHX_ f, mode, arg, tab);
#if 0
PerlIO_debug("PerlIOCrlf_pushed f=%p %s %s fl=%08" UVxf "\n",
f, PerlIOBase(f)->tab->name, (mode) ? mode : "(Null)",
b->ptr++; /* say we have read it as far as
* flush() is concerned */
b->buf++; /* Leave space in front of buffer */
+ /* Note as we have moved buf up flush's
+ posn += ptr-buf
+ will naturally make posn point at CR
+ */
b->bufsiz--; /* Buffer is thus smaller */
code = PerlIO_fill(f); /* Fetch some more */
b->bufsiz++; /* Restore size for next time */
b->buf--; /* Point at space */
b->ptr = nl = b->buf; /* Which is what we hand
* off */
- b->posn--; /* Buffer starts here */
*nl = 0xd; /* Fill in the CR */
if (code == 0)
goto test; /* fill() call worked */
}
PerlIO_funcs PerlIO_crlf = {
+ sizeof(PerlIO_funcs),
"crlf",
sizeof(PerlIOCrlf),
PERLIO_K_BUFFERED | PERLIO_K_CANCRLF | PERLIO_K_RAW,
if (!page_size) {
#if defined(HAS_SYSCONF) && (defined(_SC_PAGESIZE) || defined(_SC_PAGE_SIZE))
{
- SETERRNO(0, SS$_NORMAL);
+ SETERRNO(0, SS_NORMAL);
# ifdef _SC_PAGESIZE
page_size = sysconf(_SC_PAGESIZE);
# else
PerlIO_funcs PerlIO_mmap = {
+ sizeof(PerlIO_funcs),
"mmap",
sizeof(PerlIOMmap),
PERLIO_K_BUFFERED|PERLIO_K_RAW,
if (f && len == sizeof(Off_t))
return PerlIO_seek(f, *posn, SEEK_SET);
}
- SETERRNO(EINVAL, SS$_IVCHAN);
+ SETERRNO(EINVAL, SS_IVCHAN);
return -1;
}
#else
#endif
}
}
- SETERRNO(EINVAL, SS$_IVCHAN);
+ SETERRNO(EINVAL, SS_IVCHAN);
return -1;
}
#endif
+
+
+