1# strndup.m4 serial 21
2dnl Copyright (C) 2002-2003, 2005-2013 Free Software Foundation, Inc.
3dnl This file is free software; the Free Software Foundation
4dnl gives unlimited permission to copy and/or distribute it,
5dnl with or without modifications, as long as this notice is preserved.
6
7AC_DEFUN([cf3_FUNC_STRNDUP],
8[
9  AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
10  AC_CHECK_DECLS([strndup])
11  AC_REPLACE_FUNCS([strndup])
12  if test $ac_cv_have_decl_strndup = no; then
13    HAVE_DECL_STRNDUP=0
14  fi
15
16  if test $ac_cv_func_strndup = yes; then
17    HAVE_STRNDUP=1
18    # AIX 5.3 has a function that tries to copy the entire range specified
19    # by n, instead of just the length of src.
20    AC_CACHE_CHECK([for working strndup], [cf3_cv_func_strndup_works],
21      [AC_RUN_IFELSE([
22         AC_LANG_PROGRAM([[#include <string.h>
23                           #include <stdlib.h>]], [[
24#if !HAVE_DECL_STRNDUP
25  extern
26  #ifdef __cplusplus
27  "C"
28  #endif
29  char *strndup (const char *, size_t);
30#endif
31  char *s;
32  // Will crash if strndup tries to traverse all 2GB.
33  s = strndup ("string", 2000000000);
34  return 0;]])],
35         [cf3_cv_func_strndup_works=yes],
36         [cf3_cv_func_strndup_works=no],
37         [
38changequote(,)dnl
39          case $host_os in
40            aix | aix[3-6]*) cf3_cv_func_strndup_works="guessing no";;
41            *)               cf3_cv_func_strndup_works="guessing yes";;
42          esac
43changequote([,])dnl
44         ])])
45    case $cf3_cv_func_strndup_works in
46      *no) AC_LIBOBJ([strndup]) ;;
47    esac
48  else
49    HAVE_STRNDUP=0
50  fi
51])
52