ReactOS  0.4.15-dev-1397-g19779b3
environ.c File Reference
#include <precomp.h>
#include <internal/wine/msvcrt.h>
Include dependency graph for environ.c:

Go to the source code of this file.

Functions

int BlockEnvToEnvironA (void)
 
int BlockEnvToEnvironW (void)
 
char ** DuplicateEnvironment (char **original_environment, int wide)
 
void FreeEnvironment (char **environment)
 
int SetEnv (const wchar_t *option)
 
int__p__commode (void)
 
void __set_app_type (int app_type)
 
char ** __p__acmdln (void)
 
wchar_t ** __p__wcmdln (void)
 
char *** __p__environ (void)
 
wchar_t *** __p__wenviron (void)
 
char *** __p___initenv (void)
 
wchar_t *** __p___winitenv (void)
 
errno_t _get_osplatform (unsigned int *pValue)
 
int__p___mb_cur_max (void)
 
int CDECL ___mb_cur_max_func (void)
 
unsigned int__p__osver (void)
 
char ** __p__pgmptr (void)
 
int _get_pgmptr (char **p)
 
wchar_t ** __p__wpgmptr (void)
 
int _get_wpgmptr (WCHAR **p)
 
unsigned int__p__winmajor (void)
 
unsigned int__p__winminor (void)
 
unsigned int__p__winver (void)
 

Variables

unsigned int _osplatform = 0
 
unsigned int _osver = 0
 
unsigned int _winminor = 0
 
unsigned int _winmajor = 0
 
unsigned int _winver = 0
 
unsigned int __setlc_active = 0
 
unsigned int __unguarded_readlc_active = 0
 
char_acmdln = NULL
 
wchar_t_wcmdln = NULL
 
char ** _environ = NULL
 
wchar_t ** _wenviron = NULL
 
char ** __initenv = NULL
 
wchar_t ** __winitenv = NULL
 
char_pgmptr = NULL
 
wchar_t_wpgmptr = NULL
 
int __app_type = _UNKNOWN_APP
 
int _commode = _IOCOMMIT
 

Function Documentation

◆ ___mb_cur_max_func()

int CDECL ___mb_cur_max_func ( void  )

Definition at line 438 of file environ.c.

439 {
440  return get_locinfo()->mb_cur_max;
441 }
#define get_locinfo()
Definition: winesup.h:25

◆ __p___initenv()

char*** __p___initenv ( void  )

Definition at line 400 of file environ.c.

401 {
402  return &__initenv;
403 }
char ** __initenv
Definition: environ.c:24

◆ __p___mb_cur_max()

int* __p___mb_cur_max ( void  )

Definition at line 430 of file environ.c.

431 {
432  return &get_locinfo()->mb_cur_max;
433 }
#define get_locinfo()
Definition: winesup.h:25

◆ __p___winitenv()

wchar_t*** __p___winitenv ( void  )

Definition at line 408 of file environ.c.

409 {
410  return &__winitenv;
411 }
wchar_t ** __winitenv
Definition: environ.c:25

◆ __p__acmdln()

char** __p__acmdln ( void  )

Definition at line 368 of file environ.c.

369 {
370  return &_acmdln;
371 }
char * _acmdln
Definition: environ.c:18

Referenced by ok_argsA_imp().

◆ __p__commode()

int* __p__commode ( void  )

Definition at line 352 of file environ.c.

353 {
354  return &_commode;
355 }
int _commode
Definition: environ.c:31

◆ __p__environ()

char*** __p__environ ( void  )

Definition at line 384 of file environ.c.

385 {
386  return &_environ;
387 }
char ** _environ
Definition: environ.c:22

Referenced by getenv().

◆ __p__osver()

unsigned int* __p__osver ( void  )

Definition at line 446 of file environ.c.

447 {
448  return &_osver;
449 }
unsigned int _osver
Definition: environ.c:11

◆ __p__pgmptr()

char** __p__pgmptr ( void  )

Definition at line 454 of file environ.c.

455 {
456  return &_pgmptr;
457 }
char * _pgmptr
Definition: environ.c:27

