lptcontrol.c revision 21673
11638Srgrimes/* 21638Srgrimes * Copyright (c) 1994 Geoffrey M. Rehmet 31638Srgrimes * All rights reserved. 41638Srgrimes * 51638Srgrimes * Redistribution and use in source and binary forms, with or without 61638Srgrimes * modification, are permitted provided that the following conditions 71638Srgrimes * are met: 81638Srgrimes * 1. Redistributions of source code must retain the above copyright 91638Srgrimes * notice, this list of conditions and the following disclaimer. 101638Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111638Srgrimes * notice, this list of conditions and the following disclaimer in the 121638Srgrimes * documentation and/or other materials provided with the distribution. 131638Srgrimes * 3. All advertising materials mentioning features or use of this software 141638Srgrimes * must display the following acknowledgement: 151638Srgrimes * This product includes software developed by Geoffrey M. Rehmet 161638Srgrimes * 4. The name of the author may not be used to endorse or promote products 171638Srgrimes * derived from this software withough specific prior written permission 181638Srgrimes * 191638Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 201638Srgrimes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 211638Srgrimes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 221638Srgrimes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 231638Srgrimes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 241638Srgrimes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 251638Srgrimes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 261638Srgrimes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 271638Srgrimes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 281638Srgrimes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 291638Srgrimes * 301638Srgrimes * $FreeBSD: head/usr.sbin/lptcontrol/lptcontrol.c 21673 1997-01-14 07:20:47Z jkh $ 311638Srgrimes */ 321638Srgrimes 331638Srgrimes#include <ctype.h> 341638Srgrimes#include <limits.h> 351638Srgrimes#include <paths.h> 361638Srgrimes#include <stdio.h> 371638Srgrimes#include <stdlib.h> 381638Srgrimes#include <string.h> 391638Srgrimes#include <unistd.h> 401638Srgrimes 411638Srgrimes#include <machine/lpt.h> 421638Srgrimes#include <sys/errno.h> 431638Srgrimes#include <sys/file.h> 441638Srgrimes#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 54static 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 62static 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 77static 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 89int 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