home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Jason Aller Floppy Collection
/
267.img
/
RESIDNTC.ZIP
/
DEMOS.ZIP
/
SHARETSR.C
< prev
next >
Wrap
Text File
|
1990-02-13
|
10KB
|
326 lines
/*
Module name: SHARETSR.C
The program, a very modified version of resclock, takes over
the user timer interrupt, 0x1C, and displays the time on the
screen.
The program also takes over a user interrupt vector to share
data (a struct) with a foreground program. The struct address
is also passed on the inittsr2() parameter list.
The BIOS printer interrupt (17H) is taken over and our handler
counts the number of characters (including control codes) sent
to the printer.
Last, but not least, the program takes over the keyboard hardware
interrupt (9H) and the DOS dispatcher (21H) to count the number
of times an 'a' is pressed and the number of times DOS is called
to perform a create directory function. If a remove directory
function is called, it gets changed to a create directory function
which causes an error from DOS. This disables the ability to
remove a directory as long as this program is running.
The information above is shared with a foreground program,
sharefor.c, or available by pressing the hotkey - RightShift-E.
If a "Q" is entered, the program will free itself, otherwise it will
go back to the background. These interrupts are chained to
because of their sensitive nature.
--) NOTICE: Copyright (C) 1989 South Mountain Software, Inc.
*/
#include "stdlib.h"
#include "conio.h"
#include "string.h"
#include "stdio.h"
#include "tsr.h"
#include "resproto.h"
#include "isr.h"
#include "dos.h"
#define TRUE 1
#define FALSE 0
#define SCAN_E 18 /* scan code of E key */
#define ROW 0
#define COL 65
#define ATTR 0x70 /* black on white */
#ifdef M_I86 /* Microsoft internal define , otherwise Turbo C */
#define disable _disable
#define enable _enable
#define getvect _dos_getvect
#define setvect _dos_setvect
#define inportb inp
#endif
/* window save buffer declared globally */
char scrnbuf[400];
/* unique function number we use to see if already loaded */
unsigned int myfunc = 0x5272;
/* address to total timmer tics since midnight */
long far * systics = (long far *) 0x0000046CL;
int hr,min,sec,hd; /* used in do_clock() */
unsigned shared_vector; /* vector number to share with foreground program */
struct { /* structure to share with foreground program */
unsigned my_sig; /* signature placed here for find_sig() example */
char time_str[30]; /* time of day string placed here */
long a_key_hit; /* count of a key presses */
long dos_calls; /* count of int 21H calls */
long chars_printed; /* count of chars sent through int 17h */
} share_me;
int do_clock(void);
int free_this_one(void);
void display(char *str);
void clock(void);
int our_key_handler(void), our_dos_handler(void), our_prt_handler(void);
void main()
{
unsigned offset,segment;
struct SREGS sregs;
void far *address;
if (tsrloaded(myfunc) != FALSE)
{
printf("SHARETSR Already Loaded!\n");
exit(1);
}
initisr2(0x17,our_prt_handler,1); /* take over int 17h */
initisr3(0x21,our_dos_handler,1); /* take over int 21h */
initisr4(0x1c,do_clock,1); /* take over int 1ch */
initisr5(0x9,our_key_handler,1); /* take over int 9h */
shared_vector = find_vector(0x60,0x66); /* find a vacant vector */
if(shared_vector)
{
share_me.my_sig = 0xDCBA; /* initialize signature */
share_me.a_key_hit = 0L; /* initialize int 9 counter */
share_me.dos_calls = 0L; /* initialize int 21 counter */
share_me.chars_printed = 0L; /* initialize char count */
if((sizeof(char *)) == 4) /* if large data model */
{
offset = (unsigned)(&share_me);
address = (void far *)&share_me;
segment = FP_SEG(address);
}
else
{
segread(&sregs);
offset = (unsigned) &share_me;
segment = sregs.ds;
}
vec_set(shared_vector,offset,segment); /* put struc address in vector */
}
else
{
printf("*** Not enough vacant user vectors. Program aborted! ***\n");
disable();
enable();
freeisr2(); /* reset initisr2 vector */
freeisr3(); /* reset initisr2 vector */
freeisr4(); /* reset initisr4 vector */
freeisr5(); /* reset initisr5 vector */
exit(1);
}
inittsr(RIGHT_SHIFT,SCAN_E, clock, 30, myfunc, SIG, (void far *) &share_me);
}
static int cnt = 99; /* initialize second comparison variable */
char curtime[15]; /* buffer to hold time */
int loop;
extern int _hotkey_hit; /* set to 1 if hotkey was hit */
char key_in; /* for getch() below */
int page; /* video page */
int mode; /* video mode */
int key; /* gets thrown away */
void clock()
{
/* retrieve video mode and video page */
getvmod(&mode,&page);
/* see if we are in 80 column text modes */
if ( mode != 2 && mode != 3 && mode != 7 )
{
putch(7); /* if yes, beep and exit */
return;
}
/* save window in center of screen */
savwindo(8,23,13,55,scrnbuf);
savcur(); /* save cursor */
/* clear the window in reverse video */
scroll_vid(0,0,7,8,23,13,55,1);
/* clear inner window in normal video */
scroll_vid(0,7,0,9,24,12,54,1);
loc_cur(9,27); /* locate cursor in window */
printf(" Hit Q to free TSR ");
loc_cur(10,26); /* locate cursor in window */
printf(" a key pressed = %ld times ",share_me.a_key_hit);
loc_cur(11,26); /* locate cursor in window */
printf(" remove directories = %ld ",share_me.dos_calls);
loc_cur(12,26); /* locate cursor in window */
printf(" chars printed = %ld ",share_me.chars_printed);
key_in = (char) getch();
if(key_in == 'Q' || key_in == 'q')
{
if(free_this_one() != 0 )
{
putch(7); putch(7);
printf("Not Loaded Last! - TSR Will Still Be Loaded!");
_hotkey_hit = 0; /* reset hotkey flag */
}
}
rstcur(); /* put cursor back */
/* restore the window in center of screen */
rstwindo(8,23,13,55,scrnbuf);
_hotkey_hit = 0;
return;
}
int do_clock() /* hard work part of clock() set up as separate function */
/* so it could be called while waiting for a key */
{
char curtime[15]; /* buffer to hold time */
char temp_ptr[17]; /* temporary buffer for itoa() */
unsigned long tmp; /* will hold tics since midnight */
tmp = *systics; /* get tics since midnight */
/* this formula converts tics since midnight */
hr = (int) (tmp / 65543L);
tmp %= 65543L;
min = (int) (tmp / 1092L);
tmp %= 1092;
sec = (int) (tmp * 100L / 1821L);
/* we'll only update the clock display when sec changes */
if(cnt != sec)
{
cnt = sec;
/* cannot use sprintf because it was compiled with
stack checking on. */
/* sprintf(curtime," %2d:%2d:%2d ",hr,min,sec); */
/* do it the hard way with itoa */
curtime[0] = ' ';
curtime[1] = '\0';
itoa(hr,temp_ptr,10);
if(strlen(temp_ptr) == 1) /* force two digits for each number */
strcat(curtime,"0");
strcat(curtime,temp_ptr);
strcat(curtime,":");
itoa(min,temp_ptr,10);
if(strlen(temp_ptr) == 1) /* force two digits for each number */
strcat(curtime,"0");
strcat(curtime,temp_ptr);
strcat(curtime,":");
itoa(sec,temp_ptr,10);
if(strlen(temp_ptr) == 1) /* force two digits for each number */
strcat(curtime,"0");
strcat(curtime,temp_ptr);
strcat(curtime," ");
display(curtime); /* put the time on the screen */
}
return(0);
}
/* Interleave atribute and display time */
void display(str)
char *str;
{
register char *curptr; /* used to step thru buffer */
char buf[31]; /* actual buffer to write to screen */
strcpy(share_me.time_str,str);
curptr = buf;
/* merge char and attribute for screen format */
while(*str) {
*curptr++ = *str++;
*curptr++ = ATTR;
}
/* do the actual display */
memtoscr(10, (ROW * 160) + (COL * 2), buf);
return;
}
int free_this_one()
{
if(freetsr() == 1)
return(1);
else
return(0);
}
void tsrpre()
{
printf("SHARETSR by South Mountain Software\n");
printf("\n Hit RIGHT-SHIFT-E to Pop-Up!\n\n");
}
void tsrpost()
{
freeisr5();
freeisr4();
freeisr3();
freeisr2();
}
int our_key_handler()
{
unsigned scancode;
scancode = inportb(0x60); /* peek ahead at key in keyboard I/O port */
if(scancode == 30)
share_me.a_key_hit++;
return(0);
}
int our_prt_handler()
{
if(isr2_ah == 0) /* send character in al */
share_me.chars_printed++;
return(0);
}
int our_dos_handler()
{
if(isr3_ah == 0x3a) /* open file with handle */
{
share_me.dos_calls++;
isr3_ah = 0x39; /* change to create directory */
}
return(0);
}