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 */
32 #include "gprintfint.h"
38 * @format: a standard printf() format string, but notice
39 * <link linkend="string-precision">string precision pitfalls</link>.
40 * @Varargs: the arguments to insert in the output.
42 * An implementation of the standard printf() function which supports
43 * positional parameters, as specified in the Single Unix Specification.
45 * Returns: the number of characters printed.
50 g_printf (gchar const *format,
56 va_start (args, format);
57 retval = g_vprintf (format, args);
65 * @file: the stream to write to.
66 * @format: a standard printf() format string, but notice
67 * <link linkend="string-precision">string precision pitfalls</link>.
68 * @Varargs: the arguments to insert in the output.
70 * An implementation of the standard fprintf() function which supports
71 * positional parameters, as specified in the Single Unix Specification.
73 * Returns: the number of characters printed.
78 g_fprintf (FILE *file,
85 va_start (args, format);
86 retval = g_vfprintf (file, format, args);
94 * @string: the buffer to hold the output.
95 * @format: a standard printf() format string, but notice
96 * <link linkend="string-precision">string precision pitfalls</link>.
97 * @Varargs: the arguments to insert in the output.
99 * An implementation of the standard sprintf() function which supports
100 * positional parameters, as specified in the Single Unix Specification.
102 * Returns: the number of characters printed.
107 g_sprintf (gchar *string,
114 va_start (args, format);
115 retval = g_vsprintf (string, format, args);
123 * @string: the buffer to hold the output.
124 * @n: the maximum number of characters to produce (including the
125 * terminating nul character).
126 * @format: a standard printf() format string, but notice
127 * <link linkend="string-precision">string precision pitfalls</link>.
128 * @Varargs: the arguments to insert in the output.
130 * A safer form of the standard sprintf() function. The output is guaranteed
131 * to not exceed @n characters (including the terminating nul character), so
132 * it is easy to ensure that a buffer overflow cannot occur.
134 * See also g_strdup_printf().
136 * In versions of GLib prior to 1.2.3, this function may return -1 if the
137 * output was truncated, and the truncated string may not be nul-terminated.
138 * In versions prior to 1.3.12, this function returns the length of the output
141 * The return value of g_snprintf() conforms to the snprintf()
142 * function as standardized in ISO C99. Note that this is different from
143 * traditional snprintf(), which returns the length of the output string.
145 * The format string may contain positional parameters, as specified in
146 * the Single Unix Specification.
148 * Returns: the number of characters which would be produced if the buffer
152 g_snprintf (gchar *string,
160 va_start (args, format);
161 retval = g_vsnprintf (string, n, format, args);
169 * @format: a standard printf() format string, but notice
170 * <link linkend="string-precision">string precision pitfalls</link>.
171 * @args: the list of arguments to insert in the output.
173 * An implementation of the standard vprintf() function which supports
174 * positional parameters, as specified in the Single Unix Specification.
176 * Returns: the number of characters printed.
181 g_vprintf (gchar const *format,
184 g_return_val_if_fail (format != NULL, -1);
186 return _g_vprintf (format, args);
191 * @file: the stream to write to.
192 * @format: a standard printf() format string, but notice
193 * <link linkend="string-precision">string precision pitfalls</link>.
194 * @args: the list of arguments to insert in the output.
196 * An implementation of the standard fprintf() function which supports
197 * positional parameters, as specified in the Single Unix Specification.
199 * Returns: the number of characters printed.
204 g_vfprintf (FILE *file,
208 g_return_val_if_fail (format != NULL, -1);
210 return _g_vfprintf (file, format, args);
215 * @string: the buffer to hold the output.
216 * @format: a standard printf() format string, but notice
217 * <link linkend="string-precision">string precision pitfalls</link>.
218 * @args: the list of arguments to insert in the output.
220 * An implementation of the standard vsprintf() function which supports
221 * positional parameters, as specified in the Single Unix Specification.
223 * Returns: the number of characters printed.
228 g_vsprintf (gchar *string,
232 g_return_val_if_fail (string != NULL, -1);
233 g_return_val_if_fail (format != NULL, -1);
235 return _g_vsprintf (string, format, args);
240 * @string: the buffer to hold the output.
241 * @n: the maximum number of characters to produce (including the
242 * terminating nul character).
243 * @format: a standard printf() format string, but notice
244 * <link linkend="string-precision">string precision pitfalls</link>.
245 * @args: the list of arguments to insert in the output.
247 * A safer form of the standard vsprintf() function. The output is guaranteed
248 * to not exceed @n characters (including the terminating nul character), so
249 * it is easy to ensure that a buffer overflow cannot occur.
251 * See also g_strdup_vprintf().
253 * In versions of GLib prior to 1.2.3, this function may return -1 if the
254 * output was truncated, and the truncated string may not be nul-terminated.
255 * In versions prior to 1.3.12, this function returns the length of the output
258 * The return value of g_vsnprintf() conforms to the vsnprintf() function
259 * as standardized in ISO C99. Note that this is different from traditional
260 * vsnprintf(), which returns the length of the output string.
262 * The format string may contain positional parameters, as specified in
263 * the Single Unix Specification.
265 * Returns: the number of characters which would be produced if the buffer
269 g_vsnprintf (gchar *string,
274 g_return_val_if_fail (n == 0 || string != NULL, -1);
275 g_return_val_if_fail (format != NULL, -1);
277 return _g_vsnprintf (string, n, format, args);
282 * @string: the return location for the newly-allocated string.
283 * @format: a standard printf() format string, but notice
284 * <link linkend="string-precision">string precision pitfalls</link>.
285 * @args: the list of arguments to insert in the output.
287 * An implementation of the GNU vasprintf() function which supports
288 * positional parameters, as specified in the Single Unix Specification.
289 * This function is similar to g_vsprintf(), except that it allocates a
290 * string to hold the output, instead of putting the output in a buffer
291 * you allocate in advance.
293 * Returns: the number of characters printed.
298 g_vasprintf (gchar **string,
303 g_return_val_if_fail (string != NULL, -1);
305 #if !defined(HAVE_GOOD_PRINTF)
307 len = _g_gnulib_vasprintf (string, format, args);
311 #elif defined (HAVE_VASPRINTF)
313 len = vasprintf (string, format, args);
316 else if (!g_mem_is_system_malloc ())
318 /* vasprintf returns malloc-allocated memory */
319 gchar *string1 = g_strndup (*string, len);
329 G_VA_COPY (args2, args);
331 *string = g_new (gchar, g_printf_string_upper_bound (format, args));
333 len = _g_vsprintf (*string, format, args2);
341 #define __G_PRINTF_C__
342 #include "galiasdef.c"