1 /* This file is part of GNU tar.
2 Copyright 2007-2024 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
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/>. */
22 struct compression_suffix
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)
49 { S(txz
, XZ
) }, /* Slackware */
57 /* Extract the suffix from archive file NAME, and return a pointer to
58 compression_suffix associated with it or NULL if none is found.
59 No matter what is the return value, if RET_LEN is not NULL, store
60 there the length of NAME with that suffix stripped, or 0 if NAME has
62 static struct compression_suffix
const *
63 find_compression_suffix (const char *name
, size_t *ret_len
)
65 char *suf
= strrchr (name
, '.');
67 if (suf
&& suf
[1] != 0 && suf
[1] != '/')
70 struct compression_suffix
*p
;
75 *ret_len
= strlen (name
) - len
- 1;
77 for (p
= compression_suffixes
; p
->suffix
; p
++)
79 if (p
->length
== len
&& memcmp (p
->suffix
, suf
, len
) == 0)
90 /* Select compression program using the suffix of the archive file NAME.
91 Use DEFPROG, if there is no suffix, or if no program is associated with
92 the suffix. In the latter case, if VERBOSE is true, issue a warning.
95 set_compression_program_by_suffix (const char *name
, const char *defprog
,
99 struct compression_suffix
const *p
= find_compression_suffix (name
, &len
);
101 use_compress_program_option
= p
->program
;
104 use_compress_program_option
= defprog
;
105 if (len
> 0 && verbose
)
107 _("no compression program is defined for suffix '%s';"
110 defprog
? defprog
: "uncompressed archive"));
115 strip_compression_suffix (const char *name
)
119 struct compression_suffix
const *p
= find_compression_suffix (name
, &len
);
123 /* Strip an additional ".tar" suffix, but only if the just-stripped
124 "outer" suffix did not begin with "t". */
125 if (len
> 4 && strncmp (name
+ len
- 4, ".tar", 4) == 0
126 && p
->suffix
[0] != 't')
130 s
= xmalloc (len
+ 1);
131 memcpy (s
, name
, len
);