Skip to content

Commit 4f044ab

Browse files
committed
Added SDL_unsetenv()
1 parent 538adc5 commit 4f044ab

File tree

8 files changed

+126
-32
lines changed

8 files changed

+126
-32
lines changed

Diff for: include/SDL3/SDL_stdinc.h

+1
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,7 @@ extern SDL_DECLSPEC int SDLCALL SDL_GetNumAllocations(void);
665665

666666
extern SDL_DECLSPEC const char * SDLCALL SDL_getenv(const char *name);
667667
extern SDL_DECLSPEC int SDLCALL SDL_setenv(const char *name, const char *value, int overwrite);
668+
extern SDL_DECLSPEC int SDLCALL SDL_unsetenv(const char *name);
668669

669670
typedef int (SDLCALL *SDL_CompareCallback)(const void *a, const void *b);
670671
extern SDL_DECLSPEC void SDLCALL SDL_qsort(void *base, size_t nmemb, size_t size, SDL_CompareCallback compare);

Diff for: include/build_config/SDL_build_config_android.h

-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
#define HAVE_REALLOC 1
5959
#define HAVE_FREE 1
6060
#define HAVE_GETENV 1
61-
#define HAVE_SETENV 1
6261
#define HAVE_PUTENV 1
6362
#define HAVE_SETENV 1
6463
#define HAVE_UNSETENV 1

Diff for: include/build_config/SDL_build_config_ios.h

-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
#define HAVE_REALLOC 1
5151
#define HAVE_FREE 1
5252
#define HAVE_GETENV 1
53-
#define HAVE_SETENV 1
5453
#define HAVE_PUTENV 1
5554
#define HAVE_SETENV 1
5655
#define HAVE_UNSETENV 1

Diff for: src/dynapi/SDL_dynapi.sym

+1
Original file line numberDiff line numberDiff line change
@@ -1038,6 +1038,7 @@ SDL3_0.0.0 {
10381038
SDL_uitoa;
10391039
SDL_ulltoa;
10401040
SDL_ultoa;
1041+
SDL_unsetenv;
10411042
SDL_utf8strlcpy;
10421043
SDL_utf8strlen;
10431044
SDL_utf8strnlen;

Diff for: src/dynapi/SDL_dynapi_overrides.h

+1
Original file line numberDiff line numberDiff line change
@@ -1063,6 +1063,7 @@
10631063
#define SDL_uitoa SDL_uitoa_REAL
10641064
#define SDL_ulltoa SDL_ulltoa_REAL
10651065
#define SDL_ultoa SDL_ultoa_REAL
1066+
#define SDL_unsetenv SDL_unsetenv_REAL
10661067
#define SDL_utf8strlcpy SDL_utf8strlcpy_REAL
10671068
#define SDL_utf8strlen SDL_utf8strlen_REAL
10681069
#define SDL_utf8strnlen SDL_utf8strnlen_REAL

Diff for: src/dynapi/SDL_dynapi_procs.h

+1
Original file line numberDiff line numberDiff line change
@@ -1069,6 +1069,7 @@ SDL_DYNAPI_PROC(float,SDL_truncf,(float a),(a),return)
10691069
SDL_DYNAPI_PROC(char*,SDL_uitoa,(unsigned int a, char *b, int c),(a,b,c),return)
10701070
SDL_DYNAPI_PROC(char*,SDL_ulltoa,(Uint64 a, char *b, int c),(a,b,c),return)
10711071
SDL_DYNAPI_PROC(char*,SDL_ultoa,(unsigned long a, char *b, int c),(a,b,c),return)
1072+
SDL_DYNAPI_PROC(int,SDL_unsetenv,(const char *a),(a),return)
10721073
SDL_DYNAPI_PROC(size_t,SDL_utf8strlcpy,(SDL_OUT_Z_CAP(c) char *a, const char *b, size_t c),(a,b,c),return)
10731074
SDL_DYNAPI_PROC(size_t,SDL_utf8strlen,(const char *a),(a),return)
10741075
SDL_DYNAPI_PROC(size_t,SDL_utf8strnlen,(const char *a, size_t b),(a,b),return)

Diff for: src/stdlib/SDL_getenv.c

+75-13
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,17 @@
3030
#include "../core/android/SDL_android.h"
3131
#endif
3232

33-
#if (defined(HAVE_GETENV) && defined(HAVE_SETENV)) || \
34-
(defined(HAVE_GETENV) && defined(HAVE_PUTENV) && defined(HAVE_UNSETENV))
33+
#if 0
34+
#if defined(HAVE_GETENV) && \
35+
(defined(HAVE_SETENV) || defined(HAVE_PUTENV)) && \
36+
(defined(HAVE_UNSETENV) || defined(HAVE_PUTENV))
3537
#define HAVE_LIBC_ENVIRONMENT
3638
#elif defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK)
3739
#define HAVE_WIN32_ENVIRONMENT
3840
#else
3941
#define HAVE_LOCAL_ENVIRONMENT
4042
#endif
43+
#endif
4144

4245
/* Put a variable into the environment */
4346
/* Note: Name may not contain a '=' character. (Reference: http://www.unix.com/man-page/Linux/3/setenv/) */
@@ -64,9 +67,7 @@ int SDL_setenv(const char *name, const char *value, int overwrite)
6467
}
6568

