Avoid the need for AC_LIBSOURCES in m4 macros.
[gnulib.git] / m4 / strndup.m4
1 # strndup.m4 serial 8
2 dnl Copyright (C) 2002-2003, 2005-2006 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_GNU_SOURCE])
11
12   AC_CHECK_DECLS_ONCE([strndup])
13
14   # AIX 4.3.3, AIX 5.1 have a function that fails to add the terminating '\0'.
15   AC_CACHE_CHECK([for working strndup], gl_cv_func_strndup,
16     [AC_RUN_IFELSE([
17        AC_LANG_PROGRAM([#include <string.h>], [[
18 #ifndef HAVE_DECL_STRNDUP
19   extern char *strndup (const char *, size_t);
20 #endif
21   char *s;
22   s = strndup ("some longer string", 15);
23   free (s);
24   s = strndup ("shorter string", 13);
25   return s[13] != '\0';]])],
26        [gl_cv_func_strndup=yes],
27        [gl_cv_func_strndup=no],
28        [AC_CHECK_FUNC([strndup],
29           [AC_EGREP_CPP([too risky], [
30 #ifdef _AIX
31                too risky
32 #endif
33              ],
34              [gl_cv_func_strndup=no],
35              [gl_cv_func_strndup=yes])],
36           [gl_cv_func_strndup=no])])])
37   if test $gl_cv_func_strndup = yes; then
38     AC_DEFINE([HAVE_STRNDUP], 1,
39       [Define if you have the strndup() function and it works.])
40   else
41     AC_LIBOBJ([strndup])
42     AC_DEFINE(strndup, rpl_strndup,
43       [Define to rpl_strndup if the replacement function should be used,])
44     gl_PREREQ_STRNDUP
45   fi
46 ])
47
48 # Prerequisites of lib/strndup.c.
49 AC_DEFUN([gl_PREREQ_STRNDUP], [:])