my_printf calling printf

D. Richard Hipp drh at duke.cs.duke.edu
Fri Mar 1 02:52:19 AEST 1991


In article <1991Feb27.234041.12579 at athena.mit.edu> scs at adam.mit.edu writes:
>A simple solution, which ought to be more widespread by now, is
>to provide
>	snprintf(char *buf, int buflen, char *fmt, ...)
>and
>	vsnprintf(char *buf, int buflen, char *fmt, va_list argp)
>which are just like sprintf and vsprintf except that they LET YOU
>SPECIFY THE BUFFER SIZE so that it can't be overflowed (what a
>concept).  [Stuff omitted]

>For an even better solution, Wouldn't It Be Loverly if there were a
>	char *saprintf(char *fmt, ...)
>and a companion
>	char *vsaprintf(char *fmt, va_list argp)
>which return a pointer to malloc'ed memory just large enough for,
>and containing, the resulting, formatted string?

I have source code for the four functions described above, plus several
other variations on the printf theme.  In particular, I have a package
that implements the following:

    snprintf       Print to a string of no more than N characters.
    mprintf        Get memory from malloc to hold a string.
    xprintf        Call a function (possibly several times) to dispose
                     of the output.
    nprintf        Generate no output, but return the number of characters
                     that would have been output if the regular printf
                     had been called instead.

Source code to these is available upon request to drh at cs.duke.edu.



More information about the Comp.lang.c mailing list