Use bb_getopt_ulflags, simplify some logic, saves some bytes.
This commit is contained in:
parent
08ca752c68
commit
6cb3bc056c
@ -27,73 +27,62 @@
|
|||||||
#include "busybox.h"
|
#include "busybox.h"
|
||||||
#include "unarchive.h"
|
#include "unarchive.h"
|
||||||
|
|
||||||
|
#define BUNZIP2_OPT_STDOUT 1
|
||||||
|
#define BUNZIP2_OPT_FORCE 2
|
||||||
|
|
||||||
int bunzip2_main(int argc, char **argv)
|
int bunzip2_main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
const int bunzip_to_stdout = 1;
|
char *compressed_name;
|
||||||
const int bunzip_force = 2;
|
char *save_name;
|
||||||
int flags = 0;
|
unsigned long opt;
|
||||||
int opt = 0;
|
|
||||||
int status;
|
int status;
|
||||||
|
|
||||||
int src_fd;
|
int src_fd;
|
||||||
int dst_fd;
|
int dst_fd;
|
||||||
char *save_name = NULL;
|
|
||||||
char *delete_name = NULL;
|
|
||||||
|
|
||||||
/* if called as bzcat */
|
opt = bb_getopt_ulflags(argc, argv, "cf");
|
||||||
if (strcmp(bb_applet_name, "bzcat") == 0)
|
|
||||||
flags |= bunzip_to_stdout;
|
|
||||||
|
|
||||||
while ((opt = getopt(argc, argv, "cfh")) != -1) {
|
/* if called as bzcat force the stdout flag */
|
||||||
switch (opt) {
|
if (bb_applet_name[2] == 'c') {
|
||||||
case 'c':
|
opt |= BUNZIP2_OPT_STDOUT;
|
||||||
flags |= bunzip_to_stdout;
|
|
||||||
break;
|
|
||||||
case 'f':
|
|
||||||
flags |= bunzip_force;
|
|
||||||
break;
|
|
||||||
case 'h':
|
|
||||||
default:
|
|
||||||
bb_show_usage(); /* exit's inside usage */
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Set input filename and number */
|
/* Set input filename and number */
|
||||||
if (argv[optind] == NULL || strcmp(argv[optind], "-") == 0) {
|
compressed_name = argv[optind];
|
||||||
flags |= bunzip_to_stdout;
|
if ((compressed_name) && (compressed_name[0] != '-') && (compressed_name[1] != '\0')) {
|
||||||
src_fd = fileno(stdin);
|
|
||||||
} else {
|
|
||||||
/* Open input file */
|
/* Open input file */
|
||||||
src_fd = bb_xopen(argv[optind], O_RDONLY);
|
src_fd = bb_xopen(compressed_name, O_RDONLY);
|
||||||
|
} else {
|
||||||
save_name = bb_xstrdup(argv[optind]);
|
src_fd = fileno(stdin);
|
||||||
if (strcmp(save_name + strlen(save_name) - 4, ".bz2") != 0)
|
opt |= BUNZIP2_OPT_STDOUT;
|
||||||
bb_error_msg_and_die("Invalid extension");
|
|
||||||
save_name[strlen(save_name) - 4] = '\0';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Check that the input is sane. */
|
/* Check that the input is sane. */
|
||||||
if (isatty(src_fd) && (flags & bunzip_force) == 0) {
|
if (isatty(src_fd) && (opt & BUNZIP2_OPT_FORCE) == 0) {
|
||||||
bb_error_msg_and_die("compressed data not read from terminal. Use -f to force it.");
|
bb_error_msg_and_die("compressed data not read from terminal. Use -f to force it.");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (flags & bunzip_to_stdout) {
|
if (opt & BUNZIP2_OPT_STDOUT) {
|
||||||
dst_fd = fileno(stdout);
|
dst_fd = fileno(stdout);
|
||||||
} else {
|
} else {
|
||||||
|
int len = strlen(compressed_name) - 4;
|
||||||
|
if (strcmp(compressed_name + len, ".bz2") != 0) {
|
||||||
|
bb_error_msg_and_die("Invalid extension");
|
||||||
|
}
|
||||||
|
save_name = bb_xstrndup(compressed_name, len);
|
||||||
dst_fd = bb_xopen(save_name, O_WRONLY | O_CREAT);
|
dst_fd = bb_xopen(save_name, O_WRONLY | O_CREAT);
|
||||||
}
|
}
|
||||||
|
|
||||||
status = uncompressStream(src_fd, dst_fd);
|
status = uncompressStream(src_fd, dst_fd);
|
||||||
if(!(flags & bunzip_to_stdout)) {
|
if(!(opt & BUNZIP2_OPT_STDOUT)) {
|
||||||
|
char *delete_name;
|
||||||
if (status) {
|
if (status) {
|
||||||
delete_name = save_name;
|
delete_name = save_name;
|
||||||
} else {
|
} else {
|
||||||
delete_name = argv[optind];
|
delete_name = compressed_name;
|
||||||
|
}
|
||||||
|
if (unlink(delete_name) < 0) {
|
||||||
|
bb_error_msg_and_die("Couldn't remove %s", delete_name);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if ((delete_name) && (unlink(delete_name) < 0)) {
|
|
||||||
bb_error_msg_and_die("Couldn't remove %s", delete_name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return status;
|
return status;
|
||||||
|
Loading…
Reference in New Issue
Block a user