From: Martin Matuska Date: Sun, 30 Jul 2023 22:45:57 +0000 (+0200) Subject: unzip: support --version argument X-Git-Tag: v3.7.2~21 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a8578c0b614e3db0f66a72915f759160e56dfb07;p=thirdparty%2Flibarchive.git unzip: support --version argument --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 909fef708..4e2fec82e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -65,6 +65,7 @@ SET(VERSION "${_major}.${_trimmed_minor}.${_trimmed_revision} SET(BSDCPIO_VERSION_STRING "${VERSION}") SET(BSDTAR_VERSION_STRING "${VERSION}") SET(BSDCAT_VERSION_STRING "${VERSION}") +SET(BSDUNZIP_VERSION_STRING "${VERSION}") SET(LIBARCHIVE_VERSION_NUMBER "${_version_number}") SET(LIBARCHIVE_VERSION_STRING "${VERSION}") @@ -695,6 +696,7 @@ int main(void) { return EXT2_IOC_GETFLAGS; }" HAVE_WORKING_EXT2_IOC_GETFLAGS) LA_CHECK_INCLUDE_FILE("fcntl.h" HAVE_FCNTL_H) LA_CHECK_INCLUDE_FILE("fnmatch.h" HAVE_FNMATCH_H) +LA_CHECK_INCLUDE_FILE("getopt.h" HAVE_GETOPT_H) LA_CHECK_INCLUDE_FILE("grp.h" HAVE_GRP_H) LA_CHECK_INCLUDE_FILE("inttypes.h" HAVE_INTTYPES_H) LA_CHECK_INCLUDE_FILE("io.h" HAVE_IO_H) diff --git a/Makefile.am b/Makefile.am index e8b676443..410659b61 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1510,6 +1510,7 @@ bsdunzip_test_SOURCES= \ unzip/test/test_q.c \ unzip/test/test_t.c \ unzip/test/test_t_bad.c \ + unzip/test/test_version.c \ unzip/test/test_x.c \ unzip/test/test_Z1.c \ unzip/test/test_P_encryption.c diff --git a/build/cmake/config.h.in b/build/cmake/config.h.in index daa5e387a..570afb693 100644 --- a/build/cmake/config.h.in +++ b/build/cmake/config.h.in @@ -342,6 +342,9 @@ typedef uint64_t uintmax_t; /* Version number of bsdcat */ #cmakedefine BSDCAT_VERSION_STRING "@BSDCAT_VERSION_STRING@" +/* Version number of bsdunzip */ +#cmakedefine BSDUNZIP_VERSION_STRING "@BSDUNZIP_VERSION_STRING@" + /* Define to 1 if you have the `acl_create_entry' function. */ #cmakedefine HAVE_ACL_CREATE_ENTRY 1 @@ -645,6 +648,9 @@ typedef uint64_t uintmax_t; /* Define to 1 if you have the `getline' function. */ #cmakedefine HAVE_GETLINE 1 +/* Define to 1 if you have the header file. */ +#cmakedefine HAVE_GETOPT_H 1 + /* Define to 1 if platform uses `optreset` to reset `getopt` */ #cmakedefine HAVE_GETOPT_OPTRESET 1 diff --git a/configure.ac b/configure.ac index 11aa5e8b2..8a3aebcef 100644 --- a/configure.ac +++ b/configure.ac @@ -11,6 +11,7 @@ dnl bsdtar and bsdcpio versioning tracks libarchive m4_define([BSDTAR_VERSION_S],LIBARCHIVE_VERSION_S()) m4_define([BSDCPIO_VERSION_S],LIBARCHIVE_VERSION_S()) m4_define([BSDCAT_VERSION_S],LIBARCHIVE_VERSION_S()) +m4_define([BSDUNZIP_VERSION_S],LIBARCHIVE_VERSION_S()) AC_PREREQ([2.71]) @@ -59,6 +60,8 @@ AC_DEFINE([BSDTAR_VERSION_STRING],"BSDTAR_VERSION_S()", [Version number of bsdtar]) AC_DEFINE([BSDCAT_VERSION_STRING],"BSDTAR_VERSION_S()", [Version number of bsdcat]) +AC_DEFINE([BSDUNZIP_VERSION_STRING],"BSDUNZIP_VERSION_S()", + [Version number of bsdunzip]) # The shell variables here must be the same as the AC_SUBST() variables # below, but the shell variable names apparently cannot be the same as @@ -66,6 +69,7 @@ AC_DEFINE([BSDCAT_VERSION_STRING],"BSDTAR_VERSION_S()", BSDCPIO_VERSION_STRING=BSDCPIO_VERSION_S() BSDTAR_VERSION_STRING=BSDTAR_VERSION_S() BSDCAT_VERSION_STRING=BSDCAT_VERSION_S() +BSDUNZIP_VERSION_STRING=BSDUNZIP_VERSION_S() LIBARCHIVE_VERSION_STRING=LIBARCHIVE_VERSION_S() LIBARCHIVE_VERSION_NUMBER=LIBARCHIVE_VERSION_N() @@ -76,6 +80,7 @@ AC_SUBST(ARCHIVE_LIBTOOL_VERSION) AC_SUBST(BSDCPIO_VERSION_STRING) AC_SUBST(BSDTAR_VERSION_STRING) AC_SUBST(BSDCAT_VERSION_STRING) +AC_SUBST(BSDUNZIP_VERSION_STRING) AC_SUBST(LIBARCHIVE_VERSION_STRING) AC_SUBST(LIBARCHIVE_VERSION_NUMBER) @@ -324,7 +329,7 @@ AC_HEADER_DIRENT AC_HEADER_SYS_WAIT AC_CHECK_HEADERS([acl/libacl.h attr/xattr.h]) AC_CHECK_HEADERS([copyfile.h ctype.h]) -AC_CHECK_HEADERS([errno.h ext2fs/ext2_fs.h fcntl.h fnmatch.h grp.h]) +AC_CHECK_HEADERS([errno.h ext2fs/ext2_fs.h fcntl.h fnmatch.h getopt.h grp.h]) AC_CACHE_CHECK([whether EXT2_IOC_GETFLAGS is usable], [ac_cv_have_decl_EXT2_IOC_GETFLAGS], diff --git a/unzip/bsdunzip.c b/unzip/bsdunzip.c index 0b6506a18..83c71a2a9 100644 --- a/unzip/bsdunzip.c +++ b/unzip/bsdunzip.c @@ -59,6 +59,9 @@ #ifdef HAVE_FNMATCH_H #include #endif +#ifdef HAVE_GETOPT_H +#include +#endif #ifdef HAVE_STDARG_H #include #endif @@ -103,6 +106,7 @@ static int u_opt; /* update */ static int v_opt; /* verbose/list */ static const char *y_str = ""; /* 4 digit year */ static int Z1_opt; /* zipinfo mode list files only */ +static int version_opt; /* version string */ /* debug flag */ static int unzip_debug; @@ -1089,17 +1093,34 @@ usage(void) exit(EXIT_FAILURE); } +static void +version(void) +{ + printf("bsdunzip %s - %s \n", + BSDUNZIP_VERSION_STRING, + archive_version_details()); + exit(0); +} + static int getopts(int argc, char *argv[]) { int opt; + static struct option longopts[] = { + { "version", no_argument, &version_opt, 1 }, + { 0, 0, 0, 0} + }; + optind = 1; #ifdef HAVE_GETOPT_OPTRESET optreset = 1; #endif - while ((opt = getopt(argc, argv, "aCcd:fI:jLlnO:opP:qtuvx:yZ1")) != -1) + while ((opt = getopt_long(argc, argv, + "aCcd:fI:jLlnO:opP:qtuvx:yZ1", longopts, NULL)) != -1) { switch (opt) { + case 0: + break; case '1': Z1_opt = 1; break; @@ -1169,7 +1190,7 @@ getopts(int argc, char *argv[]) default: usage(); } - + } return (optind); } @@ -1179,6 +1200,8 @@ main(int argc, char *argv[]) const char *zipfile; int nopts; + lafe_setprogname(*argv, "bsdunzip"); + if (isatty(STDOUT_FILENO)) tty = 1; @@ -1199,6 +1222,11 @@ main(int argc, char *argv[]) */ nopts = getopts(argc, argv); + if (version_opt == 1) { + version(); + exit(EXIT_SUCCESS); + } + /* * When more of the zipinfo mode options are implemented, this * will need to change. diff --git a/unzip/test/CMakeLists.txt b/unzip/test/CMakeLists.txt index bdb344595..20e730a9c 100644 --- a/unzip/test/CMakeLists.txt +++ b/unzip/test/CMakeLists.txt @@ -23,6 +23,7 @@ IF(ENABLE_UNZIP AND ENABLE_TEST) test_q.c test_t.c test_t_bad.c + test_version.c test_x.c test_Z1.c test_P_encryption.c diff --git a/unzip/test/test.h b/unzip/test/test.h index 8da017f68..ba876afd8 100644 --- a/unzip/test/test.h +++ b/unzip/test/test.h @@ -35,6 +35,6 @@ #undef EXTRA_DUMP /* How to dump extra data */ #undef EXTRA_ERRNO /* How to dump errno */ /* How to generate extra version info. */ -#define EXTRA_VERSION (systemf("%s -v", testprog) ? "" : "") +#define EXTRA_VERSION (systemf("%s --version", testprog) ? "" : "") #include "test_common.h" diff --git a/unzip/test/test_version.c b/unzip/test/test_version.c new file mode 100644 index 000000000..efa797982 --- /dev/null +++ b/unzip/test/test_version.c @@ -0,0 +1,34 @@ +/*- + * Copyright (c) 2003-2017 Tim Kientzle + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#include "test.h" + +/* + * Test that --version option works and generates reasonable output. + */ + +DEFINE_TEST(test_version) +{ + assertVersion(testprog, "bsdunzip"); +}