diff options
author | Todd C. Miller <millert@cvs.openbsd.org> | 1998-01-12 06:14:33 +0000 |
---|---|---|
committer | Todd C. Miller <millert@cvs.openbsd.org> | 1998-01-12 06:14:33 +0000 |
commit | cf7ccc9cb187d0627ce9f7a884e9de25b123975e (patch) | |
tree | 67db59d2fcf1a3ffefdfa49f40c1a931b6137823 /lib | |
parent | bec95d96c1daca750433ebfaa73a057be3022cf6 (diff) |
Based on some FreeBSD changes:
For *s*printf, set f._file to -1 like the comments in stdio.h say.
Use '\0', not 0, where appropriate.
Don't error out on size of '0' for v?snprintf().
Diffstat (limited to 'lib')
-rw-r--r-- | lib/libc/stdio/asprintf.c | 5 | ||||
-rw-r--r-- | lib/libc/stdio/printf.3 | 17 | ||||
-rw-r--r-- | lib/libc/stdio/snprintf.c | 14 | ||||
-rw-r--r-- | lib/libc/stdio/sprintf.c | 5 | ||||
-rw-r--r-- | lib/libc/stdio/vasprintf.c | 5 | ||||
-rw-r--r-- | lib/libc/stdio/vsnprintf.c | 14 | ||||
-rw-r--r-- | lib/libc/stdio/vsprintf.c | 5 |
7 files changed, 34 insertions, 31 deletions
diff --git a/lib/libc/stdio/asprintf.c b/lib/libc/stdio/asprintf.c index 1e5f00fa1ab..a2b22a48f80 100644 --- a/lib/libc/stdio/asprintf.c +++ b/lib/libc/stdio/asprintf.c @@ -1,4 +1,4 @@ -/* $OpenBSD: asprintf.c,v 1.1 1997/11/29 19:54:48 millert Exp $ */ +/* $OpenBSD: asprintf.c,v 1.2 1998/01/12 06:14:30 millert Exp $ */ /* * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com> @@ -31,7 +31,7 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: asprintf.c,v 1.1 1997/11/29 19:54:48 millert Exp $"; +static char rcsid[] = "$OpenBSD: asprintf.c,v 1.2 1998/01/12 06:14:30 millert Exp $"; #endif /* LIBC_SCCS and not lint */ #include <stdio.h> @@ -62,6 +62,7 @@ asprintf(str, fmt, va_alist) #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) { diff --git a/lib/libc/stdio/printf.3 b/lib/libc/stdio/printf.3 index f43672a901e..a6ff5ecb6f0 100644 --- a/lib/libc/stdio/printf.3 +++ b/lib/libc/stdio/printf.3 @@ -1,4 +1,4 @@ -.\" $OpenBSD: printf.3,v 1.5 1997/11/29 19:54:49 millert Exp $ +.\" $OpenBSD: printf.3,v 1.6 1998/01/12 06:14:30 millert Exp $ .\" .\" Copyright (c) 1990, 1991, 1993 .\" The Regents of the University of California. All rights reserved. @@ -117,12 +117,6 @@ the number of characters printed (not including the trailing .Ql \e0 used to end output to strings). -.Fn Snprintf -and -.Fn vsnprintf -will return -1 if the -.Fa size -parameter is 0. .Pp .Fn Asprintf and @@ -130,14 +124,11 @@ and return a pointer to a buffer sufficiently large to hold the string in the .Fa ret -argument; +argument. This pointer should be passed to .Xr free 3 to release the allocated storage when it is no longer needed. -If sufficient space cannot be allocated, -.Fn asprintf -and -.Fn vasprintf +If sufficient space cannot be allocated, these functions will return -1 and set .Fa ret to be a NULL pointer. @@ -667,7 +658,7 @@ this is often impossible to assure. For safety, programmers should use the .Fn snprintf and -.Fn vasprintf +.Fn asprintf family of interfaces instead. Unfortunately, the .Fn snprintf diff --git a/lib/libc/stdio/snprintf.c b/lib/libc/stdio/snprintf.c index 2afabe725a0..9523431bf00 100644 --- a/lib/libc/stdio/snprintf.c +++ b/lib/libc/stdio/snprintf.c @@ -35,9 +35,10 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: snprintf.c,v 1.4 1997/11/29 19:28:29 millert Exp $"; +static char rcsid[] = "$OpenBSD: snprintf.c,v 1.5 1998/01/12 06:14:31 millert Exp $"; #endif /* LIBC_SCCS and not lint */ +#include <limits.h> #include <stdio.h> #ifdef __STDC__ #include <stdarg.h> @@ -60,18 +61,21 @@ snprintf(str, n, fmt, va_alist) va_list ap; FILE f; - if ((int)n < 1) - return (-1); + /* While snprintf(3) specifies size_t stdio uses an int internally */ + if (n > INT_MAX) + n = INT_MAX; #ifdef __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 - 1; + f._bf._size = f._w = n ? n - 1 : 0; ret = vfprintf(&f, fmt, ap); - *f._p = 0; + if (n) + *f._p = 0; va_end(ap); return (ret); } diff --git a/lib/libc/stdio/sprintf.c b/lib/libc/stdio/sprintf.c index 16b99a27f97..994192f04bc 100644 --- a/lib/libc/stdio/sprintf.c +++ b/lib/libc/stdio/sprintf.c @@ -35,7 +35,7 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: sprintf.c,v 1.3 1997/07/25 20:30:11 mickey Exp $"; +static char rcsid[] = "$OpenBSD: sprintf.c,v 1.4 1998/01/12 06:14:31 millert Exp $"; #endif /* LIBC_SCCS and not lint */ #include <stdio.h> @@ -61,6 +61,7 @@ sprintf(str, fmt, va_alist) 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; @@ -71,6 +72,6 @@ sprintf(str, fmt, va_alist) #endif ret = vfprintf(&f, fmt, ap); va_end(ap); - *f._p = 0; + *f._p = '\0'; return (ret); } diff --git a/lib/libc/stdio/vasprintf.c b/lib/libc/stdio/vasprintf.c index e11f33b7dff..5e8fa0ec932 100644 --- a/lib/libc/stdio/vasprintf.c +++ b/lib/libc/stdio/vasprintf.c @@ -1,4 +1,4 @@ -/* $OpenBSD: vasprintf.c,v 1.1 1997/11/29 19:54:47 millert Exp $ */ +/* $OpenBSD: vasprintf.c,v 1.2 1998/01/12 06:14:32 millert Exp $ */ /* * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com> @@ -31,7 +31,7 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: vasprintf.c,v 1.1 1997/11/29 19:54:47 millert Exp $"; +static char rcsid[] = "$OpenBSD: vasprintf.c,v 1.2 1998/01/12 06:14:32 millert Exp $"; #endif /* LIBC_SCCS and not lint */ #include <stdio.h> @@ -47,6 +47,7 @@ vasprintf(str, fmt, ap) int ret; FILE f; + f._file = -1; f._flags = __SWR | __SSTR | __SALC; f._bf._base = f._p = (unsigned char *)malloc(128); if (f._bf._base == NULL) { diff --git a/lib/libc/stdio/vsnprintf.c b/lib/libc/stdio/vsnprintf.c index 85e456290b2..b965b146442 100644 --- a/lib/libc/stdio/vsnprintf.c +++ b/lib/libc/stdio/vsnprintf.c @@ -35,9 +35,10 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: vsnprintf.c,v 1.3 1997/11/29 19:28:30 millert Exp $"; +static char rcsid[] = "$OpenBSD: vsnprintf.c,v 1.4 1998/01/12 06:14:32 millert Exp $"; #endif /* LIBC_SCCS and not lint */ +#include <limits.h> #include <stdio.h> int @@ -50,12 +51,15 @@ vsnprintf(str, n, fmt, ap) int ret; FILE f; - if ((int)n < 1) - return (-1); + /* While snprintf(3) specifies size_t stdio uses an int internally */ + if (n > INT_MAX) + n = INT_MAX; + f._file = -1; f._flags = __SWR | __SSTR; f._bf._base = f._p = (unsigned char *)str; - f._bf._size = f._w = n - 1; + f._bf._size = f._w = n ? n - 1 : 0; ret = vfprintf(&f, fmt, ap); - *f._p = 0; + if (n) + *f._p = '\0'; return (ret); } diff --git a/lib/libc/stdio/vsprintf.c b/lib/libc/stdio/vsprintf.c index 09c34ba71c9..665d9e1de3e 100644 --- a/lib/libc/stdio/vsprintf.c +++ b/lib/libc/stdio/vsprintf.c @@ -35,7 +35,7 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: vsprintf.c,v 1.2 1996/08/19 08:33:14 tholo Exp $"; +static char rcsid[] = "$OpenBSD: vsprintf.c,v 1.3 1998/01/12 06:14:32 millert Exp $"; #endif /* LIBC_SCCS and not lint */ #include <stdio.h> @@ -49,10 +49,11 @@ vsprintf(str, fmt, 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; + *f._p = '\0'; return (ret); } |