2 * Process environment management
4 * Copyright 1996, 1998 Alexandre Julliard
10 #include "wine/winestring.h"
13 #include "selectors.h"
16 /* Format of an environment block:
17 * ASCIIZ string 1 (xx=yy format)
22 * ASCIIZ program name (e.g. C:\WINDOWS\SYSTEM\KRNL386.EXE)
25 * - contrary to Microsoft docs, the environment strings do not appear
26 * to be sorted on Win95 (although they are on NT); so we don't bother
27 * to sort them either.
30 static const char ENV_program_name
[] = "C:\\WINDOWS\\SYSTEM\\KRNL386.EXE";
32 /* Maximum length of a Win16 environment string (including NULL) */
33 #define MAX_WIN16_LEN 128
35 /* Extra bytes to reserve at the end of an environment */
36 #define EXTRA_ENV_SIZE (sizeof(BYTE) + sizeof(WORD) + sizeof(ENV_program_name))
38 /* Fill the extra bytes with the program name and stuff */
39 #define FILL_EXTRA_ENV(p) \
41 PUT_WORD( (p) + 1, 1 ); \
42 strcpy( (p) + 3, ENV_program_name );
45 /***********************************************************************
48 * Find a variable in the environment and return a pointer to the value.
49 * Helper function for GetEnvironmentVariable and ExpandEnvironmentStrings.
51 static LPCSTR
ENV_FindVariable( LPCSTR env
, LPCSTR name
, INT len
)
55 if (!lstrncmpiA( name
, env
, len
) && (env
[len
] == '='))
57 env
+= strlen(env
) + 1;
63 /***********************************************************************
64 * ENV_BuildEnvironment
66 * Build the environment for the initial process
68 static BOOL
ENV_BuildEnvironment( PDB
*pdb
)
70 extern char **environ
;
74 /* Compute the total size of the Unix environment */
76 size
= EXTRA_ENV_SIZE
;
77 for (e
= environ
; *e
; e
++) size
+= strlen(*e
) + 1;
79 /* Now allocate the environment */
81 if (!(p
= HeapAlloc( SystemHeap
, 0, size
))) return FALSE
;
82 pdb
->env_db
->environ
= p
;
84 /* And fill it with the Unix environment */
86 for (e
= environ
; *e
; e
++)
92 /* Now add the program name */
99 /***********************************************************************
100 * ENV_InheritEnvironment
102 * Make a process inherit the environment from its parent or from an
103 * explicit environment.
105 BOOL
ENV_InheritEnvironment( PDB
*pdb
, LPCSTR env
)
111 /* FIXME: should lock the parent environment */
114 if (!pdb
->parent
) /* initial process */
115 return ENV_BuildEnvironment( pdb
);
116 env
= pdb
->parent
->env_db
->environ
;
119 /* Compute the environment size */
122 size
= EXTRA_ENV_SIZE
;
125 int len
= strlen(src
) + 1;
127 if ((len
> MAX_WIN16_LEN
) && (pdb
->flags
& PDB32_WIN16_PROC
))
132 /* Copy the environment */
134 if (!(pdb
->env_db
->environ
= HeapAlloc( pdb
->heap
, 0,
135 size
+ EXTRA_ENV_SIZE
)))
137 pdb
->env_db
->env_sel
= SELECTOR_AllocBlock( pdb
->env_db
->environ
,
138 0x10000, SEGMENT_DATA
,
141 dst
= pdb
->env_db
->environ
;
144 if (pdb
->flags
& PDB32_WIN16_PROC
)
145 lstrcpynA( dst
, src
, MAX_WIN16_LEN
);
148 src
+= strlen(src
) + 1;
149 dst
+= strlen(dst
) + 1;
151 FILL_EXTRA_ENV( dst
);
156 /***********************************************************************
157 * ENV_FreeEnvironment
159 * Free a process environment.
161 void ENV_FreeEnvironment( PDB
*pdb
)
163 if (!pdb
->env_db
) return;
164 if (pdb
->env_db
->env_sel
) SELECTOR_FreeBlock( pdb
->env_db
->env_sel
, 1 );
165 DeleteCriticalSection( &pdb
->env_db
->section
);
166 HeapFree( pdb
->heap
, 0, pdb
->env_db
);
170 /***********************************************************************
171 * GetCommandLine32A (KERNEL32.289)
173 LPCSTR WINAPI
GetCommandLineA(void)
175 return PROCESS_Current()->env_db
->cmd_line
;
178 /***********************************************************************
179 * GetCommandLine32W (KERNEL32.290)
181 LPCWSTR WINAPI
GetCommandLineW(void)
183 PDB
*pdb
= PROCESS_Current();
184 EnterCriticalSection( &pdb
->env_db
->section
);
185 if (!pdb
->env_db
->cmd_lineW
)
186 pdb
->env_db
->cmd_lineW
= HEAP_strdupAtoW( pdb
->heap
, 0,
187 pdb
->env_db
->cmd_line
);
188 LeaveCriticalSection( &pdb
->env_db
->section
);
189 return pdb
->env_db
->cmd_lineW
;
193 /***********************************************************************
194 * GetEnvironmentStrings32A (KERNEL32.319) (KERNEL32.320)
196 LPSTR WINAPI
GetEnvironmentStringsA(void)
198 PDB
*pdb
= PROCESS_Current();
199 return pdb
->env_db
->environ
;
203 /***********************************************************************
204 * GetEnvironmentStrings32W (KERNEL32.321)
206 LPWSTR WINAPI
GetEnvironmentStringsW(void)
210 PDB
*pdb
= PROCESS_Current();
212 EnterCriticalSection( &pdb
->env_db
->section
);
213 size
= HeapSize( pdb
->heap
, 0, pdb
->env_db
->environ
);
214 if ((ret
= HeapAlloc( pdb
->heap
, 0, size
* sizeof(WCHAR
) )) != NULL
)
216 LPSTR pA
= pdb
->env_db
->environ
;
218 while (size
--) *pW
++ = (WCHAR
)(BYTE
)*pA
++;
220 LeaveCriticalSection( &pdb
->env_db
->section
);
225 /***********************************************************************
226 * FreeEnvironmentStrings32A (KERNEL32.268)
228 BOOL WINAPI
FreeEnvironmentStringsA( LPSTR ptr
)
230 PDB
*pdb
= PROCESS_Current();
231 if (ptr
!= pdb
->env_db
->environ
)
233 SetLastError( ERROR_INVALID_PARAMETER
);
240 /***********************************************************************
241 * FreeEnvironmentStrings32W (KERNEL32.269)
243 BOOL WINAPI
FreeEnvironmentStringsW( LPWSTR ptr
)
245 return HeapFree( GetProcessHeap(), 0, ptr
);
249 /***********************************************************************
250 * GetEnvironmentVariable32A (KERNEL32.322)
252 DWORD WINAPI
GetEnvironmentVariableA( LPCSTR name
, LPSTR value
, DWORD size
)
256 PDB
*pdb
= PROCESS_Current();
260 SetLastError( ERROR_INVALID_PARAMETER
);
263 EnterCriticalSection( &pdb
->env_db
->section
);
264 if ((p
= ENV_FindVariable( pdb
->env_db
->environ
, name
, strlen(name
) )))
269 /* If not enough room, include the terminating null
270 * in the returned size and return an empty string */
272 if (value
) *value
= '\0';
274 else if (value
) strcpy( value
, p
);
276 LeaveCriticalSection( &pdb
->env_db
->section
);
277 return ret
; /* FIXME: SetLastError */
281 /***********************************************************************
282 * GetEnvironmentVariable32W (KERNEL32.323)
284 DWORD WINAPI
GetEnvironmentVariableW( LPCWSTR nameW
, LPWSTR valW
, DWORD size
)
286 LPSTR name
= HEAP_strdupWtoA( GetProcessHeap(), 0, nameW
);
287 LPSTR val
= valW
? HeapAlloc( GetProcessHeap(), 0, size
) : NULL
;
288 DWORD res
= GetEnvironmentVariableA( name
, val
, size
);
289 HeapFree( GetProcessHeap(), 0, name
);
292 lstrcpynAtoW( valW
, val
, size
);
293 HeapFree( GetProcessHeap(), 0, val
);
299 /***********************************************************************
300 * SetEnvironmentVariable32A (KERNEL32.641)
302 BOOL WINAPI
SetEnvironmentVariableA( LPCSTR name
, LPCSTR value
)
304 INT old_size
, len
, res
;
305 LPSTR p
, env
, new_env
;
307 PDB
*pdb
= PROCESS_Current();
309 EnterCriticalSection( &pdb
->env_db
->section
);
310 env
= p
= pdb
->env_db
->environ
;
312 /* Find a place to insert the string */
318 if (!lstrncmpiA( name
, p
, len
) && (p
[len
] == '=')) break;
321 if (!value
&& !*p
) goto done
; /* Value to remove doesn't exist */
323 /* Realloc the buffer */
325 len
= value
? strlen(name
) + strlen(value
) + 2 : 0;
326 if (*p
) len
-= strlen(p
) + 1; /* The name already exists */
327 old_size
= HeapSize( pdb
->heap
, 0, env
);
330 LPSTR next
= p
+ strlen(p
) + 1; /* We know there is a next one */
331 memmove( next
+ len
, next
, old_size
- (next
- env
) );
333 if (!(new_env
= HeapReAlloc( pdb
->heap
, 0, env
, old_size
+ len
)))
335 if (pdb
->env_db
->env_sel
)
336 SELECTOR_MoveBlock( pdb
->env_db
->env_sel
, new_env
);
337 p
= new_env
+ (p
- env
);
338 if (len
> 0) memmove( p
+ len
, p
, old_size
- (p
- new_env
) );
340 /* Set the new string */
348 pdb
->env_db
->environ
= new_env
;
352 LeaveCriticalSection( &pdb
->env_db
->section
);
357 /***********************************************************************
358 * SetEnvironmentVariable32W (KERNEL32.642)
360 BOOL WINAPI
SetEnvironmentVariableW( LPCWSTR name
, LPCWSTR value
)
362 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
363 LPSTR valueA
= HEAP_strdupWtoA( GetProcessHeap(), 0, value
);
364 BOOL ret
= SetEnvironmentVariableA( nameA
, valueA
);
365 HeapFree( GetProcessHeap(), 0, nameA
);
366 HeapFree( GetProcessHeap(), 0, valueA
);
371 /***********************************************************************
372 * ExpandEnvironmentStrings32A (KERNEL32.216)
374 * Note: overlapping buffers are not supported; this is how it should be.
376 DWORD WINAPI
ExpandEnvironmentStringsA( LPCSTR src
, LPSTR dst
, DWORD count
)
378 DWORD len
, total_size
= 1; /* 1 for terminating '\0' */
380 PDB
*pdb
= PROCESS_Current();
382 if (!count
) dst
= NULL
;
383 EnterCriticalSection( &pdb
->env_db
->section
);
389 if ((p
= strchr( src
, '%' ))) len
= p
- src
;
390 else len
= strlen(src
);
394 else /* we are at the start of a variable */
396 if ((p
= strchr( src
+ 1, '%' )))
398 len
= p
- src
- 1; /* Length of the variable name */
399 if ((var
= ENV_FindVariable( pdb
->env_db
->environ
,
402 src
+= len
+ 2; /* Skip the variable name */
407 var
= src
; /* Copy original name instead */
412 else /* unfinished variable name, ignore it */
415 len
= strlen(src
); /* Copy whole string */
422 if (count
< len
) len
= count
;
423 memcpy( dst
, var
, len
);
428 LeaveCriticalSection( &pdb
->env_db
->section
);
430 /* Null-terminate the string */
440 /***********************************************************************
441 * ExpandEnvironmentStrings32W (KERNEL32.217)
443 DWORD WINAPI
ExpandEnvironmentStringsW( LPCWSTR src
, LPWSTR dst
, DWORD len
)
445 LPSTR srcA
= HEAP_strdupWtoA( GetProcessHeap(), 0, src
);
446 LPSTR dstA
= dst
? HeapAlloc( GetProcessHeap(), 0, len
) : NULL
;
447 DWORD ret
= ExpandEnvironmentStringsA( srcA
, dstA
, len
);
450 lstrcpyAtoW( dst
, dstA
);
451 HeapFree( GetProcessHeap(), 0, dstA
);
453 HeapFree( GetProcessHeap(), 0, srcA
);