Since rangecoder is just a bunch of C functions, move it into the one user
[oweals/busybox.git] / archival / uncompress.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  *      Uncompress applet for busybox (c) 2002 Glenn McGrath
4  *
5  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
6  */
7
8 #include <stdlib.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <fcntl.h>
14
15 #include "busybox.h"
16 #include "unarchive.h"
17
18 #define GUNZIP_TO_STDOUT        1
19 #define GUNZIP_FORCE    2
20
21 int uncompress_main(int argc, char **argv)
22 {
23         int status = EXIT_SUCCESS;
24         unsigned long flags;
25
26         flags = bb_getopt_ulflags(argc, argv, "cf");
27
28         while (optind < argc) {
29                 const char *compressed_file = argv[optind++];
30                 const char *delete_path = NULL;
31                 char *uncompressed_file = NULL;
32                 int src_fd;
33                 int dst_fd;
34
35                 if (strcmp(compressed_file, "-") == 0) {
36                         src_fd = STDIN_FILENO;
37                         flags |= GUNZIP_TO_STDOUT;
38                 } else {
39                         src_fd = bb_xopen(compressed_file, O_RDONLY);
40                 }
41
42                 /* Check that the input is sane.  */
43                 if (isatty(src_fd) && ((flags & GUNZIP_FORCE) == 0)) {
44                         bb_error_msg_and_die
45                                 ("compressed data not read from terminal.  Use -f to force it.");
46                 }
47
48                 /* Set output filename and number */
49                 if (flags & GUNZIP_TO_STDOUT) {
50                         dst_fd = STDOUT_FILENO;
51                 } else {
52                         struct stat stat_buf;
53                         char *extension;
54
55                         uncompressed_file = bb_xstrdup(compressed_file);
56
57                         extension = strrchr(uncompressed_file, '.');
58                         if (!extension || (strcmp(extension, ".Z") != 0)) {
59                                 bb_error_msg_and_die("Invalid extension");
60                         }
61                         *extension = '\0';
62
63                         /* Open output file */
64                         xstat(compressed_file, &stat_buf);
65                         dst_fd = bb_xopen3(uncompressed_file, O_WRONLY | O_CREAT,
66                                         stat_buf.st_mode);
67
68                         /* If unzip succeeds remove the old file */
69                         delete_path = compressed_file;
70                 }
71
72                 /* do the decompression, and cleanup */
73                 if ((bb_xread_char(src_fd) != 0x1f) || (bb_xread_char(src_fd) != 0x9d)) {
74                         bb_error_msg_and_die("Invalid magic");
75                 }
76
77                 status = uncompress(src_fd, dst_fd);
78
79                 if ((status != EXIT_SUCCESS) && (uncompressed_file)) {
80                         /* Unzip failed, remove the uncomressed file instead of compressed file */
81                         delete_path = uncompressed_file;
82                 }
83
84                 if (dst_fd != STDOUT_FILENO) {
85                         close(dst_fd);
86                 }
87                 if (src_fd != STDIN_FILENO) {
88                         close(src_fd);
89                 }
90
91                 /* delete_path will be NULL if in test mode or from stdin */
92                 if (delete_path && (unlink(delete_path) == -1)) {
93                         bb_error_msg_and_die("Couldn't remove %s", delete_path);
94                 }
95
96                 free(uncompressed_file);
97         }
98
99         return status;
100 }