Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!decwrl!mcnc!duke!drh From: drh@duke.cs.duke.edu (D. Richard Hipp) Newsgroups: comp.lang.c Subject: Re: my_printf calling printf Message-ID: <667756338@romeo.cs.duke.edu> Date: 28 Feb 91 15:52:19 GMT References: <1991Feb26.101626.21779@fwi.uva.nl> <31530036@hpcvia.CV.HP.COM> <1991Feb27.234041.12579@athena.mit.edu> Organization: Duke University Computer Science Dept.; Durham, N.C. Lines: 30 In article <1991Feb27.234041.12579@athena.mit.edu> scs@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@cs.duke.edu.