Skip to content

Commit 5876648

Browse files
committed
Fix C# environment variables (SDL-related issue)
For some reason, these SDL defines mess with C#'s ability to use environment variables
1 parent 45509c2 commit 5876648

File tree

4 files changed

+86
-3
lines changed

4 files changed

+86
-3
lines changed

drivers/sdl/SDL_build_config_private.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,11 @@
8787
#ifdef __linux__
8888
#define HAVE_INOTIFY 1
8989
#define HAVE_INOTIFY_INIT1 1
90-
#define HAVE_GETENV 1
91-
#define HAVE_SETENV 1
92-
#define HAVE_UNSETENV 1
90+
// Don't add these defines, for some reason they mess with C#'s ability
91+
// to use environment variables (see GH-109024)
92+
//#define HAVE_GETENV 1
93+
//#define HAVE_SETENV 1
94+
//#define HAVE_UNSETENV 1
9395
#endif
9496

9597
#ifdef DBUS_ENABLED

thirdparty/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -972,6 +972,7 @@ Patches:
972972
- `0003-std-include.patch` (GH-108144)
973973
- `0004-errno-include.patch` (GH-108354)
974974
- `0005-fix-libudev-dbus.patch` (GH-108373)
975+
- `0006-fix-cs-environ.patch` (GH-109283)
975976

976977
The SDL source code folder includes `hidapi` library inside of folder `thirdparty/sdl/hidapi/`.
977978
Its version and license is described in this file under `hidapi`.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
diff --git a/thirdparty/sdl/stdlib/SDL_getenv.c b/thirdparty/sdl/stdlib/SDL_getenv.c
2+
index b4a19224655..e23f8a0ea0d 100644
3+
--- a/thirdparty/sdl/stdlib/SDL_getenv.c
4+
+++ b/thirdparty/sdl/stdlib/SDL_getenv.c
5+
@@ -50,6 +50,9 @@ extern char **environ;
6+
static char **environ;
7+
#endif
8+
9+
+#if defined(SDL_PLATFORM_LINUX) || defined(SDL_PLATFORM_MACOS) || defined(SDL_PLATFORM_FREEBSD)
10+
+#include <stdlib.h>
11+
+#endif
12+
13+
struct SDL_Environment
14+
{
15+
@@ -149,6 +152,9 @@ SDL_Environment *SDL_CreateEnvironment(bool populated)
16+
17+
const char *SDL_GetEnvironmentVariable(SDL_Environment *env, const char *name)
18+
{
19+
+#if defined(SDL_PLATFORM_LINUX) || defined(SDL_PLATFORM_MACOS) || defined(SDL_PLATFORM_FREEBSD)
20+
+ return getenv(name);
21+
+#else
22+
const char *result = NULL;
23+
24+
if (!env) {
25+
@@ -168,6 +174,7 @@ const char *SDL_GetEnvironmentVariable(SDL_Environment *env, const char *name)
26+
SDL_UnlockMutex(env->lock);
27+
28+
return result;
29+
+#endif
30+
}
31+
32+
typedef struct CountEnvStringsData
33+
@@ -246,6 +253,9 @@ char **SDL_GetEnvironmentVariables(SDL_Environment *env)
34+
35+
bool SDL_SetEnvironmentVariable(SDL_Environment *env, const char *name, const char *value, bool overwrite)
36+
{
37+
+#if defined(SDL_PLATFORM_LINUX) || defined(SDL_PLATFORM_MACOS) || defined(SDL_PLATFORM_FREEBSD)
38+
+ return setenv(name, value, overwrite);
39+
+#else
40+
bool result = false;
41+
42+
if (!env) {
43+
@@ -281,10 +291,14 @@ bool SDL_SetEnvironmentVariable(SDL_Environment *env, const char *name, const ch
44+
SDL_UnlockMutex(env->lock);
45+
46+
return result;
47+
+#endif
48+
}
49+
50+
bool SDL_UnsetEnvironmentVariable(SDL_Environment *env, const char *name)
51+
{
52+
+#if defined(SDL_PLATFORM_LINUX) || defined(SDL_PLATFORM_MACOS) || defined(SDL_PLATFORM_FREEBSD)
53+
+ return unsetenv(name);
54+
+#else
55+
bool result = false;
56+
57+
if (!env) {
58+
@@ -305,6 +319,7 @@ bool SDL_UnsetEnvironmentVariable(SDL_Environment *env, const char *name)
59+
SDL_UnlockMutex(env->lock);
60+
61+
return result;
62+
+#endif
63+
}
64+
65+
void SDL_DestroyEnvironment(SDL_Environment *env)

thirdparty/sdl/stdlib/SDL_getenv.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ extern char **environ;
5050
static char **environ;
5151
#endif
5252

53+
#if defined(SDL_PLATFORM_LINUX) || defined(SDL_PLATFORM_MACOS) || defined(SDL_PLATFORM_FREEBSD)
54+
#include <stdlib.h>
55+
#endif
5356

5457
struct SDL_Environment
5558
{
@@ -149,6 +152,9 @@ SDL_Environment *SDL_CreateEnvironment(bool populated)
149152

150153
const char *SDL_GetEnvironmentVariable(SDL_Environment *env, const char *name)
151154
{
155+
#if defined(SDL_PLATFORM_LINUX) || defined(SDL_PLATFORM_MACOS) || defined(SDL_PLATFORM_FREEBSD)
156+
return getenv(name);
157+
#else
152158
const char *result = NULL;
153159

154160
if (!env) {
@@ -168,6 +174,7 @@ const char *SDL_GetEnvironmentVariable(SDL_Environment *env, const char *name)
168174
SDL_UnlockMutex(env->lock);
169175

170176
return result;
177+
#endif
171178
}
172179

173180
typedef struct CountEnvStringsData
@@ -246,6 +253,9 @@ char **SDL_GetEnvironmentVariables(SDL_Environment *env)
246253

247254
bool SDL_SetEnvironmentVariable(SDL_Environment *env, const char *name, const char *value, bool overwrite)
248255
{
256+
#if defined(SDL_PLATFORM_LINUX) || defined(SDL_PLATFORM_MACOS) || defined(SDL_PLATFORM_FREEBSD)
257+
return setenv(name, value, overwrite);
258+
#else
249259
bool result = false;
250260

251261
if (!env) {
@@ -281,10 +291,14 @@ bool SDL_SetEnvironmentVariable(SDL_Environment *env, const char *name, const ch
281291
SDL_UnlockMutex(env->lock);
282292

283293
return result;
294+
#endif
284295
}
285296

286297
bool SDL_UnsetEnvironmentVariable(SDL_Environment *env, const char *name)
287298
{
299+
#if defined(SDL_PLATFORM_LINUX) || defined(SDL_PLATFORM_MACOS) || defined(SDL_PLATFORM_FREEBSD)
300+
return unsetenv(name);
301+
#else
288302
bool result = false;
289303

290304
if (!env) {
@@ -305,6 +319,7 @@ bool SDL_UnsetEnvironmentVariable(SDL_Environment *env, const char *name)
305319
SDL_UnlockMutex(env->lock);
306320

307321
return result;
322+
#endif
308323
}
309324

310325
void SDL_DestroyEnvironment(SDL_Environment *env)

0 commit comments

Comments
 (0)