home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_07_07 / v7n7060a.txt < prev    next >
Text File  |  1989-09-05  |  5KB  |  157 lines

  1.  
  2. Listing 4
  3.  
  4. ; Procedure DoEnviron
  5. ; Purpose Convert a string to an environment variable
  6. ; Input   String in "string"
  7. ; Output  String in "WHAT" environment variable;
  8. ;       AX has 0 for success, nonzero for failure
  9.  
  10. DoEnviron PROC
  11.      call GetEnv    ; Get environment size, length, address
  12.      mov  dx,ax     ; Save size and length
  13.      mov  bx,cx
  14.  
  15. ; Find "WHAT="
  16.  
  17.      sub  di,di     ;Point to start
  18.      sub  al,al     ;Search for zero
  19.      mov  si, OFFSET what     ; Point source at "WHAT="
  20.  
  21.  
  22.                            11
  23.  
  24.  
  25.  
  26. findwh:   repne     scasb     ; Search
  27.      cmp  BYTE PTR es:[di],)  ; If double null, end of environment         
  28.      je   gotend
  29.      jcxz noroom              ; Error if not found
  30.      push di                  ; Save
  31.      push cx
  32.      mov  si,OFFSET what      ;Load address and length of "what"
  33.      mov  cx, lwhat           ; for comparison
  34.      repe cmpsb               ; Compare
  35.      mov  si,di               ; Make copy
  36.      pop  cx                  ; Restore
  37.      pop  di
  38.      jnz  findwh
  39.  
  40. ; Find end of "WHAT" variable
  41.  
  42.      xchg      di,si
  43.      repne     scasb          ; Find end of environment variable
  44.      xchg      si,di          ; Point source to next variable
  45.  
  46. ; Calculate characters left to write
  47.      
  48.      mov  cx,bx               ; Load total characters
  49.      sub  cx,si               ; Subtract finished to get left
  50.  
  51. ; Move everything back to overwrite "WHAT="
  52.  
  53. movenv:   push ds             : Save DS 
  54.      mov  ax,es               ; Copy to ES
  55.      mov  ds,ax
  56.      rep  movsb               ; Copy
  57.      mov  BYTE PTR es:[di],0  ; Put null at end in case of error
  58.      pop  ds   ; Restore
  59.  
  60. ; Check environment space
  61.  
  62. gotend:   mov  al,actual      ; Load length of string
  63.      sub  ah,ah               ; Clear top
  64.      add  ax,lwhat            ; Add length of name
  65.      add  ax,di               ; Add position to get final length
  66.      cmp  ax,dx               ; Is it longer than environment?
  67.      jge  noroom              ; Yes? Quit
  68.  
  69. : Put WHAT= at end
  70.  
  71.      mov  si,OFFSET what      ; Load address and length of what
  72.      mov  cx,lwhat
  73.      rep  movsb
  74.  
  75. ; Put new string at end
  76.  
  77.      mov  si,OFFSET string    ; Load address and length of string
  78.      mov  cl,actual
  79.      rep  movsb
  80.      mov  WORD PTR es:[di],0  ; Put double null at end
  81.  
  82.  
  83.                            12
  84.  
  85.  
  86.  
  87.      sub  ax,ax               ; Return 0 for success
  88.      ret
  89.  
  90. noroom:   inc  ax             ; Return nonzero for fail
  91.      ret
  92. DoEnviron ENDP
  93.  
  94. ; Procedure GetEnv
  95. ; Purpose   Find and measure the environment
  96. ; Input     None
  97. ; Output    Segment of environment in ES, size in AX, length in CX
  98.  
  99. GetEnv    PROC
  100.      mov  dx,es:10h ; Load segment of COMMAND.COM 
  101.      mov  es,dx     ; into ES
  102.      mov  ax,es:2Ch ; Load COMMAND.Com's environment
  103.      or   ax,ax     ; Is it 0?
  104.      jnz  secondry  ; No? This is a secondary command
  105.                     ; and we have its environment in AX
  106.      dec  dx        ; Yes? This is original COMMAND.COM
  107.      mov  es,dx     ; so point ES to paragraph before PSP
  108.      add  dx,es:03  ; Offset of environment is 3 bytes in
  109.      add  dx,2      ; Adjust it back to PSP
  110.      mov  ax,dx     ; Put it in AX
  111. secondry:
  112.  
  113. ; Note:
  114. ; CodeView cannot debug the previous section of code, because the PSP
  115. ; addresses checked by the code are those passed from DOS to CodeView,
  116. ; not addresses passed from DOS to the program.  To debug with CodeView,
  117. ; find the actual address of the environment:
  118.  
  119. ;    S 500:0 L FFFF "COMSPEC="
  120.  
  121. ; When you find the actual address, hard code it into your program:          
  122.  
  123. ;    mov  ax,110Ch  ; Debug line
  124.  
  125. ; Comment the line out for final assembly after debugging.
  126.  
  127.      mov  si,ax     ; Save a copy
  128.      sub  dx,dx     ; Clear DX for multiply
  129.      dec  ax        ; Get paragraph before environment
  130.      mov  es,ax     ; Load into DS
  131.      mov  ax,es:03  ; Size in paragraphs is at byte 4
  132.      mov  cx, 16    ; Multiply by 16
  133.      mul  cx
  134.      mov  es,si     ; Restore environment address
  135.      sub  di,di     ; Point to start
  136.      mov  cx,ax     ; Load maximum count (size of 
  137.      mov  bx,ax     : environment) and save a copy
  138.      sub  ax,ax     : Search for double null
  139. null2:    repne     scasb     ; Look for null
  140.      jz   noerr     ; If not out of space, continue
  141.      sub  ax,ax     ; else error (return 0)
  142.  
  143.  
  144.                            13
  145.  
  146.  
  147.  
  148.      jmp  error2
  149. noerr:    cmp  BYTE PTR es: [di],0 ; Is it double null?
  150.      jne  null2     ; No? Look again
  151.      mov  cx,di     ; Yes? Save length in CX
  152.      mov  ax,bx     ; Reload size to AX
  153.  
  154.      ret
  155. GetEnv    ENDP 
  156.  
  157.