patch: short form for unzip archive listing

Paul Fox pgf at brightstareng.com
Thu Sep 6 19:17:22 UTC 2007


if you're dealing with zip archives in a script, it's useful to
be able to get a script-friendly listing.  the current '-l'
command is more like 'ls -l', i.e. lots of information, but not
script-friendly.

this patch overloads the existing '-q' flag so that if '-l' and
'-q' are used together, a "short form" listing (just pathnames)
is generated.  (without this patch, the options make no sense to
use together.)

i don't quite understand the bloatcheck, since i actually added a
few characters to the usage message, but i guess overall it now
compresses better?  odd.

function                                             old     new   delta
unzip_main                                          1807    1858     +51
packed_usage                                       23173   23151     -22
.rodata                                           122070  122038     -32
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/2 up/down: 51/-54)             Total: -3 bytes


i can commit in a day or two if no one objects.

paul

Index: archival/unzip.c
===================================================================
--- archival/unzip.c	(revision 19797)
+++ archival/unzip.c	(working copy)
@@ -97,7 +97,8 @@
 int unzip_main(int argc, char **argv)
 {
 	zip_header_t zip_header;
-	enum {v_silent, v_normal, v_list} verbosity = v_normal;
+	int verbose = 1;
+	int listing = 0;
 	enum {o_prompt, o_never, o_always} overwrite = o_prompt;
 	unsigned int total_size = 0;
 	unsigned int total_entries = 0;
@@ -115,7 +116,7 @@
 		case 0: /* Options */
 			switch (opt) {
 			case 'l': /* List */
-				verbosity = v_list;
+				listing = 1;
 				break;
 
 			case 'n': /* Never overwrite existing files */
@@ -130,7 +131,7 @@
 				dst_fd = STDOUT_FILENO;
 
 			case 'q': /* Be quiet */
-				verbosity = (verbosity == v_normal) ? v_silent : verbosity;
+				verbose = 0;
 				break;
 
 			case 1 : /* The zip file */
@@ -205,7 +206,7 @@
 	if (base_dir)
 		xchdir(base_dir);
 
-	if (verbosity != v_silent)
+	if (verbose)
 		printf("Archive:  %s\n", src_fn);
 
 	failed = 0;
@@ -245,7 +246,7 @@
 		/* Skip extra header bytes */
 		unzip_skip(src_fd, zip_header.formatted.extra_len);
 
-		if ((verbosity == v_list) && !list_header_done){
+		if (listing && verbose && !list_header_done){
 			puts("  Length     Date   Time    Name\n"
 			     " --------    ----   ----    ----");
 			list_header_done = 1;
@@ -259,7 +260,8 @@
 		} else { /* Extract entry */
 			total_size += zip_header.formatted.ucmpsize;
 
-			if (verbosity == v_list) { /* List entry */
+			if (listing) { /* List entry */
+			    if (verbose) {
 				unsigned int dostime = zip_header.formatted.modtime | (zip_header.formatted.moddate << 16);
 				printf("%9u  %02u-%02u-%02u %02u:%02u   %s\n",
 					   zip_header.formatted.ucmpsize,
@@ -270,7 +272,11 @@
 					   (dostime & 0x000007e0) >> 5,
 					   dst_fn);
 				total_entries++;
-				i = 'n';
+			    } else {
+				/* short listing -- filenames only */
+				printf("%s\n", dst_fn);
+			    }
+			    i = 'n';
 			} else if (dst_fd == STDOUT_FILENO) { /* Extracting to STDOUT */
 				i = -1;
 			} else if (last_char_is(dst_fn, '/')) { /* Extract directory */
@@ -278,7 +284,7 @@
 					if (errno != ENOENT) {
 						bb_perror_msg_and_die("cannot stat '%s'",dst_fn);
 					}
-					if (verbosity == v_normal) {
+					if (verbose) {
 						printf("   creating: %s\n", dst_fn);
 					}
 					unzip_create_leading_dirs(dst_fn);
@@ -326,7 +332,7 @@
 			unzip_create_leading_dirs(dst_fn);
 			dst_fd = xopen(dst_fn, O_WRONLY | O_CREAT | O_TRUNC);
 		case -1: /* Unzip */
-			if (verbosity == v_normal) {
+			if (verbose) {
 				printf("  inflating: %s\n", dst_fn);
 			}
 			if (unzip_extract(&zip_header, src_fd, dst_fd)) {
@@ -368,7 +374,7 @@
 		}
 	}
 
-	if (verbosity == v_list) {
+	if (listing && verbose) {
 		printf(" --------                   -------\n"
 		       "%9d                   %d files\n", total_size, total_entries);
 	}
Index: include/usage.h
===================================================================
--- include/usage.h	(revision 19795)
+++ include/usage.h	(working copy)
@@ -3763,7 +3763,7 @@
 #define unzip_full_usage \
        "Extract files from ZIP archives" \
        "\n\nOptions:\n" \
-       "	-l	List archive contents (short form)\n" \
+       "	-l	List archive contents (with -q for short form)\n" \
        "	-n	Never overwrite existing files (default)\n" \
        "	-o	Overwrite files without prompting\n" \
        "	-p	Send output to stdout\n" \





=---------------------
 paul fox, pgf at brightstareng.com



More information about the busybox mailing list