]> git.cworth.org Git - tar/blob - m4/strndup.m4
Imported Upstream version 1.24
[tar] / m4 / strndup.m4
1 # strndup.m4 serial 18
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
7 AC_DEFUN([gl_FUNC_STRNDUP],
8 [
9   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   if test $ac_cv_have_decl_strndup = no; then
17     HAVE_DECL_STRNDUP=0
18   fi
19
20   if test $ac_cv_func_strndup = yes; then
21     # AIX 4.3.3, AIX 5.1 have a function that fails to add the terminating '\0'.
22     AC_CACHE_CHECK([for working strndup], [gl_cv_func_strndup_works],
23       [AC_RUN_IFELSE([
24          AC_LANG_PROGRAM([[#include <string.h>
25                            #include <stdlib.h>]], [[
26 #ifndef HAVE_DECL_STRNDUP
27   extern char *strndup (const char *, size_t);
28 #endif
29   char *s;
30   s = strndup ("some longer string", 15);
31   free (s);
32   s = strndup ("shorter string", 13);
33   return s[13] != '\0';]])],
34          [gl_cv_func_strndup_works=yes],
35          [gl_cv_func_strndup_works=no],
36          [
37 changequote(,)dnl
38           case $host_os in
39             aix | aix[3-6]*) gl_cv_func_strndup_works="guessing no";;
40             *)               gl_cv_func_strndup_works="guessing yes";;
41           esac
42 changequote([,])dnl
43          ])])
44     case $gl_cv_func_strndup_works in
45       *no)
46         REPLACE_STRNDUP=1
47         AC_LIBOBJ([strndup])
48         ;;
49     esac
50   else
51     AC_LIBOBJ([strndup])
52   fi
53 ])