]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/digictl/digictl.c
This commit was generated by cvs2svn to compensate for changes in r76866,
[FreeBSD/FreeBSD.git] / usr.sbin / digictl / digictl.c
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$
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/digiio.h>
36 #include <sys/ioctl.h>
37 #include <unistd.h>
38
39 static int
40 usage(const char *prog)
41 {
42         fprintf(stderr, "Usage: %s [-d debug] [-ir] device...\n", prog);
43         return (EX_USAGE);
44 }
45
46 int
47 main(int argc, char **argv)
48 {
49         char namedata[256], *name = namedata;
50         const char *prog;
51         enum digi_model model;
52         int ch, debug, fd, i, res;
53         int dflag, iflag, rflag;
54
55         if ((prog = strrchr(argv[0], '/')) == NULL)
56                 prog = argv[0];
57         else
58                 prog++;
59
60         dflag = iflag = rflag = 0;
61         while ((ch = getopt(argc, argv, "d:ir")) != -1)
62                 switch (ch) {
63                 case 'd':
64                         dflag = 1;
65                         debug = atoi(optarg);
66                         break;
67                 case 'i':
68                         iflag = 1;
69                         break;
70                 case 'r':
71                         rflag = 1;
72                         break;
73                 default:
74                         return usage(prog);
75                 }
76
77         if (argc < optind)
78                 return usage(prog);
79
80         res = 0;
81         for (i = optind; i < argc; i++) {
82                 if ((fd = open(argv[i], O_RDONLY)) == -1) {
83                         fprintf(stderr, "%s: %s: open: %s\n", prog, argv[i],
84                             strerror(errno));
85                         res++;
86                         continue;
87                 }
88
89                 if (dflag && ioctl(fd, DIGIIO_DEBUG, &debug) != 0) {
90                         fprintf(stderr, "%s: %s: debug: %s\n",
91                             prog, argv[i], strerror(errno));
92                         res++;
93                 }
94
95                 if (iflag) {
96                         if (ioctl(fd, DIGIIO_MODEL, &model) != 0) {
97                                 fprintf(stderr, "%s: %s: model: %s\n",
98                                     prog, argv[i], strerror(errno));
99                                 res++;
100                         } else if (ioctl(fd, DIGIIO_IDENT, &name) != 0) {
101                                 fprintf(stderr, "%s: %s: ident: %s\n",
102                                     prog, argv[i], strerror(errno));
103                                 res++;
104                         } else
105                                 printf("%s: %s (type %d)\n",
106                                     argv[i], name, (int)model);
107                 }
108
109                 if (rflag && ioctl(fd, DIGIIO_REINIT) != 0) {
110                         fprintf(stderr, "%s: %s: reinit: %s\n",
111                             prog, argv[i], strerror(errno));
112                         res++;
113                 }
114
115                 close(fd);
116         }
117
118         return (res);
119 }