2 dnl Copyright (C) 2002-2003, 2005-2007, 2009-2011 Free Software Foundation,
4 dnl This file is free software; the Free Software Foundation
5 dnl gives unlimited permission to copy and/or distribute it,
6 dnl with or without modifications, as long as this notice is preserved.
8 AC_DEFUN([gl_FUNC_STPNCPY],
10 dnl Persuade glibc <string.h> to declare stpncpy().
11 AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
13 dnl The stpncpy() declaration in lib/string.in.h uses 'restrict'.
14 AC_REQUIRE([AC_C_RESTRICT])
16 AC_REQUIRE([gl_HEADER_STRING_H_DEFAULTS])
18 dnl Both glibc and AIX (4.3.3, 5.1) have an stpncpy() function
19 dnl declared in <string.h>. Its side effects are the same as those
21 dnl stpncpy (dest, src, n)
22 dnl overwrites dest[0..n-1], min(strlen(src),n) bytes coming from src,
23 dnl and the remaining bytes being NULs. However, the return value is
24 dnl in glibc: dest + min(strlen(src),n)
25 dnl in AIX: dest + max(0,n-1)
26 dnl Only the glibc return value is useful in practice.
28 AC_CHECK_DECLS_ONCE([stpncpy])
29 AC_CHECK_FUNCS_ONCE([stpncpy])
30 if test $ac_cv_func_stpncpy = yes; then
31 AC_CACHE_CHECK([for working stpncpy], [gl_cv_func_stpncpy], [
35 #include <string.h> /* for strcpy */
36 /* The stpncpy prototype is missing in <string.h> on AIX 4. */
37 #if !HAVE_DECL_STPNCPY
38 extern char *stpncpy (char *dest, const char *src, size_t n);
43 const char *src = "Hello";
45 /* AIX 4.3.3 and AIX 5.1 stpncpy() returns dest+1 here. */
47 strcpy (dest, "\377\377\377\377\377\377");
48 if (stpncpy (dest, src, 2) != dest + 2)
51 /* AIX 4.3.3 and AIX 5.1 stpncpy() returns dest+4 here. */
53 strcpy (dest, "\377\377\377\377\377\377");
54 if (stpncpy (dest, src, 5) != dest + 5)
57 /* AIX 4.3.3 and AIX 5.1 stpncpy() returns dest+6 here. */
59 strcpy (dest, "\377\377\377\377\377\377");
60 if (stpncpy (dest, src, 7) != dest + 5)
66 [gl_cv_func_stpncpy=yes],
67 [gl_cv_func_stpncpy=no],
68 [AC_EGREP_CPP([Thanks for using GNU], [
70 #ifdef __GNU_LIBRARY__
73 ], [gl_cv_func_stpncpy=yes], [gl_cv_func_stpncpy=no])
76 if test $gl_cv_func_stpncpy = yes; then
77 AC_DEFINE([HAVE_STPNCPY], [1],
78 [Define if you have the stpncpy() function and it works.])
87 # Prerequisites of lib/stpncpy.c.
88 AC_DEFUN([gl_PREREQ_STPNCPY], [