home *** CD-ROM | disk | FTP | other *** search
- Title SetEnv.asm
- ; by dOnut hOle in 1/1996
- ; Use TLINK /t to link - it's a com !!!
- ;
- ; This little program copies the path it was started in into the environment-
- ; variable given as parameter
- ; SYNTAX: SETENV <varname>
- ; e.g.: setenv.com is in c:\game\donut
- ; SETENV DON
- ; -> environment variable DON is set to 'C:\GAME\DONUT'
-
- .286
- .model tiny
-
- .code
- org 100h
- begin: jmp init
-
-
- ; ****** MACROS ********
- CR equ 0Dh
- LF equ 0Ah
- EOLN equ '$'
-
- ;************ data ************
- errmess1 db 'PSP of command.com not found !?',CR,LF,EOLN
- errmess2 db 'SetEnv - by dOnut hOle 1/1996',CR,LF
- db ' Sets a given environment variable to the path it was started in.',CR,LF
- db ' SYNTAX: SETENV <variable>',CR,LF
- db ' e.g.: setenv curr_path (now curr_path contains path of setenv)',CR,LF,EOLN
- errmess3 db 'MCB of environment not found !?',CR,LF,EOLN
- errmess4 db 'Not enough space left in evironment !',CR,LF,EOLN
- umb_stat db 0
- env_end dw 0
- psp_seg dw 0
- env_seg dw 0
- env_size dw 0
- save_sp dw 0
- FNAME db 60 dup(0)
- db 100 dup(0)
- STACKFRAME equ $
-
- ;******* the program is starting here ************
- init: cli
- mov [save_sp],sp
- mov sp,offset STACKFRAME ; define stack
- sti
- ;***** Search PSP of command.com ********
- push cs
- pop es ; psp starts at 0000 in this segment
- SearchPsp: mov bx,es ; save old psp (in cs) in bx
- mov dx,es:[16h] ; next psp in dx
- mov es,dx ; next psp in es
- cmp dx,bx ; es = cs ? (psp = next_psp ???)
- je FoundPsP
- cmp dx,0 ; error: psp has no next_psp-pointer
- jne SearchPsp
- mov dx,offset errmess1
- jmp Error ; exit because of error
- FoundPsp: mov psp_seg,dx
- mov dx,es:[02ch] ;Segment of Environment
- mov env_seg,dx ;now es points to "
-
- ;***** search the according memory control block (mcb) ********
- mov ah,58h
- mov al,02h
- int 21h ; read umb-status
- mov umb_stat,al ; and store it
- mov al,03h
- mov bx,1
- int 21h ; set umb-status to include UMBs
- mov ah,52h
- int 21h ; get dos-info-block
- mov dx,es
- dec dx
- mov es,dx
- add bx,12 ; es:bx points at segment of 1st mcb
- mov di,es:[bx] ; di is offset of 1st mcb
- mov dx,es:[bx+2] ; segment of 1st mcb
- mov es,dx ; es:di points at 1st mcb
- Search_MCB: inc dx ; segment of according memory
- cmp dx,env_seg ; is this the mcb for the environment?
- je Found_MCB
- cmp byte ptr es:[di],'Z'
- jne Cont_MCB ; last mcb ???
- mov dx,offset errmess3
- jmp error
- Cont_MCB: mov cx,es:[di+3] ; cx:=number of paragraphs of this mcb
- mov dx,es ; dx:= segment of this mcb
- add dx,cx
- inc dx
- mov es,dx ; es:= es(dx)+paragraphs(cx)+1
- xor di,di ; es:di points at next mcb
- jmp Search_MCB
- Found_MCB: mov dx,es:[di+3] ; dx:=number of paragraphs
- shl dx,4 ; dx:=number of bytes in env-mcb
- mov env_size,dx
- GetCmdLine: ;***** get the command line ************
- push cs
- pop es
- xor ch,ch
- mov cl,byte ptr cs:[080h] ; move length of cmdline into cx
- cmp cl,0
- jne search_end
- mov dx,offset errmess2
- jmp Error
- search_end: mov di,81h ; es:di points at cmdline
- mov al,20h ; Space= 20h
- repe scasb ; search 1st character after spaces
- jne cmd_cont ; go on, if last char<>20h
- mov dx,offset errmess2
- jmp error
- cmd_cont: dec di ; increased one too much
- inc cl ; dito
- mov bx,di ; cs:bx points at cmdline after spaces
- mov di,offset fname ; es:di points at fname
- CmdLnLoop: mov al,cs:[bx] ; get byte
- cmp al,60h
- jbe continue
- cmp al,7bh
- jae continue
- sub al,20h ; al:=upcase(al)
- continue: inc bx
- stosb ; put byte to es:di
- loop CmdLnLoop
- mov al,'='
- stosb ; fname= "%env_var%="
- push di ; save for later use
- GetName: ;****** get the path from the environment ****
- mov es,cs:[02ch] ;Segment of Environment
- xor di,di
- xor ax,ax
- SearchEnv: cmp al,es:[di] ;Search two zeroes
- je SearchEnd
- mov cx,0ffffh
- repnz scasb
- jmp SearchEnv
- SearchEnd: mov env_end,di
- add di,03 ;es:di points at path+name
- push di
- mov cx,100h ;Search End of string
- repnz scasb
- not cl ;Cl contains length
- std
- mov al,'\'
- repnz scasb ;search last '\'
- add cl,2 ;copy only path, not the '\'
- cld
- ;******* assemble full name from path and given name ********
- pop di
- mov dx,di ; store di temporarily in dx
- pop di ; di points at FNAME after "="
- push si ; save si
- mov si,dx ; and put old value of di in si
- push ds ; save ds
- push es
- pop ds ; ds:= es
- push cs
- pop es ; es:= cs
- rep movsb ; path is copied to NAME
- ;****** now the complete string is copied into environment *****
- pop ds ; restore ds
- mov es,env_seg ; segment of environment
- mov cx,di ; di points at last char
- sub cx,offset fname ; cx:= length(fname)
- mov dx,env_end
- add dx,cx ; dx:= possible end
- cmp dx,env_size ; does string fit in this mcb ???
- jb cont_copy ; jump if dx < env_size
- mov dx,offset errmess4
- jmp error
- cont_copy: mov di,env_end ; es:di points after last env-string
- mov si, offset fname ; ds:si points at fname
- rep movsb ; copy string
- xor ax,ax
- stosw ; two zeroes mark the end
- pop si
-
- EXIT: push cs
- pop ds
- mov ah,58h
- mov al,03h
- mov bl,umb_stat
- int 21h ; set umb-status back
- cli
- mov sp,[save_sp] ; restore stack pointer
- sti
- mov ax,4C00h ; Exit with error code 0
- int 21h
-
- ERROR: mov ah,9h
- int 21h
- jmp Exit
-
- end begin
-