gzip.c (7fa59e671884d8461af2f6a871058bf9c8bc113d) gzip.c (90f528e8d7868a5d31dc35b24943c673bf67821d)
1/* $NetBSD: gzip.c,v 1.109 2015/10/27 07:36:18 mrg Exp $ */
1/* $NetBSD: gzip.c,v 1.112 2017/08/23 13:04:17 christos Exp $ */
2
3/*-
2
3/*-
4 * Copyright (c) 1997, 1998, 2003, 2004, 2006 Matthew R. Green
4 * Copyright (c) 1997, 1998, 2003, 2004, 2006, 2008, 2009, 2010, 2011, 2015, 2017
5 * Matthew R. Green
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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

--- 11 unchanged lines hidden (view full) ---

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
26 * SUCH DAMAGE.
27 *
28 */
29
30#include <sys/cdefs.h>
31#ifndef lint
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright

--- 11 unchanged lines hidden (view full) ---

25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 */
30
31#include <sys/cdefs.h>
32#ifndef lint
32__COPYRIGHT("@(#) Copyright (c) 1997, 1998, 2003, 2004, 2006\
33 Matthew R. Green. All rights reserved.");
33__COPYRIGHT("@(#) Copyright (c) 1997, 1998, 2003, 2004, 2006, 2008,\
34 2009, 2010, 2011, 2015, 2017 Matthew R. Green. All rights reserved.");
34__FBSDID("$FreeBSD$");
35#endif /* not lint */
36
37/*
38 * gzip.c -- GPL free gzip using zlib.
39 *
40 * RFC 1950 covers the zlib format
41 * RFC 1951 covers the deflate format

--- 111 unchanged lines hidden (view full) ---

153#endif
154 SUFFIX(GZ_SUFFIX, ""), /* Overwritten by -S "" */
155#endif /* SMALL */
156#undef SUFFIX
157};
158#define NUM_SUFFIXES (nitems(suffixes))
159#define SUFFIX_MAXLEN 30
160
35__FBSDID("$FreeBSD$");
36#endif /* not lint */
37
38/*
39 * gzip.c -- GPL free gzip using zlib.
40 *
41 * RFC 1950 covers the zlib format
42 * RFC 1951 covers the deflate format

--- 111 unchanged lines hidden (view full) ---

154#endif
155 SUFFIX(GZ_SUFFIX, ""), /* Overwritten by -S "" */
156#endif /* SMALL */
157#undef SUFFIX
158};
159#define NUM_SUFFIXES (nitems(suffixes))
160#define SUFFIX_MAXLEN 30
161
161static const char gzip_version[] = "FreeBSD gzip 20150413";
162static const char gzip_version[] = "FreeBSD gzip 20171121";
162
163#ifndef SMALL
164static const char gzip_copyright[] = \
165" Copyright (c) 1997, 1998, 2003, 2004, 2006 Matthew R. Green\n"
166" All rights reserved.\n"
167"\n"
168" Redistribution and use in source and binary forms, with or without\n"
169" modification, are permitted provided that the following conditions\n"

--- 17 unchanged lines hidden (view full) ---

187" SUCH DAMAGE.";
188#endif
189
190static int cflag; /* stdout mode */
191static int dflag; /* decompress mode */
192static int lflag; /* list mode */
193static int numflag = 6; /* gzip -1..-9 value */
194
163
164#ifndef SMALL
165static const char gzip_copyright[] = \
166" Copyright (c) 1997, 1998, 2003, 2004, 2006 Matthew R. Green\n"
167" All rights reserved.\n"
168"\n"
169" Redistribution and use in source and binary forms, with or without\n"
170" modification, are permitted provided that the following conditions\n"

--- 17 unchanged lines hidden (view full) ---

188" SUCH DAMAGE.";
189#endif
190
191static int cflag; /* stdout mode */
192static int dflag; /* decompress mode */
193static int lflag; /* list mode */
194static int numflag = 6; /* gzip -1..-9 value */
195
195#ifndef SMALL
196static const char *remove_file = NULL; /* file to be removed upon SIGINT */
197
196static int fflag; /* force mode */
198static int fflag; /* force mode */
199#ifndef SMALL
197static int kflag; /* don't delete input files */
198static int nflag; /* don't save name/timestamp */
199static int Nflag; /* don't restore name/timestamp */
200static int qflag; /* quiet mode */
201static int rflag; /* recursive mode */
202static int tflag; /* test */
203static int vflag; /* verbose mode */
200static int kflag; /* don't delete input files */
201static int nflag; /* don't save name/timestamp */
202static int Nflag; /* don't restore name/timestamp */
203static int qflag; /* quiet mode */
204static int rflag; /* recursive mode */
205static int tflag; /* test */
206static int vflag; /* verbose mode */
204static const char *remove_file = NULL; /* file to be removed upon SIGINT */
207static sig_atomic_t print_info = 0;
205#else
206#define qflag 0
207#define tflag 0
208#endif
209
210static int exit_value = 0; /* exit value */
211
208#else
209#define qflag 0
210#define tflag 0
211#endif
212
213static int exit_value = 0; /* exit value */
214
212static char *infile; /* name of file coming in */
215static const char *infile; /* name of file coming in */
213
214static void maybe_err(const char *fmt, ...) __printflike(1, 2) __dead2;
215#if !defined(NO_BZIP2_SUPPORT) || !defined(NO_PACK_SUPPORT) || \
216 !defined(NO_XZ_SUPPORT)
217static void maybe_errx(const char *fmt, ...) __printflike(1, 2) __dead2;
218#endif
219static void maybe_warn(const char *fmt, ...) __printflike(1, 2);
220static void maybe_warnx(const char *fmt, ...) __printflike(1, 2);

--- 10 unchanged lines hidden (view full) ---

231static void handle_stdin(void);
232static void handle_stdout(void);
233static void print_ratio(off_t, off_t, FILE *);
234static void print_list(int fd, off_t, const char *, time_t);
235static void usage(void) __dead2;
236static void display_version(void) __dead2;
237#ifndef SMALL
238static void display_license(void);
216
217static void maybe_err(const char *fmt, ...) __printflike(1, 2) __dead2;
218#if !defined(NO_BZIP2_SUPPORT) || !defined(NO_PACK_SUPPORT) || \
219 !defined(NO_XZ_SUPPORT)
220static void maybe_errx(const char *fmt, ...) __printflike(1, 2) __dead2;
221#endif
222static void maybe_warn(const char *fmt, ...) __printflike(1, 2);
223static void maybe_warnx(const char *fmt, ...) __printflike(1, 2);

--- 10 unchanged lines hidden (view full) ---

