]> git.decadent.org.uk Git - ion3.git/blob - libtu/snprintf_2.2/snprintf.c
[svn-inject] Installing original source of ion3
[ion3.git] / libtu / snprintf_2.2 / snprintf.c
1 #include <stdarg.h>
2 #define NEED_ASPRINTF
3 #define NEED_VASPRINTF
4 /*
5  * snprintf.c - a portable implementation of snprintf
6  *
7  * AUTHOR
8  *   Mark Martinec <mark.martinec@ijs.si>, April 1999.
9  *
10  *   Copyright 1999, Mark Martinec. All rights reserved.
11  *
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
15  *   with this Kit.
16  *
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.
21  *
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.
25  *
26  * FEATURES
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.
35  *
36  * SUPPORTED CONVERSION SPECIFIERS AND DATA TYPES
37  *
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.
42  *
43  * Length modifiers 'h' (short int), 'l' (long int),
44  * and 'll' (long long int) are supported.
45  * NOTE:
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.
52  *
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.
59  *
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).
62  *
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.
70  *
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
77  *     synonyms C and S
78  *   - writeback of converted string length: conversion character n
79  *   - the n$ specification for direct reference to n-th argument
80  *   - locales
81  *
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).
84  *
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.
91  *
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.
97  *
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).
104  *
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.
113  *
114  * AVAILABILITY
115  *   http://www.ijs.si/software/snprintf/
116  *
117  * REVISION HISTORY
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)
140  *                to snprintf.h
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
177  *                Linux compatible;
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
187  *                not used;
188  */
189
190
191 /* Define HAVE_SNPRINTF if your system already has snprintf and vsnprintf.
192  *
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).
197  */
198 /* #define HAVE_SNPRINTF */
199
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.
207  */
208 /* #define PREFER_PORTABLE_SNPRINTF */
209
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'.
213  *
214  * If the system's sprintf does not handle 'll'
215  * the SNPRINTF_LONGLONG_SUPPORT must not be defined!
216  *
217  * This is off by default as (long long int) is a language extension.
218  */
219 /* #define SNPRINTF_LONGLONG_SUPPORT */
220
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.
226  */
227 /* #define NEED_SNPRINTF_ONLY */
228
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.
235  *
236  * Watch for name conflicts with the system library if these routines
237  * are already present there.
238  *
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
245  */
246 #ifndef va_copy
247 #define va_copy(ap2,ap) ap2 = ap 
248 #endif
249
250 /* #define NEED_ASPRINTF   */
251 /* #define NEED_ASNPRINTF  */
252 /* #define NEED_VASPRINTF  */
253 /* #define NEED_VASNPRINTF */
254
255
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,
261  *
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.
266  *
267  * - Selecting compatibility with more than one operating system
268  *   is not strictly forbidden but is not recommended.
269  *
270  * - 'x'_BUG_COMPATIBLE implies 'x'_COMPATIBLE .
271  *
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
278  *   in a certain way.
279  *
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.
282  *
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.
286  */
287
288
289 \f
290 /* ============================================= */
291 /* NO USER SERVICABLE PARTS FOLLOWING THIS POINT */
292 /* ============================================= */
293
294 #define PORTABLE_SNPRINTF_VERSION_MAJOR 2
295 #define PORTABLE_SNPRINTF_VERSION_MINOR 2
296
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
300 # endif
301 # if !defined(PREFER_PORTABLE_SNPRINTF)
302 # define PREFER_PORTABLE_SNPRINTF
303 # endif
304 #endif
305
306 #if defined(SOLARIS_BUG_COMPATIBLE) && !defined(SOLARIS_COMPATIBLE)
307 #define SOLARIS_COMPATIBLE
308 #endif
309
310 #if defined(HPUX_BUG_COMPATIBLE) && !defined(HPUX_COMPATIBLE)
311 #define HPUX_COMPATIBLE
312 #endif
313
314 #if defined(DIGITAL_UNIX_BUG_COMPATIBLE) && !defined(DIGITAL_UNIX_COMPATIBLE)
315 #define DIGITAL_UNIX_COMPATIBLE
316 #endif
317
318 #if defined(PERL_BUG_COMPATIBLE) && !defined(PERL_COMPATIBLE)
319 #define PERL_COMPATIBLE
320 #endif
321
322 #if defined(LINUX_BUG_COMPATIBLE) && !defined(LINUX_COMPATIBLE)
323 #define LINUX_COMPATIBLE
324 #endif
325
326 #include <sys/types.h>
327 #include <string.h>
328 #include <stdlib.h>
329 #include <stdio.h>
330 #include <stdarg.h>
331 #include <assert.h>
332 #include <errno.h>
333
334 #ifdef isdigit
335 #undef isdigit
336 #endif
337 #define isdigit(c) ((c) >= '0' && (c) <= '9')
338
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.
346  *
347  * Small values favor memcpy, large values favor inline code.
348  */
349 #if defined(__alpha__) || defined(__alpha)
350 #  define breakeven_point   2   /* AXP (DEC Alpha)     - gcc or cc or egcs */
351 #endif
352 #if defined(__i386__)  || defined(__i386)
353 #  define breakeven_point  12   /* Intel Pentium/Linux - gcc 2.96 */
354 #endif
355 #if defined(__hppa)
356 #  define breakeven_point  10   /* HP-PA               - gcc */
357 #endif
358 #if defined(__sparc__) || defined(__sparc)
359 #  define breakeven_point  33   /* Sun Sparc 5         - gcc 2.8.1 */
360 #endif
361
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 */
365
366 #ifndef breakeven_point
367 #  define breakeven_point   6   /* some reasonable one-size-fits-all value */
368 #endif
369
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++; } }
376
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; } }
383
384 /* prototypes */
385
386 #if defined(NEED_ASPRINTF)
387 int asprintf   (char **ptr, const char *fmt, /*args*/ ...);
388 #endif
389 #if defined(NEED_VASPRINTF)
390 int vasprintf  (char **ptr, const char *fmt, va_list ap);
391 #endif
392 #if defined(NEED_ASNPRINTF)
393 int asnprintf  (char **ptr, size_t str_m, const char *fmt, /*args*/ ...);
394 #endif
395 #if defined(NEED_VASNPRINTF)
396 int vasnprintf (char **ptr, size_t str_m, const char *fmt, va_list ap);
397 #endif
398
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 */
402 #else
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
407 #endif
408 #endif
409
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);
414 #endif
415 #endif
416
417 /* declarations */
418
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";
423
424 #if defined(NEED_ASPRINTF)
425 int asprintf(char **ptr, const char *fmt, /*args*/ ...) {
426   va_list ap;
427   size_t str_m;
428   int str_l;
429
430   *ptr = NULL;
431   va_start(ap, fmt);                            /* measure the required size */
432   str_l = portable_vsnprintf(NULL, (size_t)0, fmt, ap);
433   va_end(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; }
437   else {
438     int str_l2;
439     va_start(ap, fmt);
440     str_l2 = portable_vsnprintf(*ptr, str_m, fmt, ap);
441     va_end(ap);
442     assert(str_l2 == str_l);
443   }
444   return str_l;
445 }
446 #endif
447
448 #if defined(NEED_VASPRINTF)
449 int vasprintf(char **ptr, const char *fmt, va_list ap) {
450   size_t str_m;
451   int str_l;
452
453   *ptr = NULL;
454   { va_list ap2;
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*/
457     va_end(ap2);
458   }
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; }
462   else {
463     int str_l2 = portable_vsnprintf(*ptr, str_m, fmt, ap);
464     assert(str_l2 == str_l);
465   }
466   return str_l;
467 }
468 #endif
469
470 #if defined(NEED_ASNPRINTF)
471 int asnprintf (char **ptr, size_t str_m, const char *fmt, /*args*/ ...) {
472   va_list ap;
473   int str_l;
474
475   *ptr = NULL;
476   va_start(ap, fmt);                            /* measure the required size */
477   str_l = portable_vsnprintf(NULL, (size_t)0, fmt, ap);
478   va_end(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 */
483   } else {
484     *ptr = (char *) malloc(str_m);
485     if (*ptr == NULL) { errno = ENOMEM; str_l = -1; }
486     else {
487       int str_l2;
488       va_start(ap, fmt);
489       str_l2 = portable_vsnprintf(*ptr, str_m, fmt, ap);
490       va_end(ap);
491       assert(str_l2 == str_l);
492     }
493   }
494   return str_l;
495 }
496 #endif
497
498 #if defined(NEED_VASNPRINTF)
499 int vasnprintf (char **ptr, size_t str_m, const char *fmt, va_list ap) {
500   int str_l;
501
502   *ptr = NULL;
503   { va_list ap2;
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*/
506     va_end(ap2);
507   }
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 */
512   } else {
513     *ptr = (char *) malloc(str_m);
514     if (*ptr == NULL) { errno = ENOMEM; str_l = -1; }
515     else {
516       int str_l2 = portable_vsnprintf(*ptr, str_m, fmt, ap);
517       assert(str_l2 == str_l);
518     }
519   }
520   return str_l;
521 }
522 #endif
523
524 /*
525  * If the system does have snprintf and the portable routine is not
526  * specifically required, this module produces no code for snprintf/vsnprintf.
527  */
528 #if !defined(HAVE_SNPRINTF) || defined(PREFER_PORTABLE_SNPRINTF)
529
530 #if !defined(NEED_SNPRINTF_ONLY)
531 int portable_snprintf(char *str, size_t str_m, const char *fmt, /*args*/ ...) {
532   va_list ap;
533   int str_l;
534
535   va_start(ap, fmt);
536   str_l = portable_vsnprintf(str, str_m, fmt, ap);
537   va_end(ap);
538   return str_l;
539 }
540 #endif
541
542 #if defined(NEED_SNPRINTF_ONLY)
543 int portable_snprintf(char *str, size_t str_m, const char *fmt, /*args*/ ...) {
544 #else
545 int portable_vsnprintf(char *str, size_t str_m, const char *fmt, va_list ap) {
546 #endif
547
548 #if defined(NEED_SNPRINTF_ONLY)
549   va_list ap;
550 #endif
551   size_t str_l = 0;
552   const char *p = fmt;
553
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; */
557
558 #if defined(NEED_SNPRINTF_ONLY)
559   va_start(ap, fmt);
560 #endif
561   if (!p) p = "";
562   while (*p) {
563     if (*p != '%') {
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);
569       if (str_l < str_m) {
570         size_t avail = str_m-str_l;
571         fast_memcpy(str+str_l, p, (n>avail?avail:n));
572       }
573       p += n; str_l += n;
574     } else {
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 */
583
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
586                                    and sign */
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 */
591
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 */
595
596       size_t zero_padding_insertion_ind = 0;
597         /* index into tmp where zero padding is to be inserted */
598
599       char fmt_spec = '\0';
600         /* current conversion specifier character */
601
602       str_arg = credits;/* just to make compiler happy (defined but not used)*/
603       str_arg = NULL;
604       starting_p = p; p++;  /* skip '%' */
605    /* parse flags */
606       while (*p == '0' || *p == '-' || *p == '+' ||
607              *p == ' ' || *p == '#' || *p == '\'') {
608         switch (*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;
617 #endif
618                   break;
619         case '#': alternate_form = 1; break;
620         case '\'': break;
621         }
622         p++;
623       }
624    /* If the '0' and '-' flags both appear, the '0' flag should be ignored. */
625
626    /* parse field width */
627       if (*p == '*') {
628         int j;
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;
638       }
639    /* parse precision */
640       if (*p == '.') {
641         p++; precision_specified = 1;
642         if (*p == '*') {
643           int j = va_arg(ap, int);
644           p++;
645           if (j >= 0) precision = j;
646           else {
647             precision_specified = 0; precision = 0;
648          /* NOTE:
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.
653           */
654           }
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');
660           precision = uj;
661         }
662       }
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' */
669 #else
670           length_modifier = 'l';                 /* treat it as a single 'l' */
671 #endif
672           p++;
673         }
674       }
675       fmt_spec = *p;
676    /* common synonyms: */
677       switch (fmt_spec) {
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;
682       default: break;
683       }
684    /* get parameter value, do initial processing */
685       switch (fmt_spec) {
686       case '%': /* % behaves similar to 's' regarding flags and field widths */
687       case 'c': /* c behaves similar to 's' regarding flags and field widths */
688       case 's':
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 */
695 #endif
696         str_arg_l = 1;
697         switch (fmt_spec) {
698         case '%':
699           str_arg = p; break;
700         case 'c': {
701           int j = va_arg(ap, int);
702           uchar_arg = (unsigned char) j;   /* standard demands unsigned char */
703           str_arg = (const char *) &uchar_arg;
704           break;
705         }
706         case 's':
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;
713           else {
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);
718           }
719           break;
720         default: break;
721         }
722         break;
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 */
726
727         int arg_sign = 0;
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) */
731
732         int int_arg = 0;  unsigned int uint_arg = 0;
733           /* only defined for length modifier h, or for no length modifiers */
734
735         long int long_arg = 0;  unsigned long int ulong_arg = 0;
736           /* only defined for length modifier l */
737
738         void *ptr_arg = NULL;
739           /* pointer argument value -only defined for p conversion */
740
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 */
745 #endif
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.
749          * Digital Unix:
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).
756          */
757 #ifdef SOLARIS_COMPATIBLE
758 #  ifdef SOLARIS_BUG_COMPATIBLE
759           /* keep length modifiers even if it represents 'll' */
760 #  else
761           if (length_modifier == '2') length_modifier = '\0';
762 #  endif
763 #else
764           length_modifier = '\0';
765 #endif
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) {
770           case '\0':
771           case 'h':
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.
776           */
777             int_arg = va_arg(ap, int);
778             if      (int_arg > 0) arg_sign =  1;
779             else if (int_arg < 0) arg_sign = -1;
780             break;
781           case 'l':
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;
785             break;
786 #ifdef SNPRINTF_LONGLONG_SUPPORT
787           case '2':
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;
791             break;
792 #endif
793           }
794         } else {  /* unsigned */
795           switch (length_modifier) {
796           case '\0':
797           case 'h':
798             uint_arg = va_arg(ap, unsigned int);
799             if (uint_arg) arg_sign = 1;
800             break;
801           case 'l':
802             ulong_arg = va_arg(ap, unsigned long int);
803             if (ulong_arg) arg_sign = 1;
804             break;
805 #ifdef SNPRINTF_LONGLONG_SUPPORT
806           case '2':
807             ulong_long_arg = va_arg(ap, unsigned long long int);
808             if (ulong_long_arg) arg_sign = 1;
809             break;
810 #endif
811           }
812         }
813         str_arg = tmp; str_arg_l = 0;
814      /* NOTE:
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.
818       */
819 #ifndef PERL_COMPATIBLE
820         if (precision_specified) zero_padding = 0;
821 #endif
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 ? ' ' : '+';
830 #endif
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. */
841                    && arg_sign != 0
842 #endif
843                   ) { tmp[str_arg_l++] = '0'; tmp[str_arg_l++] = 'x'; }
844 #endif
845         }
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)
850             && fmt_spec != 'p'
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)". */
854 #endif
855         ) {
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).   */
859         } else {
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) {
869             case '\0':
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;
874 #endif
875             }
876           } else {  /* unsigned */
877             switch (length_modifier) {
878             case '\0':
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;
883 #endif
884             }
885           }
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++;
891           }
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;
897           }
898         }
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) -> ""  */
902               && (str_arg_l > 0)
903 #endif
904 #ifdef DIGITAL_UNIX_BUG_COMPATIBLE                      /* ("%#o",0) -> "00" */
905 #else
906               /* unless zero is already the first character */
907               && !(zero_padding_insertion_ind < str_arg_l
908                    && tmp[zero_padding_insertion_ind] == '0')
909 #endif
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
914                 of zero */
915               precision = num_of_digits+1; precision_specified = 1;
916             }
917           }
918        /* zero padding to specified precision? */
919           if (num_of_digits < precision) 
920             number_of_zeros_to_pad = precision - num_of_digits;
921         }
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;
926         }
927         break;
928       }
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 */
933 #endif
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"  */
939 #else
940      /* discard the unrecognized conversion, just keep *
941       * the unrecognized conversion character          */
942         str_arg = p; str_arg_l = 0;
943 #endif
944         if (*p) str_arg_l++;  /* include invalid conversion specifier unchanged
945                                  if not at end-of-string */
946         break;
947       }
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);
953         if (n > 0) {
954           if (str_l < str_m) {
955             ssize_t avail = str_m-str_l;
956             fast_memset(str+str_l, (zero_padding?'0':' '), (n>avail?avail:n));
957           }
958           str_l += n;
959         }
960       }
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;
967       } else {
968      /* insert first part of numerics (sign or '0x') before zero padding */
969         int n = zero_padding_insertion_ind;
970         if (n > 0) {
971           if (str_l < str_m) {
972             ssize_t avail = str_m-str_l;
973             fast_memcpy(str+str_l, str_arg, (n>avail?avail:n));
974           }
975           str_l += n;
976         }
977      /* insert zero padding as requested by the precision or min field width */
978         n = number_of_zeros_to_pad;
979         if (n > 0) {
980           if (str_l < str_m) {
981             ssize_t avail = str_m-str_l;
982             fast_memset(str+str_l, '0', (n>avail?avail:n));
983           }
984           str_l += n;
985         }
986       }
987    /* insert formatted string
988     * (or as-is conversion specifier for unknown conversions) */
989       { int n = str_arg_l - zero_padding_insertion_ind;
990         if (n > 0) {
991           if (str_l < str_m) {
992             ssize_t avail = str_m-str_l;
993             fast_memcpy(str+str_l, str_arg+zero_padding_insertion_ind,
994                         (n>avail?avail:n));
995           }
996           str_l += n;
997         }
998       }
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);
1002         if (n > 0) {
1003           if (str_l < str_m) {
1004             ssize_t avail = str_m-str_l;
1005             fast_memset(str+str_l, ' ', (n>avail?avail:n));
1006           }
1007           str_l += n;
1008         }
1009       }
1010     }
1011   }
1012 #if defined(NEED_SNPRINTF_ONLY)
1013   va_end(ap);
1014 #endif
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';
1019   }
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.
1023    *
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???
1029    */
1030   return (int) str_l;
1031 }
1032 #endif