]> git.saurik.com Git - apple/libc.git/blob - stdio/FreeBSD/fread.c.patch
Libc-594.9.5.tar.gz
[apple/libc.git] / stdio / FreeBSD / fread.c.patch
1 --- fread.c.orig 2008-08-29 21:58:50.000000000 -0700
2 +++ fread.c 2008-09-02 02:18:06.000000000 -0700
3 @@ -55,7 +55,7 @@ fread(buf, size, count, fp)
4 {
5 size_t resid;
6 char *p;
7 - int r;
8 + int r, ret;
9 size_t total;
10
11 /*
12 @@ -71,21 +71,70 @@ fread(buf, size, count, fp)
13 fp->_r = 0;
14 total = resid;
15 p = buf;
16 + /* first deal with anything left in buffer, plus any ungetc buffers */
17 while (resid > (r = fp->_r)) {
18 (void)memcpy((void *)p, (void *)fp->_p, (size_t)r);
19 fp->_p += r;
20 /* fp->_r = 0 ... done in __srefill */
21 p += r;
22 resid -= r;
23 - if (__srefill(fp)) {
24 + if ((ret = __srefill0(fp)) > 0)
25 + break;
26 + else if (ret) {
27 /* no more input: return partial result */
28 FUNLOCKFILE(fp);
29 return ((total - resid) / size);
30 }
31 }
32 - (void)memcpy((void *)p, (void *)fp->_p, resid);
33 - fp->_r -= resid;
34 - fp->_p += resid;
35 + /*
36 + * 5980080: don't use optimization if __SMBF not set (meaning setvbuf
37 + * was called, and the buffer belongs to the user).
38 + * 6180417: but for unbuffered (__SMBF is not set), so specifically
39 + * test for it.
40 + */
41 + if ((fp->_flags & (__SMBF | __SNBF)) && resid > fp->_bf._size) {
42 + struct __sbuf save;
43 + size_t n;
44 +
45 + save = fp->_bf;
46 + fp->_bf._base = p;
47 + fp->_bf._size = resid;
48 + while (fp->_bf._size > 0) {
49 + if ((ret = __srefill1(fp)) != 0) {
50 + /* no more input: return partial result */
51 + resid = fp->_bf._size;
52 + fp->_bf = save;
53 + fp->_p = fp->_bf._base;
54 + /* fp->_r = 0; already set in __srefill1 */
55 + FUNLOCKFILE(fp);
56 + return ((total - resid) / size);
57 + }
58 + fp->_bf._base += fp->_r;
59 + fp->_bf._size -= fp->_r;
60 + }
61 + fp->_bf = save;
62 + n = fp->_bf._size * ((resid - 1) / fp->_bf._size);
63 + r = resid - n;
64 + (void)memcpy((void *)fp->_bf._base, (void *)(p + n), (size_t)r);
65 + fp->_p = fp->_bf._base + r;
66 + fp->_r = 0;
67 + } else {
68 + while (resid > (r = fp->_r)) {
69 + (void)memcpy((void *)p, (void *)fp->_p, (size_t)r);
70 + fp->_p += r;
71 + /* fp->_r = 0 ... done in __srefill */
72 + p += r;
73 + resid -= r;
74 + if (__srefill1(fp)) {
75 + /* no more input: return partial result */
76 + FUNLOCKFILE(fp);
77 + return ((total - resid) / size);
78 + }
79 + }
80 + (void)memcpy((void *)p, (void *)fp->_p, resid);
81 + fp->_r -= resid;
82 + fp->_p += resid;
83 + }
84 FUNLOCKFILE(fp);
85 return (count);
86 }