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
;
85 PROCESS_Current()->env_db
->env_sel
= SELECTOR_AllocBlock( p
, 0x10000, SEGMENT_DATA
,
88 /* And fill it with the Unix environment */
90 for (e
= environ
; *e
; e
++)
96 /* Now add the program name */
103 /***********************************************************************
104 * GetCommandLineA (KERNEL32.289)
106 LPSTR WINAPI
GetCommandLineA(void)
108 return PROCESS_Current()->env_db
->cmd_line
;
111 /***********************************************************************
112 * GetCommandLineW (KERNEL32.290)
114 LPWSTR WINAPI
GetCommandLineW(void)
116 PDB
*pdb
= PROCESS_Current();
117 EnterCriticalSection( &pdb
->env_db
->section
);
118 if (!pdb
->env_db
->cmd_lineW
)
119 pdb
->env_db
->cmd_lineW
= HEAP_strdupAtoW( GetProcessHeap(), 0,
120 pdb
->env_db
->cmd_line
);
121 LeaveCriticalSection( &pdb
->env_db
->section
);
122 return pdb
->env_db
->cmd_lineW
;
126 /***********************************************************************
127 * GetEnvironmentStringsA (KERNEL32.319) (KERNEL32.320)
129 LPSTR WINAPI
GetEnvironmentStringsA(void)
131 PDB
*pdb
= PROCESS_Current();
132 return pdb
->env_db
->environ
;
136 /***********************************************************************
137 * GetEnvironmentStringsW (KERNEL32.321)
139 LPWSTR WINAPI
GetEnvironmentStringsW(void)
143 PDB
*pdb
= PROCESS_Current();
145 EnterCriticalSection( &pdb
->env_db
->section
);
146 size
= HeapSize( GetProcessHeap(), 0, pdb
->env_db
->environ
);
147 if ((ret
= HeapAlloc( GetProcessHeap(), 0, size
* sizeof(WCHAR
) )) != NULL
)
149 LPSTR pA
= pdb
->env_db
->environ
;
151 while (size
--) *pW
++ = (WCHAR
)(BYTE
)*pA
++;
153 LeaveCriticalSection( &pdb
->env_db
->section
);
158 /***********************************************************************
159 * FreeEnvironmentStringsA (KERNEL32.268)
161 BOOL WINAPI
FreeEnvironmentStringsA( LPSTR ptr
)
163 PDB
*pdb
= PROCESS_Current();
164 if (ptr
!= pdb
->env_db
->environ
)
166 SetLastError( ERROR_INVALID_PARAMETER
);
173 /***********************************************************************
174 * FreeEnvironmentStringsW (KERNEL32.269)
176 BOOL WINAPI
FreeEnvironmentStringsW( LPWSTR ptr
)
178 return HeapFree( GetProcessHeap(), 0, ptr
);
182 /***********************************************************************
183 * GetEnvironmentVariableA (KERNEL32.322)
185 DWORD WINAPI
GetEnvironmentVariableA( LPCSTR name
, LPSTR value
, DWORD size
)
189 PDB
*pdb
= PROCESS_Current();
193 SetLastError( ERROR_INVALID_PARAMETER
);
196 EnterCriticalSection( &pdb
->env_db
->section
);
197 if ((p
= ENV_FindVariable( pdb
->env_db
->environ
, name
, strlen(name
) )))
202 /* If not enough room, include the terminating null
203 * in the returned size and return an empty string */
205 if (value
) *value
= '\0';
207 else if (value
) strcpy( value
, p
);
209 LeaveCriticalSection( &pdb
->env_db
->section
);
210 return ret
; /* FIXME: SetLastError */
214 /***********************************************************************
215 * GetEnvironmentVariableW (KERNEL32.323)
217 DWORD WINAPI
GetEnvironmentVariableW( LPCWSTR nameW
, LPWSTR valW
, DWORD size
)
219 LPSTR name
= HEAP_strdupWtoA( GetProcessHeap(), 0, nameW
);
220 LPSTR val
= valW
? HeapAlloc( GetProcessHeap(), 0, size
) : NULL
;
221 DWORD res
= GetEnvironmentVariableA( name
, val
, size
);
222 HeapFree( GetProcessHeap(), 0, name
);
225 lstrcpynAtoW( valW
, val
, size
);
226 HeapFree( GetProcessHeap(), 0, val
);
232 /***********************************************************************
233 * SetEnvironmentVariableA (KERNEL32.641)
235 BOOL WINAPI
SetEnvironmentVariableA( LPCSTR name
, LPCSTR value
)
237 INT old_size
, len
, res
;
238 LPSTR p
, env
, new_env
;
240 PDB
*pdb
= PROCESS_Current();
242 EnterCriticalSection( &pdb
->env_db
->section
);
243 env
= p
= pdb
->env_db
->environ
;
245 /* Find a place to insert the string */
251 if (!lstrncmpiA( name
, p
, len
) && (p
[len
] == '=')) break;
254 if (!value
&& !*p
) goto done
; /* Value to remove doesn't exist */
256 /* Realloc the buffer */
258 len
= value
? strlen(name
) + strlen(value
) + 2 : 0;
259 if (*p
) len
-= strlen(p
) + 1; /* The name already exists */
260 old_size
= HeapSize( GetProcessHeap(), 0, env
);
263 LPSTR next
= p
+ strlen(p
) + 1; /* We know there is a next one */
264 memmove( next
+ len
, next
, old_size
- (next
- env
) );
266 if (!(new_env
= HeapReAlloc( GetProcessHeap(), 0, env
, old_size
+ len
)))
268 if (pdb
->env_db
->env_sel
)
269 SELECTOR_MoveBlock( pdb
->env_db
->env_sel
, new_env
);
270 p
= new_env
+ (p
- env
);
271 if (len
> 0) memmove( p
+ len
, p
, old_size
- (p
- new_env
) );
273 /* Set the new string */
281 pdb
->env_db
->environ
= new_env
;
285 LeaveCriticalSection( &pdb
->env_db
->section
);
290 /***********************************************************************
291 * SetEnvironmentVariableW (KERNEL32.642)
293 BOOL WINAPI
SetEnvironmentVariableW( LPCWSTR name
, LPCWSTR value
)
295 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
296 LPSTR valueA
= HEAP_strdupWtoA( GetProcessHeap(), 0, value
);
297 BOOL ret
= SetEnvironmentVariableA( nameA
, valueA
);
298 HeapFree( GetProcessHeap(), 0, nameA
);
299 HeapFree( GetProcessHeap(), 0, valueA
);
304 /***********************************************************************
305 * ExpandEnvironmentStringsA (KERNEL32.216)
307 * Note: overlapping buffers are not supported; this is how it should be.
309 DWORD WINAPI
ExpandEnvironmentStringsA( LPCSTR src
, LPSTR dst
, DWORD count
)
311 DWORD len
, total_size
= 1; /* 1 for terminating '\0' */
313 PDB
*pdb
= PROCESS_Current();
315 if (!count
) dst
= NULL
;
316 EnterCriticalSection( &pdb
->env_db
->section
);
322 if ((p
= strchr( src
, '%' ))) len
= p
- src
;
323 else len
= strlen(src
);
327 else /* we are at the start of a variable */
329 if ((p
= strchr( src
+ 1, '%' )))
331 len
= p
- src
- 1; /* Length of the variable name */
332 if ((var
= ENV_FindVariable( pdb
->env_db
->environ
,
335 src
+= len
+ 2; /* Skip the variable name */
340 var
= src
; /* Copy original name instead */
345 else /* unfinished variable name, ignore it */
348 len
= strlen(src
); /* Copy whole string */
355 if (count
< len
) len
= count
;
356 memcpy( dst
, var
, len
);
361 LeaveCriticalSection( &pdb
->env_db
->section
);
363 /* Null-terminate the string */
373 /***********************************************************************
374 * ExpandEnvironmentStringsW (KERNEL32.217)
376 DWORD WINAPI
ExpandEnvironmentStringsW( LPCWSTR src
, LPWSTR dst
, DWORD len
)
378 LPSTR srcA
= HEAP_strdupWtoA( GetProcessHeap(), 0, src
);
379 LPSTR dstA
= dst
? HeapAlloc( GetProcessHeap(), 0, len
) : NULL
;
380 DWORD ret
= ExpandEnvironmentStringsA( srcA
, dstA
, len
);
383 lstrcpyAtoW( dst
, dstA
);
384 HeapFree( GetProcessHeap(), 0, dstA
);
386 HeapFree( GetProcessHeap(), 0, srcA
);