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