Blame src/snprintf.c

Packit 762fc5
/*
Packit 762fc5
 * NOTE: If you change this file, please merge it into rsync, samba, etc.
Packit 762fc5
 */
Packit 762fc5
Packit 762fc5
/*
Packit 762fc5
 * Copyright Patrick Powell 1995
Packit 762fc5
 * This code is based on code written by Patrick Powell (papowell@astart.com)
Packit 762fc5
 * It may be used for any purpose as long as this notice remains intact
Packit 762fc5
 * on all source code distributions
Packit 762fc5
 */
Packit 762fc5
Packit 762fc5
/**************************************************************
Packit 762fc5
 * Original:
Packit 762fc5
 * Patrick Powell Tue Apr 11 09:48:21 PDT 1995
Packit 762fc5
 * A bombproof version of doprnt (dopr) included.
Packit 762fc5
 * Sigh.  This sort of thing is always nasty do deal with.  Note that
Packit 762fc5
 * the version here does not include floating point...
Packit 762fc5
 *
Packit 762fc5
 * snprintf() is used instead of sprintf() as it does limit checks
Packit 762fc5
 * for string length.  This covers a nasty loophole.
Packit 762fc5
 *
Packit 762fc5
 * The other functions are there to prevent NULL pointers from
Packit 762fc5
 * causing nast effects.
Packit 762fc5
 *
Packit 762fc5
 * More Recently:
Packit 762fc5
 *  Brandon Long <blong@fiction.net> 9/15/96 for mutt 0.43
Packit 762fc5
 *  This was ugly.  It is still ugly.  I opted out of floating point
Packit 762fc5
 *  numbers, but the formatter understands just about everything
Packit 762fc5
 *  from the normal C string format, at least as far as I can tell from
Packit 762fc5
 *  the Solaris 2.5 printf(3S) man page.
Packit 762fc5
 *
Packit 762fc5
 *  Brandon Long <blong@fiction.net> 10/22/97 for mutt 0.87.1
Packit 762fc5
 *    Ok, added some minimal floating point support, which means this
Packit 762fc5
 *    probably requires libm on most operating systems.  Don't yet
Packit 762fc5
 *    support the exponent (e,E) and sigfig (g,G).  Also, fmtint()
Packit 762fc5
 *    was pretty badly broken, it just wasn't being exercised in ways
Packit 762fc5
 *    which showed it, so that's been fixed.  Also, formated the code
Packit 762fc5
 *    to mutt conventions, and removed dead code left over from the
Packit 762fc5
 *    original.  Also, there is now a builtin-test, just compile with:
Packit 762fc5
 *           gcc -DTEST_SNPRINTF -o snprintf snprintf.c -lm
Packit 762fc5
 *    and run snprintf for results.
Packit 762fc5
 * 
Packit 762fc5
 *  Thomas Roessler <roessler@guug.de> 01/27/98 for mutt 0.89i
Packit 762fc5
 *    The PGP code was using unsigned hexadecimal formats. 
Packit 762fc5
 *    Unfortunately, unsigned formats simply didn't work.
Packit 762fc5
 *
Packit 762fc5
 *  Michael Elkins <me@cs.hmc.edu> 03/05/98 for mutt 0.90.8
Packit 762fc5
 *    The original code assumed that both snprintf() and vsnprintf() were
Packit 762fc5
 *    missing.  Some systems only have snprintf() but not vsnprintf(), so
Packit 762fc5
 *    the code is now broken down under HAVE_SNPRINTF and HAVE_VSNPRINTF.
Packit 762fc5
 *
Packit 762fc5
 *  Andrew Tridgell (tridge@samba.org) Oct 1998
Packit 762fc5
 *    fixed handling of %.0f
Packit 762fc5
 *    added test for HAVE_LONG_DOUBLE
Packit 762fc5
 *
Packit 762fc5
 * tridge@samba.org, idra@samba.org, April 2001
Packit 762fc5
 *    got rid of fcvt code (twas buggy and made testing harder)
Packit 762fc5
 *    added C99 semantics
Packit 762fc5
 *
Packit 762fc5
 * date: 2002/12/19 19:56:31;  author: herb;  state: Exp;  lines: +2 -0
Packit 762fc5
 * actually print args for %g and %e
Packit 762fc5
 * 
Packit 762fc5
 * date: 2002/06/03 13:37:52;  author: jmcd;  state: Exp;  lines: +8 -0
Packit 762fc5
 * Since includes.h isn't included here, VA_COPY has to be defined here.  I don't
Packit 762fc5
 * see any include file that is guaranteed to be here, so I'm defining it
Packit 762fc5
 * locally.  Fixes AIX and Solaris builds.
Packit 762fc5
 * 
Packit 762fc5
 * date: 2002/06/03 03:07:24;  author: tridge;  state: Exp;  lines: +5 -13
Packit 762fc5
 * put the ifdef for HAVE_VA_COPY in one place rather than in lots of
Packit 762fc5
 * functions
Packit 762fc5
 * 
Packit 762fc5
 * date: 2002/05/17 14:51:22;  author: jmcd;  state: Exp;  lines: +21 -4
Packit 762fc5
 * Fix usage of va_list passed as an arg.  Use __va_copy before using it
Packit 762fc5
 * when it exists.
Packit 762fc5
 * 
Packit 762fc5
 * date: 2002/04/16 22:38:04;  author: idra;  state: Exp;  lines: +20 -14
Packit 762fc5
 * Fix incorrect zpadlen handling in fmtfp.
Packit 762fc5
 * Thanks to Ollie Oldham <ollie.oldham@metro-optix.com> for spotting it.
Packit 762fc5
 * few mods to make it easier to compile the tests.
Packit 762fc5
 * addedd the "Ollie" test to the floating point ones.
Packit 762fc5
 *
Packit 762fc5
 * Martin Pool (mbp@samba.org) April 2003
Packit 762fc5
 *    Remove NO_CONFIG_H so that the test case can be built within a source
Packit 762fc5
 *    tree with less trouble.
Packit 762fc5
 *    Remove unnecessary SAFE_FREE() definition.
Packit 762fc5
 *
Packit 762fc5
 * Martin Pool (mbp@samba.org) May 2003
Packit 762fc5
 *    Put in a prototype for dummy_snprintf() to quiet compiler warnings.
Packit 762fc5
 *
Packit 762fc5
 *    Move #endif to make sure VA_COPY, LDOUBLE, etc are defined even
Packit 762fc5
 *    if the C library has some snprintf functions already.
Packit 762fc5
 **************************************************************/
