diff options
author | Glenn L McGrath <bug1@ihug.co.nz> | 2004-01-05 11:49:55 +0000 |
---|---|---|
committer | Glenn L McGrath <bug1@ihug.co.nz> | 2004-01-05 11:49:55 +0000 |
commit | 6cb3bc056c5bf49e6b0910a2437b908777bef139 (patch) | |
tree | 84c8a6d7455c40ccb757bc001192c4a6f198d0fa /archival | |
parent | 08ca752c68ead7d9911fb89ae1b03c36e11e6529 (diff) | |
download | busybox-w32-6cb3bc056c5bf49e6b0910a2437b908777bef139.tar.gz busybox-w32-6cb3bc056c5bf49e6b0910a2437b908777bef139.tar.bz2 busybox-w32-6cb3bc056c5bf49e6b0910a2437b908777bef139.zip |
Use bb_getopt_ulflags, simplify some logic, saves some bytes.
Diffstat (limited to 'archival')
-rw-r--r-- | archival/bunzip2.c | 69 |
1 files changed, 29 insertions, 40 deletions
diff --git a/archival/bunzip2.c b/archival/bunzip2.c index 191dda035..e2c3ca91d 100644 --- a/archival/bunzip2.c +++ b/archival/bunzip2.c | |||
@@ -27,73 +27,62 @@ | |||
27 | #include "busybox.h" | 27 | #include "busybox.h" |
28 | #include "unarchive.h" | 28 | #include "unarchive.h" |
29 | 29 | ||
30 | #define BUNZIP2_OPT_STDOUT 1 | ||
31 | #define BUNZIP2_OPT_FORCE 2 | ||
32 | |||
30 | int bunzip2_main(int argc, char **argv) | 33 | int bunzip2_main(int argc, char **argv) |
31 | { | 34 | { |
32 | const int bunzip_to_stdout = 1; | 35 | char *compressed_name; |
33 | const int bunzip_force = 2; | 36 | char *save_name; |
34 | int flags = 0; | 37 | unsigned long opt; |
35 | int opt = 0; | ||
36 | int status; | 38 | int status; |
37 | |||
38 | int src_fd; | 39 | int src_fd; |
39 | int dst_fd; | 40 | int dst_fd; |
40 | char *save_name = NULL; | ||
41 | char *delete_name = NULL; | ||
42 | 41 | ||
43 | /* if called as bzcat */ | 42 | opt = bb_getopt_ulflags(argc, argv, "cf"); |
44 | if (strcmp(bb_applet_name, "bzcat") == 0) | ||
45 | flags |= bunzip_to_stdout; | ||
46 | 43 | ||
47 | while ((opt = getopt(argc, argv, "cfh")) != -1) { | 44 | /* if called as bzcat force the stdout flag */ |
48 | switch (opt) { | 45 | if (bb_applet_name[2] == 'c') { |
49 | case 'c': | 46 | opt |= BUNZIP2_OPT_STDOUT; |
50 | flags |= bunzip_to_stdout; | ||
51 | break; | ||
52 | case 'f': | ||
53 | flags |= bunzip_force; | ||
54 | break; | ||
55 | case 'h': | ||
56 | default: | ||
57 | bb_show_usage(); /* exit's inside usage */ | ||
58 | } | ||
59 | } | 47 | } |
60 | 48 | ||
61 | /* Set input filename and number */ | 49 | /* Set input filename and number */ |
62 | if (argv[optind] == NULL || strcmp(argv[optind], "-") == 0) { | 50 | compressed_name = argv[optind]; |
63 | flags |= bunzip_to_stdout; | 51 | if ((compressed_name) && (compressed_name[0] != '-') && (compressed_name[1] != '\0')) { |
64 | src_fd = fileno(stdin); | ||
65 | } else { | ||
66 | /* Open input file */ | 52 | /* Open input file */ |
67 | src_fd = bb_xopen(argv[optind], O_RDONLY); | 53 | src_fd = bb_xopen(compressed_name, O_RDONLY); |
68 | 54 | } else { | |
69 | save_name = bb_xstrdup(argv[optind]); | 55 | src_fd = fileno(stdin); |
70 | if (strcmp(save_name + strlen(save_name) - 4, ".bz2") != 0) | 56 | opt |= BUNZIP2_OPT_STDOUT; |
71 | bb_error_msg_and_die("Invalid extension"); | ||
72 | save_name[strlen(save_name) - 4] = '\0'; | ||
73 | } | 57 | } |
74 | 58 | ||
75 | /* Check that the input is sane. */ | 59 | /* Check that the input is sane. */ |
76 | if (isatty(src_fd) && (flags & bunzip_force) == 0) { | 60 | if (isatty(src_fd) && (opt & BUNZIP2_OPT_FORCE) == 0) { |
77 | bb_error_msg_and_die("compressed data not read from terminal. Use -f to force it."); | 61 | bb_error_msg_and_die("compressed data not read from terminal. Use -f to force it."); |
78 | } | 62 | } |
79 | 63 | ||
80 | if (flags & bunzip_to_stdout) { | 64 | if (opt & BUNZIP2_OPT_STDOUT) { |
81 | dst_fd = fileno(stdout); | 65 | dst_fd = fileno(stdout); |
82 | } else { | 66 | } else { |
67 | int len = strlen(compressed_name) - 4; | ||
68 | if (strcmp(compressed_name + len, ".bz2") != 0) { | ||
69 | bb_error_msg_and_die("Invalid extension"); | ||
70 | } | ||
71 | save_name = bb_xstrndup(compressed_name, len); | ||
83 | dst_fd = bb_xopen(save_name, O_WRONLY | O_CREAT); | 72 | dst_fd = bb_xopen(save_name, O_WRONLY | O_CREAT); |
84 | } | 73 | } |
85 | 74 | ||
86 | status = uncompressStream(src_fd, dst_fd); | 75 | status = uncompressStream(src_fd, dst_fd); |
87 | if(!(flags & bunzip_to_stdout)) { | 76 | if(!(opt & BUNZIP2_OPT_STDOUT)) { |
77 | char *delete_name; | ||
88 | if (status) { | 78 | if (status) { |
89 | delete_name = save_name; | 79 | delete_name = save_name; |
90 | } else { | 80 | } else { |
91 | delete_name = argv[optind]; | 81 | delete_name = compressed_name; |
82 | } | ||
83 | if (unlink(delete_name) < 0) { | ||
84 | bb_error_msg_and_die("Couldn't remove %s", delete_name); | ||
92 | } | 85 | } |
93 | } | ||
94 | |||
95 | if ((delete_name) && (unlink(delete_name) < 0)) { | ||
96 | bb_error_msg_and_die("Couldn't remove %s", delete_name); | ||
97 | } | 86 | } |
98 | 87 | ||
99 | return status; | 88 | return status; |