2 * Process environment management
4 * Copyright 1996, 1998 Alexandre Julliard
12 #include "wine/winestring.h"
15 #include "selectors.h"
18 /* Format of an environment block:
19 * ASCIIZ string 1 (xx=yy format)
24 * ASCIIZ program name (e.g. C:\WINDOWS\SYSTEM\KRNL386.EXE)
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.
32 static const char ENV_program_name
[] = "C:\\WINDOWS\\SYSTEM\\KRNL386.EXE";
34 /* Maximum length of a Win16 environment string (including NULL) */
35 #define MAX_WIN16_LEN 128
37 /* Extra bytes to reserve at the end of an environment */
38 #define EXTRA_ENV_SIZE (sizeof(BYTE) + sizeof(WORD) + sizeof(ENV_program_name))
40 /* Fill the extra bytes with the program name and stuff */
41 #define FILL_EXTRA_ENV(p) \
43 PUT_WORD( (p) + 1, 1 ); \
44 strcpy( (p) + 3, ENV_program_name );
47 /***********************************************************************
50 * Find a variable in the environment and return a pointer to the value.
51 * Helper function for GetEnvironmentVariable and ExpandEnvironmentStrings.
53 static LPCSTR
ENV_FindVariable( LPCSTR env
, LPCSTR name
, INT len
)
57 if (!lstrncmpiA( name
, env
, len
) && (env
[len
] == '='))
59 env
+= strlen(env
) + 1;
65 /***********************************************************************
66 * ENV_BuildEnvironment
68 * Build the environment for the initial process
70 static BOOL
ENV_BuildEnvironment( PDB
*pdb
)
72 extern char **environ
;
76 /* Compute the total size of the Unix environment */
78 size
= EXTRA_ENV_SIZE
;
79 for (e
= environ
; *e
; e
++) size
+= strlen(*e
) + 1;
81 /* Now allocate the environment */
83 if (!(p
= HeapAlloc( GetProcessHeap(), 0, size
))) return FALSE
;
84 pdb
->env_db
->environ
= p
;
86 /* And fill it with the Unix environment */
88 for (e
= environ
; *e
; e
++)
94 /* Now add the program name */
101 /***********************************************************************
102 * ENV_InheritEnvironment
104 * Make a process inherit the environment from its parent or from an
105 * explicit environment.
107 BOOL
ENV_InheritEnvironment( LPCSTR env
)
112 PDB
*pdb
= PROCESS_Current();
114 /* FIXME: should lock the parent environment */
117 if (!pdb
->parent
) /* initial process */
118 return ENV_BuildEnvironment( pdb
);
119 env
= pdb
->parent
->env_db
->environ
;
122 /* Compute the environment size */
125 size
= EXTRA_ENV_SIZE
;
128 int len
= strlen(src
) + 1;
130 if ((len
> MAX_WIN16_LEN
) && (pdb
->flags
& PDB32_WIN16_PROC
))
135 /* Copy the environment */
137 if (!(pdb
->env_db
->environ
= HeapAlloc( GetProcessHeap(), 0, size
)))
139 pdb
->env_db
->env_sel
= SELECTOR_AllocBlock( pdb
->env_db
->environ
,
140 0x10000, SEGMENT_DATA
,
143 dst
= pdb
->env_db
->environ
;
146 if (pdb
->flags
& PDB32_WIN16_PROC
)
147 lstrcpynA( dst
, src
, MAX_WIN16_LEN
);
150 src
+= strlen(src
) + 1;
151 dst
+= strlen(dst
) + 1;
153 FILL_EXTRA_ENV( dst
);
158 /***********************************************************************
159 * ENV_FreeEnvironment
161 * Free a process environment.
163 void ENV_FreeEnvironment( PDB
*pdb
)
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 */
172 /***********************************************************************
173 * GetCommandLineA (KERNEL32.289)
175 LPCSTR WINAPI
GetCommandLineA(void)
177 return PROCESS_Current()->env_db
->cmd_line
;
180 /***********************************************************************
181 * GetCommandLineW (KERNEL32.290)
183 LPCWSTR WINAPI
GetCommandLineW(void)
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
;
195 /***********************************************************************
196 * GetEnvironmentStringsA (KERNEL32.319) (KERNEL32.320)
198 LPSTR WINAPI
GetEnvironmentStringsA(void)
200 PDB
*pdb
= PROCESS_Current();
201 return pdb
->env_db
->environ
;
205 /***********************************************************************
206 * GetEnvironmentStringsW (KERNEL32.321)
208 LPWSTR WINAPI
GetEnvironmentStringsW(void)
212 PDB
*pdb
= PROCESS_Current();
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
)
218 LPSTR pA
= pdb
->env_db
->environ
;
220 while (size
--) *pW
++ = (WCHAR
)(BYTE
)*pA
++;
222 LeaveCriticalSection( &pdb
->env_db
->section
);
227 /***********************************************************************
228 * FreeEnvironmentStringsA (KERNEL32.268)
230 BOOL WINAPI
FreeEnvironmentStringsA( LPSTR ptr
)
232 PDB
*pdb
= PROCESS_Current();
233 if (ptr
!= pdb
->env_db
->environ
)
235 SetLastError( ERROR_INVALID_PARAMETER
);
242 /***********************************************************************
243 * FreeEnvironmentStringsW (KERNEL32.269)
245 BOOL WINAPI
FreeEnvironmentStringsW( LPWSTR ptr
)
247 return HeapFree( GetProcessHeap(), 0, ptr
);
251 /***********************************************************************
252 * GetEnvironmentVariableA (KERNEL32.322)
254 DWORD WINAPI
GetEnvironmentVariableA( LPCSTR name
, LPSTR value
, DWORD size
)
258 PDB
*pdb
= PROCESS_Current();
262 SetLastError( ERROR_INVALID_PARAMETER
);
265 EnterCriticalSection( &pdb
->env_db
->section
);
266 if ((p
= ENV_FindVariable( pdb
->env_db
->environ
, name
, strlen(name
) )))
271 /* If not enough room, include the terminating null
272 * in the returned size and return an empty string */
274 if (value
) *value
= '\0';
276 else if (value
) strcpy( value
, p
);
278 LeaveCriticalSection( &pdb
->env_db
->section
);
279 return ret
; /* FIXME: SetLastError */
283 /***********************************************************************
284 * GetEnvironmentVariableW (KERNEL32.323)
286 DWORD WINAPI
GetEnvironmentVariableW( LPCWSTR nameW
, LPWSTR valW
, DWORD size
)
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
);
294 lstrcpynAtoW( valW
, val
, size
);
295 HeapFree( GetProcessHeap(), 0, val
);
301 /***********************************************************************
302 * SetEnvironmentVariableA (KERNEL32.641)
304 BOOL WINAPI
SetEnvironmentVariableA( LPCSTR name
, LPCSTR value
)
306 INT old_size
, len
, res
;
307 LPSTR p
, env
, new_env
;
309 PDB
*pdb
= PROCESS_Current();
311 EnterCriticalSection( &pdb
->env_db
->section
);
312 env
= p
= pdb
->env_db
->environ
;
314 /* Find a place to insert the string */
320 if (!lstrncmpiA( name
, p
, len
) && (p
[len
] == '=')) break;
323 if (!value
&& !*p
) goto done
; /* Value to remove doesn't exist */
325 /* Realloc the buffer */
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
);
332 LPSTR next
= p
+ strlen(p
) + 1; /* We know there is a next one */
333 memmove( next
+ len
, next
, old_size
- (next
- env
) );
335 if (!(new_env
= HeapReAlloc( GetProcessHeap(), 0, env
, old_size
+ len
)))
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
) );
342 /* Set the new string */
350 pdb
->env_db
->environ
= new_env
;
354 LeaveCriticalSection( &pdb
->env_db
->section
);
359 /***********************************************************************
360 * SetEnvironmentVariableW (KERNEL32.642)
362 BOOL WINAPI
SetEnvironmentVariableW( LPCWSTR name
, LPCWSTR value
)
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
);
373 /***********************************************************************
374 * ExpandEnvironmentStringsA (KERNEL32.216)
376 * Note: overlapping buffers are not supported; this is how it should be.
378 DWORD WINAPI
ExpandEnvironmentStringsA( LPCSTR src
, LPSTR dst
, DWORD count
)
380 DWORD len
, total_size
= 1; /* 1 for terminating '\0' */
382 PDB
*pdb
= PROCESS_Current();
384 if (!count
) dst
= NULL
;
385 EnterCriticalSection( &pdb
->env_db
->section
);
391 if ((p
= strchr( src
, '%' ))) len
= p
- src
;
392 else len
= strlen(src
);
396 else /* we are at the start of a variable */
398 if ((p
= strchr( src
+ 1, '%' )))
400 len
= p
- src
- 1; /* Length of the variable name */
401 if ((var
= ENV_FindVariable( pdb
->env_db
->environ
,
404 src
+= len
+ 2; /* Skip the variable name */
409 var
= src
; /* Copy original name instead */
414 else /* unfinished variable name, ignore it */
417 len
= strlen(src
); /* Copy whole string */
424 if (count
< len
) len
= count
;
425 memcpy( dst
, var
, len
);
430 LeaveCriticalSection( &pdb
->env_db
->section
);
432 /* Null-terminate the string */
442 /***********************************************************************
443 * ExpandEnvironmentStringsW (KERNEL32.217)
445 DWORD WINAPI
ExpandEnvironmentStringsW( LPCWSTR src
, LPWSTR dst
, DWORD len
)
447 LPSTR srcA
= HEAP_strdupWtoA( GetProcessHeap(), 0, src
);
448 LPSTR dstA
= dst
? HeapAlloc( GetProcessHeap(), 0, len
) : NULL
;
449 DWORD ret
= ExpandEnvironmentStringsA( srcA
, dstA
, len
);
452 lstrcpyAtoW( dst
, dstA
);
453 HeapFree( GetProcessHeap(), 0, dstA
);
455 HeapFree( GetProcessHeap(), 0, srcA
);