Loading...
stdio/FreeBSD/vfwprintf.c Libc-498 Libc-320
--- Libc/Libc-498/stdio/FreeBSD/vfwprintf.c
+++ Libc/Libc-320/stdio/FreeBSD/vfwprintf.c
@@ -34,13 +34,14 @@
  * SUCH DAMAGE.
  */
 
+#include <sys/cdefs.h>
 #if 0
 #if defined(LIBC_SCCS) && !defined(lint)
 static char sccsid[] = "@(#)vfprintf.c	8.1 (Berkeley) 6/4/93";
 #endif /* LIBC_SCCS and not lint */
+__FBSDID("FreeBSD: src/lib/libc/stdio/vfprintf.c,v 1.58 2003/04/14 11:24:53 das Exp");
 #endif
-#include <sys/cdefs.h>
-__FBSDID("$FreeBSD: src/lib/libc/stdio/vfwprintf.c,v 1.23 2004/08/26 06:25:28 des Exp $");
+__FBSDID("$FreeBSD: src/lib/libc/stdio/vfwprintf.c,v 1.12 2003/04/19 23:53:19 das Exp $");
 
 /*
  * Actual wprintf innards.
@@ -68,6 +69,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;
@@ -90,7 +94,7 @@
 	ptrdiff_t *pptrdiffarg;
 	size_t	*psizearg;
 	intmax_t *pintmaxarg;
-#ifndef NO_FLOATING_POINT
+#ifdef FLOATING_POINT
 	double	doublearg;
 	long double longdoublearg;
 #endif
@@ -110,10 +114,9 @@
 };
 
 static int	__sbprintf(FILE *, const wchar_t *, va_list);
-static wint_t	__xfputwc(wchar_t, FILE *);
-static wchar_t	*__ujtoa(uintmax_t, wchar_t *, int, int, const char *, int,
+static wchar_t	*__ujtoa(uintmax_t, wchar_t *, int, int, const wchar_t *, int,
 		    char, const char *);
-static wchar_t	*__ultoa(u_long, wchar_t *, int, int, const char *, int,
+static wchar_t	*__ultoa(u_long, wchar_t *, int, int, const wchar_t *, int,
 		    char, const char *);
 static wchar_t	*__mbsconv(char *, int);
 static void	__find_arguments(const wchar_t *, va_list, union arg **);
@@ -153,36 +156,6 @@
 }
 
 /*
- * Like __fputwc, but handles fake string (__SSTR) files properly.
- * File must already be locked.
- */
-static wint_t
-__xfputwc(wchar_t wc, FILE *fp)
-{
-	static const mbstate_t initial;
-	mbstate_t mbs;
-	char buf[MB_LEN_MAX];
-	struct __suio uio;
-	struct __siov iov;
-	size_t len;
-
-	if ((fp->_flags & __SSTR) == 0)
-		return (__fputwc(wc, fp));
-
-	mbs = initial;
-	if ((len = wcrtomb(buf, wc, &mbs)) == (size_t)-1) {
-		fp->_flags |= __SERR;
-		return (WEOF);
-	}
-	uio.uio_iov = &iov;
-	uio.uio_resid = len;
-	uio.uio_iovcnt = 1;
-	iov.iov_base = buf;
-	iov.iov_len = len;
-	return (__sfvwrite(fp, &uio) != EOF ? (wint_t)wc : WEOF);
-}
-
-/*
  * Macros for converting digits to letters and vice versa
  */
 #define	to_digit(c)	((c) - '0')
@@ -196,7 +169,7 @@
  * use the given digits.
  */
 static wchar_t *
