archive_write_set_format_by_name.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*-
  2. * Copyright (c) 2003-2007 Tim Kientzle
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "archive_platform.h"
  26. __FBSDID("$FreeBSD: src/lib/libarchive/archive_write_set_format_by_name.c,v 1.9 2008/09/01 02:50:53 kientzle Exp $");
  27. #ifdef HAVE_SYS_TYPES_H
  28. #include <sys/types.h>
  29. #endif
  30. #ifdef HAVE_ERRNO_H
  31. #include <errno.h>
  32. #endif
  33. #ifdef HAVE_STRING_H
  34. #include <string.h>
  35. #endif
  36. #include "archive.h"
  37. #include "archive_private.h"
  38. /* A table that maps names to functions. */
  39. static
  40. struct { const char *name; int (*setter)(struct archive *); } names[] =
  41. {
  42. { "ar", archive_write_set_format_ar_bsd },
  43. { "arbsd", archive_write_set_format_ar_bsd },
  44. { "argnu", archive_write_set_format_ar_svr4 },
  45. { "arsvr4", archive_write_set_format_ar_svr4 },
  46. { "cpio", archive_write_set_format_cpio },
  47. { "mtree", archive_write_set_format_mtree },
  48. { "newc", archive_write_set_format_cpio_newc },
  49. { "odc", archive_write_set_format_cpio },
  50. { "pax", archive_write_set_format_pax },
  51. { "posix", archive_write_set_format_pax },
  52. { "shar", archive_write_set_format_shar },
  53. { "shardump", archive_write_set_format_shar_dump },
  54. { "ustar", archive_write_set_format_ustar },
  55. { "zip", archive_write_set_format_zip },
  56. { NULL, NULL }
  57. };
  58. int
  59. archive_write_set_format_by_name(struct archive *a, const char *name)
  60. {
  61. int i;
  62. for (i = 0; names[i].name != NULL; i++) {
  63. if (strcmp(name, names[i].name) == 0)
  64. return ((names[i].setter)(a));
  65. }
  66. archive_set_error(a, EINVAL, "No such format '%s'", name);
  67. return (ARCHIVE_FATAL);
  68. }