1228753Smm/*-
2228753Smm * Copyright (c) 2003-2007 Tim Kientzle
3228753Smm * All rights reserved.
4228753Smm *
5228753Smm * Redistribution and use in source and binary forms, with or without
6228753Smm * modification, are permitted provided that the following conditions
7228753Smm * are met:
8228753Smm * 1. Redistributions of source code must retain the above copyright
9228753Smm *    notice, this list of conditions and the following disclaimer.
10228753Smm * 2. Redistributions in binary form must reproduce the above copyright
11228753Smm *    notice, this list of conditions and the following disclaimer in the
12228753Smm *    documentation and/or other materials provided with the distribution.
13228753Smm *
14228753Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15228753Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16228753Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17228753Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18228753Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19228753Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20228753Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21228753Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22228753Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23228753Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24228753Smm */
25228753Smm
26228753Smm#include "archive_platform.h"
27228763Smm__FBSDID("$FreeBSD: releng/10.3/contrib/libarchive/libarchive/archive_write_set_format_by_name.c 248616 2013-03-22 13:36:03Z mm $");
28228753Smm
29228753Smm#ifdef HAVE_SYS_TYPES_H
30228753Smm#include <sys/types.h>
31228753Smm#endif
32228753Smm
33228753Smm#ifdef HAVE_ERRNO_H
34228753Smm#include <errno.h>
35228753Smm#endif
36228753Smm#ifdef HAVE_STRING_H
37228753Smm#include <string.h>
38228753Smm#endif
39228753Smm
40228753Smm#include "archive.h"
41228753Smm#include "archive_private.h"
42228753Smm
43228753Smm/* A table that maps names to functions. */
44228753Smmstatic
45228753Smmstruct { const char *name; int (*setter)(struct archive *); } names[] =
46228753Smm{
47232153Smm	{ "7zip",	archive_write_set_format_7zip },
48228753Smm	{ "ar",		archive_write_set_format_ar_bsd },
49228753Smm	{ "arbsd",	archive_write_set_format_ar_bsd },
50228753Smm	{ "argnu",	archive_write_set_format_ar_svr4 },
51228753Smm	{ "arsvr4",	archive_write_set_format_ar_svr4 },
52232153Smm	{ "bsdtar",	archive_write_set_format_pax_restricted },
53232153Smm	{ "cd9660",	archive_write_set_format_iso9660 },
54228753Smm	{ "cpio",	archive_write_set_format_cpio },
55232153Smm	{ "gnutar",	archive_write_set_format_gnutar },
56232153Smm	{ "iso",	archive_write_set_format_iso9660 },
57232153Smm	{ "iso9660",	archive_write_set_format_iso9660 },
58228753Smm	{ "mtree",	archive_write_set_format_mtree },
59248616Smm	{ "mtree-classic",	archive_write_set_format_mtree_classic },
60228753Smm	{ "newc",	archive_write_set_format_cpio_newc },
61228753Smm	{ "odc",	archive_write_set_format_cpio },
62248616Smm	{ "oldtar",	archive_write_set_format_v7tar },
63228753Smm	{ "pax",	archive_write_set_format_pax },
64232153Smm	{ "paxr",	archive_write_set_format_pax_restricted },
65228753Smm	{ "posix",	archive_write_set_format_pax },
66232153Smm	{ "rpax",	archive_write_set_format_pax_restricted },
67228753Smm	{ "shar",	archive_write_set_format_shar },
68228753Smm	{ "shardump",	archive_write_set_format_shar_dump },
69228753Smm	{ "ustar",	archive_write_set_format_ustar },
70248616Smm	{ "v7tar",	archive_write_set_format_v7tar },
71248616Smm	{ "v7",		archive_write_set_format_v7tar },
72232153Smm	{ "xar",	archive_write_set_format_xar },
73228753Smm	{ "zip",	archive_write_set_format_zip },
74228753Smm	{ NULL,		NULL }
75228753Smm};
76228753Smm
77228753Smmint
78228753Smmarchive_write_set_format_by_name(struct archive *a, const char *name)
79228753Smm{
80228753Smm	int i;
81228753Smm
82228753Smm	for (i = 0; names[i].name != NULL; i++) {
83228753Smm		if (strcmp(name, names[i].name) == 0)
84228753Smm			return ((names[i].setter)(a));
85228753Smm	}
86228753Smm
87228753Smm	archive_set_error(a, EINVAL, "No such format '%s'", name);
88232153Smm	a->state = ARCHIVE_STATE_FATAL;
89228753Smm	return (ARCHIVE_FATAL);
90228753Smm}
91