Implement the repair mechanism in msiexec, and stub it out in
[wine.git] / dlls / ntdll / env.c
blob29e77863bc41372a713b57c50741bb1d37132a30
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
23 #include <assert.h>
24 #include <stdarg.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winreg.h"
29 #include "winternl.h"
30 #include "wine/unicode.h"
31 #include "wine/debug.h"
32 #include "ntdll_misc.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(environ);
36 /******************************************************************************
37 * RtlCreateEnvironment [NTDLL.@]
39 NTSTATUS WINAPI RtlCreateEnvironment(BOOLEAN inherit, PWSTR* env)
41 NTSTATUS nts;
43 TRACE("(%u,%p)!\n", inherit, env);
45 if (inherit)
47 MEMORY_BASIC_INFORMATION mbi;
49 RtlAcquirePebLock();
51 nts = NtQueryVirtualMemory(NtCurrentProcess(),
52 NtCurrentTeb()->Peb->ProcessParameters->Environment,
53 0, &mbi, sizeof(mbi), NULL);
54 if (nts == STATUS_SUCCESS)
56 *env = NULL;
57 nts = NtAllocateVirtualMemory(NtCurrentProcess(), (void**)env, 0, &mbi.RegionSize,
58 MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
59 if (nts == STATUS_SUCCESS)
60 memcpy(*env, NtCurrentTeb()->Peb->ProcessParameters->Environment, mbi.RegionSize);
61 else *env = NULL;
63 RtlReleasePebLock();
65 else
67 ULONG size = 1;
68 nts = NtAllocateVirtualMemory(NtCurrentProcess(), (void**)env, 0, &size,
69 MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
70 if (nts == STATUS_SUCCESS)
71 memset(*env, 0, size);
74 return nts;
77 /******************************************************************************
78 * RtlDestroyEnvironment [NTDLL.@]
80 NTSTATUS WINAPI RtlDestroyEnvironment(PWSTR env)
82 ULONG size = 0;
84 TRACE("(%p)!\n", env);
86 return NtFreeVirtualMemory(NtCurrentProcess(), (void**)&env, &size, MEM_RELEASE);
89 static LPCWSTR ENV_FindVariable(PCWSTR var, PCWSTR name, unsigned namelen)
91 for (; *var; var += strlenW(var) + 1)
93 /* match var names, but avoid setting a var with a name including a '='
94 * (a starting '=' is valid though)
96 if (strncmpiW(var, name, namelen) == 0 && var[namelen] == '=' &&
97 strchrW(var + 1, '=') == var + namelen)
99 return var + namelen + 1;
102 return NULL;
105 /******************************************************************
106 * RtlQueryEnvironmentVariable_U [NTDLL.@]
108 * NOTES: when the buffer is too small, the string is not written, but if the
109 * terminating null char is the only char that cannot be written, then
110 * all chars (except the null) are written and success is returned
111 * (behavior of Win2k at least)
113 NTSTATUS WINAPI RtlQueryEnvironmentVariable_U(PWSTR env,
114 PUNICODE_STRING name,
115 PUNICODE_STRING value)
117 NTSTATUS nts = STATUS_VARIABLE_NOT_FOUND;
118 PCWSTR var;
119 unsigned namelen;
121 TRACE("%s %s %p\n", debugstr_w(env), debugstr_w(name->Buffer), value);
123 value->Length = 0;
124 namelen = name->Length / sizeof(WCHAR);
125 if (!namelen) return nts;
127 if (!env)
129 RtlAcquirePebLock();
130 var = NtCurrentTeb()->Peb->ProcessParameters->Environment;
132 else var = env;
134 var = ENV_FindVariable(var, name->Buffer, namelen);
135 if (var != NULL)
137 value->Length = strlenW(var) * sizeof(WCHAR);
139 if (value->Length <= value->MaximumLength)
141 memmove(value->Buffer, var, min(value->Length + sizeof(WCHAR), value->MaximumLength));
142 nts = STATUS_SUCCESS;
144 else nts = STATUS_BUFFER_TOO_SMALL;
147 if (!env) RtlReleasePebLock();
149 return nts;
152 /******************************************************************
153 * RtlSetCurrentEnvironment [NTDLL.@]
156 void WINAPI RtlSetCurrentEnvironment(PWSTR new_env, PWSTR* old_env)
158 TRACE("(%p %p)\n", new_env, old_env);
160 RtlAcquirePebLock();
162 if (old_env) *old_env = NtCurrentTeb()->Peb->ProcessParameters->Environment;
163 NtCurrentTeb()->Peb->ProcessParameters->Environment = new_env;
165 RtlReleasePebLock();
169 /******************************************************************************
170 * RtlSetEnvironmentVariable [NTDLL.@]
172 NTSTATUS WINAPI RtlSetEnvironmentVariable(PWSTR* penv, PUNICODE_STRING name,
173 PUNICODE_STRING value)
175 INT len, old_size;
176 LPWSTR p, env;
177 NTSTATUS nts = STATUS_VARIABLE_NOT_FOUND;
178 MEMORY_BASIC_INFORMATION mbi;
180 TRACE("(%p,%s,%s)\n",
181 penv, debugstr_w(name->Buffer),
182 value ? debugstr_w(value->Buffer) : "--nil--");
184 if (!name || !name->Buffer || !name->Length)
185 return STATUS_INVALID_PARAMETER_1;
187 len = name->Length / sizeof(WCHAR);
189 /* variable names can't contain a '=' except as a first character */
190 for (p = name->Buffer + 1; p < name->Buffer + len; p++)
191 if (*p == '=') return STATUS_INVALID_PARAMETER;
193 if (!penv)
195 RtlAcquirePebLock();
196 env = NtCurrentTeb()->Peb->ProcessParameters->Environment;
197 } else env = *penv;
199 /* compute current size of environment */
200 for (p = env; *p; p += strlenW(p) + 1);
201 old_size = p + 1 - env;
203 /* Find a place to insert the string */
204 for (p = env; *p; p += strlenW(p) + 1)
206 if (!strncmpiW(name->Buffer, p, len) && (p[len] == '=')) break;
208 if (!value && !*p) goto done; /* Value to remove doesn't exist */
210 /* Realloc the buffer */
211 len = value ? len + value->Length / sizeof(WCHAR) + 2 : 0;
212 if (*p) len -= strlenW(p) + 1; /* The name already exists */
214 if (len < 0)
216 LPWSTR next = p + strlenW(p) + 1; /* We know there is a next one */
217 memmove(next + len, next, (old_size - (next - env)) * sizeof(WCHAR));
220 nts = NtQueryVirtualMemory(NtCurrentProcess(), env, 0,
221 &mbi, sizeof(mbi), NULL);
222 if (nts != STATUS_SUCCESS) goto done;
224 if ((old_size + len) * sizeof(WCHAR) > mbi.RegionSize)
226 LPWSTR new_env;
227 ULONG new_size = (old_size + len) * sizeof(WCHAR);
229 new_env = NULL;
230 nts = NtAllocateVirtualMemory(NtCurrentProcess(), (void**)&new_env, 0,
231 &new_size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
232 if (nts != STATUS_SUCCESS) goto done;
234 memmove(new_env, env, (p - env) * sizeof(WCHAR));
235 assert(len > 0);
236 memmove(new_env + (p - env) + len, p, (old_size - (p - env)) * sizeof(WCHAR));
237 p = new_env + (p - env);
239 RtlDestroyEnvironment(env);
240 if (!penv) NtCurrentTeb()->Peb->ProcessParameters->Environment = new_env;
241 else *penv = new_env;
242 env = new_env;
244 else
246 if (len > 0) memmove(p + len, p, (old_size - (p - env)) * sizeof(WCHAR));
249 /* Set the new string */
250 if (value)
252 memcpy( p, name->Buffer, name->Length );
253 p += name->Length / sizeof(WCHAR);
254 *p++ = '=';
255 memcpy( p, value->Buffer, value->Length );
256 p[value->Length / sizeof(WCHAR)] = 0;
258 done:
259 if (!penv) RtlReleasePebLock();
261 return nts;
264 /******************************************************************
265 * RtlExpandEnvironmentStrings_U (NTDLL.@)
268 NTSTATUS WINAPI RtlExpandEnvironmentStrings_U(PWSTR renv, const UNICODE_STRING* us_src,
269 PUNICODE_STRING us_dst, PULONG plen)
271 DWORD len, count, total_size = 1; /* 1 for terminating '\0' */
272 LPCWSTR env, src, p, var;
273 LPWSTR dst;
275 src = us_src->Buffer;
276 count = us_dst->MaximumLength / sizeof(WCHAR);
277 dst = count ? us_dst->Buffer : NULL;
279 if (!renv)
281 RtlAcquirePebLock();
282 env = NtCurrentTeb()->Peb->ProcessParameters->Environment;
284 else env = renv;
286 while (*src)
288 if (*src != '%')
290 if ((p = strchrW( src, '%' ))) len = p - src;
291 else len = strlenW(src);
292 var = src;
293 src += len;
295 else /* we are at the start of a variable */
297 if ((p = strchrW( src + 1, '%' )))
299 len = p - src - 1; /* Length of the variable name */
300 if ((var = ENV_FindVariable( env, src + 1, len )))
302 src += len + 2; /* Skip the variable name */
303 len = strlenW(var);
305 else
307 var = src; /* Copy original name instead */
308 len += 2;
309 src += len;
312 else /* unfinished variable name, ignore it */
314 var = src;
315 len = strlenW(src); /* Copy whole string */
316 src += len;
319 total_size += len;
320 if (dst)
322 if (count < len) len = count;
323 memcpy(dst, var, len * sizeof(WCHAR));
324 count -= len;
325 dst += len;
329 if (!renv) RtlReleasePebLock();
331 /* Null-terminate the string */
332 if (dst && count) *dst = '\0';
334 us_dst->Length = (dst) ? (dst - us_dst->Buffer) * sizeof(WCHAR) : 0;
335 if (plen) *plen = total_size * sizeof(WCHAR);
337 return (count) ? STATUS_SUCCESS : STATUS_BUFFER_TOO_SMALL;
341 static inline void normalize( void *base, WCHAR **ptr )
343 if (*ptr) *ptr = (WCHAR *)((char *)base + (UINT_PTR)*ptr);
346 /******************************************************************************
347 * RtlNormalizeProcessParams [NTDLL.@]
349 PRTL_USER_PROCESS_PARAMETERS WINAPI RtlNormalizeProcessParams( RTL_USER_PROCESS_PARAMETERS *params )
351 if (params && !(params->Flags & PROCESS_PARAMS_FLAG_NORMALIZED))
353 normalize( params, &params->CurrentDirectory.DosPath.Buffer );
354 normalize( params, &params->DllPath.Buffer );
355 normalize( params, &params->ImagePathName.Buffer );
356 normalize( params, &params->CommandLine.Buffer );
357 normalize( params, &params->WindowTitle.Buffer );
358 normalize( params, &params->Desktop.Buffer );
359 normalize( params, &params->ShellInfo.Buffer );
360 normalize( params, &params->RuntimeInfo.Buffer );
361 params->Flags |= PROCESS_PARAMS_FLAG_NORMALIZED;
363 return params;
367 static inline void denormalize( void *base, WCHAR **ptr )
369 if (*ptr) *ptr = (WCHAR *)(UINT_PTR)((char *)*ptr - (char *)base);
372 /******************************************************************************
373 * RtlDeNormalizeProcessParams [NTDLL.@]
375 PRTL_USER_PROCESS_PARAMETERS WINAPI RtlDeNormalizeProcessParams( RTL_USER_PROCESS_PARAMETERS *params )
377 if (params && (params->Flags & PROCESS_PARAMS_FLAG_NORMALIZED))
379 denormalize( params, &params->CurrentDirectory.DosPath.Buffer );
380 denormalize( params, &params->DllPath.Buffer );
381 denormalize( params, &params->ImagePathName.Buffer );
382 denormalize( params, &params->CommandLine.Buffer );
383 denormalize( params, &params->WindowTitle.Buffer );
384 denormalize( params, &params->Desktop.Buffer );
385 denormalize( params, &params->ShellInfo.Buffer );
386 denormalize( params, &params->RuntimeInfo.Buffer );
387 params->Flags &= ~PROCESS_PARAMS_FLAG_NORMALIZED;
389 return params;
393 /* append a unicode string to the process params data; helper for RtlCreateProcessParameters */
394 static void append_unicode_string( void **data, const UNICODE_STRING *src,
395 UNICODE_STRING *dst )
397 dst->Length = src->Length;
398 dst->MaximumLength = src->MaximumLength;
399 dst->Buffer = *data;
400 memcpy( dst->Buffer, src->Buffer, dst->MaximumLength );
401 *data = (char *)dst->Buffer + dst->MaximumLength;
405 /******************************************************************************
406 * RtlCreateProcessParameters [NTDLL.@]
408 NTSTATUS WINAPI RtlCreateProcessParameters( RTL_USER_PROCESS_PARAMETERS **result,
409 const UNICODE_STRING *ImagePathName,
410 const UNICODE_STRING *DllPath,
411 const UNICODE_STRING *CurrentDirectoryName,
412 const UNICODE_STRING *CommandLine,
413 PWSTR Environment,
414 const UNICODE_STRING *WindowTitle,
415 const UNICODE_STRING *Desktop,
416 const UNICODE_STRING *ShellInfo,
417 const UNICODE_STRING *RuntimeInfo )
419 static const WCHAR empty[] = {0};
420 static const UNICODE_STRING empty_str = { 0, sizeof(empty), (WCHAR *)empty };
422 const RTL_USER_PROCESS_PARAMETERS *cur_params;
423 ULONG size, total_size;
424 void *ptr;
425 NTSTATUS status;
427 RtlAcquirePebLock();
428 cur_params = NtCurrentTeb()->Peb->ProcessParameters;
429 if (!DllPath) DllPath = &cur_params->DllPath;
430 if (!CurrentDirectoryName) CurrentDirectoryName = &cur_params->CurrentDirectory.DosPath;
431 if (!CommandLine) CommandLine = ImagePathName;
432 if (!Environment) Environment = cur_params->Environment;
433 if (!WindowTitle) WindowTitle = &empty_str;
434 if (!Desktop) Desktop = &empty_str;
435 if (!ShellInfo) ShellInfo = &empty_str;
436 if (!RuntimeInfo) RuntimeInfo = &empty_str;
438 size = (sizeof(RTL_USER_PROCESS_PARAMETERS)
439 + ImagePathName->MaximumLength
440 + DllPath->MaximumLength
441 + CurrentDirectoryName->MaximumLength
442 + CommandLine->MaximumLength
443 + WindowTitle->MaximumLength
444 + Desktop->MaximumLength
445 + ShellInfo->MaximumLength
446 + RuntimeInfo->MaximumLength);
448 total_size = size;
449 if ((status = NtAllocateVirtualMemory( NtCurrentProcess(), &ptr, NULL, &total_size,
450 MEM_COMMIT, PAGE_READWRITE )) == STATUS_SUCCESS)
452 RTL_USER_PROCESS_PARAMETERS *params = ptr;
453 params->AllocationSize = total_size;
454 params->Size = size;
455 params->Flags = PROCESS_PARAMS_FLAG_NORMALIZED;
456 params->ProcessGroup = cur_params->ProcessGroup;
457 params->Environment = Environment;
458 /* all other fields are zero */
460 ptr = params + 1;
461 append_unicode_string( &ptr, CurrentDirectoryName, &params->CurrentDirectory.DosPath );
462 append_unicode_string( &ptr, DllPath, &params->DllPath );
463 append_unicode_string( &ptr, ImagePathName, &params->ImagePathName );
464 append_unicode_string( &ptr, CommandLine, &params->CommandLine );
465 append_unicode_string( &ptr, WindowTitle, &params->WindowTitle );
466 append_unicode_string( &ptr, Desktop, &params->Desktop );
467 append_unicode_string( &ptr, ShellInfo, &params->ShellInfo );
468 append_unicode_string( &ptr, RuntimeInfo, &params->RuntimeInfo );
469 *result = RtlDeNormalizeProcessParams( params );
471 RtlReleasePebLock();
472 return status;
476 /******************************************************************************
477 * RtlDestroyProcessParameters [NTDLL.@]
479 void WINAPI RtlDestroyProcessParameters( RTL_USER_PROCESS_PARAMETERS *params )
481 void *ptr = params;
482 ULONG size = 0;
483 NtFreeVirtualMemory( NtCurrentProcess(), &ptr, &size, MEM_RELEASE );