1
0
mirror of https://git.FreeBSD.org/src.git synced 2024-12-14 10:09:48 +00:00

1) Oops! Insert again if (n == 0) return 0.

Final word is Bruce's quote:

C9x specifies the BSD4.4-Lite behaviour:

       [#3] ...   Thus,  the
       null-terminated  output  has  been completely written if and
       only if the returned value is less than n.

It means that if we not have any null-terminated output as for n == 0
we can't return value less than n, so we forced to return value
equal to n i.e. 0

The next good thing is glibc compatibility, of course.

2) Do check for too big n in machine-independent way.
3) Minor optimization assuming EOF is < 0
This commit is contained in:
Andrey A. Chernov 1997-12-24 20:24:08 +00:00
parent d83662583c
commit e0b123f6d0
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=31979
2 changed files with 10 additions and 10 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.6 1997/12/24 12:31:31 ache Exp $";
"$Id: snprintf.c,v 1.7 1997/12/24 14:32:39 ache Exp $";
#endif /* LIBC_SCCS and not lint */
#include <stdio.h>
@ -48,6 +48,7 @@ static const char rcsid[] =
#else
#include <varargs.h>
#endif
#include <limits.h>
#if __STDC__
int
@ -65,9 +66,10 @@ snprintf(str, n, fmt, va_alist)
va_list ap;
FILE f;
if ((int)n < 1)
if (n == 0)
return (0);
if (--n > INT_MAX)
return (EOF);
n--;
#if __STDC__
va_start(ap, fmt);
#else
@ -80,7 +82,5 @@ snprintf(str, n, fmt, va_alist)
ret = vfprintf(&f, fmt, ap);
*f._p = 0;
va_end(ap);
if (ret == EOF)
return (ret);
return (ret > (int)n ? n : ret);
}

View File

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