zgrep: fix parsing of -Eh options
[gzip.git] / zip.c
blob71623660ad003fe32913acf0d7e72d7c407a219c
1 /* zip.c -- compress files to the gzip or pkzip format
3 Copyright (C) 1997-1999, 2006-2007, 2009-2010 Free Software Foundation, Inc.
4 Copyright (C) 1992-1993 Jean-loup Gailly
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
20 #include <config.h>
21 #include <ctype.h>
23 #include "tailor.h"
24 #include "gzip.h"
25 #include "crypt.h"
27 #include <unistd.h>
28 #include <fcntl.h>
30 local ulg crc; /* crc on uncompressed file data */
31 off_t header_bytes; /* number of bytes in gzip header */
33 /* ===========================================================================
34 * Deflate in to out.
35 * IN assertions: the input and output buffers are cleared.
36 * The variables time_stamp and save_orig_name are initialized.
38 int zip(in, out)
39 int in, out; /* input and output file descriptors */
41 uch flags = 0; /* general purpose bit flags */
42 ush attr = 0; /* ascii/binary flag */
43 ush deflate_flags = 0; /* pkzip -es, -en or -ex equivalent */
44 ulg stamp;
46 ifd = in;
47 ofd = out;
48 outcnt = 0;
50 /* Write the header to the gzip file. See algorithm.doc for the format */
52 method = DEFLATED;
53 put_byte(GZIP_MAGIC[0]); /* magic header */
54 put_byte(GZIP_MAGIC[1]);
55 put_byte(DEFLATED); /* compression method */
57 if (save_orig_name) {
58 flags |= ORIG_NAME;
60 put_byte(flags); /* general flags */
61 stamp = (0 <= time_stamp.tv_sec && time_stamp.tv_sec <= 0xffffffff
62 ? (ulg) time_stamp.tv_sec
63 : (ulg) 0);
64 put_long (stamp);
66 /* Write deflated file to zip file */
67 crc = updcrc(0, 0);
69 bi_init(out);
70 ct_init(&attr, &method);
71 lm_init(level, &deflate_flags);
73 put_byte((uch)deflate_flags); /* extra flags */
74 put_byte(OS_CODE); /* OS identifier */
76 if (save_orig_name) {
77 char *p = gzip_base_name (ifname); /* Don't save the directory part. */
78 do {
79 put_byte (*p);
80 } while (*p++);
82 header_bytes = (off_t)outcnt;
84 (void)deflate();
86 #ifndef NO_SIZE_CHECK
87 /* Check input size (but not in VMS -- variable record lengths mess it up)
88 * and not on MSDOS -- diet in TSR mode reports an incorrect file size)
90 if (ifile_size != -1L && bytes_in != ifile_size) {
91 fprintf(stderr, "%s: %s: file size changed while zipping\n",
92 program_name, ifname);
94 #endif
96 /* Write the crc and uncompressed size */
97 put_long(crc);
98 put_long((ulg)bytes_in);
99 header_bytes += 2*4;
101 flush_outbuf();
102 return OK;
106 /* ===========================================================================
107 * Read a new buffer from the current input file, perform end-of-line
108 * translation, and update the crc and input file size.
109 * IN assertion: size >= 2 (for end-of-line translation)
111 int file_read(buf, size)
112 char *buf;
113 unsigned size;
115 unsigned len;
117 Assert(insize == 0, "inbuf not empty");
119 len = read_buffer (ifd, buf, size);
120 if (len == 0) return (int)len;
121 if (len == (unsigned)-1) {
122 read_error();
123 return EOF;
126 crc = updcrc((uch*)buf, len);
127 bytes_in += (off_t)len;
128 return (int)len;