summaryrefslogtreecommitdiffstats
path: root/usr.sbin/digictl/digictl.c
blob: ffc8648ebbb7bae5ca64c5bc2201210e500f4cf0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*-
 * Copyright (c) 2001 Brian Somers <brian@Awfulhak.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 */

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <sys/ioctl.h>
#include <unistd.h>

#include "dev/digi/digiio.h"

static int
usage(const char *prog)
{
	fprintf(stderr, "Usage: %s [-d debug] [-ir] device...\n", prog);
	return (EX_USAGE);
}

int
main(int argc, char **argv)
{
	char namedata[256], *name = namedata;
	const char *prog;
	enum digi_model model;
	int ch, debug, fd, i, res;
	int dflag, iflag, rflag;

	if ((prog = strrchr(argv[0], '/')) == NULL)
		prog = argv[0];
	else
		prog++;

	dflag = iflag = rflag = 0;
	while ((ch = getopt(argc, argv, "d:ir")) != -1)
		switch (ch) {
		case 'd':
			dflag = 1;
			debug = atoi(optarg);
			break;
		case 'i':
			iflag = 1;
			break;
		case 'r':
			rflag = 1;
			break;
		default:
			return usage(prog);
		}

	if (argc < optind)
		return usage(prog);

	res = 0;
	for (i = optind; i < argc; i++) {
		if ((fd = open(argv[i], O_RDONLY)) == -1) {
			fprintf(stderr, "%s: %s: open: %s\n", prog, argv[i],
			    strerror(errno));
			res++;
			continue;
		}

		if (dflag && ioctl(fd, DIGIIO_DEBUG, &debug) != 0) {
			fprintf(stderr, "%s: %s: debug: %s\n",
			    prog, argv[i], strerror(errno));
			res++;
		}

		if (iflag) {
			if (ioctl(fd, DIGIIO_MODEL, &model) != 0) {
				fprintf(stderr, "%s: %s: model: %s\n",
				    prog, argv[i], strerror(errno));
				res++;
			} else if (ioctl(fd, DIGIIO_IDENT, &name) != 0) {
				fprintf(stderr, "%s: %s: ident: %s\n",
				    prog, argv[i], strerror(errno));
				res++;
			} else
				printf("%s: %s (type %d)\n",
				    argv[i], name, (int)model);
		}

		if (rflag && ioctl(fd, DIGIIO_REINIT) != 0) {
			fprintf(stderr, "%s: %s: reinit: %s\n",
			    prog, argv[i], strerror(errno));
			res++;
		}

		close(fd);
	}

	return (res);
}
OpenPOWER on IntegriCloud