suffix.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* This file is part of GNU tar.
  2. Copyright (C) 2007, 2009 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. { S(gz, GZIP) },
  26. { S(tgz, GZIP) },
  27. { S(taz, GZIP) },
  28. { S(Z, COMPRESS) },
  29. { S(taZ, COMPRESS) },
  30. { S(bz2, BZIP2) },
  31. { S(tbz, BZIP2) },
  32. { S(tbz2, BZIP2) },
  33. { S(tz2, BZIP2) },
  34. { S(lz, LZIP) },
  35. { S(lzma, LZMA) },
  36. { S(tlz, LZMA) },
  37. { S(lzo, LZOP) },
  38. { S(xz, XZ) },
  39. #undef S
  40. #undef __CAT2__
  41. };
  42. static int nsuffixes = sizeof (compression_suffixes) /
  43. sizeof (compression_suffixes[0]);
  44. static const char *
  45. find_compression_program (const char *name, const char *defprog)
  46. {
  47. char *suf = strrchr (name, '.');
  48. if (suf)
  49. {
  50. int i;
  51. size_t len;
  52. suf++;
  53. len = strlen (suf);
  54. for (i = 0; i < nsuffixes; i++)
  55. {
  56. if (compression_suffixes[i].length == len
  57. && memcmp (compression_suffixes[i].suffix, suf, len) == 0)
  58. return compression_suffixes[i].program;
  59. }
  60. }
  61. return defprog;
  62. }
  63. void
  64. set_compression_program_by_suffix (const char *name, const char *defprog)
  65. {
  66. const char *program = find_compression_program (name, defprog);
  67. if (program)
  68. use_compress_program_option = program;
  69. }