1 ///////////////////////////////////////////////////////////////////////////////
4 /// \brief Checks filename suffix and creates the destination filename
6 // Copyright (C) 2007 Lasse Collin
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
18 ///////////////////////////////////////////////////////////////////////////////
23 static char *custom_suffix = NULL;
27 const char *compressed;
28 const char *uncompressed;
32 /// \brief Checks if src_name has given compressed_suffix
34 /// \param suffix Filename suffix to look for
35 /// \param src_name Input filename
36 /// \param src_len strlen(src_name)
38 /// \return If src_name has the suffix, src_len - strlen(suffix) is
39 /// returned. It's always a positive integer. Otherwise zero
42 test_suffix(const char *suffix, const char *src_name, size_t src_len)
44 const size_t suffix_len = strlen(suffix);
46 // The filename must have at least one character in addition to
47 // the suffix. src_name may contain path to the filename, so we
48 // need to check for directory separator too.
49 if (src_len <= suffix_len || src_name[src_len - suffix_len - 1] == '/')
52 if (strcmp(suffix, src_name + src_len - suffix_len) == 0)
53 return src_len - suffix_len;
59 /// \brief Removes the filename suffix of the compressed file
61 /// \return Name of the uncompressed file, or NULL if file has unknown
64 uncompressed_name(const char *src_name, const size_t src_len)
66 static const struct suffix_pair suffixes[] = {
68 { ".txz", ".tar" }, // .txz abbreviation for .txt.gz is rare.
72 // { ".tgz", ".tar" },
75 const char *new_suffix = "";
78 if (opt_format == FORMAT_RAW) {
79 // Don't check for known suffixes when --format=raw was used.
80 if (custom_suffix == NULL) {
81 message_error(_("%s: With --format=raw, "
82 "--suffix=.SUF is required unless "
83 "writing to stdout"), src_name);
87 for (size_t i = 0; i < ARRAY_SIZE(suffixes); ++i) {
88 new_len = test_suffix(suffixes[i].compressed,
91 new_suffix = suffixes[i].uncompressed;
97 if (new_len == 0 && custom_suffix != NULL)
98 new_len = test_suffix(custom_suffix, src_name, src_len);
101 message_warning(_("%s: Filename has an unknown suffix, "
102 "skipping"), src_name);
106 const size_t new_suffix_len = strlen(new_suffix);
107 char *dest_name = xmalloc(new_len + new_suffix_len + 1);
109 memcpy(dest_name, src_name, new_len);
110 memcpy(dest_name + new_len, new_suffix, new_suffix_len);
111 dest_name[new_len + new_suffix_len] = '\0';
117 /// \brief Appends suffix to src_name
119 /// In contrast to uncompressed_name(), we check only suffixes that are valid
120 /// for the specified file format.
122 compressed_name(const char *src_name, const size_t src_len)
124 // The order of these must match the order in args.h.
125 static const struct suffix_pair all_suffixes[][3] = {
141 // --format=raw requires specifying the suffix
142 // manually or using stdout.
147 // args.c ensures this.
148 assert(opt_format != FORMAT_AUTO);
150 const size_t format = opt_format - 1;
151 const struct suffix_pair *const suffixes = all_suffixes[format];
153 for (size_t i = 0; suffixes[i].compressed != NULL; ++i) {
154 if (test_suffix(suffixes[i].compressed, src_name, src_len)
156 message_warning(_("%s: File already has `%s' "
157 "suffix, skipping"), src_name,
158 suffixes[i].compressed);
163 // TODO: Hmm, maybe it would be better to validate this in args.c,
164 // since the suffix handling when decoding is weird now.
165 if (opt_format == FORMAT_RAW && custom_suffix == NULL) {
166 message_error(_("%s: With --format=raw, "
167 "--suffix=.SUF is required unless "
168 "writing to stdout"), src_name);
172 const char *suffix = custom_suffix != NULL
173 ? custom_suffix : suffixes[0].compressed;
174 const size_t suffix_len = strlen(suffix);
176 char *dest_name = xmalloc(src_len + suffix_len + 1);
178 memcpy(dest_name, src_name, src_len);
179 memcpy(dest_name + src_len, suffix, suffix_len);
180 dest_name[src_len + suffix_len] = '\0';
187 suffix_get_dest_name(const char *src_name)
189 assert(src_name != NULL);
191 // Length of the name is needed in all cases to locate the end of
192 // the string to compare the suffix, so calculate the length here.
193 const size_t src_len = strlen(src_name);
195 return opt_mode == MODE_COMPRESS
196 ? compressed_name(src_name, src_len)
197 : uncompressed_name(src_name, src_len);
202 suffix_set(const char *suffix)
204 // Empty suffix and suffixes having a slash are rejected. Such
205 // suffixes would break things later.
206 if (suffix[0] == '\0' || strchr(suffix, '/') != NULL)
207 message_fatal(_("%s: Invalid filename suffix"), optarg);
209 // Replace the old custom_suffix (if any) with the new suffix.
211 custom_suffix = xstrdup(suffix);