suffix.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* This file is part of GNU tar.
  2. Copyright 2007-2022 Free Software Foundation, Inc.
  3. Written by Sergey Poznyakoff.
  4. GNU tar is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GNU tar is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  11. Public License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with GNU tar. If not, see <http://www.gnu.org/licenses/>. */
  14. #include <system.h>
  15. #include "common.h"
  16. struct compression_suffix
  17. {
  18. const char *suffix;
  19. size_t length;
  20. const char *program;
  21. };
  22. static struct compression_suffix compression_suffixes[] = {
  23. #define __CAT2__(a,b) a ## b
  24. #define S(s,p) #s, sizeof (#s) - 1, __CAT2__(p,_PROGRAM)
  25. { "tar", 3, NULL },
  26. { S(gz, GZIP) },
  27. { S(tgz, GZIP) },
  28. { S(taz, GZIP) },
  29. { S(Z, COMPRESS) },
  30. { S(taZ, COMPRESS) },
  31. { S(bz2, BZIP2) },
  32. { S(tbz, BZIP2) },
  33. { S(tbz2, BZIP2) },
  34. { S(tz2, BZIP2) },
  35. { S(lz, LZIP) },
  36. { S(lzma, LZMA) },
  37. { S(tlz, LZMA) },
  38. { S(lzo, LZOP) },
  39. { S(xz, XZ) },
  40. { S(txz, XZ) }, /* Slackware */
  41. { S(zst, ZSTD) },
  42. { S(tzst, ZSTD) },
  43. { NULL }
  44. #undef S
  45. #undef __CAT2__
  46. };
  47. static struct compression_suffix const *
  48. find_compression_suffix (const char *name, size_t *ret_len)
  49. {
  50. char *suf = strrchr (name, '.');
  51. if (suf)
  52. {
  53. size_t len;
  54. struct compression_suffix *p;
  55. suf++;
  56. len = strlen (suf);
  57. for (p = compression_suffixes; p->suffix; p++)
  58. {
  59. if (p->length == len && memcmp (p->suffix, suf, len) == 0)
  60. {
  61. if (ret_len)
  62. *ret_len = strlen (name) - len - 1;
  63. return p;
  64. }
  65. }
  66. }
  67. return NULL;
  68. }
  69. static const char *
  70. find_compression_program (const char *name, const char *defprog)
  71. {
  72. struct compression_suffix const *p = find_compression_suffix (name, NULL);
  73. if (p)
  74. return p->program;
  75. return defprog;
  76. }
  77. void
  78. set_compression_program_by_suffix (const char *name, const char *defprog)
  79. {
  80. const char *program = find_compression_program (name, defprog);
  81. if (program)
  82. use_compress_program_option = program;
  83. }
  84. char *
  85. strip_compression_suffix (const char *name)
  86. {
  87. char *s = NULL;
  88. size_t len;
  89. struct compression_suffix const *p = find_compression_suffix (name, &len);
  90. if (p)
  91. {
  92. /* Strip an additional ".tar" suffix, but only if the just-stripped
  93. "outer" suffix did not begin with "t". */
  94. if (len > 4 && strncmp (name + len - 4, ".tar", 4) == 0
  95. && p->suffix[0] != 't')
  96. len -= 4;
  97. if (len == 0)
  98. return NULL;
  99. s = xmalloc (len + 1);
  100. memcpy (s, name, len);
  101. s[len] = 0;
  102. }
  103. return s;
  104. }