]>
Commit | Line | Data |
---|---|---|
23396375 | 1 | /* |
96aa2d94 RM |
2 | Copyright (C) 1993, 1995 Free Software Foundation |
3 | ||
4 | This file is part of the GNU IO Library. This library is free | |
5 | software; you can redistribute it and/or modify it under the | |
6 | terms of the GNU General Public License as published by the | |
7 | Free Software Foundation; either version 2, or (at your option) | |
8 | any later version. | |
9 | ||
10 | This library is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | GNU General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU General Public License | |
16 | along with this library; see the file COPYING. If not, write to the Free | |
17 | Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
18 | ||
19 | As a special exception, if you link this library with files | |
20 | compiled with a GNU compiler to produce an executable, this does not cause | |
21 | the resulting executable to be covered by the GNU General Public License. | |
22 | This exception does not however invalidate any other reasons why | |
23 | the executable file might be covered by the GNU General Public License. */ | |
24 | ||
25 | /* written by Per Bothner (bothner@cygnus.com) */ | |
26 | ||
fa0bc87c RM |
27 | #ifndef _POSIX_SOURCE |
28 | # define _POSIX_SOURCE | |
29 | #endif | |
96aa2d94 RM |
30 | #include "libioP.h" |
31 | #include <fcntl.h> | |
32 | #include <sys/types.h> | |
33 | #include <sys/stat.h> | |
34 | #include <string.h> | |
35 | #include <errno.h> | |
36 | #ifndef errno | |
37 | extern int errno; | |
38 | #endif | |
39 | ||
68dbb3a6 UD |
40 | |
41 | #ifdef _LIBC | |
42 | # define open(Name, Flags, Prot) __open ((Name), (Flags), (Prot)) | |
43 | #endif | |
44 | ||
96aa2d94 RM |
45 | /* An fstream can be in at most one of put mode, get mode, or putback mode. |
46 | Putback mode is a variant of get mode. | |
47 | ||
48 | In a filebuf, there is only one current position, instead of two | |
6d52618b | 49 | separate get and put pointers. In get mode, the current position |
96aa2d94 RM |
50 | is that of gptr(); in put mode that of pptr(). |
51 | ||
52 | The position in the buffer that corresponds to the position | |
a18f587d UD |
53 | in external file system is normally _IO_read_end, except in putback |
54 | mode, when it is _IO_save_end. | |
96aa2d94 RM |
55 | If the field _fb._offset is >= 0, it gives the offset in |
56 | the file as a whole corresponding to eGptr(). (?) | |
57 | ||
58 | PUT MODE: | |
a18f587d UD |
59 | If a filebuf is in put mode, then all of _IO_read_ptr, _IO_read_end, |
60 | and _IO_read_base are equal to each other. These are usually equal | |
61 | to _IO_buf_base, though not necessarily if we have switched from | |
62 | get mode to put mode. (The reason is to maintain the invariant | |
63 | that _IO_read_end corresponds to the external file position.) | |
64 | _IO_write_base is non-NULL and usually equal to _IO_base_base. | |
65 | We also have _IO_write_end == _IO_buf_end, but only in fully buffered mode. | |
66 | The un-flushed character are those between _IO_write_base and _IO_write_ptr. | |
67 | ||
96aa2d94 RM |
68 | GET MODE: |
69 | If a filebuf is in get or putback mode, eback() != egptr(). | |
70 | In get mode, the unread characters are between gptr() and egptr(). | |
71 | The OS file position corresponds to that of egptr(). | |
a18f587d | 72 | |
96aa2d94 RM |
73 | PUTBACK MODE: |
74 | Putback mode is used to remember "excess" characters that have | |
75 | been sputbackc'd in a separate putback buffer. | |
76 | In putback mode, the get buffer points to the special putback buffer. | |
77 | The unread characters are the characters between gptr() and egptr() | |
78 | in the putback buffer, as well as the area between save_gptr() | |
79 | and save_egptr(), which point into the original reserve buffer. | |
80 | (The pointers save_gptr() and save_egptr() are the values | |
81 | of gptr() and egptr() at the time putback mode was entered.) | |
82 | The OS position corresponds to that of save_egptr(). | |
23396375 | 83 | |
96aa2d94 | 84 | LINE BUFFERED OUTPUT: |
a18f587d | 85 | During line buffered output, _IO_write_base==base() && epptr()==base(). |
96aa2d94 RM |
86 | However, ptr() may be anywhere between base() and ebuf(). |
87 | This forces a call to filebuf::overflow(int C) on every put. | |
88 | If there is more space in the buffer, and C is not a '\n', | |
89 | then C is inserted, and pptr() incremented. | |
23396375 | 90 | |
96aa2d94 RM |
91 | UNBUFFERED STREAMS: |
92 | If a filebuf is unbuffered(), the _shortbuf[1] is used as the buffer. | |
93 | */ | |
94 | ||
95 | #define CLOSED_FILEBUF_FLAGS \ | |
96 | (_IO_IS_FILEBUF+_IO_NO_READS+_IO_NO_WRITES+_IO_TIED_PUT_GET) | |
97 | ||
98 | ||
99 | void | |
100 | DEFUN(_IO_file_init, (fp), | |
101 | register _IO_FILE *fp) | |
102 | { | |
103 | /* POSIX.1 allows another file handle to be used to change the position | |
104 | of our file descriptor. Hence we actually don't know the actual | |
105 | position before we do the first fseek (and until a following fflush). */ | |
106 | fp->_offset = _IO_pos_BAD; | |
107 | fp->_IO_file_flags |= CLOSED_FILEBUF_FLAGS; | |
108 | ||
109 | _IO_link_in(fp); | |
110 | fp->_fileno = -1; | |
111 | } | |
112 | ||
113 | int | |
114 | DEFUN(_IO_file_close_it, (fp), | |
115 | register _IO_FILE* fp) | |
116 | { | |
fa0bc87c | 117 | int write_status, close_status; |
96aa2d94 RM |
118 | if (!_IO_file_is_open(fp)) |
119 | return EOF; | |
120 | ||
fa0bc87c | 121 | write_status = _IO_do_flush (fp); |
96aa2d94 RM |
122 | |
123 | _IO_unsave_markers(fp); | |
124 | ||
125 | close_status = _IO_SYSCLOSE (fp); | |
126 | ||
127 | /* Free buffer. */ | |
128 | _IO_setb(fp, NULL, NULL, 0); | |
129 | _IO_setg(fp, NULL, NULL, NULL); | |
130 | _IO_setp(fp, NULL, NULL); | |
131 | ||
132 | _IO_un_link(fp); | |
133 | fp->_flags = _IO_MAGIC|CLOSED_FILEBUF_FLAGS; | |
134 | fp->_fileno = EOF; | |
135 | fp->_offset = _IO_pos_BAD; | |
136 | ||
fa0bc87c | 137 | return close_status ? close_status : write_status; |
96aa2d94 RM |
138 | } |
139 | ||
140 | void | |
ceb2d9aa UD |
141 | DEFUN(_IO_file_finish, (fp, dummy), |
142 | register _IO_FILE* fp AND int dummy) | |
96aa2d94 RM |
143 | { |
144 | if (_IO_file_is_open(fp)) | |
145 | { | |
146 | _IO_do_flush (fp); | |
147 | if (!(fp->_flags & _IO_DELETE_DONT_CLOSE)) | |
148 | _IO_SYSCLOSE (fp); | |
149 | } | |
ceb2d9aa | 150 | _IO_default_finish(fp, 0); |
96aa2d94 RM |
151 | } |
152 | ||
153 | _IO_FILE * | |
154 | DEFUN(_IO_file_fopen, (fp, filename, mode), | |
155 | register _IO_FILE *fp AND const char *filename AND const char *mode) | |
156 | { | |
157 | int oflags = 0, omode; | |
158 | int read_write, fdesc; | |
159 | int oprot = 0666; | |
160 | if (_IO_file_is_open (fp)) | |
161 | return 0; | |
162 | switch (*mode++) { | |
163 | case 'r': | |
164 | omode = O_RDONLY; | |
165 | read_write = _IO_NO_WRITES; | |
166 | break; | |
167 | case 'w': | |
168 | omode = O_WRONLY; | |
169 | oflags = O_CREAT|O_TRUNC; | |
170 | read_write = _IO_NO_READS; | |
171 | break; | |
172 | case 'a': | |
173 | omode = O_WRONLY; | |
174 | oflags = O_CREAT|O_APPEND; | |
175 | read_write = _IO_NO_READS|_IO_IS_APPENDING; | |
176 | break; | |
177 | default: | |
c4029823 | 178 | __set_errno (EINVAL); |
96aa2d94 RM |
179 | return NULL; |
180 | } | |
181 | if (mode[0] == '+' || (mode[0] == 'b' && mode[1] == '+')) { | |
182 | omode = O_RDWR; | |
183 | read_write &= _IO_IS_APPENDING; | |
184 | } | |
68dbb3a6 | 185 | fdesc = open(filename, omode|oflags, oprot); |
96aa2d94 RM |
186 | if (fdesc < 0) |
187 | return NULL; | |
188 | fp->_fileno = fdesc; | |
189 | _IO_mask_flags(fp, read_write,_IO_NO_READS+_IO_NO_WRITES+_IO_IS_APPENDING); | |
190 | if (read_write & _IO_IS_APPENDING) | |
191 | if (_IO_SEEKOFF (fp, (_IO_off_t)0, _IO_seek_end, _IOS_INPUT|_IOS_OUTPUT) | |
f65fd747 | 192 | == _IO_pos_BAD && errno != ESPIPE) |
96aa2d94 RM |
193 | return NULL; |
194 | _IO_link_in(fp); | |
195 | return fp; | |
196 | } | |
197 | ||
198 | _IO_FILE* | |
199 | DEFUN(_IO_file_attach, (fp, fd), | |
200 | _IO_FILE *fp AND int fd) | |
201 | { | |
202 | if (_IO_file_is_open(fp)) | |
203 | return NULL; | |
204 | fp->_fileno = fd; | |
205 | fp->_flags &= ~(_IO_NO_READS+_IO_NO_WRITES); | |
206 | fp->_flags |= _IO_DELETE_DONT_CLOSE; | |
f65fd747 UD |
207 | /* Get the current position of the file. */ |
208 | /* We have to do that since that may be junk. */ | |
96aa2d94 | 209 | fp->_offset = _IO_pos_BAD; |
f65fd747 UD |
210 | if (_IO_SEEKOFF (fp, (_IO_off_t)0, _IO_seek_cur, _IOS_INPUT|_IOS_OUTPUT) |
211 | == _IO_pos_BAD && errno != ESPIPE) | |
212 | return NULL; | |
96aa2d94 RM |
213 | return fp; |
214 | } | |
215 | ||
216 | _IO_FILE* | |
217 | DEFUN(_IO_file_setbuf, (fp, p, len), | |
218 | register _IO_FILE *fp AND char* p AND _IO_ssize_t len) | |
219 | { | |
220 | if (_IO_default_setbuf(fp, p, len) == NULL) | |
221 | return NULL; | |
222 | ||
223 | fp->_IO_write_base = fp->_IO_write_ptr = fp->_IO_write_end | |
224 | = fp->_IO_buf_base; | |
225 | _IO_setg(fp, fp->_IO_buf_base, fp->_IO_buf_base, fp->_IO_buf_base); | |
226 | ||
227 | return fp; | |
228 | } | |
229 | ||
230 | /* Write TO_DO bytes from DATA to FP. | |
68dbb3a6 | 231 | Then mark FP as having empty buffers. */ |
96aa2d94 RM |
232 | |
233 | int | |
234 | DEFUN(_IO_do_write, (fp, data, to_do), | |
235 | register _IO_FILE *fp AND const char* data AND _IO_size_t to_do) | |
236 | { | |
237 | _IO_size_t count; | |
238 | if (to_do == 0) | |
239 | return 0; | |
240 | if (fp->_flags & _IO_IS_APPENDING) | |
241 | /* On a system without a proper O_APPEND implementation, | |
242 | you would need to sys_seek(0, SEEK_END) here, but is | |
243 | is not needed nor desirable for Unix- or Posix-like systems. | |
244 | Instead, just indicate that offset (before and after) is | |
245 | unpredictable. */ | |
246 | fp->_offset = _IO_pos_BAD; | |
247 | else if (fp->_IO_read_end != fp->_IO_write_base) | |
23396375 | 248 | { |
96aa2d94 RM |
249 | _IO_pos_t new_pos |
250 | = _IO_SYSSEEK(fp, fp->_IO_write_base - fp->_IO_read_end, 1); | |
251 | if (new_pos == _IO_pos_BAD) | |
252 | return EOF; | |
253 | fp->_offset = new_pos; | |
254 | } | |
255 | count = _IO_SYSWRITE (fp, data, to_do); | |
256 | if (fp->_cur_column) | |
257 | fp->_cur_column = _IO_adjust_column(fp->_cur_column - 1, data, to_do) + 1; | |
258 | _IO_setg(fp, fp->_IO_buf_base, fp->_IO_buf_base, fp->_IO_buf_base); | |
259 | fp->_IO_write_base = fp->_IO_write_ptr = fp->_IO_buf_base; | |
260 | fp->_IO_write_end = (fp->_flags & (_IO_LINE_BUF+_IO_UNBUFFERED)) ? fp->_IO_buf_base | |
261 | : fp->_IO_buf_end; | |
262 | return count != to_do ? EOF : 0; | |
263 | } | |
264 | ||
265 | int | |
266 | DEFUN(_IO_file_underflow, (fp), | |
267 | register _IO_FILE *fp) | |
268 | { | |
269 | _IO_ssize_t count; | |
270 | #if 0 | |
271 | /* SysV does not make this test; take it out for compatibility */ | |
272 | if (fp->_flags & _IO_EOF_SEEN) | |
273 | return (EOF); | |
274 | #endif | |
275 | ||
276 | if (fp->_flags & _IO_NO_READS) | |
feb3c934 UD |
277 | { |
278 | __set_errno (EBADF); | |
279 | return EOF; | |
280 | } | |
96aa2d94 RM |
281 | if (fp->_IO_read_ptr < fp->_IO_read_end) |
282 | return *(unsigned char*)fp->_IO_read_ptr; | |
283 | ||
284 | if (fp->_IO_buf_base == NULL) | |
285 | _IO_doallocbuf(fp); | |
286 | ||
287 | /* Flush all line buffered files before reading. */ | |
288 | /* FIXME This can/should be moved to genops ?? */ | |
289 | if (fp->_flags & (_IO_LINE_BUF|_IO_UNBUFFERED)) | |
290 | _IO_flush_all_linebuffered(); | |
291 | ||
292 | _IO_switch_to_get_mode(fp); | |
293 | ||
294 | count = _IO_SYSREAD (fp, fp->_IO_buf_base, | |
295 | fp->_IO_buf_end - fp->_IO_buf_base); | |
296 | if (count <= 0) | |
297 | { | |
298 | if (count == 0) | |
299 | fp->_flags |= _IO_EOF_SEEN; | |
300 | else | |
301 | fp->_flags |= _IO_ERR_SEEN, count = 0; | |
302 | } | |
303 | fp->_IO_read_base = fp->_IO_read_ptr = fp->_IO_buf_base; | |
304 | fp->_IO_read_end = fp->_IO_buf_base + count; | |
305 | fp->_IO_write_base = fp->_IO_write_ptr = fp->_IO_write_end | |
306 | = fp->_IO_buf_base; | |
307 | if (count == 0) | |
308 | return EOF; | |
309 | if (fp->_offset != _IO_pos_BAD) | |
310 | _IO_pos_adjust(fp->_offset, count); | |
311 | return *(unsigned char*)fp->_IO_read_ptr; | |
312 | } | |
313 | ||
314 | int | |
315 | DEFUN(_IO_file_overflow, (f, ch), | |
316 | register _IO_FILE* f AND int ch) | |
317 | { | |
318 | if (f->_flags & _IO_NO_WRITES) /* SET ERROR */ | |
feb3c934 | 319 | { |
c131718c | 320 | f->_flags |= _IO_ERR_SEEN; |
feb3c934 UD |
321 | __set_errno (EBADF); |
322 | return EOF; | |
323 | } | |
96aa2d94 RM |
324 | /* If currently reading or no buffer allocated. */ |
325 | if ((f->_flags & _IO_CURRENTLY_PUTTING) == 0) | |
326 | { | |
327 | /* Allocate a buffer if needed. */ | |
328 | if (f->_IO_write_base == 0) | |
329 | { | |
330 | _IO_doallocbuf(f); | |
331 | _IO_setg (f, f->_IO_buf_base, f->_IO_buf_base, f->_IO_buf_base); | |
332 | } | |
c6645251 UD |
333 | /* Otherwise must be currently reading. |
334 | If _IO_read_ptr (and hence also _IO_read_end) is at the buffer end, | |
335 | logically slide the buffer forwards one block (by setting the | |
336 | read pointers to all point at the beginning of the block). This | |
337 | makes room for subsequent output. | |
338 | Otherwise, set the read pointers to _IO_read_end (leaving that | |
339 | alone, so it can continue to correspond to the external position). */ | |
340 | if (f->_IO_read_ptr == f->_IO_buf_end) | |
341 | f->_IO_read_end = f->_IO_read_ptr = f->_IO_buf_base; | |
96aa2d94 RM |
342 | f->_IO_write_ptr = f->_IO_read_ptr; |
343 | f->_IO_write_base = f->_IO_write_ptr; | |
344 | f->_IO_write_end = f->_IO_buf_end; | |
345 | f->_IO_read_base = f->_IO_read_ptr = f->_IO_read_end; | |
346 | ||
347 | if (f->_flags & (_IO_LINE_BUF+_IO_UNBUFFERED)) | |
348 | f->_IO_write_end = f->_IO_write_ptr; | |
349 | f->_flags |= _IO_CURRENTLY_PUTTING; | |
350 | } | |
351 | if (ch == EOF) | |
352 | return _IO_do_flush(f); | |
353 | if (f->_IO_write_ptr == f->_IO_buf_end ) /* Buffer is really full */ | |
354 | if (_IO_do_flush(f) == EOF) | |
355 | return EOF; | |
356 | *f->_IO_write_ptr++ = ch; | |
357 | if ((f->_flags & _IO_UNBUFFERED) | |
358 | || ((f->_flags & _IO_LINE_BUF) && ch == '\n')) | |
359 | if (_IO_do_flush(f) == EOF) | |
360 | return EOF; | |
361 | return (unsigned char)ch; | |
362 | } | |
363 | ||
364 | int | |
365 | DEFUN(_IO_file_sync, (fp), | |
366 | register _IO_FILE* fp) | |
367 | { | |
368 | _IO_size_t delta; | |
369 | /* char* ptr = cur_ptr(); */ | |
370 | if (fp->_IO_write_ptr > fp->_IO_write_base) | |
371 | if (_IO_do_flush(fp)) return EOF; | |
23396375 | 372 | delta = fp->_IO_read_ptr - fp->_IO_read_end; |
96aa2d94 RM |
373 | if (delta != 0) |
374 | { | |
375 | #ifdef TODO | |
376 | if (_IO_in_backup(fp)) | |
377 | delta -= eGptr() - Gbase(); | |
378 | #endif | |
379 | _IO_off_t new_pos = _IO_SYSSEEK (fp, delta, 1); | |
380 | if (new_pos != (_IO_off_t)EOF) | |
381 | fp->_IO_read_end = fp->_IO_read_ptr; | |
382 | #ifdef ESPIPE | |
383 | else if (errno == ESPIPE) | |
384 | ; /* Ignore error from unseekable devices. */ | |
385 | #endif | |
386 | else | |
387 | return EOF; | |
388 | } | |
389 | fp->_offset = _IO_pos_BAD; | |
390 | /* FIXME: Cleanup - can this be shared? */ | |
391 | /* setg(base(), ptr, ptr); */ | |
392 | return 0; | |
393 | } | |
394 | ||
395 | _IO_pos_t | |
396 | DEFUN(_IO_file_seekoff, (fp, offset, dir, mode), | |
397 | register _IO_FILE *fp AND _IO_off_t offset AND int dir AND int mode) | |
398 | { | |
399 | _IO_pos_t result; | |
400 | _IO_off_t delta, new_offset; | |
401 | long count; | |
feb3c934 UD |
402 | /* POSIX.1 8.2.3.7 says that after a call the fflush() the file |
403 | offset of the underlying file must be exact. */ | |
404 | int must_be_exact = (fp->_IO_read_base == fp->_IO_read_end | |
405 | && fp->_IO_write_base == fp->_IO_write_ptr); | |
96aa2d94 RM |
406 | |
407 | if (mode == 0) | |
408 | dir = _IO_seek_cur, offset = 0; /* Don't move any pointers. */ | |
409 | ||
410 | /* Flush unwritten characters. | |
411 | (This may do an unneeded write if we seek within the buffer. | |
412 | But to be able to switch to reading, we would need to set | |
413 | egptr to ptr. That can't be done in the current design, | |
414 | which assumes file_ptr() is eGptr. Anyway, since we probably | |
415 | end up flushing when we close(), it doesn't make much difference.) | |
416 | FIXME: simulate mem-papped files. */ | |
417 | ||
418 | if (fp->_IO_write_ptr > fp->_IO_write_base || _IO_in_put_mode(fp)) | |
419 | if (_IO_switch_to_get_mode(fp)) return EOF; | |
420 | ||
421 | if (fp->_IO_buf_base == NULL) | |
422 | { | |
423 | _IO_doallocbuf(fp); | |
424 | _IO_setp(fp, fp->_IO_buf_base, fp->_IO_buf_base); | |
425 | _IO_setg(fp, fp->_IO_buf_base, fp->_IO_buf_base, fp->_IO_buf_base); | |
426 | } | |
427 | ||
428 | switch (dir) | |
429 | { | |
430 | case _IO_seek_cur: | |
431 | /* Adjust for read-ahead (bytes is buffer). */ | |
432 | offset -= fp->_IO_read_end - fp->_IO_read_ptr; | |
433 | if (fp->_offset == _IO_pos_BAD) | |
434 | goto dumb; | |
435 | /* Make offset absolute, assuming current pointer is file_ptr(). */ | |
436 | offset += _IO_pos_as_off(fp->_offset); | |
437 | ||
438 | dir = _IO_seek_set; | |
439 | break; | |
440 | case _IO_seek_set: | |
441 | break; | |
442 | case _IO_seek_end: | |
443 | { | |
444 | struct stat st; | |
445 | if (_IO_SYSSTAT (fp, &st) == 0 && S_ISREG(st.st_mode)) | |
446 | { | |
447 | offset += st.st_size; | |
448 | dir = _IO_seek_set; | |
449 | } | |
450 | else | |
451 | goto dumb; | |
452 | } | |
453 | } | |
454 | /* At this point, dir==_IO_seek_set. */ | |
455 | ||
456 | /* If destination is within current buffer, optimize: */ | |
457 | if (fp->_offset != _IO_pos_BAD && fp->_IO_read_base != NULL | |
458 | && !_IO_in_backup (fp)) | |
459 | { | |
460 | /* Offset relative to start of main get area. */ | |
461 | _IO_pos_t rel_offset = offset - fp->_offset | |
462 | + (fp->_IO_read_end - fp->_IO_read_base); | |
463 | if (rel_offset >= 0) | |
464 | { | |
465 | #if 0 | |
466 | if (_IO_in_backup(fp)) | |
467 | _IO_switch_to_main_get_area(fp); | |
468 | #endif | |
469 | if (rel_offset <= fp->_IO_read_end - fp->_IO_read_base) | |
470 | { | |
471 | _IO_setg(fp, fp->_IO_buf_base, fp->_IO_buf_base + rel_offset, | |
472 | fp->_IO_read_end); | |
473 | _IO_setp(fp, fp->_IO_buf_base, fp->_IO_buf_base); | |
474 | return offset; | |
475 | } | |
476 | #ifdef TODO | |
477 | /* If we have streammarkers, seek forward by reading ahead. */ | |
478 | if (_IO_have_markers(fp)) | |
479 | { | |
480 | int to_skip = rel_offset | |
481 | - (fp->_IO_read_ptr - fp->_IO_read_base); | |
482 | if (ignore(to_skip) != to_skip) | |
483 | goto dumb; | |
484 | return offset; | |
485 | } | |
486 | #endif | |
487 | } | |
488 | #ifdef TODO | |
489 | if (rel_offset < 0 && rel_offset >= Bbase() - Bptr()) | |
490 | { | |
491 | if (!_IO_in_backup(fp)) | |
492 | _IO_switch_to_backup_area(fp); | |
493 | gbump(fp->_IO_read_end + rel_offset - fp->_IO_read_ptr); | |
494 | return offset; | |
495 | } | |
496 | #endif | |
497 | } | |
498 | ||
499 | #ifdef TODO | |
500 | _IO_unsave_markers(fp); | |
501 | #endif | |
502 | ||
503 | if (fp->_flags & _IO_NO_READS) | |
504 | goto dumb; | |
505 | ||
506 | /* Try to seek to a block boundary, to improve kernel page management. */ | |
507 | new_offset = offset & ~(fp->_IO_buf_end - fp->_IO_buf_base - 1); | |
508 | delta = offset - new_offset; | |
509 | if (delta > fp->_IO_buf_end - fp->_IO_buf_base) | |
510 | { | |
511 | new_offset = offset; | |
512 | delta = 0; | |
513 | } | |
514 | result = _IO_SYSSEEK (fp, new_offset, 0); | |
515 | if (result < 0) | |
516 | return EOF; | |
517 | if (delta == 0) | |
518 | count = 0; | |
519 | else | |
520 | { | |
521 | count = _IO_SYSREAD (fp, fp->_IO_buf_base, | |
feb3c934 UD |
522 | (must_be_exact |
523 | ? delta : fp->_IO_buf_end - fp->_IO_buf_base)); | |
96aa2d94 RM |
524 | if (count < delta) |
525 | { | |
526 | /* We weren't allowed to read, but try to seek the remainder. */ | |
527 | offset = count == EOF ? delta : delta-count; | |
528 | dir = _IO_seek_cur; | |
529 | goto dumb; | |
530 | } | |
531 | } | |
532 | _IO_setg(fp, fp->_IO_buf_base, fp->_IO_buf_base+delta, fp->_IO_buf_base+count); | |
533 | _IO_setp(fp, fp->_IO_buf_base, fp->_IO_buf_base); | |
534 | fp->_offset = result + count; | |
535 | _IO_mask_flags(fp, 0, _IO_EOF_SEEN); | |
536 | return offset; | |
537 | dumb: | |
538 | ||
539 | _IO_unsave_markers(fp); | |
540 | result = _IO_SYSSEEK (fp, offset, dir); | |
541 | if (result != EOF) | |
542 | _IO_mask_flags(fp, 0, _IO_EOF_SEEN); | |
543 | fp->_offset = result; | |
544 | _IO_setg(fp, fp->_IO_buf_base, fp->_IO_buf_base, fp->_IO_buf_base); | |
545 | _IO_setp(fp, fp->_IO_buf_base, fp->_IO_buf_base); | |
546 | return result; | |
547 | } | |
548 | ||
549 | _IO_ssize_t | |
550 | DEFUN(_IO_file_read, (fp, buf, size), | |
551 | register _IO_FILE* fp AND void* buf AND _IO_ssize_t size) | |
552 | { | |
553 | for (;;) | |
554 | { | |
555 | _IO_ssize_t count = _IO_read(fp->_fileno, buf, size); | |
f8b87ef0 | 556 | #if 0 && defined EINTR |
68dbb3a6 UD |
557 | /* We must not do this optimization since POSIX.1 explicitly |
558 | requests that the stream operations must return with the | |
559 | error EINTR if this happens. There must be the possibility | |
560 | that stream operations time out. --drepper */ | |
96aa2d94 RM |
561 | if (count == -1 && errno == EINTR) |
562 | continue; | |
563 | #endif | |
564 | return count; | |
565 | } | |
566 | } | |
567 | ||
568 | _IO_pos_t | |
569 | DEFUN(_IO_file_seek, (fp, offset, dir), | |
570 | _IO_FILE *fp AND _IO_off_t offset AND int dir) | |
571 | { | |
572 | return _IO_lseek(fp->_fileno, offset, dir); | |
573 | } | |
574 | ||
575 | int | |
576 | DEFUN(_IO_file_stat, (fp, st), | |
577 | _IO_FILE *fp AND void* st) | |
578 | { | |
579 | return _IO_fstat(fp->_fileno, (struct stat*)st); | |
580 | } | |
581 | ||
582 | int | |
583 | DEFUN(_IO_file_close, (fp), | |
584 | _IO_FILE* fp) | |
585 | { | |
586 | return _IO_close(fp->_fileno); | |
587 | } | |
588 | ||
589 | _IO_ssize_t | |
590 | DEFUN(_IO_file_write, (f, data, n), | |
591 | register _IO_FILE* f AND const void* data AND _IO_ssize_t n) | |
592 | { | |
593 | _IO_ssize_t to_do = n; | |
594 | while (to_do > 0) | |
595 | { | |
596 | _IO_ssize_t count = _IO_write(f->_fileno, data, to_do); | |
597 | if (count == EOF) | |
598 | { | |
f8b87ef0 | 599 | #if 0 && defined EINTR |
68dbb3a6 UD |
600 | /* We must not do this optimization since POSIX.1 explicitly |
601 | requests that the stream operations must return with the | |
602 | error EINTR if this happens. There must be the | |
603 | possibility that stream operations time out. --drepper */ | |
96aa2d94 RM |
604 | if (errno == EINTR) |
605 | continue; | |
606 | else | |
607 | #endif | |
608 | { | |
609 | f->_flags |= _IO_ERR_SEEN; | |
610 | break; | |
611 | } | |
612 | } | |
613 | to_do -= count; | |
614 | data = (void*)((char*)data + count); | |
615 | } | |
616 | n -= to_do; | |
617 | if (f->_offset >= 0) | |
618 | f->_offset += n; | |
619 | return n; | |
620 | } | |
621 | ||
622 | _IO_size_t | |
623 | DEFUN(_IO_file_xsputn, (f, data, n), | |
624 | _IO_FILE *f AND const void *data AND _IO_size_t n) | |
625 | { | |
626 | register const char *s = (char*) data; | |
627 | _IO_size_t to_do = n; | |
628 | int must_flush = 0; | |
629 | _IO_size_t count; | |
630 | ||
631 | if (n <= 0) | |
632 | return 0; | |
633 | /* This is an optimized implementation. | |
634 | If the amount to be written straddles a block boundary | |
635 | (or the filebuf is unbuffered), use sys_write directly. */ | |
636 | ||
637 | /* First figure out how much space is available in the buffer. */ | |
638 | count = f->_IO_write_end - f->_IO_write_ptr; /* Space available. */ | |
639 | if ((f->_flags & _IO_LINE_BUF) && (f->_flags & _IO_CURRENTLY_PUTTING)) | |
640 | { | |
641 | count = f->_IO_buf_end - f->_IO_write_ptr; | |
642 | if (count >= n) | |
643 | { register const char *p; | |
644 | for (p = s + n; p > s; ) | |
645 | { | |
646 | if (*--p == '\n') { | |
647 | count = p - s + 1; | |
648 | must_flush = 1; | |
649 | break; | |
650 | } | |
651 | } | |
652 | } | |
653 | } | |
654 | /* Then fill the buffer. */ | |
655 | if (count > 0) | |
656 | { | |
657 | if (count > to_do) | |
658 | count = to_do; | |
659 | if (count > 20) { | |
660 | memcpy(f->_IO_write_ptr, s, count); | |
661 | s += count; | |
662 | } | |
663 | else | |
664 | { | |
665 | register char *p = f->_IO_write_ptr; | |
666 | register int i = (int)count; | |
667 | while (--i >= 0) *p++ = *s++; | |
668 | } | |
669 | f->_IO_write_ptr += count; | |
670 | to_do -= count; | |
671 | } | |
672 | if (to_do + must_flush > 0) | |
673 | { _IO_size_t block_size, dont_write; | |
674 | /* Next flush the (full) buffer. */ | |
675 | if (__overflow(f, EOF) == EOF) | |
676 | return n - to_do; | |
677 | ||
678 | /* Try to maintain alignment: write a whole number of blocks. | |
679 | dont_write is what gets left over. */ | |
680 | block_size = f->_IO_buf_end - f->_IO_buf_base; | |
681 | dont_write = block_size >= 128 ? to_do % block_size : 0; | |
682 | ||
683 | count = to_do - dont_write; | |
684 | if (_IO_do_write(f, s, count) == EOF) | |
685 | return n - to_do; | |
686 | to_do = dont_write; | |
23396375 | 687 | |
96aa2d94 RM |
688 | /* Now write out the remainder. Normally, this will fit in the |
689 | buffer, but it's somewhat messier for line-buffered files, | |
690 | so we let _IO_default_xsputn handle the general case. */ | |
691 | if (dont_write) | |
692 | to_do -= _IO_default_xsputn(f, s+count, dont_write); | |
693 | } | |
694 | return n - to_do; | |
695 | } | |
696 | ||
697 | #if 0 | |
698 | /* Work in progress */ | |
699 | _IO_size_t | |
700 | DEFUN(_IO_file_xsgetn, (fp, data, n), | |
701 | _IO_FILE *fp AND void *data AND _IO_size_t n) | |
702 | { | |
703 | register _IO_size_t more = n; | |
704 | register char *s = data; | |
705 | for (;;) | |
706 | { | |
707 | _IO_ssize_t count = fp->_IO_read_end - fp->_IO_read_ptr; /* Data available. */ | |
708 | if (count > 0) | |
709 | { | |
710 | if (count > more) | |
711 | count = more; | |
712 | if (count > 20) | |
713 | { | |
714 | memcpy(s, fp->_IO_read_ptr, count); | |
715 | s += count; | |
716 | fp->_IO_read_ptr += count; | |
717 | } | |
718 | else if (count <= 0) | |
719 | count = 0; | |
720 | else | |
721 | { | |
722 | register char *p = fp->_IO_read_ptr; | |
723 | register int i = (int)count; | |
724 | while (--i >= 0) *s++ = *p++; | |
725 | fp->_IO_read_ptr = p; | |
726 | } | |
727 | more -= count; | |
728 | } | |
729 | #if 0 | |
730 | if (! _IO_in put_mode (fp) | |
731 | && ! _IO_have_markers (fp) && ! IO_have_backup (fp)) | |
732 | { | |
733 | /* This is an optimization of _IO_file_underflow */ | |
734 | if (fp->_flags & _IO_NO_READS) | |
735 | break; | |
736 | /* If we're reading a lot of data, don't bother allocating | |
737 | a buffer. But if we're only reading a bit, perhaps we should ??*/ | |
738 | if (count <= 512 && fp->_IO_buf_base == NULL) | |
739 | _IO_doallocbuf(fp); | |
740 | if (fp->_flags & (_IO_LINE_BUF|_IO_UNBUFFERED)) | |
741 | _IO_flush_all_linebuffered(); | |
742 | ||
743 | _IO_switch_to_get_mode(fp); ???; | |
744 | count = _IO_SYSREAD (fp, s, more); | |
745 | if (count <= 0) | |
746 | { | |
747 | if (count == 0) | |
748 | fp->_flags |= _IO_EOF_SEEN; | |
749 | else | |
750 | fp->_flags |= _IO_ERR_SEEN, count = 0; | |
751 | } | |
23396375 | 752 | |
96aa2d94 RM |
753 | s += count; |
754 | more -= count; | |
755 | } | |
756 | #endif | |
757 | if (more == 0 || __underflow(fp) == EOF) | |
758 | break; | |
759 | } | |
760 | return n - more; | |
761 | } | |
762 | #endif | |
763 | ||
764 | struct _IO_jump_t _IO_file_jumps = { | |
765 | JUMP_INIT_DUMMY, | |
766 | JUMP_INIT(finish, _IO_file_finish), | |
767 | JUMP_INIT(overflow, _IO_file_overflow), | |
768 | JUMP_INIT(underflow, _IO_file_underflow), | |
769 | JUMP_INIT(uflow, _IO_default_uflow), | |
770 | JUMP_INIT(pbackfail, _IO_default_pbackfail), | |
771 | JUMP_INIT(xsputn, _IO_file_xsputn), | |
772 | JUMP_INIT(xsgetn, _IO_default_xsgetn), | |
773 | JUMP_INIT(seekoff, _IO_file_seekoff), | |
774 | JUMP_INIT(seekpos, _IO_default_seekpos), | |
775 | JUMP_INIT(setbuf, _IO_file_setbuf), | |
776 | JUMP_INIT(sync, _IO_file_sync), | |
777 | JUMP_INIT(doallocate, _IO_file_doallocate), | |
778 | JUMP_INIT(read, _IO_file_read), | |
779 | JUMP_INIT(write, _IO_file_write), | |
780 | JUMP_INIT(seek, _IO_file_seek), | |
781 | JUMP_INIT(close, _IO_file_close), | |
782 | JUMP_INIT(stat, _IO_file_stat) | |
783 | }; |