preparing for release of alpha.2.5
[Samba.git] / source / lib / snprintf.c
blob70ce95916fc41a662435a959899008a53d6259b2
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 **************************************************************/
54 #include "config.h"
56 #include <string.h>
57 # include <ctype.h>
58 #include <sys/types.h>
60 #if !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF)
62 /* Define this as a fall through, HAVE_STDARG_H is probably already set */
64 #define HAVE_VARARGS_H
66 /* varargs declarations: */
68 #if defined(HAVE_STDARG_H)
69 # include <stdarg.h>
70 # define HAVE_STDARGS /* let's hope that works everywhere (mj) */
71 # define VA_LOCAL_DECL va_list ap
72 # define VA_START(f) va_start(ap, f)
73 # define VA_SHIFT(v,t) ; /* no-op for ANSI */
74 # define VA_END va_end(ap)
75 #else
76 # if defined(HAVE_VARARGS_H)
77 # include <varargs.h>
78 # undef HAVE_STDARGS
79 # define VA_LOCAL_DECL va_list ap
80 # define VA_START(f) va_start(ap) /* f is ignored! */
81 # define VA_SHIFT(v,t) v = va_arg(ap,t)
82 # define VA_END va_end(ap)
83 # else
84 /*XX ** NO VARARGS ** XX*/
85 # endif
86 #endif
88 #ifdef HAVE_LONG_DOUBLE
89 #define LDOUBLE long double
90 #else
91 #define LDOUBLE double
92 #endif
94 #ifdef HAVE_LONG_LONG
95 #define LLONG long long
96 #else
97 #define LLONG long
98 #endif
100 /*int snprintf (char *str, size_t count, const char *fmt, ...);*/
101 /*int vsnprintf (char *str, size_t count, const char *fmt, va_list arg);*/
103 static void dopr (char *buffer, size_t maxlen, const char *format,
104 va_list args);
105 static void fmtstr (char *buffer, size_t *currlen, size_t maxlen,
106 char *value, int flags, int min, int max);
107 static void fmtint (char *buffer, size_t *currlen, size_t maxlen,
108 long value, int base, int min, int max, int flags);
109 static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
110 LDOUBLE fvalue, int min, int max, int flags);
111 static void dopr_outch (char *buffer, size_t *currlen, size_t maxlen, char c );
114 * dopr(): poor man's version of doprintf
117 /* format read states */
118 #define DP_S_DEFAULT 0
119 #define DP_S_FLAGS 1
120 #define DP_S_MIN 2
121 #define DP_S_DOT 3
122 #define DP_S_MAX 4
123 #define DP_S_MOD 5
124 #define DP_S_CONV 6
125 #define DP_S_DONE 7
127 /* format flags - Bits */
128 #define DP_F_MINUS (1 << 0)
129 #define DP_F_PLUS (1 << 1)
130 #define DP_F_SPACE (1 << 2)
131 #define DP_F_NUM (1 << 3)
132 #define DP_F_ZERO (1 << 4)
133 #define DP_F_UP (1 << 5)
134 #define DP_F_UNSIGNED (1 << 6)
136 /* Conversion Flags */
137 #define DP_C_SHORT 1
138 #define DP_C_LONG 2
139 #define DP_C_LDOUBLE 3
140 #define DP_C_LLONG 4
142 #define char_to_int(p) (p - '0')
143 #define MAX(p,q) ((p >= q) ? p : q)
145 static void dopr (char *buffer, size_t maxlen, const char *format, va_list args)
147 char ch;
148 LLONG value;
149 LDOUBLE fvalue;
150 char *strvalue;
151 int min;
152 int max;
153 int state;
154 int flags;
155 int cflags;
156 size_t currlen;
158 state = DP_S_DEFAULT;
159 currlen = flags = cflags = min = 0;
160 max = -1;
161 ch = *format++;
163 while (state != DP_S_DONE)
165 if ((ch == '\0') || (currlen >= maxlen))
166 state = DP_S_DONE;
168 switch(state)
170 case DP_S_DEFAULT:
171 if (ch == '%')
172 state = DP_S_FLAGS;
173 else
174 dopr_outch (buffer, &currlen, maxlen, ch);
175 ch = *format++;
176 break;
177 case DP_S_FLAGS:
178 switch (ch)
180 case '-':
181 flags |= DP_F_MINUS;
182 ch = *format++;
183 break;
184 case '+':
185 flags |= DP_F_PLUS;
186 ch = *format++;
187 break;
188 case ' ':
189 flags |= DP_F_SPACE;
190 ch = *format++;
191 break;
192 case '#':
193 flags |= DP_F_NUM;
194 ch = *format++;
195 break;
196 case '0':
197 flags |= DP_F_ZERO;
198 ch = *format++;
199 break;
200 default:
201 state = DP_S_MIN;
202 break;
204 break;
205 case DP_S_MIN:
206 if (isdigit((unsigned char)ch))
208 min = 10*min + char_to_int (ch);
209 ch = *format++;
211 else if (ch == '*')
213 min = va_arg (args, int);
214 ch = *format++;
215 state = DP_S_DOT;
217 else
218 state = DP_S_DOT;
219 break;
220 case DP_S_DOT:
221 if (ch == '.')
223 state = DP_S_MAX;
224 ch = *format++;
226 else
227 state = DP_S_MOD;
228 break;
229 case DP_S_MAX:
230 if (isdigit((unsigned char)ch))
232 if (max < 0)
233 max = 0;
234 max = 10*max + char_to_int (ch);
235 ch = *format++;
237 else if (ch == '*')
239 max = va_arg (args, int);
240 ch = *format++;
241 state = DP_S_MOD;
243 else
244 state = DP_S_MOD;
245 break;
246 case DP_S_MOD:
247 switch (ch)
249 case 'h':
250 cflags = DP_C_SHORT;
251 ch = *format++;
252 break;
253 case 'l':
254 cflags = DP_C_LONG;
255 ch = *format++;
256 if (ch == 'l') { /* It's a long long */
257 cflags = DP_C_LLONG;
258 ch = *format++;
260 break;
261 case 'L':
262 cflags = DP_C_LDOUBLE;
263 ch = *format++;
264 break;
265 default:
266 break;
268 state = DP_S_CONV;
269 break;
270 case DP_S_CONV:
271 switch (ch)
273 case 'd':
274 case 'i':
275 if (cflags == DP_C_SHORT)
276 value = va_arg (args, short int);
277 else if (cflags == DP_C_LONG)
278 value = va_arg (args, long int);
279 else if (cflags == DP_C_LLONG)
280 value = va_arg (args, LLONG);
281 else
282 value = va_arg (args, int);
283 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
284 break;
285 case 'o':
286 flags |= DP_F_UNSIGNED;
287 if (cflags == DP_C_SHORT)
288 value = va_arg (args, unsigned short int);
289 else if (cflags == DP_C_LONG)
290 value = (long)va_arg (args, unsigned long int);
291 else if (cflags == DP_C_LLONG)
292 value = (long)va_arg (args, unsigned LLONG);
293 else
294 value = (long)va_arg (args, unsigned int);
295 fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags);
296 break;
297 case 'u':
298 flags |= DP_F_UNSIGNED;
299 if (cflags == DP_C_SHORT)
300 value = va_arg (args, unsigned short int);
301 else if (cflags == DP_C_LONG)
302 value = (long)va_arg (args, unsigned long int);
303 else if (cflags == DP_C_LLONG)
304 value = (LLONG)va_arg (args, unsigned LLONG);
305 else
306 value = (long)va_arg (args, unsigned int);
307 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
308 break;
309 case 'X':
310 flags |= DP_F_UP;
311 case 'x':
312 flags |= DP_F_UNSIGNED;
313 if (cflags == DP_C_SHORT)
314 value = va_arg (args, unsigned short int);
315 else if (cflags == DP_C_LONG)
316 value = (long)va_arg (args, unsigned long int);
317 else if (cflags == DP_C_LLONG)
318 value = (LLONG)va_arg (args, unsigned LLONG);
319 else
320 value = (long)va_arg (args, unsigned int);
321 fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags);
322 break;
323 case 'f':
324 if (cflags == DP_C_LDOUBLE)
325 fvalue = va_arg (args, LDOUBLE);
326 else
327 fvalue = va_arg (args, double);
328 /* um, floating point? */
329 fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
330 break;
331 case 'E':
332 flags |= DP_F_UP;
333 case 'e':
334 if (cflags == DP_C_LDOUBLE)
335 fvalue = va_arg (args, LDOUBLE);
336 else
337 fvalue = va_arg (args, double);
338 break;
339 case 'G':
340 flags |= DP_F_UP;
341 case 'g':
342 if (cflags == DP_C_LDOUBLE)
343 fvalue = va_arg (args, LDOUBLE);
344 else
345 fvalue = va_arg (args, double);
346 break;
347 case 'c':
348 dopr_outch (buffer, &currlen, maxlen, va_arg (args, int));
349 break;
350 case 's':
351 strvalue = va_arg (args, char *);
352 if (max < 0)
353 max = maxlen; /* ie, no max */
354 fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max);
355 break;
356 case 'p':
357 strvalue = va_arg (args, void *);
358 fmtint (buffer, &currlen, maxlen, (long) strvalue, 16, min, max, flags);
359 break;
360 case 'n':
361 if (cflags == DP_C_SHORT)
363 short int *num;
364 num = va_arg (args, short int *);
365 *num = currlen;
367 else if (cflags == DP_C_LONG)
369 long int *num;
370 num = va_arg (args, long int *);
371 *num = (long int)currlen;
373 else if (cflags == DP_C_LLONG)
375 LLONG *num;
376 num = va_arg (args, LLONG *);
377 *num = (LLONG)currlen;
379 else
381 int *num;
382 num = va_arg (args, int *);
383 *num = currlen;
385 break;
386 case '%':
387 dopr_outch (buffer, &currlen, maxlen, ch);
388 break;
389 case 'w':
390 /* not supported yet, treat as next char */
391 ch = *format++;
392 break;
393 default:
394 /* Unknown, skip */
395 break;
397 ch = *format++;
398 state = DP_S_DEFAULT;
399 flags = cflags = min = 0;
400 max = -1;
401 break;
402 case DP_S_DONE:
403 break;
404 default:
405 /* hmm? */
406 break; /* some picky compilers need this */
409 if (currlen < maxlen - 1)
410 buffer[currlen] = '\0';
411 else
412 buffer[maxlen - 1] = '\0';
415 static void fmtstr (char *buffer, size_t *currlen, size_t maxlen,
416 char *value, int flags, int min, int max)
418 int padlen, strln; /* amount to pad */
419 int cnt = 0;
421 if (value == 0)
423 value = "<NULL>";
426 for (strln = 0; value[strln]; ++strln); /* strlen */
427 padlen = min - strln;
428 if (padlen < 0)
429 padlen = 0;
430 if (flags & DP_F_MINUS)
431 padlen = -padlen; /* Left Justify */
433 while ((padlen > 0) && (cnt < max))
435 dopr_outch (buffer, currlen, maxlen, ' ');
436 --padlen;
437 ++cnt;
439 while (*value && (cnt < max))
441 dopr_outch (buffer, currlen, maxlen, *value++);
442 ++cnt;
444 while ((padlen < 0) && (cnt < max))
446 dopr_outch (buffer, currlen, maxlen, ' ');
447 ++padlen;
448 ++cnt;
452 /* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
454 static void fmtint (char *buffer, size_t *currlen, size_t maxlen,
455 long value, int base, int min, int max, int flags)
457 int signvalue = 0;
458 unsigned long uvalue;
459 char convert[20];
460 int place = 0;
461 int spadlen = 0; /* amount to space pad */
462 int zpadlen = 0; /* amount to zero pad */
463 int caps = 0;
465 if (max < 0)
466 max = 0;
468 uvalue = value;
470 if(!(flags & DP_F_UNSIGNED))
472 if( value < 0 ) {
473 signvalue = '-';
474 uvalue = -value;
476 else
477 if (flags & DP_F_PLUS) /* Do a sign (+/i) */
478 signvalue = '+';
479 else
480 if (flags & DP_F_SPACE)
481 signvalue = ' ';
484 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
486 do {
487 convert[place++] =
488 (caps? "0123456789ABCDEF":"0123456789abcdef")
489 [uvalue % (unsigned)base ];
490 uvalue = (uvalue / (unsigned)base );
491 } while(uvalue && (place < 20));
492 if (place == 20) place--;
493 convert[place] = 0;
495 zpadlen = max - place;
496 spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
497 if (zpadlen < 0) zpadlen = 0;
498 if (spadlen < 0) spadlen = 0;
499 if (flags & DP_F_ZERO)
501 zpadlen = MAX(zpadlen, spadlen);
502 spadlen = 0;
504 if (flags & DP_F_MINUS)
505 spadlen = -spadlen; /* Left Justifty */
507 #ifdef DEBUG_SNPRINTF
508 printf("zpad: %d, spad: %d, min: %d, max: %d, place: %d\n",
509 zpadlen, spadlen, min, max, place);
510 #endif
512 /* Spaces */
513 while (spadlen > 0)
515 dopr_outch (buffer, currlen, maxlen, ' ');
516 --spadlen;
519 /* Sign */
520 if (signvalue)
521 dopr_outch (buffer, currlen, maxlen, signvalue);
523 /* Zeros */
524 if (zpadlen > 0)
526 while (zpadlen > 0)
528 dopr_outch (buffer, currlen, maxlen, '0');
529 --zpadlen;
533 /* Digits */
534 while (place > 0)
535 dopr_outch (buffer, currlen, maxlen, convert[--place]);
537 /* Left Justified spaces */
538 while (spadlen < 0) {
539 dopr_outch (buffer, currlen, maxlen, ' ');
540 ++spadlen;
544 static LDOUBLE abs_val (LDOUBLE value)
546 LDOUBLE result = value;
548 if (value < 0)
549 result = -value;
551 return result;
554 static LDOUBLE pow10 (int exp)
556 LDOUBLE result = 1;
558 while (exp)
560 result *= 10;
561 exp--;
564 return result;
567 static long round (LDOUBLE value)
569 long intpart;
571 intpart = (long)value;
572 value = value - intpart;
573 if (value >= 0.5)
574 intpart++;
576 return intpart;
579 static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
580 LDOUBLE fvalue, int min, int max, int flags)
582 int signvalue = 0;
583 LDOUBLE ufvalue;
584 #ifndef HAVE_FCVT
585 char iconvert[20];
586 char fconvert[20];
587 #else
588 char iconvert[311];
589 char fconvert[311];
590 char *result;
591 int dec_pt, sig;
592 int r_length;
593 # ifdef HAVE_FCVTL
594 extern char *fcvtl(long double value, int ndigit, int *decpt, int *sign);
595 # else
596 extern char *fcvt(double value, int ndigit, int *decpt, int *sign);
597 # endif
598 #endif
599 int iplace = 0;
600 int fplace = 0;
601 int padlen = 0; /* amount to pad */
602 int zpadlen = 0;
603 int caps = 0;
604 long intpart;
605 long fracpart;
608 * AIX manpage says the default is 0, but Solaris says the default
609 * is 6, and sprintf on AIX defaults to 6
611 if (max < 0)
612 max = 6;
614 ufvalue = abs_val (fvalue);
616 if (fvalue < 0)
617 signvalue = '-';
618 else
619 if (flags & DP_F_PLUS) /* Do a sign (+/i) */
620 signvalue = '+';
621 else
622 if (flags & DP_F_SPACE)
623 signvalue = ' ';
625 #if 0
626 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
627 #endif
629 #ifndef HAVE_FCVT
630 intpart = (long)ufvalue;
633 * Sorry, we only support 9 digits past the decimal because of our
634 * conversion method
636 if (max > 9)
637 max = 9;
639 /* We "cheat" by converting the fractional part to integer by
640 * multiplying by a factor of 10
642 fracpart = round ((pow10 (max)) * (ufvalue - intpart));
644 if (fracpart >= pow10 (max))
646 intpart++;
647 fracpart -= pow10 (max);
650 #ifdef DEBUG_SNPRINTF
651 printf("fmtfp: %g %d.%d min=%d max=%d\n",
652 (double)fvalue, intpart, fracpart, min, max);
653 #endif
655 /* Convert integer part */
656 do {
657 iconvert[iplace++] =
658 (caps? "0123456789ABCDEF":"0123456789abcdef")[intpart % 10];
659 intpart = (intpart / 10);
660 } while(intpart && (iplace < 20));
661 if (iplace == 20) iplace--;
662 iconvert[iplace] = 0;
664 /* Convert fractional part */
665 do {
666 fconvert[fplace++] =
667 (caps? "0123456789ABCDEF":"0123456789abcdef")[fracpart % 10];
668 fracpart = (fracpart / 10);
669 } while(fracpart && (fplace < 20));
670 if (fplace == 20) fplace--;
671 fconvert[fplace] = 0;
672 #else /* use fcvt() */
673 if (max > 310)
674 max = 310;
675 # ifdef HAVE_FCVTL
676 result = fcvtl(ufvalue, max, &dec_pt, &sig);
677 # else
678 result = fcvt(ufvalue, max, &dec_pt, &sig);
679 # endif
681 r_length = strlen(result);
684 * Fix broken fcvt implementation returns..
687 if (r_length == 0)
689 result[0] = '0';
690 result[1] = '\0';
691 r_length = 1;
694 if ( r_length < dec_pt )
695 dec_pt = r_length;
697 if (dec_pt <= 0) {
698 iplace = 1;
699 iconvert[0] = '0';
700 iconvert[1] = '\0';
702 fplace = 0;
704 while(r_length)
705 fconvert[fplace++] = result[--r_length];
707 while ((dec_pt < 0) && (fplace < max)) {
708 fconvert[fplace++] = '0';
709 dec_pt++;
711 } else {
712 int c;
714 iplace=0;
715 for(c=dec_pt; c; iconvert[iplace++] = result[--c])
717 iconvert[iplace] = '\0';
719 result += dec_pt;
720 fplace = 0;
722 for(c=(r_length-dec_pt); c; fconvert[fplace++] = result[--c])
725 #endif /* fcvt */
727 /* -1 for decimal point, another -1 if we are printing a sign */
728 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
729 zpadlen = max - fplace;
730 if (zpadlen < 0)
731 zpadlen = 0;
732 if (padlen < 0)
733 padlen = 0;
734 if (flags & DP_F_MINUS)
735 padlen = -padlen; /* Left Justifty */
737 if ((flags & DP_F_ZERO) && (padlen > 0))
739 if (signvalue)
741 dopr_outch (buffer, currlen, maxlen, signvalue);
742 --padlen;
743 signvalue = 0;
745 while (padlen > 0)
747 dopr_outch (buffer, currlen, maxlen, '0');
748 --padlen;
751 while (padlen > 0)
753 dopr_outch (buffer, currlen, maxlen, ' ');
754 --padlen;
756 if (signvalue)
757 dopr_outch (buffer, currlen, maxlen, signvalue);
759 while (iplace > 0)
760 dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]);
763 #ifdef DEBUG_SNPRINTF
764 printf("fmtfp: fplace=%d zpadlen=%d\n", fplace, zpadlen);
765 #endif
768 * Decimal point. This should probably use locale to find the correct
769 * char to print out.
771 if (max > 0) {
772 dopr_outch (buffer, currlen, maxlen, '.');
774 while (fplace > 0)
775 dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]);
778 while (zpadlen > 0)
780 dopr_outch (buffer, currlen, maxlen, '0');
781 --zpadlen;
784 while (padlen < 0)
786 dopr_outch (buffer, currlen, maxlen, ' ');
787 ++padlen;
791 static void dopr_outch (char *buffer, size_t *currlen, size_t maxlen, char c)
793 if (*currlen < maxlen)
794 buffer[(*currlen)++] = c;
796 #endif /* !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) */
798 #ifndef HAVE_VSNPRINTF
799 int vsnprintf (char *str, size_t count, const char *fmt, va_list args)
801 str[0] = 0;
802 dopr(str, count, fmt, args);
803 return(strlen(str));
805 #endif /* !HAVE_VSNPRINTF */
807 #ifndef HAVE_SNPRINTF
808 /* VARARGS3 */
809 #ifdef HAVE_STDARGS
810 int snprintf (char *str,size_t count,const char *fmt,...)
811 #else
812 int snprintf (va_alist) va_dcl
813 #endif
815 #ifndef HAVE_STDARGS
816 char *str;
817 size_t count;
818 char *fmt;
819 #endif
820 VA_LOCAL_DECL;
822 VA_START (fmt);
823 VA_SHIFT (str, char *);
824 VA_SHIFT (count, size_t );
825 VA_SHIFT (fmt, char *);
826 (void) vsnprintf(str, count, fmt, ap);
827 VA_END;
828 return(strlen(str));
832 #else
833 /* keep compilers happy about empty files */
834 void dummy_snprintf(void) {}
835 #endif /* !HAVE_SNPRINTF */
837 #ifdef TEST_SNPRINTF
838 #ifndef LONG_STRING
839 #define LONG_STRING 1024
840 #endif
841 int main (void)
843 char buf1[LONG_STRING];
844 char buf2[LONG_STRING];
845 char *fp_fmt[] = {
846 "%-1.5f",
847 "%1.5f",
848 "%123.9f",
849 "%10.5f",
850 "% 10.5f",
851 "%+22.9f",
852 "%+4.9f",
853 "%01.3f",
854 "%4f",
855 "%3.1f",
856 "%3.2f",
857 "%.0f",
858 "%.1f",
859 NULL
861 double fp_nums[] = { -1.5, 134.21, 91340.2, 341.1234, 0203.9, 0.96, 0.996,
862 0.9996, 1.996, 4.136, 6442452944.1234, 0};
863 char *int_fmt[] = {
864 "%-1.5d",
865 "%1.5d",
866 "%123.9d",
867 "%5.5d",
868 "%10.5d",
869 "% 10.5d",
870 "%+22.33d",
871 "%01.3d",
872 "%4d",
873 NULL
875 long int_nums[] = { -1, 134, 91340, 341, 0203, 0};
876 int x, y;
877 int fail = 0;
878 int num = 0;
880 printf ("Testing snprintf format codes against system sprintf...\n");
882 for (x = 0; fp_fmt[x] != NULL ; x++)
883 for (y = 0; fp_nums[y] != 0 ; y++)
885 snprintf (buf1, sizeof (buf1), fp_fmt[x], fp_nums[y]);
886 sprintf (buf2, fp_fmt[x], fp_nums[y]);
887 if (strcmp (buf1, buf2))
889 printf("snprintf doesn't match Format: %s\n\tsnprintf = %s\n\tsprintf = %s\n",
890 fp_fmt[x], buf1, buf2);
891 fail++;
893 num++;
896 for (x = 0; int_fmt[x] != NULL ; x++)
897 for (y = 0; int_nums[y] != 0 ; y++)
899 snprintf (buf1, sizeof (buf1), int_fmt[x], int_nums[y]);
900 sprintf (buf2, int_fmt[x], int_nums[y]);
901 if (strcmp (buf1, buf2))
903 printf("snprintf doesn't match Format: %s\n\tsnprintf = %s\n\tsprintf = %s\n",
904 int_fmt[x], buf1, buf2);
905 fail++;
907 num++;
909 printf ("%d tests failed out of %d.\n", fail, num);
911 #endif /* SNPRINTF_TEST */