1 /* Implement the vsnprintf function.
2 Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
3 Written by Kaveh R. Ghazi <ghazi@caip.rutgers.edu>.
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)
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.
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.
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. */
28 @deftypefn Supplemental int vsnprintf (char *@var{buf}, size_t @var{n}, const char *@var{format}, va_list @var{ap})
30 This 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
32 terminating null byte, for a total of @var{n} bytes. On error the
33 return value is -1, otherwise it returns the number of characters that
34 would have been printed had @var{n} been sufficiently large,
35 regardless of the actual value of @var{n}. Note some pre-C99 system
36 libraries do not implement this correctly so users cannot generally
37 rely on the return value if the system version of this function is
55 #include "libiberty.h"
57 /* This implementation relies on a working vasprintf. */
59 vsnprintf (char *s
, size_t n
, const char *format
, va_list ap
)
62 int result
= vasprintf (&buf
, format
, ap
);
72 result
= strlen (buf
);
75 if ((long) n
> result
)
76 memcpy (s
, buf
, result
+1);
88 /* Set the buffer to a known state. */
89 #define CLEAR(BUF) do { memset ((BUF), 'X', sizeof (BUF)); (BUF)[14] = '\0'; } while (0)
91 #define VERIFY(P) do { if (!(P)) abort(); } while (0)
93 static int ATTRIBUTE_PRINTF_3
94 checkit (char *s
, size_t n
, const char *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
);
106 extern int main (void);
114 status
= checkit (buf
, 10, "%s:%d", "foobar", 9);
115 VERIFY (status
==8 && memcmp (buf
, "foobar:9\0XXXXX\0", 15) == 0);
118 status
= checkit (buf
, 9, "%s:%d", "foobar", 9);
119 VERIFY (status
==8 && memcmp (buf
, "foobar:9\0XXXXX\0", 15) == 0);
122 status
= checkit (buf
, 8, "%s:%d", "foobar", 9);
123 VERIFY (status
==8 && memcmp (buf
, "foobar:\0XXXXXX\0", 15) == 0);
126 status
= checkit (buf
, 7, "%s:%d", "foobar", 9);
127 VERIFY (status
==8 && memcmp (buf
, "foobar\0XXXXXXX\0", 15) == 0);
130 status
= checkit (buf
, 6, "%s:%d", "foobar", 9);
131 VERIFY (status
==8 && memcmp (buf
, "fooba\0XXXXXXXX\0", 15) == 0);
134 status
= checkit (buf
, 2, "%s:%d", "foobar", 9);
135 VERIFY (status
==8 && memcmp (buf
, "f\0XXXXXXXXXXXX\0", 15) == 0);
138 status
= checkit (buf
, 1, "%s:%d", "foobar", 9);
139 VERIFY (status
==8 && memcmp (buf
, "\0XXXXXXXXXXXXX\0", 15) == 0);
142 status
= checkit (buf
, 0, "%s:%d", "foobar", 9);
143 VERIFY (status
==8 && memcmp (buf
, "XXXXXXXXXXXXXX\0", 15) == 0);
This page took 0.032075 seconds and 4 git commands to generate.