Remove miscellaneous __STDC__ conditionals.
[glibc.git] / libio / genops.c
blobaa504565415f0c3ef6dab9337caf83300953b590
1 /* Copyright (C) 1993,1995,1997-2002, 2003, 2004, 2006, 2007, 2011-2012
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C 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 GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA.
20 As a special exception, if you link the code in this file with
21 files compiled with a GNU compiler to produce an executable,
22 that does not cause the resulting executable to be covered by
23 the GNU Lesser General Public License. This exception does not
24 however invalidate any other reasons why the executable file
25 might be covered by the GNU Lesser General Public License.
26 This exception applies to code released by its copyright holders
27 in files containing the exception. */
29 /* Generic or default I/O operations. */
31 #include "libioP.h"
32 #include <stdlib.h>
33 #include <string.h>
34 #include <stdbool.h>
35 #ifdef _LIBC
36 #include <sched.h>
37 #endif
39 #ifdef _IO_MTSAFE_IO
40 static _IO_lock_t list_all_lock = _IO_lock_initializer;
41 #endif
43 /* Used to signal modifications to the list of FILE decriptors. */
44 static int _IO_list_all_stamp;
47 static _IO_FILE *run_fp;
49 static void
50 flush_cleanup (void *not_used)
52 if (run_fp != NULL)
53 _IO_funlockfile (run_fp);
54 #ifdef _IO_MTSAFE_IO
55 _IO_lock_unlock (list_all_lock);
56 #endif
59 void
60 _IO_un_link (fp)
61 struct _IO_FILE_plus *fp;
63 if (fp->file._flags & _IO_LINKED)
65 struct _IO_FILE **f;
66 #ifdef _IO_MTSAFE_IO
67 _IO_cleanup_region_start_noarg (flush_cleanup);
68 _IO_lock_lock (list_all_lock);
69 run_fp = (_IO_FILE *) fp;
70 _IO_flockfile ((_IO_FILE *) fp);
71 #endif
72 if (INTUSE(_IO_list_all) == NULL)
74 else if (fp == INTUSE(_IO_list_all))
76 INTUSE(_IO_list_all)
77 = (struct _IO_FILE_plus *) INTUSE(_IO_list_all)->file._chain;
78 ++_IO_list_all_stamp;
80 else
81 for (f = &INTUSE(_IO_list_all)->file._chain; *f; f = &(*f)->_chain)
82 if (*f == (_IO_FILE *) fp)
84 *f = fp->file._chain;
85 ++_IO_list_all_stamp;
86 break;
88 fp->file._flags &= ~_IO_LINKED;
89 #ifdef _IO_MTSAFE_IO
90 _IO_funlockfile ((_IO_FILE *) fp);
91 run_fp = NULL;
92 _IO_lock_unlock (list_all_lock);
93 _IO_cleanup_region_end (0);
94 #endif
97 INTDEF(_IO_un_link)
99 void
100 _IO_link_in (fp)
101 struct _IO_FILE_plus *fp;
103 if ((fp->file._flags & _IO_LINKED) == 0)
105 fp->file._flags |= _IO_LINKED;
106 #ifdef _IO_MTSAFE_IO
107 _IO_cleanup_region_start_noarg (flush_cleanup);
108 _IO_lock_lock (list_all_lock);
109 run_fp = (_IO_FILE *) fp;
110 _IO_flockfile ((_IO_FILE *) fp);
111 #endif
112 fp->file._chain = (_IO_FILE *) INTUSE(_IO_list_all);
113 INTUSE(_IO_list_all) = fp;
114 ++_IO_list_all_stamp;
115 #ifdef _IO_MTSAFE_IO
116 _IO_funlockfile ((_IO_FILE *) fp);
117 run_fp = NULL;
118 _IO_lock_unlock (list_all_lock);
119 _IO_cleanup_region_end (0);
120 #endif
123 INTDEF(_IO_link_in)
125 /* Return minimum _pos markers
126 Assumes the current get area is the main get area. */
127 _IO_ssize_t _IO_least_marker (_IO_FILE *fp, char *end_p);
129 _IO_ssize_t
130 _IO_least_marker (fp, end_p)
131 _IO_FILE *fp;
132 char *end_p;
134 _IO_ssize_t least_so_far = end_p - fp->_IO_read_base;
135 struct _IO_marker *mark;
136 for (mark = fp->_markers; mark != NULL; mark = mark->_next)
137 if (mark->_pos < least_so_far)
138 least_so_far = mark->_pos;
139 return least_so_far;
142 /* Switch current get area from backup buffer to (start of) main get area. */
144 void
145 _IO_switch_to_main_get_area (fp)
146 _IO_FILE *fp;
148 char *tmp;
149 fp->_flags &= ~_IO_IN_BACKUP;
150 /* Swap _IO_read_end and _IO_save_end. */
151 tmp = fp->_IO_read_end;
152 fp->_IO_read_end = fp->_IO_save_end;
153 fp->_IO_save_end= tmp;
154 /* Swap _IO_read_base and _IO_save_base. */
155 tmp = fp->_IO_read_base;
156 fp->_IO_read_base = fp->_IO_save_base;
157 fp->_IO_save_base = tmp;
158 /* Set _IO_read_ptr. */
159 fp->_IO_read_ptr = fp->_IO_read_base;
162 /* Switch current get area from main get area to (end of) backup area. */
164 void
165 _IO_switch_to_backup_area (fp)
166 _IO_FILE *fp;
168 char *tmp;
169 fp->_flags |= _IO_IN_BACKUP;
170 /* Swap _IO_read_end and _IO_save_end. */
171 tmp = fp->_IO_read_end;
172 fp->_IO_read_end = fp->_IO_save_end;
173 fp->_IO_save_end = tmp;
174 /* Swap _IO_read_base and _IO_save_base. */
175 tmp = fp->_IO_read_base;
176 fp->_IO_read_base = fp->_IO_save_base;
177 fp->_IO_save_base = tmp;
178 /* Set _IO_read_ptr. */
179 fp->_IO_read_ptr = fp->_IO_read_end;
183 _IO_switch_to_get_mode (fp)
184 _IO_FILE *fp;
186 if (fp->_IO_write_ptr > fp->_IO_write_base)
187 if (_IO_OVERFLOW (fp, EOF) == EOF)
188 return EOF;
189 if (_IO_in_backup (fp))
190 fp->_IO_read_base = fp->_IO_backup_base;
191 else
193 fp->_IO_read_base = fp->_IO_buf_base;
194 if (fp->_IO_write_ptr > fp->_IO_read_end)
195 fp->_IO_read_end = fp->_IO_write_ptr;
197 fp->_IO_read_ptr = fp->_IO_write_ptr;
199 fp->_IO_write_base = fp->_IO_write_ptr = fp->_IO_write_end = fp->_IO_read_ptr;
201 fp->_flags &= ~_IO_CURRENTLY_PUTTING;
202 return 0;
204 INTDEF(_IO_switch_to_get_mode)
206 void
207 _IO_free_backup_area (fp)
208 _IO_FILE *fp;
210 if (_IO_in_backup (fp))
211 _IO_switch_to_main_get_area (fp); /* Just in case. */
212 free (fp->_IO_save_base);
213 fp->_IO_save_base = NULL;
214 fp->_IO_save_end = NULL;
215 fp->_IO_backup_base = NULL;
217 INTDEF(_IO_free_backup_area)
219 #if 0
221 _IO_switch_to_put_mode (fp)
222 _IO_FILE *fp;
224 fp->_IO_write_base = fp->_IO_read_ptr;
225 fp->_IO_write_ptr = fp->_IO_read_ptr;
226 /* Following is wrong if line- or un-buffered? */
227 fp->_IO_write_end = (fp->_flags & _IO_IN_BACKUP
228 ? fp->_IO_read_end : fp->_IO_buf_end);
230 fp->_IO_read_ptr = fp->_IO_read_end;
231 fp->_IO_read_base = fp->_IO_read_end;
233 fp->_flags |= _IO_CURRENTLY_PUTTING;
234 return 0;
236 #endif
239 __overflow (f, ch)
240 _IO_FILE *f;
241 int ch;
243 /* This is a single-byte stream. */
244 if (f->_mode == 0)
245 _IO_fwide (f, -1);
246 return _IO_OVERFLOW (f, ch);
248 libc_hidden_def (__overflow)
250 static int save_for_backup (_IO_FILE *fp, char *end_p)
251 #ifdef _LIBC
252 internal_function
253 #endif
256 static int
257 #ifdef _LIBC
258 internal_function
259 #endif
260 save_for_backup (fp, end_p)
261 _IO_FILE *fp;
262 char *end_p;
264 /* Append [_IO_read_base..end_p] to backup area. */
265 _IO_ssize_t least_mark = _IO_least_marker (fp, end_p);
266 /* needed_size is how much space we need in the backup area. */
267 _IO_size_t needed_size = (end_p - fp->_IO_read_base) - least_mark;
268 /* FIXME: Dubious arithmetic if pointers are NULL */
269 _IO_size_t current_Bsize = fp->_IO_save_end - fp->_IO_save_base;
270 _IO_size_t avail; /* Extra space available for future expansion. */
271 _IO_ssize_t delta;
272 struct _IO_marker *mark;
273 if (needed_size > current_Bsize)
275 char *new_buffer;
276 avail = 100;
277 new_buffer = (char *) malloc (avail + needed_size);
278 if (new_buffer == NULL)
279 return EOF; /* FIXME */
280 if (least_mark < 0)
282 #ifdef _LIBC
283 __mempcpy (__mempcpy (new_buffer + avail,
284 fp->_IO_save_end + least_mark,
285 -least_mark),
286 fp->_IO_read_base,
287 end_p - fp->_IO_read_base);
288 #else
289 memcpy (new_buffer + avail,
290 fp->_IO_save_end + least_mark,
291 -least_mark);
292 memcpy (new_buffer + avail - least_mark,
293 fp->_IO_read_base,
294 end_p - fp->_IO_read_base);
295 #endif
297 else
298 memcpy (new_buffer + avail,
299 fp->_IO_read_base + least_mark,
300 needed_size);
301 free (fp->_IO_save_base);
302 fp->_IO_save_base = new_buffer;
303 fp->_IO_save_end = new_buffer + avail + needed_size;
305 else
307 avail = current_Bsize - needed_size;
308 if (least_mark < 0)
310 memmove (fp->_IO_save_base + avail,
311 fp->_IO_save_end + least_mark,
312 -least_mark);
313 memcpy (fp->_IO_save_base + avail - least_mark,
314 fp->_IO_read_base,
315 end_p - fp->_IO_read_base);
317 else if (needed_size > 0)
318 memcpy (fp->_IO_save_base + avail,
319 fp->_IO_read_base + least_mark,
320 needed_size);
322 fp->_IO_backup_base = fp->_IO_save_base + avail;
323 /* Adjust all the streammarkers. */
324 delta = end_p - fp->_IO_read_base;
325 for (mark = fp->_markers; mark != NULL; mark = mark->_next)
326 mark->_pos -= delta;
327 return 0;
331 __underflow (fp)
332 _IO_FILE *fp;
334 #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
335 if (_IO_vtable_offset (fp) == 0 && _IO_fwide (fp, -1) != -1)
336 return EOF;
337 #endif
339 if (fp->_mode == 0)
340 _IO_fwide (fp, -1);
341 if (_IO_in_put_mode (fp))
342 if (INTUSE(_IO_switch_to_get_mode) (fp) == EOF)
343 return EOF;
344 if (fp->_IO_read_ptr < fp->_IO_read_end)
345 return *(unsigned char *) fp->_IO_read_ptr;
346 if (_IO_in_backup (fp))
348 _IO_switch_to_main_get_area (fp);
349 if (fp->_IO_read_ptr < fp->_IO_read_end)
350 return *(unsigned char *) fp->_IO_read_ptr;
352 if (_IO_have_markers (fp))
354 if (save_for_backup (fp, fp->_IO_read_end))
355 return EOF;
357 else if (_IO_have_backup (fp))
358 INTUSE(_IO_free_backup_area) (fp);
359 return _IO_UNDERFLOW (fp);
361 libc_hidden_def (__underflow)
364 __uflow (fp)
365 _IO_FILE *fp;
367 #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
368 if (_IO_vtable_offset (fp) == 0 && _IO_fwide (fp, -1) != -1)
369 return EOF;
370 #endif
372 if (fp->_mode == 0)
373 _IO_fwide (fp, -1);
374 if (_IO_in_put_mode (fp))
375 if (INTUSE(_IO_switch_to_get_mode) (fp) == EOF)
376 return EOF;
377 if (fp->_IO_read_ptr < fp->_IO_read_end)
378 return *(unsigned char *) fp->_IO_read_ptr++;
379 if (_IO_in_backup (fp))
381 _IO_switch_to_main_get_area (fp);
382 if (fp->_IO_read_ptr < fp->_IO_read_end)
383 return *(unsigned char *) fp->_IO_read_ptr++;
385 if (_IO_have_markers (fp))
387 if (save_for_backup (fp, fp->_IO_read_end))
388 return EOF;
390 else if (_IO_have_backup (fp))
391 INTUSE(_IO_free_backup_area) (fp);
392 return _IO_UFLOW (fp);
394 libc_hidden_def (__uflow)
396 void
397 _IO_setb (f, b, eb, a)
398 _IO_FILE *f;
399 char *b;
400 char *eb;
401 int a;
403 if (f->_IO_buf_base && !(f->_flags & _IO_USER_BUF))
404 FREE_BUF (f->_IO_buf_base, _IO_blen (f));
405 f->_IO_buf_base = b;
406 f->_IO_buf_end = eb;
407 if (a)
408 f->_flags &= ~_IO_USER_BUF;
409 else
410 f->_flags |= _IO_USER_BUF;
412 INTDEF(_IO_setb)
414 void
415 _IO_doallocbuf (fp)
416 _IO_FILE *fp;
418 if (fp->_IO_buf_base)
419 return;
420 if (!(fp->_flags & _IO_UNBUFFERED) || fp->_mode > 0)
421 if (_IO_DOALLOCATE (fp) != EOF)
422 return;
423 INTUSE(_IO_setb) (fp, fp->_shortbuf, fp->_shortbuf+1, 0);
425 INTDEF(_IO_doallocbuf)
428 _IO_default_underflow (fp)
429 _IO_FILE *fp;
431 return EOF;
435 _IO_default_uflow (fp)
436 _IO_FILE *fp;
438 int ch = _IO_UNDERFLOW (fp);
439 if (ch == EOF)
440 return EOF;
441 return *(unsigned char *) fp->_IO_read_ptr++;
443 INTDEF(_IO_default_uflow)
445 _IO_size_t
446 _IO_default_xsputn (f, data, n)
447 _IO_FILE *f;
448 const void *data;
449 _IO_size_t n;
451 const char *s = (char *) data;
452 _IO_size_t more = n;
453 if (more <= 0)
454 return 0;
455 for (;;)
457 /* Space available. */
458 if (f->_IO_write_ptr < f->_IO_write_end)
460 _IO_size_t count = f->_IO_write_end - f->_IO_write_ptr;
461 if (count > more)
462 count = more;
463 if (count > 20)
465 #ifdef _LIBC
466 f->_IO_write_ptr = __mempcpy (f->_IO_write_ptr, s, count);
467 #else
468 memcpy (f->_IO_write_ptr, s, count);
469 f->_IO_write_ptr += count;
470 #endif
471 s += count;
473 else if (count)
475 char *p = f->_IO_write_ptr;
476 _IO_ssize_t i;
477 for (i = count; --i >= 0; )
478 *p++ = *s++;
479 f->_IO_write_ptr = p;
481 more -= count;
483 if (more == 0 || _IO_OVERFLOW (f, (unsigned char) *s++) == EOF)
484 break;
485 more--;
487 return n - more;
489 INTDEF(_IO_default_xsputn)
491 _IO_size_t
492 _IO_sgetn (fp, data, n)
493 _IO_FILE *fp;
494 void *data;
495 _IO_size_t n;
497 /* FIXME handle putback buffer here! */
498 return _IO_XSGETN (fp, data, n);
500 INTDEF(_IO_sgetn)
502 _IO_size_t
503 _IO_default_xsgetn (fp, data, n)
504 _IO_FILE *fp;
505 void *data;
506 _IO_size_t n;
508 _IO_size_t more = n;
509 char *s = (char*) data;
510 for (;;)
512 /* Data available. */
513 if (fp->_IO_read_ptr < fp->_IO_read_end)
515 _IO_size_t count = fp->_IO_read_end - fp->_IO_read_ptr;
516 if (count > more)
517 count = more;
518 if (count > 20)
520 #ifdef _LIBC
521 s = __mempcpy (s, fp->_IO_read_ptr, count);
522 #else
523 memcpy (s, fp->_IO_read_ptr, count);
524 s += count;
525 #endif
526 fp->_IO_read_ptr += count;
528 else if (count)
530 char *p = fp->_IO_read_ptr;
531 int i = (int) count;
532 while (--i >= 0)
533 *s++ = *p++;
534 fp->_IO_read_ptr = p;
536 more -= count;
538 if (more == 0 || __underflow (fp) == EOF)
539 break;
541 return n - more;
543 INTDEF(_IO_default_xsgetn)
545 #if 0
546 /* Seems not to be needed. --drepper */
548 _IO_sync (fp)
549 _IO_FILE *fp;
551 return 0;
553 #endif
555 _IO_FILE *
556 _IO_default_setbuf (fp, p, len)
557 _IO_FILE *fp;
558 char *p;
559 _IO_ssize_t len;
561 if (_IO_SYNC (fp) == EOF)
562 return NULL;
563 if (p == NULL || len == 0)
565 fp->_flags |= _IO_UNBUFFERED;
566 INTUSE(_IO_setb) (fp, fp->_shortbuf, fp->_shortbuf+1, 0);
568 else
570 fp->_flags &= ~_IO_UNBUFFERED;
571 INTUSE(_IO_setb) (fp, p, p+len, 0);
573 fp->_IO_write_base = fp->_IO_write_ptr = fp->_IO_write_end = 0;
574 fp->_IO_read_base = fp->_IO_read_ptr = fp->_IO_read_end = 0;
575 return fp;
578 _IO_off64_t
579 _IO_default_seekpos (fp, pos, mode)
580 _IO_FILE *fp;
581 _IO_off64_t pos;
582 int mode;
584 return _IO_SEEKOFF (fp, pos, 0, mode);
588 _IO_default_doallocate (fp)
589 _IO_FILE *fp;
591 char *buf;
593 ALLOC_BUF (buf, _IO_BUFSIZ, EOF);
594 INTUSE(_IO_setb) (fp, buf, buf+_IO_BUFSIZ, 1);
595 return 1;
597 INTDEF(_IO_default_doallocate)
599 void
600 _IO_init (fp, flags)
601 _IO_FILE *fp;
602 int flags;
604 _IO_no_init (fp, flags, -1, NULL, NULL);
606 INTDEF(_IO_init)
608 void
609 _IO_old_init (fp, flags)
610 _IO_FILE *fp;
611 int flags;
613 fp->_flags = _IO_MAGIC|flags;
614 fp->_flags2 = 0;
615 fp->_IO_buf_base = NULL;
616 fp->_IO_buf_end = NULL;
617 fp->_IO_read_base = NULL;
618 fp->_IO_read_ptr = NULL;
619 fp->_IO_read_end = NULL;
620 fp->_IO_write_base = NULL;
621 fp->_IO_write_ptr = NULL;
622 fp->_IO_write_end = NULL;
623 fp->_chain = NULL; /* Not necessary. */
625 fp->_IO_save_base = NULL;
626 fp->_IO_backup_base = NULL;
627 fp->_IO_save_end = NULL;
628 fp->_markers = NULL;
629 fp->_cur_column = 0;
630 #if _IO_JUMPS_OFFSET
631 fp->_vtable_offset = 0;
632 #endif
633 #ifdef _IO_MTSAFE_IO
634 if (fp->_lock != NULL)
635 _IO_lock_init (*fp->_lock);
636 #endif
639 void
640 _IO_no_init (fp, flags, orientation, wd, jmp)
641 _IO_FILE *fp;
642 int flags;
643 int orientation;
644 struct _IO_wide_data *wd;
645 const struct _IO_jump_t *jmp;
647 _IO_old_init (fp, flags);
648 fp->_mode = orientation;
649 #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
650 if (orientation >= 0)
652 fp->_wide_data = wd;
653 fp->_wide_data->_IO_buf_base = NULL;
654 fp->_wide_data->_IO_buf_end = NULL;
655 fp->_wide_data->_IO_read_base = NULL;
656 fp->_wide_data->_IO_read_ptr = NULL;
657 fp->_wide_data->_IO_read_end = NULL;
658 fp->_wide_data->_IO_write_base = NULL;
659 fp->_wide_data->_IO_write_ptr = NULL;
660 fp->_wide_data->_IO_write_end = NULL;
661 fp->_wide_data->_IO_save_base = NULL;
662 fp->_wide_data->_IO_backup_base = NULL;
663 fp->_wide_data->_IO_save_end = NULL;
665 fp->_wide_data->_wide_vtable = jmp;
667 #endif
668 fp->_freeres_list = NULL;
672 _IO_default_sync (fp)
673 _IO_FILE *fp;
675 return 0;
678 /* The way the C++ classes are mapped into the C functions in the
679 current implementation, this function can get called twice! */
681 void
682 _IO_default_finish (fp, dummy)
683 _IO_FILE *fp;
684 int dummy;
686 struct _IO_marker *mark;
687 if (fp->_IO_buf_base && !(fp->_flags & _IO_USER_BUF))
689 FREE_BUF (fp->_IO_buf_base, _IO_blen (fp));
690 fp->_IO_buf_base = fp->_IO_buf_end = NULL;
693 for (mark = fp->_markers; mark != NULL; mark = mark->_next)
694 mark->_sbuf = NULL;
696 if (fp->_IO_save_base)
698 free (fp->_IO_save_base);
699 fp->_IO_save_base = NULL;
702 INTUSE(_IO_un_link) ((struct _IO_FILE_plus *) fp);
704 #ifdef _IO_MTSAFE_IO
705 if (fp->_lock != NULL)
706 _IO_lock_fini (*fp->_lock);
707 #endif
709 INTDEF(_IO_default_finish)
711 _IO_off64_t
712 _IO_default_seekoff (fp, offset, dir, mode)
713 _IO_FILE *fp;
714 _IO_off64_t offset;
715 int dir;
716 int mode;
718 return _IO_pos_BAD;
722 _IO_sputbackc (fp, c)
723 _IO_FILE *fp;
724 int c;
726 int result;
728 if (fp->_IO_read_ptr > fp->_IO_read_base
729 && (unsigned char)fp->_IO_read_ptr[-1] == (unsigned char)c)
731 fp->_IO_read_ptr--;
732 result = (unsigned char) c;
734 else
735 result = _IO_PBACKFAIL (fp, c);
737 if (result != EOF)
738 fp->_flags &= ~_IO_EOF_SEEN;
740 return result;
742 INTDEF(_IO_sputbackc)
745 _IO_sungetc (fp)
746 _IO_FILE *fp;
748 int result;
750 if (fp->_IO_read_ptr > fp->_IO_read_base)
752 fp->_IO_read_ptr--;
753 result = (unsigned char) *fp->_IO_read_ptr;
755 else
756 result = _IO_PBACKFAIL (fp, EOF);
758 if (result != EOF)
759 fp->_flags &= ~_IO_EOF_SEEN;
761 return result;
764 #if 0 /* Work in progress */
765 /* Seems not to be needed. */
766 #if 0
767 void
768 _IO_set_column (fp, c)
769 _IO_FILE *fp;
770 int c;
772 if (c == -1)
773 fp->_column = -1;
774 else
775 fp->_column = c - (fp->_IO_write_ptr - fp->_IO_write_base);
777 #else
779 _IO_set_column (fp, i)
780 _IO_FILE *fp;
781 int i;
783 fp->_cur_column = i + 1;
784 return 0;
786 #endif
787 #endif
790 unsigned
791 _IO_adjust_column (start, line, count)
792 unsigned start;
793 const char *line;
794 int count;
796 const char *ptr = line + count;
797 while (ptr > line)
798 if (*--ptr == '\n')
799 return line + count - ptr - 1;
800 return start + count;
802 INTDEF(_IO_adjust_column)
804 #if 0
805 /* Seems not to be needed. --drepper */
807 _IO_get_column (fp)
808 _IO_FILE *fp;
810 if (fp->_cur_column)
811 return _IO_adjust_column (fp->_cur_column - 1,
812 fp->_IO_write_base,
813 fp->_IO_write_ptr - fp->_IO_write_base);
814 return -1;
816 #endif
820 _IO_flush_all_lockp (int do_lock)
822 int result = 0;
823 struct _IO_FILE *fp;
824 int last_stamp;
826 #ifdef _IO_MTSAFE_IO
827 __libc_cleanup_region_start (do_lock, flush_cleanup, 0);
828 if (do_lock)
829 _IO_lock_lock (list_all_lock);
830 #endif
832 last_stamp = _IO_list_all_stamp;
833 fp = (_IO_FILE *) INTUSE(_IO_list_all);
834 while (fp != NULL)
836 run_fp = fp;
837 if (do_lock)
838 _IO_flockfile (fp);
840 if (((fp->_mode <= 0 && fp->_IO_write_ptr > fp->_IO_write_base)
841 #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
842 || (_IO_vtable_offset (fp) == 0
843 && fp->_mode > 0 && (fp->_wide_data->_IO_write_ptr
844 > fp->_wide_data->_IO_write_base))
845 #endif
847 && _IO_OVERFLOW (fp, EOF) == EOF)
848 result = EOF;
850 if (do_lock)
851 _IO_funlockfile (fp);
852 run_fp = NULL;
854 if (last_stamp != _IO_list_all_stamp)
856 /* Something was added to the list. Start all over again. */
857 fp = (_IO_FILE *) INTUSE(_IO_list_all);
858 last_stamp = _IO_list_all_stamp;
860 else
861 fp = fp->_chain;
864 #ifdef _IO_MTSAFE_IO
865 if (do_lock)
866 _IO_lock_unlock (list_all_lock);
867 __libc_cleanup_region_end (0);
868 #endif
870 return result;
875 _IO_flush_all ()
877 /* We want locking. */
878 return _IO_flush_all_lockp (1);
880 INTDEF(_IO_flush_all)
882 void
883 _IO_flush_all_linebuffered ()
885 struct _IO_FILE *fp;
886 int last_stamp;
888 #ifdef _IO_MTSAFE_IO
889 _IO_cleanup_region_start_noarg (flush_cleanup);
890 _IO_lock_lock (list_all_lock);
891 #endif
893 last_stamp = _IO_list_all_stamp;
894 fp = (_IO_FILE *) INTUSE(_IO_list_all);
895 while (fp != NULL)
897 run_fp = fp;
898 _IO_flockfile (fp);
900 if ((fp->_flags & _IO_NO_WRITES) == 0 && fp->_flags & _IO_LINE_BUF)
901 _IO_OVERFLOW (fp, EOF);
903 _IO_funlockfile (fp);
904 run_fp = NULL;
906 if (last_stamp != _IO_list_all_stamp)
908 /* Something was added to the list. Start all over again. */
909 fp = (_IO_FILE *) INTUSE(_IO_list_all);
910 last_stamp = _IO_list_all_stamp;
912 else
913 fp = fp->_chain;
916 #ifdef _IO_MTSAFE_IO
917 _IO_lock_unlock (list_all_lock);
918 _IO_cleanup_region_end (0);
919 #endif
921 INTDEF(_IO_flush_all_linebuffered)
922 #ifdef _LIBC
923 weak_alias (_IO_flush_all_linebuffered, _flushlbf)
924 #endif
927 /* The following is a bit tricky. In general, we want to unbuffer the
928 streams so that all output which follows is seen. If we are not
929 looking for memory leaks it does not make much sense to free the
930 actual buffer because this will happen anyway once the program
931 terminated. If we do want to look for memory leaks we have to free
932 the buffers. Whether something is freed is determined by the
933 function sin the libc_freeres section. Those are called as part of
934 the atexit routine, just like _IO_cleanup. The problem is we do
935 not know whether the freeres code is called first or _IO_cleanup.
936 if the former is the case, we set the DEALLOC_BUFFER variable to
937 true and _IO_unbuffer_write will take care of the rest. If
938 _IO_unbuffer_write is called first we add the streams to a list
939 which the freeres function later can walk through. */
940 static void _IO_unbuffer_write (void);
942 static bool dealloc_buffers;
943 static _IO_FILE *freeres_list;
945 static void
946 _IO_unbuffer_write (void)
948 struct _IO_FILE *fp;
949 for (fp = (_IO_FILE *) INTUSE(_IO_list_all); fp; fp = fp->_chain)
951 if (! (fp->_flags & _IO_UNBUFFERED)
952 && (! (fp->_flags & _IO_NO_WRITES)
953 || (fp->_flags & _IO_IS_APPENDING))
954 /* Iff stream is un-orientated, it wasn't used. */
955 && fp->_mode != 0)
957 int cnt;
958 #define MAXTRIES 2
959 for (cnt = 0; cnt < MAXTRIES; ++cnt)
960 if (fp->_lock == NULL || _IO_lock_trylock (*fp->_lock) == 0)
961 break;
962 else
963 /* Give the other thread time to finish up its use of the
964 stream. */
965 __sched_yield ();
967 if (! dealloc_buffers && !(fp->_flags & _IO_USER_BUF))
969 fp->_flags |= _IO_USER_BUF;
971 fp->_freeres_list = freeres_list;
972 freeres_list = fp;
973 fp->_freeres_buf = fp->_IO_buf_base;
974 fp->_freeres_size = _IO_blen (fp);
977 _IO_SETBUF (fp, NULL, 0);
979 if (cnt < MAXTRIES && fp->_lock != NULL)
980 _IO_lock_unlock (*fp->_lock);
983 /* Make sure that never again the wide char functions can be
984 used. */
985 fp->_mode = -1;
990 libc_freeres_fn (buffer_free)
992 dealloc_buffers = true;
994 while (freeres_list != NULL)
996 FREE_BUF (freeres_list->_freeres_buf, freeres_list->_freeres_size);
998 freeres_list = freeres_list->_freeres_list;
1004 _IO_cleanup ()
1006 /* We do *not* want locking. Some threads might use streams but
1007 that is their problem, we flush them underneath them. */
1008 int result = _IO_flush_all_lockp (0);
1010 /* We currently don't have a reliable mechanism for making sure that
1011 C++ static destructors are executed in the correct order.
1012 So it is possible that other static destructors might want to
1013 write to cout - and they're supposed to be able to do so.
1015 The following will make the standard streambufs be unbuffered,
1016 which forces any output from late destructors to be written out. */
1017 _IO_unbuffer_write ();
1019 return result;
1023 void
1024 _IO_init_marker (marker, fp)
1025 struct _IO_marker *marker;
1026 _IO_FILE *fp;
1028 marker->_sbuf = fp;
1029 if (_IO_in_put_mode (fp))
1030 INTUSE(_IO_switch_to_get_mode) (fp);
1031 if (_IO_in_backup (fp))
1032 marker->_pos = fp->_IO_read_ptr - fp->_IO_read_end;
1033 else
1034 marker->_pos = fp->_IO_read_ptr - fp->_IO_read_base;
1036 /* Should perhaps sort the chain? */
1037 marker->_next = fp->_markers;
1038 fp->_markers = marker;
1041 void
1042 _IO_remove_marker (marker)
1043 struct _IO_marker *marker;
1045 /* Unlink from sb's chain. */
1046 struct _IO_marker **ptr = &marker->_sbuf->_markers;
1047 for (; ; ptr = &(*ptr)->_next)
1049 if (*ptr == NULL)
1050 break;
1051 else if (*ptr == marker)
1053 *ptr = marker->_next;
1054 return;
1057 #if 0
1058 if _sbuf has a backup area that is no longer needed, should we delete
1059 it now, or wait until the next underflow?
1060 #endif
1063 #define BAD_DELTA EOF
1066 _IO_marker_difference (mark1, mark2)
1067 struct _IO_marker *mark1;
1068 struct _IO_marker *mark2;
1070 return mark1->_pos - mark2->_pos;
1073 /* Return difference between MARK and current position of MARK's stream. */
1075 _IO_marker_delta (mark)
1076 struct _IO_marker *mark;
1078 int cur_pos;
1079 if (mark->_sbuf == NULL)
1080 return BAD_DELTA;
1081 if (_IO_in_backup (mark->_sbuf))
1082 cur_pos = mark->_sbuf->_IO_read_ptr - mark->_sbuf->_IO_read_end;
1083 else
1084 cur_pos = mark->_sbuf->_IO_read_ptr - mark->_sbuf->_IO_read_base;
1085 return mark->_pos - cur_pos;
1089 _IO_seekmark (fp, mark, delta)
1090 _IO_FILE *fp;
1091 struct _IO_marker *mark;
1092 int delta;
1094 if (mark->_sbuf != fp)
1095 return EOF;
1096 if (mark->_pos >= 0)
1098 if (_IO_in_backup (fp))
1099 _IO_switch_to_main_get_area (fp);
1100 fp->_IO_read_ptr = fp->_IO_read_base + mark->_pos;
1102 else
1104 if (!_IO_in_backup (fp))
1105 _IO_switch_to_backup_area (fp);
1106 fp->_IO_read_ptr = fp->_IO_read_end + mark->_pos;
1108 return 0;
1111 void
1112 _IO_unsave_markers (fp)
1113 _IO_FILE *fp;
1115 struct _IO_marker *mark = fp->_markers;
1116 if (mark)
1118 #ifdef TODO
1119 streampos offset = seekoff (0, ios::cur, ios::in);
1120 if (offset != EOF)
1122 offset += eGptr () - Gbase ();
1123 for ( ; mark != NULL; mark = mark->_next)
1124 mark->set_streampos (mark->_pos + offset);
1126 else
1128 for ( ; mark != NULL; mark = mark->_next)
1129 mark->set_streampos (EOF);
1131 #endif
1132 fp->_markers = 0;
1135 if (_IO_have_backup (fp))
1136 INTUSE(_IO_free_backup_area) (fp);
1138 INTDEF(_IO_unsave_markers)
1140 #if 0
1141 /* Seems not to be needed. --drepper */
1143 _IO_nobackup_pbackfail (fp, c)
1144 _IO_FILE *fp;
1145 int c;
1147 if (fp->_IO_read_ptr > fp->_IO_read_base)
1148 fp->_IO_read_ptr--;
1149 if (c != EOF && *fp->_IO_read_ptr != c)
1150 *fp->_IO_read_ptr = c;
1151 return (unsigned char) c;
1153 #endif
1156 _IO_default_pbackfail (fp, c)
1157 _IO_FILE *fp;
1158 int c;
1160 if (fp->_IO_read_ptr > fp->_IO_read_base && !_IO_in_backup (fp)
1161 && (unsigned char) fp->_IO_read_ptr[-1] == c)
1162 --fp->_IO_read_ptr;
1163 else
1165 /* Need to handle a filebuf in write mode (switch to read mode). FIXME!*/
1166 if (!_IO_in_backup (fp))
1168 /* We need to keep the invariant that the main get area
1169 logically follows the backup area. */
1170 if (fp->_IO_read_ptr > fp->_IO_read_base && _IO_have_backup (fp))
1172 if (save_for_backup (fp, fp->_IO_read_ptr))
1173 return EOF;
1175 else if (!_IO_have_backup (fp))
1177 /* No backup buffer: allocate one. */
1178 /* Use nshort buffer, if unused? (probably not) FIXME */
1179 int backup_size = 128;
1180 char *bbuf = (char *) malloc (backup_size);
1181 if (bbuf == NULL)
1182 return EOF;
1183 fp->_IO_save_base = bbuf;
1184 fp->_IO_save_end = fp->_IO_save_base + backup_size;
1185 fp->_IO_backup_base = fp->_IO_save_end;
1187 fp->_IO_read_base = fp->_IO_read_ptr;
1188 _IO_switch_to_backup_area (fp);
1190 else if (fp->_IO_read_ptr <= fp->_IO_read_base)
1192 /* Increase size of existing backup buffer. */
1193 _IO_size_t new_size;
1194 _IO_size_t old_size = fp->_IO_read_end - fp->_IO_read_base;
1195 char *new_buf;
1196 new_size = 2 * old_size;
1197 new_buf = (char *) malloc (new_size);
1198 if (new_buf == NULL)
1199 return EOF;
1200 memcpy (new_buf + (new_size - old_size), fp->_IO_read_base,
1201 old_size);
1202 free (fp->_IO_read_base);
1203 _IO_setg (fp, new_buf, new_buf + (new_size - old_size),
1204 new_buf + new_size);
1205 fp->_IO_backup_base = fp->_IO_read_ptr;
1208 *--fp->_IO_read_ptr = c;
1210 return (unsigned char) c;
1212 INTDEF(_IO_default_pbackfail)
1214 _IO_off64_t
1215 _IO_default_seek (fp, offset, dir)
1216 _IO_FILE *fp;
1217 _IO_off64_t offset;
1218 int dir;
1220 return _IO_pos_BAD;
1224 _IO_default_stat (fp, st)
1225 _IO_FILE *fp;
1226 void* st;
1228 return EOF;
1231 _IO_ssize_t
1232 _IO_default_read (fp, data, n)
1233 _IO_FILE* fp;
1234 void *data;
1235 _IO_ssize_t n;
1237 return -1;
1240 _IO_ssize_t
1241 _IO_default_write (fp, data, n)
1242 _IO_FILE *fp;
1243 const void *data;
1244 _IO_ssize_t n;
1246 return 0;
1250 _IO_default_showmanyc (fp)
1251 _IO_FILE *fp;
1253 return -1;
1256 void
1257 _IO_default_imbue (fp, locale)
1258 _IO_FILE *fp;
1259 void *locale;
1263 _IO_ITER
1264 _IO_iter_begin()
1266 return (_IO_ITER) INTUSE(_IO_list_all);
1268 libc_hidden_def (_IO_iter_begin)
1270 _IO_ITER
1271 _IO_iter_end()
1273 return NULL;
1275 libc_hidden_def (_IO_iter_end)
1277 _IO_ITER
1278 _IO_iter_next(iter)
1279 _IO_ITER iter;
1281 return iter->_chain;
1283 libc_hidden_def (_IO_iter_next)
1285 _IO_FILE *
1286 _IO_iter_file(iter)
1287 _IO_ITER iter;
1289 return iter;
1291 libc_hidden_def (_IO_iter_file)
1293 void
1294 _IO_list_lock()
1296 #ifdef _IO_MTSAFE_IO
1297 _IO_lock_lock (list_all_lock);
1298 #endif
1300 libc_hidden_def (_IO_list_lock)
1302 void
1303 _IO_list_unlock()
1305 #ifdef _IO_MTSAFE_IO
1306 _IO_lock_unlock (list_all_lock);
1307 #endif
1309 libc_hidden_def (_IO_list_unlock)
1311 void
1312 _IO_list_resetlock()
1314 #ifdef _IO_MTSAFE_IO
1315 _IO_lock_init (list_all_lock);
1316 #endif
1318 libc_hidden_def (_IO_list_resetlock)
1321 #ifdef TODO
1322 #if defined(linux)
1323 #define IO_CLEANUP ;
1324 #endif
1326 #ifdef IO_CLEANUP
1327 IO_CLEANUP
1328 #else
1329 struct __io_defs {
1330 __io_defs() { }
1331 ~__io_defs() { _IO_cleanup (); }
1333 __io_defs io_defs__;
1334 #endif
1336 #endif /* TODO */
1338 #ifdef text_set_element
1339 text_set_element(__libc_atexit, _IO_cleanup);
1340 #endif