Update the lash shell (hopefully the last time...) so things like
[oweals/busybox.git] / gunzip.c
index 7a360a6eaab5c738fb13a0c155a71c1e3f1bce61..8100003ec29c03de440d96f5d984f0445d959dcd 100644 (file)
--- a/gunzip.c
+++ b/gunzip.c
@@ -57,7 +57,6 @@ static char *license_msg[] = {
 };
 #endif
 
-#include "busybox.h"
 #include <getopt.h>
 #include <ctype.h>
 #include <sys/types.h>
@@ -71,49 +70,50 @@ static char *license_msg[] = {
 #include <stdlib.h>
 #include <time.h>
 #include <dirent.h>
+#include "busybox.h"
 #define BB_DECLARE_EXTERN
 #define bb_need_memory_exhausted
 #define bb_need_name_too_long
 #include "messages.c"
 
-#define RECORD_IO 0
+static const int RECORD_IO = 0;
 
 /* Return codes from gzip */
-#define OK      0
-#define ERROR   1
-#define WARNING 2
+static const int OK = 0;
+static const int ERROR = 1;
+static const int WARNING = 2;
 
-#define DEFLATED     8
-#define INBUFSIZ     0x2000    /* input buffer size */
-#define INBUF_EXTRA  64                /* required by unlzw() */
-#define OUTBUFSIZ    8192      /* output buffer size */
-#define OUTBUF_EXTRA 2048      /* required by unlzw() */
-#define DIST_BUFSIZE 0x2000    /* buffer for distances, see trees.c */
+static const int DEFLATED = 8;
+static const int INBUFSIZ = 0x2000;    /* input buffer size */
+static const int INBUF_EXTRA = 64;             /* required by unlzw() */
+static const int OUTBUFSIZ = 8192;     /* output buffer size */
+static const int OUTBUF_EXTRA = 2048;  /* required by unlzw() */
+static const int DIST_BUFSIZE = 0x2000;        /* buffer for distances, see trees.c */
 
 #define        GZIP_MAGIC  "\037\213"  /* Magic header for gzip files, 1F 8B */
 
 /* gzip flag byte */
-#define EXTRA_FIELD  0x04      /* bit 2 set: extra field present */
-#define ORIG_NAME    0x08      /* bit 3 set: original file name present */
-#define COMMENT      0x10      /* bit 4 set: file comment present */
-#define WSIZE 0x8000           /* window size--must be a power of two, and */
+static const int EXTRA_FIELD = 0x04;   /* bit 2 set: extra field present */
+static const int ORIG_NAME = 0x08;     /* bit 3 set: original file name present */
+static const int COMMENT = 0x10;       /* bit 4 set: file comment present */
+static const int WSIZE = 0x8000;               /* window size--must be a power of two, and */
                                /*  at least 32K for zip's deflate method */
 
 /* If BMAX needs to be larger than 16, then h and x[] should be ulg. */
-#define BMAX 16                /* maximum bit length of any code (16 for explode) */
-#define N_MAX 288              /* maximum number of codes in any set */
+static const int BMAX = 16;            /* maximum bit length of any code (16 for explode) */
+static const int N_MAX = 288;          /* maximum number of codes in any set */
 
 /* PKZIP header definitions */
-#define LOCSIG 0x04034b50L     /* four-byte lead-in (lsb first) */
-#define LOCCRC 14              /* offset of crc */
-#define LOCLEN 22              /* offset of uncompressed length */
-#define EXTHDR 16              /* size of extended local header, inc sig */
+static const int LOCSIG = 0x04034b50L; /* four-byte lead-in (lsb first) */
+static const int LOCCRC = 14;          /* offset of crc */
+static const int LOCLEN = 22;          /* offset of uncompressed length */
+static const int EXTHDR = 16;          /* size of extended local header, inc sig */
 
-#define BITS 16
+static const int BITS = 16;
 
 /* 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 ;}
@@ -132,7 +132,7 @@ static char *license_msg[] = {
 #  ifdef BUFSIZ
 #    define MAX_PATH_LEN   BUFSIZ
 #  else
-#    define MAX_PATH_LEN   1024
+static const int MAX_PATH_LEN = 1024;
 #  endif
 #endif
 
@@ -155,32 +155,28 @@ typedef unsigned short ush;
 typedef unsigned long ulg;
 typedef int file_t;                            /* Do not use stdio */
 
-uch *inbuf;
-uch *outbuf;
-ush *d_buf;
-uch *window;
-ush *tab_prefix0;
-ush *tab_prefix1;
+static uch *inbuf;
+static uch *outbuf;
+static ush *d_buf;
+static uch *window;
+static ush *tab_prefix0;
+static ush *tab_prefix1;
 
 /* local variables */
-int test_mode = 0;     /* check file integrity option */
-int foreground;                /* set if program run in foreground */
-int maxbits = BITS;    /* max bits per code for LZW */
-int method = DEFLATED; /* compression method */
-int exit_code = OK;    /* program exit code */
-int last_member;       /* set for .zip and .Z files */
-int part_nb;           /* number of parts in .gz file */
-long ifile_size;       /* input file size, -1 for devices (debug only) */
-long bytes_in;         /* number of input bytes */
-long bytes_out;                /* number of output bytes */
-long total_in = 0;     /* input bytes for all files */
-long total_out = 0;    /* output bytes for all files */
-struct stat istat;     /* status for input file */
-int ifd;               /* input file descriptor */
-int ofd;               /* output file descriptor */
-unsigned insize;       /* valid bytes in inbuf */
-unsigned inptr;                /* index of next byte to be processed in inbuf */
-unsigned outcnt;       /* bytes in output buffer */
+static int test_mode = 0;      /* check file integrity option */
+static int foreground;         /* set if program run in foreground */
+static int method;     /* compression method */
+static int exit_code;  /* program exit code */
+static int last_member;        /* set for .zip and .Z files */
+static int part_nb;            /* number of parts in .gz file */
+static long ifile_size;        /* input file size, -1 for devices (debug only) */
+static long bytes_in;          /* number of input bytes */
+static long bytes_out;         /* number of output bytes */
+static int ifd;                /* input file descriptor */
+static int ofd;                /* output file descriptor */
+static unsigned insize;        /* valid bytes in inbuf */
+static unsigned inptr;         /* index of next byte to be processed in inbuf */
+static unsigned outcnt;        /* bytes in output buffer */
 
 unsigned hufts;                /* track memory usage */
 ulg bb;                        /* bit buffer */
@@ -301,7 +297,7 @@ int in;                                     /* input file descriptor */
 
                method = (int) get_byte();
                if (method != DEFLATED) {
-                       errorMsg("unknown method %d -- get newer version of gzip\n", method);
+                       error_msg("unknown method %d -- get newer version of gzip", method);
                        exit_code = ERROR;
                        return -1;
                }
@@ -1088,7 +1084,7 @@ int inflate()
  *   the compressed data, from offsets inptr to insize-1 included.
  *   The magic header has already been checked. The output buffer is cleared.
  */
-int unzip(in, out)
+extern int unzip(in, out)
 int in, out;                                   /* input and output file descriptors */
 {
        int ext_header = 0;                             /* set if extended local header */
@@ -1118,13 +1114,13 @@ int in, out;                                    /* input and output file descriptors */
                int res = inflate();
 
                if (res == 3) {
-                       errorMsg(memory_exhausted);
+                       error_msg(memory_exhausted);
                } else if (res != 0) {
-                       errorMsg("invalid compressed data--format violated");
+                       error_msg("invalid compressed data--format violated");
                }
 
        } else {
-               errorMsg("internal error, invalid method");
+               error_msg("internal error, invalid method");
        }
 
        /* Get the crc and original length */
@@ -1153,15 +1149,15 @@ int in, out;                                    /* input and output file descriptors */
 
        /* Validate decompression */
        if (orig_crc != updcrc(outbuf, 0)) {
-               errorMsg("invalid compressed data--crc error");
+               error_msg("invalid compressed data--crc error");
        }
        if (orig_len != (ulg) bytes_out) {
-               errorMsg("invalid compressed data--length error");
+               error_msg("invalid compressed data--length error");
        }
 
        /* Check if there are more entries in a pkzip file */
        if (pkzip && inptr + 4 < insize && LG(inbuf + inptr) == LOCSIG) {
-               fprintf(stderr, "has more than one entry--rest ignored\n");
+               error_msg("has more than one entry--rest ignored");
                if (exit_code == OK)
                        exit_code = WARNING;
        }
@@ -1181,24 +1177,42 @@ void clear_bufs(void)
 }
 
 /* ===========================================================================
- * Write the output buffer outbuf[0..outcnt-1] and update bytes_out.
- * (used for the compressed data only)
+ * Initialize gunzip buffers and signals
  */
