5 * snprintf.c - a portable implementation of snprintf
8 * Mark Martinec <mark.martinec@ijs.si>, April 1999.
10 * Copyright 1999, Mark Martinec. All rights reserved.
12 * TERMS AND CONDITIONS
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the "Frontier Artistic License" which comes
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty
19 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20 * See the Frontier Artistic License for more details.
22 * You should have received a copy of the Frontier Artistic License
23 * with this Kit in the file named LICENSE.txt .
24 * If not, I'll be glad to provide one.
27 * - careful adherence to specs regarding flags, field width and precision;
28 * - good performance for large string handling (large format, large
29 * argument or large paddings). Performance is similar to system's sprintf
30 * and in several cases significantly better (make sure you compile with
31 * optimizations turned on, tell the compiler the code is strict ANSI
32 * if necessary to give it more freedom for optimizations);
33 * - return value semantics per ISO/IEC 9899:1999 ("ISO C99");
34 * - written in standard ISO/ANSI C - requires an ANSI C compiler.
36 * SUPPORTED CONVERSION SPECIFIERS AND DATA TYPES
38 * This snprintf only supports the following conversion specifiers:
39 * s, c, d, u, o, x, X, p (and synonyms: i, D, U, O - see below)
40 * with flags: '-', '+', ' ', '0' and '#'.
41 * An asterisk is supported for field width as well as precision.
43 * Length modifiers 'h' (short int), 'l' (long int),
44 * and 'll' (long long int) are supported.
46 * If macro SNPRINTF_LONGLONG_SUPPORT is not defined (default) the
47 * length modifier 'll' is recognized but treated the same as 'l',
48 * which may cause argument value truncation! Defining
49 * SNPRINTF_LONGLONG_SUPPORT requires that your system's sprintf also
50 * handles length modifier 'll'. long long int is a language extension
51 * which may not be portable.
53 * Conversion of numeric data (conversion specifiers d, u, o, x, X, p)
54 * with length modifiers (none or h, l, ll) is left to the system routine
55 * sprintf, but all handling of flags, field width and precision as well as
56 * c and s conversions is done very carefully by this portable routine.
57 * If a string precision (truncation) is specified (e.g. %.8s) it is
58 * guaranteed the string beyond the specified precision will not be referenced.
60 * Length modifiers h, l and ll are ignored for c and s conversions (data
61 * types wint_t and wchar_t are not supported).
63 * The following common synonyms for conversion characters are supported:
64 * - i is a synonym for d
65 * - D is a synonym for ld, explicit length modifiers are ignored
66 * - U is a synonym for lu, explicit length modifiers are ignored
67 * - O is a synonym for lo, explicit length modifiers are ignored
68 * The D, O and U conversion characters are nonstandard, they are supported
69 * for backward compatibility only, and should not be used for new code.
71 * The following is specifically NOT supported:
72 * - flag ' (thousands' grouping character) is recognized but ignored
73 * - numeric conversion specifiers: f, e, E, g, G and synonym F,
74 * as well as the new a and A conversion specifiers
75 * - length modifier 'L' (long double) and 'q' (quad - use 'll' instead)
76 * - wide character/string conversions: lc, ls, and nonstandard
78 * - writeback of converted string length: conversion character n
79 * - the n$ specification for direct reference to n-th argument
82 * It is permitted for str_m to be zero, and it is permitted to specify NULL
83 * pointer for resulting string argument if str_m is zero (as per ISO C99).
85 * The return value is the number of characters which would be generated
86 * for the given input, excluding the trailing null. If this value
87 * is greater or equal to str_m, not all characters from the result
88 * have been stored in str, output bytes beyond the (str_m-1) -th character
89 * are discarded. If str_m is greater than zero it is guaranteed
90 * the resulting string will be null-terminated.
92 * NOTE that this matches the ISO C99, OpenBSD, and GNU C library 2.1,
93 * but is different from some older and vendor implementations,
94 * and is also different from XPG, XSH5, SUSv2 specifications.
95 * For historical discussion on changes in the semantics and standards
96 * of snprintf see printf(3) man page in the Linux programmers manual.
98 * Routines asprintf and vasprintf return a pointer (in the ptr argument)
99 * to a buffer sufficiently large to hold the resulting string. This pointer
100 * should be passed to free(3) to release the allocated storage when it is
101 * no longer needed. If sufficient space cannot be allocated, these functions
102 * will return -1 and set ptr to be a NULL pointer. These two routines are a
103 * GNU C library extensions (glibc).
105 * Routines asnprintf and vasnprintf are similar to asprintf and vasprintf,
106 * yet, like snprintf and vsnprintf counterparts, will write at most str_m-1
107 * characters into the allocated output string, the last character in the
108 * allocated buffer then gets the terminating null. If the formatted string
109 * length (the return value) is greater than or equal to the str_m argument,
110 * the resulting string was truncated and some of the formatted characters
111 * were discarded. These routines present a handy way to limit the amount
112 * of allocated memory to some sane value.
115 * http://www.ijs.si/software/snprintf/
118 * 1999-04 V0.9 Mark Martinec
119 * - initial version, some modifications after comparing printf
120 * man pages for Digital Unix 4.0, Solaris 2.6 and HPUX 10,
121 * and checking how Perl handles sprintf (differently!);
122 * 1999-04-09 V1.0 Mark Martinec <mark.martinec@ijs.si>
123 * - added main test program, fixed remaining inconsistencies,
124 * added optional (long long int) support;
125 * 1999-04-12 V1.1 Mark Martinec <mark.martinec@ijs.si>
126 * - support the 'p' conversion (pointer to void);
127 * - if a string precision is specified
128 * make sure the string beyond the specified precision
129 * will not be referenced (e.g. by strlen);
130 * 1999-04-13 V1.2 Mark Martinec <mark.martinec@ijs.si>
131 * - support synonyms %D=%ld, %U=%lu, %O=%lo;
132 * - speed up the case of long format string with few conversions;
133 * 1999-06-30 V1.3 Mark Martinec <mark.martinec@ijs.si>
134 * - fixed runaway loop (eventually crashing when str_l wraps
135 * beyond 2^31) while copying format string without
136 * conversion specifiers to a buffer that is too short
137 * (thanks to Edwin Young <edwiny@autonomy.com> for
138 * spotting the problem);
139 * - added macros PORTABLE_SNPRINTF_VERSION_(MAJOR|MINOR)
141 * 2000-02-14 V2.0 (never released) Mark Martinec <mark.martinec@ijs.si>
142 * - relaxed license terms: The Artistic License now applies.
143 * You may still apply the GNU GENERAL PUBLIC LICENSE
144 * as was distributed with previous versions, if you prefer;
145 * - changed REVISION HISTORY dates to use ISO 8601 date format;
146 * - added vsnprintf (patch also independently proposed by
147 * Caolan McNamara 2000-05-04, and Keith M Willenson 2000-06-01)
148 * 2000-06-27 V2.1 Mark Martinec <mark.martinec@ijs.si>
149 * - removed POSIX check for str_m<1; value 0 for str_m is
150 * allowed by ISO C99 (and GNU C library 2.1) - (pointed out
151 * on 2000-05-04 by Caolan McNamara, caolan@ csn dot ul dot ie).
152 * Besides relaxed license this change in standards adherence
153 * is the main reason to bump up the major version number;
154 * - added nonstandard routines asnprintf, vasnprintf, asprintf,
155 * vasprintf that dynamically allocate storage for the
156 * resulting string; these routines are not compiled by default,
157 * see comments where NEED_V?ASN?PRINTF macros are defined;
158 * - autoconf contributed by Caolan McNamara
159 * 2000-10-06 V2.2 Mark Martinec <mark.martinec@ijs.si>
160 * - BUG FIX: the %c conversion used a temporary variable
161 * that was no longer in scope when referenced,
162 * possibly causing incorrect resulting character;
163 * - BUG FIX: make precision and minimal field width unsigned
164 * to handle huge values (2^31 <= n < 2^32) correctly;
165 * also be more careful in the use of signed/unsigned/size_t
166 * internal variables - probably more careful than many
167 * vendor implementations, but there may still be a case
168 * where huge values of str_m, precision or minimal field
169 * could cause incorrect behaviour;
170 * - use separate variables for signed/unsigned arguments,
171 * and for short/int, long, and long long argument lengths
172 * to avoid possible incompatibilities on certain
173 * computer architectures. Also use separate variable
174 * arg_sign to hold sign of a numeric argument,
175 * to make code more transparent;
176 * - some fiddling with zero padding and "0x" to make it
178 * - systematically use macros fast_memcpy and fast_memset
179 * instead of case-by-case hand optimization; determine some
180 * breakeven string lengths for different architectures;
181 * - terminology change: 'format' -> 'conversion specifier',
182 * 'C9x' -> 'ISO/IEC 9899:1999 ("ISO C99")',
183 * 'alternative form' -> 'alternate form',
184 * 'data type modifier' -> 'length modifier';
185 * - several comments rephrased and new ones added;
186 * - make compiler not complain about 'credits' defined but
191 /* Define HAVE_SNPRINTF if your system already has snprintf and vsnprintf.
193 * If HAVE_SNPRINTF is defined this module will not produce code for
194 * snprintf and vsnprintf, unless PREFER_PORTABLE_SNPRINTF is defined as well,
195 * causing this portable version of snprintf to be called portable_snprintf
196 * (and portable_vsnprintf).
198 /* #define HAVE_SNPRINTF */
200 /* Define PREFER_PORTABLE_SNPRINTF if your system does have snprintf and
201 * vsnprintf but you would prefer to use the portable routine(s) instead.
202 * In this case the portable routine is declared as portable_snprintf
203 * (and portable_vsnprintf) and a macro 'snprintf' (and 'vsnprintf')
204 * is defined to expand to 'portable_v?snprintf' - see file snprintf.h .
205 * Defining this macro is only useful if HAVE_SNPRINTF is also defined,
206 * but does does no harm if defined nevertheless.
208 /* #define PREFER_PORTABLE_SNPRINTF */
210 /* Define SNPRINTF_LONGLONG_SUPPORT if you want to support
211 * data type (long long int) and length modifier 'll' (e.g. %lld).
212 * If undefined, 'll' is recognized but treated as a single 'l'.
214 * If the system's sprintf does not handle 'll'
215 * the SNPRINTF_LONGLONG_SUPPORT must not be defined!
217 * This is off by default as (long long int) is a language extension.
219 /* #define SNPRINTF_LONGLONG_SUPPORT */
221 /* Define NEED_SNPRINTF_ONLY if you only need snprintf, and not vsnprintf.
222 * If NEED_SNPRINTF_ONLY is defined, the snprintf will be defined directly,
223 * otherwise both snprintf and vsnprintf routines will be defined
224 * and snprintf will be a simple wrapper around vsnprintf, at the expense
225 * of an extra procedure call.
227 /* #define NEED_SNPRINTF_ONLY */
229 /* Define NEED_V?ASN?PRINTF macros if you need library extension
230 * routines asprintf, vasprintf, asnprintf, vasnprintf respectively,
231 * and your system library does not provide them. They are all small
232 * wrapper routines around portable_vsnprintf. Defining any of the four
233 * NEED_V?ASN?PRINTF macros automatically turns off NEED_SNPRINTF_ONLY
234 * and turns on PREFER_PORTABLE_SNPRINTF.
236 * Watch for name conflicts with the system library if these routines
237 * are already present there.
239 * NOTE: vasprintf and vasnprintf routines need va_copy() from stdarg.h, as
240 * specified by C99, to be able to traverse the same list of arguments twice.
241 * I don't know of any other standard and portable way of achieving the same.
242 * With some versions of gcc you may use __va_copy(). You might even get away
243 * with "ap2 = ap", in this case you must not call va_end(ap2) !
244 * #define va_copy(ap2,ap) ap2 = ap
247 #define va_copy(ap2,ap) ap2 = ap
250 /* #define NEED_ASPRINTF */
251 /* #define NEED_ASNPRINTF */
252 /* #define NEED_VASPRINTF */
253 /* #define NEED_VASNPRINTF */
256 /* Define the following macros if desired:
257 * SOLARIS_COMPATIBLE, SOLARIS_BUG_COMPATIBLE,
258 * HPUX_COMPATIBLE, HPUX_BUG_COMPATIBLE, LINUX_COMPATIBLE,
259 * DIGITAL_UNIX_COMPATIBLE, DIGITAL_UNIX_BUG_COMPATIBLE,
260 * PERL_COMPATIBLE, PERL_BUG_COMPATIBLE,
262 * - For portable applications it is best not to rely on peculiarities
263 * of a given implementation so it may be best not to define any
264 * of the macros that select compatibility and to avoid features
265 * that vary among the systems.
267 * - Selecting compatibility with more than one operating system
268 * is not strictly forbidden but is not recommended.
270 * - 'x'_BUG_COMPATIBLE implies 'x'_COMPATIBLE .
272 * - 'x'_COMPATIBLE refers to (and enables) a behaviour that is
273 * documented in a sprintf man page on a given operating system
274 * and actually adhered to by the system's sprintf (but not on
275 * most other operating systems). It may also refer to and enable
276 * a behaviour that is declared 'undefined' or 'implementation specific'
277 * in the man page but a given implementation behaves predictably
280 * - 'x'_BUG_COMPATIBLE refers to (and enables) a behaviour of system's sprintf
281 * that contradicts the sprintf man page on the same operating system.
283 * - I do not claim that the 'x'_COMPATIBLE and 'x'_BUG_COMPATIBLE
284 * conditionals take into account all idiosyncrasies of a particular
285 * implementation, there may be other incompatibilities.
290 /* ============================================= */
291 /* NO USER SERVICABLE PARTS FOLLOWING THIS POINT */
292 /* ============================================= */
294 #define PORTABLE_SNPRINTF_VERSION_MAJOR 2
295 #define PORTABLE_SNPRINTF_VERSION_MINOR 2
297 #if defined(NEED_ASPRINTF) || defined(NEED_ASNPRINTF) || defined(NEED_VASPRINTF) || defined(NEED_VASNPRINTF)
298 # if defined(NEED_SNPRINTF_ONLY)
299 # undef NEED_SNPRINTF_ONLY
301 # if !defined(PREFER_PORTABLE_SNPRINTF)
302 # define PREFER_PORTABLE_SNPRINTF
306 #if defined(SOLARIS_BUG_COMPATIBLE) && !defined(SOLARIS_COMPATIBLE)
307 #define SOLARIS_COMPATIBLE
310 #if defined(HPUX_BUG_COMPATIBLE) && !defined(HPUX_COMPATIBLE)
311 #define HPUX_COMPATIBLE
314 #if defined(DIGITAL_UNIX_BUG_COMPATIBLE) && !defined(DIGITAL_UNIX_COMPATIBLE)
315 #define DIGITAL_UNIX_COMPATIBLE
318 #if defined(PERL_BUG_COMPATIBLE) && !defined(PERL_COMPATIBLE)
319 #define PERL_COMPATIBLE
322 #if defined(LINUX_BUG_COMPATIBLE) && !defined(LINUX_COMPATIBLE)
323 #define LINUX_COMPATIBLE
326 #include <sys/types.h>
337 #define isdigit(c) ((c) >= '0' && (c) <= '9')
339 /* For copying strings longer or equal to 'breakeven_point'
340 * it is more efficient to call memcpy() than to do it inline.
341 * The value depends mostly on the processor architecture,
342 * but also on the compiler and its optimization capabilities.
343 * The value is not critical, some small value greater than zero
344 * will be just fine if you don't care to squeeze every drop
345 * of performance out of the code.
347 * Small values favor memcpy, large values favor inline code.
349 #if defined(__alpha__) || defined(__alpha)
350 # define breakeven_point 2 /* AXP (DEC Alpha) - gcc or cc or egcs */
352 #if defined(__i386__) || defined(__i386)
353 # define breakeven_point 12 /* Intel Pentium/Linux - gcc 2.96 */
356 # define breakeven_point 10 /* HP-PA - gcc */
358 #if defined(__sparc__) || defined(__sparc)
359 # define breakeven_point 33 /* Sun Sparc 5 - gcc 2.8.1 */
362 /* some other values of possible interest: */
363 /* #define breakeven_point 8 */ /* VAX 4000 - vaxc */
364 /* #define breakeven_point 19 */ /* VAX 4000 - gcc 2.7.0 */
366 #ifndef breakeven_point
367 # define breakeven_point 6 /* some reasonable one-size-fits-all value */
370 #define fast_memcpy(d,s,n) \
371 { register size_t nn = (size_t)(n); \
372 if (nn >= breakeven_point) memcpy((d), (s), nn); \
373 else if (nn > 0) { /* proc call overhead is worth only for large strings*/\
374 register char *dd; register const char *ss; \
375 for (ss=(s), dd=(d); nn>0; nn--) *dd++ = *ss++; } }
377 #define fast_memset(d,c,n) \
378 { register size_t nn = (size_t)(n); \
379 if (nn >= breakeven_point) memset((d), (int)(c), nn); \
380 else if (nn > 0) { /* proc call overhead is worth only for large strings*/\
381 register char *dd; register const int cc=(int)(c); \
382 for (dd=(d); nn>0; nn--) *dd++ = cc; } }
386 #if defined(NEED_ASPRINTF)
387 int asprintf (char **ptr, const char *fmt, /*args*/ ...);
389 #if defined(NEED_VASPRINTF)
390 int vasprintf (char **ptr, const char *fmt, va_list ap);
392 #if defined(NEED_ASNPRINTF)
393 int asnprintf (char **ptr, size_t str_m, const char *fmt, /*args*/ ...);
395 #if defined(NEED_VASNPRINTF)
396 int vasnprintf (char **ptr, size_t str_m, const char *fmt, va_list ap);
399 #if defined(HAVE_SNPRINTF)
400 /* declare our portable snprintf routine under name portable_snprintf */
401 /* declare our portable vsnprintf routine under name portable_vsnprintf */
403 /* declare our portable routines under names snprintf and vsnprintf */
404 #define portable_snprintf snprintf
405 #if !defined(NEED_SNPRINTF_ONLY)
406 #define portable_vsnprintf vsnprintf
410 #if !defined(HAVE_SNPRINTF) || defined(PREFER_PORTABLE_SNPRINTF)
411 int portable_snprintf(char *str, size_t str_m, const char *fmt, /*args*/ ...);
412 #if !defined(NEED_SNPRINTF_ONLY)
413 int portable_vsnprintf(char *str, size_t str_m, const char *fmt, va_list ap);
419 static char credits[] = "\n\
420 @(#)snprintf.c, v2.2: Mark Martinec, <mark.martinec@ijs.si>\n\
421 @(#)snprintf.c, v2.2: Copyright 1999, Mark Martinec. Frontier Artistic License applies.\n\
422 @(#)snprintf.c, v2.2: http://www.ijs.si/software/snprintf/\n";
424 #if defined(NEED_ASPRINTF)
425 int asprintf(char **ptr, const char *fmt, /*args*/ ...) {
431 va_start(ap, fmt); /* measure the required size */
432 str_l = portable_vsnprintf(NULL, (size_t)0, fmt, ap);
434 assert(str_l >= 0); /* possible integer overflow if str_m > INT_MAX */
435 *ptr = (char *) malloc(str_m = (size_t)str_l + 1);
436 if (*ptr == NULL) { errno = ENOMEM; str_l = -1; }
440 str_l2 = portable_vsnprintf(*ptr, str_m, fmt, ap);
442 assert(str_l2 == str_l);
448 #if defined(NEED_VASPRINTF)
449 int vasprintf(char **ptr, const char *fmt, va_list ap) {
455 va_copy(ap2, ap); /* don't consume the original ap, we'll need it again */
456 str_l = portable_vsnprintf(NULL, (size_t)0, fmt, ap2);/*get required size*/
459 assert(str_l >= 0); /* possible integer overflow if str_m > INT_MAX */
460 *ptr = (char *) malloc(str_m = (size_t)str_l + 1);
461 if (*ptr == NULL) { errno = ENOMEM; str_l = -1; }
463 int str_l2 = portable_vsnprintf(*ptr, str_m, fmt, ap);
464 assert(str_l2 == str_l);
470 #if defined(NEED_ASNPRINTF)
471 int asnprintf (char **ptr, size_t str_m, const char *fmt, /*args*/ ...) {
476 va_start(ap, fmt); /* measure the required size */
477 str_l = portable_vsnprintf(NULL, (size_t)0, fmt, ap);
479 assert(str_l >= 0); /* possible integer overflow if str_m > INT_MAX */
480 if ((size_t)str_l + 1 < str_m) str_m = (size_t)str_l + 1; /* truncate */
481 /* if str_m is 0, no buffer is allocated, just set *ptr to NULL */
482 if (str_m == 0) { /* not interested in resulting string, just return size */
484 *ptr = (char *) malloc(str_m);
485 if (*ptr == NULL) { errno = ENOMEM; str_l = -1; }
489 str_l2 = portable_vsnprintf(*ptr, str_m, fmt, ap);
491 assert(str_l2 == str_l);
498 #if defined(NEED_VASNPRINTF)
499 int vasnprintf (char **ptr, size_t str_m, const char *fmt, va_list ap) {
504 va_copy(ap2, ap); /* don't consume the original ap, we'll need it again */
505 str_l = portable_vsnprintf(NULL, (size_t)0, fmt, ap2);/*get required size*/
508 assert(str_l >= 0); /* possible integer overflow if str_m > INT_MAX */
509 if ((size_t)str_l + 1 < str_m) str_m = (size_t)str_l + 1; /* truncate */
510 /* if str_m is 0, no buffer is allocated, just set *ptr to NULL */
511 if (str_m == 0) { /* not interested in resulting string, just return size */
513 *ptr = (char *) malloc(str_m);
514 if (*ptr == NULL) { errno = ENOMEM; str_l = -1; }
516 int str_l2 = portable_vsnprintf(*ptr, str_m, fmt, ap);
517 assert(str_l2 == str_l);
525 * If the system does have snprintf and the portable routine is not
526 * specifically required, this module produces no code for snprintf/vsnprintf.
528 #if !defined(HAVE_SNPRINTF) || defined(PREFER_PORTABLE_SNPRINTF)
530 #if !defined(NEED_SNPRINTF_ONLY)
531 int portable_snprintf(char *str, size_t str_m, const char *fmt, /*args*/ ...) {
536 str_l = portable_vsnprintf(str, str_m, fmt, ap);
542 #if defined(NEED_SNPRINTF_ONLY)
543 int portable_snprintf(char *str, size_t str_m, const char *fmt, /*args*/ ...) {
545 int portable_vsnprintf(char *str, size_t str_m, const char *fmt, va_list ap) {
548 #if defined(NEED_SNPRINTF_ONLY)
554 /* In contrast with POSIX, the ISO C99 now says
555 * that str can be NULL and str_m can be 0.
556 * This is more useful than the old: if (str_m < 1) return -1; */
558 #if defined(NEED_SNPRINTF_ONLY)
564 /* if (str_l < str_m) str[str_l++] = *p++; -- this would be sufficient */
565 /* but the following code achieves better performance for cases
566 * where format string is long and contains few conversions */
567 const char *q = strchr(p+1,'%');
568 size_t n = !q ? strlen(p) : (size_t)(q-p);
570 size_t avail = str_m-str_l;
571 fast_memcpy(str+str_l, p, (n>avail?avail:n));
575 const char *starting_p;
576 size_t min_field_width = 0, precision = 0;
577 int zero_padding = 0, precision_specified = 0, justify_left = 0;
578 int alternate_form = 0, force_sign = 0;
579 int space_for_positive = 1; /* If both the ' ' and '+' flags appear,
580 the ' ' flag should be ignored. */
581 char length_modifier = '\0'; /* allowed values: \0, h, l, L */
582 char tmp[32];/* temporary buffer for simple numeric->string conversion */
584 const char *str_arg; /* string address in case of string argument */
585 size_t str_arg_l; /* natural field width of arg without padding
587 unsigned char uchar_arg;
588 /* unsigned char argument value - only defined for c conversion.
589 N.B. standard explicitly states the char argument for
590 the c conversion is unsigned */
592 size_t number_of_zeros_to_pad = 0;
593 /* number of zeros to be inserted for numeric conversions
594 as required by the precision or minimal field width */
596 size_t zero_padding_insertion_ind = 0;
597 /* index into tmp where zero padding is to be inserted */
599 char fmt_spec = '\0';
600 /* current conversion specifier character */
602 str_arg = credits;/* just to make compiler happy (defined but not used)*/
604 starting_p = p; p++; /* skip '%' */
606 while (*p == '0' || *p == '-' || *p == '+' ||
607 *p == ' ' || *p == '#' || *p == '\'') {
609 case '0': zero_padding = 1; break;
610 case '-': justify_left = 1; break;
611 case '+': force_sign = 1; space_for_positive = 0; break;
612 case ' ': force_sign = 1;
613 /* If both the ' ' and '+' flags appear, the ' ' flag should be ignored */
614 #ifdef PERL_COMPATIBLE
615 /* ... but in Perl the last of ' ' and '+' applies */
616 space_for_positive = 1;
619 case '#': alternate_form = 1; break;
624 /* If the '0' and '-' flags both appear, the '0' flag should be ignored. */
626 /* parse field width */
629 p++; j = va_arg(ap, int);
630 if (j >= 0) min_field_width = j;
631 else { min_field_width = -j; justify_left = 1; }
632 } else if (isdigit((int)(*p))) {
633 /* size_t could be wider than unsigned int;
634 make sure we treat argument like common implementations do */
635 unsigned int uj = *p++ - '0';
636 while (isdigit((int)(*p))) uj = 10*uj + (unsigned int)(*p++ - '0');
637 min_field_width = uj;
639 /* parse precision */
641 p++; precision_specified = 1;
643 int j = va_arg(ap, int);
645 if (j >= 0) precision = j;
647 precision_specified = 0; precision = 0;
649 * Solaris 2.6 man page claims that in this case the precision
650 * should be set to 0. Digital Unix 4.0, HPUX 10 and BSD man page
651 * claim that this case should be treated as unspecified precision,
652 * which is what we do here.
655 } else if (isdigit((int)(*p))) {
656 /* size_t could be wider than unsigned int;
657 make sure we treat argument like common implementations do */
658 unsigned int uj = *p++ - '0';
659 while (isdigit((int)(*p))) uj = 10*uj + (unsigned int)(*p++ - '0');
663 /* parse 'h', 'l' and 'll' length modifiers */
664 if (*p == 'h' || *p == 'l') {
665 length_modifier = *p; p++;
666 if (length_modifier == 'l' && *p == 'l') { /* double l = long long */
667 #ifdef SNPRINTF_LONGLONG_SUPPORT
668 length_modifier = '2'; /* double l encoded as '2' */
670 length_modifier = 'l'; /* treat it as a single 'l' */
676 /* common synonyms: */
678 case 'i': fmt_spec = 'd'; break;
679 case 'D': fmt_spec = 'd'; length_modifier = 'l'; break;
680 case 'U': fmt_spec = 'u'; length_modifier = 'l'; break;
681 case 'O': fmt_spec = 'o'; length_modifier = 'l'; break;
684 /* get parameter value, do initial processing */
686 case '%': /* % behaves similar to 's' regarding flags and field widths */
687 case 'c': /* c behaves similar to 's' regarding flags and field widths */
689 length_modifier = '\0'; /* wint_t and wchar_t not supported */
690 /* the result of zero padding flag with non-numeric conversion specifier*/
691 /* is undefined. Solaris and HPUX 10 does zero padding in this case, */
692 /* Digital Unix and Linux does not. */
693 #if !defined(SOLARIS_COMPATIBLE) && !defined(HPUX_COMPATIBLE)
694 zero_padding = 0; /* turn zero padding off for string conversions */
701 int j = va_arg(ap, int);
702 uchar_arg = (unsigned char) j; /* standard demands unsigned char */
703 str_arg = (const char *) &uchar_arg;
707 str_arg = va_arg(ap, const char *);
708 if (!str_arg) str_arg_l = 0;
709 /* make sure not to address string beyond the specified precision !!! */
710 else if (!precision_specified) str_arg_l = strlen(str_arg);
711 /* truncate string if necessary as requested by precision */
712 else if (precision == 0) str_arg_l = 0;
714 /* memchr on HP does not like n > 2^31 !!! */
715 const char *q = memchr(str_arg, '\0',
716 precision <= 0x7fffffff ? precision : 0x7fffffff);
717 str_arg_l = !q ? precision : (size_t)(q-str_arg);
723 case 'd': case 'u': case 'o': case 'x': case 'X': case 'p': {
724 /* NOTE: the u, o, x, X and p conversion specifiers imply
725 the value is unsigned; d implies a signed value */
728 /* 0 if numeric argument is zero (or if pointer is NULL for 'p'),
729 +1 if greater than zero (or nonzero for unsigned arguments),
730 -1 if negative (unsigned argument is never negative) */
732 int int_arg = 0; unsigned int uint_arg = 0;
733 /* only defined for length modifier h, or for no length modifiers */
735 long int long_arg = 0; unsigned long int ulong_arg = 0;
736 /* only defined for length modifier l */
738 void *ptr_arg = NULL;
739 /* pointer argument value -only defined for p conversion */
741 #ifdef SNPRINTF_LONGLONG_SUPPORT
742 long long int long_long_arg = 0;
743 unsigned long long int ulong_long_arg = 0;
744 /* only defined for length modifier ll */
746 if (fmt_spec == 'p') {
747 /* HPUX 10: An l, h, ll or L before any other conversion character
748 * (other than d, i, u, o, x, or X) is ignored.
750 * not specified, but seems to behave as HPUX does.
751 * Solaris: If an h, l, or L appears before any other conversion
752 * specifier (other than d, i, u, o, x, or X), the behavior
753 * is undefined. (Actually %hp converts only 16-bits of address
754 * and %llp treats address as 64-bit data which is incompatible
755 * with (void *) argument on a 32-bit system).
757 #ifdef SOLARIS_COMPATIBLE
758 # ifdef SOLARIS_BUG_COMPATIBLE
759 /* keep length modifiers even if it represents 'll' */
761 if (length_modifier == '2') length_modifier = '\0';
764 length_modifier = '\0';
766 ptr_arg = va_arg(ap, void *);
767 if (ptr_arg != NULL) arg_sign = 1;
768 } else if (fmt_spec == 'd') { /* signed */
769 switch (length_modifier) {
772 /* It is non-portable to specify a second argument of char or short
773 * to va_arg, because arguments seen by the called function
774 * are not char or short. C converts char and short arguments
775 * to int before passing them to a function.
777 int_arg = va_arg(ap, int);
778 if (int_arg > 0) arg_sign = 1;
779 else if (int_arg < 0) arg_sign = -1;
782 long_arg = va_arg(ap, long int);
783 if (long_arg > 0) arg_sign = 1;
784 else if (long_arg < 0) arg_sign = -1;
786 #ifdef SNPRINTF_LONGLONG_SUPPORT
788 long_long_arg = va_arg(ap, long long int);
789 if (long_long_arg > 0) arg_sign = 1;
790 else if (long_long_arg < 0) arg_sign = -1;
794 } else { /* unsigned */
795 switch (length_modifier) {
798 uint_arg = va_arg(ap, unsigned int);
799 if (uint_arg) arg_sign = 1;
802 ulong_arg = va_arg(ap, unsigned long int);
803 if (ulong_arg) arg_sign = 1;
805 #ifdef SNPRINTF_LONGLONG_SUPPORT
807 ulong_long_arg = va_arg(ap, unsigned long long int);
808 if (ulong_long_arg) arg_sign = 1;
813 str_arg = tmp; str_arg_l = 0;
815 * For d, i, u, o, x, and X conversions, if precision is specified,
816 * the '0' flag should be ignored. This is so with Solaris 2.6,
817 * Digital UNIX 4.0, HPUX 10, Linux, FreeBSD, NetBSD; but not with Perl.
819 #ifndef PERL_COMPATIBLE
820 if (precision_specified) zero_padding = 0;
822 if (fmt_spec == 'd') {
823 if (force_sign && arg_sign >= 0)
824 tmp[str_arg_l++] = space_for_positive ? ' ' : '+';
825 /* leave negative numbers for sprintf to handle,
826 to avoid handling tricky cases like (short int)(-32768) */
827 #ifdef LINUX_COMPATIBLE
828 } else if (fmt_spec == 'p' && force_sign && arg_sign > 0) {
829 tmp[str_arg_l++] = space_for_positive ? ' ' : '+';
831 } else if (alternate_form) {
832 if (arg_sign != 0 && (fmt_spec == 'x' || fmt_spec == 'X') )
833 { tmp[str_arg_l++] = '0'; tmp[str_arg_l++] = fmt_spec; }
834 /* alternate form should have no effect for p conversion, but ... */
835 #ifdef HPUX_COMPATIBLE
836 else if (fmt_spec == 'p'
837 /* HPUX 10: for an alternate form of p conversion,
838 * a nonzero result is prefixed by 0x. */
839 #ifndef HPUX_BUG_COMPATIBLE
840 /* Actually it uses 0x prefix even for a zero value. */
843 ) { tmp[str_arg_l++] = '0'; tmp[str_arg_l++] = 'x'; }
846 zero_padding_insertion_ind = str_arg_l;
847 if (!precision_specified) precision = 1; /* default precision is 1 */
848 if (precision == 0 && arg_sign == 0
849 #if defined(HPUX_BUG_COMPATIBLE) || defined(LINUX_COMPATIBLE)
851 /* HPUX 10 man page claims: With conversion character p the result of
852 * converting a zero value with a precision of zero is a null string.
853 * Actually HP returns all zeroes, and Linux returns "(nil)". */
856 /* converted to null string */
857 /* When zero value is formatted with an explicit precision 0,
858 the resulting formatted string is empty (d, i, u, o, x, X, p). */
860 char f[5]; int f_l = 0;
861 f[f_l++] = '%'; /* construct a simple format string for sprintf */
862 if (!length_modifier) { }
863 else if (length_modifier=='2') { f[f_l++] = 'l'; f[f_l++] = 'l'; }
864 else f[f_l++] = length_modifier;
865 f[f_l++] = fmt_spec; f[f_l++] = '\0';
866 if (fmt_spec == 'p') str_arg_l += sprintf(tmp+str_arg_l, f, ptr_arg);
867 else if (fmt_spec == 'd') { /* signed */
868 switch (length_modifier) {
870 case 'h': str_arg_l+=sprintf(tmp+str_arg_l, f, int_arg); break;
871 case 'l': str_arg_l+=sprintf(tmp+str_arg_l, f, long_arg); break;
872 #ifdef SNPRINTF_LONGLONG_SUPPORT
873 case '2': str_arg_l+=sprintf(tmp+str_arg_l,f,long_long_arg); break;
876 } else { /* unsigned */
877 switch (length_modifier) {
879 case 'h': str_arg_l+=sprintf(tmp+str_arg_l, f, uint_arg); break;
880 case 'l': str_arg_l+=sprintf(tmp+str_arg_l, f, ulong_arg); break;
881 #ifdef SNPRINTF_LONGLONG_SUPPORT
882 case '2': str_arg_l+=sprintf(tmp+str_arg_l,f,ulong_long_arg);break;
886 /* include the optional minus sign and possible "0x"
887 in the region before the zero padding insertion point */
888 if (zero_padding_insertion_ind < str_arg_l &&
889 tmp[zero_padding_insertion_ind] == '-') {
890 zero_padding_insertion_ind++;
892 if (zero_padding_insertion_ind+1 < str_arg_l &&
893 tmp[zero_padding_insertion_ind] == '0' &&
894 (tmp[zero_padding_insertion_ind+1] == 'x' ||
895 tmp[zero_padding_insertion_ind+1] == 'X') ) {
896 zero_padding_insertion_ind += 2;
899 { size_t num_of_digits = str_arg_l - zero_padding_insertion_ind;
900 if (alternate_form && fmt_spec == 'o'
901 #ifdef HPUX_COMPATIBLE /* ("%#.o",0) -> "" */
904 #ifdef DIGITAL_UNIX_BUG_COMPATIBLE /* ("%#o",0) -> "00" */
906 /* unless zero is already the first character */
907 && !(zero_padding_insertion_ind < str_arg_l
908 && tmp[zero_padding_insertion_ind] == '0')
910 ) { /* assure leading zero for alternate-form octal numbers */
911 if (!precision_specified || precision < num_of_digits+1) {
912 /* precision is increased to force the first character to be zero,
913 except if a zero value is formatted with an explicit precision
915 precision = num_of_digits+1; precision_specified = 1;
918 /* zero padding to specified precision? */
919 if (num_of_digits < precision)
920 number_of_zeros_to_pad = precision - num_of_digits;
922 /* zero padding to specified minimal field width? */
923 if (!justify_left && zero_padding) {
924 int n = min_field_width - (str_arg_l+number_of_zeros_to_pad);
925 if (n > 0) number_of_zeros_to_pad += n;
929 default: /* unrecognized conversion specifier, keep format string as-is*/
930 zero_padding = 0; /* turn zero padding off for non-numeric convers. */
931 #ifndef DIGITAL_UNIX_COMPATIBLE
932 justify_left = 1; min_field_width = 0; /* reset flags */
934 #if defined(PERL_COMPATIBLE) || defined(LINUX_COMPATIBLE)
935 /* keep the entire format string unchanged */
936 str_arg = starting_p; str_arg_l = p - starting_p;
937 /* well, not exactly so for Linux, which does something inbetween,
938 * and I don't feel an urge to imitate it: "%+++++hy" -> "%+y" */
940 /* discard the unrecognized conversion, just keep *
941 * the unrecognized conversion character */
942 str_arg = p; str_arg_l = 0;
944 if (*p) str_arg_l++; /* include invalid conversion specifier unchanged
945 if not at end-of-string */
948 if (*p) p++; /* step over the just processed conversion specifier */
949 /* insert padding to the left as requested by min_field_width;
950 this does not include the zero padding in case of numerical conversions*/
951 if (!justify_left) { /* left padding with blank or zero */
952 int n = min_field_width - (str_arg_l+number_of_zeros_to_pad);
955 ssize_t avail = str_m-str_l;
956 fast_memset(str+str_l, (zero_padding?'0':' '), (n>avail?avail:n));
961 /* zero padding as requested by the precision or by the minimal field width
962 * for numeric conversions required? */
963 if (number_of_zeros_to_pad <= 0) {
964 /* will not copy first part of numeric right now, *
965 * force it to be copied later in its entirety */
966 zero_padding_insertion_ind = 0;
968 /* insert first part of numerics (sign or '0x') before zero padding */
969 int n = zero_padding_insertion_ind;
972 ssize_t avail = str_m-str_l;
973 fast_memcpy(str+str_l, str_arg, (n>avail?avail:n));
977 /* insert zero padding as requested by the precision or min field width */
978 n = number_of_zeros_to_pad;
981 ssize_t avail = str_m-str_l;
982 fast_memset(str+str_l, '0', (n>avail?avail:n));
987 /* insert formatted string
988 * (or as-is conversion specifier for unknown conversions) */
989 { int n = str_arg_l - zero_padding_insertion_ind;
992 ssize_t avail = str_m-str_l;
993 fast_memcpy(str+str_l, str_arg+zero_padding_insertion_ind,
999 /* insert right padding */
1000 if (justify_left) { /* right blank padding to the field width */
1001 int n = min_field_width - (str_arg_l+number_of_zeros_to_pad);
1003 if (str_l < str_m) {
1004 ssize_t avail = str_m-str_l;
1005 fast_memset(str+str_l, ' ', (n>avail?avail:n));
1012 #if defined(NEED_SNPRINTF_ONLY)
1015 if (str_m > 0) { /* make sure the string is null-terminated
1016 even at the expense of overwriting the last character
1017 (shouldn't happen, but just in case) */
1018 str[str_l <= str_m-1 ? str_l : str_m-1] = '\0';
1020 /* Return the number of characters formatted (excluding trailing null
1021 * character), that is, the number of characters that would have been
1022 * written to the buffer if it were large enough.
1024 * The value of str_l should be returned, but str_l is of unsigned type
1025 * size_t, and snprintf is int, possibly leading to an undetected
1026 * integer overflow, resulting in a negative return value, which is illegal.
1027 * Both XSH5 and ISO C99 (at least the draft) are silent on this issue.
1028 * Should errno be set to EOVERFLOW and EOF returned in this case???