Return back to BSD snprintf semantics which recent C9x standard adopts

instead of Singe Unix, thanx Bruce for explaining, I am not realize
standards war was there.

But now, fix n == 0 case to not return error and fix check for too
big n.

Things left to do: check for overflow in arguments.
This commit is contained in:
Andrey A. Chernov 1997-12-24 23:02:47 +00:00
parent e0b123f6d0
commit 6e690ad4ca
3 changed files with 23 additions and 21 deletions

View File

@ -39,7 +39,7 @@
static char sccsid[] = "@(#)snprintf.c 8.1 (Berkeley) 6/4/93";
#endif
static const char rcsid[] =
"$Id: snprintf.c,v 1.7 1997/12/24 14:32:39 ache Exp $";
"$Id: snprintf.c,v 1.8 1997/12/24 20:24:05 ache Exp $";
#endif /* LIBC_SCCS and not lint */
#include <stdio.h>
@ -65,10 +65,12 @@ snprintf(str, n, fmt, va_alist)
int ret;
va_list ap;
FILE f;
int on;
if (n == 0)
return (0);
if (--n > INT_MAX)
on = n;
if (n > 0)
n--;
if (n > INT_MAX)
return (EOF);
#if __STDC__
va_start(ap, fmt);
@ -80,7 +82,8 @@ snprintf(str, n, fmt, va_alist)
f._bf._base = f._p = (unsigned char *)str;
f._bf._size = f._w = n;
ret = vfprintf(&f, fmt, ap);
*f._p = 0;
if (on > 0)
*f._p = '\0';
va_end(ap);
return (ret > (int)n ? n : ret);
return (ret);
}

View File

@ -39,7 +39,7 @@
static char sccsid[] = "@(#)vfprintf.c 8.1 (Berkeley) 6/4/93";
#endif
static const char rcsid[] =
"$Id: vfprintf.c,v 1.13 1997/12/19 21:59:22 bde Exp $";
"$Id: vfprintf.c,v 1.14 1997/12/24 13:47:13 ache Exp $";
#endif /* LIBC_SCCS and not lint */
/*
@ -643,18 +643,14 @@ fp_begin: if (prec == -1)
break;
#endif /* FLOATING_POINT */
case 'n':
n = ret;
if ((fp->_flags & __SSTR) &&
fp->_bf._size < n)
n = fp->_bf._size;
if (flags & QUADINT)
*GETARG(quad_t *) = n;
*GETARG(quad_t *) = ret;
else if (flags & LONGINT)
*GETARG(long *) = n;
*GETARG(long *) = ret;
else if (flags & SHORTINT)
*GETARG(short *) = n;
*GETARG(short *) = ret;
else
*GETARG(int *) = n;
*GETARG(int *) = ret;
continue; /* no output */
case 'O':
flags |= LONGINT;

View File

@ -39,7 +39,7 @@
static char sccsid[] = "@(#)vsnprintf.c 8.1 (Berkeley) 6/4/93";
#endif
static const char rcsid[] =
"$Id: vsnprintf.c,v 1.7 1997/12/24 14:32:40 ache Exp $";
"$Id: vsnprintf.c,v 1.8 1997/12/24 20:24:08 ache Exp $";
#endif /* LIBC_SCCS and not lint */
#include <stdio.h>
@ -54,16 +54,19 @@ vsnprintf(str, n, fmt, ap)
{
int ret;
FILE f;
int on;
if (n == 0)
return (0);
if (--n > INT_MAX)
on = n;
if (n > 0)
n--;
if (n > INT_MAX)
return (EOF);
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);
*f._p = 0;
return (ret > (int)n ? n : ret);
if (on > 0)
*f._p = '\0';
return (ret);
}