]> git.ipfire.org Git - thirdparty/gcc.git/blame - libiberty/vsnprintf.c
AArch64: Cleanup memset expansion
[thirdparty/gcc.git] / libiberty / vsnprintf.c
CommitLineData
bd3fbc6b 1/* Implement the vsnprintf function.
a945c346 2 Copyright (C) 2003-2024 Free Software Foundation, Inc.
bd3fbc6b
KG
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
ee58dffd 18the Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
bd3fbc6b
KG
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
996c0cb0
RW
28@deftypefn Supplemental int vsnprintf (char *@var{buf}, size_t @var{n}, @
29 const char *@var{format}, va_list @var{ap})
bd3fbc6b 30
ec5c6f4a
EZ
31This 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
33terminating null byte, for a total of @var{n} bytes. On error the
34return value is -1, otherwise it returns the number of characters that
35would have been printed had @var{n} been sufficiently large,
36regardless of the actual value of @var{n}. Note some pre-C99 system
37libraries do not implement this correctly so users cannot generally
38rely on the return value if the system version of this function is
39used.
bd3fbc6b
KG
40
41@end deftypefn
42
43*/
44
45#include "config.h"
46#include "ansidecl.h"
47
bd3fbc6b 48#include <stdarg.h>
bd3fbc6b
KG
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. */
59int
7a17ef5e 60vsnprintf (char *s, size_t n, const char *format, va_list ap)
bd3fbc6b
KG
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 {
52dd3224
KG
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 }
bd3fbc6b
KG
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
94static int ATTRIBUTE_PRINTF_3
7a17ef5e 95checkit (char *s, size_t n, const char *format, ...)
bd3fbc6b
KG
96{
97 int result;
d2d21de9
TT
98 va_list ap;
99 va_start (ap, format);
bd3fbc6b 100 result = vsnprintf (s, n, format, ap);
d2d21de9 101 va_end (ap);
bd3fbc6b
KG
102 return result;
103}
104
7a17ef5e 105extern int main (void);
bd3fbc6b 106int
7a17ef5e 107main (void)
bd3fbc6b
KG
108{
109 char buf[128];
110 int status;
111
112 CLEAR (buf);
113 status = checkit (buf, 10, "%s:%d", "foobar", 9);
52dd3224 114 VERIFY (status==8 && memcmp (buf, "foobar:9\0XXXXX\0", 15) == 0);
bd3fbc6b
KG
115
116 CLEAR (buf);
117 status = checkit (buf, 9, "%s:%d", "foobar", 9);
52dd3224 118 VERIFY (status==8 && memcmp (buf, "foobar:9\0XXXXX\0", 15) == 0);
bd3fbc6b
KG
119
120 CLEAR (buf);
121 status = checkit (buf, 8, "%s:%d", "foobar", 9);
52dd3224 122 VERIFY (status==8 && memcmp (buf, "foobar:\0XXXXXX\0", 15) == 0);
bd3fbc6b
KG
123
124 CLEAR (buf);
125 status = checkit (buf, 7, "%s:%d", "foobar", 9);
52dd3224 126 VERIFY (status==8 && memcmp (buf, "foobar\0XXXXXXX\0", 15) == 0);
bd3fbc6b
KG
127
128 CLEAR (buf);
129 status = checkit (buf, 6, "%s:%d", "foobar", 9);
52dd3224 130 VERIFY (status==8 && memcmp (buf, "fooba\0XXXXXXXX\0", 15) == 0);
bd3fbc6b
KG
131
132 CLEAR (buf);
133 status = checkit (buf, 2, "%s:%d", "foobar", 9);
52dd3224 134 VERIFY (status==8 && memcmp (buf, "f\0XXXXXXXXXXXX\0", 15) == 0);
bd3fbc6b
KG
135
136 CLEAR (buf);
137 status = checkit (buf, 1, "%s:%d", "foobar", 9);
52dd3224 138 VERIFY (status==8 && memcmp (buf, "\0XXXXXXXXXXXXX\0", 15) == 0);
bd3fbc6b
KG
139
140 CLEAR (buf);
141 status = checkit (buf, 0, "%s:%d", "foobar", 9);
52dd3224 142 VERIFY (status==8 && memcmp (buf, "XXXXXXXXXXXXXX\0", 15) == 0);
bd3fbc6b
KG
143
144 return 0;
145}
146#endif /* TEST */