[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [leafnode-list] [ANNOUNCE] First Leafnode-2.0 beta version



I wrote:

[replacement for vsnprintf()]

> Mark Brown wrote:
> > The BSDs will have an implementation - would their license not be
> > liberal enough?
> 
> Sure. Leafnode is released under a BSDish license anyway. I'll have a
> look ASAP.

After having looked I remember why I didn't choose this apparently
obvious solution before. It has to do with the way snprintf() and
vsnprintf() are implemented under BSD (I looked at NetBSD and OpenBSD).
Both implementations seem to use internal quirks of the FILE stream
which are not available under Linux.

Basically, snprintf() from OpenBSD looks like this:

snprintf(char *str, size_t n, char const *fmt, ...)
{
        int ret;
        va_list ap;
        FILE f;

        if ((int)n < 1)
                return (-1);
        va_start(ap, fmt);
        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;
        va_end(ap);
        return (ret);
}

Without knowing anything about the internals of *BSD, it appears to
me that by setting certain flags for FILE f vfprintf() seems to write
something into a kind of buffer. How this is achieved is mysterious
to me. Trying to compile this code under Linux will give a lot of
error messages like

vsnprintf.c:71: `__SWR' undeclared (first use in this function)
vsnprintf.c:71: `__SSTR' undeclared (first use in this function)
vsnprintf.c:72: structure has no member named `_bf'
vsnprintf.c:72: structure has no member named `_p'
vsnprintf.c:73: structure has no member named `_bf'
vsnprintf.c:73: structure has no member named `_w'
vsnprintf.c:75: structure has no member named `_p'

I remember now that I run into the same problems with snprintf()
before and then decided to code my own version of snprintf().

--Cornelius.

-- 
/* Cornelius Krasel, U Wuerzburg, Dept. of Pharmacology, Versbacher Str. 9 */
/* D-97078 Wuerzburg, Germany   email: phak004@xxxxxxxxxxxxxxxxxxxxxx  SP4 */
/* "Science is the game we play with God to find out what His rules are."  */

-- 
leafnode-list@xxxxxxxxxxxxxxxxxxxxxxxxxxxx -- mailing list for leafnode
To unsubscribe, send mail with "unsubscribe" in the subject to the list