On Fri, Dec 14, 2018 at 10:49:23AM -0700, Todd C. Miller wrote: > On Fri, 14 Dec 2018 08:45:31 +0100, Sebastien Marie wrote: > > After thinking about this a bit more I believe it best to just use > the existing FILE * and swap in the buffer temporarily. There's > no need to store the value of the old buffer; since we are unbuffered > it can only point to _nbuf. > > This is simpler and should address all of your concerns. >
yes, I am fine with it. thanks. ok semarie@ > Index: lib/libc/stdio/fread.c > =================================================================== > RCS file: /cvs/src/lib/libc/stdio/fread.c,v > retrieving revision 1.15 > diff -u -p -u -r1.15 fread.c > --- lib/libc/stdio/fread.c 21 Sep 2016 04:38:56 -0000 1.15 > +++ lib/libc/stdio/fread.c 14 Dec 2018 17:48:46 -0000 > @@ -69,18 +69,33 @@ fread(void *buf, size_t size, size_t cou > total = resid; > p = buf; > > - if ((fp->_flags & __SNBF) != 0) { > - /* > - * We know if we're unbuffered that our buffer is empty, so > - * we can just read directly. This is much faster than the > - * loop below which will perform a series of one byte reads. > - */ > - while (resid > 0 && (r = (*fp->_read)(fp->_cookie, p, resid)) > > 0) { > - p += r; > - resid -= r; > + /* > + * If we're unbuffered we know that the buffer in fp is empty so > + * we can read directly into buf. This is much faster than a > + * series of one byte reads into fp->_nbuf. > + */ > + if ((fp->_flags & __SNBF) != 0 && buf != NULL) { > + /* set up the buffer */ > + fp->_bf._base = fp->_p = buf; > + fp->_bf._size = total; > + > + while (resid > 0) { > + if (__srefill(fp)) { > + /* no more input: return partial result */ > + count = (total - resid) / size; > + break; > + } > + fp->_p += fp->_r; > + resid -= fp->_r; > } > + > + /* restore the old buffer (see __smakebuf) */ > + fp->_bf._base = fp->_p = fp->_nbuf; > + fp->_bf._size = 1; > + fp->_r = 0; > + > FUNLOCKFILE(fp); > - return ((total - resid) / size); > + return (count); > } > > while (resid > (r = fp->_r)) { -- Sebastien Marie