Skip to content

Commit a8578c0

Browse files
committed
unzip: support --version argument
1 parent 46063a5 commit a8578c0

8 files changed

Lines changed: 81 additions & 4 deletions

File tree

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ SET(VERSION "${_major}.${_trimmed_minor}.${_trimmed_revision}
6565
SET(BSDCPIO_VERSION_STRING "${VERSION}")
6666
SET(BSDTAR_VERSION_STRING "${VERSION}")
6767
SET(BSDCAT_VERSION_STRING "${VERSION}")
68+
SET(BSDUNZIP_VERSION_STRING "${VERSION}")
6869
SET(LIBARCHIVE_VERSION_NUMBER "${_version_number}")
6970
SET(LIBARCHIVE_VERSION_STRING "${VERSION}")
7071

@@ -695,6 +696,7 @@ int main(void) { return EXT2_IOC_GETFLAGS; }" HAVE_WORKING_EXT2_IOC_GETFLAGS)
695696

696697
LA_CHECK_INCLUDE_FILE("fcntl.h" HAVE_FCNTL_H)
697698
LA_CHECK_INCLUDE_FILE("fnmatch.h" HAVE_FNMATCH_H)
699+
LA_CHECK_INCLUDE_FILE("getopt.h" HAVE_GETOPT_H)
698700
LA_CHECK_INCLUDE_FILE("grp.h" HAVE_GRP_H)
699701
LA_CHECK_INCLUDE_FILE("inttypes.h" HAVE_INTTYPES_H)
700702
LA_CHECK_INCLUDE_FILE("io.h" HAVE_IO_H)

Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,6 +1510,7 @@ bsdunzip_test_SOURCES= \
15101510
unzip/test/test_q.c \
15111511
unzip/test/test_t.c \
15121512
unzip/test/test_t_bad.c \
1513+
unzip/test/test_version.c \
15131514
unzip/test/test_x.c \
15141515
unzip/test/test_Z1.c \
15151516
unzip/test/test_P_encryption.c

build/cmake/config.h.in

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,9 @@ typedef uint64_t uintmax_t;
342342
/* Version number of bsdcat */
343343
#cmakedefine BSDCAT_VERSION_STRING "@BSDCAT_VERSION_STRING@"
344344

345+
/* Version number of bsdunzip */
346+
#cmakedefine BSDUNZIP_VERSION_STRING "@BSDUNZIP_VERSION_STRING@"
347+
345348
/* Define to 1 if you have the `acl_create_entry' function. */
346349
#cmakedefine HAVE_ACL_CREATE_ENTRY 1
347350

@@ -645,6 +648,9 @@ typedef uint64_t uintmax_t;
645648
/* Define to 1 if you have the `getline' function. */
646649
#cmakedefine HAVE_GETLINE 1
647650

651+
/* Define to 1 if you have the <getopt.h> header file. */
652+
#cmakedefine HAVE_GETOPT_H 1
653+
648654
/* Define to 1 if platform uses `optreset` to reset `getopt` */
649655
#cmakedefine HAVE_GETOPT_OPTRESET 1
650656

configure.ac

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ dnl bsdtar and bsdcpio versioning tracks libarchive
1111
m4_define([BSDTAR_VERSION_S],LIBARCHIVE_VERSION_S())
1212
m4_define([BSDCPIO_VERSION_S],LIBARCHIVE_VERSION_S())
1313
m4_define([BSDCAT_VERSION_S],LIBARCHIVE_VERSION_S())
14+
m4_define([BSDUNZIP_VERSION_S],LIBARCHIVE_VERSION_S())
1415

1516
AC_PREREQ([2.71])
1617

@@ -59,13 +60,16 @@ AC_DEFINE([BSDTAR_VERSION_STRING],"BSDTAR_VERSION_S()",
5960
[Version number of bsdtar])
6061
AC_DEFINE([BSDCAT_VERSION_STRING],"BSDTAR_VERSION_S()",
6162
[Version number of bsdcat])
63+
AC_DEFINE([BSDUNZIP_VERSION_STRING],"BSDUNZIP_VERSION_S()",
64+
[Version number of bsdunzip])
6265

6366
# The shell variables here must be the same as the AC_SUBST() variables
6467
# below, but the shell variable names apparently cannot be the same as
6568
# the m4 macro names above. Why? Ask autoconf.
6669
BSDCPIO_VERSION_STRING=BSDCPIO_VERSION_S()
6770
BSDTAR_VERSION_STRING=BSDTAR_VERSION_S()
6871
BSDCAT_VERSION_STRING=BSDCAT_VERSION_S()
72+
BSDUNZIP_VERSION_STRING=BSDUNZIP_VERSION_S()
6973
LIBARCHIVE_VERSION_STRING=LIBARCHIVE_VERSION_S()
7074
LIBARCHIVE_VERSION_NUMBER=LIBARCHIVE_VERSION_N()
7175

@@ -76,6 +80,7 @@ AC_SUBST(ARCHIVE_LIBTOOL_VERSION)
7680
AC_SUBST(BSDCPIO_VERSION_STRING)
7781
AC_SUBST(BSDTAR_VERSION_STRING)
7882
AC_SUBST(BSDCAT_VERSION_STRING)
83+
AC_SUBST(BSDUNZIP_VERSION_STRING)
7984
AC_SUBST(LIBARCHIVE_VERSION_STRING)
8085
AC_SUBST(LIBARCHIVE_VERSION_NUMBER)
8186

@@ -324,7 +329,7 @@ AC_HEADER_DIRENT
324329
AC_HEADER_SYS_WAIT
325330
AC_CHECK_HEADERS([acl/libacl.h attr/xattr.h])
326331
AC_CHECK_HEADERS([copyfile.h ctype.h])
327-
AC_CHECK_HEADERS([errno.h ext2fs/ext2_fs.h fcntl.h fnmatch.h grp.h])
332+
AC_CHECK_HEADERS([errno.h ext2fs/ext2_fs.h fcntl.h fnmatch.h getopt.h grp.h])
328333

329334
AC_CACHE_CHECK([whether EXT2_IOC_GETFLAGS is usable],
330335
[ac_cv_have_decl_EXT2_IOC_GETFLAGS],

unzip/bsdunzip.c

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@
5959
#ifdef HAVE_FNMATCH_H
6060
#include <fnmatch.h>
6161
#endif
62+
#ifdef HAVE_GETOPT_H
63+
#include <getopt.h>
64+
#endif
6265
#ifdef HAVE_STDARG_H
6366
#include <stdarg.h>
6467
#endif
@@ -103,6 +106,7 @@ static int u_opt; /* update */
103106
static int v_opt; /* verbose/list */
104107
static const char *y_str = ""; /* 4 digit year */
105108
static int Z1_opt; /* zipinfo mode list files only */
109+
static int version_opt; /* version string */
106110

107111
/* debug flag */
108112
static int unzip_debug;
@@ -1089,17 +1093,34 @@ usage(void)
10891093
exit(EXIT_FAILURE);
10901094
}
10911095

1096+
static void
1097+
version(void)
1098+
{
1099+
printf("bsdunzip %s - %s \n",
1100+
BSDUNZIP_VERSION_STRING,
1101+
archive_version_details());
1102+
exit(0);
1103+
}
1104+
10921105
static int
10931106
getopts(int argc, char *argv[])
10941107
{
10951108
int opt;
10961109

1110+
static struct option longopts[] = {
1111+
{ "version", no_argument, &version_opt, 1 },
1112+
{ 0, 0, 0, 0}
1113+
};
1114+
10971115
optind = 1;
10981116
#ifdef HAVE_GETOPT_OPTRESET
10991117
optreset = 1;
11001118
#endif
1101-
while ((opt = getopt(argc, argv, "aCcd:fI:jLlnO:opP:qtuvx:yZ1")) != -1)
1119+
while ((opt = getopt_long(argc, argv,
1120+
"aCcd:fI:jLlnO:opP:qtuvx:yZ1", longopts, NULL)) != -1) {
11021121
switch (opt) {
1122+
case 0:
1123+
break;
11031124
case '1':
11041125
Z1_opt = 1;
11051126
break;
@@ -1169,7 +1190,7 @@ getopts(int argc, char *argv[])
11691190
default:
11701191
usage();
11711192
}
1172-
1193+
}
11731194
return (optind);
11741195
}
11751196

@@ -1179,6 +1200,8 @@ main(int argc, char *argv[])
11791200
const char *zipfile;
11801201
int nopts;
11811202

1203+
lafe_setprogname(*argv, "bsdunzip");
1204+
11821205
if (isatty(STDOUT_FILENO))
11831206
tty = 1;
11841207

@@ -1199,6 +1222,11 @@ main(int argc, char *argv[])
11991222
*/
12001223
nopts = getopts(argc, argv);
12011224

1225+
if (version_opt == 1) {
1226+
version();
1227+
exit(EXIT_SUCCESS);
1228+
}
1229+
12021230
/*
12031231
* When more of the zipinfo mode options are implemented, this
12041232
* will need to change.

unzip/test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ IF(ENABLE_UNZIP AND ENABLE_TEST)
2323
test_q.c
2424
test_t.c
2525
test_t_bad.c
26+
test_version.c
2627
test_x.c
2728
test_Z1.c
2829
test_P_encryption.c

unzip/test/test.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@
3535
#undef EXTRA_DUMP /* How to dump extra data */
3636
#undef EXTRA_ERRNO /* How to dump errno */
3737
/* How to generate extra version info. */
38-
#define EXTRA_VERSION (systemf("%s -v", testprog) ? "" : "")
38+
#define EXTRA_VERSION (systemf("%s --version", testprog) ? "" : "")
3939

4040
#include "test_common.h"

unzip/test/test_version.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*-
2+
* Copyright (c) 2003-2017 Tim Kientzle
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met:
8+
* 1. Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* 2. Redistributions in binary form must reproduce the above copyright
11+
* notice, this list of conditions and the following disclaimer in the
12+
* documentation and/or other materials provided with the distribution.
13+
*
14+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15+
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16+
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17+
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19+
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23+
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24+
*/
25+
#include "test.h"
26+
27+
/*
28+
* Test that --version option works and generates reasonable output.
29+
*/
30+
31+
DEFINE_TEST(test_version)
32+
{
33+
assertVersion(testprog, "bsdunzip");
34+
}

0 commit comments

Comments
 (0)