strtoimax: Don't force a replacement on systems where intmax_t is int.
[gnulib.git] / m4 / strtoimax.m4
1 # strtoimax.m4 serial 12
2 dnl Copyright (C) 2002-2004, 2006, 2009-2012 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl with or without modifications, as long as this notice is preserved.
6
7 AC_DEFUN([gl_FUNC_STRTOIMAX],
8 [
9   AC_REQUIRE([gl_INTTYPES_H_DEFAULTS])
10
11   dnl On OSF/1 5.1 with cc, this function is declared but not defined.
12   AC_CHECK_FUNCS_ONCE([strtoimax])
13   AC_CHECK_DECLS_ONCE([strtoimax])
14   if test "$ac_cv_have_decl_strtoimax" != yes; then
15     HAVE_DECL_STRTOIMAX=0
16   fi
17
18   if test $ac_cv_func_strtoimax = yes; then
19     HAVE_STRTOIMAX=1
20     dnl On AIX 5.1, strtoimax() fails for values outside the 'int' range.
21     AC_REQUIRE([gl_AC_HEADER_STDINT_H])
22     AC_REQUIRE([gl_AC_HEADER_INTTYPES_H])
23     AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
24     AC_CACHE_CHECK([whether strtoimax works], [gl_cv_func_strtoimax],
25       [AC_RUN_IFELSE(
26          [AC_LANG_SOURCE([[
27 #include <errno.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <sys/types.h>
31 #if HAVE_STDINT_H_WITH_UINTMAX
32 # include <stdint.h>
33 #endif
34 #if HAVE_INTTYPES_H_WITH_UINTMAX
35 # include <inttypes.h>
36 #endif
37 int main ()
38 {
39   if (sizeof (intmax_t) > sizeof (int))
40     {
41       const char *s = "4294967295";
42       char *p;
43       intmax_t res;
44       errno = 0;
45       res = strtoimax (s, &p, 10);
46       if (p != s + strlen (s))
47         return 1;
48       if (errno != 0)
49         return 2;
50       if (res != (intmax_t) 65535 * (intmax_t) 65537)
51         return 3;
52     }
53   else
54     {
55       const char *s = "2147483647";
56       char *p;
57       intmax_t res;
58       errno = 0;
59       res = strtoimax (s, &p, 10);
60       if (p != s + strlen (s))
61         return 1;
62       if (errno != 0)
63         return 2;
64       if (res != 2147483647)
65         return 3;
66     }
67   return 0;
68 }
69 ]])],
70          [gl_cv_func_strtoimax=yes],
71          [gl_cv_func_strtoimax=no],
72          [case "$host_os" in
73                    # Guess no on AIX 5.
74             aix5*) gl_cv_func_strtoimax="guessing no" ;;
75                    # Guess yes otherwise.
76             *)     gl_cv_func_strtoimax="guessing yes" ;;
77           esac
78          ])
79       ])
80     case "$gl_cv_func_strtoimax" in
81       *no) REPLACE_STRTOIMAX=1 ;;
82     esac
83   else
84     HAVE_STRTOIMAX=0
85   fi
86 ])
87
88 # Prerequisites of lib/strtoimax.c.
89 AC_DEFUN([gl_PREREQ_STRTOIMAX], [
90   AC_CHECK_DECLS([strtoll])
91   AC_REQUIRE([AC_TYPE_LONG_LONG_INT])
92 ])