pwrite: Work around HP-UX 11.11 bug.
[gnulib.git] / m4 / pwrite.m4
1 # pwrite.m4 serial 2
2 dnl Copyright (C) 2010 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.
19     AC_CACHE_CHECK([whether pwrite works],
20       [gl_cv_func_pwrite_works],
21       [
22         dnl Initial guess, used when cross-compiling.
23 changequote(,)dnl
24         case "$host_os" in
25                  # Guess no on HP-UX.
26           hpux*) gl_cv_func_pwrite_works="guessing no" ;;
27                  # Guess yes otherwise.
28           *)     gl_cv_func_pwrite_works="guessing yes" ;;
29         esac
30 changequote([,])dnl
31         gl_save_CPPFLAGS="$CPPFLAGS"
32         CPPFLAGS="$CPPFLAGS -D_FILE_OFFSET_BITS=64"
33         rm -f conftest.out
34         AC_RUN_IFELSE(
35           [AC_LANG_SOURCE([[
36 #include <sys/types.h>
37 #include <unistd.h>
38 #include <fcntl.h>
39              ]],
40              [[
41 {
42   /* This test fails on HP-UX 11.00..11.11.  */
43   {
44     int fd = open ("conftest.out", O_RDWR | O_CREAT | O_TRUNC, 0600);
45     if (fd < 0)
46       return 1;
47     if (pwrite (fd, "b", 1, (off_t) -1) >= 0)
48       return 2;
49   }
50   return 0;
51 }]])],
52           [gl_cv_func_pwrite_works=yes],
53           [gl_cv_func_pwrite_works=no],
54           [:])
55         rm -f conftest.out
56         CPPFLAGS="$gl_save_CPPFLAGS"
57       ])
58     case "$gl_cv_func_pwrite_works" in
59       *yes) ;;
60       *) REPLACE_PWRITE=1 ;;
61     esac
62   else
63     HAVE_PWRITE=0
64   fi
65   if test $HAVE_PWRITE = 0 || test $REPLACE_PWRITE = 1; then
66     AC_LIBOBJ([pwrite])
67   fi
68 ])