Debian Codegraph @

tools/gnulib/m4/strndup.m4

raw
1 # strndup.m4
2 # serial 23
3 dnl Copyright (C) 2002-2003, 2005-2024 Free Software Foundation, Inc.
4 dnl This file is free software; the Free Software Foundation
5 dnl gives unlimited permission to copy and/or distribute it,
6 dnl with or without modifications, as long as this notice is preserved.
7
8 AC_DEFUN([gl_FUNC_STRNDUP],
9 [
10 dnl Persuade glibc <string.h> to declare strndup().
11 AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
12
13 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
14 AC_REQUIRE([gl_STRING_H_DEFAULTS])
15 AC_CHECK_DECLS_ONCE([strndup])
16 AC_CHECK_FUNCS_ONCE([strndup])
17 if test $ac_cv_have_decl_strndup = no; then
18 HAVE_DECL_STRNDUP=0
19 fi
20
21 if test $ac_cv_func_strndup = yes; then
22 HAVE_STRNDUP=1
23 # AIX 4.3.3, AIX 5.1 have a function that fails to add the terminating '\0'.
24 AC_CACHE_CHECK([for working strndup], [gl_cv_func_strndup_works],
25 [AC_RUN_IFELSE([
26 AC_LANG_PROGRAM([[#include <string.h>
27 #include <stdlib.h>]], [[
28 #if !HAVE_DECL_STRNDUP
29 extern
30 #ifdef __cplusplus
31 "C"
32 #endif
33 char *strndup (const char *, size_t);
34 #endif
35 int result;
36 char *s;
37 s = strndup ("some longer string", 15);
38 free (s);
39 s = strndup ("shorter string", 13);
40 result = s[13] != '\0';
41 free (s);
42 return result;]])],
43 [gl_cv_func_strndup_works=yes],
44 [gl_cv_func_strndup_works=no],
45 [
46 changequote(,)dnl
47 case $host_os in
48 aix | aix[3-6]*) gl_cv_func_strndup_works="guessing no";;
49 *) gl_cv_func_strndup_works="guessing yes";;
50 esac
51 changequote([,])dnl
52 ])])
53 case $gl_cv_func_strndup_works in
54 *no) REPLACE_STRNDUP=1 ;;
55 esac
56 else
57 HAVE_STRNDUP=0
58 fi
59 ])