/*
- * perlio.c Copyright (c) 1996-2001, Nick Ing-Simmons You may distribute
+ * perlio.c Copyright (c) 1996-2001, 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.
+ * Artistic License, as specified in the README file.
*/
/*
- * If we have ActivePerl-like PERL_IMPLICIT_SYS then we need a dTHX to get
+ * 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
+ * Invent a dSYS macro to abstract this out
*/
#ifdef PERL_IMPLICIT_SYS
#define dSYS dTHX
#define PERLIO_NOT_STDIO 0
#if !defined(PERLIO_IS_STDIO) && !defined(USE_SFIO)
/*
- * #define PerlIO FILE
+ * #define PerlIO FILE
*/
#endif
/*
#define PERL_IN_PERLIO_C
#include "perl.h"
-#include "XSUB.h"
+#ifdef PERL_IMPLICIT_CONTEXT
+#undef dSYS
+#define dSYS dTHX
+#endif
-#undef PerlMemShared_calloc
-#define PerlMemShared_calloc(x,y) calloc(x,y)
-#undef PerlMemShared_free
-#define PerlMemShared_free(x) free(x)
+#include "XSUB.h"
int
perlsio_binmode(FILE *fp, int iotype, int mode)
{
/*
- * This used to be contents of do_binmode in doio.c
+ * This used to be contents of do_binmode in doio.c
*/
#ifdef DOSISH
# if defined(atarist) || defined(__MINT__)
#endif
# if defined(WIN32) && defined(__BORLANDC__)
/*
- * The translation mode of the stream is maintained independent of
+ * The translation mode of the stream is maintained independent of
* the translation mode of the fd in the Borland RTL (heavy
- * digging through their runtime sources reveal). User has to set
+ * digging through their runtime sources reveal). User has to set
* the mode explicitly for the stream (though they don't document
- * this anywhere). GSAR 97-5-24
+ * this anywhere). GSAR 97-5-24
*/
fseek(fp, 0L, 0);
if (mode & O_BINARY)
#endif
}
+#ifndef O_ACCMODE
+#define O_ACCMODE 3 /* Assume traditional implementation */
+#endif
+
+int
+PerlIO_intmode2str(int rawmode, char *mode, int *writing)
+{
+ int result = rawmode & O_ACCMODE;
+ int ix = 0;
+ int ptype;
+ switch (result) {
+ case O_RDONLY:
+ ptype = IoTYPE_RDONLY;
+ break;
+ case O_WRONLY:
+ ptype = IoTYPE_WRONLY;
+ break;
+ case O_RDWR:
+ default:
+ ptype = IoTYPE_RDWR;
+ break;
+ }
+ if (writing)
+ *writing = (result != O_RDONLY);
+
+ if (result == O_RDONLY) {
+ mode[ix++] = 'r';
+ }
+#ifdef O_APPEND
+ else if (rawmode & O_APPEND) {
+ mode[ix++] = 'a';
+ if (result != O_WRONLY)
+ mode[ix++] = '+';
+ }
+#endif
+ else {
+ if (result == O_WRONLY)
+ mode[ix++] = 'w';
+ else {
+ mode[ix++] = 'r';
+ mode[ix++] = '+';
+ }
+ }
+ if (rawmode & O_BINARY)
+ mode[ix++] = 'b';
+ mode[ix] = '\0';
+ return ptype;
+}
+
#ifndef PERLIO_LAYERS
int
PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names)
}
Perl_croak(aTHX_ "Cannot apply \"%s\" in non-PerlIO perl", names);
/*
- * NOTREACHED
+ * NOTREACHED
*/
return -1;
}
#endif
}
+PerlIO *
+PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
+{
+#ifndef PERL_MICRO
+ if (f) {
+ int fd = PerlLIO_dup(PerlIO_fileno(f));
+ if (fd >= 0) {
+ char mode[8];
+ int omode = fcntl(fd, F_GETFL);
+ PerlIO_intmode2str(omode,mode,NULL);
+ /* the r+ is a hack */
+ return PerlIO_fdopen(fd, mode);
+ }
+ return NULL;
+ }
+ else {
+ SETERRNO(EBADF, SS$_IVCHAN);
+ }
+#endif
+ return NULL;
+}
+
+
/*
- * De-mux PerlIO_openn() into fdopen, freopen and fopen type entries
+ * De-mux PerlIO_openn() into fdopen, freopen and fopen type entries
*/
PerlIO *
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 {
#ifdef PERLIO_IS_STDIO
void
-PerlIO_init(void)
+PerlIO_init(pTHX)
{
/*
* Does nothing (yet) except force this file to be included in perl
- * binary. That allows this file to force inclusion of other functions
+ * binary. That allows this file to force inclusion of other functions
* that may be required by loadable extensions e.g. for
- * FileHandle::tmpfile
+ * FileHandle::tmpfile
*/
}
/*
* This section is just to make sure these functions get pulled in from
- * libsfio.a
+ * libsfio.a
*/
#undef PerlIO_tmpfile
}
void
-PerlIO_init(void)
+PerlIO_init(pTHX)
{
/*
* Force this file to be included in perl binary. Which allows this
* file to force inclusion of other functions that may be required by
- * loadable extensions e.g. for FileHandle::tmpfile
+ * loadable extensions e.g. for FileHandle::tmpfile
*/
/*
- * Hack sfio does its own 'autoflush' on stdout in common cases. Flush
+ * Hack sfio does its own 'autoflush' on stdout in common cases. Flush
* results in a lot of lseek()s to regular files and lot of small
- * writes to pipes.
+ * writes to pipes.
*/
sfset(sfstdout, SF_SHARE, 0);
}
#else /* USE_SFIO */
/*======================================================================================*/
/*
- * Implement all the PerlIO interface ourselves.
+ * Implement all the PerlIO interface ourselves.
*/
#include "perliol.h"
/*
* We _MUST_ have <unistd.h> if we are using lseek() and may have large
- * files
+ * files
*/
#ifdef I_UNISTD
#include <unistd.h>
}
if (dbg > 0) {
dTHX;
+#ifdef USE_ITHREADS
+ /* Use fixed buffer as sv_catpvf etc. needs SVs */
+ char buffer[1024];
+ char *s;
+ STRLEN len;
+ s = CopFILE(PL_curcop);
+ if (!s)
+ s = "(none)";
+ sprintf(buffer, "%s:%" IVdf " ", s, (IV) CopLINE(PL_curcop));
+ len = strlen(buffer);
+ vsprintf(buffer+len, fmt, ap);
+ PerlLIO_write(dbg, buffer, strlen(buffer));
+#else
SV *sv = newSVpvn("", 0);
char *s;
STRLEN len;
s = SvPV(sv, len);
PerlLIO_write(dbg, s, len);
SvREFCNT_dec(sv);
+#endif
}
va_end(ap);
}
/*--------------------------------------------------------------------------------------*/
/*
- * Inner level routines
+ * Inner level routines
*/
/*
- * Table of pointers to the PerlIO structs (malloc'ed)
+ * Table of pointers to the PerlIO structs (malloc'ed)
*/
-PerlIO *_perlio = NULL;
#define PERLIO_TABLE_SIZE 64
-
-
PerlIO *
PerlIO_allocate(pTHX)
{
/*
- * Find a free slot in the table, allocating new table as necessary
+ * Find a free slot in the table, allocating new table as necessary
*/
PerlIO **last;
PerlIO *f;
- last = &_perlio;
+ last = &PL_perlio;
while ((f = *last)) {
int i;
last = (PerlIO **) (f);
}
}
}
- f = PerlMemShared_calloc(PERLIO_TABLE_SIZE, sizeof(PerlIO));
+ Newz('I',f,PERLIO_TABLE_SIZE,PerlIO);
if (!f) {
return NULL;
}
return f + 1;
}
+#undef PerlIO_fdupopen
+PerlIO *
+PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
+{
+ if (f && *f) {
+ PerlIO_funcs *tab = PerlIOBase(f)->tab;
+ PerlIO *new;
+ 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 {
+ SETERRNO(EBADF, SS$_IVCHAN);
+ return NULL;
+ }
+}
+
void
PerlIO_cleantable(pTHX_ PerlIO **tablep)
{
PerlIO_close(f);
}
}
- PerlMemShared_free(table);
+ Safefree(table);
*tablep = NULL;
}
}
-PerlIO_list_t *PerlIO_known_layers;
-PerlIO_list_t *PerlIO_def_layerlist;
PerlIO_list_t *
-PerlIO_list_alloc(void)
+PerlIO_list_alloc(pTHX)
{
PerlIO_list_t *list;
Newz('L', list, 1, PerlIO_list_t);
}
void
-PerlIO_list_free(PerlIO_list_t *list)
+PerlIO_list_free(pTHX_ PerlIO_list_t *list)
{
if (list) {
if (--list->refcnt == 0) {
if (list->array) {
- dTHX;
IV i;
for (i = 0; i < list->cur; i++) {
if (list->array[i].arg)
}
void
-PerlIO_list_push(PerlIO_list_t *list, PerlIO_funcs *funcs, SV *arg)
+PerlIO_list_push(pTHX_ PerlIO_list_t *list, PerlIO_funcs *funcs, SV *arg)
{
- dTHX;
PerlIO_pair_t *p;
if (list->cur >= list->len) {
list->len += 8;
}
}
-
-void
-PerlIO_cleanup_layers(pTHX_ void *data)
+PerlIO_list_t *
+PerlIO_clone_list(pTHX_ PerlIO_list_t *proto, CLONE_PARAMS *param)
{
-#if 0
- PerlIO_known_layers = Nullhv;
- PerlIO_def_layerlist = Nullav;
-#endif
+ PerlIO_list_t *list = (PerlIO_list_t *) NULL;
+ if (proto) {
+ int i;
+ list = PerlIO_list_alloc(aTHX);
+ for (i=0; i < proto->cur; i++) {
+ SV *arg = Nullsv;
+ if (proto->array[i].arg)
+ arg = PerlIO_sv_dup(aTHX_ proto->array[i].arg,param);
+ PerlIO_list_push(aTHX_ list, proto->array[i].funcs, arg);
+ }
+ }
+ return list;
}
void
-PerlIO_cleanup()
+PerlIO_clone(pTHX_ PerlInterpreter *proto, CLONE_PARAMS *param)
{
- dTHX;
- PerlIO_cleantable(aTHX_ & _perlio);
+#ifdef USE_ITHREADS
+ PerlIO **table = &proto->Iperlio;
+ PerlIO *f;
+ PL_perlio = NULL;
+ PL_known_layers = PerlIO_clone_list(aTHX_ proto->Iknown_layers, param);
+ PL_def_layerlist = PerlIO_clone_list(aTHX_ proto->Idef_layerlist, param);
+ PerlIO_allocate(aTHX); /* root slot is never used */
+ PerlIO_debug("Clone %p from %p\n",aTHX,proto);
+ while ((f = *table)) {
+ int i;
+ table = (PerlIO **) (f++);
+ for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
+ if (*f) {
+ (void) fp_dup(f, 0, param);
+ }
+ f++;
+ }
+ }
+#endif
}
void
PerlIO_destruct(pTHX)
{
- PerlIO **table = &_perlio;
+ PerlIO **table = &PL_perlio;
PerlIO *f;
+#ifdef USE_ITHREADS
+ PerlIO_debug("Destruct %p\n",aTHX);
+#endif
while ((f = *table)) {
int i;
table = (PerlIO **) (f++);
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
+ * use
*/
if ((*l->tab->Popped) (f) != 0)
return;
}
*f = l->next;;
- PerlMemShared_free(l);
+ Safefree(l);
}
}
/*--------------------------------------------------------------------------------------*/
/*
- * XS Interface for perl code
+ * XS Interface for perl code
*/
PerlIO_funcs *
IV i;
if ((SSize_t) len <= 0)
len = strlen(name);
- for (i = 0; i < PerlIO_known_layers->cur; i++) {
- PerlIO_funcs *f = PerlIO_known_layers->array[i].funcs;
+ 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;
}
}
- if (load && PL_subname && PerlIO_def_layerlist
- && PerlIO_def_layerlist->cur >= 2) {
+ 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
+ * The two SVs are magically freed by load_module
*/
Perl_load_module(aTHX_ 0, pkgsv, Nullsv, layer, Nullsv);
LEAVE;
void
PerlIO_define_layer(pTHX_ PerlIO_funcs *tab)
{
- if (!PerlIO_known_layers)
- PerlIO_known_layers = PerlIO_list_alloc();
- PerlIO_list_push(PerlIO_known_layers, tab, Nullsv);
- PerlIO_debug("define %s %p\n", tab->name, 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, (void*)tab);
}
int
/*
* Message is consistent with how attribute lists are
* passed. Even though this means "foo : : bar" is
- * seen as an invalid separator character.
+ * seen as an invalid separator character.
*/
char q = ((*s == '\'') ? '"' : '\'');
Perl_warn(aTHX_
- "perlio: invalid separator character %c%c%c in layer specification list",
- q, *s, q);
+ "perlio: invalid separator character %c%c%c in layer specification list %s",
+ q, *s, q, s);
return -1;
}
do {
/*
* It's a nul terminated string, not allowed
* to \ the terminating null. Anything other
- * character is passed over.
+ * character is passed over.
*/
if (*e++) {
break;
}
/*
- * Drop through
+ * Drop through
*/
case '\0':
e--;
return -1;
default:
/*
- * boring.
+ * boring.
*/
break;
}
PerlIO_funcs *layer =
PerlIO_find_layer(aTHX_ s, llen, 1);
if (layer) {
- PerlIO_list_push(av, layer,
+ PerlIO_list_push(aTHX_ av, layer,
(as) ? newSVpvn(as,
alen) :
&PL_sv_undef);
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(av, PerlIO_find_layer(aTHX_ tab->name, 0, 0),
+ PerlIO_list_push(aTHX_ av, PerlIO_find_layer(aTHX_ tab->name, 0, 0),
&PL_sv_undef);
}
PerlIO_list_t *
PerlIO_default_layers(pTHX)
{
- if (!PerlIO_def_layerlist) {
+ if (!PL_def_layerlist) {
const char *s = (PL_tainting) ? Nullch : PerlEnv_getenv("PERLIO");
PerlIO_funcs *osLayer = &PerlIO_unix;
- PerlIO_def_layerlist = PerlIO_list_alloc();
+ PL_def_layerlist = PerlIO_list_alloc(aTHX);
PerlIO_define_layer(aTHX_ & PerlIO_unix);
#if defined(WIN32) && !defined(UNDER_CE)
PerlIO_define_layer(aTHX_ & PerlIO_win32);
#endif
PerlIO_define_layer(aTHX_ & PerlIO_utf8);
PerlIO_define_layer(aTHX_ & PerlIO_byte);
- PerlIO_list_push(PerlIO_def_layerlist,
+ PerlIO_list_push(aTHX_ PL_def_layerlist,
PerlIO_find_layer(aTHX_ osLayer->name, 0, 0),
&PL_sv_undef);
if (s) {
- PerlIO_parse_layers(aTHX_ PerlIO_def_layerlist, s);
+ PerlIO_parse_layers(aTHX_ PL_def_layerlist, s);
}
else {
- PerlIO_default_buffer(aTHX_ PerlIO_def_layerlist);
+ PerlIO_default_buffer(aTHX_ PL_def_layerlist);
}
}
- if (PerlIO_def_layerlist->cur < 2) {
- PerlIO_default_buffer(aTHX_ PerlIO_def_layerlist);
+ if (PL_def_layerlist->cur < 2) {
+ PerlIO_default_buffer(aTHX_ PL_def_layerlist);
}
- return PerlIO_def_layerlist;
+ return PL_def_layerlist;
}
void
void
PerlIO_stdstreams(pTHX)
{
- if (!_perlio) {
+ if (!PL_perlio) {
PerlIO_allocate(aTHX);
PerlIO_fdopen(0, "Ir" PERLIO_STDTEXT);
PerlIO_fdopen(1, "Iw" PERLIO_STDTEXT);
PerlIO_push(pTHX_ PerlIO *f, PerlIO_funcs *tab, const char *mode, SV *arg)
{
PerlIOl *l = NULL;
- l = PerlMemShared_calloc(tab->size, sizeof(char));
- if (l) {
+ 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", f, tab->name,
- (mode) ? mode : "(Null)", arg);
+ PerlIO_debug("PerlIO_push f=%p %s %s %p\n", (void*)f, tab->name,
+ (mode) ? mode : "(Null)", (void*)arg);
if ((*l->tab->Pushed) (f, mode, arg) != 0) {
PerlIO_pop(aTHX_ f);
return NULL;
PerlIORaw_pushed(PerlIO *f, const char *mode, SV *arg)
{
/*
- * Remove the dummy layer
+ * Remove the dummy layer
*/
dTHX;
PerlIO_pop(aTHX_ f);
/*
- * Pop back to bottom layer
+ * Pop back to bottom layer
*/
if (f && *f) {
PerlIO_flush(f);
}
else {
/*
- * Nothing bellow - push unix on top then remove it
+ * Nothing bellow - push unix on top then remove it
*/
if (PerlIO_push(aTHX_ f, PerlIO_default_btm(), mode, arg)) {
PerlIO_pop(aTHX_ 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;
PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names)
{
int code = 0;
- if (names) {
- PerlIO_list_t *layers = PerlIO_list_alloc();
+ 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);
}
- PerlIO_list_free(layers);
+ PerlIO_list_free(aTHX_ layers);
}
return code;
}
/*--------------------------------------------------------------------------------------*/
/*
- * Given the abstraction above the public API functions
+ * Given the abstraction above the public API functions
*/
int
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;
+ /* Can't flush if switching encodings. */
+ if (!(names && memEQ(names, ":encoding(", 10))) {
+ PerlIO_flush(f);
+#ifdef PERLIO_USING_CRLF
+ if (!names && (mode & O_BINARY)) {
+ PerlIO *top = f;
+ while (*top) {
+ if (PerlIOBase(top)->tab == &PerlIO_crlf) {
+ PerlIOBase(top)->flags &= ~PERLIO_F_CRLF;
+ break;
+ }
+ top = PerlIONext(top);
+ PerlIO_flush(top);
}
- top = PerlIONext(top);
- PerlIO_flush(top);
}
+#endif
}
return PerlIO_apply_layers(aTHX_ f, NULL, names) == 0 ? TRUE : FALSE;
}
}
}
-#undef PerlIO_fdupopen
-PerlIO *
-PerlIO_fdupopen(pTHX_ PerlIO *f)
-{
- if (f && *f) {
- char buf[8];
- int fd = PerlLIO_dup(PerlIO_fileno(f));
- PerlIO *new = PerlIO_fdopen(fd, PerlIO_modestr(f, buf));
- if (new) {
- Off_t posn = PerlIO_tell(f);
- PerlIO_seek(new, posn, SEEK_SET);
- }
- return new;
- }
- else {
- SETERRNO(EBADF, SS$_IVCHAN);
- return NULL;
- }
-}
-
#undef PerlIO_close
int
PerlIO_close(PerlIO *f)
{
const char *type = NULL;
/*
- * Need to supply default layer info from open.pm
+ * Need to supply default layer info from open.pm
*/
if (PL_curcop) {
SV *layers = PL_curcop->cop_io;
type = SvPV(layers, len);
if (type && mode[0] != 'r') {
/*
- * Skip to write part
+ * Skip to write part
*/
const char *s = strchr(type, 0);
if (s && (s - type) < len) {
PerlIO_layer_from_ref(pTHX_ SV *sv)
{
/*
- * For any scalar type load the handler which is bundled with perl
+ * 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);
/*
- * For other types allow if layer is known but don't try and load it
+ * For other types allow if layer is known but don't try and load it
*/
switch (SvTYPE(sv)) {
case SVt_PVAV:
{
PerlIO_list_t *def = PerlIO_default_layers(aTHX);
int incdef = 1;
- if (!_perlio)
+ if (!PL_perlio)
PerlIO_stdstreams(aTHX);
if (narg) {
SV *arg = *args;
/*
- * If it is a reference but not an object see if we have a handler
- * for it
+ * If it is a reference but not an object see if we have a handler
+ * for it
*/
if (SvROK(arg) && !sv_isobject(arg)) {
PerlIO_funcs *handler = PerlIO_layer_from_ref(aTHX_ SvRV(arg));
if (handler) {
- def = PerlIO_list_alloc();
- PerlIO_list_push(def, handler, &PL_sv_undef);
+ def = PerlIO_list_alloc(aTHX);
+ PerlIO_list_push(aTHX_ def, handler, &PL_sv_undef);
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.
+ * resulting string.
*/
}
}
PerlIO_list_t *av;
if (incdef) {
IV i = def->cur;
- av = PerlIO_list_alloc();
+ av = PerlIO_list_alloc(aTHX);
for (i = 0; i < def->cur; i++) {
- PerlIO_list_push(av, def->array[i].funcs,
+ PerlIO_list_push(aTHX_ av, def->array[i].funcs,
def->array[i].arg);
}
}
PerlIO_funcs *tab = NULL;
if (f && *f) {
/*
- * This is "reopen" - it is not tested as perl does not use it
- * yet
+ * This is "reopen" - it is not tested as perl does not use it
+ * yet
*/
PerlIOl *l = *f;
- layera = PerlIO_list_alloc();
+ layera = PerlIO_list_alloc(aTHX);
while (l) {
SV *arg =
(l->tab->Getarg) ? (*l->tab->
- Getarg) (&l) : &PL_sv_undef;
- PerlIO_list_push(layera, l->tab, arg);
+ Getarg) (aTHX_ &l, NULL, 0) : &PL_sv_undef;
+ PerlIO_list_push(aTHX_ layera, l->tab, arg);
l = *PerlIONext(&l);
}
}
layera = PerlIO_resolve_layers(aTHX_ layers, mode, narg, args);
}
/*
- * Start at "top" of layer stack
+ * Start at "top" of layer stack
*/
n = layera->cur - 1;
while (n >= 0) {
}
if (tab) {
/*
- * Found that layer 'n' can do opens - call it
+ * 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) {
if (n + 1 < layera->cur) {
/*
* More layers above the one that we used to open -
- * apply them now
+ * apply them now
*/
if (PerlIO_apply_layera(aTHX_ f, mode, layera, n + 1)
!= 0) {
}
}
}
- PerlIO_list_free(layera);
+ PerlIO_list_free(aTHX_ layera);
}
return f;
}
return (*tab->Flush) (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;
}
* errorneous input? Maybe some magical value (PerlIO*
* PERLIO_FLUSH_ALL = (PerlIO*)-1;)? Yes, stdio does similar
* things on fflush(NULL), but should we be bound by their design
- * decisions? --jhi
+ * decisions? --jhi
*/
- PerlIO **table = &_perlio;
+ dTHX;
+ PerlIO **table = &PL_perlio;
int code = 0;
while ((f = *table)) {
int i;
void
PerlIOBase_flush_linebuf()
{
- PerlIO **table = &_perlio;
+ dTHX;
+ PerlIO **table = &PL_perlio;
PerlIO *f;
while ((f = *table)) {
int i;
/*--------------------------------------------------------------------------------------*/
/*
- * utf8 and raw dummy layers
+ * utf8 and raw dummy layers
*/
IV
/*--------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------*/
/*
- * "Methods" of the "base class"
+ * "Methods" of the "base class"
*/
IV
*s++ = '+';
}
}
-#if O_TEXT != O_BINARY
+#ifdef PERLIO_USING_CRLF
if (!(flags & PERLIO_F_CRLF))
*s++ = 'b';
#endif
{
dTHX;
/*
- * Save the position as current head considers it
+ * Save the position as current head considers it
*/
Off_t old = PerlIO_tell(f);
SSize_t done;
}
}
+SV *
+PerlIO_sv_dup(pTHX_ SV *arg, CLONE_PARAMS *param)
+{
+ if (!arg)
+ return Nullsv;
+#ifdef sv_dup
+ if (param) {
+ return sv_dup(arg, param);
+ }
+ else {
+ return newSVsv(arg);
+ }
+#else
+ return newSVsv(arg);
+#endif
+}
+
+PerlIO *
+PerlIOBase_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
+{
+ PerlIO *nexto = PerlIONext(o);
+ if (*nexto) {
+ PerlIO_funcs *tab = PerlIOBase(nexto)->tab;
+ 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, (void*)f, (void*)o, (void*)param);
+ if (self->Getarg) {
+ arg = (*self->Getarg)(aTHX_ o,param,flags);
+ }
+ f = PerlIO_push(aTHX_ f, self, PerlIO_modestr(o,buf), arg);
+ if (arg) {
+ SvREFCNT_dec(arg);
+ }
+ }
+ return f;
+}
+
+#define PERLIO_MAX_REFCOUNTABLE_FD 2048
+#ifdef USE_THREADS
+perl_mutex PerlIO_mutex;
+#endif
+int PerlIO_fd_refcnt[PERLIO_MAX_REFCOUNTABLE_FD];
+
+void
+PerlIO_init(pTHX)
+{
+ /* Place holder for stdstreams call ??? */
+#ifdef USE_THREADS
+ MUTEX_INIT(&PerlIO_mutex);
+#endif
+}
+
+void
+PerlIOUnix_refcnt_inc(int fd)
+{
+ if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
+#ifdef USE_THREADS
+ MUTEX_LOCK(&PerlIO_mutex);
+#endif
+ PerlIO_fd_refcnt[fd]++;
+ PerlIO_debug("fd %d refcnt=%d\n",fd,PerlIO_fd_refcnt[fd]);
+#ifdef USE_THREADS
+ MUTEX_UNLOCK(&PerlIO_mutex);
+#endif
+ }
+}
+
+int
+PerlIOUnix_refcnt_dec(int fd)
+{
+ int cnt = 0;
+ if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
+#ifdef USE_THREADS
+ MUTEX_LOCK(&PerlIO_mutex);
+#endif
+ cnt = --PerlIO_fd_refcnt[fd];
+ PerlIO_debug("fd %d refcnt=%d\n",fd,cnt);
+#ifdef USE_THREADS
+ MUTEX_UNLOCK(&PerlIO_mutex);
+#endif
+ }
+ return cnt;
+}
+
+void
+PerlIO_cleanup(pTHX)
+{
+ int i;
+#ifdef USE_ITHREADS
+ PerlIO_debug("Cleanup %p\n",aTHX);
+#endif
+ /* Raise STDIN..STDERR refcount so we don't close them */
+ for (i=0; i < 3; i++)
+ PerlIOUnix_refcnt_inc(i);
+ PerlIO_cleantable(aTHX_ &PL_perlio);
+ /* Restore STDIN..STDERR refcount */
+ for (i=0; i < 3; i++)
+ PerlIOUnix_refcnt_dec(i);
+}
+
+
+
/*--------------------------------------------------------------------------------------*/
/*
- * Bottom-most level for UNIX-like case
+ * Bottom-most level for UNIX-like case
*/
typedef struct {
mode++;
}
/*
- * Always open in binary mode
+ * Always open in binary mode
*/
oflags |= O_BINARY;
if (*mode || oflags == -1) {
PerlIOUnix_pushed(PerlIO *f, const char *mode, SV *arg)
{
IV code = PerlIOBase_pushed(f, mode, arg);
+ PerlIOUnix *s = PerlIOSelf(f, PerlIOUnix);
if (*PerlIONext(f)) {
- PerlIOUnix *s = PerlIOSelf(f, PerlIOUnix);
s->fd = PerlIO_fileno(PerlIONext(f));
/*
- * XXX could (or should) we retrieve the oflags from the open file
+ * XXX could (or should) we retrieve the oflags from the open file
* handle rather than believing the "mode" we are passed in? XXX
- * Should the value on NULL mode be 0 or -1?
+ * Should the value on NULL mode be 0 or -1?
*/
s->oflags = mode ? PerlIOUnix_oflags(mode) : -1;
}
s->fd = fd;
s->oflags = imode;
PerlIOBase(f)->flags |= PERLIO_F_OPEN;
+ PerlIOUnix_refcnt_inc(fd);
return f;
}
else {
if (f) {
/*
- * FIXME: pop layers ???
+ * FIXME: pop layers ???
*/
}
return NULL;
}
}
+PerlIO *
+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, flags);
+ if (f) {
+ /* If all went well overwrite fd in dup'ed lay with the dup()'ed fd */
+ PerlIOUnix *s = PerlIOSelf(f, PerlIOUnix);
+ s->fd = fd;
+ PerlIOUnix_refcnt_inc(fd);
+ return f;
+ }
+ }
+ return NULL;
+}
+
+
SSize_t
PerlIOUnix_read(PerlIO *f, void *vbuf, Size_t count)
{
return PerlLIO_lseek(PerlIOSelf(f, PerlIOUnix)->fd, 0, SEEK_CUR);
}
+
IV
PerlIOUnix_close(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);
+ return -1;
+ }
while (PerlLIO_close(fd) != 0) {
if (errno != EINTR) {
code = -1;
PerlIOUnix_open,
NULL,
PerlIOUnix_fileno,
+ PerlIOUnix_dup,
PerlIOUnix_read,
PerlIOBase_unread,
PerlIOUnix_write,
/*--------------------------------------------------------------------------------------*/
/*
- * stdio as a layer
+ * stdio as a layer
*/
typedef struct {
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 ...
+ * This isn't used yet ...
*/
IV
PerlIOStdio_pushed(PerlIO *f, const char *mode, SV *arg)
if (f) {
char *path = SvPV_nolen(*args);
PerlIOStdio *s = PerlIOSelf(f, PerlIOStdio);
- FILE *stdio =
- PerlSIO_freopen(path, (mode = PerlIOStdio_mode(mode, tmode)),
+ FILE *stdio;
+ PerlIOUnix_refcnt_dec(fileno(s->stdio));
+ stdio = PerlSIO_freopen(path, (mode = PerlIOStdio_mode(mode, tmode)),
s->stdio);
if (!s->stdio)
return NULL;
s->stdio = stdio;
+ PerlIOUnix_refcnt_inc(fileno(s->stdio));
return f;
}
else {
PerlIOArg),
PerlIOStdio);
s->stdio = stdio;
+ PerlIOUnix_refcnt_inc(fileno(s->stdio));
}
return f;
}
(aTHX_(f = PerlIO_allocate(aTHX)), self,
mode, PerlIOArg), PerlIOStdio);
s->stdio = stdio;
+ PerlIOUnix_refcnt_inc(fileno(s->stdio));
return f;
}
}
return NULL;
}
+PerlIO *
+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, 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));
+ }
+ return f;
+}
+
+IV
+PerlIOStdio_close(PerlIO *f)
+{
+ dSYS;
+#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 */
+ PerlIO_flush(f);
+ return 0;
+ }
+ return (
+#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)
+#endif
+ );
+
+}
+
+
+
SSize_t
PerlIOStdio_read(PerlIO *f, void *vbuf, Size_t count)
{
STDCHAR *buf = (STDCHAR *) vbuf;
/*
* Perl is expecting PerlIO_getc() to fill the buffer Linux's
- * stdio does not do that for fread()
+ * stdio does not do that for fread()
*/
int ch = PerlSIO_fgetc(s);
if (ch != EOF) {
}
IV
-PerlIOStdio_close(PerlIO *f)
-{
- dSYS;
-#ifdef SOCKS5_VERSION_NAME
- int optval;
- Sock_size_t optlen = sizeof(int);
-#endif
- FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
- return (
-#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)
-#endif
- );
-
-}
-
-IV
PerlIOStdio_flush(PerlIO *f)
{
dSYS;
#if 0
/*
* FIXME: This discards ungetc() and pre-read stuff which is not
- * right if this is just a "sync" from a layer above Suspect right
+ * right if this is just a "sync" from a layer above Suspect right
* design is to do _this_ but not have layer above flush this
- * layer read-to-read
+ * layer read-to-read
*/
/*
- * Not writeable - sync by attempting a seek
+ * Not writeable - sync by attempting a seek
*/
int err = errno;
if (PerlSIO_fseek(stdio, (Off_t) 0, SEEK_CUR) != 0)
FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
int c;
/*
- * fflush()ing read-only streams can cause trouble on some stdio-s
+ * fflush()ing read-only streams can cause trouble on some stdio-s
*/
if ((PerlIOBase(f)->flags & PERLIO_F_CANWRITE)) {
if (PerlSIO_fflush(stdio) != 0)
#endif
#if (!defined(STDIO_PTR_LVAL_NOCHANGE_CNT))
/*
- * Setting ptr _does_ change cnt - we are done
+ * Setting ptr _does_ change cnt - we are done
*/
return;
#endif
#endif /* STDIO_PTR_LVALUE */
}
/*
- * Now (or only) set cnt
+ * Now (or only) set cnt
*/
#ifdef STDIO_CNT_LVALUE
PerlSIO_set_cnt(stdio, cnt);
PerlIOStdio_open,
NULL,
PerlIOStdio_fileno,
+ PerlIOStdio_dup,
PerlIOStdio_read,
PerlIOStdio_unread,
PerlIOStdio_write,
/*--------------------------------------------------------------------------------------*/
/*
- * perlio buffer layer
+ * perlio buffer layer
*/
IV
if (*mode == 'I') {
init = 1;
/*
- * mode++;
+ * mode++;
*/
}
f = (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
NULL, narg, args);
if (f) {
- PerlIO_push(aTHX_ f, self, mode, PerlIOArg);
- fd = PerlIO_fileno(f);
-#if O_BINARY != O_TEXT
- /*
- * do something about failing setmode()? --jhi
- */
- PerlLIO_setmode(fd, O_BINARY);
-#endif
- if (init && fd == 2) {
+ if (PerlIO_push(aTHX_ f, self, mode, PerlIOArg) == 0) {
/*
- * Initial stderr is unbuffered
+ * if push fails during open, open fails. close will pop us.
*/
- PerlIOBase(f)->flags |= PERLIO_F_UNBUF;
+ PerlIO_close (f);
+ return NULL;
+ } else {
+ fd = PerlIO_fileno(f);
+#ifdef PERLIO_USING_CRLF
+ /*
+ * 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;
+ }
}
}
}
/*
* This "flush" is akin to sfio's sync in that it handles files in either
- * read or write state
+ * read or write state
*/
IV
PerlIOBuf_flush(PerlIO *f)
int code = 0;
if (PerlIOBase(f)->flags & PERLIO_F_WRBUF) {
/*
- * write() the buffer
+ * write() the buffer
*/
STDCHAR *buf = b->buf;
STDCHAR *p = buf;
else if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
STDCHAR *buf = PerlIO_get_base(f);
/*
- * Note position change
+ * Note position change
*/
b->posn += (b->ptr - buf);
if (b->ptr < b->end) {
/*
- * We did not consume all of it
+ * We did not consume all of it
*/
if (PerlIO_seek(PerlIONext(f), b->posn, SEEK_SET) == 0) {
b->posn = PerlIO_tell(PerlIONext(f));
b->ptr = b->end = b->buf;
PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
/*
- * FIXME: Is this right for read case ?
+ * FIXME: Is this right for read case ?
*/
if (PerlIO_flush(PerlIONext(f)) != 0)
code = -1;
* 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)) { }
+ * if (!(PerlIOBase(f)->flags & PERLIO_F_RDBUF)) { }
*/
if (PerlIO_flush(f) != 0)
return -1;
* 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_.
+ * hand, or ask it to fill _once_.
*/
avail = PerlIO_get_cnt(n);
if (avail <= 0) {
if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
/*
* Buffer is already a read buffer, we can overwrite any chars
- * which have been read back to buffer start
+ * which have been read back to buffer start
*/
avail = (b->ptr - b->buf);
}
else {
/*
* Buffer is idle, set it up so whole buffer is available for
- * unread
+ * unread
*/
avail = b->bufsiz;
b->end = b->buf + avail;
b->ptr = b->end;
PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
/*
- * Buffer extends _back_ from where we are now
+ * Buffer extends _back_ from where we are now
*/
b->posn -= b->bufsiz;
}
if (avail > (SSize_t) count) {
/*
- * If we have space for more than count, just move count
+ * If we have space for more than count, just move count
*/
avail = count;
}
buf -= avail;
/*
* In simple stdio-like ungetc() case chars will be already
- * there
+ * there
*/
if (buf != b->ptr) {
Copy(buf, b->ptr, avail, STDCHAR);
{
PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
/*
- * b->posn is file position where b->buf was read, or will be written
+ * b->posn is file position where b->buf was read, or will be written
*/
Off_t posn = b->posn;
if (b->buf) {
/*
- * If buffer is valid adjust position by amount in buffer
+ * If buffer is valid adjust position by amount in buffer
*/
posn += (b->ptr - b->buf);
}
IV code = PerlIOBase_close(f);
PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
- PerlMemShared_free(b->buf);
+ Safefree(b->buf);
}
b->buf = NULL;
b->ptr = b->end = b->buf;
if (!b->buf) {
if (!b->bufsiz)
b->bufsiz = 4096;
- b->buf = PerlMemShared_calloc(b->bufsiz, sizeof(STDCHAR));
+ b->buf =
+ Newz('B',b->buf,b->bufsiz, STDCHAR);
if (!b->buf) {
b->buf = (STDCHAR *) & b->oneword;
b->bufsiz = sizeof(b->oneword);
PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
}
+PerlIO *
+PerlIOBuf_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
+{
+ return PerlIOBase_dup(aTHX_ f, o, param, flags);
+}
+
+
+
PerlIO_funcs PerlIO_perlio = {
"perlio",
sizeof(PerlIOBuf),
PerlIOBuf_open,
NULL,
PerlIOBase_fileno,
+ PerlIOBuf_dup,
PerlIOBuf_read,
PerlIOBuf_unread,
PerlIOBuf_write,
/*--------------------------------------------------------------------------------------*/
/*
- * Temp layer to hold unread chars when cannot do it any other way
+ * Temp layer to hold unread chars when cannot do it any other way
*/
IV
PerlIOPending_fill(PerlIO *f)
{
/*
- * Should never happen
+ * Should never happen
*/
PerlIO_flush(f);
return 0;
PerlIOPending_close(PerlIO *f)
{
/*
- * A tad tricky - flush pops us, then we close new top
+ * A tad tricky - flush pops us, then we close new top
*/
PerlIO_flush(f);
return PerlIO_close(f);
PerlIOPending_seek(PerlIO *f, Off_t offset, int whence)
{
/*
- * A tad tricky - flush pops us, then we seek new top
+ * A tad tricky - flush pops us, then we seek new top
*/
PerlIO_flush(f);
return PerlIO_seek(f, offset, whence);
dTHX;
PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
- PerlMemShared_free(b->buf);
+ Safefree(b->buf);
b->buf = NULL;
}
PerlIO_pop(aTHX_ f);
IV code = PerlIOBase_pushed(f, mode, arg);
PerlIOl *l = PerlIOBase(f);
/*
- * Our PerlIO_fast_gets must match what we are pushed on, or sv_gets()
- * etc. get muddled when it changes mid-string when we auto-pop.
+ * Our PerlIO_fast_gets must match what we are pushed on, or sv_gets()
+ * etc. get muddled when it changes mid-string when we auto-pop.
*/
l->flags = (l->flags & ~(PERLIO_F_FASTGETS | PERLIO_F_UTF8)) |
(PerlIOBase(PerlIONext(f))->
NULL,
NULL,
PerlIOBase_fileno,
+ PerlIOBuf_dup,
PerlIOPending_read,
PerlIOBuf_unread,
PerlIOBuf_write,
/*
* crlf - translation On read translate CR,LF to "\n" we do this by
* overriding ptr/cnt entries to hand back a line at a time and keeping a
- * record of which nl we "lied" about. On write translate "\n" to CR,LF
+ * record of which nl we "lied" about. On write translate "\n" to CR,LF
*/
typedef struct {
PerlIOBuf base; /* PerlIOBuf stuff */
- STDCHAR *nl; /* Position of crlf we "lied" about in the
+ STDCHAR *nl; /* Position of crlf we "lied" about in the
* buffer */
} PerlIOCrlf;
}
else {
/*
- * Not CR,LF but just CR
+ * Not CR,LF but just CR
*/
nl++;
goto scan;
}
else {
/*
- * Blast - found CR as last char in buffer
+ * Blast - found CR as last char in buffer
*/
if (b->ptr < nl) {
/*
* They may not care, defer work as long as
- * possible
+ * possible
*/
return (nl - b->ptr);
}
int code;
b->ptr++; /* say we have read it as far as
* flush() is concerned */
- b->buf++; /* Leave space an front of buffer */
+ 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 */
if (code == 0)
goto test; /* fill() call worked */
/*
- * CR at EOF - just fall through
+ * CR at EOF - just fall through
*/
}
}
}
else {
/*
- * Test code - delete when it works ...
+ * Test code - delete when it works ...
*/
STDCHAR *chk;
if (c->nl)
if (c->nl) {
if (ptr > c->nl) {
/*
- * They have taken what we lied about
+ * They have taken what we lied about
*/
*(c->nl) = 0xd;
c->nl = NULL;
if (*buf == '\n') {
if ((b->ptr + 2) > eptr) {
/*
- * Not room for both
+ * Not room for both
*/
PerlIO_flush(f);
break;
PerlIOBuf_open,
NULL,
PerlIOBase_fileno,
+ PerlIOBuf_dup,
PerlIOBuf_read, /* generic read works with ptr/cnt lies
* ... */
PerlIOCrlf_unread, /* Put CR,LF in buffer for each '\n' */
#ifdef HAS_MMAP
/*--------------------------------------------------------------------------------------*/
/*
- * mmap as "buffer" layer
+ * mmap as "buffer" layer
*/
typedef struct {
if (flags & PERLIO_F_CANREAD) {
PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
int fd = PerlIO_fileno(f);
- struct stat st;
- code = fstat(fd, &st);
+ Stat_t st;
+ code = Fstat(fd, &st);
if (code == 0 && S_ISREG(st.st_mode)) {
SSize_t len = st.st_size - b->posn;
if (len > 0) {
if (b->posn < 0) {
/*
* This is a hack - should never happen - open should
- * have set it !
+ * have set it !
*/
b->posn = PerlIO_tell(PerlIONext(f));
}
PerlIOBuf *b = &m->base;
if (b->buf && (PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
/*
- * Already have a readbuffer in progress
+ * Already have a readbuffer in progress
*/
return b->buf;
}
if (b->buf) {
/*
- * We have a write buffer or flushed PerlIOBuf read buffer
+ * 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 */
PerlIOMmap_map(f); /* Try and map it */
if (!b->buf) {
/*
- * Map did not work - recover PerlIOBuf buffer if we have one
+ * Map did not work - recover PerlIOBuf buffer if we have one
*/
b->buf = m->bbuf;
}
}
if (m->len) {
/*
- * Loose the unwritable mapped buffer
+ * Loose the unwritable mapped buffer
*/
PerlIO_flush(f);
/*
- * If flush took the "buffer" see if we have one from before
+ * If flush took the "buffer" see if we have one from before
*/
if (!b->buf && m->bbuf)
b->buf = m->bbuf;
PerlIOBuf *b = &m->base;
if (!b->buf || !(PerlIOBase(f)->flags & PERLIO_F_WRBUF)) {
/*
- * No, or wrong sort of, buffer
+ * No, or wrong sort of, buffer
*/
if (m->len) {
if (PerlIOMmap_unmap(f) != 0)
return 0;
}
/*
- * If unmap took the "buffer" see if we have one from before
+ * If unmap took the "buffer" see if we have one from before
*/
if (!b->buf && m->bbuf)
b->buf = m->bbuf;
PerlIOBuf *b = &m->base;
IV code = PerlIOBuf_flush(f);
/*
- * Now we are "synced" at PerlIOBuf level
+ * Now we are "synced" at PerlIOBuf level
*/
if (b->buf) {
if (m->len) {
/*
- * Unmap the buffer
+ * Unmap the buffer
*/
if (PerlIOMmap_unmap(f) != 0)
code = -1;
else {
/*
* We seem to have a PerlIOBuf buffer which was not mapped
- * remember it in case we need one later
+ * remember it in case we need one later
*/
m->bbuf = b->buf;
}
return code;
}
+PerlIO *
+PerlIOMmap_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
+{
+ return PerlIOBase_dup(aTHX_ f, o, param, flags);
+}
+
PerlIO_funcs PerlIO_mmap = {
"mmap",
PerlIOBuf_open,
NULL,
PerlIOBase_fileno,
+ PerlIOMmap_dup,
PerlIOBuf_read,
PerlIOMmap_unread,
PerlIOMmap_write,
#endif /* HAS_MMAP */
-void
-PerlIO_init(void)
-{
- dTHX;
-#ifndef WIN32
- call_atexit(PerlIO_cleanup_layers, NULL);
-#endif
- if (!_perlio) {
-#ifndef WIN32
- atexit(&PerlIO_cleanup);
-#endif
- }
-}
-
#undef PerlIO_stdin
PerlIO *
PerlIO_stdin(void)
{
- if (!_perlio) {
- dTHX;
+ dTHX;
+ if (!PL_perlio) {
PerlIO_stdstreams(aTHX);
}
- return &_perlio[1];
+ return &PL_perlio[1];
}
#undef PerlIO_stdout
PerlIO *
PerlIO_stdout(void)
{
- if (!_perlio) {
- dTHX;
+ dTHX;
+ if (!PL_perlio) {
PerlIO_stdstreams(aTHX);
}
- return &_perlio[2];
+ return &PL_perlio[2];
}
#undef PerlIO_stderr
PerlIO *
PerlIO_stderr(void)
{
- if (!_perlio) {
- dTHX;
+ dTHX;
+ if (!PL_perlio) {
PerlIO_stdstreams(aTHX);
}
- return &_perlio[3];
+ return &PL_perlio[3];
}
/*--------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------*/
/*
* Functions which can be called on any kind of PerlIO implemented in
- * terms of above
+ * terms of above
*/
#undef PerlIO_getc
PerlIO_tmpfile(void)
{
/*
- * I have no idea how portable mkstemp() is ...
+ * I have no idea how portable mkstemp() is ...
*/
#if defined(WIN32) || !defined(HAVE_MKSTEMP)
dTHX;
/*======================================================================================*/
/*
- * Now some functions in terms of above which may be needed even if we are
- * not in true PerlIO mode
+ * Now some functions in terms of above which may be needed even if we are
+ * not in true PerlIO mode
*/
#ifndef HAS_FSETPOS
return result;
}
#endif
+
+
+
+
+