Debian Codegraph @

m4/strndup.m4

raw
1 # strndup.m4
2 # serial 24
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 dnl This file is offered as-is, without any warranty.
8
9 AC_DEFUN([gl_FUNC_STRNDUP],
10 [
11 dnl Persuade glibc <string.h> to declare strndup().
12 AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
13
14 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
15 AC_REQUIRE([gl_STRING_H_DEFAULTS])
16 AC_CHECK_DECLS_ONCE([strndup])
17 AC_CHECK_FUNCS_ONCE([strndup])
18 if test $ac_cv_have_decl_strndup = no; then
19 HAVE_DECL_STRNDUP=0
20 fi
21
22 if test $ac_cv_func_strndup = yes; then
23 HAVE_STRNDUP=1
24 dnl AIX 5.1 has a function that fails to add the terminating '\0'.
25 dnl AIX 7.3 has a function that does not support a zero length.
26 AC_CACHE_CHECK([for working strndup], [gl_cv_func_strndup_works],
27 [AC_RUN_IFELSE([
28 AC_LANG_PROGRAM([[#include <string.h>
29 #include <stdlib.h>]], [[
30 #if !HAVE_DECL_STRNDUP
31 extern
32 #ifdef __cplusplus
33 "C"
34 #endif
35 char *strndup (const char *, size_t);
36 #endif
37 int result = 0;
38 {
39 char *s = strndup ("some longer string", 15);
40 free (s);
41 s = strndup ("shorter string", 13);
42 if (s[13] != '\0')
43 result |= 1;
44 free (s);
45 }
46 if (strndup (NULL, 0) == NULL)
47 result |= 2;
48 return result;]])],
49 [gl_cv_func_strndup_works=yes],
50 [gl_cv_func_strndup_works=no],
51 [case $host_os in
52 aix*) gl_cv_func_strndup_works="guessing no";;
53 *) gl_cv_func_strndup_works="guessing yes";;
54 esac
55 ])])
56 case $gl_cv_func_strndup_works in
57 *no) REPLACE_STRNDUP=1 ;;
58 esac
59 else
60 HAVE_STRNDUP=0
61 fi
62 ])