comparison src/SDL_getenv.c @ 1268:f098b247299d

Use Win32 API for putenv/getenv to avoid C runtime conflicts
author Sam Lantinga <slouken@libsdl.org>
date Thu, 26 Jan 2006 06:06:56 +0000
parents b8d311d90021
children 05d5d36b71f4
comparison
equal deleted inserted replaced
1267:fdc7ef6ecab4 1268:f098b247299d
32 #endif 32 #endif
33 33
34 #include "SDL_getenv.h" 34 #include "SDL_getenv.h"
35 35
36 #ifdef NEED_SDL_GETENV 36 #ifdef NEED_SDL_GETENV
37
38 #ifdef WIN32
39
40 #define WIN32_LEAN_AND_MEAN
41 #include <windows.h>
42 #include <malloc.h>
43 #include <string.h>
44
45 /* Note this isn't thread-safe! */
46
47 static char *SDL_envmem = NULL; /* Ugh, memory leak */
48 static DWORD SDL_envmemlen = 0;
49
50 /* Put a variable of the form "name=value" into the environment */
51 int SDL_putenv(const char *variable)
52 {
53 DWORD bufferlen;
54 char *value;
55 const char *sep;
56
57 sep = strchr(variable, '=');
58 if ( sep == NULL ) {
59 return -1;
60 }
61 bufferlen = strlen(variable)+1;
62 if ( bufferlen > SDL_envmemlen ) {
63 char *newmem = (char *)realloc(SDL_envmem, bufferlen);
64 if ( newmem == NULL ) {
65 return -1;
66 }
67 SDL_envmem = newmem;
68 SDL_envmemlen = bufferlen;
69 }
70 strcpy(SDL_envmem, variable);
71 value = SDL_envmem + (sep - variable);
72 *value++ = '\0';
73 if ( !SetEnvironmentVariable(SDL_envmem, *value ? value : NULL) ) {
74 return -1;
75 }
76 return 0;
77 }
78
79 /* Retrieve a variable named "name" from the environment */
80 char *SDL_getenv(const char *name)
81 {
82 DWORD bufferlen;
83
84 bufferlen = GetEnvironmentVariable(name, SDL_envmem, SDL_envmemlen);
85 if ( bufferlen == 0 ) {
86 return NULL;
87 }
88 if ( bufferlen > SDL_envmemlen ) {
89 char *newmem = (char *)realloc(SDL_envmem, bufferlen);
90 if ( newmem == NULL ) {
91 return NULL;
92 }
93 SDL_envmem = newmem;
94 SDL_envmemlen = bufferlen;
95 GetEnvironmentVariable(name, SDL_envmem, SDL_envmemlen);
96 }
97 return SDL_envmem;
98 }
99
100 #else /* roll our own */
37 101
38 #include <stdlib.h> 102 #include <stdlib.h>
39 #include <string.h> 103 #include <string.h>
40 104
41 static char **SDL_env = (char **)0; 105 static char **SDL_env = (char **)0;
121 } 185 }
122 } 186 }
123 return value; 187 return value;
124 } 188 }
125 189
190 #endif /* WIN32 */
191
126 #endif /* NEED_GETENV */ 192 #endif /* NEED_GETENV */
127 193
128 #ifdef TEST_MAIN 194 #ifdef TEST_MAIN
129 #include <stdio.h> 195 #include <stdio.h>
130 196