#include #include #include #include #include int errormsg(int); int main(int argc, char *argv[]) { unsigned int base,k,bit,i,b; unsigned char val,byte,maskset,maskclr; int bs,ro; bs=0; base=0; maskset=0; maskclr=0; byte=0; val=0; k=0; bit=0; ro=0; //read command line parameters if(argc < 2) errormsg(0); for(i=1; i 1)||(i>=argc)||(argv[i][0]<'0')||(argv[i][0]>'7')) errormsg(2); bit = atoi(argv[i]); maskset = maskset | ( 1 << bit); break; case 3: // -clr i++; if((strlen(argv[i]) > 1)||(i>=argc)||(argv[i][0]<'0')||(argv[i][0]>'7')) errormsg(3); bit = atoi(argv[i]); maskclr = maskclr | ( 1 << bit); break; case 4: // -read ro=1; break; case 5: // -help printf("Usage: %s [options] [byte]\n", argv[0]); printf("Options:\n"); printf("\t-base \t-specifie base addrres of LPT port decimal or hex, default 0x378.\n"); printf("\t-set \t-set specified bit number(0-7) to hi level. Can use to 8 options -set.\n"); printf("\t-clr \t-clear specified bit number(0-7) to low level. Can use to 8 options -clr.\n"); printf("\t-read \t-read current value from LPT without ani change in LPT port.\n"); printf("\t-help \t-show this message\n"); printf("byte \t- byte for send to LPT port. If specified -set or -clr option will be changed before send to LPT port\n"); exit(0); break; default: // byte if((argv[i][0] == '0')&&((argv[i][1]=='x')||(argv[i][1]=='X'))) { sscanf(&argv[i][2],"%x",&b); byte=b; } else { byte = atoi(argv[i]); } bs=1; break; } } if(base == 0) base = 0x378; // default base address LPT maskclr = maskclr ^ 0xff; if(ioperm(base, 3, 1))errormsg(1); // check permission val = inb(base); // read from port if(ro == 1) { printf("0x%02x\n",val); exit(0); } if(bs == 0) byte = val; byte = byte | maskset; byte = byte & maskclr; printf("0x%02x\n",byte); outb(byte,base); // write to port exit(0); } int errormsg(int msgn) { char msg[10][80]={ "Lost argument(s), use -help for help", "Couldn't get this port. You must be root or specify correct port address", "After option -set must be specified bit number, valid range 0-7", "After option -clr must be specified bit number, valid range 0-7" }; printf("ERROR: %s\n",msg[msgn]); exit(-1); }