Packit 762fc5
Packit 762fc5
#ifndef NO_CONFIG_H
Packit 762fc5
#include "config.h"
Packit 762fc5
#else
Packit 762fc5
#define NULL 0
Packit 762fc5
#endif 
Packit 762fc5
Packit 762fc5
#ifdef TEST_SNPRINTF /* need math library headers for testing */
Packit 762fc5
Packit 762fc5
/* In test mode, we pretend that this system doesn't have any snprintf
Packit 762fc5
 * functions, regardless of what config.h says. */
Packit 762fc5
#  undef HAVE_SNPRINTF
Packit 762fc5
#  undef HAVE_VSNPRINTF
Packit 762fc5
#  undef HAVE_C99_VSNPRINTF
Packit 762fc5
#  undef HAVE_ASPRINTF
Packit 762fc5
#  undef HAVE_VASPRINTF
Packit 762fc5
#  include <math.h>
Packit 762fc5
#endif /* TEST_SNPRINTF */
Packit 762fc5
Packit 762fc5
#ifdef HAVE_STRING_H
Packit 762fc5
#include <string.h>
Packit 762fc5
#endif
Packit 762fc5
Packit 762fc5
#ifdef HAVE_STRINGS_H
Packit 762fc5
#include <strings.h>
Packit 762fc5
#endif
Packit 762fc5
#ifdef HAVE_CTYPE_H
Packit 762fc5
#include <ctype.h>
Packit 762fc5
#endif
Packit 762fc5
#include <sys/types.h>
Packit 762fc5
#include <stdarg.h>
Packit 762fc5
#ifdef HAVE_STDLIB_H
Packit 762fc5
#include <stdlib.h>
Packit 762fc5
#endif
Packit 762fc5
Packit 762fc5
#if defined(HAVE_SNPRINTF) && defined(HAVE_VSNPRINTF) && defined(HAVE_C99_VSNPRINTF)
Packit 762fc5
/* only include stdio.h if we are not re-defining snprintf or vsnprintf */
Packit 762fc5
#include <stdio.h>
Packit 762fc5
 /* make the compiler happy with an empty file */
Packit 762fc5
 void dummy_snprintf(void);
Packit 762fc5
 void dummy_snprintf(void) {} 
Packit 762fc5
#endif /* HAVE_SNPRINTF, etc */
Packit 762fc5
Packit 762fc5
#ifdef HAVE_LONG_DOUBLE
Packit 762fc5
#define LDOUBLE long double
Packit 762fc5
#else
Packit 762fc5
#define LDOUBLE double
Packit 762fc5
#endif
Packit 762fc5
Packit 762fc5
#if SIZEOF_LONG_LONG
Packit 762fc5
#define LLONG long long
Packit 762fc5
#else
Packit 762fc5
#define LLONG long
Packit 762fc5
#endif
Packit 762fc5
Packit 762fc5
#ifndef VA_COPY
Packit 762fc5
#if defined HAVE_VA_COPY || defined va_copy
Packit 762fc5
#define VA_COPY(dest, src) va_copy(dest, src)
Packit 762fc5
#else
Packit 762fc5
#ifdef HAVE___VA_COPY
Packit 762fc5
#define VA_COPY(dest, src) __va_copy(dest, src)
Packit 762fc5
#else
Packit 762fc5
#define VA_COPY(dest, src) (dest) = (src)
Packit 762fc5
#endif
Packit 762fc5
#endif
Packit 762fc5
Packit 762fc5
/*
Packit 762fc5
 * dopr(): poor man's version of doprintf
Packit 762fc5
 */
Packit 762fc5
Packit 762fc5
/* format read states */
Packit 762fc5
#define DP_S_DEFAULT 0
Packit 762fc5
#define DP_S_FLAGS   1
Packit 762fc5
#define DP_S_MIN     2
Packit 762fc5
#define DP_S_DOT     3
Packit 762fc5
#define DP_S_MAX     4
Packit 762fc5
#define DP_S_MOD     5
Packit 762fc5
#define DP_S_CONV    6
Packit 762fc5
#define DP_S_DONE    7
Packit 762fc5
Packit 762fc5
/* format flags - Bits */
Packit 762fc5
#define DP_F_MINUS 	(1 << 0)
Packit 762fc5
#define DP_F_PLUS  	(1 << 1)
Packit 762fc5
#define DP_F_SPACE 	(1 << 2)
Packit 762fc5
#define DP_F_NUM   	(1 << 3)
Packit 762fc5
#define DP_F_ZERO  	(1 << 4)
Packit 762fc5
#define DP_F_UP    	(1 << 5)
Packit 762fc5
#define DP_F_UNSIGNED 	(1 << 6)
Packit 762fc5
Packit 762fc5
/* Conversion Flags */
Packit 762fc5
#define DP_C_SHORT   1
Packit 762fc5
#define DP_C_LONG    2
Packit 762fc5
#define DP_C_LDOUBLE 3
Packit 762fc5
#define DP_C_LLONG   4
Packit 762fc5
Packit 762fc5
#define char_to_int(p) ((p)- '0')
Packit 762fc5
#ifndef MAX
Packit 762fc5
#define MAX(p,q) (((p) >= (q)) ? (p) : (q))
Packit 762fc5
#endif
Packit 762fc5
Packit 762fc5
/* yes this really must be a ||. Don't muck with this (tridge) */
Packit 762fc5
#if !defined(HAVE_VSNPRINTF) || !defined(HAVE_C99_VSNPRINTF)
Packit 762fc5
Packit 762fc5
static size_t dopr(char *buffer, size_t maxlen, const char *format, 
Packit 762fc5
		   va_list args_in);
Packit 762fc5
static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
Packit 762fc5
		    char *value, int flags, int min, int max);
Packit 762fc5
static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
Packit 762fc5
		    long value, int base, int min, int max, int flags);
Packit 762fc5
static void fmtfp(char *buffer, size_t *currlen, size_t maxlen,
Packit 762fc5
		   LDOUBLE fvalue, int min, int max, int flags);
