vsnprintf.c

00001 /* Formatted output to strings.
00002    Copyright (C) 2004 Free Software Foundation, Inc.
00003    Written by Simon Josefsson and Yoann Vandoorselaere <yoann@prelude-ids.org>.
00004 
00005    This program is free software; you can redistribute it and/or modify
00006    it under the terms of the GNU Lesser General Public License as published by
00007    the Free Software Foundation; either version 2.1, or (at your option)
00008    any later version.
00009 
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013    GNU Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public License along
00016    with this program; if not, write to the Free Software Foundation,
00017    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
00018 
00019 #ifdef HAVE_CONFIG_H
00020 # include <config.h>
00021 #endif
00022 
00023 /* Specification.  */
00024 #include "vsnprintf.h"
00025 
00026 #include <stdarg.h>
00027 #include <stdio.h>
00028 #include <stdlib.h>
00029 #include <string.h>
00030 
00031 #include "vasnprintf.h"
00032 
00033 /* Print formatted output to string STR.  Similar to vsprintf, but
00034    additional length SIZE limit how much is written into STR.  Returns
00035    string length of formatted string (which may be larger than SIZE).
00036    STR may be NULL, in which case nothing will be written.  On error,
00037    return a negative value. */
00038 int
00039 vsnprintf (char *str, size_t size, const char *format, va_list args)
00040 {
00041   char *output;
00042   size_t len;
00043 
00044   len = size;
00045   output = vasnprintf (str, &len, format, args);
00046 
00047   if (!output)
00048     return -1;
00049 
00050   if (str != NULL)
00051     if (len > size - 1) /* equivalent to: (size > 0 && len >= size) */
00052       str[size - 1] = '\0';
00053 
00054   if (output != str)
00055     free (output);
00056 
00057   return len;
00058 }

Generated on Mon Feb 5 10:54:28 2007 for WvStreams by  doxygen 1.5.1