Subject: Re: bug#54291: zdiff can not handle options properly

From: Paul Eggert <[email-masked]>
Date: Mon, 7 Mar 2022 09:44:06 -0800

On 3/7/22 01:32, Lv Ying via GNU gzip discussion and bug reports. wrote:

> 2. rewrite options parse function make it work just like diff.

Thanks for reporting the problem. I installed the attached to implement 
the above suggestion, and this should appear in the next gzip release.
From 40d96d894003f4a6ce07bafbb7ff61450ec9d7bc Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email-masked]>
Date: Mon, 7 Mar 2022 09:38:08 -0800
Subject: [PATCH] zdiff: fix arg handling bug

Problem reported by Lv Ying (Bug#54291).
* zdiff.in (escape): Handle args like '-C 5'.
---
 NEWS     |  4 ++++
 zdiff.in | 11 ++++++++++-
 2 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/NEWS b/NEWS
index 19049f8..05b4166 100644
--- a/NEWS
+++ b/NEWS
@@ -12,6 +12,10 @@ GNU gzip NEWS                                    -*- outline -*-
   much more time, nowadays the correctness pros seem to outweigh the
   performance cons.
 
+** Bug fixes
+
+  'zdiff -C 5' no longer misbehaves by treating '5' as a file name.
+
 
 * Noteworthy changes in release 1.11 (2021-09-03) [stable]
 
diff --git a/zdiff.in b/zdiff.in
index 7c92251..d982d9f 100644
--- a/zdiff.in
+++ b/zdiff.in
@@ -49,14 +49,23 @@ escape='
 '
 
 while :; do
+  argdone=:
   case $1 in
   --h*) printf '%s\n' "$usage"   || exit 2; exit;;
   --v*) printf '%s\n' "$version" || exit 2; exit;;
   --) shift; break;;
-  -*\'*) cmp="$cmp '"`printf '%sX\n' "$1" | sed "$escape"`;;
+  -*\'*) argdone=false arg=$1;;
+  -[CDFISUWXx])
+    cmp="$cmp $1"
+    case ${2?} in
+    -*\'*) argdone=false arg=$2;;
+    *) cmp="$cmp '$2'";;
+    esac
+    shift;;
   -?*) cmp="$cmp '$1'";;
   *) break;;
   esac
+  $argdone || cmp="$cmp '"`printf '%sX\n' "$arg" | sed "$escape"`
   shift
 done
 cmp="$cmp --"
-- 
2.32.0