strndup.m4 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # strndup.m4 serial 21
  2. dnl Copyright (C) 2002-2003, 2005-2013 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. HAVE_STRNDUP=1
  19. # AIX 4.3.3, AIX 5.1 have a function that fails to add the terminating '\0'.
  20. AC_CACHE_CHECK([for working strndup], [gl_cv_func_strndup_works],
  21. [AC_RUN_IFELSE([
  22. AC_LANG_PROGRAM([[#include <string.h>
  23. #include <stdlib.h>]], [[
  24. #if !HAVE_DECL_STRNDUP
  25. extern
  26. #ifdef __cplusplus
  27. "C"
  28. #endif
  29. char *strndup (const char *, size_t);
  30. #endif
  31. char *s;
  32. s = strndup ("some longer string", 15);
  33. free (s);
  34. s = strndup ("shorter string", 13);
  35. return s[13] != '\0';]])],
  36. [gl_cv_func_strndup_works=yes],
  37. [gl_cv_func_strndup_works=no],
  38. [
  39. changequote(,)dnl
  40. case $host_os in
  41. aix | aix[3-6]*) gl_cv_func_strndup_works="guessing no";;
  42. *) gl_cv_func_strndup_works="guessing yes";;
  43. esac
  44. changequote([,])dnl
  45. ])])
  46. case $gl_cv_func_strndup_works in
  47. *no) REPLACE_STRNDUP=1 ;;
  48. esac
  49. else
  50. HAVE_STRNDUP=0
  51. fi
  52. ])