1# stpncpy.m4 serial 16
2dnl Copyright (C) 2002-2003, 2005-2007, 2009-2014 Free Software Foundation,
3dnl Inc.
4dnl This file is free software; the Free Software Foundation
5dnl gives unlimited permission to copy and/or distribute it,
6dnl with or without modifications, as long as this notice is preserved.
7
8AC_DEFUN([gl_FUNC_STPNCPY],
9[
10  dnl Persuade glibc <string.h> to declare stpncpy().
11  AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
12
13  dnl The stpncpy() declaration in lib/string.in.h uses 'restrict'.
14  AC_REQUIRE([AC_C_RESTRICT])
15
16  AC_REQUIRE([gl_HEADER_STRING_H_DEFAULTS])
17
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
20  dnl of strncpy():
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.
27
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], [
32      AC_RUN_IFELSE(
33        [AC_LANG_SOURCE([[
34#include <stdlib.h>
35#include <string.h> /* for strcpy */
36/* The stpncpy prototype is missing in <string.h> on AIX 4.  */
37#if !HAVE_DECL_STPNCPY
38extern
39# ifdef __cplusplus
40"C"
41# endif
42char *stpncpy (char *dest, const char *src, size_t n);
43#endif
44int main ()
45{
46  int result = 0;
47  const char *src = "Hello";
48  char dest[10];
49  /* AIX 4.3.3 and AIX 5.1 stpncpy() returns dest+1 here.  */
50  {
51    strcpy (dest, "\377\377\377\377\377\377");
52    if (stpncpy (dest, src, 2) != dest + 2)
53      result |= 1;
54  }
55  /* AIX 4.3.3 and AIX 5.1 stpncpy() returns dest+4 here.  */
56  {
57    strcpy (dest, "\377\377\377\377\377\377");
58    if (stpncpy (dest, src, 5) != dest + 5)
59      result |= 2;
60  }
61  /* AIX 4.3.3 and AIX 5.1 stpncpy() returns dest+6 here.  */
62  {
63    strcpy (dest, "\377\377\377\377\377\377");
64    if (stpncpy (dest, src, 7) != dest + 5)
65      result |= 4;
66  }
67  return result;
68}
69]])],
70        [gl_cv_func_stpncpy=yes],
71        [gl_cv_func_stpncpy=no],
72        [AC_EGREP_CPP([Thanks for using GNU], [
73#include <features.h>
74#ifdef __GNU_LIBRARY__
75  Thanks for using GNU
76#endif
77], [gl_cv_func_stpncpy="guessing yes"], [gl_cv_func_stpncpy="guessing no"])
78        ])
79    ])
80    case "$gl_cv_func_stpncpy" in
81      *yes)
82        AC_DEFINE([HAVE_STPNCPY], [1],
83          [Define if you have the stpncpy() function and it works.])
84        ;;
85      *)
86        REPLACE_STPNCPY=1
87        ;;
88    esac
89  else
90    HAVE_STPNCPY=0
91  fi
92])
93
94# Prerequisites of lib/stpncpy.c.
95AC_DEFUN([gl_PREREQ_STPNCPY], [
96  :
97])
98