pwrite: Work around HP-UX 11.11 bug.
authorBruno Haible <bruno@clisp.org>
Sat, 1 Jan 2011 01:25:00 +0000 (02:25 +0100)
committerBruno Haible <bruno@clisp.org>
Sat, 1 Jan 2011 01:25:00 +0000 (02:25 +0100)
* m4/pwrite.m4 (gl_FUNC_PWRITE): When pwrite exists, test whether it
works and set REPLACE_PWRITE if not.
* lib/pwrite.c (pwrite): Add an implementation that uses the system
function.
* doc/posix-functions/pwrite.texi: Document the HP-UX 11 bug.

ChangeLog
doc/posix-functions/pwrite.texi
lib/pwrite.c
m4/pwrite.m4

index 6c7d453..f3dcc8f 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,14 @@
 2010-12-31  Bruno Haible  <bruno@clisp.org>
 
+       pwrite: Work around HP-UX 11.11 bug.
+       * m4/pwrite.m4 (gl_FUNC_PWRITE): When pwrite exists, test whether it
+       works and set REPLACE_PWRITE if not.
+       * lib/pwrite.c (pwrite): Add an implementation that uses the system
+       function.
+       * doc/posix-functions/pwrite.texi: Document the HP-UX 11 bug.
+
+2010-12-31  Bruno Haible  <bruno@clisp.org>
+
        pread: Work around HP-UX 11 bugs.
        * m4/pread.m4 (gl_FUNC_PREAD): When pread exists, test whether it works
        and set REPLACE_PREAD if not.
index 24fb4fb..7204821 100644 (file)
@@ -11,6 +11,10 @@ Portability problems fixed by Gnulib:
 @item
 This function is missing on some platforms:
 HP-UX 10, mingw, BeOS.
+@item
+This function does not fail when an invalid (negative) offset is passed when
+large file support is enabled on some platforms:
+HP-UX 11.11.
 @end itemize
 
 Portability problems not fixed by Gnulib:
index 0071479..4a91357 100644 (file)
 
 #include <errno.h>
 
-#define __libc_lseek(f,o,w) lseek (f, o, w)
-#define __set_errno(Val) errno = (Val)
-#define __libc_write(f,b,n) write (f, b, n)
+#if HAVE_PWRITE
+
+ssize_t
+pwrite (int fd, const void *buf, size_t nbyte, off_t offset)
+# undef pwrite
+{
+  if (offset < 0)
+    {
+      errno = EINVAL;
+      return -1;
+    }
+  return pwrite (fd, buf, nbyte, offset);
+}
+
+#else
+
+# define __libc_lseek(f,o,w) lseek (f, o, w)
+# define __set_errno(Val) errno = (Val)
+# define __libc_write(f,b,n) write (f, b, n)
 
 /* Note: This implementation of pwrite is not multithread-safe.  */
 
@@ -62,3 +78,5 @@ pwrite (int fd, const void *buf, size_t nbyte, off_t offset)
 
   return result;
 }
+
+#endif
index 90d8cac..4814c5d 100644 (file)
@@ -1,4 +1,4 @@
-# pwrite.m4 serial 1
+# pwrite.m4 serial 2
 dnl Copyright (C) 2010 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -7,13 +7,62 @@ dnl with or without modifications, as long as this notice is preserved.
 AC_DEFUN([gl_FUNC_PWRITE],
 [
   AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
+  AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
 
   dnl Persuade glibc <unistd.h> to declare pwrite().
   AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
 
   AC_CHECK_FUNCS_ONCE([pwrite])
-  if test $ac_cv_func_pwrite = no; then
+  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
+    dnl fail when an invalid (negative) offset is passed.
+    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(
+          [AC_LANG_SOURCE([[
+#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;
+  }
+  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
     HAVE_PWRITE=0
+  fi
+  if test $HAVE_PWRITE = 0 || test $REPLACE_PWRITE = 1; then
     AC_LIBOBJ([pwrite])
   fi
 ])