1# strndup.m4 serial 13
2dnl Copyright (C) 2002-2003, 2005-2007 Free Software Foundation, Inc.
3dnl This file is free software; the Free Software Foundation
4dnl gives unlimited permission to copy and/or distribute it,
5dnl with or without modifications, as long as this notice is preserved.
6
7AC_DEFUN([gl_FUNC_STRNDUP],
8[
9  dnl Persuade glibc <string.h> to declare strndup().
10  AC_REQUIRE([AC_GNU_SOURCE])
11
12  AC_REQUIRE([gl_HEADER_STRING_H_DEFAULTS])
13  AC_CHECK_DECLS_ONCE([strndup])
14  if test $ac_cv_have_decl_strndup = no; then
15    HAVE_DECL_STRNDUP=0
16  fi
17
18  # AIX 4.3.3, AIX 5.1 have a function that fails to add the terminating '\0'.
19  AC_CACHE_CHECK([for working strndup], gl_cv_func_strndup,
20    [AC_RUN_IFELSE([
21       AC_LANG_PROGRAM([#include <string.h>
22		 	#include <stdlib.h>], [[
23#ifndef HAVE_DECL_STRNDUP
24  extern char *strndup (const char *, size_t);
25#endif
26  char *s;
27  s = strndup ("some longer string", 15);
28  free (s);
29  s = strndup ("shorter string", 13);
30  return s[13] != '\0';]])],
31       [gl_cv_func_strndup=yes],
32       [gl_cv_func_strndup=no],
33       [AC_CHECK_FUNC([strndup],
34          [AC_EGREP_CPP([too risky], [
35#ifdef _AIX
36               too risky
37#endif
38             ],
39             [gl_cv_func_strndup=no],
40             [gl_cv_func_strndup=yes])],
41          [gl_cv_func_strndup=no])])])
42  if test $gl_cv_func_strndup = yes; then
43    AC_DEFINE([HAVE_STRNDUP], 1,
44      [Define if you have the strndup() function and it works.])
45  else
46    HAVE_STRNDUP=0
47    AC_LIBOBJ([strndup])
48    gl_PREREQ_STRNDUP
49  fi
50])
51
52# Prerequisites of lib/strndup.c.
53AC_DEFUN([gl_PREREQ_STRNDUP], [:])
54