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"
31 #include "wine/server.h"
32 #include "wine/library.h"
35 #include "selectors.h"
37 /* Win32 process environment database */
40 LPSTR env
; /* 00 Process environment strings */
41 DWORD unknown1
; /* 04 Unknown */
42 LPSTR cmd_line
; /* 08 Command line */
43 LPSTR cur_dir
; /* 0c Current directory */
44 STARTUPINFOA
*startup_info
; /* 10 Startup information */
45 HANDLE hStdin
; /* 14 Handle for standard input */
46 HANDLE hStdout
; /* 18 Handle for standard output */
47 HANDLE hStderr
; /* 1c Handle for standard error */
48 DWORD unknown2
; /* 20 Unknown */
49 DWORD inherit_console
; /* 24 Inherit console flag */
50 DWORD break_type
; /* 28 Console events flag */
51 void *break_sem
; /* 2c SetConsoleCtrlHandler semaphore */
52 void *break_event
; /* 30 SetConsoleCtrlHandler event */
53 void *break_thread
; /* 34 SetConsoleCtrlHandler thread */
54 void *break_handlers
; /* 38 List of console handlers */
58 /* Format of an environment block:
59 * ASCIIZ string 1 (xx=yy format)
64 * ASCIIZ program name (e.g. C:\WINDOWS\SYSTEM\KRNL386.EXE)
67 * - contrary to Microsoft docs, the environment strings do not appear
68 * to be sorted on Win95 (although they are on NT); so we don't bother
69 * to sort them either.
72 static const char ENV_program_name
[] = "C:\\WINDOWS\\SYSTEM\\KRNL386.EXE";
74 /* Maximum length of a Win16 environment string (including NULL) */
75 #define MAX_WIN16_LEN 128
77 /* Extra bytes to reserve at the end of an environment */
78 #define EXTRA_ENV_SIZE (sizeof(BYTE) + sizeof(WORD) + sizeof(ENV_program_name))
80 /* Fill the extra bytes with the program name and stuff */
81 #define FILL_EXTRA_ENV(p) \
83 PUT_UA_WORD( (p) + 1, 1 ); \
84 strcpy( (p) + 3, ENV_program_name );
86 STARTUPINFOA current_startupinfo
=
88 sizeof(STARTUPINFOA
), /* cb */
96 0, /* dwXCountChars */
97 0, /* dwYCountChars */
98 0, /* dwFillAttribute */
108 ENVDB current_envdb
=
114 ¤t_startupinfo
, /* startup_info */
119 0, /* inherit_console */
123 0, /* break_thread */
124 0 /* break_handlers */
128 static WCHAR
*cmdlineW
; /* Unicode command line */
129 static WORD env_sel
; /* selector to the environment */
131 /***********************************************************************
134 * Find a variable in the environment and return a pointer to the value.
135 * Helper function for GetEnvironmentVariable and ExpandEnvironmentStrings.
137 static LPCSTR
ENV_FindVariable( LPCSTR env
, LPCSTR name
, INT len
)
141 if (!strncasecmp( name
, env
, len
) && (env
[len
] == '='))
142 return env
+ len
+ 1;
143 env
+= strlen(env
) + 1;
149 /***********************************************************************
152 * Build the environment for the initial process
154 static BOOL
build_environment(void)
156 extern char **environ
;
160 /* Compute the total size of the Unix environment */
162 size
= EXTRA_ENV_SIZE
;
163 for (e
= environ
; *e
; e
++) size
+= strlen(*e
) + 1;
165 /* Now allocate the environment */
167 if (!(p
= HeapAlloc( GetProcessHeap(), 0, size
))) return FALSE
;
168 current_envdb
.env
= p
;
169 env_sel
= SELECTOR_AllocBlock( p
, 0x10000, WINE_LDT_FLAGS_DATA
);
171 /* And fill it with the Unix environment */
173 for (e
= environ
; *e
; e
++)
179 /* Now add the program name */
186 /***********************************************************************
189 * Small helper for ENV_InitStartupInfo.
191 inline static char *copy_str( char **dst
, const char **src
, size_t len
)
195 if (!len
) return NULL
;
197 memcpy( ret
, *src
, len
);
205 /***********************************************************************
206 * ENV_InitStartupInfo
208 * Fill the startup info structure from the server.
210 ENVDB
*ENV_InitStartupInfo( handle_t info_handle
, size_t info_size
,
211 char *main_exe_name
, size_t main_exe_size
)
219 if (!build_environment()) return NULL
;
220 if (!info_size
) return ¤t_envdb
; /* nothing to retrieve */
222 if (!(data
= HeapAlloc( GetProcessHeap(), 0, info_size
))) return NULL
;
224 SERVER_START_REQ( get_startup_info
)
226 req
->info
= info_handle
;
228 wine_server_set_reply( req
, data
, info_size
);
229 wine_server_call( req
);
230 info_size
= wine_server_reply_size( reply
);
233 if (info_size
< sizeof(info
.size
)) goto done
;
234 len
= min( info_size
, ((startup_info_t
*)data
)->size
);
235 memset( &info
, 0, sizeof(info
) );
236 memcpy( &info
, data
, len
);
237 src
= (char *)data
+ len
;
240 /* fixup the lengths */
241 if (info
.filename_len
> info_size
) info
.filename_len
= info_size
;
242 info_size
-= info
.filename_len
;
243 if (info
.cmdline_len
> info_size
) info
.cmdline_len
= info_size
;
244 info_size
-= info
.cmdline_len
;
245 if (info
.desktop_len
> info_size
) info
.desktop_len
= info_size
;
246 info_size
-= info
.desktop_len
;
247 if (info
.title_len
> info_size
) info
.title_len
= info_size
;
249 /* store the filename */
250 if (info
.filename_len
)
252 len
= min( info
.filename_len
, main_exe_size
-1 );
253 memcpy( main_exe_name
, src
, len
);
254 main_exe_name
[len
] = 0;
255 src
+= info
.filename_len
;
258 /* copy the other strings */
259 len
= info
.cmdline_len
+ info
.desktop_len
+ info
.title_len
;
260 if (len
&& (dst
= HeapAlloc( GetProcessHeap(), 0, len
+ 3 )))
262 current_envdb
.cmd_line
= copy_str( &dst
, &src
, info
.cmdline_len
);
263 current_startupinfo
.lpDesktop
= copy_str( &dst
, &src
, info
.desktop_len
);
264 current_startupinfo
.lpTitle
= copy_str( &dst
, &src
, info
.title_len
);
267 current_startupinfo
.dwX
= info
.x
;
268 current_startupinfo
.dwY
= info
.y
;
269 current_startupinfo
.dwXSize
= info
.cx
;
270 current_startupinfo
.dwYSize
= info
.cy
;
271 current_startupinfo
.dwXCountChars
= info
.x_chars
;
272 current_startupinfo
.dwYCountChars
= info
.y_chars
;
273 current_startupinfo
.dwFillAttribute
= info
.attribute
;
274 current_startupinfo
.wShowWindow
= info
.cmd_show
;
275 current_startupinfo
.dwFlags
= info
.flags
;
277 HeapFree( GetProcessHeap(), 0, data
);
278 return ¤t_envdb
;
283 /***********************************************************************
286 * Set the Wine library argc/argv global variables.
288 static void set_library_argv( char **argv
)
295 for (argc
= 0; argv
[argc
]; argc
++)
296 total
+= MultiByteToWideChar( CP_ACP
, 0, argv
[argc
], -1, NULL
, 0 );
298 wargv
= HeapAlloc( GetProcessHeap(), 0,
299 total
* sizeof(WCHAR
) + (argc
+ 1) * sizeof(*wargv
) );
300 p
= (WCHAR
*)(wargv
+ argc
+ 1);
301 for (argc
= 0; argv
[argc
]; argc
++)
303 DWORD len
= MultiByteToWideChar( CP_ACP
, 0, argv
[argc
], -1, p
, total
);
310 __wine_main_argc
= argc
;
311 __wine_main_argv
= argv
;
312 __wine_main_wargv
= wargv
;
316 /***********************************************************************
317 * ENV_BuildCommandLine
319 * Build the command line of a process from the argv array.
321 * Note that it does NOT necessarily include the file name.
322 * Sometimes we don't even have any command line options at all.
324 * We must quote and escape characters so that the argv array can be rebuilt
325 * from the command line:
326 * - spaces and tabs must be quoted
328 * - quotes must be escaped
330 * - if '\'s are followed by a '"', they must be doubled and followed by '\"',
331 * resulting in an odd number of '\' followed by a '"'
334 * - '\'s that are not followed by a '"' can be left as is
338 BOOL
ENV_BuildCommandLine( char **argv
)
343 set_library_argv( argv
);
345 if (current_envdb
.cmd_line
) goto done
; /* already got it from the server */
348 for (arg
= argv
; *arg
; arg
++)
350 int has_space
,bcount
;
360 if (*a
==' ' || *a
=='\t') {
362 } else if (*a
=='"') {
363 /* doubling of '\' preceeding a '"',
364 * plus escaping of said '"'
372 len
+=(a
-*arg
)+1 /* for the separating space */;
374 len
+=2; /* for the quotes */
377 if (!(current_envdb
.cmd_line
= HeapAlloc( GetProcessHeap(), 0, len
)))
380 p
= current_envdb
.cmd_line
;
381 for (arg
= argv
; *arg
; arg
++)
383 int has_space
,has_quote
;
386 /* Check for quotes and spaces in this argument */
387 has_space
=has_quote
=0;
390 if (*a
==' ' || *a
=='\t') {
394 } else if (*a
=='"') {
402 /* Now transfer it to the command line */
419 /* Double all the '\\' preceeding this '"', plus one */
420 for (i
=0;i
<=bcount
;i
++)
438 if (p
> current_envdb
.cmd_line
)
439 p
--; /* remove last space */
442 /* now allocate the Unicode version */
444 len
= MultiByteToWideChar( CP_ACP
, 0, current_envdb
.cmd_line
, -1, NULL
, 0 );
445 if (!(cmdlineW
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) )))
447 MultiByteToWideChar( CP_ACP
, 0, current_envdb
.cmd_line
, -1, cmdlineW
, len
);
452 /***********************************************************************
453 * GetCommandLineA (KERNEL32.@)
455 * WARNING: there's a Windows incompatibility lurking here !
456 * Win32s always includes the full path of the program file,
457 * whereas Windows NT only returns the full file path plus arguments
458 * in case the program has been started with a full path.
459 * Win9x seems to have inherited NT behaviour.
461 * Note that both Start Menu Execute and Explorer start programs with
462 * fully specified quoted app file paths, which is why probably the only case
463 * where you'll see single file names is in case of direct launch
464 * via CreateProcess or WinExec.
466 * Perhaps we should take care of Win3.1 programs here (Win32s "feature").
468 * References: MS KB article q102762.txt (special Win32s handling)
470 LPSTR WINAPI
GetCommandLineA(void)
472 return current_envdb
.cmd_line
;
475 /***********************************************************************
476 * GetCommandLineW (KERNEL32.@)
478 LPWSTR WINAPI
GetCommandLineW(void)
484 /***********************************************************************
485 * GetEnvironmentStrings (KERNEL32.@)
486 * GetEnvironmentStringsA (KERNEL32.@)
488 LPSTR WINAPI
GetEnvironmentStringsA(void)
490 return current_envdb
.env
;
494 /***********************************************************************
495 * GetEnvironmentStringsW (KERNEL32.@)
497 LPWSTR WINAPI
GetEnvironmentStringsW(void)
503 size
= HeapSize( GetProcessHeap(), 0, current_envdb
.env
);
504 if ((ret
= HeapAlloc( GetProcessHeap(), 0, size
* sizeof(WCHAR
) )) != NULL
)
506 LPSTR pA
= current_envdb
.env
;
508 while (size
--) *pW
++ = (WCHAR
)(BYTE
)*pA
++;
515 /***********************************************************************
516 * FreeEnvironmentStringsA (KERNEL32.@)
518 BOOL WINAPI
FreeEnvironmentStringsA( LPSTR ptr
)
520 if (ptr
!= current_envdb
.env
)
522 SetLastError( ERROR_INVALID_PARAMETER
);
529 /***********************************************************************
530 * FreeEnvironmentStringsW (KERNEL32.@)
532 BOOL WINAPI
FreeEnvironmentStringsW( LPWSTR ptr
)
534 return HeapFree( GetProcessHeap(), 0, ptr
);
538 /***********************************************************************
539 * GetEnvironmentVariableA (KERNEL32.@)
541 DWORD WINAPI
GetEnvironmentVariableA( LPCSTR name
, LPSTR value
, DWORD size
)
548 SetLastError( ERROR_INVALID_PARAMETER
);
552 if ((p
= ENV_FindVariable( current_envdb
.env
, name
, strlen(name
) )))
557 /* If not enough room, include the terminating null
558 * in the returned size and return an empty string */
560 if (value
) *value
= '\0';
562 else if (value
) strcpy( value
, p
);
566 SetLastError( ERROR_ENVVAR_NOT_FOUND
);
571 /***********************************************************************
572 * GetEnvironmentVariableW (KERNEL32.@)
574 DWORD WINAPI
GetEnvironmentVariableW( LPCWSTR nameW
, LPWSTR valW
, DWORD size
)
576 LPSTR name
= HEAP_strdupWtoA( GetProcessHeap(), 0, nameW
);
577 LPSTR val
= valW
? HeapAlloc( GetProcessHeap(), 0, size
) : NULL
;
578 DWORD res
= GetEnvironmentVariableA( name
, val
, size
);
579 HeapFree( GetProcessHeap(), 0, name
);
582 if (size
> 0 && !MultiByteToWideChar( CP_ACP
, 0, val
, -1, valW
, size
))
584 HeapFree( GetProcessHeap(), 0, val
);
590 /***********************************************************************
591 * SetEnvironmentVariableA (KERNEL32.@)
593 BOOL WINAPI
SetEnvironmentVariableA( LPCSTR name
, LPCSTR value
)
595 INT old_size
, len
, res
;
596 LPSTR p
, env
, new_env
;
600 env
= p
= current_envdb
.env
;
602 /* Find a place to insert the string */
608 if (!strncasecmp( name
, p
, len
) && (p
[len
] == '=')) break;
611 if (!value
&& !*p
) goto done
; /* Value to remove doesn't exist */
613 /* Realloc the buffer */
615 len
= value
? strlen(name
) + strlen(value
) + 2 : 0;
616 if (*p
) len
-= strlen(p
) + 1; /* The name already exists */
617 old_size
= HeapSize( GetProcessHeap(), 0, env
);
620 LPSTR next
= p
+ strlen(p
) + 1; /* We know there is a next one */
621 memmove( next
+ len
, next
, old_size
- (next
- env
) );
623 if (!(new_env
= HeapReAlloc( GetProcessHeap(), 0, env
, old_size
+ len
)))
625 if (env_sel
) env_sel
= SELECTOR_ReallocBlock( env_sel
, new_env
, old_size
+ len
);
626 p
= new_env
+ (p
- env
);
627 if (len
> 0) memmove( p
+ len
, p
, old_size
- (p
- new_env
) );
629 /* Set the new string */
637 current_envdb
.env
= new_env
;
646 /***********************************************************************
647 * SetEnvironmentVariableW (KERNEL32.@)
649 BOOL WINAPI
SetEnvironmentVariableW( LPCWSTR name
, LPCWSTR value
)
651 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
652 LPSTR valueA
= HEAP_strdupWtoA( GetProcessHeap(), 0, value
);
653 BOOL ret
= SetEnvironmentVariableA( nameA
, valueA
);
654 HeapFree( GetProcessHeap(), 0, nameA
);
655 HeapFree( GetProcessHeap(), 0, valueA
);
660 /***********************************************************************
661 * ExpandEnvironmentStringsA (KERNEL32.@)
663 * Note: overlapping buffers are not supported; this is how it should be.
665 DWORD WINAPI
ExpandEnvironmentStringsA( LPCSTR src
, LPSTR dst
, DWORD count
)
667 DWORD len
, total_size
= 1; /* 1 for terminating '\0' */
670 if (!count
) dst
= NULL
;
677 if ((p
= strchr( src
, '%' ))) len
= p
- src
;
678 else len
= strlen(src
);
682 else /* we are at the start of a variable */
684 if ((p
= strchr( src
+ 1, '%' )))
686 len
= p
- src
- 1; /* Length of the variable name */
687 if ((var
= ENV_FindVariable( current_envdb
.env
, src
+ 1, len
)))
689 src
+= len
+ 2; /* Skip the variable name */
694 var
= src
; /* Copy original name instead */
699 else /* unfinished variable name, ignore it */
702 len
= strlen(src
); /* Copy whole string */
709 if (count
< len
) len
= count
;
710 memcpy( dst
, var
, len
);
717 /* Null-terminate the string */
727 /***********************************************************************
728 * ExpandEnvironmentStringsW (KERNEL32.@)
730 DWORD WINAPI
ExpandEnvironmentStringsW( LPCWSTR src
, LPWSTR dst
, DWORD len
)
732 LPSTR srcA
= HEAP_strdupWtoA( GetProcessHeap(), 0, src
);
733 LPSTR dstA
= dst
? HeapAlloc( GetProcessHeap(), 0, len
) : NULL
;
734 DWORD ret
= ExpandEnvironmentStringsA( srcA
, dstA
, len
);
737 ret
= MultiByteToWideChar( CP_ACP
, 0, dstA
, -1, dst
, len
);
738 HeapFree( GetProcessHeap(), 0, dstA
);
740 HeapFree( GetProcessHeap(), 0, srcA
);
745 /***********************************************************************
746 * GetDOSEnvironment (KERNEL.131)
747 * GetDOSEnvironment16 (KERNEL32.@)
749 SEGPTR WINAPI
GetDOSEnvironment16(void)
751 return MAKESEGPTR( env_sel
, 0 );
755 /***********************************************************************
756 * GetStdHandle (KERNEL32.@)
758 HANDLE WINAPI
GetStdHandle( DWORD std_handle
)
762 case STD_INPUT_HANDLE
: return current_envdb
.hStdin
;
763 case STD_OUTPUT_HANDLE
: return current_envdb
.hStdout
;
764 case STD_ERROR_HANDLE
: return current_envdb
.hStderr
;
766 SetLastError( ERROR_INVALID_PARAMETER
);
767 return INVALID_HANDLE_VALUE
;
771 /***********************************************************************
772 * SetStdHandle (KERNEL32.@)
774 BOOL WINAPI
SetStdHandle( DWORD std_handle
, HANDLE handle
)
778 case STD_INPUT_HANDLE
: current_envdb
.hStdin
= handle
; return TRUE
;
779 case STD_OUTPUT_HANDLE
: current_envdb
.hStdout
= handle
; return TRUE
;
780 case STD_ERROR_HANDLE
: current_envdb
.hStderr
= handle
; return TRUE
;
782 SetLastError( ERROR_INVALID_PARAMETER
);
787 /***********************************************************************
788 * GetStartupInfoA (KERNEL32.@)
790 VOID WINAPI
GetStartupInfoA( LPSTARTUPINFOA info
)
792 *info
= current_startupinfo
;
796 /***********************************************************************
797 * GetStartupInfoW (KERNEL32.@)
799 VOID WINAPI
GetStartupInfoW( LPSTARTUPINFOW info
)
801 info
->cb
= sizeof(STARTUPINFOW
);
802 info
->dwX
= current_startupinfo
.dwX
;
803 info
->dwY
= current_startupinfo
.dwY
;
804 info
->dwXSize
= current_startupinfo
.dwXSize
;
805 info
->dwXCountChars
= current_startupinfo
.dwXCountChars
;
806 info
->dwYCountChars
= current_startupinfo
.dwYCountChars
;
807 info
->dwFillAttribute
= current_startupinfo
.dwFillAttribute
;
808 info
->dwFlags
= current_startupinfo
.dwFlags
;
809 info
->wShowWindow
= current_startupinfo
.wShowWindow
;
810 info
->cbReserved2
= current_startupinfo
.cbReserved2
;
811 info
->lpReserved2
= current_startupinfo
.lpReserved2
;
812 info
->hStdInput
= current_startupinfo
.hStdInput
;
813 info
->hStdOutput
= current_startupinfo
.hStdOutput
;
814 info
->hStdError
= current_startupinfo
.hStdError
;
815 info
->lpReserved
= HEAP_strdupAtoW (GetProcessHeap(), 0, current_startupinfo
.lpReserved
);
816 info
->lpDesktop
= HEAP_strdupAtoW (GetProcessHeap(), 0, current_startupinfo
.lpDesktop
);
817 info
->lpTitle
= HEAP_strdupAtoW (GetProcessHeap(), 0, current_startupinfo
.lpTitle
);