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
)
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
)
66 TRACE("(%u,%p)!\n", inherit
, env
);
70 MEMORY_BASIC_INFORMATION mbi
;
74 nts
= NtQueryVirtualMemory(NtCurrentProcess(),
75 NtCurrentTeb()->Peb
->ProcessParameters
->Environment
,
76 0, &mbi
, sizeof(mbi
), NULL
);
77 if (nts
== STATUS_SUCCESS
)
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
);
92 nts
= NtAllocateVirtualMemory(NtCurrentProcess(), &addr
, 0, &size
,
93 MEM_RESERVE
| MEM_COMMIT
, PAGE_READWRITE
);
94 if (nts
== STATUS_SUCCESS
) *env
= addr
;
100 /******************************************************************************
101 * RtlDestroyEnvironment [NTDLL.@]
103 NTSTATUS WINAPI
RtlDestroyEnvironment(PWSTR env
)
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;
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
;
144 TRACE("%p %s %p\n", env
, debugstr_us(name
), value
);
147 namelen
= name
->Length
/ sizeof(WCHAR
);
148 if (!namelen
) return nts
;
153 var
= NtCurrentTeb()->Peb
->ProcessParameters
->Environment
;
157 var
= ENV_FindVariable(var
, name
->Buffer
, namelen
);
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();
175 /******************************************************************
176 * RtlSetCurrentEnvironment [NTDLL.@]
179 void WINAPI
RtlSetCurrentEnvironment(PWSTR new_env
, PWSTR
* old_env
)
181 TRACE("(%p %p)\n", new_env
, old_env
);
185 if (old_env
) *old_env
= NtCurrentTeb()->Peb
->ProcessParameters
->Environment
;
186 NtCurrentTeb()->Peb
->ProcessParameters
->Environment
= new_env
;
192 /******************************************************************************
193 * RtlSetEnvironmentVariable [NTDLL.@]
195 NTSTATUS WINAPI
RtlSetEnvironmentVariable(PWSTR
* penv
, PUNICODE_STRING name
,
196 PUNICODE_STRING value
)
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
;
217 env
= NtCurrentTeb()->Peb
->ProcessParameters
->Environment
;
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 */
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
)
248 SIZE_T new_size
= (old_size
+ len
) * sizeof(WCHAR
);
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
));
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
;
266 if (len
> 0) memmove(p
+ len
, p
, (old_size
- (p
- env
)) * sizeof(WCHAR
));
269 /* Set the new string */
272 memcpy( p
, name
->Buffer
, name
->Length
);
273 p
+= name
->Length
/ sizeof(WCHAR
);
275 memcpy( p
, value
->Buffer
, value
->Length
);
276 p
[value
->Length
/ sizeof(WCHAR
)] = 0;
279 if (!penv
) RtlReleasePebLock();
284 /******************************************************************
285 * RtlExpandEnvironmentStrings_U (NTDLL.@)
288 NTSTATUS WINAPI
RtlExpandEnvironmentStrings_U(PCWSTR renv
, const UNICODE_STRING
* us_src
,
289 PUNICODE_STRING us_dst
, PULONG plen
)
291 DWORD src_len
, len
, count
, total_size
= 1; /* 1 for terminating '\0' */
292 LPCWSTR env
, src
, p
, var
;
295 src
= us_src
->Buffer
;
296 src_len
= us_src
->Length
/ sizeof(WCHAR
);
297 count
= us_dst
->MaximumLength
/ sizeof(WCHAR
);
298 dst
= count
? us_dst
->Buffer
: NULL
;
303 env
= NtCurrentTeb()->Peb
->ProcessParameters
->Environment
;
311 if ((p
= memchrW( src
, '%', src_len
))) len
= p
- src
;
317 else /* we are at the start of a variable */
319 if ((p
= memchrW( src
+ 1, '%', src_len
- 1 )))
321 len
= p
- src
- 1; /* Length of the variable name */
322 if ((var
= ENV_FindVariable( env
, src
+ 1, len
)))
324 src
+= len
+ 2; /* Skip the variable name */
330 var
= src
; /* Copy original name instead */
336 else /* unfinished variable name, ignore it */
339 len
= src_len
; /* Copy whole string */
347 if (count
< len
) len
= count
;
348 memcpy(dst
, var
, len
* sizeof(WCHAR
));
354 if (!renv
) RtlReleasePebLock();
356 /* Null-terminate the string */
357 if (dst
&& count
) *dst
= '\0';
359 us_dst
->Length
= (dst
) ? (dst
- us_dst
->Buffer
) * sizeof(WCHAR
) : 0;
360 if (plen
) *plen
= total_size
* sizeof(WCHAR
);
362 return (count
) ? STATUS_SUCCESS
: STATUS_BUFFER_TOO_SMALL
;
366 static inline void normalize( void *base
, WCHAR
**ptr
)
368 if (*ptr
) *ptr
= (WCHAR
*)((char *)base
+ (UINT_PTR
)*ptr
);
371 /******************************************************************************
372 * RtlNormalizeProcessParams [NTDLL.@]
374 PRTL_USER_PROCESS_PARAMETERS WINAPI
RtlNormalizeProcessParams( RTL_USER_PROCESS_PARAMETERS
*params
)
376 if (params
&& !(params
->Flags
& PROCESS_PARAMS_FLAG_NORMALIZED
))
378 normalize( params
, ¶ms
->CurrentDirectory
.DosPath
.Buffer
);
379 normalize( params
, ¶ms
->DllPath
.Buffer
);
380 normalize( params
, ¶ms
->ImagePathName
.Buffer
);
381 normalize( params
, ¶ms
->CommandLine
.Buffer
);
382 normalize( params
, ¶ms
->WindowTitle
.Buffer
);
383 normalize( params
, ¶ms
->Desktop
.Buffer
);
384 normalize( params
, ¶ms
->ShellInfo
.Buffer
);
385 normalize( params
, ¶ms
->RuntimeInfo
.Buffer
);
386 params
->Flags
|= PROCESS_PARAMS_FLAG_NORMALIZED
;
392 static inline void denormalize( const void *base
, WCHAR
**ptr
)
394 if (*ptr
) *ptr
= (WCHAR
*)(UINT_PTR
)((char *)*ptr
- (const char *)base
);
397 /******************************************************************************
398 * RtlDeNormalizeProcessParams [NTDLL.@]
400 PRTL_USER_PROCESS_PARAMETERS WINAPI
RtlDeNormalizeProcessParams( RTL_USER_PROCESS_PARAMETERS
*params
)
402 if (params
&& (params
->Flags
& PROCESS_PARAMS_FLAG_NORMALIZED
))
404 denormalize( params
, ¶ms
->CurrentDirectory
.DosPath
.Buffer
);
405 denormalize( params
, ¶ms
->DllPath
.Buffer
);
406 denormalize( params
, ¶ms
->ImagePathName
.Buffer
);
407 denormalize( params
, ¶ms
->CommandLine
.Buffer
);
408 denormalize( params
, ¶ms
->WindowTitle
.Buffer
);
409 denormalize( params
, ¶ms
->Desktop
.Buffer
);
410 denormalize( params
, ¶ms
->ShellInfo
.Buffer
);
411 denormalize( params
, ¶ms
->RuntimeInfo
.Buffer
);
412 params
->Flags
&= ~PROCESS_PARAMS_FLAG_NORMALIZED
;
418 /* append a unicode string to the process params data; helper for RtlCreateProcessParameters */
419 static void append_unicode_string( void **data
, const UNICODE_STRING
*src
,
420 UNICODE_STRING
*dst
)
422 dst
->Length
= src
->Length
;
423 dst
->MaximumLength
= src
->MaximumLength
;
425 memcpy( dst
->Buffer
, src
->Buffer
, dst
->MaximumLength
);
426 *data
= (char *)dst
->Buffer
+ dst
->MaximumLength
;
430 /******************************************************************************
431 * RtlCreateProcessParameters [NTDLL.@]
433 NTSTATUS WINAPI
RtlCreateProcessParameters( RTL_USER_PROCESS_PARAMETERS
**result
,
434 const UNICODE_STRING
*ImagePathName
,
435 const UNICODE_STRING
*DllPath
,
436 const UNICODE_STRING
*CurrentDirectoryName
,
437 const UNICODE_STRING
*CommandLine
,
439 const UNICODE_STRING
*WindowTitle
,
440 const UNICODE_STRING
*Desktop
,
441 const UNICODE_STRING
*ShellInfo
,
442 const UNICODE_STRING
*RuntimeInfo
)
444 static WCHAR empty
[] = {0};
445 static const UNICODE_STRING empty_str
= { 0, sizeof(empty
), empty
};
446 static const UNICODE_STRING null_str
= { 0, 0, NULL
};
448 const RTL_USER_PROCESS_PARAMETERS
*cur_params
;
449 SIZE_T size
, total_size
;
454 cur_params
= NtCurrentTeb()->Peb
->ProcessParameters
;
455 if (!DllPath
) DllPath
= &cur_params
->DllPath
;
456 if (!CurrentDirectoryName
)
458 if (NtCurrentTeb()->Tib
.SubSystemTib
) /* FIXME: hack */
459 CurrentDirectoryName
= &((WIN16_SUBSYSTEM_TIB
*)NtCurrentTeb()->Tib
.SubSystemTib
)->curdir
.DosPath
;
461 CurrentDirectoryName
= &cur_params
->CurrentDirectory
.DosPath
;
463 if (!CommandLine
) CommandLine
= ImagePathName
;
464 if (!Environment
) Environment
= cur_params
->Environment
;
465 if (!WindowTitle
) WindowTitle
= &empty_str
;
466 if (!Desktop
) Desktop
= &empty_str
;
467 if (!ShellInfo
) ShellInfo
= &empty_str
;
468 if (!RuntimeInfo
) RuntimeInfo
= &null_str
;
470 size
= (sizeof(RTL_USER_PROCESS_PARAMETERS
)
471 + ImagePathName
->MaximumLength
472 + DllPath
->MaximumLength
473 + CurrentDirectoryName
->MaximumLength
474 + CommandLine
->MaximumLength
475 + WindowTitle
->MaximumLength
476 + Desktop
->MaximumLength
477 + ShellInfo
->MaximumLength
478 + RuntimeInfo
->MaximumLength
);
482 if ((status
= NtAllocateVirtualMemory( NtCurrentProcess(), &ptr
, 0, &total_size
,
483 MEM_COMMIT
, PAGE_READWRITE
)) == STATUS_SUCCESS
)
485 RTL_USER_PROCESS_PARAMETERS
*params
= ptr
;
486 params
->AllocationSize
= total_size
;
488 params
->Flags
= PROCESS_PARAMS_FLAG_NORMALIZED
;
489 params
->ConsoleFlags
= cur_params
->ConsoleFlags
;
490 params
->Environment
= Environment
;
491 /* all other fields are zero */
494 append_unicode_string( &ptr
, CurrentDirectoryName
, ¶ms
->CurrentDirectory
.DosPath
);
495 append_unicode_string( &ptr
, DllPath
, ¶ms
->DllPath
);
496 append_unicode_string( &ptr
, ImagePathName
, ¶ms
->ImagePathName
);
497 append_unicode_string( &ptr
, CommandLine
, ¶ms
->CommandLine
);
498 append_unicode_string( &ptr
, WindowTitle
, ¶ms
->WindowTitle
);
499 append_unicode_string( &ptr
, Desktop
, ¶ms
->Desktop
);
500 append_unicode_string( &ptr
, ShellInfo
, ¶ms
->ShellInfo
);
501 append_unicode_string( &ptr
, RuntimeInfo
, ¶ms
->RuntimeInfo
);
502 *result
= RtlDeNormalizeProcessParams( params
);
509 /******************************************************************************
510 * RtlDestroyProcessParameters [NTDLL.@]
512 void WINAPI
RtlDestroyProcessParameters( RTL_USER_PROCESS_PARAMETERS
*params
)
516 NtFreeVirtualMemory( NtCurrentProcess(), &ptr
, &size
, MEM_RELEASE
);