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
27 #define WIN32_NO_STATUS
30 #include "wine/unicode.h"
31 #include "wine/debug.h"
32 #include "ntdll_misc.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(environ);
37 /******************************************************************************
38 * NtQuerySystemEnvironmentValue [NTDLL.@]
40 NTSYSAPI NTSTATUS WINAPI NtQuerySystemEnvironmentValue(PUNICODE_STRING VariableName,
42 ULONG ValueBufferLength,
43 PULONG RequiredLength)
46 return STATUS_NOT_IMPLEMENTED;
49 /******************************************************************************
50 * RtlCreateEnvironment [NTDLL.@]
52 NTSTATUS WINAPI RtlCreateEnvironment(BOOLEAN inherit, PWSTR* env)
56 TRACE("(%u,%p)!\n", inherit, env);
60 MEMORY_BASIC_INFORMATION mbi;
64 nts = NtQueryVirtualMemory(NtCurrentProcess(),
65 NtCurrentTeb()->Peb->ProcessParameters->Environment,
66 0, &mbi, sizeof(mbi), NULL);
67 if (nts == STATUS_SUCCESS)
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);
82 nts = NtAllocateVirtualMemory(NtCurrentProcess(), &addr, 0, &size,
83 MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
84 if (nts == STATUS_SUCCESS) *env = addr;
90 /******************************************************************************
91 * RtlDestroyEnvironment [NTDLL.@]
93 NTSTATUS WINAPI RtlDestroyEnvironment(PWSTR env)
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;
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;
134 TRACE("%p %s %p\n", env, debugstr_us(name), value);
137 namelen = name->Length / sizeof(WCHAR);
138 if (!namelen) return nts;
143 var = NtCurrentTeb()->Peb->ProcessParameters->Environment;
147 var = ENV_FindVariable(var, name->Buffer, namelen);
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();
165 /******************************************************************
166 * RtlSetCurrentEnvironment [NTDLL.@]
169 void WINAPI RtlSetCurrentEnvironment(PWSTR new_env, PWSTR* old_env)
171 TRACE("(%p %p)\n", new_env, old_env);
175 if (old_env) *old_env = NtCurrentTeb()->Peb->ProcessParameters->Environment;
176 NtCurrentTeb()->Peb->ProcessParameters->Environment = new_env;
182 /******************************************************************************
183 * RtlSetEnvironmentVariable [NTDLL.@]
185 NTSTATUS WINAPI RtlSetEnvironmentVariable(PWSTR* penv, PUNICODE_STRING name,
186 PUNICODE_STRING value)
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;
207 env = NtCurrentTeb()->Peb->ProcessParameters->Environment;
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 */
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)
238 SIZE_T new_size = (old_size + len) * sizeof(WCHAR);
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));
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;
256 if (len > 0) memmove(p + len, p, (old_size - (p - env)) * sizeof(WCHAR));
259 /* Set the new string */
262 memcpy( p, name->Buffer, name->Length );
263 p += name->Length / sizeof(WCHAR);
265 memcpy( p, value->Buffer, value->Length );
266 p[value->Length / sizeof(WCHAR)] = 0;
269 if (!penv) RtlReleasePebLock();
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;
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;
293 env = NtCurrentTeb()->Peb->ProcessParameters->Environment;
301 if ((p = memchrW( src, '%', src_len ))) len = p - src;
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 */
320 var = src; /* Copy original name instead */
326 else /* unfinished variable name, ignore it */
329 len = src_len; /* Copy whole string */
337 if (count < len) len = count;
338 memcpy(dst, var, len * sizeof(WCHAR));
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, ¶ms->CurrentDirectory.DosPath.Buffer );
369 normalize( params, ¶ms->DllPath.Buffer );
370 normalize( params, ¶ms->ImagePathName.Buffer );
371 normalize( params, ¶ms->CommandLine.Buffer );
372 normalize( params, ¶ms->WindowTitle.Buffer );
373 normalize( params, ¶ms->Desktop.Buffer );
374 normalize( params, ¶ms->ShellInfo.Buffer );
375 normalize( params, ¶ms->RuntimeInfo.Buffer );
376 params->Flags |= PROCESS_PARAMS_FLAG_NORMALIZED;
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, ¶ms->CurrentDirectory.DosPath.Buffer );
395 denormalize( params, ¶ms->DllPath.Buffer );
396 denormalize( params, ¶ms->ImagePathName.Buffer );
397 denormalize( params, ¶ms->CommandLine.Buffer );
398 denormalize( params, ¶ms->WindowTitle.Buffer );
399 denormalize( params, ¶ms->Desktop.Buffer );
400 denormalize( params, ¶ms->ShellInfo.Buffer );
401 denormalize( params, ¶ms->RuntimeInfo.Buffer );
402 params->Flags &= ~PROCESS_PARAMS_FLAG_NORMALIZED;
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;
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,
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;
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;
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);
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;
478 params->Flags = PROCESS_PARAMS_FLAG_NORMALIZED;
479 params->ConsoleFlags = cur_params->ConsoleFlags;
480 params->Environment = Environment;
481 /* all other fields are zero */
484 append_unicode_string( &ptr, CurrentDirectoryName, ¶ms->CurrentDirectory.DosPath );
485 append_unicode_string( &ptr, DllPath, ¶ms->DllPath );
486 append_unicode_string( &ptr, ImagePathName, ¶ms->ImagePathName );
487 append_unicode_string( &ptr, CommandLine, ¶ms->CommandLine );
488 append_unicode_string( &ptr, WindowTitle, ¶ms->WindowTitle );
489 append_unicode_string( &ptr, Desktop, ¶ms->Desktop );
490 append_unicode_string( &ptr, ShellInfo, ¶ms->ShellInfo );
491 append_unicode_string( &ptr, RuntimeInfo, ¶ms->RuntimeInfo );
492 *result = RtlDeNormalizeProcessParams( params );
499 /******************************************************************************
500 * RtlDestroyProcessParameters [NTDLL.@]
502 void WINAPI RtlDestroyProcessParameters( RTL_USER_PROCESS_PARAMETERS *params )
506 NtFreeVirtualMemory( NtCurrentProcess(), &ptr, &size, MEM_RELEASE );