Xref: utzoo comp.unix.wizards:15157 comp.lang.c:17111 Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ukma!xanth!nic.MR.NET!thor.acc.stolaf.edu!mike From: mike@thor.acc.stolaf.edu (Mike Haertel) Newsgroups: comp.unix.wizards,comp.lang.c Subject: Re: How to predict size of outcome of vsprintf? Message-ID: <1675@thor.acc.stolaf.edu> Date: 22 Mar 89 18:03:32 GMT References: <993@etnibsd.UUCP> <9872@smoke.BRL.MIL> <28831@bu-cs.BU.EDU> <16491@mimsy.UUCP> Reply-To: mike@stolaf.edu (Mike Haertel) Followup-To: comp.unix.wizards Organization: St. Olaf College, Northfield, MN Lines: 53 In article <16491@mimsy.UUCP> chris@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 In Hell they run VMS.