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