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