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
|
/*
Simple DirectMedia Layer
Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_internal.h"
#include "SDL_getenv_c.h"
#if defined(SDL_PLATFORM_WINDOWS)
#include "../core/windows/SDL_windows.h"
#endif
#ifdef SDL_PLATFORM_ANDROID
#include "../core/android/SDL_android.h"
#endif
#if defined(SDL_PLATFORM_WINDOWS)
#define HAVE_WIN32_ENVIRONMENT
#elif defined(HAVE_GETENV) && \
(defined(HAVE_SETENV) || defined(HAVE_PUTENV)) && \
(defined(HAVE_UNSETENV) || defined(HAVE_PUTENV))
#define HAVE_LIBC_ENVIRONMENT
#if defined(SDL_PLATFORM_MACOS)
#include <crt_externs.h>
#define environ (*_NSGetEnviron())
#elif defined(SDL_PLATFORM_FREEBSD)
#include <dlfcn.h>
#define environ ((char **)dlsym(RTLD_DEFAULT, "environ"))
#else
extern char **environ;
#endif
#else
#define HAVE_LOCAL_ENVIRONMENT
static char **environ;
#endif
struct SDL_Environment
{
SDL_Mutex *lock; // !!! FIXME: reuse SDL_HashTable's lock.
SDL_HashTable *strings;
};
static SDL_Environment *SDL_environment;
SDL_Environment *SDL_GetEnvironment(void)
{
if (!SDL_environment) {
SDL_environment = SDL_CreateEnvironment(true);
}
return SDL_environment;
}
bool SDL_InitEnvironment(void)
{
return (SDL_GetEnvironment() != NULL);
}
void SDL_QuitEnvironment(void)
{
SDL_Environment *env = SDL_environment;
if (env) {
SDL_environment = NULL;
SDL_DestroyEnvironment(env);
}
}
SDL_Environment *SDL_CreateEnvironment(bool populated)
{
SDL_Environment *env = SDL_calloc(1, sizeof(*env));
if (!env) {
return NULL;
}
env->strings = SDL_CreateHashTable(0, false, SDL_HashString, SDL_KeyMatchString, SDL_DestroyHashKey, NULL);
if (!env->strings) {
SDL_free(env);
return NULL;
}
// Don't fail if we can't create a mutex (e.g. on a single-thread environment) // !!! FIXME: single-threaded environments should still return a non-NULL, do-nothing object here. Check for failure!
env->lock = SDL_CreateMutex();
if (populated) {
#ifdef SDL_PLATFORM_WINDOWS
LPWCH strings = GetEnvironmentStringsW();
if (strings) {
for (LPWCH string = strings; *string; string += SDL_wcslen(string) + 1) {
char *variable = WIN_StringToUTF8W(string);
if (!variable) {
continue;
}
char *value = SDL_strchr(variable, '=');
if (!value || value == variable) {
SDL_free(variable);
continue;
}
*value++ = '\0';
SDL_InsertIntoHashTable(env->strings, variable, value, true);
}
FreeEnvironmentStringsW(strings);
}
#else
#ifdef SDL_PLATFORM_ANDROID
// Make sure variables from the application manifest are available
Android_JNI_GetManifestEnvironmentVariables();
#endif
char **strings = environ;
if (strings) {
for (int i = 0; strings[i]; ++i) {
char *variable = SDL_strdup(strings[i]);
if (!variable) {
continue;
}
char *value = SDL_strchr(variable, '=');
if (!value || value == variable) {
SDL_free(variable);
continue;
}
*value++ = '\0';
SDL_InsertIntoHashTable(env->strings, variable, value, true);
}
}
#endif // SDL_PLATFORM_WINDOWS
}
return env;
}
const char *SDL_GetEnvironmentVariable(SDL_Environment *env, const char *name)
{
const char *result = NULL;
if (!env) {
return NULL;
} else if (!name || *name == '\0') {
return NULL;
}
SDL_LockMutex(env->lock);
{
const char *value;
if (SDL_FindInHashTable(env->strings, name, (const void **)&value)) {
result = SDL_GetPersistentString(value);
}
}
SDL_UnlockMutex(env->lock);
return result;
}
typedef struct CountEnvStringsData
{
size_t count;
size_t length;
} CountEnvStringsData;
static bool SDLCALL CountEnvStrings(void *userdata, const SDL_HashTable *table, const void *key, const void *value)
{
CountEnvStringsData *data = (CountEnvStringsData *) userdata;
data->length += SDL_strlen((const char *) key) + 1 + SDL_strlen((const char *) value) + 1;
data->count++;
return true; // keep iterating.
}
typedef struct CopyEnvStringsData
{
char **result;
char *string;
size_t count;
} CopyEnvStringsData;
static bool SDLCALL CopyEnvStrings(void *userdata, const SDL_HashTable *table, const void *vkey, const void *vvalue)
{
CopyEnvStringsData *data = (CopyEnvStringsData *) userdata;
const char *key = (const char *) vkey;
const char *value = (const char *) vvalue;
size_t len;
len = SDL_strlen(key);
data->result[data->count] = data->string;
SDL_memcpy(data->string, key, len);
data->string += len;
*(data->string++) = '=';
len = SDL_strlen(value);
SDL_memcpy(data->string, value, len);
data->string += len;
*(data->string++) = '\0';
data->count++;
return true; // keep iterating.
}
char **SDL_GetEnvironmentVariables(SDL_Environment *env)
{
char **result = NULL;
if (!env) {
SDL_InvalidParamError("env");
return NULL;
}
SDL_LockMutex(env->lock);
{
// First pass, get the size we need for all the strings
CountEnvStringsData countdata = { 0, 0 };
SDL_IterateHashTable(env->strings, CountEnvStrings, &countdata);
// Allocate memory for the strings
result = (char **)SDL_malloc((countdata.count + 1) * sizeof(*result) + countdata.length);
if (result) {
// Second pass, copy the strings
char *string = (char *)(result + countdata.count + 1);
CopyEnvStringsData cpydata = { result, string, 0 };
SDL_IterateHashTable(env->strings, CopyEnvStrings, &cpydata);
SDL_assert(countdata.count == cpydata.count);
result[cpydata.count] = NULL;
}
}
SDL_UnlockMutex(env->lock);
return result;
}
bool SDL_SetEnvironmentVariable(SDL_Environment *env, const char *name, const char *value, bool overwrite)
{
bool result = false;
if (!env) {
return SDL_InvalidParamError("env");
} else if (!name || *name == '\0' || SDL_strchr(name, '=') != NULL) {
return SDL_InvalidParamError("name");
} else if (!value) {
return SDL_InvalidParamError("value");
}
SDL_LockMutex(env->lock);
{
char *string = NULL;
if (SDL_asprintf(&string, "%s=%s", name, value) > 0) {
const size_t len = SDL_strlen(name);
string[len] = '\0';
const char *origname = name;
name = string;
value = string + len + 1;
result = SDL_InsertIntoHashTable(env->strings, name, value, overwrite);
if (!result) {
SDL_free(string);
if (!overwrite) {
const void *existing_value = NULL;
// !!! FIXME: InsertIntoHashTable does this lookup too, maybe we should have a means to report that, to avoid duplicate work?
if (SDL_FindInHashTable(env->strings, origname, &existing_value)) {
result = true; // it already existed, and we refused to overwrite it. Call it success.
}
}
}
}
}
SDL_UnlockMutex(env->lock);
return result;
}
bool SDL_UnsetEnvironmentVariable(SDL_Environment *env, const char *name)
{
bool result = false;
if (!env) {
return SDL_InvalidParamError("env");
} else if (!name || *name == '\0' || SDL_strchr(name, '=') != NULL) {
return SDL_InvalidParamError("name");
}
SDL_LockMutex(env->lock);
{
const void *value;
if (SDL_FindInHashTable(env->strings, name, &value)) {
result = SDL_RemoveFromHashTable(env->strings, name);
} else {
result = true;
}
}
SDL_UnlockMutex(env->lock);
return result;
}
void SDL_DestroyEnvironment(SDL_Environment *env)
{
if (!env || env == SDL_environment) {
return;
}
SDL_DestroyMutex(env->lock);
SDL_DestroyHashTable(env->strings);
SDL_free(env);
}
// Put a variable into the environment
// Note: Name may not contain a '=' character. (Reference: http://www.unix.com/man-page/Linux/3/setenv/)
#ifdef HAVE_LIBC_ENVIRONMENT
#if defined(HAVE_SETENV)
int SDL_setenv_unsafe(const char *name, const char *value, int overwrite)
{
// Input validation
if (!name || *name == '\0' || SDL_strchr(name, '=') != NULL || !value) {
return -1;
}
SDL_SetEnvironmentVariable(SDL_GetEnvironment(), name, value, (overwrite != 0));
return setenv(name, value, overwrite);
}
// We have a real environment table, but no real setenv? Fake it w/ putenv.
#else
int SDL_setenv_unsafe(const char *name, const char *value, int overwrite)
{
char *new_variable;
// Input validation
if (!name || *name == '\0' || SDL_strchr(name, '=') != NULL || !value) {
return -1;
}
SDL_SetEnvironmentVariable(SDL_GetEnvironment(), name, value, (overwrite != 0));
if (getenv(name) != NULL) {
if (!overwrite) {
return 0; // leave the existing one there.
}
}
// This leaks. Sorry. Get a better OS so we don't have to do this.
SDL_asprintf(&new_variable, "%s=%s", name, value);
if (!new_variable) {
return -1;
}
return putenv(new_variable);
}
#endif
#elif defined(HAVE_WIN32_ENVIRONMENT)
int SDL_setenv_unsafe(const char *name, const char *value, int overwrite)
{
// Input validation
if (!name || *name == '\0' || SDL_strchr(name, '=') != NULL || !value) {
return -1;
}
SDL_SetEnvironmentVariable(SDL_GetEnvironment(), name, value, (overwrite != 0));
if (!overwrite) {
if (GetEnvironmentVariableA(name, NULL, 0) > 0) {
return 0; // asked not to overwrite existing value.
}
}
if (!SetEnvironmentVariableA(name, value)) {
return -1;
}
return 0;
}
#else // roll our own
int SDL_setenv_unsafe(const char *name, const char *value, int overwrite)
{
int added;
size_t len, i;
char **new_env;
char *new_variable;
// Input validation
if (!name || *name == '\0' || SDL_strchr(name, '=') != NULL || !value) {
return -1;
}
// See if it already exists
if (!overwrite && SDL_getenv_unsafe(name)) {
return 0;
}
SDL_SetEnvironmentVariable(SDL_GetEnvironment(), name, value, (overwrite != 0));
// Allocate memory for the variable
len = SDL_strlen(name) + SDL_strlen(value) + 2;
new_variable = (char *)SDL_malloc(len);
if (!new_variable) {
return -1;
}
SDL_snprintf(new_variable, len, "%s=%s", name, value);
value = new_variable + SDL_strlen(name) + 1;
name = new_variable;
// Actually put it into the environment
added = 0;
i = 0;
if (environ) {
// Check to see if it's already there...
len = (value - name);
for (; environ[i]; ++i) {
if (SDL_strncmp(environ[i], name, len) == 0) {
// If we found it, just replace the entry
SDL_free(environ[i]);
environ[i] = new_variable;
added = 1;
break;
}
}
}
// Didn't find it in the environment, expand and add
if (!added) {
new_env = SDL_realloc(environ, (i + 2) * sizeof(char *));
if (new_env) {
environ = new_env;
environ[i++] = new_variable;
environ[i++] = (char *)0;
added = 1;
} else {
SDL_free(new_variable);
}
}
return added ? 0 : -1;
}
#endif // HAVE_LIBC_ENVIRONMENT
#ifdef HAVE_LIBC_ENVIRONMENT
#if defined(HAVE_UNSETENV)
int SDL_unsetenv_unsafe(const char *name)
{
// Input validation
if (!name || *name == '\0' || SDL_strchr(name, '=') != NULL) {
return -1;
}
SDL_UnsetEnvironmentVariable(SDL_GetEnvironment(), name);
return unsetenv(name);
}
// We have a real environment table, but no unsetenv? Fake it w/ putenv.
#else
int SDL_unsetenv_unsafe(const char *name)
{
// Input validation
if (!name || *name == '\0' || SDL_strchr(name, '=') != NULL) {
return -1;
}
SDL_UnsetEnvironmentVariable(SDL_GetEnvironment(), name);
// Hope this environment uses the non-standard extension of removing the environment variable if it has no '='
return putenv(name);
}
#endif
#elif defined(HAVE_WIN32_ENVIRONMENT)
int SDL_unsetenv_unsafe(const char *name)
{
// Input validation
if (!name || *name == '\0' || SDL_strchr(name, '=') != NULL) {
return -1;
}
SDL_UnsetEnvironmentVariable(SDL_GetEnvironment(), name);
if (!SetEnvironmentVariableA(name, NULL)) {
return -1;
}
return 0;
}
#else
int SDL_unsetenv_unsafe(const char *name)
{
size_t len, i;
// Input validation
if (!name || *name == '\0' || SDL_strchr(name, '=') != NULL) {
return -1;
}
SDL_UnsetEnvironmentVariable(SDL_GetEnvironment(), name);
if (environ) {
len = SDL_strlen(name);
for (i = 0; environ[i]; ++i) {
if ((SDL_strncmp(environ[i], name, len) == 0) &&
(environ[i][len] == '=')) {
// Just clear out this entry for now
*environ[i] = '\0';
break;
}
}
}
return 0;
}
#endif // HAVE_LIBC_ENVIRONMENT
// Retrieve a variable named "name" from the environment
#ifdef HAVE_LIBC_ENVIRONMENT
const char *SDL_getenv_unsafe(const char *name)
{
#ifdef SDL_PLATFORM_ANDROID
// Make sure variables from the application manifest are available
Android_JNI_GetManifestEnvironmentVariables();
#endif
// Input validation
if (!name || *name == '\0') {
return NULL;
}
return getenv(name);
}
#elif defined(HAVE_WIN32_ENVIRONMENT)
const char *SDL_getenv_unsafe(const char *name)
{
DWORD length, maxlen = 0;
char *string = NULL;
const char *result = NULL;
// Input validation
if (!name || *name == '\0') {
return NULL;
}
for ( ; ; ) {
SetLastError(ERROR_SUCCESS);
length = GetEnvironmentVariableA(name, string, maxlen);
if (length > maxlen) {
char *temp = (char *)SDL_realloc(string, length);
if (!temp) {
return NULL;
}
string = temp;
maxlen = length;
} else {
if (GetLastError() != ERROR_SUCCESS) {
if (string) {
SDL_free(string);
}
return NULL;
}
break;
}
}
if (string) {
result = SDL_GetPersistentString(string);
SDL_free(string);
}
return result;
}
#else
const char *SDL_getenv_unsafe(const char *name)
{
size_t len, i;
const char *value = NULL;
// Input validation
if (!name || *name == '\0') {
return NULL;
}
if (environ) {
len = SDL_strlen(name);
for (i = 0; environ[i]; ++i) {
if ((SDL_strncmp(environ[i], name, len) == 0) &&
(environ[i][len] == '=')) {
value = &environ[i][len + 1];
break;
}
}
}
return value;
}
#endif // HAVE_LIBC_ENVIRONMENT
const char *SDL_getenv(const char *name)
{
return SDL_GetEnvironmentVariable(SDL_GetEnvironment(), name);
}
|