wine.inf: We should not override existing associations.
[wine/hacks.git] / dlls / kernel / environ.c
blob488752553ccaf2a47e1ff5c28c8acc8838c4f5c1
1 /*
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <stdarg.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <assert.h>
29 #include "ntstatus.h"
30 #define WIN32_NO_STATUS
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winerror.h"
34 #include "wine/library.h"
35 #include "winternl.h"
36 #include "wine/unicode.h"
37 #include "wine/debug.h"
39 #include "kernel_private.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(environ);
43 /* Notes:
44 * - contrary to Microsoft docs, the environment strings do not appear
45 * to be sorted on Win95 (although they are on NT); so we don't bother
46 * to sort them either.
49 static STARTUPINFOW startup_infoW;
50 static STARTUPINFOA startup_infoA;
53 /***********************************************************************
54 * GetCommandLineA (KERNEL32.@)
56 * WARNING: there's a Windows incompatibility lurking here !
57 * Win32s always includes the full path of the program file,
58 * whereas Windows NT only returns the full file path plus arguments
59 * in case the program has been started with a full path.
60 * Win9x seems to have inherited NT behaviour.
62 * Note that both Start Menu Execute and Explorer start programs with
63 * fully specified quoted app file paths, which is why probably the only case
64 * where you'll see single file names is in case of direct launch
65 * via CreateProcess or WinExec.
67 * Perhaps we should take care of Win3.1 programs here (Win32s "feature").
69 * References: MS KB article q102762.txt (special Win32s handling)
71 LPSTR WINAPI GetCommandLineA(void)
73 static char *cmdlineA; /* ASCII command line */
75 if (!cmdlineA) /* make an ansi version if we don't have it */
77 ANSI_STRING ansi;
78 RtlAcquirePebLock();
80 cmdlineA = (RtlUnicodeStringToAnsiString( &ansi, &NtCurrentTeb()->Peb->ProcessParameters->CommandLine, TRUE) == STATUS_SUCCESS) ?
81 ansi.Buffer : NULL;
82 RtlReleasePebLock();
84 return cmdlineA;
87 /***********************************************************************
88 * GetCommandLineW (KERNEL32.@)
90 LPWSTR WINAPI GetCommandLineW(void)
92 return NtCurrentTeb()->Peb->ProcessParameters->CommandLine.Buffer;
96 /***********************************************************************
97 * GetEnvironmentStringsA (KERNEL32.@)
98 * GetEnvironmentStrings (KERNEL32.@)
100 LPSTR WINAPI GetEnvironmentStringsA(void)
102 LPWSTR ptrW;
103 unsigned len, slen;
104 LPSTR ret, ptrA;
106 RtlAcquirePebLock();
108 len = 1;
110 ptrW = NtCurrentTeb()->Peb->ProcessParameters->Environment;
111 while (*ptrW)
113 slen = strlenW(ptrW) + 1;
114 len += WideCharToMultiByte( CP_ACP, 0, ptrW, slen, NULL, 0, NULL, NULL );
115 ptrW += slen;
118 if ((ret = HeapAlloc( GetProcessHeap(), 0, len )) != NULL)
120 ptrW = NtCurrentTeb()->Peb->ProcessParameters->Environment;
121 ptrA = ret;
122 while (*ptrW)
124 slen = strlenW(ptrW) + 1;
125 WideCharToMultiByte( CP_ACP, 0, ptrW, slen, ptrA, len, NULL, NULL );
126 ptrW += slen;
127 ptrA += strlen(ptrA) + 1;
129 *ptrA = 0;
132 RtlReleasePebLock();
133 return ret;
137 /***********************************************************************
138 * GetEnvironmentStringsW (KERNEL32.@)
140 LPWSTR WINAPI GetEnvironmentStringsW(void)
142 return NtCurrentTeb()->Peb->ProcessParameters->Environment;
146 /***********************************************************************
147 * FreeEnvironmentStringsA (KERNEL32.@)
149 BOOL WINAPI FreeEnvironmentStringsA( LPSTR ptr )
151 return HeapFree( GetProcessHeap(), 0, ptr );
155 /***********************************************************************
156 * FreeEnvironmentStringsW (KERNEL32.@)
158 BOOL WINAPI FreeEnvironmentStringsW( LPWSTR ptr )
160 return TRUE;
164 /***********************************************************************
165 * GetEnvironmentVariableA (KERNEL32.@)
167 DWORD WINAPI GetEnvironmentVariableA( LPCSTR name, LPSTR value, DWORD size )
169 UNICODE_STRING us_name;
170 PWSTR valueW;
171 DWORD ret;
173 if (!name || !*name)
175 SetLastError(ERROR_ENVVAR_NOT_FOUND);
176 return 0;
179 if (!(valueW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR))))
180 return 0;
182 RtlCreateUnicodeStringFromAsciiz( &us_name, name );
183 SetLastError(0);
184 ret = GetEnvironmentVariableW( us_name.Buffer, valueW, size);
185 if (ret && ret < size)
187 WideCharToMultiByte( CP_ACP, 0, valueW, ret + 1, value, size, NULL, NULL );
189 /* this is needed to tell, with 0 as a return value, the difference between:
190 * - an error (GetLastError() != 0)
191 * - returning an empty string (in this case, we need to update the buffer)
193 if (ret == 0 && size && GetLastError() == 0)
194 value[0] = '\0';
196 RtlFreeUnicodeString( &us_name );
197 HeapFree(GetProcessHeap(), 0, valueW);
199 return ret;
203 /***********************************************************************
204 * GetEnvironmentVariableW (KERNEL32.@)
206 DWORD WINAPI GetEnvironmentVariableW( LPCWSTR name, LPWSTR val, DWORD size )
208 UNICODE_STRING us_name;
209 UNICODE_STRING us_value;
210 NTSTATUS status;
211 unsigned len;
213 TRACE("(%s %p %lu)\n", debugstr_w(name), val, size);
215 if (!name || !*name)
217 SetLastError(ERROR_ENVVAR_NOT_FOUND);
218 return 0;
221 RtlInitUnicodeString(&us_name, name);
222 us_value.Length = 0;
223 us_value.MaximumLength = (size ? size - 1 : 0) * sizeof(WCHAR);
224 us_value.Buffer = val;
226 status = RtlQueryEnvironmentVariable_U(NULL, &us_name, &us_value);
227 len = us_value.Length / sizeof(WCHAR);
228 if (status != STATUS_SUCCESS)
230 SetLastError( RtlNtStatusToDosError(status) );
231 return (status == STATUS_BUFFER_TOO_SMALL) ? len + 1 : 0;
233 if (size) val[len] = '\0';
235 return us_value.Length / sizeof(WCHAR);
239 /***********************************************************************
240 * SetEnvironmentVariableA (KERNEL32.@)
242 BOOL WINAPI SetEnvironmentVariableA( LPCSTR name, LPCSTR value )
244 UNICODE_STRING us_name;
245 BOOL ret;
247 if (!name)
249 SetLastError(ERROR_ENVVAR_NOT_FOUND);
250 return 0;
253 RtlCreateUnicodeStringFromAsciiz( &us_name, name );
254 if (value)
256 UNICODE_STRING us_value;
258 RtlCreateUnicodeStringFromAsciiz( &us_value, value );
259 ret = SetEnvironmentVariableW( us_name.Buffer, us_value.Buffer );
260 RtlFreeUnicodeString( &us_value );
262 else ret = SetEnvironmentVariableW( us_name.Buffer, NULL );
264 RtlFreeUnicodeString( &us_name );
266 return ret;
270 /***********************************************************************
271 * SetEnvironmentVariableW (KERNEL32.@)
273 BOOL WINAPI SetEnvironmentVariableW( LPCWSTR name, LPCWSTR value )
275 UNICODE_STRING us_name;
276 NTSTATUS status;
278 TRACE("(%s %s)\n", debugstr_w(name), debugstr_w(value));
280 if (!name)
282 SetLastError(ERROR_ENVVAR_NOT_FOUND);
283 return 0;
286 RtlInitUnicodeString(&us_name, name);
287 if (value)
289 UNICODE_STRING us_value;
291 RtlInitUnicodeString(&us_value, value);
292 status = RtlSetEnvironmentVariable(NULL, &us_name, &us_value);
294 else status = RtlSetEnvironmentVariable(NULL, &us_name, NULL);
296 if (status != STATUS_SUCCESS)
298 SetLastError( RtlNtStatusToDosError(status) );
299 return FALSE;
301 return TRUE;
305 /***********************************************************************
306 * ExpandEnvironmentStringsA (KERNEL32.@)
308 * Note: overlapping buffers are not supported; this is how it should be.
309 * FIXME: return value is wrong for MBCS
311 DWORD WINAPI ExpandEnvironmentStringsA( LPCSTR src, LPSTR dst, DWORD count )
313 UNICODE_STRING us_src;
314 PWSTR dstW = NULL;
315 DWORD ret;
317 RtlCreateUnicodeStringFromAsciiz( &us_src, src );
318 if (count)
320 if (!(dstW = HeapAlloc(GetProcessHeap(), 0, count * sizeof(WCHAR))))
321 return 0;
322 ret = ExpandEnvironmentStringsW( us_src.Buffer, dstW, count);
323 if (ret)
324 WideCharToMultiByte( CP_ACP, 0, dstW, ret, dst, count, NULL, NULL );
326 else ret = ExpandEnvironmentStringsW( us_src.Buffer, NULL, 0);
328 RtlFreeUnicodeString( &us_src );
329 HeapFree(GetProcessHeap(), 0, dstW);
331 return ret;
335 /***********************************************************************
336 * ExpandEnvironmentStringsW (KERNEL32.@)
338 DWORD WINAPI ExpandEnvironmentStringsW( LPCWSTR src, LPWSTR dst, DWORD len )
340 UNICODE_STRING us_src;
341 UNICODE_STRING us_dst;
342 NTSTATUS status;
343 DWORD res;
345 TRACE("(%s %p %lu)\n", debugstr_w(src), dst, len);
347 RtlInitUnicodeString(&us_src, src);
348 us_dst.Length = 0;
349 us_dst.MaximumLength = len * sizeof(WCHAR);
350 us_dst.Buffer = dst;
352 res = 0;
353 status = RtlExpandEnvironmentStrings_U(NULL, &us_src, &us_dst, &res);
354 res /= sizeof(WCHAR);
355 if (status != STATUS_SUCCESS)
357 SetLastError( RtlNtStatusToDosError(status) );
358 if (status != STATUS_BUFFER_TOO_SMALL) return 0;
359 if (len && dst) dst[len - 1] = '\0';
362 return res;
366 /***********************************************************************
367 * GetStdHandle (KERNEL32.@)
369 HANDLE WINAPI GetStdHandle( DWORD std_handle )
371 switch (std_handle)
373 case STD_INPUT_HANDLE: return NtCurrentTeb()->Peb->ProcessParameters->hStdInput;
374 case STD_OUTPUT_HANDLE: return NtCurrentTeb()->Peb->ProcessParameters->hStdOutput;
375 case STD_ERROR_HANDLE: return NtCurrentTeb()->Peb->ProcessParameters->hStdError;
377 SetLastError( ERROR_INVALID_PARAMETER );
378 return INVALID_HANDLE_VALUE;
382 /***********************************************************************
383 * SetStdHandle (KERNEL32.@)
385 BOOL WINAPI SetStdHandle( DWORD std_handle, HANDLE handle )
387 switch (std_handle)
389 case STD_INPUT_HANDLE: NtCurrentTeb()->Peb->ProcessParameters->hStdInput = handle; return TRUE;
390 case STD_OUTPUT_HANDLE: NtCurrentTeb()->Peb->ProcessParameters->hStdOutput = handle; return TRUE;
391 case STD_ERROR_HANDLE: NtCurrentTeb()->Peb->ProcessParameters->hStdError = handle; return TRUE;
393 SetLastError( ERROR_INVALID_PARAMETER );
394 return FALSE;
397 /***********************************************************************
398 * GetStartupInfoA (KERNEL32.@)
400 VOID WINAPI GetStartupInfoA( LPSTARTUPINFOA info )
402 memcpy(info, &startup_infoA, sizeof(startup_infoA));
406 /***********************************************************************
407 * GetStartupInfoW (KERNEL32.@)
409 VOID WINAPI GetStartupInfoW( LPSTARTUPINFOW info )
411 memcpy(info, &startup_infoW, sizeof(startup_infoW));
414 /******************************************************************
415 * ENV_CopyStartupInformation (internal)
417 * Creates the STARTUPINFO information from the ntdll information
419 void ENV_CopyStartupInformation(void)
421 RTL_USER_PROCESS_PARAMETERS* rupp;
422 ANSI_STRING ansi;
424 RtlAcquirePebLock();
426 rupp = NtCurrentTeb()->Peb->ProcessParameters;
428 startup_infoW.cb = sizeof(startup_infoW);
429 startup_infoW.lpReserved = NULL;
430 startup_infoW.lpDesktop = rupp->Desktop.Buffer;
431 startup_infoW.lpTitle = rupp->WindowTitle.Buffer;
432 startup_infoW.dwX = rupp->dwX;
433 startup_infoW.dwY = rupp->dwY;
434 startup_infoW.dwXSize = rupp->dwXSize;
435 startup_infoW.dwYSize = rupp->dwYSize;
436 startup_infoW.dwXCountChars = rupp->dwXCountChars;
437 startup_infoW.dwYCountChars = rupp->dwYCountChars;
438 startup_infoW.dwFillAttribute = rupp->dwFillAttribute;
439 startup_infoW.dwFlags = rupp->dwFlags;
440 startup_infoW.wShowWindow = rupp->wShowWindow;
441 startup_infoW.cbReserved2 = rupp->RuntimeInfo.MaximumLength;
442 startup_infoW.lpReserved2 = rupp->RuntimeInfo.MaximumLength ? (void*)rupp->RuntimeInfo.Buffer : NULL;
443 startup_infoW.hStdInput = rupp->hStdInput;
444 startup_infoW.hStdOutput = rupp->hStdOutput;
445 startup_infoW.hStdError = rupp->hStdError;
447 startup_infoA.cb = sizeof(startup_infoA);
448 startup_infoA.lpReserved = NULL;
449 startup_infoA.lpDesktop = (rupp->Desktop.Length &&
450 RtlUnicodeStringToAnsiString( &ansi, &rupp->Desktop, TRUE) == STATUS_SUCCESS) ?
451 ansi.Buffer : NULL;
452 startup_infoA.lpTitle = (rupp->WindowTitle.Length &&
453 RtlUnicodeStringToAnsiString( &ansi, &rupp->WindowTitle, TRUE) == STATUS_SUCCESS) ?
454 ansi.Buffer : NULL;
455 startup_infoA.dwX = rupp->dwX;
456 startup_infoA.dwY = rupp->dwY;
457 startup_infoA.dwXSize = rupp->dwXSize;
458 startup_infoA.dwYSize = rupp->dwYSize;
459 startup_infoA.dwXCountChars = rupp->dwXCountChars;
460 startup_infoA.dwYCountChars = rupp->dwYCountChars;
461 startup_infoA.dwFillAttribute = rupp->dwFillAttribute;
462 startup_infoA.dwFlags = rupp->dwFlags;
463 startup_infoA.wShowWindow = rupp->wShowWindow;
464 startup_infoA.cbReserved2 = rupp->RuntimeInfo.MaximumLength;
465 startup_infoA.lpReserved2 = rupp->RuntimeInfo.MaximumLength ? (void*)rupp->RuntimeInfo.Buffer : NULL;
466 startup_infoA.hStdInput = rupp->hStdInput;
467 startup_infoA.hStdOutput = rupp->hStdOutput;
468 startup_infoA.hStdError = rupp->hStdError;
470 RtlReleasePebLock();