Skip site navigation (1)Skip section navigation (2)
Date:      16 Jun 2001 22:46:57 +0200
From:      Assar Westerlund <assar@FreeBSD.ORG>
To:        Bruce Evans <bde@zeta.org.au>
Cc:        freebsd-audit@FreeBSD.ORG
Subject:   Re: *printf simplifications?
Message-ID:  <5l8zistany.fsf@assaris.sics.se>
In-Reply-To: Bruce Evans's message of "Sat, 16 Jun 2001 18:08:24 %2B1000 (EST)"
References:  <Pine.BSF.4.21.0106161749280.90941-100000@besplex.bde.org>

next in thread | previous in thread | raw e-mail | index | archive | help

--=-=-=

Bruce Evans <bde@zeta.org.au> writes:
> >  {
> > -	int ret;
> > -	FILE f;
> > -
> 
> Style bug: now there is no blank line after the (null) auto declarations.

fixed.

> > +	return (vsnprintf(str, INT_MAX, fmt, ap));
> >  }
> 
> vsprintf() can't call vsnprintf() due to namespace issues (the former is
> in C[89-94] but the latter is only in C99).

I added a __vsnprintf, see patch below.

> The current duplication of code for the other functions seems to be just a
> micro-(opt|pess)imization.

I would vote for pessimization.  If it doesn't matter I rather have
the as much common code as possible.

/assar

--=-=-=
Content-Type: text/x-patch
Content-Disposition: attachment; filename=libcd

Index: asprintf.c
===================================================================
RCS file: /home/ncvs/src/lib/libc/stdio/asprintf.c,v
retrieving revision 1.7
diff -u -w -r1.7 asprintf.c
--- asprintf.c	2001/02/11 22:06:39	1.7
+++ asprintf.c	2001/06/16 20:46:32
@@ -54,30 +54,13 @@
 {
 	int ret;
 	va_list ap;
-	FILE f;
 
 #if __STDC__
 	va_start(ap, fmt);
 #else
 	va_start(ap);
 #endif
-	f._file = -1;
-	f._flags = __SWR | __SSTR | __SALC;
-	f._bf._base = f._p = (unsigned char *)malloc(128);
-	if (f._bf._base == NULL) {
-		*str = NULL;
-		errno = ENOMEM;
-		return (-1);
-	}
-	f._bf._size = f._w = 127;		/* Leave room for the NULL */
-	ret = __vfprintf(&f, fmt, ap);		/* Use unlocked __vfprintf */
-	*f._p = '\0';
+	ret = vasprintf(str, fmt, ap);
 	va_end(ap);
-	f._bf._base = reallocf(f._bf._base, f._bf._size + 1);
-	if (f._bf._base == NULL) {
-		errno = ENOMEM;
-		ret = -1;
-	}
-	*str = (char *)f._bf._base;
 	return (ret);
 }
Index: local.h
===================================================================
RCS file: /home/ncvs/src/lib/libc/stdio/local.h,v
retrieving revision 1.4
diff -u -w -r1.4 local.h
--- local.h	2001/06/04 12:36:06	1.4
+++ local.h	2001/06/16 20:46:32
@@ -65,7 +65,7 @@
 extern int	__sflags __P((const char *, int *));
 extern int	__ungetc __P((int, FILE *));
 extern int	__vfprintf __P((FILE *, const char *, _BSD_VA_LIST_));
-
+extern int	__vsnprintf __P((char *, size_t, const char *, _BSD_VA_LIST_));
 extern int	__sdidinit;
 
 
Index: snprintf.c
===================================================================
RCS file: /home/ncvs/src/lib/libc/stdio/snprintf.c,v
retrieving revision 1.14
diff -u -w -r1.14 snprintf.c
--- snprintf.c	2001/06/16 05:37:57	1.14
+++ snprintf.c	2001/06/16 20:46:32
@@ -64,30 +64,15 @@
 	va_dcl
 #endif
 {
-	size_t on;
-	int ret;
 	va_list ap;
-	FILE f;
+	int ret;
 
-	on = n;
-	if (n != 0)
-		n--;
-	if (n > INT_MAX)
-		n = INT_MAX;
 #if __STDC__
 	va_start(ap, fmt);
 #else
 	va_start(ap);
 #endif
-	f._file = -1;
-	f._flags = __SWR | __SSTR;
-	f._bf._base = f._p = (unsigned char *)str;
-	f._bf._size = f._w = n;
-	ret = __vfprintf(&f, fmt, ap);
-	if (on > 0)
-		*f._p = '\0';
+	ret = __vsnprintf(str, n, fmt, ap);
 	va_end(ap);
-	if (str == NULL)
-		free(f._bf._base);
 	return (ret);
 }
Index: sprintf.c
===================================================================
RCS file: /home/ncvs/src/lib/libc/stdio/sprintf.c,v
retrieving revision 1.8
diff -u -w -r1.8 sprintf.c
--- sprintf.c	2001/06/16 05:37:57	1.8
+++ sprintf.c	2001/06/16 20:46:32
@@ -63,21 +63,13 @@
 {
 	int ret;
 	va_list ap;
-	FILE f;
 
-	f._file = -1;
-	f._flags = __SWR | __SSTR;
-	f._bf._base = f._p = (unsigned char *)str;
-	f._bf._size = f._w = INT_MAX;
 #if __STDC__
 	va_start(ap, fmt);
 #else
 	va_start(ap);
 #endif
-	ret = __vfprintf(&f, fmt, ap);
+	ret = vsprintf(str, fmt, ap);
 	va_end(ap);
-	*f._p = 0;
-	if (str == NULL)
-		free(f._bf._base);
 	return (ret);
 }
Index: vsnprintf.c
===================================================================
RCS file: /home/ncvs/src/lib/libc/stdio/vsnprintf.c,v
retrieving revision 1.14
diff -u -w -r1.14 vsnprintf.c
--- vsnprintf.c	2001/06/16 05:37:57	1.14
+++ vsnprintf.c	2001/06/16 20:46:32
@@ -47,7 +47,7 @@
 #include "local.h"
 
 int
-vsnprintf(str, n, fmt, ap)
+__vsnprintf(str, n, fmt, ap)
 	char *str;
 	size_t n;
 	const char *fmt;
@@ -72,4 +72,15 @@
 	if (str == NULL)
 		free(f._bf._base);
 	return (ret);
+}
+
+int
+vsnprintf(str, n, fmt, ap)
+	char *str;
+	size_t n;
+	const char *fmt;
+	_BSD_VA_LIST_ ap;
+{
+
+	return (__vsnprintf(str, n, fmt, ap));
 }
Index: vsprintf.c
===================================================================
RCS file: /home/ncvs/src/lib/libc/stdio/vsprintf.c,v
retrieving revision 1.8
diff -u -w -r1.8 vsprintf.c
--- vsprintf.c	2001/06/16 05:37:57	1.8
+++ vsprintf.c	2001/06/16 20:46:32
@@ -52,16 +52,6 @@
 	const char *fmt;
 	_BSD_VA_LIST_ ap;
 {
-	int ret;
-	FILE f;
 
-	f._file = -1;
-	f._flags = __SWR | __SSTR;
-	f._bf._base = f._p = (unsigned char *)str;
-	f._bf._size = f._w = INT_MAX;
-	ret = __vfprintf(&f, fmt, ap);
-	*f._p = 0;
-	if (str == NULL)
-		free(f._bf._base);
-	return (ret);
+	return (vsnprintf(str, INT_MAX, fmt, ap));
 }

--=-=-=--

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-audit" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?5l8zistany.fsf>