home *** CD-ROM | disk | FTP | other *** search
/ Software Recommendations - 1998 Season 1 / DNBCD4.iso / develop / tool / zip / SETENV.RAR / SETENV.ASM < prev    next >
Encoding:
Assembly Source File  |  1994-01-06  |  8.0 KB  |  196 lines

  1. Title SetEnv.asm
  2. ; by dOnut hOle in 1/1996
  3. ; Use TLINK /t to link - it's a com !!!
  4. ;
  5. ; This little program copies the path it was started in into the environment-
  6. ; variable given as parameter
  7. ; SYNTAX: SETENV <varname>
  8. ; e.g.: setenv.com is in c:\game\donut
  9. ;   SETENV DON
  10. ; -> environment variable DON is set to 'C:\GAME\DONUT'
  11.  
  12. .286
  13. .model tiny
  14.  
  15. .code
  16.         org 100h       
  17. begin:  jmp init
  18.  
  19.  
  20. ; ****** MACROS ********
  21. CR         equ     0Dh
  22. LF         equ     0Ah
  23. EOLN       equ     '$'
  24.  
  25. ;************ data ************
  26. errmess1          db 'PSP of command.com not found !?',CR,LF,EOLN
  27. errmess2          db 'SetEnv - by dOnut hOle 1/1996',CR,LF
  28.                   db '  Sets a given environment variable to the path it was started in.',CR,LF
  29.                   db '  SYNTAX: SETENV <variable>',CR,LF
  30.                   db '  e.g.: setenv curr_path  (now curr_path contains path of setenv)',CR,LF,EOLN
  31. errmess3          db 'MCB of environment not found !?',CR,LF,EOLN
  32. errmess4          db 'Not enough space left in evironment !',CR,LF,EOLN
  33. umb_stat          db      0
  34. env_end           dw      0
  35. psp_seg           dw      0
  36. env_seg           dw      0
  37. env_size          dw      0
  38. save_sp           dw      0
  39. FNAME             db     60 dup(0)
  40.                   db    100 dup(0)
  41. STACKFRAME        equ $
  42.  
  43. ;******* the program is starting here ************
  44. init:       cli
  45.             mov [save_sp],sp
  46.             mov sp,offset STACKFRAME    ; define stack
  47.             sti
  48.             ;***** Search PSP of command.com ********
  49.             push cs
  50.             pop es                      ; psp starts at 0000 in this segment
  51. SearchPsp:  mov bx,es                   ; save old psp (in cs) in bx
  52.             mov dx,es:[16h]             ; next psp in dx
  53.             mov es,dx                   ; next psp in es
  54.             cmp dx,bx                   ; es = cs ? (psp = next_psp ???)
  55.             je FoundPsP
  56.             cmp dx,0                    ; error: psp has no next_psp-pointer
  57.             jne SearchPsp
  58.             mov dx,offset errmess1
  59.             jmp Error                   ; exit because of error
  60. FoundPsp:   mov psp_seg,dx
  61.             mov dx,es:[02ch]            ;Segment of Environment
  62.             mov env_seg,dx              ;now es points to "
  63.  
  64.             ;***** search the according memory control block (mcb) ********
  65.             mov ah,58h
  66.             mov al,02h
  67.             int 21h                     ; read umb-status
  68.             mov umb_stat,al             ; and store it
  69.             mov al,03h                  
  70.             mov bx,1
  71.             int 21h                     ; set umb-status to include UMBs
  72.             mov ah,52h
  73.             int 21h                     ; get dos-info-block
  74.             mov dx,es
  75.             dec dx
  76.             mov es,dx
  77.             add bx,12                   ; es:bx points at segment of 1st mcb
  78.             mov di,es:[bx]              ; di is offset of 1st mcb
  79.             mov dx,es:[bx+2]            ; segment of 1st mcb
  80.             mov es,dx                   ; es:di points at 1st mcb
  81. Search_MCB: inc dx                      ; segment of according memory
  82.             cmp dx,env_seg              ; is this the mcb for the environment?
  83.             je Found_MCB
  84.             cmp byte ptr es:[di],'Z'
  85.             jne Cont_MCB                ; last mcb ???
  86.             mov dx,offset errmess3
  87.             jmp error
  88. Cont_MCB:   mov cx,es:[di+3]            ; cx:=number of paragraphs of this mcb
  89.             mov dx,es                   ; dx:= segment of this mcb
  90.             add dx,cx                    
  91.             inc dx                      
  92.             mov es,dx                   ; es:= es(dx)+paragraphs(cx)+1
  93.             xor di,di                   ; es:di points at next mcb
  94.             jmp Search_MCB
  95. Found_MCB:  mov dx,es:[di+3]            ; dx:=number of paragraphs
  96.             shl dx,4                    ; dx:=number of bytes in env-mcb
  97.             mov env_size,dx
  98. GetCmdLine: ;***** get the command line ************
  99.             push cs
  100.             pop es
  101.             xor ch,ch
  102.             mov cl,byte ptr cs:[080h]   ; move length of cmdline into cx
  103.             cmp cl,0
  104.             jne search_end
  105.             mov dx,offset errmess2
  106.             jmp Error 
  107. search_end: mov di,81h                  ; es:di points at cmdline
  108.             mov al,20h                  ; Space= 20h
  109.             repe scasb                  ; search 1st character after spaces
  110.             jne cmd_cont                ; go on, if last char<>20h
  111.             mov dx,offset errmess2
  112.             jmp error
  113. cmd_cont:   dec di                      ; increased one too much
  114.             inc cl                      ; dito
  115.             mov bx,di                   ; cs:bx points at cmdline after spaces
  116.             mov di,offset fname         ; es:di points at fname
  117. CmdLnLoop:    mov al,cs:[bx]            ; get byte
  118.               cmp al,60h
  119.               jbe continue
  120.               cmp al,7bh
  121.               jae continue
  122.               sub al,20h                ; al:=upcase(al)
  123. continue:     inc bx
  124.               stosb                     ; put byte to es:di
  125.             loop CmdLnLoop
  126.             mov al,'='
  127.             stosb                       ; fname= "%env_var%="
  128.             push di                     ; save for later use
  129. GetName:    ;****** get the path from the environment ****
  130.             mov es,cs:[02ch]            ;Segment of Environment
  131.             xor di,di
  132.             xor ax,ax
  133. SearchEnv:    cmp al,es:[di]            ;Search two zeroes
  134.               je SearchEnd
  135.               mov cx,0ffffh
  136.               repnz scasb
  137.             jmp SearchEnv
  138. SearchEnd:  mov env_end,di
  139.             add di,03                   ;es:di points at path+name
  140.             push di
  141.             mov cx,100h                 ;Search End of string 
  142.             repnz scasb
  143.             not cl                      ;Cl contains length
  144.             std
  145.             mov al,'\'
  146.             repnz scasb                 ;search last '\'
  147.             add cl,2                    ;copy only path, not the '\'
  148.             cld
  149.             ;******* assemble full name from path and given name ********
  150.             pop di
  151.             mov dx,di                   ; store di temporarily in dx
  152.             pop di                      ; di points at FNAME after "="
  153.             push si                     ; save si
  154.             mov si,dx                   ; and put old value of di in si
  155.             push ds                     ; save ds
  156.             push es
  157.             pop ds                      ; ds:= es
  158.             push cs
  159.             pop es                      ; es:= cs
  160.             rep movsb                   ; path is copied to NAME
  161.             ;****** now the complete string is copied into environment *****
  162.             pop ds                      ; restore ds
  163.             mov es,env_seg              ; segment of environment
  164.             mov cx,di                   ; di points at last char
  165.             sub cx,offset fname         ; cx:= length(fname)
  166.             mov dx,env_end
  167.             add dx,cx                   ; dx:= possible end
  168.             cmp dx,env_size             ; does string fit in this mcb ???
  169.             jb cont_copy                ; jump if dx < env_size
  170.             mov dx,offset errmess4
  171.             jmp error
  172. cont_copy:  mov di,env_end              ; es:di points after last env-string
  173.             mov si, offset fname        ; ds:si points at fname
  174.             rep movsb                   ; copy string
  175.             xor ax,ax
  176.             stosw                       ; two zeroes mark the end
  177.             pop si
  178.  
  179. EXIT:       push cs
  180.             pop ds
  181.             mov ah,58h
  182.             mov al,03h                  
  183.             mov bl,umb_stat
  184.             int 21h                     ; set umb-status back
  185.             cli
  186.             mov sp,[save_sp]            ; restore stack pointer
  187.             sti
  188.             mov ax,4C00h                ; Exit with error code 0
  189.             int 21h
  190.  
  191. ERROR:      mov ah,9h
  192.             int 21h
  193.             jmp Exit
  194.  
  195. end      begin
  196.