]> git.ipfire.org Git - thirdparty/gcc.git/blame - libiberty/vasprintf.c
Fix tests for std::variant to match original intention
[thirdparty/gcc.git] / libiberty / vasprintf.c
CommitLineData
28e9041c 1/* Like vsprintf but provides a pointer to malloc'd storage, which must
2 be freed by the caller.
fbd26352 3 Copyright (C) 1994-2019 Free Software Foundation, Inc.
28e9041c 4
5This file is part of the libiberty library.
6Libiberty is free software; you can redistribute it and/or
7modify it under the terms of the GNU Library General Public
8License as published by the Free Software Foundation; either
9version 2 of the License, or (at your option) any later version.
10
11Libiberty 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 GNU
14Library General Public License for more details.
15
16You should have received a copy of the GNU Library General Public
5d95297a 17License along with libiberty; see the file COPYING.LIB. If not, write
18to the Free Software Foundation, Inc., 51 Franklin Street - Fifth
19Floor, Boston, MA 02110-1301, USA. */
28e9041c 20
bb37062f 21#ifdef HAVE_CONFIG_H
22#include "config.h"
23#endif
24#include <ansidecl.h>
28e9041c 25#include <stdarg.h>
6f2cd531 26#if !defined (va_copy) && defined (__va_copy)
27# define va_copy(d,s) __va_copy((d),(s))
28#endif
d933c982 29#include <stdio.h>
2bfbbd2b 30#ifdef HAVE_STRING_H
d933c982 31#include <string.h>
2bfbbd2b 32#endif
bb37062f 33#ifdef HAVE_STDLIB_H
34#include <stdlib.h>
35#else
bb37062f 36extern PTR malloc ();
37#endif
38#include "libiberty.h"
5d95297a 39#include "vprintf-support.h"
28e9041c 40
41#ifdef TEST
42int global_total_width;
43#endif
44
349e8276 45/*
46
bac427a1 47@deftypefn Extension int vasprintf (char **@var{resptr}, @
48 const char *@var{format}, va_list @var{args})
349e8276 49
50Like @code{vsprintf}, but instead of passing a pointer to a buffer,
51you pass a pointer to a pointer. This function will compute the size
52of the buffer needed, allocate memory with @code{malloc}, and store a
53pointer to the allocated memory in @code{*@var{resptr}}. The value
54returned is the same as @code{vsprintf} would return. If memory could
5958dc33 55not be allocated, minus one is returned and @code{NULL} is stored in
349e8276 56@code{*@var{resptr}}.
57
58@end deftypefn
59
60*/
bb37062f 61
cd72df1f 62static int int_vasprintf (char **, const char *, va_list);
28e9041c 63
64static int
cd72df1f 65int_vasprintf (char **result, const char *format, va_list args)
28e9041c 66{
5d95297a 67 int total_width = libiberty_vprintf_buffer_size (format, args);
28e9041c 68#ifdef TEST
69 global_total_width = total_width;
70#endif
05455547 71 *result = (char *) malloc (total_width);
28e9041c 72 if (*result != NULL)
82ac7060 73 return vsprintf (*result, format, args);
28e9041c 74 else
5958dc33 75 return -1;
28e9041c 76}
77
78int
cd72df1f 79vasprintf (char **result, const char *format,
8f71bf1d 80#if defined (_BSD_VA_LIST_) && defined (__FreeBSD__)
cd72df1f 81 _BSD_VA_LIST_ args)
8f71bf1d 82#else
cd72df1f 83 va_list args)
8f71bf1d 84#endif
28e9041c 85{
82ac7060 86 return int_vasprintf (result, format, args);
28e9041c 87}
88
89#ifdef TEST
2bfbbd2b 90static void ATTRIBUTE_PRINTF_1
cd72df1f 91checkit (const char *format, ...)
28e9041c 92{
28e9041c 93 char *result;
e14937e9 94 va_list args;
95 va_start (args, format);
28e9041c 96 vasprintf (&result, format, args);
e14937e9 97 va_end (args);
2bfbbd2b 98
bb37062f 99 if (strlen (result) < (size_t) global_total_width)
28e9041c 100 printf ("PASS: ");
101 else
102 printf ("FAIL: ");
103 printf ("%d %s\n", global_total_width, result);
2bfbbd2b 104
105 free (result);
28e9041c 106}
107
cd72df1f 108extern int main (void);
bb37062f 109
28e9041c 110int
cd72df1f 111main (void)
28e9041c 112{
113 checkit ("%d", 0x12345678);
114 checkit ("%200d", 5);
115 checkit ("%.300d", 6);
116 checkit ("%100.150d", 7);
117 checkit ("%s", "jjjjjjjjjiiiiiiiiiiiiiiioooooooooooooooooppppppppppppaa\n\
118777777777777777777333333333333366666666666622222222222777777777777733333");
119 checkit ("%f%s%d%s", 1.0, "foo", 77, "asdjffffffffffffffiiiiiiiiiiixxxxx");
bb37062f 120
121 return 0;
28e9041c 122}
123#endif /* TEST */