Avoid dup3 PLT usage.
[platform/upstream/glibc.git] / libio / obprintf.c
1 /* Print output of stream to given obstack.
2    Copyright (C) 1996,1997,1999,2000,2001,2002,2003,2004,2005,2006,2008,2012
3         Free Software Foundation, Inc.
4    This file is part of the GNU C Library.
5    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
6
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.
11
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.
16
17    You should have received a copy of the GNU Lesser General Public
18    License along with the GNU C Library; if not, see
19    <http://www.gnu.org/licenses/>.  */
20
21
22 #include <stdlib.h>
23 #include "libioP.h"
24 #include "strfile.h"
25 #include <assert.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <obstack.h>
29 #include <stdarg.h>
30 #include <stdio_ext.h>
31
32
33 struct _IO_obstack_file
34 {
35   struct _IO_FILE_plus file;
36   struct obstack *obstack;
37 };
38
39
40 static int
41 _IO_obstack_overflow (_IO_FILE *fp, int c)
42 {
43   struct obstack *obstack = ((struct _IO_obstack_file *) fp)->obstack;
44   int size;
45
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);
50
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);
58
59   return c;
60 }
61
62
63 static _IO_size_t
64 _IO_obstack_xsputn (_IO_FILE *fp, const void *data, _IO_size_t n)
65 {
66   struct obstack *obstack = ((struct _IO_obstack_file *) fp)->obstack;
67
68   if (fp->_IO_write_ptr + n > fp->_IO_write_end)
69     {
70       int size;
71
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);
75
76       /* Now grow for N bytes, and put the data there.  */
77       obstack_grow (obstack, data, n);
78
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);
86     }
87   else
88     fp->_IO_write_ptr = __mempcpy (fp->_IO_write_ptr, data, n);
89
90   return n;
91 }
92
93
94 /* the jump table.  */
95 const struct _IO_jump_t _IO_obstack_jumps attribute_hidden =
96 {
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)
117 };
118
119
120 int
121 _IO_obstack_vprintf (struct obstack *obstack, const char *format, va_list args)
122 {
123   struct obstack_FILE
124     {
125       struct _IO_obstack_file ofile;
126   } new_f;
127   int result;
128   int size;
129   int room;
130
131 #ifdef _IO_MTSAFE_IO
132   new_f.ofile.file.file._lock = NULL;
133 #endif
134
135   _IO_no_init (&new_f.ofile.file.file, _IO_USER_LOCK, -1, NULL, NULL);
136   _IO_JUMPS (&new_f.ofile.file) = &_IO_obstack_jumps;
137   room = obstack_room (obstack);
138   size = obstack_object_size (obstack) + room;
139   if (size == 0)
140     {
141       /* We have to handle the allocation a bit different since the
142          `_IO_str_init_static' function would handle a size of zero
143          different from what we expect.  */
144
145       /* Get more memory.  */
146       obstack_make_room (obstack, 64);
147
148       /* Recompute how much room we have.  */
149       room = obstack_room (obstack);
150       size = room;
151
152       assert (size != 0);
153     }
154
155   _IO_str_init_static_internal ((struct _IO_strfile_ *) &new_f.ofile,
156                                 obstack_base (obstack),
157                                 size, obstack_next_free (obstack));
158   /* Now allocate the rest of the current chunk.  */
159   assert (size == (new_f.ofile.file.file._IO_write_end
160                    - new_f.ofile.file.file._IO_write_base));
161   assert (new_f.ofile.file.file._IO_write_ptr
162           == (new_f.ofile.file.file._IO_write_base
163               + obstack_object_size (obstack)));
164   obstack_blank_fast (obstack, room);
165
166   new_f.ofile.obstack = obstack;
167
168   result = INTUSE(_IO_vfprintf) (&new_f.ofile.file.file, format, args);
169
170   /* Shrink the buffer to the space we really currently need.  */
171   obstack_blank_fast (obstack, (new_f.ofile.file.file._IO_write_ptr
172                                 - new_f.ofile.file.file._IO_write_end));
173
174   return result;
175 }
176 ldbl_weak_alias (_IO_obstack_vprintf, obstack_vprintf)
177
178
179 int
180 _IO_obstack_printf (struct obstack *obstack, const char *format, ...)
181 {
182   int result;
183   va_list ap;
184   va_start (ap, format);
185   result = _IO_obstack_vprintf (obstack, format, ap);
186   va_end (ap);
187   return result;
188 }
189 ldbl_weak_alias (_IO_obstack_printf, obstack_printf)