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