* 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.anu.edu.au) Oct 1998
+ * fixed handling of %.0f
+ * added test for HAVE_LONG_DOUBLE
+ *
**************************************************************/
#include "config.h"
-#if !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF)
-
#include <string.h>
# include <ctype.h>
#include <sys/types.h>
+#if !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF)
+
/* Define this as a fall through, HAVE_STDARG_H is probably already set */
#define HAVE_VARARGS_H
# 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);*/
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 );
/*
{
char ch;
long value;
- long double fvalue;
+ LDOUBLE fvalue;
char *strvalue;
int min;
int max;
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? */
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;
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;
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 */
}
}
-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;
return result;
}
-static long double pow10 (int exp)
+static LDOUBLE pow10 (int exp)
{
- long double result = 1;
+ LDOUBLE result = 1;
while (exp)
{
return result;
}
-static long round (long double value)
+static long round (LDOUBLE value)
{
long intpart;
}
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;
}
#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 */
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)
{
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
"%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,
}
#endif /* SNPRINTF_TEST */
-#else
- /* keep compilers happy about empty files */
- void dummy_snprintf(void) {}
-#endif /* !HAVE_SNPRINTF */