Patch from Matt Kraai to enable proxy support.
[oweals/busybox.git] / gzip.c
diff --git a/gzip.c b/gzip.c
index cc6868b535056642d4509e8a609899a73e5c0e75..49c429b3af84a7da4eecd908cdcec125a59b840f 100644 (file)
--- a/gzip.c
+++ b/gzip.c
  *
  */
 
-#include "internal.h"
+#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 
 #define SMALL_MEM
 #define DYN_ALLOC
 
-
-static const char gzip_usage[] =
-       "gzip [OPTION]... FILE\n\n"
-       "Compress FILE with maximum compression.\n"
-       "When FILE is '-', reads standard input.  Implies -c.\n\n"
-
-       "Options:\n"
-       "\t-c\tWrite output to standard output instead of FILE.gz\n";
-
-
 /* I don't like nested includes, but the string and io functions are used
  * too often
  */
@@ -121,7 +114,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("insufficient memory"); \
+      if (array == NULL) error_msg(memory_exhausted); \
    }
 #  define FREE(array) {if (array != NULL) free(array), array=NULL;}
 #else
@@ -258,7 +251,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 ;}
@@ -276,8 +269,6 @@ extern int save_orig_name;          /* set if original name must be saved */
 #define WARN(msg) {if (!quiet) fprintf msg ; \
                   if (exit_code == OK) exit_code = WARNING;}
 
-#define do_exit(c) exit(c)
-
 
        /* in zip.c: */
 extern int zip (int in, int out);
@@ -323,8 +314,8 @@ extern void flush_window (void);
 extern void write_buf (int fd, void * buf, unsigned cnt);
 extern char *strlwr (char *s);
 extern char *add_envopt (int *argcp, char ***argvp, char *env);
-extern void read_error (void);
-extern void write_error (void);
+extern void read_error_msg (void);
+extern void write_error_msg (void);
 extern void display_ratio (long num, long den, FILE * file);
 
        /* in inflate.c */
@@ -1390,7 +1381,7 @@ int length;
                           (char *) window + start, length) != EQUAL) {
                fprintf(stderr,
                                " start %d, match %d, length %d\n", start, match, length);
-               errorMsg("invalid match");
+               error_msg("invalid match\n");
        }
        if (verbose > 1) {
                fprintf(stderr, "\\[%d,%d]", start - match, length);
@@ -1618,7 +1609,6 @@ ulg deflate()
 #include <ctype.h>
 #include <sys/types.h>
 #include <signal.h>
-#include <sys/stat.h>
 #include <errno.h>
 
                /* configuration */
@@ -1764,35 +1754,23 @@ DECLARE(ush, tab_prefix1, 1L << (BITS - 1));
 
                /* local variables */
 
-int ascii = 0;                                 /* convert end-of-lines to local OS conventions */
-int decompress = 0;                            /* decompress (-d) */
-int no_name = -1;                              /* don't save or restore the original file name */
-int no_time = -1;                              /* don't save or restore the original file time */
-int foreground;                                        /* set if program run in foreground */
-char *progname;                                        /* program name */
+static int foreground;                                 /* set if program run in foreground */
 static int method = DEFLATED;  /* compression method */
 static int exit_code = OK;             /* program exit code */
-int save_orig_name;                            /* set if original name must be saved */
-int last_member;                               /* set for .zip and .Z files */
-int part_nb;                                   /* number of parts in .gz file */
-long time_stamp;                               /* original time stamp (modification time) */
-long ifile_size;                               /* input file size, -1 for devices (debug only) */
-char *env;                                             /* contents of GZIP env variable */
-char **args = NULL;                            /* argv pointer if GZIP env variable defined */
-char z_suffix[MAX_SUFFIX + 1]; /* default suffix (can be set with --suffix) */
-int z_len;                                             /* strlen(z_suffix) */
-
-long bytes_in;                                 /* number of input bytes */
-long bytes_out;                                        /* number of output bytes */
-char ifname[MAX_PATH_LEN];             /* input file name */
-char ofname[MAX_PATH_LEN];             /* output file name */
-int remove_ofname = 0;                 /* remove output file on error */
-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 part_nb;                                    /* number of parts in .gz file */
+static long time_stamp;                                /* original time stamp (modification time) */
+static long ifile_size;                                /* input file size, -1 for devices (debug only) */
+static char z_suffix[MAX_SUFFIX + 1];  /* default suffix (can be set with --suffix) */
+static int z_len;                                              /* strlen(z_suffix) */
+
+static long bytes_in;                                  /* number of input bytes */
+static long bytes_out;                                 /* number of output bytes */
+static char ifname[MAX_PATH_LEN];              /* input file name */
+static char ofname[MAX_PATH_LEN];              /* output file name */
+static int ifd;                                                /* input file descriptor */
+static int ofd;                                                /* output file descriptor */
+static unsigned insize;                                /* valid bytes in inbuf */
+static unsigned outcnt;                                /* bytes in output buffer */
 
 /* local functions */
 
@@ -1811,14 +1789,11 @@ int gzip_main(int argc, char **argv)
        char *delFileName;
        int tostdout = 0;
        int fromstdin = 0;
-
-       if (argc == 1)
-               usage(gzip_usage);
+       int force = 0;
 
        /* Parse any options */
        while (--argc > 0 && **(++argv) == '-') {
                if (*((*argv) + 1) == '\0') {
-                       fromstdin = 1;
                        tostdout = 1;
                }
                while (*(++(*argv))) {
@@ -1826,11 +1801,27 @@ int gzip_main(int argc, char **argv)
                        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;
                        default:
                                usage(gzip_usage);
                        }
                }
        }