Packit 762fc5
static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c);
Packit 762fc5
Packit 762fc5
static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args_in)
Packit 762fc5
{
Packit 762fc5
	char ch;
Packit 762fc5
	LLONG value;
Packit 762fc5
	LDOUBLE fvalue;
Packit 762fc5
	char *strvalue;
Packit 762fc5
	int min;
Packit 762fc5
	int max;
Packit 762fc5
	int state;
Packit 762fc5
	int flags;
Packit 762fc5
	int cflags;
Packit 762fc5
	size_t currlen;
Packit 762fc5
	va_list args;
Packit 762fc5
Packit 762fc5
	VA_COPY(args, args_in);
Packit 762fc5
	
Packit 762fc5
	state = DP_S_DEFAULT;
Packit 762fc5
	currlen = flags = cflags = min = 0;
Packit 762fc5
	max = -1;
Packit 762fc5
	ch = *format++;
Packit 762fc5
	
Packit 762fc5
	while (state != DP_S_DONE) {
Packit 762fc5
		if (ch == '\0') 
Packit 762fc5
			state = DP_S_DONE;
Packit 762fc5
Packit 762fc5
		switch(state) {
Packit 762fc5
		case DP_S_DEFAULT:
Packit 762fc5
			if (ch == '%') 
Packit 762fc5
				state = DP_S_FLAGS;
Packit 762fc5
			else 
Packit 762fc5
				dopr_outch (buffer, &currlen, maxlen, ch);
Packit 762fc5
			ch = *format++;
Packit 762fc5
			break;
Packit 762fc5
		case DP_S_FLAGS:
Packit 762fc5
			switch (ch) {
Packit 762fc5
			case '-':
Packit 762fc5
				flags |= DP_F_MINUS;
Packit 762fc5
				ch = *format++;
Packit 762fc5
				break;
Packit 762fc5
			case '+':
Packit 762fc5
				flags |= DP_F_PLUS;
Packit 762fc5
				ch = *format++;
Packit 762fc5
				break;
Packit 762fc5
			case ' ':
Packit 762fc5
				flags |= DP_F_SPACE;
Packit 762fc5
				ch = *format++;
Packit 762fc5
				break;
Packit 762fc5
			case '#':
Packit 762fc5
				flags |= DP_F_NUM;
Packit 762fc5
				ch = *format++;
Packit 762fc5
				break;
Packit 762fc5
			case '0':
Packit 762fc5
				flags |= DP_F_ZERO;
Packit 762fc5
				ch = *format++;
Packit 762fc5
				break;
Packit 762fc5
			default:
Packit 762fc5
				state = DP_S_MIN;
Packit 762fc5
				break;
Packit 762fc5
			}
Packit 762fc5
			break;
Packit 762fc5
		case DP_S_MIN:
Packit 762fc5
			if (isdigit((unsigned char)ch)) {
Packit 762fc5
				min = 10*min + char_to_int (ch);
Packit 762fc5
				ch = *format++;
Packit 762fc5
			} else if (ch == '*') {
Packit 762fc5
				min = va_arg (args, int);
Packit 762fc5
				ch = *format++;
Packit 762fc5
				state = DP_S_DOT;
Packit 762fc5
			} else {
Packit 762fc5
				state = DP_S_DOT;
Packit 762fc5
			}
Packit 762fc5
			break;
Packit 762fc5
		case DP_S_DOT:
Packit 762fc5
			if (ch == '.') {
Packit 762fc5
				state = DP_S_MAX;
Packit 762fc5
				ch = *format++;
Packit 762fc5
			} else { 
Packit 762fc5
				state = DP_S_MOD;
Packit 762fc5
			}
Packit 762fc5
			break;
Packit 762fc5
		case DP_S_MAX:
Packit 762fc5
			if (isdigit((unsigned char)ch)) {
Packit 762fc5
				if (max < 0)
Packit 762fc5
					max = 0;
Packit 762fc5
				max = 10*max + char_to_int (ch);
Packit 762fc5
				ch = *format++;
Packit 762fc5
			} else if (ch == '*') {
Packit 762fc5
				max = va_arg (args, int);
Packit 762fc5
				ch = *format++;
Packit 762fc5
				state = DP_S_MOD;
Packit 762fc5
			} else {
Packit 762fc5
				state = DP_S_MOD;
Packit 762fc5
			}
Packit 762fc5
			break;
Packit 762fc5
		case DP_S_MOD:
Packit 762fc5
			switch (ch) {
Packit 762fc5
			case 'h':
Packit 762fc5
				cflags = DP_C_SHORT;
Packit 762fc5
				ch = *format++;
Packit 762fc5
				break;
Packit 762fc5
			case 'l':
Packit 762fc5
				cflags = DP_C_LONG;
Packit 762fc5
				ch = *format++;
Packit 762fc5
				if (ch == 'l') {	/* It's a long long */
Packit 762fc5
					cflags = DP_C_LLONG;
Packit 762fc5
					ch = *format++;
Packit 762fc5
				}
Packit 762fc5
				break;
Packit 762fc5
			case 'L':
Packit 762fc5
				cflags = DP_C_LDOUBLE;
Packit 762fc5
				ch = *format++;
Packit 762fc5
				break;
Packit 762fc5
			default:
Packit 762fc5
				break;
Packit 762fc5
			}
Packit 762fc5
			state = DP_S_CONV;
Packit 762fc5
			break;
Packit 762fc5
		case DP_S_CONV:
Packit 762fc5
			switch (ch) {
Packit 762fc5
			case 'd':
Packit 762fc5
			case 'i':
Packit 762fc5
				if (cflags == DP_C_SHORT) 
Packit 762fc5
					value = va_arg (args, int);
Packit 762fc5
				else if (cflags == DP_C_LONG)
Packit 762fc5
					value = va_arg (args, long int);
Packit 762fc5
				else if (cflags == DP_C_LLONG)
Packit 762fc5
					value = va_arg (args, LLONG);
Packit 762fc5
				else
Packit 762fc5
					value = va_arg (args, int);
Packit 762fc5
				fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
Packit 762fc5
				break;
Packit 762fc5
			case 'o':
Packit 762fc5
				flags |= DP_F_UNSIGNED;
Packit 762fc5
				if (cflags == DP_C_SHORT)
Packit 762fc5
					value = va_arg (args, unsigned int);
Packit 762fc5
				else if (cflags == DP_C_LONG)
Packit 762fc5
					value = (long)va_arg (args, unsigned long int);
Packit 762fc5
				else if (cflags == DP_C_LLONG)
Packit 762fc5
					value = (long)va_arg (args, unsigned LLONG);
Packit 762fc5
				else
Packit 762fc5
					value = (long)va_arg (args, unsigned int);
Packit 762fc5
				fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags);
Packit 762fc5
				break;
Packit 762fc5
			case 'u':
Packit 762fc5
				flags |= DP_F_UNSIGNED;
Packit 762fc5
				if (cflags == DP_C_SHORT)
Packit 762fc5
					value = va_arg (args, unsigned int);
Packit 762fc5
				else if (cflags == DP_C_LONG)
Packit 762fc5
					value = (long)va_arg (args, unsigned long int);
Packit 762fc5
				else if (cflags == DP_C_LLONG)
Packit 762fc5
					value = (LLONG)va_arg (args, unsigned LLONG);
Packit 762fc5
				else
Packit 762fc5
					value = (long)va_arg (args, unsigned int);
Packit 762fc5
				fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
Packit 762fc5
				break;
Packit 762fc5
			case 'X':
Packit 762fc5
				flags |= DP_F_UP;
Packit 762fc5
			case 'x':
Packit 762fc5
				flags |= DP_F_UNSIGNED;
Packit 762fc5
				if (cflags == DP_C_SHORT)
Packit 762fc5
					value = va_arg (args, unsigned int);
Packit 762fc5
				else if (cflags == DP_C_LONG)
Packit 762fc5
					value = (long)va_arg (args, unsigned long int);
Packit 762fc5
				else if (cflags == DP_C_LLONG)
Packit 762fc5
					value = (LLONG)va_arg (args, unsigned LLONG);
Packit 762fc5
				else
Packit 762fc5
					value = (long)va_arg (args, unsigned int);
Packit 762fc5
				fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags);
Packit 762fc5
				break;
Packit 762fc5
			case 'f':
Packit 762fc5
				if (cflags == DP_C_LDOUBLE)
Packit 762fc5
					fvalue = va_arg (args, LDOUBLE);
Packit 762fc5
				else
Packit 762fc5
					fvalue = va_arg (args, double);
Packit 762fc5
				/* um, floating point? */
Packit 762fc5
				fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
Packit 762fc5
				break;
Packit 762fc5
			case 'E':
Packit 762fc5
				flags |= DP_F_UP;
Packit 762fc5
			case 'e':
Packit 762fc5
				if (cflags == DP_C_LDOUBLE)
Packit 762fc5
					fvalue = va_arg (args, LDOUBLE);
Packit 762fc5
				else
Packit 762fc5
					fvalue = va_arg (args, double);
Packit 762fc5
				fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
Packit 762fc5
				break;
Packit 762fc5
			case 'G':
Packit 762fc5
				flags |= DP_F_UP;
Packit 762fc5
			case 'g':
Packit 762fc5
				if (cflags == DP_C_LDOUBLE)
Packit 762fc5
					fvalue = va_arg (args, LDOUBLE);
Packit 762fc5
				else
Packit 762fc5
					fvalue = va_arg (args, double);
Packit 762fc5
				fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
Packit 762fc5
				break;
Packit 762fc5
			case 'c':
Packit 762fc5
				dopr_outch (buffer, &currlen, maxlen, va_arg (args, int));
Packit 762fc5
				break;
Packit 762fc5
			case 's':
Packit 762fc5
				strvalue = va_arg (args, char *);
Packit 762fc5
				if (!strvalue) strvalue = "(NULL)";
Packit 762fc5
				if (max == -1) {
Packit 762fc5
					max = strlen(strvalue);
Packit 762fc5
				}
Packit 762fc5
				if (min > 0 && max >= 0 && min > max) max = min;
Packit 762fc5
				fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max);
Packit 762fc5
				break;
Packit 762fc5
			case 'p':
Packit 762fc5
				strvalue = va_arg (args, void *);
Packit 762fc5
				fmtint (buffer, &currlen, maxlen, (long) strvalue, 16, min, max, flags);
Packit 762fc5
				break;
Packit 762fc5
			case 'n':
Packit 762fc5
				if (cflags == DP_C_SHORT) {
Packit 762fc5
					short int *num;
Packit 762fc5
					num = va_arg (args, short int *);
Packit 762fc5
					*num = currlen;
Packit 762fc5
				} else if (cflags == DP_C_LONG) {
Packit 762fc5
					long int *num;
Packit 762fc5
					num = va_arg (args, long int *);
Packit 762fc5
					*num = (long int)currlen;
Packit 762fc5
				} else if (cflags == DP_C_LLONG) {
Packit 762fc5
					LLONG *num;
Packit 762fc5
					num = va_arg (args, LLONG *);
Packit 762fc5
					*num = (LLONG)currlen;
Packit 762fc5
				} else {
Packit 762fc5
					int *num;
Packit 762fc5
					num = va_arg (args, int *);
Packit 762fc5
					*num = currlen;
Packit 762fc5
				}
Packit 762fc5
				break;
Packit 762fc5
			case '%':
Packit 762fc5
				dopr_outch (buffer, &currlen, maxlen, ch);
Packit 762fc5
				break;
Packit 762fc5
			case 'w':
Packit 762fc5
				/* not supported yet, treat as next char */
Packit 762fc5
				ch = *format++;
Packit 762fc5
				break;
Packit 762fc5
			default:
Packit 762fc5
				/* Unknown, skip */
Packit 762fc5
				break;
Packit 762fc5
			}