6669
if (getenv(name) != NULL) {
67-
if (overwrite) {
68-
unsetenv(name);
69-
} else {
70+
if (!overwrite) {
7071
return 0; /* leave the existing one there. */
7172
}
7273
}
@@ -92,7 +93,7 @@ int SDL_setenv(const char *name, const char *value, int overwrite)
9293
return 0; /* asked not to overwrite existing value. */
9394
}
9495
}
95-
if (!SetEnvironmentVariableA(name, *value ? value : NULL)) {
96+
if (!SetEnvironmentVariableA(name, value)) {
9697
return -1;
9798
}
9899
return 0;
@@ -138,15 +139,13 @@ int SDL_setenv(const char *name, const char *value, int overwrite)
138139
len = (value - name);
139140
for (; SDL_env[i]; ++i) {
140141
if (SDL_strncmp(SDL_env[i], name, len) == 0) {
142+
/* If we found it, just replace the entry */
143+
SDL_free(SDL_env[i]);
144+
SDL_env[i] = new_variable;
145+
added = 1;
141146
break;
142147
}
143148
}
144-
/* If we found it, just replace the entry */
145-
if (SDL_env[i]) {
146-
SDL_free(SDL_env[i]);
147-
SDL_env[i] = new_variable;
148-
added = 1;
149-
}
150149
}
151150

152151
/* Didn't find it in the environment, expand and add */
@@ -165,6 +164,68 @@ int SDL_setenv(const char *name, const char *value, int overwrite)
165164
}
166165
#endif // HAVE_LIBC_ENVIRONMENT
167166

