Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.sbin / sgsc / sgsc.c
1 /* sgsc(1) - utility for the `gsc' scanner device driver
2  *
3  *
4  * Copyright (c) 1995 Gunther Schadow.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *      This product includes software developed by Gunther Schadow.
17  * 4. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #ifndef lint
33 static const char rcsid[] =
34   "$FreeBSD: src/usr.sbin/sgsc/sgsc.c,v 1.5 1999/08/28 01:19:57 peter Exp $";
35 #endif /* not lint */
36
37 #include <err.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41 #include <sys/file.h>
42 #include <sys/ioctl.h>
43 #include <machine/gsc.h>
44
45 #ifndef DEFAULT_FILE
46 #define DEFAULT_FILE "/dev/gsc0"
47 #endif
48 #ifdef FAIL
49 #undef FAIL
50 #endif
51 #define FAIL -1
52
53 static void
54 usage()
55 {
56   fprintf(stderr, "%s\n%s\n",
57         "usage: sgsc [-sq] [-f file] [-r dpi] [-w width] [-h height]",
58         "            [-b len] [-t time]");
59   exit(1);
60 }
61
62 int
63 main(int argc, char **argv)
64 {
65   char c;
66   int fd;
67
68   char *file = DEFAULT_FILE;
69
70   int show_dpi     = 0;
71   int show_width   = 0;
72   int show_height  = 0;
73   int show_blen    = 0;
74   int show_btime   = 0;
75   int show_all     = 1;
76
77   int set_blen     = 0;
78   int set_dpi      = 0;
79   int set_width    = 0;
80   int set_height   = 0;
81   int set_btime    = 0;
82   int set_switch   = 0;
83
84   if (argc == 0) usage();
85
86   while( (c = getopt(argc, argv, "sqf:b:r:w:h:t:")) != FAIL)
87     {
88       switch(c) {
89       case 'f': file = optarg; break;
90       case 'r': set_dpi = atoi(optarg); break;
91       case 'w': set_width = atoi(optarg); break;
92       case 'h': set_height = atoi(optarg); break;
93       case 'b': set_blen = atoi(optarg); break;
94       case 't': set_btime = atoi(optarg); break;
95       case 's': set_switch = 1; break;
96       case 'q': show_all = 0; break;
97       default: usage();
98       }
99     }
100
101   fd = open(file, O_RDONLY);
102   if ( fd == FAIL )
103     err(1, "%s", file);
104
105   if (set_switch != 0)
106     {
107       if(ioctl(fd, GSC_SRESSW) == FAIL)
108           err(1, "GSC_SRESSW");
109     }
110
111   if (set_dpi != 0)
112     {
113       if(ioctl(fd, GSC_SRES, &set_dpi) == FAIL)
114           err(1, "GSC_SRES");
115     }
116
117   if (set_width != 0)
118     {
119       if(ioctl(fd, GSC_SWIDTH, &set_width) == FAIL)
120           err(1, "GSC_SWIDTH");
121     }
122
123   if (set_height != 0)
124     {
125       if(ioctl(fd, GSC_SHEIGHT, &set_height) == FAIL)
126           err(1, "GSC_SHEIGHT");
127     }
128
129   if (set_blen != 0)
130     {
131       if(ioctl(fd, GSC_SBLEN, &set_blen) == FAIL)
132           err(1, "GSC_SBLEN");
133     }
134
135   if (set_btime != 0)
136     {
137       if(ioctl(fd, GSC_SBTIME, &set_btime) == FAIL)
138           err(1, "GSC_SBTIME");
139     }
140
141   if (show_all != 0)
142     {
143       if(ioctl(fd, GSC_GRES,  &show_dpi) == FAIL)
144           err(1, "GSC_GRES");
145       if(ioctl(fd, GSC_GWIDTH,  &show_width) == FAIL)
146           err(1, "GSC_GWIDTH");
147       if(ioctl(fd, GSC_GHEIGHT,  &show_height) == FAIL)
148           err(1, "GSC_GHEIGHT");
149       if(ioctl(fd, GSC_GBLEN, &show_blen) == FAIL)
150           err(1, "GSC_GBLEN");
151       if(ioctl(fd, GSC_GBTIME, &show_btime) == FAIL)
152           err(1, "GSC_GBTIME");
153
154       printf("%s:\n", file);
155       printf("resolution\t %d dpi\n", show_dpi);
156       printf("width\t\t %d\n", show_width);
157       printf("height\t\t %d\n",show_height);
158       printf("buffer length\t %d\n", show_blen);
159       printf("buffer timeout\t %d\n", show_btime);
160     }
161
162   return 0;
163 }