forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenviron.cpp
More file actions
645 lines (515 loc) · 15.7 KB
/
environ.cpp
File metadata and controls
645 lines (515 loc) · 15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
/*++
Module Name:
environ.c
Abstract:
Implementation of functions manipulating environment variables.
Revision History:
--*/
#include "pal/palinternal.h"
#include "pal/critsect.h"
#include "pal/dbgmsg.h"
#include "pal/misc.h"
#include <stdlib.h>
SET_DEFAULT_DEBUG_CHANNEL(MISC);
/*++
Function:
GetEnvironmentVariableA
The GetEnvironmentVariable function retrieves the value of the
specified variable from the environment block of the calling
process. The value is in the form of a null-terminated string of
characters.
Parameters
lpName
[in] Pointer to a null-terminated string that specifies the environment variable.
lpBuffer
[out] Pointer to a buffer to receive the value of the specified environment variable.
nSize
[in] Specifies the size, in TCHARs, of the buffer pointed to by the lpBuffer parameter.
Return Values
If the function succeeds, the return value is the number of TCHARs
stored into the buffer pointed to by lpBuffer, not including the
terminating null character.
If the specified environment variable name was not found in the
environment block for the current process, the return value is zero.
If the buffer pointed to by lpBuffer is not large enough, the return
value is the buffer size, in TCHARs, required to hold the value string
and its terminating null character.
--*/
DWORD
PALAPI
GetEnvironmentVariableA(
IN LPCSTR lpName,
OUT LPSTR lpBuffer,
IN DWORD nSize)
{
char *value;
DWORD dwRet = 0;
PERF_ENTRY(GetEnvironmentVariableA);
ENTRY("GetEnvironmentVariableA(lpName=%p (%s), lpBuffer=%p, nSize=%u)\n",
lpName?lpName:"NULL",
lpName?lpName:"NULL", lpBuffer, nSize);
if (lpName == NULL)
{
ERROR("lpName is NULL\n");
SetLastError(ERROR_INVALID_PARAMETER);
goto done;
}
if (lpName[0] == 0)
{
TRACE("lpName is empty string\n", lpName);
SetLastError(ERROR_ENVVAR_NOT_FOUND);
goto done;
}
if (strchr(lpName, '=') != NULL)
{
// GetEnvironmentVariable doesn't permit '=' in variable names.
value = NULL;
}
else
{
value = MiscGetenv(lpName);
}
if (value == NULL)
{
TRACE("%s is not found\n", lpName);
SetLastError(ERROR_ENVVAR_NOT_FOUND);
goto done;
}
if (strlen(value) < nSize)
{
strcpy_s(lpBuffer, nSize, value);
dwRet = strlen(value);
}
else
{
dwRet = strlen(value)+1;
}
SetLastError(ERROR_SUCCESS);
done:
LOGEXIT("GetEnvironmentVariableA returns DWORD 0x%x\n", dwRet);
PERF_EXIT(GetEnvironmentVariableA);
return dwRet;
}
/*++
Function:
GetEnvironmentVariableW
See MSDN doc.
--*/
DWORD
PALAPI
GetEnvironmentVariableW(
IN LPCWSTR lpName,
OUT LPWSTR lpBuffer,
IN DWORD nSize)
{
CHAR *inBuff = NULL;
CHAR *outBuff = NULL;
INT inBuffSize;
DWORD size = 0;
PERF_ENTRY(GetEnvironmentVariableW);
ENTRY("GetEnvironmentVariableW(lpName=%p (%S), lpBuffer=%p, nSize=%u)\n",
lpName?lpName:W16_NULLSTRING,
lpName?lpName:W16_NULLSTRING, lpBuffer, nSize);
inBuffSize = WideCharToMultiByte( CP_ACP, 0, lpName, -1,
inBuff, 0, NULL, NULL);
if ( 0 == inBuffSize )
{
ERROR( "lpName has to be a valid parameter\n" );
SetLastError( ERROR_INVALID_PARAMETER );
goto done;
}
inBuff = (CHAR *)PAL_malloc(inBuffSize);
if (inBuff == NULL)
{
ERROR("malloc failed\n");
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
goto done;
}
if (nSize) {
outBuff = (CHAR *)PAL_malloc(nSize*2);
if (outBuff == NULL)
{
ERROR("malloc failed\n");
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
goto done;
}
}
if ( 0 == WideCharToMultiByte( CP_ACP, 0, lpName, -1, inBuff,
inBuffSize, NULL, NULL ) )
{
ASSERT( "WideCharToMultiByte failed!\n" );
SetLastError( ERROR_INTERNAL_ERROR );
goto done;
}
size = GetEnvironmentVariableA(inBuff, outBuff, nSize);
if (size > nSize)
{
TRACE("Insufficient buffer\n");
}
else if ( size == 0 )
{
/* error handle in GetEnvironmentVariableA */
}
else
{
size = MultiByteToWideChar(CP_ACP, 0, outBuff, -1, lpBuffer, nSize);
if ( 0 != size )
{
/* Not including the NULL. */
size--;
}
else
{
ASSERT( "MultiByteToWideChar failed!\n" );
SetLastError( ERROR_INTERNAL_ERROR );
size = 0;
*lpBuffer = '\0';
}
}
done:
PAL_free(outBuff);
PAL_free(inBuff);
LOGEXIT("GetEnvironmentVariableW returns DWORD 0x%x\n", size);
PERF_EXIT(GetEnvironmentVariableW);
return size;
}
/*++
Function:
SetEnvironmentVariableW
The SetEnvironmentVariable function sets the value of an environment
variable for the current process.
Parameters
lpName
[in] Pointer to a null-terminated string that specifies the
environment variable whose value is being set. The operating
system creates the environment variable if it does not exist
and lpValue is not NULL.
lpValue
[in] Pointer to a null-terminated string containing the new
value of the specified environment variable. If this parameter
is NULL, the variable is deleted from the current process's
environment.
Return Values
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error
information, call GetLastError.
Remarks
This function has no effect on the system environment variables or the
environment variables of other processes.
--*/
BOOL
PALAPI
SetEnvironmentVariableW(
IN LPCWSTR lpName,
IN LPCWSTR lpValue)
{
PCHAR name = NULL;
PCHAR value = NULL;
INT nameSize = 0;
INT valueSize = 0;
BOOL bRet = FALSE;
PERF_ENTRY(SetEnvironmentVariableW);
ENTRY("SetEnvironmentVariableW(lpName=%p (%S), lpValue=%p (%S))\n",
lpName?lpName:W16_NULLSTRING,
lpName?lpName:W16_NULLSTRING, lpValue?lpValue:W16_NULLSTRING, lpValue?lpValue:W16_NULLSTRING);
if ((nameSize = WideCharToMultiByte(CP_ACP, 0, lpName, -1, name, 0,
NULL, NULL)) == 0)
{
ERROR("WideCharToMultiByte failed\n");
SetLastError(ERROR_INVALID_PARAMETER);
goto done;
}
name = (PCHAR)PAL_malloc(sizeof(CHAR)* nameSize);
if (name == NULL)
{
ERROR("malloc failed\n");
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
goto done;
}
if ( 0 == WideCharToMultiByte(CP_ACP, 0, lpName, -1,
name, nameSize, NULL, NULL ) )
{
ASSERT( "WideCharToMultiByte returned 0\n" );
SetLastError( ERROR_INTERNAL_ERROR );
goto done;
}
if ( NULL != lpValue )
{
if ((valueSize = WideCharToMultiByte(CP_ACP, 0, lpValue, -1, value,
0, NULL, NULL)) == 0)
{
ERROR("WideCharToMultiByte failed\n");
SetLastError(ERROR_INVALID_PARAMETER);
goto done;
}
value = (PCHAR)PAL_malloc(sizeof(CHAR)*valueSize);
if ( NULL == value )
{
ERROR("malloc failed\n");
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
goto done;
}
if ( 0 == WideCharToMultiByte( CP_ACP, 0, lpValue, -1,
value, valueSize, NULL, NULL ) )
{
ASSERT("WideCharToMultiByte failed\n");
SetLastError( ERROR_INTERNAL_ERROR );
goto done;
}
}
bRet = SetEnvironmentVariableA(name, value);
done:
PAL_free(value);
PAL_free(name);
LOGEXIT("SetEnvironmentVariableW returning BOOL %d\n", bRet);
PERF_EXIT(SetEnvironmentVariableW);
return bRet;
}
/*++
Function:
GetEnvironmentStringsW
The GetEnvironmentStrings function retrieves the environment block for
the current process.
Parameters
This function has no parameters.
Return Values
The return value is a pointer to an environment block for the current process.
Remarks
The GetEnvironmentStrings function returns a pointer to the
environment block of the calling process. This should be treated as a
read-only block; do not modify it directly. Instead, use the
GetEnvironmentVariable and SetEnvironmentVariable functions to
retrieve or change the environment variables within this block. When
the block is no longer needed, it should be freed by calling
FreeEnvironmentStrings.
--*/
LPWSTR
PALAPI
GetEnvironmentStringsW(
VOID)
{
WCHAR *wenviron = NULL, *tempEnviron;
int i, len, envNum;
PERF_ENTRY(GetEnvironmentStringsW);
ENTRY("GetEnvironmentStringsW()\n");
PALCEnterCriticalSection(&gcsEnvironment);
envNum = 0;
len = 0;
/* get total length of the bytes that we need to allocate */
for (i = 0; palEnvironment[i] != 0; i++)
{
len = MultiByteToWideChar(CP_ACP, 0, palEnvironment[i], -1, wenviron, 0);
envNum += len;
}
wenviron = (WCHAR *)PAL_malloc(sizeof(WCHAR)* (envNum + 1));
if (wenviron == NULL)
{
ERROR("malloc failed\n");
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
goto EXIT;
}
len = 0;
tempEnviron = wenviron;
for (i = 0; palEnvironment[i] != 0; i++)
{
len = MultiByteToWideChar(CP_ACP, 0, palEnvironment[i], -1, tempEnviron, envNum);
tempEnviron += len;
envNum -= len;
}
*tempEnviron = 0; /* Put an extra NULL at the end */
EXIT:
PALCLeaveCriticalSection(&gcsEnvironment);
LOGEXIT("GetEnvironmentStringsW returning %p\n", wenviron);
PERF_EXIT(GetEnvironmentStringsW);
return wenviron;
}
/*++
Function:
GetEnvironmentStringsA
See GetEnvironmentStringsW.
--*/
LPSTR
PALAPI
GetEnvironmentStringsA(
VOID)
{
char *environ = NULL, *tempEnviron;
int i, len, envNum;
PERF_ENTRY(GetEnvironmentStringsA);
ENTRY("GetEnvironmentStringsA()\n");
PALCEnterCriticalSection(&gcsEnvironment);
envNum = 0;
len = 0;
/* get total length of the bytes that we need to allocate */
for (i = 0; palEnvironment[i] != 0; i++)
{
len = strlen(palEnvironment[i]) + 1;
envNum += len;
}
environ = (char *)PAL_malloc(envNum + 1);
if (environ == NULL)
{
ERROR("malloc failed\n");
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
goto EXIT;
}
len = 0;
tempEnviron = environ;
for (i = 0; palEnvironment[i] != 0; i++)
{
len = strlen(palEnvironment[i]) + 1;
memcpy(tempEnviron, palEnvironment[i], len);
tempEnviron += len;
envNum -= len;
}
*tempEnviron = 0; /* Put an extra NULL at the end */
EXIT:
PALCLeaveCriticalSection(&gcsEnvironment);
LOGEXIT("GetEnvironmentStringsA returning %p\n", environ);
PERF_EXIT(GetEnvironmentStringsA);
return environ;
}
/*++
Function:
FreeEnvironmentStringsW
The FreeEnvironmentStrings function frees a block of environment strings.
Parameters
lpszEnvironmentBlock [in] Pointer to a block of environment strings. The pointer to
the block must be obtained by calling the
GetEnvironmentStrings function.
Return Values
If the function succeeds, the return value is nonzero. If the
function fails, the return value is zero. To get extended error
information, call GetLastError.
Remarks
When GetEnvironmentStrings is called, it allocates memory for a block
of environment strings. When the block is no longer needed, it should
be freed by calling FreeEnvironmentStrings.
--*/
BOOL
PALAPI
FreeEnvironmentStringsW(
IN LPWSTR lpValue)
{
PERF_ENTRY(FreeEnvironmentStringsW);
ENTRY("FreeEnvironmentStringsW(lpValue=%p (%S))\n", lpValue?lpValue:W16_NULLSTRING, lpValue?lpValue:W16_NULLSTRING);
if (lpValue != NULL)
{
PAL_free(lpValue);
}
LOGEXIT("FreeEnvironmentStringW returning BOOL TRUE\n");
PERF_EXIT(FreeEnvironmentStringsW);
return TRUE ;
}
/*++
Function:
FreeEnvironmentStringsA
See FreeEnvironmentStringsW.
--*/
BOOL
PALAPI
FreeEnvironmentStringsA(
IN LPSTR lpValue)
{
PERF_ENTRY(FreeEnvironmentStringsA);
ENTRY("FreeEnvironmentStringsA(lpValue=%p (%s))\n", lpValue?lpValue:"NULL", lpValue?lpValue:"NULL");
if (lpValue != NULL)
{
PAL_free(lpValue);
}
LOGEXIT("FreeEnvironmentStringA returning BOOL TRUE\n");
PERF_EXIT(FreeEnvironmentStringsA);
return TRUE ;
}
/*++
Function:
SetEnvironmentVariableA
The SetEnvironmentVariable function sets the value of an environment
variable for the current process.
Parameters
lpName
[in] Pointer to a null-terminated string that specifies the
environment variable whose value is being set. The operating
system creates the environment variable if it does not exist
and lpValue is not NULL.
lpValue
[in] Pointer to a null-terminated string containing the new
value of the specified environment variable. If this parameter
is NULL, the variable is deleted from the current process's
environment.
Return Values
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error
information, call GetLastError.
Remarks
This function has no effect on the system environment variables or the
environment variables of other processes.
--*/
BOOL
PALAPI
SetEnvironmentVariableA(
IN LPCSTR lpName,
IN LPCSTR lpValue)
{
BOOL bRet = FALSE;
int nResult =0;
PERF_ENTRY(SetEnvironmentVariableA);
ENTRY("SetEnvironmentVariableA(lpName=%p (%s), lpValue=%p (%s))\n",
lpName?lpName:"NULL",
lpName?lpName:"NULL", lpValue?lpValue:"NULL", lpValue?lpValue:"NULL");
/*check if the input variable name is null
* and if so exit*/
if ((lpName == NULL) || (lpName[0] == 0))
{
ERROR("lpName is NULL\n");
goto done;
}
/*check if the input value is null and if so
* check if the input name is valid and delete
* the variable name from process environment*/
if (lpValue == NULL)
{
if ((lpValue = MiscGetenv(lpName)) == NULL)
{
ERROR("Couldn't find environment variable (%s)\n", lpName);
SetLastError(ERROR_ENVVAR_NOT_FOUND);
goto done;
}
MiscUnsetenv(lpName);
}
/*All the conditions are met. Set the variable*/
else
{
int iLen = strlen(lpName) + strlen(lpValue) + 2;
LPSTR string = (LPSTR) PAL_malloc(iLen);
if (string == NULL)
{
bRet = FALSE;
ERROR("Unable to allocate memory\n");
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
goto done;
}
sprintf_s(string, iLen, "%s=%s", lpName, lpValue);
nResult = MiscPutenv(string, FALSE) ? 0 : -1;
PAL_free(string);
string = NULL;
// If MiscPutenv returns FALSE, it almost certainly failed to
// allocate memory.
if(nResult == -1)
{
bRet = FALSE;
ERROR("Unable to allocate memory\n");
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
goto done;
}
}
bRet = TRUE;
done:
LOGEXIT("SetEnvironmentVariableA returning BOOL %d\n", bRet);
PERF_EXIT(SetEnvironmentVariableA);
return bRet;
}