◆ __p__wcmdln()

wchar_t** __p__wcmdln ( void  )

Definition at line 376 of file environ.c.

377 {
378  return &_wcmdln;
379 }
wchar_t * _wcmdln
Definition: environ.c:19

Referenced by ok_argsW_imp().

◆ __p__wenviron()

wchar_t*** __p__wenviron ( void  )

Definition at line 392 of file environ.c.

393 {
394  return &_wenviron;
395 }
wchar_t ** _wenviron
Definition: environ.c:23

Referenced by _wgetenv().

◆ __p__winmajor()

unsigned int* __p__winmajor ( void  )

Definition at line 500 of file environ.c.

501 {
502  return &_winmajor;
503 }
unsigned int _winmajor
Definition: environ.c:13

◆ __p__winminor()

unsigned int* __p__winminor ( void  )

Definition at line 508 of file environ.c.

509 {
510  return &_winminor;
511 }
unsigned int _winminor
Definition: environ.c:12

◆ __p__winver()

unsigned int* __p__winver ( void  )

Definition at line 516 of file environ.c.

517 {
518  return &_winver;
519 }
unsigned int _winver
Definition: environ.c:14

◆ __p__wpgmptr()

wchar_t** __p__wpgmptr ( void  )

Definition at line 477 of file environ.c.

478 {
479  return &_wpgmptr;
480 }
wchar_t * _wpgmptr
Definition: environ.c:29

◆ __set_app_type()

void __set_app_type ( int  app_type)

Definition at line 360 of file environ.c.

361 {
362  __app_type = app_type;
363 }
int __app_type
Definition: environ.c:30

◆ _get_osplatform()

errno_t _get_osplatform ( unsigned int pValue)

Definition at line 416 of file environ.c.

417 {
418  if (!MSVCRT_CHECK_PMT(pValue != NULL)) {
419  *_errno() = EINVAL;
420  return EINVAL;
421  }
422 
423  *pValue = _osplatform;
424  return 0;
425 }
unsigned int _osplatform
Definition: environ.c:10
Definition: arc.h:39
PWCHAR pValue
_CRTIMP int *__cdecl _errno(void)
Definition: errno.c:19
#define MSVCRT_CHECK_PMT(x)
Definition: mbstowcs_s.c:26
#define NULL
Definition: types.h:112

◆ _get_pgmptr()

int _get_pgmptr ( char **  p)

Definition at line 462 of file environ.c.

463 {
464  if (!MSVCRT_CHECK_PMT(p))
465  {
466  *_errno() = EINVAL;
467  return EINVAL;
468  }
469 
470  *p = _pgmptr;
471  return 0;
472 }
Definition: arc.h:39
char * _pgmptr
Definition: environ.c:27
_CRTIMP int *__cdecl _errno(void)
Definition: errno.c:19
#define MSVCRT_CHECK_PMT(x)
Definition: mbstowcs_s.c:26
GLfloat GLfloat p
Definition: glext.h:8902

◆ _get_wpgmptr()

int _get_wpgmptr ( WCHAR **  p)

Definition at line 485 of file environ.c.

486 {
487  if (!MSVCRT_CHECK_PMT(p))
488  {
489  *_errno() = EINVAL;
490  return EINVAL;
491  }
492 
493  *p = _wpgmptr;
494  return 0;
495 }
Definition: arc.h:39
wchar_t * _wpgmptr
Definition: environ.c:29
_CRTIMP int *__cdecl _errno(void)
Definition: errno.c:19
#define MSVCRT_CHECK_PMT(x)
Definition: mbstowcs_s.c:26
GLfloat GLfloat p
Definition: glext.h:8902

◆ BlockEnvToEnvironA()

int BlockEnvToEnvironA ( void  )

Definition at line 34 of file environ.c.

