Credited Christophe Boyaniqu for interactive patch to rm.
[oweals/busybox.git] / gzip.c
diff --git a/gzip.c b/gzip.c
index d8c22a924f37cabd159c9ed326df2e4d0c673299..6c058d2b62a7768207133a6808c4db95da54fc6e 100644 (file)
--- a/gzip.c
+++ b/gzip.c
  *
  */
 
-#include "busybox.h"
-#define BB_DECLARE_EXTERN
-#define bb_need_memory_exhausted
-#include "messages.c"
-
 /* These defines are very important for BusyBox.  Without these,
  * huge chunks of ram are pre-allocated making the BusyBox bss 
  * size Freaking Huge(tm), which is a bad thing.*/
 /* I don't like nested includes, but the string and io functions are used
  * too often
  */
+#include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
+#include <unistd.h>
+#include "busybox.h"
+#define BB_DECLARE_EXTERN
+#define bb_need_memory_exhausted
+#include "messages.c"
+
 #define memzero(s, n)     memset ((void *)(s), 0, (n))
 
 #ifndef RETSIGTYPE
@@ -114,7 +116,7 @@ extern int method;                          /* compression method */
 #  define DECLARE(type, array, size)  type * array
 #  define ALLOC(type, array, size) { \
       array = (type*)calloc((size_t)(((size)+1L)/2), 2*sizeof(type)); \
-      if (array == NULL) errorMsg(memory_exhausted); \
+      if (array == NULL) error_msg(memory_exhausted); \
    }
 #  define FREE(array) {if (array != NULL) free(array), array=NULL;}
 #else
@@ -251,7 +253,7 @@ extern int save_orig_name;          /* set if original name must be saved */
 
 /* Diagnostic functions */
 #ifdef DEBUG
-#  define Assert(cond,msg) {if(!(cond)) errorMsg(msg);}
+#  define Assert(cond,msg) {if(!(cond)) error_msg(msg);}
 #  define Trace(x) fprintf x
 #  define Tracev(x) {if (verbose) fprintf x ;}
 #  define Tracevv(x) {if (verbose>1) fprintf x ;}
@@ -1381,7 +1383,7 @@ int length;
                           (char *) window + start, length) != EQUAL) {
                fprintf(stderr,
                                " start %d, match %d, length %d\n", start, match, length);
-               errorMsg("invalid match\n");
+               error_msg("invalid match");
        }
        if (verbose > 1) {
                fprintf(stderr, "\\[%d,%d]", start - match, length);
@@ -1627,12 +1629,6 @@ ulg deflate()
 #  include <unistd.h>
 #endif
 
-#if defined(STDC_HEADERS) || !defined(NO_STDLIB_H)
-#  include <stdlib.h>
-#else
-extern int errno;
-#endif
-
 #if defined(DIRENT)
 #  include <dirent.h>
 typedef struct dirent dir_type;
@@ -1808,8 +1804,10 @@ int gzip_main(int argc, char **argv)
                        case '1': case '2': case '3': case '4': case '5':
                        case '6': case '7': case '8': case '9':
                                break;
+                       case 'd':
+                               exit(gunzip_main(argc, argv));
                        default:
-                               usage(gzip_usage);
+                               show_usage();
                        }
                }
        }
@@ -1819,9 +1817,9 @@ int gzip_main(int argc, char **argv)
        }
 
        if (isatty(fileno(stdin)) && fromstdin==1 && force==0)
-               fatalError( "data not read from terminal. Use -f to force it.\n");
+               error_msg_and_die( "data not read from terminal. Use -f to force it.");
        if (isatty(fileno(stdout)) && tostdout==1 && force==0)
-               fatalError( "data not written to terminal. Use -f to force it.\n");
+               error_msg_and_die( "data not written to terminal. Use -f to force it.");
 
        foreground = signal(SIGINT, SIG_IGN) != SIG_IGN;
        if (foreground) {
@@ -1862,21 +1860,16 @@ int gzip_main(int argc, char **argv)
        } else {
                /* Open up the input file */
                if (argc <= 0)
-                       usage(gzip_usage);
+                       show_usage();
                strncpy(ifname, *argv, MAX_PATH_LEN);
 
-               /* Open input fille */
+               /* Open input file */
                inFileNum = open(ifname, O_RDONLY);
-               if (inFileNum < 0) {
-                       perror(ifname);
-                       exit(WARNING);
-               }
+               if (inFileNum < 0)
+                       perror_msg_and_die("%s", ifname);
                /* Get the time stamp on the input file. */
-               result = stat(ifname, &statBuf);
-               if (result < 0) {
-                       perror(ifname);
-                       exit(WARNING);
-               }
+               if (stat(ifname, &statBuf) < 0)
+                       perror_msg_and_die("%s", ifname);
                time_stamp = statBuf.st_ctime;
                ifile_size = statBuf.st_size;
        }
@@ -1907,10 +1900,8 @@ int gzip_main(int argc, char **argv)
 #else
                outFileNum = open(ofname, O_RDWR | O_CREAT | O_EXCL);
 #endif
-               if (outFileNum < 0) {
-                       perror(ofname);
-                       exit(WARNING);
-               }
+               if (outFileNum < 0)
+                       perror_msg_and_die("%s", ofname);
                SET_BINARY_MODE(outFileNum);
                /* Set permissions on the file */
                fchmod(outFileNum, statBuf.st_mode);
@@ -1928,10 +1919,8 @@ int gzip_main(int argc, char **argv)
                else
                        delFileName = ofname;
 
-               if (unlink(delFileName) < 0) {
-                       perror(delFileName);
-                       exit(EXIT_FAILURE);
-               }
+               if (unlink(delFileName) < 0)
+                       perror_msg_and_die("%s", delFileName);
        }
 
        return(exit_code);
@@ -2900,7 +2889,7 @@ int eof;                                          /* true if this is the last block for a file */
 #endif
                /* Since LIT_BUFSIZE <= 2*WSIZE, the input data must be there: */
                if (buf == (char *) 0)
-                       errorMsg("block vanished\n");
+                       error_msg("block vanished");
 
                copy_block(buf, (unsigned) stored_len, 0);      /* without header */
                compressed_len = stored_len << 3;
@@ -3083,7 +3072,7 @@ local void set_file_type()
                bin_freq += dyn_ltree[n++].Freq;
        *file_type = bin_freq > (ascii_freq >> 2) ? BINARY : ASCII;
        if (*file_type == BINARY && translate_eol) {
-               errorMsg("-l used on binary file\n");
+               error_msg("-l used on binary file");
        }
 }
 
@@ -3104,12 +3093,6 @@ local void set_file_type()
 #  include <fcntl.h>
 #endif
 
-#if defined(STDC_HEADERS) || !defined(NO_STDLIB_H)
-#  include <stdlib.h>
-#else
-extern int errno;
-#endif
-
 /* ===========================================================================
  * Copy input to output unchanged: zcat == cat with --force.
  * IN assertion: insize bytes have already been read in inbuf.
@@ -3239,13 +3222,13 @@ char *env;                                              /* name of environment variable */
        nargv = (char **) calloc(*argcp + 1, sizeof(char *));
 
        if (nargv == NULL)
-               errorMsg(memory_exhausted);
+               error_msg(memory_exhausted);
        oargv = *argvp;
        *argvp = nargv;
 
        /* Copy the program name first */
        if (oargc-- < 0)
-               errorMsg("argc<=0\n");
+               error_msg("argc<=0");
        *(nargv++) = *(oargv++);
 
        /* Then copy the environment args */