SDL_getenv.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #include "SDL_getenv_c.h"
  20. #include "../SDL_hashtable.h"
  21. #if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK)
  22. #include "../core/windows/SDL_windows.h"
  23. #endif
  24. #ifdef SDL_PLATFORM_ANDROID
  25. #include "../core/android/SDL_android.h"
  26. #endif
  27. #if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK)
  28. #define HAVE_WIN32_ENVIRONMENT
  29. #elif defined(HAVE_GETENV) && \
  30. (defined(HAVE_SETENV) || defined(HAVE_PUTENV)) && \
  31. (defined(HAVE_UNSETENV) || defined(HAVE_PUTENV))
  32. #define HAVE_LIBC_ENVIRONMENT
  33. #else
  34. #define HAVE_LOCAL_ENVIRONMENT
  35. #endif
  36. #if !defined(SDL_PLATFORM_WINDOWS)
  37. #if defined(SDL_PLATFORM_MACOS)
  38. #include <crt_externs.h>
  39. #define environ (*_NSGetEnviron())
  40. #elif defined(SDL_PLATFORM_FREEBSD)
  41. #include <dlfcn.h>
  42. #define environ ((char **)dlsym(RTLD_DEFAULT, "environ"))
  43. #else
  44. extern char **environ;
  45. #endif
  46. #endif // !SDL_PLATFORM_WINDOWS
  47. // Put a variable into the environment
  48. // Note: Name may not contain a '=' character. (Reference: http://www.unix.com/man-page/Linux/3/setenv/)
  49. #ifdef HAVE_LIBC_ENVIRONMENT
  50. #if defined(HAVE_SETENV)
  51. int SDL_setenv_unsafe(const char *name, const char *value, int overwrite)
  52. {
  53. // Input validation
  54. if (!name || *name == '\0' || SDL_strchr(name, '=') != NULL || !value) {
  55. return -1;
  56. }
  57. return setenv(name, value, overwrite);
  58. }
  59. // We have a real environment table, but no real setenv? Fake it w/ putenv.
  60. #else
  61. int SDL_setenv_unsafe(const char *name, const char *value, int overwrite)
  62. {
  63. char *new_variable;
  64. // Input validation
  65. if (!name || *name == '\0' || SDL_strchr(name, '=') != NULL || !value) {
  66. return -1;
  67. }
  68. if (getenv(name) != NULL) {
  69. if (!overwrite) {
  70. return 0; // leave the existing one there.
  71. }
  72. }
  73. // This leaks. Sorry. Get a better OS so we don't have to do this.
  74. SDL_asprintf(&new_variable, "%s=%s", name, value);
  75. if (!new_variable) {
  76. return -1;
  77. }
  78. return putenv(new_variable);
  79. }
  80. #endif
  81. #elif defined(HAVE_WIN32_ENVIRONMENT)
  82. int SDL_setenv_unsafe(const char *name, const char *value, int overwrite)
  83. {
  84. // Input validation
  85. if (!name || *name == '\0' || SDL_strchr(name, '=') != NULL || !value) {
  86. return -1;
  87. }
  88. if (!overwrite) {
  89. if (GetEnvironmentVariableA(name, NULL, 0) > 0) {
  90. return 0; // asked not to overwrite existing value.
  91. }
  92. }
  93. if (!SetEnvironmentVariableA(name, value)) {
  94. return -1;
  95. }
  96. return 0;
  97. }
  98. #else // roll our own
  99. // We'll leak this, as environment variables are intended to persist past SDL_Quit()
  100. static char **SDL_env;
  101. int SDL_setenv_unsafe(const char *name, const char *value, int overwrite)
  102. {
  103. int added;
  104. size_t len, i;
  105. char **new_env;
  106. char *new_variable;
  107. // Input validation
  108. if (!name || *name == '\0' || SDL_strchr(name, '=') != NULL || !value) {
  109. return -1;
  110. }
  111. // See if it already exists
  112. if (!overwrite && SDL_getenv_unsafe(name)) {
  113. return 0;
  114. }
  115. // Allocate memory for the variable
  116. len = SDL_strlen(name) + SDL_strlen(value) + 2;
  117. new_variable = (char *)SDL_malloc(len);
  118. if (!new_variable) {
  119. return -1;
  120. }
  121. SDL_snprintf(new_variable, len, "%s=%s", name, value);
  122. value = new_variable + SDL_strlen(name) + 1;
  123. name = new_variable;
  124. // Actually put it into the environment
  125. added = 0;
  126. i = 0;
  127. if (SDL_env) {
  128. // Check to see if it's already there...
  129. len = (value - name);
  130. for (; SDL_env[i]; ++i) {
  131. if (SDL_strncmp(SDL_env[i], name, len) == 0) {
  132. // If we found it, just replace the entry
  133. SDL_free(SDL_env[i]);
  134. SDL_env[i] = new_variable;
  135. added = 1;
  136. break;
  137. }
  138. }
  139. }
  140. // Didn't find it in the environment, expand and add
  141. if (!added) {
  142. new_env = SDL_realloc(SDL_env, (i + 2) * sizeof(char *));
  143. if (new_env) {
  144. SDL_env = new_env;
  145. SDL_env[i++] = new_variable;
  146. SDL_env[i++] = (char *)0;
  147. added = 1;
  148. } else {
  149. SDL_free(new_variable);
  150. }
  151. }
  152. return added ? 0 : -1;
  153. }
  154. #endif // HAVE_LIBC_ENVIRONMENT
  155. #ifdef HAVE_LIBC_ENVIRONMENT
  156. #if defined(HAVE_UNSETENV)
  157. int SDL_unsetenv_unsafe(const char *name)
  158. {
  159. // Input validation
  160. if (!name || *name == '\0' || SDL_strchr(name, '=') != NULL) {
  161. return -1;
  162. }
  163. return unsetenv(name);
  164. }
  165. // We have a real environment table, but no unsetenv? Fake it w/ putenv.
  166. #else
  167. int SDL_unsetenv_unsafe(const char *name)
  168. {
  169. // Input validation
  170. if (!name || *name == '\0' || SDL_strchr(name, '=') != NULL) {
  171. return -1;
  172. }
  173. // Hope this environment uses the non-standard extension of removing the environment variable if it has no '='
  174. return putenv(name);
  175. }
  176. #endif
  177. #elif defined(HAVE_WIN32_ENVIRONMENT)
  178. int SDL_unsetenv_unsafe(const char *name)
  179. {
  180. // Input validation
  181. if (!name || *name == '\0' || SDL_strchr(name, '=') != NULL) {
  182. return -1;
  183. }
  184. if (!SetEnvironmentVariableA(name, NULL)) {
  185. return -1;
  186. }
  187. return 0;
  188. }
  189. #else
  190. int SDL_unsetenv_unsafe(const char *name)
  191. {
  192. size_t len, i;
  193. // Input validation
  194. if (!name || *name == '\0' || SDL_strchr(name, '=') != NULL) {
  195. return -1;
  196. }
  197. if (SDL_env) {
  198. len = SDL_strlen(name);
  199. for (i = 0; SDL_env[i]; ++i) {
  200. if ((SDL_strncmp(SDL_env[i], name, len) == 0) &&
  201. (SDL_env[i][len] == '=')) {
  202. // Just clear out this entry for now
  203. *SDL_env[i] = '\0';
  204. break;
  205. }
  206. }
  207. }
  208. return 0;
  209. }
  210. #endif // HAVE_LIBC_ENVIRONMENT
  211. // Retrieve a variable named "name" from the environment
  212. #ifdef HAVE_LIBC_ENVIRONMENT
  213. const char *SDL_getenv_unsafe(const char *name)
  214. {
  215. #ifdef SDL_PLATFORM_ANDROID
  216. // Make sure variables from the application manifest are available
  217. Android_JNI_GetManifestEnvironmentVariables();
  218. #endif
  219. // Input validation
  220. if (!name || *name == '\0') {
  221. return NULL;
  222. }
  223. return getenv(name);
  224. }
  225. #elif defined(HAVE_WIN32_ENVIRONMENT)
  226. const char *SDL_getenv_unsafe(const char *name)
  227. {
  228. DWORD length, maxlen = 0;
  229. char *string = NULL;
  230. const char *result = NULL;
  231. // Input validation
  232. if (!name || *name == '\0') {
  233. return NULL;
  234. }
  235. for ( ; ; ) {
  236. SetLastError(ERROR_SUCCESS);
  237. length = GetEnvironmentVariableA(name, string, maxlen);
  238. if (length > maxlen) {
  239. char *temp = (char *)SDL_realloc(string, length);
  240. if (!temp) {
  241. return NULL;
  242. }
  243. string = temp;
  244. maxlen = length;
  245. } else {
  246. if (GetLastError() != ERROR_SUCCESS) {
  247. if (string) {
  248. SDL_free(string);
  249. }
  250. return NULL;
  251. }
  252. break;
  253. }
  254. }
  255. if (string) {
  256. result = SDL_GetPersistentString(string);
  257. SDL_free(string);
  258. }
  259. return result;
  260. }
  261. #else
  262. const char *SDL_getenv_unsafe(const char *name)
  263. {
  264. size_t len, i;
  265. char *value;
  266. // Input validation
  267. if (!name || *name == '\0') {
  268. return NULL;
  269. }
  270. value = (char *)0;
  271. if (SDL_env) {
  272. len = SDL_strlen(name);
  273. for (i = 0; SDL_env[i]; ++i) {
  274. if ((SDL_strncmp(SDL_env[i], name, len) == 0) &&
  275. (SDL_env[i][len] == '=')) {
  276. value = &SDL_env[i][len + 1];
  277. break;
  278. }
  279. }
  280. }
  281. return value;
  282. }
  283. #endif // HAVE_LIBC_ENVIRONMENT
  284. struct SDL_Environment
  285. {
  286. SDL_Mutex *lock;
  287. SDL_HashTable *strings;
  288. };
  289. static SDL_Environment *SDL_environment;
  290. SDL_Environment *SDL_GetEnvironment(void)
  291. {
  292. if (!SDL_environment) {
  293. SDL_environment = SDL_CreateEnvironment(false);
  294. }
  295. return SDL_environment;
  296. }
  297. void SDL_CleanupEnvironment(void)
  298. {
  299. SDL_Environment *env = SDL_environment;
  300. if (env) {
  301. SDL_environment = NULL;
  302. SDL_DestroyEnvironment(env);
  303. }
  304. }
  305. SDL_Environment *SDL_CreateEnvironment(SDL_bool empty)
  306. {
  307. SDL_Environment *env = SDL_calloc(1, sizeof(*env));
  308. if (!env) {
  309. return NULL;
  310. }
  311. env->strings = SDL_CreateHashTable(NULL, 16, SDL_HashString, SDL_KeyMatchString, SDL_NukeFreeKey, false);
  312. if (!env->strings) {
  313. SDL_free(env);
  314. return NULL;
  315. }
  316. // Don't fail if we can't create a mutex (e.g. on a single-thread environment)
  317. env->lock = SDL_CreateMutex();
  318. if (!empty) {
  319. #ifdef SDL_PLATFORM_WINDOWS
  320. LPWCH strings = GetEnvironmentStringsW();
  321. if (strings) {
  322. for (LPWCH string = strings; *string; string += SDL_wcslen(string) + 1) {
  323. char *variable = WIN_StringToUTF8W(string);
  324. if (!variable) {
  325. continue;
  326. }
  327. char *value = SDL_strchr(variable, '=');
  328. if (!value || value == variable) {
  329. SDL_free(variable);
  330. continue;
  331. }
  332. *value++ = '\0';
  333. SDL_InsertIntoHashTable(env->strings, variable, value);
  334. }
  335. FreeEnvironmentStringsW(strings);
  336. }
  337. #else
  338. #ifdef SDL_PLATFORM_ANDROID
  339. // Make sure variables from the application manifest are available
  340. Android_JNI_GetManifestEnvironmentVariables();
  341. #endif
  342. char **strings = environ;
  343. for (int i = 0; strings[i]; ++i) {
  344. char *variable = SDL_strdup(strings[i]);
  345. if (!variable) {
  346. continue;
  347. }
  348. char *value = SDL_strchr(variable, '=');
  349. if (!value || value == variable) {
  350. SDL_free(variable);
  351. continue;
  352. }
  353. *value++ = '\0';
  354. SDL_InsertIntoHashTable(env->strings, variable, value);
  355. }
  356. #endif // SDL_PLATFORM_WINDOWS
  357. }
  358. return env;
  359. }
  360. const char *SDL_GetEnvironmentVariable(SDL_Environment *env, const char *name)
  361. {
  362. const char *result = NULL;
  363. if (!env) {
  364. return NULL;
  365. } else if (!name || *name == '\0') {
  366. return NULL;
  367. }
  368. SDL_LockMutex(env->lock);
  369. {
  370. const char *value;
  371. if (SDL_FindInHashTable(env->strings, name, (const void **)&value)) {
  372. result = SDL_GetPersistentString(value);
  373. }
  374. }
  375. SDL_UnlockMutex(env->lock);
  376. return result;
  377. }
  378. char **SDL_GetEnvironmentVariables(SDL_Environment *env)
  379. {
  380. char **result = NULL;
  381. if (!env) {
  382. SDL_InvalidParamError("env");
  383. return NULL;
  384. }
  385. SDL_LockMutex(env->lock);
  386. {
  387. size_t count, length = 0;
  388. void *iter;
  389. const char *key, *value;
  390. // First pass, get the size we need for all the strings
  391. count = 0;
  392. iter = NULL;
  393. while (SDL_IterateHashTable(env->strings, (const void **)&key, (const void **)&value, &iter)) {
  394. length += SDL_strlen(key) + 1 + SDL_strlen(value) + 1;
  395. ++count;
  396. }
  397. // Allocate memory for the strings
  398. result = (char **)SDL_malloc((count + 1) * sizeof(*result) + length);
  399. char *string = (char *)(result + count + 1);
  400. // Second pass, copy the strings
  401. count = 0;
  402. iter = NULL;
  403. while (SDL_IterateHashTable(env->strings, (const void **)&key, (const void **)&value, &iter)) {
  404. size_t len;
  405. result[count] = string;
  406. len = SDL_strlen(key);
  407. SDL_memcpy(string, key, len);
  408. string += len;
  409. *string++ = '=';
  410. len = SDL_strlen(value);
  411. SDL_memcpy(string, value, len);
  412. string += len;
  413. *string++ = '\0';
  414. ++count;
  415. }
  416. result[count] = NULL;
  417. }
  418. SDL_UnlockMutex(env->lock);
  419. return result;
  420. }
  421. SDL_bool SDL_SetEnvironmentVariable(SDL_Environment *env, const char *name, const char *value, SDL_bool overwrite)
  422. {
  423. bool result = false;
  424. if (!env) {
  425. return SDL_InvalidParamError("env");
  426. } else if (!name || *name == '\0' || SDL_strchr(name, '=') != NULL) {
  427. return SDL_InvalidParamError("name");
  428. } else if (!value) {
  429. return SDL_InvalidParamError("value");
  430. }
  431. SDL_LockMutex(env->lock);
  432. {
  433. const void *existing_value;
  434. bool insert = true;
  435. if (SDL_FindInHashTable(env->strings, name, &existing_value)) {
  436. if (!overwrite) {
  437. result = true;
  438. insert = false;
  439. } else {
  440. SDL_RemoveFromHashTable(env->strings, name);
  441. }
  442. }
  443. if (insert) {
  444. char *string = NULL;
  445. if (SDL_asprintf(&string, "%s=%s", name, value) > 0) {
  446. size_t len = SDL_strlen(name);
  447. string[len] = '\0';
  448. name = string;
  449. value = string + len + 1;
  450. result = SDL_InsertIntoHashTable(env->strings, name, value);
  451. }
  452. }
  453. }
  454. SDL_UnlockMutex(env->lock);
  455. return result;
  456. }
  457. SDL_bool SDL_UnsetEnvironmentVariable(SDL_Environment *env, const char *name)
  458. {
  459. bool result = false;
  460. if (!env) {
  461. return SDL_InvalidParamError("env");
  462. } else if (!name || *name == '\0' || SDL_strchr(name, '=') != NULL) {
  463. return SDL_InvalidParamError("name");
  464. }
  465. SDL_LockMutex(env->lock);
  466. {
  467. const void *value;
  468. if (SDL_FindInHashTable(env->strings, name, &value)) {
  469. result = SDL_RemoveFromHashTable(env->strings, name);
  470. } else {
  471. result = true;
  472. }
  473. }
  474. SDL_UnlockMutex(env->lock);
  475. return result;
  476. }
  477. void SDL_DestroyEnvironment(SDL_Environment *env)
  478. {
  479. if (!env || env == SDL_environment) {
  480. return;
  481. }
  482. SDL_DestroyMutex(env->lock);
  483. SDL_DestroyHashTable(env->strings);
  484. SDL_free(env);
  485. }