aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>2002-04-13 09:10:34 +0000
committerEric Andersen <andersen@codepoet.org>2002-04-13 09:10:34 +0000
commitd75ac02a4ffb6c843794d8f7d745ee083bdb0516 (patch)
treea0b129b3314f607b98777251c0a5fa23c0957141
parent114de5566849b3ab533f2892d5edfdf16c542db5 (diff)
downloadbusybox-w32-d75ac02a4ffb6c843794d8f7d745ee083bdb0516.tar.gz
busybox-w32-d75ac02a4ffb6c843794d8f7d745ee083bdb0516.tar.bz2
busybox-w32-d75ac02a4ffb6c843794d8f7d745ee083bdb0516.zip
Rework per how I did things in version in 0.60.3 so it can
properly uncompress multiple files now. -Erik
-rw-r--r--archival/gunzip.c157
1 files changed, 88 insertions, 69 deletions
diff --git a/archival/gunzip.c b/archival/gunzip.c
index dde670167..e40982cd1 100644
--- a/archival/gunzip.c
+++ b/archival/gunzip.c
@@ -68,27 +68,22 @@ static char *license_msg[] = {
68 68
69extern int gunzip_main(int argc, char **argv) 69extern int gunzip_main(int argc, char **argv)
70{ 70{
71 FILE *in_file = stdin; 71 int flags = 0;
72 FILE *out_file = NULL; 72 int opt = 0;
73 int delete_old_file, file_count;
73 struct stat stat_buf; 74 struct stat stat_buf;
74 75 FILE *in_file, *out_file;
75 char *if_name = NULL; 76 char *if_name, *of_name, *delete_file_name;
76 char *of_name = NULL;
77 char *delete_file_name = NULL;
78
79 const int gunzip_to_stdout = 1; 77 const int gunzip_to_stdout = 1;
80 const int gunzip_force = 2; 78 const int gunzip_force = 2;
81 const int gunzip_test = 4; 79 const int gunzip_test = 4;
82 80 const int gunzip_verbose = 8;
83 int flags = 0;
84 int opt = 0;
85 int delete_old_file = FALSE;
86 81
87 /* if called as zcat */ 82 /* if called as zcat */
88 if (strcmp(applet_name, "zcat") == 0) 83 if (strcmp(applet_name, "zcat") == 0)
89 flags |= gunzip_to_stdout; 84 flags |= gunzip_to_stdout;
90 85
91 while ((opt = getopt(argc, argv, "ctfhdq")) != -1) { 86 while ((opt = getopt(argc, argv, "ctfhdqv")) != -1) {
92 switch (opt) { 87 switch (opt) {
93 case 'c': 88 case 'c':
94 flags |= gunzip_to_stdout; 89 flags |= gunzip_to_stdout;
@@ -99,6 +94,9 @@ extern int gunzip_main(int argc, char **argv)
99 case 't': 94 case 't':
100 flags |= gunzip_test; 95 flags |= gunzip_test;
101 break; 96 break;
97 case 'v':
98 flags |= gunzip_verbose;
99 break;
102 case 'd': /* Used to convert gzip to gunzip. */ 100 case 'd': /* Used to convert gzip to gunzip. */
103 break; 101 break;
104 case 'q': 102 case 'q':
@@ -110,74 +108,95 @@ extern int gunzip_main(int argc, char **argv)
110 } 108 }
111 } 109 }
112 110
113 /* Set input filename and number */ 111 file_count = argc - optind;
114 if (argv[optind] == NULL || strcmp(argv[optind], "-") == 0) { 112 while (file_count==0 || optind < argc) {
115 flags |= gunzip_to_stdout; 113 in_file = stdin;
116 } else { 114 out_file = NULL;
117 if_name = xstrdup(argv[optind]); 115 if_name = NULL;
118 /* Open input file */ 116 of_name = NULL;
119 in_file = xfopen(if_name, "r"); 117 delete_file_name = NULL;
118 delete_old_file = FALSE;
119
120 /* Set input filename and number */
121 if (argv[optind] == NULL || strcmp(argv[optind], "-") == 0) {
122 flags |= gunzip_to_stdout;
123 /* Skip to the end */
124 optind = argc;
125 } else {
126 if_name = strdup(argv[optind]);
127 /* Open input file */
128 in_file = xfopen(if_name, "r");
120 129
121 /* set the buffer size */ 130 if (flags & gunzip_verbose) {
122 setvbuf(in_file, NULL, _IOFBF, 0x8000); 131 fprintf(stderr, "%s:\t", if_name);
132 }
123 133
124 /* Get the time stamp on the input file. */ 134 /* set the buffer size */
125 if (stat(if_name, &stat_buf) < 0) { 135 setvbuf(in_file, NULL, _IOFBF, 0x8000);
126 error_msg_and_die("Couldn't stat file %s", if_name);
127 }
128 }
129 136
130 /* Check that the input is sane. */ 137 /* Get the time stamp on the input file. */
131 if (isatty(fileno(in_file)) && (flags & gunzip_force) == 0) 138 if (stat(if_name, &stat_buf) < 0) {
132 error_msg_and_die("compressed data not read from terminal. Use -f to force it."); 139 error_msg_and_die("Couldn't stat file %s", if_name);
133 140 }
134 /* Set output filename and number */
135 if (flags & gunzip_test) {
136 out_file = xfopen("/dev/null", "w"); /* why does test use filenum 2 ? */
137 } else if (flags & gunzip_to_stdout) {
138 out_file = stdout;
139 } else {
140 char *extension;
141 int length = strlen(if_name);
142
143 delete_old_file = TRUE;
144 extension = strrchr(if_name, '.');
145 if (extension && strcmp(extension, ".gz") == 0) {
146 length -= 3;
147 } else if (extension && strcmp(extension, ".tgz") == 0) {
148 length -= 4;
149 } else {
150 error_msg_and_die("Invalid extension");
151 } 141 }
152 of_name = (char *) xcalloc(sizeof(char), length + 1);
153 strncpy(of_name, if_name, length);
154 142
155 /* Open output file */ 143 /* Check that the input is sane. */
156 out_file = xfopen(of_name, "w"); 144 if (isatty(fileno(in_file)) && (flags & gunzip_force) == 0)
145 error_msg_and_die("compressed data not read from terminal. Use -f to force it.");
157 146
158 /* Set permissions on the file */ 147 /* Set output filename and number */
159 chmod(of_name, stat_buf.st_mode); 148 if (flags & gunzip_test) {
160 } 149 out_file = xfopen("/dev/null", "w"); /* why does test use filenum 2 ? */
150 } else if (flags & gunzip_to_stdout) {
151 out_file = stdout;
152 } else {
153 char *extension;
154 int length = strlen(if_name);
155
156 delete_old_file = TRUE;
157 extension = strrchr(if_name, '.');
158 if (extension && strcmp(extension, ".gz") == 0) {
159 length -= 3;
160 } else if (extension && strcmp(extension, ".tgz") == 0) {
161 length -= 4;
162 } else {
163 error_msg_and_die("Invalid extension");
164 }
165 of_name = (char *) xcalloc(sizeof(char), length + 1);
166 strncpy(of_name, if_name, length);
167
168 /* Open output file */
169 out_file = xfopen(of_name, "w");
170
171 /* Set permissions on the file */
172 chmod(of_name, stat_buf.st_mode);
173 }
161 174
162 /* do the decompression, and cleanup */ 175 /* do the decompression, and cleanup */
163 if (unzip(in_file, out_file) == 0) { 176 if (unzip(in_file, out_file) == 0) {
164 /* Success, remove .gz file */ 177 /* Success, remove .gz file */
165 delete_file_name = if_name; 178 delete_file_name = if_name;
166 } else { 179 if (flags & gunzip_verbose) {
167 /* remove failed attempt */ 180 fprintf(stderr, "OK\n");
168 delete_file_name = of_name; 181 }
169 } 182 } else {
183 /* remove failed attempt */
184 delete_file_name = of_name;
185 }
170 186
171 fclose(out_file); 187 fclose(out_file);
172 fclose(in_file); 188 fclose(in_file);
173 189
174 if (delete_old_file) { 190 if (delete_old_file == TRUE && !(flags & gunzip_test)) {
175 if (unlink(delete_file_name) < 0) { 191 if (unlink(delete_file_name) < 0) {
176 error_msg_and_die("Couldnt remove %s", delete_file_name); 192 error_msg_and_die("Couldnt remove %s", delete_file_name);
193 }
177 } 194 }
178 }
179 195
180 free(of_name); 196 free(of_name);
197 optind++;
198 } /* while () */
181 199
182 return(EXIT_SUCCESS); 200 return(EXIT_SUCCESS);
183} 201}
202