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