X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Fsnprintf.c;h=5870f8dde30f08250f6d78a1683cc14c034aaa27;hb=d0cf3d50306ab124bef29c541aa8e0c07f21c4aa;hp=92c265289a3730865150acb7c9781092b9f555cf;hpb=edafda534244e3760000faf7a088af872d146325;p=gnulib.git diff --git a/lib/snprintf.c b/lib/snprintf.c index 92c265289..5870f8dde 100644 --- a/lib/snprintf.c +++ b/lib/snprintf.c @@ -14,27 +14,20 @@ You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, - Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifdef HAVE_CONFIG_H # include #endif -/* Specification. */ #include "snprintf.h" -/* Get va_list, va_start, va_end. */ #include -/* Get free. */ #include -/* Get memcpy, size_t. */ #include -/* Get vasnprintf. */ -#include "vasnprintf.h" - -/* Get MIN. */ #include "minmax.h" +#include "vasnprintf.h" /* Print formatted output to string STR. Similar to sprintf, but additional length SIZE limit how much is written into STR. Returns @@ -49,19 +42,19 @@ snprintf (char *str, size_t size, const char *format, ...) va_list args; va_start (args, format); - output = vasnprintf (NULL, &len, format, args); + len = size; + output = vasnprintf (str, &len, format, args); va_end (args); if (!output) return -1; - if (str) - { - memcpy (str, output, MIN (len + 1, size)); + if (str != NULL) + if (len > size - 1) /* equivalent to: (size > 0 && len >= size) */ str[size - 1] = '\0'; - } - free (output); + if (output != str) + free (output); return len; }