snprintf_2.2/snprintf-orig.c

Sat, 05 Apr 2003 22:24:03 +0200

author
tuomov
date
Sat, 05 Apr 2003 22:24:03 +0200
changeset 50
855953c2f413
parent 35
5a71d53d0228
permissions
-rw-r--r--

trunk: changeset 54
Makefile refixed

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

mercurial