167+
#ifdef HAVE_LIBC_ENVIRONMENT
168+
#if defined(HAVE_UNSETENV)
169+
int SDL_unsetenv(const char *name)
170+
{
171+
/* Input validation */
172+
if (!name || *name == '\0' || SDL_strchr(name, '=') != NULL) {
173+
return -1;
174+
}
175+
176+
return unsetenv(name);
177+
}
178+
/* We have a real environment table, but no unsetenv? Fake it w/ putenv. */
179+
#else
180+
int SDL_unsetenv(const char *name)
181+
{
182+
/* Input validation */
183+
if (!name || *name == '\0' || SDL_strchr(name, '=') != NULL) {
184+
return -1;
185+
}
186+
187+
// Hope this environment uses the non-standard extension of removing the environment variable if it has no '='
188+
return putenv(name);
189+
}
190+
#endif
191+
#elif defined(HAVE_WIN32_ENVIRONMENT)
192+
int SDL_unsetenv(const char *name)
193+
{
194+
/* Input validation */
195+
if (!name || *name == '\0' || SDL_strchr(name, '=') != NULL) {
196+
return -1;
197+
}
198+
199+
if (!SetEnvironmentVariableA(name, NULL)) {
200+
return -1;
201+
}
202+
return 0;
203+
}
204+
#else
205+
int SDL_unsetenv(const char *name)
206+
{
207+
size_t len, i;
208+
209+
/* Input validation */
210+
if (!name || *name == '\0' || SDL_strchr(name, '=') != NULL) {
211+
return -1;
212+
}
213+
214+
if (SDL_env) {
215+
len = SDL_strlen(name);
216+
for (i = 0; SDL_env[i]; ++i) {
217+
if ((SDL_strncmp(SDL_env[i], name, len) == 0) &&
218+
(SDL_env[i][len] == '=')) {
219+
/* Just clear out this entry for now */
220+
*SDL_env[i] = '\0';
221+
break;
222+
}
223+
}
224+
}
225+
return 0;
226+
}
227+
#endif // HAVE_LIBC_ENVIRONMENT
228+
168229
/* Retrieve a variable named "name" from the environment */
169230
#ifdef HAVE_LIBC_ENVIRONMENT
170231
const char *SDL_getenv(const char *name)
@@ -227,10 +288,11 @@ const char *SDL_getenv(const char *name)
227288
value = (char *)0;
228289
if (SDL_env) {
229290
len = SDL_strlen(name);
230-
for (i = 0; SDL_env[i] && !value; ++i) {
291+
for (i = 0; SDL_env[i]; ++i) {
231292
if ((SDL_strncmp(SDL_env[i], name, len) == 0) &&
232293
(SDL_env[i][len] == '=')) {
233294
value = &SDL_env[i][len + 1];
295+
break;
234296
}
235297
}
236298
}

Diff for: test/testautomation_stdlib.c

+47-17
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ static int stdlib_getsetenv(void *arg)
624624
text);
625625
}
626626

627-
/* Set value 1 without overwrite */
627+
/* Set value 1 with overwrite */
628628
overwrite = 1;
629629
expected = value1;
630630
result = SDL_setenv(name, value1, overwrite);
@@ -643,6 +643,36 @@ static int stdlib_getsetenv(void *arg)
643643
text);
644644
}
645645

