xref: /freebsd/usr.sbin/lptcontrol/lptcontrol.c (revision 6dc3c6f768a8edf40c55a9b4cb9390c1551ba150)
16dc3c6f7SGeoff Rehmet /*
26dc3c6f7SGeoff Rehmet  * Copyright (c) 1994 Geoffrey M. Rehmet
36dc3c6f7SGeoff Rehmet  * All rights reserved.
46dc3c6f7SGeoff Rehmet  *
56dc3c6f7SGeoff Rehmet  * Redistribution and use in source and binary forms, with or without
66dc3c6f7SGeoff Rehmet  * modification, are permitted provided that the following conditions
76dc3c6f7SGeoff Rehmet  * are met:
86dc3c6f7SGeoff Rehmet  * 1. Redistributions of source code must retain the above copyright
96dc3c6f7SGeoff Rehmet  *    notice, this list of conditions and the following disclaimer.
106dc3c6f7SGeoff Rehmet  * 2. Redistributions in binary form must reproduce the above copyright
116dc3c6f7SGeoff Rehmet  *    notice, this list of conditions and the following disclaimer in the
126dc3c6f7SGeoff Rehmet  *    documentation and/or other materials provided with the distribution.
136dc3c6f7SGeoff Rehmet  * 3. All advertising materials mentioning features or use of this software
146dc3c6f7SGeoff Rehmet  *    must display the following acknowledgement:
156dc3c6f7SGeoff Rehmet  *      This product includes software developed by Geoffrey M. Rehmet
166dc3c6f7SGeoff Rehmet  * 4. The name of the author may not be used to endorse or promote products
176dc3c6f7SGeoff Rehmet  *    derived from this software withough specific prior written permission
186dc3c6f7SGeoff Rehmet  *
196dc3c6f7SGeoff Rehmet  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
206dc3c6f7SGeoff Rehmet  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
216dc3c6f7SGeoff Rehmet  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
226dc3c6f7SGeoff Rehmet  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
236dc3c6f7SGeoff Rehmet  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
246dc3c6f7SGeoff Rehmet  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
256dc3c6f7SGeoff Rehmet  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
266dc3c6f7SGeoff Rehmet  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
276dc3c6f7SGeoff Rehmet  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
286dc3c6f7SGeoff Rehmet  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
296dc3c6f7SGeoff Rehmet  *
306dc3c6f7SGeoff Rehmet  * $Id: lptcontrol.c,v 1.2 1994/04/08 22:23:39 csgr Exp $
316dc3c6f7SGeoff Rehmet  */
326dc3c6f7SGeoff Rehmet 
336dc3c6f7SGeoff Rehmet #include <sys/types.h>
346dc3c6f7SGeoff Rehmet #include <sys/file.h>
356dc3c6f7SGeoff Rehmet #include <sys/ioctl.h>
366dc3c6f7SGeoff Rehmet #include <machine/lpt.h>
376dc3c6f7SGeoff Rehmet #include <stdlib.h>
386dc3c6f7SGeoff Rehmet #include <unistd.h>
396dc3c6f7SGeoff Rehmet #include <stdio.h>
406dc3c6f7SGeoff Rehmet #include <string.h>
416dc3c6f7SGeoff Rehmet #include <sys/errno.h>
426dc3c6f7SGeoff Rehmet 
436dc3c6f7SGeoff Rehmet 
446dc3c6f7SGeoff Rehmet #define DEFAULT_LPT	"/dev/lpt0"
456dc3c6f7SGeoff Rehmet #define IRQ_INVALID	-1
466dc3c6f7SGeoff Rehmet #define DO_POLL		0
476dc3c6f7SGeoff Rehmet #define USE_IRQ		1
486dc3c6f7SGeoff Rehmet 
496dc3c6f7SGeoff Rehmet static char	default_printer[] = DEFAULT_LPT;
506dc3c6f7SGeoff Rehmet 
516dc3c6f7SGeoff Rehmet static void usage(const char * progname)
526dc3c6f7SGeoff Rehmet {
536dc3c6f7SGeoff Rehmet 	fprintf(stderr, "usage: %s -i | -p  [-f <file name>]\n", progname);
546dc3c6f7SGeoff Rehmet 	exit(1);
556dc3c6f7SGeoff Rehmet }
566dc3c6f7SGeoff Rehmet 
576dc3c6f7SGeoff Rehmet static void set_interrupt_status(int irq_status, const char * file)
586dc3c6f7SGeoff Rehmet {
596dc3c6f7SGeoff Rehmet 	int	fd;
606dc3c6f7SGeoff Rehmet 
616dc3c6f7SGeoff Rehmet 	if((fd = open(file, O_WRONLY, 0660)) < 0) {
626dc3c6f7SGeoff Rehmet 		perror("open");
636dc3c6f7SGeoff Rehmet 		exit(1);
646dc3c6f7SGeoff Rehmet 	}
656dc3c6f7SGeoff Rehmet 	if(ioctl(fd, LPT_IRQ, &irq_status) < 0) {
666dc3c6f7SGeoff Rehmet 		perror("ioctl");
676dc3c6f7SGeoff Rehmet 		exit(1);
686dc3c6f7SGeoff Rehmet 	}
696dc3c6f7SGeoff Rehmet 	close(fd);
706dc3c6f7SGeoff Rehmet }
716dc3c6f7SGeoff Rehmet 
726dc3c6f7SGeoff Rehmet 
736dc3c6f7SGeoff Rehmet int main (int argc, char * argv[])
746dc3c6f7SGeoff Rehmet {
756dc3c6f7SGeoff Rehmet 	int		opt;
766dc3c6f7SGeoff Rehmet 	int		irq_status = -1;
776dc3c6f7SGeoff Rehmet 	char		* file = default_printer;
786dc3c6f7SGeoff Rehmet 
796dc3c6f7SGeoff Rehmet 	while((opt = getopt(argc, argv, "pif:")) != -1)
806dc3c6f7SGeoff Rehmet 		switch(opt) {
816dc3c6f7SGeoff Rehmet 		case 'i': irq_status = USE_IRQ; break;
826dc3c6f7SGeoff Rehmet 		case 'p': irq_status = DO_POLL; break;
836dc3c6f7SGeoff Rehmet 		case 'f': file = optarg; break;
846dc3c6f7SGeoff Rehmet 		default : usage(argv[0]);
856dc3c6f7SGeoff Rehmet 		}
866dc3c6f7SGeoff Rehmet 	if(irq_status == IRQ_INVALID) usage(argv[0]);
876dc3c6f7SGeoff Rehmet 
886dc3c6f7SGeoff Rehmet 	set_interrupt_status(irq_status, file);
896dc3c6f7SGeoff Rehmet 
906dc3c6f7SGeoff Rehmet 	exit(0);
916dc3c6f7SGeoff Rehmet }
926dc3c6f7SGeoff Rehmet 
936dc3c6f7SGeoff Rehmet 
94