Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Process environment management |
| 3 | * |
| 4 | * Copyright 1996, 1998 Alexandre Julliard |
| 5 | */ |
| 6 | |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 7 | #include <stdlib.h> |
| 8 | #include <string.h> |
Ulrich Weigand | cc19592 | 2000-12-27 04:18:26 +0000 | [diff] [blame] | 9 | #include "wine/port.h" |
Jeremy White | d3e22d9 | 2000-02-10 19:03:02 +0000 | [diff] [blame] | 10 | #include "windef.h" |
Alexandre Julliard | 24a62ab | 2000-11-28 22:40:56 +0000 | [diff] [blame] | 11 | #include "winnls.h" |
| 12 | #include "winerror.h" |
Alexandre Julliard | becb9a3 | 2000-12-11 03:48:15 +0000 | [diff] [blame] | 13 | #include "ntddk.h" |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 14 | #include "heap.h" |
| 15 | #include "selectors.h" |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 16 | |
Alexandre Julliard | becb9a3 | 2000-12-11 03:48:15 +0000 | [diff] [blame] | 17 | /* Win32 process environment database */ |
| 18 | typedef struct _ENVDB |
| 19 | { |
| 20 | LPSTR environ; /* 00 Process environment strings */ |
| 21 | DWORD unknown1; /* 04 Unknown */ |
| 22 | LPSTR cmd_line; /* 08 Command line */ |
| 23 | LPSTR cur_dir; /* 0c Current directory */ |
| 24 | STARTUPINFOA *startup_info; /* 10 Startup information */ |
| 25 | HANDLE hStdin; /* 14 Handle for standard input */ |
| 26 | HANDLE hStdout; /* 18 Handle for standard output */ |
| 27 | HANDLE hStderr; /* 1c Handle for standard error */ |
| 28 | DWORD unknown2; /* 20 Unknown */ |
| 29 | DWORD inherit_console; /* 24 Inherit console flag */ |
| 30 | DWORD break_type; /* 28 Console events flag */ |
| 31 | void *break_sem; /* 2c SetConsoleCtrlHandler semaphore */ |
| 32 | void *break_event; /* 30 SetConsoleCtrlHandler event */ |
| 33 | void *break_thread; /* 34 SetConsoleCtrlHandler thread */ |
| 34 | void *break_handlers; /* 38 List of console handlers */ |
| 35 | } ENVDB; |
| 36 | |
| 37 | |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 38 | /* Format of an environment block: |
| 39 | * ASCIIZ string 1 (xx=yy format) |
| 40 | * ... |
| 41 | * ASCIIZ string n |
| 42 | * BYTE 0 |
| 43 | * WORD 1 |
| 44 | * ASCIIZ program name (e.g. C:\WINDOWS\SYSTEM\KRNL386.EXE) |
| 45 | * |
| 46 | * Notes: |
| 47 | * - contrary to Microsoft docs, the environment strings do not appear |
| 48 | * to be sorted on Win95 (although they are on NT); so we don't bother |
| 49 | * to sort them either. |
| 50 | */ |
| 51 | |
| 52 | static const char ENV_program_name[] = "C:\\WINDOWS\\SYSTEM\\KRNL386.EXE"; |
| 53 | |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 54 | /* Maximum length of a Win16 environment string (including NULL) */ |
| 55 | #define MAX_WIN16_LEN 128 |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 56 | |
| 57 | /* Extra bytes to reserve at the end of an environment */ |
| 58 | #define EXTRA_ENV_SIZE (sizeof(BYTE) + sizeof(WORD) + sizeof(ENV_program_name)) |
| 59 | |
| 60 | /* Fill the extra bytes with the program name and stuff */ |
| 61 | #define FILL_EXTRA_ENV(p) \ |
| 62 | *(p) = '\0'; \ |
Ulrich Weigand | cc19592 | 2000-12-27 04:18:26 +0000 | [diff] [blame] | 63 | PUT_UA_WORD( (p) + 1, 1 ); \ |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 64 | strcpy( (p) + 3, ENV_program_name ); |
| 65 | |
Alexandre Julliard | 52900c8 | 2000-08-09 22:33:42 +0000 | [diff] [blame] | 66 | STARTUPINFOA current_startupinfo = |
| 67 | { |
| 68 | sizeof(STARTUPINFOA), /* cb */ |
| 69 | 0, /* lpReserved */ |
| 70 | 0, /* lpDesktop */ |
| 71 | 0, /* lpTitle */ |
| 72 | 0, /* dwX */ |
| 73 | 0, /* dwY */ |
| 74 | 0, /* dwXSize */ |
| 75 | 0, /* dwYSize */ |
| 76 | 0, /* dwXCountChars */ |
| 77 | 0, /* dwYCountChars */ |
| 78 | 0, /* dwFillAttribute */ |
| 79 | 0, /* dwFlags */ |
| 80 | 0, /* wShowWindow */ |
| 81 | 0, /* cbReserved2 */ |
| 82 | 0, /* lpReserved2 */ |
| 83 | 0, /* hStdInput */ |
| 84 | 0, /* hStdOutput */ |
| 85 | 0 /* hStdError */ |
| 86 | }; |
| 87 | |
| 88 | ENVDB current_envdb = |
| 89 | { |
| 90 | 0, /* environ */ |
| 91 | 0, /* unknown1 */ |
| 92 | 0, /* cmd_line */ |
| 93 | 0, /* cur_dir */ |
| 94 | ¤t_startupinfo, /* startup_info */ |
| 95 | 0, /* hStdin */ |
| 96 | 0, /* hStdout */ |
| 97 | 0, /* hStderr */ |
| 98 | 0, /* unknown2 */ |
| 99 | 0, /* inherit_console */ |
| 100 | 0, /* break_type */ |
| 101 | 0, /* break_sem */ |
| 102 | 0, /* break_event */ |
| 103 | 0, /* break_thread */ |
Alexandre Julliard | becb9a3 | 2000-12-11 03:48:15 +0000 | [diff] [blame] | 104 | 0 /* break_handlers */ |
Alexandre Julliard | 52900c8 | 2000-08-09 22:33:42 +0000 | [diff] [blame] | 105 | }; |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 106 | |
Alexandre Julliard | becb9a3 | 2000-12-11 03:48:15 +0000 | [diff] [blame] | 107 | |
| 108 | static WCHAR *cmdlineW; /* Unicode command line */ |
| 109 | static WORD env_sel; /* selector to the environment */ |
| 110 | |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 111 | /*********************************************************************** |
| 112 | * ENV_FindVariable |
| 113 | * |
| 114 | * Find a variable in the environment and return a pointer to the value. |
| 115 | * Helper function for GetEnvironmentVariable and ExpandEnvironmentStrings. |
| 116 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 117 | static LPCSTR ENV_FindVariable( LPCSTR env, LPCSTR name, INT len ) |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 118 | { |
| 119 | while (*env) |
| 120 | { |
Alexandre Julliard | 072dfb5 | 2000-09-25 23:30:56 +0000 | [diff] [blame] | 121 | if (!strncasecmp( name, env, len ) && (env[len] == '=')) |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 122 | return env + len + 1; |
| 123 | env += strlen(env) + 1; |
| 124 | } |
| 125 | return NULL; |
| 126 | } |
| 127 | |
| 128 | |
| 129 | /*********************************************************************** |
| 130 | * ENV_BuildEnvironment |
| 131 | * |
| 132 | * Build the environment for the initial process |
| 133 | */ |
Alexandre Julliard | becb9a3 | 2000-12-11 03:48:15 +0000 | [diff] [blame] | 134 | ENVDB *ENV_BuildEnvironment(void) |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 135 | { |
| 136 | extern char **environ; |
| 137 | LPSTR p, *e; |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 138 | int size; |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 139 | |
| 140 | /* Compute the total size of the Unix environment */ |
| 141 | |
| 142 | size = EXTRA_ENV_SIZE; |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 143 | for (e = environ; *e; e++) size += strlen(*e) + 1; |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 144 | |
| 145 | /* Now allocate the environment */ |
| 146 | |
Alexandre Julliard | becb9a3 | 2000-12-11 03:48:15 +0000 | [diff] [blame] | 147 | if (!(p = HeapAlloc( GetProcessHeap(), 0, size ))) return NULL; |
Alexandre Julliard | 52900c8 | 2000-08-09 22:33:42 +0000 | [diff] [blame] | 148 | current_envdb.environ = p; |
Alexandre Julliard | becb9a3 | 2000-12-11 03:48:15 +0000 | [diff] [blame] | 149 | env_sel = SELECTOR_AllocBlock( p, 0x10000, WINE_LDT_FLAGS_DATA ); |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 150 | |
| 151 | /* And fill it with the Unix environment */ |
| 152 | |
| 153 | for (e = environ; *e; e++) |
| 154 | { |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 155 | strcpy( p, *e ); |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 156 | p += strlen(p) + 1; |
| 157 | } |
| 158 | |
| 159 | /* Now add the program name */ |
| 160 | |
| 161 | FILL_EXTRA_ENV( p ); |
Alexandre Julliard | becb9a3 | 2000-12-11 03:48:15 +0000 | [diff] [blame] | 162 | return ¤t_envdb; |
| 163 | } |
| 164 | |
| 165 | |
| 166 | /*********************************************************************** |
| 167 | * ENV_BuildCommandLine |
| 168 | * |
| 169 | * Build the command line of a process from the argv array. |
| 170 | * |
| 171 | * Note that it does NOT necessarily include the file name. |
| 172 | * Sometimes we don't even have any command line options at all. |
| 173 | */ |
| 174 | BOOL ENV_BuildCommandLine( char **argv ) |
| 175 | { |
| 176 | int len, quote = 0; |
| 177 | char *p, **arg; |
| 178 | |
| 179 | for (arg = argv, len = 0; *arg; arg++) len += strlen(*arg) + 1; |
| 180 | if ((argv[0]) && (quote = (strchr( argv[0], ' ' ) != NULL))) len += 2; |
| 181 | if (!(p = current_envdb.cmd_line = HeapAlloc( GetProcessHeap(), 0, len ))) return FALSE; |
| 182 | arg = argv; |
| 183 | if (quote) |
| 184 | { |
| 185 | *p++ = '\"'; |
| 186 | strcpy( p, *arg ); |
| 187 | p += strlen(p); |
| 188 | *p++ = '\"'; |
| 189 | *p++ = ' '; |
| 190 | arg++; |
| 191 | } |
| 192 | while (*arg) |
| 193 | { |
| 194 | strcpy( p, *arg ); |
| 195 | p += strlen(p); |
| 196 | *p++ = ' '; |
| 197 | arg++; |
| 198 | } |
| 199 | if (p > current_envdb.cmd_line) p--; /* remove last space */ |
| 200 | *p = 0; |
| 201 | /* now allocate the Unicode version */ |
| 202 | len = MultiByteToWideChar( CP_ACP, 0, current_envdb.cmd_line, -1, NULL, 0 ); |
| 203 | if (!(cmdlineW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ))) |
| 204 | return FALSE; |
| 205 | MultiByteToWideChar( CP_ACP, 0, current_envdb.cmd_line, -1, cmdlineW, len ); |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 206 | return TRUE; |
| 207 | } |
| 208 | |
| 209 | |
| 210 | /*********************************************************************** |
Patrik Stridvall | 2d6457c | 2000-03-28 20:22:59 +0000 | [diff] [blame] | 211 | * GetCommandLineA (KERNEL32.289) |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 212 | */ |
Hidenori Takeshima | 34e10ee | 2000-06-24 13:37:05 +0000 | [diff] [blame] | 213 | LPSTR WINAPI GetCommandLineA(void) |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 214 | { |
Alexandre Julliard | 52900c8 | 2000-08-09 22:33:42 +0000 | [diff] [blame] | 215 | return current_envdb.cmd_line; |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | /*********************************************************************** |
Patrik Stridvall | 2d6457c | 2000-03-28 20:22:59 +0000 | [diff] [blame] | 219 | * GetCommandLineW (KERNEL32.290) |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 220 | */ |
Hidenori Takeshima | 34e10ee | 2000-06-24 13:37:05 +0000 | [diff] [blame] | 221 | LPWSTR WINAPI GetCommandLineW(void) |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 222 | { |
Alexandre Julliard | becb9a3 | 2000-12-11 03:48:15 +0000 | [diff] [blame] | 223 | return cmdlineW; |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | |
| 227 | /*********************************************************************** |
Patrik Stridvall | 2d6457c | 2000-03-28 20:22:59 +0000 | [diff] [blame] | 228 | * GetEnvironmentStringsA (KERNEL32.319) (KERNEL32.320) |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 229 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 230 | LPSTR WINAPI GetEnvironmentStringsA(void) |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 231 | { |
Alexandre Julliard | 52900c8 | 2000-08-09 22:33:42 +0000 | [diff] [blame] | 232 | return current_envdb.environ; |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | |
| 236 | /*********************************************************************** |
Patrik Stridvall | 2d6457c | 2000-03-28 20:22:59 +0000 | [diff] [blame] | 237 | * GetEnvironmentStringsW (KERNEL32.321) |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 238 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 239 | LPWSTR WINAPI GetEnvironmentStringsW(void) |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 240 | { |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 241 | INT size; |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 242 | LPWSTR ret; |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 243 | |
Alexandre Julliard | becb9a3 | 2000-12-11 03:48:15 +0000 | [diff] [blame] | 244 | RtlAcquirePebLock(); |
Alexandre Julliard | 52900c8 | 2000-08-09 22:33:42 +0000 | [diff] [blame] | 245 | size = HeapSize( GetProcessHeap(), 0, current_envdb.environ ); |
Alexandre Julliard | 079fd72 | 2000-01-25 01:41:35 +0000 | [diff] [blame] | 246 | if ((ret = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) )) != NULL) |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 247 | { |
Alexandre Julliard | 52900c8 | 2000-08-09 22:33:42 +0000 | [diff] [blame] | 248 | LPSTR pA = current_envdb.environ; |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 249 | LPWSTR pW = ret; |
| 250 | while (size--) *pW++ = (WCHAR)(BYTE)*pA++; |
| 251 | } |
Alexandre Julliard | becb9a3 | 2000-12-11 03:48:15 +0000 | [diff] [blame] | 252 | RtlReleasePebLock(); |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 253 | return ret; |
| 254 | } |
| 255 | |
| 256 | |
| 257 | /*********************************************************************** |
Patrik Stridvall | 2d6457c | 2000-03-28 20:22:59 +0000 | [diff] [blame] | 258 | * FreeEnvironmentStringsA (KERNEL32.268) |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 259 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 260 | BOOL WINAPI FreeEnvironmentStringsA( LPSTR ptr ) |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 261 | { |
Alexandre Julliard | 52900c8 | 2000-08-09 22:33:42 +0000 | [diff] [blame] | 262 | if (ptr != current_envdb.environ) |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 263 | { |
| 264 | SetLastError( ERROR_INVALID_PARAMETER ); |
| 265 | return FALSE; |
| 266 | } |
| 267 | return TRUE; |
| 268 | } |
| 269 | |
| 270 | |
| 271 | /*********************************************************************** |
Patrik Stridvall | 2d6457c | 2000-03-28 20:22:59 +0000 | [diff] [blame] | 272 | * FreeEnvironmentStringsW (KERNEL32.269) |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 273 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 274 | BOOL WINAPI FreeEnvironmentStringsW( LPWSTR ptr ) |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 275 | { |
| 276 | return HeapFree( GetProcessHeap(), 0, ptr ); |
| 277 | } |
| 278 | |
| 279 | |
| 280 | /*********************************************************************** |
Patrik Stridvall | 2d6457c | 2000-03-28 20:22:59 +0000 | [diff] [blame] | 281 | * GetEnvironmentVariableA (KERNEL32.322) |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 282 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 283 | DWORD WINAPI GetEnvironmentVariableA( LPCSTR name, LPSTR value, DWORD size ) |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 284 | { |
| 285 | LPCSTR p; |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 286 | INT ret = 0; |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 287 | |
| 288 | if (!name || !*name) |
| 289 | { |
| 290 | SetLastError( ERROR_INVALID_PARAMETER ); |
| 291 | return 0; |
| 292 | } |
Alexandre Julliard | becb9a3 | 2000-12-11 03:48:15 +0000 | [diff] [blame] | 293 | RtlAcquirePebLock(); |
Alexandre Julliard | 52900c8 | 2000-08-09 22:33:42 +0000 | [diff] [blame] | 294 | if ((p = ENV_FindVariable( current_envdb.environ, name, strlen(name) ))) |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 295 | { |
| 296 | ret = strlen(p); |
| 297 | if (size <= ret) |
| 298 | { |
| 299 | /* If not enough room, include the terminating null |
| 300 | * in the returned size and return an empty string */ |
| 301 | ret++; |
| 302 | if (value) *value = '\0'; |
| 303 | } |
| 304 | else if (value) strcpy( value, p ); |
| 305 | } |
Alexandre Julliard | becb9a3 | 2000-12-11 03:48:15 +0000 | [diff] [blame] | 306 | RtlReleasePebLock(); |
Andreas Mohr | 0af222c | 2000-09-22 20:58:23 +0000 | [diff] [blame] | 307 | if (!ret) |
| 308 | SetLastError( ERROR_ENVVAR_NOT_FOUND ); |
| 309 | return ret; |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | |
| 313 | /*********************************************************************** |
Patrik Stridvall | 2d6457c | 2000-03-28 20:22:59 +0000 | [diff] [blame] | 314 | * GetEnvironmentVariableW (KERNEL32.323) |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 315 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 316 | DWORD WINAPI GetEnvironmentVariableW( LPCWSTR nameW, LPWSTR valW, DWORD size) |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 317 | { |
| 318 | LPSTR name = HEAP_strdupWtoA( GetProcessHeap(), 0, nameW ); |
| 319 | LPSTR val = valW ? HeapAlloc( GetProcessHeap(), 0, size ) : NULL; |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 320 | DWORD res = GetEnvironmentVariableA( name, val, size ); |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 321 | HeapFree( GetProcessHeap(), 0, name ); |
| 322 | if (val) |
| 323 | { |
Alexandre Julliard | 24a62ab | 2000-11-28 22:40:56 +0000 | [diff] [blame] | 324 | if (size > 0 && !MultiByteToWideChar( CP_ACP, 0, val, -1, valW, size )) |
| 325 | valW[size-1] = 0; |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 326 | HeapFree( GetProcessHeap(), 0, val ); |
| 327 | } |
| 328 | return res; |
| 329 | } |
| 330 | |
| 331 | |
| 332 | /*********************************************************************** |
Patrik Stridvall | 2d6457c | 2000-03-28 20:22:59 +0000 | [diff] [blame] | 333 | * SetEnvironmentVariableA (KERNEL32.641) |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 334 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 335 | BOOL WINAPI SetEnvironmentVariableA( LPCSTR name, LPCSTR value ) |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 336 | { |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 337 | INT old_size, len, res; |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 338 | LPSTR p, env, new_env; |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 339 | BOOL ret = FALSE; |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 340 | |
Alexandre Julliard | becb9a3 | 2000-12-11 03:48:15 +0000 | [diff] [blame] | 341 | RtlAcquirePebLock(); |
Alexandre Julliard | 52900c8 | 2000-08-09 22:33:42 +0000 | [diff] [blame] | 342 | env = p = current_envdb.environ; |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 343 | |
| 344 | /* Find a place to insert the string */ |
| 345 | |
| 346 | res = -1; |
| 347 | len = strlen(name); |
| 348 | while (*p) |
| 349 | { |
Alexandre Julliard | 072dfb5 | 2000-09-25 23:30:56 +0000 | [diff] [blame] | 350 | if (!strncasecmp( name, p, len ) && (p[len] == '=')) break; |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 351 | p += strlen(p) + 1; |
| 352 | } |
| 353 | if (!value && !*p) goto done; /* Value to remove doesn't exist */ |
| 354 | |
| 355 | /* Realloc the buffer */ |
| 356 | |
| 357 | len = value ? strlen(name) + strlen(value) + 2 : 0; |
| 358 | if (*p) len -= strlen(p) + 1; /* The name already exists */ |
Alexandre Julliard | 079fd72 | 2000-01-25 01:41:35 +0000 | [diff] [blame] | 359 | old_size = HeapSize( GetProcessHeap(), 0, env ); |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 360 | if (len < 0) |
| 361 | { |
| 362 | LPSTR next = p + strlen(p) + 1; /* We know there is a next one */ |
| 363 | memmove( next + len, next, old_size - (next - env) ); |
| 364 | } |
Alexandre Julliard | 079fd72 | 2000-01-25 01:41:35 +0000 | [diff] [blame] | 365 | if (!(new_env = HeapReAlloc( GetProcessHeap(), 0, env, old_size + len ))) |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 366 | goto done; |
Alexandre Julliard | becb9a3 | 2000-12-11 03:48:15 +0000 | [diff] [blame] | 367 | if (env_sel) env_sel = SELECTOR_ReallocBlock( env_sel, new_env, old_size + len ); |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 368 | p = new_env + (p - env); |
| 369 | if (len > 0) memmove( p + len, p, old_size - (p - new_env) ); |
| 370 | |
| 371 | /* Set the new string */ |
| 372 | |
| 373 | if (value) |
| 374 | { |
| 375 | strcpy( p, name ); |
| 376 | strcat( p, "=" ); |
| 377 | strcat( p, value ); |
| 378 | } |
Alexandre Julliard | 52900c8 | 2000-08-09 22:33:42 +0000 | [diff] [blame] | 379 | current_envdb.environ = new_env; |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 380 | ret = TRUE; |
| 381 | |
| 382 | done: |
Alexandre Julliard | becb9a3 | 2000-12-11 03:48:15 +0000 | [diff] [blame] | 383 | RtlReleasePebLock(); |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 384 | return ret; |
| 385 | } |
| 386 | |
| 387 | |
| 388 | /*********************************************************************** |
Patrik Stridvall | 2d6457c | 2000-03-28 20:22:59 +0000 | [diff] [blame] | 389 | * SetEnvironmentVariableW (KERNEL32.642) |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 390 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 391 | BOOL WINAPI SetEnvironmentVariableW( LPCWSTR name, LPCWSTR value ) |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 392 | { |
| 393 | LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name ); |
| 394 | LPSTR valueA = HEAP_strdupWtoA( GetProcessHeap(), 0, value ); |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 395 | BOOL ret = SetEnvironmentVariableA( nameA, valueA ); |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 396 | HeapFree( GetProcessHeap(), 0, nameA ); |
| 397 | HeapFree( GetProcessHeap(), 0, valueA ); |
| 398 | return ret; |
| 399 | } |
| 400 | |
| 401 | |
| 402 | /*********************************************************************** |
Patrik Stridvall | 2d6457c | 2000-03-28 20:22:59 +0000 | [diff] [blame] | 403 | * ExpandEnvironmentStringsA (KERNEL32.216) |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 404 | * |
| 405 | * Note: overlapping buffers are not supported; this is how it should be. |
| 406 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 407 | DWORD WINAPI ExpandEnvironmentStringsA( LPCSTR src, LPSTR dst, DWORD count ) |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 408 | { |
| 409 | DWORD len, total_size = 1; /* 1 for terminating '\0' */ |
| 410 | LPCSTR p, var; |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 411 | |
| 412 | if (!count) dst = NULL; |
Alexandre Julliard | becb9a3 | 2000-12-11 03:48:15 +0000 | [diff] [blame] | 413 | RtlAcquirePebLock(); |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 414 | |
| 415 | while (*src) |
| 416 | { |
| 417 | if (*src != '%') |
| 418 | { |
| 419 | if ((p = strchr( src, '%' ))) len = p - src; |
| 420 | else len = strlen(src); |
| 421 | var = src; |
| 422 | src += len; |
| 423 | } |
| 424 | else /* we are at the start of a variable */ |
| 425 | { |
| 426 | if ((p = strchr( src + 1, '%' ))) |
| 427 | { |
| 428 | len = p - src - 1; /* Length of the variable name */ |
Alexandre Julliard | 52900c8 | 2000-08-09 22:33:42 +0000 | [diff] [blame] | 429 | if ((var = ENV_FindVariable( current_envdb.environ, |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 430 | src + 1, len ))) |
| 431 | { |
| 432 | src += len + 2; /* Skip the variable name */ |
| 433 | len = strlen(var); |
| 434 | } |
| 435 | else |
| 436 | { |
| 437 | var = src; /* Copy original name instead */ |
| 438 | len += 2; |
| 439 | src += len; |
| 440 | } |
| 441 | } |
| 442 | else /* unfinished variable name, ignore it */ |
| 443 | { |
| 444 | var = src; |
| 445 | len = strlen(src); /* Copy whole string */ |
| 446 | src += len; |
| 447 | } |
| 448 | } |
| 449 | total_size += len; |
| 450 | if (dst) |
| 451 | { |
| 452 | if (count < len) len = count; |
| 453 | memcpy( dst, var, len ); |
| 454 | dst += len; |
| 455 | count -= len; |
| 456 | } |
| 457 | } |
Alexandre Julliard | becb9a3 | 2000-12-11 03:48:15 +0000 | [diff] [blame] | 458 | RtlReleasePebLock(); |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 459 | |
| 460 | /* Null-terminate the string */ |
| 461 | if (dst) |
| 462 | { |
| 463 | if (!count) dst--; |
| 464 | *dst = '\0'; |
| 465 | } |
| 466 | return total_size; |
| 467 | } |
| 468 | |
| 469 | |
| 470 | /*********************************************************************** |
Patrik Stridvall | 2d6457c | 2000-03-28 20:22:59 +0000 | [diff] [blame] | 471 | * ExpandEnvironmentStringsW (KERNEL32.217) |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 472 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 473 | DWORD WINAPI ExpandEnvironmentStringsW( LPCWSTR src, LPWSTR dst, DWORD len ) |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 474 | { |
| 475 | LPSTR srcA = HEAP_strdupWtoA( GetProcessHeap(), 0, src ); |
| 476 | LPSTR dstA = dst ? HeapAlloc( GetProcessHeap(), 0, len ) : NULL; |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 477 | DWORD ret = ExpandEnvironmentStringsA( srcA, dstA, len ); |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 478 | if (dstA) |
| 479 | { |
Alexandre Julliard | 24a62ab | 2000-11-28 22:40:56 +0000 | [diff] [blame] | 480 | ret = MultiByteToWideChar( CP_ACP, 0, dstA, -1, dst, len ); |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 481 | HeapFree( GetProcessHeap(), 0, dstA ); |
| 482 | } |
| 483 | HeapFree( GetProcessHeap(), 0, srcA ); |
| 484 | return ret; |
| 485 | } |
| 486 | |
Alexandre Julliard | 52900c8 | 2000-08-09 22:33:42 +0000 | [diff] [blame] | 487 | |
| 488 | /*********************************************************************** |
Patrik Stridvall | 00b86a9 | 2000-12-16 21:55:05 +0000 | [diff] [blame] | 489 | * GetDOSEnvironment16 (KERNEL.131) |
Alexandre Julliard | becb9a3 | 2000-12-11 03:48:15 +0000 | [diff] [blame] | 490 | */ |
| 491 | SEGPTR WINAPI GetDOSEnvironment16(void) |
| 492 | { |
Alexandre Julliard | 982a223 | 2000-12-13 20:20:09 +0000 | [diff] [blame] | 493 | return MAKESEGPTR( env_sel, 0 ); |
Alexandre Julliard | becb9a3 | 2000-12-11 03:48:15 +0000 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | |
| 497 | /*********************************************************************** |
Alexandre Julliard | 52900c8 | 2000-08-09 22:33:42 +0000 | [diff] [blame] | 498 | * GetStdHandle (KERNEL32.276) |
| 499 | */ |
| 500 | HANDLE WINAPI GetStdHandle( DWORD std_handle ) |
| 501 | { |
| 502 | switch(std_handle) |
| 503 | { |
| 504 | case STD_INPUT_HANDLE: return current_envdb.hStdin; |
| 505 | case STD_OUTPUT_HANDLE: return current_envdb.hStdout; |
| 506 | case STD_ERROR_HANDLE: return current_envdb.hStderr; |
| 507 | } |
| 508 | SetLastError( ERROR_INVALID_PARAMETER ); |
| 509 | return INVALID_HANDLE_VALUE; |
| 510 | } |
| 511 | |
| 512 | |
| 513 | /*********************************************************************** |
| 514 | * SetStdHandle (KERNEL32.506) |
| 515 | */ |
| 516 | BOOL WINAPI SetStdHandle( DWORD std_handle, HANDLE handle ) |
| 517 | { |
| 518 | switch(std_handle) |
| 519 | { |
| 520 | case STD_INPUT_HANDLE: current_envdb.hStdin = handle; return TRUE; |
| 521 | case STD_OUTPUT_HANDLE: current_envdb.hStdout = handle; return TRUE; |
| 522 | case STD_ERROR_HANDLE: current_envdb.hStderr = handle; return TRUE; |
| 523 | } |
| 524 | SetLastError( ERROR_INVALID_PARAMETER ); |
| 525 | return FALSE; |
| 526 | } |
| 527 | |
| 528 | |
| 529 | /*********************************************************************** |
| 530 | * GetStartupInfoA (KERNEL32.273) |
| 531 | */ |
| 532 | VOID WINAPI GetStartupInfoA( LPSTARTUPINFOA info ) |
| 533 | { |
| 534 | *info = current_startupinfo; |
| 535 | } |
| 536 | |
| 537 | |
| 538 | /*********************************************************************** |
| 539 | * GetStartupInfoW (KERNEL32.274) |
| 540 | */ |
| 541 | VOID WINAPI GetStartupInfoW( LPSTARTUPINFOW info ) |
| 542 | { |
| 543 | info->cb = sizeof(STARTUPINFOW); |
| 544 | info->dwX = current_startupinfo.dwX; |
| 545 | info->dwY = current_startupinfo.dwY; |
| 546 | info->dwXSize = current_startupinfo.dwXSize; |
| 547 | info->dwXCountChars = current_startupinfo.dwXCountChars; |
| 548 | info->dwYCountChars = current_startupinfo.dwYCountChars; |
| 549 | info->dwFillAttribute = current_startupinfo.dwFillAttribute; |
| 550 | info->dwFlags = current_startupinfo.dwFlags; |
| 551 | info->wShowWindow = current_startupinfo.wShowWindow; |
| 552 | info->cbReserved2 = current_startupinfo.cbReserved2; |
| 553 | info->lpReserved2 = current_startupinfo.lpReserved2; |
| 554 | info->hStdInput = current_startupinfo.hStdInput; |
| 555 | info->hStdOutput = current_startupinfo.hStdOutput; |
| 556 | info->hStdError = current_startupinfo.hStdError; |
| 557 | info->lpReserved = HEAP_strdupAtoW (GetProcessHeap(), 0, current_startupinfo.lpReserved ); |
| 558 | info->lpDesktop = HEAP_strdupAtoW (GetProcessHeap(), 0, current_startupinfo.lpDesktop ); |
| 559 | info->lpTitle = HEAP_strdupAtoW (GetProcessHeap(), 0, current_startupinfo.lpTitle ); |
| 560 | } |