Update.
[glibc.git] / libio / obprintf.c
blob396bb279a0a2d527b13fc167a94e85cafa69d303
1 /* Print output of stream to given obstack.
2 Copyright (C) 1996, 1997, 1999, 2000 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 Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 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 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 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>
33 struct _IO_obstack_file
35 struct _IO_FILE_plus file;
36 struct obstack *obstack;
40 static int
41 _IO_obstack_overflow (_IO_FILE *fp, int c)
43 struct obstack *obstack = ((struct _IO_obstack_file *) fp)->obstack;
44 int size;
46 /* Make room for another character. This might as well allocate a
47 new chunk a memory and moves the old contents over. */
48 assert (c != EOF);
49 obstack_1grow (obstack, c);
51 /* Setup the buffer pointers again. */
52 fp->_IO_write_base = obstack_base (obstack);
53 fp->_IO_write_ptr = obstack_next_free (obstack);
54 size = obstack_room (obstack);
55 fp->_IO_write_end = fp->_IO_write_ptr + size;
56 /* Now allocate the rest of the current chunk. */
57 obstack_blank_fast (obstack, size);
59 return c;
63 static _IO_size_t
64 _IO_obstack_xsputn (_IO_FILE *fp, const void *data, _IO_size_t n)
66 struct obstack *obstack = ((struct _IO_obstack_file *) fp)->obstack;
68 if (fp->_IO_write_ptr + n > fp->_IO_write_end)
70 int size;
72 /* We need some more memory. First shrink the buffer to the
73 space we really currently need. */
74 obstack_blank_fast (obstack, fp->_IO_write_ptr - fp->_IO_write_end);
76 /* Now grow for N bytes, and put the data there. */
77 obstack_grow (obstack, data, n);
79 /* Setup the buffer pointers again. */
80 fp->_IO_write_base = obstack_base (obstack);
81 fp->_IO_write_ptr = obstack_next_free (obstack);
82 size = obstack_room (obstack);
83 fp->_IO_write_end = fp->_IO_write_ptr + size;
84 /* Now allocate the rest of the current chunk. */
85 obstack_blank_fast (obstack, size);
87 else
88 fp->_IO_write_ptr = __mempcpy (fp->_IO_write_ptr, data, n);
90 return n;
94 /* the jump table. */
95 static struct _IO_jump_t _IO_obstack_jumps =
97 JUMP_INIT_DUMMY,
98 JUMP_INIT(finish, NULL),
99 JUMP_INIT(overflow, _IO_obstack_overflow),
100 JUMP_INIT(underflow, NULL),
101 JUMP_INIT(uflow, NULL),
102 JUMP_INIT(pbackfail, NULL),
103 JUMP_INIT(xsputn, _IO_obstack_xsputn),
104 JUMP_INIT(xsgetn, NULL),
105 JUMP_INIT(seekoff, NULL),
106 JUMP_INIT(seekpos, NULL),
107 JUMP_INIT(setbuf, NULL),
108 JUMP_INIT(sync, NULL),
109 JUMP_INIT(doallocate, NULL),
110 JUMP_INIT(read, NULL),
111 JUMP_INIT(write, NULL),
112 JUMP_INIT(seek, NULL),
113 JUMP_INIT(close, NULL),
114 JUMP_INIT(stat, NULL),
115 JUMP_INIT(showmanyc, NULL),
116 JUMP_INIT(imbue, NULL)
121 _IO_obstack_vprintf (struct obstack *obstack, const char *format, va_list args)
123 struct obstack_FILE
125 struct _IO_obstack_file ofile;
126 #ifdef _IO_MTSAFE_IO
127 _IO_lock_t lock;
128 #endif
129 } new_f;
130 int result;
131 int size;
132 int room;
134 #ifdef _IO_MTSAFE_IO
135 new_f.ofile.file.file._lock = &new_f.lock;
136 #endif
138 _IO_no_init (&new_f.ofile.file.file, 0, -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 ((struct _IO_strfile_ *) &new_f.ofile, obstack_base (obstack),
159 size, obstack_next_free (obstack));
160 /* Now allocate the rest of the current chunk. */
161 assert (size == (new_f.ofile.file.file._IO_write_end
162 - new_f.ofile.file.file._IO_write_base));
163 assert (new_f.ofile.file.file._IO_write_ptr
164 == (new_f.ofile.file.file._IO_write_base
165 + obstack_object_size (obstack)));
166 obstack_blank_fast (obstack, room);
168 new_f.ofile.obstack = obstack;
170 result = _IO_vfprintf ((_IO_FILE *) &new_f.ofile.file, format, args);
172 /* Shrink the buffer to the space we really currently need. */
173 obstack_blank_fast (obstack, (new_f.ofile.file.file._IO_write_ptr
174 - new_f.ofile.file.file._IO_write_end));
176 return result;
178 #ifdef weak_alias
179 weak_alias (_IO_obstack_vprintf, obstack_vprintf)
180 #endif
184 _IO_obstack_printf (struct obstack *obstack, const char *format, ...)
186 int result;
187 va_list ap;
188 va_start (ap, format);
189 result = _IO_obstack_vprintf (obstack, format, ap);
190 va_end (ap);
191 return result;
193 #ifdef weak_alias
194 weak_alias (_IO_obstack_printf, obstack_printf)
195 #endif