PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/src/format.c

https://github.com/pinard/paxutils
C | 150 lines | 94 code | 23 blank | 33 comment | 7 complexity | 210de9a4a9e53aadde77271d3d819ea9 MD5 | raw file
  1. /* format.c - Deal with format names.
  2. Copyright (C) 1990, 1991, 1992, 1998, 1999 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
  14. /* Written by Tom Tromey <tromey@drip.colorado.edu>. */
  15. #include "system.h"
  16. #include <assert.h>
  17. #include "common.h"
  18. struct format_list
  19. {
  20. char *name;
  21. enum archive_format value;
  22. };
  23. /* Canonical names, which must come first so that format_name() will work. */
  24. static struct format_list the_list[] =
  25. {
  26. {"crc", CRC_ASCII_FORMAT},
  27. {"newc", NEW_ASCII_FORMAT},
  28. {"odc", OLD_ASCII_FORMAT},
  29. {"bin", BINARY_FORMAT},
  30. {"ustar", POSIX_FORMAT},
  31. {"tar", V7_FORMAT},
  32. #ifdef CPIO_USE_GNUTAR
  33. {"gnutar", GNUTAR_FORMAT},
  34. #endif
  35. {"hpodc", HPUX_OLD_ASCII_FORMAT},
  36. {"hpbin", HPUX_BINARY_FORMAT},
  37. /* Names for compatibility. */
  38. /* These are from BSDI pax. */
  39. {"cpio", OLD_ASCII_FORMAT}, /* FIXME? */
  40. {"bcpio", BINARY_FORMAT},
  41. {"sv4cpio", NEW_ASCII_FORMAT},
  42. {"sv4crc", CRC_ASCII_FORMAT},
  43. {NULL, UNKNOWN_FORMAT}
  44. };
  45. /*-------------------------------------------------------------------------.
  46. | Return format value given name of format. If format isn't found, return |
  47. | UNKNOWN_FORMAT. |
  48. `-------------------------------------------------------------------------*/
  49. enum archive_format
  50. find_format (char *format)
  51. {
  52. struct format_list *fl;
  53. for (fl = the_list; fl->name != NULL; fl++)
  54. {
  55. if (! strcasecmp (format, fl->name))
  56. break;
  57. }
  58. return fl->value;
  59. }
  60. /*------------------------------------------------.
  61. | Print standard 'format unknown' error message. |
  62. `------------------------------------------------*/
  63. void
  64. format_error (char *format)
  65. {
  66. error (2, 0, _("\
  67. invalid archive format `%s'; valid formats are:\n\
  68. crc, newc, odc, bin, ustar, tar, hpodc, hpbin"),
  69. format);
  70. }
  71. /*------------------------------------------.
  72. | Set up hooks for writing a given format. |
  73. `------------------------------------------*/
  74. void
  75. set_write_pointers_from_format (enum archive_format format)
  76. {
  77. switch (format)
  78. {
  79. case BINARY_FORMAT:
  80. case HPUX_BINARY_FORMAT:
  81. header_writer = write_out_oldcpio_header;
  82. eof_writer = write_cpio_eof;
  83. name_too_long = is_cpio_filename_too_long;
  84. break;
  85. case OLD_ASCII_FORMAT:
  86. case HPUX_OLD_ASCII_FORMAT:
  87. header_writer = write_out_oldascii_header;
  88. eof_writer = write_cpio_eof;
  89. name_too_long = is_cpio_filename_too_long;
  90. break;
  91. case NEW_ASCII_FORMAT:
  92. case CRC_ASCII_FORMAT:
  93. header_writer = write_out_cpioascii_header;
  94. eof_writer = write_cpio_eof;
  95. name_too_long = is_cpio_filename_too_long;
  96. break;
  97. case V7_FORMAT:
  98. case POSIX_FORMAT:
  99. case GNUTAR_FORMAT:
  100. header_writer = write_out_tar_header;
  101. eof_writer = write_tar_eof;
  102. name_too_long = is_tar_filename_too_long;
  103. break;
  104. case UNKNOWN_FORMAT:
  105. default:
  106. assert (0);
  107. break;
  108. }
  109. }
  110. /*------------------------.
  111. | Return name of format. |
  112. `------------------------*/
  113. char *
  114. format_name (enum archive_format fmt)
  115. {
  116. struct format_list *fl;
  117. for (fl = the_list; fl->name; fl++)
  118. {
  119. if (fl->value == fmt)
  120. return fl->name;
  121. }
  122. assert (0);
  123. return NULL; /* placate compiler */
  124. }