1 /* Formatted output to strings, using POSIX/XSI format strings with positions.
2 Copyright (C) 2003, 2006-2007, 2009 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 HAVE_VISIBILITY && BUILDING_DLL
64 # define DLL_EXPORTED __attribute__((__visibility__("default")))
65 #elif defined _MSC_VER && BUILDING_DLL
66 # define DLL_EXPORTED __declspec(dllexport)
73 /* This needs to be consistent with libgnuintl.h.in. */
74 #if defined __NetBSD__ || defined __BEOS__ || defined __CYGWIN__ || defined __MINGW32__
75 /* Don't break __attribute__((format(printf,M,N))).
76 This redefinition is only possible because the libc in NetBSD, Cygwin,
77 mingw does not have a function __printf__. */
78 # define libintl_printf __printf__
81 /* Define auxiliary functions declared in "printf-args.h". */
82 #include "printf-args.c"
84 /* Define auxiliary functions declared in "printf-parse.h". */
85 #include "printf-parse.c"
87 /* Define functions declared in "vasnprintf.h". */
88 #define vasnprintf libintl_vasnprintf
89 #include "vasnprintf.c"
90 #if 0 /* not needed */
91 #define asnprintf libintl_asnprintf
92 #include "asnprintf.c"
97 libintl_vfprintf (FILE *stream, const char *format, va_list args)
99 if (strchr (format, '$') == NULL)
100 return vfprintf (stream, format, args);
104 char *result = libintl_vasnprintf (NULL, &length, format, args);
108 size_t written = fwrite (result, 1, length, stream);
110 if (written == length)
112 if (length > INT_MAX)
124 libintl_fprintf (FILE *stream, const char *format, ...)
129 va_start (args, format);
130 retval = libintl_vfprintf (stream, format, args);
137 libintl_vprintf (const char *format, va_list args)
139 return libintl_vfprintf (stdout, format, args);
144 libintl_printf (const char *format, ...)
149 va_start (args, format);
150 retval = libintl_vprintf (format, args);
157 libintl_vsprintf (char *resultbuf, const char *format, va_list args)
159 if (strchr (format, '$') == NULL)
160 return vsprintf (resultbuf, format, args);
163 size_t length = (size_t) ~0 / (4 * sizeof (char));
164 char *result = libintl_vasnprintf (resultbuf, &length, format, args);
165 if (result != resultbuf)
170 if (length > INT_MAX)
182 libintl_sprintf (char *resultbuf, const char *format, ...)
187 va_start (args, format);
188 retval = libintl_vsprintf (resultbuf, format, args);
195 # if HAVE_DECL__SNPRINTF
197 # define system_vsnprintf _vsnprintf
200 # define system_vsnprintf vsnprintf
205 libintl_vsnprintf (char *resultbuf, size_t length, const char *format, va_list args)
207 if (strchr (format, '$') == NULL)
208 return system_vsnprintf (resultbuf, length, format, args);
211 size_t maxlength = length;
212 char *result = libintl_vasnprintf (resultbuf, &length, format, args);
213 if (result != resultbuf)
217 size_t pruned_length =
218 (length < maxlength ? length : maxlength - 1);
219 memcpy (resultbuf, result, pruned_length);
220 resultbuf[pruned_length] = '\0';
224 if (length > INT_MAX)
236 libintl_snprintf (char *resultbuf, size_t length, const char *format, ...)
241 va_start (args, format);
242 retval = libintl_vsnprintf (resultbuf, length, format, args);
253 libintl_vasprintf (char **resultp, const char *format, va_list args)
256 char *result = libintl_vasnprintf (NULL, &length, format, args);
259 if (length > INT_MAX)
271 libintl_asprintf (char **resultp, const char *format, ...)
276 va_start (args, format);
277 retval = libintl_vasprintf (resultp, format, args);
288 #define WIDE_CHAR_VERSION 1
290 #include "wprintf-parse.h"
291 /* Define auxiliary functions declared in "wprintf-parse.h". */
292 #define CHAR_T wchar_t
293 #define DIRECTIVE wchar_t_directive
294 #define DIRECTIVES wchar_t_directives
295 #define PRINTF_PARSE wprintf_parse
296 #include "printf-parse.c"
298 /* Define functions declared in "vasnprintf.h". */
299 #define vasnwprintf libintl_vasnwprintf
300 #include "vasnprintf.c"
301 #if 0 /* not needed */
302 #define asnwprintf libintl_asnwprintf
303 #include "asnprintf.c"
306 # if HAVE_DECL__SNWPRINTF
308 # define system_vswprintf _vsnwprintf
311 # define system_vswprintf vswprintf
316 libintl_vfwprintf (FILE *stream, const wchar_t *format, va_list args)
318 if (wcschr (format, '$') == NULL)
319 return vfwprintf (stream, format, args);
323 wchar_t *result = libintl_vasnwprintf (NULL, &length, format, args);
328 for (i = 0; i < length; i++)
329 if (fputwc (result[i], stream) == WEOF)
334 if (length > INT_MAX)
346 libintl_fwprintf (FILE *stream, const wchar_t *format, ...)
351 va_start (args, format);
352 retval = libintl_vfwprintf (stream, format, args);
359 libintl_vwprintf (const wchar_t *format, va_list args)
361 return libintl_vfwprintf (stdout, format, args);
366 libintl_wprintf (const wchar_t *format, ...)
371 va_start (args, format);
372 retval = libintl_vwprintf (format, args);
379 libintl_vswprintf (wchar_t *resultbuf, size_t length, const wchar_t *format, va_list args)
381 if (wcschr (format, '$') == NULL)
382 return system_vswprintf (resultbuf, length, format, args);
385 size_t maxlength = length;
386 wchar_t *result = libintl_vasnwprintf (resultbuf, &length, format, args);
387 if (result != resultbuf)
391 size_t pruned_length =
392 (length < maxlength ? length : maxlength - 1);
393 memcpy (resultbuf, result, pruned_length * sizeof (wchar_t));
394 resultbuf[pruned_length] = 0;
397 /* Unlike vsnprintf, which has to return the number of character that
398 would have been produced if the resultbuf had been sufficiently
399 large, the vswprintf function has to return a negative value if
400 the resultbuf was not sufficiently large. */
401 if (length >= maxlength)
404 if (length > INT_MAX)
416 libintl_swprintf (wchar_t *resultbuf, size_t length, const wchar_t *format, ...)
421 va_start (args, format);
422 retval = libintl_vswprintf (resultbuf, length, format, args);