Skip to content

Commit

Permalink
unzip: support --version argument
Browse files Browse the repository at this point in the history
  • Loading branch information
mmatuska committed Jul 30, 2023
1 parent 46063a5 commit b458825
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}")

Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions build/cmake/config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,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 <getopt.h> header file. */
#cmakedefine HAVE_GETOPT_H 1

/* Define to 1 if platform uses `optreset` to reset `getopt` */
#cmakedefine HAVE_GETOPT_OPTRESET 1

Expand Down
9 changes: 7 additions & 2 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ dnl First, define all of the version numbers up front.
dnl In particular, this allows the version macro to be used in AC_INIT

dnl These first two version numbers are updated automatically on each release.
m4_define([LIBARCHIVE_VERSION_S],[3.7.2dev])
m4_define([LIBARCHIVE_VERSION_S],[3.7.2])
m4_define([LIBARCHIVE_VERSION_N],[3007002])

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])

Expand Down Expand Up @@ -59,13 +60,16 @@ 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
# the m4 macro names above. Why? Ask autoconf.
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()

Expand All @@ -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)

Expand Down Expand Up @@ -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],
Expand Down
2 changes: 1 addition & 1 deletion libarchive/archive.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ __LA_DECL int archive_version_number(void);
/*
* Textual name/version of the library, useful for version displays.
*/
#define ARCHIVE_VERSION_ONLY_STRING "3.7.2dev"
#define ARCHIVE_VERSION_ONLY_STRING "3.7.2"
#define ARCHIVE_VERSION_STRING "libarchive " ARCHIVE_VERSION_ONLY_STRING
__LA_DECL const char * archive_version_string(void);

Expand Down
37 changes: 33 additions & 4 deletions unzip/bsdunzip.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@
#ifdef HAVE_FNMATCH_H
#include <fnmatch.h>
#endif
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
#ifdef HAVE_STDARG_H
#include <stdarg.h>
#endif
Expand All @@ -79,8 +82,7 @@
#endif
#endif

#include <archive.h>
#include <archive_entry.h>
#include "bsdunzip.h"
#include "passphrase.h"
#include "err.h"

Expand All @@ -103,6 +105,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;
Expand Down Expand Up @@ -1089,22 +1092,38 @@ 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 '1':
Z1_opt = 1;
break;
case 'a':
a_opt = 1;
printf("a_opt: %d\n", a_opt);
break;
case 'C':
C_opt = 1;
Expand Down Expand Up @@ -1166,10 +1185,13 @@ getopts(int argc, char *argv[])
case 'Z':
zipinfo_mode = 1;
break;
case OPTION_VERSION:
version_opt = 1;
break;
default:
usage();
}

}
return (optind);
}

Expand All @@ -1179,6 +1201,8 @@ main(int argc, char *argv[])
const char *zipfile;
int nopts;

lafe_setprogname(*argv, "bsdunzip");

if (isatty(STDOUT_FILENO))
tty = 1;

Expand All @@ -1199,6 +1223,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.
Expand Down
1 change: 1 addition & 0 deletions unzip/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion unzip/test/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
34 changes: 34 additions & 0 deletions unzip/test/test_version.c
Original file line number Diff line number Diff line change
@@ -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");
}

0 comments on commit b458825

Please sign in to comment.