SDL_getenv.c 15 KB

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