OLD | NEW |
(Empty) | |
| 1 #include "stdio_impl.h" |
| 2 #include <string.h> |
| 3 |
| 4 #define MIN(a,b) ((a)<(b) ? (a) : (b)) |
| 5 |
| 6 size_t fread(void *restrict destv, size_t size, size_t nmemb, FILE *restrict f) |
| 7 { |
| 8 unsigned char *dest = destv; |
| 9 size_t len = size*nmemb, l = len, k; |
| 10 |
| 11 FLOCK(f); |
| 12 |
| 13 f->mode |= f->mode-1; |
| 14 |
| 15 if (f->rend - f->rpos > 0) { |
| 16 /* First exhaust the buffer. */ |
| 17 k = MIN(f->rend - f->rpos, l); |
| 18 memcpy(dest, f->rpos, k); |
| 19 f->rpos += k; |
| 20 dest += k; |
| 21 l -= k; |
| 22 } |
| 23 |
| 24 /* Read the remainder directly */ |
| 25 for (; l; l-=k, dest+=k) { |
| 26 k = __toread(f) ? 0 : f->read(f, dest, l); |
| 27 if (k+1<=1) { |
| 28 FUNLOCK(f); |
| 29 return (len-l)/size; |
| 30 } |
| 31 } |
| 32 |
| 33 FUNLOCK(f); |
| 34 return nmemb; |
| 35 } |
| 36 |
| 37 weak_alias(fread, fread_unlocked); |
OLD | NEW |