1
/* Formatted output to strings, using POSIX/XSI format strings with positions.
2
Copyright (C) 2003, 2006 Free Software Foundation, Inc.
3
Written by Bruno Haible <bruno@clisp.org>, 2003.
5
This program is free software; you can redistribute it and/or modify it
6
under the terms of the GNU Library General Public License as published
7
by the Free Software Foundation; either version 2, or (at your option)
10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
Library General Public License for more details.
15
You should have received a copy of the GNU Library General Public
16
License along with this program; if not, write to the Free Software
17
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
25
# define alloca __builtin_alloca
26
# define HAVE_ALLOCA 1
30
# define alloca _alloca
32
# if defined HAVE_ALLOCA_H || defined _LIBC
48
#if !HAVE_POSIX_PRINTF
55
/* Some systems, like OSF/1 4.0 and Woe32, don't have EOVERFLOW. */
57
# define EOVERFLOW E2BIG
60
/* When building a DLL, we must export some functions. Note that because
61
the functions are only defined for binary backward compatibility, we
62
don't need to use __declspec(dllimport) in any case. */
63
#if defined _MSC_VER && BUILDING_DLL
64
# define DLL_EXPORTED __declspec(dllexport)
71
/* This needs to be consistent with libgnuintl.h.in. */
72
#if defined __NetBSD__ || defined __CYGWIN__ || defined __MINGW32__
73
/* Don't break __attribute__((format(printf,M,N))).
74
This redefinition is only possible because the libc in NetBSD, Cygwin,
75
mingw does not have a function __printf__. */
76
# define libintl_printf __printf__
79
/* Define auxiliary functions declared in "printf-args.h". */
80
#include "printf-args.c"
82
/* Define auxiliary functions declared in "printf-parse.h". */
83
#include "printf-parse.c"
85
/* Define functions declared in "vasnprintf.h". */
86
#define vasnprintf libintl_vasnprintf
87
#include "vasnprintf.c"
88
#if 0 /* not needed */
89
#define asnprintf libintl_asnprintf
90
#include "asnprintf.c"
95
libintl_vfprintf (FILE *stream, const char *format, va_list args)
97
if (strchr (format, '$') == NULL)
98
return vfprintf (stream, format, args);
102
char *result = libintl_vasnprintf (NULL, &length, format, args);
106
size_t written = fwrite (result, 1, length, stream);
108
if (written == length)
110
if (length > INT_MAX)
122
libintl_fprintf (FILE *stream, const char *format, ...)
127
va_start (args, format);
128
retval = libintl_vfprintf (stream, format, args);
135
libintl_vprintf (const char *format, va_list args)
137
return libintl_vfprintf (stdout, format, args);
142
libintl_printf (const char *format, ...)
147
va_start (args, format);
148
retval = libintl_vprintf (format, args);
155
libintl_vsprintf (char *resultbuf, const char *format, va_list args)
157
if (strchr (format, '$') == NULL)
158
return vsprintf (resultbuf, format, args);
161
size_t length = (size_t) ~0 / (4 * sizeof (char));
162
char *result = libintl_vasnprintf (resultbuf, &length, format, args);
163
if (result != resultbuf)
168
if (length > INT_MAX)
180
libintl_sprintf (char *resultbuf, const char *format, ...)
185
va_start (args, format);
186
retval = libintl_vsprintf (resultbuf, format, args);
193
# if HAVE_DECL__SNPRINTF
195
# define system_vsnprintf _vsnprintf
198
# define system_vsnprintf vsnprintf
203
libintl_vsnprintf (char *resultbuf, size_t length, const char *format, va_list args)
205
if (strchr (format, '$') == NULL)
206
return system_vsnprintf (resultbuf, length, format, args);
209
size_t maxlength = length;
210
char *result = libintl_vasnprintf (resultbuf, &length, format, args);
211
if (result != resultbuf)
215
size_t pruned_length =
216
(length < maxlength ? length : maxlength - 1);
217
memcpy (resultbuf, result, pruned_length);
218
resultbuf[pruned_length] = '\0';
222
if (length > INT_MAX)
234
libintl_snprintf (char *resultbuf, size_t length, const char *format, ...)
239
va_start (args, format);
240
retval = libintl_vsnprintf (resultbuf, length, format, args);
251
libintl_vasprintf (char **resultp, const char *format, va_list args)
254
char *result = libintl_vasnprintf (NULL, &length, format, args);
257
if (length > INT_MAX)
269
libintl_asprintf (char **resultp, const char *format, ...)
274
va_start (args, format);
275
retval = libintl_vasprintf (resultp, format, args);
286
#define WIDE_CHAR_VERSION 1
288
/* Define auxiliary functions declared in "wprintf-parse.h". */
289
#include "printf-parse.c"
291
/* Define functions declared in "vasnprintf.h". */
292
#define vasnwprintf libintl_vasnwprintf
293
#include "vasnprintf.c"
294
#if 0 /* not needed */
295
#define asnwprintf libintl_asnwprintf
296
#include "asnprintf.c"
299
# if HAVE_DECL__SNWPRINTF
301
# define system_vswprintf _vsnwprintf
304
# define system_vswprintf vswprintf
309
libintl_vfwprintf (FILE *stream, const wchar_t *format, va_list args)
311
if (wcschr (format, '$') == NULL)
312
return vfwprintf (stream, format, args);
316
wchar_t *result = libintl_vasnwprintf (NULL, &length, format, args);
321
for (i = 0; i < length; i++)
322
if (fputwc (result[i], stream) == WEOF)
327
if (length > INT_MAX)
339
libintl_fwprintf (FILE *stream, const wchar_t *format, ...)
344
va_start (args, format);
345
retval = libintl_vfwprintf (stream, format, args);
352
libintl_vwprintf (const wchar_t *format, va_list args)
354
return libintl_vfwprintf (stdout, format, args);
359
libintl_wprintf (const wchar_t *format, ...)
364
va_start (args, format);
365
retval = libintl_vwprintf (format, args);
372
libintl_vswprintf (wchar_t *resultbuf, size_t length, const wchar_t *format, va_list args)
374
if (wcschr (format, '$') == NULL)
375
return system_vswprintf (resultbuf, length, format, args);
378
size_t maxlength = length;
379
wchar_t *result = libintl_vasnwprintf (resultbuf, &length, format, args);
380
if (result != resultbuf)
384
size_t pruned_length =
385
(length < maxlength ? length : maxlength - 1);
386
memcpy (resultbuf, result, pruned_length * sizeof (wchar_t));
387
resultbuf[pruned_length] = 0;
390
/* Unlike vsnprintf, which has to return the number of character that
391
would have been produced if the resultbuf had been sufficiently
392
large, the vswprintf function has to return a negative value if
393
the resultbuf was not sufficiently large. */
394
if (length >= maxlength)
397
if (length > INT_MAX)
409
libintl_swprintf (wchar_t *resultbuf, size_t length, const wchar_t *format, ...)
414
va_start (args, format);
415
retval = libintl_vswprintf (resultbuf, length, format, args);