Don't delete source file when decompressing to stdout
[oweals/busybox.git] / archival / gunzip.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Gzip implementation for busybox
4  *
5  * Based on GNU gzip v1.2.4 Copyright (C) 1992-1993 Jean-loup Gailly.
6  *
7  * Originally adjusted for busybox by Sven Rudolph <sr1@inf.tu-dresden.de>
8  * based on gzip sources
9  *
10  * Adjusted further by Erik Andersen <andersee@debian.org> to support files as
11  * well as stdin/stdout, and to generally behave itself wrt command line
12  * handling.
13  *
14  * General cleanup to better adhere to the style guide and make use of standard
15  * busybox functions by Glenn McGrath <bug1@optushome.com.au>
16  * 
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation; either version 2 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25  * General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  *
31  *
32  * gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface
33  * Copyright (C) 1992-1993 Jean-loup Gailly
34  * The unzip code was written and put in the public domain by Mark Adler.
35  * Portions of the lzw code are derived from the public domain 'compress'
36  * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
37  * Ken Turkowski, Dave Mack and Peter Jannesen.
38  *
39  * See the license_msg below and the file COPYING for the software license.
40  * See the file algorithm.doc for the compression algorithms and file formats.
41  */
42
43 #if 0
44 static char *license_msg[] = {
45         "   Copyright (C) 1992-1993 Jean-loup Gailly",
46         "   This program is free software; you can redistribute it and/or modify",
47         "   it under the terms of the GNU General Public License as published by",
48         "   the Free Software Foundation; either version 2, or (at your option)",
49         "   any later version.",
50         "",
51         "   This program is distributed in the hope that it will be useful,",
52         "   but WITHOUT ANY WARRANTY; without even the implied warranty of",
53         "   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the",
54         "   GNU General Public License for more details.",
55         "",
56         "   You should have received a copy of the GNU General Public License",
57         "   along with this program; if not, write to the Free Software",
58         "   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.",
59         0
60 };
61 #endif
62
63 #include <stdlib.h>
64 #include <string.h>
65 #include <unistd.h>
66 #include <getopt.h>
67 #include "busybox.h"
68
69 const int gunzip_to_stdout = 1;
70 const int gunzip_force = 2;
71 const int gunzip_test = 4;
72 const int gunzip_verbose = 8;
73
74 static int gunzip_file (const char *path, int flags)
75 {
76     FILE *in_file, *out_file;
77     struct stat stat_buf;
78     const char *delete_path;
79     char *out_path = NULL;
80
81     if (path == NULL || strcmp (path, "-") == 0) {
82         in_file = stdin;
83         flags |= gunzip_to_stdout;
84     } else {
85         if ((in_file = wfopen(path, "r")) == NULL)
86             return -1;
87
88         if (flags & gunzip_verbose) {
89             fprintf(stderr, "%s:\t", path);
90         }
91
92         /* set the buffer size */
93         setvbuf(in_file, NULL, _IOFBF, 0x8000);
94
95         /* Get the time stamp on the input file. */
96         if (stat(path, &stat_buf) < 0) {
97             error_msg_and_die("Couldn't stat file %s", path);
98         }
99     }
100
101     /* Check that the input is sane.  */
102     if (isatty(fileno(in_file)) && (flags & gunzip_force) == 0)
103         error_msg_and_die("compressed data not read from terminal.  Use -f to force it.");
104
105     /* Set output filename and number */
106     if (flags & gunzip_test) {
107         out_file = xfopen("/dev/null", "w"); /* why does test use filenum 2 ? */
108     } else if (flags & gunzip_to_stdout) {
109         out_file = stdout;
110     } else {
111         char *extension;
112         int length = strlen(path);
113
114         extension = strrchr(path, '.');
115         if (extension && strcmp(extension, ".gz") == 0) {
116             length -= 3;
117         } else if (extension && strcmp(extension, ".tgz") == 0) {
118             length -= 4;
119         } else {
120             error_msg_and_die("Invalid extension");
121         }
122         out_path = (char *) xcalloc(sizeof(char), length + 1);
123         strncpy(out_path, path, length);
124                         
125         /* Open output file */
126         out_file = xfopen(out_path, "w");
127
128         /* Set permissions on the file */
129         chmod(out_path, stat_buf.st_mode);
130     }
131
132     /* do the decompression, and cleanup */
133     if (unzip(in_file, out_file) == 0) {
134         /* Success, remove .gz file */
135         if ( !(flags & gunzip_to_stdout ))
136             delete_path = path;
137         if (flags & gunzip_verbose) {
138             fprintf(stderr, "OK\n");
139         }
140     } else {
141         /* remove failed attempt */
142         delete_path = out_path;
143     }
144
145     fclose(out_file);
146     fclose(in_file);
147
148     if (delete_path && !(flags & gunzip_test)) {
149         if (unlink(delete_path) < 0) {
150             error_msg_and_die("Couldn't remove %s", delete_path);
151         }
152     }
153
154     free(out_path);
155
156     return 0;
157 }
158
159 extern int gunzip_main(int argc, char **argv)
160 {
161         int flags = 0;
162         int i, opt;
163         int status = EXIT_SUCCESS;
164
165         /* if called as zcat */
166         if (strcmp(applet_name, "zcat") == 0)
167                 flags |= gunzip_to_stdout;
168
169         while ((opt = getopt(argc, argv, "ctfhdqv")) != -1) {
170                 switch (opt) {
171                 case 'c':
172                         flags |= gunzip_to_stdout;
173                         break;
174                 case 'f':
175                         flags |= gunzip_force;
176                         break;
177                 case 't':
178                         flags |= gunzip_test;
179                         break;
180                 case 'v':
181                         flags |= gunzip_verbose;
182                         break;
183                 case 'd': /* Used to convert gzip to gunzip. */
184                         break;
185                 case 'q':
186                         error_msg("-q option not supported, ignored");
187                         break;
188                 case 'h':
189                 default:
190                         show_usage(); /* exit's inside usage */
191                 }
192         }
193
194         if (optind == argc) {
195             if (gunzip_file (NULL, flags) < 0)
196                 status = EXIT_FAILURE;
197         } else
198             for (i = optind; i < argc; i++)
199                 if (gunzip_file (argv[i], flags) < 0)
200                     status = EXIT_FAILURE;
201
202         return status;
203 }