ifupdown: save some 100+ bytes of code in addstr()
[oweals/busybox.git] / archival / bunzip2.c
index eb5238cd4378ac2a807d607dd8e87ca7c473fcb3..757001fe463586ec5b8411d9761836b897c5bc98 100644 (file)
+/* vi: set sw=4 ts=4: */
 /*
- *  Modified for busybox by Glenn McGrath <bug1@optushome.com.au>
+ *  Modified for busybox by Glenn McGrath <bug1@iinet.net.au>
  *  Added support output to stdout by Thomas Lundquist <thomasez@zelow.no>
  *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
- *  (at your option) any later version.
- *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *  Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  */
 
-#include <fcntl.h>
-#include <getopt.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-
 #include "busybox.h"
 #include "unarchive.h"
 
+#define BUNZIP2_OPT_STDOUT     1
+#define BUNZIP2_OPT_FORCE      2
+
 int bunzip2_main(int argc, char **argv)
 {
-       const int bunzip_to_stdout = 1;
-       const int bunzip_force = 2;
-       int flags = 0;
-       int opt = 0;
-       int status;
-
-       int src_fd;
-       int dst_fd;
-       char *save_name = NULL;
-       char *delete_name = NULL;
+       USE_DESKTOP(long long) int status;
+       char *filename;
+       unsigned opt;
+       int src_fd, dst_fd;
 
-       /* if called as bzcat */
-       if (strcmp(bb_applet_name, "bzcat") == 0)
-               flags |= bunzip_to_stdout;
-
-       while ((opt = getopt(argc, argv, "cfh")) != -1) {
-               switch (opt) {
-               case 'c':
-                       flags |= bunzip_to_stdout;
-                       break;
-               case 'f':
-                       flags |= bunzip_force;
-                       break;
-               case 'h':
-               default:
-                       bb_show_usage(); /* exit's inside usage */
-               }
-       }
+       opt = getopt32(argc, argv, "cf");
 
        /* Set input filename and number */
-       if (argv[optind] == NULL || strcmp(argv[optind], "-") == 0) {
-               flags |= bunzip_to_stdout;
-               src_fd = fileno(stdin);
-       } else {
+       filename = argv[optind];
+       if ((filename) && (filename[0] != '-') && (filename[1] != '\0')) {
                /* Open input file */
-               src_fd = bb_xopen(argv[optind], O_RDONLY);
-
-               save_name = bb_xstrdup(argv[optind]);
-               if (strcmp(save_name + strlen(save_name) - 4, ".bz2") != 0)
-                       bb_error_msg_and_die("Invalid extension");
-               save_name[strlen(save_name) - 4] = '\0';
+               src_fd = xopen(filename, O_RDONLY);
+       } else {
+               src_fd = STDIN_FILENO;
+               filename = 0;
        }
 
-       /* Check that the input is sane.  */
-       if (isatty(src_fd) && (flags & bunzip_force) == 0) {
-               bb_error_msg_and_die("compressed data not read from terminal.  Use -f to force it.");
-       }
+       /* if called as bzcat force the stdout flag */
+       if ((opt & BUNZIP2_OPT_STDOUT) || applet_name[2] == 'c')
+               filename = 0;
 
-       if (flags & bunzip_to_stdout) {
-               dst_fd = fileno(stdout);
-       } else {
-               dst_fd = bb_xopen(save_name, O_WRONLY | O_CREAT);
+       /* Check that the input is sane.  */
+       if (isatty(src_fd) && (opt & BUNZIP2_OPT_FORCE) == 0) {
+               bb_error_msg_and_die("compressed data not read from terminal, "
+                               "use -f to force it");
        }
 
-       if (uncompressStream(src_fd, dst_fd)) {
-               if (!(flags & bunzip_to_stdout)) {
-                       delete_name = argv[optind];
+       if (filename) {
+               struct stat stat_buf;
+               /* extension = filename+strlen(filename)-4 is buggy:
+                * strlen may be less than 4 */
+               char *extension = strrchr(filename, '.');
+               if (!extension || strcmp(extension, ".bz2") != 0) {
+                       bb_error_msg_and_die("invalid extension");
                }
-               status = EXIT_SUCCESS;
-       } else {
-               if (!(flags & bunzip_to_stdout)) {
-                       delete_name = save_name;
+               xstat(filename, &stat_buf);
+               *extension = '\0';
+               dst_fd = xopen3(filename, O_WRONLY | O_CREAT | O_TRUNC,
+                               stat_buf.st_mode);
+       } else dst_fd = STDOUT_FILENO;
+       status = uncompressStream(src_fd, dst_fd);
+       if (filename) {
+               if (status >= 0) filename[strlen(filename)] = '.';
+               if (unlink(filename) < 0) {
+                       bb_error_msg_and_die("cannot remove %s", filename);
                }
-               status = EXIT_FAILURE;
-       }
-
-       if ((delete_name) && (unlink(delete_name) < 0)) {
-               bb_error_msg_and_die("Couldn't remove %s", delete_name);
        }
 
        return status;