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"
36 * @format: the format string. See the printf() documentation.
37 * @Varargs: the arguments to insert in the output.
39 * An implementation of the standard printf() function which supports
40 * positional parameters, as specified in the Single Unix Specification.
42 * Returns: the number of characters printed.
47 g_printf (gchar const *format,
53 va_start (args, format);
54 retval = g_vprintf (format, args);
62 * @file: the stream to write to.
63 * @format: the format string. See the printf() documentation.
64 * @Varargs: the arguments to insert in the output.
66 * An implementation of the standard fprintf() function which supports
67 * positional parameters, as specified in the Single Unix Specification.
69 * Returns: the number of characters printed.
74 g_fprintf (FILE *file,
81 va_start (args, format);
82 retval = g_vfprintf (file, format, args);
90 * @string: the buffer to hold the output.
91 * @format: the format string. See the printf() documentation.
92 * @Varargs: the arguments to insert in the output.
94 * An implementation of the standard sprintf() function which supports
95 * positional parameters, as specified in the Single Unix Specification.
97 * Returns: the number of characters printed.
102 g_sprintf (gchar *string,
109 va_start (args, format);
110 retval = g_vsprintf (string, format, args);
118 * @string: the buffer to hold the output.
119 * @n: the maximum number of characters to produce (including the
120 * terminating nul character).
121 * @format: the format string. See the printf() documentation.
122 * @Varargs: the arguments to insert in the output.
124 * A safer form of the standard sprintf() function. The output is guaranteed
125 * to not exceed @n characters (including the terminating nul character), so
126 * it is easy to ensure that a buffer overflow cannot occur.
128 * See also g_strdup_printf().
130 * In versions of GLib prior to 1.2.3, this function may return -1 if the
131 * output was truncated, and the truncated string may not be nul-terminated.
132 * In versions prior to 1.3.12, this function returns the length of the output
135 * The return value of g_snprintf() conforms to the snprintf()
136 * function as standardized in ISO C99. Note that this is different from
137 * traditional snprintf(), which returns the length of the output string.
139 * The format string may contain positional parameters, as specified in
140 * the Single Unix Specification.
142 * Returns: the number of characters which would be produced if the buffer
146 g_snprintf (gchar *string,
154 va_start (args, format);
155 retval = g_vsnprintf (string, n, format, args);
163 * @format: the format string. See the printf() documentation.
164 * @args: the list of arguments to insert in the output.
166 * An implementation of the standard vprintf() function which supports
167 * positional parameters, as specified in the Single Unix Specification.
169 * Returns: the number of characters printed.
174 g_vprintf (gchar const *format,
177 g_return_val_if_fail (format != NULL, 0);
179 return _g_vprintf (format, args);
184 * @file: the stream to write to.
185 * @format: the format string. See the printf() documentation.
186 * @args: the list of arguments to insert in the output.
188 * An implementation of the standard fprintf() function which supports
189 * positional parameters, as specified in the Single Unix Specification.
191 * Returns: the number of characters printed.
196 g_vfprintf (FILE *file,
200 g_return_val_if_fail (format != NULL, 0);
202 return _g_vfprintf (file, format, args);
207 * @string: the buffer to hold the output.
208 * @format: the format string. See the printf() documentation.
209 * @args: the list of arguments to insert in the output.
211 * An implementation of the standard vsprintf() function which supports
212 * positional parameters, as specified in the Single Unix Specification.
214 * Returns: the number of characters printed.
219 g_vsprintf (gchar *string,
223 g_return_val_if_fail (string != NULL, 0);
224 g_return_val_if_fail (format != NULL, 0);
226 return _g_vsprintf (string, format, args);
231 * @string: the buffer to hold the output.
232 * @n: the maximum number of characters to produce (including the
233 * terminating nul character).
234 * @format: the format string. See the printf() documentation.
235 * @args: the list of arguments to insert in the output.
237 * A safer form of the standard vsprintf() function. The output is guaranteed
238 * to not exceed @n characters (including the terminating nul character), so
239 * it is easy to ensure that a buffer overflow cannot occur.
241 * See also g_strdup_vprintf().
243 * In versions of GLib prior to 1.2.3, this function may return -1 if the
244 * output was truncated, and the truncated string may not be nul-terminated.
245 * In versions prior to 1.3.12, this function returns the length of the output
248 * The return value of g_vsnprintf() conforms to the vsnprintf() function
249 * as standardized in ISO C99. Note that this is different from traditional
250 * vsnprintf(), which returns the length of the output string.
252 * The format string may contain positional parameters, as specified in
253 * the Single Unix Specification.
255 * Returns: the number of characters which would be produced if the buffer
259 g_vsnprintf (gchar *string,
264 g_return_val_if_fail (n == 0 || string != NULL, 0);
265 g_return_val_if_fail (format != NULL, 0);
267 return _g_vsnprintf (string, n, format, args);
272 * @string: the return location for the newly-allocated string.
273 * @format: the format string. See the printf() documentation.
274 * @args: the list of arguments to insert in the output.
276 * An implementation of the GNU vasprintf() function which supports
277 * positional parameters, as specified in the Single Unix Specification.
278 * This function is similar to g_vsprintf(), except that it allocates a
279 * string to hold the output, instead of putting the output in a buffer
280 * you allocate in advance.
282 * Returns: the number of characters printed.
287 g_vasprintf (gchar **string,
292 g_return_val_if_fail (string != NULL, -1);
294 #ifdef HAVE_VASPRINTF
295 len = _g_vasprintf (string, format, args);
298 else if (!g_mem_is_system_malloc ())
300 gchar *string1 = g_strndup (*string, len);
308 G_VA_COPY (args2, args);
310 *string = g_new (gchar, g_printf_string_upper_bound (format, args));
312 len = _g_vsprintf (*string, format, args2);