234static void handle_stdin(void);
235static void handle_stdout(void);
236static void print_ratio(off_t, off_t, FILE *);
237static void print_list(int fd, off_t, const char *, time_t);
238static void usage(void) __dead2;
239static void display_version(void) __dead2;
240#ifndef SMALL
241static void display_license(void);
239static void sigint_handler(int);
240#endif
241static const suffixes_t *check_suffix(char *, int);
242static ssize_t read_retry(int, void *, size_t);
242#endif
243static const suffixes_t *check_suffix(char *, int);
244static ssize_t read_retry(int, void *, size_t);
245static ssize_t write_retry(int, const void *, size_t);
243
244#ifdef SMALL
246
247#ifdef SMALL
248#define infile_set(f,t) infile_set(f)
249#endif
250static void infile_set(const char *newinfile, off_t total);
251
252#ifdef SMALL
245#define unlink_input(f, sb) unlink(f)
253#define unlink_input(f, sb) unlink(f)
254#define check_siginfo() /* nothing */
255#define setup_signals() /* nothing */
256#define infile_newdata(t) /* nothing */
246#else
257#else
258static off_t infile_total; /* total expected to read/write */
259static off_t infile_current; /* current read/write */
260
261static void check_siginfo(void);
247static off_t cat_fd(unsigned char *, size_t, off_t *, int fd);
248static void prepend_gzip(char *, int *, char ***);
249static void handle_dir(char *);
250static void print_verbage(const char *, const char *, off_t, off_t);
251static void print_test(const char *, int);
252static void copymodes(int fd, const struct stat *, const char *file);
253static int check_outfile(const char *outfile);
262static off_t cat_fd(unsigned char *, size_t, off_t *, int fd);
263static void prepend_gzip(char *, int *, char ***);
264static void handle_dir(char *);
265static void print_verbage(const char *, const char *, off_t, off_t);
266static void print_test(const char *, int);
267static void copymodes(int fd, const struct stat *, const char *file);
268static int check_outfile(const char *outfile);
269static void setup_signals(void);
270static void infile_newdata(size_t newdata);
271static void infile_clear(void);
254#endif
255
256#ifndef NO_BZIP2_SUPPORT
257static off_t unbzip2(int, int, char *, size_t, off_t *);
258#endif
259
260#ifndef NO_COMPRESS_SUPPORT
261static FILE *zdopen(int);

--- 41 unchanged lines hidden (view full) ---

