Re: snprintf heads-up

From: Date: Fri, 08 Sep 2000 13:17:39 +0000
Subject: Re: snprintf heads-up
References: 1  Groups: php.dev 
Request: Send a blank email to php-dev+get-32657@lists.php.net to get a copy of this message
On Fri, 8 Sep 2000, Stanislav Malyshev wrote: >In a number of places in PHP code, the following construct is used: > >char buffer[LEN]; >... >buf_len = snprintf(buffer, sizeof(buffer)-1, ....); > ><code basing on buf_len, like memcpy(s,buffer,buf_len)> > >Now, the newest Linux manual I have states about snprintf: > >RETURN VALUE > If the output was truncated, the return value is -1, oth- > erwise it is the number of characters stored, not includ- > ing the terminating null. (Thus until glibc 2.0.6. Since > glibc 2.1 these functions return the number of characters > (excluding the trailing null) which would have been writ- > ten to the final string if enough space had been avail- > able.) <--- clip ---> SunOS 5.6 Last change: 27 Feb 1997 7 RETURN VALUES The printf(), fprintf(), and sprintf() functions return the number of bytes transmitted (not including the \0 in the case of sprintf()). The snprintf() function returns the number of characters formatted, that is, the number of char- acters that would have been written to the buffer if it were large enough. Each function returns a negative value if an output error was encountered. <--- clip ---> NetBSD 1.4.2 : # man snprintf 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. <--- clip ---> --Jani

« previous php.dev (#32657) next »