aboutsummaryrefslogtreecommitdiff
path: root/busybox/archival/uncompress.c
diff options
context:
space:
mode:
Diffstat (limited to 'busybox/archival/uncompress.c')
-rw-r--r--busybox/archival/uncompress.c115
1 files changed, 115 insertions, 0 deletions
diff --git a/busybox/archival/uncompress.c b/busybox/archival/uncompress.c
new file mode 100644
index 000000000..48b4e2cad
--- /dev/null
+++ b/busybox/archival/uncompress.c
@@ -0,0 +1,115 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Uncompress applet for busybox (c) 2002 Glenn McGrath
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 <stdlib.h>
21#include <string.h>
22#include <unistd.h>
23#include <getopt.h>
24#include <sys/types.h>
25#include <sys/stat.h>
26#include <fcntl.h>
27
28#include "libbb.h"
29#include "unarchive.h"
30
31#define GUNZIP_TO_STDOUT 1
32#define GUNZIP_FORCE 2
33
34extern int uncompress_main(int argc, char **argv)
35{
36 int status = EXIT_SUCCESS;
37 unsigned long flags;
38
39 flags = bb_getopt_ulflags(argc, argv, "cf");
40
41 while (optind < argc) {
42 const char *compressed_file = argv[optind++];
43 const char *delete_path = NULL;
44 char *uncompressed_file = NULL;
45 int src_fd;
46 int dst_fd;
47
48 if (strcmp(compressed_file, "-") == 0) {
49 src_fd = STDIN_FILENO;
50 flags |= GUNZIP_TO_STDOUT;
51 } else {
52 src_fd = bb_xopen(compressed_file, O_RDONLY);
53 }
54
55 /* Check that the input is sane. */
56 if (isatty(src_fd) && ((flags & GUNZIP_FORCE) == 0)) {
57 bb_error_msg_and_die
58 ("compressed data not read from terminal. Use -f to force it.");
59 }
60
61 /* Set output filename and number */
62 if (flags & GUNZIP_TO_STDOUT) {
63 dst_fd = STDOUT_FILENO;
64 } else {
65 struct stat stat_buf;
66 char *extension;
67
68 uncompressed_file = bb_xstrdup(compressed_file);
69
70 extension = strrchr(uncompressed_file, '.');
71 if (!extension || (strcmp(extension, ".Z") != 0)) {
72 bb_error_msg_and_die("Invalid extension");
73 }
74 *extension = '\0';
75
76 /* Open output file */
77 dst_fd = bb_xopen(uncompressed_file, O_WRONLY | O_CREAT);
78
79 /* Set permissions on the file */
80 stat(compressed_file, &stat_buf);
81 chmod(uncompressed_file, stat_buf.st_mode);
82
83 /* If unzip succeeds remove the old file */
84 delete_path = compressed_file;
85 }
86
87 /* do the decompression, and cleanup */
88 if ((bb_xread_char(src_fd) != 0x1f) || (bb_xread_char(src_fd) != 0x9d)) {
89 bb_error_msg_and_die("Invalid magic");
90 }
91
92 status = uncompress(src_fd, dst_fd);
93
94 if ((status != EXIT_SUCCESS) && (uncompressed_file)) {
95 /* Unzip failed, remove the uncomressed file instead of compressed file */
96 delete_path = uncompressed_file;
97 }
98
99 if (dst_fd != STDOUT_FILENO) {
100 close(dst_fd);
101 }
102 if (src_fd != STDIN_FILENO) {
103 close(src_fd);
104 }
105
106 /* delete_path will be NULL if in test mode or from stdin */
107 if (delete_path && (unlink(delete_path) == -1)) {
108 bb_error_msg_and_die("Couldn't remove %s", delete_path);
109 }
110
111 free(uncompressed_file);
112 }
113
114 return status;
115}