gsettings test: fix srcdir != builddir
[glib.git] / glib / gprintf.c
blobf106a11abb7bf93003eeeb22bb54ca4562375dec
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.
20 #include "config.h"
22 #include <stdarg.h>
23 #include <stdlib.h>
24 #include <stdio.h>
26 #include "gprintf.h"
27 #include "gprintfint.h"
30 /**
31 * g_printf:
32 * @format: a standard printf() format string, but notice
33 * <link linkend="string-precision">string precision pitfalls</link>.
34 * @...: the arguments to insert in the output.
36 * An implementation of the standard printf() function which supports
37 * positional parameters, as specified in the Single Unix Specification.
39 * As with the standard printf(), this does not automatically append a trailing
40 * new-line character to the message, so typically @format should end with its
41 * own new-line character.
43 * Returns: the number of bytes printed.
45 * Since: 2.2
46 **/
47 gint
48 g_printf (gchar const *format,
49 ...)
51 va_list args;
52 gint retval;
54 va_start (args, format);
55 retval = g_vprintf (format, args);
56 va_end (args);
58 return retval;
61 /**
62 * g_fprintf:
63 * @file: the stream to write to.
64 * @format: a standard printf() format string, but notice
65 * <link linkend="string-precision">string precision pitfalls</link>.
66 * @...: the arguments to insert in the output.
68 * An implementation of the standard fprintf() function which supports
69 * positional parameters, as specified in the Single Unix Specification.
71 * Returns: the number of bytes printed.
73 * Since: 2.2
74 **/
75 gint
76 g_fprintf (FILE *file,
77 gchar const *format,
78 ...)
80 va_list args;
81 gint retval;
83 va_start (args, format);
84 retval = g_vfprintf (file, format, args);
85 va_end (args);
87 return retval;
90 /**
91 * g_sprintf:
92 * @string: A pointer to a memory buffer to contain the resulting string. It
93 * is up to the caller to ensure that the allocated buffer is large
94 * enough to hold the formatted result
95 * @format: a standard printf() format string, but notice
96 * <link linkend="string-precision">string precision pitfalls</link>.
97 * @...: 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 * Note that it is usually better to use g_snprintf(), to avoid the
103 * risk of buffer overflow.
105 * See also g_strdup_printf().
107 * Returns: the number of bytes printed.
109 * Since: 2.2
111 gint
112 g_sprintf (gchar *string,
113 gchar const *format,
114 ...)
116 va_list args;
117 gint retval;
119 va_start (args, format);
120 retval = g_vsprintf (string, format, args);
121 va_end (args);
123 return retval;
127 * g_snprintf:
128 * @string: the buffer to hold the output.
129 * @n: the maximum number of bytes to produce (including the
130 * terminating nul character).
131 * @format: a standard printf() format string, but notice
132 * <link linkend="string-precision">string precision pitfalls</link>.
133 * @...: the arguments to insert in the output.
135 * A safer form of the standard sprintf() function. The output is guaranteed
136 * to not exceed @n characters (including the terminating nul character), so
137 * it is easy to ensure that a buffer overflow cannot occur.
139 * See also g_strdup_printf().
141 * In versions of GLib prior to 1.2.3, this function may return -1 if the
142 * output was truncated, and the truncated string may not be nul-terminated.
143 * In versions prior to 1.3.12, this function returns the length of the output
144 * string.
146 * The return value of g_snprintf() conforms to the snprintf()
147 * function as standardized in ISO C99. Note that this is different from
148 * traditional snprintf(), which returns the length of the output string.
150 * The format string may contain positional parameters, as specified in
151 * the Single Unix Specification.
153 * Returns: the number of bytes which would be produced if the buffer
154 * was large enough.
156 gint
157 g_snprintf (gchar *string,
158 gulong n,
159 gchar const *format,
160 ...)
162 va_list args;
163 gint retval;
165 va_start (args, format);
166 retval = g_vsnprintf (string, n, format, args);
167 va_end (args);
169 return retval;
173 * g_vprintf:
174 * @format: a standard printf() format string, but notice
175 * <link linkend="string-precision">string precision pitfalls</link>.
176 * @args: the list of arguments to insert in the output.
178 * An implementation of the standard vprintf() function which supports
179 * positional parameters, as specified in the Single Unix Specification.
181 * Returns: the number of bytes printed.
183 * Since: 2.2
185 gint
186 g_vprintf (gchar const *format,
187 va_list args)
189 g_return_val_if_fail (format != NULL, -1);
191 return _g_vprintf (format, args);
195 * g_vfprintf:
196 * @file: the stream to write to.
197 * @format: a standard printf() format string, but notice
198 * <link linkend="string-precision">string precision pitfalls</link>.
199 * @args: the list of arguments to insert in the output.
201 * An implementation of the standard fprintf() function which supports
202 * positional parameters, as specified in the Single Unix Specification.
204 * Returns: the number of bytes printed.
206 * Since: 2.2
208 gint
209 g_vfprintf (FILE *file,
210 gchar const *format,
211 va_list args)
213 g_return_val_if_fail (format != NULL, -1);
215 return _g_vfprintf (file, format, args);
219 * g_vsprintf:
220 * @string: the buffer to hold the output.
221 * @format: a standard printf() format string, but notice
222 * <link linkend="string-precision">string precision pitfalls</link>.
223 * @args: the list of arguments to insert in the output.
225 * An implementation of the standard vsprintf() function which supports
226 * positional parameters, as specified in the Single Unix Specification.
228 * Returns: the number of bytes printed.
230 * Since: 2.2
232 gint
233 g_vsprintf (gchar *string,
234 gchar const *format,
235 va_list args)
237 g_return_val_if_fail (string != NULL, -1);
238 g_return_val_if_fail (format != NULL, -1);
240 return _g_vsprintf (string, format, args);
243 /**
244 * g_vsnprintf:
245 * @string: the buffer to hold the output.
246 * @n: the maximum number of bytes to produce (including the
247 * terminating nul character).
248 * @format: a standard printf() format string, but notice
249 * <link linkend="string-precision">string precision pitfalls</link>.
250 * @args: the list of arguments to insert in the output.
252 * A safer form of the standard vsprintf() function. The output is guaranteed
253 * to not exceed @n characters (including the terminating nul character), so
254 * it is easy to ensure that a buffer overflow cannot occur.
256 * See also g_strdup_vprintf().
258 * In versions of GLib prior to 1.2.3, this function may return -1 if the
259 * output was truncated, and the truncated string may not be nul-terminated.
260 * In versions prior to 1.3.12, this function returns the length of the output
261 * string.
263 * The return value of g_vsnprintf() conforms to the vsnprintf() function
264 * as standardized in ISO C99. Note that this is different from traditional
265 * vsnprintf(), which returns the length of the output string.
267 * The format string may contain positional parameters, as specified in
268 * the Single Unix Specification.
270 * Returns: the number of bytes which would be produced if the buffer
271 * was large enough.
273 gint
274 g_vsnprintf (gchar *string,
275 gulong n,
276 gchar const *format,
277 va_list args)
279 g_return_val_if_fail (n == 0 || string != NULL, -1);
280 g_return_val_if_fail (format != NULL, -1);
282 return _g_vsnprintf (string, n, format, args);
286 * g_vasprintf:
287 * @string: the return location for the newly-allocated string.
288 * @format: a standard printf() format string, but notice
289 * <link linkend="string-precision">string precision pitfalls</link>.
290 * @args: the list of arguments to insert in the output.
292 * An implementation of the GNU vasprintf() function which supports
293 * positional parameters, as specified in the Single Unix Specification.
294 * This function is similar to g_vsprintf(), except that it allocates a
295 * string to hold the output, instead of putting the output in a buffer
296 * you allocate in advance.
298 * Returns: the number of bytes printed.
300 * Since: 2.4
302 gint
303 g_vasprintf (gchar **string,
304 gchar const *format,
305 va_list args)
307 gint len;
308 g_return_val_if_fail (string != NULL, -1);
310 #if !defined(HAVE_GOOD_PRINTF)
312 len = _g_gnulib_vasprintf (string, format, args);
313 if (len < 0)
314 *string = NULL;
316 #elif defined (HAVE_VASPRINTF)
318 len = vasprintf (string, format, args);
319 if (len < 0)
320 *string = NULL;
321 else if (!g_mem_is_system_malloc ())
323 /* vasprintf returns malloc-allocated memory */
324 gchar *string1 = g_strndup (*string, len);
325 free (*string);
326 *string = string1;
329 #else
332 va_list args2;
334 G_VA_COPY (args2, args);
336 *string = g_new (gchar, g_printf_string_upper_bound (format, args));
338 len = _g_vsprintf (*string, format, args2);
339 va_end (args2);
341 #endif
343 return len;