diff options
author | Glenn L McGrath <bug1@ihug.co.nz> | 2002-11-03 14:05:15 +0000 |
---|---|---|
committer | Glenn L McGrath <bug1@ihug.co.nz> | 2002-11-03 14:05:15 +0000 |
commit | 237ae42fc96ede945d28d9054f045b73e419d089 (patch) | |
tree | 3fb6a9c10150303aca3c218b47aaf327a186382a /archival/bunzip2.c | |
parent | 2fc54a9258c3aa5dad2ce9807ba85cf29af2668e (diff) | |
download | busybox-w32-237ae42fc96ede945d28d9054f045b73e419d089.tar.gz busybox-w32-237ae42fc96ede945d28d9054f045b73e419d089.tar.bz2 busybox-w32-237ae42fc96ede945d28d9054f045b73e419d089.zip |
Abstract read and seek in unarchiving code, convert bunzip to file descriptors, support tar -j
Diffstat (limited to 'archival/bunzip2.c')
-rw-r--r-- | archival/bunzip2.c | 31 |
1 files changed, 17 insertions, 14 deletions
diff --git a/archival/bunzip2.c b/archival/bunzip2.c index 9f346f266..d5c06f4fd 100644 --- a/archival/bunzip2.c +++ b/archival/bunzip2.c | |||
@@ -17,9 +17,11 @@ | |||
17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | 17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
18 | */ | 18 | */ |
19 | 19 | ||
20 | #include <fcntl.h> | ||
20 | #include <getopt.h> | 21 | #include <getopt.h> |
21 | #include <stdio.h> | 22 | #include <stdio.h> |
22 | #include <stdlib.h> | 23 | #include <stdlib.h> |
24 | #include <string.h> | ||
23 | #include <unistd.h> | 25 | #include <unistd.h> |
24 | 26 | ||
25 | #include "busybox.h" | 27 | #include "busybox.h" |
@@ -33,8 +35,8 @@ int bunzip2_main(int argc, char **argv) | |||
33 | int opt = 0; | 35 | int opt = 0; |
34 | int status; | 36 | int status; |
35 | 37 | ||
36 | FILE *src_stream; | 38 | int src_fd; |
37 | FILE *dst_stream; | 39 | int dst_fd; |
38 | char *save_name = NULL; | 40 | char *save_name = NULL; |
39 | char *delete_name = NULL; | 41 | char *delete_name = NULL; |
40 | 42 | ||
@@ -59,10 +61,10 @@ int bunzip2_main(int argc, char **argv) | |||
59 | /* Set input filename and number */ | 61 | /* Set input filename and number */ |
60 | if (argv[optind] == NULL || strcmp(argv[optind], "-") == 0) { | 62 | if (argv[optind] == NULL || strcmp(argv[optind], "-") == 0) { |
61 | flags |= bunzip_to_stdout; | 63 | flags |= bunzip_to_stdout; |
62 | src_stream = stdin; | 64 | src_fd = fileno(stdin); |
63 | } else { | 65 | } else { |
64 | /* Open input file */ | 66 | /* Open input file */ |
65 | src_stream = xfopen(argv[optind], "r"); | 67 | src_fd = xopen(argv[optind], O_RDONLY); |
66 | 68 | ||
67 | save_name = xstrdup(argv[optind]); | 69 | save_name = xstrdup(argv[optind]); |
68 | if (strcmp(save_name + strlen(save_name) - 4, ".bz2") != 0) | 70 | if (strcmp(save_name + strlen(save_name) - 4, ".bz2") != 0) |
@@ -71,29 +73,30 @@ int bunzip2_main(int argc, char **argv) | |||
71 | } | 73 | } |
72 | 74 | ||
73 | /* Check that the input is sane. */ | 75 | /* Check that the input is sane. */ |
74 | if (isatty(fileno(src_stream)) && (flags & bunzip_force) == 0) | 76 | if (isatty(src_fd) && (flags & bunzip_force) == 0) { |
75 | error_msg_and_die("compressed data not read from terminal. Use -f to force it."); | 77 | error_msg_and_die("compressed data not read from terminal. Use -f to force it."); |
78 | } | ||
76 | 79 | ||
77 | if (flags & bunzip_to_stdout) { | 80 | if (flags & bunzip_to_stdout) { |
78 | dst_stream = stdout; | 81 | dst_fd = fileno(stdout); |
79 | } else { | 82 | } else { |
80 | dst_stream = xfopen(save_name, "w"); | 83 | dst_fd = xopen(save_name, O_WRONLY | O_CREAT); |
81 | } | 84 | } |
82 | 85 | ||
83 | if (uncompressStream(src_stream, dst_stream)) { | 86 | if (uncompressStream(src_fd, dst_fd)) { |
84 | if (!(flags & bunzip_to_stdout)) | 87 | if (!(flags & bunzip_to_stdout)) { |
85 | delete_name = argv[optind]; | 88 | delete_name = argv[optind]; |
89 | } | ||
86 | status = EXIT_SUCCESS; | 90 | status = EXIT_SUCCESS; |
87 | } else { | 91 | } else { |
88 | if (!(flags & bunzip_to_stdout)) | 92 | if (!(flags & bunzip_to_stdout)) { |
89 | delete_name = save_name; | 93 | delete_name = save_name; |
94 | } | ||
90 | status = EXIT_FAILURE; | 95 | status = EXIT_FAILURE; |
91 | } | 96 | } |
92 | 97 | ||
93 | if (delete_name) { | 98 | if ((delete_name) && (unlink(delete_name) < 0)) { |
94 | if (unlink(delete_name) < 0) { | 99 | error_msg_and_die("Couldn't remove %s", delete_name); |
95 | error_msg_and_die("Couldn't remove %s", delete_name); | ||
96 | } | ||
97 | } | 100 | } |
98 | 101 | ||
99 | return status; | 102 | return status; |