1 | #include "dietstdio.h" |
---|
2 | #include <unistd.h> |
---|
3 | #include <string.h> |
---|
4 | |
---|
5 | size_t fread_unlocked(void *ptr, size_t size, size_t nmemb, FILE *stream) { |
---|
6 | int res; |
---|
7 | unsigned long i,j; |
---|
8 | j=size*nmemb; |
---|
9 | i=0; |
---|
10 | ///log_msg("fread started fd %d, size %d, nmemb %d\n", stream->fd, size, nmemb); |
---|
11 | |
---|
12 | if (!(stream->flags&CANREAD)) { |
---|
13 | log_msg("fread: fd %d, can't read %d\n", stream->fd, (int)j); |
---|
14 | stream->flags|=ERRORINDICATOR; |
---|
15 | return 0; |
---|
16 | } |
---|
17 | if (!j || j/nmemb!=size) return 0; |
---|
18 | |
---|
19 | log_msg("fread: fd %d, line %d\n", stream->fd, __LINE__); |
---|
20 | |
---|
21 | if (stream->ungotten) { |
---|
22 | stream->ungotten=0; |
---|
23 | *(char*)ptr=stream->ungetbuf; |
---|
24 | ++i; |
---|
25 | log_msg("fread, line %d\n", __LINE__); |
---|
26 | if (j==1) return 1; |
---|
27 | } |
---|
28 | |
---|
29 | #ifdef WANT_FREAD_OPTIMIZATION |
---|
30 | if ( !(stream->flags&FDPIPE) && (j>stream->buflen)) { |
---|
31 | size_t tmp=j-i; |
---|
32 | ssize_t res; |
---|
33 | size_t inbuf=stream->bs-stream->bm; |
---|
34 | memcpy(ptr+i,stream->buf+stream->bm,inbuf); |
---|
35 | stream->bm=stream->bs=0; |
---|
36 | tmp-=inbuf; |
---|
37 | i+=inbuf; |
---|
38 | |
---|
39 | if (fflush_unlocked(stream)) return 0; |
---|
40 | while ((res=read(stream->fd,ptr+i,tmp))<(ssize_t)tmp) { |
---|
41 | if (res==-1) { |
---|
42 | stream->flags|=ERRORINDICATOR; |
---|
43 | goto exit; |
---|
44 | } else if (!res) { |
---|
45 | stream->flags|=EOFINDICATOR; |
---|
46 | goto exit; |
---|
47 | } |
---|
48 | i+=res; tmp-=res; |
---|
49 | } |
---|
50 | return nmemb; |
---|
51 | } |
---|
52 | #endif |
---|
53 | |
---|
54 | for (; i<j; ++i) { |
---|
55 | res=fgetc_unlocked(stream); |
---|
56 | log_msg("fread, line %d, res %d, i %d, j %d\n", __LINE__, res,(int)i,(int)j); |
---|
57 | if (res==EOF) |
---|
58 | exit: |
---|
59 | //log_msg("fread, line %d\n", __LINE__); |
---|
60 | return i/size; |
---|
61 | else |
---|
62 | ((unsigned char*)ptr)[i]=(unsigned char)res; |
---|
63 | } |
---|
64 | //log_msg("fread, line %d, nmemb %d\n", __LINE__, nmemb); |
---|
65 | return nmemb; |
---|
66 | } |
---|
67 | |
---|
68 | size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) __attribute__((weak,alias("fread_unlocked"))); |
---|