SDL: Removed SDL_CleanupEnvironment()

From 97d40b9218038edaf7afe61d6114fb48d8f26a82 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Sun, 15 Sep 2024 09:55:38 -0700
Subject: [PATCH] Removed SDL_CleanupEnvironment()

---
 include/SDL3/SDL_stdinc.h         | 21 +++------------------
 src/SDL.c                         |  5 +++--
 src/dynapi/SDL_dynapi.sym         |  1 -
 src/dynapi/SDL_dynapi_overrides.h |  1 -
 src/dynapi/SDL_dynapi_procs.h     |  1 -
 src/stdlib/SDL_getenv.c           |  7 ++++++-
 src/stdlib/SDL_getenv_c.h         | 24 ++++++++++++++++++++++++
 7 files changed, 36 insertions(+), 24 deletions(-)
 create mode 100644 src/stdlib/SDL_getenv_c.h

diff --git a/include/SDL3/SDL_stdinc.h b/include/SDL3/SDL_stdinc.h
index cbed61392b9fc..0f98b35d11d9b 100644
--- a/include/SDL3/SDL_stdinc.h
+++ b/include/SDL3/SDL_stdinc.h
@@ -984,7 +984,6 @@ extern SDL_DECLSPEC int SDLCALL SDL_GetNumAllocations(void);
  * \since This struct is available since SDL 3.0.0.
  *
  * \sa SDL_GetEnvironment
- * \sa SDL_CleanupEnvironment
  * \sa SDL_CreateEnvironment
  * \sa SDL_GetEnvironmentVariable
  * \sa SDL_GetEnvironmentVariables
@@ -999,7 +998,9 @@ typedef struct SDL_Environment SDL_Environment;
  *
  * This is initialized at application start and is not affected by setenv()
  * and unsetenv() calls after that point. Use SDL_SetEnvironmentVariable() and
- * SDL_UnsetEnvironmentVariable() if you want to modify this environment.
+ * SDL_UnsetEnvironmentVariable() if you want to modify this environment, or
+ * SDL_setenv_unsafe() or SDL_unsetenv_unsafe() if you want changes to persist
+ * in the C runtime environment after SDL_Quit().
  *
  * \returns a pointer to the environment for the process or NULL on failure;
  *          call SDL_GetError() for more information.
@@ -1008,7 +1009,6 @@ typedef struct SDL_Environment SDL_Environment;
  *
  * \since This function is available since SDL 3.0.0.
  *
- * \sa SDL_CleanupEnvironment
  * \sa SDL_GetEnvironmentVariable
  * \sa SDL_GetEnvironmentVariables
  * \sa SDL_SetEnvironmentVariable
@@ -1016,21 +1016,6 @@ typedef struct SDL_Environment SDL_Environment;
  */
 extern SDL_DECLSPEC SDL_Environment * SDLCALL SDL_GetEnvironment(void);
 
