Windows NT 4.0 source code leak
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.

167 lines
3.6 KiB

4 years ago
  1. /***
  2. *getenv.c - get the value of an environment variable
  3. *
  4. * Copyright (c) 1985-1992, 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. *
  32. *******************************************************************************/
  33. #include <cruntime.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <os2dll.h>
  37. #include <oscalls.h>
  38. /***
  39. *char *getenv(option) - search environment for a string
  40. *
  41. *Purpose:
  42. * searches the environment for a string of the form "option=value",
  43. * if found, return value, otherwise NULL.
  44. *
  45. *Entry:
  46. * const char *option - variable to search for in environment
  47. *
  48. *Exit:
  49. * returns the value part of the environment string if found,
  50. * otherwise NULL
  51. *
  52. *Exceptions:
  53. *
  54. *******************************************************************************/
  55. #ifdef _CRUISER_
  56. char * _CALLTYPE1 getenv (
  57. REG3 const char *option
  58. )
  59. {
  60. #ifdef _POSIX_
  61. REG1 char **search = environ;
  62. #else
  63. REG1 char **search = _environ;
  64. #endif
  65. REG2 unsigned int length;
  66. _mlock( _ENV_LOCK );
  67. if (search && option)
  68. {
  69. length = strlen(option);
  70. /*
  71. ** Make sure `*search' is long enough to be a candidate
  72. ** (We must NOT index past the '\0' at the end of `*search'!)
  73. ** and that it has an equal sign (`=') in the correct spot.
  74. ** If both of these requirements are met, compare the strings.
  75. */
  76. while (*search)
  77. {
  78. if (strlen(*search) > length && (*(*search + length)
  79. == '=') && (strncmp(*search, option, length) == 0)) {
  80. _munlock( _ENV_LOCK );
  81. return(*search + length + 1);
  82. }
  83. search++;
  84. }
  85. }
  86. _munlock( _ENV_LOCK );
  87. return(NULL);
  88. }
  89. #else /* ndef _CRUISER_ */
  90. #ifdef _WIN32_
  91. #ifdef MTHREAD
  92. char * _CALLTYPE1 getenv (
  93. const char *option
  94. )
  95. {
  96. char *retval;
  97. _mlock(_ENV_LOCK);
  98. retval = _getenv_lk(option);
  99. _munlock(_ENV_LOCK);
  100. return(retval);
  101. }
  102. char * _CALLTYPE1 _getenv_lk (
  103. const char *option
  104. )
  105. #else /* ndef MTHREAD */
  106. char * _CALLTYPE1 getenv (
  107. const char *option
  108. )
  109. #endif /* MTHREAD */
  110. {
  111. char **search = _environ;
  112. unsigned int length;
  113. if (search && option)
  114. {
  115. length = strlen(option);
  116. /*
  117. ** Make sure `*search' is long enough to be a candidate
  118. ** (We must NOT index past the '\0' at the end of `*search'!)
  119. ** and that it has an equal sign (`=') in the correct spot.
  120. ** If both of these requirements are met, compare the strings.
  121. */
  122. while (*search)
  123. {
  124. if (strlen(*search) > length && (*(*search + length)
  125. == '=') && (strnicmp(*search, option, length) == 0)) {
  126. return(*search + length + 1);
  127. }
  128. search++;
  129. }
  130. }
  131. return(NULL);
  132. }
  133. #endif /* _WIN32_ */
  134. #endif /* _CRUISER_ */