Subject: Re: CVS commit: src/sys
To: None <itojun@iijlab.net>
From: Matthew Orgass <darkstar@city-net.com>
List: tech-kern
Date: 04/24/2004 15:57:23
On 2004-04-25 itojun@iijlab.net wrote:

> >	the original code (with sprintf) is already broken, as sprintf()
> >	returns -1 on failure.  we just need to fix all of these
> >	cp += sprintf (or snprintf).
>
> 	happy now?

  Two general notes:
1) Adding a bunch of four line DIAGNOSTIC checks can significantly reduce
the readability of code.  Macros can be used to reduce this to one or two
lines that fit in better.
2) Use of string concatenation in this type of situation means you can't
share the strings.  If the function was an addional printf argument, you
could.

  One easy possibility for snprintf modifications would be a snprintf_seq
which takes pointers to the buffer pointer and length and increments the
buffer pointer and decrements the length by the amount actually written.
This would directly implement what the code is trying to do, and you would
still be able do determine how much space you should have if you want to
know (it should return the amount not written).

  An even better option, IMO, would be to use a string buffer structure
that contains the buffer pointer, buffer size, and the current length.
Then you would have, say, snprintf_sbuf and snprintf_sbuf_append that get
passed a pointer to the structure and modify as appropriate.

Matthew Orgass
darkstar@city-net.com