home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c065 / 2.ddi / CLIB2.ZIP / GETENV.CAS < prev    next >
Encoding:
Text File  |  1990-06-07  |  3.2 KB  |  135 lines

  1. /*---------------------------------------------------------------------------
  2.  * filename - getenv.cas
  3.  *
  4.  * function(s)
  5.  *        getenv - get string from environment
  6.  *--------------------------------------------------------------------------*/
  7.  
  8. /*[]------------------------------------------------------------[]*/
  9. /*|                                                              |*/
  10. /*|     Turbo C Run Time Library - Version 3.0                   |*/
  11. /*|                                                              |*/
  12. /*|                                                              |*/
  13. /*|     Copyright (c) 1987,1988,1990 by Borland International    |*/
  14. /*|     All Rights Reserved.                                     |*/
  15. /*|                                                              |*/
  16. /*[]------------------------------------------------------------[]*/
  17.  
  18. #pragma inline
  19. #include <asmrules.h>
  20. #include <stdlib.h>
  21. #include <stddef.h>
  22.  
  23. /*--------------------------------------------------------------------------*
  24.  
  25. Name        getenv - get string from environment
  26.  
  27. Usage        char *getenv(const char *envvar);
  28.  
  29. Prototype in    stdlib.h
  30.  
  31. Description    The MS-DOS environment consists of a series of entries that
  32.         are of the form:
  33.  
  34.         name=string\0
  35.  
  36.         getenv searches the environment for the entry corresponding
  37.         to envvar, then returns a pointer to string.
  38.  
  39.         The string  comparison is case-sensitive, in  the usual "C"
  40.         tradition.
  41.  
  42. Return value    On  success,   getenv  returns    a  pointer   to  the  value
  43.         associated with envvar.
  44.  
  45. *---------------------------------------------------------------------------*/
  46. char *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. asm        mov    bx, cx
  113. asm        cmp    BY0 (ES_ [di+bx]), '='
  114. asm        jne    NextVariable    /* must be followed by '=' */
  115.  
  116.         /* Yes, so compare the remainder of nameP */
  117.  
  118.         pushDS_
  119. asm        LDS_    si, nameP
  120. asm    repe    cmpsb
  121.         popDS_
  122. asm        xchg    cx, bx
  123. asm        jne    NextVariable
  124. asm        inc    di
  125.  
  126. /* Now return the pointer to the caller. */
  127.  
  128. VarFound:
  129. #if (LDATA)
  130.     return (char _es *) _DI;
  131. #else
  132.     return (char *) _DI;
  133. #endif
  134. }
  135.