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