Remove environment variables with plat_setenv(name, NULL) and set them through the C runtime on Windows, so getenv sees every change and --probe-cfg passes on Linux.

This commit is contained in:
Wellington Dias committed 2026-10-31 02:02:27 -03:00
1 parent efe1b5db54
commit a4c99a47b0
3 files changed
+18 -16

No files matched your search

+14 -14
View File
@@ -35,8 +35,8 @@ int probe_cfg(void) {
"no_such_setting = 1\r\n"
"no equals sign\r\n"
"last = no newline at the end";
plat_setenv("TOTK_LOCALE", ""); // an empty variable is how _putenv_s spells "remove" on Windows
plat_setenv("TOTK_FPS60", "");
plat_setenv("TOTK_LOCALE", NULL);
plat_setenv("TOTK_FPS60", NULL);
CHECK(plat_file_write(path, ini, sizeof ini - 1), "write the probe ini");
CHECK(cfg_load(path) == 1, "load the probe ini");
CHECK(EQ(cfg_get("TOTK_FPS60"), "0"), "value read, whitespace trimmed, lower-case key");
@@ -50,7 +50,7 @@ int probe_cfg(void) {
CHECK(EQ(cfg_get("TOTK_NO_SUCH_SETTING"), "1"), "unknown key is still readable");
plat_setenv("TOTK_FPS60", "1");
CHECK(EQ(cfg_get("TOTK_FPS60"), "1"), "environment overrides the file");
plat_setenv("TOTK_FPS60", "");
plat_setenv("TOTK_FPS60", NULL);
CHECK(EQ(cfg_get("TOTK_FPS60"), "0"), "file value returns when the variable is removed");
plat_file_delete(path);
CHECK(cfg_load(path) == 0, "missing file is not an error");
@@ -90,9 +90,9 @@ int probe_cfg(void) {
"\r\n"
"[audio]\r\n"
"volume = 40\r\n";
plat_setenv("TOTK_MOTION", "");
plat_setenv("TOTK_VOLUME", "");
plat_setenv("TOTK_RUMBLE_STRENGTH", "");
plat_setenv("TOTK_MOTION", NULL);
plat_setenv("TOTK_VOLUME", NULL);
plat_setenv("TOTK_RUMBLE_STRENGTH", NULL);
CHECK(plat_file_write(path, before, sizeof before - 1), "write the save ini");
CHECK(cfg_load(path) == 1, "load the save ini");
unsigned v0 = cfg_version();
@@ -121,7 +121,7 @@ int probe_cfg(void) {
cfg_set("TOTK_VOLUME", "90");
CHECK(cfg_from_env("TOTK_VOLUME") && EQ(cfg_get("TOTK_VOLUME"), "70"), "environment wins over cfg_set");
CHECK(EQ(held, "40"), "a pointer cfg_get handed out stays valid after cfg_set");
plat_setenv("TOTK_VOLUME", "");
plat_setenv("TOTK_VOLUME", NULL);
CHECK(!cfg_from_env("TOTK_VOLUME") && EQ(cfg_get("TOTK_VOLUME"), "90"), "cfg_set value returns without the variable");
plat_file_delete(path);
CHECK(cfg_save_to(path) == 1, "cfg_save_to creates a missing file");
@@ -147,7 +147,7 @@ int probe_cfg(void) {
// cfg_reset: the key's active lines leave totk.ini, the commented example stays
static const char rbase[] = "[audio]\n# volume = 100\n[input]\nmotion = 0\n";
static const char *const vol[] = {"VOLUME"};
plat_setenv("TOTK_VOLUME", "");
plat_setenv("TOTK_VOLUME", NULL);
CHECK(plat_file_write(path, rbase, sizeof rbase - 1), "write the reset ini");
CHECK(cfg_load(path) == 1, "load the reset ini");
cfg_set("TOTK_VOLUME", "40");
@@ -188,7 +188,7 @@ int probe_cfg(void) {
CHECK(cfg_load(path) == 1 && EQ(cfg_get("TOTK_VOLUME"), "60"), "reset: reload");
plat_setenv("TOTK_VOLUME", "70");
CHECK(cfg_reset(vol, 1) == 0, "reset: an environment-set key is skipped");
plat_setenv("TOTK_VOLUME", "");
plat_setenv("TOTK_VOLUME", NULL);
CHECK(EQ(cfg_get("TOTK_VOLUME"), "60"), "reset: the skipped key keeps its value");
CHECK(cfg_save_to(path) == 1, "reset: save after a skip");
got = plat_file_read(path, &got_n);
@@ -331,16 +331,16 @@ int probe_cfg(void) {
CHECK(!strcmp(line, "none"), "edit: a stick direction without keys is none");
// defaults live in the CfgMeta table: cfg_value and the typed accessors fall back to it
cfg_load("cfg-probe-missing.ini");
plat_setenv("TOTK_FPS60", "");
plat_setenv("TOTK_MOUSE_SENS", "");
plat_setenv("TOTK_FREE_CORES", "");
plat_setenv("TOTK_FPS60", NULL);
plat_setenv("TOTK_MOUSE_SENS", NULL);
plat_setenv("TOTK_FREE_CORES", NULL);
CHECK(cfg_bool("TOTK_FPS60") == 1, "default: unset FPS60 is on");
CHECK(EQ(cfg_value("TOTK_VK_PRESENT"), "mailbox") && cfg_int("TOTK_SPLIT_DRAWS") == 64, "default: text and integer");
plat_setenv("TOTK_MOUSE_SENS", "2.5");
CHECK(cfg_double("TOTK_MOUSE_SENS") == 2.5, "double: the environment value");
plat_setenv("TOTK_MOUSE_SENS", "fast");
CHECK(cfg_double("TOTK_MOUSE_SENS") == 1.0, "double: a value that does not parse gives the default");
plat_setenv("TOTK_MOUSE_SENS", "");
plat_setenv("TOTK_MOUSE_SENS", NULL);
cfg_set("TOTK_MOUSE_SENS", "3");
CHECK(cfg_double("TOTK_MOUSE_SENS") == 3.0 && cfg_int("TOTK_MOUSE_SENS") == 3, "a cfg_set value beats the default");
CHECK(cfg_bool("TOTK_FREE_CORES") == 0, "free_cores unset: off");
@@ -348,7 +348,7 @@ int probe_cfg(void) {
CHECK(cfg_bool("TOTK_FREE_CORES") == 0, "free_cores 0 gives the same as unset");
plat_setenv("TOTK_FREE_CORES", "1");
CHECK(cfg_bool("TOTK_FREE_CORES") == 1, "free_cores 1: on");
plat_setenv("TOTK_FREE_CORES", "");
plat_setenv("TOTK_FREE_CORES", NULL);
cfg_load("cfg-probe-missing.ini");
int listed = 0;
for (int i = 0; cfg_meta_at(i); i++) {
+1 -1
View File
@@ -241,7 +241,7 @@ static char *lang(void) {
return s;
}
static void set_env(const char *name, const char *value) { plat_setenv(name, value ? value : ""); }
static void set_env(const char *name, const char *value) { plat_setenv(name, value); } // NULL removes it
static void probe_oe(void) {
char *keep_lang = cfg_get("TOTK_LANG") ? strdup(cfg_get("TOTK_LANG")) : NULL;
+3 -1
View File
@@ -621,7 +621,9 @@ void plat_exit(int code) {
ExitProcess((UINT)code);
}
int plat_setenv(const char *name, const char *value) { return SetEnvironmentVariableA(name, value) != 0; }
// _putenv_s, not SetEnvironmentVariableA: getenv reads the C runtime's copy of the environment, which only the CRT's
// own calls update. An empty value removes the variable.
int plat_setenv(const char *name, const char *value) { return _putenv_s(name, value ? value : "") == 0; }
uint32_t plat_process_id(void) { return GetCurrentProcessId(); }