]> arthur.barton.de Git - ngircd-alex.git/blob - src/portab/vsnprintf.c
081152e123b7f62fda95ad9fbb11e9606f26744d
[ngircd-alex.git] / src / portab / vsnprintf.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * Please read the file COPYING, README and AUTHORS for more information.
10  */
11
12
13 #include "portab.h"
14
15 /**
16  * @file
17  * snprintf() and vsnprintf() replacement functions
18  */
19
20 /*
21  * snprintf.c: Copyright Patrick Powell 1995
22  * This code is based on code written by Patrick Powell (papowell@astart.com)
23  * It may be used for any purpose as long as this notice remains intact
24  * on all source code distributions
25  *
26  * Original: Patrick Powell Tue Apr 11 09:48:21 PDT 1995
27  * A bombproof version of doprnt (dopr) included.
28  * Sigh. This sort of thing is always nasty do deal with. Note that
29  * the version here does not include floating point...
30  *
31  * snprintf() is used instead of sprintf() as it does limit checks
32  * for string length. This covers a nasty loophole.
33  *
34  * The other functions are there to prevent NULL pointers from
35  * causing nast effects.
36  *
37  * More Recently:
38  *  Brandon Long <blong@fiction.net> 9/15/96 for mutt 0.43
39  *  This was ugly. It is still ugly. I opted out of floating point
40  *  numbers, but the formatter understands just about everything
41  *  from the normal C string format, at least as far as I can tell from
42  *  the Solaris 2.5 printf(3S) man page.
43  *
44  * Brandon Long <blong@fiction.net> 10/22/97 for mutt 0.87.1
45  *  Ok, added some minimal floating point support, which means this
46  *  probably requires libm on most operating systems. Don't yet
47  *  support the exponent (e,E) and sigfig (g,G). Also, fmtint()
48  *  was pretty badly broken, it just wasn't being exercised in ways
49  *  which showed it, so that's been fixed. Also, formated the code
50  *  to mutt conventions, and removed dead code left over from the
51  *  original. Also, there is now a builtin-test, just compile with:
52  *    gcc -DTEST_SNPRINTF -o snprintf snprintf.c -lm
53  *  and run snprintf for results.
54  * 
55  * Thomas Roessler <roessler@guug.de> 01/27/98 for mutt 0.89i
56  *  The PGP code was using unsigned hexadecimal formats. 
57  *  Unfortunately, unsigned formats simply didn't work.
58  *
59  * Michael Elkins <me@cs.hmc.edu> 03/05/98 for mutt 0.90.8
60  *  The original code assumed that both snprintf() and vsnprintf() were
61  *  missing. Some systems only have snprintf() but not vsnprintf(), so
62  *  the code is now broken down under HAVE_SNPRINTF and HAVE_VSNPRINTF.
63  *
64  * Andrew Tridgell <tridge@samba.org>, October 1998
65  *  fixed handling of %.0f
66  *  added test for HAVE_LONG_DOUBLE
67  *
68  * tridge@samba.org, idra@samba.org, April 2001
69  *  got rid of fcvt code (twas buggy and made testing harder)
70  *  added C99 semantics
71  *
72  * Alexander Barton, <alex@barton.de>, 2002-05-19
73  *  removed [v]asprintf() and C99 tests: not needed by ngIRCd.
74  */
75
76
77 #ifdef HAVE_STRING_H
78 #include <string.h>
79 #endif
80 #ifdef HAVE_STRINGS_H
81 #include <strings.h>
82 #endif
83 #ifdef HAVE_CTYPE_H
84 #include <ctype.h>
85 #endif
86 #include <sys/types.h>
87 #include <stdarg.h>
88 #ifdef HAVE_STDLIB_H
89 #include <stdlib.h>
90 #endif
91
92
93 #if defined(HAVE_SNPRINTF) && defined(HAVE_VSNPRINTF)
94 /* only include stdio.h if we are not re-defining snprintf or vsnprintf */
95 #include <stdio.h>
96 /* make the compiler happy with an empty file */
97 void dummy_snprintf PARAMS(( void ));
98 void dummy_snprintf PARAMS(( void )) { }
99 #else
100
101 #ifdef HAVE_LONG_DOUBLE
102 #define LDOUBLE long double
103 #else
104 #define LDOUBLE double
105 #endif
106
107 #ifdef HAVE_LONG_LONG
108 #define LLONG long long
109 #else
110 #define LLONG long
111 #endif
112
113 static size_t dopr(char *buffer, size_t maxlen, const char *format, 
114                    va_list args);
115 static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
116                     char *value, int flags, int min, int max);
117 static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
118                     long value, int base, int min, int max, int flags);
119 static void fmtfp(char *buffer, size_t *currlen, size_t maxlen,
120                    LDOUBLE fvalue, int min, int max, int flags);
121 static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c);
122
123 /*
124  * dopr(): poor man's version of doprintf
125  */
126
127 /* format read states */
128 #define DP_S_DEFAULT 0
129 #define DP_S_FLAGS   1
130 #define DP_S_MIN     2
131 #define DP_S_DOT     3
132 #define DP_S_MAX     4
133 #define DP_S_MOD     5
134 #define DP_S_CONV    6
135 #define DP_S_DONE    7
136
137 /* format flags - Bits */
138 #define DP_F_MINUS      (1 << 0)
139 #define DP_F_PLUS       (1 << 1)
140 #define DP_F_SPACE      (1 << 2)
141 #define DP_F_NUM        (1 << 3)
142 #define DP_F_ZERO       (1 << 4)
143 #define DP_F_UP  (1 << 5)
144 #define DP_F_UNSIGNED   (1 << 6)
145
146 /* Conversion Flags */
147 #define DP_C_SHORT   1
148 #define DP_C_LONG    2
149 #define DP_C_LDOUBLE 3
150 #define DP_C_LLONG   4
151
152 #define char_to_int(p) ((p)- '0')
153 #ifndef MAX
154 #define MAX(p,q) (((p) >= (q)) ? (p) : (q))
155 #endif
156
157 static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args)
158 {
159         char ch;
160         LLONG value;
161         LDOUBLE fvalue;
162         char *strvalue;
163         int min;
164         int max;
165         int state;
166         int flags;
167         int cflags;
168         size_t currlen;
169         
170         state = DP_S_DEFAULT;
171         currlen = flags = cflags = min = 0;
172         max = -1;
173         ch = *format++;
174         
175         while (state != DP_S_DONE) {
176                 if (ch == '\0') 
177                         state = DP_S_DONE;
178
179                 switch(state) {
180                 case DP_S_DEFAULT:
181                         if (ch == '%') 
182                                 state = DP_S_FLAGS;
183                         else 
184                                 dopr_outch (buffer, &currlen, maxlen, ch);
185                         ch = *format++;
186                         break;
187                 case DP_S_FLAGS:
188                         switch (ch) {
189                         case '-':
190                                 flags |= DP_F_MINUS;
191                                 ch = *format++;
192                                 break;
193                         case '+':
194                                 flags |= DP_F_PLUS;
195                                 ch = *format++;
196                                 break;
197                         case ' ':
198                                 flags |= DP_F_SPACE;
199                                 ch = *format++;
200                                 break;
201                         case '#':
202                                 flags |= DP_F_NUM;
203                                 ch = *format++;
204                                 break;
205                         case '0':
206                                 flags |= DP_F_ZERO;
207                                 ch = *format++;
208                                 break;
209                         default:
210                                 state = DP_S_MIN;
211                                 break;
212                         }
213                         break;
214                 case DP_S_MIN:
215                         if (isdigit((unsigned char)ch)) {
216                                 min = 10*min + char_to_int (ch);
217                                 ch = *format++;
218                         } else if (ch == '*') {
219                                 min = va_arg (args, int);
220                                 ch = *format++;
221                                 state = DP_S_DOT;
222                         } else {
223                                 state = DP_S_DOT;
224                         }
225                         break;
226                 case DP_S_DOT:
227                         if (ch == '.') {
228                                 state = DP_S_MAX;
229                                 ch = *format++;
230                         } else { 
231                                 state = DP_S_MOD;
232                         }
233                         break;
234                 case DP_S_MAX:
235                         if (isdigit((unsigned char)ch)) {
236                                 if (max < 0)
237                                         max = 0;
238                                 max = 10*max + char_to_int (ch);
239                                 ch = *format++;
240                         } else if (ch == '*') {
241                                 max = va_arg (args, int);
242                                 ch = *format++;
243                                 state = DP_S_MOD;
244                         } else {
245                                 state = DP_S_MOD;
246                         }
247                         break;
248                 case DP_S_MOD:
249                         switch (ch) {
250                         case 'h':
251                                 cflags = DP_C_SHORT;
252                                 ch = *format++;
253                                 break;
254                         case 'l':
255                                 cflags = DP_C_LONG;
256                                 ch = *format++;
257                                 if (ch == 'l') {        /* It's a long long */
258                                         cflags = DP_C_LLONG;
259                                         ch = *format++;
260                                 }
261                                 break;
262                         case 'L':
263                                 cflags = DP_C_LDOUBLE;
264                                 ch = *format++;
265                                 break;
266                         default:
267                                 break;
268                         }
269                         state = DP_S_CONV;
270                         break;
271                 case DP_S_CONV:
272                         switch (ch) {
273                         case 'd':
274                         case 'i':
275                                 if (cflags == DP_C_SHORT) 
276                                         value = va_arg (args, 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 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 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 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 == -1) {
353                                         max = strlen(strvalue);
354                                 }
355                                 if (min > 0 && max >= 0 && min > max) max = min;
356                                 fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max);
357                                 break;
358                         case 'p':
359                                 strvalue = va_arg (args, void *);
360                                 fmtint (buffer, &currlen, maxlen, (long) strvalue, 16, min, max, flags);
361                                 break;
362                         case 'n':
363                                 if (cflags == DP_C_SHORT) {
364                                         short int *num;
365                                         num = va_arg (args, short int *);
366                                         *num = currlen;
367                                 } else if (cflags == DP_C_LONG) {
368                                         long int *num;
369                                         num = va_arg (args, long int *);
370                                         *num = (long int)currlen;
371                                 } else if (cflags == DP_C_LLONG) {
372                                         LLONG *num;
373                                         num = va_arg (args, LLONG *);
374                                         *num = (LLONG)currlen;
375                                 } else {
376                                         int *num;
377                                         num = va_arg (args, int *);
378                                         *num = currlen;
379                                 }
380                                 break;
381                         case '%':
382                                 dopr_outch (buffer, &currlen, maxlen, ch);
383                                 break;
384                         case 'w':
385                                 /* not supported yet, treat as next char */
386                                 ch = *format++;
387                                 break;
388                         default:
389                                 /* Unknown, skip */
390                                 break;
391                         }
392                         ch = *format++;
393                         state = DP_S_DEFAULT;
394                         flags = cflags = min = 0;
395                         max = -1;
396                         break;
397                 case DP_S_DONE:
398                         break;
399                 default:
400                         /* hmm? */
401                         break; /* some picky compilers need this */
402                 }
403         }
404         if (maxlen != 0) {
405                 if (currlen < maxlen - 1) 
406                         buffer[currlen] = '\0';
407                 else if (maxlen > 0) 
408                         buffer[maxlen - 1] = '\0';
409         }
410         
411         return currlen;
412 }
413
414 static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
415                     char *value, int flags, int min, int max)
416 {
417         int padlen, strln;     /* amount to pad */
418         int cnt = 0;
419
420 #ifdef DEBUG_SNPRINTF
421         printf("fmtstr min=%d max=%d s=[%s]\n", min, max, value);
422 #endif
423         if (value == 0) {
424                 value = "<NULL>";
425         }
426
427         for (strln = 0; value[strln]; ++strln); /* strlen */
428         padlen = min - strln;
429         if (padlen < 0) 
430                 padlen = 0;
431         if (flags & DP_F_MINUS) 
432                 padlen = -padlen; /* Left Justify */
433         
434         while ((padlen > 0) && (cnt < max)) {
435                 dopr_outch (buffer, currlen, maxlen, ' ');
436                 --padlen;
437                 ++cnt;
438         }
439         while (*value && (cnt < max)) {
440                 dopr_outch (buffer, currlen, maxlen, *value++);
441                 ++cnt;
442         }
443         while ((padlen < 0) && (cnt < max)) {
444                 dopr_outch (buffer, currlen, maxlen, ' ');
445                 ++padlen;
446                 ++cnt;
447         }
448 }
449
450 /* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
451
452 static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
453                     long value, int base, int min, int max, int flags)
454 {
455         int signvalue = 0;
456         unsigned long uvalue;
457         char convert[20];
458         int place = 0;
459         int spadlen = 0; /* amount to space pad */
460         int zpadlen = 0; /* amount to zero pad */
461         int caps = 0;
462         
463         if (max < 0)
464                 max = 0;
465         
466         uvalue = value;
467         
468         if(!(flags & DP_F_UNSIGNED)) {
469                 if( value < 0 ) {
470                         signvalue = '-';
471                         uvalue = -value;
472                 } else {
473                         if (flags & DP_F_PLUS)  /* Do a sign (+/i) */
474                                 signvalue = '+';
475                         else if (flags & DP_F_SPACE)
476                                 signvalue = ' ';
477                 }
478         }
479   
480         if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
481
482         do {
483                 convert[place++] =
484                         (caps? "0123456789ABCDEF":"0123456789abcdef")
485                         [uvalue % (unsigned)base  ];
486                 uvalue = (uvalue / (unsigned)base );
487         } while(uvalue && (place < 20));
488         if (place == 20) place--;
489         convert[place] = 0;
490
491         zpadlen = max - place;
492         spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
493         if (zpadlen < 0) zpadlen = 0;
494         if (spadlen < 0) spadlen = 0;
495         if (flags & DP_F_ZERO) {
496                 zpadlen = MAX(zpadlen, spadlen);
497                 spadlen = 0;
498         }
499         if (flags & DP_F_MINUS) 
500                 spadlen = -spadlen; /* Left Justifty */
501
502 #ifdef DEBUG_SNPRINTF
503         printf("zpad: %d, spad: %d, min: %d, max: %d, place: %d\n",
504                zpadlen, spadlen, min, max, place);
505 #endif
506
507         /* Spaces */
508         while (spadlen > 0) {
509                 dopr_outch (buffer, currlen, maxlen, ' ');
510                 --spadlen;
511         }
512
513         /* Sign */
514         if (signvalue) 
515                 dopr_outch (buffer, currlen, maxlen, signvalue);
516
517         /* Zeros */
518         if (zpadlen > 0) {
519                 while (zpadlen > 0) {
520                         dopr_outch (buffer, currlen, maxlen, '0');
521                         --zpadlen;
522                 }
523         }
524
525         /* Digits */
526         while (place > 0) 
527                 dopr_outch (buffer, currlen, maxlen, convert[--place]);
528   
529         /* Left Justified spaces */
530         while (spadlen < 0) {
531                 dopr_outch (buffer, currlen, maxlen, ' ');
532                 ++spadlen;
533         }
534 }
535
536 static LDOUBLE abs_val(LDOUBLE value)
537 {
538         LDOUBLE result = value;
539
540         if (value < 0)
541                 result = -value;
542         
543         return result;
544 }
545
546 static LDOUBLE POW10(int exp)
547 {
548         LDOUBLE result = 1;
549         
550         while (exp) {
551                 result *= 10;
552                 exp--;
553         }
554   
555         return result;
556 }
557
558 static LLONG ROUND(LDOUBLE value)
559 {
560         LLONG intpart;
561
562         intpart = (LLONG)value;
563         value = value - intpart;
564         if (value >= 0.5) intpart++;
565         
566         return intpart;
567 }
568
569 /* a replacement for modf that doesn't need the math library. Should
570    be portable, but slow */
571 static double my_modf(double x0, double *iptr)
572 {
573         int i;
574         long l;
575         double x = x0;
576         double f = 1.0;
577
578         for (i=0;i<100;i++) {
579                 l = (long)x;
580                 if (l <= (x+1) && l >= (x-1)) break;
581                 x *= 0.1;
582                 f *= 10.0;
583         }
584
585         if (i == 100) {
586                 /* yikes! the number is beyond what we can handle. What do we do? */
587                 (*iptr) = 0;
588                 return 0;
589         }
590
591         if (i != 0) {
592                 double i2;
593                 double ret;
594
595                 ret = my_modf(x0-l*f, &i2);
596                 (*iptr) = l*f + i2;
597                 return ret;
598         } 
599
600         (*iptr) = l;
601         return x - (*iptr);
602 }
603
604
605 static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
606                    LDOUBLE fvalue, int min, int max, int flags)
607 {
608         int signvalue = 0;
609         double ufvalue;
610         char iconvert[311];
611         char fconvert[311];
612         int iplace = 0;
613         int fplace = 0;
614         int padlen = 0; /* amount to pad */
615         int zpadlen = 0; 
616         int caps = 0;
617         int index;
618         double intpart;
619         double fracpart;
620         double temp;
621   
622         /* 
623          * AIX manpage says the default is 0, but Solaris says the default
624          * is 6, and sprintf on AIX defaults to 6
625          */
626         if (max < 0)
627                 max = 6;
628
629         ufvalue = abs_val (fvalue);
630
631         if (fvalue < 0) {
632                 signvalue = '-';
633         } else {
634                 if (flags & DP_F_PLUS) { /* Do a sign (+/i) */
635                         signvalue = '+';
636                 } else {
637                         if (flags & DP_F_SPACE)
638                                 signvalue = ' ';
639                 }
640         }
641
642 #if 0
643         if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
644 #endif
645
646 #if 0
647         if (max == 0) ufvalue += 0.5; /* if max = 0 we must round */
648 #endif
649
650         /* 
651          * Sorry, we only support 16 digits past the decimal because of our 
652          * conversion method
653          */
654         if (max > 16)
655                 max = 16;
656
657         /* We "cheat" by converting the fractional part to integer by
658          * multiplying by a factor of 10
659          */
660
661         temp = ufvalue;
662         my_modf(temp, &intpart);
663
664         fracpart = ROUND((POW10(max)) * (ufvalue - intpart));
665         
666         if (fracpart >= POW10(max)) {
667                 intpart++;
668                 fracpart -= POW10(max);
669         }
670
671
672         /* Convert integer part */
673         do {
674                 temp = intpart;
675                 my_modf(intpart*0.1, &intpart);
676                 temp = temp*0.1;
677                 index = (int) ((temp -intpart +0.05)* 10.0);
678                 /* index = (int) (((double)(temp*0.1) -intpart +0.05) *10.0); */
679                 /* printf ("%llf, %f, %x\n", temp, intpart, index); */
680                 iconvert[iplace++] =
681                         (caps? "0123456789ABCDEF":"0123456789abcdef")[index];
682         } while (intpart && (iplace < 311));
683         if (iplace == 311) iplace--;
684         iconvert[iplace] = 0;
685
686         /* Convert fractional part */
687         if (fracpart)
688         {
689                 do {
690                         temp = fracpart;
691                         my_modf(fracpart*0.1, &fracpart);
692                         temp = temp*0.1;
693                         index = (int) ((temp -fracpart +0.05)* 10.0);
694                         /* index = (int) ((((temp/10) -fracpart) +0.05) *10); */
695                         /* printf ("%lf, %lf, %ld\n", temp, fracpart, index); */
696                         fconvert[fplace++] =
697                         (caps? "0123456789ABCDEF":"0123456789abcdef")[index];
698                 } while(fracpart && (fplace < 311));
699                 if (fplace == 311) fplace--;
700         }
701         fconvert[fplace] = 0;
702   
703         /* -1 for decimal point, another -1 if we are printing a sign */
704         padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0); 
705         zpadlen = max - fplace;
706         if (zpadlen < 0) zpadlen = 0;
707         if (padlen < 0) 
708                 padlen = 0;
709         if (flags & DP_F_MINUS) 
710                 padlen = -padlen; /* Left Justifty */
711         
712         if ((flags & DP_F_ZERO) && (padlen > 0)) {
713                 if (signvalue) {
714                         dopr_outch (buffer, currlen, maxlen, signvalue);
715                         --padlen;
716                         signvalue = 0;
717                 }
718                 while (padlen > 0) {
719                         dopr_outch (buffer, currlen, maxlen, '0');
720                         --padlen;
721                 }
722         }
723         while (padlen > 0) {
724                 dopr_outch (buffer, currlen, maxlen, ' ');
725                 --padlen;
726         }
727         if (signvalue) 
728                 dopr_outch (buffer, currlen, maxlen, signvalue);
729         
730         while (iplace > 0) 
731                 dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]);
732
733 #ifdef DEBUG_SNPRINTF
734         printf("fmtfp: fplace=%d zpadlen=%d\n", fplace, zpadlen);
735 #endif
736
737         /*
738          * Decimal point.  This should probably use locale to find the correct
739          * char to print out.
740          */
741         if (max > 0) {
742                 dopr_outch (buffer, currlen, maxlen, '.');
743                 
744                 while (fplace > 0) 
745                         dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]);
746         }
747         
748         while (zpadlen > 0) {
749                 dopr_outch (buffer, currlen, maxlen, '0');
750                 --zpadlen;
751         }
752
753         while (padlen < 0) {
754                 dopr_outch (buffer, currlen, maxlen, ' ');
755                 ++padlen;
756         }
757 }
758
759 static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c)
760 {
761         if (*currlen < maxlen) {
762                 buffer[(*currlen)] = c;
763         }
764         (*currlen)++;
765 }
766
767 #if !defined(HAVE_VSNPRINTF)
768 int vsnprintf (char *str, size_t count, const char *fmt, va_list args)
769 {
770         return dopr(str, count, fmt, args);
771 }
772 #endif
773
774 #if !defined(HAVE_SNPRINTF)
775 int snprintf(char *str,size_t count,const char *fmt,...)
776 {
777         size_t ret;
778         va_list ap;
779     
780         va_start(ap, fmt);
781         ret = vsnprintf(str, count, fmt, ap);
782         va_end(ap);
783         return ret;
784 }
785 #endif
786
787 #endif
788
789
790 #ifdef TEST_SNPRINTF
791 int sprintf(char *str,const char *fmt,...);
792 int main (void)
793 {
794         char buf1[1024];
795         char buf2[1024];
796         char *fp_fmt[] = {
797                 "%1.1f",
798                 "%-1.5f",
799                 "%1.5f",
800                 "%123.9f",
801                 "%10.5f",
802                 "% 10.5f",
803                 "%+22.9f",
804                 "%+4.9f",
805                 "%01.3f",
806                 "%4f",
807                 "%3.1f",
808                 "%3.2f",
809                 "%.0f",
810                 "%f",
811                 "-16.16f",
812                 NULL
813         };
814         double fp_nums[] = { 6442452944.1234, -1.5, 134.21, 91340.2, 341.1234, 0203.9, 0.96, 0.996, 
815                              0.9996, 1.996, 4.136,  0};
816         char *int_fmt[] = {
817                 "%-1.5d",
818                 "%1.5d",
819                 "%123.9d",
820                 "%5.5d",
821                 "%10.5d",
822                 "% 10.5d",
823                 "%+22.33d",
824                 "%01.3d",
825                 "%4d",
826                 "%d",
827                 NULL
828         };
829         long int_nums[] = { -1, 134, 91340, 341, 0203, 0};
830         char *str_fmt[] = {
831                 "10.5s",
832                 "5.10s",
833                 "10.1s",
834                 "0.10s",
835                 "10.0s",
836                 "1.10s",
837                 "%s",
838                 "%.1s",
839                 "%.10s",
840                 "%10s",
841                 NULL
842         };
843         char *str_vals[] = {"hello", "a", "", "a longer string", NULL};
844         int x, y;
845         int fail = 0;
846         int num = 0;
847
848         printf ("Testing snprintf format codes against system sprintf...\n");
849
850         for (x = 0; fp_fmt[x] ; x++) {
851                 for (y = 0; fp_nums[y] != 0 ; y++) {
852                         int l1 = snprintf(NULL, 0, fp_fmt[x], fp_nums[y]);
853                         int l2 = snprintf(buf1, sizeof(buf1), fp_fmt[x], fp_nums[y]);
854                         sprintf (buf2, fp_fmt[x], fp_nums[y]);
855                         if (strcmp (buf1, buf2)) {
856                                 printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n", 
857                                        fp_fmt[x], buf1, buf2);
858                                 fail++;
859                         }
860                         if (l1 != l2) {
861                                 printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, fp_fmt[x]);
862                                 fail++;
863                         }
864                         num++;
865                 }
866         }
867
868         for (x = 0; int_fmt[x] ; x++) {
869                 for (y = 0; int_nums[y] != 0 ; y++) {
870                         int l1 = snprintf(NULL, 0, int_fmt[x], int_nums[y]);
871                         int l2 = snprintf(buf1, sizeof(buf1), int_fmt[x], int_nums[y]);
872                         sprintf (buf2, int_fmt[x], int_nums[y]);
873                         if (strcmp (buf1, buf2)) {
874                                 printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n", 
875                                        int_fmt[x], buf1, buf2);
876                                 fail++;
877                         }
878                         if (l1 != l2) {
879                                 printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, int_fmt[x]);
880                                 fail++;
881                         }
882                         num++;
883                 }
884         }
885
886         for (x = 0; str_fmt[x] ; x++) {
887                 for (y = 0; str_vals[y] != 0 ; y++) {
888                         int l1 = snprintf(NULL, 0, str_fmt[x], str_vals[y]);
889                         int l2 = snprintf(buf1, sizeof(buf1), str_fmt[x], str_vals[y]);
890                         sprintf (buf2, str_fmt[x], str_vals[y]);
891                         if (strcmp (buf1, buf2)) {
892                                 printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n", 
893                                        str_fmt[x], buf1, buf2);
894                                 fail++;
895                         }
896                         if (l1 != l2) {
897                                 printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, str_fmt[x]);
898                                 fail++;
899                         }
900                         num++;
901                 }
902         }
903
904         printf ("%d tests failed out of %d.\n", fail, num);
905
906         printf("seeing how many digits we support\n");
907         {
908                 double v0 = 0.12345678901234567890123456789012345678901;
909                 for (x=0; x<100; x++) {
910                         snprintf(buf1, sizeof(buf1), "%1.1f", v0*pow(10, x));
911                         sprintf(buf2,           "%1.1f", v0*pow(10, x));
912                         if (strcmp(buf1, buf2)) {
913                                 printf("we seem to support %d digits\n", x-1);
914                                 break;
915                         }
916                 }
917         }
918         return 0;
919 }
920 #endif /* SNPRINTF_TEST */
921
922
923 /* -eof- */