2 * Process environment management
4 * Copyright 1996, 1998 Alexandre Julliard
12 #include "selectors.h"
15 /* Format of an environment block:
16 * ASCIIZ string 1 (xx=yy format)
21 * ASCIIZ program name (e.g. C:\WINDOWS\SYSTEM\KRNL386.EXE)
24 * - contrary to Microsoft docs, the environment strings do not appear
25 * to be sorted on Win95 (although they are on NT); so we don't bother
26 * to sort them either.
29 static const char ENV_program_name
[] = "C:\\WINDOWS\\SYSTEM\\KRNL386.EXE";
31 /* Maximum length of a Win16 environment string (including NULL) */
32 #define MAX_WIN16_LEN 128
34 /* Extra bytes to reserve at the end of an environment */
35 #define EXTRA_ENV_SIZE (sizeof(BYTE) + sizeof(WORD) + sizeof(ENV_program_name))
37 /* Fill the extra bytes with the program name and stuff */
38 #define FILL_EXTRA_ENV(p) \
40 PUT_WORD( (p) + 1, 1 ); \
41 strcpy( (p) + 3, ENV_program_name );
44 /***********************************************************************
47 * Find a variable in the environment and return a pointer to the value.
48 * Helper function for GetEnvironmentVariable and ExpandEnvironmentStrings.
50 static LPCSTR
ENV_FindVariable( LPCSTR env
, LPCSTR name
, INT32 len
)
54 if (!lstrncmpi32A( name
, env
, len
) && (env
[len
] == '='))
56 env
+= strlen(env
) + 1;
62 /***********************************************************************
63 * ENV_BuildEnvironment
65 * Build the environment for the initial process
67 BOOL32
ENV_BuildEnvironment( PDB32
*pdb
)
69 extern char **environ
;
73 /* Compute the total size of the Unix environment */
75 size
= EXTRA_ENV_SIZE
;
76 for (e
= environ
; *e
; e
++) size
+= strlen(*e
) + 1;
78 /* Now allocate the environment */
80 if (!(p
= HeapAlloc( SystemHeap
, 0, size
))) return FALSE
;
81 pdb
->env_db
->environ
= p
;
83 /* And fill it with the Unix environment */
85 for (e
= environ
; *e
; e
++)
91 /* Now add the program name */
98 /***********************************************************************
99 * ENV_InheritEnvironment
101 * Make a process inherit the environment from its parent or from an
102 * explicit environment.
104 BOOL32
ENV_InheritEnvironment( PDB32
*pdb
, LPCSTR env
)
110 /* FIXME: should lock the parent environment */
111 if (!env
) env
= pdb
->parent
->env_db
->environ
;
113 /* Compute the environment size */
116 size
= EXTRA_ENV_SIZE
;
119 int len
= strlen(src
) + 1;
121 if ((len
> MAX_WIN16_LEN
) && (pdb
->flags
& PDB32_WIN16_PROC
))
126 /* Copy the environment */
128 if (!(pdb
->env_db
->environ
= HeapAlloc( pdb
->heap
, 0,
129 size
+ EXTRA_ENV_SIZE
)))
131 pdb
->env_db
->env_sel
= SELECTOR_AllocBlock( pdb
->env_db
->environ
,
132 0x10000, SEGMENT_DATA
,
135 dst
= pdb
->env_db
->environ
;
138 if (pdb
->flags
& PDB32_WIN16_PROC
)
139 lstrcpyn32A( dst
, src
, MAX_WIN16_LEN
);
142 src
+= strlen(src
) + 1;
143 dst
+= strlen(dst
) + 1;
145 FILL_EXTRA_ENV( dst
);
150 /***********************************************************************
151 * ENV_FreeEnvironment
153 * Free a process environment.
155 void ENV_FreeEnvironment( PDB32
*pdb
)
157 if (!pdb
->env_db
) return;
158 if (pdb
->env_db
->env_sel
) SELECTOR_FreeBlock( pdb
->env_db
->env_sel
, 1 );
159 DeleteCriticalSection( &pdb
->env_db
->section
);
160 HeapFree( pdb
->heap
, 0, pdb
->env_db
);
164 /***********************************************************************
165 * GetCommandLine32A (KERNEL32.289)
167 LPCSTR WINAPI
GetCommandLine32A(void)
169 return PROCESS_Current()->env_db
->cmd_line
;
172 /***********************************************************************
173 * GetCommandLine32W (KERNEL32.290)
175 LPCWSTR WINAPI
GetCommandLine32W(void)
177 PDB32
*pdb
= PROCESS_Current();
178 EnterCriticalSection( &pdb
->env_db
->section
);
179 if (!pdb
->env_db
->cmd_lineW
)
180 pdb
->env_db
->cmd_lineW
= HEAP_strdupAtoW( pdb
->heap
, 0,
181 pdb
->env_db
->cmd_line
);
182 LeaveCriticalSection( &pdb
->env_db
->section
);
183 return pdb
->env_db
->cmd_lineW
;
187 /***********************************************************************
188 * GetEnvironmentStrings32A (KERNEL32.319) (KERNEL32.320)
190 LPSTR WINAPI
GetEnvironmentStrings32A(void)
192 PDB32
*pdb
= PROCESS_Current();
193 return pdb
->env_db
->environ
;
197 /***********************************************************************
198 * GetEnvironmentStrings32W (KERNEL32.321)
200 LPWSTR WINAPI
GetEnvironmentStrings32W(void)
204 PDB32
*pdb
= PROCESS_Current();
206 EnterCriticalSection( &pdb
->env_db
->section
);
207 size
= HeapSize( pdb
->heap
, 0, pdb
->env_db
->environ
);
208 if ((ret
= HeapAlloc( pdb
->heap
, 0, size
* sizeof(WCHAR
) )) != NULL
)
210 LPSTR pA
= pdb
->env_db
->environ
;
212 while (size
--) *pW
++ = (WCHAR
)(BYTE
)*pA
++;
214 LeaveCriticalSection( &pdb
->env_db
->section
);
219 /***********************************************************************
220 * FreeEnvironmentStrings32A (KERNEL32.268)
222 BOOL32 WINAPI
FreeEnvironmentStrings32A( LPSTR ptr
)
224 PDB32
*pdb
= PROCESS_Current();
225 if (ptr
!= pdb
->env_db
->environ
)
227 SetLastError( ERROR_INVALID_PARAMETER
);
234 /***********************************************************************
235 * FreeEnvironmentStrings32W (KERNEL32.269)
237 BOOL32 WINAPI
FreeEnvironmentStrings32W( LPWSTR ptr
)
239 return HeapFree( GetProcessHeap(), 0, ptr
);
243 /***********************************************************************
244 * GetEnvironmentVariable32A (KERNEL32.322)
246 DWORD WINAPI
GetEnvironmentVariable32A( LPCSTR name
, LPSTR value
, DWORD size
)
250 PDB32
*pdb
= PROCESS_Current();
254 SetLastError( ERROR_INVALID_PARAMETER
);
257 EnterCriticalSection( &pdb
->env_db
->section
);
258 if ((p
= ENV_FindVariable( pdb
->env_db
->environ
, name
, strlen(name
) )))
263 /* If not enough room, include the terminating null
264 * in the returned size and return an empty string */
266 if (value
) *value
= '\0';
268 else if (value
) strcpy( value
, p
);
270 LeaveCriticalSection( &pdb
->env_db
->section
);
271 return ret
; /* FIXME: SetLastError */
275 /***********************************************************************
276 * GetEnvironmentVariable32W (KERNEL32.323)
278 DWORD WINAPI
GetEnvironmentVariable32W( LPCWSTR nameW
, LPWSTR valW
, DWORD size
)
280 LPSTR name
= HEAP_strdupWtoA( GetProcessHeap(), 0, nameW
);
281 LPSTR val
= valW
? HeapAlloc( GetProcessHeap(), 0, size
) : NULL
;
282 DWORD res
= GetEnvironmentVariable32A( name
, val
, size
);
283 HeapFree( GetProcessHeap(), 0, name
);
286 lstrcpynAtoW( valW
, val
, size
);
287 HeapFree( GetProcessHeap(), 0, val
);
293 /***********************************************************************
294 * SetEnvironmentVariable32A (KERNEL32.641)
296 BOOL32 WINAPI
SetEnvironmentVariable32A( LPCSTR name
, LPCSTR value
)
298 INT32 old_size
, len
, res
;
299 LPSTR p
, env
, new_env
;
301 PDB32
*pdb
= PROCESS_Current();
303 EnterCriticalSection( &pdb
->env_db
->section
);
304 env
= p
= pdb
->env_db
->environ
;
306 /* Find a place to insert the string */
312 if (!lstrncmpi32A( name
, p
, len
) && (p
[len
] == '=')) break;
315 if (!value
&& !*p
) goto done
; /* Value to remove doesn't exist */
317 /* Realloc the buffer */
319 len
= value
? strlen(name
) + strlen(value
) + 2 : 0;
320 if (*p
) len
-= strlen(p
) + 1; /* The name already exists */
321 old_size
= HeapSize( pdb
->heap
, 0, env
);
324 LPSTR next
= p
+ strlen(p
) + 1; /* We know there is a next one */
325 memmove( next
+ len
, next
, old_size
- (next
- env
) );
327 if (!(new_env
= HeapReAlloc( pdb
->heap
, 0, env
, old_size
+ len
)))
329 if (pdb
->env_db
->env_sel
)
330 SELECTOR_MoveBlock( pdb
->env_db
->env_sel
, new_env
);
331 p
= new_env
+ (p
- env
);
332 if (len
> 0) memmove( p
+ len
, p
, old_size
- (p
- new_env
) );
334 /* Set the new string */
342 pdb
->env_db
->environ
= new_env
;
346 LeaveCriticalSection( &pdb
->env_db
->section
);
351 /***********************************************************************
352 * SetEnvironmentVariable32W (KERNEL32.642)
354 BOOL32 WINAPI
SetEnvironmentVariable32W( LPCWSTR name
, LPCWSTR value
)
356 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
357 LPSTR valueA
= HEAP_strdupWtoA( GetProcessHeap(), 0, value
);
358 BOOL32 ret
= SetEnvironmentVariable32A( nameA
, valueA
);
359 HeapFree( GetProcessHeap(), 0, nameA
);
360 HeapFree( GetProcessHeap(), 0, valueA
);
365 /***********************************************************************
366 * ExpandEnvironmentStrings32A (KERNEL32.216)
368 * Note: overlapping buffers are not supported; this is how it should be.
370 DWORD WINAPI
ExpandEnvironmentStrings32A( LPCSTR src
, LPSTR dst
, DWORD count
)
372 DWORD len
, total_size
= 1; /* 1 for terminating '\0' */
374 PDB32
*pdb
= PROCESS_Current();
376 if (!count
) dst
= NULL
;
377 EnterCriticalSection( &pdb
->env_db
->section
);
383 if ((p
= strchr( src
, '%' ))) len
= p
- src
;
384 else len
= strlen(src
);
388 else /* we are at the start of a variable */
390 if ((p
= strchr( src
+ 1, '%' )))
392 len
= p
- src
- 1; /* Length of the variable name */
393 if ((var
= ENV_FindVariable( pdb
->env_db
->environ
,
396 src
+= len
+ 2; /* Skip the variable name */
401 var
= src
; /* Copy original name instead */
406 else /* unfinished variable name, ignore it */
409 len
= strlen(src
); /* Copy whole string */
416 if (count
< len
) len
= count
;
417 memcpy( dst
, var
, len
);
422 LeaveCriticalSection( &pdb
->env_db
->section
);
424 /* Null-terminate the string */
434 /***********************************************************************
435 * ExpandEnvironmentStrings32W (KERNEL32.217)
437 DWORD WINAPI
ExpandEnvironmentStrings32W( LPCWSTR src
, LPWSTR dst
, DWORD len
)
439 LPSTR srcA
= HEAP_strdupWtoA( GetProcessHeap(), 0, src
);
440 LPSTR dstA
= dst
? HeapAlloc( GetProcessHeap(), 0, len
) : NULL
;
441 DWORD ret
= ExpandEnvironmentStrings32A( srcA
, dstA
, len
);
444 lstrcpyAtoW( dst
, dstA
);
445 HeapFree( GetProcessHeap(), 0, dstA
);
447 HeapFree( GetProcessHeap(), 0, srcA
);