Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / gnu / usr.bin / gzip / zip.c
1 /* zip.c -- compress files to the gzip or pkzip format
2  * Copyright (C) 1992-1993 Jean-loup Gailly
3  * This is free software; you can redistribute it and/or modify it under the
4  * terms of the GNU General Public License, see the file COPYING.
5  *
6  * $FreeBSD: src/gnu/usr.bin/gzip/zip.c,v 1.6 1999/08/27 23:35:56 peter Exp $
7  * $DragonFly: src/gnu/usr.bin/gzip/Attic/zip.c,v 1.2 2003/06/17 04:25:46 dillon Exp $
8  */
9
10 #include <ctype.h>
11 #include <sys/types.h>
12
13 #include "tailor.h"
14 #include "gzip.h"
15 #include "crypt.h"
16
17 #ifdef HAVE_UNISTD_H
18 #  include <unistd.h>
19 #endif
20 #ifndef NO_FCNTL_H
21 #  include <fcntl.h>
22 #endif
23
24 local ulg crc;       /* crc on uncompressed file data */
25 long header_bytes;   /* number of bytes in gzip header */
26
27 /* ===========================================================================
28  * Deflate in to out.
29  * IN assertions: the input and output buffers are cleared.
30  *   The variables time_stamp and save_orig_name are initialized.
31  */
32 int zip(in, out)
33     int in, out;            /* input and output file descriptors */
34 {
35     uch  flags = 0;         /* general purpose bit flags */
36     ush  attr = 0;          /* ascii/binary flag */
37     ush  deflate_flags = 0; /* pkzip -es, -en or -ex equivalent */
38
39     ifd = in;
40     ofd = out;
41     outcnt = 0;
42
43     /* Write the header to the gzip file. See algorithm.doc for the format */
44
45     method = DEFLATED;
46     put_byte(GZIP_MAGIC[0]); /* magic header */
47     put_byte(GZIP_MAGIC[1]);
48     put_byte(DEFLATED);      /* compression method */
49
50     if (save_orig_name) {
51         flags |= ORIG_NAME;
52     }
53     put_byte(flags);         /* general flags */
54     put_long(time_stamp);
55
56     /* Write deflated file to zip file */
57     crc = updcrc(0, 0);
58
59     bi_init(out);
60     ct_init(&attr, &method);
61     lm_init(level, &deflate_flags);
62
63     put_byte((uch)deflate_flags); /* extra flags */
64     put_byte(OS_CODE);            /* OS identifier */
65
66     if (save_orig_name) {
67         char *p = basename(ifname); /* Don't save the directory part. */
68         do {
69             put_char(*p);
70         } while (*p++);
71     }
72     header_bytes = (long)outcnt;
73
74     (void)deflate();
75
76 #if !defined(NO_SIZE_CHECK) && !defined(RECORD_IO)
77   /* Check input size (but not in VMS -- variable record lengths mess it up)
78    * and not on MSDOS -- diet in TSR mode reports an incorrect file size)
79    */
80     if (ifile_size != -1L && isize != (ulg)ifile_size) {
81         Trace((stderr, " actual=%ld, read=%ld ", ifile_size, isize));
82         fprintf(stderr, "%s: %s: file size changed while zipping\n",
83                 progname, ifname);
84     }
85 #endif
86
87     /* Write the crc and uncompressed size */
88     put_long(crc);
89     put_long(isize);
90     header_bytes += 2*sizeof(long);
91
92     flush_outbuf();
93     return OK;
94 }
95
96
97 /* ===========================================================================
98  * Read a new buffer from the current input file, perform end-of-line
99  * translation, and update the crc and input file size.
100  * IN assertion: size >= 2 (for end-of-line translation)
101  */
102 int file_read(buf, size)
103     char *buf;
104     unsigned size;
105 {
106     unsigned len;
107
108     Assert(insize == 0, "inbuf not empty");
109
110     len = read(ifd, buf, size);
111     if (len == (unsigned)(-1) || len == 0) return (int)len;
112
113     crc = updcrc((uch*)buf, len);
114     isize += (ulg)len;
115     return (int)len;
116 }