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