Remove an erroneous comment.
[oweals/opkg-lede.git] / libbb / unzip.c
index 88d2a0aed8dc663d738047698e4b95c8bd2e6331..435effb3572555801a77f252ed7fd0a3ed539e61 100644 (file)
 #include <signal.h>
 #include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
+#include <errno.h>
 #include "libbb.h"
 
 static FILE *in_file, *out_file;
 
-/* these are freed by gz_close */
 static unsigned char *window;
 static unsigned long *crc_table = NULL;
 
 static unsigned long crc; /* shift register contents */
 
-/* Return codes from gzip */
-static const int ERROR = 1;
-
 /*
  * window size--must be a power of two, and
- *  at least 32K for zip's deflate method 
+ *  at least 32K for zip's deflate method
  */
 static const int WSIZE = 0x8000;
 
@@ -94,11 +92,11 @@ static const unsigned short mask_bits[] = {
 /* ========================================================================
  * Signal and error handler.
  */
+
 static void abort_gzip()
 {
        error_msg("gzip aborted\n");
-       exit(ERROR);
+       _exit(-1);
 }
 
 static void make_crc_table()
@@ -112,7 +110,7 @@ static void make_crc_table()
        static int p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
 
        /* initial shift register value */
-       crc = 0xffffffffL;      
+       crc = 0xffffffffL;
        crc_table = (unsigned long *) xmalloc(256 * sizeof(unsigned long));
 
        /* Make exclusive-or pattern from polynomial (0xedb88320) */
@@ -148,7 +146,16 @@ static void flush_window(void)
        }
 
        if (fwrite(window, 1, outcnt, out_file) != outcnt) {
-               error_msg_and_die("Couldnt write");
+               /*
+                * The Parent process may not be interested in all the data we have,
+                * in which case it will rudely close its end of the pipe and
+                * wait for us to exit.
+                */
+               if (errno == EPIPE)
+                       _exit(EXIT_SUCCESS);
+
+               error_msg("Couldnt write");
+               _exit(EXIT_FAILURE);
        }
        bytes_out += (unsigned long) outcnt;
        outcnt = 0;
@@ -157,7 +164,7 @@ static void flush_window(void)
 /*
  * Free the malloc'ed tables built by huft_build(), which makes a linked
  * list of the tables it made, with the links in a dummy first entry of
- * each table. 
+ * each table.
  * t: table to free
  */
 static int huft_free(huft_t *t)
@@ -188,7 +195,7 @@ static int huft_free(huft_t *t)
  * t:  result: starting table
  * m:  maximum lookup bits, returns actual
  */
-static int huft_build(unsigned int *b, const unsigned int n, const unsigned int s, 
+static int huft_build(unsigned int *b, const unsigned int n, const unsigned int s,
        const unsigned short *d, const unsigned short *e, huft_t **t, int *m)
 {
        unsigned a;             /* counter for codes of length k */
@@ -597,7 +604,7 @@ static int inflate_block(int *e)
                        bk = k_stored;
                        return 0;
                }
-       case 1: /* Inflate fixed 
+       case 1: /* Inflate fixed
                         * decompress an inflated type 1 (fixed Huffman codes) block.  We should
                         * either replace this with a custom decoder, or at least precompute the
                         * Huffman tables.
@@ -639,8 +646,11 @@ static int inflate_block(int *e)
                        }
 
                        /* decompress until an end-of-block code */
-                       if (inflate_codes(tl, td, bl, bd))
+                       if (inflate_codes(tl, td, bl, bd)) {
+                               huft_free(tl);
+                               huft_free(td);
                                return 1;
+                       }
 
                        /* free the decoding tables, return */
                        huft_free(tl);
@@ -816,8 +826,11 @@ static int inflate_block(int *e)
                        }
 
                        /* decompress until an end-of-block code */
-                       if (inflate_codes(tl, td, bl, bd))
+                       if (inflate_codes(tl, td, bl, bd)) {
+                               huft_free(tl);
+                               huft_free(td);
                                return 1;
+                       }
 
                        /* free the decoding tables, return */
                        huft_free(tl);
@@ -909,6 +922,8 @@ extern int unzip(FILE *l_in_file, FILE *l_out_file)
        }
 #endif
 
+       signal(SIGPIPE, SIG_IGN);
+
        /* Allocate all global buffers (for DYN_ALLOC option) */
        window = xmalloc((size_t)(((2L*WSIZE)+1L)*sizeof(unsigned char)));
        outcnt = 0;
@@ -956,8 +971,7 @@ extern int unzip(FILE *l_in_file, FILE *l_out_file)
        }
 
        if (method < 0) {
-               printf("it failed\n");
-               return(exit_code);              /* error message already emitted */
+               return(exit_code);
        }
 
        make_crc_table();
@@ -968,7 +982,7 @@ extern int unzip(FILE *l_in_file, FILE *l_out_file)
                int res = inflate();
 
                if (res == 3) {
-                       error_msg(memory_exhausted);
+                       perror_msg("inflate");
                        exit_code = 1;
                } else if (res != 0) {
                        error_msg("invalid compressed data--format violated");