35 {
36  char *ptr, *environment_strings;
37  char **envptr;
38  int count = 1;
39  size_t len;
40 
41  TRACE("BlockEnvToEnvironA()\n");
42 
43  environment_strings = GetEnvironmentStringsA();
44  if (environment_strings == NULL) {
45  return -1;
46  }
47 
48  for (ptr = environment_strings; *ptr; ptr += len)
49  {
50  len = strlen(ptr) + 1;
51  /* Skip drive letter settings. */
52  if (*ptr != '=')
53  count++;
54  }
55 
56  __initenv = _environ = malloc(count * sizeof(char*));
57  if (_environ)
58  {
59  for (ptr = environment_strings, envptr = _environ; count > 1; ptr += len)
60  {
61  len = strlen(ptr) + 1;
62  /* Skip drive letter settings. */
63  if (*ptr != '=')
64  {
65  if ((*envptr = malloc(len)) == NULL)
66  {
67  for (envptr--; envptr >= _environ; envptr--)
68  free(*envptr);
69  FreeEnvironmentStringsA(environment_strings);
70  free(_environ);
72  return -1;
73  }
74  memcpy(*envptr++, ptr, len);
75  count--;
76  }
77  }
78  /* Add terminating NULL entry. */
79  *envptr = NULL;
80  }
81 
82  FreeEnvironmentStringsA(environment_strings);
83  return _environ ? 0 : -1;
84 }
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
GLuint GLuint GLsizei count
Definition: gl.h:1545
#define free
Definition: debug_ros.c:5
BOOL WINAPI FreeEnvironmentStringsA(IN LPSTR EnvironmentStrings)
Definition: environ.c:379
char ** _environ
Definition: environ.c:22
char ** __initenv
Definition: environ.c:24
static PVOID ptr
Definition: dispmode.c:27
#define TRACE(s)
Definition: solgame.cpp:4
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
GLenum GLsizei len
Definition: glext.h:6722
LPSTR WINAPI GetEnvironmentStringsA(VOID)
Definition: environ.c:293
#define NULL
Definition: types.h:112
#define malloc
Definition: debug_ros.c:4

Referenced by DllMain().

◆ BlockEnvToEnvironW()

int BlockEnvToEnvironW ( void  )

Definition at line 86 of file environ.c.

87 {
88  wchar_t *ptr, *environment_strings;
89  wchar_t **envptr;
90  int count = 1;
91  size_t len;
92 
93  TRACE("BlockEnvToEnvironW()\n");
94 
95  environment_strings = GetEnvironmentStringsW();
96  if (environment_strings == NULL) {
97  return -1;
98  }
99 
100  for (ptr = environment_strings; *ptr; ptr += len)
101  {
102  len = wcslen(ptr) + 1;
103  /* Skip drive letter settings. */
104  if (*ptr != '=')
105  count++;
106  }
107 
108  __winitenv = _wenviron = malloc(count * sizeof(wchar_t*));
109  if (_wenviron)
110  {
111  for (ptr = environment_strings, envptr = _wenviron; count > 1; ptr += len)
112  {
113  len = wcslen(ptr) + 1;
114  /* Skip drive letter settings. */
115  if (*ptr != '=')
116  {
117  if ((*envptr = malloc(len * sizeof(wchar_t))) == NULL)
118  {
119  for (envptr--; envptr >= _wenviron; envptr--)
120  free(*envptr);
121  FreeEnvironmentStringsW(environment_strings);
122  free(_wenviron);
124  return -1;
125  }
126  memcpy(*envptr++, ptr, len * sizeof(wchar_t));
127  count--;
128  }
129  }
130  /* Add terminating NULL entry. */
131  *envptr = NULL;
132  }
133 
134  FreeEnvironmentStringsW(environment_strings);
135  return _wenviron ? 0 : -1;
136 }
GLuint GLuint GLsizei count
Definition: gl.h:1545
#define free
Definition: debug_ros.c:5
static PVOID ptr
Definition: dispmode.c:27
wchar_t ** _wenviron
Definition: environ.c:23
#define TRACE(s)
Definition: solgame.cpp:4
BOOL WINAPI FreeEnvironmentStringsW(IN LPWSTR EnvironmentStrings)
Definition: environ.c:389
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
GLenum GLsizei len
Definition: glext.h:6722
#define NULL
Definition: types.h:112
LPWSTR WINAPI GetEnvironmentStringsW(VOID)
Definition: environ.c:344
wchar_t ** __winitenv
Definition: environ.c:25
#define malloc
Definition: debug_ros.c:4
size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)

