archive_write_set_format_by_name.c revision 229592
1129198Scognet/*-
2129198Scognet * Copyright (c) 2003-2007 Tim Kientzle
3139735Simp * All rights reserved.
4129198Scognet *
5129198Scognet * Redistribution and use in source and binary forms, with or without
6129198Scognet * modification, are permitted provided that the following conditions
7129198Scognet * are met:
8129198Scognet * 1. Redistributions of source code must retain the above copyright
9129198Scognet *    notice, this list of conditions and the following disclaimer.
10129198Scognet * 2. Redistributions in binary form must reproduce the above copyright
11129198Scognet *    notice, this list of conditions and the following disclaimer in the
12129198Scognet *    documentation and/or other materials provided with the distribution.
13129198Scognet *
14129198Scognet * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15129198Scognet * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16129198Scognet * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17129198Scognet * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18129198Scognet * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19129198Scognet * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20129198Scognet * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21129198Scognet * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22129198Scognet * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23129198Scognet * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24129198Scognet */
25129198Scognet
26129198Scognet#include "archive_platform.h"
27129198Scognet__FBSDID("$FreeBSD: stable/9/contrib/libarchive/libarchive/archive_write_set_format_by_name.c 229592 2012-01-05 12:06:54Z mm $");
28129198Scognet
29129198Scognet#ifdef HAVE_SYS_TYPES_H
30129198Scognet#include <sys/types.h>
31129198Scognet#endif
32129198Scognet
33129198Scognet#ifdef HAVE_ERRNO_H
34129198Scognet#include <errno.h>
35129198Scognet#endif
36129198Scognet#ifdef HAVE_STRING_H
37129198Scognet#include <string.h>
38129198Scognet#endif
39129198Scognet
40129198Scognet#include "archive.h"
41129198Scognet#include "archive_private.h"
42129198Scognet
43129198Scognet/* A table that maps names to functions. */
44129198Scognetstatic
45129198Scognetstruct { const char *name; int (*setter)(struct archive *); } names[] =
46129198Scognet{
47129198Scognet	{ "ar",		archive_write_set_format_ar_bsd },
48129198Scognet	{ "arbsd",	archive_write_set_format_ar_bsd },
49129198Scognet	{ "argnu",	archive_write_set_format_ar_svr4 },
50290661Smmel	{ "arsvr4",	archive_write_set_format_ar_svr4 },
51129198Scognet	{ "cpio",	archive_write_set_format_cpio },
52132055Scognet	{ "mtree",	archive_write_set_format_mtree },
53132055Scognet	{ "newc",	archive_write_set_format_cpio_newc },
54132055Scognet	{ "odc",	archive_write_set_format_cpio },
55338514Sjhb	{ "pax",	archive_write_set_format_pax },
56132055Scognet	{ "posix",	archive_write_set_format_pax },
57132471Scognet	{ "shar",	archive_write_set_format_shar },
58129198Scognet	{ "shardump",	archive_write_set_format_shar_dump },
59129198Scognet	{ "ustar",	archive_write_set_format_ustar },
60129198Scognet	{ "zip",	archive_write_set_format_zip },
61317002Smmel	{ NULL,		NULL }
62129198Scognet};
63129198Scognet
64129198Scognetint
65129198Scognetarchive_write_set_format_by_name(struct archive *a, const char *name)
66129198Scognet{
67129198Scognet	int i;
68129198Scognet
69129198Scognet	for (i = 0; names[i].name != NULL; i++) {
70129198Scognet		if (strcmp(name, names[i].name) == 0)
71290648Smmel			return ((names[i].setter)(a));
72290648Smmel	}
73129198Scognet
74290648Smmel	archive_set_error(a, EINVAL, "No such format '%s'", name);
75129198Scognet	return (ARCHIVE_FATAL);
76129198Scognet}
77129198Scognet