Detect and use %zu for size_t when available

Commit db949b7fad 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>
This commit is contained in:
Andrew Deason 2009-10-11 21:55:05 -05:00 committed by Derrick Brashear
parent 1fa650cee2
commit 58628eb745
3 changed files with 40 additions and 12 deletions

View 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

27
src/cf/sizet.m4 Normal file
View File

@ -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
])

View 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