Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

TSR in C example

384 views
Skip to first unread message

Arik Anderson

unread,
Nov 18, 1991, 8:57:44 AM11/18/91
to
Following are two example TSR's written in Borland Turbo C Ver 2.0.
The programs are just fragments of larger programs, so I appologize
if they seem rather useless by themselves.

The can be compiled in Turbo C using the command tcc -mt -lt. This
will cause the TINY memory model to be used and will create a .com
when the program is complete.

Also note that I break all the rules in these examples by looking at
the hardware directly. This works fine for the IBM PCs that I wrote
this for, but it may have problems on other hardware (although the
concepts will remain correct).

/*
This program disables the control-alt-delete secquence
on the computer. This has little purpose by itself, but
can be used as a building block to permit a different
key sequence to cause a reboot. It can also be used
to cause a cold reboot rather than a warm reboot.
*/
#include <dos.h>

#define KBD_PORT 0x60
#define KBD_CNTL 0x61
#define CNTL 0x04
#define ALT 0x08
#define KBD_INT 0x09

void interrupt (*oldkbd)();
unsigned int far *kbd_state = (unsigned int far *)0x417;
unsigned char value;

void interrupt kbd()
{

value = inportb(KBD_PORT); /* GET CHARACTER JUST PRESSED ON KEYBOARD */
if (value == 0x53) { /* SEE IF IT WAS THE DELETE KEY */
if (*kbd_state == (CNTL + ALT)) { /* SEE IF CONTROL AND ALT KEYS ARE ALSO BEING PRESSED */
value = inportb(KBD_CNTL); /* YES, SO DISCARD DEL KEYPRESS */
outportb(KBD_CNTL, value | 0x80);
outportb(KBD_CNTL, value);
outportb(0x20, 0x20); /* REINABLE KEYPRESSES */
} else {
oldkbd(); /* ELSE JUST PASS KEYPRESS ONTO NEXT PROCESS */
} /* endif */
} else {
oldkbd(); /* ELSE JUST PASS KEYPRESS ONTO NEXT PROCESS */
} /* endif */
}

main()

{

/*
YOU MIGHT WANT TO DISABLE INTERRUPTS HERE,
BUT I ASSUME THAT A KEY WON'T BE PRESSED
HERE. PRETTY SAFE, BUT NOT AIR TIGHT.
*/

oldkbd = getvect(0x09); /* KEY CURRENT KEYBOARD INTERRUPT VECTOR */
setvect(KBD_INT, kbd); /* PUT OUR ROUTINE IN ITS PLACE */
keep(0,0x8c); /* TERMINATE AND STAY RESIDENT, AND KEEP */
/* 0x8C PARAGRAPHS (16 BYTE QUANTITIES) OF */
/* MEMORY FOR THE PROGRAM */
}


/*
This routine will capture the timer interrupt
and every couple of minutes will simulate the
press of a "c" on the keyboard.

This program serves as an example of how to
write a simple TSR and how to minipulate keyboard
data.

The putch routine does not check for a full keyboard
buffer. This should, of course, be done if there is
any change that the buffer would be full when the
interrupt occurs.

*/
#include <dos.h>

#define timer_int 0x1C
#define DELAY_CNT 0x1000

void interrupt (*oldvect)();

void interrupt timer()
{

static unsigned int delay = 0;

if (delay++ == DELAY_CNT) { /* SEE IF DELAY HAS ELAPSED */
delay=0;
putch(); /* PLACE CHARACTER IN KEYBOARD BUFFER */
} /* endif */
(*oldvect)(); /* CALL NEXT INTERRUPT IN CHAIN */
}
int putch()
{
unsigned char far *kbd_buff=0x41e;
unsigned int far *kbd_tail=0x41c;
int index;

index=*kbd_tail-0x1e; /* GET CURRENT INDEX INTO KEYBOARD BUFFER */
kbd_buff[index+1] = 0x2e; /* PLACE THE SCAN CODE FOR A c INTO THE NEXT BUFFER LOCATION */
kbd_buff[index] = 0x63;
*kbd_tail = ((index + 2) % 0x20) + 0x1e; /* SET THE TAIL TO THE NEXT OPEN LOCATION */
}

main()
{

oldvect = getvect(timer_int);

disable();
setvect(timer_int, timer);
enable();

printf("Remote driver installed\n");

keep(0,512);
}
--
____________________________________________
/\ /\ < Arik A. Anderson >
/__\/__\ < INTERNET: ar...@cserver.plexus.com >
/ /\ \ < UUCP: marque!cserver!arika >

0 new messages