SDL_getenv.c 7.1 KB

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