Add "volatile" keyword to "struct gdb_exception" declaration
[deliverable/binutils-gdb.git] / libiberty / vsnprintf.c
1 /* Implement the vsnprintf function.
2 Copyright (C) 2003, 2004, 2005, 2011 Free Software Foundation, Inc.
3 Written by Kaveh R. Ghazi <ghazi@caip.rutgers.edu>.
4
5 This file is part of the libiberty library. This library is free
6 software; you can redistribute it and/or modify it under the
7 terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 This 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
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
19
20 As a special exception, if you link this library with files
21 compiled with a GNU compiler to produce an executable, this does not cause
22 the resulting executable to be covered by the GNU General Public License.
23 This exception does not however invalidate any other reasons why
24 the executable file might be covered by the GNU General Public License. */
25
26 /*
27
28 @deftypefn Supplemental int vsnprintf (char *@var{buf}, size_t @var{n}, @
29 const char *@var{format}, va_list @var{ap})
30
31 This function is similar to @code{vsprintf}, but it will write to
32 @var{buf} at most @code{@var{n}-1} bytes of text, followed by a
33 terminating null byte, for a total of @var{n} bytes. On error the
34 return value is -1, otherwise it returns the number of characters that
35 would have been printed had @var{n} been sufficiently large,
36 regardless of the actual value of @var{n}. Note some pre-C99 system
37 libraries do not implement this correctly so users cannot generally
38 rely on the return value if the system version of this function is
39 used.
40
41 @end deftypefn
42
43 */
44
45 #include "config.h"
46 #include "ansidecl.h"
47
48 #include <stdarg.h>
49 #ifdef HAVE_STRING_H
50 #include <string.h>
51 #endif
52 #ifdef HAVE_STDLIB_H
53 #include <stdlib.h>
54 #endif
55
56 #include "libiberty.h"
57
58 /* This implementation relies on a working vasprintf. */
59 int
60 vsnprintf (char *s, size_t n, const char *format, va_list ap)
61 {
62 char *buf = 0;
63 int result = vasprintf (&buf, format, ap);
64
65 if (!buf)
66 return -1;
67 if (result < 0)
68 {
69 free (buf);
70 return -1;
71 }
72
73 result = strlen (buf);
74 if (n > 0)
75 {
76 if ((long) n > result)
77 memcpy (s, buf, result+1);
78 else
79 {
80 memcpy (s, buf, n-1);
81 s[n - 1] = 0;
82 }
83 }
84 free (buf);
85 return result;
86 }
87
88 #ifdef TEST
89 /* Set the buffer to a known state. */
90 #define CLEAR(BUF) do { memset ((BUF), 'X', sizeof (BUF)); (BUF)[14] = '\0'; } while (0)
91 /* For assertions. */
92 #define VERIFY(P) do { if (!(P)) abort(); } while (0)
93
94 static int ATTRIBUTE_PRINTF_3
95 checkit (char *s, size_t n, const char *format, ...)
96 {
97 int result;
98 VA_OPEN (ap, format);
99 VA_FIXEDARG (ap, char *, s);
100 VA_FIXEDARG (ap, size_t, n);
101 VA_FIXEDARG (ap, const char *, format);
102 result = vsnprintf (s, n, format, ap);
103 VA_CLOSE (ap);
104 return result;
105 }
106
107 extern int main (void);
108 int
109 main (void)
110 {
111 char buf[128];
112 int status;
113
114 CLEAR (buf);
115 status = checkit (buf, 10, "%s:%d", "foobar", 9);
116 VERIFY (status==8 && memcmp (buf, "foobar:9\0XXXXX\0", 15) == 0);
117
118 CLEAR (buf);
119 status = checkit (buf, 9, "%s:%d", "foobar", 9);
120 VERIFY (status==8 && memcmp (buf, "foobar:9\0XXXXX\0", 15) == 0);
121
122 CLEAR (buf);
123 status = checkit (buf, 8, "%s:%d", "foobar", 9);
124 VERIFY (status==8 && memcmp (buf, "foobar:\0XXXXXX\0", 15) == 0);
125
126 CLEAR (buf);
127 status = checkit (buf, 7, "%s:%d", "foobar", 9);
128 VERIFY (status==8 && memcmp (buf, "foobar\0XXXXXXX\0", 15) == 0);
129
130 CLEAR (buf);
131 status = checkit (buf, 6, "%s:%d", "foobar", 9);
132 VERIFY (status==8 && memcmp (buf, "fooba\0XXXXXXXX\0", 15) == 0);
133
134 CLEAR (buf);
135 status = checkit (buf, 2, "%s:%d", "foobar", 9);
136 VERIFY (status==8 && memcmp (buf, "f\0XXXXXXXXXXXX\0", 15) == 0);
137
138 CLEAR (buf);
139 status = checkit (buf, 1, "%s:%d", "foobar", 9);
140 VERIFY (status==8 && memcmp (buf, "\0XXXXXXXXXXXXX\0", 15) == 0);
141
142 CLEAR (buf);
143 status = checkit (buf, 0, "%s:%d", "foobar", 9);
144 VERIFY (status==8 && memcmp (buf, "XXXXXXXXXXXXXX\0", 15) == 0);
145
146 return 0;
147 }
148 #endif /* TEST */
This page took 0.03342 seconds and 4 git commands to generate.