}
int
-PerlIO_binmode(pTHX_ PerlIO *fp, int iotype, int mode, const char *names)
+perlsio_binmode(FILE *fp, int iotype, int mode)
{
/* This used to be contents of do_binmode in doio.c */
#ifdef DOSISH
# if defined(atarist) || defined(__MINT__)
- if (!PerlIO_flush(fp)) {
+ if (!fflush(fp)) {
if (mode & O_BINARY)
((FILE*)fp)->_flag |= _IOBIN;
else
}
return 0;
# else
- if (PerlLIO_setmode(PerlIO_fileno(fp), mode) != -1) {
+ if (PerlLIO_setmode(fileno(fp), mode) != -1) {
# if defined(WIN32) && defined(__BORLANDC__)
/* The translation mode of the stream is maintained independent
* of the translation mode of the fd in the Borland RTL (heavy
* set the mode explicitly for the stream (though they don't
* document this anywhere). GSAR 97-5-24
*/
- PerlIO_seek(fp,0L,0);
+ fseek(fp,0L,0);
if (mode & O_BINARY)
- ((FILE*)fp)->flags |= _F_BIN;
+ fp->flags |= _F_BIN;
else
- ((FILE*)fp)->flags &= ~ _F_BIN;
+ fp->flags &= ~ _F_BIN;
# endif
return 1;
}
#endif
}
-
-
+int
+PerlIO_binmode(pTHX_ PerlIO *fp, int iotype, int mode, const char *names)
+{
+ return perlsio_binmode(fp,iotype,mode);
+}
#endif
PerlIOl *l = *f;
if (l)
{
+ PerlIO_debug(__FUNCTION__ " f=%p %s\n",f,l->tab->name);
(*l->tab->Popped)(f);
*f = l->next;
Safefree(l);
len = av_len(PerlIO_layer_av);
if (len < 1)
{
- if (PerlIO_stdio.Set_ptrcnt)
+ if (O_BINARY != O_TEXT)
{
- av_push(PerlIO_layer_av,SvREFCNT_inc(PerlIO_find_layer(PerlIO_stdio.name,0)));
+ av_push(PerlIO_layer_av,SvREFCNT_inc(PerlIO_find_layer(PerlIO_crlf.name,0)));
}
else
- {
- av_push(PerlIO_layer_av,SvREFCNT_inc(PerlIO_find_layer(PerlIO_perlio.name,0)));
- }
+ {
+ if (PerlIO_stdio.Set_ptrcnt)
+ {
+ av_push(PerlIO_layer_av,SvREFCNT_inc(PerlIO_find_layer(PerlIO_stdio.name,0)));
+ }
+ else
+ {
+ av_push(PerlIO_layer_av,SvREFCNT_inc(PerlIO_find_layer(PerlIO_perlio.name,0)));
+ }
+ }
len = av_len(PerlIO_layer_av);
}
if (n < 0)
if (!_perlio)
{
PerlIO_allocate();
- PerlIO_fdopen(0,"Ir");
- PerlIO_fdopen(1,"Iw");
- PerlIO_fdopen(2,"Iw");
+ PerlIO_fdopen(0,"Ir" PERLIO_STDTEXT);
+ PerlIO_fdopen(1,"Iw" PERLIO_STDTEXT);
+ PerlIO_fdopen(2,"Iw" PERLIO_STDTEXT);
}
}
l->next = *f;
l->tab = tab;
*f = l;
+ PerlIO_debug(__FUNCTION__ " f=%p %s %s\n",f,tab->name,(mode) ? mode : "(Null)");
if ((*l->tab->Pushed)(f,mode) != 0)
{
PerlIO_pop(f);
/* Given the abstraction above the public API functions */
int
-PerlIO_binmode(pTHX_ PerlIO *fp, int iotype, int mode, const char *names)
+PerlIO_binmode(pTHX_ PerlIO *f, int iotype, int mode, const char *names)
{
+ PerlIO_debug(__FUNCTION__ " f=%p %s %c %x %s\n",
+ f,PerlIOBase(f)->tab->name,iotype,mode, (names) ? names : "(Null)");
if (!names || (O_TEXT != O_BINARY && mode & O_BINARY))
{
- PerlIO *top = fp;
+ PerlIO *top = f;
PerlIOl *l;
while (l = *top)
{
if (PerlIOBase(top)->tab == &PerlIO_crlf)
{
PerlIO_flush(top);
- PerlIO_pop(top);
+ PerlIOBase(top)->flags &= ~PERLIO_F_CRLF;
break;
}
top = PerlIONext(top);
}
}
- return PerlIO_apply_layers(aTHX_ fp, NULL, names) == 0 ? TRUE : FALSE;
+ return PerlIO_apply_layers(aTHX_ f, NULL, names) == 0 ? TRUE : FALSE;
+}
+
+#undef PerlIO__close
+int
+PerlIO__close(PerlIO *f)
+{
+ return (*PerlIOBase(f)->tab->Close)(f);
}
+
#undef PerlIO_close
int
PerlIO_close(PerlIO *f)
void
PerlIO_clearerr(PerlIO *f)
{
- (*PerlIOBase(f)->tab->Clearerr)(f);
+ if (f && *f)
+ (*PerlIOBase(f)->tab->Clearerr)(f);
}
#undef PerlIO_setlinebuf
return PerlIO_fileno(PerlIONext(f));
}
+char *
+PerlIO_modestr(PerlIO *f,char *buf)
+{
+ char *s = buf;
+ IV flags = PerlIOBase(f)->flags;
+ if (flags & PERLIO_F_CANREAD)
+ *s++ = 'r';
+ if (flags & PERLIO_F_CANWRITE)
+ *s++ = 'w';
+ if (flags & PERLIO_F_CRLF)
+ *s++ = 't';
+ else
+ *s++ = 'b';
+ *s = '\0';
+ return buf;
+}
+
IV
PerlIOBase_pushed(PerlIO *f, const char *mode)
{
PerlIOl *l = PerlIOBase(f);
+ const char *omode = mode;
+ char temp[8];
l->flags &= ~(PERLIO_F_CANREAD|PERLIO_F_CANWRITE|
- PERLIO_F_TRUNCATE|PERLIO_F_APPEND|PERLIO_F_BINARY);
+ PERLIO_F_TRUNCATE|PERLIO_F_APPEND);
if (mode)
{
switch (*mode++)
{
case 'r':
- l->flags = PERLIO_F_CANREAD;
+ l->flags |= PERLIO_F_CANREAD;
break;
case 'a':
- l->flags = PERLIO_F_APPEND|PERLIO_F_CANWRITE;
+ l->flags |= PERLIO_F_APPEND|PERLIO_F_CANWRITE;
break;
case 'w':
- l->flags = PERLIO_F_TRUNCATE|PERLIO_F_CANWRITE;
+ l->flags |= PERLIO_F_TRUNCATE|PERLIO_F_CANWRITE;
break;
default:
errno = EINVAL;
l->flags |= PERLIO_F_CANREAD|PERLIO_F_CANWRITE;
break;
case 'b':
- l->flags |= PERLIO_F_BINARY;
+ l->flags &= ~PERLIO_F_CRLF;
+ break;
+ case 't':
+ l->flags |= PERLIO_F_CRLF;
break;
default:
errno = EINVAL;
if (l->next)
{
l->flags |= l->next->flags &
- (PERLIO_F_CANREAD|PERLIO_F_CANWRITE|
- PERLIO_F_TRUNCATE|PERLIO_F_APPEND|PERLIO_F_BINARY);
+ (PERLIO_F_CANREAD|PERLIO_F_CANWRITE|PERLIO_F_TRUNCATE|PERLIO_F_APPEND);
}
}
+ PerlIO_debug(__FUNCTION__ " f=%p %s %s fl=%08x (%s)\n",
+ f,PerlIOBase(f)->tab->name,(omode) ? omode : "(Null)",
+ l->flags,PerlIO_modestr(f,temp));
return 0;
}
PerlIOBase_close(PerlIO *f)
{
IV code = 0;
+ PerlIO *n = PerlIONext(f);
if (PerlIO_flush(f) != 0)
code = -1;
- if (PerlIO_close(PerlIONext(f)) != 0)
+ if (n && (*PerlIOBase(n)->tab->Close)(n) != 0)
code = -1;
PerlIOBase(f)->flags &= ~(PERLIO_F_CANREAD|PERLIO_F_CANWRITE|PERLIO_F_OPEN);
return code;
{
if (f && *f)
{
- PerlIOBase(f)->flags &= ~PERLIO_F_ERROR;
+ PerlIO *n = PerlIONext(f);
+ PerlIOBase(f)->flags &= ~(PERLIO_F_ERROR|PERLIO_F_EOF);
+ if (n)
+ PerlIO_clearerr(n);
}
}
}
if (*mode == 'b')
{
- oflags |= O_BINARY;
+ oflags |= O_BINARY;
+ oflags &= ~O_TEXT;
+ mode++;
+ }
+ else if (*mode == 't')
+ {
+ oflags |= O_TEXT;
+ oflags &= ~O_BINARY;
mode++;
}
/* Always open in binary mode */
PerlIO_funcs PerlIO_unix = {
"unix",
sizeof(PerlIOUnix),
- 0,
+ PERLIO_K_RAW,
PerlIOUnix_fileno,
PerlIOUnix_fdopen,
PerlIOUnix_open,
return fileno(PerlIOSelf(f,PerlIOStdio)->stdio);
}
+const char *
+PerlIOStdio_mode(const char *mode,char *tmode)
+{
+ const char *ret = mode;
+ if (O_BINARY != O_TEXT)
+ {
+ ret = (const char *) tmode;
+ while (*mode)
+ {
+ *tmode++ = *mode++;
+ }
+ *tmode++ = 'b';
+ *tmode = '\0';
+ }
+ return ret;
+}
PerlIO *
PerlIOStdio_fdopen(PerlIO_funcs *self, int fd,const char *mode)
{
PerlIO *f = NULL;
int init = 0;
+ char tmode[8];
if (*mode == 'I')
{
init = 1;
}
}
else
- stdio = fdopen(fd,mode);
+ {
+ stdio = fdopen(fd,mode = PerlIOStdio_mode(mode,tmode));
+ }
if (stdio)
{
PerlIOStdio *s = PerlIOSelf(PerlIO_push(f = PerlIO_allocate(),self,mode),PerlIOStdio);
FILE *stdio = fopen(path,mode);
if (stdio)
{
- PerlIOStdio *s = PerlIOSelf(PerlIO_push(f = PerlIO_allocate(),self,mode),PerlIOStdio);
+ char tmode[8];
+ PerlIOStdio *s = PerlIOSelf(PerlIO_push(f = PerlIO_allocate(), self,
+ (mode = PerlIOStdio_mode(mode,tmode))),
+ PerlIOStdio);
s->stdio = stdio;
}
return f;
PerlIOStdio_reopen(const char *path, const char *mode, PerlIO *f)
{
PerlIOStdio *s = PerlIOSelf(f,PerlIOStdio);
- FILE *stdio = freopen(path,mode,s->stdio);
+ char tmode[8];
+ FILE *stdio = freopen(path,(mode = PerlIOStdio_mode(mode,tmode)),s->stdio);
if (!s->stdio)
return -1;
s->stdio = stdio;
PerlIO_funcs PerlIO_stdio = {
"stdio",
sizeof(PerlIOStdio),
- 0,
+ PERLIO_K_BUFFERED,
PerlIOStdio_fileno,
PerlIOStdio_fdopen,
PerlIOStdio_open,
{
init = 1;
mode++;
+ if (O_BINARY != O_TEXT)
+ {
+ int code = PerlLIO_setmode(fd, O_BINARY);
+ PerlIO_debug(__FUNCTION__ " %s fd=%d m=%s c=%d\n",tab->name,fd,mode,code);
+ }
}
f = (*tab->Fdopen)(tab,fd,mode);
if (f)
{
- /* Initial stderr is unbuffered */
- if (!init || fd != 2)
+ PerlIOBuf *b = PerlIOSelf(PerlIO_push(f,self,mode),PerlIOBuf);
+ b->posn = PerlIO_tell(PerlIONext(f));
+ if (init && fd == 2)
{
- PerlIOBuf *b = PerlIOSelf(PerlIO_push(f,self,NULL),PerlIOBuf);
- b->posn = PerlIO_tell(PerlIONext(f));
- }
+ /* Initial stderr is unbuffered */
+ PerlIOBase(f)->flags |= PERLIO_F_UNBUF;
+ }
+ PerlIO_debug(__FUNCTION__ " %s f=%p fd=%d m=%s fl=%08x\n",
+ self->name,f,fd,mode,PerlIOBase(f)->flags);
}
return f;
}
PerlIO *f = (*tab->Open)(tab,path,mode);
if (f)
{
- PerlIOBuf *b = PerlIOSelf(PerlIO_push(f,self,NULL),PerlIOBuf);
+ PerlIOBuf *b = PerlIOSelf(PerlIO_push(f,self,mode),PerlIOBuf);
b->posn = PerlIO_tell(PerlIONext(f));
}
return f;
if (b->ptr >= (b->buf + b->bufsiz))
PerlIO_flush(f);
}
+ if (PerlIOBase(f)->flags & PERLIO_F_UNBUF)
+ PerlIO_flush(f);
return written;
}
PerlIO_funcs PerlIO_perlio = {
"perlio",
sizeof(PerlIOBuf),
- 0,
+ PERLIO_K_BUFFERED,
PerlIOBase_fileno,
PerlIOBuf_fdopen,
PerlIOBuf_open,
STDCHAR *nl; /* Position of crlf we "lied" about in the buffer */
} PerlIOCrlf;
+IV
+PerlIOCrlf_pushed(PerlIO *f, const char *mode)
+{
+ IV code;
+ PerlIOBase(f)->flags |= PERLIO_F_CRLF;
+ code = PerlIOBase_pushed(f,mode);
+ PerlIO_debug(__FUNCTION__ " f=%p %s %s fl=%08x\n",
+ f,PerlIOBase(f)->tab->name,(mode) ? mode : "(Null)",
+ PerlIOBase(f)->flags);
+ return code;
+}
+
+
SSize_t
PerlIOCrlf_unread(PerlIO *f, const void *vbuf, Size_t count)
{
- const STDCHAR *buf = (const STDCHAR *) vbuf+count;
- PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
PerlIOCrlf *c = PerlIOSelf(f,PerlIOCrlf);
- SSize_t unread = 0;
- if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
- PerlIO_flush(f);
if (c->nl)
{
*(c->nl) = 0xd;
c->nl = NULL;
}
- if (!b->buf)
- PerlIO_get_base(f);
- if (b->buf)
+ if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF))
+ return PerlIOBuf_unread(f,vbuf,count);
+ else
{
- if (!(PerlIOBase(f)->flags & PERLIO_F_RDBUF))
- {
- b->end = b->ptr = b->buf + b->bufsiz;
- PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
- }
- while (count > 0 && b->ptr > b->buf)
+ const STDCHAR *buf = (const STDCHAR *) vbuf+count;
+ PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
+ SSize_t unread = 0;
+ if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
+ PerlIO_flush(f);
+ if (!b->buf)
+ PerlIO_get_base(f);
+ if (b->buf)
{
- int ch = *--buf;
- if (ch == '\n')
+ if (!(PerlIOBase(f)->flags & PERLIO_F_RDBUF))
+ {
+ b->end = b->ptr = b->buf + b->bufsiz;
+ PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
+ }
+ while (count > 0 && b->ptr > b->buf)
{
- if (b->ptr - 2 >= b->buf)
+ int ch = *--buf;
+ if (ch == '\n')
{
- *--(b->ptr) = 0xa;
- *--(b->ptr) = 0xd;
- unread++;
- count--;
+ if (b->ptr - 2 >= b->buf)
+ {
+ *--(b->ptr) = 0xa;
+ *--(b->ptr) = 0xd;
+ unread++;
+ count--;
+ }
+ else
+ {
+ buf++;
+ break;
+ }
}
else
{
- buf++;
- break;
+ *--(b->ptr) = ch;
+ unread++;
+ count--;
}
}
- else
- {
- *--(b->ptr) = ch;
- unread++;
- count--;
- }
}
+ return unread;
}
- return unread;
}
SSize_t
if (PerlIOBase(f)->flags & PERLIO_F_RDBUF)
{
PerlIOCrlf *c = PerlIOSelf(f,PerlIOCrlf);
- if (!c->nl)
+ if ((PerlIOBase(f)->flags & PERLIO_F_CRLF) && !c->nl)
{
STDCHAR *nl = b->ptr;
scan:
{
PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
PerlIOCrlf *c = PerlIOSelf(f,PerlIOCrlf);
+ IV flags = PerlIOBase(f)->flags;
if (!b->buf)
PerlIO_get_base(f);
if (!ptr)
else
{
ptr = b->end;
- if (ptr > b->buf && ptr[-1] == 0xd)
+ if ((flags & PERLIO_F_CRLF) && ptr > b->buf && ptr[-1] == 0xd)
ptr--;
}
ptr -= cnt;
else
{
chk = b->end;
- if (chk > b->buf && chk[-1] == 0xd)
+ if ((flags & PERLIO_F_CRLF) && chk > b->buf && chk[-1] == 0xd)
chk--;
}
chk -= cnt;
if (ptr != chk)
{
dTHX;
- Perl_croak(aTHX_ "ptr wrong %p != %p nl=%p e=%p for %d",
- ptr, chk, c->nl, b->end, cnt);
+ Perl_croak(aTHX_ "ptr wrong %p != %p fl=%08x nl=%p e=%p for %d",
+ ptr, chk, flags, c->nl, b->end, cnt);
}
}
if (c->nl)
SSize_t
PerlIOCrlf_write(PerlIO *f, const void *vbuf, Size_t count)
{
- PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
- const STDCHAR *buf = (const STDCHAR *) vbuf;
- const STDCHAR *ebuf = buf+count;
- if (!b->buf)
- PerlIO_get_base(f);
- if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
- return 0;
- while (buf < ebuf)
+ if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF))
+ return PerlIOBuf_write(f,vbuf,count);
+ else
{
- STDCHAR *eptr = b->buf+b->bufsiz;
- PerlIOBase(f)->flags |= PERLIO_F_WRBUF;
- while (buf < ebuf && b->ptr < eptr)
+ PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
+ const STDCHAR *buf = (const STDCHAR *) vbuf;
+ const STDCHAR *ebuf = buf+count;
+ if (!b->buf)
+ PerlIO_get_base(f);
+ if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
+ return 0;
+ while (buf < ebuf)
{
- if (*buf == '\n')
+ STDCHAR *eptr = b->buf+b->bufsiz;
+ PerlIOBase(f)->flags |= PERLIO_F_WRBUF;
+ while (buf < ebuf && b->ptr < eptr)
{
- if ((b->ptr + 2) > eptr)
- {
- /* Not room for both */
- PerlIO_flush(f);
- break;
- }
- else
+ if (*buf == '\n')
{
- *(b->ptr)++ = 0xd; /* CR */
- *(b->ptr)++ = 0xa; /* LF */
- buf++;
- if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF)
+ if ((b->ptr + 2) > eptr)
{
+ /* Not room for both */
PerlIO_flush(f);
break;
}
+ else
+ {
+ *(b->ptr)++ = 0xd; /* CR */
+ *(b->ptr)++ = 0xa; /* LF */
+ buf++;
+ if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF)
+ {
+ PerlIO_flush(f);
+ break;
+ }
+ }
+ }
+ else
+ {
+ int ch = *buf++;
+ *(b->ptr)++ = ch;
+ }
+ if (b->ptr >= eptr)
+ {
+ PerlIO_flush(f);
+ break;
}
- }
- else
- {
- int ch = *buf++;
- *(b->ptr)++ = ch;
- }
- if (b->ptr >= eptr)
- {
- PerlIO_flush(f);
- break;
}
}
+ if (PerlIOBase(f)->flags & PERLIO_F_UNBUF)
+ PerlIO_flush(f);
+ return (buf - (STDCHAR *) vbuf);
}
- return (buf - (STDCHAR *) vbuf);
}
IV
PerlIO_funcs PerlIO_crlf = {
"crlf",
sizeof(PerlIOCrlf),
- 0,
+ PERLIO_K_BUFFERED|PERLIO_K_CANCRLF,
PerlIOBase_fileno,
PerlIOBuf_fdopen,
PerlIOBuf_open,
PerlIOBuf_reopen,
- PerlIOBase_pushed,
+ PerlIOCrlf_pushed,
PerlIOBase_noop_ok, /* popped */
PerlIOBuf_read, /* generic read works with ptr/cnt lies ... */
PerlIOCrlf_unread, /* Put CR,LF in buffer for each '\n' */
PerlIO_funcs PerlIO_mmap = {
"mmap",
sizeof(PerlIOMmap),
- 0,
+ PERLIO_K_BUFFERED,
PerlIOBase_fileno,
PerlIOBuf_fdopen,
PerlIOBuf_open,