SDL_getenv.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2014 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. #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. /* Note: Name may not contain a '=' character. (Reference: http://www.unix.com/man-page/Linux/3/setenv/) */
  30. #if defined(HAVE_SETENV)
  31. int
  32. SDL_setenv(const char *name, const char *value, int overwrite)
  33. {
  34. /* Input validation */
  35. if (!name || SDL_strlen(name) == 0 || SDL_strchr(name, '=') != NULL || !value) {
  36. return (-1);
  37. }
  38. return setenv(name, value, overwrite);
  39. }
  40. #elif defined(__WIN32__)
  41. int
  42. SDL_setenv(const char *name, const char *value, int overwrite)
  43. {
  44. /* Input validation */
  45. if (!name || SDL_strlen(name) == 0 || SDL_strchr(name, '=') != NULL || !value) {
  46. return (-1);
  47. }
  48. if (!overwrite) {
  49. char ch = 0;
  50. const size_t len = GetEnvironmentVariableA(name, &ch, sizeof (ch));
  51. if (len > 0) {
  52. return 0; /* asked not to overwrite existing value. */
  53. }
  54. }
  55. if (!SetEnvironmentVariableA(name, *value ? value : NULL)) {
  56. return -1;
  57. }
  58. return 0;
  59. }
  60. /* We have a real environment table, but no real setenv? Fake it w/ putenv. */
  61. #elif (defined(HAVE_GETENV) && defined(HAVE_PUTENV) && !defined(HAVE_SETENV))
  62. int
  63. SDL_setenv(const char *name, const char *value, int overwrite)
  64. {
  65. size_t len;
  66. char *new_variable;
  67. /* Input validation */
  68. if (!name || SDL_strlen(name) == 0 || SDL_strchr(name, '=') != NULL || !value) {
  69. return (-1);
  70. }
  71. if (getenv(name) != NULL) {
  72. if (overwrite) {
  73. unsetenv(name);
  74. } else {
  75. return 0; /* leave the existing one there. */
  76. }
  77. }
  78. /* This leaks. Sorry. Get a better OS so we don't have to do this. */
  79. len = SDL_strlen(name) + SDL_strlen(value) + 2;
  80. new_variable = (char *) SDL_malloc(len);
  81. if (!new_variable) {
  82. return (-1);
  83. }
  84. SDL_snprintf(new_variable, len, "%s=%s", name, value);
  85. return putenv(new_variable);
  86. }
  87. #else /* roll our own */
  88. static char **SDL_env = (char **) 0;
  89. int
  90. SDL_setenv(const char *name, const char *value, int overwrite)
  91. {
  92. int added;
  93. int len, i;
  94. char **new_env;
  95. char *new_variable;
  96. /* Input validation */
  97. if (!name || SDL_strlen(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 *
  149. SDL_getenv(const char *name)
  150. {
  151. /* Input validation */
  152. if (!name || SDL_strlen(name)==0) {
  153. return NULL;
  154. }
  155. return getenv(name);
  156. }
  157. #elif defined(__WIN32__)
  158. char *
  159. SDL_getenv(const char *name)
  160. {
  161. size_t bufferlen;
  162. /* Input validation */
  163. if (!name || SDL_strlen(name)==0) {
  164. return NULL;
  165. }
  166. bufferlen =
  167. GetEnvironmentVariableA(name, SDL_envmem, (DWORD) SDL_envmemlen);
  168. if (bufferlen == 0) {
  169. return NULL;
  170. }
  171. if (bufferlen > SDL_envmemlen) {
  172. char *newmem = (char *) SDL_realloc(SDL_envmem, bufferlen);
  173. if (newmem == NULL) {
  174. return NULL;
  175. }
  176. SDL_envmem = newmem;
  177. SDL_envmemlen = bufferlen;
  178. GetEnvironmentVariableA(name, SDL_envmem, (DWORD) SDL_envmemlen);
  179. }
  180. return SDL_envmem;
  181. }
  182. #else
  183. char *
  184. SDL_getenv(const char *name)
  185. {
  186. int len, i;
  187. char *value;
  188. /* Input validation */
  189. if (!name || SDL_strlen(name)==0) {
  190. return NULL;
  191. }
  192. value = (char *) 0;
  193. if (SDL_env) {
  194. len = SDL_strlen(name);
  195. for (i = 0; SDL_env[i] && !value; ++i) {
  196. if ((SDL_strncmp(SDL_env[i], name, len) == 0) &&
  197. (SDL_env[i][len] == '=')) {
  198. value = &SDL_env[i][len + 1];
  199. }
  200. }
  201. }
  202. return value;
  203. }
  204. #endif
  205. #ifdef TEST_MAIN
  206. #include <stdio.h>
  207. int
  208. 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: */