|
1 /* REFILL.C |
|
2 * |
|
3 * Portions Copyright (c) 1990-1999 Nokia Corporation and/or its subsidiary(-ies). |
|
4 * All rights reserved. |
|
5 */ |
|
6 |
|
7 /* No user fns here. Pesch 15apr92. */ |
|
8 |
|
9 /* |
|
10 * Copyright (c) 1990 The Regents of the University of California. |
|
11 * All rights reserved. |
|
12 * |
|
13 * Redistribution and use in source and binary forms are permitted |
|
14 * provided that the above copyright notice and this paragraph are |
|
15 * duplicated in all such forms and that any documentation, |
|
16 * advertising materials, and other materials related to such |
|
17 * distribution and use acknowledge that the software was developed |
|
18 * by the University of California, Berkeley. The name of the |
|
19 * University may not be used to endorse or promote products derived |
|
20 * from this software without specific prior written permission. |
|
21 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR |
|
22 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
|
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
|
24 */ |
|
25 |
|
26 #include <stdio_r.h> |
|
27 #include "LOCAL.H" |
|
28 #include <stdlib_r.h> |
|
29 |
|
30 static int |
|
31 lflush (FILE *fp) |
|
32 { |
|
33 if ((fp->_flags & (__SLBF | __SWR)) == (__SLBF | __SWR)) |
|
34 return fflush (fp); |
|
35 return 0; |
|
36 } |
|
37 |
|
38 /* |
|
39 * Refill a stdio buffer. |
|
40 * Return EOF on eof or error, 0 otherwise. |
|
41 */ |
|
42 |
|
43 int |
|
44 __srefill (register FILE * fp) |
|
45 { |
|
46 /* make sure stdio is set up */ |
|
47 |
|
48 CHECK_INIT (fp); |
|
49 |
|
50 fp->_r = 0; /* largely a convenience for callers */ |
|
51 |
|
52 /* SysV does not make this test; take it out for compatibility */ |
|
53 if (fp->_flags & __SEOF) |
|
54 return EOF; |
|
55 |
|
56 /* if not already reading, have to be reading and writing */ |
|
57 if ((fp->_flags & __SRD) == 0) |
|
58 { |
|
59 if ((fp->_flags & __SRW) == 0) |
|
60 return EOF; |
|
61 /* switch to reading */ |
|
62 if (fp->_flags & __SWR) |
|
63 { |
|
64 if (fflush (fp)) |
|
65 return EOF; |
|
66 fp->_flags &= ~__SWR; |
|
67 fp->_w = 0; |
|
68 fp->_lbfsize = 0; |
|
69 } |
|
70 fp->_flags |= __SRD; |
|
71 } |
|
72 else |
|
73 { |
|
74 /* |
|
75 * We were reading. If there is an ungetc buffer, |
|
76 * we must have been reading from that. Drop it, |
|
77 * restoring the previous buffer (if any). If there |
|
78 * is anything in that buffer, return. |
|
79 */ |
|
80 if (HASUB (fp)) |
|
81 { |
|
82 FREEUB (fp); |
|
83 if ((fp->_r = fp->_ur) != 0) |
|
84 { |
|
85 fp->_p = fp->_up; |
|
86 return 0; |
|
87 } |
|
88 } |
|
89 } |
|
90 |
|
91 if (fp->_bf._base == NULL) |
|
92 __smakebuf (fp); |
|
93 |
|
94 /* |
|
95 * Before reading from a line buffered or unbuffered file, |
|
96 * flush all line buffered output files, per the ANSI C |
|
97 * standard. |
|
98 */ |
|
99 |
|
100 if (fp->_flags & (__SLBF | __SNBF)) |
|
101 (void) _fwalk (fp->_data, lflush); |
|
102 fp->_p = fp->_bf._base; |
|
103 fp->_r = (*fp->_read) (fp->_cookie, (char *) fp->_p, fp->_bf._size); |
|
104 fp->_flags &= ~__SMOD; /* buffer contents are again pristine */ |
|
105 if (fp->_r <= 0) |
|
106 { |
|
107 if (fp->_r == 0) |
|
108 fp->_flags |= __SEOF; |
|
109 else |
|
110 { |
|
111 fp->_r = 0; |
|
112 fp->_flags |= __SERR; |
|
113 } |
|
114 return EOF; |
|
115 } |
|
116 return 0; |
|
117 } |