SDL_getenv.c 15 KB

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