home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 5.ddi / CLIBSRC2.ZIP / GETENV.CAS < prev    next >
Encoding:
Text File  |  1992-06-10  |  3.8 KB  |  140 lines

  1. /*---------------------------------------------------------------------------
  2.  * filename - getenv.cas
  3.  *
  4.  * function(s)
  5.  *        getenv - get string from environment
  6.  *--------------------------------------------------------------------------*/
  7.  
  8. /*
  9.  *      C/C++ Run Time Library - Version 5.0
  10.  *
  11.  *      Copyright (c) 1987, 1992 by Borland International
  12.  *      All Rights Reserved.
  13.  *
  14.  */
  15.  
  16.  
  17. #pragma inline
  18. #include <asmrules.h>
  19. #include <stdlib.h>
  20. #include <stddef.h>
  21.  
  22. /*--------------------------------------------------------------------------*
  23.  
  24. Name            getenv - get string from environment
  25.  
  26. Usage           char *getenv(const char *envvar);
  27.  
  28. Prototype in    stdlib.h
  29.  
  30. Description     The MS-DOS environment consists of a series of entries that
  31.                 are of the form:
  32.  
  33.                 name=string\0
  34.  
  35.                 getenv searches the environment for the entry corresponding
  36.                 to envvar, then returns a pointer to string.
  37.  
  38.                 The string  comparison is case-sensitive, in  the usual "C"
  39.                 tradition.
  40.  
  41. Return value    On  success,   getenv  returns  a  pointer   to  the  value
  42.                 associated with envvar.
  43.  
  44. *---------------------------------------------------------------------------*/
  45.  
  46. char * _CType _FARFUNC getenv(const char *nameP)
  47. {
  48.         char  **envP;
  49.  
  50.         /* Compute nameP length and remember first character */
  51.  
  52. asm             LES_    di, nameP
  53. #if (LDATA)
  54. asm             mov     ax, ES
  55. asm             or      ax, di
  56. #else
  57. asm             push    ds
  58. asm             pop     es
  59. asm             or      di, di
  60. #endif
  61. asm             jz      VarNotFound
  62. asm             mov     al, 0
  63. asm             mov     ah, ES_ [di]    /* remember first char of name  */
  64. asm             mov     cx, -1
  65. asm             cld
  66. asm     repne   scasb
  67. asm             not     cx
  68. asm             dec     cx
  69. asm             jz      VarNotFound
  70.  
  71.         /* Look for nameP in environment array */
  72.  
  73. #if __HUGE__
  74. asm             mov     di, seg environ
  75. asm             mov     DS, di
  76. #endif
  77. asm             LES_    di, DPTR_(environ)
  78. #if (LDATA)
  79. asm             mov     W1 (envP), ES
  80. asm             mov     bx, ES
  81. asm             or      bx, di
  82. #else
  83. asm             or      di, di
  84. #endif
  85. asm             mov     W0 (envP), di
  86. asm             jnz     FirstVariable
  87.  
  88. /* If no match can be found, return a NULL pointer. */
  89.  
  90. VarNotFound:
  91.         return NULL;
  92.  
  93.                 /* Does the first character match ??? */
  94.  
  95. NextVariable:
  96. asm             add     W0 (envP), dPtrSize
  97. asm             LES_    di, envP
  98. FirstVariable:
  99. asm             LES_    di, ES_ [di]
  100. #if (LDATA)
  101. asm             mov     bx, ES
  102. asm             or      bx, di
  103. #else
  104. asm             or      di, di
  105. #endif
  106. asm             jz      VarNotFound
  107. asm             mov     al, ES_ [di]
  108. asm             or      al, al          /* "" terminates environment */
  109. asm             jz      VarNotFound
  110. asm             cmp     ah, al          /* compare first characters */
  111. asm             jne     NextVariable
  112.  
  113. asm             mov     bx, cx
  114. #ifndef _Windows
  115. asm             cmp     BY0 (ES_ [di+bx]), '='
  116. asm             jne     NextVariable    /* must be followed by '=' */
  117.                 /* Yes, so compare the remainder of nameP */
  118. #endif
  119.                 pushDS_
  120. asm             LDS_    si, nameP
  121. asm     repe    cmpsb
  122.                 popDS_
  123. asm             xchg    cx, bx
  124. asm             jne     NextVariable
  125. #ifdef  _Windows
  126. asm             cmp     BY0 (ES_ [di]), '='
  127. asm             jne     NextVariable
  128. #endif
  129. asm             inc     di
  130.  
  131. /* Now return the pointer to the caller. */
  132.  
  133. VarFound:
  134. #if (LDATA)
  135.         return (char _es *) _DI;
  136. #else
  137.         return (char *) _DI;
  138. #endif
  139. }
  140.