Packit 762fc5
			ch = *format++;
Packit 762fc5
			state = DP_S_DEFAULT;
Packit 762fc5
			flags = cflags = min = 0;
Packit 762fc5
			max = -1;
Packit 762fc5
			break;
Packit 762fc5
		case DP_S_DONE:
Packit 762fc5
			break;
Packit 762fc5
		default:
Packit 762fc5
			/* hmm? */
Packit 762fc5
			break; /* some picky compilers need this */
Packit 762fc5
		}
Packit 762fc5
	}
Packit 762fc5
	if (maxlen != 0) {
Packit 762fc5
		if (currlen < maxlen - 1) 
Packit 762fc5
			buffer[currlen] = '\0';
Packit 762fc5
		else if (maxlen > 0) 
Packit 762fc5
			buffer[maxlen - 1] = '\0';
Packit 762fc5
	}
Packit 762fc5
	
Packit 762fc5
	return currlen;
Packit 762fc5
}
Packit 762fc5
Packit 762fc5
static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
Packit 762fc5
		    char *value, int flags, int min, int max)
Packit 762fc5
{
Packit 762fc5
	int padlen, strln;     /* amount to pad */
Packit 762fc5
	int cnt = 0;
Packit 762fc5
Packit 762fc5
#ifdef DEBUG_SNPRINTF
Packit 762fc5
	printf("fmtstr min=%d max=%d s=[%s]\n", min, max, value);
Packit 762fc5
#endif
Packit 762fc5
	if (value == 0) {
Packit 762fc5
		value = "<NULL>";
Packit 762fc5
	}
Packit 762fc5
Packit 762fc5
	for (strln = 0; value[strln]; ++strln); /* strlen */
Packit 762fc5
	padlen = min - strln;
Packit 762fc5
	if (padlen < 0) 
Packit 762fc5
		padlen = 0;
Packit 762fc5
	if (flags & DP_F_MINUS) 
Packit 762fc5
		padlen = -padlen; /* Left Justify */
Packit 762fc5
	
Packit 762fc5
	while ((padlen > 0) && (cnt < max)) {
Packit 762fc5
		dopr_outch (buffer, currlen, maxlen, ' ');
Packit 762fc5
		--padlen;
Packit 762fc5
		++cnt;
Packit 762fc5
	}
Packit 762fc5
	while (*value && (cnt < max)) {
Packit 762fc5
		dopr_outch (buffer, currlen, maxlen, *value++);
Packit 762fc5
		++cnt;
Packit 762fc5
	}
Packit 762fc5
	while ((padlen < 0) && (cnt < max)) {
Packit 762fc5
		dopr_outch (buffer, currlen, maxlen, ' ');
Packit 762fc5
		++padlen;
Packit 762fc5
		++cnt;
Packit 762fc5
	}
Packit 762fc5
}
Packit 762fc5
Packit 762fc5
/* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
Packit 762fc5
Packit 762fc5
static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
Packit 762fc5
		    long value, int base, int min, int max, int flags)
Packit 762fc5
{
Packit 762fc5
	int signvalue = 0;
Packit 762fc5
	unsigned long uvalue;
Packit 762fc5
	char convert[20];
Packit 762fc5
	int place = 0;
Packit 762fc5
	int spadlen = 0; /* amount to space pad */
