Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

213 lines
6.3 KiB

  1. /***
  2. *getenv.c - get the value of an environment variable
  3. *
  4. * Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * defines getenv() - searches the environment for a string variable
  8. * and returns the value of it.
  9. *
  10. *Revision History:
  11. * 11-22-83 RN initial version
  12. * 04-13-87 JCR added const to declaration
  13. * 11-09-87 SKS avoid indexing past end of strings (add strlen check)
  14. * 12-11-87 JCR Added "_LOAD_DS" to declaration
  15. * 06-01-88 PHG Merged normal/DLL versions
  16. * 03-14-90 GJF Replaced _LOAD_DS with _CALLTYPE1, added #include
  17. * <cruntime.h>, removed #include <register.h> and fixed
  18. * the copyright. Also, cleaned up the formatting a bit.
  19. * 04-05-90 GJF Added #include <string.h>.
  20. * 07-25-90 SBM Removed redundant include (stdio.h)
  21. * 08-13-90 SBM Compiles cleanly with -W3 (made length unsigned int)
  22. * 10-04-90 GJF New-style function declarator.
  23. * 01-18-91 GJF ANSI naming.
  24. * 02-06-91 SRW Added _WIN32_ conditional for GetEnvironmentVariable
  25. * 02-18-91 SRW Removed _WIN32_ conditional for GetEnvironmentVariable
  26. * 01-10-92 GJF Final unlock and return statements shouldn't be in
  27. * if-block.
  28. * 03-11-92 GJF Use case-insensitive comparison for Win32.
  29. * 04-27-92 GJF Repackaged MTHREAD support for Win32 to create a
  30. * _getenv_lk.
  31. * 06-05-92 PLM Added _MAC_
  32. * 06-10-92 PLM Added _envinit for _MAC_
  33. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  34. * Remove OS/2, POSIX support
  35. * 04-08-93 SKS Replace strnicmp() with ANSI-conforming _strnicmp()
  36. * 09-14-93 GJF Small change for Posix compatibility.
  37. * 11-29-93 CFW Wide char enable.
  38. * 12-07-93 CFW Change _TCHAR to _TSCHAR.
  39. * 01-15-94 CFW Use _tcsnicoll for global match.
  40. * 02-04-94 CFW POSIXify.
  41. * 03-31-94 CFW Should be ifndef POSIX.
  42. * 02-14-95 CFW Debug CRT allocs.
  43. * 02-16-95 JWM Spliced _WIN32 & Mac versions.
  44. * 08-01-96 RDK For Pmac, change data type of initialization pointer.
  45. * 07-09-97 GJF Added a check that the environment initialization has
  46. * been executed. Also, detab-ed and got rid of obsolete
  47. * _CALLTYPE1 macros.
  48. * 03-05-98 GJF Exception-safe locking.
  49. * 12-18-98 GJF Changes for 64-bit size_t.
  50. * 04-28-99 PML Wrap __declspec(allocate()) in _CRTALLOC macro.
  51. * 05-17-99 PML Remove all Macintosh support.
  52. *
  53. *******************************************************************************/
  54. #include <sect_attribs.h>
  55. #include <cruntime.h>
  56. #include <internal.h>
  57. #include <mtdll.h>
  58. #include <stdlib.h>
  59. #include <string.h>
  60. #include <tchar.h>
  61. #ifndef CRTDLL
  62. /*
  63. * Flag checked by getenv() and _putenv() to determine if the environment has
  64. * been initialized.
  65. */
  66. extern int __env_initialized;
  67. #endif
  68. /***
  69. *char *getenv(option) - search environment for a string
  70. *
  71. *Purpose:
  72. * searches the environment for a string of the form "option=value",
  73. * if found, return value, otherwise NULL.
  74. *
  75. *Entry:
  76. * const char *option - variable to search for in environment
  77. *
  78. *Exit:
  79. * returns the value part of the environment string if found,
  80. * otherwise NULL
  81. *
  82. *Exceptions:
  83. *
  84. *******************************************************************************/
  85. #ifdef _MT
  86. #ifdef WPRFLAG
  87. wchar_t * __cdecl _wgetenv (
  88. #else
  89. char * __cdecl getenv (
  90. #endif
  91. const _TSCHAR *option
  92. )
  93. {
  94. _TSCHAR *retval;
  95. _mlock( _ENV_LOCK );
  96. __try {
  97. #ifdef WPRFLAG
  98. retval = _wgetenv_lk(option);
  99. #else
  100. retval = _getenv_lk(option);
  101. #endif
  102. }
  103. __finally {
  104. _munlock( _ENV_LOCK );
  105. }
  106. return(retval);
  107. }
  108. #ifdef WPRFLAG
  109. wchar_t * __cdecl _wgetenv_lk (
  110. #else
  111. char * __cdecl _getenv_lk (
  112. #endif
  113. const _TSCHAR *option
  114. )
  115. #else /* ndef _MT */
  116. #ifdef WPRFLAG
  117. wchar_t * __cdecl _wgetenv (
  118. #else
  119. char * __cdecl getenv (
  120. #endif
  121. const _TSCHAR *option
  122. )
  123. #endif /* _MT */
  124. {
  125. #ifdef _POSIX_
  126. char **search = environ;
  127. #else
  128. _TSCHAR **search = _tenviron;
  129. #endif
  130. size_t length;
  131. #ifndef CRTDLL
  132. /*
  133. * Make sure the environment is initialized.
  134. */
  135. if ( !__env_initialized )
  136. return NULL;
  137. #endif /* CRTDLL */
  138. /*
  139. * At startup, we obtain the 'native' flavor of environment strings
  140. * from the OS. So a "main" program has _environ and a "wmain" has
  141. * _wenviron loaded at startup. Only when the user gets or puts the
  142. * 'other' flavor do we convert it.
  143. */
  144. #ifndef _POSIX_
  145. #ifdef WPRFLAG
  146. if (!search && _environ)
  147. {
  148. /* don't have requested type, but other exists, so convert it */
  149. if (__mbtow_environ() != 0)
  150. return NULL;
  151. /* now requested type exists */
  152. search = _wenviron;
  153. }
  154. #else
  155. if (!search && _wenviron)
  156. {
  157. /* don't have requested type, but other exists, so convert it */
  158. if (__wtomb_environ() != 0)
  159. return NULL;
  160. /* now requested type exists */
  161. search = _environ;
  162. }
  163. #endif
  164. #endif /* _POSIX_ */
  165. if (search && option)
  166. {
  167. length = _tcslen(option);
  168. /*
  169. ** Make sure `*search' is long enough to be a candidate
  170. ** (We must NOT index past the '\0' at the end of `*search'!)
  171. ** and that it has an equal sign (`=') in the correct spot.
  172. ** If both of these requirements are met, compare the strings.
  173. */
  174. while (*search)
  175. {
  176. if (_tcslen(*search) > length && (*(*search + length)
  177. == _T('=')) && (_tcsnicoll(*search, option, length) == 0)) {
  178. return(*search + length + 1);
  179. }
  180. search++;
  181. }
  182. }
  183. return(NULL);
  184. }