this now works as a add|delete share command :-)
[Samba.git] / source / lib / snprintf.c
blob3edb50c6adb9c5749beab51be9f2e04b0efcd243
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 */
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...
15 * snprintf() is used instead of sprintf() as it does limit checks
16 * for string length. This covers a nasty loophole.
18 * The other functions are there to prevent NULL pointers from
19 * causing nast effects.
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.
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.
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.
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.
48 * Andrew Tridgell (tridge@samba.org) Oct 1998
49 * fixed handling of %.0f
50 * added test for HAVE_LONG_DOUBLE
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
56 **************************************************************/
58 #ifndef NO_CONFIG_H /* for some tests */
59 #include "config.h"
60 #endif
62 #ifdef HAVE_STRING_H
63 #include <string.h>
64 #endif
66 #ifdef HAVE_STRINGS_H
67 #include <strings.h>
68 #endif
69 #ifdef HAVE_CTYPE_H
70 #include <ctype.h>
71 #endif
72 #include <sys/types.h>
73 #include <stdarg.h>
74 #include <math.h>
75 #ifdef HAVE_STDLIB_H
76 #include <stdlib.h>
77 #endif
79 #if defined(HAVE_SNPRINTF) && defined(HAVE_VSNPRINTF) && defined(HAVE_C99_VSNPRINTF)
80 /* only include stdio.h if we are not re-defining snprintf or vsnprintf */
81 #include <stdio.h>
82 /* make the compiler happy with an empty file */
83 void dummy_snprintf(void) {}
84 #else
86 #ifdef HAVE_LONG_DOUBLE
87 #define LDOUBLE long double
88 #else
89 #define LDOUBLE double
90 #endif
92 #ifdef HAVE_LONG_LONG
93 #define LLONG long long
94 #else
95 #define LLONG long
96 #endif
98 static size_t dopr(char *buffer, size_t maxlen, const char *format,
99 va_list args);
100 static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
101 char *value, int flags, int min, int max);
102 static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
103 long value, int base, int min, int max, int flags);
104 static void fmtfp(char *buffer, size_t *currlen, size_t maxlen,
105 LDOUBLE fvalue, int min, int max, int flags);
106 static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c);
109 * dopr(): poor man's version of doprintf
112 /* format read states */
113 #define DP_S_DEFAULT 0
114 #define DP_S_FLAGS 1
115 #define DP_S_MIN 2
116 #define DP_S_DOT 3
117 #define DP_S_MAX 4
118 #define DP_S_MOD 5
119 #define DP_S_CONV 6
120 #define DP_S_DONE 7
122 /* format flags - Bits */
123 #define DP_F_MINUS (1 << 0)
124 #define DP_F_PLUS (1 << 1)
125 #define DP_F_SPACE (1 << 2)
126 #define DP_F_NUM (1 << 3)
127 #define DP_F_ZERO (1 << 4)
128 #define DP_F_UP (1 << 5)
129 #define DP_F_UNSIGNED (1 << 6)
131 /* Conversion Flags */
132 #define DP_C_SHORT 1
133 #define DP_C_LONG 2
134 #define DP_C_LDOUBLE 3
135 #define DP_C_LLONG 4
137 #define char_to_int(p) ((p)- '0')
138 #ifndef MAX
139 #define MAX(p,q) (((p) >= (q)) ? (p) : (q))
140 #endif
142 static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args)
144 char ch;
145 LLONG value;
146 LDOUBLE fvalue;
147 char *strvalue;
148 int min;
149 int max;
150 int state;
151 int flags;
152 int cflags;
153 size_t currlen;
155 state = DP_S_DEFAULT;
156 currlen = flags = cflags = min = 0;
157 max = -1;
158 ch = *format++;
160 while (state != DP_S_DONE) {
161 if (ch == '\0')
162 state = DP_S_DONE;
164 switch(state) {
165 case DP_S_DEFAULT:
166 if (ch == '%')
167 state = DP_S_FLAGS;
168 else
169 dopr_outch (buffer, &currlen, maxlen, ch);
170 ch = *format++;
171 break;
172 case DP_S_FLAGS:
173 switch (ch) {
174 case '-':
175 flags |= DP_F_MINUS;
176 ch = *format++;
177 break;
178 case '+':
179 flags |= DP_F_PLUS;
180 ch = *format++;
181 break;
182 case ' ':
183 flags |= DP_F_SPACE;
184 ch = *format++;
185 break;
186 case '#':
187 flags |= DP_F_NUM;
188 ch = *format++;
189 break;
190 case '0':
191 flags |= DP_F_ZERO;
192 ch = *format++;
193 break;
194 default:
195 state = DP_S_MIN;
196 break;
198 break;
199 case DP_S_MIN:
200 if (isdigit((unsigned char)ch)) {
201 min = 10*min + char_to_int (ch);
202 ch = *format++;
203 } else if (ch == '*') {
204 min = va_arg (args, int);
205 ch = *format++;
206 state = DP_S_DOT;
207 } else {
208 state = DP_S_DOT;
210 break;
211 case DP_S_DOT:
212 if (ch == '.') {
213 state = DP_S_MAX;
214 ch = *format++;
215 } else {
216 state = DP_S_MOD;
218 break;
219 case DP_S_MAX:
220 if (isdigit((unsigned char)ch)) {
221 if (max < 0)
222 max = 0;
223 max = 10*max + char_to_int (ch);
224 ch = *format++;
225 } else if (ch == '*') {
226 max = va_arg (args, int);
227 ch = *format++;
228 state = DP_S_MOD;
229 } else {
230 state = DP_S_MOD;
232 break;
233 case DP_S_MOD:
234 switch (ch) {
235 case 'h':
236 cflags = DP_C_SHORT;
237 ch = *format++;
238 break;
239 case 'l':
240 cflags = DP_C_LONG;
241 ch = *format++;
242 if (ch == 'l') { /* It's a long long */
243 cflags = DP_C_LLONG;
244 ch = *format++;
246 break;
247 case 'L':
248 cflags = DP_C_LDOUBLE;
249 ch = *format++;
250 break;
251 default:
252 break;
254 state = DP_S_CONV;
255 break;
256 case DP_S_CONV:
257 switch (ch) {
258 case 'd':
259 case 'i':
260 if (cflags == DP_C_SHORT)
261 value = va_arg (args, int);
262 else if (cflags == DP_C_LONG)
263 value = va_arg (args, long int);
264 else if (cflags == DP_C_LLONG)
265 value = va_arg (args, LLONG);
266 else
267 value = va_arg (args, int);
268 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
269 break;
270 case 'o':
271 flags |= DP_F_UNSIGNED;
272 if (cflags == DP_C_SHORT)
273 value = va_arg (args, unsigned int);
274 else if (cflags == DP_C_LONG)
275 value = (long)va_arg (args, unsigned long int);
276 else if (cflags == DP_C_LLONG)
277 value = (long)va_arg (args, unsigned LLONG);
278 else
279 value = (long)va_arg (args, unsigned int);
280 fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags);
281 break;
282 case 'u':
283 flags |= DP_F_UNSIGNED;
284 if (cflags == DP_C_SHORT)
285 value = va_arg (args, unsigned int);
286 else if (cflags == DP_C_LONG)
287 value = (long)va_arg (args, unsigned long int);
288 else if (cflags == DP_C_LLONG)
289 value = (LLONG)va_arg (args, unsigned LLONG);
290 else
291 value = (long)va_arg (args, unsigned int);
292 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
293 break;
294 case 'X':
295 flags |= DP_F_UP;
296 case 'x':
297 flags |= DP_F_UNSIGNED;
298 if (cflags == DP_C_SHORT)
299 value = va_arg (args, unsigned int);
300 else if (cflags == DP_C_LONG)
301 value = (long)va_arg (args, unsigned long int);
302 else if (cflags == DP_C_LLONG)
303 value = (LLONG)va_arg (args, unsigned LLONG);
304 else
305 value = (long)va_arg (args, unsigned int);
306 fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags);
307 break;
308 case 'f':
309 if (cflags == DP_C_LDOUBLE)
310 fvalue = va_arg (args, LDOUBLE);
311 else
312 fvalue = va_arg (args, double);
313 /* um, floating point? */
314 fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
315 break;
316 case 'E':
317 flags |= DP_F_UP;
318 case 'e':
319 if (cflags == DP_C_LDOUBLE)
320 fvalue = va_arg (args, LDOUBLE);
321 else
322 fvalue = va_arg (args, double);
323 break;
324 case 'G':
325 flags |= DP_F_UP;
326 case 'g':
327 if (cflags == DP_C_LDOUBLE)
328 fvalue = va_arg (args, LDOUBLE);
329 else
330 fvalue = va_arg (args, double);
331 break;
332 case 'c':
333 dopr_outch (buffer, &currlen, maxlen, va_arg (args, int));
334 break;
335 case 's':
336 strvalue = va_arg (args, char *);
337 if (max == -1) {
338 max = strlen(strvalue);
340 if (min > 0 && max >= 0 && min > max) max = min;
341 fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max);
342 break;
343 case 'p':
344 strvalue = va_arg (args, void *);
345 fmtint (buffer, &currlen, maxlen, (long) strvalue, 16, min, max, flags);
346 break;
347 case 'n':
348 if (cflags == DP_C_SHORT) {
349 short int *num;
350 num = va_arg (args, short int *);
351 *num = currlen;
352 } else if (cflags == DP_C_LONG) {
353 long int *num;
354 num = va_arg (args, long int *);
355 *num = (long int)currlen;
356 } else if (cflags == DP_C_LLONG) {
357 LLONG *num;
358 num = va_arg (args, LLONG *);
359 *num = (LLONG)currlen;
360 } else {
361 int *num;
362 num = va_arg (args, int *);
363 *num = currlen;
365 break;
366 case '%':
367 dopr_outch (buffer, &currlen, maxlen, ch);
368 break;
369 case 'w':
370 /* not supported yet, treat as next char */
371 ch = *format++;
372 break;
373 default:
374 /* Unknown, skip */
375 break;
377 ch = *format++;
378 state = DP_S_DEFAULT;
379 flags = cflags = min = 0;
380 max = -1;
381 break;
382 case DP_S_DONE:
383 break;
384 default:
385 /* hmm? */
386 break; /* some picky compilers need this */
389 if (maxlen != 0) {
390 if (currlen < maxlen - 1)
391 buffer[currlen] = '\0';
392 else if (maxlen > 0)
393 buffer[maxlen - 1] = '\0';
396 return currlen;
399 static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
400 char *value, int flags, int min, int max)
402 int padlen, strln; /* amount to pad */
403 int cnt = 0;
405 #ifdef DEBUG_SNPRINTF
406 printf("fmtstr min=%d max=%d s=[%s]\n", min, max, value);
407 #endif
408 if (value == 0) {
409 value = "<NULL>";
412 for (strln = 0; value[strln]; ++strln); /* strlen */
413 padlen = min - strln;
414 if (padlen < 0)
415 padlen = 0;
416 if (flags & DP_F_MINUS)
417 padlen = -padlen; /* Left Justify */
419 while ((padlen > 0) && (cnt < max)) {
420 dopr_outch (buffer, currlen, maxlen, ' ');
421 --padlen;
422 ++cnt;
424 while (*value && (cnt < max)) {
425 dopr_outch (buffer, currlen, maxlen, *value++);
426 ++cnt;
428 while ((padlen < 0) && (cnt < max)) {
429 dopr_outch (buffer, currlen, maxlen, ' ');
430 ++padlen;
431 ++cnt;
435 /* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
437 static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
438 long value, int base, int min, int max, int flags)
440 int signvalue = 0;
441 unsigned long uvalue;
442 char convert[20];
443 int place = 0;
444 int spadlen = 0; /* amount to space pad */
445 int zpadlen = 0; /* amount to zero pad */
446 int caps = 0;
448 if (max < 0)
449 max = 0;
451 uvalue = value;
453 if(!(flags & DP_F_UNSIGNED)) {
454 if( value < 0 ) {
455 signvalue = '-';
456 uvalue = -value;
457 } else {
458 if (flags & DP_F_PLUS) /* Do a sign (+/i) */
459 signvalue = '+';
460 else if (flags & DP_F_SPACE)
461 signvalue = ' ';
465 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
467 do {
468 convert[place++] =
469 (caps? "0123456789ABCDEF":"0123456789abcdef")
470 [uvalue % (unsigned)base ];
471 uvalue = (uvalue / (unsigned)base );
472 } while(uvalue && (place < 20));
473 if (place == 20) place--;
474 convert[place] = 0;
476 zpadlen = max - place;
477 spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
478 if (zpadlen < 0) zpadlen = 0;
479 if (spadlen < 0) spadlen = 0;
480 if (flags & DP_F_ZERO) {
481 zpadlen = MAX(zpadlen, spadlen);
482 spadlen = 0;
484 if (flags & DP_F_MINUS)
485 spadlen = -spadlen; /* Left Justifty */
487 #ifdef DEBUG_SNPRINTF
488 printf("zpad: %d, spad: %d, min: %d, max: %d, place: %d\n",
489 zpadlen, spadlen, min, max, place);
490 #endif
492 /* Spaces */
493 while (spadlen > 0) {
494 dopr_outch (buffer, currlen, maxlen, ' ');
495 --spadlen;
498 /* Sign */
499 if (signvalue)
500 dopr_outch (buffer, currlen, maxlen, signvalue);
502 /* Zeros */
503 if (zpadlen > 0) {
504 while (zpadlen > 0) {
505 dopr_outch (buffer, currlen, maxlen, '0');
506 --zpadlen;
510 /* Digits */
511 while (place > 0)
512 dopr_outch (buffer, currlen, maxlen, convert[--place]);
514 /* Left Justified spaces */
515 while (spadlen < 0) {
516 dopr_outch (buffer, currlen, maxlen, ' ');
517 ++spadlen;
521 static LDOUBLE abs_val(LDOUBLE value)
523 LDOUBLE result = value;
525 if (value < 0)
526 result = -value;
528 return result;
531 static LDOUBLE POW10(int exp)
533 LDOUBLE result = 1;
535 while (exp) {
536 result *= 10;
537 exp--;
540 return result;
543 static LLONG ROUND(LDOUBLE value)
545 LLONG intpart;
547 intpart = (LLONG)value;
548 value = value - intpart;
549 if (value >= 0.5) intpart++;
551 return intpart;
554 static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
555 LDOUBLE fvalue, int min, int max, int flags)
557 int signvalue = 0;
558 double ufvalue;
559 char iconvert[311];
560 char fconvert[311];
561 int iplace = 0;
562 int fplace = 0;
563 int padlen = 0; /* amount to pad */
564 int zpadlen = 0;
565 int caps = 0;
566 int index;
567 double intpart;
568 double fracpart;
569 double temp;
572 * AIX manpage says the default is 0, but Solaris says the default
573 * is 6, and sprintf on AIX defaults to 6
575 if (max < 0)
576 max = 6;
578 ufvalue = abs_val (fvalue);
580 if (fvalue < 0) {
581 signvalue = '-';
582 } else {
583 if (flags & DP_F_PLUS) { /* Do a sign (+/i) */
584 signvalue = '+';
585 } else {
586 if (flags & DP_F_SPACE)
587 signvalue = ' ';
591 #if 0
592 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
593 #endif
595 #if 0
596 if (max == 0) ufvalue += 0.5; /* if max = 0 we must round */
597 #endif
600 * Sorry, we only support 16 digits past the decimal because of our
601 * conversion method
603 if (max > 16)
604 max = 16;
606 /* We "cheat" by converting the fractional part to integer by
607 * multiplying by a factor of 10
610 temp = ufvalue;
611 modf(temp, &intpart);
613 fracpart = ROUND((POW10(max)) * (ufvalue - intpart));
615 if (fracpart >= POW10(max)) {
616 intpart++;
617 fracpart -= POW10(max);
621 /* Convert integer part */
622 do {
623 temp = intpart;
624 modf (intpart*0.1, &intpart);
625 temp = temp*0.1;
626 index = (int) ((temp -intpart +0.05)* 10.0);
627 /* index = (int) (((double)(temp*0.1) -intpart +0.05) *10.0); */
628 /* printf ("%llf, %f, %x\n", temp, intpart, index); */
629 iconvert[iplace++] =
630 (caps? "0123456789ABCDEF":"0123456789abcdef")[index];
631 } while (intpart && (iplace < 311));
632 if (iplace == 311) iplace--;
633 iconvert[iplace] = 0;
635 /* Convert fractional part */
636 if (fracpart)
638 do {
639 temp = fracpart;
640 modf (fracpart*0.1, &fracpart);
641 temp = temp*0.1;
642 index = (int) ((temp -fracpart +0.05)* 10.0);
643 /* index = (int) ((((temp/10) -fracpart) +0.05) *10); */
644 /* printf ("%lf, %lf, %ld\n", temp, fracpart, index); */
645 fconvert[fplace++] =
646 (caps? "0123456789ABCDEF":"0123456789abcdef")[index];
647 } while(fracpart && (fplace < 311));
648 if (fplace == 311) fplace--;
650 fconvert[fplace] = 0;
652 /* -1 for decimal point, another -1 if we are printing a sign */
653 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
654 zpadlen = max - fplace;
655 if (zpadlen < 0) zpadlen = 0;
656 if (padlen < 0)
657 padlen = 0;
658 if (flags & DP_F_MINUS)
659 padlen = -padlen; /* Left Justifty */
661 if ((flags & DP_F_ZERO) && (padlen > 0)) {
662 if (signvalue) {
663 dopr_outch (buffer, currlen, maxlen, signvalue);
664 --padlen;
665 signvalue = 0;
667 while (padlen > 0) {
668 dopr_outch (buffer, currlen, maxlen, '0');
669 --padlen;
672 while (padlen > 0) {
673 dopr_outch (buffer, currlen, maxlen, ' ');
674 --padlen;
676 if (signvalue)
677 dopr_outch (buffer, currlen, maxlen, signvalue);
679 while (iplace > 0)
680 dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]);
682 #ifdef DEBUG_SNPRINTF
683 printf("fmtfp: fplace=%d zpadlen=%d\n", fplace, zpadlen);
684 #endif
687 * Decimal point. This should probably use locale to find the correct
688 * char to print out.
690 if (max > 0) {
691 dopr_outch (buffer, currlen, maxlen, '.');
693 while (fplace > 0)
694 dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]);
697 while (zpadlen > 0) {
698 dopr_outch (buffer, currlen, maxlen, '0');
699 --zpadlen;
702 while (padlen < 0) {
703 dopr_outch (buffer, currlen, maxlen, ' ');
704 ++padlen;
708 static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c)
710 if (*currlen < maxlen) {
711 buffer[(*currlen)] = c;
713 (*currlen)++;
716 #if !defined(HAVE_VSNPRINTF) || !defined(HAVE_C99_VSNPRINTF)
717 int vsnprintf (char *str, size_t count, const char *fmt, va_list args)
719 return dopr(str, count, fmt, args);
721 #endif
723 #if !defined(HAVE_SNPRINTF) || !defined(HAVE_C99_VSNPRINTF)
724 int snprintf(char *str,size_t count,const char *fmt,...)
726 size_t ret;
727 va_list ap;
729 va_start(ap, fmt);
730 ret = vsnprintf(str, count, fmt, ap);
731 va_end(ap);
732 return ret;
734 #endif
736 #endif
738 #ifndef HAVE_VASPRINTF
739 int vasprintf(char **ptr, const char *format, va_list ap)
741 int ret;
743 ret = vsnprintf(NULL, 0, format, ap);
744 if (ret <= 0) return ret;
746 (*ptr) = (char *)malloc(ret+1);
747 if (!*ptr) return -1;
748 ret = vsnprintf(*ptr, ret+1, format, ap);
750 return ret;
752 #endif
755 #ifndef HAVE_ASPRINTF
756 int asprintf(char **ptr, const char *format, ...)
758 va_list ap;
759 int ret;
761 va_start(ap, format);
762 ret = vasprintf(ptr, format, ap);
763 va_end(ap);
765 return ret;
767 #endif
769 #ifdef TEST_SNPRINTF
771 int sprintf(char *str,const char *fmt,...);
773 int main (void)
775 char buf1[1024];
776 char buf2[1024];
777 char *fp_fmt[] = {
778 "%1.1f",
779 "%-1.5f",
780 "%1.5f",
781 "%123.9f",
782 "%10.5f",
783 "% 10.5f",
784 "%+22.9f",
785 "%+4.9f",
786 "%01.3f",
787 "%4f",
788 "%3.1f",
789 "%3.2f",
790 "%.0f",
791 "%f",
792 "-16.16f",
793 NULL
795 double fp_nums[] = { 6442452944.1234, -1.5, 134.21, 91340.2, 341.1234, 0203.9, 0.96, 0.996,
796 0.9996, 1.996, 4.136, 0};
797 char *int_fmt[] = {
798 "%-1.5d",
799 "%1.5d",
800 "%123.9d",
801 "%5.5d",
802 "%10.5d",
803 "% 10.5d",
804 "%+22.33d",
805 "%01.3d",
806 "%4d",
807 "%d",
808 NULL
810 long int_nums[] = { -1, 134, 91340, 341, 0203, 0};
811 char *str_fmt[] = {
812 "10.5s",
813 "5.10s",
814 "10.1s",
815 "0.10s",
816 "10.0s",
817 "1.10s",
818 "%s",
819 "%.1s",
820 "%.10s",
821 "%10s",
822 NULL
824 char *str_vals[] = {"hello", "a", "", "a longer string", NULL};
825 int x, y;
826 int fail = 0;
827 int num = 0;
829 printf ("Testing snprintf format codes against system sprintf...\n");
831 for (x = 0; fp_fmt[x] ; x++) {
832 for (y = 0; fp_nums[y] != 0 ; y++) {
833 int l1 = snprintf(NULL, 0, fp_fmt[x], fp_nums[y]);
834 int l2 = snprintf(buf1, sizeof(buf1), fp_fmt[x], fp_nums[y]);
835 sprintf (buf2, fp_fmt[x], fp_nums[y]);
836 if (strcmp (buf1, buf2)) {
837 printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n",
838 fp_fmt[x], buf1, buf2);
839 fail++;
841 if (l1 != l2) {
842 printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, fp_fmt[x]);
843 fail++;
845 num++;
849 for (x = 0; int_fmt[x] ; x++) {
850 for (y = 0; int_nums[y] != 0 ; y++) {
851 int l1 = snprintf(NULL, 0, int_fmt[x], int_nums[y]);
852 int l2 = snprintf(buf1, sizeof(buf1), int_fmt[x], int_nums[y]);
853 sprintf (buf2, int_fmt[x], int_nums[y]);
854 if (strcmp (buf1, buf2)) {
855 printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n",
856 int_fmt[x], buf1, buf2);
857 fail++;
859 if (l1 != l2) {
860 printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, int_fmt[x]);
861 fail++;
863 num++;
867 for (x = 0; str_fmt[x] ; x++) {
868 for (y = 0; str_vals[y] != 0 ; y++) {
869 int l1 = snprintf(NULL, 0, str_fmt[x], str_vals[y]);
870 int l2 = snprintf(buf1, sizeof(buf1), str_fmt[x], str_vals[y]);
871 sprintf (buf2, str_fmt[x], str_vals[y]);
872 if (strcmp (buf1, buf2)) {
873 printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n",
874 str_fmt[x], buf1, buf2);
875 fail++;
877 if (l1 != l2) {
878 printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, str_fmt[x]);
879 fail++;
881 num++;
885 printf ("%d tests failed out of %d.\n", fail, num);
887 printf("seeing how many digits we support\n");
889 double v0 = 0.12345678901234567890123456789012345678901;
890 for (x=0; x<100; x++) {
891 snprintf(buf1, sizeof(buf1), "%1.1f", v0*pow(10, x));
892 sprintf(buf2, "%1.1f", v0*pow(10, x));
893 if (strcmp(buf1, buf2)) {
894 printf("we seem to support %d digits\n", x-1);
895 break;
900 return 0;
902 #endif /* SNPRINTF_TEST */