New module 'sprintf-posix'.
authorBruno Haible <bruno@clisp.org>
Wed, 7 Mar 2007 03:47:50 +0000 (03:47 +0000)
committerBruno Haible <bruno@clisp.org>
Wed, 7 Mar 2007 03:47:50 +0000 (03:47 +0000)
ChangeLog
lib/sprintf.c [new file with mode: 0644]
lib/stdio_.h
m4/sprintf-posix.m4 [new file with mode: 0644]
m4/stdio_h.m4
modules/sprintf-posix [new file with mode: 0644]
modules/stdio

index f2340fa..f054f08 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,16 @@
 2007-03-06  Bruno Haible  <bruno@clisp.org>
 
+       * modules/sprintf-posix: New file.
+       * lib/sprintf.c: New file.
+       * m4/sprintf-posix.m4: New file.
+       * m4/stdio_h.m4 (gl_STDIO_H_DEFAULTS): Set also GNULIB_SPRINTF_POSIX,
+       REPLACE_SPRINTF.
+       * lib/stdio_.h (sprintf): New declaration.
+       * modules/stdio (Makefile.am): Substitute also GNULIB_SPRINTF_POSIX,
+       REPLACE_SPRINTF.
+
+2007-03-06  Bruno Haible  <bruno@clisp.org>
+
        * modules/vsprintf-posix-tests: New file.
        * tests/test-vsprintf-posix.c: New file.
        * tests/test-sprintf-posix.h: New file.
diff --git a/lib/sprintf.c b/lib/sprintf.c
new file mode 100644 (file)
index 0000000..7d58daf
--- /dev/null
@@ -0,0 +1,75 @@
+/* Formatted output to strings.
+   Copyright (C) 2004, 2006-2007 Free Software Foundation, Inc.
+
+   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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+/* Specification.  */
+#include <stdio.h>
+
+#include <errno.h>
+#include <limits.h>
+#include <stdarg.h>
+#include <stdlib.h>
+
+#include "vasnprintf.h"
+
+/* Some systems, like OSF/1 4.0 and Woe32, don't have EOVERFLOW.  */
+#ifndef EOVERFLOW
+# define EOVERFLOW E2BIG
+#endif
+
+#ifndef SIZE_MAX
+# define SIZE_MAX ((size_t) -1)
+#endif
+
+/* Print formatted output to string STR.
+   Return string length of formatted string.  On error, return a negative
+   value. */
+int
+sprintf (char *str, const char *format, ...)
+{
+  char *output;
+  size_t len;
+  size_t lenbuf = SIZE_MAX;
+  va_list args;
+
+  va_start (args, format);
+  output = vasnprintf (str, &lenbuf, format, args);
+  len = lenbuf;
+  va_end (args);
+
+  if (!output)
+    return -1;
+
+  if (output != str)
+    {
+      /* len is near SIZE_MAX.  */
+      free (output);
+      errno = EOVERFLOW;
+      return -1;
+    }
+
+  if (len > INT_MAX)
+    {
+      errno = EOVERFLOW;
+      return -1;
+    }
+
+  return len;
+}
index 20b5581..83d970d 100644 (file)
@@ -70,6 +70,20 @@ extern int vsnprintf (char *str, size_t size, const char *format, va_list args);
      vsnprintf (b, s, f, a))
 #endif
 
+#if @GNULIB_SPRINTF_POSIX@
+# if @REPLACE_SPRINTF@
+#  define sprintf rpl_sprintf
+extern int sprintf (char *str, const char *format, ...);
+# endif
+#elif defined GNULIB_POSIXCHECK
+# undef sprintf
+# define sprintf \
+    (GL_LINK_WARNING ("sprintf is not always POSIX compliant - " \
+                      "use gnulib module sprintf-posix for portable " \
+                      "POSIX compliance"), \
+     sprintf)
+#endif
+
 #if @GNULIB_VSPRINTF_POSIX@
 # if @REPLACE_VSPRINTF@
 #  define vsprintf rpl_vsprintf
