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