-void flush_outbuf()
+extern int gunzip_init()
 {
-       if (outcnt == 0)
-               return;
+       foreground = signal(SIGINT, SIG_IGN) != SIG_IGN;
+       if (foreground) {
+               (void) signal(SIGINT, (sig_type) abort_gzip);
+       }
+#ifdef SIGTERM
+       if (signal(SIGTERM, SIG_IGN) != SIG_IGN) {
+               (void) signal(SIGTERM, (sig_type) abort_gzip);
+       }
+#endif
+#ifdef SIGHUP
+       if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
+               (void) signal(SIGHUP, (sig_type) abort_gzip);
+       }
+#endif
 
-       if (!test_mode)
-               write_buf(ofd, (char *) outbuf, outcnt);
-       bytes_out += (ulg) outcnt;
-       outcnt = 0;
+       /* Allocate all global buffers (for DYN_ALLOC option) */
+       inbuf = xmalloc((size_t)((INBUFSIZ+INBUF_EXTRA+1L)*sizeof(uch)));
+       outbuf = xmalloc((size_t)((OUTBUFSIZ+OUTBUF_EXTRA+1L)*sizeof(uch)));
+       d_buf = xmalloc((size_t)((DIST_BUFSIZE+1L)*sizeof(ush)));
+       window = xmalloc((size_t)(((2L*WSIZE)+1L)*sizeof(uch)));
+       tab_prefix0 = xmalloc((size_t)(((1L<<(BITS-1))+1L)*sizeof(ush)));       
+       tab_prefix1 = xmalloc((size_t)(((1L<<(BITS-1))+1L)*sizeof(ush)));
+       
+       clear_bufs();                   /* clear input and output buffers */
+       part_nb = 0;
+       return(EXIT_SUCCESS);   
 }
 
