many *.m4 files: improve m4 quoting
[gnulib.git] / m4 / stpncpy.m4
1 # stpncpy.m4 serial 10
2 dnl Copyright (C) 2002-2003, 2005-2007, 2009 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_STPNCPY],
8 [
9   dnl Persuade glibc <string.h> to declare stpncpy().
10   AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
11
12   dnl The stpncpy() declaration in lib/string.in.h uses 'restrict'.
13   AC_REQUIRE([AC_C_RESTRICT])
14
15   AC_REQUIRE([gl_HEADER_STRING_H_DEFAULTS])
16
17   dnl Both glibc and AIX (4.3.3, 5.1) have an stpncpy() function
18   dnl declared in <string.h>. Its side effects are the same as those
19   dnl of strncpy():
20   dnl      stpncpy (dest, src, n)
21   dnl overwrites dest[0..n-1], min(strlen(src),n) bytes coming from src,
22   dnl and the remaining bytes being NULs.  However, the return value is
23   dnl   in glibc:   dest + min(strlen(src),n)
24   dnl   in AIX:     dest + max(0,n-1)
25   dnl Only the glibc return value is useful in practice.
26
27   AC_CACHE_CHECK([for working stpncpy], [gl_cv_func_stpncpy], [
28     AC_TRY_RUN([
29 #include <stdlib.h>
30 #include <string.h> /* for strcpy */
31 /* The stpncpy prototype is missing in <string.h> on AIX 4.  */
32 extern char *stpncpy (char *dest, const char *src, size_t n);
33 int main () {
34   const char *src = "Hello";
35   char dest[10];
36   /* AIX 4.3.3 and AIX 5.1 stpncpy() returns dest+1 here.  */
37   strcpy (dest, "\377\377\377\377\377\377");
38   if (stpncpy (dest, src, 2) != dest + 2) exit(1);
39   /* AIX 4.3.3 and AIX 5.1 stpncpy() returns dest+4 here.  */
40   strcpy (dest, "\377\377\377\377\377\377");
41   if (stpncpy (dest, src, 5) != dest + 5) exit(1);
42   /* AIX 4.3.3 and AIX 5.1 stpncpy() returns dest+6 here.  */
43   strcpy (dest, "\377\377\377\377\377\377");
44   if (stpncpy (dest, src, 7) != dest + 5) exit(1);
45   exit(0);
46 }
47 ], gl_cv_func_stpncpy=yes, gl_cv_func_stpncpy=no,
48   [AC_EGREP_CPP([Thanks for using GNU], [
49 #include <features.h>
50 #ifdef __GNU_LIBRARY__
51   Thanks for using GNU
52 #endif
53 ], gl_cv_func_stpncpy=yes, gl_cv_func_stpncpy=no)])])
54
55   if test $gl_cv_func_stpncpy = yes; then
56     AC_DEFINE([HAVE_STPNCPY], [1],
57       [Define if you have the stpncpy() function and it works.])
58   else
59     HAVE_STPNCPY=0
60     AC_LIBOBJ([stpncpy])
61     gl_PREREQ_STPNCPY
62   fi
63 ])
64
65 # Prerequisites of lib/stpncpy.c.
66 AC_DEFUN([gl_PREREQ_STPNCPY], [
67   :
68 ])