comparison src/stdlib/SDL_getenv.c @ 1895:c121d94672cb

SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
author Sam Lantinga <slouken@libsdl.org>
date Mon, 10 Jul 2006 21:04:37 +0000
parents 4aac8563c296
children 99210400e8b9
comparison
equal deleted inserted replaced
1894:c69cee13dd76 1895:c121d94672cb
30 #define WIN32_LEAN_AND_MEAN 30 #define WIN32_LEAN_AND_MEAN
31 #include <windows.h> 31 #include <windows.h>
32 32
33 /* Note this isn't thread-safe! */ 33 /* Note this isn't thread-safe! */
34 34
35 static char *SDL_envmem = NULL; /* Ugh, memory leak */ 35 static char *SDL_envmem = NULL; /* Ugh, memory leak */
36 static size_t SDL_envmemlen = 0; 36 static size_t SDL_envmemlen = 0;
37 37
38 /* Put a variable of the form "name=value" into the environment */ 38 /* Put a variable of the form "name=value" into the environment */
39 int SDL_putenv(const char *variable) 39 int
40 { 40 SDL_putenv(const char *variable)
41 size_t bufferlen; 41 {
42 char *value; 42 size_t bufferlen;
43 const char *sep; 43 char *value;
44 44 const char *sep;
45 sep = SDL_strchr(variable, '='); 45
46 if ( sep == NULL ) { 46 sep = SDL_strchr(variable, '=');
47 return -1; 47 if (sep == NULL) {
48 } 48 return -1;
49 bufferlen = SDL_strlen(variable)+1; 49 }
50 if ( bufferlen > SDL_envmemlen ) { 50 bufferlen = SDL_strlen(variable) + 1;
51 char *newmem = (char *)SDL_realloc(SDL_envmem, bufferlen); 51 if (bufferlen > SDL_envmemlen) {
52 if ( newmem == NULL ) { 52 char *newmem = (char *) SDL_realloc(SDL_envmem, bufferlen);
53 return -1; 53 if (newmem == NULL) {
54 } 54 return -1;
55 SDL_envmem = newmem; 55 }
56 SDL_envmemlen = bufferlen; 56 SDL_envmem = newmem;
57 } 57 SDL_envmemlen = bufferlen;
58 SDL_strlcpy(SDL_envmem, variable, bufferlen); 58 }
59 value = SDL_envmem + (sep - variable); 59 SDL_strlcpy(SDL_envmem, variable, bufferlen);
60 *value++ = '\0'; 60 value = SDL_envmem + (sep - variable);
61 if ( !SetEnvironmentVariable(SDL_envmem, *value ? value : NULL) ) { 61 *value++ = '\0';
62 return -1; 62 if (!SetEnvironmentVariable(SDL_envmem, *value ? value : NULL)) {
63 } 63 return -1;
64 return 0; 64 }
65 return 0;
65 } 66 }
66 67
67 /* Retrieve a variable named "name" from the environment */ 68 /* Retrieve a variable named "name" from the environment */
68 char *SDL_getenv(const char *name) 69 char *
69 { 70 SDL_getenv(const char *name)
70 size_t bufferlen; 71 {
71 72 size_t bufferlen;
72 bufferlen = GetEnvironmentVariable(name, SDL_envmem, (DWORD)SDL_envmemlen); 73
73 if ( bufferlen == 0 ) { 74 bufferlen =
74 return NULL; 75 GetEnvironmentVariable(name, SDL_envmem, (DWORD) SDL_envmemlen);
75 } 76 if (bufferlen == 0) {
76 if ( bufferlen > SDL_envmemlen ) { 77 return NULL;
77 char *newmem = (char *)SDL_realloc(SDL_envmem, bufferlen); 78 }
78 if ( newmem == NULL ) { 79 if (bufferlen > SDL_envmemlen) {
79 return NULL; 80 char *newmem = (char *) SDL_realloc(SDL_envmem, bufferlen);
80 } 81 if (newmem == NULL) {
81 SDL_envmem = newmem; 82 return NULL;
82 SDL_envmemlen = bufferlen; 83 }
83 GetEnvironmentVariable(name, SDL_envmem, (DWORD)SDL_envmemlen); 84 SDL_envmem = newmem;
84 } 85 SDL_envmemlen = bufferlen;
85 return SDL_envmem; 86 GetEnvironmentVariable(name, SDL_envmem, (DWORD) SDL_envmemlen);
87 }
88 return SDL_envmem;
86 } 89 }
87 90
88 #else /* roll our own */ 91 #else /* roll our own */
89 92
90 static char **SDL_env = (char **)0; 93 static char **SDL_env = (char **) 0;
91 94
92 /* Put a variable of the form "name=value" into the environment */ 95 /* Put a variable of the form "name=value" into the environment */
93 int SDL_putenv(const char *variable) 96 int
94 { 97 SDL_putenv(const char *variable)
95 const char *name, *value; 98 {
96 int added; 99 const char *name, *value;
97 int len, i; 100 int added;
98 char **new_env; 101 int len, i;
99 char *new_variable; 102 char **new_env;
100 103 char *new_variable;
101 /* A little error checking */ 104
102 if ( ! variable ) { 105 /* A little error checking */
103 return(-1); 106 if (!variable) {
104 } 107 return (-1);
105 name = variable; 108 }
106 for ( value=variable; *value && (*value != '='); ++value ) { 109 name = variable;
107 /* Keep looking for '=' */ ; 110 for (value = variable; *value && (*value != '='); ++value) {
108 } 111 /* Keep looking for '=' */ ;
109 if ( *value ) { 112 }
110 ++value; 113 if (*value) {
111 } else { 114 ++value;
112 return(-1); 115 } else {
113 } 116 return (-1);
114 117 }
115 /* Allocate memory for the variable */ 118
116 new_variable = SDL_strdup(variable); 119 /* Allocate memory for the variable */
117 if ( ! new_variable ) { 120 new_variable = SDL_strdup(variable);
118 return(-1); 121 if (!new_variable) {
119 } 122 return (-1);
120 123 }
121 /* Actually put it into the environment */ 124
122 added = 0; 125 /* Actually put it into the environment */
123 i = 0; 126 added = 0;
124 if ( SDL_env ) { 127 i = 0;
125 /* Check to see if it's already there... */ 128 if (SDL_env) {
126 len = (value - name); 129 /* Check to see if it's already there... */
127 for ( ; SDL_env[i]; ++i ) { 130 len = (value - name);
128 if ( SDL_strncmp(SDL_env[i], name, len) == 0 ) { 131 for (; SDL_env[i]; ++i) {
129 break; 132 if (SDL_strncmp(SDL_env[i], name, len) == 0) {
130 } 133 break;
131 } 134 }
132 /* If we found it, just replace the entry */ 135 }
133 if ( SDL_env[i] ) { 136 /* If we found it, just replace the entry */
134 SDL_free(SDL_env[i]); 137 if (SDL_env[i]) {
135 SDL_env[i] = new_variable; 138 SDL_free(SDL_env[i]);
136 added = 1; 139 SDL_env[i] = new_variable;
137 } 140 added = 1;
138 } 141 }
139 142 }
140 /* Didn't find it in the environment, expand and add */ 143
141 if ( ! added ) { 144 /* Didn't find it in the environment, expand and add */
142 new_env = SDL_realloc(SDL_env, (i+2)*sizeof(char *)); 145 if (!added) {
143 if ( new_env ) { 146 new_env = SDL_realloc(SDL_env, (i + 2) * sizeof(char *));
144 SDL_env = new_env; 147 if (new_env) {
145 SDL_env[i++] = new_variable; 148 SDL_env = new_env;
146 SDL_env[i++] = (char *)0; 149 SDL_env[i++] = new_variable;
147 added = 1; 150 SDL_env[i++] = (char *) 0;
148 } else { 151 added = 1;
149 SDL_free(new_variable); 152 } else {
150 } 153 SDL_free(new_variable);
151 } 154 }
152 return (added ? 0 : -1); 155 }
156 return (added ? 0 : -1);
153 } 157 }
154 158
155 /* Retrieve a variable named "name" from the environment */ 159 /* Retrieve a variable named "name" from the environment */
156 char *SDL_getenv(const char *name) 160 char *
157 { 161 SDL_getenv(const char *name)
158 int len, i; 162 {
159 char *value; 163 int len, i;
160 164 char *value;
161 value = (char *)0; 165
162 if ( SDL_env ) { 166 value = (char *) 0;
163 len = SDL_strlen(name); 167 if (SDL_env) {
164 for ( i=0; SDL_env[i] && !value; ++i ) { 168 len = SDL_strlen(name);
165 if ( (SDL_strncmp(SDL_env[i], name, len) == 0) && 169 for (i = 0; SDL_env[i] && !value; ++i) {
166 (SDL_env[i][len] == '=') ) { 170 if ((SDL_strncmp(SDL_env[i], name, len) == 0) &&
167 value = &SDL_env[i][len+1]; 171 (SDL_env[i][len] == '=')) {
168 } 172 value = &SDL_env[i][len + 1];
169 } 173 }
170 } 174 }
171 return value; 175 }
176 return value;
172 } 177 }
173 178
174 #endif /* __WIN32__ */ 179 #endif /* __WIN32__ */
175 180
176 #endif /* !HAVE_GETENV */ 181 #endif /* !HAVE_GETENV */
177 182
178 #ifdef TEST_MAIN 183 #ifdef TEST_MAIN
179 #include <stdio.h> 184 #include <stdio.h>
180 185
181 int main(int argc, char *argv[]) 186 int
182 { 187 main(int argc, char *argv[])
183 char *value; 188 {
184 189 char *value;
185 printf("Checking for non-existent variable... "); 190
186 fflush(stdout); 191 printf("Checking for non-existent variable... ");
187 if ( ! SDL_getenv("EXISTS") ) { 192 fflush(stdout);
188 printf("okay\n"); 193 if (!SDL_getenv("EXISTS")) {
189 } else { 194 printf("okay\n");
190 printf("failed\n"); 195 } else {
191 } 196 printf("failed\n");
192 printf("Setting FIRST=VALUE1 in the environment... "); 197 }
193 fflush(stdout); 198 printf("Setting FIRST=VALUE1 in the environment... ");
194 if ( SDL_putenv("FIRST=VALUE1") == 0 ) { 199 fflush(stdout);
195 printf("okay\n"); 200 if (SDL_putenv("FIRST=VALUE1") == 0) {
196 } else { 201 printf("okay\n");
197 printf("failed\n"); 202 } else {
198 } 203 printf("failed\n");
199 printf("Getting FIRST from the environment... "); 204 }
200 fflush(stdout); 205 printf("Getting FIRST from the environment... ");
201 value = SDL_getenv("FIRST"); 206 fflush(stdout);
202 if ( value && (SDL_strcmp(value, "VALUE1") == 0) ) { 207 value = SDL_getenv("FIRST");
203 printf("okay\n"); 208 if (value && (SDL_strcmp(value, "VALUE1") == 0)) {
204 } else { 209 printf("okay\n");
205 printf("failed\n"); 210 } else {
206 } 211 printf("failed\n");
207 printf("Setting SECOND=VALUE2 in the environment... "); 212 }
208 fflush(stdout); 213 printf("Setting SECOND=VALUE2 in the environment... ");
209 if ( SDL_putenv("SECOND=VALUE2") == 0 ) { 214 fflush(stdout);
210 printf("okay\n"); 215 if (SDL_putenv("SECOND=VALUE2") == 0) {
211 } else { 216 printf("okay\n");
212 printf("failed\n"); 217 } else {
213 } 218 printf("failed\n");
214 printf("Getting SECOND from the environment... "); 219 }
215 fflush(stdout); 220 printf("Getting SECOND from the environment... ");
216 value = SDL_getenv("SECOND"); 221 fflush(stdout);
217 if ( value && (SDL_strcmp(value, "VALUE2") == 0) ) { 222 value = SDL_getenv("SECOND");
218 printf("okay\n"); 223 if (value && (SDL_strcmp(value, "VALUE2") == 0)) {
219 } else { 224 printf("okay\n");
220 printf("failed\n"); 225 } else {
221 } 226 printf("failed\n");
222 printf("Setting FIRST=NOVALUE in the environment... "); 227 }
223 fflush(stdout); 228 printf("Setting FIRST=NOVALUE in the environment... ");
224 if ( SDL_putenv("FIRST=NOVALUE") == 0 ) { 229 fflush(stdout);
225 printf("okay\n"); 230 if (SDL_putenv("FIRST=NOVALUE") == 0) {
226 } else { 231 printf("okay\n");
227 printf("failed\n"); 232 } else {
228 } 233 printf("failed\n");
229 printf("Getting FIRST from the environment... "); 234 }
230 fflush(stdout); 235 printf("Getting FIRST from the environment... ");
231 value = SDL_getenv("FIRST"); 236 fflush(stdout);
232 if ( value && (SDL_strcmp(value, "NOVALUE") == 0) ) { 237 value = SDL_getenv("FIRST");
233 printf("okay\n"); 238 if (value && (SDL_strcmp(value, "NOVALUE") == 0)) {
234 } else { 239 printf("okay\n");
235 printf("failed\n"); 240 } else {
236 } 241 printf("failed\n");
237 printf("Checking for non-existent variable... "); 242 }
238 fflush(stdout); 243 printf("Checking for non-existent variable... ");
239 if ( ! SDL_getenv("EXISTS") ) { 244 fflush(stdout);
240 printf("okay\n"); 245 if (!SDL_getenv("EXISTS")) {
241 } else { 246 printf("okay\n");
242 printf("failed\n"); 247 } else {
243 } 248 printf("failed\n");
244 return(0); 249 }
250 return (0);
245 } 251 }
246 #endif /* TEST_MAIN */ 252 #endif /* TEST_MAIN */
247 253 /* vi: set ts=4 sw=4 expandtab: */