Improve handling of --test-label.
[tar/ericb.git] / src / suffix.c
blobcd9c01a2ee87e1dae6f8c988ac151908e2a18c7d
1 /* This file is part of GNU tar.
2 Copyright (C) 2007, 2009 Free Software Foundation, Inc.
4 Written by Sergey Poznyakoff.
6 GNU tar is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any later
9 version.
11 GNU tar is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
14 Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with GNU tar. If not, see <http://www.gnu.org/licenses/>. */
19 #include <system.h>
20 #include "common.h"
22 struct compression_suffix
24 const char *suffix;
25 size_t length;
26 const char *program;
29 static struct compression_suffix compression_suffixes[] = {
30 #define __CAT2__(a,b) a ## b
31 #define S(s,p) #s, sizeof (#s) - 1, __CAT2__(p,_PROGRAM)
32 { S(gz, GZIP) },
33 { S(tgz, GZIP) },
34 { S(taz, GZIP) },
35 { S(Z, COMPRESS) },
36 { S(taZ, COMPRESS) },
37 { S(bz2, BZIP2) },
38 { S(tbz, BZIP2) },
39 { S(tbz2, BZIP2) },
40 { S(tz2, BZIP2) },
41 { S(lzma, LZMA) },
42 { S(tlz, LZMA) },
43 { S(lzo, LZOP) },
44 { S(xz, XZ) },
45 #undef S
46 #undef __CAT2__
49 static int nsuffixes = sizeof (compression_suffixes) /
50 sizeof (compression_suffixes[0]);
52 static const char *
53 find_compression_program (const char *name, const char *defprog)
55 char *suf = strrchr (name, '.');
57 if (suf)
59 int i;
60 size_t len;
62 suf++;
63 len = strlen (suf);
65 for (i = 0; i < nsuffixes; i++)
67 if (compression_suffixes[i].length == len
68 && memcmp (compression_suffixes[i].suffix, suf, len) == 0)
69 return compression_suffixes[i].program;
72 return defprog;
75 void
76 set_comression_program_by_suffix (const char *name, const char *defprog)
78 const char *program = find_compression_program (name, defprog);
79 if (program)
80 use_compress_program_option = program;