summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormillert <millert@openbsd.org>2003-07-15 19:01:46 +0000
committermillert <millert@openbsd.org>2003-07-15 19:01:46 +0000
commit296fca835cc9093884c1cf8030efb34124a262eb (patch)
tree244198fbfd35a6459b4be3281ab7f97094e5a8f6
parenthptmask goes away as a register and frees one tmp cr (diff)
downloadwireguard-openbsd-296fca835cc9093884c1cf8030efb34124a262eb.tar.xz
wireguard-openbsd-296fca835cc9093884c1cf8030efb34124a262eb.zip
Don't compress a file that already has a known compression prefix.
This matches GNU gzip behavior.
-rw-r--r--usr.bin/compress/main.c90
1 files changed, 55 insertions, 35 deletions
diff --git a/usr.bin/compress/main.c b/usr.bin/compress/main.c
index a5f86702112..445ad8c0779 100644
--- a/usr.bin/compress/main.c
+++ b/usr.bin/compress/main.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: main.c,v 1.35 2003/07/14 18:57:31 millert Exp $ */
+/* $OpenBSD: main.c,v 1.36 2003/07/15 19:01:46 millert Exp $ */
static const char copyright[] =
"@(#) Copyright (c) 1992, 1993\n\
@@ -35,7 +35,7 @@ static const char license[] =
#if 0
static char sccsid[] = "@(#)compress.c 8.2 (Berkeley) 1/7/94";
#else
-static const char main_rcsid[] = "$OpenBSD: main.c,v 1.35 2003/07/14 18:57:31 millert Exp $";
+static const char main_rcsid[] = "$OpenBSD: main.c,v 1.36 2003/07/15 19:01:46 millert Exp $";
#endif
#endif /* not lint */
@@ -94,7 +94,8 @@ int compress(const char *, const char *, const struct compressor *,
int decompress(const char *, const char *, const struct compressor *,
int, struct stat *);
const struct compressor *check_method(int, struct stat *, const char *);
-char *set_outfile(char *, char *, size_t);
+const char *check_suffix(const char *);
+char *set_outfile(const char *, char *, size_t);
#define OPTSTRING "123456789ab:cdfghlLnNOo:qrS:tvV"
const struct option longopts[] = {
@@ -127,7 +128,8 @@ main(int argc, char *argv[])
FTSENT *entry;
struct stat osb;
const struct compressor *method;
- char *p, *s, *infile;
+ const char *s;
+ char *p, *infile;
char outfile[MAXPATHLEN], _infile[MAXPATHLEN], suffix[16];
char *nargv[512]; /* some estimate based on ARG_MAX */
int exists, oreg, ch, error, i, rc, oflag;
@@ -159,11 +161,11 @@ main(int argc, char *argv[])
strlcpy(suffix, method->suffix, sizeof(suffix));
nargv[0] = NULL;
- if ((s = getenv("GZIP")) != NULL) {
+ if ((p = getenv("GZIP")) != NULL) {
char *last;
nargv[0] = *argv++;
- for (i = 1, (p = strtok_r(s, " ", &last)); p;
+ for (i = 1, (p = strtok_r(p, " ", &last)); p != NULL;
(p = strtok_r(NULL, " ", &last)), i++)
if (i < sizeof(nargv)/sizeof(nargv[1]) - argc - 1)
nargv[i] = p;
@@ -342,6 +344,12 @@ main(int argc, char *argv[])
break;
}
+ if (!decomp && !pipin && (s = check_suffix(infile)) != NULL) {
+ warnx("%s already has %s suffix -- unchanged",
+ infile, s);
+ continue;
+ }
+
if (testmode)
strlcpy(outfile, _PATH_DEVNULL, sizeof outfile);
else if (cat)
@@ -630,44 +638,56 @@ permission(const char *fname)
}
/*
- * Set outfile based on the suffix. In most cases we just strip
- * off the suffix but things like .tgz and .taz are special.
+ * Check infile for a known suffix and return the suffix portion or NULL.
*/
-char *
-set_outfile(char *infile, char *outfile, size_t osize)
+const char *
+check_suffix(const char *infile)
{
int i;
- char *s;
- static char *suffixes[] = { ".Z", ".gz", ".z", ".tgz", ".taz",
- "-Z", "-gz", "-z", "_Z", "_gz", "_z",
- NULL };
-
- if ((s = strrchr(infile, '.')) == NULL &&
- (s = strrchr(infile, '-')) == NULL &&
- (s = strrchr(infile, '_')) == NULL)
- return (NULL);
+ char *suf, *sep, *separators = ".-_";
+ static char *suffixes[] = { "Z", "gz", "z", "tgz", "taz", NULL };
- for (i = 0; suffixes[i] != NULL; i++) {
- if (strcmp(s, suffixes[i]) == 0) {
- (void)strlcpy(outfile, infile, osize);
- s = outfile + (s - infile);
- /*
- * Convert .tgz and .taz -> .tar,
- * else drop the suffix.
- */
- if (strcmp(s, ".tgz") == 0) {
- s[2] = 'a';
- s[3] = 'r';
- } else if (strcmp(s, ".taz") == 0)
- s[3] = 'r';
- else
- s[0] = '\0';
- return (outfile);
+ for (sep = separators; *sep != '\0'; sep++) {
+ if ((suf = strrchr(infile, *sep)) == NULL)
+ continue;
+ suf++;
+
+ for (i = 0; suffixes[i] != NULL; i++) {
+ if (strcmp(suf, suffixes[i]) == 0)
+ return (suf - 1);
}
}
return (NULL);
}
+/*
+ * Set outfile based on the suffix. In most cases we just strip
+ * off the suffix but things like .tgz and .taz are special.
+ */
+char *
+set_outfile(const char *infile, char *outfile, size_t osize)
+{
+ const char *s;
+ char *cp;
+
+ if ((s = check_suffix(infile)) == NULL)
+ return (NULL);
+
+ (void)strlcpy(outfile, infile, osize);
+ cp = outfile + (s - infile) + 1;
+ /*
+ * Convert tgz and taz -> tar, else drop the suffix.
+ */
+ if (strcmp(cp, "tgz") == 0) {
+ cp[1] = 'a';
+ cp[2] = 'r';
+ } else if (strcmp(cp, "taz") == 0)
+ cp[2] = 'r';
+ else
+ cp[-1] = '\0';
+ return (outfile);
+}
+
__dead void
usage(int status)
{