maint: update copyright
[gnulib.git] / m4 / pwrite.m4
1 # pwrite.m4 serial 5
2 dnl Copyright (C) 2010-2014 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_PWRITE],
8 [
9   AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
10   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
11
12   dnl Persuade glibc <unistd.h> to declare pwrite().
13   AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
14
15   AC_CHECK_FUNCS_ONCE([pwrite])
16   if test $ac_cv_func_pwrite = yes; then
17     dnl On HP-UX 11.11 with _FILE_OFFSET_BITS=64, pwrite() on a file does not
18     dnl fail when an invalid (negative) offset is passed and uses an arbitrary
19     dnl offset instead of the argument.
20     AC_CACHE_CHECK([whether pwrite works],
21       [gl_cv_func_pwrite_works],
22       [
23         dnl Initial guess, used when cross-compiling.
24 changequote(,)dnl
25         case "$host_os" in
26                  # Guess no on HP-UX.
27           hpux*) gl_cv_func_pwrite_works="guessing no" ;;
28                  # Guess yes otherwise.
29           *)     gl_cv_func_pwrite_works="guessing yes" ;;
30         esac
31 changequote([,])dnl
32         gl_save_CPPFLAGS="$CPPFLAGS"
33         CPPFLAGS="$CPPFLAGS -D_FILE_OFFSET_BITS=64"
34         rm -f conftest.out
35         AC_RUN_IFELSE(
36           [AC_LANG_PROGRAM([[
37 #include <sys/types.h>
38 #include <unistd.h>
39 #include <fcntl.h>
40              ]],
41              [[
42 {
43   /* This test fails on HP-UX 11.00..11.11.  */
44   {
45     int fd = open ("conftest.out", O_RDWR | O_CREAT | O_TRUNC, 0600);
46     if (fd < 0)
47       return 1;
48     if (pwrite (fd, "b", 1, (off_t) -1) >= 0)
49       return 2;
50   }
51   /* This test fails on HP-UX 11.00..11.11.  */
52   {
53     int fd;
54     char buf[] = "01";
55
56     fd = open ("conftest.out", O_WRONLY | O_CREAT | O_TRUNC, 0600);
57     if (fd < 0)
58       return 3;
59     if (write (fd, buf, 2) < 2)
60       return 4;
61     if (close (fd) < 0)
62       return 5;
63
64     fd = open ("conftest.out", O_WRONLY, 0600);
65     if (fd < 0)
66       return 6;
67     if (pwrite (fd, "W", 1, (off_t) 0) != 1)
68       return 7;
69     if (close (fd) < 0)
70       return 8;
71
72     fd = open ("conftest.out", O_RDONLY);
73     if (fd < 0)
74       return 9;
75     if (read (fd, buf, 2) < 2)
76       return 10;
77     if (close (fd) < 0)
78       return 11;
79     if (buf[0] != 'W')
80       return 12;
81     if (buf[1] != '1')
82       return 13;
83   }
84   return 0;
85 }]])],
86           [gl_cv_func_pwrite_works=yes],
87           [gl_cv_func_pwrite_works=no],
88           [:])
89         rm -f conftest.out
90         CPPFLAGS="$gl_save_CPPFLAGS"
91       ])
92     case "$gl_cv_func_pwrite_works" in
93       *yes) ;;
94       *) REPLACE_PWRITE=1 ;;
95     esac
96   else
97     HAVE_PWRITE=0
98   fi
99 ])