Home | History | Annotate | Line # | Download | only in compress
compress.c revision 1.7.2.2
      1  1.7.2.2  mycroft /*-
      2  1.7.2.2  mycroft  * Copyright (c) 1992, 1993
      3  1.7.2.2  mycroft  *	The Regents of the University of California.  All rights reserved.
      4  1.7.2.2  mycroft  *
      5  1.7.2.2  mycroft  * Redistribution and use in source and binary forms, with or without
      6  1.7.2.2  mycroft  * modification, are permitted provided that the following conditions
      7  1.7.2.2  mycroft  * are met:
      8  1.7.2.2  mycroft  * 1. Redistributions of source code must retain the above copyright
      9  1.7.2.2  mycroft  *    notice, this list of conditions and the following disclaimer.
     10  1.7.2.2  mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.7.2.2  mycroft  *    notice, this list of conditions and the following disclaimer in the
     12  1.7.2.2  mycroft  *    documentation and/or other materials provided with the distribution.
     13  1.7.2.2  mycroft  * 3. All advertising materials mentioning features or use of this software
     14  1.7.2.2  mycroft  *    must display the following acknowledgement:
     15  1.7.2.2  mycroft  *	This product includes software developed by the University of
     16  1.7.2.2  mycroft  *	California, Berkeley and its contributors.
     17  1.7.2.2  mycroft  * 4. Neither the name of the University nor the names of its contributors
     18  1.7.2.2  mycroft  *    may be used to endorse or promote products derived from this software
     19  1.7.2.2  mycroft  *    without specific prior written permission.
     20  1.7.2.2  mycroft  *
     21  1.7.2.2  mycroft  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  1.7.2.2  mycroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.7.2.2  mycroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.7.2.2  mycroft  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  1.7.2.2  mycroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.7.2.2  mycroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.7.2.2  mycroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.7.2.2  mycroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.7.2.2  mycroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.7.2.2  mycroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.7.2.2  mycroft  * SUCH DAMAGE.
     32  1.7.2.2  mycroft  */
     33  1.7.2.2  mycroft 
     34  1.7.2.2  mycroft #ifndef lint
     35  1.7.2.2  mycroft static char copyright[] =
     36  1.7.2.2  mycroft "@(#) Copyright (c) 1992, 1993\n\
     37  1.7.2.2  mycroft 	The Regents of the University of California.  All rights reserved.\n";
     38  1.7.2.2  mycroft #endif /* not lint */
     39  1.7.2.2  mycroft 
     40  1.7.2.2  mycroft #ifndef lint
     41  1.7.2.2  mycroft /*static char sccsid[] = "from: @(#)compress.c	8.2 (Berkeley) 1/7/94";*/
     42  1.7.2.2  mycroft static char *rcsid = "$Id: compress.c,v 1.7.2.2 1994/10/06 16:43:18 mycroft Exp $";
     43  1.7.2.2  mycroft #endif /* not lint */
     44  1.7.2.2  mycroft 
     45  1.7.2.2  mycroft #include <sys/param.h>
     46  1.7.2.2  mycroft #include <sys/time.h>
     47  1.7.2.2  mycroft #include <sys/stat.h>
     48  1.7.2.2  mycroft 
     49  1.7.2.2  mycroft #include <err.h>
     50  1.7.2.2  mycroft #include <errno.h>
     51  1.7.2.2  mycroft #include <stdio.h>
     52  1.7.2.2  mycroft #include <stdlib.h>
     53  1.7.2.2  mycroft #include <string.h>
     54  1.7.2.2  mycroft #include <unistd.h>
     55  1.7.2.2  mycroft 
     56  1.7.2.2  mycroft #ifdef __STDC__
     57  1.7.2.2  mycroft #include <stdarg.h>
     58  1.7.2.2  mycroft #else
     59  1.7.2.2  mycroft #include <varargs.h>
     60  1.7.2.2  mycroft #endif
     61  1.7.2.2  mycroft 
     62  1.7.2.2  mycroft void	compress __P((char *, char *, int));
     63  1.7.2.2  mycroft void	cwarn __P((const char *, ...));
     64  1.7.2.2  mycroft void	cwarnx __P((const char *, ...));
     65  1.7.2.2  mycroft void	decompress __P((char *, char *, int));
     66  1.7.2.2  mycroft int	permission __P((char *));
     67  1.7.2.2  mycroft void	setfile __P((char *, struct stat *));
     68  1.7.2.2  mycroft void	usage __P((int));
     69  1.7.2.2  mycroft 
     70  1.7.2.2  mycroft int eval, force, verbose;
     71  1.7.2.2  mycroft 
     72  1.7.2.2  mycroft int
     73  1.7.2.2  mycroft main(argc, argv)
     74  1.7.2.2  mycroft 	int argc;
     75  1.7.2.2  mycroft 	char *argv[];
     76  1.7.2.2  mycroft {
     77  1.7.2.2  mycroft 	enum {COMPRESS, DECOMPRESS} style;
     78  1.7.2.2  mycroft 	size_t len;
     79  1.7.2.2  mycroft 	int bits, cat, ch;
     80  1.7.2.2  mycroft 	char *p, newname[MAXPATHLEN];
     81  1.7.2.2  mycroft 
     82  1.7.2.2  mycroft 	if ((p = rindex(argv[0], '/')) == NULL)
     83  1.7.2.2  mycroft 		p = argv[0];
     84  1.7.2.2  mycroft 	else
     85  1.7.2.2  mycroft 		++p;
     86  1.7.2.2  mycroft 	if (!strcmp(p, "uncompress"))
     87  1.7.2.2  mycroft 		style = DECOMPRESS;
     88  1.7.2.2  mycroft 	else if (!strcmp(p, "compress"))
     89  1.7.2.2  mycroft 		style = COMPRESS;
     90  1.7.2.2  mycroft 	else
     91  1.7.2.2  mycroft 		errx(1, "unknown program name");
     92  1.7.2.2  mycroft 
     93  1.7.2.2  mycroft 	bits = cat = 0;
     94  1.7.2.2  mycroft 	while ((ch = getopt(argc, argv, "b:cdfv")) != EOF)
     95  1.7.2.2  mycroft 		switch(ch) {
     96  1.7.2.2  mycroft 		case 'b':
     97  1.7.2.2  mycroft 			bits = strtol(optarg, &p, 10);
     98  1.7.2.2  mycroft 			if (*p)
     99  1.7.2.2  mycroft 				errx(1, "illegal bit count -- %s", optarg);
    100  1.7.2.2  mycroft 			break;
    101  1.7.2.2  mycroft 		case 'c':
    102  1.7.2.2  mycroft 			cat = 1;
    103  1.7.2.2  mycroft 			break;
    104  1.7.2.2  mycroft 		case 'd':		/* Backward compatible. */
    105  1.7.2.2  mycroft 			style = DECOMPRESS;
    106  1.7.2.2  mycroft 			break;
    107  1.7.2.2  mycroft 		case 'f':
    108  1.7.2.2  mycroft 			force = 1;
    109  1.7.2.2  mycroft 			break;
    110  1.7.2.2  mycroft 		case 'v':
    111  1.7.2.2  mycroft 			verbose = 1;
    112  1.7.2.2  mycroft 			break;
    113  1.7.2.2  mycroft 		case '?':
    114  1.7.2.2  mycroft 		default:
    115  1.7.2.2  mycroft 			usage(style == COMPRESS);
    116  1.7.2.2  mycroft 		}
    117  1.7.2.2  mycroft 	argc -= optind;
    118  1.7.2.2  mycroft 	argv += optind;
    119  1.7.2.2  mycroft 
    120  1.7.2.2  mycroft 	if (argc == 0) {
    121  1.7.2.2  mycroft 		switch(style) {
    122  1.7.2.2  mycroft 		case COMPRESS:
    123  1.7.2.2  mycroft 			(void)compress("/dev/stdin", "/dev/stdout", bits);
    124  1.7.2.2  mycroft 			break;
    125  1.7.2.2  mycroft 		case DECOMPRESS:
    126  1.7.2.2  mycroft 			(void)decompress("/dev/stdin", "/dev/stdout", bits);
    127  1.7.2.2  mycroft 			break;
    128  1.7.2.2  mycroft 		}
    129  1.7.2.2  mycroft 		exit (eval);
    130  1.7.2.2  mycroft 	}
    131  1.7.2.2  mycroft 
    132  1.7.2.2  mycroft 	if (cat == 1 && argc > 1)
    133  1.7.2.2  mycroft 		errx(1, "the -c option permits only a single file argument");
    134  1.7.2.2  mycroft 
    135  1.7.2.2  mycroft 	for (; *argv; ++argv)
    136  1.7.2.2  mycroft 		switch(style) {
    137  1.7.2.2  mycroft 		case COMPRESS:
    138  1.7.2.2  mycroft 			if (cat) {
    139  1.7.2.2  mycroft 				compress(*argv, "/dev/stdout", bits);
    140  1.7.2.2  mycroft 				break;
    141  1.7.2.2  mycroft 			}
    142  1.7.2.2  mycroft 			if ((p = rindex(*argv, '.')) != NULL &&
    143  1.7.2.2  mycroft 			    !strcmp(p, ".Z")) {
    144  1.7.2.2  mycroft 				cwarnx("%s: name already has trailing .Z",
    145  1.7.2.2  mycroft 				    *argv);
    146  1.7.2.2  mycroft 				break;
    147  1.7.2.2  mycroft 			}
    148  1.7.2.2  mycroft 			len = strlen(*argv);
    149  1.7.2.2  mycroft 			if (len > sizeof(newname) - 3) {
    150  1.7.2.2  mycroft 				cwarnx("%s: name too long", *argv);
    151  1.7.2.2  mycroft 				break;
    152  1.7.2.2  mycroft 			}
    153  1.7.2.2  mycroft 			memmove(newname, *argv, len);
    154  1.7.2.2  mycroft 			newname[len] = '.';
    155  1.7.2.2  mycroft 			newname[len + 1] = 'Z';
    156  1.7.2.2  mycroft 			newname[len + 2] = '\0';
    157  1.7.2.2  mycroft 			compress(*argv, newname, bits);
    158  1.7.2.2  mycroft 			break;
    159  1.7.2.2  mycroft 		case DECOMPRESS:
    160  1.7.2.2  mycroft 			len = strlen(*argv);
    161  1.7.2.2  mycroft 			if ((p = rindex(*argv, '.')) == NULL ||
    162  1.7.2.2  mycroft 			    strcmp(p, ".Z")) {
    163  1.7.2.2  mycroft 				if (len > sizeof(newname) - 3) {
    164  1.7.2.2  mycroft 					cwarnx("%s: name too long", *argv);
    165  1.7.2.2  mycroft 					break;
    166  1.7.2.2  mycroft 				}
    167  1.7.2.2  mycroft 				memmove(newname, *argv, len);
    168  1.7.2.2  mycroft 				newname[len] = '.';
    169  1.7.2.2  mycroft 				newname[len + 1] = 'Z';
    170  1.7.2.2  mycroft 				newname[len + 2] = '\0';
    171  1.7.2.2  mycroft 				decompress(newname,
    172  1.7.2.2  mycroft 				    cat ? "/dev/stdout" : *argv, bits);
    173  1.7.2.2  mycroft 			} else {
    174  1.7.2.2  mycroft 				if (len - 2 > sizeof(newname) - 1) {
    175  1.7.2.2  mycroft 					cwarnx("%s: name too long", *argv);
    176  1.7.2.2  mycroft 					break;
    177  1.7.2.2  mycroft 				}
    178  1.7.2.2  mycroft 				memmove(newname, *argv, len - 2);
    179  1.7.2.2  mycroft 				newname[len - 2] = '\0';
    180  1.7.2.2  mycroft 				decompress(*argv,
    181  1.7.2.2  mycroft 				    cat ? "/dev/stdout" : newname, bits);
    182  1.7.2.2  mycroft 			}
    183  1.7.2.2  mycroft 			break;
    184  1.7.2.2  mycroft 		}
    185  1.7.2.2  mycroft 	exit (eval);
    186  1.7.2.2  mycroft }
    187  1.7.2.2  mycroft 
    188  1.7.2.2  mycroft void
    189  1.7.2.2  mycroft compress(in, out, bits)
    190  1.7.2.2  mycroft 	char *in, *out;
    191  1.7.2.2  mycroft 	int bits;
    192  1.7.2.2  mycroft {
    193  1.7.2.2  mycroft 	register int nr;
    194  1.7.2.2  mycroft 	struct stat isb, sb;
    195  1.7.2.2  mycroft 	FILE *ifp, *ofp;
    196  1.7.2.2  mycroft 	int exists, isreg, oreg;
    197  1.7.2.2  mycroft 	u_char buf[1024];
    198  1.7.2.2  mycroft 
    199  1.7.2.2  mycroft 	exists = !stat(out, &sb);
    200  1.7.2.2  mycroft 	if (!force && exists && S_ISREG(sb.st_mode) && !permission(out))
    201  1.7.2.2  mycroft 		return;
    202  1.7.2.2  mycroft 	isreg = oreg = !exists || S_ISREG(sb.st_mode);
    203  1.7.2.2  mycroft 
    204  1.7.2.2  mycroft 	ifp = ofp = NULL;
    205  1.7.2.2  mycroft 	if ((ifp = fopen(in, "r")) == NULL) {
    206  1.7.2.2  mycroft 		cwarn("%s", in);
    207  1.7.2.2  mycroft 		return;
    208  1.7.2.2  mycroft 	}
    209  1.7.2.2  mycroft 	if (stat(in, &isb)) {		/* DON'T FSTAT! */
    210  1.7.2.2  mycroft 		cwarn("%s", in);
    211  1.7.2.2  mycroft 		goto err;
    212  1.7.2.2  mycroft 	}
    213  1.7.2.2  mycroft 	if (!S_ISREG(isb.st_mode))
    214  1.7.2.2  mycroft 		isreg = 0;
    215  1.7.2.2  mycroft 
    216  1.7.2.2  mycroft 	if ((ofp = zopen(out, "w", bits)) == NULL) {
    217  1.7.2.2  mycroft 		cwarn("%s", out);
    218  1.7.2.2  mycroft 		goto err;
    219  1.7.2.2  mycroft 	}
    220  1.7.2.2  mycroft 	while ((nr = fread(buf, 1, sizeof(buf), ifp)) != 0)
    221  1.7.2.2  mycroft 		if (fwrite(buf, 1, nr, ofp) != nr) {
    222  1.7.2.2  mycroft 			cwarn("%s", out);
    223  1.7.2.2  mycroft 			goto err;
    224  1.7.2.2  mycroft 		}
    225  1.7.2.2  mycroft 
    226  1.7.2.2  mycroft 	if (ferror(ifp) || fclose(ifp)) {
    227  1.7.2.2  mycroft 		cwarn("%s", in);
    228  1.7.2.2  mycroft 		goto err;
    229  1.7.2.2  mycroft 	}
    230  1.7.2.2  mycroft 	ifp = NULL;
    231  1.7.2.2  mycroft 
    232  1.7.2.2  mycroft 	if (fclose(ofp)) {
    233  1.7.2.2  mycroft 		cwarn("%s", out);
    234  1.7.2.2  mycroft 		goto err;
    235  1.7.2.2  mycroft 	}
    236  1.7.2.2  mycroft 	ofp = NULL;
    237  1.7.2.2  mycroft 
    238  1.7.2.2  mycroft 	if (isreg) {
    239  1.7.2.2  mycroft 		if (stat(out, &sb)) {
    240  1.7.2.2  mycroft 			cwarn("%s", out);
    241  1.7.2.2  mycroft 			goto err;
    242  1.7.2.2  mycroft 		}
    243  1.7.2.2  mycroft 
    244  1.7.2.2  mycroft 		if (!force && sb.st_size >= isb.st_size) {
    245  1.7.2.2  mycroft 			if (verbose)
    246  1.7.2.2  mycroft 		(void)printf("%s: file would grow; left unmodified\n", in);
    247  1.7.2.2  mycroft 			if (unlink(out))
    248  1.7.2.2  mycroft 				cwarn("%s", out);
    249  1.7.2.2  mycroft 			goto err;
    250  1.7.2.2  mycroft 		}
    251  1.7.2.2  mycroft 
    252  1.7.2.2  mycroft 		setfile(out, &isb);
    253  1.7.2.2  mycroft 
    254  1.7.2.2  mycroft 		if (unlink(in))
    255  1.7.2.2  mycroft 			cwarn("%s", in);
    256  1.7.2.2  mycroft 
    257  1.7.2.2  mycroft 		if (verbose) {
    258  1.7.2.2  mycroft 			(void)printf("%s: ", out);
    259  1.7.2.2  mycroft 			if (isb.st_size > sb.st_size)
    260  1.7.2.2  mycroft 				(void)printf("%.0f%% compression\n",
    261  1.7.2.2  mycroft 				    ((float)sb.st_size / isb.st_size) * 100.0);
    262  1.7.2.2  mycroft 			else
    263  1.7.2.2  mycroft 				(void)printf("%.0f%% expansion\n",
    264  1.7.2.2  mycroft 				    ((float)isb.st_size / sb.st_size) * 100.0);
    265  1.7.2.2  mycroft 		}
    266  1.7.2.2  mycroft 	}
    267  1.7.2.2  mycroft 	return;
    268  1.7.2.2  mycroft 
    269  1.7.2.2  mycroft err:	if (ofp) {
    270  1.7.2.2  mycroft 		if (oreg)
    271  1.7.2.2  mycroft 			(void)unlink(out);
    272  1.7.2.2  mycroft 		(void)fclose(ofp);
    273  1.7.2.2  mycroft 	}
    274  1.7.2.2  mycroft 	if (ifp)
    275  1.7.2.2  mycroft 		(void)fclose(ifp);
    276  1.7.2.2  mycroft }
    277  1.7.2.2  mycroft 
    278  1.7.2.2  mycroft void
    279  1.7.2.2  mycroft decompress(in, out, bits)
    280  1.7.2.2  mycroft 	char *in, *out;
    281  1.7.2.2  mycroft 	int bits;
    282  1.7.2.2  mycroft {
    283  1.7.2.2  mycroft 	register int nr;
    284  1.7.2.2  mycroft 	struct stat sb;
    285  1.7.2.2  mycroft 	FILE *ifp, *ofp;
    286  1.7.2.2  mycroft 	int exists, isreg, oreg;
    287  1.7.2.2  mycroft 	u_char buf[1024];
    288  1.7.2.2  mycroft 
    289  1.7.2.2  mycroft 	exists = !stat(out, &sb);
    290  1.7.2.2  mycroft 	if (!force && exists && S_ISREG(sb.st_mode) && !permission(out))
    291  1.7.2.2  mycroft 		return;
    292  1.7.2.2  mycroft 	isreg = oreg = !exists || S_ISREG(sb.st_mode);
    293  1.7.2.2  mycroft 
    294  1.7.2.2  mycroft 	ifp = ofp = NULL;
    295  1.7.2.2  mycroft 	if ((ofp = fopen(out, "w")) == NULL) {
    296  1.7.2.2  mycroft 		cwarn("%s", out);
    297  1.7.2.2  mycroft 		return;
    298  1.7.2.2  mycroft 	}
    299  1.7.2.2  mycroft 
    300  1.7.2.2  mycroft 	if ((ifp = zopen(in, "r", bits)) == NULL) {
    301  1.7.2.2  mycroft 		cwarn("%s", in);
    302  1.7.2.2  mycroft 		goto err;
    303  1.7.2.2  mycroft 	}
    304  1.7.2.2  mycroft 	if (stat(in, &sb)) {
    305  1.7.2.2  mycroft 		cwarn("%s", in);
    306  1.7.2.2  mycroft 		goto err;
    307  1.7.2.2  mycroft 	}
    308  1.7.2.2  mycroft 	if (!S_ISREG(sb.st_mode))
    309  1.7.2.2  mycroft 		isreg = 0;
    310  1.7.2.2  mycroft 
    311  1.7.2.2  mycroft 	while ((nr = fread(buf, 1, sizeof(buf), ifp)) != 0)
    312  1.7.2.2  mycroft 		if (fwrite(buf, 1, nr, ofp) != nr) {
    313  1.7.2.2  mycroft 			cwarn("%s", out);
    314  1.7.2.2  mycroft 			goto err;
    315  1.7.2.2  mycroft 		}
    316  1.7.2.2  mycroft 
    317  1.7.2.2  mycroft 	if (ferror(ifp) || fclose(ifp)) {
    318  1.7.2.2  mycroft 		cwarn("%s", in);
    319  1.7.2.2  mycroft 		goto err;
    320  1.7.2.2  mycroft 	}
    321  1.7.2.2  mycroft 	ifp = NULL;
    322  1.7.2.2  mycroft 
    323  1.7.2.2  mycroft 	if (fclose(ofp)) {
    324  1.7.2.2  mycroft 		cwarn("%s", out);
    325  1.7.2.2  mycroft 		goto err;
    326  1.7.2.2  mycroft 	}
    327  1.7.2.2  mycroft 
    328  1.7.2.2  mycroft 	if (isreg) {
    329  1.7.2.2  mycroft 		setfile(out, &sb);
    330  1.7.2.2  mycroft 
    331  1.7.2.2  mycroft 		if (unlink(in))
    332  1.7.2.2  mycroft 			cwarn("%s", in);
    333  1.7.2.2  mycroft 	}
    334  1.7.2.2  mycroft 	return;
    335  1.7.2.2  mycroft 
    336  1.7.2.2  mycroft err:	if (ofp) {
    337  1.7.2.2  mycroft 		if (oreg)
    338  1.7.2.2  mycroft 			(void)unlink(out);
    339  1.7.2.2  mycroft 		(void)fclose(ofp);
    340  1.7.2.2  mycroft 	}
    341  1.7.2.2  mycroft 	if (ifp)
    342  1.7.2.2  mycroft 		(void)fclose(ifp);
    343  1.7.2.2  mycroft }
    344  1.7.2.2  mycroft 
    345  1.7.2.2  mycroft void
    346  1.7.2.2  mycroft setfile(name, fs)
    347  1.7.2.2  mycroft 	char *name;
    348  1.7.2.2  mycroft 	register struct stat *fs;
    349  1.7.2.2  mycroft {
    350  1.7.2.2  mycroft 	static struct timeval tv[2];
    351  1.7.2.2  mycroft 
    352  1.7.2.2  mycroft 	fs->st_mode &= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
    353  1.7.2.2  mycroft 
    354  1.7.2.2  mycroft 	TIMESPEC_TO_TIMEVAL(&tv[0], &fs->st_atimespec);
    355  1.7.2.2  mycroft 	TIMESPEC_TO_TIMEVAL(&tv[1], &fs->st_mtimespec);
    356  1.7.2.2  mycroft 	if (utimes(name, tv))
    357  1.7.2.2  mycroft 		cwarn("utimes: %s", name);
    358  1.7.2.2  mycroft 
    359  1.7.2.2  mycroft 	/*
    360  1.7.2.2  mycroft 	 * Changing the ownership probably won't succeed, unless we're root
    361  1.7.2.2  mycroft 	 * or POSIX_CHOWN_RESTRICTED is not set.  Set uid/gid before setting
    362  1.7.2.2  mycroft 	 * the mode; current BSD behavior is to remove all setuid bits on
    363  1.7.2.2  mycroft 	 * chown.  If chown fails, lose setuid/setgid bits.
    364  1.7.2.2  mycroft 	 */
    365  1.7.2.2  mycroft 	if (chown(name, fs->st_uid, fs->st_gid)) {
    366  1.7.2.2  mycroft 		if (errno != EPERM)
    367  1.7.2.2  mycroft 			cwarn("chown: %s", name);
    368  1.7.2.2  mycroft 		fs->st_mode &= ~(S_ISUID|S_ISGID);
    369  1.7.2.2  mycroft 	}
    370  1.7.2.2  mycroft 	if (chmod(name, fs->st_mode))
    371  1.7.2.2  mycroft 		cwarn("chown: %s", name);
    372  1.7.2.2  mycroft 
    373  1.7.2.2  mycroft 	if (chflags(name, fs->st_flags))
    374  1.7.2.2  mycroft 		cwarn("chflags: %s", name);
    375  1.7.2.2  mycroft }
    376  1.7.2.2  mycroft 
    377  1.7.2.2  mycroft int
    378  1.7.2.2  mycroft permission(fname)
    379  1.7.2.2  mycroft 	char *fname;
    380  1.7.2.2  mycroft {
    381  1.7.2.2  mycroft 	int ch, first;
    382  1.7.2.2  mycroft 
    383  1.7.2.2  mycroft 	if (!isatty(fileno(stderr)))
    384  1.7.2.2  mycroft 		return (0);
    385  1.7.2.2  mycroft 	(void)fprintf(stderr, "overwrite %s? ", fname);
    386  1.7.2.2  mycroft 	first = ch = getchar();
    387  1.7.2.2  mycroft 	while (ch != '\n' && ch != EOF)
    388  1.7.2.2  mycroft 		ch = getchar();
    389  1.7.2.2  mycroft 	return (first == 'y');
    390  1.7.2.2  mycroft }
    391  1.7.2.2  mycroft 
    392  1.7.2.2  mycroft void
    393  1.7.2.2  mycroft usage(iscompress)
    394  1.7.2.2  mycroft 	int iscompress;
    395  1.7.2.2  mycroft {
    396  1.7.2.2  mycroft 	if (iscompress)
    397  1.7.2.2  mycroft 		(void)fprintf(stderr,
    398  1.7.2.2  mycroft 		    "usage: compress [-cfv] [-b bits] [file ...]\n");
    399  1.7.2.2  mycroft 	else
    400  1.7.2.2  mycroft 		(void)fprintf(stderr,
    401  1.7.2.2  mycroft 		    "usage: uncompress [-c] [-b bits] [file ...]\n");
    402  1.7.2.2  mycroft 	exit(1);
    403  1.7.2.2  mycroft }
    404  1.7.2.2  mycroft 
    405  1.7.2.2  mycroft void
    406  1.7.2.2  mycroft #if __STDC__
    407  1.7.2.2  mycroft cwarnx(const char *fmt, ...)
    408  1.7.2.2  mycroft #else
    409  1.7.2.2  mycroft cwarnx(fmt, va_alist)
    410  1.7.2.2  mycroft 	int eval;
    411  1.7.2.2  mycroft 	const char *fmt;
    412  1.7.2.2  mycroft 	va_dcl
    413  1.7.2.2  mycroft #endif
    414  1.7.2.2  mycroft {
    415  1.7.2.2  mycroft 	va_list ap;
    416  1.7.2.2  mycroft #if __STDC__
    417  1.7.2.2  mycroft 	va_start(ap, fmt);
    418  1.7.2.2  mycroft #else
    419  1.7.2.2  mycroft 	va_start(ap);
    420  1.7.2.2  mycroft #endif
    421  1.7.2.2  mycroft 	vwarnx(fmt, ap);
    422  1.7.2.2  mycroft 	va_end(ap);
    423  1.7.2.2  mycroft 	eval = 1;
    424  1.7.2.2  mycroft }
    425  1.7.2.2  mycroft 
    426  1.7.2.2  mycroft void
    427  1.7.2.2  mycroft #if __STDC__
    428  1.7.2.2  mycroft cwarn(const char *fmt, ...)
    429  1.7.2.2  mycroft #else
    430  1.7.2.2  mycroft cwarn(fmt, va_alist)
    431  1.7.2.2  mycroft 	int eval;
    432  1.7.2.2  mycroft 	const char *fmt;
    433  1.7.2.2  mycroft 	va_dcl
    434  1.7.2.2  mycroft #endif
    435  1.7.2.2  mycroft {
    436  1.7.2.2  mycroft 	va_list ap;
    437  1.7.2.2  mycroft #if __STDC__
    438  1.7.2.2  mycroft 	va_start(ap, fmt);
    439  1.7.2.2  mycroft #else
    440  1.7.2.2  mycroft 	va_start(ap);
    441  1.7.2.2  mycroft #endif
    442  1.7.2.2  mycroft 	vwarn(fmt, ap);
    443  1.7.2.2  mycroft 	va_end(ap);
    444  1.7.2.2  mycroft 	eval = 1;
    445  1.7.2.2  mycroft }
    446