changeset 16116:f66a61056b41

init.sh: work around OSF/1 5.1's mishandling of /dev/null * tests/init.sh: Make our compare function slightly more portable. Reported by Bruno Haible in http://thread.gmane.org/gmane.comp.gnu.grep.bugs/4020 Much improved by Eric Blake.
author Jim Meyering <meyering@redhat.com>
date Mon, 21 Nov 2011 21:50:23 +0100
parents 15d46238c9b0
children 58ec7181ad64
files ChangeLog tests/init.sh
diffstat 2 files changed, 49 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2011-11-21  Jim Meyering  <meyering@redhat.com>
+	    Eric Blake  <eblake@redhat.com>
+
+	init.sh: work around OSF/1 5.1's mishandling of /dev/null
+	* tests/init.sh: Make our compare function slightly more portable.
+	Reported by Bruno Haible in
+	http://thread.gmane.org/gmane.comp.gnu.grep.bugs/4020
+
 2011-11-21  Simon Josefsson  <simon@josefsson.org>
 
 	* m4/gnulib-common.m4 (_Noreturn): Check that _MSC_VER is defined
--- a/tests/init.sh
+++ b/tests/init.sh
@@ -221,11 +221,35 @@
 # a partition, or to undo any other global state changes.
 cleanup_ () { :; }
 
+# Arrange not to let diff or cmp operate on /dev/null,
+# since on some systems (at least OSF/1 5.1), that doesn't work.
+# When there are not two arguments, or no argument is /dev/null, return 2.
+# When one argument is /dev/null and the other is not empty,
+# cat the nonempty file to stderr and return 1.
+# Otherwise, return 0.
+compare_dev_null_ ()
+{
+  test $# = 2 || return 2
+
+  if test "x$1" = x/dev/null; then
+    set dummy "$2" "$1"; shift
+  fi
+
+  test "x$2" = x/dev/null || return 2
+
+  test -s "$1" || return 0
+
+  cat - "$1" <<EOF >&2
+Unexpected contents of $1:
+EOF
+  return 1
+}
+
 if diff_out_=`( diff -u "$0" "$0" < /dev/null ) 2>/dev/null`; then
   if test -z "$diff_out_"; then
-    compare () { diff -u "$@"; }
+    compare_ () { diff -u "$@"; }
   else
-    compare ()
+    compare_ ()
     {
       if diff -u "$@" > diff.out; then
         # No differences were found, but Solaris 'diff' produces output
@@ -241,9 +265,9 @@
   fi
 elif diff_out_=`( diff -c "$0" "$0" < /dev/null ) 2>/dev/null`; then
   if test -z "$diff_out_"; then
-    compare () { diff -c "$@"; }
+    compare_ () { diff -c "$@"; }
   else
-    compare ()
+    compare_ ()
     {
       if diff -c "$@" > diff.out; then
         # No differences were found, but AIX and HP-UX 'diff' produce output
@@ -259,11 +283,22 @@
     }
   fi
 elif ( cmp --version < /dev/null 2>&1 | grep GNU ) > /dev/null 2>&1; then
-  compare () { cmp -s "$@"; }
+  compare_ () { cmp -s "$@"; }
 else
-  compare () { cmp "$@"; }
+  compare_ () { cmp "$@"; }
 fi
 
+# Given compare_dev_null_'s preprocessing, defer to compare_ if 2 or more.
+# Otherwise, propagate $? to caller: any diffs have already been printed.
+compare ()
+{
+  compare_dev_null_ "$@"
+  case $? in
+    0|1) return $?;;
+    *) compare_ "$@";;
+  esac
+}
+
 # An arbitrary prefix to help distinguish test directories.
 testdir_prefix_ () { printf gt; }