+       if (argc <= 0 ) {
+               fromstdin = 1;
+               tostdout = 1;
+       }
+
+       if (isatty(fileno(stdin)) && fromstdin==1 && force==0)
+               error_msg_and_die( "data not read from terminal. Use -f to force it.\n");
+       if (isatty(fileno(stdout)) && tostdout==1 && force==0)
+               error_msg_and_die( "data not written to terminal. Use -f to force it.\n");
 
        foreground = signal(SIGINT, SIG_IGN) != SIG_IGN;
        if (foreground) {
@@ -1870,7 +1861,7 @@ int gzip_main(int argc, char **argv)
                ifile_size = -1L;               /* convention for unknown size */
        } else {
                /* Open up the input file */
-               if (*argv == '\0')
+               if (argc <= 0)
                        usage(gzip_usage);
                strncpy(ifname, *argv, MAX_PATH_LEN);
 
@@ -1878,13 +1869,13 @@ int gzip_main(int argc, char **argv)
                inFileNum = open(ifname, O_RDONLY);
                if (inFileNum < 0) {
                        perror(ifname);
-                       do_exit(WARNING);
+                       exit(WARNING);
                }
                /* Get the time stamp on the input file. */
                result = stat(ifname, &statBuf);
                if (result < 0) {
                        perror(ifname);
-                       do_exit(WARNING);
+                       exit(WARNING);
                }
                time_stamp = statBuf.st_ctime;
                ifile_size = statBuf.st_size;
@@ -1918,7 +1909,7 @@ int gzip_main(int argc, char **argv)
 #endif
                if (outFileNum < 0) {
                        perror(ofname);
-                       do_exit(WARNING);
+                       exit(WARNING);
                }
                SET_BINARY_MODE(outFileNum);
                /* Set permissions on the file */
@@ -1939,11 +1930,11 @@ int gzip_main(int argc, char **argv)
 
                if (unlink(delFileName) < 0) {
                        perror(delFileName);
-                       exit(FALSE);
+                       exit(EXIT_FAILURE);
                }
        }
 
-       do_exit(exit_code);
+       return(exit_code);
 }
 
 /* trees.c -- output deflated data using Huffman coding
@@ -2909,7 +2900,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");
+                       error_msg("block vanished\n");
 
                copy_block(buf, (unsigned) stored_len, 0);      /* without header */
                compressed_len = stored_len << 3;
@@ -3092,7 +3083,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");
+               error_msg("-l used on binary file\n");
        }
 }
 
@@ -3133,7 +3124,7 @@ int in, out;                                      /* input and output file descriptors */
                insize = read(in, (char *) inbuf, INBUFSIZ);
        }
        if ((int) insize == EOF && errno != 0) {
-               read_error();
+               read_error_msg();
        }
        bytes_in = bytes_out;
        return OK;
@@ -3248,13 +3239,13 @@ char *env;                                              /* name of environment variable */
        nargv = (char **) calloc(*argcp + 1, sizeof(char *));
 
        if (nargv == NULL)
-               errorMsg("out of memory");
+               error_msg(memory_exhausted);
        oargv = *argvp;
        *argvp = nargv;
 
        /* Copy the program name first */
        if (oargc-- < 0)
-               errorMsg("argc<=0");
+               error_msg("argc<=0\n");
        *(nargv++) = *(oargv++);
 
        /* Then copy the environment args */
@@ -3389,3 +3380,17 @@ unsigned size;
        isize += (ulg) len;
        return (int) len;
 }
+
+/* ===========================================================================
+ * Write the output buffer outbuf[0..outcnt-1] and update bytes_out.
+ * (used for the compressed data only)
+ */
+void flush_outbuf()
+{
+       if (outcnt == 0)
+               return;
+
+       write_buf(ofd, (char *) outbuf, outcnt);
+       bytes_out += (ulg) outcnt;
+       outcnt = 0;
+}