- whitespace cleanup and add a possible shrinkage suggestion
[oweals/busybox.git] / archival / bunzip2.c
index 714dac07744bf892096f9cd9caa9ddc28b0fc3cb..1deac7b53586d14ead0103c8136334a9de670def 100644 (file)
@@ -6,60 +6,60 @@
  *  Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  */
 
-#include <fcntl.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);
 int bunzip2_main(int argc, char **argv)
 {
+       USE_DESKTOP(long long) int status;
        char *filename;
-       unsigned long opt;
-       int status, src_fd, dst_fd;
+       unsigned opt;
+       int src_fd, dst_fd;
 
-       opt = bb_getopt_ulflags(argc, argv, "cf");
+       opt = getopt32(argc, argv, "cf");
 
        /* Set input filename and number */
        filename = argv[optind];
-       if ((filename) && (filename[0] != '-') && (filename[1] != '\0')) {
+       if (filename && NOT_LONE_DASH(filename)) {
                /* Open input file */
-               src_fd = bb_xopen(filename, O_RDONLY);
+               src_fd = xopen(filename, O_RDONLY);
        } else {
                src_fd = STDIN_FILENO;
                filename = 0;
        }
 
        /* if called as bzcat force the stdout flag */
-       if ((opt & BUNZIP2_OPT_STDOUT) || bb_applet_name[2] == 'c')
+       if ((opt & BUNZIP2_OPT_STDOUT) || applet_name[2] == 'c')
                filename = 0;
 
        /* 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.");
+               bb_error_msg_and_die("compressed data not read from terminal, "
+                               "use -f to force it");
        }
 
        if (filename) {
                struct stat stat_buf;
-               char *extension=filename+strlen(filename)-4;
-               if (strcmp(extension, ".bz2") != 0) {
-                       bb_error_msg_and_die("Invalid extension");
+               /* 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");
                }
                xstat(filename, &stat_buf);
-               *extension=0;
-               dst_fd = bb_xopen3(filename, O_WRONLY | O_CREAT, stat_buf.st_mode);
+               *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) filename[strlen(filename)]='.';
+       if (filename) {
+               if (status >= 0) filename[strlen(filename)] = '.';
                if (unlink(filename) < 0) {
-                       bb_error_msg_and_die("Couldn't remove %s", filename);
+                       bb_error_msg_and_die("cannot remove %s", filename);
                }
        }