Update copyright year
[platform/upstream/glibc.git] / debug / obprintf_chk.c
1 /* Print output of stream to given obstack.
2    Copyright (C) 1996,1997,1999,2000,2001,2002,2003,2004,2005,2006,2008
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, write to the Free
19    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20    02111-1307 USA.  */
21
22
23 #include <stdlib.h>
24 #include <libioP.h>
25 #include "../libio/strfile.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>
32
33
34 struct _IO_obstack_file
35 {
36   struct _IO_FILE_plus file;
37   struct obstack *obstack;
38 };
39
40 extern const struct _IO_jump_t _IO_obstack_jumps attribute_hidden;
41
42 int
43 __obstack_vprintf_chk (struct obstack *obstack, int flags, const char *format,
44                        va_list args)
45 {
46   struct obstack_FILE
47     {
48       struct _IO_obstack_file ofile;
49     } new_f;
50   int result;
51   int size;
52   int room;
53
54 #ifdef _IO_MTSAFE_IO
55   new_f.ofile.file.file._lock = NULL;
56 #endif
57
58   _IO_no_init (&new_f.ofile.file.file, _IO_USER_LOCK, -1, NULL, NULL);
59   _IO_JUMPS (&new_f.ofile.file) = &_IO_obstack_jumps;
60   room = obstack_room (obstack);
61   size = obstack_object_size (obstack) + room;
62   if (size == 0)
63     {
64       /* We have to handle the allocation a bit different since the
65          `_IO_str_init_static' function would handle a size of zero
66          different from what we expect.  */
67
68       /* Get more memory.  */
69       obstack_make_room (obstack, 64);
70
71       /* Recompute how much room we have.  */
72       room = obstack_room (obstack);
73       size = room;
74
75       assert (size != 0);
76     }
77
78   _IO_str_init_static_internal ((struct _IO_strfile_ *) &new_f.ofile,
79                                 obstack_base (obstack),
80                                 size, obstack_next_free (obstack));
81   /* Now allocate the rest of the current chunk.  */
82   assert (size == (new_f.ofile.file.file._IO_write_end
83                    - new_f.ofile.file.file._IO_write_base));
84   assert (new_f.ofile.file.file._IO_write_ptr
85           == (new_f.ofile.file.file._IO_write_base
86               + obstack_object_size (obstack)));
87   obstack_blank_fast (obstack, room);
88
89   new_f.ofile.obstack = obstack;
90
91   /* For flags > 0 (i.e. __USE_FORTIFY_LEVEL > 1) request that %n
92      can only come from read-only format strings.  */
93   if (flags > 0)
94     new_f.ofile.file.file._flags2 |= _IO_FLAGS2_FORTIFY;
95
96   result = INTUSE(_IO_vfprintf) (&new_f.ofile.file.file, format, args);
97
98   /* Shrink the buffer to the space we really currently need.  */
99   obstack_blank_fast (obstack, (new_f.ofile.file.file._IO_write_ptr
100                                 - new_f.ofile.file.file._IO_write_end));
101
102   return result;
103 }
104 libc_hidden_def (__obstack_vprintf_chk)
105
106
107 int
108 __obstack_printf_chk (struct obstack *obstack, int flags, const char *format,
109                       ...)
110 {
111   int result;
112   va_list ap;
113   va_start (ap, format);
114   result = __obstack_vprintf_chk (obstack, flags, format, ap);
115   va_end (ap);
116   return result;
117 }