2 * Process environment management
4 * Copyright 1996, 1998 Alexandre Julliard
16 #include "wine/winbase16.h"
19 #include "selectors.h"
21 /* Win32 process environment database */
24 LPSTR environ; /* 00 Process environment strings */
25 DWORD unknown1; /* 04 Unknown */
26 LPSTR cmd_line; /* 08 Command line */
27 LPSTR cur_dir; /* 0c Current directory */
28 STARTUPINFOA *startup_info; /* 10 Startup information */
29 HANDLE hStdin; /* 14 Handle for standard input */
30 HANDLE hStdout; /* 18 Handle for standard output */
31 HANDLE hStderr; /* 1c Handle for standard error */
32 DWORD unknown2; /* 20 Unknown */
33 DWORD inherit_console; /* 24 Inherit console flag */
34 DWORD break_type; /* 28 Console events flag */
35 void *break_sem; /* 2c SetConsoleCtrlHandler semaphore */
36 void *break_event; /* 30 SetConsoleCtrlHandler event */
37 void *break_thread; /* 34 SetConsoleCtrlHandler thread */
38 void *break_handlers; /* 38 List of console handlers */
42 /* Format of an environment block:
43 * ASCIIZ string 1 (xx=yy format)
48 * ASCIIZ program name (e.g. C:\WINDOWS\SYSTEM\KRNL386.EXE)
51 * - contrary to Microsoft docs, the environment strings do not appear
52 * to be sorted on Win95 (although they are on NT); so we don't bother
53 * to sort them either.
56 static const char ENV_program_name[] = "C:\\WINDOWS\\SYSTEM\\KRNL386.EXE";
58 /* Maximum length of a Win16 environment string (including NULL) */
59 #define MAX_WIN16_LEN 128
61 /* Extra bytes to reserve at the end of an environment */
62 #define EXTRA_ENV_SIZE (sizeof(BYTE) + sizeof(WORD) + sizeof(ENV_program_name))
64 /* Fill the extra bytes with the program name and stuff */
65 #define FILL_EXTRA_ENV(p) \
67 PUT_UA_WORD( (p) + 1, 1 ); \
68 strcpy( (p) + 3, ENV_program_name );
70 STARTUPINFOA current_startupinfo =
72 sizeof(STARTUPINFOA), /* cb */
80 0, /* dwXCountChars */
81 0, /* dwYCountChars */
82 0, /* dwFillAttribute */
98 ¤t_startupinfo, /* startup_info */
103 0, /* inherit_console */
107 0, /* break_thread */
108 0 /* break_handlers */
112 static WCHAR *cmdlineW; /* Unicode command line */
113 static WORD env_sel; /* selector to the environment */
115 /***********************************************************************
118 * Find a variable in the environment and return a pointer to the value.
119 * Helper function for GetEnvironmentVariable and ExpandEnvironmentStrings.
121 static LPCSTR ENV_FindVariable( LPCSTR env, LPCSTR name, INT len )
125 if (!strncasecmp( name, env, len ) && (env[len] == '='))
126 return env + len + 1;
127 env += strlen(env) + 1;
133 /***********************************************************************
134 * ENV_BuildEnvironment
136 * Build the environment for the initial process
138 ENVDB *ENV_BuildEnvironment(void)
140 extern char **environ;
144 /* Compute the total size of the Unix environment */
146 size = EXTRA_ENV_SIZE;
147 for (e = environ; *e; e++) size += strlen(*e) + 1;
149 /* Now allocate the environment */
151 if (!(p = HeapAlloc( GetProcessHeap(), 0, size ))) return NULL;
152 current_envdb.environ = p;
153 env_sel = SELECTOR_AllocBlock( p, 0x10000, WINE_LDT_FLAGS_DATA );
155 /* And fill it with the Unix environment */
157 for (e = environ; *e; e++)
163 /* Now add the program name */
166 return ¤t_envdb;
170 /***********************************************************************
171 * ENV_BuildCommandLine
173 * Build the command line of a process from the argv array.
175 * Note that it does NOT necessarily include the file name.
176 * Sometimes we don't even have any command line options at all.
178 * We must quote and escape characters so that the argv array can be rebuilt
179 * from the command line:
180 * - spaces and tabs must be quoted
182 * - quotes must be escaped
184 * - if '\'s are followed by a '"', they must be doubled and followed by '\"',
185 * resulting in an odd number of '\' followed by a '"'
188 * - '\'s that are not followed by a '"' can be left as is
192 BOOL ENV_BuildCommandLine( char **argv )
198 for (arg = argv; *arg; arg++)
200 int has_space,bcount;
210 if (*a==' ' || *a=='\t') {
212 } else if (*a=='"') {
213 /* doubling of '\' preceeding a '"',
214 * plus escaping of said '"'
222 len+=(a-*arg)+1 /* for the separating space */;
224 len+=2; /* for the quotes */
227 if (!(current_envdb.cmd_line = HeapAlloc( GetProcessHeap(), 0, len )))
230 p = current_envdb.cmd_line;
231 for (arg = argv; *arg; arg++)
233 int has_space,has_quote;
236 /* Check for quotes and spaces in this argument */
237 has_space=has_quote=0;
240 if (*a==' ' || *a=='\t') {
244 } else if (*a=='"') {
252 /* Now transfer it to the command line */
269 /* Double all the '\\' preceeding this '"', plus one */
270 for (i=0;i<=bcount;i++)
288 if (p > current_envdb.cmd_line)
289 p--; /* remove last space */
292 /* now allocate the Unicode version */
293 len = MultiByteToWideChar( CP_ACP, 0, current_envdb.cmd_line, -1, NULL, 0 );
294 if (!(cmdlineW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
296 MultiByteToWideChar( CP_ACP, 0, current_envdb.cmd_line, -1, cmdlineW, len );
301 /***********************************************************************
302 * GetCommandLineA (KERNEL32.@)
304 LPSTR WINAPI GetCommandLineA(void)
306 return current_envdb.cmd_line;
309 /***********************************************************************
310 * GetCommandLineW (KERNEL32.@)
312 LPWSTR WINAPI GetCommandLineW(void)
318 /***********************************************************************
319 * GetEnvironmentStrings (KERNEL32.@)
320 * GetEnvironmentStringsA (KERNEL32.@)
322 LPSTR WINAPI GetEnvironmentStringsA(void)
324 return current_envdb.environ;
328 /***********************************************************************
329 * GetEnvironmentStringsW (KERNEL32.@)
331 LPWSTR WINAPI GetEnvironmentStringsW(void)
337 size = HeapSize( GetProcessHeap(), 0, current_envdb.environ );
338 if ((ret = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) )) != NULL)
340 LPSTR pA = current_envdb.environ;
342 while (size--) *pW++ = (WCHAR)(BYTE)*pA++;
349 /***********************************************************************
350 * FreeEnvironmentStringsA (KERNEL32.@)
352 BOOL WINAPI FreeEnvironmentStringsA( LPSTR ptr )
354 if (ptr != current_envdb.environ)
356 SetLastError( ERROR_INVALID_PARAMETER );
363 /***********************************************************************
364 * FreeEnvironmentStringsW (KERNEL32.@)
366 BOOL WINAPI FreeEnvironmentStringsW( LPWSTR ptr )
368 return HeapFree( GetProcessHeap(), 0, ptr );
372 /***********************************************************************
373 * GetEnvironmentVariableA (KERNEL32.@)
375 DWORD WINAPI GetEnvironmentVariableA( LPCSTR name, LPSTR value, DWORD size )
382 SetLastError( ERROR_INVALID_PARAMETER );
386 if ((p = ENV_FindVariable( current_envdb.environ, name, strlen(name) )))
391 /* If not enough room, include the terminating null
392 * in the returned size and return an empty string */
394 if (value) *value = '\0';
396 else if (value) strcpy( value, p );
400 SetLastError( ERROR_ENVVAR_NOT_FOUND );
405 /***********************************************************************
406 * GetEnvironmentVariableW (KERNEL32.@)
408 DWORD WINAPI GetEnvironmentVariableW( LPCWSTR nameW, LPWSTR valW, DWORD size)
410 LPSTR name = HEAP_strdupWtoA( GetProcessHeap(), 0, nameW );
411 LPSTR val = valW ? HeapAlloc( GetProcessHeap(), 0, size ) : NULL;
412 DWORD res = GetEnvironmentVariableA( name, val, size );
413 HeapFree( GetProcessHeap(), 0, name );
416 if (size > 0 && !MultiByteToWideChar( CP_ACP, 0, val, -1, valW, size ))
418 HeapFree( GetProcessHeap(), 0, val );
424 /***********************************************************************
425 * SetEnvironmentVariableA (KERNEL32.@)
427 BOOL WINAPI SetEnvironmentVariableA( LPCSTR name, LPCSTR value )
429 INT old_size, len, res;
430 LPSTR p, env, new_env;
434 env = p = current_envdb.environ;
436 /* Find a place to insert the string */
442 if (!strncasecmp( name, p, len ) && (p[len] == '=')) break;
445 if (!value && !*p) goto done; /* Value to remove doesn't exist */
447 /* Realloc the buffer */
449 len = value ? strlen(name) + strlen(value) + 2 : 0;
450 if (*p) len -= strlen(p) + 1; /* The name already exists */
451 old_size = HeapSize( GetProcessHeap(), 0, env );
454 LPSTR next = p + strlen(p) + 1; /* We know there is a next one */
455 memmove( next + len, next, old_size - (next - env) );
457 if (!(new_env = HeapReAlloc( GetProcessHeap(), 0, env, old_size + len )))
459 if (env_sel) env_sel = SELECTOR_ReallocBlock( env_sel, new_env, old_size + len );
460 p = new_env + (p - env);
461 if (len > 0) memmove( p + len, p, old_size - (p - new_env) );
463 /* Set the new string */
471 current_envdb.environ = new_env;
480 /***********************************************************************
481 * SetEnvironmentVariableW (KERNEL32.@)
483 BOOL WINAPI SetEnvironmentVariableW( LPCWSTR name, LPCWSTR value )
485 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
486 LPSTR valueA = HEAP_strdupWtoA( GetProcessHeap(), 0, value );
487 BOOL ret = SetEnvironmentVariableA( nameA, valueA );
488 HeapFree( GetProcessHeap(), 0, nameA );
489 HeapFree( GetProcessHeap(), 0, valueA );
494 /***********************************************************************
495 * ExpandEnvironmentStringsA (KERNEL32.@)
497 * Note: overlapping buffers are not supported; this is how it should be.
499 DWORD WINAPI ExpandEnvironmentStringsA( LPCSTR src, LPSTR dst, DWORD count )
501 DWORD len, total_size = 1; /* 1 for terminating '\0' */
504 if (!count) dst = NULL;
511 if ((p = strchr( src, '%' ))) len = p - src;
512 else len = strlen(src);
516 else /* we are at the start of a variable */
518 if ((p = strchr( src + 1, '%' )))
520 len = p - src - 1; /* Length of the variable name */
521 if ((var = ENV_FindVariable( current_envdb.environ,
524 src += len + 2; /* Skip the variable name */
529 var = src; /* Copy original name instead */
534 else /* unfinished variable name, ignore it */
537 len = strlen(src); /* Copy whole string */
544 if (count < len) len = count;
545 memcpy( dst, var, len );
552 /* Null-terminate the string */
562 /***********************************************************************
563 * ExpandEnvironmentStringsW (KERNEL32.@)
565 DWORD WINAPI ExpandEnvironmentStringsW( LPCWSTR src, LPWSTR dst, DWORD len )
567 LPSTR srcA = HEAP_strdupWtoA( GetProcessHeap(), 0, src );
568 LPSTR dstA = dst ? HeapAlloc( GetProcessHeap(), 0, len ) : NULL;
569 DWORD ret = ExpandEnvironmentStringsA( srcA, dstA, len );
572 ret = MultiByteToWideChar( CP_ACP, 0, dstA, -1, dst, len );
573 HeapFree( GetProcessHeap(), 0, dstA );
575 HeapFree( GetProcessHeap(), 0, srcA );
580 /***********************************************************************
581 * GetDOSEnvironment (KERNEL.131)
582 * GetDOSEnvironment16 (KERNEL32.@)
584 SEGPTR WINAPI GetDOSEnvironment16(void)
586 return MAKESEGPTR( env_sel, 0 );
590 /***********************************************************************
591 * GetStdHandle (KERNEL32.@)
593 HANDLE WINAPI GetStdHandle( DWORD std_handle )
597 case STD_INPUT_HANDLE: return current_envdb.hStdin;
598 case STD_OUTPUT_HANDLE: return current_envdb.hStdout;
599 case STD_ERROR_HANDLE: return current_envdb.hStderr;
601 SetLastError( ERROR_INVALID_PARAMETER );
602 return INVALID_HANDLE_VALUE;
606 /***********************************************************************
607 * SetStdHandle (KERNEL32.@)
609 BOOL WINAPI SetStdHandle( DWORD std_handle, HANDLE handle )
613 case STD_INPUT_HANDLE: current_envdb.hStdin = handle; return TRUE;
614 case STD_OUTPUT_HANDLE: current_envdb.hStdout = handle; return TRUE;
615 case STD_ERROR_HANDLE: current_envdb.hStderr = handle; return TRUE;
617 SetLastError( ERROR_INVALID_PARAMETER );
622 /***********************************************************************
623 * GetStartupInfoA (KERNEL32.@)
625 VOID WINAPI GetStartupInfoA( LPSTARTUPINFOA info )
627 *info = current_startupinfo;
631 /***********************************************************************
632 * GetStartupInfoW (KERNEL32.@)
634 VOID WINAPI GetStartupInfoW( LPSTARTUPINFOW info )
636 info->cb = sizeof(STARTUPINFOW);
637 info->dwX = current_startupinfo.dwX;
638 info->dwY = current_startupinfo.dwY;
639 info->dwXSize = current_startupinfo.dwXSize;
640 info->dwXCountChars = current_startupinfo.dwXCountChars;
641 info->dwYCountChars = current_startupinfo.dwYCountChars;
642 info->dwFillAttribute = current_startupinfo.dwFillAttribute;
643 info->dwFlags = current_startupinfo.dwFlags;
644 info->wShowWindow = current_startupinfo.wShowWindow;
645 info->cbReserved2 = current_startupinfo.cbReserved2;
646 info->lpReserved2 = current_startupinfo.lpReserved2;
647 info->hStdInput = current_startupinfo.hStdInput;
648 info->hStdOutput = current_startupinfo.hStdOutput;
649 info->hStdError = current_startupinfo.hStdError;
650 info->lpReserved = HEAP_strdupAtoW (GetProcessHeap(), 0, current_startupinfo.lpReserved );
651 info->lpDesktop = HEAP_strdupAtoW (GetProcessHeap(), 0, current_startupinfo.lpDesktop );
652 info->lpTitle = HEAP_strdupAtoW (GetProcessHeap(), 0, current_startupinfo.lpTitle );