Update.
[glibc.git] / libio / obprintf.c
blob29e09d428156df90152d739784c9ab8a80065982
1 /* Print output of stream to given obstack.
2 Copyright (C) 1996, 1997, 1999 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 <string.h>
27 #include <errno.h>
28 #include <obstack.h>
29 #include <stdarg.h>
32 struct _IO_obstack_file
34 struct _IO_FILE_plus file;
35 struct obstack *obstack;
39 static int
40 _IO_obstack_overflow (_IO_FILE *fp, int c)
42 struct obstack *obstack = ((struct _IO_obstack_file *) fp)->obstack;
44 /* Make room for another character. This might as well allocate a
45 new chunk a memory and moves the old contents over. */
46 if (c != EOF)
47 obstack_1grow (obstack, c);
49 /* Setup the buffer pointers again. */
50 fp->_IO_write_base = obstack_base (obstack);
51 fp->_IO_write_ptr = obstack_next_free (obstack);
52 fp->_IO_write_end = fp->_IO_write_ptr + obstack_room (obstack);
53 /* Now allocate the rest of the current chunk. */
54 obstack_blank_fast (obstack, fp->_IO_write_end - fp->_IO_write_ptr);
56 return c;
60 static _IO_size_t
61 _IO_obstack_xsputn (_IO_FILE *fp, const void *data, _IO_size_t n)
63 struct obstack *obstack = ((struct _IO_obstack_file *) fp)->obstack;
65 if (fp->_IO_write_ptr + n > fp->_IO_write_end)
67 /* We need some more memory. First shrink the buffer to the
68 space we really currently need. */
69 obstack_blank_fast (obstack, fp->_IO_write_ptr - fp->_IO_write_end);
71 /* Now grow for N bytes, and put the data there. */
72 obstack_grow (obstack, data, n);
74 /* Setup the buffer pointers again. */
75 fp->_IO_write_base = obstack_base (obstack);
76 fp->_IO_write_ptr = obstack_next_free (obstack);
77 fp->_IO_write_end = (fp->_IO_write_ptr + obstack_room (obstack));
78 /* Now allocate the rest of the current chunk. */
79 obstack_blank_fast (obstack, fp->_IO_write_end - fp->_IO_write_ptr);
81 else
82 fp->_IO_write_ptr = __mempcpy (fp->_IO_write_ptr, data, n);
84 return n;
88 /* the jump table. */
89 static struct _IO_jump_t _IO_obstack_jumps =
91 JUMP_INIT_DUMMY,
92 JUMP_INIT(finish, NULL),
93 JUMP_INIT(overflow, _IO_obstack_overflow),
94 JUMP_INIT(underflow, NULL),
95 JUMP_INIT(uflow, NULL),
96 JUMP_INIT(pbackfail, NULL),
97 JUMP_INIT(xsputn, _IO_obstack_xsputn),
98 JUMP_INIT(xsgetn, NULL),
99 JUMP_INIT(seekoff, NULL),
100 JUMP_INIT(seekpos, NULL),
101 JUMP_INIT(setbuf, NULL),
102 JUMP_INIT(sync, NULL),
103 JUMP_INIT(doallocate, NULL),
104 JUMP_INIT(read, NULL),
105 JUMP_INIT(write, NULL),
106 JUMP_INIT(seek, NULL),
107 JUMP_INIT(close, NULL),
108 JUMP_INIT(stat, NULL),
109 JUMP_INIT(showmanyc, NULL),
110 JUMP_INIT(imbue, NULL)
115 _IO_obstack_vprintf (struct obstack *obstack, const char *format, va_list args)
117 struct obstack_FILE
119 struct _IO_obstack_file ofile;
120 #ifdef _IO_MTSAFE_IO
121 _IO_lock_t lock;
122 #endif
123 } new_f;
124 int result;
126 #ifdef _IO_MTSAFE_IO
127 new_f.ofile.file.file._lock = &new_f.lock;
128 #endif
130 _IO_no_init (&new_f.ofile.file.file, 0, -1, NULL, NULL);
131 _IO_JUMPS (&new_f.ofile.file) = &_IO_obstack_jumps;
132 _IO_str_init_static (&new_f.ofile.file.file, obstack_base (obstack),
133 (obstack_object_size (obstack) +
134 obstack_room (obstack)), obstack_next_free (obstack));
135 /* Now allocate the rest of the current chunk. */
136 obstack_blank_fast (obstack,
137 (new_f.ofile.file.file._IO_write_end
138 - new_f.ofile.file.file._IO_write_ptr));
139 new_f.ofile.obstack = obstack;
141 result = _IO_vfprintf (&new_f.ofile.file.file, format, args);
143 /* Shrink the buffer to the space we really currently need. */
144 obstack_blank_fast (obstack, (new_f.ofile.file.file._IO_write_ptr
145 - new_f.ofile.file.file._IO_write_end));
147 return result;
149 #ifdef weak_alias
150 weak_alias (_IO_obstack_vprintf, obstack_vprintf)
151 #endif
155 _IO_obstack_printf (struct obstack *obstack, const char *format, ...)
157 int result;
158 va_list ap;
159 va_start (ap, format);
160 result = _IO_obstack_vprintf (obstack, format, ap);
161 va_end (ap);
162 return result;
164 #ifdef weak_alias
165 weak_alias (_IO_obstack_printf, obstack_printf)
166 #endif