Mercurial > sdl-ios-xcode
annotate src/SDL_getenv.c @ 1318:f95502c6fc72
Eliminate duplicate modes with different refresh rates
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Wed, 01 Feb 2006 09:28:42 +0000 |
parents | c9b51268668f |
children |
rev | line source |
---|---|
249 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1273
diff
changeset
|
3 Copyright (C) 1997-2006 Sam Lantinga |
249 | 4 |
5 This library is free software; you can redistribute it and/or | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1273
diff
changeset
|
6 modify it under the terms of the GNU Lesser General Public |
249 | 7 License as published by the Free Software Foundation; either |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1273
diff
changeset
|
8 version 2.1 of the License, or (at your option) any later version. |
249 | 9 |
10 This library is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1273
diff
changeset
|
13 Lesser General Public License for more details. |
249 | 14 |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1273
diff
changeset
|
15 You should have received a copy of the GNU Lesser General Public |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1273
diff
changeset
|
16 License along with this library; if not, write to the Free Software |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1273
diff
changeset
|
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
249 | 18 |
19 Sam Lantinga | |
252
e8157fcb3114
Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents:
249
diff
changeset
|
20 slouken@libsdl.org |
249 | 21 */ |
22 | |
0 | 23 /* Not all environments have a working getenv()/putenv() */ |
24 | |
25 #ifdef TEST_MAIN | |
26 #define NEED_SDL_GETENV | |
27 #endif | |
28 | |
29 #include "SDL_getenv.h" | |
30 | |
31 #ifdef NEED_SDL_GETENV | |
32 | |
1273
05d5d36b71f4
Who'd have thought that Windows CE wouldn't have the environment APIs?
Sam Lantinga <slouken@libsdl.org>
parents:
1268
diff
changeset
|
33 #if defined(WIN32) && !defined(_WIN32_WCE) |
1268
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
34 |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
35 #define WIN32_LEAN_AND_MEAN |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
36 #include <windows.h> |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
37 #include <malloc.h> |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
38 #include <string.h> |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
39 |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
40 /* Note this isn't thread-safe! */ |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
41 |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
42 static char *SDL_envmem = NULL; /* Ugh, memory leak */ |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
43 static DWORD SDL_envmemlen = 0; |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
44 |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
45 /* Put a variable of the form "name=value" into the environment */ |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
46 int SDL_putenv(const char *variable) |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
47 { |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
48 DWORD bufferlen; |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
49 char *value; |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
50 const char *sep; |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
51 |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
52 sep = strchr(variable, '='); |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
53 if ( sep == NULL ) { |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
54 return -1; |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
55 } |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
56 bufferlen = strlen(variable)+1; |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
57 if ( bufferlen > SDL_envmemlen ) { |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
58 char *newmem = (char *)realloc(SDL_envmem, bufferlen); |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
59 if ( newmem == NULL ) { |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
60 return -1; |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
61 } |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
62 SDL_envmem = newmem; |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
63 SDL_envmemlen = bufferlen; |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
64 } |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
65 strcpy(SDL_envmem, variable); |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
66 value = SDL_envmem + (sep - variable); |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
67 *value++ = '\0'; |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
68 if ( !SetEnvironmentVariable(SDL_envmem, *value ? value : NULL) ) { |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
69 return -1; |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
70 } |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
71 return 0; |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
72 } |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
73 |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
74 /* Retrieve a variable named "name" from the environment */ |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
75 char *SDL_getenv(const char *name) |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
76 { |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
77 DWORD bufferlen; |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
78 |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
79 bufferlen = GetEnvironmentVariable(name, SDL_envmem, SDL_envmemlen); |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
80 if ( bufferlen == 0 ) { |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
81 return NULL; |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
82 } |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
83 if ( bufferlen > SDL_envmemlen ) { |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
84 char *newmem = (char *)realloc(SDL_envmem, bufferlen); |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
85 if ( newmem == NULL ) { |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
86 return NULL; |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
87 } |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
88 SDL_envmem = newmem; |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
89 SDL_envmemlen = bufferlen; |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
90 GetEnvironmentVariable(name, SDL_envmem, SDL_envmemlen); |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
91 } |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
92 return SDL_envmem; |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
93 } |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
94 |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
95 #else /* roll our own */ |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
96 |
0 | 97 #include <stdlib.h> |
98 #include <string.h> | |
99 | |
100 static char **SDL_env = (char **)0; | |
101 | |
102 /* Put a variable of the form "name=value" into the environment */ | |
103 int SDL_putenv(const char *variable) | |
104 { | |
105 const char *name, *value; | |
106 int added; | |
107 int len, i; | |
108 char **new_env; | |
109 char *new_variable; | |
110 | |
111 /* A little error checking */ | |
112 if ( ! variable ) { | |
113 return(-1); | |
114 } | |
115 name = variable; | |
116 for ( value=variable; *value && (*value != '='); ++value ) { | |
117 /* Keep looking for '=' */ ; | |
118 } | |
119 if ( *value ) { | |
120 ++value; | |
121 } else { | |
122 return(-1); | |
123 } | |
124 | |
125 /* Allocate memory for the variable */ | |
126 new_variable = (char *)malloc(strlen(variable)+1); | |
127 if ( ! new_variable ) { | |
128 return(-1); | |
129 } | |
130 strcpy(new_variable, variable); | |
131 | |
132 /* Actually put it into the environment */ | |
133 added = 0; | |
134 i = 0; | |
135 if ( SDL_env ) { | |
136 /* Check to see if it's already there... */ | |
137 len = (value - name); | |
138 for ( ; SDL_env[i]; ++i ) { | |
139 if ( strncmp(SDL_env[i], name, len) == 0 ) { | |
140 break; | |
141 } | |
142 } | |
143 /* If we found it, just replace the entry */ | |
144 if ( SDL_env[i] ) { | |
145 free(SDL_env[i]); | |
146 SDL_env[i] = new_variable; | |
147 added = 1; | |
148 } | |
149 } | |
150 | |
151 /* Didn't find it in the environment, expand and add */ | |
152 if ( ! added ) { | |
153 new_env = realloc(SDL_env, (i+2)*sizeof(char *)); | |
154 if ( new_env ) { | |
155 SDL_env = new_env; | |
156 SDL_env[i++] = new_variable; | |
157 SDL_env[i++] = (char *)0; | |
158 added = 1; | |
159 } else { | |
160 free(new_variable); | |
161 } | |
162 } | |
163 return (added ? 0 : -1); | |
164 } | |
165 | |
166 /* Retrieve a variable named "name" from the environment */ | |
167 char *SDL_getenv(const char *name) | |
168 { | |
169 int len, i; | |
170 char *value; | |
171 | |
172 value = (char *)0; | |
173 if ( SDL_env ) { | |
174 len = strlen(name); | |
175 for ( i=0; SDL_env[i] && !value; ++i ) { | |
176 if ( (strncmp(SDL_env[i], name, len) == 0) && | |
177 (SDL_env[i][len] == '=') ) { | |
178 value = &SDL_env[i][len+1]; | |
179 } | |
180 } | |
181 } | |
182 return value; | |
183 } | |
184 | |
1268
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
185 #endif /* WIN32 */ |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
186 |
0 | 187 #endif /* NEED_GETENV */ |
188 | |
189 #ifdef TEST_MAIN | |
190 #include <stdio.h> | |
191 | |
192 int main(int argc, char *argv[]) | |
193 { | |
194 char *value; | |
195 | |
196 printf("Checking for non-existent variable... "); | |
197 fflush(stdout); | |
198 if ( ! getenv("EXISTS") ) { | |
199 printf("okay\n"); | |
200 } else { | |
201 printf("failed\n"); | |
202 } | |
203 printf("Setting FIRST=VALUE1 in the environment... "); | |
204 fflush(stdout); | |
205 if ( putenv("FIRST=VALUE1") == 0 ) { | |
206 printf("okay\n"); | |
207 } else { | |
208 printf("failed\n"); | |
209 } | |
210 printf("Getting FIRST from the environment... "); | |
211 fflush(stdout); | |
212 value = getenv("FIRST"); | |
213 if ( value && (strcmp(value, "VALUE1") == 0) ) { | |
214 printf("okay\n"); | |
215 } else { | |
216 printf("failed\n"); | |
217 } | |
218 printf("Setting SECOND=VALUE2 in the environment... "); | |
219 fflush(stdout); | |
220 if ( putenv("SECOND=VALUE2") == 0 ) { | |
221 printf("okay\n"); | |
222 } else { | |
223 printf("failed\n"); | |
224 } | |
225 printf("Getting SECOND from the environment... "); | |
226 fflush(stdout); | |
227 value = getenv("SECOND"); | |
228 if ( value && (strcmp(value, "VALUE2") == 0) ) { | |
229 printf("okay\n"); | |
230 } else { | |
231 printf("failed\n"); | |
232 } | |
233 printf("Setting FIRST=NOVALUE in the environment... "); | |
234 fflush(stdout); | |
235 if ( putenv("FIRST=NOVALUE") == 0 ) { | |
236 printf("okay\n"); | |
237 } else { | |
238 printf("failed\n"); | |
239 } | |
240 printf("Getting FIRST from the environment... "); | |
241 fflush(stdout); | |
242 value = getenv("FIRST"); | |
243 if ( value && (strcmp(value, "NOVALUE") == 0) ) { | |
244 printf("okay\n"); | |
245 } else { | |
246 printf("failed\n"); | |
247 } | |
248 printf("Checking for non-existent variable... "); | |
249 fflush(stdout); | |
250 if ( ! getenv("EXISTS") ) { | |
251 printf("okay\n"); | |
252 } else { | |
253 printf("failed\n"); | |
254 } | |
255 return(0); | |
256 } | |
257 #endif /* TEST_MAIN */ | |
258 |