Updated to fedora-glibc-20041001T1134
[glibc/history.git] / libio / obprintf.c
blob19300e80a32f7392b05be90ef5e483b246e31e1c
1 /* Print output of stream to given obstack.
2 Copyright (C) 1996,1997,1999-2003, 2004 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
22 #ifdef __STDC__
23 #include <stdlib.h>
24 #endif
25 #include "libioP.h"
26 #include <assert.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <obstack.h>
30 #include <stdarg.h>
31 #include <stdio_ext.h>
34 struct _IO_obstack_file
36 struct _IO_FILE_plus file;
37 struct obstack *obstack;
41 static int
42 _IO_obstack_overflow (_IO_FILE *fp, int c)
44 struct obstack *obstack = ((struct _IO_obstack_file *) fp)->obstack;
45 int size;
47 /* Make room for another character. This might as well allocate a
48 new chunk a memory and moves the old contents over. */
49 assert (c != EOF);
50 obstack_1grow (obstack, c);
52 /* Setup the buffer pointers again. */
53 fp->_IO_write_base = obstack_base (obstack);
54 fp->_IO_write_ptr = obstack_next_free (obstack);
55 size = obstack_room (obstack);
56 fp->_IO_write_end = fp->_IO_write_ptr + size;
57 /* Now allocate the rest of the current chunk. */
58 obstack_blank_fast (obstack, size);
60 return c;
64 static _IO_size_t
65 _IO_obstack_xsputn (_IO_FILE *fp, const void *data, _IO_size_t n)
67 struct obstack *obstack = ((struct _IO_obstack_file *) fp)->obstack;
69 if (fp->_IO_write_ptr + n > fp->_IO_write_end)
71 int size;
73 /* We need some more memory. First shrink the buffer to the
74 space we really currently need. */
75 obstack_blank_fast (obstack, fp->_IO_write_ptr - fp->_IO_write_end);
77 /* Now grow for N bytes, and put the data there. */
78 obstack_grow (obstack, data, n);
80 /* Setup the buffer pointers again. */
81 fp->_IO_write_base = obstack_base (obstack);
82 fp->_IO_write_ptr = obstack_next_free (obstack);
83 size = obstack_room (obstack);
84 fp->_IO_write_end = fp->_IO_write_ptr + size;
85 /* Now allocate the rest of the current chunk. */
86 obstack_blank_fast (obstack, size);
88 else
89 fp->_IO_write_ptr = __mempcpy (fp->_IO_write_ptr, data, n);
91 return n;
95 /* the jump table. */
96 static const struct _IO_jump_t _IO_obstack_jumps =
98 JUMP_INIT_DUMMY,
99 JUMP_INIT(finish, NULL),
100 JUMP_INIT(overflow, _IO_obstack_overflow),
101 JUMP_INIT(underflow, NULL),
102 JUMP_INIT(uflow, NULL),
103 JUMP_INIT(pbackfail, NULL),
104 JUMP_INIT(xsputn, _IO_obstack_xsputn),
105 JUMP_INIT(xsgetn, NULL),
106 JUMP_INIT(seekoff, NULL),
107 JUMP_INIT(seekpos, NULL),
108 JUMP_INIT(setbuf, NULL),
109 JUMP_INIT(sync, NULL),
110 JUMP_INIT(doallocate, NULL),
111 JUMP_INIT(read, NULL),
112 JUMP_INIT(write, NULL),
113 JUMP_INIT(seek, NULL),
114 JUMP_INIT(close, NULL),
115 JUMP_INIT(stat, NULL),
116 JUMP_INIT(showmanyc, NULL),
117 JUMP_INIT(imbue, NULL)
122 _IO_obstack_vprintf (struct obstack *obstack, const char *format, va_list args)
124 struct obstack_FILE
126 struct _IO_obstack_file ofile;
127 } new_f;
128 int result;
129 int size;
130 int room;
132 #ifdef _IO_MTSAFE_IO
133 new_f.ofile.file.file._lock = NULL;
134 #endif
136 _IO_no_init (&new_f.ofile.file.file, _IO_USER_LOCK, -1, NULL, NULL);
137 _IO_JUMPS (&new_f.ofile.file) = &_IO_obstack_jumps;
138 room = obstack_room (obstack);
139 size = obstack_object_size (obstack) + room;
140 if (size == 0)
142 /* We have to handle the allocation a bit different since the
143 `_IO_str_init_static' function would handle a size of zero
144 different from what we expect. */
146 /* Get more memory. */
147 obstack_make_room (obstack, 64);
149 /* Recompute how much room we have. */
150 room = obstack_room (obstack);
151 size = room;
153 assert (size != 0);
156 _IO_str_init_static_internal ((struct _IO_strfile_ *) &new_f.ofile,
157 obstack_base (obstack),
158 size, obstack_next_free (obstack));
159 /* Now allocate the rest of the current chunk. */
160 assert (size == (new_f.ofile.file.file._IO_write_end
161 - new_f.ofile.file.file._IO_write_base));
162 assert (new_f.ofile.file.file._IO_write_ptr
163 == (new_f.ofile.file.file._IO_write_base
164 + obstack_object_size (obstack)));
165 obstack_blank_fast (obstack, room);
167 new_f.ofile.obstack = obstack;
169 result = INTUSE(_IO_vfprintf) (&new_f.ofile.file.file, format, args);
171 /* Shrink the buffer to the space we really currently need. */
172 obstack_blank_fast (obstack, (new_f.ofile.file.file._IO_write_ptr
173 - new_f.ofile.file.file._IO_write_end));
175 return result;
177 #ifdef weak_alias
178 weak_alias (_IO_obstack_vprintf, obstack_vprintf)
179 #endif
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 #ifdef weak_alias
193 weak_alias (_IO_obstack_printf, obstack_printf)
194 #endif