File: suffix.c

package info (click to toggle)
tar 1.30+dfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 18,352 kB
  • sloc: ansic: 77,395; sh: 52,672; yacc: 1,842; makefile: 479; sed: 16
file content (116 lines) | stat: -rw-r--r-- 2,716 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* This file is part of GNU tar.
   Copyright 2007, 2009, 2013-2014, 2016-2017 Free Software Foundation,
   Inc.

   Written by Sergey Poznyakoff.

   GNU tar is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 3, or (at your option) any later
   version.

   GNU tar is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
   Public License for more details.

   You should have received a copy of the GNU General Public License along
   with GNU tar.  If not, see <http://www.gnu.org/licenses/>.  */

#include <system.h>
#include "common.h"

struct compression_suffix
{
  const char *suffix;
  size_t length;
  const char *program;
};

static struct compression_suffix compression_suffixes[] = {
#define __CAT2__(a,b) a ## b
#define S(s,p) #s, sizeof (#s) - 1, __CAT2__(p,_PROGRAM)
  { "tar", 3, NULL },
  { S(gz,   GZIP) },
  { S(tgz,  GZIP) },
  { S(taz,  GZIP) },
  { S(Z,    COMPRESS) },
  { S(taZ,  COMPRESS) },
  { S(bz2,  BZIP2) },
  { S(tbz,  BZIP2) },
  { S(tbz2, BZIP2) },
  { S(tz2,  BZIP2) },
  { S(lz,   LZIP) },
  { S(lzma, LZMA) },
  { S(tlz,  LZMA) },
  { S(lzo,  LZOP) },
  { S(xz,   XZ) },
  { S(txz,  XZ) }, /* Slackware */
  { S(zst,  ZSTD) },
  { NULL }
#undef S
#undef __CAT2__
};

static struct compression_suffix const *
find_compression_suffix (const char *name, size_t *ret_len)
{
  char *suf = strrchr (name, '.');

  if (suf)
    {
      size_t len;
      struct compression_suffix *p;
      
      suf++;
      len = strlen (suf);

      for (p = compression_suffixes; p->suffix; p++)
	{
	  if (p->length == len && memcmp (p->suffix, suf, len) == 0)
	    {
	      if (ret_len)
		*ret_len = strlen (name) - len - 1;
	      return p;
	    }
	}
    }
  return NULL;
}

static const char *
find_compression_program (const char *name, const char *defprog)
{
  struct compression_suffix const *p = find_compression_suffix (name, NULL);
  if (p)
    return p->program;
  return defprog;
}

void
set_compression_program_by_suffix (const char *name, const char *defprog)
{
  const char *program = find_compression_program (name, defprog);
  if (program)
    use_compress_program_option = program;
}

char *
strip_compression_suffix (const char *name)
{
  char *s = NULL;
  size_t len;

  if (find_compression_suffix (name, &len))
    {
      if (strncmp (name + len - 4, ".tar", 4) == 0)
	len -= 4;
      if (len == 0)
	return NULL;
      s = xmalloc (len + 1);
      memcpy (s, name, len);
      s[len] = 0;
    }
  return s;
}