No longer directly accessing debuggee memory.
[wine] / memory / environ.c
1 /*
2  * Process environment management
3  *
4  * Copyright 1996, 1998 Alexandre Julliard
5  */
6
7 #include <stdlib.h>
8 #include <string.h>
9 #include "windef.h"
10 #include "wingdi.h"
11 #include "winuser.h"
12 #include "wine/winestring.h"
13 #include "process.h"
14 #include "heap.h"
15 #include "selectors.h"
16 #include "winerror.h"
17
18 /* Format of an environment block:
19  * ASCIIZ   string 1 (xx=yy format)
20  * ...
21  * ASCIIZ   string n
22  * BYTE     0
23  * WORD     1
24  * ASCIIZ   program name (e.g. C:\WINDOWS\SYSTEM\KRNL386.EXE)
25  *
26  * Notes:
27  * - contrary to Microsoft docs, the environment strings do not appear
28  *   to be sorted on Win95 (although they are on NT); so we don't bother
29  *   to sort them either.
30  */
31
32 static const char ENV_program_name[] = "C:\\WINDOWS\\SYSTEM\\KRNL386.EXE";
33
34 /* Maximum length of a Win16 environment string (including NULL) */
35 #define MAX_WIN16_LEN  128
36
37 /* Extra bytes to reserve at the end of an environment */
38 #define EXTRA_ENV_SIZE (sizeof(BYTE) + sizeof(WORD) + sizeof(ENV_program_name))
39
40 /* Fill the extra bytes with the program name and stuff */
41 #define FILL_EXTRA_ENV(p) \
42     *(p) = '\0'; \
43     PUT_WORD( (p) + 1, 1 ); \
44     strcpy( (p) + 3, ENV_program_name );
45
46
47 /***********************************************************************
48  *           ENV_FindVariable
49  *
50  * Find a variable in the environment and return a pointer to the value.
51  * Helper function for GetEnvironmentVariable and ExpandEnvironmentStrings.
52  */
53 static LPCSTR ENV_FindVariable( LPCSTR env, LPCSTR name, INT len )
54 {
55     while (*env)
56     {
57         if (!lstrncmpiA( name, env, len ) && (env[len] == '='))
58             return env + len + 1;
59         env += strlen(env) + 1;
60     }
61     return NULL;
62 }
63
64
65 /***********************************************************************
66  *           ENV_BuildEnvironment
67  *
68  * Build the environment for the initial process
69  */
70 static BOOL ENV_BuildEnvironment( PDB *pdb )
71 {
72     extern char **environ;
73     LPSTR p, *e;
74     int size;
75
76     /* Compute the total size of the Unix environment */
77
78     size = EXTRA_ENV_SIZE;
79     for (e = environ; *e; e++) size += strlen(*e) + 1;
80
81     /* Now allocate the environment */
82
83     if (!(p = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
84     pdb->env_db->environ = p;
85
86     /* And fill it with the Unix environment */
87
88     for (e = environ; *e; e++)
89     {
90         strcpy( p, *e );
91         p += strlen(p) + 1;
92     }
93
94     /* Now add the program name */
95
96     FILL_EXTRA_ENV( p );
97     return TRUE;
98 }
99
100
101 /***********************************************************************
102  *           ENV_InheritEnvironment
103  *
104  * Make a process inherit the environment from its parent or from an
105  * explicit environment.
106  */
107 BOOL ENV_InheritEnvironment( LPCSTR env )
108 {
109     DWORD size;
110     LPCSTR src;
111     LPSTR dst;
112     PDB *pdb = PROCESS_Current();
113
114     /* FIXME: should lock the parent environment */
115     if (!env)
116     {
117         if (!pdb->parent)  /* initial process */
118             return ENV_BuildEnvironment( pdb );
119         env = pdb->parent->env_db->environ;
120     }
121
122     /* Compute the environment size */
123
124     src = env;
125     size = EXTRA_ENV_SIZE;
126     while (*src)
127     {
128         int len = strlen(src) + 1;
129         src += len;
130         if ((len > MAX_WIN16_LEN) && (pdb->flags & PDB32_WIN16_PROC))
131             len = MAX_WIN16_LEN;
132         size += len;
133     }
134
135     /* Copy the environment */
136
137     if (!(pdb->env_db->environ = HeapAlloc( GetProcessHeap(), 0, size )))
138         return FALSE;
139     pdb->env_db->env_sel = SELECTOR_AllocBlock( pdb->env_db->environ,
140                                                 0x10000, SEGMENT_DATA,
141                                                 FALSE, FALSE );
142     src = env;
143     dst = pdb->env_db->environ;
144     while (*src)
145     {
146         if (pdb->flags & PDB32_WIN16_PROC)
147             lstrcpynA( dst, src, MAX_WIN16_LEN );
148         else
149             strcpy( dst, src );
150         src += strlen(src) + 1;
151         dst += strlen(dst) + 1;
152     }
153     FILL_EXTRA_ENV( dst );
154     return TRUE;
155 }
156
157
158 /***********************************************************************
159  *           ENV_FreeEnvironment
160  *
161  * Free a process environment.
162  */
163 void ENV_FreeEnvironment( PDB *pdb )
164 {
165     if (!pdb->env_db) return;
166     if (pdb->env_db->env_sel) SELECTOR_FreeBlock( pdb->env_db->env_sel, 1 );
167     DeleteCriticalSection( &pdb->env_db->section );
168     /* the storage will be deleted when the process heap is destroyed */
169 }
170
171
172 /***********************************************************************
173  *           GetCommandLine32A      (KERNEL32.289)
174  */
175 LPCSTR WINAPI GetCommandLineA(void)
176 {
177     return PROCESS_Current()->env_db->cmd_line;
178 }
179
180 /***********************************************************************
181  *           GetCommandLine32W      (KERNEL32.290)
182  */
183 LPCWSTR WINAPI GetCommandLineW(void)
184 {
185     PDB *pdb = PROCESS_Current();
186     EnterCriticalSection( &pdb->env_db->section );
187     if (!pdb->env_db->cmd_lineW)
188         pdb->env_db->cmd_lineW = HEAP_strdupAtoW( GetProcessHeap(), 0,
189                                                   pdb->env_db->cmd_line );
190     LeaveCriticalSection( &pdb->env_db->section );
191     return pdb->env_db->cmd_lineW;
192 }
193
194
195 /***********************************************************************
196  *           GetEnvironmentStrings32A   (KERNEL32.319) (KERNEL32.320)
197  */
198 LPSTR WINAPI GetEnvironmentStringsA(void)
199 {
200     PDB *pdb = PROCESS_Current();
201     return pdb->env_db->environ;
202 }
203
204
205 /***********************************************************************
206  *           GetEnvironmentStrings32W   (KERNEL32.321)
207  */
208 LPWSTR WINAPI GetEnvironmentStringsW(void)
209 {
210     INT size;
211     LPWSTR ret;
212     PDB *pdb = PROCESS_Current();
213
214     EnterCriticalSection( &pdb->env_db->section );
215     size = HeapSize( GetProcessHeap(), 0, pdb->env_db->environ );
216     if ((ret = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) )) != NULL)
217     {
218         LPSTR pA = pdb->env_db->environ;
219         LPWSTR pW = ret;
220         while (size--) *pW++ = (WCHAR)(BYTE)*pA++;
221     }
222     LeaveCriticalSection( &pdb->env_db->section );
223     return ret;
224 }
225
226
227 /***********************************************************************
228  *           FreeEnvironmentStrings32A   (KERNEL32.268)
229  */
230 BOOL WINAPI FreeEnvironmentStringsA( LPSTR ptr )
231 {
232     PDB *pdb = PROCESS_Current();
233     if (ptr != pdb->env_db->environ)
234     {
235         SetLastError( ERROR_INVALID_PARAMETER );
236         return FALSE;
237     }
238     return TRUE;
239 }
240
241
242 /***********************************************************************
243  *           FreeEnvironmentStrings32W   (KERNEL32.269)
244  */
245 BOOL WINAPI FreeEnvironmentStringsW( LPWSTR ptr )
246 {
247     return HeapFree( GetProcessHeap(), 0, ptr );
248 }
249
250
251 /***********************************************************************
252  *           GetEnvironmentVariable32A   (KERNEL32.322)
253  */
254 DWORD WINAPI GetEnvironmentVariableA( LPCSTR name, LPSTR value, DWORD size )
255 {
256     LPCSTR p;
257     INT ret = 0;
258     PDB *pdb = PROCESS_Current();
259
260     if (!name || !*name)
261     {
262         SetLastError( ERROR_INVALID_PARAMETER );
263         return 0;
264     }
265     EnterCriticalSection( &pdb->env_db->section );
266     if ((p = ENV_FindVariable( pdb->env_db->environ, name, strlen(name) )))
267     {
268         ret = strlen(p);
269         if (size <= ret)
270         {
271             /* If not enough room, include the terminating null
272              * in the returned size and return an empty string */
273             ret++;
274             if (value) *value = '\0';
275         }
276         else if (value) strcpy( value, p );
277     }
278     LeaveCriticalSection( &pdb->env_db->section );
279     return ret;  /* FIXME: SetLastError */
280 }
281
282
283 /***********************************************************************
284  *           GetEnvironmentVariable32W   (KERNEL32.323)
285  */
286 DWORD WINAPI GetEnvironmentVariableW( LPCWSTR nameW, LPWSTR valW, DWORD size)
287 {
288     LPSTR name = HEAP_strdupWtoA( GetProcessHeap(), 0, nameW );
289     LPSTR val  = valW ? HeapAlloc( GetProcessHeap(), 0, size ) : NULL;
290     DWORD res  = GetEnvironmentVariableA( name, val, size );
291     HeapFree( GetProcessHeap(), 0, name );
292     if (val)
293     {
294         lstrcpynAtoW( valW, val, size );
295         HeapFree( GetProcessHeap(), 0, val );
296     }
297     return res;
298 }
299
300
301 /***********************************************************************
302  *           SetEnvironmentVariable32A   (KERNEL32.641)
303  */
304 BOOL WINAPI SetEnvironmentVariableA( LPCSTR name, LPCSTR value )
305 {
306     INT old_size, len, res;
307     LPSTR p, env, new_env;
308     BOOL ret = FALSE;
309     PDB *pdb = PROCESS_Current();
310
311     EnterCriticalSection( &pdb->env_db->section );
312     env = p = pdb->env_db->environ;
313
314     /* Find a place to insert the string */
315
316     res = -1;
317     len = strlen(name);
318     while (*p)
319     {
320         if (!lstrncmpiA( name, p, len ) && (p[len] == '=')) break;
321         p += strlen(p) + 1;
322     }
323     if (!value && !*p) goto done;  /* Value to remove doesn't exist */
324
325     /* Realloc the buffer */
326
327     len = value ? strlen(name) + strlen(value) + 2 : 0;
328     if (*p) len -= strlen(p) + 1;  /* The name already exists */
329     old_size = HeapSize( GetProcessHeap(), 0, env );
330     if (len < 0)
331     {
332         LPSTR next = p + strlen(p) + 1;  /* We know there is a next one */
333         memmove( next + len, next, old_size - (next - env) );
334     }
335     if (!(new_env = HeapReAlloc( GetProcessHeap(), 0, env, old_size + len )))
336         goto done;
337     if (pdb->env_db->env_sel)
338         SELECTOR_MoveBlock( pdb->env_db->env_sel, new_env );
339     p = new_env + (p - env);
340     if (len > 0) memmove( p + len, p, old_size - (p - new_env) );
341
342     /* Set the new string */
343
344     if (value)
345     {
346         strcpy( p, name );
347         strcat( p, "=" );
348         strcat( p, value );
349     }
350     pdb->env_db->environ = new_env;
351     ret = TRUE;
352
353 done:
354     LeaveCriticalSection( &pdb->env_db->section );
355     return ret;
356 }
357
358
359 /***********************************************************************
360  *           SetEnvironmentVariable32W   (KERNEL32.642)
361  */
362 BOOL WINAPI SetEnvironmentVariableW( LPCWSTR name, LPCWSTR value )
363 {
364     LPSTR nameA  = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
365     LPSTR valueA = HEAP_strdupWtoA( GetProcessHeap(), 0, value );
366     BOOL ret = SetEnvironmentVariableA( nameA, valueA );
367     HeapFree( GetProcessHeap(), 0, nameA );
368     HeapFree( GetProcessHeap(), 0, valueA );
369     return ret;
370 }
371
372
373 /***********************************************************************
374  *           ExpandEnvironmentStrings32A   (KERNEL32.216)
375  *
376  * Note: overlapping buffers are not supported; this is how it should be.
377  */
378 DWORD WINAPI ExpandEnvironmentStringsA( LPCSTR src, LPSTR dst, DWORD count )
379 {
380     DWORD len, total_size = 1;  /* 1 for terminating '\0' */
381     LPCSTR p, var;
382     PDB *pdb = PROCESS_Current();
383
384     if (!count) dst = NULL;
385     EnterCriticalSection( &pdb->env_db->section );
386
387     while (*src)
388     {
389         if (*src != '%')
390         {
391             if ((p = strchr( src, '%' ))) len = p - src;
392             else len = strlen(src);
393             var = src;
394             src += len;
395         }
396         else  /* we are at the start of a variable */
397         {
398             if ((p = strchr( src + 1, '%' )))
399             {
400                 len = p - src - 1;  /* Length of the variable name */
401                 if ((var = ENV_FindVariable( pdb->env_db->environ,
402                                              src + 1, len )))
403                 {
404                     src += len + 2;  /* Skip the variable name */
405                     len = strlen(var);
406                 }
407                 else
408                 {
409                     var = src;  /* Copy original name instead */
410                     len += 2;
411                     src += len;
412                 }
413             }
414             else  /* unfinished variable name, ignore it */
415             {
416                 var = src;
417                 len = strlen(src);  /* Copy whole string */
418                 src += len;
419             }
420         }
421         total_size += len;
422         if (dst)
423         {
424             if (count < len) len = count;
425             memcpy( dst, var, len );
426             dst += len;
427             count -= len;
428         }
429     }
430     LeaveCriticalSection( &pdb->env_db->section );
431
432     /* Null-terminate the string */
433     if (dst)
434     {
435         if (!count) dst--;
436         *dst = '\0';
437     }
438     return total_size;
439 }
440
441
442 /***********************************************************************
443  *           ExpandEnvironmentStrings32W   (KERNEL32.217)
444  */
445 DWORD WINAPI ExpandEnvironmentStringsW( LPCWSTR src, LPWSTR dst, DWORD len )
446 {
447     LPSTR srcA = HEAP_strdupWtoA( GetProcessHeap(), 0, src );
448     LPSTR dstA = dst ? HeapAlloc( GetProcessHeap(), 0, len ) : NULL;
449     DWORD ret  = ExpandEnvironmentStringsA( srcA, dstA, len );
450     if (dstA)
451     {
452         lstrcpyAtoW( dst, dstA );
453         HeapFree( GetProcessHeap(), 0, dstA );
454     }
455     HeapFree( GetProcessHeap(), 0, srcA );
456     return ret;
457 }
458