kernel32: VerifyConsoleIoHandle does not set last error value.
[wine.git] / dlls / ntdll / env.c
blob91ad6bcbcfd5c2cd3f446c6083d2b89ebce828e6
1 /*
2 * Ntdll environment functions
4 * Copyright 1996, 1998 Alexandre Julliard
5 * Copyright 2003 Eric Pouech
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
23 #include <assert.h>
24 #include <stdarg.h>
26 #include "ntstatus.h"
27 #define WIN32_NO_STATUS
28 #include "windef.h"
29 #include "winternl.h"
30 #include "wine/unicode.h"
31 #include "wine/debug.h"
32 #include "ntdll_misc.h"
33 #include "winnt.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(environ);
37 /******************************************************************************
38 * NtQuerySystemEnvironmentValue [NTDLL.@]
40 NTSYSAPI NTSTATUS WINAPI NtQuerySystemEnvironmentValue(PUNICODE_STRING VariableName,
41 PWCHAR Value,
42 ULONG ValueBufferLength,
43 PULONG RequiredLength)
45 FIXME("stub!\n");
46 return STATUS_NOT_IMPLEMENTED;
49 /******************************************************************************
50 * RtlCreateEnvironment [NTDLL.@]
52 NTSTATUS WINAPI RtlCreateEnvironment(BOOLEAN inherit, PWSTR* env)
54 NTSTATUS nts;
56 TRACE("(%u,%p)!\n", inherit, env);
58 if (inherit)
60 MEMORY_BASIC_INFORMATION mbi;
62 RtlAcquirePebLock();
64 nts = NtQueryVirtualMemory(NtCurrentProcess(),
65 NtCurrentTeb()->Peb->ProcessParameters->Environment,
66 0, &mbi, sizeof(mbi), NULL);
67 if (nts == STATUS_SUCCESS)
69 *env = NULL;
70 nts = NtAllocateVirtualMemory(NtCurrentProcess(), (void**)env, 0, &mbi.RegionSize,
71 MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
72 if (nts == STATUS_SUCCESS)
73 memcpy(*env, NtCurrentTeb()->Peb->ProcessParameters->Environment, mbi.RegionSize);
74 else *env = NULL;
76 RtlReleasePebLock();
78 else
80 SIZE_T size = 1;
81 PVOID addr = NULL;
82 nts = NtAllocateVirtualMemory(NtCurrentProcess(), &addr, 0, &size,
83 MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
84 if (nts == STATUS_SUCCESS) *env = addr;
87 return nts;
90 /******************************************************************************
91 * RtlDestroyEnvironment [NTDLL.@]
93 NTSTATUS WINAPI RtlDestroyEnvironment(PWSTR env)
95 SIZE_T size = 0;
97 TRACE("(%p)!\n", env);
99 return NtFreeVirtualMemory(NtCurrentProcess(), (void**)&env, &size, MEM_RELEASE);
102 static LPCWSTR ENV_FindVariable(PCWSTR var, PCWSTR name, unsigned namelen)
104 for (; *var; var += strlenW(var) + 1)
106 /* match var names, but avoid setting a var with a name including a '='
107 * (a starting '=' is valid though)
109 if (strncmpiW(var, name, namelen) == 0 && var[namelen] == '=' &&
110 strchrW(var + 1, '=') == var + namelen)
112 return var + namelen + 1;
115 return NULL;
118 /******************************************************************
119 * RtlQueryEnvironmentVariable_U [NTDLL.@]
121 * NOTES: when the buffer is too small, the string is not written, but if the
122 * terminating null char is the only char that cannot be written, then
123 * all chars (except the null) are written and success is returned
124 * (behavior of Win2k at least)
126 NTSTATUS WINAPI RtlQueryEnvironmentVariable_U(PWSTR env,
127 PUNICODE_STRING name,
128 PUNICODE_STRING value)
130 NTSTATUS nts = STATUS_VARIABLE_NOT_FOUND;
131 PCWSTR var;
132 unsigned namelen;
134 TRACE("%p %s %p\n", env, debugstr_us(name), value);
136 value->Length = 0;
137 namelen = name->Length / sizeof(WCHAR);
138 if (!namelen) return nts;
140 if (!env)
142 RtlAcquirePebLock();
143 var = NtCurrentTeb()->Peb->ProcessParameters->Environment;
145 else var = env;
147 var = ENV_FindVariable(var, name->Buffer, namelen);
148 if (var != NULL)
150 value->Length = strlenW(var) * sizeof(WCHAR);
152 if (value->Length <= value->MaximumLength)
154 memmove(value->Buffer, var, min(value->Length + sizeof(WCHAR), value->MaximumLength));
155 nts = STATUS_SUCCESS;
157 else nts = STATUS_BUFFER_TOO_SMALL;
160 if (!env) RtlReleasePebLock();
162 return nts;
165 /******************************************************************
166 * RtlSetCurrentEnvironment [NTDLL.@]
169 void WINAPI RtlSetCurrentEnvironment(PWSTR new_env, PWSTR* old_env)
171 TRACE("(%p %p)\n", new_env, old_env);
173 RtlAcquirePebLock();
175 if (old_env) *old_env = NtCurrentTeb()->Peb->ProcessParameters->Environment;
176 NtCurrentTeb()->Peb->ProcessParameters->Environment = new_env;
178 RtlReleasePebLock();
182 /******************************************************************************
183 * RtlSetEnvironmentVariable [NTDLL.@]
185 NTSTATUS WINAPI RtlSetEnvironmentVariable(PWSTR* penv, PUNICODE_STRING name,
186 PUNICODE_STRING value)
188 INT len, old_size;
189 LPWSTR p, env;
190 NTSTATUS nts = STATUS_VARIABLE_NOT_FOUND;
191 MEMORY_BASIC_INFORMATION mbi;
193 TRACE("(%p, %s, %s)\n", penv, debugstr_us(name), debugstr_us(value));
195 if (!name || !name->Buffer || !name->Length)
196 return STATUS_INVALID_PARAMETER_1;
198 len = name->Length / sizeof(WCHAR);
200 /* variable names can't contain a '=' except as a first character */
201 for (p = name->Buffer + 1; p < name->Buffer + len; p++)
202 if (*p == '=') return STATUS_INVALID_PARAMETER;
204 if (!penv)
206 RtlAcquirePebLock();
207 env = NtCurrentTeb()->Peb->ProcessParameters->Environment;
208 } else env = *penv;
210 /* compute current size of environment */
211 for (p = env; *p; p += strlenW(p) + 1);
212 old_size = p + 1 - env;
214 /* Find a place to insert the string */
215 for (p = env; *p; p += strlenW(p) + 1)
217 if (!strncmpiW(name->Buffer, p, len) && (p[len] == '=')) break;
219 if (!value && !*p) goto done; /* Value to remove doesn't exist */
221 /* Realloc the buffer */
222 len = value ? len + value->Length / sizeof(WCHAR) + 2 : 0;
223 if (*p) len -= strlenW(p) + 1; /* The name already exists */
225 if (len < 0)
227 LPWSTR next = p + strlenW(p) + 1; /* We know there is a next one */
228 memmove(next + len, next, (old_size - (next - env)) * sizeof(WCHAR));
231 nts = NtQueryVirtualMemory(NtCurrentProcess(), env, 0,
232 &mbi, sizeof(mbi), NULL);
233 if (nts != STATUS_SUCCESS) goto done;
235 if ((old_size + len) * sizeof(WCHAR) > mbi.RegionSize)
237 LPWSTR new_env;
238 SIZE_T new_size = (old_size + len) * sizeof(WCHAR);
240 new_env = NULL;
241 nts = NtAllocateVirtualMemory(NtCurrentProcess(), (void**)&new_env, 0,
242 &new_size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
243 if (nts != STATUS_SUCCESS) goto done;
245 memmove(new_env, env, (p - env) * sizeof(WCHAR));
246 assert(len > 0);
247 memmove(new_env + (p - env) + len, p, (old_size - (p - env)) * sizeof(WCHAR));
248 p = new_env + (p - env);
250 RtlDestroyEnvironment(env);
251 if (!penv) NtCurrentTeb()->Peb->ProcessParameters->Environment = new_env;
252 else *penv = new_env;
254 else
256 if (len > 0) memmove(p + len, p, (old_size - (p - env)) * sizeof(WCHAR));
259 /* Set the new string */
260 if (value)
262 memcpy( p, name->Buffer, name->Length );
263 p += name->Length / sizeof(WCHAR);
264 *p++ = '=';
265 memcpy( p, value->Buffer, value->Length );
266 p[value->Length / sizeof(WCHAR)] = 0;
268 done:
269 if (!penv) RtlReleasePebLock();
271 return nts;
274 /******************************************************************
275 * RtlExpandEnvironmentStrings_U (NTDLL.@)
278 NTSTATUS WINAPI RtlExpandEnvironmentStrings_U(PCWSTR renv, const UNICODE_STRING* us_src,
279 PUNICODE_STRING us_dst, PULONG plen)
281 DWORD src_len, len, count, total_size = 1; /* 1 for terminating '\0' */
282 LPCWSTR env, src, p, var;
283 LPWSTR dst;
285 src = us_src->Buffer;
286 src_len = us_src->Length / sizeof(WCHAR);
287 count = us_dst->MaximumLength / sizeof(WCHAR);
288 dst = count ? us_dst->Buffer : NULL;
290 if (!renv)
292 RtlAcquirePebLock();
293 env = NtCurrentTeb()->Peb->ProcessParameters->Environment;
295 else env = renv;
297 while (src_len)
299 if (*src != '%')
301 if ((p = memchrW( src, '%', src_len ))) len = p - src;
302 else len = src_len;
303 var = src;
304 src += len;
305 src_len -= len;
307 else /* we are at the start of a variable */
309 if ((p = memchrW( src + 1, '%', src_len - 1 )))
311 len = p - src - 1; /* Length of the variable name */
312 if ((var = ENV_FindVariable( env, src + 1, len )))
314 src += len + 2; /* Skip the variable name */
315 src_len -= len + 2;
316 len = strlenW(var);
318 else
320 var = src; /* Copy original name instead */
321 len += 2;
322 src += len;
323 src_len -= len;
326 else /* unfinished variable name, ignore it */
328 var = src;
329 len = src_len; /* Copy whole string */
330 src += len;
331 src_len = 0;
334 total_size += len;
335 if (dst)
337 if (count < len) len = count;
338 memcpy(dst, var, len * sizeof(WCHAR));
339 count -= len;
340 dst += len;
344 if (!renv) RtlReleasePebLock();
346 /* Null-terminate the string */
347 if (dst && count) *dst = '\0';
349 us_dst->Length = (dst) ? (dst - us_dst->Buffer) * sizeof(WCHAR) : 0;
350 if (plen) *plen = total_size * sizeof(WCHAR);
352 return (count) ? STATUS_SUCCESS : STATUS_BUFFER_TOO_SMALL;
356 static inline void normalize( void *base, WCHAR **ptr )
358 if (*ptr) *ptr = (WCHAR *)((char *)base + (UINT_PTR)*ptr);
361 /******************************************************************************
362 * RtlNormalizeProcessParams [NTDLL.@]
364 PRTL_USER_PROCESS_PARAMETERS WINAPI RtlNormalizeProcessParams( RTL_USER_PROCESS_PARAMETERS *params )
366 if (params && !(params->Flags & PROCESS_PARAMS_FLAG_NORMALIZED))
368 normalize( params, &params->CurrentDirectory.DosPath.Buffer );
369 normalize( params, &params->DllPath.Buffer );
370 normalize( params, &params->ImagePathName.Buffer );
371 normalize( params, &params->CommandLine.Buffer );
372 normalize( params, &params->WindowTitle.Buffer );
373 normalize( params, &params->Desktop.Buffer );
374 normalize( params, &params->ShellInfo.Buffer );
375 normalize( params, &params->RuntimeInfo.Buffer );
376 params->Flags |= PROCESS_PARAMS_FLAG_NORMALIZED;
378 return params;
382 static inline void denormalize( const void *base, WCHAR **ptr )
384 if (*ptr) *ptr = (WCHAR *)(UINT_PTR)((char *)*ptr - (const char *)base);
387 /******************************************************************************
388 * RtlDeNormalizeProcessParams [NTDLL.@]
390 PRTL_USER_PROCESS_PARAMETERS WINAPI RtlDeNormalizeProcessParams( RTL_USER_PROCESS_PARAMETERS *params )
392 if (params && (params->Flags & PROCESS_PARAMS_FLAG_NORMALIZED))
394 denormalize( params, &params->CurrentDirectory.DosPath.Buffer );
395 denormalize( params, &params->DllPath.Buffer );
396 denormalize( params, &params->ImagePathName.Buffer );
397 denormalize( params, &params->CommandLine.Buffer );
398 denormalize( params, &params->WindowTitle.Buffer );
399 denormalize( params, &params->Desktop.Buffer );
400 denormalize( params, &params->ShellInfo.Buffer );
401 denormalize( params, &params->RuntimeInfo.Buffer );
402 params->Flags &= ~PROCESS_PARAMS_FLAG_NORMALIZED;
404 return params;
408 /* append a unicode string to the process params data; helper for RtlCreateProcessParameters */
409 static void append_unicode_string( void **data, const UNICODE_STRING *src,
410 UNICODE_STRING *dst )
412 dst->Length = src->Length;
413 dst->MaximumLength = src->MaximumLength;
414 dst->Buffer = *data;
415 memcpy( dst->Buffer, src->Buffer, dst->MaximumLength );
416 *data = (char *)dst->Buffer + dst->MaximumLength;
420 /******************************************************************************
421 * RtlCreateProcessParameters [NTDLL.@]
423 NTSTATUS WINAPI RtlCreateProcessParameters( RTL_USER_PROCESS_PARAMETERS **result,
424 const UNICODE_STRING *ImagePathName,
425 const UNICODE_STRING *DllPath,
426 const UNICODE_STRING *CurrentDirectoryName,
427 const UNICODE_STRING *CommandLine,
428 PWSTR Environment,
429 const UNICODE_STRING *WindowTitle,
430 const UNICODE_STRING *Desktop,
431 const UNICODE_STRING *ShellInfo,
432 const UNICODE_STRING *RuntimeInfo )
434 static WCHAR empty[] = {0};
435 static const UNICODE_STRING empty_str = { 0, sizeof(empty), empty };
436 static const UNICODE_STRING null_str = { 0, 0, NULL };
438 const RTL_USER_PROCESS_PARAMETERS *cur_params;
439 SIZE_T size, total_size;
440 void *ptr;
441 NTSTATUS status;
443 RtlAcquirePebLock();
444 cur_params = NtCurrentTeb()->Peb->ProcessParameters;
445 if (!DllPath) DllPath = &cur_params->DllPath;
446 if (!CurrentDirectoryName)
448 if (NtCurrentTeb()->Tib.SubSystemTib) /* FIXME: hack */
449 CurrentDirectoryName = &((WIN16_SUBSYSTEM_TIB *)NtCurrentTeb()->Tib.SubSystemTib)->curdir.DosPath;
450 else
451 CurrentDirectoryName = &cur_params->CurrentDirectory.DosPath;
453 if (!CommandLine) CommandLine = ImagePathName;
454 if (!Environment) Environment = cur_params->Environment;
455 if (!WindowTitle) WindowTitle = &empty_str;
456 if (!Desktop) Desktop = &empty_str;
457 if (!ShellInfo) ShellInfo = &empty_str;
458 if (!RuntimeInfo) RuntimeInfo = &null_str;
460 size = (sizeof(RTL_USER_PROCESS_PARAMETERS)
461 + ImagePathName->MaximumLength
462 + DllPath->MaximumLength
463 + CurrentDirectoryName->MaximumLength
464 + CommandLine->MaximumLength
465 + WindowTitle->MaximumLength
466 + Desktop->MaximumLength
467 + ShellInfo->MaximumLength
468 + RuntimeInfo->MaximumLength);
470 total_size = size;
471 ptr = NULL;
472 if ((status = NtAllocateVirtualMemory( NtCurrentProcess(), &ptr, 0, &total_size,
473 MEM_COMMIT, PAGE_READWRITE )) == STATUS_SUCCESS)
475 RTL_USER_PROCESS_PARAMETERS *params = ptr;
476 params->AllocationSize = total_size;
477 params->Size = size;
478 params->Flags = PROCESS_PARAMS_FLAG_NORMALIZED;
479 params->ConsoleFlags = cur_params->ConsoleFlags;
480 params->Environment = Environment;
481 /* all other fields are zero */
483 ptr = params + 1;
484 append_unicode_string( &ptr, CurrentDirectoryName, &params->CurrentDirectory.DosPath );
485 append_unicode_string( &ptr, DllPath, &params->DllPath );
486 append_unicode_string( &ptr, ImagePathName, &params->ImagePathName );
487 append_unicode_string( &ptr, CommandLine, &params->CommandLine );
488 append_unicode_string( &ptr, WindowTitle, &params->WindowTitle );
489 append_unicode_string( &ptr, Desktop, &params->Desktop );
490 append_unicode_string( &ptr, ShellInfo, &params->ShellInfo );
491 append_unicode_string( &ptr, RuntimeInfo, &params->RuntimeInfo );
492 *result = RtlDeNormalizeProcessParams( params );
494 RtlReleasePebLock();
495 return status;
499 /******************************************************************************
500 * RtlDestroyProcessParameters [NTDLL.@]
502 void WINAPI RtlDestroyProcessParameters( RTL_USER_PROCESS_PARAMETERS *params )
504 void *ptr = params;
505 SIZE_T size = 0;
506 NtFreeVirtualMemory( NtCurrentProcess(), &ptr, &size, MEM_RELEASE );