suffix.c 2.6 KB

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