1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997, 2002 Peter Mattis, Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
24 #define _GNU_SOURCE /* For vasprintf */
33 #include "gprintfint.h"
37 * @format: a standard printf() format string, but notice
38 * <link linkend="string-precision">string precision pitfalls</link>.
39 * @Varargs: the arguments to insert in the output.
41 * An implementation of the standard printf() function which supports
42 * positional parameters, as specified in the Single Unix Specification.
44 * Returns: the number of characters printed.
49 g_printf (gchar const *format,
55 va_start (args, format);
56 retval = g_vprintf (format, args);
64 * @file: the stream to write to.
65 * @format: a standard printf() format string, but notice
66 * <link linkend="string-precision">string precision pitfalls</link>.
67 * @Varargs: the arguments to insert in the output.
69 * An implementation of the standard fprintf() function which supports
70 * positional parameters, as specified in the Single Unix Specification.
72 * Returns: the number of characters printed.
77 g_fprintf (FILE *file,
84 va_start (args, format);
85 retval = g_vfprintf (file, format, args);
93 * @string: the buffer to hold the output.
94 * @format: a standard printf() format string, but notice
95 * <link linkend="string-precision">string precision pitfalls</link>.
96 * @Varargs: the arguments to insert in the output.
98 * An implementation of the standard sprintf() function which supports
99 * positional parameters, as specified in the Single Unix Specification.
101 * Returns: the number of characters printed.
106 g_sprintf (gchar *string,
113 va_start (args, format);
114 retval = g_vsprintf (string, format, args);
122 * @string: the buffer to hold the output.
123 * @n: the maximum number of characters to produce (including the
124 * terminating nul character).
125 * @format: a standard printf() format string, but notice
126 * <link linkend="string-precision">string precision pitfalls</link>.
127 * @Varargs: the arguments to insert in the output.
129 * A safer form of the standard sprintf() function. The output is guaranteed
130 * to not exceed @n characters (including the terminating nul character), so
131 * it is easy to ensure that a buffer overflow cannot occur.
133 * See also g_strdup_printf().
135 * In versions of GLib prior to 1.2.3, this function may return -1 if the
136 * output was truncated, and the truncated string may not be nul-terminated.
137 * In versions prior to 1.3.12, this function returns the length of the output
140 * The return value of g_snprintf() conforms to the snprintf()
141 * function as standardized in ISO C99. Note that this is different from
142 * traditional snprintf(), which returns the length of the output string.
144 * The format string may contain positional parameters, as specified in
145 * the Single Unix Specification.
147 * Returns: the number of characters which would be produced if the buffer
151 g_snprintf (gchar *string,
159 va_start (args, format);
160 retval = g_vsnprintf (string, n, format, args);
168 * @format: a standard printf() format string, but notice
169 * <link linkend="string-precision">string precision pitfalls</link>.
170 * @args: the list of arguments to insert in the output.
172 * An implementation of the standard vprintf() function which supports
173 * positional parameters, as specified in the Single Unix Specification.
175 * Returns: the number of characters printed.
180 g_vprintf (gchar const *format,
183 g_return_val_if_fail (format != NULL, -1);
185 return _g_vprintf (format, args);
190 * @file: the stream to write to.
191 * @format: a standard printf() format string, but notice
192 * <link linkend="string-precision">string precision pitfalls</link>.
193 * @args: the list of arguments to insert in the output.
195 * An implementation of the standard fprintf() function which supports
196 * positional parameters, as specified in the Single Unix Specification.
198 * Returns: the number of characters printed.
203 g_vfprintf (FILE *file,
207 g_return_val_if_fail (format != NULL, -1);
209 return _g_vfprintf (file, format, args);
214 * @string: the buffer to hold the output.
215 * @format: a standard printf() format string, but notice
216 * <link linkend="string-precision">string precision pitfalls</link>.
217 * @args: the list of arguments to insert in the output.
219 * An implementation of the standard vsprintf() function which supports
220 * positional parameters, as specified in the Single Unix Specification.
222 * Returns: the number of characters printed.
227 g_vsprintf (gchar *string,
231 g_return_val_if_fail (string != NULL, -1);
232 g_return_val_if_fail (format != NULL, -1);
234 return _g_vsprintf (string, format, args);
239 * @string: the buffer to hold the output.
240 * @n: the maximum number of characters to produce (including the
241 * terminating nul character).
242 * @format: a standard printf() format string, but notice
243 * <link linkend="string-precision">string precision pitfalls</link>.
244 * @args: the list of arguments to insert in the output.
246 * A safer form of the standard vsprintf() function. The output is guaranteed
247 * to not exceed @n characters (including the terminating nul character), so
248 * it is easy to ensure that a buffer overflow cannot occur.
250 * See also g_strdup_vprintf().
252 * In versions of GLib prior to 1.2.3, this function may return -1 if the
253 * output was truncated, and the truncated string may not be nul-terminated.
254 * In versions prior to 1.3.12, this function returns the length of the output
257 * The return value of g_vsnprintf() conforms to the vsnprintf() function
258 * as standardized in ISO C99. Note that this is different from traditional
259 * vsnprintf(), which returns the length of the output string.
261 * The format string may contain positional parameters, as specified in
262 * the Single Unix Specification.
264 * Returns: the number of characters which would be produced if the buffer
268 g_vsnprintf (gchar *string,
273 g_return_val_if_fail (n == 0 || string != NULL, -1);
274 g_return_val_if_fail (format != NULL, -1);
276 return _g_vsnprintf (string, n, format, args);
281 * @string: the return location for the newly-allocated string.
282 * @format: a standard printf() format string, but notice
283 * <link linkend="string-precision">string precision pitfalls</link>.
284 * @args: the list of arguments to insert in the output.
286 * An implementation of the GNU vasprintf() function which supports
287 * positional parameters, as specified in the Single Unix Specification.
288 * This function is similar to g_vsprintf(), except that it allocates a
289 * string to hold the output, instead of putting the output in a buffer
290 * you allocate in advance.
292 * Returns: the number of characters printed.
297 g_vasprintf (gchar **string,
302 g_return_val_if_fail (string != NULL, -1);
304 #if !defined(HAVE_GOOD_PRINTF)
306 len = _g_gnulib_vasprintf (string, format, args);
310 #elif defined (HAVE_VASPRINTF)
312 len = vasprintf (string, format, args);
315 else if (!g_mem_is_system_malloc ())
317 /* vasprintf returns malloc-allocated memory */
318 gchar *string1 = g_strndup (*string, len);
328 G_VA_COPY (args2, args);
330 *string = g_new (gchar, g_printf_string_upper_bound (format, args));
332 len = _g_vsprintf (*string, format, args2);