#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)
#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)
#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
*/
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
}
void
-PerlIO_init(void)
+PerlIO_init(pTHX)
{
/*
* Force this file to be included in perl binary. Which allows this
/*
* Table of pointers to the PerlIO structs (malloc'ed)
*/
-PerlIO *_perlio = NULL;
#define PERLIO_TABLE_SIZE 64
-
-
PerlIO *
PerlIO_allocate(pTHX)
{
*/
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
return;
}
*f = l->next;;
- PerlMemShared_free(l);
+ Safefree(l);
}
}
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;
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
*/
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 {
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;
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;
}
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, CLONE_PARAMS *param)
-{
- if (f && *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);
- return new;
- }
- else {
- SETERRNO(EBADF, SS$_IVCHAN);
- return NULL;
- }
-}
-
#undef PerlIO_close
int
PerlIO_close(PerlIO *f)
{
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 (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;
}
/*
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);
}
}
* 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);
}
}
/*
* 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) {
}
}
}
- 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;
}
* things on fflush(NULL), but should we be bound by their design
* 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;
*s++ = '+';
}
}
-#if O_TEXT != O_BINARY
+#ifdef PERLIO_USING_CRLF
if (!(flags & PERLIO_F_CRLF))
*s++ = 'b';
#endif
}
}
+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
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
s->fd = fd;
s->oflags = imode;
PerlIOBase(f)->flags |= PERLIO_F_OPEN;
+ PerlIOUnix_refcnt_inc(fd);
return f;
}
else {
}
}
-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)
-{
- PerlIO *nexto = PerlIONext(o);
- PerlIO_debug("PerlIOBase_dup f=%p o=%p param=%p\n",f,o,param);
- if (*nexto) {
- PerlIO_funcs *tab = PerlIOBase(nexto)->tab;
- f = (*tab->Dup)(aTHX_ f, nexto, param);
- }
- if (f) {
- PerlIO_funcs *self = PerlIOBase(o)->tab;
- SV *arg = Nullsv;
- char buf[8];
- if (self->Getarg) {
- arg = (*self->Getarg)(o);
- if (arg) {
- arg = PerlIO_sv_dup(aTHX_ arg, param);
- }
- }
- f = PerlIO_push(aTHX_ f, self, PerlIO_modestr(o,buf), arg);
- if (!f && arg) {
- SvREFCNT_dec(arg);
- }
- }
- return f;
-}
-
-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 = PerlLIO_dup(os->fd);
- if (fd >= 0) {
- f = PerlIOBase_dup(aTHX_ f, o, param);
+ 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;
}
- else {
- PerlLIO_close(fd);
- }
}
return NULL;
}
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;
while (*mode) {
*tmode++ = *mode++;
}
- if (O_BINARY != O_TEXT) {
- *tmode++ = 'b';
- }
+#ifdef PERLIO_USING_CRLF
+ *tmode++ = 'b';
+#endif
*tmode = '\0';
return ret;
}
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)
{
}
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;
#endif
-PerlIO *
-PerlIOStdio_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param)
-{
- return PerlIOBase_dup(aTHX_ f, o, param);
-}
-
PerlIO_funcs PerlIO_stdio = {
"stdio",
sizeof(PerlIOStdio),
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) {
+ /*
+ * if push fails during open, open fails. close will pop us.
+ */
+ PerlIO_close (f);
+ return NULL;
+ } else {
+ fd = PerlIO_fileno(f);
+#ifdef PERLIO_USING_CRLF
/*
- * Initial stderr is unbuffered
+ * do something about failing setmode()? --jhi
*/
- PerlIOBase(f)->flags |= PERLIO_F_UNBUF;
+ PerlLIO_setmode(fd, O_BINARY);
+#endif
+ if (init && fd == 2) {
+ /*
+ * Initial stderr is unbuffered
+ */
+ PerlIOBase(f)->flags |= PERLIO_F_UNBUF;
+ }
}
}
}
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);
}
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);
}
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);
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 (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) {
}
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);
}
#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];
}
/*--------------------------------------------------------------------------------------*/
return result;
}
#endif
+
+
+
+
+