snprintf_2.2/snprintf.c

Sat, 26 Feb 2005 22:09:33 +0100

author
Tuomo Valkonen <tuomov@iki.fi>
date
Sat, 26 Feb 2005 22:09:33 +0100
changeset 92
55fcdff5bcea
parent 35
5a71d53d0228
child 100
8ae0f0aa5a16
permissions
-rw-r--r--

Added routines to take first/last elements of objlist and ptrlist.

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

mercurial