303{
304 const char *progname = getprogname();
305#ifndef SMALL
306 char *gzip;
307 int len;
308#endif
309 int ch;
310
272#endif
273
274#ifndef NO_BZIP2_SUPPORT
275static off_t unbzip2(int, int, char *, size_t, off_t *);
276#endif
277
278#ifndef NO_COMPRESS_SUPPORT
279static FILE *zdopen(int);

--- 41 unchanged lines hidden (view full) ---

321{
322 const char *progname = getprogname();
323#ifndef SMALL
324 char *gzip;
325 int len;
326#endif
327 int ch;
328
329 setup_signals();
330
311#ifndef SMALL
312 if ((gzip = getenv("GZIP")) != NULL)
313 prepend_gzip(gzip, &argc, &argv);
331#ifndef SMALL
332 if ((gzip = getenv("GZIP")) != NULL)
333 prepend_gzip(gzip, &argc, &argv);
314 signal(SIGINT, sigint_handler);
315#endif
316
317 /*
318 * XXX
319 * handle being called `gunzip', `zcat' and `gzcat'
320 */
321 if (strcmp(progname, "gunzip") == 0)
322 dflag = 1;

--- 259 unchanged lines hidden (view full) ---

582 memcpy(outbufp, header, sizeof header);
583 i = sizeof header;
584#else
585 if (nflag != 0) {
586 mtime = 0;
587 origname = "";
588 }
589
334#endif
335
336 /*
337 * XXX
338 * handle being called `gunzip', `zcat' and `gzcat'
339 */
340 if (strcmp(progname, "gunzip") == 0)
341 dflag = 1;

--- 259 unchanged lines hidden (view full) ---

601 memcpy(outbufp, header, sizeof header);
602 i = sizeof header;
603#else
604 if (nflag != 0) {
605 mtime = 0;
606 origname = "";
607 }
608
590 i = snprintf(outbufp, BUFLEN, "%c%c%c%c%c%c%c%c%c%c%s",
609 i = snprintf(outbufp, BUFLEN, "%c%c%c%c%c%c%c%c%c%c%s",
591 GZIP_MAGIC0, GZIP_MAGIC1, Z_DEFLATED,
592 *origname ? ORIG_NAME : 0,
593 mtime & 0xff,
594 (mtime >> 8) & 0xff,
595 (mtime >> 16) & 0xff,
596 (mtime >> 24) & 0xff,
597 numflag == 1 ? 4 : numflag == 9 ? 2 : 0,
598 OS_CODE, origname);
610 GZIP_MAGIC0, GZIP_MAGIC1, Z_DEFLATED,
611 *origname ? ORIG_NAME : 0,
612 mtime & 0xff,
613 (mtime >> 8) & 0xff,
614 (mtime >> 16) & 0xff,
615 (mtime >> 24) & 0xff,
616 numflag == 1 ? 4 : numflag == 9 ? 2 : 0,
617 OS_CODE, origname);
599 if (i >= BUFLEN)
618 if (i >= BUFLEN)
600 /* this need PATH_MAX > BUFLEN ... */
601 maybe_err("snprintf");
602 if (*origname)
603 i++;
604#endif
605
606 z.next_out = (unsigned char *)outbufp + i;
607 z.avail_out = BUFLEN - i;

--- 4 unchanged lines hidden (view full) ---

612 maybe_warnx("deflateInit2 failed");
613 in_tot = -1;
614 goto out;
615 }
616
617 crc = crc32(0L, Z_NULL, 0);
618 for (;;) {
619 if (z.avail_out == 0) {
619 /* this need PATH_MAX > BUFLEN ... */
620 maybe_err("snprintf");
621 if (*origname)
622 i++;
623#endif
624
625 z.next_out = (unsigned char *)outbufp + i;
626 z.avail_out = BUFLEN - i;

--- 4 unchanged lines hidden (view full) ---

631 maybe_warnx("deflateInit2 failed");
632 in_tot = -1;
633 goto out;
634 }
635
636 crc = crc32(0L, Z_NULL, 0);
637 for (;;) {
638 if (z.avail_out == 0) {
620 if (write(out, outbufp, BUFLEN) != BUFLEN) {
639 if (write_retry(out, outbufp, BUFLEN) != BUFLEN) {
621 maybe_warn("write");
622 out_tot = -1;
623 goto out;
624 }
625
626 out_tot += BUFLEN;
627 z.next_out = (unsigned char *)outbufp;
628 z.avail_out = BUFLEN;
629 }
630
631 if (z.avail_in == 0) {
632 in_size = read(in, inbufp, BUFLEN);
633 if (in_size < 0) {
634 maybe_warn("read");
635 in_tot = -1;
636 goto out;
637 }
638 if (in_size == 0)
639 break;
640 maybe_warn("write");
641 out_tot = -1;
642 goto out;
643 }
644
645 out_tot += BUFLEN;
646 z.next_out = (unsigned char *)outbufp;
647 z.avail_out = BUFLEN;
648 }
649
650 if (z.avail_in == 0) {
651 in_size = read(in, inbufp, BUFLEN);
652 if (in_size < 0) {
653 maybe_warn("read");
654 in_tot = -1;
655 goto out;
656 }
657 if (in_size == 0)
658 break;
659 infile_newdata(in_size);
640
641 crc = crc32(crc, (const Bytef *)inbufp, (unsigned)in_size);
642 in_tot += in_size;
643 z.next_in = (unsigned char *)inbufp;
644 z.avail_in = in_size;
645 }
646
647 error = deflate(&z, Z_NO_FLUSH);

--- 13 unchanged lines hidden (view full) ---

661 if (error != Z_OK && error != Z_STREAM_END) {
662 maybe_warnx("deflate failed");
663 in_tot = -1;
664 goto out;
665 }
666
667 len = (char *)z.next_out - outbufp;
668
660
661 crc = crc32(crc, (const Bytef *)inbufp, (unsigned)in_size);
662 in_tot += in_size;
663 z.next_in = (unsigned char *)inbufp;
664 z.avail_in = in_size;
665 }
666
667 error = deflate(&z, Z_NO_FLUSH);

--- 13 unchanged lines hidden (view full) ---

681 if (error != Z_OK && error != Z_STREAM_END) {
682 maybe_warnx("deflate failed");
683 in_tot = -1;
684 goto out;
685 }
686
687 len = (char *)z.next_out - outbufp;
688
669 w = write(out, outbufp, len);
689 w = write_retry(out, outbufp, len);
670 if (w == -1 || (size_t)w != len) {
671 maybe_warn("write");
672 out_tot = -1;
673 goto out;
674 }
675 out_tot += len;
676 z.next_out = (unsigned char *)outbufp;
677 z.avail_out = BUFLEN;
678
679 if (error == Z_STREAM_END)
680 break;
681 }
682
683 if (deflateEnd(&z) != Z_OK) {
684 maybe_warnx("deflateEnd failed");
685 in_tot = -1;
686 goto out;
687 }
688
690 if (w == -1 || (size_t)w != len) {
691 maybe_warn("write");
692 out_tot = -1;
693 goto out;
694 }
695 out_tot += len;
696 z.next_out = (unsigned char *)outbufp;
697 z.avail_out = BUFLEN;
698
699 if (error == Z_STREAM_END)
700 break;
701 }
702
703 if (deflateEnd(&z) != Z_OK) {
704 maybe_warnx("deflateEnd failed");
705 in_tot = -1;
706 goto out;
707 }
708
689 i = snprintf(outbufp, BUFLEN, "%c%c%c%c%c%c%c%c",
709 i = snprintf(outbufp, BUFLEN, "%c%c%c%c%c%c%c%c",
690 (int)crc & 0xff,
691 (int)(crc >> 8) & 0xff,
692 (int)(crc >> 16) & 0xff,
693 (int)(crc >> 24) & 0xff,
694 (int)in_tot & 0xff,
695 (int)(in_tot >> 8) & 0xff,
696 (int)(in_tot >> 16) & 0xff,
697 (int)(in_tot >> 24) & 0xff);
698 if (i != 8)
699 maybe_err("snprintf");
710 (int)crc & 0xff,
711 (int)(crc >> 8) & 0xff,
712 (int)(crc >> 16) & 0xff,
713 (int)(crc >> 24) & 0xff,
714 (int)in_tot & 0xff,
715 (int)(in_tot >> 8) & 0xff,
716 (int)(in_tot >> 16) & 0xff,
717 (int)(in_tot >> 24) & 0xff);
718 if (i != 8)
719 maybe_err("snprintf");
700 if (write(out, outbufp, i) != i) {
720 if (write_retry(out, outbufp, i) != i) {
701 maybe_warn("write");
702 in_tot = -1;
703 } else
704 out_tot += i;
705
706out:
707 if (inbufp != NULL)
708 free(inbufp);

--- 60 unchanged lines hidden (view full) ---

769 z.zalloc = NULL;
770 z.zfree = NULL;
771 z.opaque = 0;
772
773 in_tot = prelen;
774 out_tot = 0;
775
776 for (;;) {
721 maybe_warn("write");
722 in_tot = -1;
723 } else
724 out_tot += i;
725
726out:
727 if (inbufp != NULL)
728 free(inbufp);

--- 60 unchanged lines hidden (view full) ---

789 z.zalloc = NULL;
790 z.zfree = NULL;
791 z.opaque = 0;
792
793 in_tot = prelen;
794 out_tot = 0;
795
796 for (;;) {
797 check_siginfo();
777 if ((z.avail_in == 0 || needmore) && done_reading == 0) {
778 ssize_t in_size;
779
780 if (z.avail_in > 0) {
781 memmove(inbufp, z.next_in, z.avail_in);
782 }
783 z.next_in = (unsigned char *)inbufp;
784 in_size = read(in, z.next_in + z.avail_in,
785 BUFLEN - z.avail_in);
786
787 if (in_size == -1) {
788 maybe_warn("failed to read stdin");
789 goto stop_and_fail;
790 } else if (in_size == 0) {
791 done_reading = 1;
792 }
798 if ((z.avail_in == 0 || needmore) && done_reading == 0) {
799 ssize_t in_size;
800
801 if (z.avail_in > 0) {
802 memmove(inbufp, z.next_in, z.avail_in);
803 }
804 z.next_in = (unsigned char *)inbufp;
805 in_size = read(in, z.next_in + z.avail_in,
806 BUFLEN - z.avail_in);
807
808 if (in_size == -1) {
809 maybe_warn("failed to read stdin");
810 goto stop_and_fail;
811 } else if (in_size == 0) {
812 done_reading = 1;
813 }
814 infile_newdata(in_size);
793
794 z.avail_in += in_size;
795 needmore = 0;
796
797 in_tot += in_size;
798 }
799 if (z.avail_in == 0) {
800 if (done_reading && state != GZSTATE_MAGIC0) {

--- 156 unchanged lines hidden (view full) ---

957
958 if (wr != 0) {
959 crc = crc32(crc, (const Bytef *)outbufp, (unsigned)wr);
960 if (
961#ifndef SMALL
962 /* don't write anything with -t */
963 tflag == 0 &&
964#endif
815
816 z.avail_in += in_size;
817 needmore = 0;
818
819 in_tot += in_size;
820 }
821 if (z.avail_in == 0) {
822 if (done_reading && state != GZSTATE_MAGIC0) {

--- 156 unchanged lines hidden (view full) ---

979
980 if (wr != 0) {
981 crc = crc32(crc, (const Bytef *)outbufp, (unsigned)wr);
982 if (
983#ifndef SMALL
984 /* don't write anything with -t */
985 tflag == 0 &&
986#endif
965 write(out, outbufp, wr) != wr) {
987 write_retry(out, outbufp, wr) != wr) {
966 maybe_warn("error writing to output");
967 goto stop_and_fail;
968 }
969
970 out_tot += wr;
971 out_sub_tot += wr;
972 }
973

--- 104 unchanged lines hidden (view full) ---

1078 * If we have no info on the input, give this file some
1079 * default values and return..
1080 */
1081 if (sbp == NULL) {
1082 mode_t mask = umask(022);
1083
1084 (void)fchmod(fd, DEFFILEMODE & ~mask);
1085 (void)umask(mask);
988 maybe_warn("error writing to output");
989 goto stop_and_fail;
990 }
991
992 out_tot += wr;
993 out_sub_tot += wr;
994 }
995

--- 104 unchanged lines hidden (view full) ---

1100 * If we have no info on the input, give this file some
1101 * default values and return..
1102 */
1103 if (sbp == NULL) {
1104 mode_t mask = umask(022);
1105
1106 (void)fchmod(fd, DEFFILEMODE & ~mask);
1107 (void)umask(mask);
1086 return;
1108 return;
1087 }
1088 sb = *sbp;
1089
1090 /* if the chown fails, remove set-id bits as-per compress(1) */
1091 if (fchown(fd, sb.st_uid, sb.st_gid) < 0) {
1092 if (errno != EPERM)
1093 maybe_warn("couldn't fchown: %s", file);
1094 sb.st_mode &= ~(S_ISUID|S_ISGID);

--- 90 unchanged lines hidden (view full) ---

1185 return;
1186 if (nsb.st_dev != sb->st_dev || nsb.st_ino != sb->st_ino)
1187 /* Definitely a different file */
1188 return;
1189 unlink(file);
1190}
1191
1192static void
1109 }
1110 sb = *sbp;
1111
1112 /* if the chown fails, remove set-id bits as-per compress(1) */
1113 if (fchown(fd, sb.st_uid, sb.st_gid) < 0) {
1114 if (errno != EPERM)
1115 maybe_warn("couldn't fchown: %s", file);
1116 sb.st_mode &= ~(S_ISUID|S_ISGID);

--- 90 unchanged lines hidden (view full) ---

1207 return;
1208 if (nsb.st_dev != sb->st_dev || nsb.st_ino != sb->st_ino)
1209 /* Definitely a different file */
1210 return;
1211 unlink(file);
1212}
1213
1214static void
1193sigint_handler(int signo __unused)
1215got_sigint(int signo __unused)
1194{
1195
1196 if (remove_file != NULL)
1197 unlink(remove_file);
1198 _exit(2);
1199}
1216{
1217
1218 if (remove_file != NULL)
1219 unlink(remove_file);
1220 _exit(2);
1221}
1222
1223static void
1224got_siginfo(int signo __unused)
1225{
1226
1227 print_info = 1;
1228}
1229
1230static void
1231setup_signals(void)
1232{
1233
1234 signal(SIGINFO, got_siginfo);
1235 signal(SIGINT, got_sigint);
1236}
1237
1238static void
1239infile_newdata(size_t newdata)
1240{
1241
1242 infile_current += newdata;
1243}
1200#endif
1201
1244#endif
1245
1246static void
1247infile_set(const char *newinfile, off_t total)
1248{
1249
1250 if (newinfile)
1251 infile = newinfile;
1252#ifndef SMALL
1253 infile_total = total;
1254#endif
1255}
1256
1257static void
1258infile_clear(void)
1259{
1260
1261 infile = NULL;
1262#ifndef SMALL
1263 infile_total = infile_current = 0;
1264#endif
1265}
1266
1202static const suffixes_t *
1203check_suffix(char *file, int xlate)
1204{
1205 const suffixes_t *s;
1206 int len = strlen(file);
1207 char *sp;
1208
1209 for (s = suffixes; s != suffixes + NUM_SUFFIXES; s++) {

--- 14 unchanged lines hidden (view full) ---

1224 * compress the given file: create a corresponding .gz file and remove the
1225 * original.
1226 */
1227static off_t
1228file_compress(char *file, char *outfile, size_t outsize)
1229{
1230 int in;
1231 int out;
1267static const suffixes_t *
1268check_suffix(char *file, int xlate)
1269{
1270 const suffixes_t *s;
1271 int len = strlen(file);
1272 char *sp;
1273
1274 for (s = suffixes; s != suffixes + NUM_SUFFIXES; s++) {

--- 14 unchanged lines hidden (view full) ---

1289 * compress the given file: create a corresponding .gz file and remove the
1290 * original.
1291 */
1292static off_t
1293file_compress(char *file, char *outfile, size_t outsize)
1294{
1295 int in;
1296 int out;
1232 off_t size, insize;
1297 off_t size, in_size;
1233#ifndef SMALL
1234 struct stat isb, osb;
1235 const suffixes_t *suff;
1236#endif
1237
1238 in = open(file, O_RDONLY);
1239 if (in == -1) {
1240 maybe_warn("can't open %s", file);
1241 return (-1);
1242 }
1243
1244#ifndef SMALL
1245 if (fstat(in, &isb) != 0) {
1246 maybe_warn("couldn't stat: %s", file);
1247 close(in);
1248 return (-1);
1249 }
1250#endif
1251
1298#ifndef SMALL
1299 struct stat isb, osb;
1300 const suffixes_t *suff;
1301#endif
1302
1303 in = open(file, O_RDONLY);
1304 if (in == -1) {
1305 maybe_warn("can't open %s", file);
1306 return (-1);
1307 }
1308
1309#ifndef SMALL
1310 if (fstat(in, &isb) != 0) {
1311 maybe_warn("couldn't stat: %s", file);
1312 close(in);
1313 return (-1);
1314 }
1315#endif
1316
1317#ifndef SMALL
1318 if (fstat(in, &isb) != 0) {
1319 close(in);
1320 maybe_warn("can't stat %s", file);
1321 return -1;
1322 }
1323 infile_set(file, isb.st_size);
1324#endif
1325
1252 if (cflag == 0) {
1253#ifndef SMALL
1254 if (isb.st_nlink > 1 && fflag == 0) {
1326 if (cflag == 0) {
1327#ifndef SMALL
1328 if (isb.st_nlink > 1 && fflag == 0) {
1255 maybe_warnx("%s has %ju other link%s -- skipping",
1256 file, (uintmax_t)isb.st_nlink - 1,
1257 (isb.st_nlink - 1) == 1 ? "" : "s");
1329 maybe_warnx("%s has %ju other link%s -- "
1330 "skipping", file,
1331 (uintmax_t)isb.st_nlink - 1,
1332 isb.st_nlink == 1 ? "" : "s");
1258 close(in);
1333 close(in);
1259 return (-1);
1334 return -1;
1260 }
1261
1262 if (fflag == 0 && (suff = check_suffix(file, 0)) &&
1263 suff->zipped[0] != 0) {
1264 maybe_warnx("%s already has %s suffix -- unchanged",
1265 file, suff->zipped);
1266 close(in);
1267 return (-1);

--- 22 unchanged lines hidden (view full) ---

1290 return (-1);
1291 }
1292#ifndef SMALL
1293 remove_file = outfile;
1294#endif
1295 } else
1296 out = STDOUT_FILENO;
1297
1335 }
1336
1337 if (fflag == 0 && (suff = check_suffix(file, 0)) &&
1338 suff->zipped[0] != 0) {
1339 maybe_warnx("%s already has %s suffix -- unchanged",
1340 file, suff->zipped);
1341 close(in);
1342 return (-1);

--- 22 unchanged lines hidden (view full) ---

1365 return (-1);
1366 }
1367#ifndef SMALL
1368 remove_file = outfile;
1369#endif
1370 } else
1371 out = STDOUT_FILENO;
1372
1298 insize = gz_compress(in, out, &size, basename(file), (uint32_t)isb.st_mtime);
1373 in_size = gz_compress(in, out, &size, basename(file), (uint32_t)isb.st_mtime);
1299
1300 (void)close(in);
1301
1302 /*
1374
1375 (void)close(in);
1376
1377 /*
1303 * If there was an error, insize will be -1.
1378 * If there was an error, in_size will be -1.
1304 * If we compressed to stdout, just return the size.
1305 * Otherwise stat the file and check it is the correct size.
1306 * We only blow away the file if we can stat the output and it
1307 * has the expected size.
1308 */
1309 if (cflag != 0)
1379 * If we compressed to stdout, just return the size.
1380 * Otherwise stat the file and check it is the correct size.
1381 * We only blow away the file if we can stat the output and it
1382 * has the expected size.
1383 */
1384 if (cflag != 0)
1310 return (insize == -1 ? -1 : size);
1385 return in_size == -1 ? -1 : size;
1311
1312#ifndef SMALL
1313 if (fstat(out, &osb) != 0) {
1314 maybe_warn("couldn't stat: %s", outfile);
1315 goto bad_outfile;
1316 }
1317
1318 if (osb.st_size != size) {

--- 28 unchanged lines hidden (view full) ---

1347file_uncompress(char *file, char *outfile, size_t outsize)
1348{
1349 struct stat isb, osb;
1350 off_t size;
1351 ssize_t rbytes;
1352 unsigned char header1[4];
1353 enum filetype method;
1354 int fd, ofd, zfd = -1;
1386
1387#ifndef SMALL
1388 if (fstat(out, &osb) != 0) {
1389 maybe_warn("couldn't stat: %s", outfile);
1390 goto bad_outfile;
1391 }
1392
1393 if (osb.st_size != size) {

--- 28 unchanged lines hidden (view full) ---

1422file_uncompress(char *file, char *outfile, size_t outsize)
1423{
1424 struct stat isb, osb;
1425 off_t size;
1426 ssize_t rbytes;
1427 unsigned char header1[4];
1428 enum filetype method;
1429 int fd, ofd, zfd = -1;
1430 size_t in_size;
1355#ifndef SMALL
1356 ssize_t rv;
1357 time_t timestamp = 0;
1358 char name[PATH_MAX + 1];
1359#endif
1360
1361 /* gather the old name info */
1362
1363 fd = open(file, O_RDONLY);
1364 if (fd < 0) {
1365 maybe_warn("can't open %s", file);
1366 goto lose;
1367 }
1431#ifndef SMALL
1432 ssize_t rv;
1433 time_t timestamp = 0;
1434 char name[PATH_MAX + 1];
1435#endif
1436
1437 /* gather the old name info */
1438
1439 fd = open(file, O_RDONLY);
1440 if (fd < 0) {
1441 maybe_warn("can't open %s", file);
1442 goto lose;
1443 }
1444 if (fstat(fd, &isb) != 0) {
1445 close(fd);
1446 maybe_warn("can't stat %s", file);
1447 goto lose;
1448 }
1449 if (S_ISREG(isb.st_mode))
1450 in_size = isb.st_size;
1451 else
1452 in_size = 0;
1453 infile_set(file, in_size);
1368
1369 strlcpy(outfile, file, outsize);
1370 if (check_suffix(outfile, 1) == NULL && !(cflag || lflag)) {
1371 maybe_warnx("%s: unknown suffix -- ignored", file);
1372 goto lose;
1373 }
1374
1375 rbytes = read(fd, header1, sizeof header1);

--- 4 unchanged lines hidden (view full) ---

1380 goto lose;
1381#endif
1382 if (rbytes == -1)
1383 maybe_warn("can't read %s", file);
1384 else
1385 goto unexpected_EOF;
1386 goto lose;
1387 }
1454
1455 strlcpy(outfile, file, outsize);
1456 if (check_suffix(outfile, 1) == NULL && !(cflag || lflag)) {
1457 maybe_warnx("%s: unknown suffix -- ignored", file);
1458 goto lose;
1459 }
1460
1461 rbytes = read(fd, header1, sizeof header1);

--- 4 unchanged lines hidden (view full) ---

1466 goto lose;
1467#endif
1468 if (rbytes == -1)
1469 maybe_warn("can't read %s", file);
1470 else
1471 goto unexpected_EOF;
1472 goto lose;
1473 }
1474 infile_newdata(rbytes);
1388
1389 method = file_gettype(header1);
1390#ifndef SMALL
1391 if (fflag == 0 && method == FT_UNKNOWN) {
1392 maybe_warnx("%s: not in gzip format", file);
1393 goto lose;
1394 }
1395

--- 6 unchanged lines hidden (view full) ---

1402 rv = pread(fd, ts, sizeof ts, GZIP_TIMESTAMP);
1403 if (rv >= 0 && rv < (ssize_t)(sizeof ts))
1404 goto unexpected_EOF;
1405 if (rv == -1) {
1406 if (!fflag)
1407 maybe_warn("can't read %s", file);
1408 goto lose;
1409 }
1475
1476 method = file_gettype(header1);
1477#ifndef SMALL
1478 if (fflag == 0 && method == FT_UNKNOWN) {
1479 maybe_warnx("%s: not in gzip format", file);
1480 goto lose;
1481 }
1482

--- 6 unchanged lines hidden (view full) ---

1489 rv = pread(fd, ts, sizeof ts, GZIP_TIMESTAMP);
1490 if (rv >= 0 && rv < (ssize_t)(sizeof ts))
1491 goto unexpected_EOF;
1492 if (rv == -1) {
1493 if (!fflag)
1494 maybe_warn("can't read %s", file);
1495 goto lose;
1496 }
1497 infile_newdata(rv);
1410 timestamp = ts[3] << 24 | ts[2] << 16 | ts[1] << 8 | ts[0];
1411
1412 if (header1[3] & ORIG_NAME) {
1413 rbytes = pread(fd, name, sizeof(name) - 1, GZIP_ORIGNAME);
1414 if (rbytes < 0) {
1415 maybe_warn("can't read %s", file);
1416 goto lose;
1417 }

--- 12 unchanged lines hidden (view full) ---

1430
1431 /* preserve original directory name */
1432 dp = strrchr(file, '/');
1433 if (dp == NULL)
1434 dp = file;
1435 else
1436 dp++;
1437 snprintf(outfile, outsize, "%.*s%.*s",
1498 timestamp = ts[3] << 24 | ts[2] << 16 | ts[1] << 8 | ts[0];
1499
1500 if (header1[3] & ORIG_NAME) {
1501 rbytes = pread(fd, name, sizeof(name) - 1, GZIP_ORIGNAME);
1502 if (rbytes < 0) {
1503 maybe_warn("can't read %s", file);
1504 goto lose;
1505 }

--- 12 unchanged lines hidden (view full) ---

1518
1519 /* preserve original directory name */
1520 dp = strrchr(file, '/');
1521 if (dp == NULL)
1522 dp = file;
1523 else
1524 dp++;
1525 snprintf(outfile, outsize, "%.*s%.*s",
1438 (int) (dp - file),
1526 (int) (dp - file),
1439 file, (int) rbytes, nf);
1440 }
1441 }
1442 }
1443#endif
1444 lseek(fd, 0, SEEK_SET);
1445
1446 if (cflag == 0 || lflag) {
1527 file, (int) rbytes, nf);
1528 }
1529 }
1530 }
1531#endif
1532 lseek(fd, 0, SEEK_SET);
1533
1534 if (cflag == 0 || lflag) {
1447 if (fstat(fd, &isb) != 0)
1448 goto lose;
1449#ifndef SMALL
1450 if (isb.st_nlink > 1 && lflag == 0 && fflag == 0) {
1451 maybe_warnx("%s has %ju other links -- skipping",
1452 file, (uintmax_t)isb.st_nlink - 1);
1453 goto lose;
1454 }
1455 if (nflag == 0 && timestamp)
1456 isb.st_mtime = timestamp;
1457 if (check_outfile(outfile) == 0)
1458 goto lose;
1459#endif
1460 }
1461
1535#ifndef SMALL
1536 if (isb.st_nlink > 1 && lflag == 0 && fflag == 0) {
1537 maybe_warnx("%s has %ju other links -- skipping",
1538 file, (uintmax_t)isb.st_nlink - 1);
1539 goto lose;
1540 }
1541 if (nflag == 0 && timestamp)
1542 isb.st_mtime = timestamp;
1543 if (check_outfile(outfile) == 0)
1544 goto lose;
1545#endif
1546 }
1547
1462 if (cflag == 0 && lflag == 0) {
1548 if (cflag)
1549 zfd = STDOUT_FILENO;
1550 else if (lflag)
1551 zfd = -1;
1552 else {
1463 zfd = open(outfile, O_WRONLY|O_CREAT|O_EXCL, 0600);
1464 if (zfd == STDOUT_FILENO) {
1465 /* We won't close STDOUT_FILENO later... */
1466 zfd = dup(zfd);
1467 close(STDOUT_FILENO);
1468 }
1469 if (zfd == -1) {
1470 maybe_warn("can't open %s", outfile);
1471 goto lose;
1472 }
1553 zfd = open(outfile, O_WRONLY|O_CREAT|O_EXCL, 0600);
1554 if (zfd == STDOUT_FILENO) {
1555 /* We won't close STDOUT_FILENO later... */
1556 zfd = dup(zfd);
1557 close(STDOUT_FILENO);
1558 }
1559 if (zfd == -1) {
1560 maybe_warn("can't open %s", outfile);
1561 goto lose;
1562 }
1473#ifndef SMALL
1474 remove_file = outfile;
1563 remove_file = outfile;
1475#endif
1476 } else
1477 zfd = STDOUT_FILENO;
1564 }
1478
1479 switch (method) {
1480#ifndef NO_BZIP2_SUPPORT
1481 case FT_BZIP2:
1482 /* XXX */
1483 if (lflag) {
1484 maybe_warnx("no -l with bzip2 files");
1485 goto lose;

--- 69 unchanged lines hidden (view full) ---

1555 maybe_warnx("no -l for unknown filetypes");
1556 goto lose;
1557 }
1558 size = cat_fd(NULL, 0, NULL, fd);
1559 break;
1560#endif
1561 default:
1562 if (lflag) {
1565
1566 switch (method) {
1567#ifndef NO_BZIP2_SUPPORT
1568 case FT_BZIP2:
1569 /* XXX */
1570 if (lflag) {
1571 maybe_warnx("no -l with bzip2 files");
1572 goto lose;

--- 69 unchanged lines hidden (view full) ---

1642 maybe_warnx("no -l for unknown filetypes");
1643 goto lose;
1644 }
1645 size = cat_fd(NULL, 0, NULL, fd);
1646 break;
1647#endif
1648 default:
1649 if (lflag) {
1563 print_list(fd, isb.st_size, outfile, isb.st_mtime);
1650 print_list(fd, in_size, outfile, isb.st_mtime);
1564 close(fd);
1565 return -1; /* XXX */
1566 }
1567
1568 size = gz_uncompress(fd, zfd, NULL, 0, NULL, file);
1569 break;
1570 }
1571

--- 58 unchanged lines hidden (view full) ---

1630 if (fd != -1)
1631 close(fd);
1632 if (zfd != -1 && zfd != STDOUT_FILENO)
1633 close(fd);
1634 return -1;
1635}
1636
1637#ifndef SMALL
1651 close(fd);
1652 return -1; /* XXX */
1653 }
1654
1655 size = gz_uncompress(fd, zfd, NULL, 0, NULL, file);
1656 break;
1657 }
1658

--- 58 unchanged lines hidden (view full) ---

1717 if (fd != -1)
1718 close(fd);
1719 if (zfd != -1 && zfd != STDOUT_FILENO)
1720 close(fd);
1721 return -1;
1722}
1723
1724#ifndef SMALL
1725static void
1726check_siginfo(void)
1727{
1728 if (print_info == 0)
1729 return;
1730 if (infile) {
1731 if (infile_total) {
1732 int pcent = (int)((100.0 * infile_current) / infile_total);
1733
1734 fprintf(stderr, "%s: done %llu/%llu bytes %d%%\n",
1735 infile, (unsigned long long)infile_current,
1736 (unsigned long long)infile_total, pcent);
1737 } else
1738 fprintf(stderr, "%s: done %llu bytes\n",
1739 infile, (unsigned long long)infile_current);
1740 }
1741 print_info = 0;
1742}
1743
1638static off_t
1639cat_fd(unsigned char * prepend, size_t count, off_t *gsizep, int fd)
1640{
1641 char buf[BUFLEN];
1642 off_t in_tot;
1643 ssize_t w;
1644
1645 in_tot = count;
1744static off_t
1745cat_fd(unsigned char * prepend, size_t count, off_t *gsizep, int fd)
1746{
1747 char buf[BUFLEN];
1748 off_t in_tot;
1749 ssize_t w;
1750
1751 in_tot = count;
1646 w = write(STDOUT_FILENO, prepend, count);
1752 w = write_retry(STDOUT_FILENO, prepend, count);
1647 if (w == -1 || (size_t)w != count) {
1648 maybe_warn("write to stdout");
1649 return -1;
1650 }
1651 for (;;) {
1652 ssize_t rv;
1653
1654 rv = read(fd, buf, sizeof buf);
1655 if (rv == 0)
1656 break;
1657 if (rv < 0) {
1658 maybe_warn("read from fd %d", fd);
1659 break;
1660 }
1753 if (w == -1 || (size_t)w != count) {
1754 maybe_warn("write to stdout");
1755 return -1;
1756 }
1757 for (;;) {
1758 ssize_t rv;
1759
1760 rv = read(fd, buf, sizeof buf);
1761 if (rv == 0)
1762 break;
1763 if (rv < 0) {
1764 maybe_warn("read from fd %d", fd);
1765 break;
1766 }
1767 infile_newdata(rv);
1661
1768
1662 if (write(STDOUT_FILENO, buf, rv) != rv) {
1769 if (write_retry(STDOUT_FILENO, buf, rv) != rv) {
1663 maybe_warn("write to stdout");
1664 break;
1665 }
1666 in_tot += rv;
1667 }
1668
1669 if (gsizep)
1670 *gsizep = in_tot;
1671 return (in_tot);
1672}
1673#endif
1674
1675static void
1676handle_stdin(void)
1677{
1770 maybe_warn("write to stdout");
1771 break;
1772 }
1773 in_tot += rv;
1774 }
1775
1776 if (gsizep)
1777 *gsizep = in_tot;
1778 return (in_tot);
1779}
1780#endif
1781
1782static void
1783handle_stdin(void)
1784{
1785 struct stat isb;
1678 unsigned char header1[4];
1786 unsigned char header1[4];
1787 size_t in_size;
1679 off_t usize, gsize;
1680 enum filetype method;
1681 ssize_t bytes_read;
1682#ifndef NO_COMPRESS_SUPPORT
1683 FILE *in;
1684#endif
1685
1686#ifndef SMALL
1687 if (fflag == 0 && lflag == 0 && isatty(STDIN_FILENO)) {
1688 maybe_warnx("standard input is a terminal -- ignoring");
1788 off_t usize, gsize;
1789 enum filetype method;
1790 ssize_t bytes_read;
1791#ifndef NO_COMPRESS_SUPPORT
1792 FILE *in;
1793#endif
1794
1795#ifndef SMALL
1796 if (fflag == 0 && lflag == 0 && isatty(STDIN_FILENO)) {
1797 maybe_warnx("standard input is a terminal -- ignoring");
1689 return;
1798 goto out;
1690 }
1691#endif
1692
1799 }
1800#endif
1801
1693 if (lflag) {
1694 struct stat isb;
1802 if (fstat(STDIN_FILENO, &isb) < 0) {
1803 maybe_warn("fstat");
1804 goto out;
1805 }
1806 if (S_ISREG(isb.st_mode))
1807 in_size = isb.st_size;
1808 else
1809 in_size = 0;
1810 infile_set("(stdin)", in_size);
1695
1811
1696 /* XXX could read the whole file, etc. */
1697 if (fstat(STDIN_FILENO, &isb) < 0) {
1698 maybe_warn("fstat");
1699 return;
1700 }
1701 print_list(STDIN_FILENO, isb.st_size, "stdout", isb.st_mtime);
1702 return;
1812 if (lflag) {
1813 print_list(STDIN_FILENO, in_size, infile, isb.st_mtime);
1814 goto out;
1703 }
1704
1705 bytes_read = read_retry(STDIN_FILENO, header1, sizeof header1);
1706 if (bytes_read == -1) {
1707 maybe_warn("can't read stdin");
1815 }
1816
1817 bytes_read = read_retry(STDIN_FILENO, header1, sizeof header1);
1818 if (bytes_read == -1) {
1819 maybe_warn("can't read stdin");
1708 return;
1820 goto out;
1709 } else if (bytes_read != sizeof(header1)) {
1710 maybe_warnx("(stdin): unexpected end of file");
1821 } else if (bytes_read != sizeof(header1)) {
1822 maybe_warnx("(stdin): unexpected end of file");
1711 return;
1823 goto out;
1712 }
1713
1714 method = file_gettype(header1);
1715 switch (method) {
1716 default:
1717#ifndef SMALL
1718 if (fflag == 0) {
1719 maybe_warnx("unknown compression format");
1824 }
1825
1826 method = file_gettype(header1);
1827 switch (method) {
1828 default:
1829#ifndef SMALL
1830 if (fflag == 0) {
1831 maybe_warnx("unknown compression format");
1720 return;
1832 goto out;
1721 }
1722 usize = cat_fd(header1, sizeof header1, &gsize, STDIN_FILENO);
1723 break;
1724#endif
1725 case FT_GZIP:
1833 }
1834 usize = cat_fd(header1, sizeof header1, &gsize, STDIN_FILENO);
1835 break;
1836#endif
1837 case FT_GZIP:
1726 usize = gz_uncompress(STDIN_FILENO, STDOUT_FILENO,
1838 usize = gz_uncompress(STDIN_FILENO, STDOUT_FILENO,
1727 (char *)header1, sizeof header1, &gsize, "(stdin)");
1728 break;
1729#ifndef NO_BZIP2_SUPPORT
1730 case FT_BZIP2:
1731 usize = unbzip2(STDIN_FILENO, STDOUT_FILENO,
1732 (char *)header1, sizeof header1, &gsize);
1733 break;
1734#endif
1735#ifndef NO_COMPRESS_SUPPORT
1736 case FT_Z:
1737 if ((in = zdopen(STDIN_FILENO)) == NULL) {
1738 maybe_warnx("zopen of stdin");
1839 (char *)header1, sizeof header1, &gsize, "(stdin)");
1840 break;
1841#ifndef NO_BZIP2_SUPPORT
1842 case FT_BZIP2:
1843 usize = unbzip2(STDIN_FILENO, STDOUT_FILENO,
1844 (char *)header1, sizeof header1, &gsize);
1845 break;
1846#endif
1847#ifndef NO_COMPRESS_SUPPORT
1848 case FT_Z:
1849 if ((in = zdopen(STDIN_FILENO)) == NULL) {
1850 maybe_warnx("zopen of stdin");
1739 return;
1851 goto out;
1740 }
1741
1742 usize = zuncompress(in, stdout, (char *)header1,
1743 sizeof header1, &gsize);
1744 fclose(in);
1745 break;
1746#endif
1747#ifndef NO_PACK_SUPPORT

--- 10 unchanged lines hidden (view full) ---

1758#endif
1759 }
1760
1761#ifndef SMALL
1762 if (vflag && !tflag && usize != -1 && gsize != -1)
1763 print_verbage(NULL, NULL, usize, gsize);
1764 if (vflag && tflag)
1765 print_test("(stdin)", usize != -1);
1852 }
1853
1854 usize = zuncompress(in, stdout, (char *)header1,
1855 sizeof header1, &gsize);
1856 fclose(in);
1857 break;
1858#endif
1859#ifndef NO_PACK_SUPPORT

--- 10 unchanged lines hidden (view full) ---

1870#endif
1871 }
1872
1873#ifndef SMALL
1874 if (vflag && !tflag && usize != -1 && gsize != -1)
1875 print_verbage(NULL, NULL, usize, gsize);
1876 if (vflag && tflag)
1877 print_test("(stdin)", usize != -1);
1766#endif
1878#else
1879 (void)&usize;
1880#endif
1767
1881
1882out:
1883 infile_clear();
1768}
1769
1770static void
1771handle_stdout(void)
1772{
1884}
1885
1886static void
1887handle_stdout(void)
1888{
1773 off_t gsize, usize;
1889 off_t gsize;
1890#ifndef SMALL
1891 off_t usize;
1774 struct stat sb;
1775 time_t systime;
1776 uint32_t mtime;
1777 int ret;
1778
1892 struct stat sb;
1893 time_t systime;
1894 uint32_t mtime;
1895 int ret;
1896
1779#ifndef SMALL
1897 infile_set("(stdout)", 0);
1898
1780 if (fflag == 0 && isatty(STDOUT_FILENO)) {
1781 maybe_warnx("standard output is a terminal -- ignoring");
1782 return;
1783 }
1899 if (fflag == 0 && isatty(STDOUT_FILENO)) {
1900 maybe_warnx("standard output is a terminal -- ignoring");
1901 return;
1902 }
1784#endif
1903
1785 /* If stdin is a file use its mtime, otherwise use current time */
1786 ret = fstat(STDIN_FILENO, &sb);
1904 /* If stdin is a file use its mtime, otherwise use current time */
1905 ret = fstat(STDIN_FILENO, &sb);
1787
1788#ifndef SMALL
1789 if (ret < 0) {
1790 maybe_warn("Can't stat stdin");
1791 return;
1792 }
1906 if (ret < 0) {
1907 maybe_warn("Can't stat stdin");
1908 return;
1909 }
1793#endif
1794
1910
1795 if (S_ISREG(sb.st_mode))
1911 if (S_ISREG(sb.st_mode)) {
1912 infile_set("(stdout)", sb.st_size);
1796 mtime = (uint32_t)sb.st_mtime;
1913 mtime = (uint32_t)sb.st_mtime;
1797 else {
1914 } else {
1798 systime = time(NULL);
1915 systime = time(NULL);
1799#ifndef SMALL
1800 if (systime == -1) {
1801 maybe_warn("time");
1802 return;
1916 if (systime == -1) {
1917 maybe_warn("time");
1918 return;
1803 }
1804#endif
1919 }
1805 mtime = (uint32_t)systime;
1806 }
1807
1920 mtime = (uint32_t)systime;
1921 }
1922
1808 usize = gz_compress(STDIN_FILENO, STDOUT_FILENO, &gsize, "", mtime);
1923 usize =
1924#endif
1925 gz_compress(STDIN_FILENO, STDOUT_FILENO, &gsize, "", mtime);
1809#ifndef SMALL
1810 if (vflag && !tflag && usize != -1 && gsize != -1)
1811 print_verbage(NULL, NULL, usize, gsize);
1926#ifndef SMALL
1927 if (vflag && !tflag && usize != -1 && gsize != -1)
1928 print_verbage(NULL, NULL, usize, gsize);
1812#endif
1929#endif
1813}
1814
1815/* do what is asked for, for the path name */
1816static void
1817handle_pathname(char *path)
1818{
1819 char *opath = path, *s = NULL;
1820 ssize_t len;

--- 50 unchanged lines hidden (view full) ---

1871
1872/* compress/decompress a file */
1873static void
1874handle_file(char *file, struct stat *sbp)
1875{
1876 off_t usize, gsize;
1877 char outfile[PATH_MAX];
1878
1930}
1931
1932/* do what is asked for, for the path name */
1933static void
1934handle_pathname(char *path)
1935{
1936 char *opath = path, *s = NULL;
1937 ssize_t len;

--- 50 unchanged lines hidden (view full) ---

1988
1989/* compress/decompress a file */
1990static void
1991handle_file(char *file, struct stat *sbp)
1992{
1993 off_t usize, gsize;
1994 char outfile[PATH_MAX];
1995
1879 infile = file;
1996 infile_set(file, sbp->st_size);
1880 if (dflag) {
1881 usize = file_uncompress(file, outfile, sizeof(outfile));
1882#ifndef SMALL
1883 if (vflag && tflag)
1884 print_test(file, usize != -1);
1885#endif
1886 if (usize == -1)
1887 return;
1888 gsize = sbp->st_size;
1889 } else {
1890 gsize = file_compress(file, outfile, sizeof(outfile));
1891 if (gsize == -1)
1892 return;
1893 usize = sbp->st_size;
1894 }
1997 if (dflag) {
1998 usize = file_uncompress(file, outfile, sizeof(outfile));
1999#ifndef SMALL
2000 if (vflag && tflag)
2001 print_test(file, usize != -1);
2002#endif
2003 if (usize == -1)
2004 return;
2005 gsize = sbp->st_size;
2006 } else {
2007 gsize = file_compress(file, outfile, sizeof(outfile));
2008 if (gsize == -1)
2009 return;
2010 usize = sbp->st_size;
2011 }
2012 infile_clear();
1895
2013
1896
1897#ifndef SMALL
1898 if (vflag && !tflag)
1899 print_verbage(file, (cflag) ? NULL : outfile, usize, gsize);
1900#endif
1901}
1902
1903#ifndef SMALL
1904/* this is used with -r to recursively descend directories */

--- 36 unchanged lines hidden (view full) ---

1941print_ratio(off_t in, off_t out, FILE *where)
1942{
1943 int percent10; /* 10 * percent */
1944 off_t diff;
1945 char buff[8];
1946 int len;
1947
1948 diff = in - out/2;
2014#ifndef SMALL
2015 if (vflag && !tflag)
2016 print_verbage(file, (cflag) ? NULL : outfile, usize, gsize);
2017#endif
2018}
2019
2020#ifndef SMALL
2021/* this is used with -r to recursively descend directories */

--- 36 unchanged lines hidden (view full) ---

2058print_ratio(off_t in, off_t out, FILE *where)
2059{
2060 int percent10; /* 10 * percent */
2061 off_t diff;
2062 char buff[8];
2063 int len;
2064
2065 diff = in - out/2;
1949 if (diff <= 0)
2066 if (in == 0 && out == 0)
2067 percent10 = 0;
2068 else if (diff < 0)
1950 /*
1951 * Output is more than double size of input! print -99.9%
1952 * Quite possibly we've failed to get the original size.
1953 */
1954 percent10 = -999;
1955 else {
1956 /*
1957 * We only need 12 bits of result from the final division,

--- 209 unchanged lines hidden (view full) ---

2167 break; /* EOF */
2168 }
2169 cp += ret;
2170 left -= ret;
2171 }
2172
2173 return sz - left;
2174}
2069 /*
2070 * Output is more than double size of input! print -99.9%
2071 * Quite possibly we've failed to get the original size.
2072 */
2073 percent10 = -999;
2074 else {
2075 /*
2076 * We only need 12 bits of result from the final division,

--- 209 unchanged lines hidden (view full) ---

2286 break; /* EOF */
2287 }
2288 cp += ret;
2289 left -= ret;
2290 }
2291
2292 return sz - left;
2293}
2294
2295static ssize_t
2296write_retry(int fd, const void *buf, size_t sz)
2297{
2298 const char *cp = buf;
2299 size_t left = MIN(sz, (size_t) SSIZE_MAX);
2300
2301 while (left > 0) {
2302 ssize_t ret;
2303
2304 ret = write(fd, cp, left);
2305 if (ret == -1) {
2306 return ret;
2307 } else if (ret == 0) {
2308 abort(); /* Can't happen */
2309 }
2310 cp += ret;
2311 left -= ret;
2312 }
2313
2314 return sz - left;
2315}