[comp.sys.ibm.pc] Accessing 'root' environment

daanw@neabbs.UUCP (DAAN VAN DER WEIDE) (10/31/88)

 
Hi,
 
I've seen quite a lot questions about modifying the 
'root' environment especially from a child process. The 
main problem is that each child process receives a copy 
of the environment, and all the process can do is modify 
it's own copy. I've seen interesting solutions using 
batch-files etc, nevertheless would I like to present my 
own, BUT BE CAREFUL : I use an UNDOCUMENTED feature 
(interrupt) present in all DOS versions I know. I use the 
mechanism myself very frequently but that's not a 
guarantee that it'll work on any other brand of CPU &| 
DOS version, nor that it will in any new DOS version etc. 
etc. as usual with these kind of hacks. The advantage is 
that it works quite elegant :) 
 
Below follows the C and ASM source. The program runs fine 
using either MSC 5.1 or TurboC 2.0. Btw. you'll need MASM 
5.1 to assemble. 
 
The DosCommand function modifies the passed string. I.e. 
it makes it a 'DOS string' that is, it moves the whole 
thing one byte up, puts a byte with the length in front 
of it, and appends a CR. So you should pass a buffer that 
can be expanded with two extra characters. Now the fun 
starts : INT 2Eh seems to call resident or 'root' 
COMMAND.COM and allows you to execute any command that 
can be entered thru the normal command line, including 
SET commands, that do affect the 'root' environment. And 
that's what we wanted in the first place...
 
D.A.A.N.
 
 
-- CUT HERE FOR ENV.C -----------------------------------
 
#include <string.h>
 
extern void DosCommand(char Cmd[]);
 
void main(int Argc, char *Argv[])
{
    char Cmd[128];
 
 
    if (Argc == 1)
        strcpy(Cmd, "PROMPT [$p]");
    else
        strcpy(Cmd, Argv[1]);
 
    DosCommand(Cmd);
}
 
-- END OF ENV.C ----------------------------------------
-- CUT HERE FOR DOSCMD.ASM -----------------------------
 
;---------------
ifndef model
                model   equ <small>            ; change for other
endif
%               .model  model,c
;---------------
                .data
;---------------
SaveSS          dw      ?
SaveSP          dw      ?
;---------------
                .fardata?
;---------------
Stack           dw      512 dup (?)
TopOfStack      equ     $ - offset Stack
;---------------
                .code
;---------------
DosCommand      proc    uses ds si di, Cmd:ptr
 
                mov     [SaveSS],ss             ; these get screwed
                mov     [SaveSP],sp
 
if @datasize
                lds     si,[Cmd]
else
                mov     si,[Cmd]
endif
                mov     ax,SEG Stack            ; Stack Switch
                mov     ss,ax
                mov     sp,TopOfStack
 
                mov     ax,ds                   ; duplicate ptr into
                mov     es,ax
                mov     di,si
                mov     dx,si                   ; dup offset into DX
 
                mov     al,0                    ; determine length
                mov     cx,-1
                repne   scasb
                jcxz    exit
 
                not     cx
                dec     cx
                mov     bx,cx                   ; length into BX
 
                std                             ; move backwards
                mov     al,0Dh                  ; append CR
                stosb
                mov     si,di
                dec     si
                rep     movsb                   ; copy
                mov     al,bl
                stosb                           ; length
                cld
 
                mov     si,dx                   ; restore SI
                int     02Eh                    ; call DOS
 
exit:           mov     ax,@data                ; restore DS
                mov     ds,ax
                mov     ss,[SaveSS]             ; and stack
                mov     sp,[SaveSP]
 
                ret
 
DosCommand      endp
;---------------
                end
 
-- END OF DOSCMD.ASM -----------------------------------