Subject: Re: bug#30000: OS X 10.5, gzip 1.8 and two failed self tests
From: Paul Eggert <[email-masked]>
To: [email-masked], [email-masked]
Date: Sat, 6 Jan 2018 15:04:32 -0800
List-Id: <debbugs-submit.debbugs.gnu.org>

Thanks for reporting that. I see the problem even in the latest master, and 
installed the attached patch. Please give it a try. I'm boldly closing this bug 
report; if I'm wrong and this patch doesn't fix things we can reopen the bug report.
From 201f73179c5b1c3baa5bdb892e3aaa1a4b17e3ba Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email-masked]>
Date: Sat, 6 Jan 2018 14:59:47 -0800
Subject: [PATCH] gzexe: port to macOS mktemp
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Problem reported by Jeffrey Walton (Bug#30000).
* gzexe.in, zdiff.in, zgrep.in:
Don’t assume that mktemp works when given no arguments,
as this is not true for macOS.  Instead, use a syntax
that should work with coreutils, NetBSD, and macOS.
While we’re in the neighborhood, be more consistent about how program
names are used in temporary file names.  Also, watch out for TMPDIR
values that are not absolute file names, since they can cause problems
(e.g., leading "-").
---
 gzexe.in | 17 +++++++++++++----
 zdiff.in | 11 ++++++++---
 zgrep.in |  9 +++++++--
 3 files changed, 28 insertions(+), 9 deletions(-)

diff --git a/gzexe.in b/gzexe.in
index 62f8960..6c61183 100644
--- a/gzexe.in
+++ b/gzexe.in
@@ -119,10 +119,14 @@ for i do
     type mktemp >/dev/null 2>&1
     mktemp_status=$?
   fi
+  case $dir in
+    */) ;;
+    *) dir=$dir/;;
+  esac
   if test $mktemp_status -eq 0; then
-    tmp=`TMPDIR=$dir mktemp`
+    tmp=`mktemp "${dir}gzexeXXXXXXXXX"`
   else
-    tmp=$dir/gzexe$$
+    tmp=${dir}gzexe$$
   fi && { cp -p "$file" "$tmp" 2>/dev/null || cp "$file" "$tmp"; } || {
     res=$?
     printf >&2 '%s\n' "$0: cannot copy $file"
@@ -157,10 +161,15 @@ trap 'res=$?
   (exit $res); exit $res
 ' 0 1 2 3 5 10 13 15
 
+case $TMPDIR in
+  / | /*/) ;;
+  /*) TMPDIR=$TMPDIR/;;
+  *) TMPDIR=/tmp/;;
+esac
 if type mktemp >/dev/null 2>&1; then
-  gztmpdir=`mktemp -d`
+  gztmpdir=`mktemp -d "${TMPDIR}gztmpXXXXXXXXX"`
 else
-  gztmpdir=/tmp/gztmp$$; mkdir $gztmpdir
+  gztmpdir=${TMPDIR}gztmp$$; mkdir $gztmpdir
 fi || { (exit 127); exit 127; }
 
 gztmp=$gztmpdir/$0
diff --git a/zdiff.in b/zdiff.in
index bf644dd..960bf86 100644
--- a/zdiff.in
+++ b/zdiff.in
@@ -116,12 +116,17 @@ elif test $# -eq 2; then
                           test -n "$tmp" && rm -f "$tmp"
                           (exit 2); exit 2
                         ' HUP INT PIPE TERM 0
+                        case $TMPDIR in
+                          / | /*/) ;;
+                          /*) TMPDIR=$TMPDIR/;;
+                          *) TMPDIR=/tmp/;;
+                        esac
                         if type mktemp >/dev/null 2>&1; then
-                          tmp=`mktemp` || exit 2
+                          tmp=`mktemp "${TMPDIR}zdiffXXXXXXXXX"` ||
+                            exit 2
                         else
-                          F=`expr "/$2" : '.*/\(.*\)[-.][zZtga]*$'` || F=$prog
                           set -C
-                          tmp=${TMPDIR-/tmp}/$F.$$
+                          tmp=${TMPDIR}zdiff$$
                         fi
                         gzip -cdfq -- "$2" > "$tmp" || exit 2
                         gzip_status=$(
diff --git a/zgrep.in b/zgrep.in
index 3e08452..7a4a2d4 100644
--- a/zgrep.in
+++ b/zgrep.in
@@ -133,11 +133,16 @@ while test $# -ne 0; do
         test -n "$pattmp" && rm -f "$pattmp"
         (exit 2); exit 2
       ' HUP INT PIPE TERM 0
+      case $TMPDIR in
+        / | /*/) ;;
+        /*) TMPDIR=$TMPDIR/;;
+        *) TMPDIR=/tmp/;;
+      esac
       if type mktemp >/dev/null 2>&1; then
-        pattmp=$(mktemp) || exit 2
+        pattmp=$(mktemp "${TMPDIR}zgrepXXXXXXXXX") || exit 2
       else
         set -C
-        pattmp=${TMPDIR-/tmp}/zgrep.$$
+        pattmp=${TMPDIR}zgrep$$
       fi
       eval "cat --$optarg" >"$pattmp" || exit 2
       optarg=' "$pattmp"'
-- 
2.14.3