Move bunzip2 idecompression code to libunarchive
[oweals/busybox.git] / archival / bunzip2.c
1 /*
2  *  Modified for busybox by Glenn McGrath <bug1@optushome.com.au>
3  *  Added support output to stdout by Thomas Lundquist <thomasez@zelow.no>
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #include <getopt.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24
25 #include "busybox.h"
26 #include "unarchive.h"
27
28 int bunzip2_main(int argc, char **argv)
29 {
30         const int bunzip_to_stdout = 1;
31         const int bunzip_force = 2;
32         int flags = 0;
33         int opt = 0;
34         int status;
35
36         FILE *src_stream;
37         FILE *dst_stream;
38         char *save_name = NULL;
39         char *delete_name = NULL;
40
41         /* if called as bzcat */
42         if (strcmp(applet_name, "bzcat") == 0)
43                 flags |= bunzip_to_stdout;
44
45         while ((opt = getopt(argc, argv, "cfh")) != -1) {
46                 switch (opt) {
47                 case 'c':
48                         flags |= bunzip_to_stdout;
49                         break;
50                 case 'f':
51                         flags |= bunzip_force;
52                         break;
53                 case 'h':
54                 default:
55                         show_usage(); /* exit's inside usage */
56                 }
57         }
58
59         /* Set input filename and number */
60         if (argv[optind] == NULL || strcmp(argv[optind], "-") == 0) {
61                 flags |= bunzip_to_stdout;
62                 src_stream = stdin;
63         } else {
64                 /* Open input file */
65                 src_stream = xfopen(argv[optind], "r");
66
67                 save_name = xstrdup(argv[optind]);
68                 if (strcmp(save_name + strlen(save_name) - 4, ".bz2") != 0)
69                         error_msg_and_die("Invalid extension");
70                 save_name[strlen(save_name) - 4] = '\0';
71         }
72
73         /* Check that the input is sane.  */
74         if (isatty(fileno(src_stream)) && (flags & bunzip_force) == 0)
75                 error_msg_and_die("compressed data not read from terminal.  Use -f to force it.");
76
77         if (flags & bunzip_to_stdout) {
78                 dst_stream = stdout;
79         } else {
80                 dst_stream = xfopen(save_name, "w");
81         }
82
83         if (uncompressStream(src_stream, dst_stream)) {
84                 if (!(flags & bunzip_to_stdout))
85                         delete_name = argv[optind];
86                 status = EXIT_SUCCESS;
87         } else {
88                 if (!(flags & bunzip_to_stdout))
89                         delete_name = save_name;
90                 status = EXIT_FAILURE;
91         }
92
93         if (delete_name) {
94                 if (unlink(delete_name) < 0) {
95                         error_msg_and_die("Couldn't remove %s", delete_name);
96                 }
97         }
98
99         return status;
100 }