snprintf.c
Go to the documentation of this file.
00001 /*
00002  * snprintf.c - a portable implementation of snprintf
00003  *
00004  * AUTHOR
00005  *   Mark Martinec <mark.martinec@ijs.si>, April 1999.
00006  *
00007  *   Copyright 1999, Mark Martinec. All rights reserved.
00008  *
00009  * TERMS AND CONDITIONS
00010  *   This program is free software; you can redistribute it and/or modify
00011  *   it under the terms of the "Frontier Artistic License" which comes
00012  *   with this Kit.
00013  *
00014  *   This program is distributed in the hope that it will be useful,
00015  *   but WITHOUT ANY WARRANTY; without even the implied warranty
00016  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00017  *   See the Frontier Artistic License for more details.
00018  *
00019  *   You should have received a copy of the Frontier Artistic License
00020  *   with this Kit in the file named LICENSE.txt .
00021  *   If not, I'll be glad to provide one.
00022  *
00023  * FEATURES
00024  * - careful adherence to specs regarding flags, field width and precision;
00025  * - good performance for large string handling (large format, large
00026  *   argument or large paddings). Performance is similar to system's sprintf
00027  *   and in several cases significantly better (make sure you compile with
00028  *   optimizations turned on, tell the compiler the code is strict ANSI
00029  *   if necessary to give it more freedom for optimizations);
00030  * - return value semantics per ISO/IEC 9899:1999 ("ISO C99");
00031  * - written in standard ISO/ANSI C - requires an ANSI C compiler.
00032  *
00033  * SUPPORTED CONVERSION SPECIFIERS AND DATA TYPES
00034  *
00035  * This snprintf only supports the following conversion specifiers:
00036  * s, c, d, u, o, x, X, p  (and synonyms: i, D, U, O - see below)
00037  * with flags: '-', '+', ' ', '0' and '#'.
00038  * An asterisk is supported for field width as well as precision.
00039  *
00040  * Length modifiers 'h' (short int), 'l' (long int),
00041  * and 'll' (long long int) are supported.
00042  * NOTE:
00043  *   If macro SNPRINTF_LONGLONG_SUPPORT is not defined (default) the
00044  *   length modifier 'll' is recognized but treated the same as 'l',
00045  *   which may cause argument value truncation! Defining
00046  *   SNPRINTF_LONGLONG_SUPPORT requires that your system's sprintf also
00047  *   handles length modifier 'll'.  long long int is a language extension
00048  *   which may not be portable.
00049  *
00050  * Conversion of numeric data (conversion specifiers d, u, o, x, X, p)
00051  * with length modifiers (none or h, l, ll) is left to the system routine
00052  * sprintf, but all handling of flags, field width and precision as well as
00053  * c and s conversions is done very carefully by this portable routine.
00054  * If a string precision (truncation) is specified (e.g. %.8s) it is
00055  * guaranteed the string beyond the specified precision will not be referenced.
00056  *
00057  * Length modifiers h, l and ll are ignored for c and s conversions (data
00058  * types wint_t and wchar_t are not supported).
00059  *
00060  * The following common synonyms for conversion characters are supported:
00061  *   - i is a synonym for d
00062  *   - D is a synonym for ld, explicit length modifiers are ignored
00063  *   - U is a synonym for lu, explicit length modifiers are ignored
00064  *   - O is a synonym for lo, explicit length modifiers are ignored
00065  * The D, O and U conversion characters are nonstandard, they are supported
00066  * for backward compatibility only, and should not be used for new code.
00067  *
00068  * The following is specifically NOT supported:
00069  *   - flag ' (thousands' grouping character) is recognized but ignored
00070  *   - numeric conversion specifiers: f, e, E, g, G and synonym F,
00071  *     as well as the new a and A conversion specifiers
00072  *   - length modifier 'L' (long double) and 'q' (quad - use 'll' instead)
00073  *   - wide character/string conversions: lc, ls, and nonstandard
00074  *     synonyms C and S
00075  *   - writeback of converted string length: conversion character n
00076  *   - the n$ specification for direct reference to n-th argument
00077  *   - locales
00078  *
00079  * It is permitted for str_m to be zero, and it is permitted to specify NULL
00080  * pointer for resulting string argument if str_m is zero (as per ISO C99).
00081  *
00082  * The return value is the number of characters which would be generated
00083  * for the given input, excluding the trailing null. If this value
00084  * is greater or equal to str_m, not all characters from the result
00085  * have been stored in str, output bytes beyond the (str_m-1) -th character
00086  * are discarded. If str_m is greater than zero it is guaranteed
00087  * the resulting string will be null-terminated.
00088  *
00089  * NOTE that this matches the ISO C99, OpenBSD, and GNU C library 2.1,
00090  * but is different from some older and vendor implementations,
00091  * and is also different from XPG, XSH5, SUSv2 specifications.
00092  * For historical discussion on changes in the semantics and standards
00093  * of snprintf see printf(3) man page in the Linux programmers manual.
00094  *
00095  * Routines asprintf and vasprintf return a pointer (in the ptr argument)
00096  * to a buffer sufficiently large to hold the resulting string. This pointer
00097  * should be passed to free(3) to release the allocated storage when it is
00098  * no longer needed. If sufficient space cannot be allocated, these functions
00099  * will return -1 and set ptr to be a NULL pointer. These two routines are a
00100  * GNU C library extensions (glibc).
00101  *
00102  * Routines asnprintf and vasnprintf are similar to asprintf and vasprintf,
00103  * yet, like snprintf and vsnprintf counterparts, will write at most str_m-1
00104  * characters into the allocated output string, the last character in the
00105  * allocated buffer then gets the terminating null. If the formatted string
00106  * length (the return value) is greater than or equal to the str_m argument,
00107  * the resulting string was truncated and some of the formatted characters
00108  * were discarded. These routines present a handy way to limit the amount
00109  * of allocated memory to some sane value.
00110  *
00111  * AVAILABILITY
00112  *   http://www.ijs.si/software/snprintf/
00113  *
00114  * REVISION HISTORY
00115  * 1999-04      V0.9  Mark Martinec
00116  *              - initial version, some modifications after comparing printf
00117  *                man pages for Digital Unix 4.0, Solaris 2.6 and HPUX 10,
00118  *                and checking how Perl handles sprintf (differently!);
00119  * 1999-04-09   V1.0  Mark Martinec <mark.martinec@ijs.si>
00120  *              - added main test program, fixed remaining inconsistencies,
00121  *                added optional (long long int) support;
00122  * 1999-04-12   V1.1  Mark Martinec <mark.martinec@ijs.si>
00123  *              - support the 'p' conversion (pointer to void);
00124  *              - if a string precision is specified
00125  *                make sure the string beyond the specified precision
00126  *                will not be referenced (e.g. by strlen);
00127  * 1999-04-13   V1.2  Mark Martinec <mark.martinec@ijs.si>
00128  *              - support synonyms %D=%ld, %U=%lu, %O=%lo;
00129  *              - speed up the case of long format string with few conversions;
00130  * 1999-06-30   V1.3  Mark Martinec <mark.martinec@ijs.si>
00131  *              - fixed runaway loop (eventually crashing when str_l wraps
00132  *                beyond 2^31) while copying format string without
00133  *                conversion specifiers to a buffer that is too short
00134  *                (thanks to Edwin Young <edwiny@autonomy.com> for
00135  *                spotting the problem);
00136  *              - added macros PORTABLE_SNPRINTF_VERSION_(MAJOR|MINOR)
00137  *                to snprintf.h
00138  * 2000-02-14   V2.0 (never released) Mark Martinec <mark.martinec@ijs.si>
00139  *              - relaxed license terms: The Artistic License now applies.
00140  *                You may still apply the GNU GENERAL PUBLIC LICENSE
00141  *                as was distributed with previous versions, if you prefer;
00142  *              - changed REVISION HISTORY dates to use ISO 8601 date format;
00143  *              - added vsnprintf (patch also independently proposed by
00144  *                Caolan McNamara 2000-05-04, and Keith M Willenson 2000-06-01)
00145  * 2000-06-27   V2.1  Mark Martinec <mark.martinec@ijs.si>
00146  *              - removed POSIX check for str_m<1; value 0 for str_m is
00147  *                allowed by ISO C99 (and GNU C library 2.1) - (pointed out
00148  *                on 2000-05-04 by Caolan McNamara, caolan@ csn dot ul dot ie).
00149  *                Besides relaxed license this change in standards adherence
00150  *                is the main reason to bump up the major version number;
00151  *              - added nonstandard routines asnprintf, vasnprintf, asprintf,
00152  *                vasprintf that dynamically allocate storage for the
00153  *                resulting string; these routines are not compiled by default,
00154  *                see comments where NEED_V?ASN?PRINTF macros are defined;
00155  *              - autoconf contributed by Caolan McNamara
00156  * 2000-10-06   V2.2  Mark Martinec <mark.martinec@ijs.si>
00157  *              - BUG FIX: the %c conversion used a temporary variable
00158  *                that was no longer in scope when referenced,
00159  *                possibly causing incorrect resulting character;
00160  *              - BUG FIX: make precision and minimal field width unsigned
00161  *                to handle huge values (2^31 <= n < 2^32) correctly;
00162  *                also be more careful in the use of signed/unsigned/size_t
00163  *                internal variables - probably more careful than many
00164  *                vendor implementations, but there may still be a case
00165  *                where huge values of str_m, precision or minimal field
00166  *                could cause incorrect behaviour;
00167  *              - use separate variables for signed/unsigned arguments,
00168  *                and for short/int, long, and long long argument lengths
00169  *                to avoid possible incompatibilities on certain
00170  *                computer architectures. Also use separate variable
00171  *                arg_sign to hold sign of a numeric argument,
00172  *                to make code more transparent;
00173  *              - some fiddling with zero padding and "0x" to make it
00174  *                Linux compatible;
00175  *              - systematically use macros fast_memcpy and fast_memset
00176  *                instead of case-by-case hand optimization; determine some
00177  *                breakeven string lengths for different architectures;
00178  *              - terminology change: 'format' -> 'conversion specifier',
00179  *                'C9x' -> 'ISO/IEC 9899:1999 ("ISO C99")',
00180  *                'alternative form' -> 'alternate form',
00181  *                'data type modifier' -> 'length modifier';
00182  *              - several comments rephrased and new ones added;
00183  *              - make compiler not complain about 'credits' defined but
00184  *                not used;
00185  */
00186 
00187 
00188 /* Define HAVE_SNPRINTF if your system already has snprintf and vsnprintf.
00189  *
00190  * If HAVE_SNPRINTF is defined this module will not produce code for
00191  * snprintf and vsnprintf, unless PREFER_PORTABLE_SNPRINTF is defined as well,
00192  * causing this portable version of snprintf to be called portable_snprintf
00193  * (and portable_vsnprintf).
00194  */
00195 /* #define HAVE_SNPRINTF */
00196 
00197 /* Define PREFER_PORTABLE_SNPRINTF if your system does have snprintf and
00198  * vsnprintf but you would prefer to use the portable routine(s) instead.
00199  * In this case the portable routine is declared as portable_snprintf
00200  * (and portable_vsnprintf) and a macro 'snprintf' (and 'vsnprintf')
00201  * is defined to expand to 'portable_v?snprintf' - see file snprintf.h .
00202  * Defining this macro is only useful if HAVE_SNPRINTF is also defined,
00203  * but does does no harm if defined nevertheless.
00204  */
00205 /* #define PREFER_PORTABLE_SNPRINTF */
00206 
00207 /* Define SNPRINTF_LONGLONG_SUPPORT if you want to support
00208  * data type (long long int) and length modifier 'll' (e.g. %lld).
00209  * If undefined, 'll' is recognized but treated as a single 'l'.
00210  *
00211  * If the system's sprintf does not handle 'll'
00212  * the SNPRINTF_LONGLONG_SUPPORT must not be defined!
00213  *
00214  * This is off by default as (long long int) is a language extension.
00215  */
00216 /* #define SNPRINTF_LONGLONG_SUPPORT */
00217 
00218 /* Define NEED_SNPRINTF_ONLY if you only need snprintf, and not vsnprintf.
00219  * If NEED_SNPRINTF_ONLY is defined, the snprintf will be defined directly,
00220  * otherwise both snprintf and vsnprintf routines will be defined
00221  * and snprintf will be a simple wrapper around vsnprintf, at the expense
00222  * of an extra procedure call.
00223  */
00224 /* #define NEED_SNPRINTF_ONLY */
00225 
00226 /* Define NEED_V?ASN?PRINTF macros if you need library extension
00227  * routines asprintf, vasprintf, asnprintf, vasnprintf respectively,
00228  * and your system library does not provide them. They are all small
00229  * wrapper routines around portable_vsnprintf. Defining any of the four
00230  * NEED_V?ASN?PRINTF macros automatically turns off NEED_SNPRINTF_ONLY
00231  * and turns on PREFER_PORTABLE_SNPRINTF.
00232  *
00233  * Watch for name conflicts with the system library if these routines
00234  * are already present there.
00235  *
00236  * NOTE: vasprintf and vasnprintf routines need va_copy() from stdarg.h, as
00237  * specified by C99, to be able to traverse the same list of arguments twice.
00238  * I don't know of any other standard and portable way of achieving the same.
00239  * With some versions of gcc you may use __va_copy(). You might even get away
00240  * with "ap2 = ap", in this case you must not call va_end(ap2) !
00241  *   #define va_copy(ap2,ap) ap2 = ap
00242  */
00243 /* #define NEED_ASPRINTF   */
00244 /* #define NEED_ASNPRINTF  */
00245 /* #define NEED_VASPRINTF  */
00246 /* #define NEED_VASNPRINTF */
00247 
00248 
00249 /* Define the following macros if desired:
00250  *   SOLARIS_COMPATIBLE, SOLARIS_BUG_COMPATIBLE,
00251  *   HPUX_COMPATIBLE, HPUX_BUG_COMPATIBLE, LINUX_COMPATIBLE,
00252  *   DIGITAL_UNIX_COMPATIBLE, DIGITAL_UNIX_BUG_COMPATIBLE,
00253  *   PERL_COMPATIBLE, PERL_BUG_COMPATIBLE,
00254  *
00255  * - For portable applications it is best not to rely on peculiarities
00256  *   of a given implementation so it may be best not to define any
00257  *   of the macros that select compatibility and to avoid features
00258  *   that vary among the systems.
00259  *
00260  * - Selecting compatibility with more than one operating system
00261  *   is not strictly forbidden but is not recommended.
00262  *
00263  * - 'x'_BUG_COMPATIBLE implies 'x'_COMPATIBLE .
00264  *
00265  * - 'x'_COMPATIBLE refers to (and enables) a behaviour that is
00266  *   documented in a sprintf man page on a given operating system
00267  *   and actually adhered to by the system's sprintf (but not on
00268  *   most other operating systems). It may also refer to and enable
00269  *   a behaviour that is declared 'undefined' or 'implementation specific'
00270  *   in the man page but a given implementation behaves predictably
00271  *   in a certain way.
00272  *
00273  * - 'x'_BUG_COMPATIBLE refers to (and enables) a behaviour of system's sprintf
00274  *   that contradicts the sprintf man page on the same operating system.
00275  *
00276  * - I do not claim that the 'x'_COMPATIBLE and 'x'_BUG_COMPATIBLE
00277  *   conditionals take into account all idiosyncrasies of a particular
00278  *   implementation, there may be other incompatibilities.
00279  */
00280 
00281 
00282 
00283 /* ============================================= */
00284 /* NO USER SERVICABLE PARTS FOLLOWING THIS POINT */
00285 /* ============================================= */
00286 
00287 #define PORTABLE_SNPRINTF_VERSION_MAJOR 2
00288 #define PORTABLE_SNPRINTF_VERSION_MINOR 2
00289 
00290 #if defined(NEED_ASPRINTF) || defined(NEED_ASNPRINTF) || defined(NEED_VASPRINTF) || defined(NEED_VASNPRINTF)
00291 # if defined(NEED_SNPRINTF_ONLY)
00292 # undef NEED_SNPRINTF_ONLY
00293 # endif
00294 # if !defined(PREFER_PORTABLE_SNPRINTF)
00295 # define PREFER_PORTABLE_SNPRINTF
00296 # endif
00297 #endif
00298 
00299 #if defined(SOLARIS_BUG_COMPATIBLE) && !defined(SOLARIS_COMPATIBLE)
00300 #define SOLARIS_COMPATIBLE
00301 #endif
00302 
00303 #if defined(HPUX_BUG_COMPATIBLE) && !defined(HPUX_COMPATIBLE)
00304 #define HPUX_COMPATIBLE
00305 #endif
00306 
00307 #if defined(DIGITAL_UNIX_BUG_COMPATIBLE) && !defined(DIGITAL_UNIX_COMPATIBLE)
00308 #define DIGITAL_UNIX_COMPATIBLE
00309 #endif
00310 
00311 #if defined(PERL_BUG_COMPATIBLE) && !defined(PERL_COMPATIBLE)
00312 #define PERL_COMPATIBLE
00313 #endif
00314 
00315 #if defined(LINUX_BUG_COMPATIBLE) && !defined(LINUX_COMPATIBLE)
00316 #define LINUX_COMPATIBLE
00317 #endif
00318 
00319 #include <sys/types.h>
00320 #include <string.h>
00321 #include <stdlib.h>
00322 #include <stdio.h>
00323 #include <stdarg.h>
00324 #include <assert.h>
00325 #include <errno.h>
00326 
00327 #ifdef isdigit
00328 #undef isdigit
00329 #endif
00330 #define isdigit(c) ((c) >= '0' && (c) <= '9')
00331 
00332 /* For copying strings longer or equal to 'breakeven_point'
00333  * it is more efficient to call memcpy() than to do it inline.
00334  * The value depends mostly on the processor architecture,
00335  * but also on the compiler and its optimization capabilities.
00336  * The value is not critical, some small value greater than zero
00337  * will be just fine if you don't care to squeeze every drop
00338  * of performance out of the code.
00339  *
00340  * Small values favor memcpy, large values favor inline code.
00341  */
00342 #if defined(__alpha__) || defined(__alpha)
00343 #  define breakeven_point   2   /* AXP (DEC Alpha)     - gcc or cc or egcs */
00344 #endif
00345 #if defined(__i386__)  || defined(__i386)
00346 #  define breakeven_point  12   /* Intel Pentium/Linux - gcc 2.96 */
00347 #endif
00348 #if defined(__hppa)
00349 #  define breakeven_point  10   /* HP-PA               - gcc */
00350 #endif
00351 #if defined(__sparc__) || defined(__sparc)
00352 #  define breakeven_point  33   /* Sun Sparc 5         - gcc 2.8.1 */
00353 #endif
00354 
00355 /* some other values of possible interest: */
00356 /* #define breakeven_point  8 */  /* VAX 4000          - vaxc */
00357 /* #define breakeven_point 19 */  /* VAX 4000          - gcc 2.7.0 */
00358 
00359 #ifndef breakeven_point
00360 #  define breakeven_point   6   /* some reasonable one-size-fits-all value */
00361 #endif
00362 
00363 #define fast_memcpy(d,s,n) \
00364   { register size_t nn = (size_t)(n); \
00365     if (nn >= breakeven_point) memcpy((d), (s), nn); \
00366     else if (nn > 0) { /* proc call overhead is worth only for large strings*/\
00367       register char *dd; register const char *ss; \
00368       for (ss=(s), dd=(d); nn>0; nn--) *dd++ = *ss++; } }
00369 
00370 #define fast_memset(d,c,n) \
00371   { register size_t nn = (size_t)(n); \
00372     if (nn >= breakeven_point) memset((d), (int)(c), nn); \
00373     else if (nn > 0) { /* proc call overhead is worth only for large strings*/\
00374       register char *dd; register const int cc=(int)(c); \
00375       for (dd=(d); nn>0; nn--) *dd++ = cc; } }
00376 
00377 /* prototypes */
00378 
00379 #if defined(NEED_ASPRINTF)
00380 int asprintf   (char **ptr, const char *fmt, /*args*/ ...);
00381 #endif
00382 #if defined(NEED_VASPRINTF)
00383 int vasprintf  (char **ptr, const char *fmt, va_list ap);
00384 #endif
00385 #if defined(NEED_ASNPRINTF)
00386 int asnprintf  (char **ptr, size_t str_m, const char *fmt, /*args*/ ...);
00387 #endif
00388 #if defined(NEED_VASNPRINTF)
00389 int vasnprintf (char **ptr, size_t str_m, const char *fmt, va_list ap);
00390 #endif
00391 
00392 #if defined(HAVE_SNPRINTF)
00393 /* declare our portable snprintf  routine under name portable_snprintf  */
00394 /* declare our portable vsnprintf routine under name portable_vsnprintf */
00395 #else
00396 /* declare our portable routines under names snprintf and vsnprintf */
00397 #define portable_snprintf snprintf
00398 #if !defined(NEED_SNPRINTF_ONLY)
00399 #define portable_vsnprintf vsnprintf
00400 #endif
00401 #endif
00402 
00403 #if !defined(HAVE_SNPRINTF) || defined(PREFER_PORTABLE_SNPRINTF)
00404 int portable_snprintf(char *str, size_t str_m, const char *fmt, /*args*/ ...);
00405 #if !defined(NEED_SNPRINTF_ONLY)
00406 int portable_vsnprintf(char *str, size_t str_m, const char *fmt, va_list ap);
00407 #endif
00408 #endif
00409 
00410 /* declarations */
00411 
00412 static char credits[] = "\n\
00413 @(#)snprintf.c, v2.2: Mark Martinec, <mark.martinec@ijs.si>\n\
00414 @(#)snprintf.c, v2.2: Copyright 1999, Mark Martinec. Frontier Artistic License applies.\n\
00415 @(#)snprintf.c, v2.2: http://www.ijs.si/software/snprintf/\n";
00416 
00417 #if defined(NEED_ASPRINTF)
00418 int asprintf(char **ptr, const char *fmt, /*args*/ ...) {
00419   va_list ap;
00420   size_t str_m;
00421   int str_l;
00422 
00423   *ptr = NULL;
00424   va_start(ap, fmt);                            /* measure the required size */
00425   str_l = portable_vsnprintf(NULL, (size_t)0, fmt, ap);
00426   va_end(ap);
00427   assert(str_l >= 0);        /* possible integer overflow if str_m > INT_MAX */
00428   *ptr = (char *) malloc(str_m = (size_t)str_l + 1);
00429   if (*ptr == NULL) { errno = ENOMEM; str_l = -1; }
00430   else {
00431     int str_l2;
00432     va_start(ap, fmt);
00433     str_l2 = portable_vsnprintf(*ptr, str_m, fmt, ap);
00434     va_end(ap);
00435     assert(str_l2 == str_l);
00436   }
00437   return str_l;
00438 }
00439 #endif
00440 
00441 #if defined(NEED_VASPRINTF)
00442 int vasprintf(char **ptr, const char *fmt, va_list ap) {
00443   size_t str_m;
00444   int str_l;
00445 
00446   *ptr = NULL;
00447   { va_list ap2;
00448     va_copy(ap2, ap);  /* don't consume the original ap, we'll need it again */
00449     str_l = portable_vsnprintf(NULL, (size_t)0, fmt, ap2);/*get required size*/
00450     va_end(ap2);
00451   }
00452   assert(str_l >= 0);        /* possible integer overflow if str_m > INT_MAX */
00453   *ptr = (char *) malloc(str_m = (size_t)str_l + 1);
00454   if (*ptr == NULL) { errno = ENOMEM; str_l = -1; }
00455   else {
00456     int str_l2 = portable_vsnprintf(*ptr, str_m, fmt, ap);
00457     assert(str_l2 == str_l);
00458   }
00459   return str_l;
00460 }
00461 #endif
00462 
00463 #if defined(NEED_ASNPRINTF)
00464 int asnprintf (char **ptr, size_t str_m, const char *fmt, /*args*/ ...) {
00465   va_list ap;
00466   int str_l;
00467 
00468   *ptr = NULL;
00469   va_start(ap, fmt);                            /* measure the required size */
00470   str_l = portable_vsnprintf(NULL, (size_t)0, fmt, ap);
00471   va_end(ap);
00472   assert(str_l >= 0);        /* possible integer overflow if str_m > INT_MAX */
00473   if ((size_t)str_l + 1 < str_m) str_m = (size_t)str_l + 1;      /* truncate */
00474   /* if str_m is 0, no buffer is allocated, just set *ptr to NULL */
00475   if (str_m == 0) {  /* not interested in resulting string, just return size */
00476   } else {
00477     *ptr = (char *) malloc(str_m);
00478     if (*ptr == NULL) { errno = ENOMEM; str_l = -1; }
00479     else {
00480       int str_l2;
00481       va_start(ap, fmt);
00482       str_l2 = portable_vsnprintf(*ptr, str_m, fmt, ap);
00483       va_end(ap);
00484       assert(str_l2 == str_l);
00485     }
00486   }
00487   return str_l;
00488 }
00489 #endif
00490 
00491 #if defined(NEED_VASNPRINTF)
00492 int vasnprintf (char **ptr, size_t str_m, const char *fmt, va_list ap) {
00493   int str_l;
00494 
00495   *ptr = NULL;
00496   { va_list ap2;
00497     va_copy(ap2, ap);  /* don't consume the original ap, we'll need it again */
00498     str_l = portable_vsnprintf(NULL, (size_t)0, fmt, ap2);/*get required size*/
00499     va_end(ap2);
00500   }
00501   assert(str_l >= 0);        /* possible integer overflow if str_m > INT_MAX */
00502   if ((size_t)str_l + 1 < str_m) str_m = (size_t)str_l + 1;      /* truncate */
00503   /* if str_m is 0, no buffer is allocated, just set *ptr to NULL */
00504   if (str_m == 0) {  /* not interested in resulting string, just return size */
00505   } else {
00506     *ptr = (char *) malloc(str_m);
00507     if (*ptr == NULL) { errno = ENOMEM; str_l = -1; }
00508     else {
00509       int str_l2 = portable_vsnprintf(*ptr, str_m, fmt, ap);
00510       assert(str_l2 == str_l);
00511     }
00512   }
00513   return str_l;
00514 }
00515 #endif
00516 
00517 /*
00518  * If the system does have snprintf and the portable routine is not
00519  * specifically required, this module produces no code for snprintf/vsnprintf.
00520  */
00521 #if !defined(HAVE_SNPRINTF) || defined(PREFER_PORTABLE_SNPRINTF)
00522 
00523 #if !defined(NEED_SNPRINTF_ONLY)
00524 int portable_snprintf(char *str, size_t str_m, const char *fmt, /*args*/ ...) {
00525   va_list ap;
00526   int str_l;
00527 
00528   va_start(ap, fmt);
00529   str_l = portable_vsnprintf(str, str_m, fmt, ap);
00530   va_end(ap);
00531   return str_l;
00532 }
00533 #endif
00534 
00535 #if defined(NEED_SNPRINTF_ONLY)
00536 int portable_snprintf(char *str, size_t str_m, const char *fmt, /*args*/ ...) {
00537 #else
00538 int portable_vsnprintf(char *str, size_t str_m, const char *fmt, va_list ap) {
00539 #endif
00540 
00541 #if defined(NEED_SNPRINTF_ONLY)
00542   va_list ap;
00543 #endif
00544   size_t str_l = 0;
00545   const char *p = fmt;
00546 
00547 /* In contrast with POSIX, the ISO C99 now says
00548  * that str can be NULL and str_m can be 0.
00549  * This is more useful than the old:  if (str_m < 1) return -1; */
00550 
00551 #if defined(NEED_SNPRINTF_ONLY)
00552   va_start(ap, fmt);
00553 #endif
00554   if (!p) p = "";
00555   while (*p) {
00556     if (*p != '%') {
00557    /* if (str_l < str_m) str[str_l++] = *p++;    -- this would be sufficient */
00558    /* but the following code achieves better performance for cases
00559     * where format string is long and contains few conversions */
00560       const char *q = strchr(p+1,'%');
00561       size_t n = !q ? strlen(p) : (q-p);
00562       if (str_l < str_m) {
00563         size_t avail = str_m-str_l;
00564         fast_memcpy(str+str_l, p, (n>avail?avail:n));
00565       }
00566       p += n; str_l += n;
00567     } else {
00568       const char *starting_p;
00569       size_t min_field_width = 0, precision = 0;
00570       int zero_padding = 0, precision_specified = 0, justify_left = 0;
00571       int alternate_form = 0, force_sign = 0;
00572       int space_for_positive = 1; /* If both the ' ' and '+' flags appear,
00573                                      the ' ' flag should be ignored. */
00574       char length_modifier = '\0';            /* allowed values: \0, h, l, L */
00575       char tmp[32];/* temporary buffer for simple numeric->string conversion */
00576 
00577       const char *str_arg;      /* string address in case of string argument */
00578       size_t str_arg_l;         /* natural field width of arg without padding
00579                                    and sign */
00580       unsigned char uchar_arg;
00581         /* unsigned char argument value - only defined for c conversion.
00582            N.B. standard explicitly states the char argument for
00583            the c conversion is unsigned */
00584 
00585       size_t number_of_zeros_to_pad = 0;
00586         /* number of zeros to be inserted for numeric conversions
00587            as required by the precision or minimal field width */
00588 
00589       size_t zero_padding_insertion_ind = 0;
00590         /* index into tmp where zero padding is to be inserted */
00591 
00592       char fmt_spec = '\0';
00593         /* current conversion specifier character */
00594 
00595       str_arg = credits;/* just to make compiler happy (defined but not used)*/
00596       str_arg = NULL;
00597       starting_p = p; p++;  /* skip '%' */
00598    /* parse flags */
00599       while (*p == '0' || *p == '-' || *p == '+' ||
00600              *p == ' ' || *p == '#' || *p == '\'') {
00601         switch (*p) {
00602         case '0': zero_padding = 1; break;
00603         case '-': justify_left = 1; break;
00604         case '+': force_sign = 1; space_for_positive = 0; break;
00605         case ' ': force_sign = 1;
00606      /* If both the ' ' and '+' flags appear, the ' ' flag should be ignored */
00607 #ifdef PERL_COMPATIBLE
00608      /* ... but in Perl the last of ' ' and '+' applies */
00609                   space_for_positive = 1;
00610 #endif
00611                   break;
00612         case '#': alternate_form = 1; break;
00613         case '\'': break;
00614         }
00615         p++;
00616       }
00617    /* If the '0' and '-' flags both appear, the '0' flag should be ignored. */
00618 
00619    /* parse field width */
00620       if (*p == '*') {
00621         int j;
00622         p++; j = va_arg(ap, int);
00623         if (j >= 0) min_field_width = j;
00624         else { min_field_width = -j; justify_left = 1; }
00625       } else if (isdigit((int)(*p))) {
00626         /* size_t could be wider than unsigned int;
00627            make sure we treat argument like common implementations do */
00628         unsigned int uj = *p++ - '0';
00629         while (isdigit((int)(*p))) uj = 10*uj + (unsigned int)(*p++ - '0');
00630         min_field_width = uj;
00631       }
00632    /* parse precision */
00633       if (*p == '.') {
00634         p++; precision_specified = 1;
00635         if (*p == '*') {
00636           int j = va_arg(ap, int);
00637           p++;
00638           if (j >= 0) precision = j;
00639           else {
00640             precision_specified = 0; precision = 0;
00641          /* NOTE:
00642           *   Solaris 2.6 man page claims that in this case the precision
00643           *   should be set to 0.  Digital Unix 4.0, HPUX 10 and BSD man page
00644           *   claim that this case should be treated as unspecified precision,
00645           *   which is what we do here.
00646           */
00647           }
00648         } else if (isdigit((int)(*p))) {
00649           /* size_t could be wider than unsigned int;
00650              make sure we treat argument like common implementations do */
00651           unsigned int uj = *p++ - '0';
00652           while (isdigit((int)(*p))) uj = 10*uj + (unsigned int)(*p++ - '0');
00653           precision = uj;
00654         }
00655       }
00656    /* parse 'h', 'l' and 'll' length modifiers */
00657       if (*p == 'h' || *p == 'l') {
00658         length_modifier = *p; p++;
00659         if (length_modifier == 'l' && *p == 'l') {   /* double l = long long */
00660 #ifdef SNPRINTF_LONGLONG_SUPPORT
00661           length_modifier = '2';                  /* double l encoded as '2' */
00662 #else
00663           length_modifier = 'l';                 /* treat it as a single 'l' */
00664 #endif
00665           p++;
00666         }
00667       }
00668       fmt_spec = *p;
00669    /* common synonyms: */
00670       switch (fmt_spec) {
00671       case 'i': fmt_spec = 'd'; break;
00672       case 'D': fmt_spec = 'd'; length_modifier = 'l'; break;
00673       case 'U': fmt_spec = 'u'; length_modifier = 'l'; break;
00674       case 'O': fmt_spec = 'o'; length_modifier = 'l'; break;
00675       default: break;
00676       }
00677    /* get parameter value, do initial processing */
00678       switch (fmt_spec) {
00679       case '%': /* % behaves similar to 's' regarding flags and field widths */
00680       case 'c': /* c behaves similar to 's' regarding flags and field widths */
00681       case 's':
00682         length_modifier = '\0';          /* wint_t and wchar_t not supported */
00683      /* the result of zero padding flag with non-numeric conversion specifier*/
00684      /* is undefined. Solaris and HPUX 10 does zero padding in this case,    */
00685      /* Digital Unix and Linux does not. */
00686 #if !defined(SOLARIS_COMPATIBLE) && !defined(HPUX_COMPATIBLE)
00687         zero_padding = 0;    /* turn zero padding off for string conversions */
00688 #endif
00689         str_arg_l = 1;
00690         switch (fmt_spec) {
00691         case '%':
00692           str_arg = p; break;
00693         case 'c': {
00694           int j = va_arg(ap, int);
00695           uchar_arg = (unsigned char) j;   /* standard demands unsigned char */
00696           str_arg = (const char *) &uchar_arg;
00697           break;
00698         }
00699         case 's':
00700           str_arg = va_arg(ap, const char *);
00701           if (!str_arg) str_arg_l = 0;
00702        /* make sure not to address string beyond the specified precision !!! */
00703           else if (!precision_specified) str_arg_l = strlen(str_arg);
00704        /* truncate string if necessary as requested by precision */
00705           else if (precision == 0) str_arg_l = 0;
00706           else {
00707        /* memchr on HP does not like n > 2^31  !!! */
00708             const char *q = (const char*)(memchr(str_arg, '\0',
00709                              precision <= 0x7fffffff ? precision : 0x7fffffff));
00710             str_arg_l = !q ? precision : (q-str_arg);
00711           }
00712           break;
00713         default: break;
00714         }
00715         break;
00716       case 'd': case 'u': case 'o': case 'x': case 'X': case 'p': {
00717         /* NOTE: the u, o, x, X and p conversion specifiers imply
00718                  the value is unsigned;  d implies a signed value */
00719 
00720         int arg_sign = 0;
00721           /* 0 if numeric argument is zero (or if pointer is NULL for 'p'),
00722             +1 if greater than zero (or nonzero for unsigned arguments),
00723             -1 if negative (unsigned argument is never negative) */
00724 
00725         int int_arg = 0;  unsigned int uint_arg = 0;
00726           /* only defined for length modifier h, or for no length modifiers */
00727 
00728         long int long_arg = 0;  unsigned long int ulong_arg = 0;
00729           /* only defined for length modifier l */
00730 
00731         void *ptr_arg = NULL;
00732           /* pointer argument value -only defined for p conversion */
00733 
00734 #ifdef SNPRINTF_LONGLONG_SUPPORT
00735         long long int long_long_arg = 0;
00736         unsigned long long int ulong_long_arg = 0;
00737           /* only defined for length modifier ll */
00738 #endif
00739         if (fmt_spec == 'p') {
00740         /* HPUX 10: An l, h, ll or L before any other conversion character
00741          *   (other than d, i, u, o, x, or X) is ignored.
00742          * Digital Unix:
00743          *   not specified, but seems to behave as HPUX does.
00744          * Solaris: If an h, l, or L appears before any other conversion
00745          *   specifier (other than d, i, u, o, x, or X), the behavior
00746          *   is undefined. (Actually %hp converts only 16-bits of address
00747          *   and %llp treats address as 64-bit data which is incompatible
00748          *   with (void *) argument on a 32-bit system).
00749          */
00750 #ifdef SOLARIS_COMPATIBLE
00751 #  ifdef SOLARIS_BUG_COMPATIBLE
00752           /* keep length modifiers even if it represents 'll' */
00753 #  else
00754           if (length_modifier == '2') length_modifier = '\0';
00755 #  endif
00756 #else
00757           length_modifier = '\0';
00758 #endif
00759           ptr_arg = va_arg(ap, void *);
00760           if (ptr_arg != NULL) arg_sign = 1;
00761         } else if (fmt_spec == 'd') {  /* signed */
00762           switch (length_modifier) {
00763           case '\0':
00764           case 'h':
00765          /* It is non-portable to specify a second argument of char or short
00766           * to va_arg, because arguments seen by the called function
00767           * are not char or short.  C converts char and short arguments
00768           * to int before passing them to a function.
00769           */
00770             int_arg = va_arg(ap, int);
00771             if      (int_arg > 0) arg_sign =  1;
00772             else if (int_arg < 0) arg_sign = -1;
00773             break;
00774           case 'l':
00775             long_arg = va_arg(ap, long int);
00776             if      (long_arg > 0) arg_sign =  1;
00777             else if (long_arg < 0) arg_sign = -1;
00778             break;
00779 #ifdef SNPRINTF_LONGLONG_SUPPORT
00780           case '2':
00781             long_long_arg = va_arg(ap, long long int);
00782             if      (long_long_arg > 0) arg_sign =  1;
00783             else if (long_long_arg < 0) arg_sign = -1;
00784             break;
00785 #endif
00786           }
00787         } else {  /* unsigned */
00788           switch (length_modifier) {
00789           case '\0':
00790           case 'h':
00791             uint_arg = va_arg(ap, unsigned int);
00792             if (uint_arg) arg_sign = 1;
00793             break;
00794           case 'l':
00795             ulong_arg = va_arg(ap, unsigned long int);
00796             if (ulong_arg) arg_sign = 1;
00797             break;
00798 #ifdef SNPRINTF_LONGLONG_SUPPORT
00799           case '2':
00800             ulong_long_arg = va_arg(ap, unsigned long long int);
00801             if (ulong_long_arg) arg_sign = 1;
00802             break;
00803 #endif
00804           }
00805         }
00806         str_arg = tmp; str_arg_l = 0;
00807      /* NOTE:
00808       *   For d, i, u, o, x, and X conversions, if precision is specified,
00809       *   the '0' flag should be ignored. This is so with Solaris 2.6,
00810       *   Digital UNIX 4.0, HPUX 10, Linux, FreeBSD, NetBSD; but not with Perl.
00811       */
00812 #ifndef PERL_COMPATIBLE
00813         if (precision_specified) zero_padding = 0;
00814 #endif
00815         if (fmt_spec == 'd') {
00816           if (force_sign && arg_sign >= 0)
00817             tmp[str_arg_l++] = space_for_positive ? ' ' : '+';
00818          /* leave negative numbers for sprintf to handle,
00819             to avoid handling tricky cases like (short int)(-32768) */
00820 #ifdef LINUX_COMPATIBLE
00821         } else if (fmt_spec == 'p' && force_sign && arg_sign > 0) {
00822           tmp[str_arg_l++] = space_for_positive ? ' ' : '+';
00823 #endif
00824         } else if (alternate_form) {
00825           if (arg_sign != 0 && (fmt_spec == 'x' || fmt_spec == 'X') )
00826             { tmp[str_arg_l++] = '0'; tmp[str_arg_l++] = fmt_spec; }
00827          /* alternate form should have no effect for p conversion, but ... */
00828 #ifdef HPUX_COMPATIBLE
00829           else if (fmt_spec == 'p'
00830          /* HPUX 10: for an alternate form of p conversion,
00831           *          a nonzero result is prefixed by 0x. */
00832 #ifndef HPUX_BUG_COMPATIBLE
00833          /* Actually it uses 0x prefix even for a zero value. */
00834                    && arg_sign != 0
00835 #endif
00836                   ) { tmp[str_arg_l++] = '0'; tmp[str_arg_l++] = 'x'; }
00837 #endif
00838         }
00839         zero_padding_insertion_ind = str_arg_l;
00840         if (!precision_specified) precision = 1;   /* default precision is 1 */
00841         if (precision == 0 && arg_sign == 0
00842 #if defined(HPUX_BUG_COMPATIBLE) || defined(LINUX_COMPATIBLE)
00843             && fmt_spec != 'p'
00844          /* HPUX 10 man page claims: With conversion character p the result of
00845           * converting a zero value with a precision of zero is a null string.
00846           * Actually HP returns all zeroes, and Linux returns "(nil)". */
00847 #endif
00848         ) {
00849          /* converted to null string */
00850          /* When zero value is formatted with an explicit precision 0,
00851             the resulting formatted string is empty (d, i, u, o, x, X, p).   */
00852         } else {
00853           char f[5]; int f_l = 0;
00854           f[f_l++] = '%';    /* construct a simple format string for sprintf */
00855           if (!length_modifier) { }
00856           else if (length_modifier=='2') { f[f_l++] = 'l'; f[f_l++] = 'l'; }
00857           else f[f_l++] = length_modifier;
00858           f[f_l++] = fmt_spec; f[f_l++] = '\0';
00859           if (fmt_spec == 'p') str_arg_l += sprintf(tmp+str_arg_l, f, ptr_arg);
00860           else if (fmt_spec == 'd') {  /* signed */
00861             switch (length_modifier) {
00862             case '\0':
00863             case 'h': str_arg_l+=sprintf(tmp+str_arg_l, f, int_arg);  break;
00864             case 'l': str_arg_l+=sprintf(tmp+str_arg_l, f, long_arg); break;
00865 #ifdef SNPRINTF_LONGLONG_SUPPORT
00866             case '2': str_arg_l+=sprintf(tmp+str_arg_l,f,long_long_arg); break;
00867 #endif
00868             }
00869           } else {  /* unsigned */
00870             switch (length_modifier) {
00871             case '\0':
00872             case 'h': str_arg_l+=sprintf(tmp+str_arg_l, f, uint_arg);  break;
00873             case 'l': str_arg_l+=sprintf(tmp+str_arg_l, f, ulong_arg); break;
00874 #ifdef SNPRINTF_LONGLONG_SUPPORT
00875             case '2': str_arg_l+=sprintf(tmp+str_arg_l,f,ulong_long_arg);break;
00876 #endif
00877             }
00878           }
00879          /* include the optional minus sign and possible "0x"
00880             in the region before the zero padding insertion point */
00881           if (zero_padding_insertion_ind < str_arg_l &&
00882               tmp[zero_padding_insertion_ind] == '-') {
00883             zero_padding_insertion_ind++;
00884           }
00885           if (zero_padding_insertion_ind+1 < str_arg_l &&
00886               tmp[zero_padding_insertion_ind]   == '0' &&
00887              (tmp[zero_padding_insertion_ind+1] == 'x' ||
00888               tmp[zero_padding_insertion_ind+1] == 'X') ) {
00889             zero_padding_insertion_ind += 2;
00890           }
00891         }
00892         { size_t num_of_digits = str_arg_l - zero_padding_insertion_ind;
00893           if (alternate_form && fmt_spec == 'o'
00894 #ifdef HPUX_COMPATIBLE                                  /* ("%#.o",0) -> ""  */
00895               && (str_arg_l > 0)
00896 #endif
00897 #ifdef DIGITAL_UNIX_BUG_COMPATIBLE                      /* ("%#o",0) -> "00" */
00898 #else
00899               /* unless zero is already the first character */
00900               && !(zero_padding_insertion_ind < str_arg_l
00901                    && tmp[zero_padding_insertion_ind] == '0')
00902 #endif
00903           ) {        /* assure leading zero for alternate-form octal numbers */
00904             if (!precision_specified || precision < num_of_digits+1) {
00905              /* precision is increased to force the first character to be zero,
00906                 except if a zero value is formatted with an explicit precision
00907                 of zero */
00908               precision = num_of_digits+1; precision_specified = 1;
00909             }
00910           }
00911        /* zero padding to specified precision? */
00912           if (num_of_digits < precision) 
00913             number_of_zeros_to_pad = precision - num_of_digits;
00914         }
00915      /* zero padding to specified minimal field width? */
00916         if (!justify_left && zero_padding) {
00917           size_t n = min_field_width - (str_arg_l+number_of_zeros_to_pad);
00918           if (n > 0) number_of_zeros_to_pad += n;
00919         }
00920         break;
00921       }
00922       default: /* unrecognized conversion specifier, keep format string as-is*/
00923         zero_padding = 0;  /* turn zero padding off for non-numeric convers. */
00924 #ifndef DIGITAL_UNIX_COMPATIBLE
00925         justify_left = 1; min_field_width = 0;                /* reset flags */
00926 #endif
00927 #if defined(PERL_COMPATIBLE) || defined(LINUX_COMPATIBLE)
00928      /* keep the entire format string unchanged */
00929         str_arg = starting_p; str_arg_l = p - starting_p;
00930      /* well, not exactly so for Linux, which does something inbetween,
00931       * and I don't feel an urge to imitate it: "%+++++hy" -> "%+y"  */
00932 #else
00933      /* discard the unrecognized conversion, just keep *
00934       * the unrecognized conversion character          */
00935         str_arg = p; str_arg_l = 0;
00936 #endif
00937         if (*p) str_arg_l++;  /* include invalid conversion specifier unchanged
00938                                  if not at end-of-string */
00939         break;
00940       }
00941       if (*p) p++;      /* step over the just processed conversion specifier */
00942    /* insert padding to the left as requested by min_field_width;
00943       this does not include the zero padding in case of numerical conversions*/
00944       if (!justify_left) {                /* left padding with blank or zero */
00945         size_t n = min_field_width - (str_arg_l+number_of_zeros_to_pad);
00946         if (n > 0) {
00947           if (str_l < str_m) {
00948             size_t avail = str_m-str_l;
00949             fast_memset(str+str_l, (zero_padding?'0':' '), (n>avail?avail:n));
00950           }
00951           str_l += n;
00952         }
00953       }
00954    /* zero padding as requested by the precision or by the minimal field width
00955     * for numeric conversions required? */
00956       if (number_of_zeros_to_pad <= 0) {
00957      /* will not copy first part of numeric right now, *
00958       * force it to be copied later in its entirety    */
00959         zero_padding_insertion_ind = 0;
00960       } else {
00961      /* insert first part of numerics (sign or '0x') before zero padding */
00962         size_t n = zero_padding_insertion_ind;
00963         if (n > 0) {
00964           if (str_l < str_m) {
00965             size_t avail = str_m-str_l;
00966             fast_memcpy(str+str_l, str_arg, (n>avail?avail:n));
00967           }
00968           str_l += n;
00969         }
00970      /* insert zero padding as requested by the precision or min field width */
00971         n = number_of_zeros_to_pad;
00972         if (n > 0) {
00973           if (str_l < str_m) {
00974             size_t avail = str_m-str_l;
00975             fast_memset(str+str_l, '0', (n>avail?avail:n));
00976           }
00977           str_l += n;
00978         }
00979       }
00980    /* insert formatted string
00981     * (or as-is conversion specifier for unknown conversions) */
00982       { size_t n = str_arg_l - zero_padding_insertion_ind;
00983         if (n > 0) {
00984           if (str_l < str_m) {
00985             size_t avail = str_m-str_l;
00986             fast_memcpy(str+str_l, str_arg+zero_padding_insertion_ind,
00987                         (n>avail?avail:n));
00988           }
00989           str_l += n;
00990         }
00991       }
00992    /* insert right padding */
00993       if (justify_left) {          /* right blank padding to the field width */
00994         size_t n = min_field_width - (str_arg_l+number_of_zeros_to_pad);
00995         if (n > 0) {
00996           if (str_l < str_m) {
00997             size_t avail = str_m-str_l;
00998             fast_memset(str+str_l, ' ', (n>avail?avail:n));
00999           }
01000           str_l += n;
01001         }
01002       }
01003     }
01004   }
01005 #if defined(NEED_SNPRINTF_ONLY)
01006   va_end(ap);
01007 #endif
01008   if (str_m > 0) { /* make sure the string is null-terminated
01009                       even at the expense of overwriting the last character
01010                       (shouldn't happen, but just in case) */
01011     str[str_l <= str_m-1 ? str_l : str_m-1] = '\0';
01012   }
01013   /* Return the number of characters formatted (excluding trailing null
01014    * character), that is, the number of characters that would have been
01015    * written to the buffer if it were large enough.
01016    *
01017    * The value of str_l should be returned, but str_l is of unsigned type
01018    * size_t, and snprintf is int, possibly leading to an undetected
01019    * integer overflow, resulting in a negative return value, which is illegal.
01020    * Both XSH5 and ISO C99 (at least the draft) are silent on this issue.
01021    * Should errno be set to EOVERFLOW and EOF returned in this case???
01022    */
01023   return (int) str_l;
01024 }
01025 #endif


log4cpp
Author(s): Stephen Roderick
autogenerated on Mon Oct 6 2014 03:13:14