pwrite.m4 2.62 KB
Newer Older
1
# pwrite.m4 serial 5
2
dnl Copyright (C) 2010-2018 Free Software Foundation, Inc.
Peter O'Gorman's avatar
Peter O'Gorman committed
3 4 5 6 7 8 9
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
dnl with or without modifications, as long as this notice is preserved.

AC_DEFUN([gl_FUNC_PWRITE],
[
  AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
10
  AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
Peter O'Gorman's avatar
Peter O'Gorman committed
11 12 13 14 15

  dnl Persuade glibc <unistd.h> to declare pwrite().
  AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])

  AC_CHECK_FUNCS_ONCE([pwrite])
16 17
  if test $ac_cv_func_pwrite = yes; then
    dnl On HP-UX 11.11 with _FILE_OFFSET_BITS=64, pwrite() on a file does not
18 19
    dnl fail when an invalid (negative) offset is passed and uses an arbitrary
    dnl offset instead of the argument.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
    AC_CACHE_CHECK([whether pwrite works],
      [gl_cv_func_pwrite_works],
      [
        dnl Initial guess, used when cross-compiling.
changequote(,)dnl
        case "$host_os" in
                 # Guess no on HP-UX.
          hpux*) gl_cv_func_pwrite_works="guessing no" ;;
                 # Guess yes otherwise.
          *)     gl_cv_func_pwrite_works="guessing yes" ;;
        esac
changequote([,])dnl
        gl_save_CPPFLAGS="$CPPFLAGS"
        CPPFLAGS="$CPPFLAGS -D_FILE_OFFSET_BITS=64"
        rm -f conftest.out
        AC_RUN_IFELSE(
36
          [AC_LANG_PROGRAM([[
37 38 39 40 41 42 43 44 45 46 47 48 49 50
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
             ]],
             [[
{
  /* This test fails on HP-UX 11.00..11.11.  */
  {
    int fd = open ("conftest.out", O_RDWR | O_CREAT | O_TRUNC, 0600);
    if (fd < 0)
      return 1;
    if (pwrite (fd, "b", 1, (off_t) -1) >= 0)
      return 2;
  }
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
  /* This test fails on HP-UX 11.00..11.11.  */
  {
    int fd;
    char buf[] = "01";

    fd = open ("conftest.out", O_WRONLY | O_CREAT | O_TRUNC, 0600);
    if (fd < 0)
      return 3;
    if (write (fd, buf, 2) < 2)
      return 4;
    if (close (fd) < 0)
      return 5;

    fd = open ("conftest.out", O_WRONLY, 0600);
    if (fd < 0)
      return 6;
    if (pwrite (fd, "W", 1, (off_t) 0) != 1)
      return 7;
    if (close (fd) < 0)
      return 8;

    fd = open ("conftest.out", O_RDONLY);
    if (fd < 0)
      return 9;
    if (read (fd, buf, 2) < 2)
      return 10;
    if (close (fd) < 0)
      return 11;
    if (buf[0] != 'W')
      return 12;
    if (buf[1] != '1')
      return 13;
  }
84 85 86 87 88 89 90 91 92 93 94 95 96
  return 0;
}]])],
          [gl_cv_func_pwrite_works=yes],
          [gl_cv_func_pwrite_works=no],
          [:])
        rm -f conftest.out
        CPPFLAGS="$gl_save_CPPFLAGS"
      ])
    case "$gl_cv_func_pwrite_works" in
      *yes) ;;
      *) REPLACE_PWRITE=1 ;;
    esac
  else
Peter O'Gorman's avatar
Peter O'Gorman committed
97
    HAVE_PWRITE=0
98
  fi
Peter O'Gorman's avatar
Peter O'Gorman committed
99
])