Vladimir N. Oleynik writes:
[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 <fcntl.h>
21 #include <getopt.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26
27 #include "busybox.h"
28 #include "unarchive.h"
29
30 int bunzip2_main(int argc, char **argv)
31 {
32         const int bunzip_to_stdout = 1;
33         const int bunzip_force = 2;
34         int flags = 0;
35         int opt = 0;
36         int status;
37
38         int src_fd;
39         int dst_fd;
40         char *save_name = NULL;
41         char *delete_name = NULL;
42
43         /* if called as bzcat */
44         if (strcmp(bb_applet_name, "bzcat") == 0)
45                 flags |= bunzip_to_stdout;
46
47         while ((opt = getopt(argc, argv, "cfh")) != -1) {
48                 switch (opt) {
49                 case 'c':
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         }
60
61         /* Set input filename and number */
62         if (argv[optind] == NULL || strcmp(argv[optind], "-") == 0) {
63                 flags |= bunzip_to_stdout;
64                 src_fd = fileno(stdin);
65         } else {
66                 /* Open input file */
67                 src_fd = bb_xopen(argv[optind], O_RDONLY);
68
69                 save_name = bb_xstrdup(argv[optind]);
70                 if (strcmp(save_name + strlen(save_name) - 4, ".bz2") != 0)
71                         bb_error_msg_and_die("Invalid extension");
72                 save_name[strlen(save_name) - 4] = '\0';
73         }
74
75         /* Check that the input is sane.  */
76         if (isatty(src_fd) && (flags & bunzip_force) == 0) {
77                 bb_error_msg_and_die("compressed data not read from terminal.  Use -f to force it.");
78         }
79
80         if (flags & bunzip_to_stdout) {
81                 dst_fd = fileno(stdout);
82         } else {
83                 dst_fd = bb_xopen(save_name, O_WRONLY | O_CREAT);
84         }
85
86         if (uncompressStream(src_fd, dst_fd)) {
87                 if (!(flags & bunzip_to_stdout)) {
88                         delete_name = argv[optind];
89                 }
90                 status = EXIT_SUCCESS;
91         } else {
92                 if (!(flags & bunzip_to_stdout)) {
93                         delete_name = save_name;
94                 }
95                 status = EXIT_FAILURE;
96         }
97
98         if ((delete_name) && (unlink(delete_name) < 0)) {
99                 bb_error_msg_and_die("Couldn't remove %s", delete_name);
100         }
101
102         return status;
103 }