]>
Commit | Line | Data |
---|---|---|
1c14df9e | 1 | /* |
2 | * Copyright Patrick Powell 1995 | |
3 | * This code is based on code written by Patrick Powell (papowell@astart.com) | |
4 | * It may be used for any purpose as long as this notice remains intact | |
5 | * on all source code distributions | |
6 | */ | |
7 | ||
3c0ef626 | 8 | /************************************************************** |
9 | * Original: | |
10 | * Patrick Powell Tue Apr 11 09:48:21 PDT 1995 | |
11 | * A bombproof version of doprnt (dopr) included. | |
12 | * Sigh. This sort of thing is always nasty do deal with. Note that | |
13 | * the version here does not include floating point... | |
14 | * | |
15 | * snprintf() is used instead of sprintf() as it does limit checks | |
16 | * for string length. This covers a nasty loophole. | |
17 | * | |
18 | * The other functions are there to prevent NULL pointers from | |
19 | * causing nast effects. | |
20 | * | |
21 | * More Recently: | |
22 | * Brandon Long <blong@fiction.net> 9/15/96 for mutt 0.43 | |
23 | * This was ugly. It is still ugly. I opted out of floating point | |
24 | * numbers, but the formatter understands just about everything | |
25 | * from the normal C string format, at least as far as I can tell from | |
26 | * the Solaris 2.5 printf(3S) man page. | |
27 | * | |
28 | * Brandon Long <blong@fiction.net> 10/22/97 for mutt 0.87.1 | |
29 | * Ok, added some minimal floating point support, which means this | |
30 | * probably requires libm on most operating systems. Don't yet | |
31 | * support the exponent (e,E) and sigfig (g,G). Also, fmtint() | |
32 | * was pretty badly broken, it just wasn't being exercised in ways | |
33 | * which showed it, so that's been fixed. Also, formated the code | |
34 | * to mutt conventions, and removed dead code left over from the | |
35 | * original. Also, there is now a builtin-test, just compile with: | |
36 | * gcc -DTEST_SNPRINTF -o snprintf snprintf.c -lm | |
37 | * and run snprintf for results. | |
38 | * | |
39 | * Thomas Roessler <roessler@guug.de> 01/27/98 for mutt 0.89i | |
40 | * The PGP code was using unsigned hexadecimal formats. | |
41 | * Unfortunately, unsigned formats simply didn't work. | |
42 | * | |
43 | * Michael Elkins <me@cs.hmc.edu> 03/05/98 for mutt 0.90.8 | |
44 | * The original code assumed that both snprintf() and vsnprintf() were | |
45 | * missing. Some systems only have snprintf() but not vsnprintf(), so | |
46 | * the code is now broken down under HAVE_SNPRINTF and HAVE_VSNPRINTF. | |
47 | * | |
e00da40d | 48 | * Andrew Tridgell (tridge@samba.org) Oct 1998 |
49 | * fixed handling of %.0f | |
50 | * added test for HAVE_LONG_DOUBLE | |
3c0ef626 | 51 | * |
e00da40d | 52 | * tridge@samba.org, idra@samba.org, April 2001 |
53 | * got rid of fcvt code (twas buggy and made testing harder) | |
54 | * added C99 semantics | |
55 | * | |
56 | * date: 2002/12/19 19:56:31; author: herb; state: Exp; lines: +2 -0 | |
57 | * actually print args for %g and %e | |
58 | * | |
59 | * date: 2002/06/03 13:37:52; author: jmcd; state: Exp; lines: +8 -0 | |
60 | * Since includes.h isn't included here, VA_COPY has to be defined here. I don't | |
61 | * see any include file that is guaranteed to be here, so I'm defining it | |
62 | * locally. Fixes AIX and Solaris builds. | |
63 | * | |
64 | * date: 2002/06/03 03:07:24; author: tridge; state: Exp; lines: +5 -13 | |
65 | * put the ifdef for HAVE_VA_COPY in one place rather than in lots of | |
66 | * functions | |
67 | * | |
68 | * date: 2002/05/17 14:51:22; author: jmcd; state: Exp; lines: +21 -4 | |
69 | * Fix usage of va_list passed as an arg. Use __va_copy before using it | |
70 | * when it exists. | |
71 | * | |
72 | * date: 2002/04/16 22:38:04; author: idra; state: Exp; lines: +20 -14 | |
73 | * Fix incorrect zpadlen handling in fmtfp. | |
74 | * Thanks to Ollie Oldham <ollie.oldham@metro-optix.com> for spotting it. | |
75 | * few mods to make it easier to compile the tests. | |
76 | * addedd the "Ollie" test to the floating point ones. | |
77 | * | |
78 | * Martin Pool (mbp@samba.org) April 2003 | |
79 | * Remove NO_CONFIG_H so that the test case can be built within a source | |
80 | * tree with less trouble. | |
81 | * Remove unnecessary SAFE_FREE() definition. | |
82 | * | |
83 | * Martin Pool (mbp@samba.org) May 2003 | |
84 | * Put in a prototype for dummy_snprintf() to quiet compiler warnings. | |
85 | * | |
86 | * Move #endif to make sure VA_COPY, LDOUBLE, etc are defined even | |
87 | * if the C library has some snprintf functions already. | |
3c0ef626 | 88 | **************************************************************/ |
89 | ||
90 | #include "includes.h" | |
91 | ||
3c0ef626 | 92 | #if defined(BROKEN_SNPRINTF) /* For those with broken snprintf() */ |
93 | # undef HAVE_SNPRINTF | |
94 | # undef HAVE_VSNPRINTF | |
95 | #endif | |
96 | ||
e00da40d | 97 | #ifndef VA_COPY |
98 | # ifdef HAVE_VA_COPY | |
99 | # define VA_COPY(dest, src) va_copy(dest, src) | |
100 | # else | |
101 | # ifdef HAVE___VA_COPY | |
102 | # define VA_COPY(dest, src) __va_copy(dest, src) | |
103 | # else | |
104 | # define VA_COPY(dest, src) (dest) = (src) | |
105 | # endif | |
106 | # endif | |
107 | #endif | |
3c0ef626 | 108 | |
e00da40d | 109 | #if !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) |
3c0ef626 | 110 | |
2e437378 | 111 | #include <ctype.h> |
112 | #include <stdarg.h> | |
113 | #include <stdlib.h> | |
114 | #include <string.h> | |
115 | ||
e00da40d | 116 | #ifdef HAVE_LONG_DOUBLE |
117 | # define LDOUBLE long double | |
118 | #else | |
119 | # define LDOUBLE double | |
120 | #endif | |
3c0ef626 | 121 | |
e00da40d | 122 | #ifdef HAVE_LONG_LONG |
123 | # define LLONG long long | |
124 | #else | |
125 | # define LLONG long | |
126 | #endif | |
3c0ef626 | 127 | |
128 | /* | |
129 | * dopr(): poor man's version of doprintf | |
130 | */ | |
131 | ||
132 | /* format read states */ | |
133 | #define DP_S_DEFAULT 0 | |
134 | #define DP_S_FLAGS 1 | |
135 | #define DP_S_MIN 2 | |
136 | #define DP_S_DOT 3 | |
137 | #define DP_S_MAX 4 | |
138 | #define DP_S_MOD 5 | |
139 | #define DP_S_CONV 6 | |
140 | #define DP_S_DONE 7 | |
141 | ||
142 | /* format flags - Bits */ | |
143 | #define DP_F_MINUS (1 << 0) | |
144 | #define DP_F_PLUS (1 << 1) | |
145 | #define DP_F_SPACE (1 << 2) | |
146 | #define DP_F_NUM (1 << 3) | |
147 | #define DP_F_ZERO (1 << 4) | |
148 | #define DP_F_UP (1 << 5) | |
149 | #define DP_F_UNSIGNED (1 << 6) | |
150 | ||
151 | /* Conversion Flags */ | |
e00da40d | 152 | #define DP_C_SHORT 1 |
153 | #define DP_C_LONG 2 | |
154 | #define DP_C_LDOUBLE 3 | |
155 | #define DP_C_LLONG 4 | |
156 | ||
157 | #define char_to_int(p) ((p)- '0') | |
158 | #ifndef MAX | |
159 | # define MAX(p,q) (((p) >= (q)) ? (p) : (q)) | |
160 | #endif | |
3c0ef626 | 161 | |
e00da40d | 162 | static size_t dopr(char *buffer, size_t maxlen, const char *format, |
163 | va_list args_in); | |
164 | static void fmtstr(char *buffer, size_t *currlen, size_t maxlen, | |
165 | char *value, int flags, int min, int max); | |
166 | static void fmtint(char *buffer, size_t *currlen, size_t maxlen, | |
2e437378 | 167 | LLONG value, int base, int min, int max, int flags); |
e00da40d | 168 | static void fmtfp(char *buffer, size_t *currlen, size_t maxlen, |
169 | LDOUBLE fvalue, int min, int max, int flags); | |
170 | static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c); | |
171 | ||
172 | static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args_in) | |
3c0ef626 | 173 | { |
e00da40d | 174 | char ch; |
175 | LLONG value; | |
176 | LDOUBLE fvalue; | |
177 | char *strvalue; | |
178 | int min; | |
179 | int max; | |
180 | int state; | |
181 | int flags; | |
182 | int cflags; | |
183 | size_t currlen; | |
184 | va_list args; | |
185 | ||
186 | VA_COPY(args, args_in); | |
187 | ||
188 | state = DP_S_DEFAULT; | |
189 | currlen = flags = cflags = min = 0; | |
190 | max = -1; | |
3c0ef626 | 191 | ch = *format++; |
e00da40d | 192 | |
3c0ef626 | 193 | while (state != DP_S_DONE) { |
e00da40d | 194 | if (ch == '\0') |
3c0ef626 | 195 | state = DP_S_DONE; |
196 | ||
197 | switch(state) { | |
70791e56 | 198 | case DP_S_DEFAULT: |
199 | if (ch == '%') | |
200 | state = DP_S_FLAGS; | |
201 | else | |
e00da40d | 202 | dopr_outch (buffer, &currlen, maxlen, ch); |
70791e56 | 203 | ch = *format++; |
204 | break; | |
205 | case DP_S_FLAGS: | |
206 | switch (ch) { | |
207 | case '-': | |
208 | flags |= DP_F_MINUS; | |
3c0ef626 | 209 | ch = *format++; |
210 | break; | |
70791e56 | 211 | case '+': |
212 | flags |= DP_F_PLUS; | |
213 | ch = *format++; | |
3c0ef626 | 214 | break; |
70791e56 | 215 | case ' ': |
216 | flags |= DP_F_SPACE; | |
217 | ch = *format++; | |
3c0ef626 | 218 | break; |
70791e56 | 219 | case '#': |
220 | flags |= DP_F_NUM; | |
221 | ch = *format++; | |
3c0ef626 | 222 | break; |
70791e56 | 223 | case '0': |
224 | flags |= DP_F_ZERO; | |
225 | ch = *format++; | |
3c0ef626 | 226 | break; |
70791e56 | 227 | default: |
228 | state = DP_S_MIN; | |
3c0ef626 | 229 | break; |
70791e56 | 230 | } |
231 | break; | |
232 | case DP_S_MIN: | |
233 | if (isdigit((unsigned char)ch)) { | |
e00da40d | 234 | min = 10*min + char_to_int (ch); |
70791e56 | 235 | ch = *format++; |
236 | } else if (ch == '*') { | |
237 | min = va_arg (args, int); | |
238 | ch = *format++; | |
239 | state = DP_S_DOT; | |
e00da40d | 240 | } else { |
70791e56 | 241 | state = DP_S_DOT; |
e00da40d | 242 | } |
70791e56 | 243 | break; |
244 | case DP_S_DOT: | |
245 | if (ch == '.') { | |
246 | state = DP_S_MAX; | |
247 | ch = *format++; | |
e00da40d | 248 | } else { |
70791e56 | 249 | state = DP_S_MOD; |
e00da40d | 250 | } |
70791e56 | 251 | break; |
252 | case DP_S_MAX: | |
253 | if (isdigit((unsigned char)ch)) { | |
254 | if (max < 0) | |
255 | max = 0; | |
e00da40d | 256 | max = 10*max + char_to_int (ch); |
70791e56 | 257 | ch = *format++; |
258 | } else if (ch == '*') { | |
259 | max = va_arg (args, int); | |
260 | ch = *format++; | |
261 | state = DP_S_MOD; | |
e00da40d | 262 | } else { |
70791e56 | 263 | state = DP_S_MOD; |
e00da40d | 264 | } |
70791e56 | 265 | break; |
266 | case DP_S_MOD: | |
267 | switch (ch) { | |
268 | case 'h': | |
269 | cflags = DP_C_SHORT; | |
270 | ch = *format++; | |
271 | break; | |
272 | case 'l': | |
273 | cflags = DP_C_LONG; | |
274 | ch = *format++; | |
e00da40d | 275 | if (ch == 'l') { /* It's a long long */ |
276 | cflags = DP_C_LLONG; | |
70791e56 | 277 | ch = *format++; |
3c0ef626 | 278 | } |
70791e56 | 279 | break; |
70791e56 | 280 | case 'L': |
281 | cflags = DP_C_LDOUBLE; | |
3c0ef626 | 282 | ch = *format++; |
3c0ef626 | 283 | break; |
70791e56 | 284 | default: |
285 | break; | |
286 | } | |
287 | state = DP_S_CONV; | |
288 | break; | |
289 | case DP_S_CONV: | |
290 | switch (ch) { | |
291 | case 'd': | |
292 | case 'i': | |
293 | if (cflags == DP_C_SHORT) | |
e00da40d | 294 | value = va_arg (args, int); |
70791e56 | 295 | else if (cflags == DP_C_LONG) |
e00da40d | 296 | value = va_arg (args, long int); |
297 | else if (cflags == DP_C_LLONG) | |
298 | value = va_arg (args, LLONG); | |
70791e56 | 299 | else |
300 | value = va_arg (args, int); | |
e00da40d | 301 | fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags); |
70791e56 | 302 | break; |
303 | case 'o': | |
304 | flags |= DP_F_UNSIGNED; | |
305 | if (cflags == DP_C_SHORT) | |
e00da40d | 306 | value = va_arg (args, unsigned int); |
70791e56 | 307 | else if (cflags == DP_C_LONG) |
e00da40d | 308 | value = (long)va_arg (args, unsigned long int); |
309 | else if (cflags == DP_C_LLONG) | |
310 | value = (long)va_arg (args, unsigned LLONG); | |
70791e56 | 311 | else |
e00da40d | 312 | value = (long)va_arg (args, unsigned int); |
313 | fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags); | |
3c0ef626 | 314 | break; |
70791e56 | 315 | case 'u': |
316 | flags |= DP_F_UNSIGNED; | |
317 | if (cflags == DP_C_SHORT) | |
e00da40d | 318 | value = va_arg (args, unsigned int); |
70791e56 | 319 | else if (cflags == DP_C_LONG) |
e00da40d | 320 | value = (long)va_arg (args, unsigned long int); |
321 | else if (cflags == DP_C_LLONG) | |
322 | value = (LLONG)va_arg (args, unsigned LLONG); | |
70791e56 | 323 | else |
e00da40d | 324 | value = (long)va_arg (args, unsigned int); |
70791e56 | 325 | fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags); |
326 | break; | |
327 | case 'X': | |
328 | flags |= DP_F_UP; | |
329 | case 'x': | |
330 | flags |= DP_F_UNSIGNED; | |
331 | if (cflags == DP_C_SHORT) | |
e00da40d | 332 | value = va_arg (args, unsigned int); |
70791e56 | 333 | else if (cflags == DP_C_LONG) |
e00da40d | 334 | value = (long)va_arg (args, unsigned long int); |
335 | else if (cflags == DP_C_LLONG) | |
336 | value = (LLONG)va_arg (args, unsigned LLONG); | |
70791e56 | 337 | else |
e00da40d | 338 | value = (long)va_arg (args, unsigned int); |
339 | fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags); | |
70791e56 | 340 | break; |
341 | case 'f': | |
342 | if (cflags == DP_C_LDOUBLE) | |
e00da40d | 343 | fvalue = va_arg (args, LDOUBLE); |
70791e56 | 344 | else |
e00da40d | 345 | fvalue = va_arg (args, double); |
70791e56 | 346 | /* um, floating point? */ |
e00da40d | 347 | fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags); |
70791e56 | 348 | break; |
349 | case 'E': | |
350 | flags |= DP_F_UP; | |
351 | case 'e': | |
352 | if (cflags == DP_C_LDOUBLE) | |
e00da40d | 353 | fvalue = va_arg (args, LDOUBLE); |
70791e56 | 354 | else |
e00da40d | 355 | fvalue = va_arg (args, double); |
356 | fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags); | |
70791e56 | 357 | break; |
358 | case 'G': | |
359 | flags |= DP_F_UP; | |
360 | case 'g': | |
361 | if (cflags == DP_C_LDOUBLE) | |
e00da40d | 362 | fvalue = va_arg (args, LDOUBLE); |
70791e56 | 363 | else |
e00da40d | 364 | fvalue = va_arg (args, double); |
365 | fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags); | |
70791e56 | 366 | break; |
367 | case 'c': | |
e00da40d | 368 | dopr_outch (buffer, &currlen, maxlen, va_arg (args, int)); |
70791e56 | 369 | break; |
370 | case 's': | |
e00da40d | 371 | strvalue = va_arg (args, char *); |
372 | if (!strvalue) strvalue = "(NULL)"; | |
373 | if (max == -1) { | |
374 | max = strlen(strvalue); | |
375 | } | |
376 | if (min > 0 && max >= 0 && min > max) max = min; | |
377 | fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max); | |
70791e56 | 378 | break; |
379 | case 'p': | |
e00da40d | 380 | strvalue = va_arg (args, void *); |
381 | fmtint (buffer, &currlen, maxlen, (long) strvalue, 16, min, max, flags); | |
70791e56 | 382 | break; |
383 | case 'n': | |
384 | if (cflags == DP_C_SHORT) { | |
385 | short int *num; | |
e00da40d | 386 | num = va_arg (args, short int *); |
70791e56 | 387 | *num = currlen; |
388 | } else if (cflags == DP_C_LONG) { | |
389 | long int *num; | |
e00da40d | 390 | num = va_arg (args, long int *); |
391 | *num = (long int)currlen; | |
392 | } else if (cflags == DP_C_LLONG) { | |
393 | LLONG *num; | |
394 | num = va_arg (args, LLONG *); | |
395 | *num = (LLONG)currlen; | |
70791e56 | 396 | } else { |
397 | int *num; | |
e00da40d | 398 | num = va_arg (args, int *); |
70791e56 | 399 | *num = currlen; |
400 | } | |
401 | break; | |
402 | case '%': | |
e00da40d | 403 | dopr_outch (buffer, &currlen, maxlen, ch); |
70791e56 | 404 | break; |
e00da40d | 405 | case 'w': |
406 | /* not supported yet, treat as next char */ | |
70791e56 | 407 | ch = *format++; |
408 | break; | |
e00da40d | 409 | default: |
410 | /* Unknown, skip */ | |
411 | break; | |
70791e56 | 412 | } |
413 | ch = *format++; | |
414 | state = DP_S_DEFAULT; | |
415 | flags = cflags = min = 0; | |
416 | max = -1; | |
417 | break; | |
418 | case DP_S_DONE: | |
419 | break; | |
e00da40d | 420 | default: |
421 | /* hmm? */ | |
70791e56 | 422 | break; /* some picky compilers need this */ |
3c0ef626 | 423 | } |
424 | } | |
e00da40d | 425 | if (maxlen != 0) { |
426 | if (currlen < maxlen - 1) | |
427 | buffer[currlen] = '\0'; | |
428 | else if (maxlen > 0) | |
429 | buffer[maxlen - 1] = '\0'; | |
430 | } | |
431 | ||
432 | return currlen; | |
3c0ef626 | 433 | } |
434 | ||
e00da40d | 435 | static void fmtstr(char *buffer, size_t *currlen, size_t maxlen, |
436 | char *value, int flags, int min, int max) | |
3c0ef626 | 437 | { |
e00da40d | 438 | int padlen, strln; /* amount to pad */ |
439 | int cnt = 0; | |
440 | ||
441 | #ifdef DEBUG_SNPRINTF | |
442 | printf("fmtstr min=%d max=%d s=[%s]\n", min, max, value); | |
443 | #endif | |
444 | if (value == 0) { | |
3c0ef626 | 445 | value = "<NULL>"; |
e00da40d | 446 | } |
3c0ef626 | 447 | |
34fee935 | 448 | for (strln = 0; strln < max && value[strln]; ++strln); /* strlen */ |
3c0ef626 | 449 | padlen = min - strln; |
450 | if (padlen < 0) | |
451 | padlen = 0; | |
452 | if (flags & DP_F_MINUS) | |
453 | padlen = -padlen; /* Left Justify */ | |
e00da40d | 454 | |
3c0ef626 | 455 | while ((padlen > 0) && (cnt < max)) { |
e00da40d | 456 | dopr_outch (buffer, currlen, maxlen, ' '); |
3c0ef626 | 457 | --padlen; |
458 | ++cnt; | |
459 | } | |
460 | while (*value && (cnt < max)) { | |
e00da40d | 461 | dopr_outch (buffer, currlen, maxlen, *value++); |
3c0ef626 | 462 | ++cnt; |
463 | } | |
464 | while ((padlen < 0) && (cnt < max)) { | |
e00da40d | 465 | dopr_outch (buffer, currlen, maxlen, ' '); |
3c0ef626 | 466 | ++padlen; |
467 | ++cnt; | |
468 | } | |
469 | } | |
470 | ||
471 | /* Have to handle DP_F_NUM (ie 0x and 0 alternates) */ | |
472 | ||
e00da40d | 473 | static void fmtint(char *buffer, size_t *currlen, size_t maxlen, |
2e437378 | 474 | LLONG value, int base, int min, int max, int flags) |
3c0ef626 | 475 | { |
e00da40d | 476 | int signvalue = 0; |
2e437378 | 477 | unsigned LLONG uvalue; |
3c0ef626 | 478 | char convert[20]; |
e00da40d | 479 | int place = 0; |
3c0ef626 | 480 | int spadlen = 0; /* amount to space pad */ |
481 | int zpadlen = 0; /* amount to zero pad */ | |
e00da40d | 482 | int caps = 0; |
483 | ||
3c0ef626 | 484 | if (max < 0) |
485 | max = 0; | |
e00da40d | 486 | |
3c0ef626 | 487 | uvalue = value; |
e00da40d | 488 | |
489 | if(!(flags & DP_F_UNSIGNED)) { | |
490 | if( value < 0 ) { | |
3c0ef626 | 491 | signvalue = '-'; |
492 | uvalue = -value; | |
e00da40d | 493 | } else { |
494 | if (flags & DP_F_PLUS) /* Do a sign (+/i) */ | |
495 | signvalue = '+'; | |
496 | else if (flags & DP_F_SPACE) | |
497 | signvalue = ' '; | |
498 | } | |
3c0ef626 | 499 | } |
500 | ||
e00da40d | 501 | if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */ |
502 | ||
3c0ef626 | 503 | do { |
504 | convert[place++] = | |
e00da40d | 505 | (caps? "0123456789ABCDEF":"0123456789abcdef") |
506 | [uvalue % (unsigned)base ]; | |
3c0ef626 | 507 | uvalue = (uvalue / (unsigned)base ); |
e00da40d | 508 | } while(uvalue && (place < 20)); |
509 | if (place == 20) place--; | |
3c0ef626 | 510 | convert[place] = 0; |
511 | ||
512 | zpadlen = max - place; | |
513 | spadlen = min - MAX (max, place) - (signvalue ? 1 : 0); | |
e00da40d | 514 | if (zpadlen < 0) zpadlen = 0; |
515 | if (spadlen < 0) spadlen = 0; | |
3c0ef626 | 516 | if (flags & DP_F_ZERO) { |
517 | zpadlen = MAX(zpadlen, spadlen); | |
518 | spadlen = 0; | |
519 | } | |
520 | if (flags & DP_F_MINUS) | |
521 | spadlen = -spadlen; /* Left Justifty */ | |
522 | ||
e00da40d | 523 | #ifdef DEBUG_SNPRINTF |
524 | printf("zpad: %d, spad: %d, min: %d, max: %d, place: %d\n", | |
525 | zpadlen, spadlen, min, max, place); | |
526 | #endif | |
527 | ||
3c0ef626 | 528 | /* Spaces */ |
529 | while (spadlen > 0) { | |
e00da40d | 530 | dopr_outch (buffer, currlen, maxlen, ' '); |
3c0ef626 | 531 | --spadlen; |
532 | } | |
533 | ||
534 | /* Sign */ | |
535 | if (signvalue) | |
e00da40d | 536 | dopr_outch (buffer, currlen, maxlen, signvalue); |
3c0ef626 | 537 | |
538 | /* Zeros */ | |
539 | if (zpadlen > 0) { | |
540 | while (zpadlen > 0) { | |
e00da40d | 541 | dopr_outch (buffer, currlen, maxlen, '0'); |
3c0ef626 | 542 | --zpadlen; |
543 | } | |
544 | } | |
545 | ||
546 | /* Digits */ | |
547 | while (place > 0) | |
e00da40d | 548 | dopr_outch (buffer, currlen, maxlen, convert[--place]); |
3c0ef626 | 549 | |
550 | /* Left Justified spaces */ | |
551 | while (spadlen < 0) { | |
552 | dopr_outch (buffer, currlen, maxlen, ' '); | |
553 | ++spadlen; | |
554 | } | |
555 | } | |
556 | ||
e00da40d | 557 | static LDOUBLE abs_val(LDOUBLE value) |
3c0ef626 | 558 | { |
e00da40d | 559 | LDOUBLE result = value; |
560 | ||
561 | if (value < 0) | |
562 | result = -value; | |
563 | ||
564 | return result; | |
565 | } | |
3c0ef626 | 566 | |
e00da40d | 567 | static LDOUBLE POW10(int exp) |
568 | { | |
569 | LDOUBLE result = 1; | |
570 | ||
3c0ef626 | 571 | while (exp) { |
572 | result *= 10; | |
573 | exp--; | |
574 | } | |
575 | ||
576 | return result; | |
577 | } | |
578 | ||
e00da40d | 579 | static LLONG ROUND(LDOUBLE value) |
3c0ef626 | 580 | { |
e00da40d | 581 | LLONG intpart; |
3c0ef626 | 582 | |
e00da40d | 583 | intpart = (LLONG)value; |
584 | value = value - intpart; | |
585 | if (value >= 0.5) intpart++; | |
586 | ||
3c0ef626 | 587 | return intpart; |
588 | } | |
589 | ||
e00da40d | 590 | /* a replacement for modf that doesn't need the math library. Should |
591 | be portable, but slow */ | |
592 | static double my_modf(double x0, double *iptr) | |
3c0ef626 | 593 | { |
e00da40d | 594 | int i; |
595 | long l; | |
596 | double x = x0; | |
597 | double f = 1.0; | |
598 | ||
599 | for (i=0;i<100;i++) { | |
600 | l = (long)x; | |
601 | if (l <= (x+1) && l >= (x-1)) break; | |
602 | x *= 0.1; | |
603 | f *= 10.0; | |
604 | } | |
605 | ||
606 | if (i == 100) { | |
607 | /* yikes! the number is beyond what we can handle. What do we do? */ | |
608 | (*iptr) = 0; | |
609 | return 0; | |
610 | } | |
611 | ||
612 | if (i != 0) { | |
613 | double i2; | |
614 | double ret; | |
615 | ||
616 | ret = my_modf(x0-l*f, &i2); | |
617 | (*iptr) = l*f + i2; | |
618 | return ret; | |
619 | } | |
620 | ||
621 | (*iptr) = l; | |
622 | return x - (*iptr); | |
623 | } | |
624 | ||
625 | ||
626 | static void fmtfp (char *buffer, size_t *currlen, size_t maxlen, | |
627 | LDOUBLE fvalue, int min, int max, int flags) | |
628 | { | |
629 | int signvalue = 0; | |
630 | double ufvalue; | |
631 | char iconvert[311]; | |
632 | char fconvert[311]; | |
633 | int iplace = 0; | |
634 | int fplace = 0; | |
3c0ef626 | 635 | int padlen = 0; /* amount to pad */ |
e00da40d | 636 | int zpadlen = 0; |
637 | int caps = 0; | |
638 | int idx; | |
639 | double intpart; | |
640 | double fracpart; | |
641 | double temp; | |
3c0ef626 | 642 | |
643 | /* | |
644 | * AIX manpage says the default is 0, but Solaris says the default | |
645 | * is 6, and sprintf on AIX defaults to 6 | |
646 | */ | |
647 | if (max < 0) | |
648 | max = 6; | |
649 | ||
e00da40d | 650 | ufvalue = abs_val (fvalue); |
3c0ef626 | 651 | |
e00da40d | 652 | if (fvalue < 0) { |
3c0ef626 | 653 | signvalue = '-'; |
e00da40d | 654 | } else { |
655 | if (flags & DP_F_PLUS) { /* Do a sign (+/i) */ | |
656 | signvalue = '+'; | |
657 | } else { | |
658 | if (flags & DP_F_SPACE) | |
659 | signvalue = ' '; | |
660 | } | |
661 | } | |
3c0ef626 | 662 | |
e00da40d | 663 | #if 0 |
664 | if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */ | |
665 | #endif | |
666 | ||
667 | #if 0 | |
668 | if (max == 0) ufvalue += 0.5; /* if max = 0 we must round */ | |
669 | #endif | |
3c0ef626 | 670 | |
671 | /* | |
e00da40d | 672 | * Sorry, we only support 16 digits past the decimal because of our |
3c0ef626 | 673 | * conversion method |
674 | */ | |
e00da40d | 675 | if (max > 16) |
676 | max = 16; | |
3c0ef626 | 677 | |
678 | /* We "cheat" by converting the fractional part to integer by | |
679 | * multiplying by a factor of 10 | |
680 | */ | |
3c0ef626 | 681 | |
e00da40d | 682 | temp = ufvalue; |
683 | my_modf(temp, &intpart); | |
684 | ||
685 | fracpart = ROUND((POW10(max)) * (ufvalue - intpart)); | |
686 | ||
687 | if (fracpart >= POW10(max)) { | |
3c0ef626 | 688 | intpart++; |
e00da40d | 689 | fracpart -= POW10(max); |
3c0ef626 | 690 | } |
691 | ||
692 | /* Convert integer part */ | |
693 | do { | |
e00da40d | 694 | temp = intpart*0.1; |
695 | my_modf(temp, &intpart); | |
696 | idx = (int) ((temp -intpart +0.05)* 10.0); | |
697 | /* idx = (int) (((double)(temp*0.1) -intpart +0.05) *10.0); */ | |
698 | /* printf ("%llf, %f, %x\n", temp, intpart, idx); */ | |
3c0ef626 | 699 | iconvert[iplace++] = |
e00da40d | 700 | (caps? "0123456789ABCDEF":"0123456789abcdef")[idx]; |
701 | } while (intpart && (iplace < 311)); | |
702 | if (iplace == 311) iplace--; | |
3c0ef626 | 703 | iconvert[iplace] = 0; |
704 | ||
705 | /* Convert fractional part */ | |
e00da40d | 706 | if (fracpart) |
707 | { | |
708 | do { | |
709 | temp = fracpart*0.1; | |
710 | my_modf(temp, &fracpart); | |
711 | idx = (int) ((temp -fracpart +0.05)* 10.0); | |
712 | /* idx = (int) ((((temp/10) -fracpart) +0.05) *10); */ | |
713 | /* printf ("%lf, %lf, %ld\n", temp, fracpart, idx ); */ | |
714 | fconvert[fplace++] = | |
715 | (caps? "0123456789ABCDEF":"0123456789abcdef")[idx]; | |
716 | } while(fracpart && (fplace < 311)); | |
717 | if (fplace == 311) fplace--; | |
718 | } | |
3c0ef626 | 719 | fconvert[fplace] = 0; |
e00da40d | 720 | |
3c0ef626 | 721 | /* -1 for decimal point, another -1 if we are printing a sign */ |
722 | padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0); | |
723 | zpadlen = max - fplace; | |
e00da40d | 724 | if (zpadlen < 0) zpadlen = 0; |
3c0ef626 | 725 | if (padlen < 0) |
726 | padlen = 0; | |
727 | if (flags & DP_F_MINUS) | |
728 | padlen = -padlen; /* Left Justifty */ | |
e00da40d | 729 | |
3c0ef626 | 730 | if ((flags & DP_F_ZERO) && (padlen > 0)) { |
731 | if (signvalue) { | |
e00da40d | 732 | dopr_outch (buffer, currlen, maxlen, signvalue); |
3c0ef626 | 733 | --padlen; |
734 | signvalue = 0; | |
735 | } | |
736 | while (padlen > 0) { | |
e00da40d | 737 | dopr_outch (buffer, currlen, maxlen, '0'); |
3c0ef626 | 738 | --padlen; |
739 | } | |
740 | } | |
741 | while (padlen > 0) { | |
e00da40d | 742 | dopr_outch (buffer, currlen, maxlen, ' '); |
3c0ef626 | 743 | --padlen; |
744 | } | |
745 | if (signvalue) | |
e00da40d | 746 | dopr_outch (buffer, currlen, maxlen, signvalue); |
747 | ||
3c0ef626 | 748 | while (iplace > 0) |
e00da40d | 749 | dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]); |
750 | ||
751 | #ifdef DEBUG_SNPRINTF | |
752 | printf("fmtfp: fplace=%d zpadlen=%d\n", fplace, zpadlen); | |
753 | #endif | |
3c0ef626 | 754 | |
755 | /* | |
e00da40d | 756 | * Decimal point. This should probably use locale to find the correct |
757 | * char to print out. | |
3c0ef626 | 758 | */ |
e00da40d | 759 | if (max > 0) { |
760 | dopr_outch (buffer, currlen, maxlen, '.'); | |
761 | ||
762 | while (zpadlen > 0) { | |
763 | dopr_outch (buffer, currlen, maxlen, '0'); | |
764 | --zpadlen; | |
765 | } | |
3c0ef626 | 766 | |
e00da40d | 767 | while (fplace > 0) |
768 | dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]); | |
3c0ef626 | 769 | } |
770 | ||
771 | while (padlen < 0) { | |
e00da40d | 772 | dopr_outch (buffer, currlen, maxlen, ' '); |
3c0ef626 | 773 | ++padlen; |
774 | } | |
775 | } | |
776 | ||
e00da40d | 777 | static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c) |
3c0ef626 | 778 | { |
e00da40d | 779 | if (*currlen < maxlen) { |
780 | buffer[(*currlen)] = c; | |
781 | } | |
782 | (*currlen)++; | |
3c0ef626 | 783 | } |
784 | #endif /* !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) */ | |
785 | ||
e00da40d | 786 | #if !defined(HAVE_VSNPRINTF) |
787 | int vsnprintf (char *str, size_t count, const char *fmt, va_list args) | |
3c0ef626 | 788 | { |
e00da40d | 789 | return dopr(str, count, fmt, args); |
3c0ef626 | 790 | } |
e00da40d | 791 | #endif |
3c0ef626 | 792 | |
e00da40d | 793 | #if !defined(HAVE_SNPRINTF) |
794 | int snprintf(char *str, size_t count, SNPRINTF_CONST char *fmt, ...) | |
3c0ef626 | 795 | { |
e00da40d | 796 | size_t ret; |
3c0ef626 | 797 | va_list ap; |
798 | ||
799 | va_start(ap, fmt); | |
e00da40d | 800 | ret = vsnprintf(str, count, fmt, ap); |
3c0ef626 | 801 | va_end(ap); |
e00da40d | 802 | return ret; |
3c0ef626 | 803 | } |
e00da40d | 804 | #endif |
3c0ef626 | 805 |