>From 
>http://www.openbsd.org/cgi-bin/cvsweb.cgi/src/lib/libc/stdio/vsnprintf.c?rev=1.5&content-type=text/x-cvsweb-markup,
> ...

[..]
#if defined(LIBC_SCCS) && !defined(lint)
static char rcsid[] = "$OpenBSD: vsnprintf.c,v 1.5 1998/07/27 02:28:28 millert Exp $";
#endif /* LIBC_SCCS and not lint */

#include <limits.h>
#include <stdio.h>

int
vsnprintf(str, n, fmt, ap)
        char *str;
        size_t n;
        const char *fmt;
        _BSD_VA_LIST_ ap;
{
        int ret;
        char dummy;
        FILE f;

        /* While snprintf(3) specifies size_t stdio uses an int internally */
        if (n > INT_MAX)
                n = INT_MAX;
        /* Stdio internals do not deal correctly with zero length buffer */
        if (n == 0) {
                str = &dummy;
                n = 1;
        }
        f._file = -1;
        f._flags = __SWR | __SSTR;
        f._bf._base = f._p = (unsigned char *)str;
        f._bf._size = f._w = n - 1;
        ret = vfprintf(&f, fmt, ap);
        *f._p = 0;
        return (ret);
}

Look here if you want vfprintf:

http://www.openbsd.org/cgi-bin/cvsweb.cgi/src/lib/libc/stdio/vfprintf.c?rev=1.17&content-type=text/x-cvsweb-markup


Penned by Christoph Egger on Sun, Jan 27, 2002 at 02:00:59PM +0100, we have:
| On Sun, 27 Jan 2002, Andreas Beck wrote:
| 
| > Christoph Egger <[EMAIL PROTECTED]> wrote:
| >
| > > > Will it write a trailing \0 ? If not, that should be generated, to avoid
| > > snip from the manual page:
| > >
| > >    Return value
| > >        These  functions  return  the number of characters printed
| > >        (not including the trailing `\0' used  to  end  output  to
| > >        strings).   snprintf  and vsnprintf do not write more than
| > >        size bytes (including the trailing '\0'), and return -1 if
| > >        the  output  was truncated due to this limit.  (Thus until
| > >        glibc 2.0.6. Since glibc 2.1 these  functions  follow  the
| > >        C99  standard and return the number of characters (exclud
| > >        ing the trailing '\0') which would have  been  written  to
| > >        the final string if enough space had been available.)
| >
| > That's the point. I cannot derive from that, if snprintf will
| > terminate the string, if an overflow occurs, without leaving a little
| > doubt. Does anyone have the C99 standard ready and can confirm, that
| > snprintf is supposed to terminate the string in _any_ case?
| >
| > If that is not the case or unsure, I would suggest to explicitly
| > terminate the string after every usage of snprintf using something
| > like
| >
| > #define TERMINATE_ARRAY(x) x[sizeof(x)-1]='\0'
| > #define TERMINATE_POINTER(x,len) x[len-1]='\0'
| 
| 
| AFAIK sprintf() and snprintf() are nothing other than this:
| 
| int sprintf(char *str, const char *format, ...)
| {
|       int rc;
|       va_list ap;
| 
|       va_start(ap, format);
| 
|       rc = vsnprintf(str, 0xFFFFFFFF, format, ap);
| 
|       va_end(ap);
| 
|       return rc;
| }
| 
| int snprintf(char *str, size_t size, const  char  *format, ...)
| {
|       int rc;
|       va_list ap;
| 
|       va_start(ap, format);
| 
|       rc = vsnprintf(str, size, format, ap);
| 
|       va_end(ap);
| 
|       return rc;
| }
| 
| 
| 
| So, the question is, what does vsnprintf() ?
| 
| 
| 
| CU,
| 
| Christoph Egger
| E-Mail: [EMAIL PROTECTED]

-- 
Todd Fries .. [EMAIL PROTECTED]

Reply via email to