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
26 #include "wine/unicode.h"
27 #include "wine/debug.h"
28 #include "ntdll_misc.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(environ);
32 /******************************************************************************
33 * RtlCreateEnvironment [NTDLL.@]
35 NTSTATUS WINAPI RtlCreateEnvironment(BOOLEAN inherit, PWSTR* env)
39 TRACE("(%u,%p)!\n", inherit, env);
43 MEMORY_BASIC_INFORMATION mbi;
47 nts = NtQueryVirtualMemory(NtCurrentProcess(), ntdll_get_process_pmts()->Environment,
48 0, &mbi, sizeof(mbi), NULL);
49 if (nts == STATUS_SUCCESS)
52 nts = NtAllocateVirtualMemory(NtCurrentProcess(), (void**)env, 0, &mbi.RegionSize,
53 MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
54 if (nts == STATUS_SUCCESS)
55 memcpy(*env, ntdll_get_process_pmts()->Environment, mbi.RegionSize);
63 nts = NtAllocateVirtualMemory(NtCurrentProcess(), (void**)env, 0, &size,
64 MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
65 if (nts == STATUS_SUCCESS)
66 memset(*env, 0, size);
72 /******************************************************************************
73 * RtlDestroyEnvironment [NTDLL.@]
75 NTSTATUS WINAPI RtlDestroyEnvironment(PWSTR env)
79 TRACE("(%p)!\n", env);
81 return NtFreeVirtualMemory(NtCurrentProcess(), (void**)&env, &size, MEM_RELEASE);
84 static LPCWSTR ENV_FindVariable(PCWSTR var, PCWSTR name, unsigned namelen)
86 for (; *var; var += strlenW(var) + 1)
88 /* match var names, but avoid setting a var with a name including a '='
89 * (a starting '=' is valid though)
91 if (strncmpiW(var, name, namelen) == 0 && var[namelen] == '=' &&
92 strchrW(var + 1, '=') == var + namelen)
94 return var + namelen + 1;
100 /******************************************************************
101 * RtlQueryEnvironmentVariable_U [NTDLL.@]
103 * NOTES: when the buffer is too small, the string is not written, but if the
104 * terminating null char is the only char that cannot be written, then
105 * all chars (except the null) are written and success is returned
106 * (behavior of Win2k at least)
108 NTSTATUS WINAPI RtlQueryEnvironmentVariable_U(PWSTR env,
109 PUNICODE_STRING name,
110 PUNICODE_STRING value)
112 NTSTATUS nts = STATUS_VARIABLE_NOT_FOUND;
116 TRACE("%s %s %p\n", debugstr_w(env), debugstr_w(name->Buffer), value);
119 namelen = name->Length / sizeof(WCHAR);
120 if (!namelen) return nts;
125 var = ntdll_get_process_pmts()->Environment;
129 var = ENV_FindVariable(var, name->Buffer, namelen);
132 value->Length = strlenW(var) * sizeof(WCHAR);
133 if (value->Length <= value->MaximumLength)
135 memmove(value->Buffer, var, min(value->Length + sizeof(WCHAR), value->MaximumLength));
136 nts = STATUS_SUCCESS;
138 else nts = STATUS_BUFFER_TOO_SMALL;
141 if (!env) RtlReleasePebLock();
146 /******************************************************************
147 * RtlSetCurrentEnvironment [NTDLL.@]
150 void WINAPI RtlSetCurrentEnvironment(PWSTR new_env, PWSTR* old_env)
152 TRACE("(%p %p)\n", new_env, old_env);
156 if (old_env) *old_env = ntdll_get_process_pmts()->Environment;
157 ntdll_get_process_pmts()->Environment = new_env;
163 /******************************************************************************
164 * RtlSetEnvironmentVariable [NTDLL.@]
166 NTSTATUS WINAPI RtlSetEnvironmentVariable(PWSTR* penv, PUNICODE_STRING name,
167 PUNICODE_STRING value)
171 NTSTATUS nts = STATUS_VARIABLE_NOT_FOUND;
172 MEMORY_BASIC_INFORMATION mbi;
174 TRACE("(%p,%s,%s): stub!\n", penv, debugstr_w(name->Buffer), debugstr_w(value->Buffer));
176 if (!name || !name->Buffer || !name->Buffer[0])
177 return STATUS_INVALID_PARAMETER_1;
178 /* variable names can't contain a '=' except as a first character */
179 if (strchrW(name->Buffer + 1, '=')) return STATUS_INVALID_PARAMETER;
184 env = ntdll_get_process_pmts()->Environment;
187 len = name->Length / sizeof(WCHAR);
189 /* compute current size of environment */
190 for (p = env; *p; p += strlenW(p) + 1);
191 old_size = p + 1 - env;
193 /* Find a place to insert the string */
194 for (p = env; *p; p += strlenW(p) + 1)
196 if (!strncmpiW(name->Buffer, p, len) && (p[len] == '=')) break;
198 if (!value && !*p) goto done; /* Value to remove doesn't exist */
200 /* Realloc the buffer */
201 len = value ? len + value->Length / sizeof(WCHAR) + 2 : 0;
202 if (*p) len -= strlenW(p) + 1; /* The name already exists */
206 LPWSTR next = p + strlenW(p) + 1; /* We know there is a next one */
207 memmove(next + len, next, (old_size - (next - env)) * sizeof(WCHAR));
210 nts = NtQueryVirtualMemory(NtCurrentProcess(), env, 0,
211 &mbi, sizeof(mbi), NULL);
212 if (nts != STATUS_SUCCESS) goto done;
214 if ((old_size + len) * sizeof(WCHAR) > mbi.RegionSize)
217 ULONG new_size = (old_size + len) * sizeof(WCHAR);
220 nts = NtAllocateVirtualMemory(NtCurrentProcess(), (void**)&new_env, 0,
221 &new_size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
222 if (nts != STATUS_SUCCESS) goto done;
224 memmove(new_env, env, (p - env) * sizeof(WCHAR));
226 memmove(new_env + (p - env) + len, p, (old_size - (p - env)) * sizeof(WCHAR));
227 p = new_env + (p - env);
229 RtlDestroyEnvironment(env);
230 if (!penv) ntdll_get_process_pmts()->Environment = new_env;
231 else *penv = new_env;
236 if (len > 0) memmove(p + len, p, (old_size - (p - env)) * sizeof(WCHAR));
239 /* Set the new string */
242 static const WCHAR equalW[] = {'=',0};
244 strcpyW(p, name->Buffer);
246 strcatW(p, value->Buffer);
250 if (!penv) RtlReleasePebLock();
255 /******************************************************************
256 * RtlExpandEnvironmentStrings_U (NTDLL.@)
259 NTSTATUS WINAPI RtlExpandEnvironmentStrings_U(PWSTR env, const UNICODE_STRING* us_src,
260 PUNICODE_STRING us_dst, PULONG plen)
262 DWORD len, count, total_size = 1; /* 1 for terminating '\0' */
266 src = us_src->Buffer;
267 count = us_dst->MaximumLength / sizeof(WCHAR);
268 dst = count ? us_dst->Buffer : NULL;
276 if ((p = strchrW( src, '%' ))) len = p - src;
277 else len = strlenW(src);
281 else /* we are at the start of a variable */
283 if ((p = strchrW( src + 1, '%' )))
285 len = p - src - 1; /* Length of the variable name */
286 if ((var = ENV_FindVariable( env, src + 1, len )))
288 src += len + 2; /* Skip the variable name */
293 var = src; /* Copy original name instead */
298 else /* unfinished variable name, ignore it */
301 len = strlenW(src); /* Copy whole string */
308 if (count < len) len = count;
309 memcpy(dst, var, len * sizeof(WCHAR));
317 /* Null-terminate the string */
318 if (dst && count) *dst = '\0';
320 us_dst->Length = (dst) ? (dst - us_dst->Buffer) * sizeof(WCHAR): 0;
321 if (plen) *plen = total_size * sizeof(WCHAR);
323 return (count) ? STATUS_SUCCESS : STATUS_BUFFER_TOO_SMALL;
326 /***********************************************************************
329 * Build the Win32 environment from the Unix environment
331 BOOL build_initial_environment(void)
333 extern char **environ;
340 /* Compute the total size of the Unix environment */
342 for (e = environ; *e; e++)
344 if (!memcmp(*e, "PATH=", 5)) continue;
345 size += strlen(*e) + 1;
347 size *= sizeof(WCHAR);
349 /* Now allocate the environment */
350 nts = NtAllocateVirtualMemory(NtCurrentProcess(), (void**)&p, 0, &size,
351 MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
352 if (nts != STATUS_SUCCESS) return FALSE;
354 ntdll_get_process_pmts()->Environment = p;
355 /* And fill it with the Unix environment */
356 for (e = environ; *e; e++)
358 /* skip Unix PATH and store WINEPATH as PATH */
359 if (!memcmp(*e, "PATH=", 5)) continue;
360 if (!memcmp(*e, "WINEPATH=", 9 )) te = *e + 4; else te = *e;
362 RtlMultiByteToUnicodeN(p, len * sizeof(WCHAR), NULL, te, len);