Subject: Re: snprintf returns? [was Re: CVS commit: src/sys]
To: Eric Haszlakiewicz <erh@nimenees.com>
From: Rafal Boni <rafal@pobox.com>
List: tech-kern
Date: 04/24/2004 22:45:44
In message <20040425014334.GA24115@jodi.nimenees.com>, you write: 

-> On Sat, Apr 24, 2004 at 02:27:28PM +0200, Klaus Klein wrote:
-> > snprintf() returns the amount of storage _required_ to format the
-> > arguments given, which may be larger than the available storage size
-> > passed to it (ep - cp).  In a worst case scenario, the storage will
-> 	huh?  man snprintf, 3rd paragraph of the description:
->      These functions return the number of characters printed (not including
->      the trailing `\0' used to end output to strings).  If an output error
->	was encountered, these functions shall return a negative value.

Now read two paragraphs down...

     snprintf() and vsnprintf() will write at most size-1 of the characters
     printed into the output string (the size'th character then gets the ter-
     minating `\0'); if the return value is greater than or equal to the size
     argument, the string was too short and some of the printed characters
     were discarded.  If size is zero, nothing is written and str may be a
     NULL pointer.

IIRC, this is also not entirely portable, because I do believe some
systems *did* indeed return either -1 or size-1 if the string was
truncated, but in this case we're talking about NetBSD and not some
random system.

(And as a random off-topic comment the phrase "the size'th character"
 makes me snicker.. Only software people would write something like
 that :-))

--rafal

----
Rafal Boni                                                     rafal@pobox.com
  We are all worms.  But I do believe I am a glowworm.  -- Winston Churchill