Packit 762fc5
	int zpadlen = 0; /* amount to zero pad */
Packit 762fc5
	int caps = 0;
Packit 762fc5
	
Packit 762fc5
	if (max < 0)
Packit 762fc5
		max = 0;
Packit 762fc5
	
Packit 762fc5
	uvalue = value;
Packit 762fc5
	
Packit 762fc5
	if(!(flags & DP_F_UNSIGNED)) {
Packit 762fc5
		if( value < 0 ) {
Packit 762fc5
			signvalue = '-';
Packit 762fc5
			uvalue = -value;
Packit 762fc5
		} else {
Packit 762fc5
			if (flags & DP_F_PLUS)  /* Do a sign (+/i) */
Packit 762fc5
				signvalue = '+';
Packit 762fc5
			else if (flags & DP_F_SPACE)
Packit 762fc5
				signvalue = ' ';
Packit 762fc5
		}
Packit 762fc5
	}
Packit 762fc5
  
Packit 762fc5
	if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
Packit 762fc5
Packit 762fc5
	do {
Packit 762fc5
		convert[place++] =
Packit 762fc5
			(caps? "0123456789ABCDEF":"0123456789abcdef")
Packit 762fc5
			[uvalue % (unsigned)base  ];
Packit 762fc5
		uvalue = (uvalue / (unsigned)base );
Packit 762fc5
	} while(uvalue && (place < 20));
Packit 762fc5
	if (place == 20) place--;
Packit 762fc5
	convert[place] = 0;
Packit 762fc5
Packit 762fc5
	zpadlen = max - place;
Packit 762fc5
	spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
Packit 762fc5
	if (zpadlen < 0) zpadlen = 0;
Packit 762fc5
	if (spadlen < 0) spadlen = 0;
Packit 762fc5
	if (flags & DP_F_ZERO) {
Packit 762fc5
		zpadlen = MAX(zpadlen, spadlen);
Packit 762fc5
		spadlen = 0;
Packit 762fc5
	}
Packit 762fc5
	if (flags & DP_F_MINUS) 
Packit 762fc5
		spadlen = -spadlen; /* Left Justifty */
Packit 762fc5
Packit 762fc5
#ifdef DEBUG_SNPRINTF
Packit 762fc5
	printf("zpad: %d, spad: %d, min: %d, max: %d, place: %d\n",
Packit 762fc5
	       zpadlen, spadlen, min, max, place);
Packit 762fc5
#endif
Packit 762fc5
Packit 762fc5
	/* Spaces */
Packit 762fc5
	while (spadlen > 0) {
Packit 762fc5
		dopr_outch (buffer, currlen, maxlen, ' ');
Packit 762fc5
		--spadlen;
Packit 762fc5
	}
Packit 762fc5
Packit 762fc5
	/* Sign */
Packit 762fc5
	if (signvalue) 
Packit 762fc5
		dopr_outch (buffer, currlen, maxlen, signvalue);
Packit 762fc5
Packit 762fc5
	/* Zeros */
Packit 762fc5
	if (zpadlen > 0) {
Packit 762fc5
		while (zpadlen > 0) {
Packit 762fc5
			dopr_outch (buffer, currlen, maxlen, '0');
Packit 762fc5
			--zpadlen;
Packit 762fc5
		}
Packit 762fc5
	}
Packit 762fc5
Packit 762fc5
	/* Digits */
Packit 762fc5
	while (place > 0) 
Packit 762fc5
		dopr_outch (buffer, currlen, maxlen, convert[--place]);
Packit 762fc5
  
Packit 762fc5
	/* Left Justified spaces */
Packit 762fc5
	while (spadlen < 0) {
Packit 762fc5
		dopr_outch (buffer, currlen, maxlen, ' ');
Packit 762fc5
		++spadlen;
Packit 762fc5
	}
Packit 762fc5
}
Packit 762fc5
Packit 762fc5
static LDOUBLE abs_val(LDOUBLE value)
Packit 762fc5
{
Packit 762fc5
	LDOUBLE result = value;
Packit 762fc5
Packit 762fc5
	if (value < 0)
Packit 762fc5
		result = -value;
Packit 762fc5
	
Packit 762fc5
	return result;
Packit 762fc5
}
Packit 762fc5
Packit 762fc5
static LDOUBLE POW10(int exp)
Packit 762fc5
{
Packit 762fc5
	LDOUBLE result = 1;
Packit 762fc5
	
Packit 762fc5
	while (exp) {
Packit 762fc5
		result *= 10;
Packit 762fc5
		exp--;
Packit 762fc5
	}
Packit 762fc5
  
Packit 762fc5
	return result;
Packit 762fc5
}
Packit 762fc5
Packit 762fc5
static LLONG ROUND(LDOUBLE value)
Packit 762fc5
{
Packit 762fc5
	LLONG intpart;
Packit 762fc5
Packit 762fc5
	intpart = (LLONG)value;
Packit 762fc5
	value = value - intpart;
Packit 762fc5
	if (value >= 0.5) intpart++;
Packit 762fc5
	
Packit 762fc5
	return intpart;
Packit 762fc5
}
Packit 762fc5
Packit 762fc5
/* a replacement for modf that doesn't need the math library. Should
Packit 762fc5
   be portable, but slow */
