digictl.c revision 76707
1/*-
2 * Copyright (c) 2001 Brian Somers <brian@Awfulhak.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: head/usr.sbin/digictl/digictl.c 76707 2001-05-17 01:42:52Z brian $
27 */
28
29#include <errno.h>
30#include <fcntl.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include <sysexits.h>
35#include <sys/ioctl.h>
36#include <unistd.h>
37
38#include "dev/digi/digiio.h"
39
40static int
41usage(const char *prog)
42{
43	fprintf(stderr, "Usage: %s [-d debug] [-ir] device...\n", prog);
44	return (EX_USAGE);
45}
46
47int
48main(int argc, char **argv)
49{
50	char namedata[256], *name = namedata;
51	const char *prog;
52	enum digi_model model;
53	int ch, debug, fd, i, res;
54	int dflag, iflag, rflag;
55
56	if ((prog = strrchr(argv[0], '/')) == NULL)
57		prog = argv[0];
58	else
59		prog++;
60
61	dflag = iflag = rflag = 0;
62	while ((ch = getopt(argc, argv, "d:ir")) != -1)
63		switch (ch) {
64		case 'd':
65			dflag = 1;
66			debug = atoi(optarg);
67			break;
68		case 'i':
69			iflag = 1;
70			break;
71		case 'r':
72			rflag = 1;
73			break;
74		default:
75			return usage(prog);
76		}
77
78	if (argc < optind)
79		return usage(prog);
80
81	res = 0;
82	for (i = optind; i < argc; i++) {
83		if ((fd = open(argv[i], O_RDONLY)) == -1) {
84			fprintf(stderr, "%s: %s: open: %s\n", prog, argv[i],
85			    strerror(errno));
86			res++;
87			continue;
88		}
89
90		if (dflag && ioctl(fd, DIGIIO_DEBUG, &debug) != 0) {
91			fprintf(stderr, "%s: %s: debug: %s\n",
92			    prog, argv[i], strerror(errno));
93			res++;
94		}
95
96		if (iflag) {
97			if (ioctl(fd, DIGIIO_MODEL, &model) != 0) {
98				fprintf(stderr, "%s: %s: model: %s\n",
99				    prog, argv[i], strerror(errno));
100				res++;
101			} else if (ioctl(fd, DIGIIO_IDENT, &name) != 0) {
102				fprintf(stderr, "%s: %s: ident: %s\n",
103				    prog, argv[i], strerror(errno));
104				res++;
105			} else
106				printf("%s: %s (type %d)\n",
107				    argv[i], name, (int)model);
108		}
109
110		if (rflag && ioctl(fd, DIGIIO_REINIT) != 0) {
111			fprintf(stderr, "%s: %s: reinit: %s\n",
112			    prog, argv[i], strerror(errno));
113			res++;
114		}
115
116		close(fd);
117	}
118
119	return (res);
120}
121