snprintf_2.2/README

Mon, 20 Apr 2020 10:14:32 -0500

author
Tuomo Valkonen <tuomov@iki.fi>
date
Mon, 20 Apr 2020 10:14:32 -0500
changeset 119
87e3bb5086e8
parent 35
5a71d53d0228
permissions
-rw-r--r--

Convert README to markdown

35
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
1
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
2 snprintf.c
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
3 - a portable implementation of snprintf,
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
4 including vsnprintf.c, asnprintf, vasnprintf, asprintf, vasprintf
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
5
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
6 snprintf is a routine to convert numeric and string arguments to
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
7 formatted strings. It is similar to sprintf(3) provided in a system's
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
8 C library, yet it requires an additional argument - the buffer size -
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
9 and it guarantees never to store anything beyond the given buffer,
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
10 regardless of the format or arguments to be formatted. Some newer
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
11 operating systems do provide snprintf in their C library, but many do
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
12 not or do provide an inadequate (slow or idiosyncratic) version, which
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
13 calls for a portable implementation of this routine.
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
14
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
15 Author
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
16
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
17 Mark Martinec <mark.martinec@ijs.si>, April 1999, June 2000
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
18 Copyright © 1999, Mark Martinec
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
19
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
20 Terms and conditions ...
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
21
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
22 This program is free software; you can redistribute it and/or modify
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
23 it under the terms of the Frontier Artistic License which comes with
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
24 this Kit.
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
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
28 * careful adherence to specs regarding flags, field width and
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
29 precision;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
30 * good performance for large string handling (large format, large
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
31 argument or large paddings). Performance is similar to system's
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
32 sprintf and in several cases significantly better (make sure you
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
33 compile with optimizations turned on, tell the compiler the code
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
34 is strict ANSI if necessary to give it more freedom for
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
35 optimizations);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
36 * return value semantics per ISO/IEC 9899:1999 ("ISO C99");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
37 * written in standard ISO/ANSI C - requires an ANSI C compiler.
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
38
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
39 Supported conversion specifiers and data types
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
40
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
41 This snprintf only supports the following conversion specifiers: s, c,
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
42 d, o, u, x, X, p (and synonyms: i, D, U, O - see below) with flags:
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
43 '-', '+', ' ', '0' and '#'. An asterisk is supported for field width
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
44 as well as precision.
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
45
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
46 Length modifiers 'h' (short int), 'l' (long int), and 'll' (long long
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
47 int) are supported.
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
48
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
49 NOTE:
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
50
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
51 If macro SNPRINTF_LONGLONG_SUPPORT is not defined (default) the
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
52 length modifier 'll' is recognized but treated the same as 'l',
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
53 which may cause argument value truncation! Defining
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
54 SNPRINTF_LONGLONG_SUPPORT requires that your system's sprintf also
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
55 handles length modifier 'll'. long long int is a language extension
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
56 which may not be portable.
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
57
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
58 Conversion of numeric data (conversion specifiers d, o, u, x, X, p)
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
59 with length modifiers (none or h, l, ll) is left to the system routine
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
60 sprintf, but all handling of flags, field width and precision as well
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
61 as c and s conversions is done very carefully by this portable
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
62 routine. If a string precision (truncation) is specified (e.g. %.8s)
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
63 it is guaranteed the string beyond the specified precision will not be
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
64 referenced.
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
65
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
66 Length modifiers h, l and ll are ignored for c and s conversions (data
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
67 types wint_t and wchar_t are not supported).
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
68
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
69 The following common synonyms for conversion characters are supported:
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
70 * i is a synonym for d
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
71 * D is a synonym for ld, explicit length modifiers are ignored
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
72 * U is a synonym for lu, explicit length modifiers are ignored
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
73 * O is a synonym for lo, explicit length modifiers are ignored
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
74
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
75 The D, O and U conversion characters are nonstandard, they are
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
76 supported for backward compatibility only, and should not be used for
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
77 new code.
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
78
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
79 The following is specifically not supported:
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
80 * flag ' (thousands' grouping character) is recognized but ignored
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
81 * numeric conversion specifiers: f, e, E, g, G and synonym F, as
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
82 well as the new a and A conversion specifiers
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
83 * length modifier 'L' (long double) and 'q' (quad - use 'll'
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
84 instead)
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
85 * wide character/string conversions: lc, ls, and nonstandard
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
86 synonyms C and S
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
87 * writeback of converted string length: conversion character n
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
88 * the n$ specification for direct reference to n-th argument
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
89 * locales
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
90
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
91 It is permitted for str_m to be zero, and it is permitted to specify
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
92 NULL pointer for resulting string argument if str_m is zero (as per
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
93 ISO C99).
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
94
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
95 The return value is the number of characters which would be generated
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
96 for the given input, excluding the trailing null. If this value is
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
97 greater or equal to str_m, not all characters from the result have
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
98 been stored in str, output bytes beyond the (str_m-1) -th character
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
99 are discarded. If str_m is greater than zero it is guaranteed the
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
100 resulting string will be null-terminated.
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
101
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
102 NOTE that this matches the ISO C99, OpenBSD, and GNU C library 2.1,
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
103 but is different from some older and vendor implementations, and is
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
104 also different from XPG, XSH5, SUSv2 specifications. For historical
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
105 discussion on changes in the semantics and standards of snprintf see
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
106 printf(3) man page in the Linux programmers manual.
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
107
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
108 Routines asprintf and vasprintf return a pointer (in the ptr argument)
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
109 to a buffer sufficiently large to hold the resulting string. This
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
110 pointer should be passed to free(3) to release the allocated storage
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
111 when it is no longer needed. If sufficient space cannot be allocated,
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
112 these functions will return -1 and set ptr to be a NULL pointer. These
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
113 two routines are a GNU C library extensions (glibc).
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
114
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
115 Routines asnprintf and vasnprintf are similar to asprintf and
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
116 vasprintf, yet, like snprintf and vsnprintf counterparts, will write
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
117 at most str_m-1 characters into the allocated output string, the last
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
118 character in the allocated buffer then gets the terminating null. If
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
119 the formatted string length (the return value) is greater than or
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
120 equal to the str_m argument, the resulting string was truncated and
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
121 some of the formatted characters were discarded. These routines
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
122 present a handy way to limit the amount of allocated memory to some
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
123 sane value.
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
124
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
125 Availability
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
126
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
127 http://www.ijs.si/software/snprintf/
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
128 * snprintf_1.3.tar.gz (1999-06-30), md5 sum: snprintf_1.3.tar.gz.md5
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
129 * snprintf_2.1.tar.gz (2000-07-14), md5 sum: snprintf_2.1.tar.gz.md5
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
130 * snprintf_2.2.tar.gz (2000-10-18), md5 sum: snprintf_2.2.tar.gz.md5
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
131
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
132 Mailing list
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
133
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
134 There is a very low-traffic mailing list snprintf-announce@ijs.si
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
135 where announcements about new versions will be posted as well as
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
136 warnings about threatening bugs if discovered. The posting is
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
137 restricted to snprintf developer(s).
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
138
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
139 To subscribe to (or unsubscribe from) the mailing list please visit
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
140 the list server's web page
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
141 http://mailman.ijs.si/listinfo/snprintf-announce
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
142
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
143 You can also subscribe to the list by mailing the command SUBSCRIBE
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
144 either in the subject or in the message body to the address
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
145 snprintf-announce-request@ijs.si . You will be asked for confirmation
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
146 before subscription will be effective.
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
147
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
148 The list of members is only accessible to the list administrator, so
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
149 there is no need for concern about automatic e-mail address gatherers.
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
150
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
151 Questions about the mailing list and concerns for the attention of a
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
152 person should be sent to snprintf-announce-admin@ijs.si
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
153
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
154 There is no general discussion list about portable snprintf at the
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
155 moment. Please send comments and suggestion to the author.
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
156
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
157 Revision history
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
158
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
159 Version 1.3 fixes a runaway loop problem from 1.2. Please upgrade.
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
160
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
161 1999-06-30 V1.3 Mark Martinec <mark.martinec@ijs.si>
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
162
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
163 + fixed runaway loop (eventually crashing when str_l wraps
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
164 beyond 2^31) while copying format string without conversion
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
165 specifiers to a buffer that is too short (thanks to Edwin
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
166 Young <edwiny@autonomy.com> for spotting the problem);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
167 + added macros PORTABLE_SNPRINTF_VERSION_(MAJOR|MINOR) to
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
168 snprintf.h
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
169
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
170 2000-02-14 V2.0 (never released) Mark Martinec <mark.martinec@ijs.si>
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
171
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
172 + relaxed license terms: The Artistic License now applies. You
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
173 may still apply the GNU GENERAL PUBLIC LICENSE as was
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
174 distributed with previous versions, if you prefer;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
175 + changed REVISION HISTORY dates to use ISO 8601 date format;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
176 + added vsnprintf (patch also independently proposed by Caolán
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
177 McNamara 2000-05-04, and Keith M Willenson 2000-06-01)
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
178
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
179 2000-06-27 V2.1 Mark Martinec <mark.martinec@ijs.si>
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
180
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
181 + removed POSIX check for str_m < 1; value 0 for str_m is
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
182 allowed by ISO C99 (and GNU C library 2.1) (pointed out on
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
183 2000-05-04 by Caolán McNamara, caolan@ csn dot ul dot ie).
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
184 Besides relaxed license this change in standards adherence is
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
185 the main reason to bump up the major version number;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
186 + added nonstandard routines asnprintf, vasnprintf, asprintf,
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
187 vasprintf that dynamically allocate storage for the resulting
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
188 string; these routines are not compiled by default, see
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
189 comments where NEED_V?ASN?PRINTF macros are defined;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
190 + autoconf contributed by Caolán McNamara
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
191
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
192 2000-10-06 V2.2 Mark Martinec <mark.martinec@ijs.si>
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
193
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
194 + BUG FIX: the %c conversion used a temporary variable that was
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
195 no longer in scope when referenced, possibly causing
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
196 incorrect resulting character;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
197 + BUG FIX: make precision and minimal field width unsigned to
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
198 handle huge values (2^31 <= n < 2^32) correctly; also be more
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
199 careful in the use of signed/unsigned/size_t internal
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
200 variables -- probably more careful than many vendor
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
201 implementations, but there may still be a case where huge
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
202 values of str_m, precision or minimal field could cause
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
203 incorrect behaviour;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
204 + use separate variables for signed/unsigned arguments, and for
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
205 short/int, long, and long long argument lengths to avoid
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
206 possible incompatibilities on certain computer architectures.
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
207 Also use separate variable arg_sign to hold sign of a numeric
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
208 argument, to make code more transparent;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
209 + some fiddling with zero padding and "0x" to make it Linux
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
210 compatible;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
211 + systematically use macros fast_memcpy and fast_memset instead
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
212 of case-by-case hand optimization; determine some breakeven
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
213 string lengths for different architectures;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
214 + terminology change: format -> conversion specifier, C9x ->
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
215 ISO/IEC 9899:1999 ("ISO C99"), alternative form -> alternate
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
216 form, data type modifier -> length modifier;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
217 + several comments rephrased and new ones added;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
218 + make compiler not complain about 'credits' defined but not
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
219 used;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
220
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
221 Other implementations of snprintf
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
222
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
223 I am aware of some other (more or less) portable implementations of
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
224 snprintf. I do not claim they are free software - please refer to
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
225 their respective copyright and licensing terms. If you know of other
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
226 versions please let me know.
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
227 * a very thorough implementation (src/util_snprintf.c) by the Apache
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
228 Group distributed with the Apache web server -
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
229 http://www.apache.org/ . Does its own floating point conversions
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
230 using routines ecvt(3), fcvt(3) and gcvt(3) from the standard C
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
231 library or from the GNU libc.
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
232 This is from the code:
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
233
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
234 This software [...] was originally based on public domain software
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
235 written at the National Center for Supercomputing Applications,
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
236 University of Illinois, Urbana-Champaign.
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
237 [...] This code is based on, and used with the permission of, the
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
238 SIO stdio-replacement strx_* functions by Panos Tsirigotis
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
239 <panos@alumni.cs.colorado.edu> for xinetd.
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
240 * QCI Utilities use a modified version of snprintf from the Apache
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
241 group.
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
242 * implementations as distributed with OpenBSD, FreeBSD, and NetBSD
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
243 are all wrappers to vfprintf.c, which is derived from software
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
244 contributed to Berkeley by Chris Torek.
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
245 * implementation from Prof. Patrick Powell <papowell@sdsu.edu>,
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
246 Dept. Electrical and Computer Engineering, San Diego State
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
247 University, San Diego, CA 92182-1309, published in Bugtraq
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
248 archives for 3rd quarter (Jul-Aug) 1995. No floating point
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
249 conversions.
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
250 * Brandon Long's <blong@fiction.net> modified version of Prof.
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
251 Patrick Powell's snprintf with contributions from others. With
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
252 minimal floating point support.
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
253 * implementation (src/snprintf.c) as distributed with sendmail -
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
254 http://www.sendmail.org/ is a cleaned up Prof. Patrick Powell's
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
255 version to compile properly and to support .precision and %lx.
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
256 * implementation from Caolán McNamara available at
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
257 http://www.csn.ul.ie/~caolan/publink/snprintf-1.1.tar.gz, handles
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
258 floating point.
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
259 * implementation used by newlog (a replacement for syslog(3)) made
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
260 available by the SOS Corporation. Enabling floating point support
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
261 is a compile-time option.
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
262 * implementation by Michael Richardson <mcr@metis.milkyway.com> is
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
263 available at http://sandelman.ottawa.on.ca/SSW/snp/snp.html. It is
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
264 based on BSD44-lite's vfprintf() call, modified to function on
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
265 SunOS. Needs internal routines from the 4.4 strtod (included),
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
266 requires GCC to compile the long long (aka quad_t) portions.
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
267 * implementation from Tomi Salo <ttsalo@ssh.fi> distributed with SSH
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
268 2.0 Unix Server. Not in public domain. Floating point conversions
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
269 done by system's sprintf.
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
270 * and for completeness: my portable version described in this very
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
271 document available at http://www.ijs.si/software/snprintf/ .
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
272
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
273 In retrospect, it appears that a lot of effort was wasted by many
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
274 people for not being aware of what others are doing. Sigh.
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
275
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
276 Also of interest: The Approved Base Working Group Resolution for XSH5,
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
277 Ref: bwg98-006, Topic: snprintf.
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
278 _________________________________________________________________
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
279
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
280 mm
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
281 Last updated: 2000-10-18
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
282
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
283 Valid HTML 4.0!

mercurial