aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGlenn L McGrath <bug1@ihug.co.nz>2002-08-24 14:32:17 +0000
committerGlenn L McGrath <bug1@ihug.co.nz>2002-08-24 14:32:17 +0000
commitabac53b33c6aef4f6746ef33964768d39b34e67c (patch)
tree351fdaac64de1fc91054c74db5f09a6186c65e1f
parent1ee52e8b14da3cb35956f266b963050ccee5da7b (diff)
downloadbusybox-w32-abac53b33c6aef4f6746ef33964768d39b34e67c.tar.gz
busybox-w32-abac53b33c6aef4f6746ef33964768d39b34e67c.tar.bz2
busybox-w32-abac53b33c6aef4f6746ef33964768d39b34e67c.zip
Reorganise, make it just one function, remove -v option it didnt work properly anyway, dont setvbuf it doesnt make any difference in performance.
-rw-r--r--archival/gunzip.c201
1 files changed, 87 insertions, 114 deletions
diff --git a/archival/gunzip.c b/archival/gunzip.c
index e379ccf10..c4712270d 100644
--- a/archival/gunzip.c
+++ b/archival/gunzip.c
@@ -66,114 +66,22 @@ static char *license_msg[] = {
66#include <getopt.h> 66#include <getopt.h>
67#include "busybox.h" 67#include "busybox.h"
68 68
69const int gunzip_to_stdout = 1; 69const char gunzip_to_stdout = 1;
70const int gunzip_force = 2; 70const char gunzip_force = 2;
71const int gunzip_test = 4; 71const char gunzip_test = 4;
72const int gunzip_verbose = 8;
73
74static int gunzip_file(const char *path, int flags)
75{
76 FILE *in_file, *out_file;
77 struct stat stat_buf;
78 const char *delete_path = NULL;
79 char *out_path = NULL;
80
81 if (path == NULL || strcmp(path, "-") == 0) {
82 in_file = stdin;
83 flags |= gunzip_to_stdout;
84 } else {
85 if ((in_file = wfopen(path, "r")) == NULL) {
86 return -1;
87 }
88 if (flags & gunzip_verbose) {
89 fprintf(stderr, "%s:\t", path);
90 }
91
92 /* set the buffer size */
93 setvbuf(in_file, NULL, _IOFBF, 0x8000);
94
95 /* Get the time stamp on the input file. */
96 if (stat(path, &stat_buf) < 0) {
97 error_msg_and_die("Couldn't stat file %s", path);
98 }
99 }
100
101 /* Check that the input is sane. */
102 if (isatty(fileno(in_file)) && (flags & gunzip_force) == 0) {
103 error_msg_and_die
104 ("compressed data not read from terminal. Use -f to force it.");
105 }
106
107 /* Set output filename and number */
108 if (flags & gunzip_test) {
109 out_file = xfopen("/dev/null", "w"); /* why does test use filenum 2 ? */
110 } else if (flags & gunzip_to_stdout) {
111 out_file = stdout;
112 } else {
113 char *extension;
114 int length = strlen(path);
115
116 extension = strrchr(path, '.');
117 if (extension && strcmp(extension, ".gz") == 0) {
118 length -= 3;
119 } else if (extension && strcmp(extension, ".tgz") == 0) {
120 length -= 4;
121 } else {
122 error_msg_and_die("Invalid extension");
123 }
124 out_path = xstrndup(path, length);
125
126 /* Open output file */
127 out_file = xfopen(out_path, "w");
128
129 /* Set permissions on the file */
130 chmod(out_path, stat_buf.st_mode);
131 }
132
133 /* do the decompression, and cleanup */
134 if (unzip(in_file, out_file) == 0) {
135 /* Success, remove .gz file */
136 if (!(flags & gunzip_to_stdout)) {
137 delete_path = path;
138 }
139 if (flags & gunzip_verbose) {
140 fprintf(stderr, "OK\n");
141 }
142 } else {
143 /* remove failed attempt */
144 delete_path = out_path;
145 }
146
147 if (out_file != stdout) {
148 fclose(out_file);
149 }
150 if (in_file != stdin) {
151 fclose(in_file);
152 }
153
154 if (delete_path && !(flags & gunzip_test)) {
155 if (unlink(delete_path) < 0) {
156 error_msg_and_die("Couldn't remove %s", delete_path);
157 }
158 }
159
160 free(out_path);
161
162 return 0;
163}
164 72
165extern int gunzip_main(int argc, char **argv) 73extern int gunzip_main(int argc, char **argv)
166{ 74{
167 int flags = 0; 75 char status = EXIT_SUCCESS;
168 int i, opt; 76 char flags = 0;
169 int status = EXIT_SUCCESS; 77 int opt;
170 78
171 /* if called as zcat */ 79 /* if called as zcat */
172 if (strcmp(applet_name, "zcat") == 0) { 80 if (strcmp(applet_name, "zcat") == 0) {
173 flags |= gunzip_to_stdout; 81 flags |= gunzip_to_stdout;
174 } 82 }
175 83
176 while ((opt = getopt(argc, argv, "ctfhdqv")) != -1) { 84 while ((opt = getopt(argc, argv, "ctfhd")) != -1) {
177 switch (opt) { 85 switch (opt) {
178 case 'c': 86 case 'c':
179 flags |= gunzip_to_stdout; 87 flags |= gunzip_to_stdout;
@@ -184,30 +92,95 @@ extern int gunzip_main(int argc, char **argv)
184 case 't': 92 case 't':
185 flags |= gunzip_test; 93 flags |= gunzip_test;
186 break; 94 break;
187 case 'v':
188 flags |= gunzip_verbose;
189 break;
190 case 'd': /* Used to convert gzip to gunzip. */ 95 case 'd': /* Used to convert gzip to gunzip. */
191 break; 96 break;
192 case 'q':
193 error_msg("-q option not supported, ignored");
194 break;
195 case 'h':
196 default: 97 default:
197 show_usage(); /* exit's inside usage */ 98 show_usage(); /* exit's inside usage */
198 } 99 }
199 } 100 }
200 101
201 if (optind == argc) { 102 do {
202 if (gunzip_file(NULL, flags) < 0) { 103 FILE *in_file, *out_file;
203 status = EXIT_FAILURE; 104 struct stat stat_buf;
204 } 105 const char *old_path = argv[optind];
205 } else { 106 const char *delete_path = NULL;
206 for (i = optind; i < argc; i++) { 107 char *new_path = NULL;
207 if (gunzip_file(argv[i], flags) < 0) { 108
109 optind++;
110
111 if (old_path == NULL || strcmp(old_path, "-") == 0) {
112 in_file = stdin;
113 flags |= gunzip_to_stdout;
114 } else {
115 in_file = wfopen(old_path, "r");
116 if (in_file == NULL) {
208 status = EXIT_FAILURE; 117 status = EXIT_FAILURE;
118 break;
119 }
120
121 /* Get the time stamp on the input file. */
122 if (stat(old_path, &stat_buf) < 0) {
123 error_msg_and_die("Couldn't stat file %s", old_path);
209 } 124 }
210 } 125 }
211 } 126
127 /* Check that the input is sane. */
128 if (isatty(fileno(in_file)) && ((flags & gunzip_force) == 0)) {
129 error_msg_and_die
130 ("compressed data not read from terminal. Use -f to force it.");
131 }
132
133 /* Set output filename and number */
134 if (flags & gunzip_test) {
135 out_file = xfopen("/dev/null", "w"); /* why does test use filenum 2 ? */
136 } else if (flags & gunzip_to_stdout) {
137 out_file = stdout;
138 } else {
139 char *extension;
140
141 new_path = xstrdup(old_path);
142
143 extension = strrchr(new_path, '.');
144 if (extension && (strcmp(extension, ".gz") == 0)) {
145 *extension = '\0';
146 } else if (extension && (strcmp(extension, ".tgz") == 0)) {
147 extension[2] = 'a';
148 extension[3] = 'r';
149 } else {
150 error_msg_and_die("Invalid extension");
151 }
152
153 /* Open output file */
154 out_file = xfopen(new_path, "w");
155
156 /* Set permissions on the file */
157 chmod(old_path, stat_buf.st_mode);
158
159 /* If unzip succeeds remove the old file */
160 delete_path = old_path;
161 }
162
163 /* do the decompression, and cleanup */
164 if ((unzip(in_file, out_file) != 0) && (new_path)) {
165 /* Unzip failed, remove new path instead of old path */
166 delete_path = new_path;
167 }
168
169 if (out_file != stdout) {
170 fclose(out_file);
171 }
172 if (in_file != stdin) {
173 fclose(in_file);
174 }
175
176 /* delete_path will be NULL if in test mode or from stdin */
177 if (delete_path && (unlink(delete_path) == -1)) {
178 error_msg_and_die("Couldn't remove %s", delete_path);
179 }
180
181 free(new_path);
182
183 } while (optind < argc);
184
212 return status; 185 return status;
213} 186}