Loading...
stdio/FreeBSD/vfprintf.c Libc-583 Libc-320.1.3
--- Libc/Libc-583/stdio/FreeBSD/vfprintf.c
+++ Libc/Libc-320.1.3/stdio/FreeBSD/vfprintf.c
@@ -38,7 +38,7 @@
 static char sccsid[] = "@(#)vfprintf.c	8.1 (Berkeley) 6/4/93";
 #endif /* LIBC_SCCS and not lint */
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: src/lib/libc/stdio/vfprintf.c,v 1.68 2004/08/26 06:25:28 des Exp $");
+__FBSDID("$FreeBSD: src/lib/libc/stdio/vfprintf.c,v 1.59 2003/04/19 23:53:19 das Exp $");
 
 /*
  * Actual printf innards.
@@ -65,6 +65,9 @@
 #include "libc_private.h"
 #include "local.h"
 #include "fvwrite.h"
+
+/* Define FLOATING_POINT to get floating point. */
+#define	FLOATING_POINT
 
 union arg {
 	int	intarg;
@@ -87,7 +90,7 @@
 	ptrdiff_t *pptrdiffarg;
 	size_t	*psizearg;
 	intmax_t *pintmaxarg;
-#ifndef NO_FLOATING_POINT
+#ifdef FLOATING_POINT
 	double	doublearg;
 	long double longdoublearg;
 #endif
@@ -338,21 +341,20 @@
 static char *
 __wcsconv(wchar_t *wcsarg, int prec)
 {
-	static const mbstate_t initial;
-	mbstate_t mbs;
 	char buf[MB_LEN_MAX];
 	wchar_t *p;
 	char *convbuf, *mbp;
 	size_t clen, nbytes;
+	mbstate_t mbs;
 
 	/*
 	 * Determine the number of bytes to output and allocate space for
 	 * the output.
 	 */
+	memset(&mbs, 0, sizeof(mbs));
 	if (prec >= 0) {
 		nbytes = 0;
 		p = wcsarg;
-		mbs = initial;
 		for (;;) {
 			clen = wcrtomb(buf, *p++, &mbs);
 			if (clen == 0 || clen == (size_t)-1 ||
@@ -362,7 +364,6 @@
 		}
 	} else {
 		p = wcsarg;
-		mbs = initial;
 		nbytes = wcsrtombs(NULL, (const wchar_t **)&p, 0, &mbs);
 		if (nbytes == (size_t)-1)
 			return (NULL);
@@ -376,7 +377,7 @@
 	 */
 	mbp = convbuf;
 	p = wcsarg;
-	mbs = initial;
+	memset(&mbs, 0, sizeof(mbs));
 	while (mbp - convbuf < nbytes) {
 		clen = wcrtomb(mbp, *p++, &mbs);
 		if (clen == 0 || clen == (size_t)-1)
@@ -407,7 +408,7 @@
 	return (ret);
 }
 
-#ifndef NO_FLOATING_POINT
+#ifdef FLOATING_POINT
 
 #define	dtoa		__dtoa
 #define	freedtoa	__freedtoa
@@ -421,7 +422,7 @@
 
 static int exponent(char *, int, int);
 
-#endif /* !NO_FLOATING_POINT */
+#endif /* FLOATING_POINT */
 
 /*
  * The size of the buffer we use as scratch space for integer
@@ -470,7 +471,7 @@
 	char sign;		/* sign prefix (' ', '+', '-', or \0) */
 	char thousands_sep;	/* locale specific thousands separator */
 	const char *grouping;	/* locale specific numeric grouping rules */
-#ifndef NO_FLOATING_POINT
+#ifdef FLOATING_POINT
 	/*
 	 * We can decompose the printed representation of floating
 	 * point numbers into several parts, some of which may be empty:
@@ -637,12 +638,12 @@
 	thousands_sep = '\0';
 	grouping = NULL;
 	convbuf = NULL;
-#ifndef NO_FLOATING_POINT
+#ifdef FLOATING_POINT
 	dtoaresult = NULL;
 	decimal_point = localeconv()->decimal_point;
 #endif
 	/* sorry, fprintf(read_only_file, "") returns EOF, not 0 */
-	if (prepwrite(fp) != 0)
+	if (cantwrite(fp))
 		return (EOF);
 
 	/* optimise fprintf(stderr) (and other unbuffered Unix files) */
@@ -758,7 +759,7 @@
 			}
 			width = n;
 			goto reswitch;
-#ifndef NO_FLOATING_POINT
+#ifdef FLOATING_POINT
 		case 'L':
 			flags |= LONGDBL;
 			goto rflag;
@@ -794,11 +795,10 @@
 			/*FALLTHROUGH*/
 		case 'c':
 			if (flags & LONGINT) {
-				static const mbstate_t initial;
 				mbstate_t mbs;
 				size_t mbseqlen;
 
-				mbs = initial;
+				memset(&mbs, 0, sizeof(mbs));
 				mbseqlen = wcrtomb(cp = buf,
 				    (wchar_t)GETARG(wint_t), &mbs);
 				if (mbseqlen == (size_t)-1) {
@@ -832,7 +832,8 @@
 			}
 			base = 10;
 			goto number;
-#ifndef NO_FLOATING_POINT
+#ifdef FLOATING_POINT
+#ifdef HEXFLOAT
 		case 'a':
 		case 'A':
 			if (ch == 'a') {
@@ -844,12 +845,12 @@
 				xdigs = xdigs_upper;
 				expchar = 'P';
 			}
-			if (prec >= 0)
-				prec++;
-			if (dtoaresult != NULL)
-				freedtoa(dtoaresult);
+			/*
+			 * XXX We don't actually have a conversion
+			 * XXX routine for this yet.
+			 */
 			if (flags & LONGDBL) {
-				fparg.ldbl = GETARG(long double);
+				fparg.ldbl = (double)GETARG(long double);
 				dtoaresult = cp =
 				    __hldtoa(fparg.ldbl, xdigs, prec,
 				    &expt, &signflag, &dtoaend);
@@ -859,11 +860,8 @@
 				    __hdtoa(fparg.dbl, xdigs, prec,
 				    &expt, &signflag, &dtoaend);
 			}
-			if (prec < 0)
-				prec = dtoaend - cp;
-			if (expt == INT_MAX)
-				ox[1] = '\0';
-			goto fp_common;
+			goto fp_begin;
+#endif
 		case 'e':
 		case 'E':
 			expchar = ch;
@@ -899,7 +897,6 @@
 				if (expt == 9999)
 					expt = INT_MAX;
 			}
-fp_common:
 			if (signflag)
 				sign = '-';
 			if (expt == INT_MAX) {	/* inf or nan */
@@ -965,7 +962,7 @@
 					lead = expt;
 			}
 			break;
-#endif /* !NO_FLOATING_POINT */
+#endif /* FLOATING_POINT */
 		case 'n':
 			/*
 			 * Assignment-like behavior is specified if the
@@ -1140,7 +1137,7 @@
 		realsz = dprec > size ? dprec : size;
 		if (sign)
 			realsz++;
-		if (ox[1])
+		else if (ox[1])
 			realsz += 2;
 
 		prsize = width > realsz ? width : realsz;
@@ -1154,10 +1151,9 @@
 			PAD(width - realsz, blanks);
 
 		/* prefix */
-		if (sign)
+		if (sign) {
 			PRINT(&sign, 1);
-
-		if (ox[1]) {	/* ox[1] is either x, X, or \0 */
+		} else if (ox[1]) {	/* ox[1] is either x, X, or \0 */
 			ox[0] = '0';
 			PRINT(ox, 2);
 		}
@@ -1170,7 +1166,7 @@
 		PAD(dprec - size, zeroes);
 
 		/* the string or number proper */
-#ifndef NO_FLOATING_POINT
+#ifdef FLOATING_POINT
 		if ((flags & FPT) == 0) {
 			PRINT(cp, size);
 		} else {	/* glue together f_p fragments */
@@ -1233,8 +1229,7 @@
 done:
 	FLUSH();
 error:
-	va_end(orgap);
-#ifndef NO_FLOATING_POINT
+#ifdef FLOATING_POINT
 	if (dtoaresult != NULL)
 		freedtoa(dtoaresult);
 #endif
@@ -1274,7 +1269,7 @@
 	 */
 #define ADDTYPE(type) \
 	((nextarg >= tablesize) ? \
-		__grow_type_table(nextarg, &typetable, &tablesize) : (void)0, \
+		__grow_type_table(nextarg, &typetable, &tablesize) : 0, \
 	(nextarg > tablemax) ? tablemax = nextarg : 0, \
 	typetable[nextarg++] = type)
 
@@ -1316,8 +1311,7 @@
 	tablesize = STATIC_ARG_TBL_SIZE;
 	tablemax = 0; 
 	nextarg = 1;
-	for (n = 0; n < STATIC_ARG_TBL_SIZE; n++)
-		typetable[n] = T_UNUSED;
+	memset (typetable, T_UNUSED, STATIC_ARG_TBL_SIZE);
 
 	/*
 	 * Scan the format for conversions (`%' character).
@@ -1368,7 +1362,7 @@
 			}
 			width = n;
 			goto reswitch;
-#ifndef NO_FLOATING_POINT
+#ifdef FLOATING_POINT
 		case 'L':
 			flags |= LONGDBL;
 			goto rflag;
@@ -1415,9 +1409,11 @@
 		case 'i':
 			ADDSARG();
 			break;
-#ifndef NO_FLOATING_POINT
+#ifdef FLOATING_POINT
+#ifdef HEXFLOAT
 		case 'a':
 		case 'A':
+#endif
 		case 'e':
 		case 'E':
 		case 'f':
@@ -1428,7 +1424,7 @@
 			else
 				ADDTYPE(T_DOUBLE);
 			break;
-#endif /* !NO_FLOATING_POINT */
+#endif /* FLOATING_POINT */
 		case 'n':
 			if (flags & INTMAXT)
 				ADDTYPE(TP_INTMAXT);
@@ -1548,7 +1544,7 @@
 		    case TP_INTMAXT:
 			(*argtable) [n].pintmaxarg = va_arg (ap, intmax_t *);
 			break;
-#ifndef NO_FLOATING_POINT
+#ifdef FLOATING_POINT
 		    case T_DOUBLE:
 			(*argtable) [n].doublearg = va_arg (ap, double);
 			break;
@@ -1584,28 +1580,26 @@
 	enum typeid *const oldtable = *typetable;
 	const int oldsize = *tablesize;
 	enum typeid *newtable;
-	int n, newsize = oldsize * 2;
+	int newsize = oldsize * 2;
 
 	if (newsize < nextarg + 1)
 		newsize = nextarg + 1;
 	if (oldsize == STATIC_ARG_TBL_SIZE) {
-		if ((newtable = malloc(newsize * sizeof(enum typeid))) == NULL)
+		if ((newtable = malloc(newsize)) == NULL)
 			abort();			/* XXX handle better */
-		bcopy(oldtable, newtable, oldsize * sizeof(enum typeid));
+		bcopy(oldtable, newtable, oldsize);
 	} else {
-		newtable = reallocf(oldtable, newsize * sizeof(enum typeid));
-		if (newtable == NULL)
+		if ((newtable = reallocf(oldtable, newsize)) == NULL)
 			abort();			/* XXX handle better */
 	}
-	for (n = oldsize; n < newsize; n++)
-		newtable[n] = T_UNUSED;
+	memset(&newtable[oldsize], T_UNUSED, newsize - oldsize);
 
 	*typetable = newtable;
 	*tablesize = newsize;
 }
 
 
-#ifndef NO_FLOATING_POINT
+#ifdef FLOATING_POINT
 
 static int
 exponent(char *p0, int exp, int fmtch)
@@ -1642,4 +1636,4 @@
 	}
 	return (p - p0);
 }
-#endif /* !NO_FLOATING_POINT */
+#endif /* FLOATING_POINT */