home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 5 / FreshFish_July-August1994.bin / bbs / gnu / gzip-1.2.4-src.lha / src / amiga / gzip-1.2.4 / gzip.c < prev    next >
C/C++ Source or Header  |  1993-08-19  |  53KB  |  1,745 lines

  1. /* gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface
  2.  * Copyright (C) 1992-1993 Jean-loup Gailly
  3.  * The unzip code was written and put in the public domain by Mark Adler.
  4.  * Portions of the lzw code are derived from the public domain 'compress'
  5.  * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
  6.  * Ken Turkowski, Dave Mack and Peter Jannesen.
  7.  *
  8.  * See the license_msg below and the file COPYING for the software license.
  9.  * See the file algorithm.doc for the compression algorithms and file formats.
  10.  */
  11.  
  12. static char  *license_msg[] = {
  13. "   Copyright (C) 1992-1993 Jean-loup Gailly",
  14. "   This program is free software; you can redistribute it and/or modify",
  15. "   it under the terms of the GNU General Public License as published by",
  16. "   the Free Software Foundation; either version 2, or (at your option)",
  17. "   any later version.",
  18. "",
  19. "   This program is distributed in the hope that it will be useful,",
  20. "   but WITHOUT ANY WARRANTY; without even the implied warranty of",
  21. "   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the",
  22. "   GNU General Public License for more details.",
  23. "",
  24. "   You should have received a copy of the GNU General Public License",
  25. "   along with this program; if not, write to the Free Software",
  26. "   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.",
  27. 0};
  28.  
  29. /* Compress files with zip algorithm and 'compress' interface.
  30.  * See usage() and help() functions below for all options.
  31.  * Outputs:
  32.  *        file.gz:   compressed file with same mode, owner, and utimes
  33.  *     or stdout with -c option or if stdin used as input.
  34.  * If the output file name had to be truncated, the original name is kept
  35.  * in the compressed file.
  36.  * On MSDOS, file.tmp -> file.tmz. On VMS, file.tmp -> file.tmp-gz.
  37.  *
  38.  * Using gz on MSDOS would create too many file name conflicts. For
  39.  * example, foo.txt -> foo.tgz (.tgz must be reserved as shorthand for
  40.  * tar.gz). Similarly, foo.dir and foo.doc would both be mapped to foo.dgz.
  41.  * I also considered 12345678.txt -> 12345txt.gz but this truncates the name
  42.  * too heavily. There is no ideal solution given the MSDOS 8+3 limitation. 
  43.  *
  44.  * For the meaning of all compilation flags, see comments in Makefile.in.
  45.  */
  46.  
  47. #ifdef RCSID
  48. static char rcsid[] = "$Id: gzip.c,v 0.24 1993/06/24 10:52:07 jloup Exp $";
  49. #endif
  50.  
  51. #include <ctype.h>
  52. #include <sys/types.h>
  53. #include <signal.h>
  54. #include <sys/stat.h>
  55. #include <errno.h>
  56.  
  57. #include "tailor.h"
  58. #include "gzip.h"
  59. #include "lzw.h"
  60. #include "revision.h"
  61. #include "getopt.h"
  62.  
  63.         /* configuration */
  64.  
  65. #ifdef NO_TIME_H
  66. #  include <sys/time.h>
  67. #else
  68. #  include <time.h>
  69. #endif
  70.  
  71. #ifndef NO_FCNTL_H
  72. #  include <fcntl.h>
  73. #endif
  74.  
  75. #ifdef HAVE_UNISTD_H
  76. #  include <unistd.h>
  77. #endif
  78.  
  79. #if defined(STDC_HEADERS) || !defined(NO_STDLIB_H)
  80. #  include <stdlib.h>
  81. #else
  82.    extern int errno;
  83. #endif
  84.  
  85. #if defined(DIRENT)
  86. #  include <dirent.h>
  87.    typedef struct dirent dir_type;
  88. #  define NLENGTH(dirent) ((int)strlen((dirent)->d_name))
  89. #  define DIR_OPT "DIRENT"
  90. #else
  91. #  define NLENGTH(dirent) ((dirent)->d_namlen)
  92. #  ifdef SYSDIR
  93. #    include <sys/dir.h>
  94.      typedef struct direct dir_type;
  95. #    define DIR_OPT "SYSDIR"
  96. #  else
  97. #    ifdef SYSNDIR
  98. #      include <sys/ndir.h>
  99.        typedef struct direct dir_type;
  100. #      define DIR_OPT "SYSNDIR"
  101. #    else
  102. #      ifdef NDIR
  103. #        include <ndir.h>
  104.          typedef struct direct dir_type;
  105. #        define DIR_OPT "NDIR"
  106. #      else
  107. #        define NO_DIR
  108. #        define DIR_OPT "NO_DIR"
  109. #      endif
  110. #    endif
  111. #  endif
  112. #endif
  113.  
  114. #ifndef NO_UTIME
  115. #  ifndef NO_UTIME_H
  116. #    include <utime.h>
  117. #    define TIME_OPT "UTIME"
  118. #  else
  119. #    ifdef HAVE_SYS_UTIME_H
  120. #      include <sys/utime.h>
  121. #      define TIME_OPT "SYS_UTIME"
  122. #    else
  123.        struct utimbuf {
  124.          time_t actime;
  125.          time_t modtime;
  126.        };
  127. #      define TIME_OPT ""
  128. #    endif
  129. #  endif
  130. #else
  131. #  define TIME_OPT "NO_UTIME"
  132. #endif
  133.  
  134. #if !defined(S_ISDIR) && defined(S_IFDIR)
  135. #  define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
  136. #endif
  137. #if !defined(S_ISREG) && defined(S_IFREG)
  138. #  define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
  139. #endif
  140.  
  141. typedef RETSIGTYPE (*sig_type) OF((int));
  142.  
  143. #ifndef    O_BINARY
  144. #  define  O_BINARY  0  /* creation mode for open() */
  145. #endif
  146.  
  147. #ifndef O_CREAT
  148.    /* Pure BSD system? */
  149. #  include <sys/file.h>
  150. #  ifndef O_CREAT
  151. #    define O_CREAT FCREAT
  152. #  endif
  153. #  ifndef O_EXCL
  154. #    define O_EXCL FEXCL
  155. #  endif
  156. #endif
  157.  
  158. #ifndef S_IRUSR
  159. #  define S_IRUSR 0400
  160. #endif
  161. #ifndef S_IWUSR
  162. #  define S_IWUSR 0200
  163. #endif
  164. #define RW_USER (S_IRUSR | S_IWUSR)  /* creation mode for open() */
  165.  
  166. #ifndef MAX_PATH_LEN
  167. #  define MAX_PATH_LEN   1024 /* max pathname length */
  168. #endif
  169.  
  170. #ifndef SEEK_END
  171. #  define SEEK_END 2
  172. #endif
  173.  
  174. #ifdef NO_OFF_T
  175.   typedef long off_t;
  176.   off_t lseek OF((int fd, off_t offset, int whence));
  177. #endif
  178.  
  179. /* Separator for file name parts (see shorten_name()) */
  180. #ifdef NO_MULTIPLE_DOTS
  181. #  define PART_SEP "-"
  182. #else
  183. #  define PART_SEP "."
  184. #endif
  185.  
  186.         /* global buffers */
  187.  
  188. DECLARE(uch, inbuf,  INBUFSIZ +INBUF_EXTRA);
  189. DECLARE(uch, outbuf, OUTBUFSIZ+OUTBUF_EXTRA);
  190. DECLARE(ush, d_buf,  DIST_BUFSIZE);
  191. DECLARE(uch, window, 2L*WSIZE);
  192. #ifndef MAXSEG_64K
  193.     DECLARE(ush, tab_prefix, 1L<<BITS);
  194. #else
  195.     DECLARE(ush, tab_prefix0, 1L<<(BITS-1));
  196.     DECLARE(ush, tab_prefix1, 1L<<(BITS-1));
  197. #endif
  198.  
  199.         /* local variables */
  200.  
  201. int ascii = 0;        /* convert end-of-lines to local OS conventions */
  202. int to_stdout = 0;    /* output to stdout (-c) */
  203. int decompress = 0;   /* decompress (-d) */
  204. int force = 0;        /* don't ask questions, compress links (-f) */
  205. int no_name = -1;     /* don't save or restore the original file name */
  206. int no_time = -1;     /* don't save or restore the original file time */
  207. int recursive = 0;    /* recurse through directories (-r) */
  208. int list = 0;         /* list the file contents (-l) */
  209. int verbose = 0;      /* be verbose (-v) */
  210. int quiet = 0;        /* be very quiet (-q) */
  211. int do_lzw = 0;       /* generate output compatible with old compress (-Z) */
  212. int test = 0;         /* test .gz file integrity */
  213. int foreground;       /* set if program run in foreground */
  214. char *progname;       /* program name */
  215. int maxbits = BITS;   /* max bits per code for LZW */
  216. int method = DEFLATED;/* compression method */
  217. int level = 6;        /* compression level */
  218. int exit_code = OK;   /* program exit code */
  219. int save_orig_name;   /* set if original name must be saved */
  220. int last_member;      /* set for .zip and .Z files */
  221. int part_nb;          /* number of parts in .gz file */
  222. long time_stamp;      /* original time stamp (modification time) */
  223. long ifile_size;      /* input file size, -1 for devices (debug only) */
  224. char *env;            /* contents of GZIP env variable */
  225. char **args = NULL;   /* argv pointer if GZIP env variable defined */
  226. char z_suffix[MAX_SUFFIX+1]; /* default suffix (can be set with --suffix) */
  227. int  z_len;           /* strlen(z_suffix) */
  228.  
  229. long bytes_in;             /* number of input bytes */
  230. long bytes_out;            /* number of output bytes */
  231. long total_in = 0;         /* input bytes for all files */
  232. long total_out = 0;        /* output bytes for all files */
  233. char ifname[MAX_PATH_LEN]; /* input file name */
  234. char ofname[MAX_PATH_LEN]; /* output file name */
  235. int  remove_ofname = 0;       /* remove output file on error */
  236. struct stat istat;         /* status for input file */
  237. int  ifd;                  /* input file descriptor */
  238. int  ofd;                  /* output file descriptor */
  239. unsigned insize;           /* valid bytes in inbuf */
  240. unsigned inptr;            /* index of next byte to be processed in inbuf */
  241. unsigned outcnt;           /* bytes in output buffer */
  242.  
  243. struct option longopts[] =
  244. {
  245.  /* { name  has_arg  *flag  val } */
  246.     {"ascii",      0, 0, 'a'}, /* ascii text mode */
  247.     {"to-stdout",  0, 0, 'c'}, /* write output on standard output */
  248.     {"stdout",     0, 0, 'c'}, /* write output on standard output */
  249.     {"decompress", 0, 0, 'd'}, /* decompress */
  250.     {"uncompress", 0, 0, 'd'}, /* decompress */
  251.  /* {"encrypt",    0, 0, 'e'},    encrypt */
  252.     {"force",      0, 0, 'f'}, /* force overwrite of output file */
  253.     {"help",       0, 0, 'h'}, /* give help */
  254.  /* {"pkzip",      0, 0, 'k'},    force out