suffix.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* This file is part of GNU tar.
  2. Copyright 2007, 2009, 2013-2014 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. { NULL }
  42. #undef S
  43. #undef __CAT2__
  44. };
  45. static struct compression_suffix const *
  46. find_compression_suffix (const char *name, size_t *ret_len)
  47. {
  48. char *suf = strrchr (name, '.');
  49. if (suf)
  50. {
  51. size_t len;
  52. struct compression_suffix *p;
  53. suf++;
  54. len = strlen (suf);
  55. for (p = compression_suffixes; p->suffix; p++)
  56. {
  57. if (p->length == len && memcmp (p->suffix, suf, len) == 0)
  58. {
  59. if (ret_len)
  60. *ret_len = strlen (name) - len - 1;
  61. return p;
  62. }
  63. }
  64. }
  65. return NULL;
  66. }
  67. static const char *
  68. find_compression_program (const char *name, const char *defprog)
  69. {
  70. struct compression_suffix const *p = find_compression_suffix (name, NULL);
  71. if (p)
  72. return p->program;
  73. return defprog;
  74. }
  75. void
  76. set_compression_program_by_suffix (const char *name, const char *defprog)
  77. {
  78. const char *program = find_compression_program (name, defprog);
  79. if (program)
  80. use_compress_program_option = program;
  81. }
  82. char *
  83. strip_compression_suffix (const char *name)
  84. {
  85. char *s = NULL;
  86. size_t len;
  87. if (find_compression_suffix (name, &len))
  88. {
  89. if (strncmp (name + len - 4, ".tar", 4) == 0)
  90. len -= 4;
  91. if (len == 0)
  92. return NULL;
  93. s = xmalloc (len + 1);
  94. memcpy (s, name, len);
  95. s[len] = 0;
  96. }
  97. return s;
  98. }