06fb6cb2b27b7bff1650835514a9a1c21f8116db
[platform/upstream/glib.git] / glib / gprintf.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997, 2002  Peter Mattis, Red Hat, Inc.
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include "config.h"
21
22 #ifndef _WIN32
23 #define _GNU_SOURCE             /* For vasprintf */
24 #endif
25
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29
30 #include "glib.h"
31 #include "gprintf.h"
32 #include "gprintfint.h"
33
34
35 /**
36  * g_printf:
37  * @format: a standard printf() format string, but notice 
38  *          <link linkend="string-precision">string precision pitfalls</link>.
39  * @Varargs: the arguments to insert in the output.
40  *
41  * An implementation of the standard printf() function which supports 
42  * positional parameters, as specified in the Single Unix Specification.
43  *
44  * Returns: the number of bytes printed.
45  *
46  * Since: 2.2
47  **/
48 gint
49 g_printf (gchar const *format,
50           ...)
51 {
52   va_list args;
53   gint retval;
54
55   va_start (args, format);
56   retval = g_vprintf (format, args);
57   va_end (args);
58   
59   return retval;
60 }
61
62 /**
63  * g_fprintf:
64  * @file: the stream to write to.
65  * @format: a standard printf() format string, but notice 
66  *          <link linkend="string-precision">string precision pitfalls</link>.
67  * @Varargs: the arguments to insert in the output.
68  *
69  * An implementation of the standard fprintf() function which supports 
70  * positional parameters, as specified in the Single Unix Specification.
71  *
72  * Returns: the number of bytes printed.
73  *
74  * Since: 2.2
75  **/
76 gint
77 g_fprintf (FILE        *file, 
78            gchar const *format,
79            ...)
80 {
81   va_list args;
82   gint retval;
83
84   va_start (args, format);
85   retval = g_vfprintf (file, format, args);
86   va_end (args);
87   
88   return retval;
89 }
90
91 /**
92  * g_sprintf:
93  * @string: A pointer to a memory buffer to contain the resulting string. It
94  *          is up to the caller to ensure that the allocated buffer is large
95  *          enough to hold the formatted result
96  * @format: a standard printf() format string, but notice
97  *          <link linkend="string-precision">string precision pitfalls</link>.
98  * @Varargs: the arguments to insert in the output.
99  *
100  * An implementation of the standard sprintf() function which supports
101  * positional parameters, as specified in the Single Unix Specification.
102  *
103  * Note that it is usually better to use g_snprintf(), to avoid the
104  * risk of buffer overflow.
105  *
106  * See also g_strdup_printf().
107  *
108  * Returns: the number of bytes printed.
109  *
110  * Since: 2.2
111  **/
112 gint
113 g_sprintf (gchar       *string,
114            gchar const *format,
115            ...)
116 {
117   va_list args;
118   gint retval;
119
120   va_start (args, format);
121   retval = g_vsprintf (string, format, args);
122   va_end (args);
123   
124   return retval;
125 }
126
127 /**
128  * g_snprintf:
129  * @string: the buffer to hold the output.
130  * @n: the maximum number of bytes to produce (including the
131  *     terminating nul character).
132  * @format: a standard printf() format string, but notice
133  *          <link linkend="string-precision">string precision pitfalls</link>.
134  * @Varargs: the arguments to insert in the output.
135  *
136  * A safer form of the standard sprintf() function. The output is guaranteed
137  * to not exceed @n characters (including the terminating nul character), so
138  * it is easy to ensure that a buffer overflow cannot occur.
139  *
140  * See also g_strdup_printf().
141  *
142  * In versions of GLib prior to 1.2.3, this function may return -1 if the
143  * output was truncated, and the truncated string may not be nul-terminated.
144  * In versions prior to 1.3.12, this function returns the length of the output
145  * string.
146  *
147  * The return value of g_snprintf() conforms to the snprintf()
148  * function as standardized in ISO C99. Note that this is different from
149  * traditional snprintf(), which returns the length of the output string.
150  *
151  * The format string may contain positional parameters, as specified in
152  * the Single Unix Specification.
153  *
154  * Returns: the number of bytes which would be produced if the buffer 
155  *     was large enough.
156  **/
157 gint
158 g_snprintf (gchar       *string,
159             gulong       n,
160             gchar const *format,
161             ...)
162 {
163   va_list args;
164   gint retval;
165
166   va_start (args, format);
167   retval = g_vsnprintf (string, n, format, args);
168   va_end (args);
169   
170   return retval;
171 }
172
173 /**
174  * g_vprintf:
175  * @format: a standard printf() format string, but notice 
176  *          <link linkend="string-precision">string precision pitfalls</link>.
177  * @args: the list of arguments to insert in the output.
178  *
179  * An implementation of the standard vprintf() function which supports 
180  * positional parameters, as specified in the Single Unix Specification.
181  *
182  * Returns: the number of bytes printed.
183  *
184  * Since: 2.2
185  **/
186 gint
187 g_vprintf (gchar const *format,
188            va_list      args)
189 {
190   g_return_val_if_fail (format != NULL, -1);
191
192   return _g_vprintf (format, args);
193 }
194
195 /**
196  * g_vfprintf:
197  * @file: the stream to write to.
198  * @format: a standard printf() format string, but notice 
199  *          <link linkend="string-precision">string precision pitfalls</link>.
200  * @args: the list of arguments to insert in the output.
201  *
202  * An implementation of the standard fprintf() function which supports 
203  * positional parameters, as specified in the Single Unix Specification.
204  *
205  * Returns: the number of bytes printed.
206  *
207  * Since: 2.2
208  **/
209 gint
210 g_vfprintf (FILE        *file,
211             gchar const *format,
212             va_list      args)
213 {
214   g_return_val_if_fail (format != NULL, -1);
215
216   return _g_vfprintf (file, format, args);
217 }
218
219 /**
220  * g_vsprintf:
221  * @string: the buffer to hold the output.
222  * @format: a standard printf() format string, but notice 
223  *          <link linkend="string-precision">string precision pitfalls</link>.
224  * @args: the list of arguments to insert in the output.
225  *
226  * An implementation of the standard vsprintf() function which supports 
227  * positional parameters, as specified in the Single Unix Specification.
228  *
229  * Returns: the number of bytes printed.
230  *
231  * Since: 2.2
232  **/
233 gint
234 g_vsprintf (gchar        *string,
235             gchar const *format,
236             va_list      args)
237 {
238   g_return_val_if_fail (string != NULL, -1);
239   g_return_val_if_fail (format != NULL, -1);
240
241   return _g_vsprintf (string, format, args);
242 }
243
244 /** 
245  * g_vsnprintf:
246  * @string: the buffer to hold the output.
247  * @n: the maximum number of bytes to produce (including the 
248  *     terminating nul character).
249  * @format: a standard printf() format string, but notice 
250  *          <link linkend="string-precision">string precision pitfalls</link>.
251  * @args: the list of arguments to insert in the output.
252  *
253  * A safer form of the standard vsprintf() function. The output is guaranteed
254  * to not exceed @n characters (including the terminating nul character), so 
255  * it is easy to ensure that a buffer overflow cannot occur.
256  *
257  * See also g_strdup_vprintf().
258  *
259  * In versions of GLib prior to 1.2.3, this function may return -1 if the 
260  * output was truncated, and the truncated string may not be nul-terminated.
261  * In versions prior to 1.3.12, this function returns the length of the output 
262  * string.
263  *
264  * The return value of g_vsnprintf() conforms to the vsnprintf() function 
265  * as standardized in ISO C99. Note that this is different from traditional 
266  * vsnprintf(), which returns the length of the output string.
267  *
268  * The format string may contain positional parameters, as specified in 
269  * the Single Unix Specification.
270  *
271  * Returns: the number of bytes which would be produced if the buffer 
272  *  was large enough.
273  */
274 gint
275 g_vsnprintf (gchar       *string,
276              gulong       n,
277              gchar const *format,
278              va_list      args)
279 {
280   g_return_val_if_fail (n == 0 || string != NULL, -1);
281   g_return_val_if_fail (format != NULL, -1);
282
283   return _g_vsnprintf (string, n, format, args);
284 }
285
286 /**
287  * g_vasprintf:
288  * @string: the return location for the newly-allocated string.
289  * @format: a standard printf() format string, but notice
290  *          <link linkend="string-precision">string precision pitfalls</link>.
291  * @args: the list of arguments to insert in the output.
292  *
293  * An implementation of the GNU vasprintf() function which supports 
294  * positional parameters, as specified in the Single Unix Specification.
295  * This function is similar to g_vsprintf(), except that it allocates a 
296  * string to hold the output, instead of putting the output in a buffer 
297  * you allocate in advance.
298  *
299  * Returns: the number of bytes printed.
300  *
301  * Since: 2.4
302  **/
303 gint 
304 g_vasprintf (gchar      **string,
305              gchar const *format,
306              va_list      args)
307 {
308   gint len;
309   g_return_val_if_fail (string != NULL, -1);
310
311 #if !defined(HAVE_GOOD_PRINTF)
312
313   len = _g_gnulib_vasprintf (string, format, args);
314   if (len < 0)
315     *string = NULL;
316
317 #elif defined (HAVE_VASPRINTF)
318
319   len = vasprintf (string, format, args);
320   if (len < 0)
321     *string = NULL;
322   else if (!g_mem_is_system_malloc ()) 
323     {
324       /* vasprintf returns malloc-allocated memory */
325       gchar *string1 = g_strndup (*string, len);
326       free (*string);
327       *string = string1;
328     }
329
330 #else
331
332   {
333     va_list args2;
334
335     G_VA_COPY (args2, args);
336
337     *string = g_new (gchar, g_printf_string_upper_bound (format, args));
338
339     len = _g_vsprintf (*string, format, args2);
340     va_end (args2);
341   }
342 #endif
343
344   return len;
345 }