suffix.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 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. { S(lzo, lzop) },
  36. { S(xz, xz) },
  37. #undef S
  38. };
  39. static int nsuffixes = sizeof (compression_suffixes) /
  40. sizeof (compression_suffixes[0]);
  41. static const char *
  42. find_compression_program (const char *name, const char *defprog)
  43. {
  44. char *suf = strrchr (name, '.');
  45. if (suf)
  46. {
  47. int i;
  48. size_t len;
  49. suf++;
  50. len = strlen (suf);
  51. for (i = 0; i < nsuffixes; i++)
  52. {
  53. if (compression_suffixes[i].length == len
  54. && memcmp (compression_suffixes[i].suffix, suf, len) == 0)
  55. return compression_suffixes[i].program;
  56. }
  57. }
  58. return defprog;
  59. }
  60. void
  61. set_comression_program_by_suffix (const char *name, const char *defprog)
  62. {
  63. const char *program = find_compression_program (name, defprog);
  64. if (program)
  65. use_compress_program_option = program;
  66. }