comparison src/stdlib/SDL_getenv.c @ 1668:4da1ee79c9af SDL-1.3

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