diff options
Diffstat (limited to 'archival/gunzip.c')
-rw-r--r-- | archival/gunzip.c | 40 |
1 files changed, 24 insertions, 16 deletions
diff --git a/archival/gunzip.c b/archival/gunzip.c index ac2fb17ed..4489204fb 100644 --- a/archival/gunzip.c +++ b/archival/gunzip.c | |||
@@ -64,7 +64,12 @@ static char *license_msg[] = { | |||
64 | #include <string.h> | 64 | #include <string.h> |
65 | #include <unistd.h> | 65 | #include <unistd.h> |
66 | #include <getopt.h> | 66 | #include <getopt.h> |
67 | #include <sys/types.h> | ||
68 | #include <sys/stat.h> | ||
69 | #include <fcntl.h> | ||
70 | |||
67 | #include "busybox.h" | 71 | #include "busybox.h" |
72 | #include "unarchive.h" | ||
68 | 73 | ||
69 | const char gunzip_to_stdout = 1; | 74 | const char gunzip_to_stdout = 1; |
70 | const char gunzip_force = 2; | 75 | const char gunzip_force = 2; |
@@ -100,23 +105,20 @@ extern int gunzip_main(int argc, char **argv) | |||
100 | } | 105 | } |
101 | 106 | ||
102 | do { | 107 | do { |
103 | FILE *in_file, *out_file; | ||
104 | struct stat stat_buf; | 108 | struct stat stat_buf; |
105 | const char *old_path = argv[optind]; | 109 | const char *old_path = argv[optind]; |
106 | const char *delete_path = NULL; | 110 | const char *delete_path = NULL; |
107 | char *new_path = NULL; | 111 | char *new_path = NULL; |
112 | int src_fd; | ||
113 | int dst_fd; | ||
108 | 114 | ||
109 | optind++; | 115 | optind++; |
110 | 116 | ||
111 | if (old_path == NULL || strcmp(old_path, "-") == 0) { | 117 | if (old_path == NULL || strcmp(old_path, "-") == 0) { |
112 | in_file = stdin; | 118 | src_fd = fileno(stdin); |
113 | flags |= gunzip_to_stdout; | 119 | flags |= gunzip_to_stdout; |
114 | } else { | 120 | } else { |
115 | in_file = wfopen(old_path, "r"); | 121 | src_fd = xopen(old_path, O_RDONLY); |
116 | if (in_file == NULL) { | ||
117 | status = EXIT_FAILURE; | ||
118 | break; | ||
119 | } | ||
120 | 122 | ||
121 | /* Get the time stamp on the input file. */ | 123 | /* Get the time stamp on the input file. */ |
122 | if (stat(old_path, &stat_buf) < 0) { | 124 | if (stat(old_path, &stat_buf) < 0) { |
@@ -125,16 +127,16 @@ extern int gunzip_main(int argc, char **argv) | |||
125 | } | 127 | } |
126 | 128 | ||
127 | /* Check that the input is sane. */ | 129 | /* Check that the input is sane. */ |
128 | if (isatty(fileno(in_file)) && ((flags & gunzip_force) == 0)) { | 130 | if (isatty(src_fd) && ((flags & gunzip_force) == 0)) { |
129 | error_msg_and_die | 131 | error_msg_and_die |
130 | ("compressed data not read from terminal. Use -f to force it."); | 132 | ("compressed data not read from terminal. Use -f to force it."); |
131 | } | 133 | } |
132 | 134 | ||
133 | /* Set output filename and number */ | 135 | /* Set output filename and number */ |
134 | if (flags & gunzip_test) { | 136 | if (flags & gunzip_test) { |
135 | out_file = xfopen("/dev/null", "w"); /* why does test use filenum 2 ? */ | 137 | dst_fd = xopen("/dev/null", O_WRONLY); /* why does test use filenum 2 ? */ |
136 | } else if (flags & gunzip_to_stdout) { | 138 | } else if (flags & gunzip_to_stdout) { |
137 | out_file = stdout; | 139 | dst_fd = fileno(stdout); |
138 | } else { | 140 | } else { |
139 | char *extension; | 141 | char *extension; |
140 | 142 | ||
@@ -151,7 +153,7 @@ extern int gunzip_main(int argc, char **argv) | |||
151 | } | 153 | } |
152 | 154 | ||
153 | /* Open output file */ | 155 | /* Open output file */ |
154 | out_file = xfopen(new_path, "w"); | 156 | dst_fd = xopen(new_path, O_WRONLY | O_CREAT); |
155 | 157 | ||
156 | /* Set permissions on the file */ | 158 | /* Set permissions on the file */ |
157 | chmod(new_path, stat_buf.st_mode); | 159 | chmod(new_path, stat_buf.st_mode); |
@@ -161,16 +163,22 @@ extern int gunzip_main(int argc, char **argv) | |||
161 | } | 163 | } |
162 | 164 | ||
163 | /* do the decompression, and cleanup */ | 165 | /* do the decompression, and cleanup */ |
164 | if ((unzip(in_file, out_file) != 0) && (new_path)) { | 166 | check_header_gzip(src_fd); |
167 | if (inflate(src_fd, dst_fd) != 0) { | ||
168 | error_msg("Error inflating"); | ||
169 | } | ||
170 | check_trailer_gzip(src_fd); | ||
171 | |||
172 | if ((status != EXIT_SUCCESS) && (new_path)) { | ||
165 | /* Unzip failed, remove new path instead of old path */ | 173 | /* Unzip failed, remove new path instead of old path */ |
166 | delete_path = new_path; | 174 | delete_path = new_path; |
167 | } | 175 | } |
168 | 176 | ||
169 | if (out_file != stdout) { | 177 | if (dst_fd != fileno(stdout)) { |
170 | fclose(out_file); | 178 | close(dst_fd); |
171 | } | 179 | } |
172 | if (in_file != stdin) { | 180 | if (src_fd != fileno(stdin)) { |
173 | fclose(in_file); | 181 | close(src_fd); |
174 | } | 182 | } |
175 | 183 | ||
176 | /* delete_path will be NULL if in test mode or from stdin */ | 184 | /* delete_path will be NULL if in test mode or from stdin */ |