update nearly all FSF copyright year lists to include 2010
[gnulib.git] / m4 / stpncpy.m4
1 # stpncpy.m4 serial 10
2 dnl Copyright (C) 2002-2003, 2005-2007, 2009-2010 Free Software Foundation,
3 dnl Inc.
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.
7
8 AC_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_CACHE_CHECK([for working stpncpy], [gl_cv_func_stpncpy], [
29     AC_TRY_RUN([
30 #include <stdlib.h>
31 #include <string.h> /* for strcpy */
32 /* The stpncpy prototype is missing in <string.h> on AIX 4.  */
33 extern char *stpncpy (char *dest, const char *src, size_t n);
34 int main () {
35   const char *src = "Hello";
36   char dest[10];
37   /* AIX 4.3.3 and AIX 5.1 stpncpy() returns dest+1 here.  */
38   strcpy (dest, "\377\377\377\377\377\377");
39   if (stpncpy (dest, src, 2) != dest + 2) exit(1);
40   /* AIX 4.3.3 and AIX 5.1 stpncpy() returns dest+4 here.  */
41   strcpy (dest, "\377\377\377\377\377\377");
42   if (stpncpy (dest, src, 5) != dest + 5) exit(1);
43   /* AIX 4.3.3 and AIX 5.1 stpncpy() returns dest+6 here.  */
44   strcpy (dest, "\377\377\377\377\377\377");
45   if (stpncpy (dest, src, 7) != dest + 5) exit(1);
46   exit(0);
47 }
48 ], gl_cv_func_stpncpy=yes, gl_cv_func_stpncpy=no,
49   [AC_EGREP_CPP([Thanks for using GNU], [
50 #include <features.h>
51 #ifdef __GNU_LIBRARY__
52   Thanks for using GNU
53 #endif
54 ], gl_cv_func_stpncpy=yes, gl_cv_func_stpncpy=no)])])
55
56   if test $gl_cv_func_stpncpy = yes; then
57     AC_DEFINE([HAVE_STPNCPY], [1],
58       [Define if you have the stpncpy() function and it works.])
59   else
60     HAVE_STPNCPY=0
61     AC_LIBOBJ([stpncpy])
62     gl_PREREQ_STPNCPY
63   fi
64 ])
65
66 # Prerequisites of lib/stpncpy.c.
67 AC_DEFUN([gl_PREREQ_STPNCPY], [
68   :
69 ])