Referenced by DllMain().

◆ DuplicateEnvironment()

char** DuplicateEnvironment ( char **  original_environment,
int  wide 
)

Internal function to duplicate environment block. Although it's parameter are defined as char**, it's able to work also with wide character environment block which are of type wchar_t**.

Parameters
original_environmentEnvironment to duplicate.
wideSet to zero for multibyte environments, non-zero otherwise.
Returns
Original environment in case of failure, otherwise pointer to new environment block.

Definition at line 151 of file environ.c.

152 {
153  int count = 1;
154  char **envptr, **newenvptr, **newenv;
155 
156  for (envptr = original_environment; *envptr != NULL; envptr++, count++)
157  ;
158 
159  newenvptr = newenv = malloc(count * sizeof(char*));
160  if (newenv == NULL)
161  return original_environment;
162 
163  for (envptr = original_environment; count > 1; newenvptr++, count--)
164  {
165  if (wide)
166  *newenvptr = (char*)_wcsdup((wchar_t*)*envptr++);
167  else
168  *newenvptr = _strdup(*envptr++);
169  if (*newenvptr == NULL)
170  {
171  for (newenvptr--; newenvptr >= newenv; newenvptr--)
172  free(*newenvptr);
173  free(newenv);
174  return original_environment;
175  }
176  }
177  *newenvptr = NULL;
178 
179  return newenv;
180 }
GLuint GLuint GLsizei count
Definition: gl.h:1545
#define free
Definition: debug_ros.c:5
#define _strdup
Definition: debug_ros.c:7
#define NULL
Definition: types.h:112
#define malloc
Definition: debug_ros.c:4
_Check_return_ _CRTIMP wchar_t *__cdecl _wcsdup(_In_z_ const wchar_t *_Str)

Referenced by SetEnv().

◆ FreeEnvironment()

void FreeEnvironment ( char **  environment)

Internal function to deallocate environment block. Although it's parameter are defined as char**, it's able to work also with wide character environment block which are of type wchar_t**.

Parameters
environmentEnvironment to free.

Definition at line 190 of file environ.c.

191 {
192  char **envptr;
193  for (envptr = environment; *envptr != NULL; envptr++)
194  free(*envptr);
195  free(environment);
196 }
#define free
Definition: debug_ros.c:5
#define NULL
Definition: types.h:112

Referenced by DllMain().

◆ SetEnv()

int SetEnv ( const wchar_t option)

Internal version of _wputenv and _putenv. It works duplicates the original envirnments created during initilization if needed to prevent having spurious pointers floating around. Then it updates the internal environment tables (_environ and _wenviron) and at last updates the OS environemnt.

Note that there can happen situation when the internal [_w]environ arrays will be updated, but the OS environment update will fail. In this case we don't undo the changes to the [_w]environ tables to comply with the Microsoft behaviour (and it's also much easier :-).

Definition at line 210 of file environ.c.

