From 918dbc6bb3fb93c4e4417a48dad2fb25b8f886c0 Mon Sep 17 00:00:00 2001 From: Mingye Wang Date: Sat, 27 May 2023 21:28:23 +0800 Subject: [PATCH] bsdunzip: make it possible to pass filenames starting with dash We now check for residual arguments after the second scan, so any filename protected by `--` is picked up. --- unzip/bsdunzip.1 | 13 +++++++++++++ unzip/bsdunzip.c | 8 ++++++++ 2 files changed, 21 insertions(+) diff --git a/unzip/bsdunzip.1 b/unzip/bsdunzip.1 index 1bf76ead3..3c656ebc4 100644 --- a/unzip/bsdunzip.1 +++ b/unzip/bsdunzip.1 @@ -201,3 +201,16 @@ It uses the .Xr archive 3 library developed by .An Tim Kientzle Aq Mt kientzle@FreeBSD.org . +.Sh CAVEATS +The +.Nm +utility performs two scans of the command-line for arguments before +and after the archive name, so as to maintain compatibility with +Info-ZIP unzip. +As a result, the POSIX +.Ql -- +double-dash string used to separate options from arguments will need to +be repeated. +For example, to extract a "-a.jpg" from "-b.zip" with overwrite, one +would need to invoke +.Dl bsdunzip -o -- -a.jpg -- -b.zip diff --git a/unzip/bsdunzip.c b/unzip/bsdunzip.c index 1d9a99b51..e1b9ca5aa 100644 --- a/unzip/bsdunzip.c +++ b/unzip/bsdunzip.c @@ -1114,6 +1114,10 @@ main(int argc, char *argv[]) for (int i = 0; i < argc; ++i) debug("%s%c", argv[i], (i < argc - 1) ? ' ' : '\n'); +#ifdef __GLIBC__ + /* Prevent GNU getopt(3) from rearranging options. */ + setenv("POSIXLY_CORRECT", ""); +#endif /* * Info-ZIP's unzip(1) expects certain options to come before the * zipfile name, and others to come after - though it does not @@ -1144,6 +1148,10 @@ main(int argc, char *argv[]) nopts--; /* fake argv[0] */ nopts += getopts(argc - nopts, argv + nopts); + /* There may be residual arguments if we encountered -- */ + while (nopts < argc) + add_pattern(&include, argv[nopts++]); + if (n_opt + o_opt + u_opt > 1) errorx("-n, -o and -u are contradictory"); -- 2.47.2