* pthreadP.h (PTHREAD_RWLOCK_PREFER_READER_P): Define.
[glibc/pb-stable.git] / libio / obprintf.c
blob0a50c8e0fcc5dd75956f556d1336055f8f08b3da
1 /* Print output of stream to given obstack.
2 Copyright (C) 1996,1997,1999,2000,2001,2002,2003,2004,2005,2006
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, write to the Free
19 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307 USA. */
23 #ifdef __STDC__
24 #include <stdlib.h>
25 #endif
26 #include "libioP.h"
27 #include "strfile.h"
28 #include <assert.h>
29 #include <string.h>
30 #include <errno.h>
31 #include <obstack.h>
32 #include <stdarg.h>
33 #include <stdio_ext.h>
36 struct _IO_obstack_file
38 struct _IO_FILE_plus file;
39 struct obstack *obstack;
43 static int
44 _IO_obstack_overflow (_IO_FILE *fp, int c)
46 struct obstack *obstack = ((struct _IO_obstack_file *) fp)->obstack;
47 int size;
49 /* Make room for another character. This might as well allocate a
50 new chunk a memory and moves the old contents over. */
51 assert (c != EOF);
52 obstack_1grow (obstack, c);
54 /* Setup the buffer pointers again. */
55 fp->_IO_write_base = obstack_base (obstack);
56 fp->_IO_write_ptr = obstack_next_free (obstack);
57 size = obstack_room (obstack);
58 fp->_IO_write_end = fp->_IO_write_ptr + size;
59 /* Now allocate the rest of the current chunk. */
60 obstack_blank_fast (obstack, size);
62 return c;
66 static _IO_size_t
67 _IO_obstack_xsputn (_IO_FILE *fp, const void *data, _IO_size_t n)
69 struct obstack *obstack = ((struct _IO_obstack_file *) fp)->obstack;
71 if (fp->_IO_write_ptr + n > fp->_IO_write_end)
73 int size;
75 /* We need some more memory. First shrink the buffer to the
76 space we really currently need. */
77 obstack_blank_fast (obstack, fp->_IO_write_ptr - fp->_IO_write_end);
79 /* Now grow for N bytes, and put the data there. */
80 obstack_grow (obstack, data, n);
82 /* Setup the buffer pointers again. */
83 fp->_IO_write_base = obstack_base (obstack);
84 fp->_IO_write_ptr = obstack_next_free (obstack);
85 size = obstack_room (obstack);
86 fp->_IO_write_end = fp->_IO_write_ptr + size;
87 /* Now allocate the rest of the current chunk. */
88 obstack_blank_fast (obstack, size);
90 else
91 fp->_IO_write_ptr = __mempcpy (fp->_IO_write_ptr, data, n);
93 return n;
97 /* the jump table. */
98 static const struct _IO_jump_t _IO_obstack_jumps =
100 JUMP_INIT_DUMMY,
101 JUMP_INIT(finish, NULL),
102 JUMP_INIT(overflow, _IO_obstack_overflow),
103 JUMP_INIT(underflow, NULL),
104 JUMP_INIT(uflow, NULL),
105 JUMP_INIT(pbackfail, NULL),
106 JUMP_INIT(xsputn, _IO_obstack_xsputn),
107 JUMP_INIT(xsgetn, NULL),
108 JUMP_INIT(seekoff, NULL),
109 JUMP_INIT(seekpos, NULL),
110 JUMP_INIT(setbuf, NULL),
111 JUMP_INIT(sync, NULL),
112 JUMP_INIT(doallocate, NULL),
113 JUMP_INIT(read, NULL),
114 JUMP_INIT(write, NULL),
115 JUMP_INIT(seek, NULL),
116 JUMP_INIT(close, NULL),
117 JUMP_INIT(stat, NULL),
118 JUMP_INIT(showmanyc, NULL),
119 JUMP_INIT(imbue, NULL)
124 _IO_obstack_vprintf (struct obstack *obstack, const char *format, va_list args)
126 struct obstack_FILE
128 struct _IO_obstack_file ofile;
129 } new_f;
130 int result;
131 int size;
132 int room;
134 #ifdef _IO_MTSAFE_IO
135 new_f.ofile.file.file._lock = NULL;
136 #endif
138 _IO_no_init (&new_f.ofile.file.file, _IO_USER_LOCK, -1, NULL, NULL);
139 _IO_JUMPS (&new_f.ofile.file) = &_IO_obstack_jumps;
140 room = obstack_room (obstack);
141 size = obstack_object_size (obstack) + room;
142 if (size == 0)
144 /* We have to handle the allocation a bit different since the
145 `_IO_str_init_static' function would handle a size of zero
146 different from what we expect. */
148 /* Get more memory. */
149 obstack_make_room (obstack, 64);
151 /* Recompute how much room we have. */
152 room = obstack_room (obstack);
153 size = room;
155 assert (size != 0);
158 _IO_str_init_static_internal ((struct _IO_strfile_ *) &new_f.ofile,
159 obstack_base (obstack),
160 size, obstack_next_free (obstack));
161 /* Now allocate the rest of the current chunk. */
162 assert (size == (new_f.ofile.file.file._IO_write_end
163 - new_f.ofile.file.file._IO_write_base));
164 assert (new_f.ofile.file.file._IO_write_ptr
165 == (new_f.ofile.file.file._IO_write_base
166 + obstack_object_size (obstack)));
167 obstack_blank_fast (obstack, room);
169 new_f.ofile.obstack = obstack;
171 result = INTUSE(_IO_vfprintf) (&new_f.ofile.file.file, format, args);
173 /* Shrink the buffer to the space we really currently need. */
174 obstack_blank_fast (obstack, (new_f.ofile.file.file._IO_write_ptr
175 - new_f.ofile.file.file._IO_write_end));
177 return result;
179 ldbl_weak_alias (_IO_obstack_vprintf, obstack_vprintf)
183 _IO_obstack_printf (struct obstack *obstack, const char *format, ...)
185 int result;
186 va_list ap;
187 va_start (ap, format);
188 result = _IO_obstack_vprintf (obstack, format, ap);
189 va_end (ap);
190 return result;
192 ldbl_weak_alias (_IO_obstack_printf, obstack_printf)