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
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
)
43 TRACE("(%u,%p)!\n", inherit
, env
);
47 MEMORY_BASIC_INFORMATION mbi
;
51 nts
= NtQueryVirtualMemory(NtCurrentProcess(), ntdll_get_process_pmts()->Environment
,
52 0, &mbi
, sizeof(mbi
), NULL
);
53 if (nts
== STATUS_SUCCESS
)
56 nts
= NtAllocateVirtualMemory(NtCurrentProcess(), (void**)env
, 0, &mbi
.RegionSize
,
57 MEM_RESERVE
| MEM_COMMIT
, PAGE_READWRITE
);
58 if (nts
== STATUS_SUCCESS
)
59 memcpy(*env
, ntdll_get_process_pmts()->Environment
, mbi
.RegionSize
);
67 nts
= NtAllocateVirtualMemory(NtCurrentProcess(), (void**)env
, 0, &size
,
68 MEM_RESERVE
| MEM_COMMIT
, PAGE_READWRITE
);
69 if (nts
== STATUS_SUCCESS
)
70 memset(*env
, 0, size
);
76 /******************************************************************************
77 * RtlDestroyEnvironment [NTDLL.@]
79 NTSTATUS WINAPI
RtlDestroyEnvironment(PWSTR env
)
83 TRACE("(%p)!\n", env
);
85 return NtFreeVirtualMemory(NtCurrentProcess(), (void**)&env
, &size
, MEM_RELEASE
);
88 static LPCWSTR
ENV_FindVariable(PCWSTR var
, PCWSTR name
, unsigned namelen
)
90 for (; *var
; var
+= strlenW(var
) + 1)
92 /* match var names, but avoid setting a var with a name including a '='
93 * (a starting '=' is valid though)
95 if (strncmpiW(var
, name
, namelen
) == 0 && var
[namelen
] == '=' &&
96 strchrW(var
+ 1, '=') == var
+ namelen
)
98 return var
+ namelen
+ 1;
104 /******************************************************************
105 * RtlQueryEnvironmentVariable_U [NTDLL.@]
107 * NOTES: when the buffer is too small, the string is not written, but if the
108 * terminating null char is the only char that cannot be written, then
109 * all chars (except the null) are written and success is returned
110 * (behavior of Win2k at least)
112 NTSTATUS WINAPI
RtlQueryEnvironmentVariable_U(PWSTR env
,
113 PUNICODE_STRING name
,
114 PUNICODE_STRING value
)
116 NTSTATUS nts
= STATUS_VARIABLE_NOT_FOUND
;
120 TRACE("%s %s %p\n", debugstr_w(env
), debugstr_w(name
->Buffer
), value
);
123 namelen
= name
->Length
/ sizeof(WCHAR
);
124 if (!namelen
) return nts
;
129 var
= ntdll_get_process_pmts()->Environment
;
133 var
= ENV_FindVariable(var
, name
->Buffer
, namelen
);
136 value
->Length
= strlenW(var
) * sizeof(WCHAR
);
138 if (value
->Length
<= value
->MaximumLength
)
140 memmove(value
->Buffer
, var
, min(value
->Length
+ sizeof(WCHAR
), value
->MaximumLength
));
141 nts
= STATUS_SUCCESS
;
143 else nts
= STATUS_BUFFER_TOO_SMALL
;
146 if (!env
) RtlReleasePebLock();
151 /******************************************************************
152 * RtlSetCurrentEnvironment [NTDLL.@]
155 void WINAPI
RtlSetCurrentEnvironment(PWSTR new_env
, PWSTR
* old_env
)
157 TRACE("(%p %p)\n", new_env
, old_env
);
161 if (old_env
) *old_env
= ntdll_get_process_pmts()->Environment
;
162 ntdll_get_process_pmts()->Environment
= new_env
;
168 /******************************************************************************
169 * RtlSetEnvironmentVariable [NTDLL.@]
171 NTSTATUS WINAPI
RtlSetEnvironmentVariable(PWSTR
* penv
, PUNICODE_STRING name
,
172 PUNICODE_STRING value
)
176 NTSTATUS nts
= STATUS_VARIABLE_NOT_FOUND
;
177 MEMORY_BASIC_INFORMATION mbi
;
179 TRACE("(%p,%s,%s)\n",
180 penv
, debugstr_w(name
->Buffer
),
181 value
? debugstr_w(value
->Buffer
) : "--nil--");
183 if (!name
|| !name
->Buffer
|| !name
->Buffer
[0])
184 return STATUS_INVALID_PARAMETER_1
;
185 /* variable names can't contain a '=' except as a first character */
186 if (strchrW(name
->Buffer
+ 1, '=')) return STATUS_INVALID_PARAMETER
;
191 env
= ntdll_get_process_pmts()->Environment
;
194 len
= name
->Length
/ sizeof(WCHAR
);
196 /* compute current size of environment */
197 for (p
= env
; *p
; p
+= strlenW(p
) + 1);
198 old_size
= p
+ 1 - env
;
200 /* Find a place to insert the string */
201 for (p
= env
; *p
; p
+= strlenW(p
) + 1)
203 if (!strncmpiW(name
->Buffer
, p
, len
) && (p
[len
] == '=')) break;
205 if (!value
&& !*p
) goto done
; /* Value to remove doesn't exist */
207 /* Realloc the buffer */
208 len
= value
? len
+ value
->Length
/ sizeof(WCHAR
) + 2 : 0;
209 if (*p
) len
-= strlenW(p
) + 1; /* The name already exists */
213 LPWSTR next
= p
+ strlenW(p
) + 1; /* We know there is a next one */
214 memmove(next
+ len
, next
, (old_size
- (next
- env
)) * sizeof(WCHAR
));
217 nts
= NtQueryVirtualMemory(NtCurrentProcess(), env
, 0,
218 &mbi
, sizeof(mbi
), NULL
);
219 if (nts
!= STATUS_SUCCESS
) goto done
;
221 if ((old_size
+ len
) * sizeof(WCHAR
) > mbi
.RegionSize
)
224 ULONG new_size
= (old_size
+ len
) * sizeof(WCHAR
);
227 nts
= NtAllocateVirtualMemory(NtCurrentProcess(), (void**)&new_env
, 0,
228 &new_size
, MEM_RESERVE
| MEM_COMMIT
, PAGE_READWRITE
);
229 if (nts
!= STATUS_SUCCESS
) goto done
;
231 memmove(new_env
, env
, (p
- env
) * sizeof(WCHAR
));
233 memmove(new_env
+ (p
- env
) + len
, p
, (old_size
- (p
- env
)) * sizeof(WCHAR
));
234 p
= new_env
+ (p
- env
);
236 RtlDestroyEnvironment(env
);
237 if (!penv
) ntdll_get_process_pmts()->Environment
= new_env
;
238 else *penv
= new_env
;
243 if (len
> 0) memmove(p
+ len
, p
, (old_size
- (p
- env
)) * sizeof(WCHAR
));
246 /* Set the new string */
249 static const WCHAR equalW
[] = {'=',0};
251 strcpyW(p
, name
->Buffer
);
253 strcatW(p
, value
->Buffer
);
256 if (!penv
) RtlReleasePebLock();
261 /******************************************************************
262 * RtlExpandEnvironmentStrings_U (NTDLL.@)
265 NTSTATUS WINAPI
RtlExpandEnvironmentStrings_U(PWSTR renv
, const UNICODE_STRING
* us_src
,
266 PUNICODE_STRING us_dst
, PULONG plen
)
268 DWORD len
, count
, total_size
= 1; /* 1 for terminating '\0' */
269 LPCWSTR env
, src
, p
, var
;
272 src
= us_src
->Buffer
;
273 count
= us_dst
->MaximumLength
/ sizeof(WCHAR
);
274 dst
= count
? us_dst
->Buffer
: NULL
;
279 env
= ntdll_get_process_pmts()->Environment
;
287 if ((p
= strchrW( src
, '%' ))) len
= p
- src
;
288 else len
= strlenW(src
);
292 else /* we are at the start of a variable */
294 if ((p
= strchrW( src
+ 1, '%' )))
296 len
= p
- src
- 1; /* Length of the variable name */
297 if ((var
= ENV_FindVariable( env
, src
+ 1, len
)))
299 src
+= len
+ 2; /* Skip the variable name */
304 var
= src
; /* Copy original name instead */
309 else /* unfinished variable name, ignore it */
312 len
= strlenW(src
); /* Copy whole string */
319 if (count
< len
) len
= count
;
320 memcpy(dst
, var
, len
* sizeof(WCHAR
));
326 if (!renv
) RtlReleasePebLock();
328 /* Null-terminate the string */
329 if (dst
&& count
) *dst
= '\0';
331 us_dst
->Length
= (dst
) ? (dst
- us_dst
->Buffer
) * sizeof(WCHAR
) : 0;
332 if (plen
) *plen
= total_size
* sizeof(WCHAR
);
334 return (count
) ? STATUS_SUCCESS
: STATUS_BUFFER_TOO_SMALL
;
338 static inline void normalize( void *base
, WCHAR
**ptr
)
340 if (*ptr
) *ptr
= (WCHAR
*)((char *)base
+ (UINT_PTR
)*ptr
);
343 /******************************************************************************
344 * RtlNormalizeProcessParams [NTDLL.@]
346 PRTL_USER_PROCESS_PARAMETERS WINAPI
RtlNormalizeProcessParams( RTL_USER_PROCESS_PARAMETERS
*params
)
348 if (params
&& !(params
->Flags
& PROCESS_PARAMS_FLAG_NORMALIZED
))
350 normalize( params
, ¶ms
->CurrentDirectoryName
.Buffer
);
351 normalize( params
, ¶ms
->DllPath
.Buffer
);
352 normalize( params
, ¶ms
->ImagePathName
.Buffer
);
353 normalize( params
, ¶ms
->CommandLine
.Buffer
);
354 normalize( params
, ¶ms
->WindowTitle
.Buffer
);
355 normalize( params
, ¶ms
->Desktop
.Buffer
);
356 normalize( params
, ¶ms
->ShellInfo
.Buffer
);
357 normalize( params
, ¶ms
->RuntimeInfo
.Buffer
);
358 params
->Flags
|= PROCESS_PARAMS_FLAG_NORMALIZED
;
364 static inline void denormalize( void *base
, WCHAR
**ptr
)
366 if (*ptr
) *ptr
= (WCHAR
*)(UINT_PTR
)((char *)*ptr
- (char *)base
);
369 /******************************************************************************
370 * RtlDeNormalizeProcessParams [NTDLL.@]
372 PRTL_USER_PROCESS_PARAMETERS WINAPI
RtlDeNormalizeProcessParams( RTL_USER_PROCESS_PARAMETERS
*params
)
374 if (params
&& (params
->Flags
& PROCESS_PARAMS_FLAG_NORMALIZED
))
376 denormalize( params
, ¶ms
->CurrentDirectoryName
.Buffer
);
377 denormalize( params
, ¶ms
->DllPath
.Buffer
);
378 denormalize( params
, ¶ms
->ImagePathName
.Buffer
);
379 denormalize( params
, ¶ms
->CommandLine
.Buffer
);
380 denormalize( params
, ¶ms
->WindowTitle
.Buffer
);
381 denormalize( params
, ¶ms
->Desktop
.Buffer
);
382 denormalize( params
, ¶ms
->ShellInfo
.Buffer
);
383 denormalize( params
, ¶ms
->RuntimeInfo
.Buffer
);
384 params
->Flags
&= ~PROCESS_PARAMS_FLAG_NORMALIZED
;
390 /* append a unicode string to the process params data; helper for RtlCreateProcessParameters */
391 static void append_unicode_string( void **data
, const UNICODE_STRING
*src
,
392 UNICODE_STRING
*dst
)
394 dst
->Length
= src
->Length
;
395 dst
->MaximumLength
= src
->MaximumLength
;
397 memcpy( dst
->Buffer
, src
->Buffer
, dst
->MaximumLength
);
398 *data
= (char *)dst
->Buffer
+ dst
->MaximumLength
;
402 /******************************************************************************
403 * RtlCreateProcessParameters [NTDLL.@]
405 NTSTATUS WINAPI
RtlCreateProcessParameters( RTL_USER_PROCESS_PARAMETERS
**result
,
406 const UNICODE_STRING
*ImagePathName
,
407 const UNICODE_STRING
*DllPath
,
408 const UNICODE_STRING
*CurrentDirectoryName
,
409 const UNICODE_STRING
*CommandLine
,
411 const UNICODE_STRING
*WindowTitle
,
412 const UNICODE_STRING
*Desktop
,
413 const UNICODE_STRING
*ShellInfo
,
414 const UNICODE_STRING
*RuntimeInfo
)
416 static const WCHAR empty
[] = {0};
417 static const UNICODE_STRING empty_str
= { 0, sizeof(empty
), (WCHAR
*)empty
};
419 const RTL_USER_PROCESS_PARAMETERS
*cur_params
;
420 ULONG size
, total_size
;
425 cur_params
= NtCurrentTeb()->Peb
->ProcessParameters
;
426 if (!DllPath
) DllPath
= &cur_params
->DllPath
;
427 if (!CurrentDirectoryName
) CurrentDirectoryName
= &cur_params
->CurrentDirectoryName
;
428 if (!CommandLine
) CommandLine
= ImagePathName
;
429 if (!Environment
) Environment
= cur_params
->Environment
;
430 if (!WindowTitle
) WindowTitle
= &empty_str
;
431 if (!Desktop
) Desktop
= &empty_str
;
432 if (!ShellInfo
) ShellInfo
= &empty_str
;
433 if (!RuntimeInfo
) RuntimeInfo
= &empty_str
;
435 size
= (sizeof(RTL_USER_PROCESS_PARAMETERS
)
436 + ImagePathName
->MaximumLength
437 + DllPath
->MaximumLength
438 + CurrentDirectoryName
->MaximumLength
439 + CommandLine
->MaximumLength
440 + WindowTitle
->MaximumLength
441 + Desktop
->MaximumLength
442 + ShellInfo
->MaximumLength
443 + RuntimeInfo
->MaximumLength
);
446 if ((status
= NtAllocateVirtualMemory( NtCurrentProcess(), &ptr
, NULL
, &total_size
,
447 MEM_COMMIT
, PAGE_READWRITE
)) == STATUS_SUCCESS
)
449 RTL_USER_PROCESS_PARAMETERS
*params
= ptr
;
450 params
->AllocationSize
= total_size
;
452 params
->Flags
= PROCESS_PARAMS_FLAG_NORMALIZED
;
453 params
->ProcessGroup
= cur_params
->ProcessGroup
;
454 params
->Environment
= Environment
;
455 /* all other fields are zero */
458 append_unicode_string( &ptr
, CurrentDirectoryName
, ¶ms
->CurrentDirectoryName
);
459 append_unicode_string( &ptr
, DllPath
, ¶ms
->DllPath
);
460 append_unicode_string( &ptr
, ImagePathName
, ¶ms
->ImagePathName
);
461 append_unicode_string( &ptr
, CommandLine
, ¶ms
->CommandLine
);
462 append_unicode_string( &ptr
, WindowTitle
, ¶ms
->WindowTitle
);
463 append_unicode_string( &ptr
, Desktop
, ¶ms
->Desktop
);
464 append_unicode_string( &ptr
, ShellInfo
, ¶ms
->ShellInfo
);
465 append_unicode_string( &ptr
, RuntimeInfo
, ¶ms
->RuntimeInfo
);
466 *result
= RtlDeNormalizeProcessParams( params
);
473 /******************************************************************************
474 * RtlDestroyProcessParameters [NTDLL.@]
476 void WINAPI
RtlDestroyProcessParameters( RTL_USER_PROCESS_PARAMETERS
*params
)
480 NtFreeVirtualMemory( NtCurrentProcess(), &ptr
, &size
, MEM_RELEASE
);