]>
git.cameronkatri.com Git - apple_cmds.git/blob - file_cmds/gzip/gzip.c
1 /* $NetBSD: gzip.c,v 1.109 2015/10/27 07:36:18 mrg Exp $ */
4 * Copyright (c) 1997, 1998, 2003, 2004, 2006 Matthew R. Green
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
32 __COPYRIGHT("@(#) Copyright (c) 1997, 1998, 2003, 2004, 2006\
33 Matthew R. Green. All rights reserved.");
34 __FBSDID("$FreeBSD: head/usr.bin/gzip/gzip.c 290073 2015-10-27 21:26:05Z delphij $");
38 * gzip.c -- GPL free gzip using zlib.
40 * RFC 1950 covers the zlib format
41 * RFC 1951 covers the deflate format
42 * RFC 1952 covers the gzip format
45 * - use mmap where possible
46 * - make bzip2/compress -v/-t/-l support work as well as possible
49 #include <sys/param.h>
71 #include <get_compat.h>
72 int futimens(int fd
, const struct timespec times
[2]);
73 #endif /* __APPLE__ */
75 /* what type of file are we dealing with */
78 #ifndef NO_BZIP2_SUPPORT
81 #ifndef NO_COMPRESS_SUPPORT
84 #ifndef NO_PACK_SUPPORT
94 #ifndef NO_BZIP2_SUPPORT
97 #define BZ2_SUFFIX ".bz2"
98 #define BZIP2_MAGIC "\102\132\150"
101 #ifndef NO_COMPRESS_SUPPORT
102 #define Z_SUFFIX ".Z"
103 #define Z_MAGIC "\037\235"
106 #ifndef NO_PACK_SUPPORT
107 #define PACK_MAGIC "\037\036"
110 #ifndef NO_XZ_SUPPORT
112 #define XZ_SUFFIX ".xz"
113 #define XZ_MAGIC "\3757zXZ"
116 #define GZ_SUFFIX ".gz"
118 #define BUFLEN (64 * 1024)
120 #define GZIP_MAGIC0 0x1F
121 #define GZIP_MAGIC1 0x8B
122 #define GZIP_OMAGIC1 0x9E
124 #define GZIP_TIMESTAMP (off_t)4
125 #define GZIP_ORIGNAME (off_t)10
127 #define HEAD_CRC 0x02
128 #define EXTRA_FIELD 0x04
129 #define ORIG_NAME 0x08
132 #define OS_CODE 3 /* Unix */
137 const char *normal
; /* for unzip - must not be longer than zipped */
139 static suffixes_t suffixes
[] = {
140 #define SUFFIX(Z, N) {Z, sizeof Z - 1, N}
141 SUFFIX(GZ_SUFFIX
, ""), /* Overwritten by -S .xxx */
143 SUFFIX(GZ_SUFFIX
, ""),
148 SUFFIX(".taz", ".tar"),
149 SUFFIX(".tgz", ".tar"),
150 #ifndef NO_BZIP2_SUPPORT
151 SUFFIX(BZ2_SUFFIX
, ""),
152 SUFFIX(".tbz", ".tar"),
153 SUFFIX(".tbz2", ".tar"),
155 #ifndef NO_COMPRESS_SUPPORT
156 SUFFIX(Z_SUFFIX
, ""),
158 #ifndef NO_XZ_SUPPORT
159 SUFFIX(XZ_SUFFIX
, ""),
161 SUFFIX(GZ_SUFFIX
, ""), /* Overwritten by -S "" */
165 #define NUM_SUFFIXES (sizeof suffixes / sizeof suffixes[0])
166 #define SUFFIX_MAXLEN 30
169 static const char gzip_version
[] = "Apple gzip " GZIP_APPLE_VERSION
;
171 static const char gzip_version
[] = "FreeBSD gzip 20150413";
175 static const char gzip_copyright
[] = \
176 " Copyright (c) 1997, 1998, 2003, 2004, 2006 Matthew R. Green\n"
177 " All rights reserved.\n"
179 " Redistribution and use in source and binary forms, with or without\n"
180 " modification, are permitted provided that the following conditions\n"
182 " 1. Redistributions of source code must retain the above copyright\n"
183 " notice, this list of conditions and the following disclaimer.\n"
184 " 2. Redistributions in binary form must reproduce the above copyright\n"
185 " notice, this list of conditions and the following disclaimer in the\n"
186 " documentation and/or other materials provided with the distribution.\n"
188 " THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\n"
189 " IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\n"
190 " OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\n"
191 " IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,\n"
192 " INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,\n"
193 " BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\n"
194 " LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED\n"
195 " AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,\n"
196 " OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\n"
197 " OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\n"
201 static int cflag
; /* stdout mode */
202 static int dflag
; /* decompress mode */
203 static int lflag
; /* list mode */
204 static int numflag
= 6; /* gzip -1..-9 value */
207 static int fflag
; /* force mode */
208 static int kflag
; /* don't delete input files */
209 static int nflag
; /* don't save name/timestamp */
210 static int Nflag
; /* don't restore name/timestamp */
211 static int qflag
; /* quiet mode */
212 static int rflag
; /* recursive mode */
213 static int tflag
; /* test */
214 static int vflag
; /* verbose mode */
215 static const char *remove_file
= NULL
; /* file to be removed upon SIGINT */
221 static int exit_value
= 0; /* exit value */
223 static char *infile
; /* name of file coming in */
229 static void maybe_err(const char *fmt
, ...) __printflike(1, 2) __dead2
;
230 #if !defined(NO_BZIP2_SUPPORT) || !defined(NO_PACK_SUPPORT) || \
231 !defined(NO_XZ_SUPPORT)
232 static void maybe_errx(const char *fmt
, ...) __printflike(1, 2) __dead2
;
234 static void maybe_warn(const char *fmt
, ...) __printflike(1, 2);
235 static void maybe_warnx(const char *fmt
, ...) __printflike(1, 2);
236 static enum filetype
file_gettype(u_char
*);
238 #define gz_compress(if, of, sz, fn, tm) gz_compress(if, of, sz)
240 static off_t
gz_compress(int, int, off_t
*, const char *, uint32_t);
241 static off_t
gz_uncompress(int, int, char *, size_t, off_t
*, const char *);
242 static off_t
file_compress(char *, char *, size_t);
243 static off_t
file_uncompress(char *, char *, size_t);
244 static void handle_pathname(char *);
245 static void handle_file(char *, struct stat
*);
246 static void handle_stdin(void);
247 static void handle_stdout(void);
248 static void print_ratio(off_t
, off_t
, FILE *);
249 static void print_list(int fd
, off_t
, const char *, time_t);
250 static void usage(void) __dead2
;
251 static void display_version(void) __dead2
;
253 static void display_license(void);
254 static void sigint_handler(int);
256 static const suffixes_t
*check_suffix(char *, int);
257 static ssize_t
read_retry(int, void *, size_t);
260 #define unlink_input(f, sb) unlink(f)
262 static off_t
cat_fd(unsigned char *, size_t, off_t
*, int fd
);
263 static void prepend_gzip(char *, int *, char ***);
264 static void handle_dir(char *);
265 static void print_verbage(const char *, const char *, off_t
, off_t
);
266 static void print_test(const char *, int);
267 static void copymodes(int fd
, const struct stat
*, const char *file
);
268 static int check_outfile(const char *outfile
);
271 #ifndef NO_BZIP2_SUPPORT
272 static off_t
unbzip2(int, int, char *, size_t, off_t
*);
275 #ifndef NO_COMPRESS_SUPPORT
276 static FILE *zdopen(int);
277 static off_t
zuncompress(FILE *, FILE *, char *, size_t, off_t
*);
280 #ifndef NO_PACK_SUPPORT
281 static off_t
unpack(int, int, char *, size_t, off_t
*);
284 #ifndef NO_XZ_SUPPORT
285 static off_t
unxz(int, int, char *, size_t, off_t
*);
289 #define getopt_long(a,b,c,d,e) getopt(a,b,c)
291 static const struct option longopts
[] = {
292 { "stdout", no_argument
, 0, 'c' },
293 { "to-stdout", no_argument
, 0, 'c' },
294 { "decompress", no_argument
, 0, 'd' },
295 { "uncompress", no_argument
, 0, 'd' },
296 { "force", no_argument
, 0, 'f' },
297 { "help", no_argument
, 0, 'h' },
298 { "keep", no_argument
, 0, 'k' },
299 { "list", no_argument
, 0, 'l' },
300 { "no-name", no_argument
, 0, 'n' },
301 { "name", no_argument
, 0, 'N' },
302 { "quiet", no_argument
, 0, 'q' },
303 { "recursive", no_argument
, 0, 'r' },
304 { "suffix", required_argument
, 0, 'S' },
305 { "test", no_argument
, 0, 't' },
306 { "verbose", no_argument
, 0, 'v' },
307 { "version", no_argument
, 0, 'V' },
308 { "fast", no_argument
, 0, '1' },
309 { "best", no_argument
, 0, '9' },
310 { "ascii", no_argument
, 0, 'a' },
311 { "license", no_argument
, 0, 'L' },
312 { NULL
, no_argument
, 0, 0 },
317 main(int argc
, char **argv
)
319 const char *progname
= getprogname();
327 if ((gzip
= getenv("GZIP")) != NULL
)
328 prepend_gzip(gzip
, &argc
, &argv
);
329 signal(SIGINT
, sigint_handler
);
334 * handle being called `gunzip', `zcat' and `gzcat'
336 if (strcmp(progname
, "gunzip") == 0)
338 else if (strcmp(progname
, "zcat") == 0 ||
339 strcmp(progname
, "gzcat") == 0)
343 if (strcmp(progname
, "zcat") == 0) {
349 #define OPT_LIST "123456789cdhlV"
351 #define OPT_LIST "123456789acdfhklLNnqrS:tVv"
354 while ((ch
= getopt_long(argc
, argv
, OPT_LIST
, longopts
, NULL
)) != -1) {
356 case '1': case '2': case '3':
357 case '4': case '5': case '6':
358 case '7': case '8': case '9':
376 fprintf(stderr
, "%s: option --ascii ignored on this system\n", progname
);
402 len
= strlen(optarg
);
404 if (len
> SUFFIX_MAXLEN
)
405 errx(1, "incorrect suffix: '%s': too long", optarg
);
406 suffixes
[0].zipped
= optarg
;
407 suffixes
[0].ziplen
= len
;
409 suffixes
[NUM_SUFFIXES
- 1].zipped
= "";
410 suffixes
[NUM_SUFFIXES
- 1].ziplen
= 0;
431 if (dflag
) /* stdin mode */
433 else /* stdout mode */
437 handle_pathname(argv
[0]);
441 if (qflag
== 0 && lflag
&& argc
> 1)
442 print_list(-1, 0, "(totals)", 0);
447 /* maybe print a warning */
449 maybe_warn(const char *fmt
, ...)
462 /* ... without an errno. */
464 maybe_warnx(const char *fmt
, ...)
477 /* maybe print an error */
479 maybe_err(const char *fmt
, ...)
491 #if !defined(NO_BZIP2_SUPPORT) || !defined(NO_PACK_SUPPORT) || \
492 !defined(NO_XZ_SUPPORT)
493 /* ... without an errno. */
495 maybe_errx(const char *fmt
, ...)
509 /* split up $GZIP and prepend it to the argument list */
511 prepend_gzip(char *gzip
, int *argc
, char ***argv
)
513 char *s
, **nargv
, **ac
;
516 /* scan how many arguments there are */
518 while (*s
== ' ' || *s
== '\t')
523 while (*s
!= ' ' && *s
!= '\t')
535 nargv
= (char **)malloc((*argc
+ 1) * sizeof(char *));
539 /* stash this away */
542 /* copy the program name first */
544 nargv
[i
++] = *(ac
++);
548 /* Skip whitespaces. */
549 while (*s
== ' ' || *s
== '\t')
555 /* Find the end of this argument. */
556 while (*s
!= ' ' && *s
!= '\t')
558 /* Argument followed by NUL. */
560 /* copy any unterminated args */
561 nargv
[i
-1] = strndup(nargv
[i
-1], s
-nargv
[i
-1]);
562 if (nargv
[i
-1] == NULL
)
563 maybe_err("strndup");
568 /* copy the original arguments and a NULL */
570 nargv
[i
++] = *(ac
++);
575 /* compress input to output. Return bytes read, -1 on error */
577 gz_compress(int in
, int out
, off_t
*gsizep
, const char *origname
, uint32_t mtime
)
580 char *outbufp
, *inbufp
;
581 off_t in_tot
= 0, out_tot
= 0;
586 static char header
[] = { GZIP_MAGIC0
, GZIP_MAGIC1
, Z_DEFLATED
, 0,
591 outbufp
= malloc(BUFLEN
);
592 inbufp
= malloc(BUFLEN
);
593 if (outbufp
== NULL
|| inbufp
== NULL
) {
594 maybe_err("malloc failed");
598 memset(&z
, 0, sizeof z
);
604 memcpy(outbufp
, header
, sizeof header
);
612 i
= snprintf(outbufp
, BUFLEN
, "%c%c%c%c%c%c%c%c%c%c%s",
613 GZIP_MAGIC0
, GZIP_MAGIC1
, Z_DEFLATED
,
614 *origname
? ORIG_NAME
: 0,
617 (mtime
>> 16) & 0xff,
618 (mtime
>> 24) & 0xff,
619 numflag
== 1 ? 4 : numflag
== 9 ? 2 : 0,
622 /* this need PATH_MAX > BUFLEN ... */
623 maybe_err("snprintf");
628 z
.next_out
= (unsigned char *)outbufp
+ i
;
629 z
.avail_out
= BUFLEN
- i
;
631 error
= deflateInit2(&z
, numflag
, Z_DEFLATED
,
632 (-MAX_WBITS
), 8, Z_DEFAULT_STRATEGY
);
634 maybe_warnx("deflateInit2 failed");
639 crc
= crc32(0L, Z_NULL
, 0);
641 if (z
.avail_out
== 0) {
642 if (write(out
, outbufp
, BUFLEN
) != BUFLEN
) {
649 z
.next_out
= (unsigned char *)outbufp
;
650 z
.avail_out
= BUFLEN
;
653 if (z
.avail_in
== 0) {
654 in_size
= read(in
, inbufp
, BUFLEN
);
663 crc
= crc32(crc
, (const Bytef
*)inbufp
, (unsigned)in_size
);
665 z
.next_in
= (unsigned char *)inbufp
;
666 z
.avail_in
= in_size
;
669 error
= deflate(&z
, Z_NO_FLUSH
);
670 if (error
!= Z_OK
&& error
!= Z_STREAM_END
) {
671 maybe_warnx("deflate failed");
682 error
= deflate(&z
, Z_FINISH
);
683 if (error
!= Z_OK
&& error
!= Z_STREAM_END
) {
684 maybe_warnx("deflate failed");
689 len
= (char *)z
.next_out
- outbufp
;
691 w
= write(out
, outbufp
, len
);
692 if (w
== -1 || (size_t)w
!= len
) {
698 z
.next_out
= (unsigned char *)outbufp
;
699 z
.avail_out
= BUFLEN
;
701 if (error
== Z_STREAM_END
)
705 if (deflateEnd(&z
) != Z_OK
) {
706 maybe_warnx("deflateEnd failed");
711 i
= snprintf(outbufp
, BUFLEN
, "%c%c%c%c%c%c%c%c",
713 (int)(crc
>> 8) & 0xff,
714 (int)(crc
>> 16) & 0xff,
715 (int)(crc
>> 24) & 0xff,
717 (int)(in_tot
>> 8) & 0xff,
718 (int)(in_tot
>> 16) & 0xff,
719 (int)(in_tot
>> 24) & 0xff);
721 maybe_err("snprintf");
722 if (write(out
, outbufp
, i
) != i
) {
739 * uncompress input to output then close the input. return the
740 * uncompressed size written, and put the compressed sized read
744 gz_uncompress(int in
, int out
, char *pre
, size_t prelen
, off_t
*gsizep
,
745 const char *filename
)
748 char *outbufp
, *inbufp
;
749 off_t out_tot
= -1, in_tot
= 0;
750 uint32_t out_sub_tot
= 0;
768 } state
= GZSTATE_MAGIC0
;
769 int flags
= 0, skip_count
= 0;
770 int error
= Z_STREAM_ERROR
, done_reading
= 0;
775 #define ADVANCE() { z.next_in++; z.avail_in--; }
777 if ((outbufp
= malloc(BUFLEN
)) == NULL
) {
778 maybe_err("malloc failed");
781 if ((inbufp
= malloc(BUFLEN
)) == NULL
) {
782 maybe_err("malloc failed");
786 memset(&z
, 0, sizeof z
);
788 z
.next_in
= (unsigned char *)pre
;
789 z
.avail_out
= BUFLEN
;
790 z
.next_out
= (unsigned char *)outbufp
;
799 if ((z
.avail_in
== 0 || needmore
) && done_reading
== 0) {
802 if (z
.avail_in
> 0) {
803 memmove(inbufp
, z
.next_in
, z
.avail_in
);
805 z
.next_in
= (unsigned char *)inbufp
;
806 in_size
= read(in
, z
.next_in
+ z
.avail_in
,
807 BUFLEN
- z
.avail_in
);
810 maybe_warn("failed to read stdin");
812 } else if (in_size
== 0) {
816 z
.avail_in
+= in_size
;
821 if (z
.avail_in
== 0) {
822 if (done_reading
&& state
!= GZSTATE_MAGIC0
) {
823 maybe_warnx("%s: unexpected end of file",
831 if (*z
.next_in
!= GZIP_MAGIC0
) {
833 maybe_warnx("%s: trailing garbage "
834 "ignored", filename
);
838 maybe_warnx("input not gziped (MAGIC0)");
844 crc
= crc32(0L, Z_NULL
, 0);
848 if (*z
.next_in
!= GZIP_MAGIC1
&&
849 *z
.next_in
!= GZIP_OMAGIC1
) {
850 maybe_warnx("input not gziped (MAGIC1)");
858 if (*z
.next_in
!= Z_DEFLATED
) {
859 maybe_warnx("unknown compression method");
873 case GZSTATE_SKIPPING
:
874 if (skip_count
> 0) {
882 if ((flags
& EXTRA_FIELD
) == 0) {
883 state
= GZSTATE_ORIGNAME
;
886 skip_count
= *z
.next_in
;
892 skip_count
|= ((*z
.next_in
) << 8);
898 if (skip_count
> 0) {
905 case GZSTATE_ORIGNAME
:
906 if ((flags
& ORIG_NAME
) == 0) {
915 case GZSTATE_COMMENT
:
916 if ((flags
& COMMENT
) == 0) {
925 case GZSTATE_HEAD_CRC1
:
926 if (flags
& HEAD_CRC
)
933 case GZSTATE_HEAD_CRC2
:
934 if (skip_count
> 0) {
942 if (inflateInit2(&z
, -MAX_WBITS
) != Z_OK
) {
943 maybe_warnx("failed to inflateInit");
950 error
= inflate(&z
, Z_FINISH
);
952 /* Z_BUF_ERROR goes with Z_FINISH... */
954 if (z
.avail_out
> 0 && !done_reading
)
962 maybe_warnx("Z_NEED_DICT error");
965 maybe_warnx("data stream error");
968 maybe_warnx("internal stream error");
971 maybe_warnx("memory allocation error");
975 maybe_warn("unknown error from inflate(): %d",
978 wr
= BUFLEN
- z
.avail_out
;
981 crc
= crc32(crc
, (const Bytef
*)outbufp
, (unsigned)wr
);
984 /* don't write anything with -t */
987 write(out
, outbufp
, wr
) != wr
) {
988 maybe_warn("error writing to output");
996 if (error
== Z_STREAM_END
) {
1001 z
.next_out
= (unsigned char *)outbufp
;
1002 z
.avail_out
= BUFLEN
;
1009 if (z
.avail_in
< 4) {
1010 if (!done_reading
) {
1014 maybe_warnx("truncated input");
1017 origcrc
= ((unsigned)z
.next_in
[0] & 0xff) |
1018 ((unsigned)z
.next_in
[1] & 0xff) << 8 |
1019 ((unsigned)z
.next_in
[2] & 0xff) << 16 |
1020 ((unsigned)z
.next_in
[3] & 0xff) << 24;
1021 if (origcrc
!= crc
) {
1022 maybe_warnx("invalid compressed"
1023 " data--crc error");
1031 if (!z
.avail_in
&& done_reading
) {
1040 if (z
.avail_in
< 4) {
1041 if (!done_reading
) {
1045 maybe_warnx("truncated input");
1048 origlen
= ((unsigned)z
.next_in
[0] & 0xff) |
1049 ((unsigned)z
.next_in
[1] & 0xff) << 8 |
1050 ((unsigned)z
.next_in
[2] & 0xff) << 16 |
1051 ((unsigned)z
.next_in
[3] & 0xff) << 24;
1053 if (origlen
!= out_sub_tot
) {
1054 maybe_warnx("invalid compressed"
1055 " data--length error");
1064 maybe_warnx("decompression error");
1067 state
= GZSTATE_MAGIC0
;
1076 if (state
> GZSTATE_INIT
)
1090 * set the owner, mode, flags & utimes using the given file descriptor.
1091 * file is only used in possible warning messages.
1094 copymodes(int fd
, const struct stat
*sbp
, const char *file
)
1096 struct timespec times
[2];
1100 * If we have no info on the input, give this file some
1101 * default values and return..
1104 mode_t mask
= umask(022);
1106 (void)fchmod(fd
, DEFFILEMODE
& ~mask
);
1112 /* if the chown fails, remove set-id bits as-per compress(1) */
1113 if (fchown(fd
, sb
.st_uid
, sb
.st_gid
) < 0) {
1115 maybe_warn("couldn't fchown: %s", file
);
1116 sb
.st_mode
&= ~(S_ISUID
|S_ISGID
);
1119 /* we only allow set-id and the 9 normal permission bits */
1120 sb
.st_mode
&= S_ISUID
| S_ISGID
| S_IRWXU
| S_IRWXG
| S_IRWXO
;
1121 if (fchmod(fd
, sb
.st_mode
) < 0)
1122 maybe_warn("couldn't fchmod: %s", file
);
1125 times
[0] = sb
.st_atimespec
;
1126 times
[1] = sb
.st_mtimespec
;
1128 times
[0] = sb
.st_atim
;
1129 times
[1] = sb
.st_mtim
;
1131 if (futimens(fd
, times
) < 0)
1132 maybe_warn("couldn't futimens: %s", file
);
1134 /* only try flags if they exist already */
1135 if (sb
.st_flags
!= 0 && fchflags(fd
, sb
.st_flags
) < 0)
1136 maybe_warn("couldn't fchflags: %s", file
);
1140 /* what sort of file is this? */
1141 static enum filetype
1142 file_gettype(u_char
*buf
)
1145 if (buf
[0] == GZIP_MAGIC0
&&
1146 (buf
[1] == GZIP_MAGIC1
|| buf
[1] == GZIP_OMAGIC1
))
1149 #ifndef NO_BZIP2_SUPPORT
1150 if (memcmp(buf
, BZIP2_MAGIC
, 3) == 0 &&
1151 buf
[3] >= '0' && buf
[3] <= '9')
1155 #ifndef NO_COMPRESS_SUPPORT
1156 if (memcmp(buf
, Z_MAGIC
, 2) == 0)
1160 #ifndef NO_PACK_SUPPORT
1161 if (memcmp(buf
, PACK_MAGIC
, 2) == 0)
1165 #ifndef NO_XZ_SUPPORT
1166 if (memcmp(buf
, XZ_MAGIC
, 4) == 0) /* XXX: We only have 4 bytes */
1174 /* check the outfile is OK. */
1176 check_outfile(const char *outfile
)
1181 if (lflag
== 0 && stat(outfile
, &sb
) == 0) {
1184 else if (isatty(STDIN_FILENO
)) {
1185 char ans
[10] = { 'n', '\0' }; /* default */
1187 fprintf(stderr
, "%s already exists -- do you wish to "
1188 "overwrite (y or n)? " , outfile
);
1189 (void)fgets(ans
, sizeof(ans
) - 1, stdin
);
1190 if (ans
[0] != 'y' && ans
[0] != 'Y') {
1191 fprintf(stderr
, "\tnot overwriting\n");
1196 maybe_warnx("%s already exists -- skipping", outfile
);
1204 unlink_input(const char *file
, const struct stat
*sb
)
1210 bzero(&nsb
, sizeof(nsb
));
1211 if (stat(file
, &nsb
) != 0)
1212 /* Must be gone already */
1214 if (nsb
.st_dev
!= sb
->st_dev
|| nsb
.st_ino
!= sb
->st_ino
)
1215 /* Definitely a different file */
1221 sigint_handler(int signo __unused
)
1224 if (remove_file
!= NULL
)
1225 unlink(remove_file
);
1230 static const suffixes_t
*
1231 check_suffix(char *file
, int xlate
)
1233 const suffixes_t
*s
;
1234 int len
= strlen(file
);
1237 for (s
= suffixes
; s
!= suffixes
+ NUM_SUFFIXES
; s
++) {
1238 /* if it doesn't fit in "a.suf", don't bother */
1239 if (s
->ziplen
>= len
)
1241 sp
= file
+ len
- s
->ziplen
;
1242 if (strcmp(s
->zipped
, sp
) != 0)
1245 strcpy(sp
, s
->normal
);
1253 clear_type_and_creator(int fd
)
1255 struct attrlist alist
;
1261 memset(&alist
, 0, sizeof(alist
));
1262 alist
.bitmapcount
= ATTR_BIT_MAP_COUNT
;
1263 alist
.commonattr
= ATTR_CMN_FNDRINFO
;
1265 if (!fgetattrlist(fd
, &alist
, &abuf
, sizeof(abuf
), 0) && abuf
.length
== sizeof(abuf
)) {
1266 memset(abuf
.info
, 0, 8);
1267 fsetattrlist(fd
, &alist
, abuf
.info
, sizeof(abuf
.info
), 0);
1270 #endif /* __APPLE__ */
1273 * compress the given file: create a corresponding .gz file and remove the
1277 file_compress(char *file
, char *outfile
, size_t outsize
)
1283 struct stat isb
, osb
;
1284 const suffixes_t
*suff
;
1287 in
= open(file
, O_RDONLY
);
1289 maybe_warn("can't open %s", file
);
1294 bzero(&isb
, sizeof(isb
));
1295 if (fstat(in
, &isb
) != 0) {
1296 maybe_warn("couldn't stat: %s", file
);
1304 if (isb
.st_nlink
> 1 && fflag
== 0) {
1305 maybe_warnx("%s has %d other link%s -- skipping",
1306 file
, isb
.st_nlink
- 1,
1307 (isb
.st_nlink
- 1) == 1 ? "" : "s");
1312 if (fflag
== 0 && (suff
= check_suffix(file
, 0)) &&
1313 suff
->zipped
[0] != 0) {
1314 maybe_warnx("%s already has %s suffix -- unchanged",
1315 file
, suff
->zipped
);
1321 /* Add (usually) .gz to filename */
1322 if ((size_t)snprintf(outfile
, outsize
, "%s%s",
1323 file
, suffixes
[0].zipped
) >= outsize
)
1324 memcpy(outfile
+ outsize
- suffixes
[0].ziplen
- 1,
1325 suffixes
[0].zipped
, suffixes
[0].ziplen
+ 1);
1328 if (check_outfile(outfile
) == 0) {
1336 out
= open(outfile
, O_WRONLY
| O_CREAT
| O_EXCL
, 0600);
1338 maybe_warn("could not create output: %s", outfile
);
1343 remove_file
= outfile
;
1346 out
= STDOUT_FILENO
;
1348 insize
= gz_compress(in
, out
, &size
, basename(file
), (uint32_t)isb
.st_mtime
);
1352 #endif /* !__APPLE__ */
1355 * If there was an error, insize will be -1.
1356 * If we compressed to stdout, just return the size.
1357 * Otherwise stat the file and check it is the correct size.
1358 * We only blow away the file if we can stat the output and it
1359 * has the expected size.
1362 return (insize
== -1 ? -1 : size
);
1365 if (fstat(out
, &osb
) != 0) {
1366 maybe_warn("couldn't stat: %s", outfile
);
1370 if (osb
.st_size
!= size
) {
1371 maybe_warnx("output file: %s wrong size (%ju != %ju), deleting",
1372 outfile
, (uintmax_t)osb
.st_size
, (uintmax_t)size
);
1377 fcopyfile(in
, out
, 0, COPYFILE_ACL
| COPYFILE_XATTR
);
1378 clear_type_and_creator(out
);
1379 #endif /* __APPLE__ */
1380 copymodes(out
, &isb
, outfile
);
1385 #endif /* __APPLE__ */
1386 if (close(out
) == -1)
1387 maybe_warn("couldn't close output");
1389 /* output is good, ok to delete input */
1390 unlink_input(file
, &isb
);
1395 if (close(out
) == -1)
1396 maybe_warn("couldn't close output");
1398 maybe_warnx("leaving original %s", file
);
1404 /* uncompress the given file and remove the original */
1406 file_uncompress(char *file
, char *outfile
, size_t outsize
)
1408 struct stat isb
, osb
;
1411 unsigned char header1
[4];
1412 enum filetype method
;
1413 int fd
, ofd
, zfd
= -1;
1416 time_t timestamp
= 0;
1417 char name
[PATH_MAX
+ 1];
1420 /* gather the old name info */
1422 fd
= open(file
, O_RDONLY
);
1424 maybe_warn("can't open %s", file
);
1428 strlcpy(outfile
, file
, outsize
);
1429 if (check_suffix(outfile
, 1) == NULL
&& !(cflag
|| lflag
)) {
1430 maybe_warnx("%s: unknown suffix -- ignored", file
);
1434 rbytes
= read(fd
, header1
, sizeof header1
);
1435 if (rbytes
!= sizeof header1
) {
1436 /* we don't want to fail here. */
1442 maybe_warn("can't read %s", file
);
1444 goto unexpected_EOF
;
1448 method
= file_gettype(header1
);
1450 if (fflag
== 0 && method
== FT_UNKNOWN
) {
1451 maybe_warnx("%s: not in gzip format", file
);
1458 if (method
== FT_GZIP
&& Nflag
) {
1459 unsigned char ts
[4]; /* timestamp */
1461 rv
= pread(fd
, ts
, sizeof ts
, GZIP_TIMESTAMP
);
1462 if (rv
>= 0 && rv
< (ssize_t
)(sizeof ts
))
1463 goto unexpected_EOF
;
1466 maybe_warn("can't read %s", file
);
1469 timestamp
= ts
[3] << 24 | ts
[2] << 16 | ts
[1] << 8 | ts
[0];
1471 if (header1
[3] & ORIG_NAME
) {
1472 rbytes
= pread(fd
, name
, sizeof(name
) - 1, GZIP_ORIGNAME
);
1474 maybe_warn("can't read %s", file
);
1477 if (name
[0] != '\0') {
1480 /* Make sure that name is NUL-terminated */
1481 name
[rbytes
] = '\0';
1483 /* strip saved directory name */
1484 nf
= strrchr(name
, '/');
1490 /* preserve original directory name */
1491 dp
= strrchr(file
, '/');
1496 snprintf(outfile
, outsize
, "%.*s%.*s",
1498 file
, (int) rbytes
, nf
);
1503 lseek(fd
, 0, SEEK_SET
);
1504 bzero(&isb
, sizeof(isb
));
1505 if (cflag
== 0 || lflag
) {
1506 if (fstat(fd
, &isb
) != 0)
1509 if (isb
.st_nlink
> 1 && lflag
== 0 && fflag
== 0) {
1510 maybe_warnx("%s has %d other links -- skipping",
1511 file
, isb
.st_nlink
- 1);
1514 if (nflag
== 0 && timestamp
)
1515 isb
.st_mtime
= timestamp
;
1516 if (check_outfile(outfile
) == 0)
1521 if (cflag
== 0 && lflag
== 0) {
1522 zfd
= open(outfile
, O_WRONLY
|O_CREAT
|O_EXCL
, 0600);
1523 if (zfd
== STDOUT_FILENO
) {
1524 /* We won't close STDOUT_FILENO later... */
1526 close(STDOUT_FILENO
);
1529 maybe_warn("can't open %s", outfile
);
1533 remove_file
= outfile
;
1536 zfd
= STDOUT_FILENO
;
1539 #ifndef NO_BZIP2_SUPPORT
1543 maybe_warnx("no -l with bzip2 files");
1547 size
= unbzip2(fd
, zfd
, NULL
, 0, NULL
);
1551 #ifndef NO_COMPRESS_SUPPORT
1557 maybe_warnx("no -l with Lempel-Ziv files");
1561 if ((in
= zdopen(fd
)) == NULL
) {
1562 maybe_warn("zdopen for read: %s", file
);
1566 out
= fdopen(dup(zfd
), "w");
1568 maybe_warn("fdopen for write: %s", outfile
);
1573 size
= zuncompress(in
, out
, NULL
, 0, NULL
);
1574 /* need to fclose() if ferror() is true... */
1575 if (ferror(in
) | fclose(in
)) {
1576 maybe_warn("failed infile fclose");
1580 if (fclose(out
) != 0) {
1581 maybe_warn("failed outfile fclose");
1589 #ifndef NO_PACK_SUPPORT
1592 maybe_warnx("no -l with packed files");
1596 size
= unpack(fd
, zfd
, NULL
, 0, NULL
);
1600 #ifndef NO_XZ_SUPPORT
1603 maybe_warnx("no -l with xz files");
1607 size
= unxz(fd
, zfd
, NULL
, 0, NULL
);
1614 maybe_warnx("no -l for unknown filetypes");
1617 size
= cat_fd(NULL
, 0, NULL
, fd
);
1622 print_list(fd
, isb
.st_size
, outfile
, isb
.st_mtime
);
1624 return -1; /* XXX */
1627 size
= gz_uncompress(fd
, zfd
, NULL
, 0, NULL
, file
);
1632 maybe_warn("couldn't close input");
1633 if (zfd
!= STDOUT_FILENO
&& close(zfd
) != 0)
1634 maybe_warn("couldn't close output");
1639 maybe_warnx("%s: uncompress failed", file
);
1643 /* if testing, or we uncompressed to stdout, this is all we need */
1648 /* if we are uncompressing to stdin, don't remove the file. */
1653 * if we create a file...
1656 * if we can't stat the file don't remove the file.
1659 ofd
= open(outfile
, O_RDWR
, 0);
1661 maybe_warn("couldn't open (leaving original): %s",
1665 if (fstat(ofd
, &osb
) != 0) {
1666 maybe_warn("couldn't stat (leaving original): %s",
1671 if (osb
.st_size
!= size
) {
1672 maybe_warnx("stat gave different size: %ju != %ju (leaving original)",
1673 (uintmax_t)size
, (uintmax_t)osb
.st_size
);
1679 copymodes(ofd
, &isb
, outfile
);
1683 unlink_input(file
, &isb
);
1687 maybe_warnx("%s: unexpected end of file", file
);
1691 if (zfd
!= -1 && zfd
!= STDOUT_FILENO
)
1698 cat_fd(unsigned char * prepend
, size_t count
, off_t
*gsizep
, int fd
)
1705 w
= write(STDOUT_FILENO
, prepend
, count
);
1706 if (w
== -1 || (size_t)w
!= count
) {
1707 maybe_warn("write to stdout");
1713 rv
= read(fd
, buf
, sizeof buf
);
1717 maybe_warn("read from fd %d", fd
);
1721 if (write(STDOUT_FILENO
, buf
, rv
) != rv
) {
1722 maybe_warn("write to stdout");
1737 unsigned char header1
[4];
1739 enum filetype method
;
1741 #ifndef NO_COMPRESS_SUPPORT
1746 if (fflag
== 0 && lflag
== 0 && isatty(STDIN_FILENO
)) {
1747 maybe_warnx("standard input is a terminal -- ignoring");
1755 /* XXX could read the whole file, etc. */
1756 if (fstat(STDIN_FILENO
, &isb
) < 0) {
1757 maybe_warn("fstat");
1760 print_list(STDIN_FILENO
, isb
.st_size
, "stdout", isb
.st_mtime
);
1764 bytes_read
= read_retry(STDIN_FILENO
, header1
, sizeof header1
);
1765 if (bytes_read
== -1) {
1766 maybe_warn("can't read stdin");
1768 } else if (bytes_read
!= sizeof(header1
)) {
1769 maybe_warnx("(stdin): unexpected end of file");
1773 method
= file_gettype(header1
);
1778 maybe_warnx("unknown compression format");
1781 usize
= cat_fd(header1
, sizeof header1
, &gsize
, STDIN_FILENO
);
1785 usize
= gz_uncompress(STDIN_FILENO
, STDOUT_FILENO
,
1786 (char *)header1
, sizeof header1
, &gsize
, "(stdin)");
1788 #ifndef NO_BZIP2_SUPPORT
1790 usize
= unbzip2(STDIN_FILENO
, STDOUT_FILENO
,
1791 (char *)header1
, sizeof header1
, &gsize
);
1794 #ifndef NO_COMPRESS_SUPPORT
1796 if ((in
= zdopen(STDIN_FILENO
)) == NULL
) {
1797 maybe_warnx("zopen of stdin");
1801 usize
= zuncompress(in
, stdout
, (char *)header1
,
1802 sizeof header1
, &gsize
);
1806 #ifndef NO_PACK_SUPPORT
1808 usize
= unpack(STDIN_FILENO
, STDOUT_FILENO
,
1809 (char *)header1
, sizeof header1
, &gsize
);
1812 #ifndef NO_XZ_SUPPORT
1814 usize
= unxz(STDIN_FILENO
, STDOUT_FILENO
,
1815 (char *)header1
, sizeof header1
, &gsize
);
1821 if (vflag
&& !tflag
&& usize
!= -1 && gsize
!= -1)
1822 print_verbage(NULL
, NULL
, usize
, gsize
);
1824 print_test("(stdin)", usize
!= -1);
1839 if (fflag
== 0 && isatty(STDOUT_FILENO
)) {
1840 maybe_warnx("standard output is a terminal -- ignoring");
1844 /* If stdin is a file use its mtime, otherwise use current time */
1845 ret
= fstat(STDIN_FILENO
, &sb
);
1849 maybe_warn("Can't stat stdin");
1854 if (S_ISREG(sb
.st_mode
))
1855 mtime
= (uint32_t)sb
.st_mtime
;
1857 systime
= time(NULL
);
1859 if (systime
== -1) {
1864 mtime
= (uint32_t)systime
;
1867 usize
= gz_compress(STDIN_FILENO
, STDOUT_FILENO
, &gsize
, "", mtime
);
1869 if (vflag
&& !tflag
&& usize
!= -1 && gsize
!= -1)
1870 print_verbage(NULL
, NULL
, usize
, gsize
);
1874 /* do what is asked for, for the path name */
1876 handle_pathname(char *path
)
1878 char *opath
= path
, *s
= NULL
;
1883 /* check for stdout/stdin */
1884 if (path
[0] == '-' && path
[1] == '\0') {
1893 if (zcat
&& COMPAT_MODE("bin/zcat", "Unix2003")) {
1894 char *suffix
= strrchr(path
, '.');
1895 if (suffix
== NULL
|| strcmp(suffix
, Z_SUFFIX
) != 0) {
1897 slen
= sizeof(Z_SUFFIX
) - 1;
1898 s
= malloc(len
+ slen
+ 1);
1899 memcpy(s
, path
, len
);
1900 memcpy(s
+ len
, Z_SUFFIX
, slen
+ 1);
1907 if (stat(path
, &sb
) != 0 || (fflag
== 0 && cflag
== 0 &&
1908 lstat(path
, &sb
) != 0)) {
1909 /* lets try <path>.gz if we're decompressing */
1910 if (dflag
&& s
== NULL
&& errno
== ENOENT
) {
1912 slen
= suffixes
[0].ziplen
;
1913 s
= malloc(len
+ slen
+ 1);
1915 maybe_err("malloc");
1916 memcpy(s
, path
, len
);
1917 memcpy(s
+ len
, suffixes
[0].zipped
, slen
+ 1);
1922 /* Include actual path for clarity. */
1923 maybe_warn("can't stat: %s (%s)", opath
, path
);
1925 maybe_warn("can't stat: %s", opath
);
1930 if (S_ISDIR(sb
.st_mode
)) {
1936 maybe_warnx("%s is a directory", path
);
1940 if (S_ISREG(sb
.st_mode
))
1941 handle_file(path
, &sb
);
1943 maybe_warnx("%s is not a regular file", path
);
1950 /* compress/decompress a file */
1952 handle_file(char *file
, struct stat
*sbp
)
1955 char outfile
[PATH_MAX
];
1959 usize
= file_uncompress(file
, outfile
, sizeof(outfile
));
1962 print_test(file
, usize
!= -1);
1966 gsize
= sbp
->st_size
;
1968 gsize
= file_compress(file
, outfile
, sizeof(outfile
));
1971 usize
= sbp
->st_size
;
1976 if (vflag
&& !tflag
)
1977 print_verbage(file
, (cflag
) ? NULL
: outfile
, usize
, gsize
);
1982 /* this is used with -r to recursively descend directories */
1984 handle_dir(char *dir
)
1992 fts
= fts_open(path_argv
, FTS_PHYSICAL
| FTS_NOCHDIR
, NULL
);
1994 warn("couldn't fts_open %s", dir
);
1998 while ((entry
= fts_read(fts
))) {
1999 switch(entry
->fts_info
) {
2007 maybe_warn("%s", entry
->fts_path
);
2010 handle_file(entry
->fts_path
, entry
->fts_statp
);
2013 (void)fts_close(fts
);
2017 /* print a ratio - size reduction as a fraction of uncompressed size */
2019 print_ratio(off_t in
, off_t out
, FILE *where
)
2021 int percent10
; /* 10 * percent */
2029 * Output is more than double size of input! print -99.9%
2030 * Quite possibly we've failed to get the original size.
2035 * We only need 12 bits of result from the final division,
2036 * so reduce the values until a 32bit division will suffice.
2038 while (in
> 0x100000) {
2043 percent10
= ((u_int
)diff
* 2000) / (u_int
)in
- 1000;
2048 len
= snprintf(buff
, sizeof buff
, "%2.2d.", percent10
);
2049 /* Move the '.' to before the last digit */
2050 buff
[len
- 1] = buff
[len
- 2];
2051 buff
[len
- 2] = '.';
2052 fprintf(where
, "%5s%%", buff
);
2056 /* print compression statistics, and the new name (if there is one!) */
2058 print_verbage(const char *file
, const char *nfile
, off_t usize
, off_t gsize
)
2061 fprintf(stderr
, "%s:%s ", file
,
2062 strlen(file
) < 7 ? "\t\t" : "\t");
2063 print_ratio(usize
, gsize
, stderr
);
2065 fprintf(stderr
, " -- replaced with %s", nfile
);
2066 fprintf(stderr
, "\n");
2070 /* print test results */
2072 print_test(const char *file
, int ok
)
2075 if (exit_value
== 0 && ok
== 0)
2077 fprintf(stderr
, "%s:%s %s\n", file
,
2078 strlen(file
) < 7 ? "\t\t" : "\t", ok
? "OK" : "NOT OK");
2083 /* print a file's info ala --list */
2085 compressed uncompressed ratio uncompressed_name
2086 354841 1679360 78.8% /usr/pkgsrc/distfiles/libglade-2.0.1.tar
2089 print_list(int fd
, off_t out
, const char *outfile
, time_t ts
)
2091 static int first
= 1;
2093 static off_t in_tot
, out_tot
;
2101 printf("method crc date time ");
2104 printf(" compressed uncompressed "
2105 "ratio uncompressed_name\n");
2117 /* read the last 4 bytes - this is the uncompressed size */
2118 rv
= lseek(fd
, (off_t
)(-8), SEEK_END
);
2120 unsigned char buf
[8];
2123 rv
= read(fd
, (char *)buf
, sizeof(buf
));
2125 maybe_warn("read of uncompressed size");
2126 else if (rv
!= sizeof(buf
))
2127 maybe_warnx("read of uncompressed size");
2130 usize
= buf
[4] | buf
[5] << 8 |
2131 buf
[6] << 16 | buf
[7] << 24;
2134 crc
= buf
[0] | buf
[1] << 8 |
2135 buf
[2] << 16 | buf
[3] << 24;
2142 if (vflag
&& fd
== -1)
2145 char *date
= ctime(&ts
);
2147 /* skip the day, 1/100th second, and year */
2150 printf("%5s %08x %11s ", "defla"/*XXX*/, crc
, date
);
2155 (void)&ts
; /* XXX */
2157 printf("%12llu %12llu ", (unsigned long long)out
, (unsigned long long)in
);
2158 print_ratio(in
, out
, stdout
);
2159 printf(" %s\n", outfile
);
2162 /* display the usage of NetBSD gzip */
2167 fprintf(stderr
, "%s\n", gzip_version
);
2170 "usage: %s [-" OPT_LIST
"] [<file> [<file> ...]]\n",
2172 "usage: %s [-123456789acdfhklLNnqrtVv] [-S .suffix] [<file> [<file> ...]]\n"
2173 " -1 --fast fastest (worst) compression\n"
2174 " -2 .. -8 set compression level\n"
2175 " -9 --best best (slowest) compression\n"
2176 " -c --stdout write to stdout, keep original files\n"
2178 " -d --decompress uncompress files\n"
2180 " -f --force force overwriting & compress links\n"
2181 " -h --help display this help\n"
2182 " -k --keep don't delete input files during operation\n"
2183 " -l --list list compressed file contents\n"
2184 " -N --name save or restore original file name and time stamp\n"
2185 " -n --no-name don't save original file name or time stamp\n"
2186 " -q --quiet output no warnings\n"
2187 " -r --recursive recursively compress files in directories\n"
2188 " -S .suf use suffix .suf instead of .gz\n"
2190 " -t --test test compressed file\n"
2191 " -V --version display program version\n"
2192 " -v --verbose print extra statistics\n",
2199 /* display the license information of FreeBSD gzip */
2201 display_license(void)
2205 fprintf(stderr
, "%s (based on FreeBSD gzip 20150113)\n", gzip_version
);
2207 fprintf(stderr
, "%s (based on NetBSD gzip 20150113)\n", gzip_version
);
2209 fprintf(stderr
, "%s\n", gzip_copyright
);
2214 /* display the version of NetBSD gzip */
2216 display_version(void)
2219 fprintf(stderr
, "%s\n", gzip_version
);
2223 #ifndef NO_BZIP2_SUPPORT
2224 #include "unbzip2.c"
2226 #ifndef NO_COMPRESS_SUPPORT
2227 #include "zuncompress.c"
2229 #ifndef NO_PACK_SUPPORT
2232 #ifndef NO_XZ_SUPPORT
2237 read_retry(int fd
, void *buf
, size_t sz
)
2240 size_t left
= MIN(sz
, (size_t) SSIZE_MAX
);
2245 ret
= read(fd
, cp
, left
);
2248 } else if (ret
== 0) {