SDL_getenv.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 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. #if defined(__clang_analyzer__) && !defined(SDL_DISABLE_ANALYZE_MACROS)
  19. #define SDL_DISABLE_ANALYZE_MACROS 1
  20. #endif
  21. #include "../SDL_internal.h"
  22. #if defined(__WIN32__) || defined(__WINGDK__)
  23. #include "../core/windows/SDL_windows.h"
  24. #endif
  25. #if defined(__ANDROID__)
  26. #include "../core/android/SDL_android.h"
  27. #endif
  28. #include "SDL_stdinc.h"
  29. #if (defined(__WIN32__) || defined(__WINGDK__)) && (!defined(HAVE_SETENV) || !defined(HAVE_GETENV))
  30. /* Note this isn't thread-safe! */
  31. static char *SDL_envmem = NULL; /* Ugh, memory leak */
  32. static size_t SDL_envmemlen = 0;
  33. #endif
  34. /* Put a variable into the environment */
  35. /* Note: Name may not contain a '=' character. (Reference: http://www.unix.com/man-page/Linux/3/setenv/) */
  36. #if defined(HAVE_SETENV)
  37. int SDL_setenv(const char *name, const char *value, int overwrite)
  38. {
  39. /* Input validation */
  40. if (!name || *name == '\0' || SDL_strchr(name, '=') != NULL || !value) {
  41. return -1;
  42. }
  43. return setenv(name, value, overwrite);
  44. }
  45. #elif defined(__WIN32__) || defined(__WINGDK__)
  46. int SDL_setenv(const char *name, const char *value, int overwrite)
  47. {
  48. /* Input validation */
  49. if (!name || *name == '\0' || SDL_strchr(name, '=') != NULL || !value) {
  50. return -1;
  51. }
  52. if (!overwrite) {
  53. if (GetEnvironmentVariableA(name, NULL, 0) > 0) {
  54. return 0; /* asked not to overwrite existing value. */
  55. }
  56. }
  57. if (!SetEnvironmentVariableA(name, *value ? value : NULL)) {
  58. return -1;
  59. }
  60. return 0;
  61. }
  62. /* We have a real environment table, but no real setenv? Fake it w/ putenv. */
  63. #elif (defined(HAVE_GETENV) && defined(HAVE_PUTENV) && !defined(HAVE_SETENV))
  64. int SDL_setenv(const char *name, const char *value, int overwrite)
  65. {
  66. size_t len;
  67. char *new_variable;
  68. /* Input validation */
  69. if (!name || *name == '\0' || SDL_strchr(name, '=') != NULL || !value) {
  70. return -1;
  71. }
  72. if (getenv(name) != NULL) {
  73. if (overwrite) {
  74. unsetenv(name);
  75. } else {
  76. return 0; /* leave the existing one there. */
  77. }
  78. }
  79. /* This leaks. Sorry. Get a better OS so we don't have to do this. */
  80. len = SDL_strlen(name) + SDL_strlen(value) + 2;
  81. new_variable = (char *)SDL_malloc(len);
  82. if (!new_variable) {
  83. return -1;
  84. }
  85. SDL_snprintf(new_variable, len, "%s=%s", name, value);
  86. return putenv(new_variable);
  87. }
  88. #else /* roll our own */
  89. static char **SDL_env = (char **)0;
  90. int SDL_setenv(const char *name, const char *value, int overwrite)
  91. {
  92. int added;
  93. size_t len, i;
  94. char **new_env;
  95. char *new_variable;
  96. /* Input validation */
  97. if (!name || *name == '\0' || SDL_strchr(name, '=') != NULL || !value) {
  98. return -1;
  99. }
  100. /* See if it already exists */
  101. if (!overwrite && SDL_getenv(name)) {
  102. return 0;
  103. }
  104. /* Allocate memory for the variable */
  105. len = SDL_strlen(name) + SDL_strlen(value) + 2;
  106. new_variable = (char *)SDL_malloc(len);
  107. if (!new_variable) {
  108. return -1;
  109. }
  110. SDL_snprintf(new_variable, len, "%s=%s", name, value);
  111. value = new_variable + SDL_strlen(name) + 1;
  112. name = new_variable;
  113. /* Actually put it into the environment */
  114. added = 0;
  115. i = 0;
  116. if (SDL_env) {
  117. /* Check to see if it's already there... */
  118. len = (value - name);
  119. for (; SDL_env[i]; ++i) {
  120. if (SDL_strncmp(SDL_env[i], name, len) == 0) {
  121. break;
  122. }
  123. }
  124. /* If we found it, just replace the entry */
  125. if (SDL_env[i]) {
  126. SDL_free(SDL_env[i]);
  127. SDL_env[i] = new_variable;
  128. added = 1;
  129. }
  130. }
  131. /* Didn't find it in the environment, expand and add */
  132. if (!added) {
  133. new_env = SDL_realloc(SDL_env, (i + 2) * sizeof(char *));
  134. if (new_env) {
  135. SDL_env = new_env;
  136. SDL_env[i++] = new_variable;
  137. SDL_env[i++] = (char *)0;
  138. added = 1;
  139. } else {
  140. SDL_free(new_variable);
  141. }
  142. }
  143. return added ? 0 : -1;
  144. }
  145. #endif
  146. /* Retrieve a variable named "name" from the environment */
  147. #if defined(HAVE_GETENV)
  148. char *SDL_getenv(const char *name)
  149. {
  150. #if defined(__ANDROID__)
  151. /* Make sure variables from the application manifest are available */
  152. Android_JNI_GetManifestEnvironmentVariables();
  153. #endif
  154. /* Input validation */
  155. if (!name || *name == '\0') {
  156. return NULL;
  157. }
  158. return getenv(name);
  159. }
  160. #elif defined(__WIN32__) || defined(__WINGDK__)
  161. char *SDL_getenv(const char *name)
  162. {
  163. size_t bufferlen;
  164. /* Input validation */
  165. if (!name || *name == '\0') {
  166. return NULL;
  167. }
  168. bufferlen =
  169. GetEnvironmentVariableA(name, SDL_envmem, (DWORD)SDL_envmemlen);
  170. if (bufferlen == 0) {
  171. return NULL;
  172. }
  173. if (bufferlen > SDL_envmemlen) {
  174. char *newmem = (char *)SDL_realloc(SDL_envmem, bufferlen);
  175. if (!newmem) {
  176. return NULL;
  177. }
  178. SDL_envmem = newmem;
  179. SDL_envmemlen = bufferlen;
  180. GetEnvironmentVariableA(name, SDL_envmem, (DWORD)SDL_envmemlen);
  181. }
  182. return SDL_envmem;
  183. }
  184. #else
  185. char *SDL_getenv(const char *name)
  186. {
  187. size_t len, i;
  188. char *value;
  189. /* Input validation */
  190. if (!name || *name == '\0') {
  191. return NULL;
  192. }
  193. value = (char *)0;
  194. if (SDL_env) {
  195. len = SDL_strlen(name);
  196. for (i = 0; SDL_env[i] && !value; ++i) {
  197. if ((SDL_strncmp(SDL_env[i], name, len) == 0) &&
  198. (SDL_env[i][len] == '=')) {
  199. value = &SDL_env[i][len + 1];
  200. }
  201. }
  202. }
  203. return value;
  204. }
  205. #endif
  206. #ifdef TEST_MAIN
  207. #include <stdio.h>
  208. int main(int argc, char *argv[])
  209. {
  210. char *value;
  211. printf("Checking for non-existent variable... ");
  212. fflush(stdout);
  213. if (!SDL_getenv("EXISTS")) {
  214. printf("okay\n");
  215. } else {
  216. printf("failed\n");
  217. }
  218. printf("Setting FIRST=VALUE1 in the environment... ");
  219. fflush(stdout);
  220. if (SDL_setenv("FIRST", "VALUE1", 0) == 0) {
  221. printf("okay\n");
  222. } else {
  223. printf("failed\n");
  224. }
  225. printf("Getting FIRST from the environment... ");
  226. fflush(stdout);
  227. value = SDL_getenv("FIRST");
  228. if (value && (SDL_strcmp(value, "VALUE1") == 0)) {
  229. printf("okay\n");
  230. } else {
  231. printf("failed\n");
  232. }
  233. printf("Setting SECOND=VALUE2 in the environment... ");
  234. fflush(stdout);
  235. if (SDL_setenv("SECOND", "VALUE2", 0) == 0) {
  236. printf("okay\n");
  237. } else {
  238. printf("failed\n");
  239. }
  240. printf("Getting SECOND from the environment... ");
  241. fflush(stdout);
  242. value = SDL_getenv("SECOND");
  243. if (value && (SDL_strcmp(value, "VALUE2") == 0)) {
  244. printf("okay\n");
  245. } else {
  246. printf("failed\n");
  247. }
  248. printf("Setting FIRST=NOVALUE in the environment... ");
  249. fflush(stdout);
  250. if (SDL_setenv("FIRST", "NOVALUE", 1) == 0) {
  251. printf("okay\n");
  252. } else {
  253. printf("failed\n");
  254. }
  255. printf("Getting FIRST from the environment... ");
  256. fflush(stdout);
  257. value = SDL_getenv("FIRST");
  258. if (value && (SDL_strcmp(value, "NOVALUE") == 0)) {
  259. printf("okay\n");
  260. } else {
  261. printf("failed\n");
  262. }
  263. printf("Checking for non-existent variable... ");
  264. fflush(stdout);
  265. if (!SDL_getenv("EXISTS")) {
  266. printf("okay\n");
  267. } else {
  268. printf("failed\n");
  269. }
  270. return 0;
  271. }
  272. #endif /* TEST_MAIN */
  273. /* vi: set ts=4 sw=4 expandtab: */