]> andersk Git - openssh.git/blob - openbsd-compat/bsd-snprintf.c
- (dtucker) [ssh-keyscan.c ssh-rand-helper.c ssh.c sshconnect.c
[openssh.git] / openbsd-compat / bsd-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  * date: 2002/12/19 19:56:31;  author: herb;  state: Exp;  lines: +2 -0
57  * actually print args for %g and %e
58  * 
59  * date: 2002/06/03 13:37:52;  author: jmcd;  state: Exp;  lines: +8 -0
60  * Since includes.h isn't included here, VA_COPY has to be defined here.  I don't
61  * see any include file that is guaranteed to be here, so I'm defining it
62  * locally.  Fixes AIX and Solaris builds.
63  * 
64  * date: 2002/06/03 03:07:24;  author: tridge;  state: Exp;  lines: +5 -13
65  * put the ifdef for HAVE_VA_COPY in one place rather than in lots of
66  * functions
67  * 
68  * date: 2002/05/17 14:51:22;  author: jmcd;  state: Exp;  lines: +21 -4
69  * Fix usage of va_list passed as an arg.  Use __va_copy before using it
70  * when it exists.
71  * 
72  * date: 2002/04/16 22:38:04;  author: idra;  state: Exp;  lines: +20 -14
73  * Fix incorrect zpadlen handling in fmtfp.
74  * Thanks to Ollie Oldham <ollie.oldham@metro-optix.com> for spotting it.
75  * few mods to make it easier to compile the tests.
76  * addedd the "Ollie" test to the floating point ones.
77  *
78  * Martin Pool (mbp@samba.org) April 2003
79  *    Remove NO_CONFIG_H so that the test case can be built within a source
80  *    tree with less trouble.
81  *    Remove unnecessary SAFE_FREE() definition.
82  *
83  * Martin Pool (mbp@samba.org) May 2003
84  *    Put in a prototype for dummy_snprintf() to quiet compiler warnings.
85  *
86  *    Move #endif to make sure VA_COPY, LDOUBLE, etc are defined even
87  *    if the C library has some snprintf functions already.
88  **************************************************************/
89
90 #include "includes.h"
91
92 #if defined(BROKEN_SNPRINTF)            /* For those with broken snprintf() */
93 # undef HAVE_SNPRINTF
94 # undef HAVE_VSNPRINTF
95 #endif
96
97 #ifndef VA_COPY
98 # ifdef HAVE_VA_COPY
99 #  define VA_COPY(dest, src) va_copy(dest, src)
100 # else
101 #  ifdef HAVE___VA_COPY
102 #   define VA_COPY(dest, src) __va_copy(dest, src)
103 #  else
104 #   define VA_COPY(dest, src) (dest) = (src)
105 #  endif
106 # endif
107 #endif
108
109 #if !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF)
110
111 #include <ctype.h>
112 #include <stdlib.h>
113 #include <string.h>
114
115 #ifdef HAVE_LONG_DOUBLE
116 # define LDOUBLE long double
117 #else
118 # define LDOUBLE double
119 #endif
120
121 #ifdef HAVE_LONG_LONG
122 # define LLONG long long
123 #else
124 # define LLONG long
125 #endif
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, 
162                    va_list args_in);
163 static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
164                     char *value, int flags, int min, int max);
165 static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
166                     LLONG value, int base, int min, int max, int flags);
167 static void fmtfp(char *buffer, size_t *currlen, size_t maxlen,
168                    LDOUBLE fvalue, int min, int max, int flags);
169 static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c);
170
171 static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args_in)
172 {
173         char ch;
174         LLONG value;
175         LDOUBLE fvalue;
176         char *strvalue;
177         int min;
178         int max;
179         int state;
180         int flags;
181         int cflags;
182         size_t currlen;
183         va_list args;
184
185         VA_COPY(args, args_in);
186         
187         state = DP_S_DEFAULT;
188         currlen = flags = cflags = min = 0;
189         max = -1;
190         ch = *format++;
191         
192         while (state != DP_S_DONE) {
193                 if (ch == '\0') 
194                         state = DP_S_DONE;
195
196                 switch(state) {
197                 case DP_S_DEFAULT:
198                         if (ch == '%') 
199                                 state = DP_S_FLAGS;
200                         else 
201                                 dopr_outch (buffer, &currlen, maxlen, ch);
202                         ch = *format++;
203                         break;
204                 case DP_S_FLAGS:
205                         switch (ch) {
206                         case '-':
207                                 flags |= DP_F_MINUS;
208                                 ch = *format++;
209                                 break;
210                         case '+':
211                                 flags |= DP_F_PLUS;
212                                 ch = *format++;
213                                 break;
214                         case ' ':
215                                 flags |= DP_F_SPACE;
216                                 ch = *format++;
217                                 break;
218                         case '#':
219                                 flags |= DP_F_NUM;
220                                 ch = *format++;
221                                 break;
222                         case '0':
223                                 flags |= DP_F_ZERO;
224                                 ch = *format++;
225                                 break;
226                         default:
227                                 state = DP_S_MIN;
228                                 break;
229                         }
230                         break;
231                 case DP_S_MIN:
232                         if (isdigit((unsigned char)ch)) {
233                                 min = 10*min + char_to_int (ch);
234                                 ch = *format++;
235                         } else if (ch == '*') {
236                                 min = va_arg (args, int);
237                                 ch = *format++;
238                                 state = DP_S_DOT;
239                         } else {
240                                 state = DP_S_DOT;
241                         }
242                         break;
243                 case DP_S_DOT:
244                         if (ch == '.') {
245                                 state = DP_S_MAX;
246                                 ch = *format++;
247                         } else { 
248                                 state = DP_S_MOD;
249                         }
250                         break;
251                 case DP_S_MAX:
252                         if (isdigit((unsigned char)ch)) {
253                                 if (max < 0)
254                                         max = 0;
255                                 max = 10*max + char_to_int (ch);
256                                 ch = *format++;
257                         } else if (ch == '*') {
258                                 max = va_arg (args, int);
259                                 ch = *format++;
260                                 state = DP_S_MOD;
261                         } else {
262                                 state = DP_S_MOD;
263                         }
264                         break;
265                 case DP_S_MOD:
266                         switch (ch) {
267                         case 'h':
268                                 cflags = DP_C_SHORT;
269                                 ch = *format++;
270                                 break;
271                         case 'l':
272                                 cflags = DP_C_LONG;
273                                 ch = *format++;
274                                 if (ch == 'l') {        /* It's a long long */
275                                         cflags = DP_C_LLONG;
276                                         ch = *format++;
277                                 }
278                                 break;
279                         case 'L':
280                                 cflags = DP_C_LDOUBLE;
281                                 ch = *format++;
282                                 break;
283                         default:
284                                 break;
285                         }
286                         state = DP_S_CONV;
287                         break;
288                 case DP_S_CONV:
289                         switch (ch) {
290                         case 'd':
291                         case 'i':
292                                 if (cflags == DP_C_SHORT) 
293                                         value = va_arg (args, int);
294                                 else if (cflags == DP_C_LONG)
295                                         value = va_arg (args, long int);
296                                 else if (cflags == DP_C_LLONG)
297                                         value = va_arg (args, LLONG);
298                                 else
299                                         value = va_arg (args, int);
300                                 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
301                                 break;
302                         case 'o':
303                                 flags |= DP_F_UNSIGNED;
304                                 if (cflags == DP_C_SHORT)
305                                         value = va_arg (args, unsigned int);
306                                 else if (cflags == DP_C_LONG)
307                                         value = (long)va_arg (args, unsigned long int);
308                                 else if (cflags == DP_C_LLONG)
309                                         value = (long)va_arg (args, unsigned LLONG);
310                                 else
311                                         value = (long)va_arg (args, unsigned int);
312                                 fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags);
313                                 break;
314                         case 'u':
315                                 flags |= DP_F_UNSIGNED;
316                                 if (cflags == DP_C_SHORT)
317                                         value = va_arg (args, unsigned int);
318                                 else if (cflags == DP_C_LONG)
319                                         value = (long)va_arg (args, unsigned long int);
320                                 else if (cflags == DP_C_LLONG)
321                                         value = (LLONG)va_arg (args, unsigned LLONG);
322                                 else
323                                         value = (long)va_arg (args, unsigned int);
324                                 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
325                                 break;
326                         case 'X':
327                                 flags |= DP_F_UP;
328                         case 'x':
329                                 flags |= DP_F_UNSIGNED;
330                                 if (cflags == DP_C_SHORT)
331                                         value = va_arg (args, unsigned int);
332                                 else if (cflags == DP_C_LONG)
333                                         value = (long)va_arg (args, unsigned long int);
334                                 else if (cflags == DP_C_LLONG)
335                                         value = (LLONG)va_arg (args, unsigned LLONG);
336                                 else
337                                         value = (long)va_arg (args, unsigned int);
338                                 fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags);
339                                 break;
340                         case 'f':
341                                 if (cflags == DP_C_LDOUBLE)
342                                         fvalue = va_arg (args, LDOUBLE);
343                                 else
344                                         fvalue = va_arg (args, double);
345                                 /* um, floating point? */
346                                 fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
347                                 break;
348                         case 'E':
349                                 flags |= DP_F_UP;
350                         case 'e':
351                                 if (cflags == DP_C_LDOUBLE)
352                                         fvalue = va_arg (args, LDOUBLE);
353                                 else
354                                         fvalue = va_arg (args, double);
355                                 fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
356                                 break;
357                         case 'G':
358                                 flags |= DP_F_UP;
359                         case 'g':
360                                 if (cflags == DP_C_LDOUBLE)
361                                         fvalue = va_arg (args, LDOUBLE);
362                                 else
363                                         fvalue = va_arg (args, double);
364                                 fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
365                                 break;
366                         case 'c':
367                                 dopr_outch (buffer, &currlen, maxlen, va_arg (args, int));
368                                 break;
369                         case 's':
370                                 strvalue = va_arg (args, char *);
371                                 if (!strvalue) strvalue = "(NULL)";
372                                 if (max == -1) {
373                                         max = strlen(strvalue);
374                                 }
375                                 if (min > 0 && max >= 0 && min > max) max = min;
376                                 fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max);
377                                 break;
378                         case 'p':
379                                 strvalue = va_arg (args, void *);
380                                 fmtint (buffer, &currlen, maxlen, (long) strvalue, 16, min, max, flags);
381                                 break;
382                         case 'n':
383                                 if (cflags == DP_C_SHORT) {
384                                         short int *num;
385                                         num = va_arg (args, short int *);
386                                         *num = currlen;
387                                 } else if (cflags == DP_C_LONG) {
388                                         long int *num;
389                                         num = va_arg (args, long int *);
390                                         *num = (long int)currlen;
391                                 } else if (cflags == DP_C_LLONG) {
392                                         LLONG *num;
393                                         num = va_arg (args, LLONG *);
394                                         *num = (LLONG)currlen;
395                                 } else {
396                                         int *num;
397                                         num = va_arg (args, int *);
398                                         *num = currlen;
399                                 }
400                                 break;
401                         case '%':
402                                 dopr_outch (buffer, &currlen, maxlen, ch);
403                                 break;
404                         case 'w':
405                                 /* not supported yet, treat as next char */
406                                 ch = *format++;
407                                 break;
408                         default:
409                                 /* Unknown, skip */
410                                 break;
411                         }
412                         ch = *format++;
413                         state = DP_S_DEFAULT;
414                         flags = cflags = min = 0;
415                         max = -1;
416                         break;
417                 case DP_S_DONE:
418                         break;
419                 default:
420                         /* hmm? */
421                         break; /* some picky compilers need this */
422                 }
423         }
424         if (maxlen != 0) {
425                 if (currlen < maxlen - 1) 
426                         buffer[currlen] = '\0';
427                 else if (maxlen > 0) 
428                         buffer[maxlen - 1] = '\0';
429         }
430         
431         return currlen;
432 }
433
434 static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
435                     char *value, int flags, int min, int max)
436 {
437         int padlen, strln;     /* amount to pad */
438         int cnt = 0;
439
440 #ifdef DEBUG_SNPRINTF
441         printf("fmtstr min=%d max=%d s=[%s]\n", min, max, value);
442 #endif
443         if (value == 0) {
444                 value = "<NULL>";
445         }
446
447         for (strln = 0; strln < max && value[strln]; ++strln); /* strlen */
448         padlen = min - strln;
449         if (padlen < 0) 
450                 padlen = 0;
451         if (flags & DP_F_MINUS) 
452                 padlen = -padlen; /* Left Justify */
453         
454         while ((padlen > 0) && (cnt < max)) {
455                 dopr_outch (buffer, currlen, maxlen, ' ');
456                 --padlen;
457                 ++cnt;
458         }
459         while (*value && (cnt < max)) {
460                 dopr_outch (buffer, currlen, maxlen, *value++);
461                 ++cnt;
462         }
463         while ((padlen < 0) && (cnt < max)) {
464                 dopr_outch (buffer, currlen, maxlen, ' ');
465                 ++padlen;
466                 ++cnt;
467         }
468 }
469
470 /* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
471
472 static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
473                     LLONG value, int base, int min, int max, int flags)
474 {
475         int signvalue = 0;
476         unsigned LLONG uvalue;
477         char convert[20];
478         int place = 0;
479         int spadlen = 0; /* amount to space pad */
480         int zpadlen = 0; /* amount to zero pad */
481         int caps = 0;
482         
483         if (max < 0)
484                 max = 0;
485         
486         uvalue = value;
487         
488         if(!(flags & DP_F_UNSIGNED)) {
489                 if( value < 0 ) {
490                         signvalue = '-';
491                         uvalue = -value;
492                 } else {
493                         if (flags & DP_F_PLUS)  /* Do a sign (+/i) */
494                                 signvalue = '+';
495                         else if (flags & DP_F_SPACE)
496                                 signvalue = ' ';
497                 }
498         }
499   
500         if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
501
502         do {
503                 convert[place++] =
504                         (caps? "0123456789ABCDEF":"0123456789abcdef")
505                         [uvalue % (unsigned)base  ];
506                 uvalue = (uvalue / (unsigned)base );
507         } while(uvalue && (place < 20));
508         if (place == 20) place--;
509         convert[place] = 0;
510
511         zpadlen = max - place;
512         spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
513         if (zpadlen < 0) zpadlen = 0;
514         if (spadlen < 0) spadlen = 0;
515         if (flags & DP_F_ZERO) {
516                 zpadlen = MAX(zpadlen, spadlen);
517                 spadlen = 0;
518         }
519         if (flags & DP_F_MINUS) 
520                 spadlen = -spadlen; /* Left Justifty */
521
522 #ifdef DEBUG_SNPRINTF
523         printf("zpad: %d, spad: %d, min: %d, max: %d, place: %d\n",
524                zpadlen, spadlen, min, max, place);
525 #endif
526
527         /* Spaces */
528         while (spadlen > 0) {
529                 dopr_outch (buffer, currlen, maxlen, ' ');
530                 --spadlen;
531         }
532
533         /* Sign */
534         if (signvalue) 
535                 dopr_outch (buffer, currlen, maxlen, signvalue);
536
537         /* Zeros */
538         if (zpadlen > 0) {
539                 while (zpadlen > 0) {
540                         dopr_outch (buffer, currlen, maxlen, '0');
541                         --zpadlen;
542                 }
543         }
544
545         /* Digits */
546         while (place > 0) 
547                 dopr_outch (buffer, currlen, maxlen, convert[--place]);
548   
549         /* Left Justified spaces */
550         while (spadlen < 0) {
551                 dopr_outch (buffer, currlen, maxlen, ' ');
552                 ++spadlen;
553         }
554 }
555
556 static LDOUBLE abs_val(LDOUBLE value)
557 {
558         LDOUBLE result = value;
559
560         if (value < 0)
561                 result = -value;
562         
563         return result;
564 }
565
566 static LDOUBLE POW10(int exp)
567 {
568         LDOUBLE result = 1;
569         
570         while (exp) {
571                 result *= 10;
572                 exp--;
573         }
574   
575         return result;
576 }
577
578 static LLONG ROUND(LDOUBLE value)
579 {
580         LLONG intpart;
581
582         intpart = (LLONG)value;
583         value = value - intpart;
584         if (value >= 0.5) intpart++;
585         
586         return intpart;
587 }
588
589 /* a replacement for modf that doesn't need the math library. Should
590    be portable, but slow */
591 static double my_modf(double x0, double *iptr)
592 {
593         int i;
594         long l;
595         double x = x0;
596         double f = 1.0;
597
598         for (i=0;i<100;i++) {
599                 l = (long)x;
600                 if (l <= (x+1) && l >= (x-1)) break;
601                 x *= 0.1;
602                 f *= 10.0;
603         }
604
605         if (i == 100) {
606                 /* yikes! the number is beyond what we can handle. What do we do? */
607                 (*iptr) = 0;
608                 return 0;
609         }
610
611         if (i != 0) {
612                 double i2;
613                 double ret;
614
615                 ret = my_modf(x0-l*f, &i2);
616                 (*iptr) = l*f + i2;
617                 return ret;
618         } 
619
620         (*iptr) = l;
621         return x - (*iptr);
622 }
623
624
625 static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
626                    LDOUBLE fvalue, int min, int max, int flags)
627 {
628         int signvalue = 0;
629         double ufvalue;
630         char iconvert[311];
631         char fconvert[311];
632         int iplace = 0;
633         int fplace = 0;
634         int padlen = 0; /* amount to pad */
635         int zpadlen = 0; 
636         int caps = 0;
637         int idx;
638         double intpart;
639         double fracpart;
640         double temp;
641   
642         /* 
643          * AIX manpage says the default is 0, but Solaris says the default
644          * is 6, and sprintf on AIX defaults to 6
645          */
646         if (max < 0)
647                 max = 6;
648
649         ufvalue = abs_val (fvalue);
650
651         if (fvalue < 0) {
652                 signvalue = '-';
653         } else {
654                 if (flags & DP_F_PLUS) { /* Do a sign (+/i) */
655                         signvalue = '+';
656                 } else {
657                         if (flags & DP_F_SPACE)
658                                 signvalue = ' ';
659                 }
660         }
661
662 #if 0
663         if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
664 #endif
665
666 #if 0
667          if (max == 0) ufvalue += 0.5; /* if max = 0 we must round */
668 #endif
669
670         /* 
671          * Sorry, we only support 16 digits past the decimal because of our 
672          * conversion method
673          */
674         if (max > 16)
675                 max = 16;
676
677         /* We "cheat" by converting the fractional part to integer by
678          * multiplying by a factor of 10
679          */
680
681         temp = ufvalue;
682         my_modf(temp, &intpart);
683
684         fracpart = ROUND((POW10(max)) * (ufvalue - intpart));
685         
686         if (fracpart >= POW10(max)) {
687                 intpart++;
688                 fracpart -= POW10(max);
689         }
690
691         /* Convert integer part */
692         do {
693                 temp = intpart*0.1;
694                 my_modf(temp, &intpart);
695                 idx = (int) ((temp -intpart +0.05)* 10.0);
696                 /* idx = (int) (((double)(temp*0.1) -intpart +0.05) *10.0); */
697                 /* printf ("%llf, %f, %x\n", temp, intpart, idx); */
698                 iconvert[iplace++] =
699                         (caps? "0123456789ABCDEF":"0123456789abcdef")[idx];
700         } while (intpart && (iplace < 311));
701         if (iplace == 311) iplace--;
702         iconvert[iplace] = 0;
703
704         /* Convert fractional part */
705         if (fracpart)
706         {
707                 do {
708                         temp = fracpart*0.1;
709                         my_modf(temp, &fracpart);
710                         idx = (int) ((temp -fracpart +0.05)* 10.0);
711                         /* idx = (int) ((((temp/10) -fracpart) +0.05) *10); */
712                         /* printf ("%lf, %lf, %ld\n", temp, fracpart, idx ); */
713                         fconvert[fplace++] =
714                         (caps? "0123456789ABCDEF":"0123456789abcdef")[idx];
715                 } while(fracpart && (fplace < 311));
716                 if (fplace == 311) fplace--;
717         }
718         fconvert[fplace] = 0;
719   
720         /* -1 for decimal point, another -1 if we are printing a sign */
721         padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0); 
722         zpadlen = max - fplace;
723         if (zpadlen < 0) zpadlen = 0;
724         if (padlen < 0) 
725                 padlen = 0;
726         if (flags & DP_F_MINUS) 
727                 padlen = -padlen; /* Left Justifty */
728         
729         if ((flags & DP_F_ZERO) && (padlen > 0)) {
730                 if (signvalue) {
731                         dopr_outch (buffer, currlen, maxlen, signvalue);
732                         --padlen;
733                         signvalue = 0;
734                 }
735                 while (padlen > 0) {
736                         dopr_outch (buffer, currlen, maxlen, '0');
737                         --padlen;
738                 }
739         }
740         while (padlen > 0) {
741                 dopr_outch (buffer, currlen, maxlen, ' ');
742                 --padlen;
743         }
744         if (signvalue) 
745                 dopr_outch (buffer, currlen, maxlen, signvalue);
746         
747         while (iplace > 0) 
748                 dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]);
749
750 #ifdef DEBUG_SNPRINTF
751         printf("fmtfp: fplace=%d zpadlen=%d\n", fplace, zpadlen);
752 #endif
753
754         /*
755          * Decimal point.  This should probably use locale to find the correct
756          * char to print out.
757          */
758         if (max > 0) {
759                 dopr_outch (buffer, currlen, maxlen, '.');
760                 
761                 while (zpadlen > 0) {
762                         dopr_outch (buffer, currlen, maxlen, '0');
763                         --zpadlen;
764                 }
765
766                 while (fplace > 0) 
767                         dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]);
768         }
769
770         while (padlen < 0) {
771                 dopr_outch (buffer, currlen, maxlen, ' ');
772                 ++padlen;
773         }
774 }
775
776 static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c)
777 {
778         if (*currlen < maxlen) {
779                 buffer[(*currlen)] = c;
780         }
781         (*currlen)++;
782 }
783 #endif /* !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) */
784
785 #if !defined(HAVE_VSNPRINTF)
786 int vsnprintf (char *str, size_t count, const char *fmt, va_list args)
787 {
788         return dopr(str, count, fmt, args);
789 }
790 #endif
791
792 #if !defined(HAVE_SNPRINTF)
793 int snprintf(char *str, size_t count, SNPRINTF_CONST char *fmt, ...)
794 {
795         size_t ret;
796         va_list ap;
797
798         va_start(ap, fmt);
799         ret = vsnprintf(str, count, fmt, ap);
800         va_end(ap);
801         return ret;
802 }
803 #endif
804
This page took 0.723861 seconds and 5 git commands to generate.