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