Packit 762fc5
static double my_modf(double x0, double *iptr)
Packit 762fc5
{
Packit 762fc5
	int i;
Packit 762fc5
	long l;
Packit 762fc5
	double x = x0;
Packit 762fc5
	double f = 1.0;
Packit 762fc5
Packit 762fc5
	for (i=0;i<100;i++) {
Packit 762fc5
		l = (long)x;
Packit 762fc5
		if (l <= (x+1) && l >= (x-1)) {
Packit 762fc5
			if (i != 0) {
Packit 762fc5
				double i2;
Packit 762fc5
				double ret;
Packit 762fc5
Packit 762fc5
				ret = my_modf(x0-l*f, &i2;;
Packit 762fc5
				(*iptr) = l*f + i2;
Packit 762fc5
				return ret;
Packit 762fc5
			} 
Packit 762fc5
Packit 762fc5
			(*iptr) = l;
Packit 762fc5
			return x - (*iptr);
Packit 762fc5
		}
Packit 762fc5
		x *= 0.1;
Packit 762fc5
		f *= 10.0;
Packit 762fc5
	}
Packit 762fc5
Packit 762fc5
	/* yikes! the number is beyond what we can handle. What do we do? */
Packit 762fc5
	(*iptr) = 0;
Packit 762fc5
	return 0;
Packit 762fc5
}
Packit 762fc5
Packit 762fc5
Packit 762fc5
static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
Packit 762fc5
		   LDOUBLE fvalue, int min, int max, int flags)
Packit 762fc5
{
Packit 762fc5
	int signvalue = 0;
Packit 762fc5
	double ufvalue;
Packit 762fc5
	char iconvert[311];
Packit 762fc5
	char fconvert[311];
Packit 762fc5
	int iplace = 0;
Packit 762fc5
	int fplace = 0;
Packit 762fc5
	int padlen = 0; /* amount to pad */
Packit 762fc5
	int zpadlen = 0; 
Packit 762fc5
	int caps = 0;
Packit 762fc5
	int idx;
Packit 762fc5
	double intpart;
Packit 762fc5
	double fracpart;
Packit 762fc5
	double temp;
Packit 762fc5
  
Packit 762fc5
	/* 
Packit 762fc5
	 * AIX manpage says the default is 0, but Solaris says the default
Packit 762fc5
	 * is 6, and sprintf on AIX defaults to 6
Packit 762fc5
	 */
Packit 762fc5
	if (max < 0)
Packit 762fc5
		max = 6;
Packit 762fc5
Packit 762fc5
	ufvalue = abs_val (fvalue);
Packit 762fc5
Packit 762fc5
	if (fvalue < 0) {
Packit 762fc5
		signvalue = '-';
Packit 762fc5
	} else {
Packit 762fc5
		if (flags & DP_F_PLUS) { /* Do a sign (+/i) */
Packit 762fc5
			signvalue = '+';
Packit 762fc5
		} else {
Packit 762fc5
			if (flags & DP_F_SPACE)
Packit 762fc5
				signvalue = ' ';
Packit 762fc5
		}
Packit 762fc5
	}
Packit 762fc5
Packit 762fc5
#if 0
Packit 762fc5
	if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
Packit 762fc5
#endif
Packit 762fc5
Packit 762fc5
#if 0
Packit 762fc5
	 if (max == 0) ufvalue += 0.5; /* if max = 0 we must round */
Packit 762fc5
#endif
Packit 762fc5
Packit 762fc5
	/* 
Packit 762fc5
	 * Sorry, we only support 16 digits past the decimal because of our 
Packit 762fc5
	 * conversion method
Packit 762fc5
	 */
Packit 762fc5
	if (max > 16)
Packit 762fc5
		max = 16;
Packit 762fc5
Packit 762fc5
	/* We "cheat" by converting the fractional part to integer by
Packit 762fc5
	 * multiplying by a factor of 10
Packit 762fc5
	 */
Packit 762fc5
Packit 762fc5
	temp = ufvalue;
Packit 762fc5
	my_modf(temp, &intpart);
Packit 762fc5
Packit 762fc5
	fracpart = ROUND((POW10(max)) * (ufvalue - intpart));
Packit 762fc5
	
Packit 762fc5
	if (fracpart >= POW10(max)) {
Packit 762fc5
		intpart++;
Packit 762fc5
		fracpart -= POW10(max);
Packit 762fc5
	}
Packit 762fc5
Packit 762fc5
Packit 762fc5
	/* Convert integer part */
Packit 762fc5
	do {
Packit 762fc5
		temp = intpart*0.1;
Packit 762fc5
		my_modf(temp, &intpart);
Packit 762fc5
		idx = (int) ((temp -intpart +0.05)* 10.0);
Packit 762fc5
		/* idx = (int) (((double)(temp*0.1) -intpart +0.05) *10.0); */
Packit 762fc5
		/* printf ("%llf, %f, %x\n", temp, intpart, idx); */
Packit 762fc5
		iconvert[iplace++] =
Packit 762fc5
			(caps? "0123456789ABCDEF":"0123456789abcdef")[idx];
Packit 762fc5
	} while (intpart && (iplace < 311));
Packit 762fc5
	if (iplace == 311) iplace--;
Packit 762fc5
	iconvert[iplace] = 0;
Packit 762fc5
Packit 762fc5
	/* Convert fractional part */
Packit 762fc5
	if (fracpart)
Packit 762fc5
	{
Packit 762fc5
		do {
Packit 762fc5
			temp = fracpart*0.1;
Packit 762fc5
			my_modf(temp, &fracpart);
Packit 762fc5
			idx = (int) ((temp -fracpart +0.05)* 10.0);
Packit 762fc5
			/* idx = (int) ((((temp/10) -fracpart) +0.05) *10); */
Packit 762fc5
			/* printf ("%lf, %lf, %ld\n", temp, fracpart, idx ); */
Packit 762fc5
			fconvert[fplace++] =
Packit 762fc5
			(caps? "0123456789ABCDEF":"0123456789abcdef")[idx];
Packit 762fc5
		} while(fracpart && (fplace < 311));
Packit 762fc5
		if (fplace == 311) fplace--;
Packit 762fc5
	}
Packit 762fc5
	fconvert[fplace] = 0;
Packit 762fc5
  
Packit 762fc5
	/* -1 for decimal point, another -1 if we are printing a sign */
Packit 762fc5
	padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0); 
Packit 762fc5
	zpadlen = max - fplace;
Packit 762fc5
	if (zpadlen < 0) zpadlen = 0;
Packit 762fc5
	if (padlen < 0) 
