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 /******************************************************************
85 * RtlQueryEnvironmentVariable_U [NTDLL.@]
88 NTSTATUS WINAPI RtlQueryEnvironmentVariable_U(PWSTR env,
90 PUNICODE_STRING value)
92 NTSTATUS nts = STATUS_VARIABLE_NOT_FOUND;
94 unsigned namelen, varlen;
96 TRACE("%s %s %p\n", debugstr_w(env), debugstr_w(name->Buffer), value);
99 namelen = name->Length / sizeof(WCHAR);
100 if (!namelen) return nts;
105 var = ntdll_get_process_pmts()->Environment;
109 for (; *var; var += varlen + 1)
111 varlen = strlenW(var);
112 /* match var names, but avoid setting a var with a name including a '='
113 * (a starting '=' is valid though)
115 if (strncmpiW(var, name->Buffer, namelen) == 0 && var[namelen] == '=' &&
116 strchrW(var + 1, '=') == var + namelen)
118 value->Length = (varlen - namelen - 1) * sizeof(WCHAR);
119 if (value->Length <= value->MaximumLength)
121 memmove(value->Buffer, var + namelen + 1, value->Length + sizeof(WCHAR));
122 nts = STATUS_SUCCESS;
124 else nts = STATUS_BUFFER_TOO_SMALL;
129 if (!env) RtlReleasePebLock();
134 /******************************************************************
135 * RtlSetCurrentEnvironment [NTDLL.@]
138 void WINAPI RtlSetCurrentEnvironment(PWSTR new_env, PWSTR* old_env)
140 TRACE("(%p %p)\n", new_env, old_env);
144 if (old_env) *old_env = ntdll_get_process_pmts()->Environment;
145 ntdll_get_process_pmts()->Environment = new_env;
151 /******************************************************************************
152 * RtlSetEnvironmentVariable [NTDLL.@]
154 NTSTATUS WINAPI RtlSetEnvironmentVariable(PWSTR* penv, PUNICODE_STRING name,
155 PUNICODE_STRING value)
159 NTSTATUS nts = STATUS_VARIABLE_NOT_FOUND;
160 MEMORY_BASIC_INFORMATION mbi;
162 TRACE("(%p,%s,%s): stub!\n", penv, debugstr_w(name->Buffer), debugstr_w(value->Buffer));
164 if (!name || !name->Buffer || !name->Buffer[0])
165 return STATUS_INVALID_PARAMETER_1;
166 /* variable names can't contain a '=' except as a first character */
167 if (strchrW(name->Buffer + 1, '=')) return STATUS_INVALID_PARAMETER;
172 env = ntdll_get_process_pmts()->Environment;
175 len = name->Length / sizeof(WCHAR);
177 /* compute current size of environment */
178 for (p = env; *p; p += strlenW(p) + 1);
179 old_size = p + 1 - env;
181 /* Find a place to insert the string */
182 for (p = env; *p; p += strlenW(p) + 1)
184 if (!strncmpiW(name->Buffer, p, len) && (p[len] == '=')) break;
186 if (!value && !*p) goto done; /* Value to remove doesn't exist */
188 /* Realloc the buffer */
189 len = value ? len + value->Length / sizeof(WCHAR) + 2 : 0;
190 if (*p) len -= strlenW(p) + 1; /* The name already exists */
194 LPWSTR next = p + strlenW(p) + 1; /* We know there is a next one */
195 memmove(next + len, next, (old_size - (next - env)) * sizeof(WCHAR));
198 nts = NtQueryVirtualMemory(NtCurrentProcess(), env, 0,
199 &mbi, sizeof(mbi), NULL);
200 if (nts != STATUS_SUCCESS) goto done;
202 if ((old_size + len) * sizeof(WCHAR) > mbi.RegionSize)
205 ULONG new_size = (old_size + len) * sizeof(WCHAR);
208 nts = NtAllocateVirtualMemory(NtCurrentProcess(), (void**)&new_env, 0,
209 &new_size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
210 if (nts != STATUS_SUCCESS) goto done;
212 memmove(new_env, env, (p - env) * sizeof(WCHAR));
214 memmove(new_env + (p - env) + len, p, (old_size - (p - env)) * sizeof(WCHAR));
215 p = new_env + (p - env);
217 RtlDestroyEnvironment(env);
218 if (!penv) ntdll_get_process_pmts()->Environment = new_env;
219 else *penv = new_env;
224 if (len > 0) memmove(p + len, p, (old_size - (p - env)) * sizeof(WCHAR));
227 /* Set the new string */
230 static const WCHAR equalW[] = {'=',0};
232 strcpyW(p, name->Buffer);
234 strcatW(p, value->Buffer);
238 if (!penv) RtlReleasePebLock();
243 /***********************************************************************
246 * Build the Win32 environment from the Unix environment
248 BOOL build_initial_environment(void)
250 extern char **environ;
257 /* Compute the total size of the Unix environment */
259 for (e = environ; *e; e++)
261 if (!memcmp(*e, "PATH=", 5)) continue;
262 size += strlen(*e) + 1;
264 size *= sizeof(WCHAR);
266 /* Now allocate the environment */
267 nts = NtAllocateVirtualMemory(NtCurrentProcess(), (void**)&p, 0, &size,
268 MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
269 if (nts != STATUS_SUCCESS) return FALSE;
271 ntdll_get_process_pmts()->Environment = p;
272 /* And fill it with the Unix environment */
273 for (e = environ; *e; e++)
275 /* skip Unix PATH and store WINEPATH as PATH */
276 if (!memcmp(*e, "PATH=", 5)) continue;
277 if (!memcmp(*e, "WINEPATH=", 9 )) te = *e + 4; else te = *e;
279 RtlMultiByteToUnicodeN(p, len * sizeof(WCHAR), NULL, te, len);