home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / Samples / CASM.ARJ / GETENV.ASM < prev    next >
Encoding:
Assembly Source File  |  1988-01-24  |  3.0 KB  |  146 lines

  1. ;_ getenv.asm   Sun Jan 24 1988   Modified by: Walter Bright */
  2. ; Copyright (C) 1985-1988 by Northwest Software
  3. ; All Rights Reserved
  4. ; Written by Walter Bright
  5.  
  6.     include    MACROS.ASM
  7.  
  8.     begdata
  9.     c_extrn    _psp,word
  10.  
  11.     if SPTR
  12. envptr    dw    0        ;pointer to start of environment string
  13.     endif
  14.     enddata
  15.  
  16.     if SPTR
  17.     if LCODE
  18.     c_extrn    malloc,far
  19.     else
  20.     c_extrn    malloc,near
  21.     endif
  22.     endif
  23.  
  24.     begcode    getenv
  25.  
  26. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  27. ; Get environment string
  28. ; Return pointer to it.
  29.  
  30.     c_public getenv
  31. func    getenv
  32.     push    BP
  33.     mov    BP,SP
  34.     .save    <SI,DI>
  35.     cld
  36.     if LPTR
  37.     mov    ES,_psp
  38.     mov    ES,ES:2Ch        ;get segment of environment string
  39.     clr    DI
  40.     else
  41.       ife ESeqDS
  42.     push    DS
  43.     pop    ES
  44.       endif
  45.     mov    DI,envptr        ;pointer to environment string
  46.     tst    DI            ;initialized?
  47.     jnz    L1            ;yes
  48.  
  49.     ;Copy the environment string into the data segment so we can
  50.     ;access it without far pointers.
  51.  
  52.     ;First figure out how long it is.
  53.  
  54.     push    ES
  55.     mov    ES,_psp
  56.     mov    AX,ES:2Ch        ;get segment of environment string
  57.     tst    AX
  58.     jz    L4            ;no environment string
  59.     mov    ES,AX
  60.     clr    DI
  61.     clr    AL            ;look for 0
  62.     mov    CX,0FFFFh        ;largest possible count
  63. L2:    repne    scasb            ;scan for 0
  64.     scasb                ;look at next byte
  65.     jnz    L2            ;read next environment string
  66.     pop    ES
  67.     push    DI
  68.     push    DI            ;DI = # of bytes in environment string
  69.     callm    malloc            ;allocate that many bytes
  70.     add    SP,2
  71.     pop    CX            ;CX = # of bytes in string
  72.     tst    AX            ;error?
  73.     jz    L3            ;yes
  74.     mov    DI,AX
  75.     mov    envptr,AX        ;save environment pointer
  76.     clr    SI
  77.     push    DS            ;save old segment
  78.     mov    DS,_psp
  79.     mov    DS,DS:2Ch        ;DS:SI -> original environment string
  80.     rep    movsb            ;transfer the bytes
  81.     pop    DS            ;restore original segment
  82.     mov    DI,AX            ;DI -> new environment string
  83. L1:
  84.     endif
  85.  
  86.     ;Now look for the environment string
  87.     ;ES:DI -> environment
  88.  
  89.     clr    AX            ;0 used for the .ifs, scasb and error
  90.                     ; return value!
  91.     mov    CX,-1            ;arbitrarilly large count
  92.     mov    BX,CX            ;BX = -1 (save cycles on indexing)
  93.     if SPTR
  94. L10:    mov    SI,P[BP]        ;SI -> name we're looking for
  95.     repe    cmpsb            ;scan till first mismatch
  96.     .if    <[BX + DI]> e AL, L3    ;end of environment string, no match
  97.     else
  98.     push    DS
  99. L10:    lds    SI,P[BP]        ;SI -> name we're looking for
  100.     repe    cmpsb            ;scan till first mismatch
  101.     .if    <ES:[BX + DI]> e AL, L6    ;end of environment string, no match
  102.     endif
  103.     .if    <[BX + SI]> e AL, L7    ;possible match
  104. L8:    repne    scasb            ;scan for terminating 0
  105.     jmp    L10
  106.  
  107. L7:
  108.     if SPTR
  109.     .if    <byte ptr [BX + DI]> ne '=', L8
  110.     mov    AX,DI            ;point to value of environment var
  111. L3:
  112.     else
  113.     .if    <byte ptr ES:[BX + DI]> ne '=', L8
  114.       ifdef MSC
  115.     mov    AX,DI            ;point to value of environment var
  116.     mov    DX,ES
  117.       else
  118.     mov    BX,DI            ;point to value of environment var
  119.     mov    AX,ES
  120.       endif
  121. L11:    pop    DS
  122.     endif
  123.     .restore <DI,SI>
  124.     pop    BP
  125.     ret
  126.  
  127.     if SPTR
  128. L4:    pop    ES
  129.     .restore <DI,SI>
  130.     pop    BP
  131.     ret
  132.     else
  133. L6:
  134.       ifdef MSC
  135.     cwd                ;DX,AX = 0
  136.       else
  137.     inc    BX            ;AX,BX = 0
  138.       endif
  139.     jmp    L11
  140.     endif
  141. c_endp    getenv
  142.  
  143.     endcode    getenv
  144.  
  145.     end
  146.