1eae2f09b9bc5a6bf6525fda7336230cc325b291
[samba.git] / source3 / 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  * 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  **************************************************************/
57
58 #ifndef NO_CONFIG_H /* for some tests */
59 #include "config.h"
60 #else
61 #define NULL 0
62 #endif
63
64 #ifdef TEST_SNPRINTF /* need math library headers for testing */
65 #include <math.h>
66 #endif
67
68 #ifdef HAVE_STRING_H
69 #include <string.h>
70 #endif
71
72 #ifdef HAVE_STRINGS_H
73 #include <strings.h>
74 #endif
75 #ifdef HAVE_CTYPE_H
76 #include <ctype.h>
77 #endif
78 #include <sys/types.h>
79 #include <stdarg.h>
80 #ifdef HAVE_STDLIB_H
81 #include <stdlib.h>
82 #endif
83
84 #if defined(HAVE_SNPRINTF) && defined(HAVE_VSNPRINTF) && defined(HAVE_C99_VSNPRINTF)
85 /* only include stdio.h if we are not re-defining snprintf or vsnprintf */
86 #include <stdio.h>
87  /* make the compiler happy with an empty file */
88  void dummy_snprintf(void) {} 
89 #else
90
91 #ifdef HAVE_LONG_DOUBLE
92 #define LDOUBLE long double
93 #else
94 #define LDOUBLE double
95 #endif
96
97 #ifdef HAVE_LONG_LONG
98 #define LLONG long long
99 #else
100 #define LLONG long
101 #endif
102
103 /* free memory if the pointer is valid and zero the pointer */
104 #ifndef SAFE_FREE
105 #define SAFE_FREE(x) do { if ((x) != NULL) {free((x)); (x)=NULL;} } while(0)
106 #endif
107
108 #ifndef VA_COPY
109 #ifdef HAVE_VA_COPY
110 #define VA_COPY(dest, src) __va_copy(dest, src)
111 #else
112 #define VA_COPY(dest, src) (dest) = (src)
113 #endif
114 #endif
115
116 static size_t dopr(char *buffer, size_t maxlen, const char *format, 
117                    va_list args_in);
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);
125
126 /*
127  * dopr(): poor man's version of doprintf
128  */
129
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
139
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)
148
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
154
155 #define char_to_int(p) ((p)- '0')
156 #ifndef MAX
157 #define MAX(p,q) (((p) >= (q)) ? (p) : (q))
158 #endif
159
160 static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args_in)
161 {
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;
172         va_list args;
173
174         VA_COPY(args, args_in);
175         
176         state = DP_S_DEFAULT;
177         currlen = flags = cflags = min = 0;
178         max = -1;
179         ch = *format++;
180         
181         while (state != DP_S_DONE) {
182                 if (ch == '\0') 
183                         state = DP_S_DONE;
184
185                 switch(state) {
186                 case DP_S_DEFAULT:
187                         if (ch == '%') 
188                                 state = DP_S_FLAGS;
189                         else 
190                                 dopr_outch (buffer, &currlen, maxlen, ch);
191                         ch = *format++;
192                         break;
193                 case DP_S_FLAGS:
194                         switch (ch) {
195                         case '-':
196                                 flags |= DP_F_MINUS;
197                                 ch = *format++;
198                                 break;
199                         case '+':
200                                 flags |= DP_F_PLUS;
201                                 ch = *format++;
202                                 break;
203                         case ' ':
204                                 flags |= DP_F_SPACE;
205                                 ch = *format++;
206                                 break;
207                         case '#':
208                                 flags |= DP_F_NUM;
209                                 ch = *format++;
210                                 break;
211                         case '0':
212                                 flags |= DP_F_ZERO;
213                                 ch = *format++;
214                                 break;
215                         default:
216                                 state = DP_S_MIN;
217                                 break;
218                         }
219                         break;
220                 case DP_S_MIN:
221                         if (isdigit((unsigned char)ch)) {
222                                 min = 10*min + char_to_int (ch);
223                                 ch = *format++;
224                         } else if (ch == '*') {
225                                 min = va_arg (args, int);
226                                 ch = *format++;
227                                 state = DP_S_DOT;
228                         } else {
229                                 state = DP_S_DOT;
230                         }
231                         break;
232                 case DP_S_DOT:
233                         if (ch == '.') {
234                                 state = DP_S_MAX;
235                                 ch = *format++;
236                         } else { 
237                                 state = DP_S_MOD;
238                         }
239                         break;
240                 case DP_S_MAX:
241                         if (isdigit((unsigned char)ch)) {
242                                 if (max < 0)
243                                         max = 0;
244                                 max = 10*max + char_to_int (ch);
245                                 ch = *format++;
246                         } else if (ch == '*') {
247                                 max = va_arg (args, int);
248                                 ch = *format++;
249                                 state = DP_S_MOD;
250                         } else {
251                                 state = DP_S_MOD;
252                         }
253                         break;
254                 case DP_S_MOD:
255                         switch (ch) {
256                         case 'h':
257                                 cflags = DP_C_SHORT;
258                                 ch = *format++;
259                                 break;
260                         case 'l':
261                                 cflags = DP_C_LONG;
262                                 ch = *format++;
263                                 if (ch == 'l') {        /* It's a long long */
264                                         cflags = DP_C_LLONG;
265                                         ch = *format++;
266                                 }
267                                 break;
268                         case 'L':
269                                 cflags = DP_C_LDOUBLE;
270                                 ch = *format++;
271                                 break;
272                         default:
273                                 break;
274                         }
275                         state = DP_S_CONV;
276                         break;
277                 case DP_S_CONV:
278                         switch (ch) {
279                         case 'd':
280                         case 'i':
281                                 if (cflags == DP_C_SHORT) 
282                                         value = va_arg (args, int);
283                                 else if (cflags == DP_C_LONG)
284                                         value = va_arg (args, long int);
285                                 else if (cflags == DP_C_LLONG)
286                                         value = va_arg (args, LLONG);
287                                 else
288                                         value = va_arg (args, int);
289                                 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
290                                 break;
291                         case 'o':
292                                 flags |= DP_F_UNSIGNED;
293                                 if (cflags == DP_C_SHORT)
294                                         value = va_arg (args, unsigned int);
295                                 else if (cflags == DP_C_LONG)
296                                         value = (long)va_arg (args, unsigned long int);
297                                 else if (cflags == DP_C_LLONG)
298                                         value = (long)va_arg (args, unsigned LLONG);
299                                 else
300                                         value = (long)va_arg (args, unsigned int);
301                                 fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags);
302                                 break;
303                         case 'u':
304                                 flags |= DP_F_UNSIGNED;
305                                 if (cflags == DP_C_SHORT)
306                                         value = va_arg (args, unsigned int);
307                                 else if (cflags == DP_C_LONG)
308                                         value = (long)va_arg (args, unsigned long int);
309                                 else if (cflags == DP_C_LLONG)
310                                         value = (LLONG)va_arg (args, unsigned LLONG);
311                                 else
312                                         value = (long)va_arg (args, unsigned int);
313                                 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
314                                 break;
315                         case 'X':
316                                 flags |= DP_F_UP;
317                         case 'x':
318                                 flags |= DP_F_UNSIGNED;
319                                 if (cflags == DP_C_SHORT)
320                                         value = va_arg (args, unsigned int);
321                                 else if (cflags == DP_C_LONG)
322                                         value = (long)va_arg (args, unsigned long int);
323                                 else if (cflags == DP_C_LLONG)
324                                         value = (LLONG)va_arg (args, unsigned LLONG);
325                                 else
326                                         value = (long)va_arg (args, unsigned int);
327                                 fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags);
328                                 break;
329                         case 'f':
330                                 if (cflags == DP_C_LDOUBLE)
331                                         fvalue = va_arg (args, LDOUBLE);
332                                 else
333                                         fvalue = va_arg (args, double);
334                                 /* um, floating point? */
335                                 fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
336                                 break;
337                         case 'E':
338                                 flags |= DP_F_UP;
339                         case 'e':
340                                 if (cflags == DP_C_LDOUBLE)
341                                         fvalue = va_arg (args, LDOUBLE);
342                                 else
343                                         fvalue = va_arg (args, double);
344                                 fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
345                                 break;
346                         case 'G':
347                                 flags |= DP_F_UP;
348                         case 'g':
349                                 if (cflags == DP_C_LDOUBLE)
350                                         fvalue = va_arg (args, LDOUBLE);
351                                 else
352                                         fvalue = va_arg (args, double);
353                                 fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
354                                 break;
355                         case 'c':
356                                 dopr_outch (buffer, &currlen, maxlen, va_arg (args, int));
357                                 break;
358                         case 's':
359                                 strvalue = va_arg (args, char *);
360                                 if (!strvalue) strvalue = "(NULL)";
361                                 if (max == -1) {
362                                         max = strlen(strvalue);
363                                 }
364                                 if (min > 0 && max >= 0 && min > max) max = min;
365                                 fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max);
366                                 break;
367                         case 'p':
368                                 strvalue = va_arg (args, void *);
369                                 fmtint (buffer, &currlen, maxlen, (long) strvalue, 16, min, max, flags);
370                                 break;
371                         case 'n':
372                                 if (cflags == DP_C_SHORT) {
373                                         short int *num;
374                                         num = va_arg (args, short int *);
375                                         *num = currlen;
376                                 } else if (cflags == DP_C_LONG) {
377                                         long int *num;
378                                         num = va_arg (args, long int *);
379                                         *num = (long int)currlen;
380                                 } else if (cflags == DP_C_LLONG) {
381                                         LLONG *num;
382                                         num = va_arg (args, LLONG *);
383                                         *num = (LLONG)currlen;
384                                 } else {
385                                         int *num;
386                                         num = va_arg (args, int *);
387                                         *num = currlen;
388                                 }
389                                 break;
390                         case '%':
391                                 dopr_outch (buffer, &currlen, maxlen, ch);
392                                 break;
393                         case 'w':
394                                 /* not supported yet, treat as next char */
395                                 ch = *format++;
396                                 break;
397                         default:
398                                 /* Unknown, skip */
399                                 break;
400                         }
401                         ch = *format++;
402                         state = DP_S_DEFAULT;
403                         flags = cflags = min = 0;
404                         max = -1;
405                         break;
406                 case DP_S_DONE:
407                         break;
408                 default:
409                         /* hmm? */
410                         break; /* some picky compilers need this */
411                 }
412         }
413         if (maxlen != 0) {
414                 if (currlen < maxlen - 1) 
415                         buffer[currlen] = '\0';
416                 else if (maxlen > 0) 
417                         buffer[maxlen - 1] = '\0';
418         }
419         
420         return currlen;
421 }
422
423 static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
424                     char *value, int flags, int min, int max)
425 {
426         int padlen, strln;     /* amount to pad */
427         int cnt = 0;
428
429 #ifdef DEBUG_SNPRINTF
430         printf("fmtstr min=%d max=%d s=[%s]\n", min, max, value);
431 #endif
432         if (value == 0) {
433                 value = "<NULL>";
434         }
435
436         for (strln = 0; value[strln]; ++strln); /* strlen */
437         padlen = min - strln;
438         if (padlen < 0) 
439                 padlen = 0;
440         if (flags & DP_F_MINUS) 
441                 padlen = -padlen; /* Left Justify */
442         
443         while ((padlen > 0) && (cnt < max)) {
444                 dopr_outch (buffer, currlen, maxlen, ' ');
445                 --padlen;
446                 ++cnt;
447         }
448         while (*value && (cnt < max)) {
449                 dopr_outch (buffer, currlen, maxlen, *value++);
450                 ++cnt;
451         }
452         while ((padlen < 0) && (cnt < max)) {
453                 dopr_outch (buffer, currlen, maxlen, ' ');
454                 ++padlen;
455                 ++cnt;
456         }
457 }
458
459 /* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
460
461 static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
462                     long value, int base, int min, int max, int flags)
463 {
464         int signvalue = 0;
465         unsigned long uvalue;
466         char convert[20];
467         int place = 0;
468         int spadlen = 0; /* amount to space pad */
469         int zpadlen = 0; /* amount to zero pad */
470         int caps = 0;
471         
472         if (max < 0)
473                 max = 0;
474         
475         uvalue = value;
476         
477         if(!(flags & DP_F_UNSIGNED)) {
478                 if( value < 0 ) {
479                         signvalue = '-';
480                         uvalue = -value;
481                 } else {
482                         if (flags & DP_F_PLUS)  /* Do a sign (+/i) */
483                                 signvalue = '+';
484                         else if (flags & DP_F_SPACE)
485                                 signvalue = ' ';
486                 }
487         }
488   
489         if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
490
491         do {
492                 convert[place++] =
493                         (caps? "0123456789ABCDEF":"0123456789abcdef")
494                         [uvalue % (unsigned)base  ];
495                 uvalue = (uvalue / (unsigned)base );
496         } while(uvalue && (place < 20));
497         if (place == 20) place--;
498         convert[place] = 0;
499
500         zpadlen = max - place;
501         spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
502         if (zpadlen < 0) zpadlen = 0;
503         if (spadlen < 0) spadlen = 0;
504         if (flags & DP_F_ZERO) {
505                 zpadlen = MAX(zpadlen, spadlen);
506                 spadlen = 0;
507         }
508         if (flags & DP_F_MINUS) 
509                 spadlen = -spadlen; /* Left Justifty */
510
511 #ifdef DEBUG_SNPRINTF
512         printf("zpad: %d, spad: %d, min: %d, max: %d, place: %d\n",
513                zpadlen, spadlen, min, max, place);
514 #endif
515
516         /* Spaces */
517         while (spadlen > 0) {
518                 dopr_outch (buffer, currlen, maxlen, ' ');
519                 --spadlen;
520         }
521
522         /* Sign */
523         if (signvalue) 
524                 dopr_outch (buffer, currlen, maxlen, signvalue);
525
526         /* Zeros */
527         if (zpadlen > 0) {
528                 while (zpadlen > 0) {
529                         dopr_outch (buffer, currlen, maxlen, '0');
530                         --zpadlen;
531                 }
532         }
533
534         /* Digits */
535         while (place > 0) 
536                 dopr_outch (buffer, currlen, maxlen, convert[--place]);
537   
538         /* Left Justified spaces */
539         while (spadlen < 0) {
540                 dopr_outch (buffer, currlen, maxlen, ' ');
541                 ++spadlen;
542         }
543 }
544
545 static LDOUBLE abs_val(LDOUBLE value)
546 {
547         LDOUBLE result = value;
548
549         if (value < 0)
550                 result = -value;
551         
552         return result;
553 }
554
555 static LDOUBLE POW10(int exp)
556 {
557         LDOUBLE result = 1;
558         
559         while (exp) {
560                 result *= 10;
561                 exp--;
562         }
563   
564         return result;
565 }
566
567 static LLONG ROUND(LDOUBLE value)
568 {
569         LLONG intpart;
570
571         intpart = (LLONG)value;
572         value = value - intpart;
573         if (value >= 0.5) intpart++;
574         
575         return intpart;
576 }
577
578 /* a replacement for modf that doesn't need the math library. Should
579    be portable, but slow */
580 static double my_modf(double x0, double *iptr)
581 {
582         int i;
583         long l;
584         double x = x0;
585         double f = 1.0;
586
587         for (i=0;i<100;i++) {
588                 l = (long)x;
589                 if (l <= (x+1) && l >= (x-1)) break;
590                 x *= 0.1;
591                 f *= 10.0;
592         }
593
594         if (i == 100) {
595                 /* yikes! the number is beyond what we can handle. What do we do? */
596                 (*iptr) = 0;
597                 return 0;
598         }
599
600         if (i != 0) {
601                 double i2;
602                 double ret;
603
604                 ret = my_modf(x0-l*f, &i2);
605                 (*iptr) = l*f + i2;
606                 return ret;
607         } 
608
609         (*iptr) = l;
610         return x - (*iptr);
611 }
612
613
614 static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
615                    LDOUBLE fvalue, int min, int max, int flags)
616 {
617         int signvalue = 0;
618         double ufvalue;
619         char iconvert[311];
620         char fconvert[311];
621         int iplace = 0;
622         int fplace = 0;
623         int padlen = 0; /* amount to pad */
624         int zpadlen = 0; 
625         int caps = 0;
626         int index;
627         double intpart;
628         double fracpart;
629         double temp;
630   
631         /* 
632          * AIX manpage says the default is 0, but Solaris says the default
633          * is 6, and sprintf on AIX defaults to 6
634          */
635         if (max < 0)
636                 max = 6;
637
638         ufvalue = abs_val (fvalue);
639
640         if (fvalue < 0) {
641                 signvalue = '-';
642         } else {
643                 if (flags & DP_F_PLUS) { /* Do a sign (+/i) */
644                         signvalue = '+';
645                 } else {
646                         if (flags & DP_F_SPACE)
647                                 signvalue = ' ';
648                 }
649         }
650
651 #if 0
652         if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
653 #endif
654
655 #if 0
656          if (max == 0) ufvalue += 0.5; /* if max = 0 we must round */
657 #endif
658
659         /* 
660          * Sorry, we only support 16 digits past the decimal because of our 
661          * conversion method
662          */
663         if (max > 16)
664                 max = 16;
665
666         /* We "cheat" by converting the fractional part to integer by
667          * multiplying by a factor of 10
668          */
669
670         temp = ufvalue;
671         my_modf(temp, &intpart);
672
673         fracpart = ROUND((POW10(max)) * (ufvalue - intpart));
674         
675         if (fracpart >= POW10(max)) {
676                 intpart++;
677                 fracpart -= POW10(max);
678         }
679
680
681         /* Convert integer part */
682         do {
683                 temp = intpart*0.1;
684                 my_modf(temp, &intpart);
685                 index = (int) ((temp -intpart +0.05)* 10.0);
686                 /* index = (int) (((double)(temp*0.1) -intpart +0.05) *10.0); */
687                 /* printf ("%llf, %f, %x\n", temp, intpart, index); */
688                 iconvert[iplace++] =
689                         (caps? "0123456789ABCDEF":"0123456789abcdef")[index];
690         } while (intpart && (iplace < 311));
691         if (iplace == 311) iplace--;
692         iconvert[iplace] = 0;
693
694         /* Convert fractional part */
695         if (fracpart)
696         {
697                 do {
698                         temp = fracpart*0.1;
699                         my_modf(temp, &fracpart);
700                         index = (int) ((temp -fracpart +0.05)* 10.0);
701                         /* index = (int) ((((temp/10) -fracpart) +0.05) *10); */
702                         /* printf ("%lf, %lf, %ld\n", temp, fracpart, index); */
703                         fconvert[fplace++] =
704                         (caps? "0123456789ABCDEF":"0123456789abcdef")[index];
705                 } while(fracpart && (fplace < 311));
706                 if (fplace == 311) fplace--;
707         }
708         fconvert[fplace] = 0;
709   
710         /* -1 for decimal point, another -1 if we are printing a sign */
711         padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0); 
712         zpadlen = max - fplace;
713         if (zpadlen < 0) zpadlen = 0;
714         if (padlen < 0) 
715                 padlen = 0;
716         if (flags & DP_F_MINUS) 
717                 padlen = -padlen; /* Left Justifty */
718         
719         if ((flags & DP_F_ZERO) && (padlen > 0)) {
720                 if (signvalue) {
721                         dopr_outch (buffer, currlen, maxlen, signvalue);
722                         --padlen;
723                         signvalue = 0;
724                 }
725                 while (padlen > 0) {
726                         dopr_outch (buffer, currlen, maxlen, '0');
727                         --padlen;
728                 }
729         }
730         while (padlen > 0) {
731                 dopr_outch (buffer, currlen, maxlen, ' ');
732                 --padlen;
733         }
734         if (signvalue) 
735                 dopr_outch (buffer, currlen, maxlen, signvalue);
736         
737         while (iplace > 0) 
738                 dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]);
739
740 #ifdef DEBUG_SNPRINTF
741         printf("fmtfp: fplace=%d zpadlen=%d\n", fplace, zpadlen);
742 #endif
743
744         /*
745          * Decimal point.  This should probably use locale to find the correct
746          * char to print out.
747          */
748         if (max > 0) {
749                 dopr_outch (buffer, currlen, maxlen, '.');
750                 
751                 while (zpadlen > 0) {
752                         dopr_outch (buffer, currlen, maxlen, '0');
753                         --zpadlen;
754                 }
755
756                 while (fplace > 0) 
757                         dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]);
758         }
759
760         while (padlen < 0) {
761                 dopr_outch (buffer, currlen, maxlen, ' ');
762                 ++padlen;
763         }
764 }
765
766 static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c)
767 {
768         if (*currlen < maxlen) {
769                 buffer[(*currlen)] = c;
770         }
771         (*currlen)++;
772 }
773
774 /* yes this really must be a ||. Don't muck with this (tridge) */
775 #if !defined(HAVE_VSNPRINTF) || !defined(HAVE_C99_VSNPRINTF)
776  int vsnprintf (char *str, size_t count, const char *fmt, va_list args)
777 {
778         return dopr(str, count, fmt, args);
779 }
780 #endif
781
782 /* yes this really must be a ||. Don't muck wiith this (tridge)
783  *
784  * The logic for these two is that we need our own definition if the
785  * OS *either* has no definition of *sprintf, or if it does have one
786  * that doesn't work properly according to the autoconf test.  Perhaps
787  * these should really be smb_snprintf to avoid conflicts with buggy
788  * linkers? -- mbp
789  */
790 #if !defined(HAVE_SNPRINTF) || !defined(HAVE_C99_SNPRINTF)
791  int snprintf(char *str,size_t count,const char *fmt,...)
792 {
793         size_t ret;
794         va_list ap;
795     
796         va_start(ap, fmt);
797         ret = vsnprintf(str, count, fmt, ap);
798         va_end(ap);
799         return ret;
800 }
801 #endif
802
803 #endif 
804
805 #ifndef HAVE_VASPRINTF
806  int vasprintf(char **ptr, const char *format, va_list ap)
807 {
808         int ret;
809         va_list ap2;
810
811         VA_COPY(ap2, ap);
812         
813         ret = vsnprintf(NULL, 0, format, ap2);
814         if (ret <= 0) return ret;
815
816         (*ptr) = (char *)malloc(ret+1);
817         if (!*ptr) return -1;
818
819         VA_COPY(ap2, ap);
820
821         ret = vsnprintf(*ptr, ret+1, format, ap2);
822
823         return ret;
824 }
825 #endif
826
827
828 #ifndef HAVE_ASPRINTF
829  int asprintf(char **ptr, const char *format, ...)
830 {
831         va_list ap;
832         int ret;
833         
834         *ptr = NULL;
835         va_start(ap, format);
836         ret = vasprintf(ptr, format, ap);
837         va_end(ap);
838
839         return ret;
840 }
841 #endif
842
843 #ifdef TEST_SNPRINTF
844
845  int sprintf(char *str,const char *fmt,...);
846
847  int main (void)
848 {
849         char buf1[1024];
850         char buf2[1024];
851         char *fp_fmt[] = {
852                 "%1.1f",
853                 "%-1.5f",
854                 "%1.5f",
855                 "%123.9f",
856                 "%10.5f",
857                 "% 10.5f",
858                 "%+22.9f",
859                 "%+4.9f",
860                 "%01.3f",
861                 "%4f",
862                 "%3.1f",
863                 "%3.2f",
864                 "%.0f",
865                 "%f",
866                 "-16.16f",
867                 NULL
868         };
869         double fp_nums[] = { 6442452944.1234, -1.5, 134.21, 91340.2, 341.1234, 0203.9, 0.96, 0.996, 
870                              0.9996, 1.996, 4.136, 5.030201, 0};
871         char *int_fmt[] = {
872                 "%-1.5d",
873                 "%1.5d",
874                 "%123.9d",
875                 "%5.5d",
876                 "%10.5d",
877                 "% 10.5d",
878                 "%+22.33d",
879                 "%01.3d",
880                 "%4d",
881                 "%d",
882                 NULL
883         };
884         long int_nums[] = { -1, 134, 91340, 341, 0203, 0};
885         char *str_fmt[] = {
886                 "10.5s",
887                 "5.10s",
888                 "10.1s",
889                 "0.10s",
890                 "10.0s",
891                 "1.10s",
892                 "%s",
893                 "%.1s",
894                 "%.10s",
895                 "%10s",
896                 NULL
897         };
898         char *str_vals[] = {"hello", "a", "", "a longer string", NULL};
899         int x, y;
900         int fail = 0;
901         int num = 0;
902
903         printf ("Testing snprintf format codes against system sprintf...\n");
904
905         for (x = 0; fp_fmt[x] ; x++) {
906                 for (y = 0; fp_nums[y] != 0 ; y++) {
907                         int l1 = snprintf(NULL, 0, fp_fmt[x], fp_nums[y]);
908                         int l2 = snprintf(buf1, sizeof(buf1), fp_fmt[x], fp_nums[y]);
909                         sprintf (buf2, fp_fmt[x], fp_nums[y]);
910                         if (strcmp (buf1, buf2)) {
911                                 printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n", 
912                                        fp_fmt[x], buf1, buf2);
913                                 fail++;
914                         }
915                         if (l1 != l2) {
916                                 printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, fp_fmt[x]);
917                                 fail++;
918                         }
919                         num++;
920                 }
921         }
922
923         for (x = 0; int_fmt[x] ; x++) {
924                 for (y = 0; int_nums[y] != 0 ; y++) {
925                         int l1 = snprintf(NULL, 0, int_fmt[x], int_nums[y]);
926                         int l2 = snprintf(buf1, sizeof(buf1), int_fmt[x], int_nums[y]);
927                         sprintf (buf2, int_fmt[x], int_nums[y]);
928                         if (strcmp (buf1, buf2)) {
929                                 printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n", 
930                                        int_fmt[x], buf1, buf2);
931                                 fail++;
932                         }
933                         if (l1 != l2) {
934                                 printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, int_fmt[x]);
935                                 fail++;
936                         }
937                         num++;
938                 }
939         }
940
941         for (x = 0; str_fmt[x] ; x++) {
942                 for (y = 0; str_vals[y] != 0 ; y++) {
943                         int l1 = snprintf(NULL, 0, str_fmt[x], str_vals[y]);
944                         int l2 = snprintf(buf1, sizeof(buf1), str_fmt[x], str_vals[y]);
945                         sprintf (buf2, str_fmt[x], str_vals[y]);
946                         if (strcmp (buf1, buf2)) {
947                                 printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n", 
948                                        str_fmt[x], buf1, buf2);
949                                 fail++;
950                         }
951                         if (l1 != l2) {
952                                 printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, str_fmt[x]);
953                                 fail++;
954                         }
955                         num++;
956                 }
957         }
958
959         printf ("%d tests failed out of %d.\n", fail, num);
960
961         printf("seeing how many digits we support\n");
962         {
963                 double v0 = 0.12345678901234567890123456789012345678901;
964                 for (x=0; x<100; x++) {
965                         double p = pow(10, x); 
966                         double r = v0*p;
967                         snprintf(buf1, sizeof(buf1), "%1.1f", r);
968                         sprintf(buf2,                "%1.1f", r);
969                         if (strcmp(buf1, buf2)) {
970                                 printf("we seem to support %d digits\n", x-1);
971                                 break;
972                         }
973                 }
974         }
975
976         return 0;
977 }
978 #endif /* SNPRINTF_TEST */