/*
- * perlio.c Copyright (c) 1996-2001, Nick Ing-Simmons You may distribute
+ * perlio.c Copyright (c) 1996-2002, 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.
*/
/*
+ * Hour after hour for nearly three weary days he had jogged up and down,
+ * over passes, and through long dales, and across many streams.
+ */
+
+/*
* 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.
* Invent a dSYS macro to abstract this out
#define PERL_IN_PERLIO_C
#include "perl.h"
+#ifdef PERL_IMPLICIT_CONTEXT
+#undef dSYS
+#define dSYS dTHX
+#endif
+
#include "XSUB.h"
int
# endif
#else
# if defined(USEMYBINMODE)
+ dTHX;
if (my_binmode(fp, iotype, mode) != FALSE)
return 1;
else
}
#ifndef O_ACCMODE
-#define O_ACCMODE 3 /* Assume traditional implementation */
+#define O_ACCMODE 3 /* Assume traditional implementation */
#endif
int
}
PerlIO *
-PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param)
+PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
{
-#ifndef PERL_MICRO
+#ifdef PERL_MICRO
+ return NULL;
+#else
+#ifdef PERL_IMPLICIT_SYS
+ return PerlSIO_fdupopen(f);
+#else
+#ifdef WIN32
+ return win32_fdupopen(f);
+#else
if (f) {
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);
+#endif
PerlIO_intmode2str(omode,mode,NULL);
/* the r+ is a hack */
return PerlIO_fdopen(fd, mode);
}
#endif
return NULL;
+#endif
+#endif
}
PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd,
int imode, int perm, PerlIO *old, int narg, SV **args)
{
- if (narg == 1) {
+ if (narg) {
+ if (narg > 1) {
+ Perl_croak(aTHX_ "More than one argument to open");
+ }
if (*args == &PL_sv_undef)
return PerlIO_tmpfile();
else {
return tmpfile();
}
-#else /* PERLIO_IS_STDIO */
+#else /* PERLIO_IS_STDIO */
#ifdef USE_SFIO
}
-#else /* USE_SFIO */
+#else /* USE_SFIO */
/*======================================================================================*/
/*
* Implement all the PerlIO interface ourselves.
if (!s)
s = "(none)";
sprintf(buffer, "%s:%" IVdf " ", s, (IV) CopLINE(PL_curcop));
- len = strlen(buffer);
+ len = strlen(buffer);
vsprintf(buffer+len, fmt, ap);
PerlLIO_write(dbg, buffer, strlen(buffer));
#else
#undef PerlIO_fdupopen
PerlIO *
-PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param)
+PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
{
- if (f && *f) {
+ if (PerlIOValid(f)) {
PerlIO_funcs *tab = PerlIOBase(f)->tab;
PerlIO *new;
- PerlIO_debug("fdupopen f=%p param=%p\n",f,param);
- new = (*tab->Dup)(aTHX_ PerlIO_allocate(aTHX),f,param);
+ PerlIO_debug("fdupopen f=%p param=%p\n",(void*)f,(void*)param);
+ new = (*tab->Dup)(aTHX_ PerlIO_allocate(aTHX),f,param, flags);
return new;
}
else {
f++;
}
}
- PerlIO_list_free(aTHX_ PL_known_layers);
- PL_known_layers = NULL;
- PerlIO_list_free(aTHX_ PL_def_layerlist);
- PL_def_layerlist = NULL;
}
void
{
PerlIOl *l = *f;
if (l) {
- PerlIO_debug("PerlIO_pop f=%p %s\n", f, l->tab->name);
+ PerlIO_debug("PerlIO_pop f=%p %s\n", (void*)f, l->tab->name);
if (l->tab->Popped) {
/*
* If popped returns non-zero do not free its layer structure
* it has either done so itself, or it is shared and still in
* use
*/
- if ((*l->tab->Popped) (f) != 0)
+ if ((*l->tab->Popped) (aTHX_ f) != 0)
return;
}
*f = l->next;;
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_debug("%.*s => %p\n", (int) len, name, f);
+ PerlIO_debug("%.*s => %p\n", (int) len, name, (void*)f);
return f;
}
}
MGVTBL perlio_vtab = {
perlio_mg_get,
perlio_mg_set,
- NULL, /* len */
+ NULL, /* len */
perlio_mg_clear,
perlio_mg_free
};
XSRETURN(count);
}
-#endif /* USE_ATTIBUTES_FOR_PERLIO */
+#endif /* USE_ATTIBUTES_FOR_PERLIO */
SV *
PerlIO_tab_sv(pTHX_ PerlIO_funcs *tab)
if (!PL_known_layers)
PL_known_layers = PerlIO_list_alloc(aTHX);
PerlIO_list_push(aTHX_ PL_known_layers, tab, Nullsv);
- PerlIO_debug("define %s %p\n", tab->name, tab);
+ PerlIO_debug("define %s %p\n", tab->name, (void*)tab);
}
int
* seen as an invalid separator character.
*/
char q = ((*s == '\'') ? '"' : '\'');
- Perl_warn(aTHX_
+ if (ckWARN(WARN_LAYER))
+ Perl_warner(aTHX_ packWARN(WARN_LAYER),
"perlio: invalid separator character %c%c%c in layer specification list %s",
q, *s, q, s);
return -1;
*/
case '\0':
e--;
- Perl_warn(aTHX_
+ if (ckWARN(WARN_LAYER))
+ Perl_warner(aTHX_ packWARN(WARN_LAYER),
"perlio: argument list not closed for layer \"%.*s\"",
(int) (e - s), s);
return -1;
}
}
if (e > s) {
+ bool warn_layer = ckWARN(WARN_LAYER);
PerlIO_funcs *layer =
PerlIO_find_layer(aTHX_ s, llen, 1);
if (layer) {
&PL_sv_undef);
}
else {
- Perl_warn(aTHX_ "perlio: unknown layer \"%.*s\"",
+ if (warn_layer)
+ Perl_warner(aTHX_ packWARN(WARN_LAYER), "perlio: unknown layer \"%.*s\"",
(int) llen, s);
return -1;
}
PerlIO_default_buffer(pTHX_ PerlIO_list_t *av)
{
PerlIO_funcs *tab = &PerlIO_perlio;
- if (O_BINARY != O_TEXT) {
- tab = &PerlIO_crlf;
- }
- else {
- if (PerlIO_stdio.Set_ptrcnt) {
- tab = &PerlIO_stdio;
- }
- }
+#ifdef PERLIO_USING_CRLF
+ tab = &PerlIO_crlf;
+#else
+ if (PerlIO_stdio.Set_ptrcnt)
+ tab = &PerlIO_stdio;
+#endif
PerlIO_debug("Pushing %s\n", tab->name);
PerlIO_list_push(aTHX_ av, PerlIO_find_layer(aTHX_ tab->name, 0, 0),
&PL_sv_undef);
{
PerlIOl *l = NULL;
Newc('L',l,tab->size,char,PerlIOl);
- if (l) {
+ 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", f, tab->name,
- (mode) ? mode : "(Null)", arg);
- if ((*l->tab->Pushed) (f, mode, arg) != 0) {
+ 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);
return NULL;
}
}
IV
-PerlIOPop_pushed(PerlIO *f, const char *mode, SV *arg)
+PerlIOPop_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
{
- dTHX;
PerlIO_pop(aTHX_ f);
if (*f) {
PerlIO_flush(f);
}
IV
-PerlIORaw_pushed(PerlIO *f, const char *mode, SV *arg)
+PerlIORaw_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
{
/*
* Remove the dummy layer
*/
- dTHX;
PerlIO_pop(aTHX_ f);
/*
* Pop back to bottom layer
*/
- if (f && *f) {
+ if (PerlIOValid(f)) {
PerlIO_flush(f);
while (!(PerlIOBase(f)->tab->kind & PERLIO_K_RAW)) {
if (*PerlIONext(f)) {
break;
}
}
- PerlIO_debug(":raw f=%p :%s\n", f, PerlIOBase(f)->tab->name);
+ PerlIO_debug(":raw f=%p :%s\n", (void*)f, PerlIOBase(f)->tab->name);
return 0;
}
return -1;
int
PerlIO_apply_layera(pTHX_ PerlIO *f, const char *mode,
- PerlIO_list_t *layers, IV n)
+ PerlIO_list_t *layers, IV n, IV max)
{
- IV max = layers->cur;
int code = 0;
while (n < max) {
PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n, NULL);
PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names)
{
int code = 0;
- if (names) {
+ if (f && names) {
PerlIO_list_t *layers = PerlIO_list_alloc(aTHX);
code = PerlIO_parse_layers(aTHX_ layers, names);
if (code == 0) {
- code = PerlIO_apply_layera(aTHX_ f, mode, layers, 0);
+ code = PerlIO_apply_layera(aTHX_ f, mode, layers, 0, layers->cur);
}
PerlIO_list_free(aTHX_ layers);
}
PerlIO_binmode(pTHX_ PerlIO *f, int iotype, int mode, const char *names)
{
PerlIO_debug("PerlIO_binmode f=%p %s %c %x %s\n",
- f, PerlIOBase(f)->tab->name, iotype, mode,
+ (void*)f, PerlIOBase(f)->tab->name, iotype, mode,
(names) ? names : "(Null)");
- PerlIO_flush(f);
- if (!names && (O_TEXT != O_BINARY && (mode & O_BINARY))) {
- PerlIO *top = f;
- while (*top) {
- if (PerlIOBase(top)->tab == &PerlIO_crlf) {
- PerlIOBase(top)->flags &= ~PERLIO_F_CRLF;
- break;
+ if (names) {
+ /* Do not flush etc. if (e.g.) switching encodings.
+ if a pushed layer knows it needs to flush lower layers
+ (for example :unix which is never going to call them)
+ it can do the flush when it is pushed.
+ */
+ return PerlIO_apply_layers(aTHX_ f, NULL, names) == 0 ? TRUE : FALSE;
+ }
+ else {
+ /* FIXME?: Looking down the layer stack seems wrong,
+ but is a way of reaching past (say) an encoding layer
+ to flip CRLF-ness of the layer(s) below
+ */
+#ifdef PERLIO_USING_CRLF
+ /* Legacy binmode only has meaning if O_TEXT has a value distinct from
+ O_BINARY so we can look for it in mode.
+ */
+ if (!(mode & O_BINARY)) {
+ /* Text mode */
+ while (*f) {
+ /* Perhaps we should turn on bottom-most aware layer
+ e.g. Ilya's idea that UNIX TTY could serve
+ */
+ if (PerlIOBase(f)->tab->kind & PERLIO_K_CANCRLF) {
+ if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF)) {
+ /* Not in text mode - flush any pending stuff and flip it */
+ PerlIO_flush(f);
+ PerlIOBase(f)->flags |= PERLIO_F_CRLF;
+ }
+ /* Only need to turn it on in one layer so we are done */
+ return TRUE;
+ }
+ f = PerlIONext(f);
}
- top = PerlIONext(top);
- PerlIO_flush(top);
+ /* Not finding a CRLF aware layer presumably means we are binary
+ which is not what was requested - so we failed
+ We _could_ push :crlf layer but so could caller
+ */
+ return FALSE;
}
+#endif
+ /* Either asked for BINMODE or that is normal on this platform
+ see if any CRLF aware layers are present and turn off the flag
+ and possibly remove layer.
+ */
+ while (*f) {
+ if (PerlIOBase(f)->tab->kind & PERLIO_K_CANCRLF) {
+ if ((PerlIOBase(f)->flags & PERLIO_F_CRLF)) {
+ /* In text mode - flush any pending stuff and flip it */
+ PerlIO_flush(f);
+ PerlIOBase(f)->flags &= ~PERLIO_F_CRLF;
+#ifndef PERLIO_USING_CRLF
+ /* CRLF is unusual case - if this is just the :crlf layer pop it */
+ if (PerlIOBase(f)->tab == &PerlIO_crlf) {
+ PerlIO_pop(aTHX_ f);
+ }
+#endif
+ /* Normal case is only one layer doing this, so exit on first
+ abnormal case can always do multiple binmode calls
+ */
+ return TRUE;
+ }
+ }
+ f = PerlIONext(f);
+ }
+ return TRUE;
}
- return PerlIO_apply_layers(aTHX_ f, NULL, names) == 0 ? TRUE : FALSE;
}
-#undef PerlIO__close
int
-PerlIO__close(PerlIO *f)
+PerlIO__close(pTHX_ PerlIO *f)
{
- if (f && *f)
- return (*PerlIOBase(f)->tab->Close) (f);
+ if (PerlIOValid(f))
+ return (*PerlIOBase(f)->tab->Close) (aTHX_ f);
else {
SETERRNO(EBADF, SS$_IVCHAN);
return -1;
}
}
-#undef PerlIO_close
int
-PerlIO_close(PerlIO *f)
+Perl_PerlIO_close(pTHX_ PerlIO *f)
{
- dTHX;
int code = -1;
- if (f && *f) {
- code = (*PerlIOBase(f)->tab->Close) (f);
+ if (PerlIOValid(f)) {
+ code = (*PerlIOBase(f)->tab->Close) (aTHX_ f);
while (*f) {
PerlIO_pop(aTHX_ f);
}
return code;
}
-#undef PerlIO_fileno
int
-PerlIO_fileno(PerlIO *f)
+Perl_PerlIO_fileno(pTHX_ PerlIO *f)
{
- if (f && *f)
- return (*PerlIOBase(f)->tab->Fileno) (f);
+ if (PerlIOValid(f))
+ return (*PerlIOBase(f)->tab->Fileno) (aTHX_ f);
else {
SETERRNO(EBADF, SS$_IVCHAN);
return -1;
* Skip to write part
*/
const char *s = strchr(type, 0);
- if (s && (s - type) < len) {
+ if (s && (STRLEN)(s - type) < len) {
type = s + 1;
}
}
PerlIO_list_t *layera = NULL;
IV n;
PerlIO_funcs *tab = NULL;
- if (f && *f) {
+ if (PerlIOValid(f)) {
/*
* This is "reopen" - it is not tested as perl does not use it
* yet
PerlIOl *l = *f;
layera = PerlIO_list_alloc(aTHX);
while (l) {
- SV *arg =
- (l->tab->Getarg) ? (*l->tab->
- Getarg) (&l) : &PL_sv_undef;
+ SV *arg = (l->tab->Getarg)
+ ? (*l->tab->Getarg) (aTHX_ &l, NULL, 0)
+ : &PL_sv_undef;
PerlIO_list_push(aTHX_ layera, l->tab, arg);
l = *PerlIONext(&l);
}
/*
* Found that layer 'n' can do opens - call it
*/
+ if (narg > 1 && !(tab->kind & PERLIO_K_MULTIARG)) {
+ 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, f, narg,
- args);
+ tab->name, layers, mode, fd, imode, perm,
+ (void*)f, narg, (void*)args);
f = (*tab->Open) (aTHX_ tab, layera, n, mode, fd, imode, perm,
f, narg, args);
if (f) {
* More layers above the one that we used to open -
* apply them now
*/
- if (PerlIO_apply_layera(aTHX_ f, mode, layera, n + 1)
- != 0) {
+ if (PerlIO_apply_layera(aTHX_ f, mode, layera, n + 1, layera->cur) != 0) {
+ /* If pushing layers fails close the file */
+ PerlIO_close(f);
f = NULL;
}
}
}
-#undef PerlIO_fdopen
-PerlIO *
-PerlIO_fdopen(int fd, const char *mode)
-{
- dTHX;
- return PerlIO_openn(aTHX_ Nullch, mode, fd, 0, 0, NULL, 0, NULL);
-}
-
-#undef PerlIO_open
-PerlIO *
-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);
-}
-
-#undef PerlIO_reopen
-PerlIO *
-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);
-}
-
-#undef PerlIO_read
SSize_t
-PerlIO_read(PerlIO *f, void *vbuf, Size_t count)
+Perl_PerlIO_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
{
- if (f && *f)
- return (*PerlIOBase(f)->tab->Read) (f, vbuf, count);
+ if (PerlIOValid(f))
+ return (*PerlIOBase(f)->tab->Read) (aTHX_ f, vbuf, count);
else {
SETERRNO(EBADF, SS$_IVCHAN);
return -1;
}
}
-#undef PerlIO_unread
SSize_t
-PerlIO_unread(PerlIO *f, const void *vbuf, Size_t count)
+Perl_PerlIO_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
{
- if (f && *f)
- return (*PerlIOBase(f)->tab->Unread) (f, vbuf, count);
+ if (PerlIOValid(f))
+ return (*PerlIOBase(f)->tab->Unread) (aTHX_ f, vbuf, count);
else {
SETERRNO(EBADF, SS$_IVCHAN);
return -1;
}
}
-#undef PerlIO_write
SSize_t
-PerlIO_write(PerlIO *f, const void *vbuf, Size_t count)
+Perl_PerlIO_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
{
- if (f && *f)
- return (*PerlIOBase(f)->tab->Write) (f, vbuf, count);
+ if (PerlIOValid(f))
+ return (*PerlIOBase(f)->tab->Write) (aTHX_ f, vbuf, count);
else {
SETERRNO(EBADF, SS$_IVCHAN);
return -1;
}
}
-#undef PerlIO_seek
int
-PerlIO_seek(PerlIO *f, Off_t offset, int whence)
+Perl_PerlIO_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
{
- if (f && *f)
- return (*PerlIOBase(f)->tab->Seek) (f, offset, whence);
+ if (PerlIOValid(f))
+ return (*PerlIOBase(f)->tab->Seek) (aTHX_ f, offset, whence);
else {
SETERRNO(EBADF, SS$_IVCHAN);
return -1;
}
}
-#undef PerlIO_tell
Off_t
-PerlIO_tell(PerlIO *f)
+Perl_PerlIO_tell(pTHX_ PerlIO *f)
{
- if (f && *f)
- return (*PerlIOBase(f)->tab->Tell) (f);
+ if (PerlIOValid(f))
+ return (*PerlIOBase(f)->tab->Tell) (aTHX_ f);
else {
SETERRNO(EBADF, SS$_IVCHAN);
return -1;
}
}
-#undef PerlIO_flush
int
-PerlIO_flush(PerlIO *f)
+Perl_PerlIO_flush(pTHX_ PerlIO *f)
{
if (f) {
if (*f) {
PerlIO_funcs *tab = PerlIOBase(f)->tab;
if (tab && tab->Flush) {
- return (*tab->Flush) (f);
+ return (*tab->Flush) (aTHX_ f);
}
else {
- PerlIO_debug("Cannot flush f=%p :%s\n", f, tab->name);
+ PerlIO_debug("Cannot flush f=%p :%s\n", (void*)f, tab->name);
SETERRNO(EBADF, SS$_IVCHAN);
return -1;
}
}
else {
- PerlIO_debug("Cannot flush f=%p\n", f);
+ PerlIO_debug("Cannot flush f=%p\n", (void*)f);
SETERRNO(EBADF, SS$_IVCHAN);
return -1;
}
* things on fflush(NULL), but should we be bound by their design
* decisions? --jhi
*/
- dTHX;
PerlIO **table = &PL_perlio;
int code = 0;
while ((f = *table)) {
}
void
-PerlIOBase_flush_linebuf()
+PerlIOBase_flush_linebuf(pTHX)
{
- dTHX;
PerlIO **table = &PL_perlio;
PerlIO *f;
while ((f = *table)) {
}
}
-#undef PerlIO_fill
int
-PerlIO_fill(PerlIO *f)
+Perl_PerlIO_fill(pTHX_ PerlIO *f)
{
- if (f && *f)
- return (*PerlIOBase(f)->tab->Fill) (f);
+ if (PerlIOValid(f))
+ return (*PerlIOBase(f)->tab->Fill) (aTHX_ f);
else {
SETERRNO(EBADF, SS$_IVCHAN);
return -1;
}
}
-#undef PerlIO_isutf8
int
PerlIO_isutf8(PerlIO *f)
{
- if (f && *f)
+ if (PerlIOValid(f))
return (PerlIOBase(f)->flags & PERLIO_F_UTF8) != 0;
else {
SETERRNO(EBADF, SS$_IVCHAN);
}
}
-#undef PerlIO_eof
int
-PerlIO_eof(PerlIO *f)
+Perl_PerlIO_eof(pTHX_ PerlIO *f)
{
- if (f && *f)
- return (*PerlIOBase(f)->tab->Eof) (f);
+ if (PerlIOValid(f))
+ return (*PerlIOBase(f)->tab->Eof) (aTHX_ f);
else {
SETERRNO(EBADF, SS$_IVCHAN);
return -1;
}
}
-#undef PerlIO_error
int
-PerlIO_error(PerlIO *f)
+Perl_PerlIO_error(pTHX_ PerlIO *f)
{
- if (f && *f)
- return (*PerlIOBase(f)->tab->Error) (f);
+ if (PerlIOValid(f))
+ return (*PerlIOBase(f)->tab->Error) (aTHX_ f);
else {
SETERRNO(EBADF, SS$_IVCHAN);
return -1;
}
}
-#undef PerlIO_clearerr
void
-PerlIO_clearerr(PerlIO *f)
+Perl_PerlIO_clearerr(pTHX_ PerlIO *f)
{
- if (f && *f)
- (*PerlIOBase(f)->tab->Clearerr) (f);
+ if (PerlIOValid(f))
+ (*PerlIOBase(f)->tab->Clearerr) (aTHX_ f);
else
SETERRNO(EBADF, SS$_IVCHAN);
}
-#undef PerlIO_setlinebuf
void
-PerlIO_setlinebuf(PerlIO *f)
+Perl_PerlIO_setlinebuf(pTHX_ PerlIO *f)
{
- if (f && *f)
- (*PerlIOBase(f)->tab->Setlinebuf) (f);
+ if (PerlIOValid(f))
+ (*PerlIOBase(f)->tab->Setlinebuf) (aTHX_ f);
else
SETERRNO(EBADF, SS$_IVCHAN);
}
-#undef PerlIO_has_base
int
PerlIO_has_base(PerlIO *f)
{
- if (f && *f) {
+ if (PerlIOValid(f)) {
return (PerlIOBase(f)->tab->Get_base != NULL);
}
return 0;
}
-#undef PerlIO_fast_gets
int
PerlIO_fast_gets(PerlIO *f)
{
- if (f && *f && (PerlIOBase(f)->flags & PERLIO_F_FASTGETS)) {
+ if (PerlIOValid(f) && (PerlIOBase(f)->flags & PERLIO_F_FASTGETS)) {
PerlIO_funcs *tab = PerlIOBase(f)->tab;
return (tab->Set_ptrcnt != NULL);
}
return 0;
}
-#undef PerlIO_has_cntptr
int
PerlIO_has_cntptr(PerlIO *f)
{
- if (f && *f) {
+ if (PerlIOValid(f)) {
PerlIO_funcs *tab = PerlIOBase(f)->tab;
return (tab->Get_ptr != NULL && tab->Get_cnt != NULL);
}
return 0;
}
-#undef PerlIO_canset_cnt
int
PerlIO_canset_cnt(PerlIO *f)
{
- if (f && *f) {
+ if (PerlIOValid(f)) {
PerlIOl *l = PerlIOBase(f);
return (l->tab->Set_ptrcnt != NULL);
}
return 0;
}
-#undef PerlIO_get_base
STDCHAR *
-PerlIO_get_base(PerlIO *f)
+Perl_PerlIO_get_base(pTHX_ PerlIO *f)
{
- if (f && *f)
- return (*PerlIOBase(f)->tab->Get_base) (f);
+ if (PerlIOValid(f))
+ return (*PerlIOBase(f)->tab->Get_base) (aTHX_ f);
return NULL;
}
-#undef PerlIO_get_bufsiz
int
-PerlIO_get_bufsiz(PerlIO *f)
+Perl_PerlIO_get_bufsiz(pTHX_ PerlIO *f)
{
- if (f && *f)
- return (*PerlIOBase(f)->tab->Get_bufsiz) (f);
+ if (PerlIOValid(f))
+ return (*PerlIOBase(f)->tab->Get_bufsiz) (aTHX_ f);
return 0;
}
-#undef PerlIO_get_ptr
STDCHAR *
-PerlIO_get_ptr(PerlIO *f)
+Perl_PerlIO_get_ptr(pTHX_ PerlIO *f)
{
- PerlIO_funcs *tab = PerlIOBase(f)->tab;
- if (tab->Get_ptr == NULL)
- return NULL;
- return (*tab->Get_ptr) (f);
+ if (PerlIOValid(f)) {
+ PerlIO_funcs *tab = PerlIOBase(f)->tab;
+ if (tab->Get_ptr == NULL)
+ return NULL;
+ return (*tab->Get_ptr) (aTHX_ f);
+ }
+ return NULL;
}
-#undef PerlIO_get_cnt
int
-PerlIO_get_cnt(PerlIO *f)
+Perl_PerlIO_get_cnt(pTHX_ PerlIO *f)
{
- PerlIO_funcs *tab = PerlIOBase(f)->tab;
- if (tab->Get_cnt == NULL)
- return 0;
- return (*tab->Get_cnt) (f);
+ if (PerlIOValid(f)) {
+ PerlIO_funcs *tab = PerlIOBase(f)->tab;
+ if (tab->Get_cnt == NULL)
+ return 0;
+ return (*tab->Get_cnt) (aTHX_ f);
+ }
+ return 0;
}
-#undef PerlIO_set_cnt
void
-PerlIO_set_cnt(PerlIO *f, int cnt)
+Perl_PerlIO_set_cnt(pTHX_ PerlIO *f, int cnt)
{
- (*PerlIOBase(f)->tab->Set_ptrcnt) (f, NULL, cnt);
+ if (PerlIOValid(f)) {
+ (*PerlIOBase(f)->tab->Set_ptrcnt) (aTHX_ f, NULL, cnt);
+ }
}
-#undef PerlIO_set_ptrcnt
void
-PerlIO_set_ptrcnt(PerlIO *f, STDCHAR * ptr, int cnt)
+Perl_PerlIO_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, int cnt)
{
- PerlIO_funcs *tab = PerlIOBase(f)->tab;
- if (tab->Set_ptrcnt == NULL) {
- dTHX;
- Perl_croak(aTHX_ "PerlIO buffer snooping abuse");
+ if (PerlIOValid(f)) {
+ PerlIO_funcs *tab = PerlIOBase(f)->tab;
+ if (tab->Set_ptrcnt == NULL) {
+ Perl_croak(aTHX_ "PerlIO buffer snooping abuse");
+ }
+ (*PerlIOBase(f)->tab->Set_ptrcnt) (aTHX_ f, ptr, cnt);
}
- (*PerlIOBase(f)->tab->Set_ptrcnt) (f, ptr, cnt);
}
/*--------------------------------------------------------------------------------------*/
*/
IV
-PerlIOUtf8_pushed(PerlIO *f, const char *mode, SV *arg)
+PerlIOUtf8_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
{
- if (PerlIONext(f)) {
- dTHX;
+ if (*PerlIONext(f)) {
PerlIO_funcs *tab = PerlIOBase(f)->tab;
PerlIO_pop(aTHX_ f);
if (tab->kind & PERLIO_K_UTF8)
NULL,
NULL,
NULL,
- NULL, /* flush */
- NULL, /* fill */
+ NULL, /* flush */
+ NULL, /* fill */
NULL,
NULL,
NULL,
NULL,
- NULL, /* get_base */
- NULL, /* get_bufsiz */
- NULL, /* get_ptr */
- NULL, /* get_cnt */
- NULL, /* set_ptrcnt */
+ NULL, /* get_base */
+ NULL, /* get_bufsiz */
+ NULL, /* get_ptr */
+ NULL, /* get_cnt */
+ NULL, /* set_ptrcnt */
};
PerlIO_funcs PerlIO_byte = {
NULL,
NULL,
NULL,
- NULL, /* flush */
- NULL, /* fill */
+ NULL, /* flush */
+ NULL, /* fill */
NULL,
NULL,
NULL,
NULL,
- NULL, /* get_base */
- NULL, /* get_bufsiz */
- NULL, /* get_ptr */
- NULL, /* get_cnt */
- NULL, /* set_ptrcnt */
+ NULL, /* get_base */
+ NULL, /* get_bufsiz */
+ NULL, /* get_ptr */
+ NULL, /* get_cnt */
+ NULL, /* set_ptrcnt */
};
PerlIO *
NULL,
NULL,
NULL,
- NULL, /* flush */
- NULL, /* fill */
+ NULL, /* flush */
+ NULL, /* fill */
NULL,
NULL,
NULL,
NULL,
- NULL, /* get_base */
- NULL, /* get_bufsiz */
- NULL, /* get_ptr */
- NULL, /* get_cnt */
- NULL, /* set_ptrcnt */
+ NULL, /* get_base */
+ NULL, /* get_bufsiz */
+ NULL, /* get_ptr */
+ NULL, /* get_cnt */
+ NULL, /* set_ptrcnt */
};
/*--------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------*/
*/
IV
-PerlIOBase_fileno(PerlIO *f)
+PerlIOBase_fileno(pTHX_ PerlIO *f)
{
- return PerlIO_fileno(PerlIONext(f));
+ return PerlIOValid(f) ? PerlIO_fileno(PerlIONext(f)) : -1;
}
char *
*s++ = '+';
}
}
-#if O_TEXT != O_BINARY
+#ifdef PERLIO_USING_CRLF
if (!(flags & PERLIO_F_CRLF))
*s++ = 'b';
#endif
}
IV
-PerlIOBase_pushed(PerlIO *f, const char *mode, SV *arg)
+PerlIOBase_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
{
PerlIOl *l = PerlIOBase(f);
#if 0
}
IV
-PerlIOBase_popped(PerlIO *f)
+PerlIOBase_popped(pTHX_ PerlIO *f)
{
return 0;
}
SSize_t
-PerlIOBase_unread(PerlIO *f, const void *vbuf, Size_t count)
+PerlIOBase_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
{
- dTHX;
/*
* Save the position as current head considers it
*/
SSize_t done;
PerlIO_push(aTHX_ f, &PerlIO_pending, "r", Nullsv);
PerlIOSelf(f, PerlIOBuf)->posn = old;
- done = PerlIOBuf_unread(f, vbuf, count);
+ done = PerlIOBuf_unread(aTHX_ f, vbuf, count);
return done;
}
SSize_t
-PerlIOBase_read(PerlIO *f, void *vbuf, Size_t count)
+PerlIOBase_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
{
STDCHAR *buf = (STDCHAR *) vbuf;
if (f) {
SSize_t avail = PerlIO_get_cnt(f);
SSize_t take = 0;
if (avail > 0)
- take = (count < avail) ? count : avail;
+ take = ((SSize_t)count < avail) ? count : avail;
if (take > 0) {
STDCHAR *ptr = PerlIO_get_ptr(f);
Copy(ptr, buf, take, STDCHAR);
}
IV
-PerlIOBase_noop_ok(PerlIO *f)
+PerlIOBase_noop_ok(pTHX_ PerlIO *f)
{
return 0;
}
IV
-PerlIOBase_noop_fail(PerlIO *f)
+PerlIOBase_noop_fail(pTHX_ PerlIO *f)
{
return -1;
}
IV
-PerlIOBase_close(PerlIO *f)
+PerlIOBase_close(pTHX_ PerlIO *f)
{
IV code = 0;
PerlIO *n = PerlIONext(f);
if (PerlIO_flush(f) != 0)
code = -1;
- if (n && *n && (*PerlIOBase(n)->tab->Close) (n) != 0)
+ if (PerlIOValid(n) && (*PerlIOBase(n)->tab->Close)(aTHX_ n) != 0)
code = -1;
PerlIOBase(f)->flags &=
~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_OPEN);
}
IV
-PerlIOBase_eof(PerlIO *f)
+PerlIOBase_eof(pTHX_ PerlIO *f)
{
- if (f && *f) {
+ if (PerlIOValid(f)) {
return (PerlIOBase(f)->flags & PERLIO_F_EOF) != 0;
}
return 1;
}
IV
-PerlIOBase_error(PerlIO *f)
+PerlIOBase_error(pTHX_ PerlIO *f)
{
- if (f && *f) {
+ if (PerlIOValid(f)) {
return (PerlIOBase(f)->flags & PERLIO_F_ERROR) != 0;
}
return 1;
}
void
-PerlIOBase_clearerr(PerlIO *f)
+PerlIOBase_clearerr(pTHX_ PerlIO *f)
{
- if (f && *f) {
+ if (PerlIOValid(f)) {
PerlIO *n = PerlIONext(f);
PerlIOBase(f)->flags &= ~(PERLIO_F_ERROR | PERLIO_F_EOF);
- if (n)
+ if (PerlIOValid(n))
PerlIO_clearerr(n);
}
}
void
-PerlIOBase_setlinebuf(PerlIO *f)
+PerlIOBase_setlinebuf(pTHX_ PerlIO *f)
{
- if (f) {
+ if (PerlIOValid(f)) {
PerlIOBase(f)->flags |= PERLIO_F_LINEBUF;
}
}
}
PerlIO *
-PerlIOBase_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param)
+PerlIOBase_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
{
PerlIO *nexto = PerlIONext(o);
- if (*nexto) {
+ if (PerlIOValid(nexto)) {
PerlIO_funcs *tab = PerlIOBase(nexto)->tab;
- f = (*tab->Dup)(aTHX_ f, nexto, param);
+ f = (*tab->Dup)(aTHX_ f, nexto, param, flags);
}
if (f) {
PerlIO_funcs *self = PerlIOBase(o)->tab;
SV *arg = Nullsv;
char buf[8];
- PerlIO_debug("PerlIOBase_dup %s f=%p o=%p param=%p\n",self->name,f,o,param);
+ PerlIO_debug("PerlIOBase_dup %s f=%p o=%p param=%p\n",
+ self->name, (void*)f, (void*)o, (void*)param);
if (self->Getarg) {
- arg = (*self->Getarg)(o);
- if (arg) {
- arg = PerlIO_sv_dup(aTHX_ arg, param);
- }
+ arg = (*self->Getarg)(aTHX_ o,param,flags);
}
f = PerlIO_push(aTHX_ f, self, PerlIO_modestr(o,buf), arg);
- if (!f && arg) {
+ if (arg) {
SvREFCNT_dec(arg);
}
}
{
int i;
#ifdef USE_ITHREADS
- PerlIO_debug("Cleanup %p\n",aTHX);
+ PerlIO_debug("Cleanup layers for %p\n",aTHX);
+#else
+ PerlIO_debug("Cleanup layers\n");
#endif
/* Raise STDIN..STDERR refcount so we don't close them */
for (i=0; i < 3; i++)
/* Restore STDIN..STDERR refcount */
for (i=0; i < 3; i++)
PerlIOUnix_refcnt_dec(i);
+
+ if (PL_known_layers) {
+ PerlIO_list_free(aTHX_ PL_known_layers);
+ PL_known_layers = NULL;
+ }
+ if(PL_def_layerlist) {
+ PerlIO_list_free(aTHX_ PL_def_layerlist);
+ PL_def_layerlist = NULL;
+ }
}
*/
typedef struct {
- struct _PerlIO base; /* The generic part */
- int fd; /* UNIX like file descriptor */
- int oflags; /* open/fcntl flags */
+ struct _PerlIO base; /* The generic part */
+ int fd; /* UNIX like file descriptor */
+ int oflags; /* open/fcntl flags */
} PerlIOUnix;
int
}
IV
-PerlIOUnix_fileno(PerlIO *f)
+PerlIOUnix_fileno(pTHX_ PerlIO *f)
{
return PerlIOSelf(f, PerlIOUnix)->fd;
}
IV
-PerlIOUnix_pushed(PerlIO *f, const char *mode, SV *arg)
+PerlIOUnix_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
{
- IV code = PerlIOBase_pushed(f, mode, arg);
+ IV code = PerlIOBase_pushed(aTHX_ f, mode, arg);
PerlIOUnix *s = PerlIOSelf(f, PerlIOUnix);
if (*PerlIONext(f)) {
+ /* We never call down so any pending stuff now */
+ PerlIO_flush(PerlIONext(f));
s->fd = PerlIO_fileno(PerlIONext(f));
/*
* XXX could (or should) we retrieve the oflags from the open file
IV n, const char *mode, int fd, int imode,
int perm, PerlIO *f, int narg, SV **args)
{
- if (f) {
+ if (PerlIOValid(f)) {
if (PerlIOBase(f)->flags & PERLIO_F_OPEN)
- (*PerlIOBase(f)->tab->Close) (f);
+ (*PerlIOBase(f)->tab->Close)(aTHX_ f);
}
if (narg > 0) {
char *path = SvPV_nolen(*args);
mode++;
if (!f) {
f = PerlIO_allocate(aTHX);
+ }
+ if (!PerlIOValid(f)) {
s = PerlIOSelf(PerlIO_push(aTHX_ f, self, mode, PerlIOArg),
PerlIOUnix);
}
- else
+ else {
s = PerlIOSelf(f, PerlIOUnix);
+ }
s->fd = fd;
s->oflags = imode;
PerlIOBase(f)->flags |= PERLIO_F_OPEN;
- PerlIOUnix_refcnt_inc(fd);
+ PerlIOUnix_refcnt_inc(fd);
return f;
}
else {
}
PerlIO *
-PerlIOUnix_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param)
+PerlIOUnix_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
{
PerlIOUnix *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) {
- f = PerlIOBase_dup(aTHX_ f, o, param);
+ 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 */
PerlIOUnix *s = PerlIOSelf(f, PerlIOUnix);
SSize_t
-PerlIOUnix_read(PerlIO *f, void *vbuf, Size_t count)
+PerlIOUnix_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
{
- dTHX;
int fd = PerlIOSelf(f, PerlIOUnix)->fd;
if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD))
return 0;
}
SSize_t
-PerlIOUnix_write(PerlIO *f, const void *vbuf, Size_t count)
+PerlIOUnix_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
{
- dTHX;
int fd = PerlIOSelf(f, PerlIOUnix)->fd;
while (1) {
SSize_t len = PerlLIO_write(fd, vbuf, count);
}
IV
-PerlIOUnix_seek(PerlIO *f, Off_t offset, int whence)
+PerlIOUnix_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
{
- dSYS;
Off_t new =
PerlLIO_lseek(PerlIOSelf(f, PerlIOUnix)->fd, offset, whence);
PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
}
Off_t
-PerlIOUnix_tell(PerlIO *f)
+PerlIOUnix_tell(pTHX_ PerlIO *f)
{
- dSYS;
return PerlLIO_lseek(PerlIOSelf(f, PerlIOUnix)->fd, 0, SEEK_CUR);
}
IV
-PerlIOUnix_close(PerlIO *f)
+PerlIOUnix_close(pTHX_ PerlIO *f)
{
- dTHX;
int fd = PerlIOSelf(f, PerlIOUnix)->fd;
int code = 0;
if (PerlIOBase(f)->flags & PERLIO_F_OPEN) {
if (PerlIOUnix_refcnt_dec(fd) > 0) {
PerlIOBase(f)->flags &= ~PERLIO_F_OPEN;
return 0;
- }
+ }
}
else {
SETERRNO(EBADF,SS$_IVCHAN);
PerlIOUnix_seek,
PerlIOUnix_tell,
PerlIOUnix_close,
- PerlIOBase_noop_ok, /* flush */
- PerlIOBase_noop_fail, /* fill */
+ PerlIOBase_noop_ok, /* flush */
+ PerlIOBase_noop_fail, /* fill */
PerlIOBase_eof,
PerlIOBase_error,
PerlIOBase_clearerr,
PerlIOBase_setlinebuf,
- NULL, /* get_base */
- NULL, /* get_bufsiz */
- NULL, /* get_ptr */
- NULL, /* get_cnt */
- NULL, /* set_ptrcnt */
+ NULL, /* get_base */
+ NULL, /* get_bufsiz */
+ NULL, /* get_ptr */
+ NULL, /* get_cnt */
+ NULL, /* set_ptrcnt */
};
/*--------------------------------------------------------------------------------------*/
typedef struct {
struct _PerlIO base;
- FILE *stdio; /* The stream */
+ FILE *stdio; /* The stream */
} PerlIOStdio;
IV
-PerlIOStdio_fileno(PerlIO *f)
+PerlIOStdio_fileno(pTHX_ PerlIO *f)
{
- dSYS;
return PerlSIO_fileno(PerlIOSelf(f, PerlIOStdio)->stdio);
}
while (*mode) {
*tmode++ = *mode++;
}
- if (O_BINARY != O_TEXT) {
- *tmode++ = 'b';
- }
+#ifdef PERLIO_USING_CRLF
+ *tmode++ = 'b';
+#endif
*tmode = '\0';
return ret;
}
* This isn't used yet ...
*/
IV
-PerlIOStdio_pushed(PerlIO *f, const char *mode, SV *arg)
+PerlIOStdio_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
{
if (*PerlIONext(f)) {
- dSYS;
PerlIOStdio *s = PerlIOSelf(f, PerlIOStdio);
char tmode[8];
FILE *stdio =
PerlSIO_fdopen(PerlIO_fileno(PerlIONext(f)), mode =
PerlIOStdio_mode(mode, tmode));
- if (stdio)
+ if (stdio) {
s->stdio = stdio;
+ /* We never call down so any pending stuff now */
+ PerlIO_flush(PerlIONext(f));
+ }
else
return -1;
}
- return PerlIOBase_pushed(f, mode, arg);
+ return PerlIOBase_pushed(aTHX_ f, mode, arg);
}
-#undef PerlIO_importFILE
+
PerlIO *
PerlIO_importFILE(FILE *stdio, int fl)
{
dTHX;
PerlIO *f = NULL;
if (stdio) {
- PerlIOStdio *s =
- PerlIOSelf(PerlIO_push
- (aTHX_(f = PerlIO_allocate(aTHX)), &PerlIO_stdio,
- "r+", Nullsv), PerlIOStdio);
+ /* We need to probe to see how we can open the stream
+ so start with read/write and then try write and read
+ we dup() so that we can fclose without loosing the fd.
+ */
+ int fd = PerlLIO_dup(fileno(stdio));
+ char *mode = "r+";
+ FILE *f2 = fdopen(fd, mode);
+ PerlIOStdio *s;
+ if (!f2 && errno == EINVAL) {
+ mode = "w";
+ f2 = fdopen(fd, mode);
+ }
+ if (!f2 && errno == EINVAL) {
+ mode = "r";
+ f2 = fdopen(fd, mode);
+ }
+ if (!f2) {
+ /* Don't seem to be able to open */
+ return f;
+ }
+ fclose(f2);
+ s = PerlIOSelf(PerlIO_push
+ (aTHX_(f = PerlIO_allocate(aTHX)), &PerlIO_stdio,
+ mode, Nullsv), PerlIOStdio);
s->stdio = stdio;
}
return f;
int perm, PerlIO *f, int narg, SV **args)
{
char tmode[8];
- if (f) {
+ if (PerlIOValid(f)) {
char *path = SvPV_nolen(*args);
PerlIOStdio *s = PerlIOSelf(f, PerlIOStdio);
FILE *stdio;
else {
FILE *stdio = PerlSIO_fopen(path, mode);
if (stdio) {
- PerlIOStdio *s =
- PerlIOSelf(PerlIO_push
- (aTHX_(f = PerlIO_allocate(aTHX)), self,
+ PerlIOStdio *s;
+ if (!f) {
+ f = PerlIO_allocate(aTHX);
+ }
+ s = PerlIOSelf(PerlIO_push(aTHX_ f, self,
(mode = PerlIOStdio_mode(mode, tmode)),
PerlIOArg),
PerlIOStdio);
PerlIOStdio_mode(mode, tmode));
}
if (stdio) {
- PerlIOStdio *s =
- PerlIOSelf(PerlIO_push
- (aTHX_(f = PerlIO_allocate(aTHX)), self,
- mode, PerlIOArg), PerlIOStdio);
+ PerlIOStdio *s;
+ if (!f) {
+ f = PerlIO_allocate(aTHX);
+ }
+ s = PerlIOSelf(PerlIO_push(aTHX_ f, self, mode, PerlIOArg), PerlIOStdio);
s->stdio = stdio;
PerlIOUnix_refcnt_inc(fileno(s->stdio));
return f;
}
PerlIO *
-PerlIOStdio_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param)
+PerlIOStdio_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
{
/* This assumes no layers underneath - which is what
happens, but is not how I remember it. NI-S 2001/10/16
*/
- if ((f = PerlIOBase_dup(aTHX_ f, o, param))) {
+ if ((f = PerlIOBase_dup(aTHX_ f, o, param, flags))) {
FILE *stdio = PerlIOSelf(o, PerlIOStdio)->stdio;
+ if (flags & PERLIO_DUP_FD) {
+ int fd = PerlLIO_dup(fileno(stdio));
+ if (fd >= 0) {
+ char mode[8];
+ stdio = fdopen(fd, PerlIO_modestr(o,mode));
+ }
+ else {
+ /* FIXME: To avoid messy error recovery if dup fails
+ re-use the existing stdio as though flag was not set
+ */
+ }
+ }
PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
PerlIOUnix_refcnt_inc(fileno(stdio));
}
}
IV
-PerlIOStdio_close(PerlIO *f)
+PerlIOStdio_close(pTHX_ PerlIO *f)
{
- dSYS;
#ifdef SOCKS5_VERSION_NAME
int optval;
Sock_size_t optlen = sizeof(int);
FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
if (PerlIOUnix_refcnt_dec(fileno(stdio)) > 0) {
/* Do not close it but do flush any buffers */
- PerlIO_flush(f);
- return 0;
+ return PerlIO_flush(f);
}
return (
#ifdef SOCKS5_VERSION_NAME
SSize_t
-PerlIOStdio_read(PerlIO *f, void *vbuf, Size_t count)
+PerlIOStdio_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
{
- dSYS;
FILE *s = PerlIOSelf(f, PerlIOStdio)->stdio;
SSize_t got = 0;
if (count == 1) {
}
SSize_t
-PerlIOStdio_unread(PerlIO *f, const void *vbuf, Size_t count)
+PerlIOStdio_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
{
- dSYS;
FILE *s = PerlIOSelf(f, PerlIOStdio)->stdio;
STDCHAR *buf = ((STDCHAR *) vbuf) + count - 1;
SSize_t unread = 0;
}
SSize_t
-PerlIOStdio_write(PerlIO *f, const void *vbuf, Size_t count)
+PerlIOStdio_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
{
- dSYS;
return PerlSIO_fwrite(vbuf, 1, count,
PerlIOSelf(f, PerlIOStdio)->stdio);
}
IV
-PerlIOStdio_seek(PerlIO *f, Off_t offset, int whence)
+PerlIOStdio_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
{
- dSYS;
FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
return PerlSIO_fseek(stdio, offset, whence);
}
Off_t
-PerlIOStdio_tell(PerlIO *f)
+PerlIOStdio_tell(pTHX_ PerlIO *f)
{
- dSYS;
FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
return PerlSIO_ftell(stdio);
}
IV
-PerlIOStdio_flush(PerlIO *f)
+PerlIOStdio_flush(pTHX_ PerlIO *f)
{
- dSYS;
FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
if (PerlIOBase(f)->flags & PERLIO_F_CANWRITE) {
return PerlSIO_fflush(stdio);
}
IV
-PerlIOStdio_fill(PerlIO *f)
+PerlIOStdio_fill(pTHX_ PerlIO *f)
{
- dSYS;
FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
int c;
/*
}
IV
-PerlIOStdio_eof(PerlIO *f)
+PerlIOStdio_eof(pTHX_ PerlIO *f)
{
- dSYS;
return PerlSIO_feof(PerlIOSelf(f, PerlIOStdio)->stdio);
}
IV
-PerlIOStdio_error(PerlIO *f)
+PerlIOStdio_error(pTHX_ PerlIO *f)
{
- dSYS;
return PerlSIO_ferror(PerlIOSelf(f, PerlIOStdio)->stdio);
}
void
-PerlIOStdio_clearerr(PerlIO *f)
+PerlIOStdio_clearerr(pTHX_ PerlIO *f)
{
- dSYS;
PerlSIO_clearerr(PerlIOSelf(f, PerlIOStdio)->stdio);
}
void
-PerlIOStdio_setlinebuf(PerlIO *f)
+PerlIOStdio_setlinebuf(pTHX_ PerlIO *f)
{
- dSYS;
#ifdef HAS_SETLINEBUF
PerlSIO_setlinebuf(PerlIOSelf(f, PerlIOStdio)->stdio);
#else
#ifdef FILE_base
STDCHAR *
-PerlIOStdio_get_base(PerlIO *f)
+PerlIOStdio_get_base(pTHX_ PerlIO *f)
{
- dSYS;
FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
- return PerlSIO_get_base(stdio);
+ return (STDCHAR*)PerlSIO_get_base(stdio);
}
Size_t
-PerlIOStdio_get_bufsiz(PerlIO *f)
+PerlIOStdio_get_bufsiz(pTHX_ PerlIO *f)
{
- dSYS;
FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
return PerlSIO_get_bufsiz(stdio);
}
#ifdef USE_STDIO_PTR
STDCHAR *
-PerlIOStdio_get_ptr(PerlIO *f)
+PerlIOStdio_get_ptr(pTHX_ PerlIO *f)
{
- dSYS;
FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
- return PerlSIO_get_ptr(stdio);
+ return (STDCHAR*)PerlSIO_get_ptr(stdio);
}
SSize_t
-PerlIOStdio_get_cnt(PerlIO *f)
+PerlIOStdio_get_cnt(pTHX_ PerlIO *f)
{
- dSYS;
FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
return PerlSIO_get_cnt(stdio);
}
void
-PerlIOStdio_set_ptrcnt(PerlIO *f, STDCHAR * ptr, SSize_t cnt)
+PerlIOStdio_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
{
FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
- dSYS;
if (ptr != NULL) {
#ifdef STDIO_PTR_LVALUE
- PerlSIO_set_ptr(stdio, ptr);
+ PerlSIO_set_ptr(stdio, (void*)ptr); /* LHS STDCHAR* cast non-portable */
#ifdef STDIO_PTR_LVAL_SETS_CNT
if (PerlSIO_get_cnt(stdio) != (cnt)) {
- dTHX;
assert(PerlSIO_get_cnt(stdio) == (cnt));
}
#endif
*/
return;
#endif
-#else /* STDIO_PTR_LVALUE */
+#else /* STDIO_PTR_LVALUE */
PerlProc_abort();
-#endif /* STDIO_PTR_LVALUE */
+#endif /* STDIO_PTR_LVALUE */
}
/*
* Now (or only) set cnt
*/
#ifdef STDIO_CNT_LVALUE
PerlSIO_set_cnt(stdio, cnt);
-#else /* STDIO_CNT_LVALUE */
+#else /* STDIO_CNT_LVALUE */
#if (defined(STDIO_PTR_LVALUE) && defined(STDIO_PTR_LVAL_SETS_CNT))
PerlSIO_set_ptr(stdio,
PerlSIO_get_ptr(stdio) + (PerlSIO_get_cnt(stdio) -
cnt));
-#else /* STDIO_PTR_LVAL_SETS_CNT */
+#else /* STDIO_PTR_LVAL_SETS_CNT */
PerlProc_abort();
-#endif /* STDIO_PTR_LVAL_SETS_CNT */
-#endif /* STDIO_CNT_LVALUE */
+#endif /* STDIO_PTR_LVAL_SETS_CNT */
+#endif /* STDIO_CNT_LVALUE */
}
#endif
PerlIOStdio_get_cnt,
#if (defined(STDIO_PTR_LVALUE) && (defined(STDIO_CNT_LVALUE) || defined(STDIO_PTR_LVAL_SETS_CNT)))
PerlIOStdio_set_ptrcnt
-#else /* STDIO_PTR_LVALUE */
+#else /* STDIO_PTR_LVALUE */
NULL
-#endif /* STDIO_PTR_LVALUE */
-#else /* USE_STDIO_PTR */
+#endif /* STDIO_PTR_LVALUE */
+#else /* USE_STDIO_PTR */
NULL,
NULL,
NULL
-#endif /* USE_STDIO_PTR */
+#endif /* USE_STDIO_PTR */
};
-#undef PerlIO_exportFILE
FILE *
PerlIO_exportFILE(PerlIO *f, int fl)
{
+ dTHX;
FILE *stdio;
+ char buf[8];
PerlIO_flush(f);
- stdio = fdopen(PerlIO_fileno(f), "r+");
+ stdio = fdopen(PerlIO_fileno(f), PerlIO_modestr(f,buf));
if (stdio) {
- dTHX;
PerlIOStdio *s =
- PerlIOSelf(PerlIO_push(aTHX_ f, &PerlIO_stdio, "r+", Nullsv),
+ PerlIOSelf(PerlIO_push(aTHX_ f, &PerlIO_stdio, buf, Nullsv),
PerlIOStdio);
s->stdio = stdio;
}
return stdio;
}
-#undef PerlIO_findFILE
FILE *
PerlIO_findFILE(PerlIO *f)
{
return PerlIO_exportFILE(f, 0);
}
-#undef PerlIO_releaseFILE
void
PerlIO_releaseFILE(PerlIO *p, FILE *f)
{
+ PerlIOl *l;
+ while ((l = *p)) {
+ if (l->tab == &PerlIO_stdio) {
+ PerlIOStdio *s = PerlIOSelf(&l, PerlIOStdio);
+ if (s->stdio == f) {
+ dTHX;
+ PerlIO_pop(aTHX_ p);
+ return;
+ }
+ }
+ p = PerlIONext(p);
+ }
+ return;
}
/*--------------------------------------------------------------------------------------*/
*/
IV
-PerlIOBuf_pushed(PerlIO *f, const char *mode, SV *arg)
+PerlIOBuf_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
{
- dSYS;
PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
int fd = PerlIO_fileno(f);
Off_t posn;
if (posn != (Off_t) - 1) {
b->posn = posn;
}
- return PerlIOBase_pushed(f, mode, arg);
+ return PerlIOBase_pushed(aTHX_ f, mode, arg);
}
PerlIO *
IV n, const char *mode, int fd, int imode, int perm,
PerlIO *f, int narg, SV **args)
{
- if (f) {
+ 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,
+ 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) (f, mode, PerlIOArg) != 0) {
+ if (!next || (*PerlIOBase(f)->tab->Pushed) (aTHX_ f, mode, PerlIOArg) != 0) {
return NULL;
}
}
else {
- PerlIO_funcs *tab =
- PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIO_default_btm());
+ PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIO_default_btm());
int init = 0;
if (*mode == 'I') {
init = 1;
*/
}
f = (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
- NULL, narg, args);
+ f, narg, args);
if (f) {
- if (PerlIO_push(aTHX_ f, self, mode, PerlIOArg) == 0) {
+ if (PerlIO_push(aTHX_ f, self, mode, PerlIOArg) == 0) {
/*
* if push fails during open, open fails. close will pop us.
*/
return NULL;
} else {
fd = PerlIO_fileno(f);
-#if (O_BINARY != O_TEXT) && !defined(__BEOS__)
- /*
- * do something about failing setmode()? --jhi
- */
- PerlLIO_setmode(fd, O_BINARY);
-#endif
if (init && fd == 2) {
/*
* Initial stderr is unbuffered
*/
PerlIOBase(f)->flags |= PERLIO_F_UNBUF;
}
+#ifdef PERLIO_USING_CRLF
+# ifdef PERLIO_IS_BINMODE_FD
+ if (PERLIO_IS_BINMODE_FD(fd))
+ PerlIO_binmode(f, '<'/*not used*/, O_BINARY, Nullch);
+ else
+# endif
+ /*
+ * do something about failing setmode()? --jhi
+ */
+ PerlLIO_setmode(fd, O_BINARY);
+#endif
}
}
}
* read or write state
*/
IV
-PerlIOBuf_flush(PerlIO *f)
+PerlIOBuf_flush(pTHX_ PerlIO *f)
{
PerlIOBuf *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;
- PerlIO *n = PerlIONext(f);
while (p < b->ptr) {
SSize_t count = PerlIO_write(n, p, b->ptr - p);
if (count > 0) {
/*
* We did not consume all of it
*/
- if (PerlIO_seek(PerlIONext(f), b->posn, SEEK_SET) == 0) {
- b->posn = PerlIO_tell(PerlIONext(f));
+ if (PerlIO_seek(n, b->posn, SEEK_SET) == 0) {
+ /* Reload n as some layers may pop themselves on seek */
+ b->posn = PerlIO_tell(n = PerlIONext(f));
}
}
}
b->ptr = b->end = b->buf;
PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
- /*
- * FIXME: Is this right for read case ?
- */
- if (PerlIO_flush(PerlIONext(f)) != 0)
+ /* We check for Valid because of dubious decision to make PerlIO_flush(NULL) flush all */
+ /* FIXME: Doing downstream flush may be sub-optimal see PerlIOBuf_fill() below */
+ if (PerlIOValid(n) && PerlIO_flush(n) != 0)
code = -1;
return code;
}
IV
-PerlIOBuf_fill(PerlIO *f)
+PerlIOBuf_fill(pTHX_ PerlIO *f)
{
PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
PerlIO *n = PerlIONext(f);
SSize_t avail;
/*
- * FIXME: doing the down-stream flush is a bad idea if it causes
- * pre-read data in stdio buffer to be discarded but this is too
- * simplistic - as it skips _our_ hosekeeping and breaks tell tests.
- * if (!(PerlIOBase(f)->flags & PERLIO_F_RDBUF)) { }
+ * FIXME: doing the down-stream flush maybe sub-optimal if it causes
+ * pre-read data in stdio buffer to be discarded.
+ * However, skipping the flush also skips _our_ hosekeeping
+ * and breaks tell tests. So we do the flush.
*/
if (PerlIO_flush(f) != 0)
return -1;
if (PerlIOBase(f)->flags & PERLIO_F_TTY)
- PerlIOBase_flush_linebuf();
+ PerlIOBase_flush_linebuf(aTHX);
if (!b->buf)
- PerlIO_get_base(f); /* allocate via vtable */
+ PerlIO_get_base(f); /* allocate via vtable */
b->ptr = b->end = b->buf;
if (PerlIO_fast_gets(n)) {
/*
- * Layer below is also buffered We do _NOT_ want to call its
+ * Layer below is also buffered. We do _NOT_ want to call its
* ->Read() because that will loop till it gets what we asked for
* which may hang on a pipe etc. Instead take anything it has to
* hand, or ask it to fill _once_.
if (avail > 0) {
STDCHAR *ptr = PerlIO_get_ptr(n);
SSize_t cnt = avail;
- if (avail > b->bufsiz)
+ if (avail > (SSize_t)b->bufsiz)
avail = b->bufsiz;
Copy(ptr, b->buf, avail, STDCHAR);
PerlIO_set_ptrcnt(n, ptr + avail, cnt - avail);
}
SSize_t
-PerlIOBuf_read(PerlIO *f, void *vbuf, Size_t count)
+PerlIOBuf_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
{
PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
- if (f) {
+ if (PerlIOValid(f)) {
if (!b->ptr)
PerlIO_get_base(f);
- return PerlIOBase_read(f, vbuf, count);
+ return PerlIOBase_read(aTHX_ f, vbuf, count);
}
return 0;
}
SSize_t
-PerlIOBuf_unread(PerlIO *f, const void *vbuf, Size_t count)
+PerlIOBuf_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
{
const STDCHAR *buf = (const STDCHAR *) vbuf + count;
PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
}
SSize_t
-PerlIOBuf_write(PerlIO *f, const void *vbuf, Size_t count)
+PerlIOBuf_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
{
PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
const STDCHAR *buf = (const STDCHAR *) vbuf;
}
IV
-PerlIOBuf_seek(PerlIO *f, Off_t offset, int whence)
+PerlIOBuf_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
{
IV code;
if ((code = PerlIO_flush(f)) == 0) {
}
Off_t
-PerlIOBuf_tell(PerlIO *f)
+PerlIOBuf_tell(pTHX_ PerlIO *f)
{
PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
/*
}
IV
-PerlIOBuf_close(PerlIO *f)
+PerlIOBuf_close(pTHX_ PerlIO *f)
{
- IV code = PerlIOBase_close(f);
+ IV code = PerlIOBase_close(aTHX_ f);
PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
Safefree(b->buf);
}
STDCHAR *
-PerlIOBuf_get_ptr(PerlIO *f)
+PerlIOBuf_get_ptr(pTHX_ PerlIO *f)
{
PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
if (!b->buf)
}
SSize_t
-PerlIOBuf_get_cnt(PerlIO *f)
+PerlIOBuf_get_cnt(pTHX_ PerlIO *f)
{
PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
if (!b->buf)
}
STDCHAR *
-PerlIOBuf_get_base(PerlIO *f)
+PerlIOBuf_get_base(pTHX_ PerlIO *f)
{
PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
if (!b->buf) {
}
Size_t
-PerlIOBuf_bufsiz(PerlIO *f)
+PerlIOBuf_bufsiz(pTHX_ PerlIO *f)
{
PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
if (!b->buf)
}
void
-PerlIOBuf_set_ptrcnt(PerlIO *f, STDCHAR * ptr, SSize_t cnt)
+PerlIOBuf_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
{
PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
if (!b->buf)
PerlIO_get_base(f);
b->ptr = ptr;
if (PerlIO_get_cnt(f) != cnt || b->ptr < b->buf) {
- dTHX;
assert(PerlIO_get_cnt(f) == cnt);
assert(b->ptr >= b->buf);
}
}
PerlIO *
-PerlIOBuf_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param)
+PerlIOBuf_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
{
- return PerlIOBase_dup(aTHX_ f, o, param);
+ return PerlIOBase_dup(aTHX_ f, o, param, flags);
}
*/
IV
-PerlIOPending_fill(PerlIO *f)
+PerlIOPending_fill(pTHX_ PerlIO *f)
{
/*
* Should never happen
}
IV
-PerlIOPending_close(PerlIO *f)
+PerlIOPending_close(pTHX_ PerlIO *f)
{
/*
* A tad tricky - flush pops us, then we close new top
}
IV
-PerlIOPending_seek(PerlIO *f, Off_t offset, int whence)
+PerlIOPending_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
{
/*
* A tad tricky - flush pops us, then we seek new top
IV
-PerlIOPending_flush(PerlIO *f)
+PerlIOPending_flush(pTHX_ PerlIO *f)
{
- dTHX;
PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
Safefree(b->buf);
}
void
-PerlIOPending_set_ptrcnt(PerlIO *f, STDCHAR * ptr, SSize_t cnt)
+PerlIOPending_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
{
if (cnt <= 0) {
PerlIO_flush(f);
}
else {
- PerlIOBuf_set_ptrcnt(f, ptr, cnt);
+ PerlIOBuf_set_ptrcnt(aTHX_ f, ptr, cnt);
}
}
IV
-PerlIOPending_pushed(PerlIO *f, const char *mode, SV *arg)
+PerlIOPending_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
{
- IV code = PerlIOBase_pushed(f, mode, arg);
+ IV code = PerlIOBase_pushed(aTHX_ f, mode, arg);
PerlIOl *l = PerlIOBase(f);
/*
* Our PerlIO_fast_gets must match what we are pushed on, or sv_gets()
}
SSize_t
-PerlIOPending_read(PerlIO *f, void *vbuf, Size_t count)
+PerlIOPending_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
{
SSize_t avail = PerlIO_get_cnt(f);
SSize_t got = 0;
- if (count < avail)
+ if ((SSize_t)count < avail)
avail = count;
if (avail > 0)
- got = PerlIOBuf_read(f, vbuf, avail);
- if (got >= 0 && got < count) {
+ got = PerlIOBuf_read(aTHX_ f, vbuf, avail);
+ if (got >= 0 && got < (SSize_t)count) {
SSize_t more =
PerlIO_read(f, ((STDCHAR *) vbuf) + got, count - got);
if (more >= 0 || got == 0)
*/
typedef struct {
- PerlIOBuf base; /* PerlIOBuf stuff */
- STDCHAR *nl; /* Position of crlf we "lied" about in the
+ PerlIOBuf base; /* PerlIOBuf stuff */
+ STDCHAR *nl; /* Position of crlf we "lied" about in the
* buffer */
} PerlIOCrlf;
IV
-PerlIOCrlf_pushed(PerlIO *f, const char *mode, SV *arg)
+PerlIOCrlf_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
{
IV code;
PerlIOBase(f)->flags |= PERLIO_F_CRLF;
- code = PerlIOBuf_pushed(f, mode, arg);
+ code = PerlIOBuf_pushed(aTHX_ f, mode, arg);
#if 0
PerlIO_debug("PerlIOCrlf_pushed f=%p %s %s fl=%08" UVxf "\n",
f, PerlIOBase(f)->tab->name, (mode) ? mode : "(Null)",
SSize_t
-PerlIOCrlf_unread(PerlIO *f, const void *vbuf, Size_t count)
+PerlIOCrlf_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
{
PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
if (c->nl) {
c->nl = NULL;
}
if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF))
- return PerlIOBuf_unread(f, vbuf, count);
+ return PerlIOBuf_unread(aTHX_ f, vbuf, count);
else {
const STDCHAR *buf = (const STDCHAR *) vbuf + count;
PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
}
SSize_t
-PerlIOCrlf_get_cnt(PerlIO *f)
+PerlIOCrlf_get_cnt(pTHX_ PerlIO *f)
{
PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
if (!b->buf)
PerlIO_get_base(f);
if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
- if ((PerlIOBase(f)->flags & PERLIO_F_CRLF) && !c->nl) {
- STDCHAR *nl = b->ptr;
+ if ((PerlIOBase(f)->flags & PERLIO_F_CRLF) && (!c->nl || *c->nl == 0xd)) {
+ STDCHAR *nl = (c->nl) ? c->nl : b->ptr;
scan:
while (nl < b->end && *nl != 0xd)
nl++;
/*
* Blast - found CR as last char in buffer
*/
+
if (b->ptr < nl) {
/*
* They may not care, defer work as long as
* possible
*/
+ c->nl = nl;
return (nl - b->ptr);
}
else {
int code;
- b->ptr++; /* say we have read it as far as
+ b->ptr++; /* say we have read it as far as
* flush() is concerned */
- b->buf++; /* Leave space an front of buffer */
- 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
+ b->buf++; /* Leave space in front of buffer */
+ 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 */
+ b->posn--; /* Buffer starts here */
+ *nl = 0xd; /* Fill in the CR */
if (code == 0)
- goto test; /* fill() call worked */
+ goto test; /* fill() call worked */
/*
* CR at EOF - just fall through
*/
+ /* Should we clear EOF though ??? */
}
}
}
}
void
-PerlIOCrlf_set_ptrcnt(PerlIO *f, STDCHAR * ptr, SSize_t cnt)
+PerlIOCrlf_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
{
PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
- IV flags = PerlIOBase(f)->flags;
if (!b->buf)
PerlIO_get_base(f);
if (!ptr) {
- if (c->nl)
+ if (c->nl) {
ptr = c->nl + 1;
+ if (ptr == b->end && *c->nl == 0xd) {
+ /* Defered CR at end of buffer case - we lied about count */
+ ptr--;
+ }
+ }
else {
ptr = b->end;
- if ((flags & PERLIO_F_CRLF) && ptr > b->buf && ptr[-1] == 0xd)
- ptr--;
}
ptr -= cnt;
}
else {
+#if 0
/*
* Test code - delete when it works ...
*/
- STDCHAR *chk;
- if (c->nl)
- chk = c->nl + 1;
- else {
- chk = b->end;
- if ((flags & PERLIO_F_CRLF) && chk > b->buf && chk[-1] == 0xd)
- chk--;
+ IV flags = PerlIOBase(f)->flags;
+ STDCHAR *chk = (c->nl) ? (c->nl+1) : b->end;
+ if (ptr+cnt == c->nl && c->nl+1 == b->end && *c->nl == 0xd) {
+ /* Defered CR at end of buffer case - we lied about count */
+ chk--;
}
chk -= cnt;
- if (ptr != chk) {
- dTHX;
+ if (ptr != chk ) {
Perl_croak(aTHX_ "ptr wrong %p != %p fl=%08" UVxf
" nl=%p e=%p for %d", ptr, chk, flags, c->nl,
b->end, cnt);
}
+#endif
}
if (c->nl) {
if (ptr > c->nl) {
}
SSize_t
-PerlIOCrlf_write(PerlIO *f, const void *vbuf, Size_t count)
+PerlIOCrlf_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
{
if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF))
- return PerlIOBuf_write(f, vbuf, count);
+ return PerlIOBuf_write(aTHX_ f, vbuf, count);
else {
PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
const STDCHAR *buf = (const STDCHAR *) vbuf;
break;
}
else {
- *(b->ptr)++ = 0xd; /* CR */
- *(b->ptr)++ = 0xa; /* LF */
+ *(b->ptr)++ = 0xd; /* CR */
+ *(b->ptr)++ = 0xa; /* LF */
buf++;
if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF) {
PerlIO_flush(f);
}
IV
-PerlIOCrlf_flush(PerlIO *f)
+PerlIOCrlf_flush(pTHX_ PerlIO *f)
{
PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
if (c->nl) {
*(c->nl) = 0xd;
c->nl = NULL;
}
- return PerlIOBuf_flush(f);
+ return PerlIOBuf_flush(aTHX_ f);
}
PerlIO_funcs PerlIO_crlf = {
sizeof(PerlIOCrlf),
PERLIO_K_BUFFERED | PERLIO_K_CANCRLF,
PerlIOCrlf_pushed,
- PerlIOBase_noop_ok, /* popped */
+ PerlIOBase_noop_ok, /* popped */
PerlIOBuf_open,
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' */
+ PerlIOCrlf_unread, /* Put CR,LF in buffer for each '\n' */
+ PerlIOCrlf_write, /* Put CR,LF in buffer for each '\n' */
PerlIOBuf_seek,
PerlIOBuf_tell,
PerlIOBuf_close,
*/
typedef struct {
- PerlIOBuf base; /* PerlIOBuf stuff */
- Mmap_t mptr; /* Mapped address */
- Size_t len; /* mapped length */
- STDCHAR *bbuf; /* malloced buffer if map fails */
+ PerlIOBuf base; /* PerlIOBuf stuff */
+ Mmap_t mptr; /* Mapped address */
+ Size_t len; /* mapped length */
+ STDCHAR *bbuf; /* malloced buffer if map fails */
} PerlIOMmap;
static size_t page_size = 0;
IV
-PerlIOMmap_map(PerlIO *f)
+PerlIOMmap_map(pTHX_ PerlIO *f)
{
- dTHX;
PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
IV flags = PerlIOBase(f)->flags;
IV code = 0;
page_size = getpagesize();
# else
# if defined(I_SYS_PARAM) && defined(PAGESIZE)
- page_size = PAGESIZE; /* compiletime, bad */
+ page_size = PAGESIZE; /* compiletime, bad */
# endif
# endif
#endif
}
IV
-PerlIOMmap_unmap(PerlIO *f)
+PerlIOMmap_unmap(pTHX_ PerlIO *f)
{
PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
PerlIOBuf *b = &m->base;
}
STDCHAR *
-PerlIOMmap_get_base(PerlIO *f)
+PerlIOMmap_get_base(pTHX_ PerlIO *f)
{
PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
PerlIOBuf *b = &m->base;
/*
* We have a write buffer or flushed PerlIOBuf read buffer
*/
- m->bbuf = b->buf; /* save it in case we need it again */
- b->buf = NULL; /* Clear to trigger below */
+ m->bbuf = b->buf; /* save it in case we need it again */
+ b->buf = NULL; /* Clear to trigger below */
}
if (!b->buf) {
- PerlIOMmap_map(f); /* Try and map it */
+ PerlIOMmap_map(aTHX_ f); /* Try and map it */
if (!b->buf) {
/*
* Map did not work - recover PerlIOBuf buffer if we have one
b->ptr = b->end = b->buf;
if (b->buf)
return b->buf;
- return PerlIOBuf_get_base(f);
+ return PerlIOBuf_get_base(aTHX_ f);
}
SSize_t
-PerlIOMmap_unread(PerlIO *f, const void *vbuf, Size_t count)
+PerlIOMmap_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
{
PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
PerlIOBuf *b = &m->base;
if (!b->buf && m->bbuf)
b->buf = m->bbuf;
if (!b->buf) {
- PerlIOBuf_get_base(f);
+ PerlIOBuf_get_base(aTHX_ f);
m->bbuf = b->buf;
}
}
- return PerlIOBuf_unread(f, vbuf, count);
+ return PerlIOBuf_unread(aTHX_ f, vbuf, count);
}
SSize_t
-PerlIOMmap_write(PerlIO *f, const void *vbuf, Size_t count)
+PerlIOMmap_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
{
PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
PerlIOBuf *b = &m->base;
* No, or wrong sort of, buffer
*/
if (m->len) {
- if (PerlIOMmap_unmap(f) != 0)
+ if (PerlIOMmap_unmap(aTHX_ f) != 0)
return 0;
}
/*
if (!b->buf && m->bbuf)
b->buf = m->bbuf;
if (!b->buf) {
- PerlIOBuf_get_base(f);
+ PerlIOBuf_get_base(aTHX_ f);
m->bbuf = b->buf;
}
}
- return PerlIOBuf_write(f, vbuf, count);
+ return PerlIOBuf_write(aTHX_ f, vbuf, count);
}
IV
-PerlIOMmap_flush(PerlIO *f)
+PerlIOMmap_flush(pTHX_ PerlIO *f)
{
PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
PerlIOBuf *b = &m->base;
- IV code = PerlIOBuf_flush(f);
+ IV code = PerlIOBuf_flush(aTHX_ f);
/*
* Now we are "synced" at PerlIOBuf level
*/
/*
* Unmap the buffer
*/
- if (PerlIOMmap_unmap(f) != 0)
+ if (PerlIOMmap_unmap(aTHX_ f) != 0)
code = -1;
}
else {
}
IV
-PerlIOMmap_fill(PerlIO *f)
+PerlIOMmap_fill(pTHX_ PerlIO *f)
{
PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
IV code = PerlIO_flush(f);
if (code == 0 && !b->buf) {
- code = PerlIOMmap_map(f);
+ code = PerlIOMmap_map(aTHX_ f);
}
if (code == 0 && !(PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
- code = PerlIOBuf_fill(f);
+ code = PerlIOBuf_fill(aTHX_ f);
}
return code;
}
IV
-PerlIOMmap_close(PerlIO *f)
+PerlIOMmap_close(pTHX_ PerlIO *f)
{
PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
PerlIOBuf *b = &m->base;
m->bbuf = NULL;
b->ptr = b->end = b->buf;
}
- if (PerlIOBuf_close(f) != 0)
+ if (PerlIOBuf_close(aTHX_ f) != 0)
code = -1;
return code;
}
PerlIO *
-PerlIOMmap_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param)
+PerlIOMmap_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
{
- return PerlIOBase_dup(aTHX_ f, o, param);
+ return PerlIOBase_dup(aTHX_ f, o, param, flags);
}
PerlIOBuf_set_ptrcnt,
};
-#endif /* HAS_MMAP */
+#endif /* HAS_MMAP */
-#undef PerlIO_stdin
PerlIO *
-PerlIO_stdin(void)
+Perl_PerlIO_stdin(pTHX)
{
- dTHX;
if (!PL_perlio) {
PerlIO_stdstreams(aTHX);
}
return &PL_perlio[1];
}
-#undef PerlIO_stdout
PerlIO *
-PerlIO_stdout(void)
+Perl_PerlIO_stdout(pTHX)
{
- dTHX;
if (!PL_perlio) {
PerlIO_stdstreams(aTHX);
}
return &PL_perlio[2];
}
-#undef PerlIO_stderr
PerlIO *
-PerlIO_stderr(void)
+Perl_PerlIO_stderr(pTHX)
{
- dTHX;
if (!PL_perlio) {
PerlIO_stdstreams(aTHX);
}
/*--------------------------------------------------------------------------------------*/
-#undef PerlIO_getname
char *
PerlIO_getname(PerlIO *f, char *buf)
{
* terms of above
*/
+#undef PerlIO_fdopen
+PerlIO *
+PerlIO_fdopen(int fd, const char *mode)
+{
+ dTHX;
+ return PerlIO_openn(aTHX_ Nullch, mode, fd, 0, 0, NULL, 0, NULL);
+}
+
+#undef PerlIO_open
+PerlIO *
+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);
+}
+
+#undef Perlio_reopen
+PerlIO *
+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);
+}
+
#undef PerlIO_getc
int
PerlIO_getc(PerlIO *f)
{
+ dTHX;
STDCHAR buf[1];
SSize_t count = PerlIO_read(f, buf, 1);
if (count == 1) {
int
PerlIO_ungetc(PerlIO *f, int ch)
{
+ dTHX;
if (ch != EOF) {
STDCHAR buf = ch;
if (PerlIO_unread(f, &buf, 1) == 1)
int
PerlIO_putc(PerlIO *f, int ch)
{
+ dTHX;
STDCHAR buf = ch;
return PerlIO_write(f, &buf, 1);
}
int
PerlIO_puts(PerlIO *f, const char *s)
{
+ dTHX;
STRLEN len = strlen(s);
return PerlIO_write(f, s, len);
}
void
PerlIO_rewind(PerlIO *f)
{
+ dTHX;
PerlIO_seek(f, (Off_t) 0, SEEK_SET);
PerlIO_clearerr(f);
}
int
PerlIO_stdoutf(const char *fmt, ...)
{
+ dTHX;
va_list ap;
int result;
va_start(ap, fmt);
#undef HAS_FSETPOS
#undef HAS_FGETPOS
-#endif /* USE_SFIO */
-#endif /* PERLIO_IS_STDIO */
+#endif /* USE_SFIO */
+#endif /* PERLIO_IS_STDIO */
/*======================================================================================*/
/*
vprintf(char *pat, char *args)
{
_doprnt(pat, args, stdout);
- return 0; /* wrong, but perl doesn't use the return
+ return 0; /* wrong, but perl doesn't use the return
* value */
}
vfprintf(FILE *fd, char *pat, char *args)
{
_doprnt(pat, args, fd);
- return 0; /* wrong, but perl doesn't use the return
+ return 0; /* wrong, but perl doesn't use the return
* value */
}