1f18462f5SDamien Miller /*
2f18462f5SDamien Miller  * Copyright Patrick Powell 1995
3f18462f5SDamien Miller  * This code is based on code written by Patrick Powell (papowell@astart.com)
4f18462f5SDamien Miller  * It may be used for any purpose as long as this notice remains intact
5f18462f5SDamien Miller  * on all source code distributions
6f18462f5SDamien Miller  */
7f18462f5SDamien Miller 
83c06f6a0SBen Lindstrom /**************************************************************
93c06f6a0SBen Lindstrom  * Original:
103c06f6a0SBen Lindstrom  * Patrick Powell Tue Apr 11 09:48:21 PDT 1995
113c06f6a0SBen Lindstrom  * A bombproof version of doprnt (dopr) included.
123c06f6a0SBen Lindstrom  * Sigh.  This sort of thing is always nasty do deal with.  Note that
133c06f6a0SBen Lindstrom  * the version here does not include floating point...
143c06f6a0SBen Lindstrom  *
153c06f6a0SBen Lindstrom  * snprintf() is used instead of sprintf() as it does limit checks
163c06f6a0SBen Lindstrom  * for string length.  This covers a nasty loophole.
173c06f6a0SBen Lindstrom  *
183c06f6a0SBen Lindstrom  * The other functions are there to prevent NULL pointers from
193c06f6a0SBen Lindstrom  * causing nast effects.
203c06f6a0SBen Lindstrom  *
213c06f6a0SBen Lindstrom  * More Recently:
223c06f6a0SBen Lindstrom  *  Brandon Long <blong@fiction.net> 9/15/96 for mutt 0.43
233c06f6a0SBen Lindstrom  *  This was ugly.  It is still ugly.  I opted out of floating point
243c06f6a0SBen Lindstrom  *  numbers, but the formatter understands just about everything
253c06f6a0SBen Lindstrom  *  from the normal C string format, at least as far as I can tell from
263c06f6a0SBen Lindstrom  *  the Solaris 2.5 printf(3S) man page.
273c06f6a0SBen Lindstrom  *
283c06f6a0SBen Lindstrom  *  Brandon Long <blong@fiction.net> 10/22/97 for mutt 0.87.1
293c06f6a0SBen Lindstrom  *    Ok, added some minimal floating point support, which means this
303c06f6a0SBen Lindstrom  *    probably requires libm on most operating systems.  Don't yet
313c06f6a0SBen Lindstrom  *    support the exponent (e,E) and sigfig (g,G).  Also, fmtint()
323c06f6a0SBen Lindstrom  *    was pretty badly broken, it just wasn't being exercised in ways
3310479cc2SDamien Miller  *    which showed it, so that's been fixed.  Also, formatted the code
343c06f6a0SBen Lindstrom  *    to mutt conventions, and removed dead code left over from the
353c06f6a0SBen Lindstrom  *    original.  Also, there is now a builtin-test, just compile with:
363c06f6a0SBen Lindstrom  *           gcc -DTEST_SNPRINTF -o snprintf snprintf.c -lm
373c06f6a0SBen Lindstrom  *    and run snprintf for results.
383c06f6a0SBen Lindstrom  *
393c06f6a0SBen Lindstrom  *  Thomas Roessler <roessler@guug.de> 01/27/98 for mutt 0.89i
403c06f6a0SBen Lindstrom  *    The PGP code was using unsigned hexadecimal formats.
413c06f6a0SBen Lindstrom  *    Unfortunately, unsigned formats simply didn't work.
423c06f6a0SBen Lindstrom  *
433c06f6a0SBen Lindstrom  *  Michael Elkins <me@cs.hmc.edu> 03/05/98 for mutt 0.90.8
443c06f6a0SBen Lindstrom  *    The original code assumed that both snprintf() and vsnprintf() were
453c06f6a0SBen Lindstrom  *    missing.  Some systems only have snprintf() but not vsnprintf(), so
463c06f6a0SBen Lindstrom  *    the code is now broken down under HAVE_SNPRINTF and HAVE_VSNPRINTF.
473c06f6a0SBen Lindstrom  *
4857f3915bSDamien Miller  *  Andrew Tridgell (tridge@samba.org) Oct 1998
4957f3915bSDamien Miller  *    fixed handling of %.0f
5057f3915bSDamien Miller  *    added test for HAVE_LONG_DOUBLE
516c92dab9SBen Lindstrom  *
5257f3915bSDamien Miller  * tridge@samba.org, idra@samba.org, April 2001
5357f3915bSDamien Miller  *    got rid of fcvt code (twas buggy and made testing harder)
5457f3915bSDamien Miller  *    added C99 semantics
5557f3915bSDamien Miller  *
5657f3915bSDamien Miller  * date: 2002/12/19 19:56:31;  author: herb;  state: Exp;  lines: +2 -0
5757f3915bSDamien Miller  * actually print args for %g and %e
5857f3915bSDamien Miller  *
5957f3915bSDamien Miller  * date: 2002/06/03 13:37:52;  author: jmcd;  state: Exp;  lines: +8 -0
6057f3915bSDamien Miller  * Since includes.h isn't included here, VA_COPY has to be defined here.  I don't
6157f3915bSDamien Miller  * see any include file that is guaranteed to be here, so I'm defining it
6257f3915bSDamien Miller  * locally.  Fixes AIX and Solaris builds.
6357f3915bSDamien Miller  *
6457f3915bSDamien Miller  * date: 2002/06/03 03:07:24;  author: tridge;  state: Exp;  lines: +5 -13
6557f3915bSDamien Miller  * put the ifdef for HAVE_VA_COPY in one place rather than in lots of
6657f3915bSDamien Miller  * functions
6757f3915bSDamien Miller  *
6857f3915bSDamien Miller  * date: 2002/05/17 14:51:22;  author: jmcd;  state: Exp;  lines: +21 -4
6957f3915bSDamien Miller  * Fix usage of va_list passed as an arg.  Use __va_copy before using it
7057f3915bSDamien Miller  * when it exists.
7157f3915bSDamien Miller  *
7257f3915bSDamien Miller  * date: 2002/04/16 22:38:04;  author: idra;  state: Exp;  lines: +20 -14
7357f3915bSDamien Miller  * Fix incorrect zpadlen handling in fmtfp.
7457f3915bSDamien Miller  * Thanks to Ollie Oldham <ollie.oldham@metro-optix.com> for spotting it.
7557f3915bSDamien Miller  * few mods to make it easier to compile the tests.
76*397f217eSDamien Miller  * added the "Ollie" test to the floating point ones.
7757f3915bSDamien Miller  *
7857f3915bSDamien Miller  * Martin Pool (mbp@samba.org) April 2003
7957f3915bSDamien Miller  *    Remove NO_CONFIG_H so that the test case can be built within a source
8057f3915bSDamien Miller  *    tree with less trouble.
8157f3915bSDamien Miller  *    Remove unnecessary SAFE_FREE() definition.
8257f3915bSDamien Miller  *
8357f3915bSDamien Miller  * Martin Pool (mbp@samba.org) May 2003
8457f3915bSDamien Miller  *    Put in a prototype for dummy_snprintf() to quiet compiler warnings.
8557f3915bSDamien Miller  *
8657f3915bSDamien Miller  *    Move #endif to make sure VA_COPY, LDOUBLE, etc are defined even
8757f3915bSDamien Miller  *    if the C library has some snprintf functions already.
88742cc1c1SDamien Miller  *
89742cc1c1SDamien Miller  * Damien Miller (djm@mindrot.org) Jan 2007
90742cc1c1SDamien Miller  *    Fix integer overflows in return value.
91742cc1c1SDamien Miller  *    Make formatting quite a bit faster by inlining dopr_outch()
92742cc1c1SDamien Miller  *
933c06f6a0SBen Lindstrom  **************************************************************/
943c06f6a0SBen Lindstrom 
95e9cf357aSDamien Miller #include "includes.h"
96e9cf357aSDamien Miller 
9763941f96SBen Lindstrom #if defined(BROKEN_SNPRINTF)		/* For those with broken snprintf() */
9863941f96SBen Lindstrom # undef HAVE_SNPRINTF
9963941f96SBen Lindstrom # undef HAVE_VSNPRINTF
10063941f96SBen Lindstrom #endif
1013c06f6a0SBen Lindstrom 
1023c06f6a0SBen Lindstrom #if !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF)
1033c06f6a0SBen Lindstrom 
104f78fb544SDarren Tucker #include <ctype.h>
10508432d54SDarren Tucker #include <stdarg.h>
106f78fb544SDarren Tucker #include <stdlib.h>
10762da44f0SDamien Miller #include <string.h>
108742cc1c1SDamien Miller #include <limits.h>
109742cc1c1SDamien Miller #include <errno.h>
11062da44f0SDamien Miller 
11157f3915bSDamien Miller #ifdef HAVE_LONG_DOUBLE
11257f3915bSDamien Miller # define LDOUBLE long double
11357f3915bSDamien Miller #else
11457f3915bSDamien Miller # define LDOUBLE double
11557f3915bSDamien Miller #endif
1163c06f6a0SBen Lindstrom 
11757f3915bSDamien Miller #ifdef HAVE_LONG_LONG
11857f3915bSDamien Miller # define LLONG long long
11957f3915bSDamien Miller #else
12057f3915bSDamien Miller # define LLONG long
12157f3915bSDamien Miller #endif
122116b6bddSBen Lindstrom 
1233c06f6a0SBen Lindstrom /*
1243c06f6a0SBen Lindstrom  * dopr(): poor man's version of doprintf
1253c06f6a0SBen Lindstrom  */
1263c06f6a0SBen Lindstrom 
1273c06f6a0SBen Lindstrom /* format read states */
1283c06f6a0SBen Lindstrom #define DP_S_DEFAULT 0
1293c06f6a0SBen Lindstrom #define DP_S_FLAGS   1
1303c06f6a0SBen Lindstrom #define DP_S_MIN     2
1313c06f6a0SBen Lindstrom #define DP_S_DOT     3
1323c06f6a0SBen Lindstrom #define DP_S_MAX     4
1333c06f6a0SBen Lindstrom #define DP_S_MOD     5
1343c06f6a0SBen Lindstrom #define DP_S_CONV    6
1353c06f6a0SBen Lindstrom #define DP_S_DONE    7
1363c06f6a0SBen Lindstrom 
1373c06f6a0SBen Lindstrom /* format flags - Bits */
1383c06f6a0SBen Lindstrom #define DP_F_MINUS	(1 << 0)
1393c06f6a0SBen Lindstrom #define DP_F_PLUS	(1 << 1)
1403c06f6a0SBen Lindstrom #define DP_F_SPACE	(1 << 2)
1413c06f6a0SBen Lindstrom #define DP_F_NUM	(1 << 3)
1423c06f6a0SBen Lindstrom #define DP_F_ZERO	(1 << 4)
1433c06f6a0SBen Lindstrom #define DP_F_UP		(1 << 5)
1443c06f6a0SBen Lindstrom #define DP_F_UNSIGNED	(1 << 6)
1453c06f6a0SBen Lindstrom 
1463c06f6a0SBen Lindstrom /* Conversion Flags */
1473c06f6a0SBen Lindstrom #define DP_C_SHORT   1
1483c06f6a0SBen Lindstrom #define DP_C_LONG    2
1493c06f6a0SBen Lindstrom #define DP_C_LDOUBLE 3
15057f3915bSDamien Miller #define DP_C_LLONG   4
15104be8b9eSDamien Miller #define DP_C_SIZE    5
15204be8b9eSDamien Miller #define DP_C_INTMAX  6
1533c06f6a0SBen Lindstrom 
15457f3915bSDamien Miller #define char_to_int(p) ((p)- '0')
15557f3915bSDamien Miller #ifndef MAX
15657f3915bSDamien Miller # define MAX(p,q) (((p) >= (q)) ? (p) : (q))
15757f3915bSDamien Miller #endif
1583c06f6a0SBen Lindstrom 
159742cc1c1SDamien Miller #define DOPR_OUTCH(buf, pos, buflen, thechar) \
160742cc1c1SDamien Miller 	do { \
16107877ca6SDarren Tucker 		if (pos + 1 >= INT_MAX) { \
162742cc1c1SDamien Miller 			errno = ERANGE; \
163742cc1c1SDamien Miller 			return -1; \
16407877ca6SDarren Tucker 		} \
165742cc1c1SDamien Miller 		if (pos < buflen) \
166742cc1c1SDamien Miller 			buf[pos] = thechar; \
16707877ca6SDarren Tucker 		(pos)++; \
168742cc1c1SDamien Miller 	} while (0)
1696c92dab9SBen Lindstrom 
170742cc1c1SDamien Miller static int dopr(char *buffer, size_t maxlen, const char *format,
171742cc1c1SDamien Miller     va_list args_in);
172742cc1c1SDamien Miller static int fmtstr(char *buffer, size_t *currlen, size_t maxlen,
173742cc1c1SDamien Miller     char *value, int flags, int min, int max);
174742cc1c1SDamien Miller static int fmtint(char *buffer, size_t *currlen, size_t maxlen,
17504be8b9eSDamien Miller     intmax_t value, int base, int min, int max, int flags);
176742cc1c1SDamien Miller static int fmtfp(char *buffer, size_t *currlen, size_t maxlen,
177742cc1c1SDamien Miller     LDOUBLE fvalue, int min, int max, int flags);
178742cc1c1SDamien Miller 
179742cc1c1SDamien Miller static int
dopr(char * buffer,size_t maxlen,const char * format,va_list args_in)180742cc1c1SDamien Miller dopr(char *buffer, size_t maxlen, const char *format, va_list args_in)
1813c06f6a0SBen Lindstrom {
18257f3915bSDamien Miller 	char ch;
18304be8b9eSDamien Miller 	intmax_t value;
18457f3915bSDamien Miller 	LDOUBLE fvalue;
18557f3915bSDamien Miller 	char *strvalue;
18657f3915bSDamien Miller 	int min;
18757f3915bSDamien Miller 	int max;
18857f3915bSDamien Miller 	int state;
18957f3915bSDamien Miller 	int flags;
19057f3915bSDamien Miller 	int cflags;
19157f3915bSDamien Miller 	size_t currlen;
19257f3915bSDamien Miller 	va_list args;
1933c06f6a0SBen Lindstrom 
19457f3915bSDamien Miller 	VA_COPY(args, args_in);
19557f3915bSDamien Miller 
19657f3915bSDamien Miller 	state = DP_S_DEFAULT;
19757f3915bSDamien Miller 	currlen = flags = cflags = min = 0;
19857f3915bSDamien Miller 	max = -1;
1993c06f6a0SBen Lindstrom 	ch = *format++;
2003c06f6a0SBen Lindstrom 
2016c92dab9SBen Lindstrom 	while (state != DP_S_DONE) {
20257f3915bSDamien Miller 		if (ch == '\0')
2033c06f6a0SBen Lindstrom 			state = DP_S_DONE;
2043c06f6a0SBen Lindstrom 
2056c92dab9SBen Lindstrom 		switch(state) {
2063c06f6a0SBen Lindstrom 		case DP_S_DEFAULT:
2073c06f6a0SBen Lindstrom 			if (ch == '%')
2083c06f6a0SBen Lindstrom 				state = DP_S_FLAGS;
2093c06f6a0SBen Lindstrom 			else
210742cc1c1SDamien Miller 				DOPR_OUTCH(buffer, currlen, maxlen, ch);
2113c06f6a0SBen Lindstrom 			ch = *format++;
2123c06f6a0SBen Lindstrom 			break;
2133c06f6a0SBen Lindstrom 		case DP_S_FLAGS:
2146c92dab9SBen Lindstrom 			switch (ch) {
2153c06f6a0SBen Lindstrom 			case '-':
2163c06f6a0SBen Lindstrom 				flags |= DP_F_MINUS;
2173c06f6a0SBen Lindstrom 				ch = *format++;
2183c06f6a0SBen Lindstrom 				break;
2193c06f6a0SBen Lindstrom 			case '+':
2203c06f6a0SBen Lindstrom 				flags |= DP_F_PLUS;
2213c06f6a0SBen Lindstrom 				ch = *format++;
2223c06f6a0SBen Lindstrom 				break;
2233c06f6a0SBen Lindstrom 			case ' ':
2243c06f6a0SBen Lindstrom 				flags |= DP_F_SPACE;
2253c06f6a0SBen Lindstrom 				ch = *format++;
2263c06f6a0SBen Lindstrom 				break;
2273c06f6a0SBen Lindstrom 			case '#':
2283c06f6a0SBen Lindstrom 				flags |= DP_F_NUM;
2293c06f6a0SBen Lindstrom 				ch = *format++;
2303c06f6a0SBen Lindstrom 				break;
2313c06f6a0SBen Lindstrom 			case '0':
2323c06f6a0SBen Lindstrom 				flags |= DP_F_ZERO;
2333c06f6a0SBen Lindstrom 				ch = *format++;
2343c06f6a0SBen Lindstrom 				break;
2353c06f6a0SBen Lindstrom 			default:
2363c06f6a0SBen Lindstrom 				state = DP_S_MIN;
2373c06f6a0SBen Lindstrom 				break;
2383c06f6a0SBen Lindstrom 			}
2393c06f6a0SBen Lindstrom 			break;
2403c06f6a0SBen Lindstrom 		case DP_S_MIN:
2416c92dab9SBen Lindstrom 			if (isdigit((unsigned char)ch)) {
2423c06f6a0SBen Lindstrom 				min = 10*min + char_to_int (ch);
2433c06f6a0SBen Lindstrom 				ch = *format++;
2446c92dab9SBen Lindstrom 			} else if (ch == '*') {
2453c06f6a0SBen Lindstrom 				min = va_arg (args, int);
2463c06f6a0SBen Lindstrom 				ch = *format++;
2473c06f6a0SBen Lindstrom 				state = DP_S_DOT;
24857f3915bSDamien Miller 			} else {
2493c06f6a0SBen Lindstrom 				state = DP_S_DOT;
25057f3915bSDamien Miller 			}
2513c06f6a0SBen Lindstrom 			break;
2523c06f6a0SBen Lindstrom 		case DP_S_DOT:
2536c92dab9SBen Lindstrom 			if (ch == '.') {
2543c06f6a0SBen Lindstrom 				state = DP_S_MAX;
2553c06f6a0SBen Lindstrom 				ch = *format++;
25657f3915bSDamien Miller 			} else {
2573c06f6a0SBen Lindstrom 				state = DP_S_MOD;
25857f3915bSDamien Miller 			}
2593c06f6a0SBen Lindstrom 			break;
2603c06f6a0SBen Lindstrom 		case DP_S_MAX:
2616c92dab9SBen Lindstrom 			if (isdigit((unsigned char)ch)) {
2623c06f6a0SBen Lindstrom 				if (max < 0)
2633c06f6a0SBen Lindstrom 					max = 0;
2643c06f6a0SBen Lindstrom 				max = 10*max + char_to_int (ch);
2653c06f6a0SBen Lindstrom 				ch = *format++;
2666c92dab9SBen Lindstrom 			} else if (ch == '*') {
2673c06f6a0SBen Lindstrom 				max = va_arg (args, int);
2683c06f6a0SBen Lindstrom 				ch = *format++;
2693c06f6a0SBen Lindstrom 				state = DP_S_MOD;
27057f3915bSDamien Miller 			} else {
2713c06f6a0SBen Lindstrom 				state = DP_S_MOD;
27257f3915bSDamien Miller 			}
2733c06f6a0SBen Lindstrom 			break;
2743c06f6a0SBen Lindstrom 		case DP_S_MOD:
2756c92dab9SBen Lindstrom 			switch (ch) {
2763c06f6a0SBen Lindstrom 			case 'h':
2773c06f6a0SBen Lindstrom 				cflags = DP_C_SHORT;
2783c06f6a0SBen Lindstrom 				ch = *format++;
2793c06f6a0SBen Lindstrom 				break;
28004be8b9eSDamien Miller 			case 'j':
28104be8b9eSDamien Miller 				cflags = DP_C_INTMAX;
28204be8b9eSDamien Miller 				ch = *format++;
28304be8b9eSDamien Miller 				break;
2843c06f6a0SBen Lindstrom 			case 'l':
2853c06f6a0SBen Lindstrom 				cflags = DP_C_LONG;
2863c06f6a0SBen Lindstrom 				ch = *format++;
28757f3915bSDamien Miller 				if (ch == 'l') {	/* It's a long long */
28857f3915bSDamien Miller 					cflags = DP_C_LLONG;
2893c06f6a0SBen Lindstrom 					ch = *format++;
2903c06f6a0SBen Lindstrom 				}
2913c06f6a0SBen Lindstrom 				break;
2923c06f6a0SBen Lindstrom 			case 'L':
2933c06f6a0SBen Lindstrom 				cflags = DP_C_LDOUBLE;
2943c06f6a0SBen Lindstrom 				ch = *format++;
2953c06f6a0SBen Lindstrom 				break;
29604be8b9eSDamien Miller 			case 'z':
29704be8b9eSDamien Miller 				cflags = DP_C_SIZE;
29804be8b9eSDamien Miller 				ch = *format++;
29904be8b9eSDamien Miller 				break;
3003c06f6a0SBen Lindstrom 			default:
3013c06f6a0SBen Lindstrom 				break;
3023c06f6a0SBen Lindstrom 			}
3033c06f6a0SBen Lindstrom 			state = DP_S_CONV;
3043c06f6a0SBen Lindstrom 			break;
3053c06f6a0SBen Lindstrom 		case DP_S_CONV:
3066c92dab9SBen Lindstrom 			switch (ch) {
3073c06f6a0SBen Lindstrom 			case 'd':
3083c06f6a0SBen Lindstrom 			case 'i':
3093c06f6a0SBen Lindstrom 				if (cflags == DP_C_SHORT)
3103c06f6a0SBen Lindstrom 					value = va_arg (args, int);
3113c06f6a0SBen Lindstrom 				else if (cflags == DP_C_LONG)
3123c06f6a0SBen Lindstrom 					value = va_arg (args, long int);
31357f3915bSDamien Miller 				else if (cflags == DP_C_LLONG)
31457f3915bSDamien Miller 					value = va_arg (args, LLONG);
31504be8b9eSDamien Miller 				else if (cflags == DP_C_SIZE)
31604be8b9eSDamien Miller 					value = va_arg (args, ssize_t);
31704be8b9eSDamien Miller 				else if (cflags == DP_C_INTMAX)
31804be8b9eSDamien Miller 					value = va_arg (args, intmax_t);
3193c06f6a0SBen Lindstrom 				else
3203c06f6a0SBen Lindstrom 					value = va_arg (args, int);
321742cc1c1SDamien Miller 				if (fmtint(buffer, &currlen, maxlen,
322742cc1c1SDamien Miller 				    value, 10, min, max, flags) == -1)
323742cc1c1SDamien Miller 					return -1;
3243c06f6a0SBen Lindstrom 				break;
3253c06f6a0SBen Lindstrom 			case 'o':
3263c06f6a0SBen Lindstrom 				flags |= DP_F_UNSIGNED;
3273c06f6a0SBen Lindstrom 				if (cflags == DP_C_SHORT)
3283c06f6a0SBen Lindstrom 					value = va_arg (args, unsigned int);
3293c06f6a0SBen Lindstrom 				else if (cflags == DP_C_LONG)
33057f3915bSDamien Miller 					value = (long)va_arg (args, unsigned long int);
33157f3915bSDamien Miller 				else if (cflags == DP_C_LLONG)
33257f3915bSDamien Miller 					value = (long)va_arg (args, unsigned LLONG);
33304be8b9eSDamien Miller 				else if (cflags == DP_C_SIZE)
33404be8b9eSDamien Miller 					value = va_arg (args, size_t);
33543968a8eSDamien Miller #ifdef notyet
33604be8b9eSDamien Miller 				else if (cflags == DP_C_INTMAX)
33704be8b9eSDamien Miller 					value = va_arg (args, uintmax_t);
33843968a8eSDamien Miller #endif
3393c06f6a0SBen Lindstrom 				else
34057f3915bSDamien Miller 					value = (long)va_arg (args, unsigned int);
341742cc1c1SDamien Miller 				if (fmtint(buffer, &currlen, maxlen, value,
342742cc1c1SDamien Miller 				    8, min, max, flags) == -1)
343742cc1c1SDamien Miller 					return -1;
3443c06f6a0SBen Lindstrom 				break;
3453c06f6a0SBen Lindstrom 			case 'u':
3463c06f6a0SBen Lindstrom 				flags |= DP_F_UNSIGNED;
3473c06f6a0SBen Lindstrom 				if (cflags == DP_C_SHORT)
3483c06f6a0SBen Lindstrom 					value = va_arg (args, unsigned int);
3493c06f6a0SBen Lindstrom 				else if (cflags == DP_C_LONG)
35057f3915bSDamien Miller 					value = (long)va_arg (args, unsigned long int);
35157f3915bSDamien Miller 				else if (cflags == DP_C_LLONG)
35257f3915bSDamien Miller 					value = (LLONG)va_arg (args, unsigned LLONG);
35304be8b9eSDamien Miller 				else if (cflags == DP_C_SIZE)
35404be8b9eSDamien Miller 					value = va_arg (args, size_t);
35543968a8eSDamien Miller #ifdef notyet
35604be8b9eSDamien Miller 				else if (cflags == DP_C_INTMAX)
35704be8b9eSDamien Miller 					value = va_arg (args, uintmax_t);
35843968a8eSDamien Miller #endif
3593c06f6a0SBen Lindstrom 				else
36057f3915bSDamien Miller 					value = (long)va_arg (args, unsigned int);
361742cc1c1SDamien Miller 				if (fmtint(buffer, &currlen, maxlen, value,
362742cc1c1SDamien Miller 				    10, min, max, flags) == -1)
363742cc1c1SDamien Miller 					return -1;
3643c06f6a0SBen Lindstrom 				break;
3653c06f6a0SBen Lindstrom 			case 'X':
3663c06f6a0SBen Lindstrom 				flags |= DP_F_UP;
3673c06f6a0SBen Lindstrom 			case 'x':
3683c06f6a0SBen Lindstrom 				flags |= DP_F_UNSIGNED;
3693c06f6a0SBen Lindstrom 				if (cflags == DP_C_SHORT)
3703c06f6a0SBen Lindstrom 					value = va_arg (args, unsigned int);
3713c06f6a0SBen Lindstrom 				else if (cflags == DP_C_LONG)
37257f3915bSDamien Miller 					value = (long)va_arg (args, unsigned long int);
37357f3915bSDamien Miller 				else if (cflags == DP_C_LLONG)
37457f3915bSDamien Miller 					value = (LLONG)va_arg (args, unsigned LLONG);
37504be8b9eSDamien Miller 				else if (cflags == DP_C_SIZE)
37604be8b9eSDamien Miller 					value = va_arg (args, size_t);
37743968a8eSDamien Miller #ifdef notyet
37804be8b9eSDamien Miller 				else if (cflags == DP_C_INTMAX)
37904be8b9eSDamien Miller 					value = va_arg (args, uintmax_t);
38043968a8eSDamien Miller #endif
3813c06f6a0SBen Lindstrom 				else
38257f3915bSDamien Miller 					value = (long)va_arg (args, unsigned int);
383742cc1c1SDamien Miller 				if (fmtint(buffer, &currlen, maxlen, value,
384742cc1c1SDamien Miller 				    16, min, max, flags) == -1)
385742cc1c1SDamien Miller 					return -1;
3863c06f6a0SBen Lindstrom 				break;
3873c06f6a0SBen Lindstrom 			case 'f':
3883c06f6a0SBen Lindstrom 				if (cflags == DP_C_LDOUBLE)
38957f3915bSDamien Miller 					fvalue = va_arg (args, LDOUBLE);
3903c06f6a0SBen Lindstrom 				else
3913c06f6a0SBen Lindstrom 					fvalue = va_arg (args, double);
392742cc1c1SDamien Miller 				if (fmtfp(buffer, &currlen, maxlen, fvalue,
393742cc1c1SDamien Miller 				    min, max, flags) == -1)
394742cc1c1SDamien Miller 					return -1;
3953c06f6a0SBen Lindstrom 				break;
3963c06f6a0SBen Lindstrom 			case 'E':
3973c06f6a0SBen Lindstrom 				flags |= DP_F_UP;
3983c06f6a0SBen Lindstrom 			case 'e':
3993c06f6a0SBen Lindstrom 				if (cflags == DP_C_LDOUBLE)
40057f3915bSDamien Miller 					fvalue = va_arg (args, LDOUBLE);
4013c06f6a0SBen Lindstrom 				else
4023c06f6a0SBen Lindstrom 					fvalue = va_arg (args, double);
403742cc1c1SDamien Miller 				if (fmtfp(buffer, &currlen, maxlen, fvalue,
404742cc1c1SDamien Miller 				    min, max, flags) == -1)
405742cc1c1SDamien Miller 					return -1;
4063c06f6a0SBen Lindstrom 				break;
4073c06f6a0SBen Lindstrom 			case 'G':
4083c06f6a0SBen Lindstrom 				flags |= DP_F_UP;
4093c06f6a0SBen Lindstrom 			case 'g':
4103c06f6a0SBen Lindstrom 				if (cflags == DP_C_LDOUBLE)
41157f3915bSDamien Miller 					fvalue = va_arg (args, LDOUBLE);
4123c06f6a0SBen Lindstrom 				else
4133c06f6a0SBen Lindstrom 					fvalue = va_arg (args, double);
414742cc1c1SDamien Miller 				if (fmtfp(buffer, &currlen, maxlen, fvalue,
415742cc1c1SDamien Miller 				    min, max, flags) == -1)
416742cc1c1SDamien Miller 					return -1;
4173c06f6a0SBen Lindstrom 				break;
4183c06f6a0SBen Lindstrom 			case 'c':
419742cc1c1SDamien Miller 				DOPR_OUTCH(buffer, currlen, maxlen,
420742cc1c1SDamien Miller 				    va_arg (args, int));
4213c06f6a0SBen Lindstrom 				break;
4223c06f6a0SBen Lindstrom 			case 's':
4233c06f6a0SBen Lindstrom 				strvalue = va_arg (args, char *);
42457f3915bSDamien Miller 				if (!strvalue) strvalue = "(NULL)";
42557f3915bSDamien Miller 				if (max == -1) {
42657f3915bSDamien Miller 					max = strlen(strvalue);
42757f3915bSDamien Miller 				}
42857f3915bSDamien Miller 				if (min > 0 && max >= 0 && min > max) max = min;
429742cc1c1SDamien Miller 				if (fmtstr(buffer, &currlen, maxlen,
430742cc1c1SDamien Miller 				    strvalue, flags, min, max) == -1)
431742cc1c1SDamien Miller 					return -1;
4323c06f6a0SBen Lindstrom 				break;
4333c06f6a0SBen Lindstrom 			case 'p':
4343c06f6a0SBen Lindstrom 				strvalue = va_arg (args, void *);
435742cc1c1SDamien Miller 				if (fmtint(buffer, &currlen, maxlen,
436742cc1c1SDamien Miller 				    (long) strvalue, 16, min, max, flags) == -1)
437742cc1c1SDamien Miller 					return -1;
4383c06f6a0SBen Lindstrom 				break;
43904be8b9eSDamien Miller #if we_dont_want_this_in_openssh
4403c06f6a0SBen Lindstrom 			case 'n':
4416c92dab9SBen Lindstrom 				if (cflags == DP_C_SHORT) {
4423c06f6a0SBen Lindstrom 					short int *num;
4433c06f6a0SBen Lindstrom 					num = va_arg (args, short int *);
4443c06f6a0SBen Lindstrom 					*num = currlen;
4456c92dab9SBen Lindstrom 				} else if (cflags == DP_C_LONG) {
4463c06f6a0SBen Lindstrom 					long int *num;
4473c06f6a0SBen Lindstrom 					num = va_arg (args, long int *);
44857f3915bSDamien Miller 					*num = (long int)currlen;
44957f3915bSDamien Miller 				} else if (cflags == DP_C_LLONG) {
45057f3915bSDamien Miller 					LLONG *num;
45157f3915bSDamien Miller 					num = va_arg (args, LLONG *);
45257f3915bSDamien Miller 					*num = (LLONG)currlen;
45304be8b9eSDamien Miller 				} else if (cflags == DP_C_SIZE) {
45404be8b9eSDamien Miller 					ssize_t *num;
45504be8b9eSDamien Miller 					num = va_arg (args, ssize_t *);
45604be8b9eSDamien Miller 					*num = (ssize_t)currlen;
45704be8b9eSDamien Miller 				} else if (cflags == DP_C_INTMAX) {
45804be8b9eSDamien Miller 					intmax_t *num;
45904be8b9eSDamien Miller 					num = va_arg (args, intmax_t *);
46004be8b9eSDamien Miller 					*num = (intmax_t)currlen;
4616c92dab9SBen Lindstrom 				} else {
4623c06f6a0SBen Lindstrom 					int *num;
4633c06f6a0SBen Lindstrom 					num = va_arg (args, int *);
4643c06f6a0SBen Lindstrom 					*num = currlen;
4653c06f6a0SBen Lindstrom 				}
4663c06f6a0SBen Lindstrom 				break;
46704be8b9eSDamien Miller #endif
4683c06f6a0SBen Lindstrom 			case '%':
469742cc1c1SDamien Miller 				DOPR_OUTCH(buffer, currlen, maxlen, ch);
4703c06f6a0SBen Lindstrom 				break;
47157f3915bSDamien Miller 			case 'w':
47257f3915bSDamien Miller 				/* not supported yet, treat as next char */
4733c06f6a0SBen Lindstrom 				ch = *format++;
4743c06f6a0SBen Lindstrom 				break;
47557f3915bSDamien Miller 			default:
47657f3915bSDamien Miller 				/* Unknown, skip */
4773c06f6a0SBen Lindstrom 				break;
4783c06f6a0SBen Lindstrom 			}
4793c06f6a0SBen Lindstrom 			ch = *format++;
4803c06f6a0SBen Lindstrom 			state = DP_S_DEFAULT;
4813c06f6a0SBen Lindstrom 			flags = cflags = min = 0;
4823c06f6a0SBen Lindstrom 			max = -1;
4833c06f6a0SBen Lindstrom 			break;
4843c06f6a0SBen Lindstrom 		case DP_S_DONE:
4853c06f6a0SBen Lindstrom 			break;
48657f3915bSDamien Miller 		default:
48757f3915bSDamien Miller 			/* hmm? */
4883c06f6a0SBen Lindstrom 			break; /* some picky compilers need this */
4893c06f6a0SBen Lindstrom 		}
4903c06f6a0SBen Lindstrom 	}
49157f3915bSDamien Miller 	if (maxlen != 0) {
4923c06f6a0SBen Lindstrom 		if (currlen < maxlen - 1)
4933c06f6a0SBen Lindstrom 			buffer[currlen] = '\0';
49457f3915bSDamien Miller 		else if (maxlen > 0)
4953c06f6a0SBen Lindstrom 			buffer[maxlen - 1] = '\0';
4963c06f6a0SBen Lindstrom 	}
4973c06f6a0SBen Lindstrom 
498742cc1c1SDamien Miller 	return currlen < INT_MAX ? (int)currlen : -1;
49957f3915bSDamien Miller }
50057f3915bSDamien Miller 
501742cc1c1SDamien Miller static int
fmtstr(char * buffer,size_t * currlen,size_t maxlen,char * value,int flags,int min,int max)502742cc1c1SDamien Miller fmtstr(char *buffer, size_t *currlen, size_t maxlen,
5033c06f6a0SBen Lindstrom     char *value, int flags, int min, int max)
5043c06f6a0SBen Lindstrom {
50557f3915bSDamien Miller 	int padlen, strln;     /* amount to pad */
50657f3915bSDamien Miller 	int cnt = 0;
5073c06f6a0SBen Lindstrom 
50857f3915bSDamien Miller #ifdef DEBUG_SNPRINTF
50957f3915bSDamien Miller 	printf("fmtstr min=%d max=%d s=[%s]\n", min, max, value);
51057f3915bSDamien Miller #endif
51157f3915bSDamien Miller 	if (value == 0) {
5123c06f6a0SBen Lindstrom 		value = "<NULL>";
51357f3915bSDamien Miller 	}
5143c06f6a0SBen Lindstrom 
5154127f559SDarren Tucker 	for (strln = 0; strln < max && value[strln]; ++strln); /* strlen */
5163c06f6a0SBen Lindstrom 	padlen = min - strln;
5173c06f6a0SBen Lindstrom 	if (padlen < 0)
5183c06f6a0SBen Lindstrom 		padlen = 0;
5193c06f6a0SBen Lindstrom 	if (flags & DP_F_MINUS)
5203c06f6a0SBen Lindstrom 		padlen = -padlen; /* Left Justify */
5213c06f6a0SBen Lindstrom 
5226c92dab9SBen Lindstrom 	while ((padlen > 0) && (cnt < max)) {
523742cc1c1SDamien Miller 		DOPR_OUTCH(buffer, *currlen, maxlen, ' ');
5243c06f6a0SBen Lindstrom 		--padlen;
5253c06f6a0SBen Lindstrom 		++cnt;
5263c06f6a0SBen Lindstrom 	}
5276c92dab9SBen Lindstrom 	while (*value && (cnt < max)) {
52807877ca6SDarren Tucker 		DOPR_OUTCH(buffer, *currlen, maxlen, *value);
5294d69aeabSDamien Miller 		value++;
5303c06f6a0SBen Lindstrom 		++cnt;
5313c06f6a0SBen Lindstrom 	}
5326c92dab9SBen Lindstrom 	while ((padlen < 0) && (cnt < max)) {
533742cc1c1SDamien Miller 		DOPR_OUTCH(buffer, *currlen, maxlen, ' ');
5343c06f6a0SBen Lindstrom 		++padlen;
5353c06f6a0SBen Lindstrom 		++cnt;
5363c06f6a0SBen Lindstrom 	}
537742cc1c1SDamien Miller 	return 0;
5383c06f6a0SBen Lindstrom }
5393c06f6a0SBen Lindstrom 
5403c06f6a0SBen Lindstrom /* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
5413c06f6a0SBen Lindstrom 
542742cc1c1SDamien Miller static int
fmtint(char * buffer,size_t * currlen,size_t maxlen,intmax_t value,int base,int min,int max,int flags)543742cc1c1SDamien Miller fmtint(char *buffer, size_t *currlen, size_t maxlen,
5444d69aeabSDamien Miller     intmax_t value, int base, int min, int max, int flags)
5453c06f6a0SBen Lindstrom {
54657f3915bSDamien Miller 	int signvalue = 0;
5479834cab3SDarren Tucker 	unsigned LLONG uvalue;
5483c06f6a0SBen Lindstrom 	char convert[20];
54957f3915bSDamien Miller 	int place = 0;
5503c06f6a0SBen Lindstrom 	int spadlen = 0; /* amount to space pad */
5513c06f6a0SBen Lindstrom 	int zpadlen = 0; /* amount to zero pad */
55257f3915bSDamien Miller 	int caps = 0;
5533c06f6a0SBen Lindstrom 
5543c06f6a0SBen Lindstrom 	if (max < 0)
5553c06f6a0SBen Lindstrom 		max = 0;
5563c06f6a0SBen Lindstrom 
5573c06f6a0SBen Lindstrom 	uvalue = value;
5583c06f6a0SBen Lindstrom 
5596c92dab9SBen Lindstrom 	if(!(flags & DP_F_UNSIGNED)) {
5603c06f6a0SBen Lindstrom 		if( value < 0 ) {
5613c06f6a0SBen Lindstrom 			signvalue = '-';
5623c06f6a0SBen Lindstrom 			uvalue = -value;
56357f3915bSDamien Miller 		} else {
56457f3915bSDamien Miller 			if (flags & DP_F_PLUS)  /* Do a sign (+/i) */
5653c06f6a0SBen Lindstrom 				signvalue = '+';
5666c92dab9SBen Lindstrom 			else if (flags & DP_F_SPACE)
5673c06f6a0SBen Lindstrom 				signvalue = ' ';
5683c06f6a0SBen Lindstrom 		}
56957f3915bSDamien Miller 	}
5703c06f6a0SBen Lindstrom 
57157f3915bSDamien Miller 	if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
57257f3915bSDamien Miller 
5733c06f6a0SBen Lindstrom 	do {
5743c06f6a0SBen Lindstrom 		convert[place++] =
5753c06f6a0SBen Lindstrom 			(caps? "0123456789ABCDEF":"0123456789abcdef")
5763c06f6a0SBen Lindstrom 			[uvalue % (unsigned)base  ];
5773c06f6a0SBen Lindstrom 		uvalue = (uvalue / (unsigned)base );
5783c06f6a0SBen Lindstrom 	} while(uvalue && (place < 20));
57957f3915bSDamien Miller 	if (place == 20) place--;
5803c06f6a0SBen Lindstrom 	convert[place] = 0;
5813c06f6a0SBen Lindstrom 
5823c06f6a0SBen Lindstrom 	zpadlen = max - place;
5833c06f6a0SBen Lindstrom 	spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
58457f3915bSDamien Miller 	if (zpadlen < 0) zpadlen = 0;
58557f3915bSDamien Miller 	if (spadlen < 0) spadlen = 0;
5866c92dab9SBen Lindstrom 	if (flags & DP_F_ZERO) {
5873c06f6a0SBen Lindstrom 		zpadlen = MAX(zpadlen, spadlen);
5883c06f6a0SBen Lindstrom 		spadlen = 0;
5893c06f6a0SBen Lindstrom 	}
5903c06f6a0SBen Lindstrom 	if (flags & DP_F_MINUS)
5913c06f6a0SBen Lindstrom 		spadlen = -spadlen; /* Left Justifty */
5923c06f6a0SBen Lindstrom 
59357f3915bSDamien Miller #ifdef DEBUG_SNPRINTF
59457f3915bSDamien Miller 	printf("zpad: %d, spad: %d, min: %d, max: %d, place: %d\n",
59557f3915bSDamien Miller 	    zpadlen, spadlen, min, max, place);
59657f3915bSDamien Miller #endif
59757f3915bSDamien Miller 
5983c06f6a0SBen Lindstrom 	/* Spaces */
5996c92dab9SBen Lindstrom 	while (spadlen > 0) {
600742cc1c1SDamien Miller 		DOPR_OUTCH(buffer, *currlen, maxlen, ' ');
6013c06f6a0SBen Lindstrom 		--spadlen;
6023c06f6a0SBen Lindstrom 	}
6033c06f6a0SBen Lindstrom 
6043c06f6a0SBen Lindstrom 	/* Sign */
6053c06f6a0SBen Lindstrom 	if (signvalue)
606742cc1c1SDamien Miller 		DOPR_OUTCH(buffer, *currlen, maxlen, signvalue);
6073c06f6a0SBen Lindstrom 
6083c06f6a0SBen Lindstrom 	/* Zeros */
6096c92dab9SBen Lindstrom 	if (zpadlen > 0) {
6106c92dab9SBen Lindstrom 		while (zpadlen > 0) {
611742cc1c1SDamien Miller 			DOPR_OUTCH(buffer, *currlen, maxlen, '0');
6123c06f6a0SBen Lindstrom 			--zpadlen;
6133c06f6a0SBen Lindstrom 		}
6143c06f6a0SBen Lindstrom 	}
6153c06f6a0SBen Lindstrom 
6163c06f6a0SBen Lindstrom 	/* Digits */
61707877ca6SDarren Tucker 	while (place > 0) {
61807877ca6SDarren Tucker 		--place;
61907877ca6SDarren Tucker 		DOPR_OUTCH(buffer, *currlen, maxlen, convert[place]);
62007877ca6SDarren Tucker 	}
6213c06f6a0SBen Lindstrom 
6223c06f6a0SBen Lindstrom 	/* Left Justified spaces */
6233c06f6a0SBen Lindstrom 	while (spadlen < 0) {
624742cc1c1SDamien Miller 		DOPR_OUTCH(buffer, *currlen, maxlen, ' ');
6253c06f6a0SBen Lindstrom 		++spadlen;
6263c06f6a0SBen Lindstrom 	}
627742cc1c1SDamien Miller 	return 0;
6283c06f6a0SBen Lindstrom }
6293c06f6a0SBen Lindstrom 
abs_val(LDOUBLE value)63057f3915bSDamien Miller static LDOUBLE abs_val(LDOUBLE value)
6313c06f6a0SBen Lindstrom {
63257f3915bSDamien Miller 	LDOUBLE result = value;
63357f3915bSDamien Miller 
63457f3915bSDamien Miller 	if (value < 0)
63557f3915bSDamien Miller 		result = -value;
63657f3915bSDamien Miller 
63757f3915bSDamien Miller 	return result;
63857f3915bSDamien Miller }
63957f3915bSDamien Miller 
POW10(int val)640742cc1c1SDamien Miller static LDOUBLE POW10(int val)
64157f3915bSDamien Miller {
64257f3915bSDamien Miller 	LDOUBLE result = 1;
6433c06f6a0SBen Lindstrom 
644742cc1c1SDamien Miller 	while (val) {
6453c06f6a0SBen Lindstrom 		result *= 10;
646742cc1c1SDamien Miller 		val--;
6473c06f6a0SBen Lindstrom 	}
6483c06f6a0SBen Lindstrom 
6493c06f6a0SBen Lindstrom 	return result;
6503c06f6a0SBen Lindstrom }
6513c06f6a0SBen Lindstrom 
ROUND(LDOUBLE value)65257f3915bSDamien Miller static LLONG ROUND(LDOUBLE value)
6533c06f6a0SBen Lindstrom {
65457f3915bSDamien Miller 	LLONG intpart;
6553c06f6a0SBen Lindstrom 
65657f3915bSDamien Miller 	intpart = (LLONG)value;
65757f3915bSDamien Miller 	value = value - intpart;
65857f3915bSDamien Miller 	if (value >= 0.5) intpart++;
6593c06f6a0SBen Lindstrom 
6603c06f6a0SBen Lindstrom 	return intpart;
6613c06f6a0SBen Lindstrom }
6623c06f6a0SBen Lindstrom 
66357f3915bSDamien Miller /* a replacement for modf that doesn't need the math library. Should
66457f3915bSDamien Miller    be portable, but slow */
my_modf(double x0,double * iptr)66557f3915bSDamien Miller static double my_modf(double x0, double *iptr)
6663c06f6a0SBen Lindstrom {
66757f3915bSDamien Miller 	int i;
66857f3915bSDamien Miller 	long l;
66957f3915bSDamien Miller 	double x = x0;
67057f3915bSDamien Miller 	double f = 1.0;
67157f3915bSDamien Miller 
67257f3915bSDamien Miller 	for (i=0;i<100;i++) {
67357f3915bSDamien Miller 		l = (long)x;
67457f3915bSDamien Miller 		if (l <= (x+1) && l >= (x-1)) break;
675