home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 2.ddi / CLIBSRC3.ZIP / SETENVP.ASM < prev    next >
Encoding:
Assembly Source File  |  1992-06-10  |  4.4 KB  |  153 lines

  1. ;[]-----------------------------------------------------------------[]
  2. ;|      SETENVP.ASM -- Prepare Environment                           |
  3. ;[]-----------------------------------------------------------------[]
  4.  
  5. ;
  6. ;       C/C++ Run Time Library - Version 5.0
  7. ;       Copyright (c) 1987, 1992 by Borland International
  8. ;       All Rights Reserved.
  9.  
  10.  
  11.         INCLUDE RULES.ASI
  12.  
  13. ;       Segment and Group declarations
  14.  
  15. Header@
  16.  
  17. ;       External references
  18.  
  19. ExtProc@        malloc, __CDECL__
  20. ExtProc@        abort, __CDECL__
  21.  
  22. ExtSym@         _envseg, WORD, __CDECL__
  23. ExtSym@         _envLng, WORD, __CDECL__
  24. ExtSym@         _envSize, WORD, __CDECL__
  25. dPtrExt@        _C0environ, __CDECL__
  26.  
  27.         SUBTTL  Prepare Environment
  28.         PAGE
  29. ;/*                                                     */
  30. ;/*-----------------------------------------------------*/
  31. ;/*                                                     */
  32. ;/*     Prepare Environment                             */
  33. ;/*     -------------------                             */
  34. ;/*                                                     */
  35. ;/*-----------------------------------------------------*/
  36. ;/*                                                     */
  37.  
  38. ;This is the symbol which pulls in _setenvp
  39.                 public __setenvp__
  40. __setenvp__     equ     0
  41.  
  42. _INIT_          SEGMENT WORD PUBLIC 'INITDATA'
  43.                 db      0                       ;near call
  44.                 db      16                      ;priority 16
  45.                 dw      offset __setenvp
  46.                 dw      ?
  47. _INIT_          ENDS
  48.  
  49. ;       Allocate a buffer to hold environment variables
  50.  
  51. _TEXT           segment
  52.  
  53. __setenvp       proc near
  54.                 push    si
  55.                 push    di
  56.  
  57. IF      LDATA EQ 0
  58.                 mov     cx, _envLng@
  59.                 push    cx
  60.                 call    malloc@
  61.                 pop     cx
  62.                 mov     di, ax
  63.                 or      ax, ax
  64.                 jz      _Failed         ; Memory allocation failed
  65.                 push    ds
  66.                 push    ds
  67.                 pop     es
  68.                 mov     ds, _envseg@
  69.                 xor     si, si
  70.                 cld
  71.                 rep     movsb
  72.                 pop     ds
  73.                 mov     di, ax
  74. ELSE
  75.                 mov     es, _envseg@
  76.                 xor     di, di
  77. ENDIF
  78.  
  79. ;       Allocate a buffer to hold envp array
  80.  
  81.                 push    es              ; Save Environment Segment address
  82.                 push    _envSize@
  83.                 call    malloc@
  84.                 pop     bx
  85.                 mov     bx, ax
  86.                 pop     es              ; Restore Environment Segment address
  87. IF      LDATA
  88.                 mov     word ptr environ@, ax
  89.                 mov     word ptr environ@+2, dx
  90.                 push    ds
  91.                 mov     ds, dx
  92.                 or      ax, dx
  93. ELSE
  94.                 mov     word ptr environ@, ax
  95.                 or      ax, ax
  96. ENDIF
  97.                 jnz     SetEnviron
  98. _Failed         label   near            ; Memory allocation failed
  99.                 jmp     abort@
  100.  
  101.  
  102. ;       Now, store environment variables address
  103.  
  104. SetEnviron      label   near
  105.                 xor     ax, ax
  106.                 mov     cx, -1
  107. IF  LDATA
  108.                 cmp byte ptr es:[di], 0     ; Is the environment empty?
  109. ELSE
  110.                 cmp byte ptr [di], 0        ; Is the environment empty?
  111. ENDIF
  112.                 je  SetEnviron1
  113. SetEnviron0     label   near
  114.                 mov     [bx], di
  115. IF      LDATA
  116.                 mov     [bx+2], es
  117.                 add     bx, 4
  118. ELSE
  119.                 add     bx, 2
  120. ENDIF
  121.                 repnz   scasb
  122.                 cmp     es:[di], al
  123.                 jne     SetEnviron0         ; Set next pointer
  124. SetEnviron1     label   near
  125. IF      LDATA
  126.                 mov     [bx], ax
  127.                 mov     [bx+2], ax
  128.                 pop     ds
  129. ELSE
  130.                 mov     [bx], ax
  131. ENDIF
  132.                 pop     di
  133.                 pop     si
  134.  
  135. IF      LDATA
  136.                 mov     ax,word ptr _environ+2
  137.                 mov     word ptr __C0environ+2,ax
  138. ENDIF
  139.                 mov     ax,word ptr _environ
  140.                 mov     word ptr __C0environ,ax
  141.  
  142.                 ret
  143.  
  144.                 endp
  145.                 ends
  146.  
  147. _DATA           segment
  148. dPtrPub@        environ,        0,                      __CDECL__
  149.                 ends
  150.                 END
  151.