* client/client.c (dir_total): use SMB_BIG_UINT
[sfrench/samba-autobuild/.git] / source / lib / snprintf.c
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
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  *
48  *  Andrew Tridgell (tridge@samba.org) Oct 1998
49  *    fixed handling of %.0f
50  *    added test for HAVE_LONG_DOUBLE
51  *
52  **************************************************************/
53
54 #include "config.h"
55
56 #include <string.h>
57 # include <ctype.h>
58 #include <sys/types.h>
59
60 #if !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF)
61
62 /* Define this as a fall through, HAVE_STDARG_H is probably already set */
63
64 #define HAVE_VARARGS_H
65
66 /* varargs declarations: */
67
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
87
88 #ifdef HAVE_LONG_DOUBLE
89 #define LDOUBLE long double
90 #else
91 #define LDOUBLE double
92 #endif
93
94 #ifdef HAVE_LONG_LONG
95 #define LLONG long long
96 #else
97 #define LLONG long
98 #endif
99
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);*/
102
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 );
112
113 /*
114  * dopr(): poor man's version of doprintf
115  */
116
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
126
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)
135
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
141
142 #define char_to_int(p) (p - '0')
143 #define MAX(p,q) ((p >= q) ? p : q)
144
145 static void dopr (char *buffer, size_t maxlen, const char *format, va_list args)
146 {
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;
157   
158   state = DP_S_DEFAULT;
159   currlen = flags = cflags = min = 0;
160   max = -1;
161   ch = *format++;
162
163   while (state != DP_S_DONE)
164   {
165     if ((ch == '\0') || (currlen >= maxlen)) 
166       state = DP_S_DONE;
167
168     switch(state) 
169     {
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) 
179       {
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;
203       }
204       break;
205     case DP_S_MIN:
206       if (isdigit((unsigned char)ch)) 
207       {
208         min = 10*min + char_to_int (ch);
209         ch = *format++;
210       } 
211       else if (ch == '*') 
212       {
213         min = va_arg (args, int);
214         ch = *format++;
215         state = DP_S_DOT;
216       } 
217       else 
218         state = DP_S_DOT;
219       break;
220     case DP_S_DOT:
221       if (ch == '.') 
222       {
223         state = DP_S_MAX;
224         ch = *format++;
225       } 
226       else 
227         state = DP_S_MOD;
228       break;
229     case DP_S_MAX:
230       if (isdigit((unsigned char)ch)) 
231       {
232         if (max < 0)
233           max = 0;
234         max = 10*max + char_to_int (ch);
235         ch = *format++;
236       } 
237       else if (ch == '*') 
238       {
239         max = va_arg (args, int);
240         ch = *format++;
241         state = DP_S_MOD;
242       } 
243       else 
244         state = DP_S_MOD;
245       break;
246     case DP_S_MOD:
247       switch (ch) 
248       {
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++;
259         }
260         break;
261       case 'L':
262         cflags = DP_C_LDOUBLE;
263         ch = *format++;
264         break;
265       default:
266         break;
267       }
268       state = DP_S_CONV;
269       break;
270     case DP_S_CONV:
271       switch (ch) 
272       {
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) 
362         {
363           short int *num;
364           num = va_arg (args, short int *);
365           *num = currlen;
366         } 
367         else if (cflags == DP_C_LONG) 
368         {
369           long int *num;
370           num = va_arg (args, long int *);
371           *num = (long int)currlen;
372         } 
373         else if (cflags == DP_C_LLONG) 
374         {
375           LLONG *num;
376           num = va_arg (args, LLONG *);
377           *num = (LLONG)currlen;
378         } 
379         else 
380         {
381           int *num;
382           num = va_arg (args, int *);
383           *num = currlen;
384         }
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;
396       }
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 */
407     }
408   }
409   if (currlen < maxlen - 1) 
410     buffer[currlen] = '\0';
411   else 
412     buffer[maxlen - 1] = '\0';
413 }
414
415 static void fmtstr (char *buffer, size_t *currlen, size_t maxlen,
416                     char *value, int flags, int min, int max)
417 {
418   int padlen, strln;     /* amount to pad */
419   int cnt = 0;
420   
421   if (value == 0)
422   {
423     value = "<NULL>";
424   }
425
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 */
432
433   while ((padlen > 0) && (cnt < max)) 
434   {
435     dopr_outch (buffer, currlen, maxlen, ' ');
436     --padlen;
437     ++cnt;
438   }
439   while (*value && (cnt < max)) 
440   {
441     dopr_outch (buffer, currlen, maxlen, *value++);
442     ++cnt;
443   }
444   while ((padlen < 0) && (cnt < max)) 
445   {
446     dopr_outch (buffer, currlen, maxlen, ' ');
447     ++padlen;
448     ++cnt;
449   }
450 }
451
452 /* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
453
454 static void fmtint (char *buffer, size_t *currlen, size_t maxlen,
455                     long value, int base, int min, int max, int flags)
456 {
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;
464   
465   if (max < 0)
466     max = 0;
467
468   uvalue = value;
469
470   if(!(flags & DP_F_UNSIGNED))
471   {
472     if( value < 0 ) {
473       signvalue = '-';
474       uvalue = -value;
475     }
476     else
477       if (flags & DP_F_PLUS)  /* Do a sign (+/i) */
478         signvalue = '+';
479     else
480       if (flags & DP_F_SPACE)
481         signvalue = ' ';
482   }
483   
484   if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
485
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;
494
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)
500   {
501     zpadlen = MAX(zpadlen, spadlen);
502     spadlen = 0;
503   }
504   if (flags & DP_F_MINUS) 
505     spadlen = -spadlen; /* Left Justifty */
506
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
511
512   /* Spaces */
513   while (spadlen > 0) 
514   {
515     dopr_outch (buffer, currlen, maxlen, ' ');
516     --spadlen;
517   }
518
519   /* Sign */
520   if (signvalue) 
521     dopr_outch (buffer, currlen, maxlen, signvalue);
522
523   /* Zeros */
524   if (zpadlen > 0) 
525   {
526     while (zpadlen > 0)
527     {
528       dopr_outch (buffer, currlen, maxlen, '0');
529       --zpadlen;
530     }
531   }
532
533   /* Digits */
534   while (place > 0) 
535     dopr_outch (buffer, currlen, maxlen, convert[--place]);
536   
537   /* Left Justified spaces */
538   while (spadlen < 0) {
539     dopr_outch (buffer, currlen, maxlen, ' ');
540     ++spadlen;
541   }
542 }
543
544 static LDOUBLE abs_val (LDOUBLE value)
545 {
546   LDOUBLE result = value;
547
548   if (value < 0)
549     result = -value;
550
551   return result;
552 }
553
554 static LDOUBLE pow10 (int exp)
555 {
556   LDOUBLE result = 1;
557
558   while (exp)
559   {
560     result *= 10;
561     exp--;
562   }
563   
564   return result;
565 }
566
567 static long round (LDOUBLE value)
568 {
569   long intpart;
570
571   intpart = (long)value;
572   value = value - intpart;
573   if (value >= 0.5)
574     intpart++;
575
576   return intpart;
577 }
578
579 static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
580                    LDOUBLE fvalue, int min, int max, int flags)
581 {
582   int signvalue = 0;
583   LDOUBLE ufvalue;
584   char iconvert[20];
585   char fconvert[20];
586   int iplace = 0;
587   int fplace = 0;
588   int padlen = 0; /* amount to pad */
589   int zpadlen = 0; 
590   int caps = 0;
591   long intpart;
592   long fracpart;
593   
594   /* 
595    * AIX manpage says the default is 0, but Solaris says the default
596    * is 6, and sprintf on AIX defaults to 6
597    */
598   if (max < 0)
599     max = 6;
600
601   ufvalue = abs_val (fvalue);
602
603   if (fvalue < 0)
604     signvalue = '-';
605   else
606     if (flags & DP_F_PLUS)  /* Do a sign (+/i) */
607       signvalue = '+';
608     else
609       if (flags & DP_F_SPACE)
610         signvalue = ' ';
611
612 #if 0
613   if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
614 #endif
615
616   intpart = (long)ufvalue;
617
618   /* 
619    * Sorry, we only support 9 digits past the decimal because of our 
620    * conversion method
621    */
622   if (max > 9)
623     max = 9;
624
625   /* We "cheat" by converting the fractional part to integer by
626    * multiplying by a factor of 10
627    */
628   fracpart = round ((pow10 (max)) * (ufvalue - intpart));
629
630   if (fracpart >= pow10 (max))
631   {
632     intpart++;
633     fracpart -= pow10 (max);
634   }
635
636 #ifdef DEBUG_SNPRINTF
637   printf("fmtfp: %g %d.%d min=%d max=%d\n", 
638          (double)fvalue, intpart, fracpart, min, max);
639 #endif
640
641   /* Convert integer part */
642   do {
643     iconvert[iplace++] =
644       (caps? "0123456789ABCDEF":"0123456789abcdef")[intpart % 10];
645     intpart = (intpart / 10);
646   } while(intpart && (iplace < 20));
647   if (iplace == 20) iplace--;
648   iconvert[iplace] = 0;
649
650   /* Convert fractional part */
651   do {
652     fconvert[fplace++] =
653       (caps? "0123456789ABCDEF":"0123456789abcdef")[fracpart % 10];
654     fracpart = (fracpart / 10);
655   } while(fracpart && (fplace < 20));
656   if (fplace == 20) fplace--;
657   fconvert[fplace] = 0;
658
659   /* -1 for decimal point, another -1 if we are printing a sign */
660   padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0); 
661   zpadlen = max - fplace;
662   if (zpadlen < 0)
663     zpadlen = 0;
664   if (padlen < 0) 
665     padlen = 0;
666   if (flags & DP_F_MINUS) 
667     padlen = -padlen; /* Left Justifty */
668
669   if ((flags & DP_F_ZERO) && (padlen > 0)) 
670   {
671     if (signvalue) 
672     {
673       dopr_outch (buffer, currlen, maxlen, signvalue);
674       --padlen;
675       signvalue = 0;
676     }
677     while (padlen > 0)
678     {
679       dopr_outch (buffer, currlen, maxlen, '0');
680       --padlen;
681     }
682   }
683   while (padlen > 0)
684   {
685     dopr_outch (buffer, currlen, maxlen, ' ');
686     --padlen;
687   }
688   if (signvalue) 
689     dopr_outch (buffer, currlen, maxlen, signvalue);
690
691   while (iplace > 0) 
692     dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]);
693
694
695 #ifdef DEBUG_SNPRINTF
696   printf("fmtfp: fplace=%d zpadlen=%d\n", fplace, zpadlen);
697 #endif
698
699   /*
700    * Decimal point.  This should probably use locale to find the correct
701    * char to print out.
702    */
703   if (max > 0) {
704           dopr_outch (buffer, currlen, maxlen, '.');
705
706           while (fplace > 0) 
707                   dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]);
708   }
709
710   while (zpadlen > 0)
711   {
712     dopr_outch (buffer, currlen, maxlen, '0');
713     --zpadlen;
714   }
715
716   while (padlen < 0) 
717   {
718     dopr_outch (buffer, currlen, maxlen, ' ');
719     ++padlen;
720   }
721 }
722
723 static void dopr_outch (char *buffer, size_t *currlen, size_t maxlen, char c)
724 {
725   if (*currlen < maxlen)
726     buffer[(*currlen)++] = c;
727 }
728 #endif /* !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) */
729
730 #ifndef HAVE_VSNPRINTF
731  int vsnprintf (char *str, size_t count, const char *fmt, va_list args)
732 {
733   str[0] = 0;
734   dopr(str, count, fmt, args);
735   return(strlen(str));
736 }
737 #endif /* !HAVE_VSNPRINTF */
738
739 #ifndef HAVE_SNPRINTF
740 /* VARARGS3 */
741 #ifdef HAVE_STDARGS
742  int snprintf (char *str,size_t count,const char *fmt,...)
743 #else
744  int snprintf (va_alist) va_dcl
745 #endif
746 {
747 #ifndef HAVE_STDARGS
748   char *str;
749   size_t count;
750   char *fmt;
751 #endif
752   VA_LOCAL_DECL;
753     
754   VA_START (fmt);
755   VA_SHIFT (str, char *);
756   VA_SHIFT (count, size_t );
757   VA_SHIFT (fmt, char *);
758   (void) vsnprintf(str, count, fmt, ap);
759   VA_END;
760   return(strlen(str));
761 }
762
763
764 #else
765  /* keep compilers happy about empty files */
766  void dummy_snprintf(void) {}
767 #endif /* !HAVE_SNPRINTF */
768
769 #ifdef TEST_SNPRINTF
770 #ifndef LONG_STRING
771 #define LONG_STRING 1024
772 #endif
773  int main (void)
774 {
775   char buf1[LONG_STRING];
776   char buf2[LONG_STRING];
777   char *fp_fmt[] = {
778     "%-1.5f",
779     "%1.5f",
780     "%123.9f",
781     "%10.5f",
782     "% 10.5f",
783     "%+22.9f",
784     "%+4.9f",
785     "%01.3f",
786     "%4f",
787     "%3.1f",
788     "%3.2f",
789     "%.0f",
790     "%.1f",
791     NULL
792   };
793   double fp_nums[] = { -1.5, 134.21, 91340.2, 341.1234, 0203.9, 0.96, 0.996, 
794     0.9996, 1.996, 4.136, 0};
795   char *int_fmt[] = {
796     "%-1.5d",
797     "%1.5d",
798     "%123.9d",
799     "%5.5d",
800     "%10.5d",
801     "% 10.5d",
802     "%+22.33d",
803     "%01.3d",
804     "%4d",
805     NULL
806   };
807   long int_nums[] = { -1, 134, 91340, 341, 0203, 0};
808   int x, y;
809   int fail = 0;
810   int num = 0;
811
812   printf ("Testing snprintf format codes against system sprintf...\n");
813
814   for (x = 0; fp_fmt[x] != NULL ; x++)
815     for (y = 0; fp_nums[y] != 0 ; y++)
816     {
817       snprintf (buf1, sizeof (buf1), fp_fmt[x], fp_nums[y]);
818       sprintf (buf2, fp_fmt[x], fp_nums[y]);
819       if (strcmp (buf1, buf2))
820       {
821         printf("snprintf doesn't match Format: %s\n\tsnprintf = %s\n\tsprintf  = %s\n", 
822             fp_fmt[x], buf1, buf2);
823         fail++;
824       }
825       num++;
826     }
827
828   for (x = 0; int_fmt[x] != NULL ; x++)
829     for (y = 0; int_nums[y] != 0 ; y++)
830     {
831       snprintf (buf1, sizeof (buf1), int_fmt[x], int_nums[y]);
832       sprintf (buf2, int_fmt[x], int_nums[y]);
833       if (strcmp (buf1, buf2))
834       {
835         printf("snprintf doesn't match Format: %s\n\tsnprintf = %s\n\tsprintf  = %s\n", 
836             int_fmt[x], buf1, buf2);
837         fail++;
838       }
839       num++;
840     }
841   printf ("%d tests failed out of %d.\n", fail, num);
842 }
843 #endif /* SNPRINTF_TEST */
844