summaryrefslogtreecommitdiff
path: root/archival/uncompress.c
diff options
context:
space:
mode:
authorGlenn L McGrath <bug1@ihug.co.nz>2003-06-22 06:59:34 +0000
committerGlenn L McGrath <bug1@ihug.co.nz>2003-06-22 06:59:34 +0000
commita0b3705fa12280f29cde2b4c8d272b703735715f (patch)
tree0ce8a0c4c74f4a67909251be8047cc14893ff902 /archival/uncompress.c
parentf0f754aeaf47b416abba8206dd2632cf24bb94a3 (diff)
downloadbusybox-w32-a0b3705fa12280f29cde2b4c8d272b703735715f.tar.gz
busybox-w32-a0b3705fa12280f29cde2b4c8d272b703735715f.tar.bz2
busybox-w32-a0b3705fa12280f29cde2b4c8d272b703735715f.zip
Save a few bytes by using bb_getopt_ulflags and a few other minor
improvments
Diffstat (limited to '')
-rw-r--r--archival/uncompress.c80
1 files changed, 31 insertions, 49 deletions
diff --git a/archival/uncompress.c b/archival/uncompress.c
index 2d2145d18..2de37d273 100644
--- a/archival/uncompress.c
+++ b/archival/uncompress.c
@@ -28,89 +28,72 @@
28#include "libbb.h" 28#include "libbb.h"
29#include "unarchive.h" 29#include "unarchive.h"
30 30
31int uncompress_main(int argc, char **argv) 31#define GUNZIP_TO_STDOUT 1
32#define GUNZIP_FORCE 2
33
34extern int uncompress_main(int argc, char **argv)
32{ 35{
33 const char gunzip_to_stdout = 1; 36 int status = EXIT_SUCCESS;
34 const char gunzip_force = 2; 37 unsigned long flags;
35 char status = EXIT_SUCCESS;
36 char flags = 0;
37 int opt;
38
39 while ((opt = getopt(argc, argv, "cfh")) != -1) {
40 switch (opt) {
41 case 'c':
42 flags |= gunzip_to_stdout;
43 break;
44 case 'f':
45 flags |= gunzip_force;
46 break;
47 default:
48 bb_show_usage(); /* exit's inside usage */
49 }
50 }
51 38
52 do { 39 flags = bb_getopt_ulflags(argc, argv, "cf");
53 struct stat stat_buf; 40
54 const char *old_path = argv[optind]; 41 while (optind < argc) {
42 const char *compressed_file = argv[optind++];
55 const char *delete_path = NULL; 43 const char *delete_path = NULL;
56 char *new_path = NULL; 44 char *uncompressed_file = NULL;
57 int src_fd; 45 int src_fd;
58 int dst_fd; 46 int dst_fd;
59 47
60 optind++; 48 if (strcmp(compressed_file, "-") == 0) {
61
62 if (old_path == NULL || strcmp(old_path, "-") == 0) {
63 src_fd = fileno(stdin); 49 src_fd = fileno(stdin);
64 flags |= gunzip_to_stdout; 50 flags |= GUNZIP_TO_STDOUT;
65 } else { 51 } else {
66 src_fd = bb_xopen(old_path, O_RDONLY); 52 src_fd = bb_xopen(compressed_file, O_RDONLY);
67
68 /* Get the time stamp on the input file. */
69 if (stat(old_path, &stat_buf) < 0) {
70 bb_error_msg_and_die("Couldn't stat file %s", old_path);
71 }
72 } 53 }
73 54
74 /* Check that the input is sane. */ 55 /* Check that the input is sane. */
75 if (isatty(src_fd) && ((flags & gunzip_force) == 0)) { 56 if (isatty(src_fd) && ((flags & GUNZIP_FORCE) == 0)) {
76 bb_error_msg_and_die 57 bb_error_msg_and_die
77 ("compressed data not read from terminal. Use -f to force it."); 58 ("compressed data not read from terminal. Use -f to force it.");
78 } 59 }
79 60
80 /* Set output filename and number */ 61 /* Set output filename and number */
81 if (flags & gunzip_to_stdout) { 62 if (flags & GUNZIP_TO_STDOUT) {
82 dst_fd = fileno(stdout); 63 dst_fd = fileno(stdout);
83 } else { 64 } else {
65 struct stat stat_buf;
84 char *extension; 66 char *extension;
85 67
86 new_path = bb_xstrdup(old_path); 68 uncompressed_file = bb_xstrdup(compressed_file);
87 69
88 extension = strrchr(new_path, '.'); 70 extension = strrchr(uncompressed_file, '.');
89 if (!extension || (strcmp(extension, ".Z") != 0)) { 71 if (!extension || (strcmp(extension, ".Z") != 0)) {
90 bb_error_msg_and_die("Invalid extension"); 72 bb_error_msg_and_die("Invalid extension");
91 } 73 }
92 *extension = '\0'; 74 *extension = '\0';
93 75
94 /* Open output file */ 76 /* Open output file */
95 dst_fd = bb_xopen(new_path, O_WRONLY | O_CREAT); 77 dst_fd = bb_xopen(uncompressed_file, O_WRONLY | O_CREAT);
96 78
97 /* Set permissions on the file */ 79 /* Set permissions on the file */
98 chmod(new_path, stat_buf.st_mode); 80 stat(compressed_file, &stat_buf);
81 chmod(uncompressed_file, stat_buf.st_mode);
99 82
100 /* If unzip succeeds remove the old file */ 83 /* If unzip succeeds remove the old file */
101 delete_path = old_path; 84 delete_path = compressed_file;
102 } 85 }
103 86
104 /* do the decompression, and cleanup */ 87 /* do the decompression, and cleanup */
105 if ((bb_xread_char(src_fd) == 0x1f) && (bb_xread_char(src_fd) == 0x9d)) { 88 if ((bb_xread_char(src_fd) != 0x1f) || (bb_xread_char(src_fd) != 0x9d)) {
106 status = uncompress(src_fd, dst_fd);
107 } else {
108 bb_error_msg_and_die("Invalid magic"); 89 bb_error_msg_and_die("Invalid magic");
109 } 90 }
110 91
111 if ((status != EXIT_SUCCESS) && (new_path)) { 92 status = uncompress(src_fd, dst_fd);
112 /* Unzip failed, remove new path instead of old path */ 93
113 delete_path = new_path; 94 if ((status != EXIT_SUCCESS) && (uncompressed_file)) {
95 /* Unzip failed, remove the uncomressed file instead of compressed file */
96 delete_path = uncompressed_file;
114 } 97 }
115 98
116 if (dst_fd != fileno(stdout)) { 99 if (dst_fd != fileno(stdout)) {
@@ -125,9 +108,8 @@ int uncompress_main(int argc, char **argv)
125 bb_error_msg_and_die("Couldn't remove %s", delete_path); 108 bb_error_msg_and_die("Couldn't remove %s", delete_path);
126 } 109 }
127 110
128 free(new_path); 111 free(uncompressed_file);
129 112 }
130 } while (optind < argc);
131 113
132 return status; 114 return status;
133} 115}