suffix.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* This file is part of GNU tar.
  2. Copyright (C) 2007 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. struct compression_suffix compression_suffixes[] = {
  23. #define S(s,p) #s, sizeof (#s) - 1, #p
  24. { S(gz, gzip) },
  25. { S(tgz, gzip) },
  26. { S(taz, gzip) },
  27. { S(Z, compress) },
  28. { S(taZ, compress) },
  29. { S(bz2, bzip2) },
  30. { S(tbz, bzip2) },
  31. { S(tbz2, bzip2) },
  32. { S(tz2, bzip2) },
  33. { S(lzma, lzma) },
  34. { S(tlz, lzma) },
  35. #undef S
  36. };
  37. int nsuffixes = sizeof (compression_suffixes) /
  38. sizeof (compression_suffixes[0]);
  39. static const char *
  40. find_compression_program (const char *name, const char *defprog)
  41. {
  42. char *suf = strrchr (name, '.');
  43. if (suf)
  44. {
  45. int i;
  46. size_t len;
  47. suf++;
  48. len = strlen (suf);
  49. for (i = 0; i < nsuffixes; i++)
  50. {
  51. if (compression_suffixes[i].length == len
  52. && memcmp (compression_suffixes[i].suffix, suf, len) == 0)
  53. return compression_suffixes[i].program;
  54. }
  55. }
  56. return defprog;
  57. }
  58. void
  59. set_comression_program_by_suffix (const char *name, const char *defprog)
  60. {
  61. const char *program = find_compression_program (name, defprog);
  62. if (program)
  63. use_compress_program_option = program;
  64. }