Detect and use %zu for size_t when available
authorAndrew Deason <adeason@sinenomine.net>
Mon, 12 Oct 2009 02:55:05 +0000 (21:55 -0500)
committerDerrick Brashear <shadow|account-1000005@unknown>
Thu, 15 Oct 2009 19:15:38 +0000 (12:15 -0700)
Commit db949b7fade69d7eb1e38ad85d5b822c443306cb was incorrect and
generated warnings on many platforms; we cannot determine the correct
format string for a size_t at compile-time. Instead use the %z length
modifier when we can detect that it is safe to use. Otherwise, fall back
to just %lu on Unix, since it is reasonable to assume
sizeof(size_t) == sizeof(unsigned long) on Unix.

Reviewed-on: http://gerrit.openafs.org/647
Reviewed-by: Russ Allbery <rra@stanford.edu>
Reviewed-by: Simon Wilkinson <sxw@inf.ed.ac.uk>
Reviewed-by: Derrick Brashear <shadow@dementia.org>
Tested-by: Derrick Brashear <shadow@dementia.org>

acinclude.m4
src/cf/sizet.m4 [new file with mode: 0644]
src/config/stds.h

index 275bc22..c965b64 100644 (file)
@@ -334,6 +334,7 @@ AC_PROG_YACC
 AM_PROG_LEX
 
 OPENAFS_CHECK_BIGENDIAN
+OPENAFS_PRINTF_TAKES_Z_LEN
 
 AC_MSG_CHECKING(your OS)
 system=$host
diff --git a/src/cf/sizet.m4 b/src/cf/sizet.m4
new file mode 100644 (file)
index 0000000..1a0e788
--- /dev/null
@@ -0,0 +1,27 @@
+AC_DEFUN([OPENAFS_PRINTF_TAKES_Z_LEN],
+[
+AC_CACHE_CHECK([whether printf understands the %z length modifier],
+[openafs_cv_printf_takes_z], [
+        AC_TRY_RUN([
+#include <stdio.h>
+#include <string.h>
+
+int main(void) {
+        char buf[8];
+        memset(buf, 0, sizeof(buf));
+        snprintf(buf, 8, "%zu", sizeof(char));
+        if (buf[0] == '1' && buf[1] == '\0') {
+                return 0;
+        } else {
+                return 1;
+        }
+}],
+                [openafs_cv_printf_takes_z="yes"],
+                [openafs_cv_printf_takes_z="no"],
+                [openafs_cv_printf_takes_z="no"])
+])
+
+if test "x$openafs_cv_printf_takes_z" = "xyes"; then
+        AC_DEFINE([PRINTF_TAKES_Z_LEN], 1, [define if printf and friends understand the %z length modifier])
+fi
+])
index 1f7af62..d0516ac 100644 (file)
@@ -282,19 +282,19 @@ typedef struct afsUUID afsUUID;
  * windows use a different format string
  */
 #ifdef AFS_NT40_ENV
-#define AFS_INT64_FMT "I64d"
-#define AFS_UINT64_FMT "I64u"
-#define AFS_PTR_FMT   "Ip"
-#define AFS_SIZET_FMT "Iu"
+# define AFS_INT64_FMT "I64d"
+# define AFS_UINT64_FMT "I64u"
+# define AFS_PTR_FMT   "Ip"
+# define AFS_SIZET_FMT "Iu"
 #else
-#define AFS_INT64_FMT "lld"
-#define AFS_UINT64_FMT "llu"
-#define AFS_PTR_FMT   "p"
-#ifdef AFS_64BITPOINTER_ENV
-#define AFS_SIZET_FMT "lu"
-#else
-#define AFS_SIZET_FMT "u"
-#endif /* AFS_64BITPOINTER_ENV */
+# define AFS_INT64_FMT "lld"
+# define AFS_UINT64_FMT "llu"
+# define AFS_PTR_FMT   "p"
+# ifdef PRINTF_TAKES_Z_LEN
+#  define AFS_SIZET_FMT "zu"
+# else
+#  define AFS_SIZET_FMT "lu"
+# endif /* PRINTF_TAKES_Z_LEN */
 #endif /* AFS_NT40_ENV */
 
 /* Functions to safely cast afs_int32 and afs_uint32 so they can be used in