Packit 762fc5
		padlen = 0;
Packit 762fc5
	if (flags & DP_F_MINUS) 
Packit 762fc5
		padlen = -padlen; /* Left Justifty */
Packit 762fc5
	
Packit 762fc5
	if ((flags & DP_F_ZERO) && (padlen > 0)) {
Packit 762fc5
		if (signvalue) {
Packit 762fc5
			dopr_outch (buffer, currlen, maxlen, signvalue);
Packit 762fc5
			--padlen;
Packit 762fc5
			signvalue = 0;
Packit 762fc5
		}
Packit 762fc5
		while (padlen > 0) {
Packit 762fc5
			dopr_outch (buffer, currlen, maxlen, '0');
Packit 762fc5
			--padlen;
Packit 762fc5
		}
Packit 762fc5
	}
Packit 762fc5
	while (padlen > 0) {
Packit 762fc5
		dopr_outch (buffer, currlen, maxlen, ' ');
Packit 762fc5
		--padlen;
Packit 762fc5
	}
Packit 762fc5
	if (signvalue) 
Packit 762fc5
		dopr_outch (buffer, currlen, maxlen, signvalue);
Packit 762fc5
	
Packit 762fc5
	while (iplace > 0) 
Packit 762fc5
		dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]);
Packit 762fc5
Packit 762fc5
#ifdef DEBUG_SNPRINTF
Packit 762fc5
	printf("fmtfp: fplace=%d zpadlen=%d\n", fplace, zpadlen);
Packit 762fc5
#endif
Packit 762fc5
Packit 762fc5
	/*
Packit 762fc5
	 * Decimal point.  This should probably use locale to find the correct
Packit 762fc5
	 * char to print out.
Packit 762fc5
	 */
Packit 762fc5
	if (max > 0) {
Packit 762fc5
		dopr_outch (buffer, currlen, maxlen, '.');
Packit 762fc5
		
Packit 762fc5
		while (zpadlen > 0) {
Packit 762fc5
			dopr_outch (buffer, currlen, maxlen, '0');
Packit 762fc5
			--zpadlen;
Packit 762fc5
		}
Packit 762fc5
Packit 762fc5
		while (fplace > 0) 
Packit 762fc5
			dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]);
Packit 762fc5
	}
Packit 762fc5
Packit 762fc5
	while (padlen < 0) {
Packit 762fc5
		dopr_outch (buffer, currlen, maxlen, ' ');
Packit 762fc5
		++padlen;
Packit 762fc5
	}
Packit 762fc5
}
Packit 762fc5
Packit 762fc5
static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c)
Packit 762fc5
{
Packit 762fc5
	if (*currlen < maxlen) {
Packit 762fc5
		buffer[(*currlen)] = c;
Packit 762fc5
	}
Packit 762fc5
	(*currlen)++;
Packit 762fc5
}
Packit 762fc5
Packit 762fc5
 int rsync_vsnprintf (char *str, size_t count, const char *fmt, va_list args)
Packit 762fc5
{
Packit 762fc5
	return dopr(str, count, fmt, args);
Packit 762fc5
}
Packit 762fc5
#define vsnprintf rsync_vsnprintf
Packit 762fc5
#endif
Packit 762fc5
Packit 762fc5
/* yes this really must be a ||. Don't muck with this (tridge)
Packit 762fc5
 *
Packit 762fc5
 * The logic for these two is that we need our own definition if the
Packit 762fc5
 * OS *either* has no definition of *sprintf, or if it does have one
Packit 762fc5
 * that doesn't work properly according to the autoconf test.
Packit 762fc5
 */
Packit 762fc5
#if !defined(HAVE_SNPRINTF) || !defined(HAVE_C99_VSNPRINTF)
Packit 762fc5
int rsync_snprintf(char *str,size_t count,const char *fmt,...)
Packit 762fc5
{
Packit 762fc5
	size_t ret;
Packit 762fc5
	va_list ap;
Packit 762fc5
    
Packit 762fc5
	va_start(ap, fmt);
Packit 762fc5
	ret = vsnprintf(str, count, fmt, ap);
Packit 762fc5
	va_end(ap);
Packit 762fc5
	return ret;
Packit 762fc5
}
Packit 762fc5
#define snprintf rsync_snprintf
Packit 762fc5
#endif
Packit 762fc5
Packit 762fc5
#endif 
Packit 762fc5
Packit 762fc5
#ifndef HAVE_VASPRINTF
Packit 762fc5
 int vasprintf(char **ptr, const char *format, va_list ap)
Packit 762fc5
{
Packit 762fc5
	int ret;
Packit 762fc5
	va_list ap2;
Packit 762fc5
Packit 762fc5
	VA_COPY(ap2, ap);
Packit 762fc5
	
Packit 762fc5
	ret = vsnprintf(NULL, 0, format, ap2);
Packit 762fc5
	if (ret <= 0) return ret;
Packit 762fc5
Packit 762fc5
	(*ptr) = (char *)malloc(ret+1);
Packit 762fc5
	if (!*ptr) return -1;
Packit 762fc5
Packit 762fc5
	VA_COPY(ap2, ap);
Packit 762fc5
Packit 762fc5
	ret = vsnprintf(*ptr, ret+1, format, ap2);
Packit 762fc5
Packit 762fc5
	return ret;
Packit 762fc5
}
Packit 762fc5
#endif
Packit 762fc5
Packit 762fc5
Packit 762fc5
#ifndef HAVE_ASPRINTF
Packit 762fc5
 int asprintf(char **ptr, const char *format, ...)
Packit 762fc5
{
Packit 762fc5
	va_list ap;
Packit 762fc5
	int ret;
Packit 762fc5
	
Packit 762fc5
	*ptr = NULL;
Packit 762fc5
	va_start(ap, format);
Packit 762fc5
	ret = vasprintf(ptr, format, ap);
Packit 762fc5
	va_end(ap);
Packit 762fc5
Packit 762fc5
	return ret;
Packit 762fc5
}
Packit 762fc5
#endif
Packit 762fc5
Packit 762fc5
#ifdef TEST_SNPRINTF
Packit 762fc5
Packit 762fc5
 int sprintf(char *str,const char *fmt,...);
Packit 762fc5
Packit 762fc5
 int main (void)
