Rev 4921 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4921 | Rev 6536 | ||
---|---|---|---|
Line 102... | Line 102... | ||
102 | register int mode _AND |
102 | register int mode _AND |
103 | register size_t size) |
103 | register size_t size) |
104 | { |
104 | { |
105 | int ret = 0; |
105 | int ret = 0; |
106 | struct _reent *reent = _REENT; |
106 | struct _reent *reent = _REENT; |
- | 107 | size_t iosize; |
|
- | 108 | int ttyflag; |
|
Line 107... | Line 109... | ||
107 | 109 | ||
Line 108... | Line -... | ||
108 | CHECK_INIT (reent, fp); |
- | |
109 | - | ||
110 | _newlib_flockfile_start (fp); |
110 | CHECK_INIT (reent, fp); |
111 | 111 | ||
112 | /* |
112 | /* |
- | 113 | * Verify arguments. The `int' limit on `size' is due to this |
|
113 | * Verify arguments. The `int' limit on `size' is due to this |
114 | * particular implementation. Note, buf and size are ignored |
114 | * particular implementation. |
- | |
- | 115 | * when setting _IONBF. |
|
115 | */ |
116 | */ |
116 | - | ||
117 | if ((mode != _IOFBF && mode != _IOLBF && mode != _IONBF) || (int)(_POINTER_INT) size < 0) |
- | |
118 | { |
117 | if (mode != _IONBF) |
119 | _newlib_flockfile_exit (fp); |
118 | if ((mode != _IOFBF && mode != _IOLBF) || (int)(_POINTER_INT) size < 0) |
Line 120... | Line 119... | ||
120 | return (EOF); |
119 | return (EOF); |
121 | } |
120 | |
122 | 121 | ||
123 | /* |
122 | /* |
124 | * Write current buffer, if any; drop read count, if any. |
123 | * Write current buffer, if any; drop read count, if any. |
125 | * Make sure putc() will not think fp is line buffered. |
124 | * Make sure putc() will not think fp is line buffered. |
126 | * Free old buffer if it was from malloc(). Clear line and |
- | |
- | 125 | * Free old buffer if it was from malloc(). Clear line and |
|
127 | * non buffer flags, and clear malloc flag. |
126 | * non buffer flags, and clear malloc flag. |
128 | */ |
127 | */ |
- | 128 | _newlib_flockfile_start (fp); |
|
129 | 129 | _fflush_r (reent, fp); |
|
130 | _fflush_r (reent, fp); |
130 | if (HASUB(fp)) |
131 | fp->_r = 0; |
131 | FREEUB(reent, fp); |
132 | fp->_lbfsize = 0; |
132 | fp->_r = fp->_lbfsize = 0; |
Line 133... | Line 133... | ||
133 | if (fp->_flags & __SMBF) |
133 | if (fp->_flags & __SMBF) |
134 | _free_r (reent, (_PTR) fp->_bf._base); |
134 | _free_r (reent, (_PTR) fp->_bf._base); |
Line 135... | Line 135... | ||
135 | fp->_flags &= ~(__SLBF | __SNBF | __SMBF); |
135 | fp->_flags &= ~(__SLBF | __SNBF | __SMBF | __SOPT | __SNPT | __SEOF); |
- | 136 | ||
- | 137 | if (mode == _IONBF) |
|
- | 138 | goto nbf; |
|
- | 139 | ||
- | 140 | /* |
|
- | 141 | * Find optimal I/O size for seek optimization. This also returns |
|
- | 142 | * a `tty flag' to suggest that we check isatty(fd), but we do not |
|
- | 143 | * care since our caller told us how to buffer. |
|
- | 144 | */ |
|
- | 145 | fp->_flags |= __swhatbuf_r (reent, fp, &iosize, &ttyflag); |
|
- | 146 | if (size == 0) |
|
136 | 147 | { |
|
137 | if (mode == _IONBF) |
148 | buf = NULL; |
138 | goto nbf; |
149 | size = iosize; |
139 | - | ||
140 | /* |
- | |
141 | * Allocate buffer if needed. */ |
- | |
142 | if (buf == NULL) |
150 | } |
143 | { |
151 | |
- | 152 | /* Allocate buffer if needed. */ |
|
- | 153 | if (buf == NULL) |
|
- | 154 | { |
|
- | 155 | if ((buf = malloc (size)) == NULL) |
|
144 | /* we need this here because malloc() may return a pointer |
156 | { |
145 | even if size == 0 */ |
157 | /* |
- | 158 | * Unable to honor user's request. We will return |
|
- | 159 | * failure, but try again with file system size. |
|
146 | if (!size) size = BUFSIZ; |
160 | */ |
147 | if ((buf = malloc (size)) == NULL) |
161 | ret = EOF; |
148 | { |
162 | if (size != iosize) |
149 | ret = EOF; |
163 | { |
150 | /* Try another size... */ |
164 | size = iosize; |
151 | buf = malloc (BUFSIZ); |
165 | buf = malloc (size); |
152 | size = BUFSIZ; |
166 | } |
153 | } |
167 | } |
154 | if (buf == NULL) |
168 | if (buf == NULL) |
155 | { |
169 | { |
156 | /* Can't allocate it, let's try another approach */ |
170 | /* No luck; switch to unbuffered I/O. */ |
157 | nbf: |
171 | nbf: |
158 | fp->_flags |= __SNBF; |
172 | fp->_flags |= __SNBF; |
159 | fp->_w = 0; |
173 | fp->_w = 0; |
160 | fp->_bf._base = fp->_p = fp->_nbuf; |
174 | fp->_bf._base = fp->_p = fp->_nbuf; |
161 | fp->_bf._size = 1; |
175 | fp->_bf._size = 1; |
- | 176 | _newlib_flockfile_exit (fp); |
|
162 | _newlib_flockfile_exit (fp); |
177 | return (ret); |
163 | return (ret); |
- | |
164 | } |
178 | } |
165 | fp->_flags |= __SMBF; |
179 | fp->_flags |= __SMBF; |
166 | } |
- | |
167 | /* |
- | |
168 | * Now put back whichever flag is needed, and fix _lbfsize |
180 | } |
- | 181 | ||
- | 182 | /* |
|
Line -... | Line 183... | ||
- | 183 | * We're committed to buffering from here, so make sure we've |
|
- | 184 | * registered to flush buffers on exit. |
|
- | 185 | */ |
|
169 | * if line buffered. Ensure output flush on exit if the |
186 | if (!reent->__sdidinit) |
- | 187 | __sinit(reent); |
|
- | 188 | ||
170 | * stream will be buffered at all. |
189 | #ifdef _FSEEK_OPTIMIZATION |
171 | * If buf is NULL then make _lbfsize 0 to force the buffer |
190 | /* |
172 | * to be flushed and hence malloced on first use |
191 | * Kill any seek optimization if the buffer is not the |
173 | */ |
- | |
174 | 192 | * right size. |
|
Line -... | Line 193... | ||
- | 193 | * |
|
- | 194 | * SHOULD WE ALLOW MULTIPLES HERE (i.e., ok iff (size % iosize) == 0)? |
|
- | 195 | */ |
|
175 | switch (mode) |
196 | if (size != iosize) |
176 | { |
197 | fp->_flags |= __SNPT; |
177 | case _IOLBF: |
198 | #endif |
178 | fp->_flags |= __SLBF; |
199 | |
179 | fp->_lbfsize = buf ? -size : 0; |
200 | /* |
- | 201 | * Fix up the FILE fields, and set __cleanup for output flush on |
|
180 | /* FALLTHROUGH */ |
202 | * exit (since we are buffered in some way). |
181 | 203 | */ |
|
182 | case _IOFBF: |
- | |
183 | /* no flag */ |
204 | if (mode == _IOLBF) |
- | 205 | fp->_flags |= __SLBF; |
|
184 | reent->__cleanup = _cleanup_r; |
206 | fp->_bf._base = fp->_p = (unsigned char *) buf; |
185 | fp->_bf._base = fp->_p = (unsigned char *) buf; |
207 | fp->_bf._size = size; |
- | 208 | /* fp->_lbfsize is still 0 */ |
|
- | 209 | if (fp->_flags & __SWR) |
|
- | 210 | { |
|
- | 211 | /* |
|
186 | fp->_bf._size = size; |
212 | * Begin or continue writing: see __swsetup(). Note |
- | 213 | * that __SNBF is impossible (it was handled earlier). |
|
187 | break; |
214 | */ |
- | 215 | if (fp->_flags & __SLBF) |
|
- | 216 | { |
|
- | 217 | fp->_w = 0; |
|
188 | } |
218 | fp->_lbfsize = -fp->_bf._size; |
- | 219 | } |
|
- | 220 | else |
|
Line 189... | Line 221... | ||
189 | 221 | fp->_w = size; |
|
190 | /* |
222 | } |
191 | * Patch up write count if necessary. |
223 | else |