Replace FSF snail mail address with URLs.
[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, see
19    <http://www.gnu.org/licenses/>.  */
20
21
22 #include <stdlib.h>
23 #include <libioP.h>
24 #include "../libio/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 extern const struct _IO_jump_t _IO_obstack_jumps attribute_hidden;
40
41 int
42 __obstack_vprintf_chk (struct obstack *obstack, int flags, const char *format,
43                        va_list args)
44 {
45   struct obstack_FILE
46     {
47       struct _IO_obstack_file ofile;
48     } new_f;
49   int result;
50   int size;
51   int room;
52
53 #ifdef _IO_MTSAFE_IO
54   new_f.ofile.file.file._lock = NULL;
55 #endif
56
57   _IO_no_init (&new_f.ofile.file.file, _IO_USER_LOCK, -1, NULL, NULL);
58   _IO_JUMPS (&new_f.ofile.file) = &_IO_obstack_jumps;
59   room = obstack_room (obstack);
60   size = obstack_object_size (obstack) + room;
61   if (size == 0)
62     {
63       /* We have to handle the allocation a bit different since the
64          `_IO_str_init_static' function would handle a size of zero
65          different from what we expect.  */
66
67       /* Get more memory.  */
68       obstack_make_room (obstack, 64);
69
70       /* Recompute how much room we have.  */
71       room = obstack_room (obstack);
72       size = room;
73
74       assert (size != 0);
75     }
76
77   _IO_str_init_static_internal ((struct _IO_strfile_ *) &new_f.ofile,
78                                 obstack_base (obstack),
79                                 size, obstack_next_free (obstack));
80   /* Now allocate the rest of the current chunk.  */
81   assert (size == (new_f.ofile.file.file._IO_write_end
82                    - new_f.ofile.file.file._IO_write_base));
83   assert (new_f.ofile.file.file._IO_write_ptr
84           == (new_f.ofile.file.file._IO_write_base
85               + obstack_object_size (obstack)));
86   obstack_blank_fast (obstack, room);
87
88   new_f.ofile.obstack = obstack;
89
90   /* For flags > 0 (i.e. __USE_FORTIFY_LEVEL > 1) request that %n
91      can only come from read-only format strings.  */
92   if (flags > 0)
93     new_f.ofile.file.file._flags2 |= _IO_FLAGS2_FORTIFY;
94
95   result = INTUSE(_IO_vfprintf) (&new_f.ofile.file.file, format, args);
96
97   /* Shrink the buffer to the space we really currently need.  */
98   obstack_blank_fast (obstack, (new_f.ofile.file.file._IO_write_ptr
99                                 - new_f.ofile.file.file._IO_write_end));
100
101   return result;
102 }
103 libc_hidden_def (__obstack_vprintf_chk)
104
105
106 int
107 __obstack_printf_chk (struct obstack *obstack, int flags, const char *format,
108                       ...)
109 {
110   int result;
111   va_list ap;
112   va_start (ap, format);
113   result = __obstack_vprintf_chk (obstack, flags, format, ap);
114   va_end (ap);
115   return result;
116 }