aboutsummaryrefslogtreecommitdiff
path: root/archival/gunzip.c
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2006-10-01 15:55:11 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2006-10-01 15:55:11 +0000
commit97a8dd3857aea9730382e2975a2ee2000fd23ebb (patch)
tree608f73898f3ed5f466dff68189625fa9328a15be /archival/gunzip.c
parentf8aa109a9f7c67b291f240fb3ed91da90f26359b (diff)
downloadbusybox-w32-97a8dd3857aea9730382e2975a2ee2000fd23ebb.tar.gz
busybox-w32-97a8dd3857aea9730382e2975a2ee2000fd23ebb.tar.bz2
busybox-w32-97a8dd3857aea9730382e2975a2ee2000fd23ebb.zip
g[un]zip: add support for -v (verbose).
Add CONFIG_DESKTOP, almost all bloat from this change is hidden under that.
Diffstat (limited to 'archival/gunzip.c')
-rw-r--r--archival/gunzip.c56
1 files changed, 33 insertions, 23 deletions
diff --git a/archival/gunzip.c b/archival/gunzip.c
index 9a68e62e4..1b4968628 100644
--- a/archival/gunzip.c
+++ b/archival/gunzip.c
@@ -34,13 +34,15 @@
34#define GUNZIP_OPT_FORCE 2 34#define GUNZIP_OPT_FORCE 2
35#define GUNZIP_OPT_TEST 4 35#define GUNZIP_OPT_TEST 4
36#define GUNZIP_OPT_DECOMPRESS 8 36#define GUNZIP_OPT_DECOMPRESS 8
37#define GUNZIP_OPT_VERBOSE 0x10
37 38
38int gunzip_main(int argc, char **argv) 39int gunzip_main(int argc, char **argv)
39{ 40{
40 char status = EXIT_SUCCESS; 41 USE_DESKTOP(long long) int status;
42 int exitcode = 0;
41 unsigned long opt; 43 unsigned long opt;
42 44
43 opt = bb_getopt_ulflags(argc, argv, "cftd"); 45 opt = bb_getopt_ulflags(argc, argv, "cftdv");
44 /* if called as zcat */ 46 /* if called as zcat */
45 if (strcmp(bb_applet_name, "zcat") == 0) { 47 if (strcmp(bb_applet_name, "zcat") == 0) {
46 opt |= GUNZIP_OPT_STDOUT; 48 opt |= GUNZIP_OPT_STDOUT;
@@ -59,6 +61,8 @@ int gunzip_main(int argc, char **argv)
59 if (old_path == NULL || strcmp(old_path, "-") == 0) { 61 if (old_path == NULL || strcmp(old_path, "-") == 0) {
60 src_fd = STDIN_FILENO; 62 src_fd = STDIN_FILENO;
61 opt |= GUNZIP_OPT_STDOUT; 63 opt |= GUNZIP_OPT_STDOUT;
64 USE_DESKTOP(opt &= ~GUNZIP_OPT_VERBOSE;)
65 optind = argc; /* we don't handle "gunzip - a.gz b.gz" */
62 } else { 66 } else {
63 src_fd = xopen(old_path, O_RDONLY); 67 src_fd = xopen(old_path, O_RDONLY);
64 68
@@ -67,9 +71,9 @@ int gunzip_main(int argc, char **argv)
67 } 71 }
68 72
69 /* Check that the input is sane. */ 73 /* Check that the input is sane. */
70 if (isatty(src_fd) && ((opt & GUNZIP_OPT_FORCE) == 0)) { 74 if (isatty(src_fd) && !(opt & GUNZIP_OPT_FORCE)) {
71 bb_error_msg_and_die 75 bb_error_msg_and_die
72 ("compressed data not read from terminal. Use -f to force it."); 76 ("compressed data not read from terminal, use -f to force it");
73 } 77 }
74 78
75 /* Set output filename and number */ 79 /* Set output filename and number */
@@ -94,7 +98,8 @@ int gunzip_main(int argc, char **argv)
94 extension[2] = 'a'; 98 extension[2] = 'a';
95 extension[3] = 'r'; 99 extension[3] = 'r';
96 } else { 100 } else {
97 bb_error_msg_and_die("Invalid extension"); 101 // FIXME: should we die or just skip to next?
102 bb_error_msg_and_die("invalid extension");
98 } 103 }
99 104
100 /* Open output file (with correct permissions) */ 105 /* Open output file (with correct permissions) */
@@ -105,30 +110,34 @@ int gunzip_main(int argc, char **argv)
105 delete_path = old_path; 110 delete_path = old_path;
106 } 111 }
107 112
113 status = -1;
108 /* do the decompression, and cleanup */ 114 /* do the decompression, and cleanup */
109 if (xread_char(src_fd) == 0x1f) { 115 if (xread_char(src_fd) == 0x1f) {
110 unsigned char magic2; 116 unsigned char magic2;
111 117
112 magic2 = xread_char(src_fd); 118 magic2 = xread_char(src_fd);
113#ifdef CONFIG_FEATURE_GUNZIP_UNCOMPRESS 119 if (ENABLE_FEATURE_GUNZIP_UNCOMPRESS && magic2 == 0x9d) {
114 if (magic2 == 0x9d) {
115 status = uncompress(src_fd, dst_fd); 120 status = uncompress(src_fd, dst_fd);
116 } else 121 } else if (magic2 == 0x8b) {
117#endif 122 check_header_gzip(src_fd); // FIXME: xfunc? _or_die?
118 if (magic2 == 0x8b) { 123 status = inflate_gunzip(src_fd, dst_fd);
119 check_header_gzip(src_fd); 124 } else {
120 status = inflate_gunzip(src_fd, dst_fd); 125 bb_error_msg("invalid magic");
121 if (status != 0) { 126 exitcode = 1;
122 bb_error_msg_and_die("error inflating"); 127 }
123 } 128 if (status < 0) {
124 } else { 129 bb_error_msg("error inflating");
125 bb_error_msg_and_die("invalid magic"); 130 exitcode = 1;
126 } 131 }
132 else if (ENABLE_DESKTOP && (opt & GUNZIP_OPT_VERBOSE)) {
133 fprintf(stderr, "%s: %u%% - replaced with %s\n",
134 // TODO: LARGEFILE support for stat_buf.st_size?
135 old_path, (unsigned)(stat_buf.st_size*100 / (status+1)), new_path);
136 }
127 } else { 137 } else {
128 bb_error_msg_and_die("invalid magic"); 138 bb_error_msg("invalid magic"); exitcode = 1;
129 } 139 }
130 140 if (status < 0 && new_path) {
131 if ((status != EXIT_SUCCESS) && (new_path)) {
132 /* Unzip failed, remove new path instead of old path */ 141 /* Unzip failed, remove new path instead of old path */
133 delete_path = new_path; 142 delete_path = new_path;
134 } 143 }
@@ -142,12 +151,13 @@ int gunzip_main(int argc, char **argv)
142 151
143 /* delete_path will be NULL if in test mode or from stdin */ 152 /* delete_path will be NULL if in test mode or from stdin */
144 if (delete_path && (unlink(delete_path) == -1)) { 153 if (delete_path && (unlink(delete_path) == -1)) {
145 bb_error_msg_and_die("cannot remove %s", delete_path); 154 bb_error_msg("cannot remove %s", delete_path);
155 exitcode = 1;
146 } 156 }
147 157
148 free(new_path); 158 free(new_path);
149 159
150 } while (optind < argc); 160 } while (optind < argc);
151 161
152 return status; 162 return exitcode;
153} 163}