1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997, 2002 Peter Mattis, Red Hat, Inc.
4 * SPDX-License-Identifier: LGPL-2.1-or-later
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
28 #include "gprintfint.h"
33 * @format: a standard printf() format string, but notice
34 * [string precision pitfalls][string-precision]
35 * @...: the arguments to insert in the output.
37 * An implementation of the standard printf() function which supports
38 * positional parameters, as specified in the Single Unix Specification.
40 * As with the standard printf(), this does not automatically append a trailing
41 * new-line character to the message, so typically @format should end with its
42 * own new-line character.
44 * `glib/gprintf.h` must be explicitly included in order to use this function.
46 * Returns: the number of bytes printed.
51 g_printf (gchar const *format,
57 va_start (args, format);
58 retval = g_vprintf (format, args);
66 * @file: (not nullable): the stream to write to.
67 * @format: a standard printf() format string, but notice
68 * [string precision pitfalls][string-precision]
69 * @...: the arguments to insert in the output.
71 * An implementation of the standard fprintf() function which supports
72 * positional parameters, as specified in the Single Unix Specification.
74 * `glib/gprintf.h` must be explicitly included in order to use this function.
76 * Returns: the number of bytes printed.
81 g_fprintf (FILE *file,
88 va_start (args, format);
89 retval = g_vfprintf (file, format, args);
97 * @string: A pointer to a memory buffer to contain the resulting string. It
98 * is up to the caller to ensure that the allocated buffer is large
99 * enough to hold the formatted result
100 * @format: a standard printf() format string, but notice
101 * [string precision pitfalls][string-precision]
102 * @...: the arguments to insert in the output.
104 * An implementation of the standard sprintf() function which supports
105 * positional parameters, as specified in the Single Unix Specification.
107 * Note that it is usually better to use g_snprintf(), to avoid the
108 * risk of buffer overflow.
110 * `glib/gprintf.h` must be explicitly included in order to use this function.
112 * See also g_strdup_printf().
114 * Returns: the number of bytes printed.
119 g_sprintf (gchar *string,
126 va_start (args, format);
127 retval = g_vsprintf (string, format, args);
135 * @string: the buffer to hold the output.
136 * @n: the maximum number of bytes to produce (including the
137 * terminating nul character).
138 * @format: a standard printf() format string, but notice
139 * [string precision pitfalls][string-precision]
140 * @...: the arguments to insert in the output.
142 * A safer form of the standard sprintf() function. The output is guaranteed
143 * to not exceed @n characters (including the terminating nul character), so
144 * it is easy to ensure that a buffer overflow cannot occur.
146 * See also g_strdup_printf().
148 * In versions of GLib prior to 1.2.3, this function may return -1 if the
149 * output was truncated, and the truncated string may not be nul-terminated.
150 * In versions prior to 1.3.12, this function returns the length of the output
153 * The return value of g_snprintf() conforms to the snprintf()
154 * function as standardized in ISO C99. Note that this is different from
155 * traditional snprintf(), which returns the length of the output string.
157 * The format string may contain positional parameters, as specified in
158 * the Single Unix Specification.
160 * Returns: the number of bytes which would be produced if the buffer
164 g_snprintf (gchar *string,
172 va_start (args, format);
173 retval = g_vsnprintf (string, n, format, args);
181 * @format: a standard printf() format string, but notice
182 * [string precision pitfalls][string-precision]
183 * @args: the list of arguments to insert in the output.
185 * An implementation of the standard vprintf() function which supports
186 * positional parameters, as specified in the Single Unix Specification.
188 * `glib/gprintf.h` must be explicitly included in order to use this function.
190 * Returns: the number of bytes printed.
195 g_vprintf (gchar const *format,
198 g_return_val_if_fail (format != NULL, -1);
200 return _g_vprintf (format, args);
205 * @file: (not nullable): the stream to write to.
206 * @format: a standard printf() format string, but notice
207 * [string precision pitfalls][string-precision]
208 * @args: the list of arguments to insert in the output.
210 * An implementation of the standard fprintf() function which supports
211 * positional parameters, as specified in the Single Unix Specification.
213 * `glib/gprintf.h` must be explicitly included in order to use this function.
215 * Returns: the number of bytes printed.
220 g_vfprintf (FILE *file,
224 g_return_val_if_fail (format != NULL, -1);
226 return _g_vfprintf (file, format, args);
231 * @string: the buffer to hold the output.
232 * @format: a standard printf() format string, but notice
233 * [string precision pitfalls][string-precision]
234 * @args: the list of arguments to insert in the output.
236 * An implementation of the standard vsprintf() function which supports
237 * positional parameters, as specified in the Single Unix Specification.
239 * `glib/gprintf.h` must be explicitly included in order to use this function.
241 * Returns: the number of bytes printed.
246 g_vsprintf (gchar *string,
250 g_return_val_if_fail (string != NULL, -1);
251 g_return_val_if_fail (format != NULL, -1);
253 return _g_vsprintf (string, format, args);
258 * @string: the buffer to hold the output.
259 * @n: the maximum number of bytes to produce (including the
260 * terminating nul character).
261 * @format: a standard printf() format string, but notice
262 * [string precision pitfalls][string-precision]
263 * @args: the list of arguments to insert in the output.
265 * A safer form of the standard vsprintf() function. The output is guaranteed
266 * to not exceed @n characters (including the terminating nul character), so
267 * it is easy to ensure that a buffer overflow cannot occur.
269 * See also g_strdup_vprintf().
271 * In versions of GLib prior to 1.2.3, this function may return -1 if the
272 * output was truncated, and the truncated string may not be nul-terminated.
273 * In versions prior to 1.3.12, this function returns the length of the output
276 * The return value of g_vsnprintf() conforms to the vsnprintf() function
277 * as standardized in ISO C99. Note that this is different from traditional
278 * vsnprintf(), which returns the length of the output string.
280 * The format string may contain positional parameters, as specified in
281 * the Single Unix Specification.
283 * Returns: the number of bytes which would be produced if the buffer
287 g_vsnprintf (gchar *string,
292 g_return_val_if_fail (n == 0 || string != NULL, -1);
293 g_return_val_if_fail (format != NULL, -1);
295 return _g_vsnprintf (string, n, format, args);
300 * @string: (not optional) (nullable): the return location for the newly-allocated string,
301 * which will be %NULL if (and only if) this function fails
302 * @format: (not nullable): a standard printf() format string, but notice
303 * [string precision pitfalls][string-precision]
304 * @args: the list of arguments to insert in the output.
306 * An implementation of the GNU vasprintf() function which supports
307 * positional parameters, as specified in the Single Unix Specification.
308 * This function is similar to g_vsprintf(), except that it allocates a
309 * string to hold the output, instead of putting the output in a buffer
310 * you allocate in advance.
312 * The returned value in @string is guaranteed to be non-NULL, unless
313 * @format contains `%lc` or `%ls` conversions, which can fail if no
314 * multibyte representation is available for the given character.
316 * `glib/gprintf.h` must be explicitly included in order to use this function.
318 * Returns: the number of bytes printed, or `-1` on failure
323 g_vasprintf (gchar **string,
328 g_return_val_if_fail (string != NULL, -1);
330 #if !defined(USE_SYSTEM_PRINTF)
332 len = _g_gnulib_vasprintf (string, format, args);
336 #elif defined (HAVE_VASPRINTF)
340 len = vasprintf (string, format, args);
344 if (saved_errno == ENOMEM)
346 /* Try and print a message to be a bit helpful, but stick to the
347 * bare minimum to avoid any code path which could try and fail to
348 * allocate additional memory. */
349 fputs (G_STRLOC, stderr);
350 fputs (": failed to allocate memory\n", stderr);
363 va_copy (args2, args);
365 *string = g_new (gchar, g_printf_string_upper_bound (format, args));
367 len = _g_vsprintf (*string, format, args2);