Avoid symbol naming conflict with libm
[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 #define BUNZIP2_OPT_STDOUT      1
31 #define BUNZIP2_OPT_FORCE       2
32
33 int bunzip2_main(int argc, char **argv)
34 {
35         char *compressed_name;
36         char *save_name;
37         unsigned long opt;
38         int status;
39         int src_fd;
40         int dst_fd;
41
42         opt = bb_getopt_ulflags(argc, argv, "cf");
43
44         /* if called as bzcat force the stdout flag */
45         if (bb_applet_name[2] == 'c') {
46                 opt |= BUNZIP2_OPT_STDOUT;
47         }
48
49         /* Set input filename and number */
50         compressed_name = argv[optind];
51         if ((compressed_name) && (compressed_name[0] != '-') && (compressed_name[1] != '\0')) {
52                 /* Open input file */
53                 src_fd = bb_xopen(compressed_name, O_RDONLY);
54         } else {
55                 src_fd = fileno(stdin);
56                 opt |= BUNZIP2_OPT_STDOUT;
57         }
58
59         /* Check that the input is sane.  */
60         if (isatty(src_fd) && (opt & BUNZIP2_OPT_FORCE) == 0) {
61                 bb_error_msg_and_die("compressed data not read from terminal.  Use -f to force it.");
62         }
63
64         if (opt & BUNZIP2_OPT_STDOUT) {
65                 dst_fd = fileno(stdout);
66         } else {
67                 int len = strlen(compressed_name) - 4;
68                 if (strcmp(compressed_name + len, ".bz2") != 0) {
69                         bb_error_msg_and_die("Invalid extension");
70                 }
71                 save_name = bb_xstrndup(compressed_name, len);
72                 dst_fd = bb_xopen(save_name, O_WRONLY | O_CREAT);
73         }
74
75         status = uncompressStream(src_fd, dst_fd);
76         if(!(opt & BUNZIP2_OPT_STDOUT)) {
77                 char *delete_name;
78                 if (status) {
79                         delete_name = save_name;
80                 } else {
81                         delete_name = compressed_name;
82                 }
83                 if (unlink(delete_name) < 0) {
84                         bb_error_msg_and_die("Couldn't remove %s", delete_name);
85                 }
86         }
87
88         return status;
89 }