home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / assemblr / library / masmsubs / getenv.asm < prev    next >
Assembly Source File  |  1987-07-27  |  3KB  |  142 lines

  1.  
  2.     name    getenv
  3.  
  4.     title    getenv - return value of environment variable
  5.  
  6.  
  7. save    macro    p1,p2,p3,p4,p5,p6,p7,p8,p9
  8.     irp    y,<p1,p2,p3,p4,p5,p6,p7,p8,p9>
  9.     ifnb    <y>
  10.     push    y
  11.     endif
  12.     endm
  13. restore macro
  14.     irp    z,<p9,p8,p7,p6,p5,p4,p3,p2,p1>
  15.     ifnb    <z>
  16.     pop    z
  17.     endif
  18.     endm
  19.     endm
  20.     endm
  21.  
  22.  
  23. cseg    segment byte    public    'code'
  24.  
  25.     assume    cs:cseg,ds:cseg,ss:nothing
  26.  
  27.  
  28.     comment    `
  29.  
  30. Routine to get the value of an environment variable.
  31.  
  32.  
  33. Calling stack frame:
  34.     <sp+00> -> dword ptr to environment variable name
  35.     <sp+04> -> dword ptr to environment variable return buffer
  36.  
  37. Where:
  38.     the environment variable is simply the name used in the SET command,
  39.     null terminated.
  40.     the environment variable return buffer is formatted as follows:
  41.  
  42.  
  43. ev_buffer    db    ev_buf_length    ; Max length of the buffer area
  44.         db    ev_buf_length - 1 dup(0)
  45. ev_buf_length    = $ - ev_buffer - 1
  46.  
  47.  
  48.     The string associated with the e.v. will be returned in this area,
  49.     and it will be null terminated.
  50.     If the e.v. name cannot be found, the buffer will be empty.
  51.  
  52. `
  53.  
  54.         public    getenv
  55.  
  56. ev_name equ    [bp+4]
  57. ev_buf    equ    [bp+8]
  58.  
  59. getenv    proc    near
  60.  
  61.     push    bp
  62.     mov    bp,sp            ; Get frame pointer
  63.     save    ax,bx,cx,dx,si,di,ds,es
  64.     mov    ah,30h            ; Get DOS version
  65.     int    21h
  66. ;
  67. ; We're going to assume that if it ain't v3.x, it's v2.x.  Who uses v1.x?
  68. ;  I suppose it could fail with v4.x, but I don't think that's something
  69. ;  I need to worry about.
  70. ;
  71.     mov    ah,51h            ; Assume 2.x
  72.     cmp    al,3            ; At least 3.0?
  73.     jb    ge_l1            ; No, assume 2.x
  74.     mov    ah,62h            ; If at least 3.0, use funct 62h
  75. ge_l1:
  76.     sub    al,al            ; Just in case...
  77.     int    21h            ; Get the PSP seg for our process
  78.     mov    ds,bx            ; Point to the PSP
  79.     mov    bx,2ch            ; Point to env base seg
  80.     mov    ds,[bx]            ; Get that pointer
  81.     les    di,ev_name        ; Point to the name they're looking for
  82.     sub    si,si            ; Start at beginning of env
  83. ge_l3:
  84.     push    di            ; Save this pointer
  85. ge_l4:
  86.     cmp    byte ptr es:[di],0    ; Is this the terminator?
  87.     je    ge_l6            ; If so, done looking
  88.     mov    al,es:[di]
  89.     cmp    al,'a'
  90.     jb    ge_l41
  91.     cmp    al,'z'+1
  92.     jae    ge_l41
  93.     sub    al,20h
  94.     mov    es:[di],al
  95. ge_l41:
  96.     cmpsb                ; Compare a byte
  97.     je    ge_l4            ; If not a match, stop looking
  98. ge_l5:
  99.     cmp    byte ptr [si],0        ; Scan for 0 byte
  100.     je    ge_l7            ; When found, continue
  101.     inc    si            ; Point to next
  102.     jmp    short ge_l5
  103. ge_l7:
  104.     pop    di            ; Get pointer back
  105.     inc    si
  106.     cmp    byte ptr [si],0        ; Is this the second null?
  107.     jne    ge_l3            ; If not, then keep looking
  108.     les    di,ev_buf
  109.     sub    al,al
  110.     inc    di
  111.     stosb
  112.     jmp    short ge_done        ; If it is, we're done looking
  113. ge_l6:
  114.     cmp    byte ptr [si],'='    ; Is it the separator?
  115.     jne    ge_l5            ; If not, this ain't the one...
  116.     inc    si            ; Point past the "="
  117.     pop    di            ; Clear stack
  118.     les    di,ev_buf        ; Point to buffer
  119.     mov    cl,es:[di]        ; Get max buffer len
  120.     sub    ch,ch
  121.     dec    cx            ; Allow for terminator
  122.     inc    di            ; Point to first byte of buffer
  123. ge_l8:
  124.     lodsb                ; Get a byte
  125.     stosb                ; And put it in
  126.     or    al,al            ; Is it the terminator?
  127.     je    ge_l9
  128.     loop    ge_l8            ; Do for up to max buffer len
  129. ge_l9:
  130.     sub    al,al
  131.     stosb                ; Put in OUR terminator
  132. ge_done:
  133.     restore
  134.     pop    bp            ; Restore old frame pointer
  135.     ret    8
  136.  
  137. getenv    endp
  138.  
  139. cseg    ends
  140.  
  141.     end
  142.