home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / PP0802.ZIP / GETENV1.ASM next >
Assembly Source File  |  1988-10-11  |  3KB  |  79 lines

  1. ;----------------------------------------------------------------------
  2. ; GETENV --- Return address and length of variable portion of environment
  3. ;            string OS/2 version using DosGetEnv
  4. ;
  5. ; Copyright (c) 1989 Ziff Communications Co.
  6. ; PC Magazine * Ray Duncan
  7. ;
  8. ; Call with:    DS:SI = ASCIIZ env. variable name
  9. ; Returns:      ES:DI = address of env. variable
  10. ;               AX    = length (0 = not found)
  11. ; Uses:         nothing
  12. ;----------------------------------------------------------------------
  13.         .286
  14.         extrn   DosGetEnv:far   ; OS/2 API function
  15.  
  16. _TEXT   segment word public 'CODE'
  17.         assume  cs:_TEXT
  18.                                 ; local variables...
  19. envseg  equ     [bp-2]          ; environment segment
  20. cmdoffs equ     [bp-4]          ; command line offset   
  21.  
  22.         public  getenv          ; make visible to Linker
  23.  
  24. getenv  proc    near
  25.  
  26.         enter   4,0             ; allocate local variables
  27.         push    cx              ; save registers
  28.         push    si
  29.  
  30.         push    ss              ; get selector for environment 
  31.         lea     ax,envseg       ; and offset of command line 
  32.         push    ax
  33.         push    ss
  34.         lea     ax,cmdoffs
  35.         push    ax
  36.         call    DosGetEnv       ; transfer to OS/2      
  37.         or      ax,ax           ; did function succeed?
  38.         jz      get1            ; jump if successful
  39.  
  40.         xor     ax,ax           ; DosGetEnv failed, 
  41.         jmp     get5            ; return AX = 0
  42.  
  43. get1:   mov     es,envseg       ; set ES:BX = command line
  44.         mov     cx,8000h        ; assume max env. = 32 KB       
  45.         xor     di,di           ; initial env. offset
  46.         xor     ax,ax           ; default length result 
  47.  
  48. get2:   cmp     byte ptr es:[di],0 ; check for end of environment
  49.         je      get5            ; end reached, return AX = 0
  50.  
  51.         pop     si              ; initialize address of target
  52.         push    si              ; variable to be found
  53.  
  54.         repe cmpsb              ; compare target and env. strings
  55.         cmp     byte ptr [si-1],0       
  56.         jne     get3            ; jump if incomplete match
  57.         cmp     byte ptr es:[di-1],'='
  58.         je      get4            ; jump if match was complete
  59.  
  60. get3:   repne scasb             ; match incomplete, scan for end of env str
  61.         jmp     get2            ; and try again to match
  62.  
  63. get4:   push    di              ; save address after = sign
  64.         repne scasb             ; look for end of this string
  65.         pop     ax              ; get back starting address 
  66.         xchg    di,ax           ; find string length
  67.         sub     ax,di
  68.         dec     ax              ; don't include null byte
  69.  
  70. get5:   pop     si              ; common exit point restore registers
  71.         pop     cx
  72.         leave                   ; discard local variables
  73.         ret                     ; return to caller
  74.  
  75. getenv  endp
  76.  
  77. _TEXT   ends
  78.         end
  79.