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]