Merge from coreutils.
[gnulib.git] / m4 / calloc.m4
1 #serial 2
2
3 # Determine whether calloc (N, S) returns non-NULL when N*S is zero.
4 # If so, define HAVE_CALLOC.  Otherwise, define calloc to rpl_calloc
5 # and arrange to use a calloc wrapper function that does work in that case.
6
7 # _AC_FUNC_CALLOC_IF(IF-WORKS, IF-NOT)
8 # -------------------------------------
9 # If `calloc (0, 0)' is properly handled, run IF-WORKS, otherwise, IF-NOT.
10 AC_DEFUN([_AC_FUNC_CALLOC_IF],
11 [AC_REQUIRE([AC_HEADER_STDC])dnl
12 AC_CHECK_HEADERS(stdlib.h)
13 AC_CACHE_CHECK([for GNU libc compatible calloc], ac_cv_func_calloc_0_nonnull,
14 [AC_RUN_IFELSE(
15 [AC_LANG_PROGRAM(
16 [[#if STDC_HEADERS || HAVE_STDLIB_H
17 # include <stdlib.h>
18 #else
19 char *calloc ();
20 #endif
21 ]],
22                  [exit (calloc (0, 0) ? 0 : 1);])],
23                [ac_cv_func_calloc_0_nonnull=yes],
24                [ac_cv_func_calloc_0_nonnull=no],
25                [ac_cv_func_calloc_0_nonnull=no])])
26 AS_IF([test $ac_cv_func_calloc_0_nonnull = yes], [$1], [$2])
27 ])# AC_FUNC_CALLOC
28
29
30 # AC_FUNC_CALLOC
31 # ---------------
32 # Report whether `calloc (0, 0)' is properly handled, and replace calloc if
33 # needed.
34 AC_DEFUN([AC_FUNC_CALLOC],
35 [_AC_FUNC_CALLOC_IF(
36   [AC_DEFINE([HAVE_CALLOC], 1,
37              [Define to 1 if your system has a GNU libc compatible `calloc'
38               function, and to 0 otherwise.])],
39   [AC_DEFINE([HAVE_CALLOC], 0)
40    AC_LIBOBJ([calloc])
41    AC_DEFINE([calloc], [rpl_calloc],
42       [Define to rpl_calloc if the replacement function should be used.])])
43 ])# AC_FUNC_CALLOC