-__ultoa(u_long val, wchar_t *endp, int base, int octzero, const char *xdigs,
+__ultoa(u_long val, wchar_t *endp, int base, int octzero, const wchar_t *xdigs,
 	int needgrp, char thousep, const char *grp)
 {
 	wchar_t *cp = endp;
@@ -274,7 +247,7 @@
 /* Identical to __ultoa, but for intmax_t. */
 static wchar_t *
 __ujtoa(uintmax_t val, wchar_t *endp, int base, int octzero,
-	const char *xdigs, int needgrp, char thousep, const char *grp)
+	const wchar_t *xdigs, int needgrp, char thousep, const char *grp)
 {
 	wchar_t *cp = endp;
 	intmax_t sval;
@@ -352,11 +325,10 @@
 static wchar_t *
 __mbsconv(char *mbsarg, int prec)
 {
-	static const mbstate_t initial;
-	mbstate_t mbs;
 	wchar_t *convbuf, *wcp;
 	const char *p;
 	size_t insize, nchars, nconv;
+	mbstate_t mbs;
 
 	if (mbsarg == NULL)
 		return (NULL);
@@ -370,9 +342,9 @@
 		 * String is not guaranteed to be NUL-terminated. Find the
 		 * number of characters to print.
 		 */
+		memset(&mbs, 0, sizeof(mbs));
 		p = mbsarg;
 		insize = nchars = 0;
-		mbs = initial;
 		while (nchars != (size_t)prec) {
 			nconv = mbrlen(p, MB_CUR_MAX, &mbs);
 			if (nconv == 0 || nconv == (size_t)-1 ||
@@ -397,7 +369,7 @@
 		return (NULL);
 	wcp = convbuf;
 	p = mbsarg;
-	mbs = initial;
+	memset(&mbs, 0, sizeof(mbs));
 	while (insize != 0) {
 		nconv = mbrtowc(wcp, p, insize, &mbs);
 		if (nconv == 0 || nconv == (size_t)-1 || nconv == (size_t)-2)
@@ -430,7 +402,7 @@
 	return (ret);
 }
 
-#ifndef NO_FLOATING_POINT
+#ifdef FLOATING_POINT
 
 #define	dtoa		__dtoa
 #define	freedtoa	__freedtoa
@@ -444,7 +416,7 @@
 
 static int exponent(wchar_t *, int, wchar_t);
 
-#endif /* !NO_FLOATING_POINT */
+#endif /* FLOATING_POINT */
 
 /*
  * The size of the buffer we use as scratch space for integer
@@ -492,7 +464,7 @@
 	wchar_t 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:
@@ -531,7 +503,7 @@
 	int realsz;		/* field size expanded by dprec, sign, etc */
 	int size;		/* size of converted field or string */
 	int prsize;             /* max size of printed field */
-	const char *xdigs;	/* digits for [xX] conversion */
+	const wchar_t *xdigs;	/* digits for [xX] conversion */
 	wchar_t buf[BUF];	/* buffer with space for digits of uintmax_t */
 	wchar_t ox[2];		/* space for 0x hex-prefix */
 	union arg *argtable;	/* args, built due to positional arg */
@@ -551,8 +523,8 @@
 	static wchar_t zeroes[PADSIZE] =
 	 {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
 
-	static const char xdigs_lower[16] = "0123456789abcdef";
-	static const char xdigs_upper[16] = "0123456789ABCDEF";
+	static const wchar_t xdigs_lower[16] = L"0123456789abcdef";
+	static const wchar_t xdigs_upper[16] = L"0123456789ABCDEF";
 
 	/*
 	 * BEWARE, these `goto error' on error, PRINT uses `n2' and
@@ -560,7 +532,7 @@
 	 */
 #define	PRINT(ptr, len)	do {			\
 	for (n3 = 0; n3 < (len); n3++)		\
-		__xfputwc((ptr)[n3], fp);	\
+		__fputwc((ptr)[n3], fp);	\
 } while (0)
 #define	PAD(howmany, with)	do {		\
 	if ((n = (howmany)) > 0) {		\
@@ -643,12 +615,12 @@
 
 	thousands_sep = '\0';
 	grouping = NULL;
-#ifndef NO_FLOATING_POINT
+#ifdef FLOATING_POINT
 	decimal_point = localeconv()->decimal_point;
 #endif
 	convbuf = NULL;
 	/* sorry, fwprintf(read_only_file, L"") returns WEOF, not 0 */
-	if (prepwrite(fp) != 0)
+	if (cantwrite(fp))
 		return (EOF);
 
 	/* optimise fprintf(stderr) (and other unbuffered Unix files) */
@@ -761,7 +733,7 @@
 			}
 			width = n;
 			goto reswitch;
-#ifndef NO_FLOATING_POINT
+#ifdef FLOATING_POINT
 		case 'L':
 			flags |= LONGDBL;
 			goto rflag;
@@ -823,7 +795,8 @@
 			}
 			base = 10;
 			goto number;
-#ifndef NO_FLOATING_POINT
+#ifdef FLOATING_POINT
+#ifdef HEXFLOAT
 		case 'a':
 		case 'A':
 			if (ch == 'a') {
@@ -835,10 +808,12 @@
 				xdigs = xdigs_upper;
 				expchar = 'P';
 			}
-			if (prec >= 0)
-				prec++;
+			/*
+			 * 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 =
 				    __hldtoa(fparg.ldbl, xdigs, prec,
 				        &expt, &signflag, &dtoaend);
@@ -848,16 +823,12 @@
 				    __hdtoa(fparg.dbl, xdigs, prec,
 				        &expt, &signflag, &dtoaend);
 			}
-			if (prec < 0)
-				prec = dtoaend - dtoaresult;
-			if (expt == INT_MAX)
-				ox[1] = '\0';
 			if (convbuf != NULL)
 				free(convbuf);
-			ndig = dtoaend - dtoaresult;
 			cp = convbuf = __mbsconv(dtoaresult, -1);
 			freedtoa(dtoaresult);
-			goto fp_common;
+			goto fp_begin;
+#endif
 		case 'e':
 		case 'E':
 			expchar = ch;
@@ -896,7 +867,6 @@
 			ndig = dtoaend - dtoaresult;
 			cp = convbuf = __mbsconv(dtoaresult, -1);
 			freedtoa(dtoaresult);
-fp_common:
 			if (signflag)
 				sign = '-';
 			if (expt == INT_MAX) {	/* inf or nan */
@@ -961,7 +931,7 @@
 					lead = expt;
 			}
 			break;
-#endif /* !NO_FLOATING_POINT */
+#endif /* FLOATING_POINT */
 		case 'n':
 			/*
 			 * Assignment-like behavior is specified if the
@@ -1139,7 +1109,7 @@
 		realsz = dprec > size ? dprec : size;
 		if (sign)
 			realsz++;
-		if (ox[1])
+		else if (ox[1])
 			realsz += 2;
 
 		prsize = width > realsz ? width : realsz;
@@ -1153,10 +1123,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);
 		}
@@ -1169,7 +1138,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 */
@@ -1232,7 +1201,6 @@
 	}
 done:
 error:
-	va_end(orgap);
 	if (convbuf != NULL)
 		free(convbuf);
 	if (__sferror(fp))
@@ -1269,7 +1237,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)
 
@@ -1311,8 +1279,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).
@@ -1363,7 +1330,7 @@
 			}
 			width = n;
 			goto reswitch;
-#ifndef NO_FLOATING_POINT
+#ifdef FLOATING_POINT
 		case 'L':
 			flags |= LONGDBL;
 			goto rflag;
@@ -1410,9 +1377,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':
@@ -1423,7 +1392,7 @@
 			else
 				ADDTYPE(T_DOUBLE);
 			break;
-#endif /* !NO_FLOATING_POINT */
+#endif /* FLOATING_POINT */
 		case 'n':
 			if (flags & INTMAXT)
 				ADDTYPE(TP_INTMAXT);
@@ -1543,7 +1512,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;
@@ -1579,28 +1548,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(wchar_t *p0, int exp, wchar_t fmtch)
@@ -1637,4 +1604,4 @@
 	}
 	return (p - p0);
 }
-#endif /* !NO_FLOATING_POINT */
+#endif /* FLOATING_POINT */