syslogd: fix "readpath bug" by using readlink instead
[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 "busybox.h"
9 #include "unarchive.h"
10
11 #define GUNZIP_TO_STDOUT        1
12 #define GUNZIP_FORCE    2
13
14 int uncompress_main(int argc, char **argv);
15 int uncompress_main(int argc, char **argv)
16 {
17         int status = EXIT_SUCCESS;
18         unsigned long flags;
19
20         flags = getopt32(argc, argv, "cf");
21
22         while (optind < argc) {
23                 char *compressed_file = argv[optind++];
24                 char *delete_path = NULL;
25                 char *uncompressed_file = NULL;
26                 int src_fd;
27                 int dst_fd;
28
29                 if (LONE_DASH(compressed_file)) {
30                         src_fd = STDIN_FILENO;
31                         flags |= GUNZIP_TO_STDOUT;
32                 } else {
33                         src_fd = xopen(compressed_file, O_RDONLY);
34                 }
35
36                 /* Check that the input is sane.  */
37                 if (isatty(src_fd) && ((flags & GUNZIP_FORCE) == 0)) {
38                         bb_error_msg_and_die
39                                 ("compressed data not read from terminal.  Use -f to force it.");
40                 }
41
42                 /* Set output filename and number */
43                 if (flags & GUNZIP_TO_STDOUT) {
44                         dst_fd = STDOUT_FILENO;
45                 } else {
46                         struct stat stat_buf;
47                         char *extension;
48
49                         uncompressed_file = xstrdup(compressed_file);
50
51                         extension = strrchr(uncompressed_file, '.');
52                         if (!extension || (strcmp(extension, ".Z") != 0)) {
53                                 bb_error_msg_and_die("invalid extension");
54                         }
55                         *extension = '\0';
56
57                         /* Open output file */
58                         xstat(compressed_file, &stat_buf);
59                         dst_fd = xopen3(uncompressed_file,
60                                         O_WRONLY | O_CREAT | O_TRUNC,
61                                         stat_buf.st_mode);
62
63                         /* If unzip succeeds remove the old file */
64                         delete_path = compressed_file;
65                 }
66
67                 /* do the decompression, and cleanup */
68                 if ((xread_char(src_fd) != 0x1f) || (xread_char(src_fd) != 0x9d)) {
69                         bb_error_msg_and_die("invalid magic");
70                 }
71
72                 status = uncompress(src_fd, dst_fd);
73
74                 if ((status != EXIT_SUCCESS) && (uncompressed_file)) {
75                         /* Unzip failed, remove the uncomressed file instead of compressed file */
76                         delete_path = uncompressed_file;
77                 }
78
79                 if (dst_fd != STDOUT_FILENO) {
80                         close(dst_fd);
81                 }
82                 if (src_fd != STDIN_FILENO) {
83                         close(src_fd);
84                 }
85
86                 /* delete_path will be NULL if in test mode or from stdin */
87                 if (delete_path && (unlink(delete_path) == -1)) {
88                         bb_error_msg_and_die("cannot remove %s", delete_path);
89                 }
90
91                 free(uncompressed_file);
92         }
93
94         return status;
95 }