Added version information.
[wine] / dlls / ntdll / env.c
1 /*
2  * Ntdll environment functions
3  *
4  * Copyright 1996, 1998 Alexandre Julliard
5  * Copyright 2003       Eric Pouech
6  *
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.
11  *
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.
16  *
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
20  */
21 #include "config.h"
22
23 #include <assert.h>
24
25 #include "winternl.h"
26 #include "wine/unicode.h"
27 #include "wine/debug.h"
28 #include "ntdll_misc.h"
29
30 WINE_DEFAULT_DEBUG_CHANNEL(environ);
31
32 /******************************************************************************
33  *  RtlCreateEnvironment                [NTDLL.@]
34  */
35 NTSTATUS WINAPI RtlCreateEnvironment(BOOLEAN inherit, PWSTR* env)
36 {
37     NTSTATUS    nts;
38
39     TRACE("(%u,%p)!\n", inherit, env);
40
41     if (inherit)
42     {
43         MEMORY_BASIC_INFORMATION        mbi;
44
45         RtlAcquirePebLock();
46
47         nts = NtQueryVirtualMemory(NtCurrentProcess(), ntdll_get_process_pmts()->Environment, 
48                                    0, &mbi, sizeof(mbi), NULL);
49         if (nts == STATUS_SUCCESS)
50         {
51             *env = NULL;
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);
56             else *env = NULL;
57         }
58         RtlReleasePebLock();
59     }
60     else 
61     {
62         ULONG       size = 1;
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);
67     }
68
69     return nts;
70 }
71
72 /******************************************************************************
73  *  RtlDestroyEnvironment               [NTDLL.@]
74  */
75 NTSTATUS WINAPI RtlDestroyEnvironment(PWSTR env) 
76 {
77     ULONG size = 0;
78
79     TRACE("(%p)!\n", env);
80
81     return NtFreeVirtualMemory(NtCurrentProcess(), (void**)&env, &size, MEM_RELEASE);
82 }
83
84 static LPCWSTR ENV_FindVariable(PCWSTR var, PCWSTR name, unsigned namelen)
85 {
86     for (; *var; var += strlenW(var) + 1)
87     {
88         /* match var names, but avoid setting a var with a name including a '='
89          * (a starting '=' is valid though)
90          */
91         if (strncmpiW(var, name, namelen) == 0 && var[namelen] == '=' &&
92             strchrW(var + 1, '=') == var + namelen) 
93         {
94             return var + namelen + 1;
95         }
96     }
97     return NULL;
98 }
99
100 /******************************************************************
101  *              RtlQueryEnvironmentVariable_U   [NTDLL.@]
102  *
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)
107  */
108 NTSTATUS WINAPI RtlQueryEnvironmentVariable_U(PWSTR env,
109                                               PUNICODE_STRING name,
110                                               PUNICODE_STRING value)
111 {
112     NTSTATUS    nts = STATUS_VARIABLE_NOT_FOUND;
113     PCWSTR      var;
114     unsigned    namelen;
115
116     TRACE("%s %s %p\n", debugstr_w(env), debugstr_w(name->Buffer), value);
117
118     value->Length = 0;
119     namelen = name->Length / sizeof(WCHAR);
120     if (!namelen) return nts;
121
122     if (!env)
123     {
124         RtlAcquirePebLock();
125         var = ntdll_get_process_pmts()->Environment;
126     }
127     else var = env;
128
129     var = ENV_FindVariable(var, name->Buffer, namelen);
130     if (var != NULL)
131     {
132         value->Length = strlenW(var) * sizeof(WCHAR);
133         if (value->Length <= value->MaximumLength)
134         {
135             memmove(value->Buffer, var, min(value->Length + sizeof(WCHAR), value->MaximumLength));
136             nts = STATUS_SUCCESS;
137         }
138         else nts = STATUS_BUFFER_TOO_SMALL;
139     }
140
141     if (!env) RtlReleasePebLock();
142
143     return nts;
144 }
145
146 /******************************************************************
147  *              RtlSetCurrentEnvironment        [NTDLL.@]
148  *
149  */
150 void WINAPI RtlSetCurrentEnvironment(PWSTR new_env, PWSTR* old_env)
151 {
152     TRACE("(%p %p)\n", new_env, old_env);
153
154     RtlAcquirePebLock();
155
156     if (old_env) *old_env = ntdll_get_process_pmts()->Environment;
157     ntdll_get_process_pmts()->Environment = new_env;
158
159     RtlReleasePebLock();
160 }
161
162
163 /******************************************************************************
164  *  RtlSetEnvironmentVariable           [NTDLL.@]
165  */
166 NTSTATUS WINAPI RtlSetEnvironmentVariable(PWSTR* penv, PUNICODE_STRING name, 
167                                           PUNICODE_STRING value)
168 {
169     INT         len, old_size;
170     LPWSTR      p, env;
171     NTSTATUS    nts = STATUS_VARIABLE_NOT_FOUND;
172     MEMORY_BASIC_INFORMATION mbi;
173
174     TRACE("(%p,%s,%s): stub!\n", penv, debugstr_w(name->Buffer), debugstr_w(value->Buffer));
175
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;
180
181     if (!penv)
182     {
183         RtlAcquirePebLock();
184         env = ntdll_get_process_pmts()->Environment;
185     } else env = *penv;
186
187     len = name->Length / sizeof(WCHAR);
188
189     /* compute current size of environment */
190     for (p = env; *p; p += strlenW(p) + 1);
191     old_size = p + 1 - env;
192
193     /* Find a place to insert the string */
194     for (p = env; *p; p += strlenW(p) + 1)
195     {
196         if (!strncmpiW(name->Buffer, p, len) && (p[len] == '=')) break;
197     }
198     if (!value && !*p) goto done;  /* Value to remove doesn't exist */
199
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 */
203
204     if (len < 0)
205     {
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));
208     }
209
210     nts = NtQueryVirtualMemory(NtCurrentProcess(), env, 0,
211                                &mbi, sizeof(mbi), NULL);
212     if (nts != STATUS_SUCCESS) goto done;
213
214     if ((old_size + len) * sizeof(WCHAR) > mbi.RegionSize)
215     {
216         LPWSTR  new_env;
217         ULONG   new_size = (old_size + len) * sizeof(WCHAR);
218
219         new_env = NULL;
220         nts = NtAllocateVirtualMemory(NtCurrentProcess(), (void**)&new_env, 0,
221                                       &new_size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
222         if (nts != STATUS_SUCCESS) goto done;
223
224         memmove(new_env, env, (p - env) * sizeof(WCHAR));
225         assert(len > 0);
226         memmove(new_env + (p - env) + len, p, (old_size - (p - env)) * sizeof(WCHAR));
227         p = new_env + (p - env);
228
229         RtlDestroyEnvironment(env);
230         if (!penv) ntdll_get_process_pmts()->Environment = new_env;
231         else *penv = new_env;
232         env = new_env;
233     }
234     else
235     {
236         if (len > 0) memmove(p + len, p, (old_size - (p - env)) * sizeof(WCHAR));
237     }
238
239     /* Set the new string */
240     if (value)
241     {
242         static const WCHAR equalW[] = {'=',0};
243
244         strcpyW(p, name->Buffer);
245         strcatW(p, equalW);
246         strcatW(p, value->Buffer);
247     }
248
249 done:
250     if (!penv) RtlReleasePebLock();
251
252     return nts;
253 }
254
255 /******************************************************************
256  *              RtlExpandEnvironmentStrings_U (NTDLL.@)
257  *
258  */
259 NTSTATUS WINAPI RtlExpandEnvironmentStrings_U(PWSTR env, const UNICODE_STRING* us_src,
260                                               PUNICODE_STRING us_dst, PULONG plen)
261 {
262     DWORD       len, count, total_size = 1;  /* 1 for terminating '\0' */
263     LPCWSTR     src, p, var;
264     LPWSTR      dst;
265
266     src = us_src->Buffer;
267     count = us_dst->MaximumLength / sizeof(WCHAR);
268     dst = count ? us_dst->Buffer : NULL;
269
270     RtlAcquirePebLock();
271
272     while (*src)
273     {
274         if (*src != '%')
275         {
276             if ((p = strchrW( src, '%' ))) len = p - src;
277             else len = strlenW(src);
278             var = src;
279             src += len;
280         }
281         else  /* we are at the start of a variable */
282         {
283             if ((p = strchrW( src + 1, '%' )))
284             {
285                 len = p - src - 1;  /* Length of the variable name */
286                 if ((var = ENV_FindVariable( env, src + 1, len )))
287                 {
288                     src += len + 2;  /* Skip the variable name */
289                     len = strlenW(var);
290                 }
291                 else
292                 {
293                     var = src;  /* Copy original name instead */
294                     len += 2;
295                     src += len;
296                 }
297             }
298             else  /* unfinished variable name, ignore it */
299             {
300                 var = src;
301                 len = strlenW(src);  /* Copy whole string */
302                 src += len;
303             }
304         }
305         total_size += len;
306         if (dst)
307         {
308             if (count < len) len = count;
309             memcpy(dst, var, len * sizeof(WCHAR));
310             count -= len;
311             dst += len;
312         }
313     }
314
315     RtlReleasePebLock();
316
317     /* Null-terminate the string */
318     if (dst && count) *dst = '\0';
319
320     us_dst->Length = (dst) ? (dst - us_dst->Buffer) * sizeof(WCHAR): 0;
321     if (plen) *plen = total_size * sizeof(WCHAR);
322
323     return (count) ? STATUS_SUCCESS : STATUS_BUFFER_TOO_SMALL;
324 }
325
326 /***********************************************************************
327  *           build_environment
328  *
329  * Build the Win32 environment from the Unix environment
330  */
331 BOOL build_initial_environment(void)
332 {
333     extern char **environ;
334     LPSTR*      e, te;
335     LPWSTR      p;
336     ULONG       size;
337     NTSTATUS    nts;
338     int         len;
339
340     /* Compute the total size of the Unix environment */
341     size = sizeof(BYTE);
342     for (e = environ; *e; e++)
343     {
344         if (!memcmp(*e, "PATH=", 5)) continue;
345         size += strlen(*e) + 1;
346     }
347     size *= sizeof(WCHAR);
348
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;
353
354     ntdll_get_process_pmts()->Environment = p;
355     /* And fill it with the Unix environment */
356     for (e = environ; *e; e++)
357     {
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;
361         len = strlen(te);
362         RtlMultiByteToUnicodeN(p, len * sizeof(WCHAR), NULL, te, len);
363         p[len] = 0;
364         p += len + 1;
365     }
366     *p = 0;
367
368     return TRUE;
369 }