ifupdown: save some 100+ bytes of code in addstr()
[oweals/busybox.git] / archival / gzip.c
index ad2c24df1eab2e09bb99ee41afae534846ac8ef2..ef3724c3419b8eddfac624aa2a2af158006ef50e 100644 (file)
  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  */
 
+/* TODO: full support for -v for DESKTOP
+/usr/bin/gzip -v a bogus aa
+a:       85.1% -- replaced with a.gz
+gzip: bogus: No such file or directory
+aa:      85.1% -- replaced with aa.gz
+*/
+
 #define SMALL_MEM
 
 #include <stdlib.h>
@@ -123,8 +130,8 @@ typedef int file_t;         /* Do not use stdio */
 #define ASCII   1
 
 #ifndef WSIZE
-#  define WSIZE 0x8000 /* window size--must be a power of two, and */
-#endif                                                 /*  at least 32K for zip's deflate method */
+#  define WSIZE 0x8000  /* window size--must be a power of two, and */
+#endif                  /*  at least 32K for zip's deflate method */
 
 #define MIN_MATCH  3
 #define MAX_MATCH  258
@@ -1122,44 +1129,37 @@ typedef struct dirent dir_type;
 /* ======================================================================== */
 int gzip_main(int argc, char **argv)
 {
+       enum {
+               OPT_tostdout = 0x1,
+               OPT_force = 0x2,
+       };
+
+       unsigned opt;
        int result;
        int inFileNum;
        int outFileNum;
        struct stat statBuf;
        char *delFileName;
-       int tostdout = 0;
-       int force = 0;
-       int opt;
-
-       while ((opt = getopt(argc, argv, "cf123456789dq")) != -1) {
-               switch (opt) {
-               case 'c':
-                       tostdout = 1;
-                       break;
-               case 'f':
-                       force = 1;
-                       break;
-                       /* Ignore 1-9 (compression level) options */
-               case '1':
-               case '2':
-               case '3':
-               case '4':
-               case '5':
-               case '6':
-               case '7':
-               case '8':
-               case '9':
-                       break;
-               case 'q':
-                       break;
-#ifdef CONFIG_GUNZIP
-               case 'd':
-                       optind = 1;
-                       return gunzip_main(argc, argv);
-#endif
-               default:
-                       bb_show_usage();
-               }
+
+       opt = getopt32(argc, argv, "cf123456789qv" USE_GUNZIP("d"));
+       //if (opt & 0x1) // -c
+       //if (opt & 0x2) // -f
+       /* Ignore 1-9 (compression level) options */
+       //if (opt & 0x4) // -1
+       //if (opt & 0x8) // -2
+       //if (opt & 0x10) // -3
+       //if (opt & 0x20) // -4
+       //if (opt & 0x40) // -5
+       //if (opt & 0x80) // -6
+       //if (opt & 0x100) // -7
+       //if (opt & 0x200) // -8
+       //if (opt & 0x400) // -9
+       //if (opt & 0x800) // -q
+       //if (opt & 0x1000) // -v
+       if (ENABLE_GUNZIP && (opt & 0x2000)) { // -d
+               /* FIXME: getopt32 should not depend on optind */
+               optind = 1;
+               return gunzip_main(argc, argv);
        }
 
        foreground = signal(SIGINT, SIG_IGN) != SIG_IGN;
@@ -1206,12 +1206,12 @@ int gzip_main(int argc, char **argv)
                                inFileNum = STDIN_FILENO;
                                outFileNum = STDOUT_FILENO;
                        } else {
-                               inFileNum = xopen3(argv[i], O_RDONLY, 0);
+                               inFileNum = xopen(argv[i], O_RDONLY);
                                if (fstat(inFileNum, &statBuf) < 0)
                                        bb_perror_msg_and_die("%s", argv[i]);
                                time_stamp = statBuf.st_ctime;
 
-                               if (!tostdout) {
+                               if (!(opt & OPT_tostdout)) {
                                        path = xasprintf("%s.gz", argv[i]);
 
                                        /* Open output file */
@@ -1233,7 +1233,7 @@ int gzip_main(int argc, char **argv)
                                        outFileNum = STDOUT_FILENO;
                        }
 
-                       if (path == NULL && isatty(outFileNum) && force == 0) {
+                       if (path == NULL && isatty(outFileNum) && !(opt & OPT_force)) {
                                bb_error_msg
                                        ("compressed data not written to a terminal. Use -f to force compression.");
                                free(path);
@@ -2407,7 +2407,6 @@ static int zip(int in, int out)
 
        /* Write the header to the gzip file. See algorithm.doc for the format */
 
-
        method = DEFLATED;
        put_header_byte(GZIP_MAGIC[0]); /* magic header */
        put_header_byte(GZIP_MAGIC[1]);