strndup.m4 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # strndup.m4 serial 17
  2. dnl Copyright (C) 2002-2003, 2005-2010 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.
  6. AC_DEFUN([gl_FUNC_STRNDUP],
  7. [
  8. dnl Persuade glibc <string.h> to declare strndup().
  9. AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
  10. AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
  11. AC_REQUIRE([gl_HEADER_STRING_H_DEFAULTS])
  12. AC_CHECK_DECLS_ONCE([strndup])
  13. AC_CHECK_FUNCS_ONCE([strndup])
  14. if test $ac_cv_have_decl_strndup = no; then
  15. HAVE_DECL_STRNDUP=0
  16. fi
  17. if test $ac_cv_func_strndup = yes; then
  18. # AIX 4.3.3, AIX 5.1 have a function that fails to add the terminating '\0'.
  19. AC_CACHE_CHECK([for working strndup], [gl_cv_func_strndup_works],
  20. [AC_RUN_IFELSE([
  21. AC_LANG_PROGRAM([[#include <string.h>
  22. #include <stdlib.h>]], [[
  23. #ifndef HAVE_DECL_STRNDUP
  24. extern char *strndup (const char *, size_t);
  25. #endif
  26. char *s;
  27. s = strndup ("some longer string", 15);
  28. free (s);
  29. s = strndup ("shorter string", 13);
  30. return s[13] != '\0';]])],
  31. [gl_cv_func_strndup_works=yes],
  32. [gl_cv_func_strndup_works=no],
  33. [case $host_os in
  34. aix*) gl_cv_func_strndup_works="guessing no";;
  35. *) gl_cv_func_strndup_works="guessing yes";;
  36. esac])])
  37. case $gl_cv_func_strndup_works in
  38. *no)
  39. REPLACE_STRNDUP=1
  40. AC_LIBOBJ([strndup])
  41. ;;
  42. esac
  43. else
  44. AC_LIBOBJ([strndup])
  45. fi
  46. ])