+
 /* ======================================================================== */
 int gunzip_main(int argc, char **argv)
 {
-       int file_count;                         /* number of files to precess */
        int tostdout = 0;
        int fromstdin = 0;
        int result;
@@ -1208,8 +1222,11 @@ int gunzip_main(int argc, char **argv)
        int force = 0;
        struct stat statBuf;
        char *delFileName;
-       char ifname[MAX_PATH_LEN + 1];  /* input file name */
-       char ofname[MAX_PATH_LEN + 1];  /* output file name */
+       RESERVE_BB_BUFFER(ifname, MAX_PATH_LEN+1);  /* input file name */
+       RESERVE_BB_BUFFER(ofname, MAX_PATH_LEN+1);  /* output file name */
+
+       method = DEFLATED;      /* default compression method */
+       exit_code = OK; /* let's go out on a limb and assume everything will run fine (wink wink) */
 
        if (strcmp(applet_name, "zcat") == 0) {
                force = 1;
@@ -1233,7 +1250,7 @@ int gunzip_main(int argc, char **argv)
                                force = 1;
                                break;
                        default:
-                               usage(gunzip_usage);
+                               show_usage();
                        }
                }
        }
@@ -1244,35 +1261,11 @@ int gunzip_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) {
-               (void) signal(SIGINT, (sig_type) abort_gzip);
-       }
-#ifdef SIGTERM
-       if (signal(SIGTERM, SIG_IGN) != SIG_IGN) {
-               (void) signal(SIGTERM, (sig_type) abort_gzip);
-       }
-#endif
-#ifdef SIGHUP
-       if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
-               (void) signal(SIGHUP, (sig_type) abort_gzip);
-       }
-#endif
-
-       file_count = argc - optind;
-
-       /* Allocate all global buffers (for DYN_ALLOC option) */
-       inbuf = xmalloc((size_t)((INBUFSIZ+INBUF_EXTRA+1L)*sizeof(uch)));
-       outbuf = xmalloc((size_t)((OUTBUFSIZ+OUTBUF_EXTRA+1L)*sizeof(uch)));
-       d_buf = xmalloc((size_t)((DIST_BUFSIZE+1L)*sizeof(ush)));
-       window = xmalloc((size_t)(((2L*WSIZE)+1L)*sizeof(uch)));
-       tab_prefix0 = xmalloc((size_t)(((1L<<(BITS-1))+1L)*sizeof(ush)));       
-       tab_prefix1 = xmalloc((size_t)(((1L<<(BITS-1))+1L)*sizeof(ush)));       
+       gunzip_init();
 
        if (fromstdin == 1) {
                strcpy(ofname, "stdin");
@@ -1282,9 +1275,9 @@ int gunzip_main(int argc, char **argv)
        } else {
                /* Open up the input file */
                if (argc <= 0)
-                       usage(gunzip_usage);
+                       show_usage();
                if (strlen(*argv) > MAX_PATH_LEN) {
-                       errorMsg(name_too_long);
+                       error_msg(name_too_long);
                        exit(WARNING);
                }
                strcpy(ifname, *argv);
@@ -1309,9 +1302,6 @@ int gunzip_main(int argc, char **argv)
                strcpy(ofname, "stdout");
                outFileNum = fileno(stdout);
 
-               clear_bufs();                   /* clear input and output buffers */
-               part_nb = 0;
-
                /* Actually do the compression/decompression. */
                unzip(inFileNum, outFileNum);
 
@@ -1323,7 +1313,7 @@ int gunzip_main(int argc, char **argv)
 
                /* And get to work */
                if (strlen(ifname) > MAX_PATH_LEN - 4) {
-                       errorMsg(name_too_long);
+                       error_msg(name_too_long);
                        exit(WARNING);
                }
                strcpy(ofname, ifname);
@@ -1353,9 +1343,6 @@ int gunzip_main(int argc, char **argv)
                /* Set permissions on the file */
                fchmod(outFileNum, statBuf.st_mode);
 
-               clear_bufs();                   /* clear input and output buffers */
-               part_nb = 0;
-
                /* Actually do the compression/decompression. */
                result = unzip(inFileNum, outFileNum);
 
@@ -1369,7 +1356,7 @@ int gunzip_main(int argc, char **argv)
 
                if (delInputFile == 1 && unlink(delFileName) < 0) {
                        perror(delFileName);
-                       exit(FALSE);
+                       return EXIT_FAILURE;
                }
        }
        return(exit_code);