zenilib  0.5.3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
SDL_getenv.c
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any damages
7  arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any purpose,
10  including commercial applications, and to alter it and redistribute it
11  freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you must not
14  claim that you wrote the original software. If you use this software
15  in a product, an acknowledgment in the product documentation would be
16  appreciated but is not required.
17  2. Altered source versions must be plainly marked as such, and must not be
18  misrepresented as being the original software.
19  3. This notice may not be removed or altered from any source distribution.
20 */
21 #include "SDL_config.h"
22 
23 #include "SDL_stdinc.h"
24 
25 #if !defined(SDL_setenv) && defined(__WIN32__)
26 #include "../core/windows/SDL_windows.h"
27 /* Note this isn't thread-safe! */
28 static char *SDL_envmem = NULL; /* Ugh, memory leak */
29 static size_t SDL_envmemlen = 0;
30 #endif
31 
32 
33 /* Put a variable into the environment */
34 #if defined(HAVE_SETENV)
35 int
36 SDL_setenv(const char *name, const char *value, int overwrite)
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  if (!overwrite) {
45  char ch = 0;
46  const size_t len = GetEnvironmentVariableA(name, &ch, sizeof (ch));
47  if (len > 0) {
48  return 0; /* asked not to overwrite existing value. */
49  }
50  }
51  if (!SetEnvironmentVariableA(name, *value ? value : NULL)) {
52  return -1;
53  }
54  return 0;
55 }
56 /* We have a real environment table, but no real setenv? Fake it w/ putenv. */
57 #elif (defined(HAVE_GETENV) && defined(HAVE_PUTENV) && !defined(HAVE_SETENV))
58 int
59 SDL_setenv(const char *name, const char *value, int overwrite)
60 {
61  size_t len;
62  char *new_variable;
63 
64  if (getenv(name) != NULL) {
65  if (overwrite) {
66  unsetenv(name);
67  } else {
68  return 0; /* leave the existing one there. */
69  }
70  }
71 
72  /* This leaks. Sorry. Get a better OS so we don't have to do this. */
73  len = SDL_strlen(name) + SDL_strlen(value) + 2;
74  new_variable = (char *) SDL_malloc(len);
75  if (!new_variable) {
76  return (-1);
77  }
78 
79  SDL_snprintf(new_variable, len, "%s=%s", name, value);
80  return putenv(new_variable);
81 }
82 #else /* roll our own */
83 static char **SDL_env = (char **) 0;
84 int
85 SDL_setenv(const char *name, const char *value, int overwrite)
86 {
87  int added;
88  int len, i;
89  char **new_env;
90  char *new_variable;
91 
92  /* A little error checking */
93  if (!name || !value) {
94  return (-1);
95  }
96 
97  /* See if it already exists */
98  if (!overwrite && SDL_getenv(name)) {
99  return 0;
100  }
101 
102  /* Allocate memory for the variable */
103  len = SDL_strlen(name) + SDL_strlen(value) + 2;
104  new_variable = (char *) SDL_malloc(len);
105  if (!new_variable) {
106  return (-1);
107  }
108 
109  SDL_snprintf(new_variable, len, "%s=%s", name, value);
110  value = new_variable + SDL_strlen(name) + 1;
111  name = new_variable;
112 
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 
132  /* Didn't find it in the environment, expand and add */
133  if (!added) {
134  new_env = SDL_realloc(SDL_env, (i + 2) * sizeof(char *));
135  if (new_env) {
136  SDL_env = new_env;
137  SDL_env[i++] = new_variable;
138  SDL_env[i++] = (char *) 0;
139  added = 1;
140  } else {
141  SDL_free(new_variable);
142  }
143  }
144  return (added ? 0 : -1);
145 }
146 #endif
147 
148 /* Retrieve a variable named "name" from the environment */
149 #if defined(HAVE_GETENV)
150 char *
151 SDL_getenv(const char *name)
152 {
153  return getenv(name);
154 }
155 #elif defined(__WIN32__)
156 char *
157 SDL_getenv(const char *name)
158 {
159  size_t bufferlen;
160 
161  bufferlen =
162  GetEnvironmentVariableA(name, SDL_envmem, (DWORD) SDL_envmemlen);
163  if (bufferlen == 0) {
164  return NULL;
165  }
166  if (bufferlen > SDL_envmemlen) {
167  char *newmem = (char *) SDL_realloc(SDL_envmem, bufferlen);
168  if (newmem == NULL) {
169  return NULL;
170  }
171  SDL_envmem = newmem;
172  SDL_envmemlen = bufferlen;
173  GetEnvironmentVariableA(name, SDL_envmem, (DWORD) SDL_envmemlen);
174  }
175  return SDL_envmem;
176 }
177 #else
178 char *
179 SDL_getenv(const char *name)
180 {
181  int len, i;
182  char *value;
183 
184  value = (char *) 0;
185  if (SDL_env) {
186  len = SDL_strlen(name);
187  for (i = 0; SDL_env[i] && !value; ++i) {
188  if ((SDL_strncmp(SDL_env[i], name, len) == 0) &&
189  (SDL_env[i][len] == '=')) {
190  value = &SDL_env[i][len + 1];
191  }
192  }
193  }
194  return value;
195 }
196 #endif
197 
198 
199 #ifdef TEST_MAIN
200 #include <stdio.h>
201 
202 int
203 main(int argc, char *argv[])
204 {
205  char *value;
206 
207  printf("Checking for non-existent variable... ");
208  fflush(stdout);
209  if (!SDL_getenv("EXISTS")) {
210  printf("okay\n");
211  } else {
212  printf("failed\n");
213  }
214  printf("Setting FIRST=VALUE1 in the environment... ");
215  fflush(stdout);
216  if (SDL_setenv("FIRST", "VALUE1", 0) == 0) {
217  printf("okay\n");
218  } else {
219  printf("failed\n");
220  }
221  printf("Getting FIRST from the environment... ");
222  fflush(stdout);
223  value = SDL_getenv("FIRST");
224  if (value && (SDL_strcmp(value, "VALUE1") == 0)) {
225  printf("okay\n");
226  } else {
227  printf("failed\n");
228  }
229  printf("Setting SECOND=VALUE2 in the environment... ");
230  fflush(stdout);
231  if (SDL_setenv("SECOND", "VALUE2", 0) == 0) {
232  printf("okay\n");
233  } else {
234  printf("failed\n");
235  }
236  printf("Getting SECOND from the environment... ");
237  fflush(stdout);
238  value = SDL_getenv("SECOND");
239  if (value && (SDL_strcmp(value, "VALUE2") == 0)) {
240  printf("okay\n");
241  } else {
242  printf("failed\n");
243  }
244  printf("Setting FIRST=NOVALUE in the environment... ");
245  fflush(stdout);
246  if (SDL_setenv("FIRST", "NOVALUE", 1) == 0) {
247  printf("okay\n");
248  } else {
249  printf("failed\n");
250  }
251  printf("Getting FIRST from the environment... ");
252  fflush(stdout);
253  value = SDL_getenv("FIRST");
254  if (value && (SDL_strcmp(value, "NOVALUE") == 0)) {
255  printf("okay\n");
256  } else {
257  printf("failed\n");
258  }
259  printf("Checking for non-existent variable... ");
260  fflush(stdout);
261  if (!SDL_getenv("EXISTS")) {
262  printf("okay\n");
263  } else {
264  printf("failed\n");
265  }
266  return (0);
267 }
268 #endif /* TEST_MAIN */
269 
270 /* vi: set ts=4 sw=4 expandtab: */
int main(int argc, char **argv)
Definition: bootstrap.cpp:102
#define NULL
Definition: ftobjs.h:61
DECLSPEC int SDLCALL SDL_snprintf(char *text, size_t maxlen, const char *fmt,...)
Definition: SDL_string.c:1277
DECLSPEC void *SDLCALL SDL_realloc(void *mem, size_t size)
DECLSPEC void SDLCALL SDL_free(void *mem)
EGLImageKHR EGLint * name
Definition: eglext.h:284
GLenum GLsizei len
Definition: glew.h:7035
static char ** SDL_env
Definition: SDL_getenv.c:83
DECLSPEC void *SDLCALL SDL_malloc(size_t size)
DECLSPEC int SDLCALL SDL_strcmp(const char *str1, const char *str2)
Definition: SDL_string.c:910
DECLSPEC int SDLCALL SDL_strncmp(const char *str1, const char *str2, size_t maxlen)
Definition: SDL_string.c:926
DECLSPEC size_t SDLCALL SDL_strlen(const char *str)
Definition: SDL_string.c:389
EGLSurface EGLint void ** value
Definition: eglext.h:301
typedef DWORD(WINAPI *XInputGetState_t)(DWORD dwUserIndex
DECLSPEC char *SDLCALL SDL_getenv(const char *name)
Definition: SDL_getenv.c:179
int i
Definition: pngrutil.c:1377
DECLSPEC int SDLCALL SDL_setenv(const char *name, const char *value, int overwrite)
Definition: SDL_getenv.c:85