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