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

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