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"
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(),
52 NtCurrentTeb()->Peb
->ProcessParameters
->Environment
,
53 0, &mbi
, sizeof(mbi
), NULL
);
54 if (nts
== STATUS_SUCCESS
)
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
);
69 nts
= NtAllocateVirtualMemory(NtCurrentProcess(), &addr
, 0, &size
,
70 MEM_RESERVE
| MEM_COMMIT
, PAGE_READWRITE
);
71 if (nts
== STATUS_SUCCESS
) *env
= addr
;
77 /******************************************************************************
78 * RtlDestroyEnvironment [NTDLL.@]
80 NTSTATUS WINAPI
RtlDestroyEnvironment(PWSTR env
)
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;
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
;
121 TRACE("%p %s %p\n", env
, debugstr_us(name
), value
);
124 namelen
= name
->Length
/ sizeof(WCHAR
);
125 if (!namelen
) return nts
;
130 var
= NtCurrentTeb()->Peb
->ProcessParameters
->Environment
;
134 var
= ENV_FindVariable(var
, name
->Buffer
, namelen
);
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();
152 /******************************************************************
153 * RtlSetCurrentEnvironment [NTDLL.@]
156 void WINAPI
RtlSetCurrentEnvironment(PWSTR new_env
, PWSTR
* old_env
)
158 TRACE("(%p %p)\n", new_env
, old_env
);
162 if (old_env
) *old_env
= NtCurrentTeb()->Peb
->ProcessParameters
->Environment
;
163 NtCurrentTeb()->Peb
->ProcessParameters
->Environment
= new_env
;
169 /******************************************************************************
170 * RtlSetEnvironmentVariable [NTDLL.@]
172 NTSTATUS WINAPI
RtlSetEnvironmentVariable(PWSTR
* penv
, PUNICODE_STRING name
,
173 PUNICODE_STRING value
)
177 NTSTATUS nts
= STATUS_VARIABLE_NOT_FOUND
;
178 MEMORY_BASIC_INFORMATION mbi
;
180 TRACE("(%p, %s, %s)\n", penv
, debugstr_us(name
), debugstr_us(value
));
182 if (!name
|| !name
->Buffer
|| !name
->Length
)
183 return STATUS_INVALID_PARAMETER_1
;
185 len
= name
->Length
/ sizeof(WCHAR
);
187 /* variable names can't contain a '=' except as a first character */
188 for (p
= name
->Buffer
+ 1; p
< name
->Buffer
+ len
; p
++)
189 if (*p
== '=') return STATUS_INVALID_PARAMETER
;
194 env
= NtCurrentTeb()->Peb
->ProcessParameters
->Environment
;
197 /* compute current size of environment */
198 for (p
= env
; *p
; p
+= strlenW(p
) + 1);
199 old_size
= p
+ 1 - env
;
201 /* Find a place to insert the string */
202 for (p
= env
; *p
; p
+= strlenW(p
) + 1)
204 if (!strncmpiW(name
->Buffer
, p
, len
) && (p
[len
] == '=')) break;
206 if (!value
&& !*p
) goto done
; /* Value to remove doesn't exist */
208 /* Realloc the buffer */
209 len
= value
? len
+ value
->Length
/ sizeof(WCHAR
) + 2 : 0;
210 if (*p
) len
-= strlenW(p
) + 1; /* The name already exists */
214 LPWSTR next
= p
+ strlenW(p
) + 1; /* We know there is a next one */
215 memmove(next
+ len
, next
, (old_size
- (next
- env
)) * sizeof(WCHAR
));
218 nts
= NtQueryVirtualMemory(NtCurrentProcess(), env
, 0,
219 &mbi
, sizeof(mbi
), NULL
);
220 if (nts
!= STATUS_SUCCESS
) goto done
;
222 if ((old_size
+ len
) * sizeof(WCHAR
) > mbi
.RegionSize
)
225 SIZE_T new_size
= (old_size
+ len
) * sizeof(WCHAR
);
228 nts
= NtAllocateVirtualMemory(NtCurrentProcess(), (void**)&new_env
, 0,
229 &new_size
, MEM_RESERVE
| MEM_COMMIT
, PAGE_READWRITE
);
230 if (nts
!= STATUS_SUCCESS
) goto done
;
232 memmove(new_env
, env
, (p
- env
) * sizeof(WCHAR
));
234 memmove(new_env
+ (p
- env
) + len
, p
, (old_size
- (p
- env
)) * sizeof(WCHAR
));
235 p
= new_env
+ (p
- env
);
237 RtlDestroyEnvironment(env
);
238 if (!penv
) NtCurrentTeb()->Peb
->ProcessParameters
->Environment
= new_env
;
239 else *penv
= new_env
;
243 if (len
> 0) memmove(p
+ len
, p
, (old_size
- (p
- env
)) * sizeof(WCHAR
));
246 /* Set the new string */
249 memcpy( p
, name
->Buffer
, name
->Length
);
250 p
+= name
->Length
/ sizeof(WCHAR
);
252 memcpy( p
, value
->Buffer
, value
->Length
);
253 p
[value
->Length
/ sizeof(WCHAR
)] = 0;
256 if (!penv
) RtlReleasePebLock();
261 /******************************************************************
262 * RtlExpandEnvironmentStrings_U (NTDLL.@)
265 NTSTATUS WINAPI
RtlExpandEnvironmentStrings_U(PCWSTR renv
, const UNICODE_STRING
* us_src
,
266 PUNICODE_STRING us_dst
, PULONG plen
)
268 DWORD src_len
, len
, count
, total_size
= 1; /* 1 for terminating '\0' */
269 LPCWSTR env
, src
, p
, var
;
272 src
= us_src
->Buffer
;
273 src_len
= us_src
->Length
/ sizeof(WCHAR
);
274 count
= us_dst
->MaximumLength
/ sizeof(WCHAR
);
275 dst
= count
? us_dst
->Buffer
: NULL
;
280 env
= NtCurrentTeb()->Peb
->ProcessParameters
->Environment
;
288 if ((p
= memchrW( src
, '%', src_len
))) len
= p
- src
;
294 else /* we are at the start of a variable */
296 if ((p
= memchrW( src
+ 1, '%', src_len
- 1 )))
298 len
= p
- src
- 1; /* Length of the variable name */
299 if ((var
= ENV_FindVariable( env
, src
+ 1, len
)))
301 src
+= len
+ 2; /* Skip the variable name */
307 var
= src
; /* Copy original name instead */
313 else /* unfinished variable name, ignore it */
316 len
= src_len
; /* Copy whole string */
324 if (count
< len
) len
= count
;
325 memcpy(dst
, var
, len
* sizeof(WCHAR
));
331 if (!renv
) RtlReleasePebLock();
333 /* Null-terminate the string */
334 if (dst
&& count
) *dst
= '\0';
336 us_dst
->Length
= (dst
) ? (dst
- us_dst
->Buffer
) * sizeof(WCHAR
) : 0;
337 if (plen
) *plen
= total_size
* sizeof(WCHAR
);
339 return (count
) ? STATUS_SUCCESS
: STATUS_BUFFER_TOO_SMALL
;
343 static inline void normalize( void *base
, WCHAR
**ptr
)
345 if (*ptr
) *ptr
= (WCHAR
*)((char *)base
+ (UINT_PTR
)*ptr
);
348 /******************************************************************************
349 * RtlNormalizeProcessParams [NTDLL.@]
351 PRTL_USER_PROCESS_PARAMETERS WINAPI
RtlNormalizeProcessParams( RTL_USER_PROCESS_PARAMETERS
*params
)
353 if (params
&& !(params
->Flags
& PROCESS_PARAMS_FLAG_NORMALIZED
))
355 normalize( params
, ¶ms
->CurrentDirectory
.DosPath
.Buffer
);
356 normalize( params
, ¶ms
->DllPath
.Buffer
);
357 normalize( params
, ¶ms
->ImagePathName
.Buffer
);
358 normalize( params
, ¶ms
->CommandLine
.Buffer
);
359 normalize( params
, ¶ms
->WindowTitle
.Buffer
);
360 normalize( params
, ¶ms
->Desktop
.Buffer
);
361 normalize( params
, ¶ms
->ShellInfo
.Buffer
);
362 normalize( params
, ¶ms
->RuntimeInfo
.Buffer
);
363 params
->Flags
|= PROCESS_PARAMS_FLAG_NORMALIZED
;
369 static inline void denormalize( const void *base
, WCHAR
**ptr
)
371 if (*ptr
) *ptr
= (WCHAR
*)(UINT_PTR
)((char *)*ptr
- (const char *)base
);
374 /******************************************************************************
375 * RtlDeNormalizeProcessParams [NTDLL.@]
377 PRTL_USER_PROCESS_PARAMETERS WINAPI
RtlDeNormalizeProcessParams( RTL_USER_PROCESS_PARAMETERS
*params
)
379 if (params
&& (params
->Flags
& PROCESS_PARAMS_FLAG_NORMALIZED
))
381 denormalize( params
, ¶ms
->CurrentDirectory
.DosPath
.Buffer
);
382 denormalize( params
, ¶ms
->DllPath
.Buffer
);
383 denormalize( params
, ¶ms
->ImagePathName
.Buffer
);
384 denormalize( params
, ¶ms
->CommandLine
.Buffer
);
385 denormalize( params
, ¶ms
->WindowTitle
.Buffer
);
386 denormalize( params
, ¶ms
->Desktop
.Buffer
);
387 denormalize( params
, ¶ms
->ShellInfo
.Buffer
);
388 denormalize( params
, ¶ms
->RuntimeInfo
.Buffer
);
389 params
->Flags
&= ~PROCESS_PARAMS_FLAG_NORMALIZED
;
395 /* append a unicode string to the process params data; helper for RtlCreateProcessParameters */
396 static void append_unicode_string( void **data
, const UNICODE_STRING
*src
,
397 UNICODE_STRING
*dst
)
399 dst
->Length
= src
->Length
;
400 dst
->MaximumLength
= src
->MaximumLength
;
402 memcpy( dst
->Buffer
, src
->Buffer
, dst
->MaximumLength
);
403 *data
= (char *)dst
->Buffer
+ dst
->MaximumLength
;
407 /******************************************************************************
408 * RtlCreateProcessParameters [NTDLL.@]
410 NTSTATUS WINAPI
RtlCreateProcessParameters( RTL_USER_PROCESS_PARAMETERS
**result
,
411 const UNICODE_STRING
*ImagePathName
,
412 const UNICODE_STRING
*DllPath
,
413 const UNICODE_STRING
*CurrentDirectoryName
,
414 const UNICODE_STRING
*CommandLine
,
416 const UNICODE_STRING
*WindowTitle
,
417 const UNICODE_STRING
*Desktop
,
418 const UNICODE_STRING
*ShellInfo
,
419 const UNICODE_STRING
*RuntimeInfo
)
421 static WCHAR empty
[] = {0};
422 static const UNICODE_STRING empty_str
= { 0, sizeof(empty
), empty
};
423 static const UNICODE_STRING null_str
= { 0, 0, NULL
};
425 const RTL_USER_PROCESS_PARAMETERS
*cur_params
;
426 SIZE_T size
, total_size
;
431 cur_params
= NtCurrentTeb()->Peb
->ProcessParameters
;
432 if (!DllPath
) DllPath
= &cur_params
->DllPath
;
433 if (!CurrentDirectoryName
)
435 if (NtCurrentTeb()->Tib
.SubSystemTib
) /* FIXME: hack */
436 CurrentDirectoryName
= &((WIN16_SUBSYSTEM_TIB
*)NtCurrentTeb()->Tib
.SubSystemTib
)->curdir
.DosPath
;
438 CurrentDirectoryName
= &cur_params
->CurrentDirectory
.DosPath
;
440 if (!CommandLine
) CommandLine
= ImagePathName
;
441 if (!Environment
) Environment
= cur_params
->Environment
;
442 if (!WindowTitle
) WindowTitle
= &empty_str
;
443 if (!Desktop
) Desktop
= &empty_str
;
444 if (!ShellInfo
) ShellInfo
= &empty_str
;
445 if (!RuntimeInfo
) RuntimeInfo
= &null_str
;
447 size
= (sizeof(RTL_USER_PROCESS_PARAMETERS
)
448 + ImagePathName
->MaximumLength
449 + DllPath
->MaximumLength
450 + CurrentDirectoryName
->MaximumLength
451 + CommandLine
->MaximumLength
452 + WindowTitle
->MaximumLength
453 + Desktop
->MaximumLength
454 + ShellInfo
->MaximumLength
455 + RuntimeInfo
->MaximumLength
);
459 if ((status
= NtAllocateVirtualMemory( NtCurrentProcess(), &ptr
, 0, &total_size
,
460 MEM_COMMIT
, PAGE_READWRITE
)) == STATUS_SUCCESS
)
462 RTL_USER_PROCESS_PARAMETERS
*params
= ptr
;
463 params
->AllocationSize
= total_size
;
465 params
->Flags
= PROCESS_PARAMS_FLAG_NORMALIZED
;
466 params
->ConsoleFlags
= cur_params
->ConsoleFlags
;
467 params
->Environment
= Environment
;
468 /* all other fields are zero */
471 append_unicode_string( &ptr
, CurrentDirectoryName
, ¶ms
->CurrentDirectory
.DosPath
);
472 append_unicode_string( &ptr
, DllPath
, ¶ms
->DllPath
);
473 append_unicode_string( &ptr
, ImagePathName
, ¶ms
->ImagePathName
);
474 append_unicode_string( &ptr
, CommandLine
, ¶ms
->CommandLine
);
475 append_unicode_string( &ptr
, WindowTitle
, ¶ms
->WindowTitle
);
476 append_unicode_string( &ptr
, Desktop
, ¶ms
->Desktop
);
477 append_unicode_string( &ptr
, ShellInfo
, ¶ms
->ShellInfo
);
478 append_unicode_string( &ptr
, RuntimeInfo
, ¶ms
->RuntimeInfo
);
479 *result
= RtlDeNormalizeProcessParams( params
);
486 /******************************************************************************
487 * RtlDestroyProcessParameters [NTDLL.@]
489 void WINAPI
RtlDestroyProcessParameters( RTL_USER_PROCESS_PARAMETERS
*params
)
493 NtFreeVirtualMemory( NtCurrentProcess(), &ptr
, &size
, MEM_RELEASE
);