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