2 * Process environment management
4 * Copyright 1996, 1998 Alexandre Julliard
14 #include "selectors.h"
16 /* Win32 process environment database */
19 LPSTR environ
; /* 00 Process environment strings */
20 DWORD unknown1
; /* 04 Unknown */
21 LPSTR cmd_line
; /* 08 Command line */
22 LPSTR cur_dir
; /* 0c Current directory */
23 STARTUPINFOA
*startup_info
; /* 10 Startup information */
24 HANDLE hStdin
; /* 14 Handle for standard input */
25 HANDLE hStdout
; /* 18 Handle for standard output */
26 HANDLE hStderr
; /* 1c Handle for standard error */
27 DWORD unknown2
; /* 20 Unknown */
28 DWORD inherit_console
; /* 24 Inherit console flag */
29 DWORD break_type
; /* 28 Console events flag */
30 void *break_sem
; /* 2c SetConsoleCtrlHandler semaphore */
31 void *break_event
; /* 30 SetConsoleCtrlHandler event */
32 void *break_thread
; /* 34 SetConsoleCtrlHandler thread */
33 void *break_handlers
; /* 38 List of console handlers */
37 /* Format of an environment block:
38 * ASCIIZ string 1 (xx=yy format)
43 * ASCIIZ program name (e.g. C:\WINDOWS\SYSTEM\KRNL386.EXE)
46 * - contrary to Microsoft docs, the environment strings do not appear
47 * to be sorted on Win95 (although they are on NT); so we don't bother
48 * to sort them either.
51 static const char ENV_program_name
[] = "C:\\WINDOWS\\SYSTEM\\KRNL386.EXE";
53 /* Maximum length of a Win16 environment string (including NULL) */
54 #define MAX_WIN16_LEN 128
56 /* Extra bytes to reserve at the end of an environment */
57 #define EXTRA_ENV_SIZE (sizeof(BYTE) + sizeof(WORD) + sizeof(ENV_program_name))
59 /* Fill the extra bytes with the program name and stuff */
60 #define FILL_EXTRA_ENV(p) \
62 PUT_WORD( (p) + 1, 1 ); \
63 strcpy( (p) + 3, ENV_program_name );
65 STARTUPINFOA current_startupinfo
=
67 sizeof(STARTUPINFOA
), /* cb */
75 0, /* dwXCountChars */
76 0, /* dwYCountChars */
77 0, /* dwFillAttribute */
93 ¤t_startupinfo
, /* startup_info */
98 0, /* inherit_console */
102 0, /* break_thread */
103 0 /* break_handlers */
107 static WCHAR
*cmdlineW
; /* Unicode command line */
108 static WORD env_sel
; /* selector to the environment */
110 /***********************************************************************
113 * Find a variable in the environment and return a pointer to the value.
114 * Helper function for GetEnvironmentVariable and ExpandEnvironmentStrings.
116 static LPCSTR
ENV_FindVariable( LPCSTR env
, LPCSTR name
, INT len
)
120 if (!strncasecmp( name
, env
, len
) && (env
[len
] == '='))
121 return env
+ len
+ 1;
122 env
+= strlen(env
) + 1;
128 /***********************************************************************
129 * ENV_BuildEnvironment
131 * Build the environment for the initial process
133 ENVDB
*ENV_BuildEnvironment(void)
135 extern char **environ
;
139 /* Compute the total size of the Unix environment */
141 size
= EXTRA_ENV_SIZE
;
142 for (e
= environ
; *e
; e
++) size
+= strlen(*e
) + 1;
144 /* Now allocate the environment */
146 if (!(p
= HeapAlloc( GetProcessHeap(), 0, size
))) return NULL
;
147 current_envdb
.environ
= p
;
148 env_sel
= SELECTOR_AllocBlock( p
, 0x10000, WINE_LDT_FLAGS_DATA
);
150 /* And fill it with the Unix environment */
152 for (e
= environ
; *e
; e
++)
158 /* Now add the program name */
161 return ¤t_envdb
;
165 /***********************************************************************
166 * ENV_BuildCommandLine
168 * Build the command line of a process from the argv array.
170 * Note that it does NOT necessarily include the file name.
171 * Sometimes we don't even have any command line options at all.
173 BOOL
ENV_BuildCommandLine( char **argv
)
178 for (arg
= argv
, len
= 0; *arg
; arg
++) len
+= strlen(*arg
) + 1;
179 if ((argv
[0]) && (quote
= (strchr( argv
[0], ' ' ) != NULL
))) len
+= 2;
180 if (!(p
= current_envdb
.cmd_line
= HeapAlloc( GetProcessHeap(), 0, len
))) return FALSE
;
198 if (p
> current_envdb
.cmd_line
) p
--; /* remove last space */
200 /* now allocate the Unicode version */
201 len
= MultiByteToWideChar( CP_ACP
, 0, current_envdb
.cmd_line
, -1, NULL
, 0 );
202 if (!(cmdlineW
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) )))
204 MultiByteToWideChar( CP_ACP
, 0, current_envdb
.cmd_line
, -1, cmdlineW
, len
);
209 /***********************************************************************
210 * GetCommandLineA (KERNEL32.289)
212 LPSTR WINAPI
GetCommandLineA(void)
214 return current_envdb
.cmd_line
;
217 /***********************************************************************
218 * GetCommandLineW (KERNEL32.290)
220 LPWSTR WINAPI
GetCommandLineW(void)
226 /***********************************************************************
227 * GetEnvironmentStringsA (KERNEL32.319) (KERNEL32.320)
229 LPSTR WINAPI
GetEnvironmentStringsA(void)
231 return current_envdb
.environ
;
235 /***********************************************************************
236 * GetEnvironmentStringsW (KERNEL32.321)
238 LPWSTR WINAPI
GetEnvironmentStringsW(void)
244 size
= HeapSize( GetProcessHeap(), 0, current_envdb
.environ
);
245 if ((ret
= HeapAlloc( GetProcessHeap(), 0, size
* sizeof(WCHAR
) )) != NULL
)
247 LPSTR pA
= current_envdb
.environ
;
249 while (size
--) *pW
++ = (WCHAR
)(BYTE
)*pA
++;
256 /***********************************************************************
257 * FreeEnvironmentStringsA (KERNEL32.268)
259 BOOL WINAPI
FreeEnvironmentStringsA( LPSTR ptr
)
261 if (ptr
!= current_envdb
.environ
)
263 SetLastError( ERROR_INVALID_PARAMETER
);
270 /***********************************************************************
271 * FreeEnvironmentStringsW (KERNEL32.269)
273 BOOL WINAPI
FreeEnvironmentStringsW( LPWSTR ptr
)
275 return HeapFree( GetProcessHeap(), 0, ptr
);
279 /***********************************************************************
280 * GetEnvironmentVariableA (KERNEL32.322)
282 DWORD WINAPI
GetEnvironmentVariableA( LPCSTR name
, LPSTR value
, DWORD size
)
289 SetLastError( ERROR_INVALID_PARAMETER
);
293 if ((p
= ENV_FindVariable( current_envdb
.environ
, name
, strlen(name
) )))
298 /* If not enough room, include the terminating null
299 * in the returned size and return an empty string */
301 if (value
) *value
= '\0';
303 else if (value
) strcpy( value
, p
);
307 SetLastError( ERROR_ENVVAR_NOT_FOUND
);
312 /***********************************************************************
313 * GetEnvironmentVariableW (KERNEL32.323)
315 DWORD WINAPI
GetEnvironmentVariableW( LPCWSTR nameW
, LPWSTR valW
, DWORD size
)
317 LPSTR name
= HEAP_strdupWtoA( GetProcessHeap(), 0, nameW
);
318 LPSTR val
= valW
? HeapAlloc( GetProcessHeap(), 0, size
) : NULL
;
319 DWORD res
= GetEnvironmentVariableA( name
, val
, size
);
320 HeapFree( GetProcessHeap(), 0, name
);
323 if (size
> 0 && !MultiByteToWideChar( CP_ACP
, 0, val
, -1, valW
, size
))
325 HeapFree( GetProcessHeap(), 0, val
);
331 /***********************************************************************
332 * SetEnvironmentVariableA (KERNEL32.641)
334 BOOL WINAPI
SetEnvironmentVariableA( LPCSTR name
, LPCSTR value
)
336 INT old_size
, len
, res
;
337 LPSTR p
, env
, new_env
;
341 env
= p
= current_envdb
.environ
;
343 /* Find a place to insert the string */
349 if (!strncasecmp( name
, p
, len
) && (p
[len
] == '=')) break;
352 if (!value
&& !*p
) goto done
; /* Value to remove doesn't exist */
354 /* Realloc the buffer */
356 len
= value
? strlen(name
) + strlen(value
) + 2 : 0;
357 if (*p
) len
-= strlen(p
) + 1; /* The name already exists */
358 old_size
= HeapSize( GetProcessHeap(), 0, env
);
361 LPSTR next
= p
+ strlen(p
) + 1; /* We know there is a next one */
362 memmove( next
+ len
, next
, old_size
- (next
- env
) );
364 if (!(new_env
= HeapReAlloc( GetProcessHeap(), 0, env
, old_size
+ len
)))
366 if (env_sel
) env_sel
= SELECTOR_ReallocBlock( env_sel
, new_env
, old_size
+ len
);
367 p
= new_env
+ (p
- env
);
368 if (len
> 0) memmove( p
+ len
, p
, old_size
- (p
- new_env
) );
370 /* Set the new string */
378 current_envdb
.environ
= new_env
;
387 /***********************************************************************
388 * SetEnvironmentVariableW (KERNEL32.642)
390 BOOL WINAPI
SetEnvironmentVariableW( LPCWSTR name
, LPCWSTR value
)
392 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
393 LPSTR valueA
= HEAP_strdupWtoA( GetProcessHeap(), 0, value
);
394 BOOL ret
= SetEnvironmentVariableA( nameA
, valueA
);
395 HeapFree( GetProcessHeap(), 0, nameA
);
396 HeapFree( GetProcessHeap(), 0, valueA
);
401 /***********************************************************************
402 * ExpandEnvironmentStringsA (KERNEL32.216)
404 * Note: overlapping buffers are not supported; this is how it should be.
406 DWORD WINAPI
ExpandEnvironmentStringsA( LPCSTR src
, LPSTR dst
, DWORD count
)
408 DWORD len
, total_size
= 1; /* 1 for terminating '\0' */
411 if (!count
) dst
= NULL
;
418 if ((p
= strchr( src
, '%' ))) len
= p
- src
;
419 else len
= strlen(src
);
423 else /* we are at the start of a variable */
425 if ((p
= strchr( src
+ 1, '%' )))
427 len
= p
- src
- 1; /* Length of the variable name */
428 if ((var
= ENV_FindVariable( current_envdb
.environ
,
431 src
+= len
+ 2; /* Skip the variable name */
436 var
= src
; /* Copy original name instead */
441 else /* unfinished variable name, ignore it */
444 len
= strlen(src
); /* Copy whole string */
451 if (count
< len
) len
= count
;
452 memcpy( dst
, var
, len
);
459 /* Null-terminate the string */
469 /***********************************************************************
470 * ExpandEnvironmentStringsW (KERNEL32.217)
472 DWORD WINAPI
ExpandEnvironmentStringsW( LPCWSTR src
, LPWSTR dst
, DWORD len
)
474 LPSTR srcA
= HEAP_strdupWtoA( GetProcessHeap(), 0, src
);
475 LPSTR dstA
= dst
? HeapAlloc( GetProcessHeap(), 0, len
) : NULL
;
476 DWORD ret
= ExpandEnvironmentStringsA( srcA
, dstA
, len
);
479 ret
= MultiByteToWideChar( CP_ACP
, 0, dstA
, -1, dst
, len
);
480 HeapFree( GetProcessHeap(), 0, dstA
);
482 HeapFree( GetProcessHeap(), 0, srcA
);
487 /***********************************************************************
488 * GetDOSEnvironment16 (KERNEL.131)
490 SEGPTR WINAPI
GetDOSEnvironment16(void)
492 return MAKESEGPTR( env_sel
, 0 );
496 /***********************************************************************
497 * GetStdHandle (KERNEL32.276)
499 HANDLE WINAPI
GetStdHandle( DWORD std_handle
)
503 case STD_INPUT_HANDLE
: return current_envdb
.hStdin
;
504 case STD_OUTPUT_HANDLE
: return current_envdb
.hStdout
;
505 case STD_ERROR_HANDLE
: return current_envdb
.hStderr
;
507 SetLastError( ERROR_INVALID_PARAMETER
);
508 return INVALID_HANDLE_VALUE
;
512 /***********************************************************************
513 * SetStdHandle (KERNEL32.506)
515 BOOL WINAPI
SetStdHandle( DWORD std_handle
, HANDLE handle
)
519 case STD_INPUT_HANDLE
: current_envdb
.hStdin
= handle
; return TRUE
;
520 case STD_OUTPUT_HANDLE
: current_envdb
.hStdout
= handle
; return TRUE
;
521 case STD_ERROR_HANDLE
: current_envdb
.hStderr
= handle
; return TRUE
;
523 SetLastError( ERROR_INVALID_PARAMETER
);
528 /***********************************************************************
529 * GetStartupInfoA (KERNEL32.273)
531 VOID WINAPI
GetStartupInfoA( LPSTARTUPINFOA info
)
533 *info
= current_startupinfo
;
537 /***********************************************************************
538 * GetStartupInfoW (KERNEL32.274)
540 VOID WINAPI
GetStartupInfoW( LPSTARTUPINFOW info
)
542 info
->cb
= sizeof(STARTUPINFOW
);
543 info
->dwX
= current_startupinfo
.dwX
;
544 info
->dwY
= current_startupinfo
.dwY
;
545 info
->dwXSize
= current_startupinfo
.dwXSize
;
546 info
->dwXCountChars
= current_startupinfo
.dwXCountChars
;
547 info
->dwYCountChars
= current_startupinfo
.dwYCountChars
;
548 info
->dwFillAttribute
= current_startupinfo
.dwFillAttribute
;
549 info
->dwFlags
= current_startupinfo
.dwFlags
;
550 info
->wShowWindow
= current_startupinfo
.wShowWindow
;
551 info
->cbReserved2
= current_startupinfo
.cbReserved2
;
552 info
->lpReserved2
= current_startupinfo
.lpReserved2
;
553 info
->hStdInput
= current_startupinfo
.hStdInput
;
554 info
->hStdOutput
= current_startupinfo
.hStdOutput
;
555 info
->hStdError
= current_startupinfo
.hStdError
;
556 info
->lpReserved
= HEAP_strdupAtoW (GetProcessHeap(), 0, current_startupinfo
.lpReserved
);
557 info
->lpDesktop
= HEAP_strdupAtoW (GetProcessHeap(), 0, current_startupinfo
.lpDesktop
);
558 info
->lpTitle
= HEAP_strdupAtoW (GetProcessHeap(), 0, current_startupinfo
.lpTitle
);