New snprintf module from Simon Josefsson.
authorPaul Eggert <eggert@cs.ucla.edu>
Thu, 30 Sep 2004 23:29:25 +0000 (23:29 +0000)
committerPaul Eggert <eggert@cs.ucla.edu>
Thu, 30 Sep 2004 23:29:25 +0000 (23:29 +0000)
ChangeLog
MODULES.html.sh
lib/ChangeLog
lib/snprintf.c [new file with mode: 0644]
lib/snprintf.h [new file with mode: 0644]
m4/ChangeLog
m4/snprintf.m4 [new file with mode: 0644]
modules/snprintf [new file with mode: 0644]

index cdf5587..e075f06 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2004-09-30  Simon Josefsson  <jas@extundo.com>
+
+       * MODULES.html.sh (Support for systems lacking POSIX:2001): Add
+       snprintf.
+
+       * modules/snprintf: New file.
+
 2004-09-30  Paul Eggert  <eggert@cs.ucla.edu>
 
        * modules/argp (Maintainer): Replace Simon Josefsson
index d03092a..9d660ff 100755 (executable)
@@ -1735,6 +1735,7 @@ func_all_modules ()
   func_module regex
   func_module rename
   func_module rmdir
+  func_module snprintf
   func_module utime
   func_end_table
 
index d712b8d..1bf3783 100644 (file)
@@ -1,3 +1,7 @@
+2004-09-30  Simon Josefsson  <jas@extundo.com>
+
+       * snprintf.h, snprintf.c: New files.
+
 2004-09-30  Sergey Poznyakoff  <gray@Mirddin.farlep.net>
 
        * argp-help.c (canon_doc_option): Fixed coredump if *name==NULL
diff --git a/lib/snprintf.c b/lib/snprintf.c
new file mode 100644 (file)
index 0000000..6cc44aa
--- /dev/null
@@ -0,0 +1,55 @@
+/* Formatted output to strings.
+   Copyright (C) 2004 Free Software Foundation, Inc.
+   Written by Simon Josefsson.
+
+   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
+
+/* Get specification.  */
+#include "snprintf.h"
+
+/* Get vasnprintf.  */
+#include "vasnprintf.h"
+
+/* Get MIN. */
+#include <minmax.h>
+
+/* Print formatted output to string STR.  Similar to sprintf, 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
+snprintf (char *str, size_t size, const char *format, ...)
+{
+  size_t len;
+  char *out = vasnprintf (NULL, &len, format, args);
+
+  if (!out)
+    return -1;
+
+  if (str)
+    {
+      memcpy (str, out, MIN (len + 1, size));
+      str[size - 1] = '\0';
+    }
+
+  free (out);
+
+  return len;
+}
diff --git a/lib/snprintf.h b/lib/snprintf.h
new file mode 100644 (file)
index 0000000..18628f6
--- /dev/null
@@ -0,0 +1,29 @@
+/* Formatted output to strings.
+   Copyright (C) 2004 Free Software Foundation, Inc.
+   Written by Simon Josefsson.
+
+   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 SNPRINTF_H
+#define SNPRINTF_H
+
+/* Get snprintf declaration, if available.  */
+#include <stdio.h>
+
+#if defined HAVE_DECL_SNPRINTF && !HAVE_DECL_SNPRINTF
+int snprintf(char *str, size_t size, const char *format, ...);
+#endif
+
+#endif /* SNPRINTF_H */
index 06598bf..8137cbb 100644 (file)
@@ -1,3 +1,7 @@
+2004-09-30  Simon Josefsson  <jas@extundo.com>
+
+       * snprintf.m4: New file.
+
 2004-09-09  Bruno Haible  <bruno@clisp.org>
 
        * eoverflow.m4: New file, taken from GNU libiconv eilseq.m4 with
diff --git a/m4/snprintf.m4 b/m4/snprintf.m4
new file mode 100644 (file)
index 0000000..c3728ca
--- /dev/null
@@ -0,0 +1,17 @@
+# sprintf.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_SNPRINTF],
+[
+  AC_REPLACE_FUNCS(snprintf)
+  AC_CHECK_DECLS_ONCE(snprintf)
+  gl_PREREQ_SNPRINTF
+])
+
+# Prerequisites of lib/snprintf.c.
+AC_DEFUN([gl_PREREQ_SNPRINTF], [:])
diff --git a/modules/snprintf b/modules/snprintf
new file mode 100644 (file)
index 0000000..d5471c4
--- /dev/null
@@ -0,0 +1,23 @@
+Description:
+snprintf() function: print formatted output to a fixed length string
+
+Files:
+lib/snprintf.h
+lib/snprintf.c
+m4/snprintf.m4
+
+Depends-on:
+vasnprintf
+minmax
+
+configure.ac:
+gl_FUNC_SNPRINTF
+
+Makefile.am:
+lib_SOURCES += snprintf.h
+
+Include:
+"snprintf.h"
+
+Maintainer:
+Simon Josefsson