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, see <http://www.gnu.org/licenses/>.
25 #include "gprintfint.h"
30 * @format: a standard printf() format string, but notice
31 * [string precision pitfalls][string-precision]
32 * @...: the arguments to insert in the output.
34 * An implementation of the standard printf() function which supports
35 * positional parameters, as specified in the Single Unix Specification.
37 * As with the standard printf(), this does not automatically append a trailing
38 * new-line character to the message, so typically @format should end with its
39 * own new-line character.
41 * Returns: the number of bytes printed.
46 g_printf (gchar
const *format
,
52 va_start (args
, format
);
53 retval
= g_vprintf (format
, args
);
61 * @file: (not nullable): the stream to write to.
62 * @format: a standard printf() format string, but notice
63 * [string precision pitfalls][string-precision]
64 * @...: 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 bytes printed.
74 g_fprintf (FILE *file
,
81 va_start (args
, format
);
82 retval
= g_vfprintf (file
, format
, args
);
90 * @string: A pointer to a memory buffer to contain the resulting string. It
91 * is up to the caller to ensure that the allocated buffer is large
92 * enough to hold the formatted result
93 * @format: a standard printf() format string, but notice
94 * [string precision pitfalls][string-precision]
95 * @...: the arguments to insert in the output.
97 * An implementation of the standard sprintf() function which supports
98 * positional parameters, as specified in the Single Unix Specification.
100 * Note that it is usually better to use g_snprintf(), to avoid the
101 * risk of buffer overflow.
103 * See also g_strdup_printf().
105 * Returns: the number of bytes printed.
110 g_sprintf (gchar
*string
,
117 va_start (args
, format
);
118 retval
= g_vsprintf (string
, format
, args
);
126 * @string: the buffer to hold the output.
127 * @n: the maximum number of bytes to produce (including the
128 * terminating nul character).
129 * @format: a standard printf() format string, but notice
130 * [string precision pitfalls][string-precision]
131 * @...: the arguments to insert in the output.
133 * A safer form of the standard sprintf() function. The output is guaranteed
134 * to not exceed @n characters (including the terminating nul character), so
135 * it is easy to ensure that a buffer overflow cannot occur.
137 * See also g_strdup_printf().
139 * In versions of GLib prior to 1.2.3, this function may return -1 if the
140 * output was truncated, and the truncated string may not be nul-terminated.
141 * In versions prior to 1.3.12, this function returns the length of the output
144 * The return value of g_snprintf() conforms to the snprintf()
145 * function as standardized in ISO C99. Note that this is different from
146 * traditional snprintf(), which returns the length of the output string.
148 * The format string may contain positional parameters, as specified in
149 * the Single Unix Specification.
151 * Returns: the number of bytes which would be produced if the buffer
155 g_snprintf (gchar
*string
,
163 va_start (args
, format
);
164 retval
= g_vsnprintf (string
, n
, format
, args
);
172 * @format: a standard printf() format string, but notice
173 * [string precision pitfalls][string-precision]
174 * @args: the list of arguments to insert in the output.
176 * An implementation of the standard vprintf() function which supports
177 * positional parameters, as specified in the Single Unix Specification.
179 * Returns: the number of bytes printed.
184 g_vprintf (gchar
const *format
,
187 g_return_val_if_fail (format
!= NULL
, -1);
189 return _g_vprintf (format
, args
);
194 * @file: (not nullable): the stream to write to.
195 * @format: a standard printf() format string, but notice
196 * [string precision pitfalls][string-precision]
197 * @args: the list of arguments to insert in the output.
199 * An implementation of the standard fprintf() function which supports
200 * positional parameters, as specified in the Single Unix Specification.
202 * Returns: the number of bytes printed.
207 g_vfprintf (FILE *file
,
211 g_return_val_if_fail (format
!= NULL
, -1);
213 return _g_vfprintf (file
, format
, args
);
218 * @string: the buffer to hold the output.
219 * @format: a standard printf() format string, but notice
220 * [string precision pitfalls][string-precision]
221 * @args: the list of arguments to insert in the output.
223 * An implementation of the standard vsprintf() function which supports
224 * positional parameters, as specified in the Single Unix Specification.
226 * Returns: the number of bytes printed.
231 g_vsprintf (gchar
*string
,
235 g_return_val_if_fail (string
!= NULL
, -1);
236 g_return_val_if_fail (format
!= NULL
, -1);
238 return _g_vsprintf (string
, format
, args
);
243 * @string: the buffer to hold the output.
244 * @n: the maximum number of bytes to produce (including the
245 * terminating nul character).
246 * @format: a standard printf() format string, but notice
247 * string precision pitfalls][string-precision]
248 * @args: the list of arguments to insert in the output.
250 * A safer form of the standard vsprintf() function. The output is guaranteed
251 * to not exceed @n characters (including the terminating nul character), so
252 * it is easy to ensure that a buffer overflow cannot occur.
254 * See also g_strdup_vprintf().
256 * In versions of GLib prior to 1.2.3, this function may return -1 if the
257 * output was truncated, and the truncated string may not be nul-terminated.
258 * In versions prior to 1.3.12, this function returns the length of the output
261 * The return value of g_vsnprintf() conforms to the vsnprintf() function
262 * as standardized in ISO C99. Note that this is different from traditional
263 * vsnprintf(), which returns the length of the output string.
265 * The format string may contain positional parameters, as specified in
266 * the Single Unix Specification.
268 * Returns: the number of bytes which would be produced if the buffer
272 g_vsnprintf (gchar
*string
,
277 g_return_val_if_fail (n
== 0 || string
!= NULL
, -1);
278 g_return_val_if_fail (format
!= NULL
, -1);
280 return _g_vsnprintf (string
, n
, format
, args
);
285 * @string: the return location for the newly-allocated string.
286 * @format: a standard printf() format string, but notice
287 * [string precision pitfalls][string-precision]
288 * @args: the list of arguments to insert in the output.
290 * An implementation of the GNU vasprintf() function which supports
291 * positional parameters, as specified in the Single Unix Specification.
292 * This function is similar to g_vsprintf(), except that it allocates a
293 * string to hold the output, instead of putting the output in a buffer
294 * you allocate in advance.
296 * Returns: the number of bytes printed.
301 g_vasprintf (gchar
**string
,
306 g_return_val_if_fail (string
!= NULL
, -1);
308 #if !defined(HAVE_GOOD_PRINTF)
310 len
= _g_gnulib_vasprintf (string
, format
, args
);
314 #elif defined (HAVE_VASPRINTF)
316 len
= vasprintf (string
, format
, args
);
325 G_VA_COPY (args2
, args
);
327 *string
= g_new (gchar
, g_printf_string_upper_bound (format
, args
));
329 len
= _g_vsprintf (*string
, format
, args2
);