646+
/* Verify setenv() with empty string vs unsetenv() */
647+
result = SDL_setenv("FOO", "1", 1);
648+
SDLTest_AssertPass("Call to SDL_setenv('FOO','1', 1)");
649+
SDLTest_AssertCheck(result == 0, "Check result, expected: 0, got: %i", result);
650+
expected = "1";
651+
text = SDL_getenv("FOO");
652+
SDLTest_AssertPass("Call to SDL_getenv('FOO')");
653+
SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Verify returned text, expected: %s, got: %s", expected, text);
654+
result = SDL_setenv("FOO", "", 1);
655+
SDLTest_AssertPass("Call to SDL_setenv('FOO','', 1)");
656+
SDLTest_AssertCheck(result == 0, "Check result, expected: 0, got: %i", result);
657+
expected = "";
658+
text = SDL_getenv("FOO");
659+
SDLTest_AssertPass("Call to SDL_getenv('FOO')");
660+
SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Verify returned text, expected: '%s', got: '%s'", expected, text);
661+
result = SDL_unsetenv("FOO");
662+
SDLTest_AssertPass("Call to SDL_unsetenv('FOO')");
663+
SDLTest_AssertCheck(result == 0, "Check result, expected: 0, got: %i", result);
664+
expected = NULL;
665+
text = SDL_getenv("FOO");
666+
SDLTest_AssertPass("Call to SDL_getenv('FOO')");
667+
SDLTest_AssertCheck(text == NULL, "Verify returned text, expected: %s, got: %s", expected, text);
668+
result = SDL_setenv("FOO", "0", 0);
669+
SDLTest_AssertPass("Call to SDL_setenv('FOO','0', 0)");
670+
SDLTest_AssertCheck(result == 0, "Check result, expected: 0, got: %i", result);
671+
expected = "0";
672+
text = SDL_getenv("FOO");
673+
SDLTest_AssertPass("Call to SDL_getenv('FOO')");
674+
SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Verify returned text, expected: %s, got: %s", expected, text);
675+
646676
/* Negative cases */
647677
for (overwrite = 0; overwrite <= 1; overwrite++) {
648678
result = SDL_setenv(NULL, value1, overwrite);
@@ -1026,35 +1056,35 @@ stdlib_overflow(void *arg)
10261056
/* ================= Test References ================== */
10271057

10281058
/* Standard C routine test cases */
1029-
static const SDLTest_TestCaseReference stdlibTest1 = {
1059+
static const SDLTest_TestCaseReference stdlibTest_strnlen = {
10301060
stdlib_strnlen, "stdlib_strnlen", "Call to SDL_strnlen", TEST_ENABLED
10311061
};
10321062

1033-
static const SDLTest_TestCaseReference stdlibTest2 = {
1063+
static const SDLTest_TestCaseReference stdlibTest_strlcpy = {
10341064
stdlib_strlcpy, "stdlib_strlcpy", "Call to SDL_strlcpy", TEST_ENABLED
10351065
};
10361066

1037-
static const SDLTest_TestCaseReference stdlibTest3 = {
1067+
static const SDLTest_TestCaseReference stdlibTest_strstr = {
10381068
stdlib_strstr, "stdlib_strstr", "Call to SDL_strstr", TEST_ENABLED
10391069
};
10401070

1041-
static const SDLTest_TestCaseReference stdlibTest4 = {
1071+
static const SDLTest_TestCaseReference stdlibTest_snprintf = {
10421072
stdlib_snprintf, "stdlib_snprintf", "Call to SDL_snprintf", TEST_ENABLED
10431073
};
10441074

1045-
static const SDLTest_TestCaseReference stdlibTest5 = {
1075+
static const SDLTest_TestCaseReference stdlibTest_swprintf = {
10461076
stdlib_swprintf, "stdlib_swprintf", "Call to SDL_swprintf", TEST_ENABLED
10471077
};
10481078

1049-
static const SDLTest_TestCaseReference stdlibTest6 = {
1079+
static const SDLTest_TestCaseReference stdlibTest_getsetenv = {
10501080
stdlib_getsetenv, "stdlib_getsetenv", "Call to SDL_getenv and SDL_setenv", TEST_ENABLED
10511081
};
10521082

1053-
static const SDLTest_TestCaseReference stdlibTest7 = {
1083+
static const SDLTest_TestCaseReference stdlibTest_sscanf = {
10541084
stdlib_sscanf, "stdlib_sscanf", "Call to SDL_sscanf", TEST_ENABLED
10551085
};
10561086

1057-
static const SDLTest_TestCaseReference stdlibTest8 = {
1087+
static const SDLTest_TestCaseReference stdlibTest_aligned_alloc = {
10581088
stdlib_aligned_alloc, "stdlib_aligned_alloc", "Call to SDL_aligned_alloc", TEST_ENABLED
10591089
};
10601090

@@ -1064,14 +1094,14 @@ static const SDLTest_TestCaseReference stdlibTestOverflow = {
10641094

10651095
/* Sequence of Standard C routine test cases */
10661096
static const SDLTest_TestCaseReference *stdlibTests[] = {
1067-
&stdlibTest1,
1068-
&stdlibTest2,
1069-
&stdlibTest3,
1070-
&stdlibTest4,
1071-
&stdlibTest5,
1072-
&stdlibTest6,
1073-
&stdlibTest7,
1074-
&stdlibTest8,
1097+
&stdlibTest_strnlen,
1098+
&stdlibTest_strlcpy,
1099+
&stdlibTest_strstr,
1100+
&stdlibTest_snprintf,
1101+
&stdlibTest_swprintf,
1102+
&stdlibTest_getsetenv,
1103+
&stdlibTest_sscanf,
1104+
&stdlibTest_aligned_alloc,
10751105
&stdlibTestOverflow,
10761106
NULL
10771107
};

0 commit comments

Comments
 (0)