X-Git-Url: https://mattmccutchen.net/rsync/rsync.git/blobdiff_plain/f8be5ef4cb86bee9227b14b41238ac867c820759..9e3c856a39fe363e5459e1b24c082535282ce71a:/lib/snprintf.c diff --git a/lib/snprintf.c b/lib/snprintf.c index 98c189c3..8bbc67a3 100644 --- a/lib/snprintf.c +++ b/lib/snprintf.c @@ -1,11 +1,8 @@ -/* This is taken from cvslock. The same code is used in several - * freeware and GPLd applications. I contacted the email addresses - * listed in the header and have been told that the code is probably - * public domain, but I am still seeking confirmation from Patrick - * Powell. The original code was posted to BugTraq by Patrick in 1995 - * but without any notice as to the copyright or license status. - * - * October 1998, Andrew Tridgell (tridge@samba.anu.edu.au) +/* + * Copyright Patrick Powell 1995 + * This code is based on code written by Patrick Powell (papowell@astart.com) + * It may be used for any purpose as long as this notice remains intact + * on all source code distributions */ /************************************************************** @@ -48,16 +45,20 @@ * missing. Some systems only have snprintf() but not vsnprintf(), so * the code is now broken down under HAVE_SNPRINTF and HAVE_VSNPRINTF. * + * Andrew Tridgell (tridge@samba.org) Oct 1998 + * fixed handling of %.0f + * added test for HAVE_LONG_DOUBLE + * **************************************************************/ #include "config.h" -#if !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) - #include # include #include +#if !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) + /* Define this as a fall through, HAVE_STDARG_H is probably already set */ #define HAVE_VARARGS_H @@ -84,6 +85,12 @@ # endif #endif +#ifdef HAVE_LONG_DOUBLE +#define LDOUBLE long double +#else +#define LDOUBLE double +#endif + /*int snprintf (char *str, size_t count, const char *fmt, ...);*/ /*int vsnprintf (char *str, size_t count, const char *fmt, va_list arg);*/ @@ -94,7 +101,7 @@ static void fmtstr (char *buffer, size_t *currlen, size_t maxlen, static void fmtint (char *buffer, size_t *currlen, size_t maxlen, long value, int base, int min, int max, int flags); static void fmtfp (char *buffer, size_t *currlen, size_t maxlen, - long double fvalue, int min, int max, int flags); + LDOUBLE fvalue, int min, int max, int flags); static void dopr_outch (char *buffer, size_t *currlen, size_t maxlen, char c ); /* @@ -132,7 +139,7 @@ static void dopr (char *buffer, size_t maxlen, const char *format, va_list args) { char ch; long value; - long double fvalue; + LDOUBLE fvalue; char *strvalue; int min; int max; @@ -268,9 +275,9 @@ static void dopr (char *buffer, size_t maxlen, const char *format, va_list args) if (cflags == DP_C_SHORT) value = va_arg (args, unsigned short int); else if (cflags == DP_C_LONG) - value = va_arg (args, unsigned long int); + value = (long)va_arg (args, unsigned long int); else - value = va_arg (args, unsigned int); + value = (long)va_arg (args, unsigned int); fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags); break; case 'u': @@ -278,9 +285,9 @@ static void dopr (char *buffer, size_t maxlen, const char *format, va_list args) if (cflags == DP_C_SHORT) value = va_arg (args, unsigned short int); else if (cflags == DP_C_LONG) - value = va_arg (args, unsigned long int); + value = (long)va_arg (args, unsigned long int); else - value = va_arg (args, unsigned int); + value = (long)va_arg (args, unsigned int); fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags); break; case 'X': @@ -290,14 +297,14 @@ static void dopr (char *buffer, size_t maxlen, const char *format, va_list args) if (cflags == DP_C_SHORT) value = va_arg (args, unsigned short int); else if (cflags == DP_C_LONG) - value = va_arg (args, unsigned long int); + value = (long)va_arg (args, unsigned long int); else - value = va_arg (args, unsigned int); + value = (long)va_arg (args, unsigned int); fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags); break; case 'f': if (cflags == DP_C_LDOUBLE) - fvalue = va_arg (args, long double); + fvalue = va_arg (args, LDOUBLE); else fvalue = va_arg (args, double); /* um, floating point? */ @@ -307,7 +314,7 @@ static void dopr (char *buffer, size_t maxlen, const char *format, va_list args) flags |= DP_F_UP; case 'e': if (cflags == DP_C_LDOUBLE) - fvalue = va_arg (args, long double); + fvalue = va_arg (args, LDOUBLE); else fvalue = va_arg (args, double); break; @@ -315,7 +322,7 @@ static void dopr (char *buffer, size_t maxlen, const char *format, va_list args) flags |= DP_F_UP; case 'g': if (cflags == DP_C_LDOUBLE) - fvalue = va_arg (args, long double); + fvalue = va_arg (args, LDOUBLE); else fvalue = va_arg (args, double); break; @@ -343,7 +350,7 @@ static void dopr (char *buffer, size_t maxlen, const char *format, va_list args) { long int *num; num = va_arg (args, long int *); - *num = currlen; + *num = (long int)currlen; } else { @@ -474,8 +481,8 @@ static void fmtint (char *buffer, size_t *currlen, size_t maxlen, spadlen = -spadlen; /* Left Justifty */ #ifdef DEBUG_SNPRINTF - dprint (1, (debugfile, "zpad: %d, spad: %d, min: %d, max: %d, place: %d\n", - zpadlen, spadlen, min, max, place)); + printf("zpad: %d, spad: %d, min: %d, max: %d, place: %d\n", + zpadlen, spadlen, min, max, place); #endif /* Spaces */ @@ -510,9 +517,9 @@ static void fmtint (char *buffer, size_t *currlen, size_t maxlen, } } -static long double abs_val (long double value) +static LDOUBLE abs_val (LDOUBLE value) { - long double result = value; + LDOUBLE result = value; if (value < 0) result = -value; @@ -520,9 +527,9 @@ static long double abs_val (long double value) return result; } -static long double pow10 (int exp) +static LDOUBLE pow10 (int exp) { - long double result = 1; + LDOUBLE result = 1; while (exp) { @@ -533,11 +540,11 @@ static long double pow10 (int exp) return result; } -static long round (long double value) +static long round (LDOUBLE value) { long intpart; - intpart = value; + intpart = (long)value; value = value - intpart; if (value >= 0.5) intpart++; @@ -546,10 +553,10 @@ static long round (long double value) } static void fmtfp (char *buffer, size_t *currlen, size_t maxlen, - long double fvalue, int min, int max, int flags) + LDOUBLE fvalue, int min, int max, int flags) { int signvalue = 0; - long double ufvalue; + LDOUBLE ufvalue; char iconvert[20]; char fconvert[20]; int iplace = 0; @@ -582,7 +589,7 @@ static void fmtfp (char *buffer, size_t *currlen, size_t maxlen, if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */ #endif - intpart = ufvalue; + intpart = (long)ufvalue; /* * Sorry, we only support 9 digits past the decimal because of our @@ -603,7 +610,8 @@ static void fmtfp (char *buffer, size_t *currlen, size_t maxlen, } #ifdef DEBUG_SNPRINTF - dprint (1, (debugfile, "fmtfp: %f =? %d.%d\n", fvalue, intpart, fracpart)); + printf("fmtfp: %g %d.%d min=%d max=%d\n", + (double)fvalue, intpart, fracpart, min, max); #endif /* Convert integer part */ @@ -659,14 +667,21 @@ static void fmtfp (char *buffer, size_t *currlen, size_t maxlen, while (iplace > 0) dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]); + +#ifdef DEBUG_SNPRINTF + printf("fmtfp: fplace=%d zpadlen=%d\n", fplace, zpadlen); +#endif + /* * Decimal point. This should probably use locale to find the correct * char to print out. */ - dopr_outch (buffer, currlen, maxlen, '.'); + if (max > 0) { + dopr_outch (buffer, currlen, maxlen, '.'); - while (fplace > 0) - dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]); + while (fplace > 0) + dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]); + } while (zpadlen > 0) { @@ -689,7 +704,7 @@ static void dopr_outch (char *buffer, size_t *currlen, size_t maxlen, char c) #endif /* !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) */ #ifndef HAVE_VSNPRINTF -int vsnprintf (char *str, size_t count, const char *fmt, va_list args) + int vsnprintf (char *str, size_t count, const char *fmt, va_list args) { str[0] = 0; dopr(str, count, fmt, args); @@ -700,9 +715,9 @@ int vsnprintf (char *str, size_t count, const char *fmt, va_list args) #ifndef HAVE_SNPRINTF /* VARARGS3 */ #ifdef HAVE_STDARGS -int snprintf (char *str,size_t count,const char *fmt,...) + int snprintf (char *str,size_t count,const char *fmt,...) #else -int snprintf (va_alist) va_dcl + int snprintf (va_alist) va_dcl #endif { #ifndef HAVE_STDARGS @@ -721,11 +736,17 @@ int snprintf (va_alist) va_dcl return(strlen(str)); } + +#else + /* keep compilers happy about empty files */ + void dummy_snprintf(void) {} +#endif /* !HAVE_SNPRINTF */ + #ifdef TEST_SNPRINTF #ifndef LONG_STRING #define LONG_STRING 1024 #endif -int main (void) + int main (void) { char buf1[LONG_STRING]; char buf2[LONG_STRING]; @@ -741,6 +762,8 @@ int main (void) "%4f", "%3.1f", "%3.2f", + "%.0f", + "%.1f", NULL }; double fp_nums[] = { -1.5, 134.21, 91340.2, 341.1234, 0203.9, 0.96, 0.996, @@ -795,7 +818,3 @@ int main (void) } #endif /* SNPRINTF_TEST */ -#else - /* keep compilers happy about empty files */ - void dummy_snprintf(void) {} -#endif /* !HAVE_SNPRINTF */