summaryrefslogtreecommitdiff
path: root/archival/bunzip2.c
diff options
context:
space:
mode:
authorMatt Kraai2002-03-27 17:31:01 +0000
committerMatt Kraai2002-03-27 17:31:01 +0000
commit9cdb0601eb53ffdb3fb6b94bb1336adbc7d59e5c (patch)
treea8c2712e1b703f28ba0ca4ad662227ddea2bf413 /archival/bunzip2.c
parentfa15f702d2e4c7d377d6cf89e950227405622f1b (diff)
downloadbusybox-9cdb0601eb53ffdb3fb6b94bb1336adbc7d59e5c.zip
busybox-9cdb0601eb53ffdb3fb6b94bb1336adbc7d59e5c.tar.gz
* archival/bunzip2.c: Include <unistd.h>.
(bunzip2_main): Read data from standard input if FILE argument is `-' or omitted. * include/usage.h (bunzip2_trivial_usage, bunzip2_full_usage): Rewrite. * testsuite/bunzip2/bunzip2-reads-from-standard-input: New.
Diffstat (limited to 'archival/bunzip2.c')
-rw-r--r--archival/bunzip2.c39
1 files changed, 22 insertions, 17 deletions
diff --git a/archival/bunzip2.c b/archival/bunzip2.c
index b0a11fd..678779e 100644
--- a/archival/bunzip2.c
+++ b/archival/bunzip2.c
@@ -58,6 +58,7 @@
#include <stdio.h>
#include <string.h>
#include <getopt.h>
+#include <unistd.h>
#include <busybox.h>
//#define TRUE 1
@@ -2319,45 +2320,49 @@ errhandler_io:
int bunzip2_main(int argc, char **argv)
{
const int bunzip_to_stdout = 1;
+ const int bunzip_force = 2;
int flags = 0;
int opt = 0;
FILE *src_stream;
FILE *dst_stream;
- char *save_name;
- char *save_name_ptr;
+ char *save_name = NULL;
/* if called as bzcat */
if (strcmp(applet_name, "bzcat") == 0)
flags |= bunzip_to_stdout;
- while ((opt = getopt(argc, argv, "ch")) != -1) {
+ while ((opt = getopt(argc, argv, "cfh")) != -1) {
switch (opt) {
case 'c':
flags |= bunzip_to_stdout;
break;
+ case 'f':
+ flags |= bunzip_force;
+ break;
case 'h':
default:
show_usage(); /* exit's inside usage */
}
}
- save_name = xstrdup(argv[optind]);
-
- if (save_name == NULL) {
- show_usage();
- }
-
- src_stream = xfopen(argv[optind], "r");
+ /* Set input filename and number */
+ if (argv[optind] == NULL || strcmp(argv[optind], "-") == 0) {
+ flags |= bunzip_to_stdout;
+ src_stream = stdin;
+ } else {
+ /* Open input file */
+ src_stream = xfopen(argv[optind], "r");
- save_name_ptr = strrchr(save_name, '.');
- if (save_name_ptr == NULL) {
- return(FALSE);
- }
- if (strcmp(save_name_ptr, ".bz2") != 0) {
- error_msg("Invalid extension, expected .bz2");
+ save_name = xstrdup(argv[optind]);
+ if (strcmp(save_name + strlen(save_name) - 4, ".bz2") != 0)
+ error_msg_and_die("Invalid extension");
+ save_name[strlen(save_name) - 4] = '\0';
}
- *save_name_ptr = '\0';
+
+ /* Check that the input is sane. */
+ if (isatty(fileno(src_stream)) && (flags & bunzip_force) == 0)
+ error_msg_and_die("compressed data not read from terminal. Use -f to force it.");
if (flags & bunzip_to_stdout) {
dst_stream = stdout;