comparison src/stdlib/SDL_getenv.c @ 3583:65f66674b7fb

Allocate memory only if we're going to overwrite an environment variable
author Sam Lantinga <slouken@libsdl.org>
date Wed, 16 Dec 2009 16:08:17 +0000
parents 15eea7a1fa97
children db4af3cb938e
comparison
equal deleted inserted replaced
3582:ecfc42323d0c 3583:65f66674b7fb
89 char *new_variable; 89 char *new_variable;
90 90
91 /* A little error checking */ 91 /* A little error checking */
92 if (!name || !value) { 92 if (!name || !value) {
93 return (-1); 93 return (-1);
94 }
95
96 /* See if it already exists */
97 if (!overwrite && SDL_getenv(name)) {
98 return 0;
94 } 99 }
95 100
96 /* Allocate memory for the variable */ 101 /* Allocate memory for the variable */
97 len = SDL_strlen(name) + SDL_strlen(value) + 2; 102 len = SDL_strlen(name) + SDL_strlen(value) + 2;
98 new_variable = (char *) SDL_malloc(len); 103 new_variable = (char *) SDL_malloc(len);
115 break; 120 break;
116 } 121 }
117 } 122 }
118 /* If we found it, just replace the entry */ 123 /* If we found it, just replace the entry */
119 if (SDL_env[i]) { 124 if (SDL_env[i]) {
120 if (!overwrite) {
121 SDL_free(new_variable);
122 return 0;
123 }
124 SDL_free(SDL_env[i]); 125 SDL_free(SDL_env[i]);
125 SDL_env[i] = new_variable; 126 SDL_env[i] = new_variable;
126 added = 1; 127 added = 1;
127 } 128 }
128 } 129 }