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