home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c065 / 2.ddi / CLIB2.ZIP / SETENVP.ASM < prev    next >
Encoding:
Assembly Source File  |  1990-06-07  |  3.0 KB  |  144 lines

  1. ;[]-----------------------------------------------------------------[]
  2. ;|      SETENVP.ASM -- Prepare Environment                           |
  3. ;|                                                                   |
  4. ;|      Turbo-C Run Time Library        Version 3.0                  |
  5. ;|                                                                   |
  6. ;|      Copyright (c) 1987,1988,1990 by Borland International Inc.   |
  7. ;|      All Rights Reserved.                                         |
  8. ;[]-----------------------------------------------------------------[]
  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    ; Memory allocation failed
  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. SetEnviron0    label    near
  108.         mov    [bx], di
  109. IF    LDATA
  110.         mov    [bx+2], es
  111.         add    bx, 4
  112. ELSE
  113.         add    bx, 2
  114. ENDIF
  115.         repnz    scasb
  116.         cmp    es:[di], al
  117.         jne    SetEnviron0    ; Set next pointer
  118. IF    LDATA
  119.         mov    [bx], ax
  120.         mov    [bx+2], ax
  121.         pop    ds
  122. ELSE
  123.         mov    [bx], ax
  124. ENDIF
  125.         pop    di
  126.         pop    si
  127.  
  128. IF    LDATA
  129.                 mov     ax,word ptr _environ+2
  130.                 mov     word ptr __C0environ+2,ax
  131. ENDIF
  132.                 mov     ax,word ptr _environ
  133.                 mov     word ptr __C0environ,ax
  134.  
  135.         ret
  136.  
  137.         endp
  138.                 ends
  139.  
  140. _DATA           segment
  141. dPtrPub@        environ,        0,                      __CDECL__
  142.                 ends
  143.                 END
  144.