Xref: utzoo comp.unix.questions:6942 comp.unix.wizards:8354
Path: utzoo!mnetor!uunet!munnari!vuwcomp!andrew
From: andrew@comp.vuw.ac.nz (Andrew Vignaux)
Newsgroups: comp.unix.questions,comp.unix.wizards
Subject: Re: Guessing buffer size needed for sprintf beforehand
Message-ID: <13597@comp.vuw.ac.nz>
Date: 9 May 88 00:25:29 GMT
References: <136@insyte.uucp> <11331@mimsy.UUCP>
Reply-To: andrew@comp.vuw.ac.nz (Andrew Vignaux)
Organization: Comp Sci, Victoria Univ, Wellington, New Zealand
Lines: 31
Keywords: sprintf

In article <11331@mimsy.UUCP> chris@mimsy.UUCP (Chris Torek) writes:
>By the time you reach a routine that takes a `va_list' argument,
>it is too late to scan the argument list twice: there is no way
>to `back up' the va_list parameter.  So, while this would get
>you a suitable count, it would then be too late to use that count.

The `varargs' manual page says "Multiple traversals, each bracketed by
va_start ...  va_end, are possible." so it is possible to rescan the
argument list.  (or have I misinterpreted what you said?)

I can't see any nice/portable/efficient solution to the original problem.
- Push the fudge factor up -- the classic unix solution.
- Chris Torek's solution.
- Write a function to interpret the printf format.  Release it to the
  world.  Convince everyone who writes "yet another stdio" to include
  your function.  Convince the various standards committees to accept
  the function.  Then in 5-10 years you will have a standard solution :-)
- send the output off to a pipe, and wait until it comes back.  Then
  if the pipe buffer is too small you can blame the operating system :-)
- open a FILE* like Chris.  setbuf() the buffer to point out of your
  address space.  Set up a signal handler to trap SIGSEGV.  Then count
  the number of segmentation violations :-) :-)

I suggest you push up the fudge factor and turn it into a SEP.

BTW, I think there should be a stropen()-like function so mere-mortals
can open string based stdio streams.  Is this in the standard?  Comments?

Andrew
------------------------------------------------------------------------------
Domain address: andrew@comp.vuw.ac.nz   Path address: ...!uunet!vuwcomp!andrew