Parcourir la source

build: Check for C99 snprintf family of functions

We need the new semantics that allow passing NULL and 0 size to retrieve
the full size of the formatted string. Add a new DPKG_FUNC_C99_SNPRINTF
macro and remove the simple compat checks for snprintf and vsnprintf.
Guillem Jover il y a 17 ans
Parent
commit
b1f61777e7
3 fichiers modifiés avec 50 ajouts et 8 suppressions
  1. 2 1
      configure.ac
  2. 2 6
      lib/compat/Makefile.am
  3. 46 1
      m4/funcs.m4

+ 2 - 1
configure.ac

@@ -96,9 +96,10 @@ DPKG_C_ATTRIBUTE
 
 # Checks for library functions.
 DPKG_FUNC_VA_COPY
+DPKG_FUNC_C99_SNPRINTF
 DPKG_CHECK_DECL([WCOREDUMP], [sys/wait.h])
 DPKG_CHECK_COMPAT_FUNCS([getopt getopt_long obstack_free \
-                         vsnprintf snprintf strnlen strerror strsignal \
+                         strnlen strerror strsignal \
                          scandir alphasort unsetenv])
 AC_CHECK_FUNCS([strtoul isascii bcopy memcpy lchown setsid getdtablesize])
 

+ 2 - 6
lib/compat/Makefile.am

@@ -34,12 +34,8 @@ if !HAVE_STRSIGNAL
 libcompat_a_SOURCES += strsignal.c
 endif
 
-if !HAVE_SNPRINTF
-libcompat_a_SOURCES += snprintf.c
-endif
-
-if !HAVE_VSNPRINTF
-libcompat_a_SOURCES += vsnprintf.c
+if !HAVE_C99_SNPRINTF
+libcompat_a_SOURCES += snprintf.c vsnprintf.c
 endif
 
 if !HAVE_ALPHASORT

+ 46 - 1
m4/funcs.m4

@@ -1,4 +1,4 @@
-# Copyright © 2008 Guillem Jover <guillem@debian.org>
+# Copyright © 2008, 2009 Guillem Jover <guillem@debian.org>
 
 # DPKG_FUNC_VA_COPY
 # -----------------
@@ -22,6 +22,51 @@ AS_IF([test "x$dpkg_cv_va_copy" = "xyes"],
 	           [Define to 1 if the 'va_copy' macro exists])])
 ])# DPKG_FUNC_VA_COPY
 
+# DPKG_FUNC_C99_SNPRINTF
+# -----------------------
+# Define HAVE_C99_SNPRINTF if we have C99 snprintf family semantics
+AC_DEFUN([DPKG_FUNC_C99_SNPRINTF],
+[AC_CACHE_CHECK([for C99 snprintf functions], [dpkg_cv_c99_snprintf],
+	[AC_RUN_IFELSE([AC_LANG_SOURCE([[
+#include <stdarg.h>
+#include <stdio.h>
+#include <string.h>
+int test_vsnprintf(const char *fmt, ...)
+{
+	int n;
+	va_list ap;
+
+	va_start(ap, fmt);
+	n = vsnprintf(NULL, 0, fmt, ap);
+	va_end(ap);
+
+	return n;
+}
+int main()
+{
+	int n;
+
+	n = snprintf(NULL, 0, "format %s %d", "string", 10);
+	if (n != strlen("format string 10"))
+		return 1;
+
+	n = test_vsnprintf("format %s %d", "string", 10);
+	if (n != strlen("format string 10"))
+		return 1;
+
+	return 0;
+}
+	]])],
+	[dpkg_cv_c99_snprintf=yes],
+	[dpkg_cv_c99_snprintf=no],
+	[dpkg_cv_c99_snprintf=no])])
+AS_IF([test "x$dpkg_cv_c99_snprintf" = "xyes"],
+	[AC_DEFINE([HAVE_C99_SNPRINTF], 1,
+	           [Define to 1 if the 'snprintf' family is C99 conformant])],
+	)
+AM_CONDITIONAL(HAVE_C99_SNPRINTF, [test "x$dpkg_cv_c99_snprintf" = "xyes"])
+])# DPKG_FUNC_C99_SNPRINTF
+
 # DPKG_CHECK_COMPAT_FUNCS(LIST)
 # -----------------------
 # Check each function and define an automake conditional