Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!iuvax!rutgers!rochester!pt.cs.cmu.edu!andrew.cmu.edu!bader+ From: bader+@andrew.cmu.edu (Miles Bader) Newsgroups: comp.unix.wizards,comp.lang.c Subject: Re: How to predict size of outcome of vsprintf? Message-ID: <4Y9mcHy00UkaI=3KkV@andrew.cmu.edu> Date: 22 Mar 89 05:07:31 GMT References: <993@etnibsd.UUCP> <9872@smoke.BRL.MIL> <28831@bu-cs.BU.EDU> <1618@thor.acc.stolaf.edu> <9979@bloom-beacon.MIT.EDU>, <28874@bu-cs.BU.EDU>, Organization: Information Technology Center, Carnegie Mellon, Pittsburgh, PA Lines: 44 In-Reply-To: Xref: utzoo comp.unix.wizards:15154 comp.lang.c:17109 [Why do problems with my messages always become evident 10 seconds AFTER I send them?] Miles Bader writes: > /* fmtP and valP are modified so that re-calling the function > * will start formatting where it left off > */ > int rvsnprintf(buf,maxlen,fmtP,valP) > char *buf; > int maxlen; > char **fmtP; /* in/out */ > va_list *valP; /* in/out */ Oops, you need one more variable, since sometimes a directive (%s, etc) can be too large for the buffer, so now: /* fmtP, valP and offsP are modified so that re-calling the function * will start formatting where it left off */ int rvsnprintf(buf,maxlen,fmtP,valP,offsP) char *buf; int maxlen; char **fmtP; /* in/out */ va_list *valP; /* in/out */ int *offsP; /* in/out */ *offsP says how many characters to drop before putting stuff into the buffer, and should initially be 0. With this change, it should work correctly for any size output buffer (even small ones). > { > ... > while(*fmt!='\0){ > int written=rvsnprintf(stream->pos,stream->left,&fmt,&val); Becomes: { int offs=0; ... while(*fmt!='\0){ int written=rvsnprintf(stream->pos,stream->left,&fmt,&val,&offs); -Miles