xref: /freebsd/usr.sbin/lptcontrol/lptcontrol.c (revision a316b26e50bbed7cf655fbba726ab87d8ab7599d)
1 /*
2  * Copyright (c) 1994 Geoffrey M. Rehmet
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  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by Geoffrey M. Rehmet
16  * 4. The name of the author may not be used to endorse or promote products
17  *    derived from this software withough specific prior written permission
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * $Id: lptcontrol.c,v 1.1.1.1 1994/09/03 13:10:09 csgr Exp $
31  */
32 
33 #include <ctype.h>
34 #include <limits.h>
35 #include <paths.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 
41 #include <machine/lpt.h>
42 #include <sys/errno.h>
43 #include <sys/file.h>
44 #include <sys/ioctl.h>
45 #include <sys/types.h>
46 
47 
48 #define PATH_LPCTL	_PATH_DEV "lpctl"
49 #define DEFAULT_UNIT	"0"
50 #define IRQ_INVALID	-1
51 #define DO_POLL		0
52 #define USE_IRQ		1
53 
54 static void usage(const char * progname)
55 {
56 	fprintf(stderr, "usage: %s -i | -p  [-u <unit no.>]\n", progname);
57 	fprintf(stderr, "\tUnit no. is a value in the range 0 to 3\n");
58 	fprintf(stderr, "\tThe default unit no is 0 (ie. /dev/lpt0)\n");
59 	exit(1);
60 }
61 
62 static void set_interrupt_status(int irq_status, const char * file)
63 {
64 	int	fd;
65 
66 	if((fd = open(file, O_WRONLY, 0660)) < 0) {
67 		perror("open");
68 		exit(1);
69 	}
70 	if(ioctl(fd, LPT_IRQ, &irq_status) < 0) {
71 		perror("ioctl");
72 		exit(1);
73 	}
74 	close(fd);
75 }
76 
77 static char * dev_file(char unit_no)
78 {
79 	static char	devname[_POSIX_PATH_MAX+1];
80 	int		len;
81 
82 	strncpy(devname, PATH_LPCTL, _POSIX_PATH_MAX);
83 	devname[len = strlen(devname)] = unit_no;
84 	devname[++len] = '\0';
85 
86 	return(devname);
87 }
88 
89 int main (int argc, char * argv[])
90 {
91 	int		opt;
92 	int		irq_status = IRQ_INVALID;
93 	char		* unit = DEFAULT_UNIT;
94 
95 	while((opt = getopt(argc, argv, "ipu:")) != -1)
96 		switch(opt) {
97 		case 'i': irq_status = USE_IRQ; break;
98 		case 'p': irq_status = DO_POLL; break;
99 		case 'u': unit = optarg;
100 			  if(!isdigit(*unit))
101 				usage(argv[0]);
102 			  break;
103 		default : usage(argv[0]);
104 		}
105 	if(irq_status == IRQ_INVALID)
106 		usage(argv[0]);
107 
108 	set_interrupt_status(irq_status, dev_file(*unit));
109 
110 	exit(0);
111 }
112 
113 
114