How to predict size of outcome of vsprintf?

Miles Bader bader+ at andrew.cmu.edu
Thu Mar 23 11:55:48 AEST 1989


chris at mimsy.UUCP (Chris Torek) writes:
> 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.
> 
> This is fairly difficult to implement (one wants coroutines for something
> like this).  Instead, why not allow stdio streams to `read' and `write'
> via user-provided functions?  Everyone knows how to use read() and
> write(); simply provide your own write() that mallocs space, or prints
> in a window, or whatever.

I agree that stdio should have been written with user-provided read/write
routines.  But I disagree that a restartable sprintf would be difficult.  The
code needn't be much different at all from a putc based printf (the only
difference is that state changes inside the routine are passed out of it).

One reason why I like the restartable printf idea is that it's lower overhead
and less complex than creating/destroying a stream just for the purpose of
implementing sprintf.  I just wrote my own stdio-type package; I don't want
to have to go though the overhead of passing things through a stdio stream
just to get put into MY stream (not that it would be that high; but it's the
principle of the thing).

-Miles



More information about the Comp.unix.wizards mailing list