hook gvariant vectors up to kdbus
[platform/upstream/glib.git] / glib / gprintf.c
index 209abf6..cf4bf21 100644 (file)
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  */
 
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-
-#define _GNU_SOURCE            /* For vasprintf */
+#include "config.h"
 
 #include <stdarg.h>
 #include <stdlib.h>
 #include <stdio.h>
 
-#include "glib.h"
 #include "gprintf.h"
 #include "gprintfint.h"
 
+
 /**
  * g_printf:
- * @format: the format string. See the printf() documentation.
- * @Varargs: the arguments to insert in the output.
+ * @format: a standard printf() format string, but notice 
+ *          [string precision pitfalls][string-precision]
+ * @...: the arguments to insert in the output.
  *
  * An implementation of the standard printf() function which supports 
  * positional parameters, as specified in the Single Unix Specification.
  *
- * Returns: the number of characters printed.
+ * As with the standard printf(), this does not automatically append a trailing
+ * new-line character to the message, so typically @format should end with its
+ * own new-line character.
+ *
+ * Returns: the number of bytes printed.
  *
  * Since: 2.2
  **/
@@ -60,13 +59,14 @@ g_printf (gchar const *format,
 /**
  * g_fprintf:
  * @file: the stream to write to.
- * @format: the format string. See the printf() documentation.
- * @Varargs: the arguments to insert in the output.
+ * @format: a standard printf() format string, but notice 
+ *          [string precision pitfalls][string-precision]
+ * @...: the arguments to insert in the output.
  *
  * An implementation of the standard fprintf() function which supports 
  * positional parameters, as specified in the Single Unix Specification.
  *
- * Returns: the number of characters printed.
+ * Returns: the number of bytes printed.
  *
  * Since: 2.2
  **/
@@ -87,14 +87,22 @@ g_fprintf (FILE        *file,
 
 /**
  * g_sprintf:
- * @string: the buffer to hold the output.
- * @format: the format string. See the printf() documentation.
- * @Varargs: the arguments to insert in the output.
- *
- * An implementation of the standard sprintf() function which supports 
+ * @string: A pointer to a memory buffer to contain the resulting string. It
+ *          is up to the caller to ensure that the allocated buffer is large
+ *          enough to hold the formatted result
+ * @format: a standard printf() format string, but notice
+ *          [string precision pitfalls][string-precision]
+ * @...: the arguments to insert in the output.
+ *
+ * An implementation of the standard sprintf() function which supports
  * positional parameters, as specified in the Single Unix Specification.
  *
- * Returns: the number of characters printed.
+ * Note that it is usually better to use g_snprintf(), to avoid the
+ * risk of buffer overflow.
+ *
+ * See also g_strdup_printf().
+ *
+ * Returns: the number of bytes printed.
  *
  * Since: 2.2
  **/
@@ -116,30 +124,31 @@ g_sprintf (gchar       *string,
 /**
  * g_snprintf:
  * @string: the buffer to hold the output.
- * @n: the maximum number of characters to produce (including the 
+ * @n: the maximum number of bytes to produce (including the
  *     terminating nul character).
- * @format: the format string. See the printf() documentation.
- * @Varargs: the arguments to insert in the output.
+ * @format: a standard printf() format string, but notice
+ *          [string precision pitfalls][string-precision]
+ * @...: the arguments to insert in the output.
  *
  * A safer form of the standard sprintf() function. The output is guaranteed
- * to not exceed @n characters (including the terminating nul character), so 
+ * to not exceed @n characters (including the terminating nul character), so
  * it is easy to ensure that a buffer overflow cannot occur.
- * 
+ *
  * See also g_strdup_printf().
  *
- * In versions of GLib prior to 1.2.3, this function may return -1 if the 
- * output was truncated, and the truncated string may not be nul-terminated. 
- * In versions prior to 1.3.12, this function returns the length of the output 
+ * In versions of GLib prior to 1.2.3, this function may return -1 if the
+ * output was truncated, and the truncated string may not be nul-terminated.
+ * In versions prior to 1.3.12, this function returns the length of the output
  * string.
  *
  * The return value of g_snprintf() conforms to the snprintf()
- * function as standardized in ISO C99. Note that this is different from 
+ * function as standardized in ISO C99. Note that this is different from
  * traditional snprintf(), which returns the length of the output string.
  *
- * The format string may contain positional parameters, as specified in 
+ * The format string may contain positional parameters, as specified in
  * the Single Unix Specification.
  *
- * Returns: the number of characters which would be produced if the buffer 
+ * Returns: the number of bytes which would be produced if the buffer 
  *     was large enough.
  **/
 gint
@@ -160,13 +169,14 @@ g_snprintf (gchar *string,
 
 /**
  * g_vprintf:
- * @format: the format string. See the printf() documentation.
+ * @format: a standard printf() format string, but notice 
+ *          [string precision pitfalls][string-precision]
  * @args: the list of arguments to insert in the output.
  *
  * An implementation of the standard vprintf() function which supports 
  * positional parameters, as specified in the Single Unix Specification.
  *
- * Returns: the number of characters printed.
+ * Returns: the number of bytes printed.
  *
  * Since: 2.2
  **/
@@ -174,7 +184,7 @@ gint
 g_vprintf (gchar const *format,
           va_list      args)
 {
-  g_return_val_if_fail (format != NULL, 0);
+  g_return_val_if_fail (format != NULL, -1);
 
   return _g_vprintf (format, args);
 }
@@ -182,13 +192,14 @@ g_vprintf (gchar const *format,
 /**
  * g_vfprintf:
  * @file: the stream to write to.
- * @format: the format string. See the printf() documentation.
+ * @format: a standard printf() format string, but notice 
+ *          [string precision pitfalls][string-precision]
  * @args: the list of arguments to insert in the output.
  *
  * An implementation of the standard fprintf() function which supports 
  * positional parameters, as specified in the Single Unix Specification.
  *
- * Returns: the number of characters printed.
+ * Returns: the number of bytes printed.
  *
  * Since: 2.2
  **/
@@ -197,7 +208,7 @@ g_vfprintf (FILE        *file,
             gchar const *format,
            va_list      args)
 {
-  g_return_val_if_fail (format != NULL, 0);
+  g_return_val_if_fail (format != NULL, -1);
 
   return _g_vfprintf (file, format, args);
 }
@@ -205,13 +216,14 @@ g_vfprintf (FILE        *file,
 /**
  * g_vsprintf:
  * @string: the buffer to hold the output.
- * @format: the format string. See the printf() documentation.
+ * @format: a standard printf() format string, but notice 
+ *          [string precision pitfalls][string-precision]
  * @args: the list of arguments to insert in the output.
  *
  * An implementation of the standard vsprintf() function which supports 
  * positional parameters, as specified in the Single Unix Specification.
  *
- * Returns: the number of characters printed.
+ * Returns: the number of bytes printed.
  *
  * Since: 2.2
  **/
@@ -220,8 +232,8 @@ g_vsprintf (gchar    *string,
            gchar const *format,
            va_list      args)
 {
-  g_return_val_if_fail (string != NULL, 0);
-  g_return_val_if_fail (format != NULL, 0);
+  g_return_val_if_fail (string != NULL, -1);
+  g_return_val_if_fail (format != NULL, -1);
 
   return _g_vsprintf (string, format, args);
 }
@@ -229,9 +241,10 @@ g_vsprintf (gchar   *string,
 /** 
  * g_vsnprintf:
  * @string: the buffer to hold the output.
- * @n: the maximum number of characters to produce (including the 
+ * @n: the maximum number of bytes to produce (including the 
  *     terminating nul character).
- * @format: the format string. See the printf() documentation.
+ * @format: a standard printf() format string, but notice 
+ *          string precision pitfalls][string-precision]
  * @args: the list of arguments to insert in the output.
  *
  * A safer form of the standard vsprintf() function. The output is guaranteed
@@ -252,7 +265,7 @@ g_vsprintf (gchar    *string,
  * The format string may contain positional parameters, as specified in 
  * the Single Unix Specification.
  *
- * Returns: the number of characters which would be produced if the buffer 
+ * Returns: the number of bytes which would be produced if the buffer 
  *  was large enough.
  */
 gint
@@ -261,8 +274,8 @@ g_vsnprintf (gchar   *string,
             gchar const *format,
             va_list      args)
 {
-  g_return_val_if_fail (n == 0 || string != NULL, 0);
-  g_return_val_if_fail (format != NULL, 0);
+  g_return_val_if_fail (n == 0 || string != NULL, -1);
+  g_return_val_if_fail (format != NULL, -1);
 
   return _g_vsnprintf (string, n, format, args);
 }
@@ -270,7 +283,8 @@ g_vsnprintf (gchar   *string,
 /**
  * g_vasprintf:
  * @string: the return location for the newly-allocated string.
- * @format: the format string. See the printf() documentation.
+ * @format: a standard printf() format string, but notice
+ *          [string precision pitfalls][string-precision]
  * @args: the list of arguments to insert in the output.
  *
  * An implementation of the GNU vasprintf() function which supports 
@@ -279,7 +293,7 @@ g_vsnprintf (gchar   *string,
  * string to hold the output, instead of putting the output in a buffer 
  * you allocate in advance.
  *
- * Returns: the number of characters printed.
+ * Returns: the number of bytes printed.
  *
  * Since: 2.4
  **/
@@ -291,17 +305,27 @@ g_vasprintf (gchar      **string,
   gint len;
   g_return_val_if_fail (string != NULL, -1);
 
-#ifdef HAVE_VASPRINTF
-  len = _g_vasprintf (string, format, args);
+#if !defined(HAVE_GOOD_PRINTF)
+
+  len = _g_gnulib_vasprintf (string, format, args);
+  if (len < 0)
+    *string = NULL;
+
+#elif defined (HAVE_VASPRINTF)
+
+  len = vasprintf (string, format, args);
   if (len < 0)
     *string = NULL;
   else if (!g_mem_is_system_malloc ()) 
     {
+      /* vasprintf returns malloc-allocated memory */
       gchar *string1 = g_strndup (*string, len);
       free (*string);
       *string = string1;
     }
+
 #else
+
   {
     va_list args2;
 
@@ -316,6 +340,3 @@ g_vasprintf (gchar      **string,
 
   return len;
 }
-
-
-