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 f0d633b5 2002-12-12 alex * This program is free software; you can redistribute it and/or modify
6 f0d633b5 2002-12-12 alex * it under the terms of the GNU General Public License as published by
7 f0d633b5 2002-12-12 alex * the Free Software Foundation; either version 2 of the License, or
8 f0d633b5 2002-12-12 alex * (at your option) any later version.
9 f0d633b5 2002-12-12 alex * Please read the file COPYING, README and AUTHORS for more information.
10 67956560 2002-05-19 alex *
11 f0d633b5 2002-12-12 alex * snprintf() and vsnprintf() replacement functions
12 67956560 2002-05-19 alex */
13 67956560 2002-05-19 alex
14 67956560 2002-05-19 alex
15 67956560 2002-05-19 alex #include "portab.h"
16 f0d633b5 2002-12-12 alex
17 daa996f1 2003-04-21 alex static char UNUSED id[] = "$Id: vsnprintf.c,v 1.5 2003/04/21 10:53:38 alex Exp $";
18 f0d633b5 2002-12-12 alex
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 daa996f1 2003-04-21 alex void dummy_snprintf PARAMS(( void ));
102 ba258e65 2002-05-27 alex void dummy_snprintf PARAMS(( void )) { }
103 67956560 2002-05-19 alex #else
104 67956560 2002-05-19 alex
105 67956560 2002-05-19 alex #ifdef HAVE_LONG_DOUBLE
106 67956560 2002-05-19 alex #define LDOUBLE long double
107 67956560 2002-05-19 alex #else
108 67956560 2002-05-19 alex #define LDOUBLE double
109 67956560 2002-05-19 alex #endif
110 67956560 2002-05-19 alex
111 67956560 2002-05-19 alex #ifdef HAVE_LONG_LONG
112 67956560 2002-05-19 alex #define LLONG long long
113 67956560 2002-05-19 alex #else
114 67956560 2002-05-19 alex #define LLONG long
115 67956560 2002-05-19 alex #endif
116 67956560 2002-05-19 alex
117 67956560 2002-05-19 alex static size_t dopr(char *buffer, size_t maxlen, const char *format,
118 67956560 2002-05-19 alex va_list args);
119 67956560 2002-05-19 alex static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
120 67956560 2002-05-19 alex char *value, int flags, int min, int max);
121 67956560 2002-05-19 alex static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
122 67956560 2002-05-19 alex long value, int base, int min, int max, int flags);
123 67956560 2002-05-19 alex static void fmtfp(char *buffer, size_t *currlen, size_t maxlen,
124 67956560 2002-05-19 alex LDOUBLE fvalue, int min, int max, int flags);
125 67956560 2002-05-19 alex static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c);
126 67956560 2002-05-19 alex
127 67956560 2002-05-19 alex /*
128 67956560 2002-05-19 alex * dopr(): poor man's version of doprintf
129 67956560 2002-05-19 alex */
130 67956560 2002-05-19 alex
131 67956560 2002-05-19 alex /* format read states */
132 67956560 2002-05-19 alex #define DP_S_DEFAULT 0
133 67956560 2002-05-19 alex #define DP_S_FLAGS 1
134 67956560 2002-05-19 alex #define DP_S_MIN 2
135 67956560 2002-05-19 alex #define DP_S_DOT 3
136 67956560 2002-05-19 alex #define DP_S_MAX 4
137 67956560 2002-05-19 alex #define DP_S_MOD 5
138 67956560 2002-05-19 alex #define DP_S_CONV 6
139 67956560 2002-05-19 alex #define DP_S_DONE 7
140 67956560 2002-05-19 alex
141 67956560 2002-05-19 alex /* format flags - Bits */
142 67956560 2002-05-19 alex #define DP_F_MINUS (1 << 0)
143 67956560 2002-05-19 alex #define DP_F_PLUS (1 << 1)
144 67956560 2002-05-19 alex #define DP_F_SPACE (1 << 2)
145 67956560 2002-05-19 alex #define DP_F_NUM (1 << 3)
146 67956560 2002-05-19 alex #define DP_F_ZERO (1 << 4)
147 67956560 2002-05-19 alex #define DP_F_UP (1 << 5)
148 67956560 2002-05-19 alex #define DP_F_UNSIGNED (1 << 6)
149 67956560 2002-05-19 alex
150 67956560 2002-05-19 alex /* Conversion Flags */
151 67956560 2002-05-19 alex #define DP_C_SHORT 1
152 67956560 2002-05-19 alex #define DP_C_LONG 2
153 67956560 2002-05-19 alex #define DP_C_LDOUBLE 3
154 67956560 2002-05-19 alex #define DP_C_LLONG 4
155 67956560 2002-05-19 alex
156 67956560 2002-05-19 alex #define char_to_int(p) ((p)- '0')
157 67956560 2002-05-19 alex #ifndef MAX
158 67956560 2002-05-19 alex #define MAX(p,q) (((p) >= (q)) ? (p) : (q))
159 67956560 2002-05-19 alex #endif
160 67956560 2002-05-19 alex
161 67956560 2002-05-19 alex static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args)
162 67956560 2002-05-19 alex {
163 67956560 2002-05-19 alex char ch;
164 67956560 2002-05-19 alex LLONG value;
165 67956560 2002-05-19 alex LDOUBLE fvalue;
166 67956560 2002-05-19 alex char *strvalue;
167 67956560 2002-05-19 alex int min;
168 67956560 2002-05-19 alex int max;
169 67956560 2002-05-19 alex int state;
170 67956560 2002-05-19 alex int flags;
171 67956560 2002-05-19 alex int cflags;
172 67956560 2002-05-19 alex size_t currlen;
173 67956560 2002-05-19 alex
174 67956560 2002-05-19 alex state = DP_S_DEFAULT;
175 67956560 2002-05-19 alex currlen = flags = cflags = min = 0;
176 67956560 2002-05-19 alex max = -1;
177 67956560 2002-05-19 alex ch = *format++;
178 67956560 2002-05-19 alex
179 67956560 2002-05-19 alex while (state != DP_S_DONE) {
180 67956560 2002-05-19 alex if (ch == '\0')
181 67956560 2002-05-19 alex state = DP_S_DONE;
182 67956560 2002-05-19 alex
183 67956560 2002-05-19 alex switch(state) {
184 67956560 2002-05-19 alex case DP_S_DEFAULT:
185 67956560 2002-05-19 alex if (ch == '%')
186 67956560 2002-05-19 alex state = DP_S_FLAGS;
187 67956560 2002-05-19 alex else
188 67956560 2002-05-19 alex dopr_outch (buffer, &currlen, maxlen, ch);
189 67956560 2002-05-19 alex ch = *format++;
190 67956560 2002-05-19 alex break;
191 67956560 2002-05-19 alex case DP_S_FLAGS:
192 67956560 2002-05-19 alex switch (ch) {
193 67956560 2002-05-19 alex case '-':
194 67956560 2002-05-19 alex flags |= DP_F_MINUS;
195 67956560 2002-05-19 alex ch = *format++;
196 67956560 2002-05-19 alex break;
197 67956560 2002-05-19 alex case '+':
198 67956560 2002-05-19 alex flags |= DP_F_PLUS;
199 67956560 2002-05-19 alex ch = *format++;
200 67956560 2002-05-19 alex break;
201 67956560 2002-05-19 alex case ' ':
202 67956560 2002-05-19 alex flags |= DP_F_SPACE;
203 67956560 2002-05-19 alex ch = *format++;
204 67956560 2002-05-19 alex break;
205 67956560 2002-05-19 alex case '#':
206 67956560 2002-05-19 alex flags |= DP_F_NUM;
207 67956560 2002-05-19 alex ch = *format++;
208 67956560 2002-05-19 alex break;
209 67956560 2002-05-19 alex case '0':
210 67956560 2002-05-19 alex flags |= DP_F_ZERO;
211 67956560 2002-05-19 alex ch = *format++;
212 67956560 2002-05-19 alex break;
213 67956560 2002-05-19 alex default:
214 67956560 2002-05-19 alex state = DP_S_MIN;
215 67956560 2002-05-19 alex break;
216 67956560 2002-05-19 alex }
217 67956560 2002-05-19 alex break;
218 67956560 2002-05-19 alex case DP_S_MIN:
219 67956560 2002-05-19 alex if (isdigit((unsigned char)ch)) {
220 67956560 2002-05-19 alex min = 10*min + char_to_int (ch);
221 67956560 2002-05-19 alex ch = *format++;
222 67956560 2002-05-19 alex } else if (ch == '*') {
223 67956560 2002-05-19 alex min = va_arg (args, int);
224 67956560 2002-05-19 alex ch = *format++;
225 67956560 2002-05-19 alex state = DP_S_DOT;
226 67956560 2002-05-19 alex } else {
227 67956560 2002-05-19 alex state = DP_S_DOT;
228 67956560 2002-05-19 alex }
229 67956560 2002-05-19 alex break;
230 67956560 2002-05-19 alex case DP_S_DOT:
231 67956560 2002-05-19 alex if (ch == '.') {
232 67956560 2002-05-19 alex state = DP_S_MAX;
233 67956560 2002-05-19 alex ch = *format++;
234 67956560 2002-05-19 alex } else {
235 67956560 2002-05-19 alex state = DP_S_MOD;
236 67956560 2002-05-19 alex }
237 67956560 2002-05-19 alex break;
238 67956560 2002-05-19 alex case DP_S_MAX:
239 67956560 2002-05-19 alex if (isdigit((unsigned char)ch)) {
240 67956560 2002-05-19 alex if (max < 0)
241 67956560 2002-05-19 alex max = 0;
242 67956560 2002-05-19 alex max = 10*max + char_to_int (ch);
243 67956560 2002-05-19 alex ch = *format++;
244 67956560 2002-05-19 alex } else if (ch == '*') {
245 67956560 2002-05-19 alex max = va_arg (args, int);
246 67956560 2002-05-19 alex ch = *format++;
247 67956560 2002-05-19 alex state = DP_S_MOD;
248 67956560 2002-05-19 alex } else {
249 67956560 2002-05-19 alex state = DP_S_MOD;
250 67956560 2002-05-19 alex }
251 67956560 2002-05-19 alex break;
252 67956560 2002-05-19 alex case DP_S_MOD:
253 67956560 2002-05-19 alex switch (ch) {
254 67956560 2002-05-19 alex case 'h':
255 67956560 2002-05-19 alex cflags = DP_C_SHORT;
256 67956560 2002-05-19 alex ch = *format++;
257 67956560 2002-05-19 alex break;
258 67956560 2002-05-19 alex case 'l':
259 67956560 2002-05-19 alex cflags = DP_C_LONG;
260 67956560 2002-05-19 alex ch = *format++;
261 67956560 2002-05-19 alex if (ch == 'l') { /* It's a long long */
262 67956560 2002-05-19 alex cflags = DP_C_LLONG;
263 67956560 2002-05-19 alex ch = *format++;
264 67956560 2002-05-19 alex }
265 67956560 2002-05-19 alex break;
266 67956560 2002-05-19 alex case 'L':
267 67956560 2002-05-19 alex cflags = DP_C_LDOUBLE;
268 67956560 2002-05-19 alex ch = *format++;
269 67956560 2002-05-19 alex break;
270 67956560 2002-05-19 alex default:
271 67956560 2002-05-19 alex break;
272 67956560 2002-05-19 alex }
273 67956560 2002-05-19 alex state = DP_S_CONV;
274 67956560 2002-05-19 alex break;
275 67956560 2002-05-19 alex case DP_S_CONV:
276 67956560 2002-05-19 alex switch (ch) {
277 67956560 2002-05-19 alex case 'd':
278 67956560 2002-05-19 alex case 'i':
279 67956560 2002-05-19 alex if (cflags == DP_C_SHORT)
280 67956560 2002-05-19 alex value = va_arg (args, int);
281 67956560 2002-05-19 alex else if (cflags == DP_C_LONG)
282 67956560 2002-05-19 alex value = va_arg (args, long int);
283 67956560 2002-05-19 alex else if (cflags == DP_C_LLONG)
284 67956560 2002-05-19 alex value = va_arg (args, LLONG);
285 67956560 2002-05-19 alex else
286 67956560 2002-05-19 alex value = va_arg (args, int);
287 67956560 2002-05-19 alex fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
288 67956560 2002-05-19 alex break;
289 67956560 2002-05-19 alex case 'o':
290 67956560 2002-05-19 alex flags |= DP_F_UNSIGNED;
291 67956560 2002-05-19 alex if (cflags == DP_C_SHORT)
292 67956560 2002-05-19 alex value = va_arg (args, unsigned int);
293 67956560 2002-05-19 alex else if (cflags == DP_C_LONG)
294 67956560 2002-05-19 alex value = (long)va_arg (args, unsigned long int);
295 67956560 2002-05-19 alex else if (cflags == DP_C_LLONG)
296 67956560 2002-05-19 alex value = (long)va_arg (args, unsigned LLONG);
297 67956560 2002-05-19 alex else
298 67956560 2002-05-19 alex value = (long)va_arg (args, unsigned int);
299 67956560 2002-05-19 alex fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags);
300 67956560 2002-05-19 alex break;
301 67956560 2002-05-19 alex case 'u':
302 67956560 2002-05-19 alex flags |= DP_F_UNSIGNED;
303 67956560 2002-05-19 alex if (cflags == DP_C_SHORT)
304 67956560 2002-05-19 alex value = va_arg (args, unsigned int);
305 67956560 2002-05-19 alex else if (cflags == DP_C_LONG)
306 67956560 2002-05-19 alex value = (long)va_arg (args, unsigned long int);
307 67956560 2002-05-19 alex else if (cflags == DP_C_LLONG)
308 67956560 2002-05-19 alex value = (LLONG)va_arg (args, unsigned LLONG);
309 67956560 2002-05-19 alex else
310 67956560 2002-05-19 alex value = (long)va_arg (args, unsigned int);
311 67956560 2002-05-19 alex fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
312 67956560 2002-05-19 alex break;
313 67956560 2002-05-19 alex case 'X':
314 67956560 2002-05-19 alex flags |= DP_F_UP;
315 67956560 2002-05-19 alex case 'x':
316 67956560 2002-05-19 alex flags |= DP_F_UNSIGNED;
317 67956560 2002-05-19 alex if (cflags == DP_C_SHORT)
318 67956560 2002-05-19 alex value = va_arg (args, unsigned int);
319 67956560 2002-05-19 alex else if (cflags == DP_C_LONG)
320 67956560 2002-05-19 alex value = (long)va_arg (args, unsigned long int);
321 67956560 2002-05-19 alex else if (cflags == DP_C_LLONG)
322 67956560 2002-05-19 alex value = (LLONG)va_arg (args, unsigned LLONG);
323 67956560 2002-05-19 alex else
324 67956560 2002-05-19 alex value = (long)va_arg (args, unsigned int);
325 67956560 2002-05-19 alex fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags);
326 67956560 2002-05-19 alex break;
327 67956560 2002-05-19 alex case 'f':
328 67956560 2002-05-19 alex if (cflags == DP_C_LDOUBLE)
329 67956560 2002-05-19 alex fvalue = va_arg (args, LDOUBLE);
330 67956560 2002-05-19 alex else
331 67956560 2002-05-19 alex fvalue = va_arg (args, double);
332 67956560 2002-05-19 alex /* um, floating point? */
333 67956560 2002-05-19 alex fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
334 67956560 2002-05-19 alex break;
335 67956560 2002-05-19 alex case 'E':
336 67956560 2002-05-19 alex flags |= DP_F_UP;
337 67956560 2002-05-19 alex case 'e':
338 67956560 2002-05-19 alex if (cflags == DP_C_LDOUBLE)
339 67956560 2002-05-19 alex fvalue = va_arg (args, LDOUBLE);
340 67956560 2002-05-19 alex else
341 67956560 2002-05-19 alex fvalue = va_arg (args, double);
342 67956560 2002-05-19 alex break;
343 67956560 2002-05-19 alex case 'G':
344 67956560 2002-05-19 alex flags |= DP_F_UP;
345 67956560 2002-05-19 alex case 'g':
346 67956560 2002-05-19 alex if (cflags == DP_C_LDOUBLE)
347 67956560 2002-05-19 alex fvalue = va_arg (args, LDOUBLE);
348 67956560 2002-05-19 alex else
349 67956560 2002-05-19 alex fvalue = va_arg (args, double);
350 67956560 2002-05-19 alex break;
351 67956560 2002-05-19 alex case 'c':
352 67956560 2002-05-19 alex dopr_outch (buffer, &currlen, maxlen, va_arg (args, int));
353 67956560 2002-05-19 alex break;
354 67956560 2002-05-19 alex case 's':
355 67956560 2002-05-19 alex strvalue = va_arg (args, char *);
356 67956560 2002-05-19 alex if (max == -1) {
357 67956560 2002-05-19 alex max = strlen(strvalue);
358 67956560 2002-05-19 alex }
359 67956560 2002-05-19 alex if (min > 0 && max >= 0 && min > max) max = min;
360 67956560 2002-05-19 alex fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max);
361 67956560 2002-05-19 alex break;
362 67956560 2002-05-19 alex case 'p':
363 67956560 2002-05-19 alex strvalue = va_arg (args, void *);
364 67956560 2002-05-19 alex fmtint (buffer, &currlen, maxlen, (long) strvalue, 16, min, max, flags);
365 67956560 2002-05-19 alex break;
366 67956560 2002-05-19 alex case 'n':
367 67956560 2002-05-19 alex if (cflags == DP_C_SHORT) {
368 67956560 2002-05-19 alex short int *num;
369 67956560 2002-05-19 alex num = va_arg (args, short int *);
370 67956560 2002-05-19 alex *num = currlen;
371 67956560 2002-05-19 alex } else if (cflags == DP_C_LONG) {
372 67956560 2002-05-19 alex long int *num;
373 67956560 2002-05-19 alex num = va_arg (args, long int *);
374 67956560 2002-05-19 alex *num = (long int)currlen;
375 67956560 2002-05-19 alex } else if (cflags == DP_C_LLONG) {
376 67956560 2002-05-19 alex LLONG *num;
377 67956560 2002-05-19 alex num = va_arg (args, LLONG *);
378 67956560 2002-05-19 alex *num = (LLONG)currlen;
379 67956560 2002-05-19 alex } else {
380 67956560 2002-05-19 alex int *num;
381 67956560 2002-05-19 alex num = va_arg (args, int *);
382 67956560 2002-05-19 alex *num = currlen;
383 67956560 2002-05-19 alex }
384 67956560 2002-05-19 alex break;
385 67956560 2002-05-19 alex case '%':
386 67956560 2002-05-19 alex dopr_outch (buffer, &currlen, maxlen, ch);
387 67956560 2002-05-19 alex break;
388 67956560 2002-05-19 alex case 'w':
389 67956560 2002-05-19 alex /* not supported yet, treat as next char */
390 67956560 2002-05-19 alex ch = *format++;
391 67956560 2002-05-19 alex break;
392 67956560 2002-05-19 alex default:
393 67956560 2002-05-19 alex /* Unknown, skip */
394 67956560 2002-05-19 alex break;
395 67956560 2002-05-19 alex }
396 67956560 2002-05-19 alex ch = *format++;
397 67956560 2002-05-19 alex state = DP_S_DEFAULT;
398 67956560 2002-05-19 alex flags = cflags = min = 0;
399 67956560 2002-05-19 alex max = -1;
400 67956560 2002-05-19 alex break;
401 67956560 2002-05-19 alex case DP_S_DONE:
402 67956560 2002-05-19 alex break;
403 67956560 2002-05-19 alex default:
404 67956560 2002-05-19 alex /* hmm? */
405 67956560 2002-05-19 alex break; /* some picky compilers need this */
406 67956560 2002-05-19 alex }
407 67956560 2002-05-19 alex }
408 67956560 2002-05-19 alex if (maxlen != 0) {
409 67956560 2002-05-19 alex if (currlen < maxlen - 1)
410 67956560 2002-05-19 alex buffer[currlen] = '\0';
411 67956560 2002-05-19 alex else if (maxlen > 0)
412 67956560 2002-05-19 alex buffer[maxlen - 1] = '\0';
413 67956560 2002-05-19 alex }
414 67956560 2002-05-19 alex
415 67956560 2002-05-19 alex return currlen;
416 67956560 2002-05-19 alex }
417 67956560 2002-05-19 alex
418 67956560 2002-05-19 alex static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
419 67956560 2002-05-19 alex char *value, int flags, int min, int max)
420 67956560 2002-05-19 alex {
421 67956560 2002-05-19 alex int padlen, strln; /* amount to pad */
422 67956560 2002-05-19 alex int cnt = 0;
423 67956560 2002-05-19 alex
424 67956560 2002-05-19 alex #ifdef DEBUG_SNPRINTF
425 67956560 2002-05-19 alex printf("fmtstr min=%d max=%d s=[%s]\n", min, max, value);
426 67956560 2002-05-19 alex #endif
427 67956560 2002-05-19 alex if (value == 0) {
428 67956560 2002-05-19 alex value = "<NULL>";
429 67956560 2002-05-19 alex }
430 67956560 2002-05-19 alex
431 67956560 2002-05-19 alex for (strln = 0; value[strln]; ++strln); /* strlen */
432 67956560 2002-05-19 alex padlen = min - strln;
433 67956560 2002-05-19 alex if (padlen < 0)
434 67956560 2002-05-19 alex padlen = 0;
435 67956560 2002-05-19 alex if (flags & DP_F_MINUS)
436 67956560 2002-05-19 alex padlen = -padlen; /* Left Justify */
437 67956560 2002-05-19 alex
438 67956560 2002-05-19 alex while ((padlen > 0) && (cnt < max)) {
439 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, ' ');
440 67956560 2002-05-19 alex --padlen;
441 67956560 2002-05-19 alex ++cnt;
442 67956560 2002-05-19 alex }
443 67956560 2002-05-19 alex while (*value && (cnt < max)) {
444 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, *value++);
445 67956560 2002-05-19 alex ++cnt;
446 67956560 2002-05-19 alex }
447 67956560 2002-05-19 alex while ((padlen < 0) && (cnt < max)) {
448 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, ' ');
449 67956560 2002-05-19 alex ++padlen;
450 67956560 2002-05-19 alex ++cnt;
451 67956560 2002-05-19 alex }
452 67956560 2002-05-19 alex }
453 67956560 2002-05-19 alex
454 67956560 2002-05-19 alex /* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
455 67956560 2002-05-19 alex
456 67956560 2002-05-19 alex static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
457 67956560 2002-05-19 alex long value, int base, int min, int max, int flags)
458 67956560 2002-05-19 alex {
459 67956560 2002-05-19 alex int signvalue = 0;
460 67956560 2002-05-19 alex unsigned long uvalue;
461 67956560 2002-05-19 alex char convert[20];
462 67956560 2002-05-19 alex int place = 0;
463 67956560 2002-05-19 alex int spadlen = 0; /* amount to space pad */
464 67956560 2002-05-19 alex int zpadlen = 0; /* amount to zero pad */
465 67956560 2002-05-19 alex int caps = 0;
466 67956560 2002-05-19 alex
467 67956560 2002-05-19 alex if (max < 0)
468 67956560 2002-05-19 alex max = 0;
469 67956560 2002-05-19 alex
470 67956560 2002-05-19 alex uvalue = value;
471 67956560 2002-05-19 alex
472 67956560 2002-05-19 alex if(!(flags & DP_F_UNSIGNED)) {
473 67956560 2002-05-19 alex if( value < 0 ) {
474 67956560 2002-05-19 alex signvalue = '-';
475 67956560 2002-05-19 alex uvalue = -value;
476 67956560 2002-05-19 alex } else {
477 67956560 2002-05-19 alex if (flags & DP_F_PLUS) /* Do a sign (+/i) */
478 67956560 2002-05-19 alex signvalue = '+';
479 67956560 2002-05-19 alex else if (flags & DP_F_SPACE)
480 67956560 2002-05-19 alex signvalue = ' ';
481 67956560 2002-05-19 alex }
482 67956560 2002-05-19 alex }
483 67956560 2002-05-19 alex
484 67956560 2002-05-19 alex if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
485 67956560 2002-05-19 alex
486 67956560 2002-05-19 alex do {
487 67956560 2002-05-19 alex convert[place++] =
488 67956560 2002-05-19 alex (caps? "0123456789ABCDEF":"0123456789abcdef")
489 67956560 2002-05-19 alex [uvalue % (unsigned)base ];
490 67956560 2002-05-19 alex uvalue = (uvalue / (unsigned)base );
491 67956560 2002-05-19 alex } while(uvalue && (place < 20));
492 67956560 2002-05-19 alex if (place == 20) place--;
493 67956560 2002-05-19 alex convert[place] = 0;
494 67956560 2002-05-19 alex
495 67956560 2002-05-19 alex zpadlen = max - place;
496 67956560 2002-05-19 alex spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
497 67956560 2002-05-19 alex if (zpadlen < 0) zpadlen = 0;
498 67956560 2002-05-19 alex if (spadlen < 0) spadlen = 0;
499 67956560 2002-05-19 alex if (flags & DP_F_ZERO) {
500 67956560 2002-05-19 alex zpadlen = MAX(zpadlen, spadlen);
501 67956560 2002-05-19 alex spadlen = 0;
502 67956560 2002-05-19 alex }
503 67956560 2002-05-19 alex if (flags & DP_F_MINUS)
504 67956560 2002-05-19 alex spadlen = -spadlen; /* Left Justifty */
505 67956560 2002-05-19 alex
506 67956560 2002-05-19 alex #ifdef DEBUG_SNPRINTF
507 67956560 2002-05-19 alex printf("zpad: %d, spad: %d, min: %d, max: %d, place: %d\n",
508 67956560 2002-05-19 alex zpadlen, spadlen, min, max, place);
509 67956560 2002-05-19 alex #endif
510 67956560 2002-05-19 alex
511 67956560 2002-05-19 alex /* Spaces */
512 67956560 2002-05-19 alex while (spadlen > 0) {
513 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, ' ');
514 67956560 2002-05-19 alex --spadlen;
515 67956560 2002-05-19 alex }
516 67956560 2002-05-19 alex
517 67956560 2002-05-19 alex /* Sign */
518 67956560 2002-05-19 alex if (signvalue)
519 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, signvalue);
520 67956560 2002-05-19 alex
521 67956560 2002-05-19 alex /* Zeros */
522 67956560 2002-05-19 alex if (zpadlen > 0) {
523 67956560 2002-05-19 alex while (zpadlen > 0) {
524 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, '0');
525 67956560 2002-05-19 alex --zpadlen;
526 67956560 2002-05-19 alex }
527 67956560 2002-05-19 alex }
528 67956560 2002-05-19 alex
529 67956560 2002-05-19 alex /* Digits */
530 67956560 2002-05-19 alex while (place > 0)
531 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, convert[--place]);
532 67956560 2002-05-19 alex
533 67956560 2002-05-19 alex /* Left Justified spaces */
534 67956560 2002-05-19 alex while (spadlen < 0) {
535 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, ' ');
536 67956560 2002-05-19 alex ++spadlen;
537 67956560 2002-05-19 alex }
538 67956560 2002-05-19 alex }
539 67956560 2002-05-19 alex
540 67956560 2002-05-19 alex static LDOUBLE abs_val(LDOUBLE value)
541 67956560 2002-05-19 alex {
542 67956560 2002-05-19 alex LDOUBLE result = value;
543 67956560 2002-05-19 alex
544 67956560 2002-05-19 alex if (value < 0)
545 67956560 2002-05-19 alex result = -value;
546 67956560 2002-05-19 alex
547 67956560 2002-05-19 alex return result;
548 67956560 2002-05-19 alex }
549 67956560 2002-05-19 alex
550 67956560 2002-05-19 alex static LDOUBLE POW10(int exp)
551 67956560 2002-05-19 alex {
552 67956560 2002-05-19 alex LDOUBLE result = 1;
553 67956560 2002-05-19 alex
554 67956560 2002-05-19 alex while (exp) {
555 67956560 2002-05-19 alex result *= 10;
556 67956560 2002-05-19 alex exp--;
557 67956560 2002-05-19 alex }
558 67956560 2002-05-19 alex
559 67956560 2002-05-19 alex return result;
560 67956560 2002-05-19 alex }
561 67956560 2002-05-19 alex
562 67956560 2002-05-19 alex static LLONG ROUND(LDOUBLE value)
563 67956560 2002-05-19 alex {
564 67956560 2002-05-19 alex LLONG intpart;
565 67956560 2002-05-19 alex
566 67956560 2002-05-19 alex intpart = (LLONG)value;
567 67956560 2002-05-19 alex value = value - intpart;
568 67956560 2002-05-19 alex if (value >= 0.5) intpart++;
569 67956560 2002-05-19 alex
570 67956560 2002-05-19 alex return intpart;
571 67956560 2002-05-19 alex }
572 67956560 2002-05-19 alex
573 67956560 2002-05-19 alex /* a replacement for modf that doesn't need the math library. Should
574 67956560 2002-05-19 alex be portable, but slow */
575 67956560 2002-05-19 alex static double my_modf(double x0, double *iptr)
576 67956560 2002-05-19 alex {
577 67956560 2002-05-19 alex int i;
578 67956560 2002-05-19 alex long l;
579 67956560 2002-05-19 alex double x = x0;
580 67956560 2002-05-19 alex double f = 1.0;
581 67956560 2002-05-19 alex
582 67956560 2002-05-19 alex for (i=0;i<100;i++) {
583 67956560 2002-05-19 alex l = (long)x;
584 67956560 2002-05-19 alex if (l <= (x+1) && l >= (x-1)) break;
585 67956560 2002-05-19 alex x *= 0.1;
586 67956560 2002-05-19 alex f *= 10.0;
587 67956560 2002-05-19 alex }
588 67956560 2002-05-19 alex
589 67956560 2002-05-19 alex if (i == 100) {
590 67956560 2002-05-19 alex /* yikes! the number is beyond what we can handle. What do we do? */
591 67956560 2002-05-19 alex (*iptr) = 0;
592 67956560 2002-05-19 alex return 0;
593 67956560 2002-05-19 alex }
594 67956560 2002-05-19 alex
595 67956560 2002-05-19 alex if (i != 0) {
596 67956560 2002-05-19 alex double i2;
597 67956560 2002-05-19 alex double ret;
598 67956560 2002-05-19 alex
599 67956560 2002-05-19 alex ret = my_modf(x0-l*f, &i2);
600 67956560 2002-05-19 alex (*iptr) = l*f + i2;
601 67956560 2002-05-19 alex return ret;
602 67956560 2002-05-19 alex }
603 67956560 2002-05-19 alex
604 67956560 2002-05-19 alex (*iptr) = l;
605 67956560 2002-05-19 alex return x - (*iptr);
606 67956560 2002-05-19 alex }
607 67956560 2002-05-19 alex
608 67956560 2002-05-19 alex
609 67956560 2002-05-19 alex static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
610 67956560 2002-05-19 alex LDOUBLE fvalue, int min, int max, int flags)
611 67956560 2002-05-19 alex {
612 67956560 2002-05-19 alex int signvalue = 0;
613 67956560 2002-05-19 alex double ufvalue;
614 67956560 2002-05-19 alex char iconvert[311];
615 67956560 2002-05-19 alex char fconvert[311];
616 67956560 2002-05-19 alex int iplace = 0;
617 67956560 2002-05-19 alex int fplace = 0;
618 67956560 2002-05-19 alex int padlen = 0; /* amount to pad */
619 67956560 2002-05-19 alex int zpadlen = 0;
620 67956560 2002-05-19 alex int caps = 0;
621 67956560 2002-05-19 alex int index;
622 67956560 2002-05-19 alex double intpart;
623 67956560 2002-05-19 alex double fracpart;
624 67956560 2002-05-19 alex double temp;
625 67956560 2002-05-19 alex
626 67956560 2002-05-19 alex /*
627 67956560 2002-05-19 alex * AIX manpage says the default is 0, but Solaris says the default
628 67956560 2002-05-19 alex * is 6, and sprintf on AIX defaults to 6
629 67956560 2002-05-19 alex */
630 67956560 2002-05-19 alex if (max < 0)
631 67956560 2002-05-19 alex max = 6;
632 67956560 2002-05-19 alex
633 67956560 2002-05-19 alex ufvalue = abs_val (fvalue);
634 67956560 2002-05-19 alex
635 67956560 2002-05-19 alex if (fvalue < 0) {
636 67956560 2002-05-19 alex signvalue = '-';
637 67956560 2002-05-19 alex } else {
638 67956560 2002-05-19 alex if (flags & DP_F_PLUS) { /* Do a sign (+/i) */
639 67956560 2002-05-19 alex signvalue = '+';
640 67956560 2002-05-19 alex } else {
641 67956560 2002-05-19 alex if (flags & DP_F_SPACE)
642 67956560 2002-05-19 alex signvalue = ' ';
643 67956560 2002-05-19 alex }
644 67956560 2002-05-19 alex }
645 67956560 2002-05-19 alex
646 67956560 2002-05-19 alex #if 0
647 67956560 2002-05-19 alex if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
648 67956560 2002-05-19 alex #endif
649 67956560 2002-05-19 alex
650 67956560 2002-05-19 alex #if 0
651 c5461c45 2002-05-19 alex if (max == 0) ufvalue += 0.5; /* if max = 0 we must round */
652 67956560 2002-05-19 alex #endif
653 67956560 2002-05-19 alex
654 67956560 2002-05-19 alex /*
655 67956560 2002-05-19 alex * Sorry, we only support 16 digits past the decimal because of our
656 67956560 2002-05-19 alex * conversion method
657 67956560 2002-05-19 alex */
658 67956560 2002-05-19 alex if (max > 16)
659 67956560 2002-05-19 alex max = 16;
660 67956560 2002-05-19 alex
661 67956560 2002-05-19 alex /* We "cheat" by converting the fractional part to integer by
662 67956560 2002-05-19 alex * multiplying by a factor of 10
663 67956560 2002-05-19 alex */
664 67956560 2002-05-19 alex
665 67956560 2002-05-19 alex temp = ufvalue;
666 67956560 2002-05-19 alex my_modf(temp, &intpart);
667 67956560 2002-05-19 alex
668 67956560 2002-05-19 alex fracpart = ROUND((POW10(max)) * (ufvalue - intpart));
669 67956560 2002-05-19 alex
670 67956560 2002-05-19 alex if (fracpart >= POW10(max)) {
671 67956560 2002-05-19 alex intpart++;
672 67956560 2002-05-19 alex fracpart -= POW10(max);
673 67956560 2002-05-19 alex }
674 67956560 2002-05-19 alex
675 67956560 2002-05-19 alex
676 67956560 2002-05-19 alex /* Convert integer part */
677 67956560 2002-05-19 alex do {
678 67956560 2002-05-19 alex temp = intpart;
679 67956560 2002-05-19 alex my_modf(intpart*0.1, &intpart);
680 67956560 2002-05-19 alex temp = temp*0.1;
681 67956560 2002-05-19 alex index = (int) ((temp -intpart +0.05)* 10.0);
682 67956560 2002-05-19 alex /* index = (int) (((double)(temp*0.1) -intpart +0.05) *10.0); */
683 67956560 2002-05-19 alex /* printf ("%llf, %f, %x\n", temp, intpart, index); */
684 67956560 2002-05-19 alex iconvert[iplace++] =
685 67956560 2002-05-19 alex (caps? "0123456789ABCDEF":"0123456789abcdef")[index];
686 67956560 2002-05-19 alex } while (intpart && (iplace < 311));
687 67956560 2002-05-19 alex if (iplace == 311) iplace--;
688 67956560 2002-05-19 alex iconvert[iplace] = 0;
689 67956560 2002-05-19 alex
690 67956560 2002-05-19 alex /* Convert fractional part */
691 67956560 2002-05-19 alex if (fracpart)
692 67956560 2002-05-19 alex {
693 67956560 2002-05-19 alex do {
694 67956560 2002-05-19 alex temp = fracpart;
695 67956560 2002-05-19 alex my_modf(fracpart*0.1, &fracpart);
696 67956560 2002-05-19 alex temp = temp*0.1;
697 67956560 2002-05-19 alex index = (int) ((temp -fracpart +0.05)* 10.0);
698 67956560 2002-05-19 alex /* index = (int) ((((temp/10) -fracpart) +0.05) *10); */
699 67956560 2002-05-19 alex /* printf ("%lf, %lf, %ld\n", temp, fracpart, index); */
700 67956560 2002-05-19 alex fconvert[fplace++] =
701 67956560 2002-05-19 alex (caps? "0123456789ABCDEF":"0123456789abcdef")[index];
702 67956560 2002-05-19 alex } while(fracpart && (fplace < 311));
703 67956560 2002-05-19 alex if (fplace == 311) fplace--;
704 67956560 2002-05-19 alex }
705 67956560 2002-05-19 alex fconvert[fplace] = 0;
706 67956560 2002-05-19 alex
707 67956560 2002-05-19 alex /* -1 for decimal point, another -1 if we are printing a sign */
708 67956560 2002-05-19 alex padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
709 67956560 2002-05-19 alex zpadlen = max - fplace;
710 67956560 2002-05-19 alex if (zpadlen < 0) zpadlen = 0;
711 67956560 2002-05-19 alex if (padlen < 0)
712 67956560 2002-05-19 alex padlen = 0;
713 67956560 2002-05-19 alex if (flags & DP_F_MINUS)
714 67956560 2002-05-19 alex padlen = -padlen; /* Left Justifty */
715 67956560 2002-05-19 alex
716 67956560 2002-05-19 alex if ((flags & DP_F_ZERO) && (padlen > 0)) {
717 67956560 2002-05-19 alex if (signvalue) {
718 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, signvalue);
719 67956560 2002-05-19 alex --padlen;
720 67956560 2002-05-19 alex signvalue = 0;
721 67956560 2002-05-19 alex }
722 67956560 2002-05-19 alex while (padlen > 0) {
723 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, '0');
724 67956560 2002-05-19 alex --padlen;
725 67956560 2002-05-19 alex }
726 67956560 2002-05-19 alex }
727 67956560 2002-05-19 alex while (padlen > 0) {
728 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, ' ');
729 67956560 2002-05-19 alex --padlen;
730 67956560 2002-05-19 alex }
731 67956560 2002-05-19 alex if (signvalue)
732 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, signvalue);
733 67956560 2002-05-19 alex
734 67956560 2002-05-19 alex while (iplace > 0)
735 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]);
736 67956560 2002-05-19 alex
737 67956560 2002-05-19 alex #ifdef DEBUG_SNPRINTF
738 67956560 2002-05-19 alex printf("fmtfp: fplace=%d zpadlen=%d\n", fplace, zpadlen);
739 67956560 2002-05-19 alex #endif
740 67956560 2002-05-19 alex
741 67956560 2002-05-19 alex /*
742 67956560 2002-05-19 alex * Decimal point. This should probably use locale to find the correct
743 67956560 2002-05-19 alex * char to print out.
744 67956560 2002-05-19 alex */
745 67956560 2002-05-19 alex if (max > 0) {
746 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, '.');
747 67956560 2002-05-19 alex
748 67956560 2002-05-19 alex while (fplace > 0)
749 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]);
750 67956560 2002-05-19 alex }
751 67956560 2002-05-19 alex
752 67956560 2002-05-19 alex while (zpadlen > 0) {
753 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, '0');
754 67956560 2002-05-19 alex --zpadlen;
755 67956560 2002-05-19 alex }
756 67956560 2002-05-19 alex
757 67956560 2002-05-19 alex while (padlen < 0) {
758 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, ' ');
759 67956560 2002-05-19 alex ++padlen;
760 67956560 2002-05-19 alex }
761 67956560 2002-05-19 alex }
762 67956560 2002-05-19 alex
763 67956560 2002-05-19 alex static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c)
764 67956560 2002-05-19 alex {
765 67956560 2002-05-19 alex if (*currlen < maxlen) {
766 67956560 2002-05-19 alex buffer[(*currlen)] = c;
767 67956560 2002-05-19 alex }
768 67956560 2002-05-19 alex (*currlen)++;
769 67956560 2002-05-19 alex }
770 67956560 2002-05-19 alex
771 c5461c45 2002-05-19 alex #if !defined(HAVE_VSNPRINTF)
772 c5461c45 2002-05-19 alex int vsnprintf (char *str, size_t count, const char *fmt, va_list args)
773 67956560 2002-05-19 alex {
774 67956560 2002-05-19 alex return dopr(str, count, fmt, args);
775 67956560 2002-05-19 alex }
776 67956560 2002-05-19 alex #endif
777 67956560 2002-05-19 alex
778 c5461c45 2002-05-19 alex #if !defined(HAVE_SNPRINTF)
779 c5461c45 2002-05-19 alex int snprintf(char *str,size_t count,const char *fmt,...)
780 67956560 2002-05-19 alex {
781 67956560 2002-05-19 alex size_t ret;
782 67956560 2002-05-19 alex va_list ap;
783 67956560 2002-05-19 alex
784 67956560 2002-05-19 alex va_start(ap, fmt);
785 67956560 2002-05-19 alex ret = vsnprintf(str, count, fmt, ap);
786 67956560 2002-05-19 alex va_end(ap);
787 67956560 2002-05-19 alex return ret;
788 67956560 2002-05-19 alex }
789 67956560 2002-05-19 alex #endif
790 67956560 2002-05-19 alex
791 67956560 2002-05-19 alex #endif
792 67956560 2002-05-19 alex
793 67956560 2002-05-19 alex
794 67956560 2002-05-19 alex #ifdef TEST_SNPRINTF
795 67956560 2002-05-19 alex int sprintf(char *str,const char *fmt,...);
796 67956560 2002-05-19 alex int main (void)
797 67956560 2002-05-19 alex {
798 67956560 2002-05-19 alex char buf1[1024];
799 67956560 2002-05-19 alex char buf2[1024];
800 67956560 2002-05-19 alex char *fp_fmt[] = {
801 67956560 2002-05-19 alex "%1.1f",
802 67956560 2002-05-19 alex "%-1.5f",
803 67956560 2002-05-19 alex "%1.5f",
804 67956560 2002-05-19 alex "%123.9f",
805 67956560 2002-05-19 alex "%10.5f",
806 67956560 2002-05-19 alex "% 10.5f",
807 67956560 2002-05-19 alex "%+22.9f",
808 67956560 2002-05-19 alex "%+4.9f",
809 67956560 2002-05-19 alex "%01.3f",
810 67956560 2002-05-19 alex "%4f",
811 67956560 2002-05-19 alex "%3.1f",
812 67956560 2002-05-19 alex "%3.2f",
813 67956560 2002-05-19 alex "%.0f",
814 67956560 2002-05-19 alex "%f",
815 67956560 2002-05-19 alex "-16.16f",
816 67956560 2002-05-19 alex NULL
817 67956560 2002-05-19 alex };
818 67956560 2002-05-19 alex double fp_nums[] = { 6442452944.1234, -1.5, 134.21, 91340.2, 341.1234, 0203.9, 0.96, 0.996,
819 67956560 2002-05-19 alex 0.9996, 1.996, 4.136, 0};
820 67956560 2002-05-19 alex char *int_fmt[] = {
821 67956560 2002-05-19 alex "%-1.5d",
822 67956560 2002-05-19 alex "%1.5d",
823 67956560 2002-05-19 alex "%123.9d",
824 67956560 2002-05-19 alex "%5.5d",
825 67956560 2002-05-19 alex "%10.5d",
826 67956560 2002-05-19 alex "% 10.5d",
827 67956560 2002-05-19 alex "%+22.33d",
828 67956560 2002-05-19 alex "%01.3d",
829 67956560 2002-05-19 alex "%4d",
830 67956560 2002-05-19 alex "%d",
831 67956560 2002-05-19 alex NULL
832 67956560 2002-05-19 alex };
833 67956560 2002-05-19 alex long int_nums[] = { -1, 134, 91340, 341, 0203, 0};
834 67956560 2002-05-19 alex char *str_fmt[] = {
835 67956560 2002-05-19 alex "10.5s",
836 67956560 2002-05-19 alex "5.10s",
837 67956560 2002-05-19 alex "10.1s",
838 67956560 2002-05-19 alex "0.10s",
839 67956560 2002-05-19 alex "10.0s",
840 67956560 2002-05-19 alex "1.10s",
841 67956560 2002-05-19 alex "%s",
842 67956560 2002-05-19 alex "%.1s",
843 67956560 2002-05-19 alex "%.10s",
844 67956560 2002-05-19 alex "%10s",
845 67956560 2002-05-19 alex NULL
846 67956560 2002-05-19 alex };
847 67956560 2002-05-19 alex char *str_vals[] = {"hello", "a", "", "a longer string", NULL};
848 67956560 2002-05-19 alex int x, y;
849 67956560 2002-05-19 alex int fail = 0;
850 67956560 2002-05-19 alex int num = 0;
851 67956560 2002-05-19 alex
852 67956560 2002-05-19 alex printf ("Testing snprintf format codes against system sprintf...\n");
853 67956560 2002-05-19 alex
854 67956560 2002-05-19 alex for (x = 0; fp_fmt[x] ; x++) {
855 67956560 2002-05-19 alex for (y = 0; fp_nums[y] != 0 ; y++) {
856 67956560 2002-05-19 alex int l1 = snprintf(NULL, 0, fp_fmt[x], fp_nums[y]);
857 67956560 2002-05-19 alex int l2 = snprintf(buf1, sizeof(buf1), fp_fmt[x], fp_nums[y]);
858 67956560 2002-05-19 alex sprintf (buf2, fp_fmt[x], fp_nums[y]);
859 67956560 2002-05-19 alex if (strcmp (buf1, buf2)) {
860 67956560 2002-05-19 alex printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n",
861 67956560 2002-05-19 alex fp_fmt[x], buf1, buf2);
862 67956560 2002-05-19 alex fail++;
863 67956560 2002-05-19 alex }
864 67956560 2002-05-19 alex if (l1 != l2) {
865 67956560 2002-05-19 alex printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, fp_fmt[x]);
866 67956560 2002-05-19 alex fail++;
867 67956560 2002-05-19 alex }
868 67956560 2002-05-19 alex num++;
869 67956560 2002-05-19 alex }
870 67956560 2002-05-19 alex }
871 67956560 2002-05-19 alex
872 67956560 2002-05-19 alex for (x = 0; int_fmt[x] ; x++) {
873 67956560 2002-05-19 alex for (y = 0; int_nums[y] != 0 ; y++) {
874 67956560 2002-05-19 alex int l1 = snprintf(NULL, 0, int_fmt[x], int_nums[y]);
875 67956560 2002-05-19 alex int l2 = snprintf(buf1, sizeof(buf1), int_fmt[x], int_nums[y]);
876 67956560 2002-05-19 alex sprintf (buf2, int_fmt[x], int_nums[y]);
877 67956560 2002-05-19 alex if (strcmp (buf1, buf2)) {
878 67956560 2002-05-19 alex printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n",
879 67956560 2002-05-19 alex int_fmt[x], buf1, buf2);
880 67956560 2002-05-19 alex fail++;
881 67956560 2002-05-19 alex }
882 67956560 2002-05-19 alex if (l1 != l2) {
883 67956560 2002-05-19 alex printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, int_fmt[x]);
884 67956560 2002-05-19 alex fail++;
885 67956560 2002-05-19 alex }
886 67956560 2002-05-19 alex num++;
887 67956560 2002-05-19 alex }
888 67956560 2002-05-19 alex }
889 67956560 2002-05-19 alex
890 67956560 2002-05-19 alex for (x = 0; str_fmt[x] ; x++) {
891 67956560 2002-05-19 alex for (y = 0; str_vals[y] != 0 ; y++) {
892 67956560 2002-05-19 alex int l1 = snprintf(NULL, 0, str_fmt[x], str_vals[y]);
893 67956560 2002-05-19 alex int l2 = snprintf(buf1, sizeof(buf1), str_fmt[x], str_vals[y]);
894 67956560 2002-05-19 alex sprintf (buf2, str_fmt[x], str_vals[y]);
895 67956560 2002-05-19 alex if (strcmp (buf1, buf2)) {
896 67956560 2002-05-19 alex printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n",
897 67956560 2002-05-19 alex str_fmt[x], buf1, buf2);
898 67956560 2002-05-19 alex fail++;
899 67956560 2002-05-19 alex }
900 67956560 2002-05-19 alex if (l1 != l2) {
901 67956560 2002-05-19 alex printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, str_fmt[x]);
902 67956560 2002-05-19 alex fail++;
903 67956560 2002-05-19 alex }
904 67956560 2002-05-19 alex num++;
905 67956560 2002-05-19 alex }
906 67956560 2002-05-19 alex }
907 67956560 2002-05-19 alex
908 67956560 2002-05-19 alex printf ("%d tests failed out of %d.\n", fail, num);
909 67956560 2002-05-19 alex
910 67956560 2002-05-19 alex printf("seeing how many digits we support\n");
911 67956560 2002-05-19 alex {
912 67956560 2002-05-19 alex double v0 = 0.12345678901234567890123456789012345678901;
913 67956560 2002-05-19 alex for (x=0; x<100; x++) {
914 67956560 2002-05-19 alex snprintf(buf1, sizeof(buf1), "%1.1f", v0*pow(10, x));
915 67956560 2002-05-19 alex sprintf(buf2, "%1.1f", v0*pow(10, x));
916 67956560 2002-05-19 alex if (strcmp(buf1, buf2)) {
917 67956560 2002-05-19 alex printf("we seem to support %d digits\n", x-1);
918 67956560 2002-05-19 alex break;
919 67956560 2002-05-19 alex }
920 67956560 2002-05-19 alex }
921 67956560 2002-05-19 alex }
922 67956560 2002-05-19 alex return 0;
923 67956560 2002-05-19 alex }
924 67956560 2002-05-19 alex #endif /* SNPRINTF_TEST */
925 67956560 2002-05-19 alex
926 67956560 2002-05-19 alex
927 67956560 2002-05-19 alex /* -eof- */