ReactOS 0.4.16-dev-853-g88d9285
getenv.cpp File Reference
#include <corecrt_internal_traits.h>
#include <stdlib.h>
#include <string.h>
Include dependency graph for getenv.cpp:

Go to the source code of this file.

Functions

template<typename Character >
static Character *__cdecl common_getenv_nolock (Character const *const name) throw ()
 
template<typename Character >
static Character *__cdecl common_getenv (Character const *const name) throw ()
 
char *__cdecl getenv (char const *const name)
 
wchar_t *__cdecl _wgetenv (wchar_t const *const name)
 
template<typename Character >
 _Success_ (return==0) static errno_t __cdecl common_getenv_s_nolock(size_t *const required_count
 
 _Out_writes_z_ (buffer_count) Character *const buffer
 
size_t const Character const *const name throw ()
 
errno_t __cdecl getenv_s (size_t *const required_count, char *const buffer, size_t const buffer_count, char const *const name)
 
errno_t __cdecl _wgetenv_s (size_t *const required_count, wchar_t *const buffer, size_t const buffer_count, wchar_t const *const name)
 
template<typename Character >
static errno_t __cdecl common_dupenv_s_nolock (_Outptr_result_buffer_maybenull_(*buffer_count) _Outptr_result_maybenull_z_ Character **const buffer_pointer, _Out_opt_ size_t *const buffer_count, Character const *const name, int const block_use, char const *const file_name, int const line_number) throw ()
 
template<typename Character >
 _Success_ (return !=0) static errno_t __cdecl common_dupenv_s(_Outptr_result_buffer_maybenull_(*buffer_count) _Outptr_result_maybenull_z_ Character **const buffer_pointer
 
errno_t __cdecl _dupenv_s (char **const buffer_pointer, size_t *const buffer_count, char const *const name)
 
errno_t __cdecl _wdupenv_s (wchar_t **const buffer_pointer, size_t *const buffer_count, wchar_t const *const name)
 

Variables

size_t const buffer_count
 
_Out_opt_ size_t *const Character const *const name
 
_Out_opt_ size_t *const Character const *const int const block_use
 
_Out_opt_ size_t *const Character const *const int const char const *const file_name
 

Function Documentation

◆ _dupenv_s()

errno_t __cdecl _dupenv_s ( char **const  buffer_pointer,
size_t *const  buffer_count,
char const *const  name 
)

Definition at line 285 of file getenv.cpp.

290{
291 return common_dupenv_s(buffer_pointer, buffer_count, name, _NORMAL_BLOCK, nullptr, 0);
292}
#define _NORMAL_BLOCK
Definition: crtdbg.h:67
size_t const buffer_count
Definition: getenv.cpp:111
Definition: name.c:39

◆ _Out_writes_z_()

_Out_writes_z_ ( buffer_count  ) const

◆ _Success_() [1/2]

template<typename Character >
_Success_ ( return = 0) const

◆ _Success_() [2/2]

template<typename Character >
_Success_ ( return  = = 0) const

Definition at line 57 of file chdir.cpp.

59{
60 typedef __crt_char_traits<Character> traits;
61
63
64 if (!traits::set_current_directory(path))
65 {
67 return -1;
68 }
69
70 // If the new current directory path is not a UNC path, we must update the
71 // OS environment variable specifying the current full current directory,
72 // build the environment variable string, and call SetEnvironmentVariable().
73 // We need to do this because the SetCurrentDirectory() API does not update
74 // the environment variables, and other functions (fullpath, spawn, etc.)
75 // need them to be set.
76 //
77 // If associated with a 'drive', the current directory should have the
78 // form of the example below:
79 //
80 // D:\nt\private\mytests
81 //
82 // So that the environment variable should be of the form:
83 //
84 // =D:=D:\nt\private\mytests
85
86 Character buffer_initial_storage[MAX_PATH + 1];
87 __crt_internal_win32_buffer<Character> current_directory_buffer(buffer_initial_storage);
88
89 errno_t const err = traits::get_current_directory(current_directory_buffer);
90
91 if (err != 0) {
92 // Appropriate error already set
93 return -1;
94 }
95
96 return set_cwd_environment_variable(current_directory_buffer.data());
97}
#define EINVAL
Definition: acclib.h:90
void __cdecl __acrt_errno_map_os_error(unsigned long)
Definition: errno.cpp:91
#define MAX_PATH
Definition: compat.h:34
#define _VALIDATE_CLEAR_OSSERR_RETURN(expr, errorcode, retexpr)
#define err(...)
int errno_t
Definition: corecrt.h:615
DWORD WINAPI GetLastError(void)
Definition: except.c:1042

◆ _wdupenv_s()

errno_t __cdecl _wdupenv_s ( wchar_t **const  buffer_pointer,
size_t *const  buffer_count,
wchar_t const *const  name 
)

Definition at line 294 of file getenv.cpp.

299{
300 return common_dupenv_s(buffer_pointer, buffer_count, name, _NORMAL_BLOCK, nullptr, 0);
301}

◆ _wgetenv()

wchar_t *__cdecl _wgetenv ( wchar_t const *const  name)

Definition at line 89 of file getenv.cpp.

90{
91 return common_getenv(name);
92}
static Character *__cdecl common_getenv(Character const *const name)
Definition: getenv.cpp:61

◆ _wgetenv_s()

errno_t __cdecl _wgetenv_s ( size_t *const  required_count,
wchar_t *const  buffer,
size_t const  buffer_count,
wchar_t const *const  name 
)

Definition at line 179 of file getenv.cpp.

185{
186 return common_getenv_s(required_count, buffer, buffer_count, name);
187}
GLuint buffer
Definition: glext.h:5915

Referenced by get_tz_environment_variable().

◆ common_dupenv_s_nolock()

template<typename Character >
static errno_t __cdecl common_dupenv_s_nolock ( _Outptr_result_buffer_maybenull_ *buffer_count _Outptr_result_maybenull_z_ Character **const  buffer_pointer,
_Out_opt_ size_t *const  buffer_count,
Character const *const  name,
int const  block_use,
char const *const  file_name,
int const  line_number 
)
throw (
)
static

Definition at line 207 of file getenv.cpp.

215{
216 // These are referenced only in the Debug CRT build
220
221 typedef __crt_char_traits<Character> traits;
222
223 _VALIDATE_RETURN_ERRCODE(buffer_pointer != nullptr, EINVAL);
224 *buffer_pointer = nullptr;
225
226 if (buffer_count != nullptr)
227 *buffer_count = 0;
228
230
231 Character const* const value = common_getenv_nolock(name);
232 if (value == nullptr)
233 return 0;
234
235 size_t const value_count = traits::tcslen(value) + 1;
236
237 *buffer_pointer = static_cast<Character*>(_calloc_dbg(
238 value_count,
239 sizeof(Character),
240 block_use,
241 file_name,
242 line_number));
243 _VALIDATE_RETURN_ERRCODE_NOEXC(*buffer_pointer != nullptr, ENOMEM);
244
245 _ERRCHECK(traits::tcscpy_s(*buffer_pointer, value_count, value));
246 if (buffer_count != nullptr)
247 *buffer_count = value_count;
248
249 return 0;
250}
#define ENOMEM
Definition: acclib.h:84
#define _ERRCHECK(e)
#define _calloc_dbg(c, s, t, f, l)
Definition: crtdbg.h:205
int const char const *const int const line_number
Definition: debug_heap.cpp:499
_Out_opt_ size_t *const Character const *const int const char const *const file_name
Definition: getenv.cpp:259
_Out_opt_ size_t *const Character const *const int const block_use
Definition: getenv.cpp:258
static Character *__cdecl common_getenv_nolock(Character const *const name)
Definition: getenv.cpp:26
#define _VALIDATE_RETURN_ERRCODE(expr, errorcode)
#define _VALIDATE_RETURN_ERRCODE_NOEXC(expr, errorcode)
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:325
Definition: pdh_main.c:96

◆ common_getenv()

template<typename Character >
static Character *__cdecl common_getenv ( Character const *const  name)
throw (
)
static

Definition at line 61 of file getenv.cpp.

62{
63 typedef __crt_char_traits<Character> traits;
64
65 _VALIDATE_RETURN(name != nullptr, EINVAL, nullptr);
66 _VALIDATE_RETURN(traits::tcsnlen(name, _MAX_ENV) < _MAX_ENV, EINVAL, nullptr);
67
68 Character* result = 0;
69
71 __try
72 {
74 }
76 {
78 }
80
81 return result;
82}
void __cdecl __acrt_unlock(_In_ __acrt_lock_id lock)
Definition: locks.cpp:57
@ __acrt_environment_lock
#define _VALIDATE_RETURN(expr, errorcode, retexpr)
__acrt_lock(__acrt_heap_lock)
GLuint64EXT * result
Definition: glext.h:11304
#define _MAX_ENV
Definition: stdlib.h:118
#define __try
Definition: pseh2_64.h:172
#define __endtry
Definition: pseh2_64.h:175
#define __finally
Definition: pseh2_64.h:174

Referenced by _wgetenv(), and getenv().

◆ common_getenv_nolock()

template<typename Character >
static Character *__cdecl common_getenv_nolock ( Character const *const  name)
throw (
)
static

Definition at line 26 of file getenv.cpp.

27{
28 typedef __crt_char_traits<Character> traits;
29
30 Character** const environment = traits::get_or_create_environment_nolock();
31 if (environment == nullptr || name == nullptr)
32 return nullptr;
33
34 size_t const name_length = traits::tcslen(name);
35
36 for (Character** current = environment; *current; ++current)
37 {
38 if (traits::tcslen(*current) <= name_length)
39 continue;
40
41 if (*(*current + name_length) != '=')
42 continue;
43
44 if (traits::tcsnicoll(*current, name, name_length) != 0)
45 continue;
46
47 // Internal consistency check: The environment string should never use
48 // a bigger buffer than _MAX_ENV. See also the SetEnvironmentVariable
49 // SDK function.
50 _ASSERTE(traits::tcsnlen(*current + name_length + 1, _MAX_ENV) < _MAX_ENV);
51
52 return *current + name_length + 1;
53 }
54
55 return nullptr;
56}
#define _ASSERTE(expr)
Definition: crtdbg.h:114
struct task_struct * current
Definition: linux.c:32

Referenced by common_dupenv_s_nolock(), common_getenv(), and throw().

◆ getenv()

char *__cdecl getenv ( char const *const  name)

Definition at line 84 of file getenv.cpp.

85{
86 return common_getenv(name);
87}

◆ getenv_s()

errno_t __cdecl getenv_s ( size_t *const  required_count,
char *const  buffer,
size_t const  buffer_count,
char const *const  name 
)

Definition at line 169 of file getenv.cpp.

175{
176 return common_getenv_s(required_count, buffer, buffer_count, name);
177}

◆ throw()

size_t const Character const *const name throw ( )

Definition at line 113 of file getenv.cpp.

114{
115 typedef __crt_char_traits<Character> traits;
116
117 _VALIDATE_RETURN_ERRCODE(required_count != nullptr, EINVAL);
118 *required_count = 0;
119
121 (buffer != nullptr && buffer_count > 0) ||
122 (buffer == nullptr && buffer_count == 0), EINVAL);
123
124 if (buffer)
125 buffer[0] = '\0';
126
127 Character const* const value = common_getenv_nolock(name);
128 if (!value)
129 return 0;
130
131 *required_count = traits::tcslen(value) + 1;
132 if (buffer_count == 0)
133 return 0;
134
135 // The buffer is too small; we return an error code and the caller can have
136 // the opportunity to try again with a larger buffer:
137 if (*required_count > buffer_count)
138 return ERANGE;
139
140 _ERRCHECK(traits::tcscpy_s(buffer, buffer_count, value));
141 return 0;
142}
#define ERANGE
Definition: acclib.h:92

Variable Documentation

◆ block_use

__endtry int const block_use
Initial value:

Definition at line 258 of file getenv.cpp.

Referenced by _CrtDefaultAllocHook(), _strdup(), _wcsdup(), and common_dupenv_s_nolock().

◆ buffer_count

_Out_opt_ size_t* const buffer_count

Definition at line 111 of file getenv.cpp.

Referenced by _dupenv_s(), _wdupenv_s(), _wgetenv_s(), common_dupenv_s_nolock(), getenv_s(), and throw().

◆ file_name

Definition at line 259 of file getenv.cpp.

Referenced by common_dupenv_s_nolock().

◆ name

Definition at line 257 of file getenv.cpp.