1 /* $OpenBSD: util.c,v 1.1 2018/12/17 19:26:25 anton Exp $ */
2
3 /*
4 * Copyright (c) 2018 Anton Lindqvist <anton@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <err.h>
20 #include <fcntl.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25
26 #include "util.h"
27
28 static __dead void usage(void);
29
30 int
dotest(int argc,char * argv[],const struct test * tests)31 dotest(int argc, char *argv[], const struct test *tests)
32 {
33 const struct test *test;
34 const char *dev = NULL;
35 int c, fd;
36
37 while ((c = getopt(argc, argv, "d:")) != -1)
38 switch (c) {
39 case 'd':
40 dev = optarg;
41 break;
42 default:
43 usage();
44 }
45 argc -= optind;
46 argv += optind;
47 if (dev == NULL || argc != 1)
48 usage();
49
50 fd = open(dev, O_RDWR);
51 if (fd == -1)
52 err(1, "open: %s", dev);
53
54 for (test = tests; test->t_name != NULL; test++) {
55 if (strcmp(argv[0], test->t_name) == 0)
56 break;
57 }
58 if (test->t_name == NULL)
59 errx(1, "%s: no such test", argv[0]);
60
61 return test->t_func(fd);
62 }
63
64 static __dead void
usage(void)65 usage(void)
66 {
67 fprintf(stderr, "usage: %s -d device test\n", getprogname());
68 exit(1);
69 }
70