test.c revision 150305
1150305Smarcel/*-
2150305Smarcel * Copyright (c) 2005 Marcel Moolenaar
3150305Smarcel * All rights reserved.
4150305Smarcel *
5150305Smarcel * Redistribution and use in source and binary forms, with or without
6150305Smarcel * modification, are permitted provided that the following conditions
7150305Smarcel * are met:
8150305Smarcel *
9150305Smarcel * 1. Redistributions of source code must retain the above copyright
10150305Smarcel *    notice, this list of conditions and the following disclaimer.
11150305Smarcel * 2. Redistributions in binary form must reproduce the above copyright
12150305Smarcel *    notice, this list of conditions and the following disclaimer in the
13150305Smarcel *    documentation and/or other materials provided with the distribution.
14150305Smarcel *
15150305Smarcel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16150305Smarcel * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17150305Smarcel * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18150305Smarcel * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19150305Smarcel * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20150305Smarcel * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21150305Smarcel * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22150305Smarcel * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23150305Smarcel * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24150305Smarcel * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25150305Smarcel */
26150305Smarcel
27150305Smarcel#include <sys/cdefs.h>
28150305Smarcel__FBSDID("$FreeBSD: head/tools/regression/geom_gpt/test.c 150305 2005-09-19 02:59:09Z marcel $");
29150305Smarcel
30150305Smarcel#include <errno.h>
31150305Smarcel#include <libgeom.h>
32150305Smarcel#include <limits.h>
33150305Smarcel#include <stdio.h>
34150305Smarcel#include <stdlib.h>
35150305Smarcel#include <string.h>
36150305Smarcel#include <unistd.h>
37150305Smarcel
38150305Smarcelint verbose;
39150305Smarcel
40150305Smarcelstatic void
41150305Smarcelusage()
42150305Smarcel{
43150305Smarcel	fprintf(stdout, "usage: %s [-v] param ...\n", getprogname());
44150305Smarcel	exit(1);
45150305Smarcel}
46150305Smarcel
47150305Smarcelstatic int
48150305Smarcelparse(char *arg, char **param, char **value)
49150305Smarcel{
50150305Smarcel	char *e;
51150305Smarcel
52150305Smarcel	*param = arg;
53150305Smarcel	e = strchr(arg, '=');
54150305Smarcel	if (e != NULL) {
55150305Smarcel		*e = '\0';
56150305Smarcel		*value = e + 1;
57150305Smarcel	} else
58150305Smarcel		*value = NULL;
59150305Smarcel	return (0);
60150305Smarcel}
61150305Smarcel
62150305Smarcelstatic int
63150305Smarceladd_int32(struct gctl_req *req, char *arg)
64150305Smarcel{
65150305Smarcel	char *param, *nptr, *endptr;
66150305Smarcel	uint32_t value;
67150305Smarcel	int error;
68150305Smarcel
69150305Smarcel	error = parse(arg, &param, &nptr);
70150305Smarcel	if (error)
71150305Smarcel		return (error);
72150305Smarcel	if (nptr == NULL)
73150305Smarcel		return (EINVAL);
74150305Smarcel	value = strtol(nptr, &endptr, 0);
75150305Smarcel	if (*endptr)
76150305Smarcel		return (EINVAL);
77150305Smarcel	gctl_ro_param(req, param, sizeof(value), &value);
78150305Smarcel	return (0);
79150305Smarcel}
80150305Smarcel
81150305Smarcelstatic int
82150305Smarceladd_string(struct gctl_req *req, char *arg)
83150305Smarcel{
84150305Smarcel	char *param, *value;
85150305Smarcel	int error;
86150305Smarcel
87150305Smarcel	error = parse(arg, &param, &value);
88150305Smarcel	if (error)
89150305Smarcel		return (error);
90150305Smarcel	if (value == NULL)
91150305Smarcel		return (EINVAL);
92150305Smarcel
93150305Smarcel	gctl_ro_param(req, param, -1, value);
94150305Smarcel	return (0);
95150305Smarcel}
96150305Smarcel
97150305Smarcelint main(int argc, char *argv[])
98150305Smarcel{
99150305Smarcel	struct gctl_req *req;
100150305Smarcel	const char *s;
101150305Smarcel	int c;
102150305Smarcel
103150305Smarcel	req = gctl_get_handle();
104150305Smarcel	gctl_ro_param(req, "class", -1, "GPT");
105150305Smarcel
106150305Smarcel	while ((c = getopt(argc, argv, "4:s:v")) != -1) {
107150305Smarcel		switch (c) {
108150305Smarcel		case '4':	/* uint32_t */
109150305Smarcel			add_int32(req, optarg);
110150305Smarcel			break;
111150305Smarcel		case 's':	/* string */
112150305Smarcel			add_string(req, optarg);
113150305Smarcel			break;
114150305Smarcel		case 'v':
115150305Smarcel			verbose = 1;
116150305Smarcel			break;
117150305Smarcel		case '?':
118150305Smarcel		default:
119150305Smarcel			usage();
120150305Smarcel			/* NOTREACHED */
121150305Smarcel			break;
122150305Smarcel		}
123150305Smarcel	}
124150305Smarcel	if (argc != optind)
125150305Smarcel		usage();
126150305Smarcel
127150305Smarcel	if (verbose)
128150305Smarcel		gctl_dump(req, stdout);
129150305Smarcel
130150305Smarcel	s = gctl_issue(req);
131150305Smarcel	if (s != NULL)
132150305Smarcel		printf("FAIL %s\n", s);
133150305Smarcel	else
134150305Smarcel		printf("PASS\n");
135150305Smarcel	gctl_free(req);
136150305Smarcel	return (0);
137150305Smarcel}
138