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