strndup.m4 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # strndup.m4 serial 22
  2. dnl Copyright (C) 2002-2003, 2005-2017 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. int result;
  32. char *s;
  33. s = strndup ("some longer string", 15);
  34. free (s);
  35. s = strndup ("shorter string", 13);
  36. result = s[13] != '\0';
  37. free (s);
  38. return result;]])],
  39. [gl_cv_func_strndup_works=yes],
  40. [gl_cv_func_strndup_works=no],
  41. [
  42. changequote(,)dnl
  43. case $host_os in
  44. aix | aix[3-6]*) gl_cv_func_strndup_works="guessing no";;
  45. *) gl_cv_func_strndup_works="guessing yes";;
  46. esac
  47. changequote([,])dnl
  48. ])])
  49. case $gl_cv_func_strndup_works in
  50. *no) REPLACE_STRNDUP=1 ;;
  51. esac
  52. else
  53. HAVE_STRNDUP=0
  54. fi
  55. ])