211 {
212  wchar_t *epos, *name;
213  wchar_t **wenvptr;
214  wchar_t *woption;
215  char *mboption;
216  int remove, index, count, size, result = 0, found = 0;
217  wchar_t **wnewenv;
218  char **mbnewenv;
219 
220  if (option == NULL || (epos = wcschr(option, L'=')) == NULL)
221  return -1;
222  remove = (epos[1] == 0);
223 
224  /* Duplicate environment if needed. */
225  if (_environ == __initenv)
226  {
228  return -1;
229  }
230  if (_wenviron == __winitenv)
231  {
232  if ((_wenviron = (wchar_t**)DuplicateEnvironment((char**)_wenviron, 1)) ==
233  __winitenv)
234  return -1;
235  }
236 
237  /* Create a copy of the option name. */
238  name = malloc((epos - option + 1) * sizeof(wchar_t));
239  if (name == NULL)
240  return -1;
241  memcpy(name, option, (epos - option) * sizeof(wchar_t));
242  name[epos - option] = 0;
243 
244  /* Find the option we're trying to modify. */
245  for (index = 0, wenvptr = _wenviron; *wenvptr != NULL; wenvptr++, index++)
246  {
247  if (!_wcsnicmp(*wenvptr, option, epos - option))
248  {
249  found = 1;
250  break;
251  }
252  }
253 
254  if (remove)
255  {
256  if (!found)
257  {
258  free(name);
259  return 0;
260  }
261 
262  /* Remove the option from wide character environment. */
263  free(*wenvptr);
264  for (count = index; *wenvptr != NULL; wenvptr++, count++)
265  *wenvptr = *(wenvptr + 1);
266  wnewenv = realloc(_wenviron, count * sizeof(wchar_t*));
267  if (wnewenv != NULL)
268  _wenviron = wnewenv;
269 
270  /* Remove the option from multibyte environment. We assume
271  * the environments are in sync and the option is at the
272  * same position. */
273  free(_environ[index]);
274  memmove(&_environ[index], &_environ[index+1], (count - index) * sizeof(char*));
275  mbnewenv = realloc(_environ, count * sizeof(char*));
276  if (mbnewenv != NULL)
277  _environ = mbnewenv;
278 
280  }
281  else
282  {
283  /* Make a copy of the option that we will store in the environment block. */
284  woption = _wcsdup((wchar_t*)option);
285  if (woption == NULL)
286  {
287  free(name);
288  return -1;
289  }
290 
291  /* Create a multibyte copy of the option. */
293  mboption = malloc(size);
294  if (mboption == NULL)
295  {
296  free(name);
297  free(woption);
298  return -1;
299  }
300  WideCharToMultiByte(CP_ACP, 0, option, -1, mboption, size, NULL, NULL);
301 
302  if (found)
303  {
304  /* Replace the current entry. */
305  free(*wenvptr);
306  *wenvptr = woption;
307  free(_environ[index]);
308  _environ[index] = mboption;
309  }
310  else
311  {
312  /* Get the size of the original environment. */
313  for (count = index; *wenvptr != NULL; wenvptr++, count++)
314  ;
315 
316  /* Create a new entry. */
317  if ((wnewenv = realloc(_wenviron, (count + 2) * sizeof(wchar_t*))) == NULL)
318  {
319  free(name);
320  free(mboption);
321  free(woption);
322  return -1;
323  }
324  _wenviron = wnewenv;
325  if ((mbnewenv = realloc(_environ, (count + 2) * sizeof(char*))) == NULL)
326  {
327  free(name);
328  free(mboption);
329  free(woption);
330  return -1;
331  }
332  _environ = mbnewenv;
333 
334  /* Set the last entry to our option. */
335  _wenviron[count] = woption;
336  _environ[count] = mboption;
337  _wenviron[count + 1] = NULL;
338  _environ[count + 1] = NULL;
339  }
340 
341  /* And finally update the OS environment. */
342  result = SetEnvironmentVariableW(name, epos + 1) ? 0 : -1;
343  }
344  free(name);
345 
346  return result;
347 }
#define realloc
Definition: debug_ros.c:6
#define memmove(s1, s2, n)
Definition: mkisofs.h:881
#define WideCharToMultiByte
Definition: compat.h:111
GLuint64EXT * result
Definition: glext.h:11304
#define CP_ACP
Definition: compat.h:109
GLuint GLuint GLsizei count
Definition: gl.h:1545
#define free
Definition: debug_ros.c:5
_Check_return_ _CRTIMP int __cdecl _wcsnicmp(_In_reads_or_z_(_MaxCount) const wchar_t *_Str1, _In_reads_or_z_(_MaxCount) const wchar_t *_Str2, _In_ size_t _MaxCount)
char ** DuplicateEnvironment(char **original_environment, int wide)
Definition: environ.c:151
char ** _environ
Definition: environ.c:22
char ** __initenv
Definition: environ.c:24
Definition: getopt.h:108
GLuint index
Definition: glext.h:6031
GLuint GLenum option
Definition: glext.h:11211
wchar_t ** _wenviron
Definition: environ.c:23
GLsizeiptr size
Definition: glext.h:5919
_CONST_RETURN wchar_t *__cdecl wcschr(_In_z_ const wchar_t *_Str, wchar_t _Ch)
BOOL WINAPI DECLSPEC_HOTPATCH SetEnvironmentVariableW(IN LPCWSTR lpName, IN LPCWSTR lpValue)
Definition: environ.c:259
#define index(s, c)
Definition: various.h:29
static const WCHAR L[]
Definition: oid.c:1250
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
#define NULL
Definition: types.h:112
wchar_t ** __winitenv
Definition: environ.c:25
Definition: name.c:38
#define malloc
Definition: debug_ros.c:4
_Check_return_ _CRTIMP wchar_t *__cdecl _wcsdup(_In_z_ const wchar_t *_Str)
int remove
Definition: msacm.c:1365
GLuint const GLchar * name
Definition: glext.h:6031