diff --git a/m4/sprintf-posix.m4 b/m4/sprintf-posix.m4
new file mode 100644 (file)
index 0000000..bd28762
--- /dev/null
@@ -0,0 +1,38 @@
+# sprintf-posix.m4 serial 1
+dnl Copyright (C) 2007 Free Software Foundation, Inc.
+dnl This file is free software; the Free Software Foundation
+dnl gives unlimited permission to copy and/or distribute it,
+dnl with or without modifications, as long as this notice is preserved.
+
+AC_DEFUN([gl_FUNC_SPRINTF_POSIX],
+[
+  AC_REQUIRE([gl_EOVERFLOW])
+  AC_REQUIRE([gl_PRINTF_SIZES_C99])
+  AC_REQUIRE([gl_PRINTF_DIRECTIVE_A])
+  AC_REQUIRE([gl_PRINTF_DIRECTIVE_N])
+  AC_REQUIRE([gl_PRINTF_POSITIONS])
+  if expr "$gl_cv_func_printf_sizes_c99" : ".*yes" > /dev/null \
+     && expr "$gl_cv_func_printf_directive_a" : ".*yes" > /dev/null \
+     && expr "$gl_cv_func_printf_directive_n" : ".*yes" > /dev/null \
+     && expr "$gl_cv_func_printf_positions" : ".*yes" > /dev/null; then
+    : # sprintf exists and is already POSIX compliant.
+  else
+    if ! expr "$gl_cv_func_printf_directive_a" : ".*yes" > /dev/null; then
+      AC_DEFINE([NEED_PRINTF_DIRECTIVE_A], 1,
+        [Define if the vasnprintf implementation needs special code for
+         the 'a' and 'A' directives.])
+    fi
+    gl_REPLACE_VASNPRINTF
+    gl_REPLACE_SPRINTF
+  fi
+])
+
+AC_DEFUN([gl_REPLACE_SPRINTF],
+[
+  AC_REQUIRE([gl_STDIO_H_DEFAULTS])
+  AC_LIBOBJ([sprintf])
+  REPLACE_SPRINTF=1
+  gl_PREREQ_SPRINTF
+])
+
+AC_DEFUN([gl_PREREQ_SPRINTF], [:])
index 2b7546c..14e7d75 100644 (file)
@@ -22,6 +22,7 @@ AC_DEFUN([gl_STDIO_MODULE_INDICATOR],
 AC_DEFUN([gl_STDIO_H_DEFAULTS],
 [
   GNULIB_SNPRINTF=0;       AC_SUBST([GNULIB_SNPRINTF])
+  GNULIB_SPRINTF_POSIX=0;  AC_SUBST([GNULIB_SPRINTF_POSIX])
   GNULIB_VSNPRINTF=0;      AC_SUBST([GNULIB_VSNPRINTF])
   GNULIB_VSPRINTF_POSIX=0; AC_SUBST([GNULIB_VSPRINTF_POSIX])
   dnl Assume proper GNU behavior unless another module says otherwise.
@@ -29,5 +30,6 @@ AC_DEFUN([gl_STDIO_H_DEFAULTS],
   HAVE_DECL_SNPRINTF=1;    AC_SUBST([HAVE_DECL_SNPRINTF])
   REPLACE_VSNPRINTF=0;     AC_SUBST([REPLACE_VSNPRINTF])
   HAVE_DECL_VSNPRINTF=1;   AC_SUBST([HAVE_DECL_VSNPRINTF])
+  REPLACE_SPRINTF=0;       AC_SUBST([REPLACE_SPRINTF])
   REPLACE_VSPRINTF=0;      AC_SUBST([REPLACE_VSPRINTF])
 ])
diff --git a/modules/sprintf-posix b/modules/sprintf-posix
new file mode 100644 (file)
index 0000000..498e3d5
--- /dev/null
@@ -0,0 +1,31 @@
+Description:
+POSIX compatible sprintf() function: print formatted output to a string
+
+Files:
+lib/sprintf.c
+m4/sprintf-posix.m4
+m4/printf.m4
+
+Depends-on:
+stdio
+vasnprintf
+isnan-nolibm
+isnanl-nolibm
+printf-frexp
+printf-frexpl
+
+configure.ac:
+gl_FUNC_SPRINTF_POSIX
+gl_STDIO_MODULE_INDICATOR([sprintf-posix])
+
+Makefile.am:
+
+Include:
+<stdio.h>
+
+License:
+LGPL
+
+Maintainer:
+Bruno Haible
+
index b480fc4..3a8bdf4 100644 (file)
@@ -22,12 +22,14 @@ stdio.h: stdio_.h
        { echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */' && \
          sed -e 's|@''ABSOLUTE_STDIO_H''@|$(ABSOLUTE_STDIO_H)|g' \
              -e 's|@''GNULIB_SNPRINTF''@|$(GNULIB_SNPRINTF)|g' \
+             -e 's|@''GNULIB_SPRINTF_POSIX''@|$(GNULIB_SPRINTF_POSIX)|g' \
              -e 's|@''GNULIB_VSNPRINTF''@|$(GNULIB_VSNPRINTF)|g' \
              -e 's|@''GNULIB_VSPRINTF_POSIX''@|$(GNULIB_VSPRINTF_POSIX)|g' \
              -e 's|@''REPLACE_SNPRINTF''@|$(REPLACE_SNPRINTF)|g' \
              -e 's|@''HAVE_DECL_SNPRINTF''@|$(HAVE_DECL_SNPRINTF)|g' \
              -e 's|@''REPLACE_VSNPRINTF''@|$(REPLACE_VSNPRINTF)|g' \
              -e 's|@''HAVE_DECL_VSNPRINTF''@|$(HAVE_DECL_VSNPRINTF)|g' \
+             -e 's|@''REPLACE_SPRINTF''@|$(REPLACE_SPRINTF)|g' \
              -e 's|@''REPLACE_VSPRINTF''@|$(REPLACE_VSPRINTF)|g' \
              -e '/definition of GL_LINK_WARNING/r $(LINK_WARNING_H)' \
              < $(srcdir)/stdio_.h; \