Blob


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