2 * Process environment management
4 * Copyright 1996, 1998 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
30 #include "wine/winbase16.h"
33 #include "selectors.h"
35 /* Win32 process environment database */
38 LPSTR environ; /* 00 Process environment strings */
39 DWORD unknown1; /* 04 Unknown */
40 LPSTR cmd_line; /* 08 Command line */
41 LPSTR cur_dir; /* 0c Current directory */
42 STARTUPINFOA *startup_info; /* 10 Startup information */
43 HANDLE hStdin; /* 14 Handle for standard input */
44 HANDLE hStdout; /* 18 Handle for standard output */
45 HANDLE hStderr; /* 1c Handle for standard error */
46 DWORD unknown2; /* 20 Unknown */
47 DWORD inherit_console; /* 24 Inherit console flag */
48 DWORD break_type; /* 28 Console events flag */
49 void *break_sem; /* 2c SetConsoleCtrlHandler semaphore */
50 void *break_event; /* 30 SetConsoleCtrlHandler event */
51 void *break_thread; /* 34 SetConsoleCtrlHandler thread */
52 void *break_handlers; /* 38 List of console handlers */
56 /* Format of an environment block:
57 * ASCIIZ string 1 (xx=yy format)
62 * ASCIIZ program name (e.g. C:\WINDOWS\SYSTEM\KRNL386.EXE)
65 * - contrary to Microsoft docs, the environment strings do not appear
66 * to be sorted on Win95 (although they are on NT); so we don't bother
67 * to sort them either.
70 static const char ENV_program_name[] = "C:\\WINDOWS\\SYSTEM\\KRNL386.EXE";
72 /* Maximum length of a Win16 environment string (including NULL) */
73 #define MAX_WIN16_LEN 128
75 /* Extra bytes to reserve at the end of an environment */
76 #define EXTRA_ENV_SIZE (sizeof(BYTE) + sizeof(WORD) + sizeof(ENV_program_name))
78 /* Fill the extra bytes with the program name and stuff */
79 #define FILL_EXTRA_ENV(p) \
81 PUT_UA_WORD( (p) + 1, 1 ); \
82 strcpy( (p) + 3, ENV_program_name );
84 STARTUPINFOA current_startupinfo =
86 sizeof(STARTUPINFOA), /* cb */
94 0, /* dwXCountChars */
95 0, /* dwYCountChars */
96 0, /* dwFillAttribute */
106 ENVDB current_envdb =
112 ¤t_startupinfo, /* startup_info */
117 0, /* inherit_console */
121 0, /* break_thread */
122 0 /* break_handlers */
126 static WCHAR *cmdlineW; /* Unicode command line */
127 static WORD env_sel; /* selector to the environment */
129 /***********************************************************************
132 * Find a variable in the environment and return a pointer to the value.
133 * Helper function for GetEnvironmentVariable and ExpandEnvironmentStrings.
135 static LPCSTR ENV_FindVariable( LPCSTR env, LPCSTR name, INT len )
139 if (!strncasecmp( name, env, len ) && (env[len] == '='))
140 return env + len + 1;
141 env += strlen(env) + 1;
147 /***********************************************************************
148 * ENV_BuildEnvironment
150 * Build the environment for the initial process
152 ENVDB *ENV_BuildEnvironment(void)
154 extern char **environ;
158 /* Compute the total size of the Unix environment */
160 size = EXTRA_ENV_SIZE;
161 for (e = environ; *e; e++) size += strlen(*e) + 1;
163 /* Now allocate the environment */
165 if (!(p = HeapAlloc( GetProcessHeap(), 0, size ))) return NULL;
166 current_envdb.environ = p;
167 env_sel = SELECTOR_AllocBlock( p, 0x10000, WINE_LDT_FLAGS_DATA );
169 /* And fill it with the Unix environment */
171 for (e = environ; *e; e++)
177 /* Now add the program name */
180 return ¤t_envdb;
184 /***********************************************************************
185 * ENV_BuildCommandLine
187 * Build the command line of a process from the argv array.
189 * Note that it does NOT necessarily include the file name.
190 * Sometimes we don't even have any command line options at all.
192 * We must quote and escape characters so that the argv array can be rebuilt
193 * from the command line:
194 * - spaces and tabs must be quoted
196 * - quotes must be escaped
198 * - if '\'s are followed by a '"', they must be doubled and followed by '\"',
199 * resulting in an odd number of '\' followed by a '"'
202 * - '\'s that are not followed by a '"' can be left as is
206 BOOL ENV_BuildCommandLine( char **argv )
212 for (arg = argv; *arg; arg++)
214 int has_space,bcount;
224 if (*a==' ' || *a=='\t') {
226 } else if (*a=='"') {
227 /* doubling of '\' preceeding a '"',
228 * plus escaping of said '"'
236 len+=(a-*arg)+1 /* for the separating space */;
238 len+=2; /* for the quotes */
241 if (!(current_envdb.cmd_line = HeapAlloc( GetProcessHeap(), 0, len )))
244 p = current_envdb.cmd_line;
245 for (arg = argv; *arg; arg++)
247 int has_space,has_quote;
250 /* Check for quotes and spaces in this argument */
251 has_space=has_quote=0;
254 if (*a==' ' || *a=='\t') {
258 } else if (*a=='"') {
266 /* Now transfer it to the command line */
283 /* Double all the '\\' preceeding this '"', plus one */
284 for (i=0;i<=bcount;i++)
302 if (p > current_envdb.cmd_line)
303 p--; /* remove last space */
306 /* now allocate the Unicode version */
307 len = MultiByteToWideChar( CP_ACP, 0, current_envdb.cmd_line, -1, NULL, 0 );
308 if (!(cmdlineW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
310 MultiByteToWideChar( CP_ACP, 0, current_envdb.cmd_line, -1, cmdlineW, len );
315 /***********************************************************************
316 * GetCommandLineA (KERNEL32.@)
318 * WARNING: there's a Windows incompatibility lurking here !
319 * Win32s always includes the full path of the program file,
320 * whereas Windows NT only returns the full file path plus arguments
321 * in case the program has been started with a full path.
322 * Win9x seems to have inherited NT behaviour.
324 * Note that both Start Menu Execute and Explorer start programs with
325 * fully specified quoted app file paths, which is why probably the only case
326 * where you'll see single file names is in case of direct launch
327 * via CreateProcess or WinExec.
329 * Perhaps we should take care of Win3.1 programs here (Win32s "feature").
331 * References: MS KB article q102762.txt (special Win32s handling)
333 LPSTR WINAPI GetCommandLineA(void)
335 return current_envdb.cmd_line;
338 /***********************************************************************
339 * GetCommandLineW (KERNEL32.@)
341 LPWSTR WINAPI GetCommandLineW(void)
347 /***********************************************************************
348 * GetEnvironmentStrings (KERNEL32.@)
349 * GetEnvironmentStringsA (KERNEL32.@)
351 LPSTR WINAPI GetEnvironmentStringsA(void)
353 return current_envdb.environ;
357 /***********************************************************************
358 * GetEnvironmentStringsW (KERNEL32.@)
360 LPWSTR WINAPI GetEnvironmentStringsW(void)
366 size = HeapSize( GetProcessHeap(), 0, current_envdb.environ );
367 if ((ret = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) )) != NULL)
369 LPSTR pA = current_envdb.environ;
371 while (size--) *pW++ = (WCHAR)(BYTE)*pA++;
378 /***********************************************************************
379 * FreeEnvironmentStringsA (KERNEL32.@)
381 BOOL WINAPI FreeEnvironmentStringsA( LPSTR ptr )
383 if (ptr != current_envdb.environ)
385 SetLastError( ERROR_INVALID_PARAMETER );
392 /***********************************************************************
393 * FreeEnvironmentStringsW (KERNEL32.@)
395 BOOL WINAPI FreeEnvironmentStringsW( LPWSTR ptr )
397 return HeapFree( GetProcessHeap(), 0, ptr );
401 /***********************************************************************
402 * GetEnvironmentVariableA (KERNEL32.@)
404 DWORD WINAPI GetEnvironmentVariableA( LPCSTR name, LPSTR value, DWORD size )
411 SetLastError( ERROR_INVALID_PARAMETER );
415 if ((p = ENV_FindVariable( current_envdb.environ, name, strlen(name) )))
420 /* If not enough room, include the terminating null
421 * in the returned size and return an empty string */
423 if (value) *value = '\0';
425 else if (value) strcpy( value, p );
429 SetLastError( ERROR_ENVVAR_NOT_FOUND );
434 /***********************************************************************
435 * GetEnvironmentVariableW (KERNEL32.@)
437 DWORD WINAPI GetEnvironmentVariableW( LPCWSTR nameW, LPWSTR valW, DWORD size)
439 LPSTR name = HEAP_strdupWtoA( GetProcessHeap(), 0, nameW );
440 LPSTR val = valW ? HeapAlloc( GetProcessHeap(), 0, size ) : NULL;
441 DWORD res = GetEnvironmentVariableA( name, val, size );
442 HeapFree( GetProcessHeap(), 0, name );
445 if (size > 0 && !MultiByteToWideChar( CP_ACP, 0, val, -1, valW, size ))
447 HeapFree( GetProcessHeap(), 0, val );
453 /***********************************************************************
454 * SetEnvironmentVariableA (KERNEL32.@)
456 BOOL WINAPI SetEnvironmentVariableA( LPCSTR name, LPCSTR value )
458 INT old_size, len, res;
459 LPSTR p, env, new_env;
463 env = p = current_envdb.environ;
465 /* Find a place to insert the string */
471 if (!strncasecmp( name, p, len ) && (p[len] == '=')) break;
474 if (!value && !*p) goto done; /* Value to remove doesn't exist */
476 /* Realloc the buffer */
478 len = value ? strlen(name) + strlen(value) + 2 : 0;
479 if (*p) len -= strlen(p) + 1; /* The name already exists */
480 old_size = HeapSize( GetProcessHeap(), 0, env );
483 LPSTR next = p + strlen(p) + 1; /* We know there is a next one */
484 memmove( next + len, next, old_size - (next - env) );
486 if (!(new_env = HeapReAlloc( GetProcessHeap(), 0, env, old_size + len )))
488 if (env_sel) env_sel = SELECTOR_ReallocBlock( env_sel, new_env, old_size + len );
489 p = new_env + (p - env);
490 if (len > 0) memmove( p + len, p, old_size - (p - new_env) );
492 /* Set the new string */
500 current_envdb.environ = new_env;
509 /***********************************************************************
510 * SetEnvironmentVariableW (KERNEL32.@)
512 BOOL WINAPI SetEnvironmentVariableW( LPCWSTR name, LPCWSTR value )
514 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
515 LPSTR valueA = HEAP_strdupWtoA( GetProcessHeap(), 0, value );
516 BOOL ret = SetEnvironmentVariableA( nameA, valueA );
517 HeapFree( GetProcessHeap(), 0, nameA );
518 HeapFree( GetProcessHeap(), 0, valueA );
523 /***********************************************************************
524 * ExpandEnvironmentStringsA (KERNEL32.@)
526 * Note: overlapping buffers are not supported; this is how it should be.
528 DWORD WINAPI ExpandEnvironmentStringsA( LPCSTR src, LPSTR dst, DWORD count )
530 DWORD len, total_size = 1; /* 1 for terminating '\0' */
533 if (!count) dst = NULL;
540 if ((p = strchr( src, '%' ))) len = p - src;
541 else len = strlen(src);
545 else /* we are at the start of a variable */
547 if ((p = strchr( src + 1, '%' )))
549 len = p - src - 1; /* Length of the variable name */
550 if ((var = ENV_FindVariable( current_envdb.environ,
553 src += len + 2; /* Skip the variable name */
558 var = src; /* Copy original name instead */
563 else /* unfinished variable name, ignore it */
566 len = strlen(src); /* Copy whole string */
573 if (count < len) len = count;
574 memcpy( dst, var, len );
581 /* Null-terminate the string */
591 /***********************************************************************
592 * ExpandEnvironmentStringsW (KERNEL32.@)
594 DWORD WINAPI ExpandEnvironmentStringsW( LPCWSTR src, LPWSTR dst, DWORD len )
596 LPSTR srcA = HEAP_strdupWtoA( GetProcessHeap(), 0, src );
597 LPSTR dstA = dst ? HeapAlloc( GetProcessHeap(), 0, len ) : NULL;
598 DWORD ret = ExpandEnvironmentStringsA( srcA, dstA, len );
601 ret = MultiByteToWideChar( CP_ACP, 0, dstA, -1, dst, len );
602 HeapFree( GetProcessHeap(), 0, dstA );
604 HeapFree( GetProcessHeap(), 0, srcA );
609 /***********************************************************************
610 * GetDOSEnvironment (KERNEL.131)
611 * GetDOSEnvironment16 (KERNEL32.@)
613 SEGPTR WINAPI GetDOSEnvironment16(void)
615 return MAKESEGPTR( env_sel, 0 );
619 /***********************************************************************
620 * GetStdHandle (KERNEL32.@)
622 HANDLE WINAPI GetStdHandle( DWORD std_handle )
626 case STD_INPUT_HANDLE: return current_envdb.hStdin;
627 case STD_OUTPUT_HANDLE: return current_envdb.hStdout;
628 case STD_ERROR_HANDLE: return current_envdb.hStderr;
630 SetLastError( ERROR_INVALID_PARAMETER );
631 return INVALID_HANDLE_VALUE;
635 /***********************************************************************
636 * SetStdHandle (KERNEL32.@)
638 BOOL WINAPI SetStdHandle( DWORD std_handle, HANDLE handle )
642 case STD_INPUT_HANDLE: current_envdb.hStdin = handle; return TRUE;
643 case STD_OUTPUT_HANDLE: current_envdb.hStdout = handle; return TRUE;
644 case STD_ERROR_HANDLE: current_envdb.hStderr = handle; return TRUE;
646 SetLastError( ERROR_INVALID_PARAMETER );
651 /***********************************************************************
652 * GetStartupInfoA (KERNEL32.@)
654 VOID WINAPI GetStartupInfoA( LPSTARTUPINFOA info )
656 *info = current_startupinfo;
660 /***********************************************************************
661 * GetStartupInfoW (KERNEL32.@)
663 VOID WINAPI GetStartupInfoW( LPSTARTUPINFOW info )
665 info->cb = sizeof(STARTUPINFOW);
666 info->dwX = current_startupinfo.dwX;
667 info->dwY = current_startupinfo.dwY;
668 info->dwXSize = current_startupinfo.dwXSize;
669 info->dwXCountChars = current_startupinfo.dwXCountChars;
670 info->dwYCountChars = current_startupinfo.dwYCountChars;
671 info->dwFillAttribute = current_startupinfo.dwFillAttribute;
672 info->dwFlags = current_startupinfo.dwFlags;
673 info->wShowWindow = current_startupinfo.wShowWindow;
674 info->cbReserved2 = current_startupinfo.cbReserved2;
675 info->lpReserved2 = current_startupinfo.lpReserved2;
676 info->hStdInput = current_startupinfo.hStdInput;
677 info->hStdOutput = current_startupinfo.hStdOutput;
678 info->hStdError = current_startupinfo.hStdError;
679 info->lpReserved = HEAP_strdupAtoW (GetProcessHeap(), 0, current_startupinfo.lpReserved );
680 info->lpDesktop = HEAP_strdupAtoW (GetProcessHeap(), 0, current_startupinfo.lpDesktop );
681 info->lpTitle = HEAP_strdupAtoW (GetProcessHeap(), 0, current_startupinfo.lpTitle );