X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=src%2Fstdio%2Ffread.c;h=c461256c3b0de4d6af84c0df426710fa121ddc06;hb=96fbcf7d80f469e39d1dd12533f8bb8d13b64fe5;hp=0fa0b2aa1d24f528d1704b53e62cb45b54229c48;hpb=0b44a0315b47dd8eced9f3b7f31580cf14bbfc01;p=musl diff --git a/src/stdio/fread.c b/src/stdio/fread.c index 0fa0b2aa..c461256c 100644 --- a/src/stdio/fread.c +++ b/src/stdio/fread.c @@ -1,8 +1,9 @@ #include "stdio_impl.h" +#include #define MIN(a,b) ((a)<(b) ? (a) : (b)) -size_t fread(void *destv, size_t size, size_t nmemb, FILE *f) +size_t fread(void *restrict destv, size_t size, size_t nmemb, FILE *restrict f) { unsigned char *dest = destv; size_t len = size*nmemb, l = len, k; @@ -12,38 +13,26 @@ size_t fread(void *destv, size_t size, size_t nmemb, FILE *f) FLOCK(f); - for (;;) { + if (f->rend - f->rpos > 0) { /* First exhaust the buffer. */ k = MIN(f->rend - f->rpos, l); memcpy(dest, f->rpos, k); f->rpos += k; dest += k; l -= k; - - /* Stop on EOF or errors */ - if (f->flags & (F_EOF|F_ERR|F_NORD)) goto eof; - - /* Done? Or going unbuffered? */ - if (!l || l > f->buf_size/2) break; - - /* Otherwise, refill & read thru buffer. */ - __underflow(f); } - + /* Read the remainder directly */ for (; l; l-=k, dest+=k) { - k = f->read(f, dest, l); + k = __toread(f) ? 0 : f->read(f, dest, l); if (k+1<=1) { - f->flags |= F_EOF | (F_ERR & k); - goto eof; + FUNLOCK(f); + return (len-l)/size; } } FUNLOCK(f); return nmemb; -eof: - FUNLOCK(f); - return (len-l)/size; } weak_alias(fread, fread_unlocked);