Blame


1 67956560 2002-05-19 alex /*
2 67956560 2002-05-19 alex * ngIRCd -- The Next Generation IRC Daemon
3 67956560 2002-05-19 alex * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
4 67956560 2002-05-19 alex *
5 67956560 2002-05-19 alex * Dieses Programm ist freie Software. Sie koennen es unter den Bedingungen
6 67956560 2002-05-19 alex * der GNU General Public License (GPL), wie von der Free Software Foundation
7 67956560 2002-05-19 alex * herausgegeben, weitergeben und/oder modifizieren, entweder unter Version 2
8 67956560 2002-05-19 alex * der Lizenz oder (wenn Sie es wuenschen) jeder spaeteren Version.
9 67956560 2002-05-19 alex * Naehere Informationen entnehmen Sie bitter der Datei COPYING. Eine Liste
10 67956560 2002-05-19 alex * der an ngIRCd beteiligten Autoren finden Sie in der Datei AUTHORS.
11 67956560 2002-05-19 alex *
12 ba258e65 2002-05-27 alex * $Id: vsnprintf.c,v 1.3 2002/05/27 13:01:04 alex Exp $
13 67956560 2002-05-19 alex *
14 c5461c45 2002-05-19 alex * vsnprintf.c: u.a. Ersatz fuer vsnprintf()
15 67956560 2002-05-19 alex */
16 67956560 2002-05-19 alex
17 67956560 2002-05-19 alex
18 67956560 2002-05-19 alex #include "portab.h"
19 67956560 2002-05-19 alex #include "imp.h"
20 67956560 2002-05-19 alex
21 67956560 2002-05-19 alex #include "exp.h"
22 67956560 2002-05-19 alex
23 67956560 2002-05-19 alex
24 67956560 2002-05-19 alex /*
25 67956560 2002-05-19 alex * snprintf.c: Copyright Patrick Powell 1995
26 67956560 2002-05-19 alex * This code is based on code written by Patrick Powell (papowell@astart.com)
27 67956560 2002-05-19 alex * It may be used for any purpose as long as this notice remains intact
28 67956560 2002-05-19 alex * on all source code distributions
29 67956560 2002-05-19 alex *
30 67956560 2002-05-19 alex * Original: Patrick Powell Tue Apr 11 09:48:21 PDT 1995
31 67956560 2002-05-19 alex * A bombproof version of doprnt (dopr) included.
32 67956560 2002-05-19 alex * Sigh. This sort of thing is always nasty do deal with. Note that
33 67956560 2002-05-19 alex * the version here does not include floating point...
34 67956560 2002-05-19 alex *
35 67956560 2002-05-19 alex * snprintf() is used instead of sprintf() as it does limit checks
36 67956560 2002-05-19 alex * for string length. This covers a nasty loophole.
37 67956560 2002-05-19 alex *
38 67956560 2002-05-19 alex * The other functions are there to prevent NULL pointers from
39 67956560 2002-05-19 alex * causing nast effects.
40 67956560 2002-05-19 alex *
41 67956560 2002-05-19 alex * More Recently:
42 67956560 2002-05-19 alex * Brandon Long <blong@fiction.net> 9/15/96 for mutt 0.43
43 67956560 2002-05-19 alex * This was ugly. It is still ugly. I opted out of floating point
44 67956560 2002-05-19 alex * numbers, but the formatter understands just about everything
45 67956560 2002-05-19 alex * from the normal C string format, at least as far as I can tell from
46 67956560 2002-05-19 alex * the Solaris 2.5 printf(3S) man page.
47 67956560 2002-05-19 alex *
48 c5461c45 2002-05-19 alex * Brandon Long <blong@fiction.net> 10/22/97 for mutt 0.87.1
49 c5461c45 2002-05-19 alex * Ok, added some minimal floating point support, which means this
50 c5461c45 2002-05-19 alex * probably requires libm on most operating systems. Don't yet
51 c5461c45 2002-05-19 alex * support the exponent (e,E) and sigfig (g,G). Also, fmtint()
52 c5461c45 2002-05-19 alex * was pretty badly broken, it just wasn't being exercised in ways
53 c5461c45 2002-05-19 alex * which showed it, so that's been fixed. Also, formated the code
54 c5461c45 2002-05-19 alex * to mutt conventions, and removed dead code left over from the
55 c5461c45 2002-05-19 alex * original. Also, there is now a builtin-test, just compile with:
56 c5461c45 2002-05-19 alex * gcc -DTEST_SNPRINTF -o snprintf snprintf.c -lm
57 c5461c45 2002-05-19 alex * and run snprintf for results.
58 67956560 2002-05-19 alex *
59 c5461c45 2002-05-19 alex * Thomas Roessler <roessler@guug.de> 01/27/98 for mutt 0.89i
60 c5461c45 2002-05-19 alex * The PGP code was using unsigned hexadecimal formats.
61 c5461c45 2002-05-19 alex * Unfortunately, unsigned formats simply didn't work.
62 67956560 2002-05-19 alex *
63 c5461c45 2002-05-19 alex * Michael Elkins <me@cs.hmc.edu> 03/05/98 for mutt 0.90.8
64 c5461c45 2002-05-19 alex * The original code assumed that both snprintf() and vsnprintf() were
65 c5461c45 2002-05-19 alex * missing. Some systems only have snprintf() but not vsnprintf(), so
66 c5461c45 2002-05-19 alex * the code is now broken down under HAVE_SNPRINTF and HAVE_VSNPRINTF.
67 67956560 2002-05-19 alex *
68 c5461c45 2002-05-19 alex * Andrew Tridgell <tridge@samba.org>, October 1998
69 c5461c45 2002-05-19 alex * fixed handling of %.0f
70 c5461c45 2002-05-19 alex * added test for HAVE_LONG_DOUBLE
71 67956560 2002-05-19 alex *
72 67956560 2002-05-19 alex * tridge@samba.org, idra@samba.org, April 2001
73 c5461c45 2002-05-19 alex * got rid of fcvt code (twas buggy and made testing harder)
74 c5461c45 2002-05-19 alex * added C99 semantics
75 c5461c45 2002-05-19 alex *
76 c5461c45 2002-05-19 alex * Alexander Barton, <alex@barton.de>, 2002-05-19
77 c5461c45 2002-05-19 alex * removed [v]asprintf() and C99 tests: not needed by ngIRCd.
78 67956560 2002-05-19 alex */
79 67956560 2002-05-19 alex
80 67956560 2002-05-19 alex
81 67956560 2002-05-19 alex #ifdef HAVE_STRING_H
82 67956560 2002-05-19 alex #include <string.h>
83 67956560 2002-05-19 alex #endif
84 67956560 2002-05-19 alex #ifdef HAVE_STRINGS_H
85 67956560 2002-05-19 alex #include <strings.h>
86 67956560 2002-05-19 alex #endif
87 67956560 2002-05-19 alex #ifdef HAVE_CTYPE_H
88 67956560 2002-05-19 alex #include <ctype.h>
89 67956560 2002-05-19 alex #endif
90 67956560 2002-05-19 alex #include <sys/types.h>
91 67956560 2002-05-19 alex #include <stdarg.h>
92 67956560 2002-05-19 alex #ifdef HAVE_STDLIB_H
93 67956560 2002-05-19 alex #include <stdlib.h>
94 67956560 2002-05-19 alex #endif
95 67956560 2002-05-19 alex
96 c5461c45 2002-05-19 alex
97 c5461c45 2002-05-19 alex #if defined(HAVE_SNPRINTF) && defined(HAVE_VSNPRINTF)
98 67956560 2002-05-19 alex /* only include stdio.h if we are not re-defining snprintf or vsnprintf */
99 67956560 2002-05-19 alex #include <stdio.h>
100 c5461c45 2002-05-19 alex /* make the compiler happy with an empty file */
101 ba258e65 2002-05-27 alex void dummy_snprintf PARAMS(( void )) { }
102 67956560 2002-05-19 alex #else
103 67956560 2002-05-19 alex
104 67956560 2002-05-19 alex #ifdef HAVE_LONG_DOUBLE
105 67956560 2002-05-19 alex #define LDOUBLE long double
106 67956560 2002-05-19 alex #else
107 67956560 2002-05-19 alex #define LDOUBLE double
108 67956560 2002-05-19 alex #endif
109 67956560 2002-05-19 alex
110 67956560 2002-05-19 alex #ifdef HAVE_LONG_LONG
111 67956560 2002-05-19 alex #define LLONG long long
112 67956560 2002-05-19 alex #else
113 67956560 2002-05-19 alex #define LLONG long
114 67956560 2002-05-19 alex #endif
115 67956560 2002-05-19 alex
116 67956560 2002-05-19 alex static size_t dopr(char *buffer, size_t maxlen, const char *format,
117 67956560 2002-05-19 alex va_list args);
118 67956560 2002-05-19 alex static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
119 67956560 2002-05-19 alex char *value, int flags, int min, int max);
120 67956560 2002-05-19 alex static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
121 67956560 2002-05-19 alex long value, int base, int min, int max, int flags);
122 67956560 2002-05-19 alex static void fmtfp(char *buffer, size_t *currlen, size_t maxlen,
123 67956560 2002-05-19 alex LDOUBLE fvalue, int min, int max, int flags);
124 67956560 2002-05-19 alex static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c);
125 67956560 2002-05-19 alex
126 67956560 2002-05-19 alex /*
127 67956560 2002-05-19 alex * dopr(): poor man's version of doprintf
128 67956560 2002-05-19 alex */
129 67956560 2002-05-19 alex
130 67956560 2002-05-19 alex /* format read states */
131 67956560 2002-05-19 alex #define DP_S_DEFAULT 0
132 67956560 2002-05-19 alex #define DP_S_FLAGS 1
133 67956560 2002-05-19 alex #define DP_S_MIN 2
134 67956560 2002-05-19 alex #define DP_S_DOT 3
135 67956560 2002-05-19 alex #define DP_S_MAX 4
136 67956560 2002-05-19 alex #define DP_S_MOD 5
137 67956560 2002-05-19 alex #define DP_S_CONV 6
138 67956560 2002-05-19 alex #define DP_S_DONE 7
139 67956560 2002-05-19 alex
140 67956560 2002-05-19 alex /* format flags - Bits */
141 67956560 2002-05-19 alex #define DP_F_MINUS (1 << 0)
142 67956560 2002-05-19 alex #define DP_F_PLUS (1 << 1)
143 67956560 2002-05-19 alex #define DP_F_SPACE (1 << 2)
144 67956560 2002-05-19 alex #define DP_F_NUM (1 << 3)
145 67956560 2002-05-19 alex #define DP_F_ZERO (1 << 4)
146 67956560 2002-05-19 alex #define DP_F_UP (1 << 5)
147 67956560 2002-05-19 alex #define DP_F_UNSIGNED (1 << 6)
148 67956560 2002-05-19 alex
149 67956560 2002-05-19 alex /* Conversion Flags */
150 67956560 2002-05-19 alex #define DP_C_SHORT 1
151 67956560 2002-05-19 alex #define DP_C_LONG 2
152 67956560 2002-05-19 alex #define DP_C_LDOUBLE 3
153 67956560 2002-05-19 alex #define DP_C_LLONG 4
154 67956560 2002-05-19 alex
155 67956560 2002-05-19 alex #define char_to_int(p) ((p)- '0')
156 67956560 2002-05-19 alex #ifndef MAX
157 67956560 2002-05-19 alex #define MAX(p,q) (((p) >= (q)) ? (p) : (q))
158 67956560 2002-05-19 alex #endif
159 67956560 2002-05-19 alex
160 67956560 2002-05-19 alex static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args)
161 67956560 2002-05-19 alex {
162 67956560 2002-05-19 alex char ch;
163 67956560 2002-05-19 alex LLONG value;
164 67956560 2002-05-19 alex LDOUBLE fvalue;
165 67956560 2002-05-19 alex char *strvalue;
166 67956560 2002-05-19 alex int min;
167 67956560 2002-05-19 alex int max;
168 67956560 2002-05-19 alex int state;
169 67956560 2002-05-19 alex int flags;
170 67956560 2002-05-19 alex int cflags;
171 67956560 2002-05-19 alex size_t currlen;
172 67956560 2002-05-19 alex
173 67956560 2002-05-19 alex state = DP_S_DEFAULT;
174 67956560 2002-05-19 alex currlen = flags = cflags = min = 0;
175 67956560 2002-05-19 alex max = -1;
176 67956560 2002-05-19 alex ch = *format++;
177 67956560 2002-05-19 alex
178 67956560 2002-05-19 alex while (state != DP_S_DONE) {
179 67956560 2002-05-19 alex if (ch == '\0')
180 67956560 2002-05-19 alex state = DP_S_DONE;
181 67956560 2002-05-19 alex
182 67956560 2002-05-19 alex switch(state) {
183 67956560 2002-05-19 alex case DP_S_DEFAULT:
184 67956560 2002-05-19 alex if (ch == '%')
185 67956560 2002-05-19 alex state = DP_S_FLAGS;
186 67956560 2002-05-19 alex else
187 67956560 2002-05-19 alex dopr_outch (buffer, &currlen, maxlen, ch);
188 67956560 2002-05-19 alex ch = *format++;
189 67956560 2002-05-19 alex break;
190 67956560 2002-05-19 alex case DP_S_FLAGS:
191 67956560 2002-05-19 alex switch (ch) {
192 67956560 2002-05-19 alex case '-':
193 67956560 2002-05-19 alex flags |= DP_F_MINUS;
194 67956560 2002-05-19 alex ch = *format++;
195 67956560 2002-05-19 alex break;
196 67956560 2002-05-19 alex case '+':
197 67956560 2002-05-19 alex flags |= DP_F_PLUS;
198 67956560 2002-05-19 alex ch = *format++;
199 67956560 2002-05-19 alex break;
200 67956560 2002-05-19 alex case ' ':
201 67956560 2002-05-19 alex flags |= DP_F_SPACE;
202 67956560 2002-05-19 alex ch = *format++;
203 67956560 2002-05-19 alex break;
204 67956560 2002-05-19 alex case '#':
205 67956560 2002-05-19 alex flags |= DP_F_NUM;
206 67956560 2002-05-19 alex ch = *format++;
207 67956560 2002-05-19 alex break;
208 67956560 2002-05-19 alex case '0':
209 67956560 2002-05-19 alex flags |= DP_F_ZERO;
210 67956560 2002-05-19 alex ch = *format++;
211 67956560 2002-05-19 alex break;
212 67956560 2002-05-19 alex default:
213 67956560 2002-05-19 alex state = DP_S_MIN;
214 67956560 2002-05-19 alex break;
215 67956560 2002-05-19 alex }
216 67956560 2002-05-19 alex break;
217 67956560 2002-05-19 alex case DP_S_MIN:
218 67956560 2002-05-19 alex if (isdigit((unsigned char)ch)) {
219 67956560 2002-05-19 alex min = 10*min + char_to_int (ch);
220 67956560 2002-05-19 alex ch = *format++;
221 67956560 2002-05-19 alex } else if (ch == '*') {
222 67956560 2002-05-19 alex min = va_arg (args, int);
223 67956560 2002-05-19 alex ch = *format++;
224 67956560 2002-05-19 alex state = DP_S_DOT;
225 67956560 2002-05-19 alex } else {
226 67956560 2002-05-19 alex state = DP_S_DOT;
227 67956560 2002-05-19 alex }
228 67956560 2002-05-19 alex break;
229 67956560 2002-05-19 alex case DP_S_DOT:
230 67956560 2002-05-19 alex if (ch == '.') {
231 67956560 2002-05-19 alex state = DP_S_MAX;
232 67956560 2002-05-19 alex ch = *format++;
233 67956560 2002-05-19 alex } else {
234 67956560 2002-05-19 alex state = DP_S_MOD;
235 67956560 2002-05-19 alex }
236 67956560 2002-05-19 alex break;
237 67956560 2002-05-19 alex case DP_S_MAX:
238 67956560 2002-05-19 alex if (isdigit((unsigned char)ch)) {
239 67956560 2002-05-19 alex if (max < 0)
240 67956560 2002-05-19 alex max = 0;
241 67956560 2002-05-19 alex max = 10*max + char_to_int (ch);
242 67956560 2002-05-19 alex ch = *format++;
243 67956560 2002-05-19 alex } else if (ch == '*') {
244 67956560 2002-05-19 alex max = va_arg (args, int);
245 67956560 2002-05-19 alex ch = *format++;
246 67956560 2002-05-19 alex state = DP_S_MOD;
247 67956560 2002-05-19 alex } else {
248 67956560 2002-05-19 alex state = DP_S_MOD;
249 67956560 2002-05-19 alex }
250 67956560 2002-05-19 alex break;
251 67956560 2002-05-19 alex case DP_S_MOD:
252 67956560 2002-05-19 alex switch (ch) {
253 67956560 2002-05-19 alex case 'h':
254 67956560 2002-05-19 alex cflags = DP_C_SHORT;
255 67956560 2002-05-19 alex ch = *format++;
256 67956560 2002-05-19 alex break;
257 67956560 2002-05-19 alex case 'l':
258 67956560 2002-05-19 alex cflags = DP_C_LONG;
259 67956560 2002-05-19 alex ch = *format++;
260 67956560 2002-05-19 alex if (ch == 'l') { /* It's a long long */
261 67956560 2002-05-19 alex cflags = DP_C_LLONG;
262 67956560 2002-05-19 alex ch = *format++;
263 67956560 2002-05-19 alex }
264 67956560 2002-05-19 alex break;
265 67956560 2002-05-19 alex case 'L':
266 67956560 2002-05-19 alex cflags = DP_C_LDOUBLE;
267 67956560 2002-05-19 alex ch = *format++;
268 67956560 2002-05-19 alex break;
269 67956560 2002-05-19 alex default:
270 67956560 2002-05-19 alex break;
271 67956560 2002-05-19 alex }
272 67956560 2002-05-19 alex state = DP_S_CONV;
273 67956560 2002-05-19 alex break;
274 67956560 2002-05-19 alex case DP_S_CONV:
275 67956560 2002-05-19 alex switch (ch) {
276 67956560 2002-05-19 alex case 'd':
277 67956560 2002-05-19 alex case 'i':
278 67956560 2002-05-19 alex if (cflags == DP_C_SHORT)
279 67956560 2002-05-19 alex value = va_arg (args, int);
280 67956560 2002-05-19 alex else if (cflags == DP_C_LONG)
281 67956560 2002-05-19 alex value = va_arg (args, long int);
282 67956560 2002-05-19 alex else if (cflags == DP_C_LLONG)
283 67956560 2002-05-19 alex value = va_arg (args, LLONG);
284 67956560 2002-05-19 alex else
285 67956560 2002-05-19 alex value = va_arg (args, int);
286 67956560 2002-05-19 alex fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
287 67956560 2002-05-19 alex break;
288 67956560 2002-05-19 alex case 'o':
289 67956560 2002-05-19 alex flags |= DP_F_UNSIGNED;
290 67956560 2002-05-19 alex if (cflags == DP_C_SHORT)
291 67956560 2002-05-19 alex value = va_arg (args, unsigned int);
292 67956560 2002-05-19 alex else if (cflags == DP_C_LONG)
293 67956560 2002-05-19 alex value = (long)va_arg (args, unsigned long int);
294 67956560 2002-05-19 alex else if (cflags == DP_C_LLONG)
295 67956560 2002-05-19 alex value = (long)va_arg (args, unsigned LLONG);
296 67956560 2002-05-19 alex else
297 67956560 2002-05-19 alex value = (long)va_arg (args, unsigned int);
298 67956560 2002-05-19 alex fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags);
299 67956560 2002-05-19 alex break;
300 67956560 2002-05-19 alex case 'u':
301 67956560 2002-05-19 alex flags |= DP_F_UNSIGNED;
302 67956560 2002-05-19 alex if (cflags == DP_C_SHORT)
303 67956560 2002-05-19 alex value = va_arg (args, unsigned int);
304 67956560 2002-05-19 alex else if (cflags == DP_C_LONG)
305 67956560 2002-05-19 alex value = (long)va_arg (args, unsigned long int);
306 67956560 2002-05-19 alex else if (cflags == DP_C_LLONG)
307 67956560 2002-05-19 alex value = (LLONG)va_arg (args, unsigned LLONG);
308 67956560 2002-05-19 alex else
309 67956560 2002-05-19 alex value = (long)va_arg (args, unsigned int);
310 67956560 2002-05-19 alex fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
311 67956560 2002-05-19 alex break;
312 67956560 2002-05-19 alex case 'X':
313 67956560 2002-05-19 alex flags |= DP_F_UP;
314 67956560 2002-05-19 alex case 'x':
315 67956560 2002-05-19 alex flags |= DP_F_UNSIGNED;
316 67956560 2002-05-19 alex if (cflags == DP_C_SHORT)
317 67956560 2002-05-19 alex value = va_arg (args, unsigned int);
318 67956560 2002-05-19 alex else if (cflags == DP_C_LONG)
319 67956560 2002-05-19 alex value = (long)va_arg (args, unsigned long int);
320 67956560 2002-05-19 alex else if (cflags == DP_C_LLONG)
321 67956560 2002-05-19 alex value = (LLONG)va_arg (args, unsigned LLONG);
322 67956560 2002-05-19 alex else
323 67956560 2002-05-19 alex value = (long)va_arg (args, unsigned int);
324 67956560 2002-05-19 alex fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags);
325 67956560 2002-05-19 alex break;
326 67956560 2002-05-19 alex case 'f':
327 67956560 2002-05-19 alex if (cflags == DP_C_LDOUBLE)
328 67956560 2002-05-19 alex fvalue = va_arg (args, LDOUBLE);
329 67956560 2002-05-19 alex else
330 67956560 2002-05-19 alex fvalue = va_arg (args, double);
331 67956560 2002-05-19 alex /* um, floating point? */
332 67956560 2002-05-19 alex fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
333 67956560 2002-05-19 alex break;
334 67956560 2002-05-19 alex case 'E':
335 67956560 2002-05-19 alex flags |= DP_F_UP;
336 67956560 2002-05-19 alex case 'e':
337 67956560 2002-05-19 alex if (cflags == DP_C_LDOUBLE)
338 67956560 2002-05-19 alex fvalue = va_arg (args, LDOUBLE);
339 67956560 2002-05-19 alex else
340 67956560 2002-05-19 alex fvalue = va_arg (args, double);
341 67956560 2002-05-19 alex break;
342 67956560 2002-05-19 alex case 'G':
343 67956560 2002-05-19 alex flags |= DP_F_UP;
344 67956560 2002-05-19 alex case 'g':
345 67956560 2002-05-19 alex if (cflags == DP_C_LDOUBLE)
346 67956560 2002-05-19 alex fvalue = va_arg (args, LDOUBLE);
347 67956560 2002-05-19 alex else
348 67956560 2002-05-19 alex fvalue = va_arg (args, double);
349 67956560 2002-05-19 alex break;
350 67956560 2002-05-19 alex case 'c':
351 67956560 2002-05-19 alex dopr_outch (buffer, &currlen, maxlen, va_arg (args, int));
352 67956560 2002-05-19 alex break;
353 67956560 2002-05-19 alex case 's':
354 67956560 2002-05-19 alex strvalue = va_arg (args, char *);
355 67956560 2002-05-19 alex if (max == -1) {
356 67956560 2002-05-19 alex max = strlen(strvalue);
357 67956560 2002-05-19 alex }
358 67956560 2002-05-19 alex if (min > 0 && max >= 0 && min > max) max = min;
359 67956560 2002-05-19 alex fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max);
360 67956560 2002-05-19 alex break;
361 67956560 2002-05-19 alex case 'p':
362 67956560 2002-05-19 alex strvalue = va_arg (args, void *);
363 67956560 2002-05-19 alex fmtint (buffer, &currlen, maxlen, (long) strvalue, 16, min, max, flags);
364 67956560 2002-05-19 alex break;
365 67956560 2002-05-19 alex case 'n':
366 67956560 2002-05-19 alex if (cflags == DP_C_SHORT) {
367 67956560 2002-05-19 alex short int *num;
368 67956560 2002-05-19 alex num = va_arg (args, short int *);
369 67956560 2002-05-19 alex *num = currlen;
370 67956560 2002-05-19 alex } else if (cflags == DP_C_LONG) {
371 67956560 2002-05-19 alex long int *num;
372 67956560 2002-05-19 alex num = va_arg (args, long int *);
373 67956560 2002-05-19 alex *num = (long int)currlen;
374 67956560 2002-05-19 alex } else if (cflags == DP_C_LLONG) {
375 67956560 2002-05-19 alex LLONG *num;
376 67956560 2002-05-19 alex num = va_arg (args, LLONG *);
377 67956560 2002-05-19 alex *num = (LLONG)currlen;
378 67956560 2002-05-19 alex } else {
379 67956560 2002-05-19 alex int *num;
380 67956560 2002-05-19 alex num = va_arg (args, int *);
381 67956560 2002-05-19 alex *num = currlen;
382 67956560 2002-05-19 alex }
383 67956560 2002-05-19 alex break;
384 67956560 2002-05-19 alex case '%':
385 67956560 2002-05-19 alex dopr_outch (buffer, &currlen, maxlen, ch);
386 67956560 2002-05-19 alex break;
387 67956560 2002-05-19 alex case 'w':
388 67956560 2002-05-19 alex /* not supported yet, treat as next char */
389 67956560 2002-05-19 alex ch = *format++;
390 67956560 2002-05-19 alex break;
391 67956560 2002-05-19 alex default:
392 67956560 2002-05-19 alex /* Unknown, skip */
393 67956560 2002-05-19 alex break;
394 67956560 2002-05-19 alex }
395 67956560 2002-05-19 alex ch = *format++;
396 67956560 2002-05-19 alex state = DP_S_DEFAULT;
397 67956560 2002-05-19 alex flags = cflags = min = 0;
398 67956560 2002-05-19 alex max = -1;
399 67956560 2002-05-19 alex break;
400 67956560 2002-05-19 alex case DP_S_DONE:
401 67956560 2002-05-19 alex break;
402 67956560 2002-05-19 alex default:
403 67956560 2002-05-19 alex /* hmm? */
404 67956560 2002-05-19 alex break; /* some picky compilers need this */
405 67956560 2002-05-19 alex }
406 67956560 2002-05-19 alex }
407 67956560 2002-05-19 alex if (maxlen != 0) {
408 67956560 2002-05-19 alex if (currlen < maxlen - 1)
409 67956560 2002-05-19 alex buffer[currlen] = '\0';
410 67956560 2002-05-19 alex else if (maxlen > 0)
411 67956560 2002-05-19 alex buffer[maxlen - 1] = '\0';
412 67956560 2002-05-19 alex }
413 67956560 2002-05-19 alex
414 67956560 2002-05-19 alex return currlen;
415 67956560 2002-05-19 alex }
416 67956560 2002-05-19 alex
417 67956560 2002-05-19 alex static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
418 67956560 2002-05-19 alex char *value, int flags, int min, int max)
419 67956560 2002-05-19 alex {
420 67956560 2002-05-19 alex int padlen, strln; /* amount to pad */
421 67956560 2002-05-19 alex int cnt = 0;
422 67956560 2002-05-19 alex
423 67956560 2002-05-19 alex #ifdef DEBUG_SNPRINTF
424 67956560 2002-05-19 alex printf("fmtstr min=%d max=%d s=[%s]\n", min, max, value);
425 67956560 2002-05-19 alex #endif
426 67956560 2002-05-19 alex if (value == 0) {
427 67956560 2002-05-19 alex value = "<NULL>";
428 67956560 2002-05-19 alex }
429 67956560 2002-05-19 alex
430 67956560 2002-05-19 alex for (strln = 0; value[strln]; ++strln); /* strlen */
431 67956560 2002-05-19 alex padlen = min - strln;
432 67956560 2002-05-19 alex if (padlen < 0)
433 67956560 2002-05-19 alex padlen = 0;
434 67956560 2002-05-19 alex if (flags & DP_F_MINUS)
435 67956560 2002-05-19 alex padlen = -padlen; /* Left Justify */
436 67956560 2002-05-19 alex
437 67956560 2002-05-19 alex while ((padlen > 0) && (cnt < max)) {
438 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, ' ');
439 67956560 2002-05-19 alex --padlen;
440 67956560 2002-05-19 alex ++cnt;
441 67956560 2002-05-19 alex }
442 67956560 2002-05-19 alex while (*value && (cnt < max)) {
443 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, *value++);
444 67956560 2002-05-19 alex ++cnt;
445 67956560 2002-05-19 alex }
446 67956560 2002-05-19 alex while ((padlen < 0) && (cnt < max)) {
447 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, ' ');
448 67956560 2002-05-19 alex ++padlen;
449 67956560 2002-05-19 alex ++cnt;
450 67956560 2002-05-19 alex }
451 67956560 2002-05-19 alex }
452 67956560 2002-05-19 alex
453 67956560 2002-05-19 alex /* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
454 67956560 2002-05-19 alex
455 67956560 2002-05-19 alex static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
456 67956560 2002-05-19 alex long value, int base, int min, int max, int flags)
457 67956560 2002-05-19 alex {
458 67956560 2002-05-19 alex int signvalue = 0;
459 67956560 2002-05-19 alex unsigned long uvalue;
460 67956560 2002-05-19 alex char convert[20];
461 67956560 2002-05-19 alex int place = 0;
462 67956560 2002-05-19 alex int spadlen = 0; /* amount to space pad */
463 67956560 2002-05-19 alex int zpadlen = 0; /* amount to zero pad */
464 67956560 2002-05-19 alex int caps = 0;
465 67956560 2002-05-19 alex
466 67956560 2002-05-19 alex if (max < 0)
467 67956560 2002-05-19 alex max = 0;
468 67956560 2002-05-19 alex
469 67956560 2002-05-19 alex uvalue = value;
470 67956560 2002-05-19 alex
471 67956560 2002-05-19 alex if(!(flags & DP_F_UNSIGNED)) {
472 67956560 2002-05-19 alex if( value < 0 ) {
473 67956560 2002-05-19 alex signvalue = '-';
474 67956560 2002-05-19 alex uvalue = -value;
475 67956560 2002-05-19 alex } else {
476 67956560 2002-05-19 alex if (flags & DP_F_PLUS) /* Do a sign (+/i) */
477 67956560 2002-05-19 alex signvalue = '+';
478 67956560 2002-05-19 alex else if (flags & DP_F_SPACE)
479 67956560 2002-05-19 alex signvalue = ' ';
480 67956560 2002-05-19 alex }
481 67956560 2002-05-19 alex }
482 67956560 2002-05-19 alex
483 67956560 2002-05-19 alex if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
484 67956560 2002-05-19 alex
485 67956560 2002-05-19 alex do {
486 67956560 2002-05-19 alex convert[place++] =
487 67956560 2002-05-19 alex (caps? "0123456789ABCDEF":"0123456789abcdef")
488 67956560 2002-05-19 alex [uvalue % (unsigned)base ];
489 67956560 2002-05-19 alex uvalue = (uvalue / (unsigned)base );
490 67956560 2002-05-19 alex } while(uvalue && (place < 20));
491 67956560 2002-05-19 alex if (place == 20) place--;
492 67956560 2002-05-19 alex convert[place] = 0;
493 67956560 2002-05-19 alex
494 67956560 2002-05-19 alex zpadlen = max - place;
495 67956560 2002-05-19 alex spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
496 67956560 2002-05-19 alex if (zpadlen < 0) zpadlen = 0;
497 67956560 2002-05-19 alex if (spadlen < 0) spadlen = 0;
498 67956560 2002-05-19 alex if (flags & DP_F_ZERO) {
499 67956560 2002-05-19 alex zpadlen = MAX(zpadlen, spadlen);
500 67956560 2002-05-19 alex spadlen = 0;
501 67956560 2002-05-19 alex }
502 67956560 2002-05-19 alex if (flags & DP_F_MINUS)
503 67956560 2002-05-19 alex spadlen = -spadlen; /* Left Justifty */
504 67956560 2002-05-19 alex
505 67956560 2002-05-19 alex #ifdef DEBUG_SNPRINTF
506 67956560 2002-05-19 alex printf("zpad: %d, spad: %d, min: %d, max: %d, place: %d\n",
507 67956560 2002-05-19 alex zpadlen, spadlen, min, max, place);
508 67956560 2002-05-19 alex #endif
509 67956560 2002-05-19 alex
510 67956560 2002-05-19 alex /* Spaces */
511 67956560 2002-05-19 alex while (spadlen > 0) {
512 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, ' ');
513 67956560 2002-05-19 alex --spadlen;
514 67956560 2002-05-19 alex }
515 67956560 2002-05-19 alex
516 67956560 2002-05-19 alex /* Sign */
517 67956560 2002-05-19 alex if (signvalue)
518 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, signvalue);
519 67956560 2002-05-19 alex
520 67956560 2002-05-19 alex /* Zeros */
521 67956560 2002-05-19 alex if (zpadlen > 0) {
522 67956560 2002-05-19 alex while (zpadlen > 0) {
523 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, '0');
524 67956560 2002-05-19 alex --zpadlen;
525 67956560 2002-05-19 alex }
526 67956560 2002-05-19 alex }
527 67956560 2002-05-19 alex
528 67956560 2002-05-19 alex /* Digits */
529 67956560 2002-05-19 alex while (place > 0)
530 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, convert[--place]);
531 67956560 2002-05-19 alex
532 67956560 2002-05-19 alex /* Left Justified spaces */
533 67956560 2002-05-19 alex while (spadlen < 0) {
534 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, ' ');
535 67956560 2002-05-19 alex ++spadlen;
536 67956560 2002-05-19 alex }
537 67956560 2002-05-19 alex }
538 67956560 2002-05-19 alex
539 67956560 2002-05-19 alex static LDOUBLE abs_val(LDOUBLE value)
540 67956560 2002-05-19 alex {
541 67956560 2002-05-19 alex LDOUBLE result = value;
542 67956560 2002-05-19 alex
543 67956560 2002-05-19 alex if (value < 0)
544 67956560 2002-05-19 alex result = -value;
545 67956560 2002-05-19 alex
546 67956560 2002-05-19 alex return result;
547 67956560 2002-05-19 alex }
548 67956560 2002-05-19 alex
549 67956560 2002-05-19 alex static LDOUBLE POW10(int exp)
550 67956560 2002-05-19 alex {
551 67956560 2002-05-19 alex LDOUBLE result = 1;
552 67956560 2002-05-19 alex
553 67956560 2002-05-19 alex while (exp) {
554 67956560 2002-05-19 alex result *= 10;
555 67956560 2002-05-19 alex exp--;
556 67956560 2002-05-19 alex }
557 67956560 2002-05-19 alex
558 67956560 2002-05-19 alex return result;
559 67956560 2002-05-19 alex }
560 67956560 2002-05-19 alex
561 67956560 2002-05-19 alex static LLONG ROUND(LDOUBLE value)
562 67956560 2002-05-19 alex {
563 67956560 2002-05-19 alex LLONG intpart;
564 67956560 2002-05-19 alex
565 67956560 2002-05-19 alex intpart = (LLONG)value;
566 67956560 2002-05-19 alex value = value - intpart;
567 67956560 2002-05-19 alex if (value >= 0.5) intpart++;
568 67956560 2002-05-19 alex
569 67956560 2002-05-19 alex return intpart;
570 67956560 2002-05-19 alex }
571 67956560 2002-05-19 alex
572 67956560 2002-05-19 alex /* a replacement for modf that doesn't need the math library. Should
573 67956560 2002-05-19 alex be portable, but slow */
574 67956560 2002-05-19 alex static double my_modf(double x0, double *iptr)
575 67956560 2002-05-19 alex {
576 67956560 2002-05-19 alex int i;
577 67956560 2002-05-19 alex long l;
578 67956560 2002-05-19 alex double x = x0;
579 67956560 2002-05-19 alex double f = 1.0;
580 67956560 2002-05-19 alex
581 67956560 2002-05-19 alex for (i=0;i<100;i++) {
582 67956560 2002-05-19 alex l = (long)x;
583 67956560 2002-05-19 alex if (l <= (x+1) && l >= (x-1)) break;
584 67956560 2002-05-19 alex x *= 0.1;
585 67956560 2002-05-19 alex f *= 10.0;
586 67956560 2002-05-19 alex }
587 67956560 2002-05-19 alex
588 67956560 2002-05-19 alex if (i == 100) {
589 67956560 2002-05-19 alex /* yikes! the number is beyond what we can handle. What do we do? */
590 67956560 2002-05-19 alex (*iptr) = 0;
591 67956560 2002-05-19 alex return 0;
592 67956560 2002-05-19 alex }
593 67956560 2002-05-19 alex
594 67956560 2002-05-19 alex if (i != 0) {
595 67956560 2002-05-19 alex double i2;
596 67956560 2002-05-19 alex double ret;
597 67956560 2002-05-19 alex
598 67956560 2002-05-19 alex ret = my_modf(x0-l*f, &i2);
599 67956560 2002-05-19 alex (*iptr) = l*f + i2;
600 67956560 2002-05-19 alex return ret;
601 67956560 2002-05-19 alex }
602 67956560 2002-05-19 alex
603 67956560 2002-05-19 alex (*iptr) = l;
604 67956560 2002-05-19 alex return x - (*iptr);
605 67956560 2002-05-19 alex }
606 67956560 2002-05-19 alex
607 67956560 2002-05-19 alex
608 67956560 2002-05-19 alex static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
609 67956560 2002-05-19 alex LDOUBLE fvalue, int min, int max, int flags)
610 67956560 2002-05-19 alex {
611 67956560 2002-05-19 alex int signvalue = 0;
612 67956560 2002-05-19 alex double ufvalue;
613 67956560 2002-05-19 alex char iconvert[311];
614 67956560 2002-05-19 alex char fconvert[311];
615 67956560 2002-05-19 alex int iplace = 0;
616 67956560 2002-05-19 alex int fplace = 0;
617 67956560 2002-05-19 alex int padlen = 0; /* amount to pad */
618 67956560 2002-05-19 alex int zpadlen = 0;
619 67956560 2002-05-19 alex int caps = 0;
620 67956560 2002-05-19 alex int index;
621 67956560 2002-05-19 alex double intpart;
622 67956560 2002-05-19 alex double fracpart;
623 67956560 2002-05-19 alex double temp;
624 67956560 2002-05-19 alex
625 67956560 2002-05-19 alex /*
626 67956560 2002-05-19 alex * AIX manpage says the default is 0, but Solaris says the default
627 67956560 2002-05-19 alex * is 6, and sprintf on AIX defaults to 6
628 67956560 2002-05-19 alex */
629 67956560 2002-05-19 alex if (max < 0)
630 67956560 2002-05-19 alex max = 6;
631 67956560 2002-05-19 alex
632 67956560 2002-05-19 alex ufvalue = abs_val (fvalue);
633 67956560 2002-05-19 alex
634 67956560 2002-05-19 alex if (fvalue < 0) {
635 67956560 2002-05-19 alex signvalue = '-';
636 67956560 2002-05-19 alex } else {
637 67956560 2002-05-19 alex if (flags & DP_F_PLUS) { /* Do a sign (+/i) */
638 67956560 2002-05-19 alex signvalue = '+';
639 67956560 2002-05-19 alex } else {
640 67956560 2002-05-19 alex if (flags & DP_F_SPACE)
641 67956560 2002-05-19 alex signvalue = ' ';
642 67956560 2002-05-19 alex }
643 67956560 2002-05-19 alex }
644 67956560 2002-05-19 alex
645 67956560 2002-05-19 alex #if 0
646 67956560 2002-05-19 alex if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
647 67956560 2002-05-19 alex #endif
648 67956560 2002-05-19 alex
649 67956560 2002-05-19 alex #if 0
650 c5461c45 2002-05-19 alex if (max == 0) ufvalue += 0.5; /* if max = 0 we must round */
651 67956560 2002-05-19 alex #endif
652 67956560 2002-05-19 alex
653 67956560 2002-05-19 alex /*
654 67956560 2002-05-19 alex * Sorry, we only support 16 digits past the decimal because of our
655 67956560 2002-05-19 alex * conversion method
656 67956560 2002-05-19 alex */
657 67956560 2002-05-19 alex if (max > 16)
658 67956560 2002-05-19 alex max = 16;
659 67956560 2002-05-19 alex
660 67956560 2002-05-19 alex /* We "cheat" by converting the fractional part to integer by
661 67956560 2002-05-19 alex * multiplying by a factor of 10
662 67956560 2002-05-19 alex */
663 67956560 2002-05-19 alex
664 67956560 2002-05-19 alex temp = ufvalue;
665 67956560 2002-05-19 alex my_modf(temp, &intpart);
666 67956560 2002-05-19 alex
667 67956560 2002-05-19 alex fracpart = ROUND((POW10(max)) * (ufvalue - intpart));
668 67956560 2002-05-19 alex
669 67956560 2002-05-19 alex if (fracpart >= POW10(max)) {
670 67956560 2002-05-19 alex intpart++;
671 67956560 2002-05-19 alex fracpart -= POW10(max);
672 67956560 2002-05-19 alex }
673 67956560 2002-05-19 alex
674 67956560 2002-05-19 alex
675 67956560 2002-05-19 alex /* Convert integer part */
676 67956560 2002-05-19 alex do {
677 67956560 2002-05-19 alex temp = intpart;
678 67956560 2002-05-19 alex my_modf(intpart*0.1, &intpart);
679 67956560 2002-05-19 alex temp = temp*0.1;
680 67956560 2002-05-19 alex index = (int) ((temp -intpart +0.05)* 10.0);
681 67956560 2002-05-19 alex /* index = (int) (((double)(temp*0.1) -intpart +0.05) *10.0); */
682 67956560 2002-05-19 alex /* printf ("%llf, %f, %x\n", temp, intpart, index); */
683 67956560 2002-05-19 alex iconvert[iplace++] =
684 67956560 2002-05-19 alex (caps? "0123456789ABCDEF":"0123456789abcdef")[index];
685 67956560 2002-05-19 alex } while (intpart && (iplace < 311));
686 67956560 2002-05-19 alex if (iplace == 311) iplace--;
687 67956560 2002-05-19 alex iconvert[iplace] = 0;
688 67956560 2002-05-19 alex
689 67956560 2002-05-19 alex /* Convert fractional part */
690 67956560 2002-05-19 alex if (fracpart)
691 67956560 2002-05-19 alex {
692 67956560 2002-05-19 alex do {
693 67956560 2002-05-19 alex temp = fracpart;
694 67956560 2002-05-19 alex my_modf(fracpart*0.1, &fracpart);
695 67956560 2002-05-19 alex temp = temp*0.1;
696 67956560 2002-05-19 alex index = (int) ((temp -fracpart +0.05)* 10.0);
697 67956560 2002-05-19 alex /* index = (int) ((((temp/10) -fracpart) +0.05) *10); */
698 67956560 2002-05-19 alex /* printf ("%lf, %lf, %ld\n", temp, fracpart, index); */
699 67956560 2002-05-19 alex fconvert[fplace++] =
700 67956560 2002-05-19 alex (caps? "0123456789ABCDEF":"0123456789abcdef")[index];
701 67956560 2002-05-19 alex } while(fracpart && (fplace < 311));
702 67956560 2002-05-19 alex if (fplace == 311) fplace--;
703 67956560 2002-05-19 alex }
704 67956560 2002-05-19 alex fconvert[fplace] = 0;
705 67956560 2002-05-19 alex
706 67956560 2002-05-19 alex /* -1 for decimal point, another -1 if we are printing a sign */
707 67956560 2002-05-19 alex padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
708 67956560 2002-05-19 alex zpadlen = max - fplace;
709 67956560 2002-05-19 alex if (zpadlen < 0) zpadlen = 0;
710 67956560 2002-05-19 alex if (padlen < 0)
711 67956560 2002-05-19 alex padlen = 0;
712 67956560 2002-05-19 alex if (flags & DP_F_MINUS)
713 67956560 2002-05-19 alex padlen = -padlen; /* Left Justifty */
714 67956560 2002-05-19 alex
715 67956560 2002-05-19 alex if ((flags & DP_F_ZERO) && (padlen > 0)) {
716 67956560 2002-05-19 alex if (signvalue) {
717 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, signvalue);
718 67956560 2002-05-19 alex --padlen;
719 67956560 2002-05-19 alex signvalue = 0;
720 67956560 2002-05-19 alex }
721 67956560 2002-05-19 alex while (padlen > 0) {
722 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, '0');
723 67956560 2002-05-19 alex --padlen;
724 67956560 2002-05-19 alex }
725 67956560 2002-05-19 alex }
726 67956560 2002-05-19 alex while (padlen > 0) {
727 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, ' ');
728 67956560 2002-05-19 alex --padlen;
729 67956560 2002-05-19 alex }
730 67956560 2002-05-19 alex if (signvalue)
731 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, signvalue);
732 67956560 2002-05-19 alex
733 67956560 2002-05-19 alex while (iplace > 0)
734 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]);
735 67956560 2002-05-19 alex
736 67956560 2002-05-19 alex #ifdef DEBUG_SNPRINTF
737 67956560 2002-05-19 alex printf("fmtfp: fplace=%d zpadlen=%d\n", fplace, zpadlen);
738 67956560 2002-05-19 alex #endif
739 67956560 2002-05-19 alex
740 67956560 2002-05-19 alex /*
741 67956560 2002-05-19 alex * Decimal point. This should probably use locale to find the correct
742 67956560 2002-05-19 alex * char to print out.
743 67956560 2002-05-19 alex */
744 67956560 2002-05-19 alex if (max > 0) {
745 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, '.');
746 67956560 2002-05-19 alex
747 67956560 2002-05-19 alex while (fplace > 0)
748 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]);
749 67956560 2002-05-19 alex }
750 67956560 2002-05-19 alex
751 67956560 2002-05-19 alex while (zpadlen > 0) {
752 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, '0');
753 67956560 2002-05-19 alex --zpadlen;
754 67956560 2002-05-19 alex }
755 67956560 2002-05-19 alex
756 67956560 2002-05-19 alex while (padlen < 0) {
757 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, ' ');
758 67956560 2002-05-19 alex ++padlen;
759 67956560 2002-05-19 alex }
760 67956560 2002-05-19 alex }
761 67956560 2002-05-19 alex
762 67956560 2002-05-19 alex static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c)
763 67956560 2002-05-19 alex {
764 67956560 2002-05-19 alex if (*currlen < maxlen) {
765 67956560 2002-05-19 alex buffer[(*currlen)] = c;
766 67956560 2002-05-19 alex }
767 67956560 2002-05-19 alex (*currlen)++;
768 67956560 2002-05-19 alex }
769 67956560 2002-05-19 alex
770 c5461c45 2002-05-19 alex #if !defined(HAVE_VSNPRINTF)
771 c5461c45 2002-05-19 alex int vsnprintf (char *str, size_t count, const char *fmt, va_list args)
772 67956560 2002-05-19 alex {
773 67956560 2002-05-19 alex return dopr(str, count, fmt, args);
774 67956560 2002-05-19 alex }
775 67956560 2002-05-19 alex #endif
776 67956560 2002-05-19 alex
777 c5461c45 2002-05-19 alex #if !defined(HAVE_SNPRINTF)
778 c5461c45 2002-05-19 alex int snprintf(char *str,size_t count,const char *fmt,...)
779 67956560 2002-05-19 alex {
780 67956560 2002-05-19 alex size_t ret;
781 67956560 2002-05-19 alex va_list ap;
782 67956560 2002-05-19 alex
783 67956560 2002-05-19 alex va_start(ap, fmt);
784 67956560 2002-05-19 alex ret = vsnprintf(str, count, fmt, ap);
785 67956560 2002-05-19 alex va_end(ap);
786 67956560 2002-05-19 alex return ret;
787 67956560 2002-05-19 alex }
788 67956560 2002-05-19 alex #endif
789 67956560 2002-05-19 alex
790 67956560 2002-05-19 alex #endif
791 67956560 2002-05-19 alex
792 67956560 2002-05-19 alex
793 67956560 2002-05-19 alex #ifdef TEST_SNPRINTF
794 67956560 2002-05-19 alex int sprintf(char *str,const char *fmt,...);
795 67956560 2002-05-19 alex int main (void)
796 67956560 2002-05-19 alex {
797 67956560 2002-05-19 alex char buf1[1024];
798 67956560 2002-05-19 alex char buf2[1024];
799 67956560 2002-05-19 alex char *fp_fmt[] = {
800 67956560 2002-05-19 alex "%1.1f",
801 67956560 2002-05-19 alex "%-1.5f",
802 67956560 2002-05-19 alex "%1.5f",
803 67956560 2002-05-19 alex "%123.9f",
804 67956560 2002-05-19 alex "%10.5f",
805 67956560 2002-05-19 alex "% 10.5f",
806 67956560 2002-05-19 alex "%+22.9f",
807 67956560 2002-05-19 alex "%+4.9f",
808 67956560 2002-05-19 alex "%01.3f",
809 67956560 2002-05-19 alex "%4f",
810 67956560 2002-05-19 alex "%3.1f",
811 67956560 2002-05-19 alex "%3.2f",
812 67956560 2002-05-19 alex "%.0f",
813 67956560 2002-05-19 alex "%f",
814 67956560 2002-05-19 alex "-16.16f",
815 67956560 2002-05-19 alex NULL
816 67956560 2002-05-19 alex };
817 67956560 2002-05-19 alex double fp_nums[] = { 6442452944.1234, -1.5, 134.21, 91340.2, 341.1234, 0203.9, 0.96, 0.996,
818 67956560 2002-05-19 alex 0.9996, 1.996, 4.136, 0};
819 67956560 2002-05-19 alex char *int_fmt[] = {
820 67956560 2002-05-19 alex "%-1.5d",
821 67956560 2002-05-19 alex "%1.5d",
822 67956560 2002-05-19 alex "%123.9d",
823 67956560 2002-05-19 alex "%5.5d",
824 67956560 2002-05-19 alex "%10.5d",
825 67956560 2002-05-19 alex "% 10.5d",
826 67956560 2002-05-19 alex "%+22.33d",
827 67956560 2002-05-19 alex "%01.3d",
828 67956560 2002-05-19 alex "%4d",
829 67956560 2002-05-19 alex "%d",
830 67956560 2002-05-19 alex NULL
831 67956560 2002-05-19 alex };
832 67956560 2002-05-19 alex long int_nums[] = { -1, 134, 91340, 341, 0203, 0};
833 67956560 2002-05-19 alex char *str_fmt[] = {
834 67956560 2002-05-19 alex "10.5s",
835 67956560 2002-05-19 alex "5.10s",
836 67956560 2002-05-19 alex "10.1s",
837 67956560 2002-05-19 alex "0.10s",
838 67956560 2002-05-19 alex "10.0s",
839 67956560 2002-05-19 alex "1.10s",
840 67956560 2002-05-19 alex "%s",
841 67956560 2002-05-19 alex "%.1s",
842 67956560 2002-05-19 alex "%.10s",
843 67956560 2002-05-19 alex "%10s",
844 67956560 2002-05-19 alex NULL
845 67956560 2002-05-19 alex };
846 67956560 2002-05-19 alex char *str_vals[] = {"hello", "a", "", "a longer string", NULL};
847 67956560 2002-05-19 alex int x, y;
848 67956560 2002-05-19 alex int fail = 0;
849 67956560 2002-05-19 alex int num = 0;
850 67956560 2002-05-19 alex
851 67956560 2002-05-19 alex printf ("Testing snprintf format codes against system sprintf...\n");
852 67956560 2002-05-19 alex
853 67956560 2002-05-19 alex for (x = 0; fp_fmt[x] ; x++) {
854 67956560 2002-05-19 alex for (y = 0; fp_nums[y] != 0 ; y++) {
855 67956560 2002-05-19 alex int l1 = snprintf(NULL, 0, fp_fmt[x], fp_nums[y]);
856 67956560 2002-05-19 alex int l2 = snprintf(buf1, sizeof(buf1), fp_fmt[x], fp_nums[y]);
857 67956560 2002-05-19 alex sprintf (buf2, fp_fmt[x], fp_nums[y]);
858 67956560 2002-05-19 alex if (strcmp (buf1, buf2)) {
859 67956560 2002-05-19 alex printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n",
860 67956560 2002-05-19 alex fp_fmt[x], buf1, buf2);
861 67956560 2002-05-19 alex fail++;
862 67956560 2002-05-19 alex }
863 67956560 2002-05-19 alex if (l1 != l2) {
864 67956560 2002-05-19 alex printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, fp_fmt[x]);
865 67956560 2002-05-19 alex fail++;
866 67956560 2002-05-19 alex }
867 67956560 2002-05-19 alex num++;
868 67956560 2002-05-19 alex }
869 67956560 2002-05-19 alex }
870 67956560 2002-05-19 alex
871 67956560 2002-05-19 alex for (x = 0; int_fmt[x] ; x++) {
872 67956560 2002-05-19 alex for (y = 0; int_nums[y] != 0 ; y++) {
873 67956560 2002-05-19 alex int l1 = snprintf(NULL, 0, int_fmt[x], int_nums[y]);
874 67956560 2002-05-19 alex int l2 = snprintf(buf1, sizeof(buf1), int_fmt[x], int_nums[y]);
875 67956560 2002-05-19 alex sprintf (buf2, int_fmt[x], int_nums[y]);
876 67956560 2002-05-19 alex if (strcmp (buf1, buf2)) {
877 67956560 2002-05-19 alex printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n",
878 67956560 2002-05-19 alex int_fmt[x], buf1, buf2);
879 67956560 2002-05-19 alex fail++;
880 67956560 2002-05-19 alex }
881 67956560 2002-05-19 alex if (l1 != l2) {
882 67956560 2002-05-19 alex printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, int_fmt[x]);
883 67956560 2002-05-19 alex fail++;
884 67956560 2002-05-19 alex }
885 67956560 2002-05-19 alex num++;
886 67956560 2002-05-19 alex }
887 67956560 2002-05-19 alex }
888 67956560 2002-05-19 alex
889 67956560 2002-05-19 alex for (x = 0; str_fmt[x] ; x++) {
890 67956560 2002-05-19 alex for (y = 0; str_vals[y] != 0 ; y++) {
891 67956560 2002-05-19 alex int l1 = snprintf(NULL, 0, str_fmt[x], str_vals[y]);
892 67956560 2002-05-19 alex int l2 = snprintf(buf1, sizeof(buf1), str_fmt[x], str_vals[y]);
893 67956560 2002-05-19 alex sprintf (buf2, str_fmt[x], str_vals[y]);
894 67956560 2002-05-19 alex if (strcmp (buf1, buf2)) {
895 67956560 2002-05-19 alex printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n",
896 67956560 2002-05-19 alex str_fmt[x], buf1, buf2);
897 67956560 2002-05-19 alex fail++;
898 67956560 2002-05-19 alex }
899 67956560 2002-05-19 alex if (l1 != l2) {
900 67956560 2002-05-19 alex printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, str_fmt[x]);
901 67956560 2002-05-19 alex fail++;
902 67956560 2002-05-19 alex }
903 67956560 2002-05-19 alex num++;
904 67956560 2002-05-19 alex }
905 67956560 2002-05-19 alex }
906 67956560 2002-05-19 alex
907 67956560 2002-05-19 alex printf ("%d tests failed out of %d.\n", fail, num);
908 67956560 2002-05-19 alex
909 67956560 2002-05-19 alex printf("seeing how many digits we support\n");
910 67956560 2002-05-19 alex {
911 67956560 2002-05-19 alex double v0 = 0.12345678901234567890123456789012345678901;
912 67956560 2002-05-19 alex for (x=0; x<100; x++) {
913 67956560 2002-05-19 alex snprintf(buf1, sizeof(buf1), "%1.1f", v0*pow(10, x));
914 67956560 2002-05-19 alex sprintf(buf2, "%1.1f", v0*pow(10, x));
915 67956560 2002-05-19 alex if (strcmp(buf1, buf2)) {
916 67956560 2002-05-19 alex printf("we seem to support %d digits\n", x-1);
917 67956560 2002-05-19 alex break;
918 67956560 2002-05-19 alex }
919 67956560 2002-05-19 alex }
920 67956560 2002-05-19 alex }
921 67956560 2002-05-19 alex return 0;
922 67956560 2002-05-19 alex }
923 67956560 2002-05-19 alex #endif /* SNPRINTF_TEST */
924 67956560 2002-05-19 alex
925 67956560 2002-05-19 alex
926 67956560 2002-05-19 alex /* -eof- */