aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkraai <kraai@69ca8d6d-28ef-0310-b511-8ec308f3f277>2002-04-29 15:32:32 +0000
committerkraai <kraai@69ca8d6d-28ef-0310-b511-8ec308f3f277>2002-04-29 15:32:32 +0000
commit3f4af4cec5bbc65de99179e1ad3952d22f72a725 (patch)
tree213b101164633343c6ccf22bc40b8a5f3459b869
parent32c4a42c619bc3a07e556147bc1e9074694a11e7 (diff)
downloadbusybox-w32-3f4af4cec5bbc65de99179e1ad3952d22f72a725.tar.gz
busybox-w32-3f4af4cec5bbc65de99179e1ad3952d22f72a725.tar.bz2
busybox-w32-3f4af4cec5bbc65de99179e1ad3952d22f72a725.zip
* archival/gunzip.c (gunzip_file): New.
(gunzip_main): Handle no arguments correctly. git-svn-id: svn://busybox.net/trunk/busybox@4708 69ca8d6d-28ef-0310-b511-8ec308f3f277
-rw-r--r--archival/gunzip.c198
1 files changed, 99 insertions, 99 deletions
diff --git a/archival/gunzip.c b/archival/gunzip.c
index e40982cd1..e47dd3f83 100644
--- a/archival/gunzip.c
+++ b/archival/gunzip.c
@@ -66,18 +66,100 @@ 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;
70const int gunzip_force = 2;
71const int 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;
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("compressed data not read from terminal. Use -f to force it.");
104
105 /* Set output filename and number */
106 if (flags & gunzip_test) {
107 out_file = xfopen("/dev/null", "w"); /* why does test use filenum 2 ? */
108 } else if (flags & gunzip_to_stdout) {
109 out_file = stdout;
110 } else {
111 char *extension;
112 int length = strlen(path);
113
114 extension = strrchr(path, '.');
115 if (extension && strcmp(extension, ".gz") == 0) {
116 length -= 3;
117 } else if (extension && strcmp(extension, ".tgz") == 0) {
118 length -= 4;
119 } else {
120 error_msg_and_die("Invalid extension");
121 }
122 out_path = (char *) xcalloc(sizeof(char), length + 1);
123 strncpy(out_path, path, length);
124
125 /* Open output file */
126 out_file = xfopen(out_path, "w");
127
128 /* Set permissions on the file */
129 chmod(out_path, stat_buf.st_mode);
130 }
131
132 /* do the decompression, and cleanup */
133 if (unzip(in_file, out_file) == 0) {
134 /* Success, remove .gz file */
135 delete_path = path;
136 if (flags & gunzip_verbose) {
137 fprintf(stderr, "OK\n");
138 }
139 } else {
140 /* remove failed attempt */
141 delete_path = out_path;
142 }
143
144 fclose(out_file);
145 fclose(in_file);
146
147 if (delete_path && !(flags & gunzip_test)) {
148 if (unlink(delete_path) < 0) {
149 error_msg_and_die("Couldn't remove %s", delete_path);
150 }
151 }
152
153 free(out_path);
154
155 return 0;
156}
157
69extern int gunzip_main(int argc, char **argv) 158extern int gunzip_main(int argc, char **argv)
70{ 159{
71 int flags = 0; 160 int flags = 0;
72 int opt = 0; 161 int i, opt;
73 int delete_old_file, file_count; 162 int status = EXIT_SUCCESS;
74 struct stat stat_buf;
75 FILE *in_file, *out_file;
76 char *if_name, *of_name, *delete_file_name;
77 const int gunzip_to_stdout = 1;
78 const int gunzip_force = 2;
79 const int gunzip_test = 4;
80 const int gunzip_verbose = 8;
81 163
82 /* if called as zcat */ 164 /* if called as zcat */
83 if (strcmp(applet_name, "zcat") == 0) 165 if (strcmp(applet_name, "zcat") == 0)
@@ -108,95 +190,13 @@ extern int gunzip_main(int argc, char **argv)
108 } 190 }
109 } 191 }
110 192
111 file_count = argc - optind; 193 if (optind == argc) {
112 while (file_count==0 || optind < argc) { 194 if (gunzip_file (NULL, flags) < 0)
113 in_file = stdin; 195 status = EXIT_FAILURE;
114 out_file = NULL; 196 } else
115 if_name = NULL; 197 for (i = optind; i < argc; i++)
116 of_name = NULL; 198 if (gunzip_file (argv[i], flags) < 0)
117 delete_file_name = NULL; 199 status = EXIT_FAILURE;
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");
129
130 if (flags & gunzip_verbose) {
131 fprintf(stderr, "%s:\t", if_name);
132 }
133
134 /* set the buffer size */
135 setvbuf(in_file, NULL, _IOFBF, 0x8000);
136
137 /* Get the time stamp on the input file. */
138 if (stat(if_name, &stat_buf) < 0) {
139 error_msg_and_die("Couldn't stat file %s", if_name);
140 }
141 }
142 200
143 /* Check that the input is sane. */ 201 return status;
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.");
146
147 /* Set output filename and number */
148 if (flags & gunzip_test) {
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 }
174
175 /* do the decompression, and cleanup */
176 if (unzip(in_file, out_file) == 0) {
177 /* Success, remove .gz file */
178 delete_file_name = if_name;
179 if (flags & gunzip_verbose) {
180 fprintf(stderr, "OK\n");
181 }
182 } else {
183 /* remove failed attempt */
184 delete_file_name = of_name;
185 }
186
187 fclose(out_file);
188 fclose(in_file);
189
190 if (delete_old_file == TRUE && !(flags & gunzip_test)) {
191 if (unlink(delete_file_name) < 0) {
192 error_msg_and_die("Couldnt remove %s", delete_file_name);
193 }
194 }
195
196 free(of_name);
197 optind++;
198 } /* while () */
199
200 return(EXIT_SUCCESS);
201} 202}
202