Guessing buffer size needed for sprintf beforehand

Andrew Vignaux andrew at comp.vuw.ac.nz
Mon May 9 10:25:29 AEST 1988


In article <11331 at mimsy.UUCP> chris at 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 at comp.vuw.ac.nz   Path address: ...!uunet!vuwcomp!andrew



More information about the Comp.unix.wizards mailing list