strndup.m4 1.68 KB
Newer Older
1
# strndup.m4 serial 22
2
dnl Copyright (C) 2002-2003, 2005-2018 Free Software Foundation, Inc.
3 4 5
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
dnl with or without modifications, as long as this notice is preserved.
6 7 8 9

AC_DEFUN([gl_FUNC_STRNDUP],
[
  dnl Persuade glibc <string.h> to declare strndup().
10
  AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
11

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 17 18
  if test $ac_cv_have_decl_strndup = no; then
    HAVE_DECL_STRNDUP=0
  fi
19

20
  if test $ac_cv_func_strndup = yes; then
21
    HAVE_STRNDUP=1
22 23 24 25
    # AIX 4.3.3, AIX 5.1 have a function that fails to add the terminating '\0'.
    AC_CACHE_CHECK([for working strndup], [gl_cv_func_strndup_works],
      [AC_RUN_IFELSE([
         AC_LANG_PROGRAM([[#include <string.h>
26
                           #include <stdlib.h>]], [[
Eric Blake's avatar
Eric Blake committed
27
#if !HAVE_DECL_STRNDUP
28 29 30 31 32
  extern
  #ifdef __cplusplus
  "C"
  #endif
  char *strndup (const char *, size_t);
33
#endif
34
  int result;
35 36 37 38
  char *s;
  s = strndup ("some longer string", 15);
  free (s);
  s = strndup ("shorter string", 13);
39 40 41
  result = s[13] != '\0';
  free (s);
  return result;]])],
42 43
         [gl_cv_func_strndup_works=yes],
         [gl_cv_func_strndup_works=no],
44 45 46 47 48 49 50 51
         [
changequote(,)dnl
          case $host_os in
            aix | aix[3-6]*) gl_cv_func_strndup_works="guessing no";;
            *)               gl_cv_func_strndup_works="guessing yes";;
          esac
changequote([,])dnl
         ])])
52
    case $gl_cv_func_strndup_works in
53
      *no) REPLACE_STRNDUP=1 ;;
54
    esac
55
  else
56
    HAVE_STRNDUP=0
57 58
  fi
])