summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--doio.c11
-rw-r--r--perlio.c347
-rw-r--r--perliol.h22
3 files changed, 256 insertions, 124 deletions
diff --git a/doio.c b/doio.c
index 0da9856c30..3e36ea36e9 100644
--- a/doio.c
+++ b/doio.c
@@ -474,13 +474,17 @@ Perl_do_open9(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
#endif
}
if (saveifp) { /* must use old fp? */
+ /* If fd is less that PL_maxsysfd i.e. STDIN..STDERR
+ then dup the new fileno down
+ */
fd = PerlIO_fileno(saveifp);
if (saveofp) {
- PerlIO_flush(saveofp); /* emulate PerlIO_close() */
+ PerlIO_flush(saveofp); /* emulate PerlIO_close() */
if (saveofp != saveifp) { /* was a socket? */
PerlIO_close(saveofp);
+ /* This looks very suspect - NI-S 24 Nov 2000 */
if (fd > 2)
- Safefree(saveofp);
+ Safefree(saveofp); /* ??? */
}
}
if (fd != PerlIO_fileno(fp)) {
@@ -528,9 +532,6 @@ Perl_do_open9(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
}
}
}
- else if (O_BINARY != O_TEXT && IoTYPE(io) != IoTYPE_STD && !saveifp) {
- type = ":crlf";
- }
}
if (type) {
while (isSPACE(*type)) type++;
diff --git a/perlio.c b/perlio.c
index 8af1cf3b3b..5bad8c0991 100644
--- a/perlio.c
+++ b/perlio.c
@@ -42,12 +42,12 @@ PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names)
}
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
@@ -56,7 +56,7 @@ PerlIO_binmode(pTHX_ PerlIO *fp, int iotype, int mode, const char *names)
}
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
@@ -64,11 +64,11 @@ PerlIO_binmode(pTHX_ PerlIO *fp, int iotype, int mode, const char *names)
* 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;
}
@@ -87,8 +87,11 @@ PerlIO_binmode(pTHX_ PerlIO *fp, int iotype, int mode, const char *names)
#endif
}
-
-
+int
+PerlIO_binmode(pTHX_ PerlIO *fp, int iotype, int mode, const char *names)
+{
+ return perlsio_binmode(fp,iotype,mode);
+}
#endif
@@ -268,6 +271,7 @@ PerlIO_pop(PerlIO *f)
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);
@@ -460,14 +464,21 @@ PerlIO_default_layer(I32 n)
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)
@@ -490,9 +501,9 @@ PerlIO_stdstreams()
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);
}
}
@@ -507,6 +518,7 @@ PerlIO_push(PerlIO *f,PerlIO_funcs *tab,const char *mode)
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);
@@ -577,26 +589,36 @@ PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names)
/* 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)
@@ -750,7 +772,8 @@ PerlIO_error(PerlIO *f)
void
PerlIO_clearerr(PerlIO *f)
{
- (*PerlIOBase(f)->tab->Clearerr)(f);
+ if (f && *f)
+ (*PerlIOBase(f)->tab->Clearerr)(f);
}
#undef PerlIO_setlinebuf
@@ -858,24 +881,43 @@ PerlIOBase_fileno(PerlIO *f)
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;
@@ -889,7 +931,10 @@ PerlIOBase_pushed(PerlIO *f, const char *mode)
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;
@@ -902,10 +947,12 @@ PerlIOBase_pushed(PerlIO *f, const char *mode)
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;
}
@@ -943,9 +990,10 @@ IV
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;
@@ -976,7 +1024,10 @@ PerlIOBase_clearerr(PerlIO *f)
{
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);
}
}
@@ -1035,7 +1086,14 @@ PerlIOUnix_oflags(const char *mode)
}
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 */
@@ -1187,7 +1245,7 @@ PerlIOUnix_close(PerlIO *f)
PerlIO_funcs PerlIO_unix = {
"unix",
sizeof(PerlIOUnix),
- 0,
+ PERLIO_K_RAW,
PerlIOUnix_fileno,
PerlIOUnix_fdopen,
PerlIOUnix_open,
@@ -1228,12 +1286,29 @@ PerlIOStdio_fileno(PerlIO *f)
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;
@@ -1258,7 +1333,9 @@ PerlIOStdio_fdopen(PerlIO_funcs *self, int fd,const char *mode)
}
}
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);
@@ -1288,7 +1365,10 @@ PerlIOStdio_open(PerlIO_funcs *self, const char *path,const char *mode)
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;
@@ -1298,7 +1378,8 @@ int
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;
@@ -1513,7 +1594,7 @@ PerlIOStdio_set_ptrcnt(PerlIO *f,STDCHAR *ptr,SSize_t cnt)
PerlIO_funcs PerlIO_stdio = {
"stdio",
sizeof(PerlIOStdio),
- 0,
+ PERLIO_K_BUFFERED,
PerlIOStdio_fileno,
PerlIOStdio_fdopen,
PerlIOStdio_open,
@@ -1589,16 +1670,24 @@ PerlIOBuf_fdopen(PerlIO_funcs *self, int fd, const char *mode)
{
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;
}
@@ -1610,7 +1699,7 @@ PerlIOBuf_open(PerlIO_funcs *self, const char *path, const char *mode)
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;
@@ -1868,6 +1957,8 @@ PerlIOBuf_write(PerlIO *f, const void *vbuf, Size_t count)
if (b->ptr >= (b->buf + b->bufsiz))
PerlIO_flush(f);
}
+ if (PerlIOBase(f)->flags & PERLIO_F_UNBUF)
+ PerlIO_flush(f);
return written;
}
@@ -1990,7 +2081,7 @@ PerlIOBuf_set_ptrcnt(PerlIO *f, STDCHAR *ptr, SSize_t cnt)
PerlIO_funcs PerlIO_perlio = {
"perlio",
sizeof(PerlIOBuf),
- 0,
+ PERLIO_K_BUFFERED,
PerlIOBase_fileno,
PerlIOBuf_fdopen,
PerlIOBuf_open,
@@ -2029,56 +2120,74 @@ typedef struct
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
@@ -2090,7 +2199,7 @@ PerlIOCrlf_get_cnt(PerlIO *f)
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:
@@ -2151,6 +2260,7 @@ PerlIOCrlf_set_ptrcnt(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)
@@ -2160,7 +2270,7 @@ PerlIOCrlf_set_ptrcnt(PerlIO *f, STDCHAR *ptr, SSize_t cnt)
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;
@@ -2174,7 +2284,7 @@ PerlIOCrlf_set_ptrcnt(PerlIO *f, STDCHAR *ptr, SSize_t 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;
@@ -2182,8 +2292,8 @@ PerlIOCrlf_set_ptrcnt(PerlIO *f, STDCHAR *ptr, SSize_t 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)
@@ -2203,52 +2313,59 @@ PerlIOCrlf_set_ptrcnt(PerlIO *f, STDCHAR *ptr, SSize_t cnt)
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
@@ -2266,12 +2383,12 @@ PerlIOCrlf_flush(PerlIO *f)
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' */
@@ -2569,7 +2686,7 @@ PerlIOMmap_close(PerlIO *f)
PerlIO_funcs PerlIO_mmap = {
"mmap",
sizeof(PerlIOMmap),
- 0,
+ PERLIO_K_BUFFERED,
PerlIOBase_fileno,
PerlIOBuf_fdopen,
PerlIOBuf_open,
diff --git a/perliol.h b/perliol.h
index 1b7c1724e1..3d9c0e6f00 100644
--- a/perliol.h
+++ b/perliol.h
@@ -34,6 +34,13 @@ struct _PerlIO_funcs
void (*Set_ptrcnt)(PerlIO *f,STDCHAR *ptr,SSize_t cnt);
};
+/*--------------------------------------------------------------------------------------*/
+/* Kind values */
+#define PERLIO_K_RAW 0x00000001
+#define PERLIO_K_BUFFERED 0x00000002
+#define PERLIO_K_CANCRLF 0x00000004
+
+/*--------------------------------------------------------------------------------------*/
struct _PerlIO
{
PerlIOl * next; /* Lower layer */
@@ -50,13 +57,14 @@ struct _PerlIO
#define PERLIO_F_ERROR 0x00080000
#define PERLIO_F_TRUNCATE 0x00100000
#define PERLIO_F_APPEND 0x00200000
-#define PERLIO_F_BINARY 0x00400000
+#define PERLIO_F_CRLF 0x00400000
#define PERLIO_F_UTF8 0x00800000
-#define PERLIO_F_LINEBUF 0x01000000
+#define PERLIO_F_UNBUF 0x01000000
#define PERLIO_F_WRBUF 0x02000000
#define PERLIO_F_RDBUF 0x04000000
-#define PERLIO_F_TEMP 0x08000000
-#define PERLIO_F_OPEN 0x10000000
+#define PERLIO_F_LINEBUF 0x08000000
+#define PERLIO_F_TEMP 0x10000000
+#define PERLIO_F_OPEN 0x20000000
#define PerlIOBase(f) (*(f))
#define PerlIOSelf(f,type) ((type *)PerlIOBase(f))
@@ -74,6 +82,12 @@ extern PerlIO_funcs PerlIO_mmap;
extern PerlIO *PerlIO_allocate(void);
+#if O_BINARY != O_TEXT
+#define PERLIO_STDTEXT "t"
+#else
+#define PERLIO_STDTEXT ""
+#endif
+
/*--------------------------------------------------------------------------------------*/
/* Generic, or stub layer functions */