ntdll: Implement RtlExpandEnvironmentStrings().
[wine.git] / dlls / ntdll / env.c
blob1428a1b4353accf761385d13e655f65d62203274
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("(%s, %p, %u, %p), stub\n", debugstr_us(VariableName), Value, ValueBufferLength, RequiredLength);
46 return STATUS_NOT_IMPLEMENTED;
49 /******************************************************************************
50 * NtQuerySystemEnvironmentValueEx [NTDLL.@]
52 NTSYSAPI NTSTATUS WINAPI NtQuerySystemEnvironmentValueEx(PUNICODE_STRING name, LPGUID vendor,
53 PVOID value, PULONG retlength, PULONG attrib)
55 FIXME("(%s, %s, %p, %p, %p), stub\n", debugstr_us(name), debugstr_guid(vendor), value, retlength, attrib);
56 return STATUS_NOT_IMPLEMENTED;
59 /******************************************************************************
60 * RtlCreateEnvironment [NTDLL.@]
62 NTSTATUS WINAPI RtlCreateEnvironment(BOOLEAN inherit, PWSTR* env)
64 NTSTATUS nts;
66 TRACE("(%u,%p)!\n", inherit, env);
68 if (inherit)
70 MEMORY_BASIC_INFORMATION mbi;
72 RtlAcquirePebLock();
74 nts = NtQueryVirtualMemory(NtCurrentProcess(),
75 NtCurrentTeb()->Peb->ProcessParameters->Environment,
76 0, &mbi, sizeof(mbi), NULL);
77 if (nts == STATUS_SUCCESS)
79 *env = NULL;
80 nts = NtAllocateVirtualMemory(NtCurrentProcess(), (void**)env, 0, &mbi.RegionSize,
81 MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
82 if (nts == STATUS_SUCCESS)
83 memcpy(*env, NtCurrentTeb()->Peb->ProcessParameters->Environment, mbi.RegionSize);
84 else *env = NULL;
86 RtlReleasePebLock();
88 else
90 SIZE_T size = 1;
91 PVOID addr = NULL;
92 nts = NtAllocateVirtualMemory(NtCurrentProcess(), &addr, 0, &size,
93 MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
94 if (nts == STATUS_SUCCESS) *env = addr;
97 return nts;
100 /******************************************************************************
101 * RtlDestroyEnvironment [NTDLL.@]
103 NTSTATUS WINAPI RtlDestroyEnvironment(PWSTR env)
105 SIZE_T size = 0;
107 TRACE("(%p)!\n", env);
109 return NtFreeVirtualMemory(NtCurrentProcess(), (void**)&env, &size, MEM_RELEASE);
112 static LPCWSTR ENV_FindVariable(PCWSTR var, PCWSTR name, unsigned namelen)
114 for (; *var; var += strlenW(var) + 1)
116 /* match var names, but avoid setting a var with a name including a '='
117 * (a starting '=' is valid though)
119 if (strncmpiW(var, name, namelen) == 0 && var[namelen] == '=' &&
120 strchrW(var + 1, '=') == var + namelen)
122 return var + namelen + 1;
125 return NULL;
128 /******************************************************************
129 * RtlQueryEnvironmentVariable_U [NTDLL.@]
131 * NOTES: when the buffer is too small, the string is not written, but if the
132 * terminating null char is the only char that cannot be written, then
133 * all chars (except the null) are written and success is returned
134 * (behavior of Win2k at least)
136 NTSTATUS WINAPI RtlQueryEnvironmentVariable_U(PWSTR env,
137 PUNICODE_STRING name,
138 PUNICODE_STRING value)
140 NTSTATUS nts = STATUS_VARIABLE_NOT_FOUND;
141 PCWSTR var;
142 unsigned namelen;
144 TRACE("%p %s %p\n", env, debugstr_us(name), value);
146 value->Length = 0;
147 namelen = name->Length / sizeof(WCHAR);
148 if (!namelen) return nts;
150 if (!env)
152 RtlAcquirePebLock();
153 var = NtCurrentTeb()->Peb->ProcessParameters->Environment;
155 else var = env;
157 var = ENV_FindVariable(var, name->Buffer, namelen);
158 if (var != NULL)
160 value->Length = strlenW(var) * sizeof(WCHAR);
162 if (value->Length <= value->MaximumLength)
164 memmove(value->Buffer, var, min(value->Length + sizeof(WCHAR), value->MaximumLength));
165 nts = STATUS_SUCCESS;
167 else nts = STATUS_BUFFER_TOO_SMALL;
170 if (!env) RtlReleasePebLock();
172 return nts;
175 /******************************************************************
176 * RtlSetCurrentEnvironment [NTDLL.@]
179 void WINAPI RtlSetCurrentEnvironment(PWSTR new_env, PWSTR* old_env)
181 TRACE("(%p %p)\n", new_env, old_env);
183 RtlAcquirePebLock();
185 if (old_env) *old_env = NtCurrentTeb()->Peb->ProcessParameters->Environment;
186 NtCurrentTeb()->Peb->ProcessParameters->Environment = new_env;
188 RtlReleasePebLock();
192 /******************************************************************************
193 * RtlSetEnvironmentVariable [NTDLL.@]
195 NTSTATUS WINAPI RtlSetEnvironmentVariable(PWSTR* penv, PUNICODE_STRING name,
196 PUNICODE_STRING value)
198 INT len, old_size;
199 LPWSTR p, env;
200 NTSTATUS nts = STATUS_VARIABLE_NOT_FOUND;
201 MEMORY_BASIC_INFORMATION mbi;
203 TRACE("(%p, %s, %s)\n", penv, debugstr_us(name), debugstr_us(value));
205 if (!name || !name->Buffer || !name->Length)
206 return STATUS_INVALID_PARAMETER_1;
208 len = name->Length / sizeof(WCHAR);
210 /* variable names can't contain a '=' except as a first character */
211 for (p = name->Buffer + 1; p < name->Buffer + len; p++)
212 if (*p == '=') return STATUS_INVALID_PARAMETER;
214 if (!penv)
216 RtlAcquirePebLock();
217 env = NtCurrentTeb()->Peb->ProcessParameters->Environment;
218 } else env = *penv;
220 /* compute current size of environment */
221 for (p = env; *p; p += strlenW(p) + 1);
222 old_size = p + 1 - env;
224 /* Find a place to insert the string */
225 for (p = env; *p; p += strlenW(p) + 1)
227 if (!strncmpiW(name->Buffer, p, len) && (p[len] == '=')) break;
229 if (!value && !*p) goto done; /* Value to remove doesn't exist */
231 /* Realloc the buffer */
232 len = value ? len + value->Length / sizeof(WCHAR) + 2 : 0;
233 if (*p) len -= strlenW(p) + 1; /* The name already exists */
235 if (len < 0)
237 LPWSTR next = p + strlenW(p) + 1; /* We know there is a next one */
238 memmove(next + len, next, (old_size - (next - env)) * sizeof(WCHAR));
241 nts = NtQueryVirtualMemory(NtCurrentProcess(), env, 0,
242 &mbi, sizeof(mbi), NULL);
243 if (nts != STATUS_SUCCESS) goto done;
245 if ((old_size + len) * sizeof(WCHAR) > mbi.RegionSize)
247 LPWSTR new_env;
248 SIZE_T new_size = (old_size + len) * sizeof(WCHAR);
250 new_env = NULL;
251 nts = NtAllocateVirtualMemory(NtCurrentProcess(), (void**)&new_env, 0,
252 &new_size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
253 if (nts != STATUS_SUCCESS) goto done;
255 memmove(new_env, env, (p - env) * sizeof(WCHAR));
256 assert(len > 0);
257 memmove(new_env + (p - env) + len, p, (old_size - (p - env)) * sizeof(WCHAR));
258 p = new_env + (p - env);
260 RtlDestroyEnvironment(env);
261 if (!penv) NtCurrentTeb()->Peb->ProcessParameters->Environment = new_env;
262 else *penv = new_env;
264 else
266 if (len > 0) memmove(p + len, p, (old_size - (p - env)) * sizeof(WCHAR));
269 /* Set the new string */
270 if (value)
272 memcpy( p, name->Buffer, name->Length );
273 p += name->Length / sizeof(WCHAR);
274 *p++ = '=';
275 memcpy( p, value->Buffer, value->Length );
276 p[value->Length / sizeof(WCHAR)] = 0;
278 done:
279 if (!penv) RtlReleasePebLock();
281 return nts;
284 /******************************************************************************
285 * RtlExpandEnvironmentStrings (NTDLL.@)
287 NTSTATUS WINAPI RtlExpandEnvironmentStrings( const WCHAR *renv, WCHAR *src, SIZE_T src_len,
288 WCHAR *dst, SIZE_T count, SIZE_T *plen )
290 SIZE_T len, total_size = 1; /* 1 for terminating '\0' */
291 LPCWSTR env, p, var;
293 if (!renv)
295 RtlAcquirePebLock();
296 env = NtCurrentTeb()->Peb->ProcessParameters->Environment;
298 else env = renv;
300 while (src_len)
302 if (*src != '%')
304 if ((p = memchrW( src, '%', src_len ))) len = p - src;
305 else len = src_len;
306 var = src;
307 src += len;
308 src_len -= len;
310 else /* we are at the start of a variable */
312 if ((p = memchrW( src + 1, '%', src_len - 1 )))
314 len = p - src - 1; /* Length of the variable name */
315 if ((var = ENV_FindVariable( env, src + 1, len )))
317 src += len + 2; /* Skip the variable name */
318 src_len -= len + 2;
319 len = strlenW(var);
321 else
323 var = src; /* Copy original name instead */
324 len += 2;
325 src += len;
326 src_len -= len;
329 else /* unfinished variable name, ignore it */
331 var = src;
332 len = src_len; /* Copy whole string */
333 src += len;
334 src_len = 0;
337 total_size += len;
338 if (dst)
340 if (count < len) len = count;
341 memcpy(dst, var, len * sizeof(WCHAR));
342 count -= len;
343 dst += len;
347 if (!renv) RtlReleasePebLock();
349 if (dst && count) *dst = '\0';
350 if (plen) *plen = total_size;
352 return (count) ? STATUS_SUCCESS : STATUS_BUFFER_TOO_SMALL;
355 /******************************************************************
356 * RtlExpandEnvironmentStrings_U (NTDLL.@)
358 NTSTATUS WINAPI RtlExpandEnvironmentStrings_U( const WCHAR *env, const UNICODE_STRING *src,
359 UNICODE_STRING *dst, ULONG *plen )
361 SIZE_T len;
362 NTSTATUS ret;
364 ret = RtlExpandEnvironmentStrings( env, src->Buffer, src->Length / sizeof(WCHAR),
365 dst->Buffer, dst->MaximumLength / sizeof(WCHAR), &len );
366 if (plen) *plen = len * sizeof(WCHAR); /* FIXME: check for overflow? */
367 if (len > UNICODE_STRING_MAX_CHARS) ret = STATUS_BUFFER_TOO_SMALL;
368 if (!ret) dst->Length = (len - 1) * sizeof(WCHAR);
369 return ret;
372 static inline void normalize( void *base, WCHAR **ptr )
374 if (*ptr) *ptr = (WCHAR *)((char *)base + (UINT_PTR)*ptr);
377 /******************************************************************************
378 * RtlNormalizeProcessParams [NTDLL.@]
380 PRTL_USER_PROCESS_PARAMETERS WINAPI RtlNormalizeProcessParams( RTL_USER_PROCESS_PARAMETERS *params )
382 if (params && !(params->Flags & PROCESS_PARAMS_FLAG_NORMALIZED))
384 normalize( params, &params->CurrentDirectory.DosPath.Buffer );
385 normalize( params, &params->DllPath.Buffer );
386 normalize( params, &params->ImagePathName.Buffer );
387 normalize( params, &params->CommandLine.Buffer );
388 normalize( params, &params->WindowTitle.Buffer );
389 normalize( params, &params->Desktop.Buffer );
390 normalize( params, &params->ShellInfo.Buffer );
391 normalize( params, &params->RuntimeInfo.Buffer );
392 params->Flags |= PROCESS_PARAMS_FLAG_NORMALIZED;
394 return params;
398 static inline void denormalize( const void *base, WCHAR **ptr )
400 if (*ptr) *ptr = (WCHAR *)(UINT_PTR)((char *)*ptr - (const char *)base);
403 /******************************************************************************
404 * RtlDeNormalizeProcessParams [NTDLL.@]
406 PRTL_USER_PROCESS_PARAMETERS WINAPI RtlDeNormalizeProcessParams( RTL_USER_PROCESS_PARAMETERS *params )
408 if (params && (params->Flags & PROCESS_PARAMS_FLAG_NORMALIZED))
410 denormalize( params, &params->CurrentDirectory.DosPath.Buffer );
411 denormalize( params, &params->DllPath.Buffer );
412 denormalize( params, &params->ImagePathName.Buffer );
413 denormalize( params, &params->CommandLine.Buffer );
414 denormalize( params, &params->WindowTitle.Buffer );
415 denormalize( params, &params->Desktop.Buffer );
416 denormalize( params, &params->ShellInfo.Buffer );
417 denormalize( params, &params->RuntimeInfo.Buffer );
418 params->Flags &= ~PROCESS_PARAMS_FLAG_NORMALIZED;
420 return params;
424 /* append a unicode string to the process params data; helper for RtlCreateProcessParameters */
425 static void append_unicode_string( void **data, const UNICODE_STRING *src,
426 UNICODE_STRING *dst )
428 dst->Length = src->Length;
429 dst->MaximumLength = src->MaximumLength;
430 dst->Buffer = *data;
431 memcpy( dst->Buffer, src->Buffer, dst->MaximumLength );
432 *data = (char *)dst->Buffer + dst->MaximumLength;
436 /******************************************************************************
437 * RtlCreateProcessParameters [NTDLL.@]
439 NTSTATUS WINAPI RtlCreateProcessParameters( RTL_USER_PROCESS_PARAMETERS **result,
440 const UNICODE_STRING *ImagePathName,
441 const UNICODE_STRING *DllPath,
442 const UNICODE_STRING *CurrentDirectoryName,
443 const UNICODE_STRING *CommandLine,
444 PWSTR Environment,
445 const UNICODE_STRING *WindowTitle,
446 const UNICODE_STRING *Desktop,
447 const UNICODE_STRING *ShellInfo,
448 const UNICODE_STRING *RuntimeInfo )
450 static WCHAR empty[] = {0};
451 static const UNICODE_STRING empty_str = { 0, sizeof(empty), empty };
452 static const UNICODE_STRING null_str = { 0, 0, NULL };
454 const RTL_USER_PROCESS_PARAMETERS *cur_params;
455 SIZE_T size, total_size;
456 void *ptr;
457 NTSTATUS status;
459 RtlAcquirePebLock();
460 cur_params = NtCurrentTeb()->Peb->ProcessParameters;
461 if (!DllPath) DllPath = &cur_params->DllPath;
462 if (!CurrentDirectoryName)
464 if (NtCurrentTeb()->Tib.SubSystemTib) /* FIXME: hack */
465 CurrentDirectoryName = &((WIN16_SUBSYSTEM_TIB *)NtCurrentTeb()->Tib.SubSystemTib)->curdir.DosPath;
466 else
467 CurrentDirectoryName = &cur_params->CurrentDirectory.DosPath;
469 if (!CommandLine) CommandLine = ImagePathName;
470 if (!Environment) Environment = cur_params->Environment;
471 if (!WindowTitle) WindowTitle = &empty_str;
472 if (!Desktop) Desktop = &empty_str;
473 if (!ShellInfo) ShellInfo = &empty_str;
474 if (!RuntimeInfo) RuntimeInfo = &null_str;
476 size = (sizeof(RTL_USER_PROCESS_PARAMETERS)
477 + ImagePathName->MaximumLength
478 + DllPath->MaximumLength
479 + CurrentDirectoryName->MaximumLength
480 + CommandLine->MaximumLength
481 + WindowTitle->MaximumLength
482 + Desktop->MaximumLength
483 + ShellInfo->MaximumLength
484 + RuntimeInfo->MaximumLength);
486 total_size = size;
487 ptr = NULL;
488 if ((status = NtAllocateVirtualMemory( NtCurrentProcess(), &ptr, 0, &total_size,
489 MEM_COMMIT, PAGE_READWRITE )) == STATUS_SUCCESS)
491 RTL_USER_PROCESS_PARAMETERS *params = ptr;
492 params->AllocationSize = total_size;
493 params->Size = size;
494 params->Flags = PROCESS_PARAMS_FLAG_NORMALIZED;
495 params->ConsoleFlags = cur_params->ConsoleFlags;
496 params->Environment = Environment;
497 /* all other fields are zero */
499 ptr = params + 1;
500 append_unicode_string( &ptr, CurrentDirectoryName, &params->CurrentDirectory.DosPath );
501 append_unicode_string( &ptr, DllPath, &params->DllPath );
502 append_unicode_string( &ptr, ImagePathName, &params->ImagePathName );
503 append_unicode_string( &ptr, CommandLine, &params->CommandLine );
504 append_unicode_string( &ptr, WindowTitle, &params->WindowTitle );
505 append_unicode_string( &ptr, Desktop, &params->Desktop );
506 append_unicode_string( &ptr, ShellInfo, &params->ShellInfo );
507 append_unicode_string( &ptr, RuntimeInfo, &params->RuntimeInfo );
508 *result = RtlDeNormalizeProcessParams( params );
510 RtlReleasePebLock();
511 return status;
515 /******************************************************************************
516 * RtlDestroyProcessParameters [NTDLL.@]
518 void WINAPI RtlDestroyProcessParameters( RTL_USER_PROCESS_PARAMETERS *params )
520 void *ptr = params;
521 SIZE_T size = 0;
522 NtFreeVirtualMemory( NtCurrentProcess(), &ptr, &size, MEM_RELEASE );