How to predict size of outcome of vsprintf?

Mike Rendell michael at garfield.MUN.EDU
Fri Mar 24 01:29:49 AEST 1989


In article <wY9m4Hy00UkaI=3Ihb at andrew.cmu.edu> bader+ at andrew.cmu.edu
(Miles Bader) writes:
>What I'd really like would be a new function that could act as a basis
>for all the other printf functions, and make it possible to emulate
>them in nice ways.
>
>I would make it a varargs function that takes a bounded output buffer
>and is restartable at the point where it stops due to running into the
>end of the buffer.

I wrote a version of doprnt() which does something like this.  Instead
of taking a FILE * argument it's passed a function pointer and a
"generic argument" (void/char *).  new_doprnt() fills up a stack buffer
and calls the function (which takes the same arguments as fwrite()) to
deal with it.  This makes printf() and a bounds checking sprintf() (or
a re-alloc()ing sprintf()) easy to write as well as providing a simple
method for programmers to hook into it.  I've used it in some screen
based programs to page output and in some stand alone programs to do
output to a console.  The only draw back is that it doesn't handle
%[egf] - but who needs those anyway :-).

Mike Rendell				Department of Computer Science
michael at garfield.mun.edu		Memorial University of Newfoundland
uunet!garfield!michael			St. John's, Nfld., Canada
 (709) 737-4550				A1C 5S7



More information about the Comp.unix.wizards mailing list