Referenced by _putenv(), and _wputenv().

Variable Documentation

◆ __app_type

int __app_type = _UNKNOWN_APP

Definition at line 30 of file environ.c.

Referenced by __set_app_type(), _assert(), and abort().

◆ __initenv

char** __initenv = NULL

Definition at line 24 of file environ.c.

Referenced by __p___initenv(), BlockEnvToEnvironA(), DllMain(), and SetEnv().

◆ __setlc_active

unsigned int __setlc_active = 0

Definition at line 16 of file environ.c.

Referenced by ___setlc_active_func().

◆ __unguarded_readlc_active

unsigned int __unguarded_readlc_active = 0

Definition at line 17 of file environ.c.

Referenced by ___unguarded_readlc_active_add_func().

◆ __winitenv

wchar_t** __winitenv = NULL

Definition at line 25 of file environ.c.

Referenced by __p___winitenv(), __wgetmainargs(), BlockEnvToEnvironW(), DllMain(), and SetEnv().

◆ _acmdln

char* _acmdln = NULL

Definition at line 18 of file environ.c.

Referenced by __getmainargs(), __p__acmdln(), and DllMain().

◆ _commode

int _commode = _IOCOMMIT

Definition at line 31 of file environ.c.

Referenced by __p__commode(), and msvcrt_get_flags().

◆ _environ

char** _environ = NULL

Definition at line 22 of file environ.c.

Referenced by __getmainargs(), __p__environ(), BlockEnvToEnvironA(), DllMain(), and SetEnv().

◆ _osplatform

unsigned int _osplatform = 0

Definition at line 10 of file environ.c.

Referenced by _get_osplatform(), and DllMain().

◆ _osver

unsigned int _osver = 0

Definition at line 11 of file environ.c.

Referenced by __p__osver(), and DllMain().

◆ _pgmptr

char* _pgmptr = NULL

Definition at line 27 of file environ.c.

Referenced by __getmainargs(), __p__pgmptr(), and _get_pgmptr().

◆ _wcmdln

wchar_t* _wcmdln = NULL

Definition at line 19 of file environ.c.

Referenced by __p__wcmdln(), __wgetmainargs(), and DllMain().

◆ _wenviron

wchar_t** _wenviron = NULL

Definition at line 23 of file environ.c.

Referenced by __p__wenviron(), BlockEnvToEnvironW(), DllMain(), and SetEnv().

◆ _winmajor

unsigned int _winmajor = 0

Definition at line 13 of file environ.c.

Referenced by __p__winmajor(), and DllMain().

◆ _winminor

unsigned int _winminor = 0

Definition at line 12 of file environ.c.

Referenced by __p__winminor(), and DllMain().

◆ _winver

unsigned int _winver = 0

Definition at line 14 of file environ.c.

Referenced by __p__winver(), and DllMain().

◆ _wpgmptr

wchar_t* _wpgmptr = NULL

Definition at line 29 of file environ.c.

Referenced by __p__wpgmptr(), __wgetmainargs(), and _get_wpgmptr().