Packit 762fc5
{
Packit 762fc5
	char buf1[1024];
Packit 762fc5
	char buf2[1024];
Packit 762fc5
	char *fp_fmt[] = {
Packit 762fc5
		"%1.1f",
Packit 762fc5
		"%-1.5f",
Packit 762fc5
		"%1.5f",
Packit 762fc5
		"%123.9f",
Packit 762fc5
		"%10.5f",
Packit 762fc5
		"% 10.5f",
Packit 762fc5
		"%+22.9f",
Packit 762fc5
		"%+4.9f",
Packit 762fc5
		"%01.3f",
Packit 762fc5
		"%4f",
Packit 762fc5
		"%3.1f",
Packit 762fc5
		"%3.2f",
Packit 762fc5
		"%.0f",
Packit 762fc5
		"%f",
Packit 762fc5
		"-16.16f",
Packit 762fc5
		NULL
Packit 762fc5
	};
Packit 762fc5
	double fp_nums[] = { 6442452944.1234, -1.5, 134.21, 91340.2, 341.1234, 203.9, 0.96, 0.996, 
Packit 762fc5
			     0.9996, 1.996, 4.136, 5.030201, 0.00205,
Packit 762fc5
			     /* END LIST */ 0};
Packit 762fc5
	char *int_fmt[] = {
Packit 762fc5
		"%-1.5d",
Packit 762fc5
		"%1.5d",
Packit 762fc5
		"%123.9d",
Packit 762fc5
		"%5.5d",
Packit 762fc5
		"%10.5d",
Packit 762fc5
		"% 10.5d",
Packit 762fc5
		"%+22.33d",
Packit 762fc5
		"%01.3d",
Packit 762fc5
		"%4d",
Packit 762fc5
		"%d",
Packit 762fc5
		NULL
Packit 762fc5
	};
Packit 762fc5
	long int_nums[] = { -1, 134, 91340, 341, 0203, 0};
Packit 762fc5
	char *str_fmt[] = {
Packit 762fc5
		"10.5s",
Packit 762fc5
		"5.10s",
Packit 762fc5
		"10.1s",
Packit 762fc5
		"0.10s",
Packit 762fc5
		"10.0s",
Packit 762fc5
		"1.10s",
Packit 762fc5
		"%s",
Packit 762fc5
		"%.1s",
Packit 762fc5
		"%.10s",
Packit 762fc5
		"%10s",
Packit 762fc5
		NULL
Packit 762fc5
	};
Packit 762fc5
	char *str_vals[] = {"hello", "a", "", "a longer string", NULL};
Packit 762fc5
	int x, y;
Packit 762fc5
	int fail = 0;
Packit 762fc5
	int num = 0;
Packit 762fc5
Packit 762fc5
	printf ("Testing snprintf format codes against system sprintf...\n");
Packit 762fc5
Packit 762fc5
	for (x = 0; fp_fmt[x] ; x++) {
Packit 762fc5
		for (y = 0; fp_nums[y] != 0 ; y++) {
Packit 762fc5
			int l1 = snprintf(NULL, 0, fp_fmt[x], fp_nums[y]);
Packit 762fc5
			int l2 = snprintf(buf1, sizeof(buf1), fp_fmt[x], fp_nums[y]);
Packit 762fc5
			sprintf (buf2, fp_fmt[x], fp_nums[y]);
Packit 762fc5
			if (strcmp (buf1, buf2)) {
Packit 762fc5
				printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n", 
Packit 762fc5
				       fp_fmt[x], buf1, buf2);
Packit 762fc5
				fail++;
Packit 762fc5
			}
Packit 762fc5
			if (l1 != l2) {
Packit 762fc5
				printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, fp_fmt[x]);
Packit 762fc5
				fail++;
Packit 762fc5
			}
Packit 762fc5
			num++;
Packit 762fc5
		}
Packit 762fc5
	}
Packit 762fc5
Packit 762fc5
	for (x = 0; int_fmt[x] ; x++) {
Packit 762fc5
		for (y = 0; int_nums[y] != 0 ; y++) {
Packit 762fc5
			int l1 = snprintf(NULL, 0, int_fmt[x], int_nums[y]);
Packit 762fc5
			int l2 = snprintf(buf1, sizeof(buf1), int_fmt[x], int_nums[y]);
Packit 762fc5
			sprintf (buf2, int_fmt[x], int_nums[y]);
Packit 762fc5
			if (strcmp (buf1, buf2)) {
Packit 762fc5
				printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n", 
Packit 762fc5
				       int_fmt[x], buf1, buf2);
Packit 762fc5
				fail++;
Packit 762fc5
			}
Packit 762fc5
			if (l1 != l2) {
Packit 762fc5
				printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, int_fmt[x]);
Packit 762fc5
				fail++;
Packit 762fc5
			}
Packit 762fc5
			num++;
Packit 762fc5
		}
Packit 762fc5
	}
Packit 762fc5
Packit 762fc5
	for (x = 0; str_fmt[x] ; x++) {
Packit 762fc5
		for (y = 0; str_vals[y] != 0 ; y++) {
Packit 762fc5
			int l1 = snprintf(NULL, 0, str_fmt[x], str_vals[y]);
Packit 762fc5
			int l2 = snprintf(buf1, sizeof(buf1), str_fmt[x], str_vals[y]);
Packit 762fc5
			sprintf (buf2, str_fmt[x], str_vals[y]);
Packit 762fc5
			if (strcmp (buf1, buf2)) {
Packit 762fc5
				printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n", 
Packit 762fc5
				       str_fmt[x], buf1, buf2);
Packit 762fc5
				fail++;
Packit 762fc5
			}
Packit 762fc5
			if (l1 != l2) {
Packit 762fc5
				printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, str_fmt[x]);
Packit 762fc5
				fail++;
Packit 762fc5
			}
Packit 762fc5
			num++;
Packit 762fc5
		}
Packit 762fc5
	}
Packit 762fc5
Packit 762fc5
	printf ("%d tests failed out of %d.\n", fail, num);
Packit 762fc5
Packit 762fc5
	printf("seeing how many digits we support\n");
Packit 762fc5
	{
Packit 762fc5
		double v0 = 0.12345678901234567890123456789012345678901;
Packit 762fc5
		for (x=0; x<100; x++) {
Packit 762fc5
			double p = pow(10, x); 
Packit 762fc5
			double r = v0*p;
Packit 762fc5
			snprintf(buf1, sizeof(buf1), "%1.1f", r);
Packit 762fc5
			sprintf(buf2,                "%1.1f", r);
Packit 762fc5
			if (strcmp(buf1, buf2)) {
Packit 762fc5
				printf("we seem to support %d digits\n", x-1);
Packit 762fc5
				break;
Packit 762fc5
			}
Packit 762fc5
		}
Packit 762fc5
	}
Packit 762fc5
Packit 762fc5
	return 0;
Packit 762fc5
}
Packit 762fc5
#endif /* TEST_SNPRINTF */