2 dnl Copyright (C) 2002-2003, 2005-2020 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.
7 AC_DEFUN([gl_FUNC_STRNDUP],
9 dnl Persuade glibc <string.h> to declare strndup().
10 AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
12 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
13 AC_REQUIRE([gl_HEADER_STRING_H_DEFAULTS])
14 AC_CHECK_DECLS_ONCE([strndup])
15 AC_CHECK_FUNCS_ONCE([strndup])
16 if test $ac_cv_have_decl_strndup = no; then
20 if test $ac_cv_func_strndup = yes; then
22 # AIX 4.3.3, AIX 5.1 have a function that fails to add the terminating '\0'.
23 AC_CACHE_CHECK([for working strndup], [gl_cv_func_strndup_works],
25 AC_LANG_PROGRAM([[#include <string.h>
26 #include <stdlib.h>]], [[
27 #if !HAVE_DECL_STRNDUP
32 char *strndup (const char *, size_t);
36 s = strndup ("some longer string", 15);
38 s = strndup ("shorter string", 13);
39 result = s[13] != '\0';
42 [gl_cv_func_strndup_works=yes],
43 [gl_cv_func_strndup_works=no],
47 aix | aix[3-6]*) gl_cv_func_strndup_works="guessing no";;
48 *) gl_cv_func_strndup_works="guessing yes";;
52 case $gl_cv_func_strndup_works in
53 *no) REPLACE_STRNDUP=1 ;;