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 BOOL
ENV_BuildEnvironment(void)
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 PROCESS_Current()->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( PDB
*pdb
, LPCSTR env
)
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( GetProcessHeap(), 0, size
)))
130 pdb
->env_db
->env_sel
= SELECTOR_AllocBlock( pdb
->env_db
->environ
,
131 0x10000, SEGMENT_DATA
,
134 dst
= pdb
->env_db
->environ
;
137 if (pdb
->flags
& PDB32_WIN16_PROC
)
138 lstrcpynA( dst
, src
, MAX_WIN16_LEN
);
141 src
+= strlen(src
) + 1;
142 dst
+= strlen(dst
) + 1;
144 FILL_EXTRA_ENV( dst
);
149 /***********************************************************************
150 * ENV_FreeEnvironment
152 * Free a process environment.
154 void ENV_FreeEnvironment( PDB
*pdb
)
156 if (!pdb
->env_db
) return;
157 if (pdb
->env_db
->env_sel
) SELECTOR_FreeBlock( pdb
->env_db
->env_sel
, 1 );
158 DeleteCriticalSection( &pdb
->env_db
->section
);
159 /* the storage will be deleted when the process heap is destroyed */
163 /***********************************************************************
164 * GetCommandLineA (KERNEL32.289)
166 LPCSTR WINAPI
GetCommandLineA(void)
168 return PROCESS_Current()->env_db
->cmd_line
;
171 /***********************************************************************
172 * GetCommandLineW (KERNEL32.290)
174 LPCWSTR WINAPI
GetCommandLineW(void)
176 PDB
*pdb
= PROCESS_Current();
177 EnterCriticalSection( &pdb
->env_db
->section
);
178 if (!pdb
->env_db
->cmd_lineW
)
179 pdb
->env_db
->cmd_lineW
= HEAP_strdupAtoW( GetProcessHeap(), 0,
180 pdb
->env_db
->cmd_line
);
181 LeaveCriticalSection( &pdb
->env_db
->section
);
182 return pdb
->env_db
->cmd_lineW
;
186 /***********************************************************************
187 * GetEnvironmentStringsA (KERNEL32.319) (KERNEL32.320)
189 LPSTR WINAPI
GetEnvironmentStringsA(void)
191 PDB
*pdb
= PROCESS_Current();
192 return pdb
->env_db
->environ
;
196 /***********************************************************************
197 * GetEnvironmentStringsW (KERNEL32.321)
199 LPWSTR WINAPI
GetEnvironmentStringsW(void)
203 PDB
*pdb
= PROCESS_Current();
205 EnterCriticalSection( &pdb
->env_db
->section
);
206 size
= HeapSize( GetProcessHeap(), 0, pdb
->env_db
->environ
);
207 if ((ret
= HeapAlloc( GetProcessHeap(), 0, size
* sizeof(WCHAR
) )) != NULL
)
209 LPSTR pA
= pdb
->env_db
->environ
;
211 while (size
--) *pW
++ = (WCHAR
)(BYTE
)*pA
++;
213 LeaveCriticalSection( &pdb
->env_db
->section
);
218 /***********************************************************************
219 * FreeEnvironmentStringsA (KERNEL32.268)
221 BOOL WINAPI
FreeEnvironmentStringsA( LPSTR ptr
)
223 PDB
*pdb
= PROCESS_Current();
224 if (ptr
!= pdb
->env_db
->environ
)
226 SetLastError( ERROR_INVALID_PARAMETER
);
233 /***********************************************************************
234 * FreeEnvironmentStringsW (KERNEL32.269)
236 BOOL WINAPI
FreeEnvironmentStringsW( LPWSTR ptr
)
238 return HeapFree( GetProcessHeap(), 0, ptr
);
242 /***********************************************************************
243 * GetEnvironmentVariableA (KERNEL32.322)
245 DWORD WINAPI
GetEnvironmentVariableA( LPCSTR name
, LPSTR value
, DWORD size
)
249 PDB
*pdb
= PROCESS_Current();
253 SetLastError( ERROR_INVALID_PARAMETER
);
256 EnterCriticalSection( &pdb
->env_db
->section
);
257 if ((p
= ENV_FindVariable( pdb
->env_db
->environ
, name
, strlen(name
) )))
262 /* If not enough room, include the terminating null
263 * in the returned size and return an empty string */
265 if (value
) *value
= '\0';
267 else if (value
) strcpy( value
, p
);
269 LeaveCriticalSection( &pdb
->env_db
->section
);
270 return ret
; /* FIXME: SetLastError */
274 /***********************************************************************
275 * GetEnvironmentVariableW (KERNEL32.323)
277 DWORD WINAPI
GetEnvironmentVariableW( LPCWSTR nameW
, LPWSTR valW
, DWORD size
)
279 LPSTR name
= HEAP_strdupWtoA( GetProcessHeap(), 0, nameW
);
280 LPSTR val
= valW
? HeapAlloc( GetProcessHeap(), 0, size
) : NULL
;
281 DWORD res
= GetEnvironmentVariableA( name
, val
, size
);
282 HeapFree( GetProcessHeap(), 0, name
);
285 lstrcpynAtoW( valW
, val
, size
);
286 HeapFree( GetProcessHeap(), 0, val
);
292 /***********************************************************************
293 * SetEnvironmentVariableA (KERNEL32.641)
295 BOOL WINAPI
SetEnvironmentVariableA( LPCSTR name
, LPCSTR value
)
297 INT old_size
, len
, res
;
298 LPSTR p
, env
, new_env
;
300 PDB
*pdb
= PROCESS_Current();
302 EnterCriticalSection( &pdb
->env_db
->section
);
303 env
= p
= pdb
->env_db
->environ
;
305 /* Find a place to insert the string */
311 if (!lstrncmpiA( name
, p
, len
) && (p
[len
] == '=')) break;
314 if (!value
&& !*p
) goto done
; /* Value to remove doesn't exist */
316 /* Realloc the buffer */
318 len
= value
? strlen(name
) + strlen(value
) + 2 : 0;
319 if (*p
) len
-= strlen(p
) + 1; /* The name already exists */
320 old_size
= HeapSize( GetProcessHeap(), 0, env
);
323 LPSTR next
= p
+ strlen(p
) + 1; /* We know there is a next one */
324 memmove( next
+ len
, next
, old_size
- (next
- env
) );
326 if (!(new_env
= HeapReAlloc( GetProcessHeap(), 0, env
, old_size
+ len
)))
328 if (pdb
->env_db
->env_sel
)
329 SELECTOR_MoveBlock( pdb
->env_db
->env_sel
, new_env
);
330 p
= new_env
+ (p
- env
);
331 if (len
> 0) memmove( p
+ len
, p
, old_size
- (p
- new_env
) );
333 /* Set the new string */
341 pdb
->env_db
->environ
= new_env
;
345 LeaveCriticalSection( &pdb
->env_db
->section
);
350 /***********************************************************************
351 * SetEnvironmentVariableW (KERNEL32.642)
353 BOOL WINAPI
SetEnvironmentVariableW( LPCWSTR name
, LPCWSTR value
)
355 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
356 LPSTR valueA
= HEAP_strdupWtoA( GetProcessHeap(), 0, value
);
357 BOOL ret
= SetEnvironmentVariableA( nameA
, valueA
);
358 HeapFree( GetProcessHeap(), 0, nameA
);
359 HeapFree( GetProcessHeap(), 0, valueA
);
364 /***********************************************************************
365 * ExpandEnvironmentStringsA (KERNEL32.216)
367 * Note: overlapping buffers are not supported; this is how it should be.
369 DWORD WINAPI
ExpandEnvironmentStringsA( LPCSTR src
, LPSTR dst
, DWORD count
)
371 DWORD len
, total_size
= 1; /* 1 for terminating '\0' */
373 PDB
*pdb
= PROCESS_Current();
375 if (!count
) dst
= NULL
;
376 EnterCriticalSection( &pdb
->env_db
->section
);
382 if ((p
= strchr( src
, '%' ))) len
= p
- src
;
383 else len
= strlen(src
);
387 else /* we are at the start of a variable */
389 if ((p
= strchr( src
+ 1, '%' )))
391 len
= p
- src
- 1; /* Length of the variable name */
392 if ((var
= ENV_FindVariable( pdb
->env_db
->environ
,
395 src
+= len
+ 2; /* Skip the variable name */
400 var
= src
; /* Copy original name instead */
405 else /* unfinished variable name, ignore it */
408 len
= strlen(src
); /* Copy whole string */
415 if (count
< len
) len
= count
;
416 memcpy( dst
, var
, len
);
421 LeaveCriticalSection( &pdb
->env_db
->section
);
423 /* Null-terminate the string */
433 /***********************************************************************
434 * ExpandEnvironmentStringsW (KERNEL32.217)
436 DWORD WINAPI
ExpandEnvironmentStringsW( LPCWSTR src
, LPWSTR dst
, DWORD len
)
438 LPSTR srcA
= HEAP_strdupWtoA( GetProcessHeap(), 0, src
);
439 LPSTR dstA
= dst
? HeapAlloc( GetProcessHeap(), 0, len
) : NULL
;
440 DWORD ret
= ExpandEnvironmentStringsA( srcA
, dstA
, len
);
443 lstrcpyAtoW( dst
, dstA
);
444 HeapFree( GetProcessHeap(), 0, dstA
);
446 HeapFree( GetProcessHeap(), 0, srcA
);