-/**
- * Cleanup the process environment.
- *
- * This is called during SDL_Quit() to free the process environment. If
- * SDL_GetEnvironment() is called afterwards, it will automatically create a
- * new environment copied from the C runtime environment.
- *
- * \threadsafety This function is not thread-safe.
- *
- * \since This function is available since SDL 3.0.0.
- *
- * \sa SDL_GetEnvironment
- */
-extern SDL_DECLSPEC void SDLCALL SDL_CleanupEnvironment(void);
-
 /**
  * Create a set of environment variables
  *
diff --git a/src/SDL.c b/src/SDL.c
index 63c3347fb0e81..4e93d27381095 100644
--- a/src/SDL.c
+++ b/src/SDL.c
@@ -49,6 +49,7 @@
 #include "joystick/SDL_joystick_c.h"
 #include "render/SDL_sysrender.h"
 #include "sensor/SDL_sensor_c.h"
+#include "stdlib/SDL_getenv_c.h"
 #include "thread/SDL_thread_c.h"
 #include "video/SDL_pixels_c.h"
 #include "video/SDL_video_c.h"
@@ -250,23 +251,23 @@ void SDL_SetMainReady(void)
 void SDL_InitMainThread(void)
 {
     SDL_InitTLSData();
+    SDL_InitEnvironment();
     SDL_InitTicks();
     SDL_InitFilesystem();
     SDL_InitLog();
     SDL_InitProperties();
     SDL_GetGlobalProperties();
-    SDL_GetEnvironment();
     SDL_InitHints();
 }
 
 static void SDL_QuitMainThread(void)
 {
     SDL_QuitHints();
-    SDL_CleanupEnvironment();
     SDL_QuitProperties();
     SDL_QuitLog();
     SDL_QuitFilesystem();
     SDL_QuitTicks();
+    SDL_QuitEnvironment();
     SDL_QuitTLSData();
 }
 
diff --git a/src/dynapi/SDL_dynapi.sym b/src/dynapi/SDL_dynapi.sym
index 01f6f506d0602..a1268d580a484 100644
--- a/src/dynapi/SDL_dynapi.sym
+++ b/src/dynapi/SDL_dynapi.sym
@@ -52,7 +52,6 @@ SDL3_0.0.0 {
     SDL_BroadcastCondition;
     SDL_CaptureMouse;
     SDL_ClaimWindowForGPUDevice;
-    SDL_CleanupEnvironment;
     SDL_CleanupTLS;
     SDL_ClearAudioStream;
     SDL_ClearClipboardData;
diff --git a/src/dynapi/SDL_dynapi_overrides.h b/src/dynapi/SDL_dynapi_overrides.h
index e0d1be09216d5..341e316c728d8 100644
--- a/src/dynapi/SDL_dynapi_overrides.h
+++ b/src/dynapi/SDL_dynapi_overrides.h
@@ -77,7 +77,6 @@
 #define SDL_BroadcastCondition SDL_BroadcastCondition_REAL
 #define SDL_CaptureMouse SDL_CaptureMouse_REAL
 #define SDL_ClaimWindowForGPUDevice SDL_ClaimWindowForGPUDevice_REAL
-#define SDL_CleanupEnvironment SDL_CleanupEnvironment_REAL
 #define SDL_CleanupTLS SDL_CleanupTLS_REAL
 #define SDL_ClearAudioStream SDL_ClearAudioStream_REAL
 #define SDL_ClearClipboardData SDL_ClearClipboardData_REAL
diff --git a/src/dynapi/SDL_dynapi_procs.h b/src/dynapi/SDL_dynapi_procs.h
index 8c3c10a05a0bf..ae2a1e4ddc37c 100644
--- a/src/dynapi/SDL_dynapi_procs.h
+++ b/src/dynapi/SDL_dynapi_procs.h
@@ -97,7 +97,6 @@ SDL_DYNAPI_PROC(SDL_bool,SDL_BlitSurfaceUncheckedScaled,(SDL_Surface *a, const S
 SDL_DYNAPI_PROC(void,SDL_BroadcastCondition,(SDL_Condition *a),(a),)
 SDL_DYNAPI_PROC(SDL_bool,SDL_CaptureMouse,(SDL_bool a),(a),return)
 SDL_DYNAPI_PROC(SDL_bool,SDL_ClaimWindowForGPUDevice,(SDL_GPUDevice *a, SDL_Window *b),(a,b),return)
-SDL_DYNAPI_PROC(void,SDL_CleanupEnvironment,(void),(),)
 SDL_DYNAPI_PROC(void,SDL_CleanupTLS,(void),(),)
 SDL_DYNAPI_PROC(SDL_bool,SDL_ClearAudioStream,(SDL_AudioStream *a),(a),return)
 SDL_DYNAPI_PROC(SDL_bool,SDL_ClearClipboardData,(void),(),return)
diff --git a/src/stdlib/SDL_getenv.c b/src/stdlib/SDL_getenv.c
index 0243b9bf92446..c78b95167cedf 100644
--- a/src/stdlib/SDL_getenv.c
+++ b/src/stdlib/SDL_getenv.c
@@ -66,7 +66,12 @@ SDL_Environment *SDL_GetEnvironment(void)
     return SDL_environment;
 }
 
-void SDL_CleanupEnvironment(void)
+SDL_bool SDL_InitEnvironment(void)
+{
+    return (SDL_GetEnvironment() != NULL);
+}
+
+void SDL_QuitEnvironment(void)
 {
     SDL_Environment *env = SDL_environment;
 
diff --git a/src/stdlib/SDL_getenv_c.h b/src/stdlib/SDL_getenv_c.h
new file mode 100644
index 0000000000000..f1be3d33473ba
--- /dev/null
+++ b/src/stdlib/SDL_getenv_c.h
@@ -0,0 +1,24 @@
+/*
+  Simple DirectMedia Layer
+  Copyright (C) 1997-2024 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"
+
+extern bool SDL_InitEnvironment(void);
+extern void SDL_QuitEnvironment(void);