Skip to content

Commit 918dbc6

Browse files
Artoria2e5mmatuska
authored andcommitted
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.
1 parent c157e4c commit 918dbc6

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

unzip/bsdunzip.1

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,16 @@ It uses the
201201
.Xr archive 3
202202
library developed by
203203
.An Tim Kientzle Aq Mt kientzle@FreeBSD.org .
204+
.Sh CAVEATS
205+
The
206+
.Nm
207+
utility performs two scans of the command-line for arguments before
208+
and after the archive name, so as to maintain compatibility with
209+
Info-ZIP unzip.
210+
As a result, the POSIX
211+
.Ql --
212+
double-dash string used to separate options from arguments will need to
213+
be repeated.
214+
For example, to extract a "-a.jpg" from "-b.zip" with overwrite, one
215+
would need to invoke
216+
.Dl bsdunzip -o -- -a.jpg -- -b.zip

unzip/bsdunzip.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,10 @@ main(int argc, char *argv[])
11141114
for (int i = 0; i < argc; ++i)
11151115
debug("%s%c", argv[i], (i < argc - 1) ? ' ' : '\n');
11161116

1117+
#ifdef __GLIBC__
1118+
/* Prevent GNU getopt(3) from rearranging options. */
1119+
setenv("POSIXLY_CORRECT", "");
1120+
#endif
11171121
/*
11181122
* Info-ZIP's unzip(1) expects certain options to come before the
11191123
* zipfile name, and others to come after - though it does not
@@ -1144,6 +1148,10 @@ main(int argc, char *argv[])
11441148
nopts--; /* fake argv[0] */
11451149
nopts += getopts(argc - nopts, argv + nopts);
11461150

1151+
/* There may be residual arguments if we encountered -- */
1152+
while (nopts < argc)
1153+
add_pattern(&include, argv[nopts++]);
1154+
11471155
if (n_opt + o_opt + u_opt > 1)
11481156
errorx("-n, -o and -u are contradictory");
11491157

0 commit comments

Comments
 (0)