How to predict size of outcome of vsprintf?
Mike Haertel
mike at thor.acc.stolaf.edu
Thu Mar 23 04:03:32 AEST 1989
In article <16491 at mimsy.UUCP> chris at mimsy.UUCP (Chris Torek) writes:
>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 have been writing just exactly such a stdio package lately.
The only things I have left to do are printf() and friends, and scanf()
and friends. The reason I am writing it is I am experimenting with
standalone programs on my machine, and I wanted a stdio library. It
also works on Unix, that is where I test it. Incidentally the stuff
that is written so far all conforms to the pANS. When I finish it
I will make it available to anyone who is interested.
The way it works is like this:
typedef struct _FILE {
...
struct __iofuncs *__iofuncs; /* Vector to actual I/O functions. */
void *__info; /* Opaque info for I/O functions. */
...
} FILE;
When fopen() is called it loops through all available I/O function tables,
trying the open() entry in each one (iofuncs->open() just gets the same
arguments as fopen()); the first to return non-NULL has that value
installed in FILE->__info.
Then, for example, the actual write call in fflush() looks like this:
(*fp->__iofuncs->write)(fp->__info, buf, len);
There will be a library entry point to install user-defined I/O
function tables.
I haven't decided exactly how to do printf() yet; I would like to
have a function switch table based on format letters so I can have
an extensible printf(). I will probably try that tonight. I will
not be doing the floating point stuff in the near future as the
standalone environment I am using is a 68010, and I have not yet
written the floating point run time support for the compiler, and
I really don't feel like doing that any time soon, I don't use
floating point much . . .
I got the idea for the extensible printf() from the manual page
describing the FIO package and in particular fmtinstall() in
the Ninth Edition manual. Whoever you are at Bell Labs
who invented that, thanks, it's a cool idea.
--
Mike Haertel <mike at stolaf.edu>
In Hell they run VMS.
More information about the Comp.unix.wizards
mailing list