Path: utzoo!attcan!uunet!lll-winken!lll-lcc!ames!haven!vrdxhq!bms-at!stuart From: stuart@bms-at.UUCP (Stuart Gathman) Newsgroups: comp.sys.ibm.pc Subject: File handles in TSR's: A tip and a question Keywords: Aaaargh! MesS DOS Message-ID: <146@bms-at.UUCP> Date: 5 Jan 89 02:03:49 GMT Organization: Business Management Systems, Inc., Fairfax, VA Lines: 68 First: A tip for writing TSR's in Turbo C 1.5 & 2.0. The setjmp() and longjmp() functions save ss:sp. This allows easy context switching. Non-trivial interrupt handlers usually need to switch to an internal stack to avoid overflowing a caller's stack of unknown size. (And so that pointers to stack variables work as expected for a small model TSR.) A handler plus main for small model looks something like this: -------------------------- #include #include #define VECT 0x7f #define DSPARAS 0x1000 /* 64K data segment */ static jmp_buf u_state, state; void interrupt handler() unsigned ...,ax; { _AX = setjmp(u_state); /* save user stack */ if (_AX == 0) longjmp(state,ax); /* switch to internal stack */ } main() { void interrupt (*saveint)(); int op; op = setjmp(state); if (op == 0) { /* should check if already installed, etc. . . . */ saveint = getvect(VECT); setvect(VECT,handler); keep(_DS - _psp + DSPARAS); /* TSR */ } if (op != UNINSTALL) { doit(op); /* do stuff */ longjmp(u_state); /* return to user */ } /* uninstall */ setvect(VECT,saveint); /* restore vector */ freemem(*(unsigned far *)MK_FP(_psp,0x2c)); /* free environment */ freemem(_psp); /* free program */ longjmp(u_state); /* return to user */ } --------------- Nice & high level, huh? The handler doesn't need to be interrupt for huge model (assuming the caller doesn't need to save AX..DX). The interrupt form ensures that _DS is loaded with the local data segment. This could also be accomplished with inline assembler as could pushing only those registers not saved by setjmp(). NOW - my question is this: is there any way to keep files open in a TSR? DOS likes to close them whenever the last process loaded before they were opened exits. (Got that?) Dos does not simply close all files whenever any program exits. It carefully keeps track of which files were opened by which psp (apparently). Unfortunately, TSR's don't count. Is there any way to fool DOS into saving TSR files also? The alternative of keeping an internal file descriptor table with qualified path names and replacements for open/read/write/lseek/close doesn't appeal to me. Strangely enough, file desciptors 0,1,2,3,4 are not closed even for TSR's. Please reply by E-mail (in addition to posting if desired). I read this group, but can't keep up with the thousands of articles. -- Stuart D. Gathman <..!{vrdxhq|daitc}!bms-at!stuart>