]> git.ipfire.org Git - thirdparty/glibc.git/blob - libio/genops.c
Mechanically remove _IO_ name aliases for types and constants.
[thirdparty/glibc.git] / libio / genops.c
1 /* Copyright (C) 1993-2018 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>.
17
18 As a special exception, if you link the code in this file with
19 files compiled with a GNU compiler to produce an executable,
20 that does not cause the resulting executable to be covered by
21 the GNU Lesser General Public License. This exception does not
22 however invalidate any other reasons why the executable file
23 might be covered by the GNU Lesser General Public License.
24 This exception applies to code released by its copyright holders
25 in files containing the exception. */
26
27 /* Generic or default I/O operations. */
28
29 #include "libioP.h"
30 #include <stdlib.h>
31 #include <string.h>
32 #include <stdbool.h>
33 #include <sched.h>
34
35 #ifdef _IO_MTSAFE_IO
36 static _IO_lock_t list_all_lock = _IO_lock_initializer;
37 #endif
38
39 static FILE *run_fp;
40
41 #ifdef _IO_MTSAFE_IO
42 static void
43 flush_cleanup (void *not_used)
44 {
45 if (run_fp != NULL)
46 _IO_funlockfile (run_fp);
47 _IO_lock_unlock (list_all_lock);
48 }
49 #endif
50
51 void
52 _IO_un_link (struct _IO_FILE_plus *fp)
53 {
54 if (fp->file._flags & _IO_LINKED)
55 {
56 FILE **f;
57 #ifdef _IO_MTSAFE_IO
58 _IO_cleanup_region_start_noarg (flush_cleanup);
59 _IO_lock_lock (list_all_lock);
60 run_fp = (FILE *) fp;
61 _IO_flockfile ((FILE *) fp);
62 #endif
63 if (_IO_list_all == NULL)
64 ;
65 else if (fp == _IO_list_all)
66 _IO_list_all = (struct _IO_FILE_plus *) _IO_list_all->file._chain;
67 else
68 for (f = &_IO_list_all->file._chain; *f; f = &(*f)->_chain)
69 if (*f == (FILE *) fp)
70 {
71 *f = fp->file._chain;
72 break;
73 }
74 fp->file._flags &= ~_IO_LINKED;
75 #ifdef _IO_MTSAFE_IO
76 _IO_funlockfile ((FILE *) fp);
77 run_fp = NULL;
78 _IO_lock_unlock (list_all_lock);
79 _IO_cleanup_region_end (0);
80 #endif
81 }
82 }
83 libc_hidden_def (_IO_un_link)
84
85 void
86 _IO_link_in (struct _IO_FILE_plus *fp)
87 {
88 if ((fp->file._flags & _IO_LINKED) == 0)
89 {
90 fp->file._flags |= _IO_LINKED;
91 #ifdef _IO_MTSAFE_IO
92 _IO_cleanup_region_start_noarg (flush_cleanup);
93 _IO_lock_lock (list_all_lock);
94 run_fp = (FILE *) fp;
95 _IO_flockfile ((FILE *) fp);
96 #endif
97 fp->file._chain = (FILE *) _IO_list_all;
98 _IO_list_all = fp;
99 #ifdef _IO_MTSAFE_IO
100 _IO_funlockfile ((FILE *) fp);
101 run_fp = NULL;
102 _IO_lock_unlock (list_all_lock);
103 _IO_cleanup_region_end (0);
104 #endif
105 }
106 }
107 libc_hidden_def (_IO_link_in)
108
109 /* Return minimum _pos markers
110 Assumes the current get area is the main get area. */
111 ssize_t _IO_least_marker (FILE *fp, char *end_p);
112
113 ssize_t
114 _IO_least_marker (FILE *fp, char *end_p)
115 {
116 ssize_t least_so_far = end_p - fp->_IO_read_base;
117 struct _IO_marker *mark;
118 for (mark = fp->_markers; mark != NULL; mark = mark->_next)
119 if (mark->_pos < least_so_far)
120 least_so_far = mark->_pos;
121 return least_so_far;
122 }
123
124 /* Switch current get area from backup buffer to (start of) main get area. */
125
126 void
127 _IO_switch_to_main_get_area (FILE *fp)
128 {
129 char *tmp;
130 fp->_flags &= ~_IO_IN_BACKUP;
131 /* Swap _IO_read_end and _IO_save_end. */
132 tmp = fp->_IO_read_end;
133 fp->_IO_read_end = fp->_IO_save_end;
134 fp->_IO_save_end= tmp;
135 /* Swap _IO_read_base and _IO_save_base. */
136 tmp = fp->_IO_read_base;
137 fp->_IO_read_base = fp->_IO_save_base;
138 fp->_IO_save_base = tmp;
139 /* Set _IO_read_ptr. */
140 fp->_IO_read_ptr = fp->_IO_read_base;
141 }
142
143 /* Switch current get area from main get area to (end of) backup area. */
144
145 void
146 _IO_switch_to_backup_area (FILE *fp)
147 {
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_end;
160 }
161
162 int
163 _IO_switch_to_get_mode (FILE *fp)
164 {
165 if (fp->_IO_write_ptr > fp->_IO_write_base)
166 if (_IO_OVERFLOW (fp, EOF) == EOF)
167 return EOF;
168 if (_IO_in_backup (fp))
169 fp->_IO_read_base = fp->_IO_backup_base;
170 else
171 {
172 fp->_IO_read_base = fp->_IO_buf_base;
173 if (fp->_IO_write_ptr > fp->_IO_read_end)
174 fp->_IO_read_end = fp->_IO_write_ptr;
175 }
176 fp->_IO_read_ptr = fp->_IO_write_ptr;
177
178 fp->_IO_write_base = fp->_IO_write_ptr = fp->_IO_write_end = fp->_IO_read_ptr;
179
180 fp->_flags &= ~_IO_CURRENTLY_PUTTING;
181 return 0;
182 }
183 libc_hidden_def (_IO_switch_to_get_mode)
184
185 void
186 _IO_free_backup_area (FILE *fp)
187 {
188 if (_IO_in_backup (fp))
189 _IO_switch_to_main_get_area (fp); /* Just in case. */
190 free (fp->_IO_save_base);
191 fp->_IO_save_base = NULL;
192 fp->_IO_save_end = NULL;
193 fp->_IO_backup_base = NULL;
194 }
195 libc_hidden_def (_IO_free_backup_area)
196
197 #if 0
198 int
199 _IO_switch_to_put_mode (FILE *fp)
200 {
201 fp->_IO_write_base = fp->_IO_read_ptr;
202 fp->_IO_write_ptr = fp->_IO_read_ptr;
203 /* Following is wrong if line- or un-buffered? */
204 fp->_IO_write_end = (fp->_flags & _IO_IN_BACKUP
205 ? fp->_IO_read_end : fp->_IO_buf_end);
206
207 fp->_IO_read_ptr = fp->_IO_read_end;
208 fp->_IO_read_base = fp->_IO_read_end;
209
210 fp->_flags |= _IO_CURRENTLY_PUTTING;
211 return 0;
212 }
213 #endif
214
215 int
216 __overflow (FILE *f, int ch)
217 {
218 /* This is a single-byte stream. */
219 if (f->_mode == 0)
220 _IO_fwide (f, -1);
221 return _IO_OVERFLOW (f, ch);
222 }
223 libc_hidden_def (__overflow)
224
225 static int
226 save_for_backup (FILE *fp, char *end_p)
227 {
228 /* Append [_IO_read_base..end_p] to backup area. */
229 ssize_t least_mark = _IO_least_marker (fp, end_p);
230 /* needed_size is how much space we need in the backup area. */
231 size_t needed_size = (end_p - fp->_IO_read_base) - least_mark;
232 /* FIXME: Dubious arithmetic if pointers are NULL */
233 size_t current_Bsize = fp->_IO_save_end - fp->_IO_save_base;
234 size_t avail; /* Extra space available for future expansion. */
235 ssize_t delta;
236 struct _IO_marker *mark;
237 if (needed_size > current_Bsize)
238 {
239 char *new_buffer;
240 avail = 100;
241 new_buffer = (char *) malloc (avail + needed_size);
242 if (new_buffer == NULL)
243 return EOF; /* FIXME */
244 if (least_mark < 0)
245 {
246 __mempcpy (__mempcpy (new_buffer + avail,
247 fp->_IO_save_end + least_mark,
248 -least_mark),
249 fp->_IO_read_base,
250 end_p - fp->_IO_read_base);
251 }
252 else
253 memcpy (new_buffer + avail,
254 fp->_IO_read_base + least_mark,
255 needed_size);
256 free (fp->_IO_save_base);
257 fp->_IO_save_base = new_buffer;
258 fp->_IO_save_end = new_buffer + avail + needed_size;
259 }
260 else
261 {
262 avail = current_Bsize - needed_size;
263 if (least_mark < 0)
264 {
265 memmove (fp->_IO_save_base + avail,
266 fp->_IO_save_end + least_mark,
267 -least_mark);
268 memcpy (fp->_IO_save_base + avail - least_mark,
269 fp->_IO_read_base,
270 end_p - fp->_IO_read_base);
271 }
272 else if (needed_size > 0)
273 memcpy (fp->_IO_save_base + avail,
274 fp->_IO_read_base + least_mark,
275 needed_size);
276 }
277 fp->_IO_backup_base = fp->_IO_save_base + avail;
278 /* Adjust all the streammarkers. */
279 delta = end_p - fp->_IO_read_base;
280 for (mark = fp->_markers; mark != NULL; mark = mark->_next)
281 mark->_pos -= delta;
282 return 0;
283 }
284
285 int
286 __underflow (FILE *fp)
287 {
288 if (_IO_vtable_offset (fp) == 0 && _IO_fwide (fp, -1) != -1)
289 return EOF;
290
291 if (fp->_mode == 0)
292 _IO_fwide (fp, -1);
293 if (_IO_in_put_mode (fp))
294 if (_IO_switch_to_get_mode (fp) == EOF)
295 return EOF;
296 if (fp->_IO_read_ptr < fp->_IO_read_end)
297 return *(unsigned char *) fp->_IO_read_ptr;
298 if (_IO_in_backup (fp))
299 {
300 _IO_switch_to_main_get_area (fp);
301 if (fp->_IO_read_ptr < fp->_IO_read_end)
302 return *(unsigned char *) fp->_IO_read_ptr;
303 }
304 if (_IO_have_markers (fp))
305 {
306 if (save_for_backup (fp, fp->_IO_read_end))
307 return EOF;
308 }
309 else if (_IO_have_backup (fp))
310 _IO_free_backup_area (fp);
311 return _IO_UNDERFLOW (fp);
312 }
313 libc_hidden_def (__underflow)
314
315 int
316 __uflow (FILE *fp)
317 {
318 if (_IO_vtable_offset (fp) == 0 && _IO_fwide (fp, -1) != -1)
319 return EOF;
320
321 if (fp->_mode == 0)
322 _IO_fwide (fp, -1);
323 if (_IO_in_put_mode (fp))
324 if (_IO_switch_to_get_mode (fp) == EOF)
325 return EOF;
326 if (fp->_IO_read_ptr < fp->_IO_read_end)
327 return *(unsigned char *) fp->_IO_read_ptr++;
328 if (_IO_in_backup (fp))
329 {
330 _IO_switch_to_main_get_area (fp);
331 if (fp->_IO_read_ptr < fp->_IO_read_end)
332 return *(unsigned char *) fp->_IO_read_ptr++;
333 }
334 if (_IO_have_markers (fp))
335 {
336 if (save_for_backup (fp, fp->_IO_read_end))
337 return EOF;
338 }
339 else if (_IO_have_backup (fp))
340 _IO_free_backup_area (fp);
341 return _IO_UFLOW (fp);
342 }
343 libc_hidden_def (__uflow)
344
345 void
346 _IO_setb (FILE *f, char *b, char *eb, int a)
347 {
348 if (f->_IO_buf_base && !(f->_flags & _IO_USER_BUF))
349 free (f->_IO_buf_base);
350 f->_IO_buf_base = b;
351 f->_IO_buf_end = eb;
352 if (a)
353 f->_flags &= ~_IO_USER_BUF;
354 else
355 f->_flags |= _IO_USER_BUF;
356 }
357 libc_hidden_def (_IO_setb)
358
359 void
360 _IO_doallocbuf (FILE *fp)
361 {
362 if (fp->_IO_buf_base)
363 return;
364 if (!(fp->_flags & _IO_UNBUFFERED) || fp->_mode > 0)
365 if (_IO_DOALLOCATE (fp) != EOF)
366 return;
367 _IO_setb (fp, fp->_shortbuf, fp->_shortbuf+1, 0);
368 }
369 libc_hidden_def (_IO_doallocbuf)
370
371 int
372 _IO_default_underflow (FILE *fp)
373 {
374 return EOF;
375 }
376
377 int
378 _IO_default_uflow (FILE *fp)
379 {
380 int ch = _IO_UNDERFLOW (fp);
381 if (ch == EOF)
382 return EOF;
383 return *(unsigned char *) fp->_IO_read_ptr++;
384 }
385 libc_hidden_def (_IO_default_uflow)
386
387 size_t
388 _IO_default_xsputn (FILE *f, const void *data, size_t n)
389 {
390 const char *s = (char *) data;
391 size_t more = n;
392 if (more <= 0)
393 return 0;
394 for (;;)
395 {
396 /* Space available. */
397 if (f->_IO_write_ptr < f->_IO_write_end)
398 {
399 size_t count = f->_IO_write_end - f->_IO_write_ptr;
400 if (count > more)
401 count = more;
402 if (count > 20)
403 {
404 f->_IO_write_ptr = __mempcpy (f->_IO_write_ptr, s, count);
405 s += count;
406 }
407 else if (count)
408 {
409 char *p = f->_IO_write_ptr;
410 ssize_t i;
411 for (i = count; --i >= 0; )
412 *p++ = *s++;
413 f->_IO_write_ptr = p;
414 }
415 more -= count;
416 }
417 if (more == 0 || _IO_OVERFLOW (f, (unsigned char) *s++) == EOF)
418 break;
419 more--;
420 }
421 return n - more;
422 }
423 libc_hidden_def (_IO_default_xsputn)
424
425 size_t
426 _IO_sgetn (FILE *fp, void *data, size_t n)
427 {
428 /* FIXME handle putback buffer here! */
429 return _IO_XSGETN (fp, data, n);
430 }
431 libc_hidden_def (_IO_sgetn)
432
433 size_t
434 _IO_default_xsgetn (FILE *fp, void *data, size_t n)
435 {
436 size_t more = n;
437 char *s = (char*) data;
438 for (;;)
439 {
440 /* Data available. */
441 if (fp->_IO_read_ptr < fp->_IO_read_end)
442 {
443 size_t count = fp->_IO_read_end - fp->_IO_read_ptr;
444 if (count > more)
445 count = more;
446 if (count > 20)
447 {
448 s = __mempcpy (s, fp->_IO_read_ptr, count);
449 fp->_IO_read_ptr += count;
450 }
451 else if (count)
452 {
453 char *p = fp->_IO_read_ptr;
454 int i = (int) count;
455 while (--i >= 0)
456 *s++ = *p++;
457 fp->_IO_read_ptr = p;
458 }
459 more -= count;
460 }
461 if (more == 0 || __underflow (fp) == EOF)
462 break;
463 }
464 return n - more;
465 }
466 libc_hidden_def (_IO_default_xsgetn)
467
468 #if 0
469 /* Seems not to be needed. --drepper */
470 int
471 _IO_sync (FILE *fp)
472 {
473 return 0;
474 }
475 #endif
476
477 FILE *
478 _IO_default_setbuf (FILE *fp, char *p, ssize_t len)
479 {
480 if (_IO_SYNC (fp) == EOF)
481 return NULL;
482 if (p == NULL || len == 0)
483 {
484 fp->_flags |= _IO_UNBUFFERED;
485 _IO_setb (fp, fp->_shortbuf, fp->_shortbuf+1, 0);
486 }
487 else
488 {
489 fp->_flags &= ~_IO_UNBUFFERED;
490 _IO_setb (fp, p, p+len, 0);
491 }
492 fp->_IO_write_base = fp->_IO_write_ptr = fp->_IO_write_end = 0;
493 fp->_IO_read_base = fp->_IO_read_ptr = fp->_IO_read_end = 0;
494 return fp;
495 }
496
497 off64_t
498 _IO_default_seekpos (FILE *fp, off64_t pos, int mode)
499 {
500 return _IO_SEEKOFF (fp, pos, 0, mode);
501 }
502
503 int
504 _IO_default_doallocate (FILE *fp)
505 {
506 char *buf;
507
508 buf = malloc(BUFSIZ);
509 if (__glibc_unlikely (buf == NULL))
510 return EOF;
511
512 _IO_setb (fp, buf, buf+BUFSIZ, 1);
513 return 1;
514 }
515 libc_hidden_def (_IO_default_doallocate)
516
517 void
518 _IO_init_internal (FILE *fp, int flags)
519 {
520 _IO_no_init (fp, flags, -1, NULL, NULL);
521 }
522
523 void
524 _IO_init (FILE *fp, int flags)
525 {
526 IO_set_accept_foreign_vtables (&_IO_vtable_check);
527 _IO_init_internal (fp, flags);
528 }
529
530 static int stdio_needs_locking;
531
532 /* In a single-threaded process most stdio locks can be omitted. After
533 _IO_enable_locks is called, locks are not optimized away any more.
534 It must be first called while the process is still single-threaded.
535
536 This lock optimization can be disabled on a per-file basis by setting
537 _IO_FLAGS2_NEED_LOCK, because a file can have user-defined callbacks
538 or can be locked with flockfile and then a thread may be created
539 between a lock and unlock, so omitting the lock is not valid.
540
541 Here we have to make sure that the flag is set on all existing files
542 and files created later. */
543 void
544 _IO_enable_locks (void)
545 {
546 _IO_ITER i;
547
548 if (stdio_needs_locking)
549 return;
550 stdio_needs_locking = 1;
551 for (i = _IO_iter_begin (); i != _IO_iter_end (); i = _IO_iter_next (i))
552 _IO_iter_file (i)->_flags2 |= _IO_FLAGS2_NEED_LOCK;
553 }
554 libc_hidden_def (_IO_enable_locks)
555
556 void
557 _IO_old_init (FILE *fp, int flags)
558 {
559 fp->_flags = _IO_MAGIC|flags;
560 fp->_flags2 = 0;
561 if (stdio_needs_locking)
562 fp->_flags2 |= _IO_FLAGS2_NEED_LOCK;
563 fp->_IO_buf_base = NULL;
564 fp->_IO_buf_end = NULL;
565 fp->_IO_read_base = NULL;
566 fp->_IO_read_ptr = NULL;
567 fp->_IO_read_end = NULL;
568 fp->_IO_write_base = NULL;
569 fp->_IO_write_ptr = NULL;
570 fp->_IO_write_end = NULL;
571 fp->_chain = NULL; /* Not necessary. */
572
573 fp->_IO_save_base = NULL;
574 fp->_IO_backup_base = NULL;
575 fp->_IO_save_end = NULL;
576 fp->_markers = NULL;
577 fp->_cur_column = 0;
578 #if _IO_JUMPS_OFFSET
579 fp->_vtable_offset = 0;
580 #endif
581 #ifdef _IO_MTSAFE_IO
582 if (fp->_lock != NULL)
583 _IO_lock_init (*fp->_lock);
584 #endif
585 }
586
587 void
588 _IO_no_init (FILE *fp, int flags, int orientation,
589 struct _IO_wide_data *wd, const struct _IO_jump_t *jmp)
590 {
591 _IO_old_init (fp, flags);
592 fp->_mode = orientation;
593 if (orientation >= 0)
594 {
595 fp->_wide_data = wd;
596 fp->_wide_data->_IO_buf_base = NULL;
597 fp->_wide_data->_IO_buf_end = NULL;
598 fp->_wide_data->_IO_read_base = NULL;
599 fp->_wide_data->_IO_read_ptr = NULL;
600 fp->_wide_data->_IO_read_end = NULL;
601 fp->_wide_data->_IO_write_base = NULL;
602 fp->_wide_data->_IO_write_ptr = NULL;
603 fp->_wide_data->_IO_write_end = NULL;
604 fp->_wide_data->_IO_save_base = NULL;
605 fp->_wide_data->_IO_backup_base = NULL;
606 fp->_wide_data->_IO_save_end = NULL;
607
608 fp->_wide_data->_wide_vtable = jmp;
609 }
610 else
611 /* Cause predictable crash when a wide function is called on a byte
612 stream. */
613 fp->_wide_data = (struct _IO_wide_data *) -1L;
614 fp->_freeres_list = NULL;
615 }
616
617 int
618 _IO_default_sync (FILE *fp)
619 {
620 return 0;
621 }
622
623 /* The way the C++ classes are mapped into the C functions in the
624 current implementation, this function can get called twice! */
625
626 void
627 _IO_default_finish (FILE *fp, int dummy)
628 {
629 struct _IO_marker *mark;
630 if (fp->_IO_buf_base && !(fp->_flags & _IO_USER_BUF))
631 {
632 free (fp->_IO_buf_base);
633 fp->_IO_buf_base = fp->_IO_buf_end = NULL;
634 }
635
636 for (mark = fp->_markers; mark != NULL; mark = mark->_next)
637 mark->_sbuf = NULL;
638
639 if (fp->_IO_save_base)
640 {
641 free (fp->_IO_save_base);
642 fp->_IO_save_base = NULL;
643 }
644
645 _IO_un_link ((struct _IO_FILE_plus *) fp);
646
647 #ifdef _IO_MTSAFE_IO
648 if (fp->_lock != NULL)
649 _IO_lock_fini (*fp->_lock);
650 #endif
651 }
652 libc_hidden_def (_IO_default_finish)
653
654 off64_t
655 _IO_default_seekoff (FILE *fp, off64_t offset, int dir, int mode)
656 {
657 return _IO_pos_BAD;
658 }
659
660 int
661 _IO_sputbackc (FILE *fp, int c)
662 {
663 int result;
664
665 if (fp->_IO_read_ptr > fp->_IO_read_base
666 && (unsigned char)fp->_IO_read_ptr[-1] == (unsigned char)c)
667 {
668 fp->_IO_read_ptr--;
669 result = (unsigned char) c;
670 }
671 else
672 result = _IO_PBACKFAIL (fp, c);
673
674 if (result != EOF)
675 fp->_flags &= ~_IO_EOF_SEEN;
676
677 return result;
678 }
679 libc_hidden_def (_IO_sputbackc)
680
681 int
682 _IO_sungetc (FILE *fp)
683 {
684 int result;
685
686 if (fp->_IO_read_ptr > fp->_IO_read_base)
687 {
688 fp->_IO_read_ptr--;
689 result = (unsigned char) *fp->_IO_read_ptr;
690 }
691 else
692 result = _IO_PBACKFAIL (fp, EOF);
693
694 if (result != EOF)
695 fp->_flags &= ~_IO_EOF_SEEN;
696
697 return result;
698 }
699
700 #if 0 /* Work in progress */
701 /* Seems not to be needed. */
702 #if 0
703 void
704 _IO_set_column (FILE *fp, int c)
705 {
706 if (c == -1)
707 fp->_column = -1;
708 else
709 fp->_column = c - (fp->_IO_write_ptr - fp->_IO_write_base);
710 }
711 #else
712 int
713 _IO_set_column (FILE *fp, int i)
714 {
715 fp->_cur_column = i + 1;
716 return 0;
717 }
718 #endif
719 #endif
720
721
722 unsigned
723 _IO_adjust_column (unsigned start, const char *line, int count)
724 {
725 const char *ptr = line + count;
726 while (ptr > line)
727 if (*--ptr == '\n')
728 return line + count - ptr - 1;
729 return start + count;
730 }
731 libc_hidden_def (_IO_adjust_column)
732
733 #if 0
734 /* Seems not to be needed. --drepper */
735 int
736 _IO_get_column (FILE *fp)
737 {
738 if (fp->_cur_column)
739 return _IO_adjust_column (fp->_cur_column - 1,
740 fp->_IO_write_base,
741 fp->_IO_write_ptr - fp->_IO_write_base);
742 return -1;
743 }
744 #endif
745
746
747 int
748 _IO_flush_all_lockp (int do_lock)
749 {
750 int result = 0;
751 FILE *fp;
752
753 #ifdef _IO_MTSAFE_IO
754 _IO_cleanup_region_start_noarg (flush_cleanup);
755 _IO_lock_lock (list_all_lock);
756 #endif
757
758 for (fp = (FILE *) _IO_list_all; fp != NULL; fp = fp->_chain)
759 {
760 run_fp = fp;
761 if (do_lock)
762 _IO_flockfile (fp);
763
764 if (((fp->_mode <= 0 && fp->_IO_write_ptr > fp->_IO_write_base)
765 || (_IO_vtable_offset (fp) == 0
766 && fp->_mode > 0 && (fp->_wide_data->_IO_write_ptr
767 > fp->_wide_data->_IO_write_base))
768 )
769 && _IO_OVERFLOW (fp, EOF) == EOF)
770 result = EOF;
771
772 if (do_lock)
773 _IO_funlockfile (fp);
774 run_fp = NULL;
775 }
776
777 #ifdef _IO_MTSAFE_IO
778 _IO_lock_unlock (list_all_lock);
779 _IO_cleanup_region_end (0);
780 #endif
781
782 return result;
783 }
784
785
786 int
787 _IO_flush_all (void)
788 {
789 /* We want locking. */
790 return _IO_flush_all_lockp (1);
791 }
792 libc_hidden_def (_IO_flush_all)
793
794 void
795 _IO_flush_all_linebuffered (void)
796 {
797 FILE *fp;
798
799 #ifdef _IO_MTSAFE_IO
800 _IO_cleanup_region_start_noarg (flush_cleanup);
801 _IO_lock_lock (list_all_lock);
802 #endif
803
804 for (fp = (FILE *) _IO_list_all; fp != NULL; fp = fp->_chain)
805 {
806 run_fp = fp;
807 _IO_flockfile (fp);
808
809 if ((fp->_flags & _IO_NO_WRITES) == 0 && fp->_flags & _IO_LINE_BUF)
810 _IO_OVERFLOW (fp, EOF);
811
812 _IO_funlockfile (fp);
813 run_fp = NULL;
814 }
815
816 #ifdef _IO_MTSAFE_IO
817 _IO_lock_unlock (list_all_lock);
818 _IO_cleanup_region_end (0);
819 #endif
820 }
821 libc_hidden_def (_IO_flush_all_linebuffered)
822 weak_alias (_IO_flush_all_linebuffered, _flushlbf)
823
824
825 /* The following is a bit tricky. In general, we want to unbuffer the
826 streams so that all output which follows is seen. If we are not
827 looking for memory leaks it does not make much sense to free the
828 actual buffer because this will happen anyway once the program
829 terminated. If we do want to look for memory leaks we have to free
830 the buffers. Whether something is freed is determined by the
831 function sin the libc_freeres section. Those are called as part of
832 the atexit routine, just like _IO_cleanup. The problem is we do
833 not know whether the freeres code is called first or _IO_cleanup.
834 if the former is the case, we set the DEALLOC_BUFFER variable to
835 true and _IO_unbuffer_all will take care of the rest. If
836 _IO_unbuffer_all is called first we add the streams to a list
837 which the freeres function later can walk through. */
838 static void _IO_unbuffer_all (void);
839
840 static bool dealloc_buffers;
841 static FILE *freeres_list;
842
843 static void
844 _IO_unbuffer_all (void)
845 {
846 FILE *fp;
847
848 #ifdef _IO_MTSAFE_IO
849 _IO_cleanup_region_start_noarg (flush_cleanup);
850 _IO_lock_lock (list_all_lock);
851 #endif
852
853 for (fp = (FILE *) _IO_list_all; fp; fp = fp->_chain)
854 {
855 if (! (fp->_flags & _IO_UNBUFFERED)
856 /* Iff stream is un-orientated, it wasn't used. */
857 && fp->_mode != 0)
858 {
859 #ifdef _IO_MTSAFE_IO
860 int cnt;
861 #define MAXTRIES 2
862 for (cnt = 0; cnt < MAXTRIES; ++cnt)
863 if (fp->_lock == NULL || _IO_lock_trylock (*fp->_lock) == 0)
864 break;
865 else
866 /* Give the other thread time to finish up its use of the
867 stream. */
868 __sched_yield ();
869 #endif
870
871 if (! dealloc_buffers && !(fp->_flags & _IO_USER_BUF))
872 {
873 fp->_flags |= _IO_USER_BUF;
874
875 fp->_freeres_list = freeres_list;
876 freeres_list = fp;
877 fp->_freeres_buf = fp->_IO_buf_base;
878 }
879
880 _IO_SETBUF (fp, NULL, 0);
881
882 if (fp->_mode > 0)
883 _IO_wsetb (fp, NULL, NULL, 0);
884
885 #ifdef _IO_MTSAFE_IO
886 if (cnt < MAXTRIES && fp->_lock != NULL)
887 _IO_lock_unlock (*fp->_lock);
888 #endif
889 }
890
891 /* Make sure that never again the wide char functions can be
892 used. */
893 fp->_mode = -1;
894 }
895
896 #ifdef _IO_MTSAFE_IO
897 _IO_lock_unlock (list_all_lock);
898 _IO_cleanup_region_end (0);
899 #endif
900 }
901
902
903 libc_freeres_fn (buffer_free)
904 {
905 dealloc_buffers = true;
906
907 while (freeres_list != NULL)
908 {
909 free (freeres_list->_freeres_buf);
910
911 freeres_list = freeres_list->_freeres_list;
912 }
913 }
914
915
916 int
917 _IO_cleanup (void)
918 {
919 /* We do *not* want locking. Some threads might use streams but
920 that is their problem, we flush them underneath them. */
921 int result = _IO_flush_all_lockp (0);
922
923 /* We currently don't have a reliable mechanism for making sure that
924 C++ static destructors are executed in the correct order.
925 So it is possible that other static destructors might want to
926 write to cout - and they're supposed to be able to do so.
927
928 The following will make the standard streambufs be unbuffered,
929 which forces any output from late destructors to be written out. */
930 _IO_unbuffer_all ();
931
932 return result;
933 }
934
935
936 void
937 _IO_init_marker (struct _IO_marker *marker, FILE *fp)
938 {
939 marker->_sbuf = fp;
940 if (_IO_in_put_mode (fp))
941 _IO_switch_to_get_mode (fp);
942 if (_IO_in_backup (fp))
943 marker->_pos = fp->_IO_read_ptr - fp->_IO_read_end;
944 else
945 marker->_pos = fp->_IO_read_ptr - fp->_IO_read_base;
946
947 /* Should perhaps sort the chain? */
948 marker->_next = fp->_markers;
949 fp->_markers = marker;
950 }
951
952 void
953 _IO_remove_marker (struct _IO_marker *marker)
954 {
955 /* Unlink from sb's chain. */
956 struct _IO_marker **ptr = &marker->_sbuf->_markers;
957 for (; ; ptr = &(*ptr)->_next)
958 {
959 if (*ptr == NULL)
960 break;
961 else if (*ptr == marker)
962 {
963 *ptr = marker->_next;
964 return;
965 }
966 }
967 #if 0
968 if _sbuf has a backup area that is no longer needed, should we delete
969 it now, or wait until the next underflow?
970 #endif
971 }
972
973 #define BAD_DELTA EOF
974
975 int
976 _IO_marker_difference (struct _IO_marker *mark1, struct _IO_marker *mark2)
977 {
978 return mark1->_pos - mark2->_pos;
979 }
980
981 /* Return difference between MARK and current position of MARK's stream. */
982 int
983 _IO_marker_delta (struct _IO_marker *mark)
984 {
985 int cur_pos;
986 if (mark->_sbuf == NULL)
987 return BAD_DELTA;
988 if (_IO_in_backup (mark->_sbuf))
989 cur_pos = mark->_sbuf->_IO_read_ptr - mark->_sbuf->_IO_read_end;
990 else
991 cur_pos = mark->_sbuf->_IO_read_ptr - mark->_sbuf->_IO_read_base;
992 return mark->_pos - cur_pos;
993 }
994
995 int
996 _IO_seekmark (FILE *fp, struct _IO_marker *mark, int delta)
997 {
998 if (mark->_sbuf != fp)
999 return EOF;
1000 if (mark->_pos >= 0)
1001 {
1002 if (_IO_in_backup (fp))
1003 _IO_switch_to_main_get_area (fp);
1004 fp->_IO_read_ptr = fp->_IO_read_base + mark->_pos;
1005 }
1006 else
1007 {
1008 if (!_IO_in_backup (fp))
1009 _IO_switch_to_backup_area (fp);
1010 fp->_IO_read_ptr = fp->_IO_read_end + mark->_pos;
1011 }
1012 return 0;
1013 }
1014
1015 void
1016 _IO_unsave_markers (FILE *fp)
1017 {
1018 struct _IO_marker *mark = fp->_markers;
1019 if (mark)
1020 {
1021 #ifdef TODO
1022 streampos offset = seekoff (0, ios::cur, ios::in);
1023 if (offset != EOF)
1024 {
1025 offset += eGptr () - Gbase ();
1026 for ( ; mark != NULL; mark = mark->_next)
1027 mark->set_streampos (mark->_pos + offset);
1028 }
1029 else
1030 {
1031 for ( ; mark != NULL; mark = mark->_next)
1032 mark->set_streampos (EOF);
1033 }
1034 #endif
1035 fp->_markers = 0;
1036 }
1037
1038 if (_IO_have_backup (fp))
1039 _IO_free_backup_area (fp);
1040 }
1041 libc_hidden_def (_IO_unsave_markers)
1042
1043 #if 0
1044 /* Seems not to be needed. --drepper */
1045 int
1046 _IO_nobackup_pbackfail (FILE *fp, int c)
1047 {
1048 if (fp->_IO_read_ptr > fp->_IO_read_base)
1049 fp->_IO_read_ptr--;
1050 if (c != EOF && *fp->_IO_read_ptr != c)
1051 *fp->_IO_read_ptr = c;
1052 return (unsigned char) c;
1053 }
1054 #endif
1055
1056 int
1057 _IO_default_pbackfail (FILE *fp, int c)
1058 {
1059 if (fp->_IO_read_ptr > fp->_IO_read_base && !_IO_in_backup (fp)
1060 && (unsigned char) fp->_IO_read_ptr[-1] == c)
1061 --fp->_IO_read_ptr;
1062 else
1063 {
1064 /* Need to handle a filebuf in write mode (switch to read mode). FIXME!*/
1065 if (!_IO_in_backup (fp))
1066 {
1067 /* We need to keep the invariant that the main get area
1068 logically follows the backup area. */
1069 if (fp->_IO_read_ptr > fp->_IO_read_base && _IO_have_backup (fp))
1070 {
1071 if (save_for_backup (fp, fp->_IO_read_ptr))
1072 return EOF;
1073 }
1074 else if (!_IO_have_backup (fp))
1075 {
1076 /* No backup buffer: allocate one. */
1077 /* Use nshort buffer, if unused? (probably not) FIXME */
1078 int backup_size = 128;
1079 char *bbuf = (char *) malloc (backup_size);
1080 if (bbuf == NULL)
1081 return EOF;
1082 fp->_IO_save_base = bbuf;
1083 fp->_IO_save_end = fp->_IO_save_base + backup_size;
1084 fp->_IO_backup_base = fp->_IO_save_end;
1085 }
1086 fp->_IO_read_base = fp->_IO_read_ptr;
1087 _IO_switch_to_backup_area (fp);
1088 }
1089 else if (fp->_IO_read_ptr <= fp->_IO_read_base)
1090 {
1091 /* Increase size of existing backup buffer. */
1092 size_t new_size;
1093 size_t old_size = fp->_IO_read_end - fp->_IO_read_base;
1094 char *new_buf;
1095 new_size = 2 * old_size;
1096 new_buf = (char *) malloc (new_size);
1097 if (new_buf == NULL)
1098 return EOF;
1099 memcpy (new_buf + (new_size - old_size), fp->_IO_read_base,
1100 old_size);
1101 free (fp->_IO_read_base);
1102 _IO_setg (fp, new_buf, new_buf + (new_size - old_size),
1103 new_buf + new_size);
1104 fp->_IO_backup_base = fp->_IO_read_ptr;
1105 }
1106
1107 *--fp->_IO_read_ptr = c;
1108 }
1109 return (unsigned char) c;
1110 }
1111 libc_hidden_def (_IO_default_pbackfail)
1112
1113 off64_t
1114 _IO_default_seek (FILE *fp, off64_t offset, int dir)
1115 {
1116 return _IO_pos_BAD;
1117 }
1118
1119 int
1120 _IO_default_stat (FILE *fp, void *st)
1121 {
1122 return EOF;
1123 }
1124
1125 ssize_t
1126 _IO_default_read (FILE *fp, void *data, ssize_t n)
1127 {
1128 return -1;
1129 }
1130
1131 ssize_t
1132 _IO_default_write (FILE *fp, const void *data, ssize_t n)
1133 {
1134 return 0;
1135 }
1136
1137 int
1138 _IO_default_showmanyc (FILE *fp)
1139 {
1140 return -1;
1141 }
1142
1143 void
1144 _IO_default_imbue (FILE *fp, void *locale)
1145 {
1146 }
1147
1148 _IO_ITER
1149 _IO_iter_begin (void)
1150 {
1151 return (_IO_ITER) _IO_list_all;
1152 }
1153 libc_hidden_def (_IO_iter_begin)
1154
1155 _IO_ITER
1156 _IO_iter_end (void)
1157 {
1158 return NULL;
1159 }
1160 libc_hidden_def (_IO_iter_end)
1161
1162 _IO_ITER
1163 _IO_iter_next (_IO_ITER iter)
1164 {
1165 return iter->_chain;
1166 }
1167 libc_hidden_def (_IO_iter_next)
1168
1169 FILE *
1170 _IO_iter_file (_IO_ITER iter)
1171 {
1172 return iter;
1173 }
1174 libc_hidden_def (_IO_iter_file)
1175
1176 void
1177 _IO_list_lock (void)
1178 {
1179 #ifdef _IO_MTSAFE_IO
1180 _IO_lock_lock (list_all_lock);
1181 #endif
1182 }
1183 libc_hidden_def (_IO_list_lock)
1184
1185 void
1186 _IO_list_unlock (void)
1187 {
1188 #ifdef _IO_MTSAFE_IO
1189 _IO_lock_unlock (list_all_lock);
1190 #endif
1191 }
1192 libc_hidden_def (_IO_list_unlock)
1193
1194 void
1195 _IO_list_resetlock (void)
1196 {
1197 #ifdef _IO_MTSAFE_IO
1198 _IO_lock_init (list_all_lock);
1199 #endif
1200 }
1201 libc_hidden_def (_IO_list_resetlock)
1202
1203
1204 #ifdef TODO
1205 #if defined(linux)
1206 #define IO_CLEANUP ;
1207 #endif
1208
1209 #ifdef IO_CLEANUP
1210 IO_CLEANUP
1211 #else
1212 struct __io_defs {
1213 __io_defs() { }
1214 ~__io_defs() { _IO_cleanup (); }
1215 };
1216 __io_defs io_defs__;
1217 #endif
1218
1219 #endif /* TODO */
1220
1221 #ifdef text_set_element
1222 text_set_element(__libc_atexit, _IO_cleanup);
1223 #endif