New module 'vsnprintf'.
authorBruno Haible <bruno@clisp.org>
Mon, 11 Oct 2004 13:08:55 +0000 (13:08 +0000)
committerBruno Haible <bruno@clisp.org>
Mon, 11 Oct 2004 13:08:55 +0000 (13:08 +0000)
ChangeLog
MODULES.html.sh
lib/ChangeLog
lib/vsnprintf.c [new file with mode: 0644]
lib/vsnprintf.h [new file with mode: 0644]
m4/ChangeLog
m4/vsnprintf.m4 [new file with mode: 0644]
modules/vsnprintf [new file with mode: 0644]

index e05a3ad..22d5563 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2004-10-11  Bruno Haible  <bruno@clisp.org>
+
+       * MODULES.html.sh (Support for systems lacking ISO C 99): Add
+       vsnprintf.
+
+2004-10-11  Yoann Vandoorselaere  <yoann@prelude-ids.org>
+
+       * modules/vsnprintf: New file.
+
 2004-10-04  Paul Eggert  <eggert@cs.ucla.edu>
 
        * modules/xalloc (Files, Makefile.am): Remove xstrdup.c.
index 260405d..9f56391 100755 (executable)
@@ -1661,6 +1661,7 @@ func_all_modules ()
 
   func_begin_table
   func_module snprintf
+  func_module vsnprintf
   func_end_table
 
   element="Numeric conversion functions <stdlib.h>"
index 08659a3..91e7fef 100644 (file)
@@ -1,3 +1,8 @@
+2004-10-11  Yoann Vandoorselaere  <yoann@prelude-ids.org>
+
+       * vsnprintf.h: New file.
+       * vsnprintf.c: New file.
+
 2004-10-07  Bruno Haible  <bruno@clisp.org>
 
        * snprintf.c (snprintf): Avoid a memory allocation if the result fits
diff --git a/lib/vsnprintf.c b/lib/vsnprintf.c
new file mode 100644 (file)
index 0000000..f35aee4
--- /dev/null
@@ -0,0 +1,58 @@
+/* Formatted output to strings.
+   Copyright (C) 2004 Free Software Foundation, Inc.
+   Written by Simon Josefsson and Yoann Vandoorselaere <yoann@prelude-ids.org>.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   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.  */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+/* Specification.  */
+#include "vsnprintf.h"
+
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "vasnprintf.h"
+
+/* Print formatted output to string STR.  Similar to vsprintf, but
+   additional length SIZE limit how much is written into STR.  Returns
+   string length of formatted string (which may be larger than SIZE).
+   STR may be NULL, in which case nothing will be written.  On error,
+   return a negative value. */
+int
+vsnprintf (char *str, size_t size, const char *format, va_list args)
+{
+  char *output;
+  size_t len;
+
+  len = size;
+  output = vasnprintf (str, &len, format, args);
+
+  if (!output)
+    return -1;
+
+  if (str != NULL)
+    if (len > size - 1) /* equivalent to: (size > 0 && len >= size) */
+      str[size - 1] = '\0';
+
+  if (output != str)
+    free (output);
+
+  return len;
+}
diff --git a/lib/vsnprintf.h b/lib/vsnprintf.h
new file mode 100644 (file)
index 0000000..36f74df
--- /dev/null
@@ -0,0 +1,31 @@
+/* Formatted output to strings.
+   Copyright (C) 2004 Free Software Foundation, Inc.
+   Written by Simon Josefsson and Yoann Vandoorselaere <yoann@prelude-ids.org>.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   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.  */
+
+#ifndef VSNPRINTF_H
+#define VSNPRINTF_H
+
+#include <stdarg.h>
+
+/* Get vsnprintf declaration, if available.  */
+#include <stdio.h>
+
+#if defined HAVE_DECL_VSNPRINTF && !HAVE_DECL_VSNPRINTF
+int vsnprintf (char *str, size_t size, const char *format, va_list args);
+#endif
+
+#endif /* VSNPRINTF_H */
index 9af4ee9..478c206 100644 (file)
@@ -1,3 +1,7 @@
+2004-10-11  Yoann Vandoorselaere  <yoann@prelude-ids.org>
+
+       * vsnprintf.m4: New file.
+
 2004-10-05  Bruno Haible  <bruno@clisp.org>
 
        * strsep.m4 (gl_FUNC_STRSEP): Require AC_GNU_SOURCE. Don't check
diff --git a/m4/vsnprintf.m4 b/m4/vsnprintf.m4
new file mode 100644 (file)
index 0000000..0ffac7a
--- /dev/null
@@ -0,0 +1,17 @@
+# vsnprintf.m4 serial 1
+dnl Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
+dnl This file is free software, distributed under the terms of the GNU
+dnl General Public License.  As a special exception to the GNU General
+dnl Public License, this file may be distributed as part of a program
+dnl that contains a configuration script generated by Autoconf, under
+dnl the same distribution terms as the rest of that program.
+
+AC_DEFUN([gl_FUNC_VSNPRINTF],
+[
+  AC_REPLACE_FUNCS(vsnprintf)
+  AC_CHECK_DECLS_ONCE(vsnprintf)
+  gl_PREREQ_VSNPRINTF
+])
+
+# Prerequisites of lib/vsnprintf.c.
+AC_DEFUN([gl_PREREQ_VSNPRINTF], [:])
diff --git a/modules/vsnprintf b/modules/vsnprintf
new file mode 100644 (file)
index 0000000..99c6d7c
--- /dev/null
@@ -0,0 +1,27 @@
+Description:
+vsnprintf() function: print formatted output from an stdarg argument list
+to a fixed length string
+
+Files:
+lib/vsnprintf.h
+lib/vsnprintf.c
+m4/vsnprintf.m4
+
+Depends-on:
+vasnprintf
+minmax
+
+configure.ac:
+gl_FUNC_VSNPRINTF
+
+Makefile.am:
+lib_SOURCES += vsnprintf.h
+
+Include:
+"vsnprintf.h"
+
+License:
+LGPL
+
+Maintainer:
+Yoann Vandoorselaere