src/stdlib/SDL_getenv.c
branchSDL-1.3
changeset 1662 782fd950bd46
parent 1472 4aac8563c296
child 1668 4da1ee79c9af
     1.1 --- a/src/stdlib/SDL_getenv.c	Sun May 21 17:27:13 2006 +0000
     1.2 +++ b/src/stdlib/SDL_getenv.c	Sun May 28 13:04:16 2006 +0000
     1.3 @@ -32,143 +32,148 @@
     1.4  
     1.5  /* Note this isn't thread-safe! */
     1.6  
     1.7 -static char *SDL_envmem = NULL;	/* Ugh, memory leak */
     1.8 +static char *SDL_envmem = NULL; /* Ugh, memory leak */
     1.9  static size_t SDL_envmemlen = 0;
    1.10  
    1.11  /* Put a variable of the form "name=value" into the environment */
    1.12 -int SDL_putenv(const char *variable)
    1.13 +int
    1.14 +SDL_putenv (const char *variable)
    1.15  {
    1.16 -	size_t bufferlen;
    1.17 -	char *value;
    1.18 -	const char *sep;
    1.19 +    size_t bufferlen;
    1.20 +    char *value;
    1.21 +    const char *sep;
    1.22  
    1.23 -	sep = SDL_strchr(variable, '=');
    1.24 -	if ( sep == NULL ) {
    1.25 -		return -1;
    1.26 -	}
    1.27 -	bufferlen = SDL_strlen(variable)+1;
    1.28 -	if ( bufferlen > SDL_envmemlen ) {
    1.29 -		char *newmem = (char *)SDL_realloc(SDL_envmem, bufferlen);
    1.30 -		if ( newmem == NULL ) {
    1.31 -			return -1;
    1.32 -		}
    1.33 -		SDL_envmem = newmem;
    1.34 -		SDL_envmemlen = bufferlen;
    1.35 -	}
    1.36 -	SDL_strlcpy(SDL_envmem, variable, bufferlen);
    1.37 -	value = SDL_envmem + (sep - variable);
    1.38 -	*value++ = '\0';
    1.39 -	if ( !SetEnvironmentVariable(SDL_envmem, *value ? value : NULL) ) {
    1.40 -		return -1;
    1.41 -	}
    1.42 -	return 0;
    1.43 +    sep = SDL_strchr (variable, '=');
    1.44 +    if (sep == NULL) {
    1.45 +        return -1;
    1.46 +    }
    1.47 +    bufferlen = SDL_strlen (variable) + 1;
    1.48 +    if (bufferlen > SDL_envmemlen) {
    1.49 +        char *newmem = (char *) SDL_realloc (SDL_envmem, bufferlen);
    1.50 +        if (newmem == NULL) {
    1.51 +            return -1;
    1.52 +        }
    1.53 +        SDL_envmem = newmem;
    1.54 +        SDL_envmemlen = bufferlen;
    1.55 +    }
    1.56 +    SDL_strlcpy (SDL_envmem, variable, bufferlen);
    1.57 +    value = SDL_envmem + (sep - variable);
    1.58 +    *value++ = '\0';
    1.59 +    if (!SetEnvironmentVariable (SDL_envmem, *value ? value : NULL)) {
    1.60 +        return -1;
    1.61 +    }
    1.62 +    return 0;
    1.63  }
    1.64  
    1.65  /* Retrieve a variable named "name" from the environment */
    1.66 -char *SDL_getenv(const char *name)
    1.67 +char *
    1.68 +SDL_getenv (const char *name)
    1.69  {
    1.70 -	size_t bufferlen;
    1.71 +    size_t bufferlen;
    1.72  
    1.73 -	bufferlen = GetEnvironmentVariable(name, SDL_envmem, (DWORD)SDL_envmemlen);
    1.74 -	if ( bufferlen == 0 ) {
    1.75 -		return NULL;
    1.76 -	}
    1.77 -	if ( bufferlen > SDL_envmemlen ) {
    1.78 -		char *newmem = (char *)SDL_realloc(SDL_envmem, bufferlen);
    1.79 -		if ( newmem == NULL ) {
    1.80 -			return NULL;
    1.81 -		}
    1.82 -		SDL_envmem = newmem;
    1.83 -		SDL_envmemlen = bufferlen;
    1.84 -		GetEnvironmentVariable(name, SDL_envmem, (DWORD)SDL_envmemlen);
    1.85 -	}
    1.86 -	return SDL_envmem;
    1.87 +    bufferlen =
    1.88 +        GetEnvironmentVariable (name, SDL_envmem, (DWORD) SDL_envmemlen);
    1.89 +    if (bufferlen == 0) {
    1.90 +        return NULL;
    1.91 +    }
    1.92 +    if (bufferlen > SDL_envmemlen) {
    1.93 +        char *newmem = (char *) SDL_realloc (SDL_envmem, bufferlen);
    1.94 +        if (newmem == NULL) {
    1.95 +            return NULL;
    1.96 +        }
    1.97 +        SDL_envmem = newmem;
    1.98 +        SDL_envmemlen = bufferlen;
    1.99 +        GetEnvironmentVariable (name, SDL_envmem, (DWORD) SDL_envmemlen);
   1.100 +    }
   1.101 +    return SDL_envmem;
   1.102  }
   1.103  
   1.104  #else /* roll our own */
   1.105  
   1.106 -static char **SDL_env = (char **)0;
   1.107 +static char **SDL_env = (char **) 0;
   1.108  
   1.109  /* Put a variable of the form "name=value" into the environment */
   1.110 -int SDL_putenv(const char *variable)
   1.111 +int
   1.112 +SDL_putenv (const char *variable)
   1.113  {
   1.114 -	const char *name, *value;
   1.115 -	int added;
   1.116 -	int len, i;
   1.117 -	char **new_env;
   1.118 -	char *new_variable;
   1.119 +    const char *name, *value;
   1.120 +    int added;
   1.121 +    int len, i;
   1.122 +    char **new_env;
   1.123 +    char *new_variable;
   1.124  
   1.125 -	/* A little error checking */
   1.126 -	if ( ! variable ) {
   1.127 -		return(-1);
   1.128 -	}
   1.129 -	name = variable;
   1.130 -	for ( value=variable; *value && (*value != '='); ++value ) {
   1.131 -		/* Keep looking for '=' */ ;
   1.132 -	}
   1.133 -	if ( *value ) {
   1.134 -		++value;
   1.135 -	} else {
   1.136 -		return(-1);
   1.137 -	}
   1.138 +    /* A little error checking */
   1.139 +    if (!variable) {
   1.140 +        return (-1);
   1.141 +    }
   1.142 +    name = variable;
   1.143 +    for (value = variable; *value && (*value != '='); ++value) {
   1.144 +        /* Keep looking for '=' */ ;
   1.145 +    }
   1.146 +    if (*value) {
   1.147 +        ++value;
   1.148 +    } else {
   1.149 +        return (-1);
   1.150 +    }
   1.151  
   1.152 -	/* Allocate memory for the variable */
   1.153 -	new_variable = SDL_strdup(variable);
   1.154 -	if ( ! new_variable ) {
   1.155 -		return(-1);
   1.156 -	}
   1.157 +    /* Allocate memory for the variable */
   1.158 +    new_variable = SDL_strdup (variable);
   1.159 +    if (!new_variable) {
   1.160 +        return (-1);
   1.161 +    }
   1.162  
   1.163 -	/* Actually put it into the environment */
   1.164 -	added = 0;
   1.165 -	i = 0;
   1.166 -	if ( SDL_env ) {
   1.167 -		/* Check to see if it's already there... */
   1.168 -		len = (value - name);
   1.169 -		for ( ; SDL_env[i]; ++i ) {
   1.170 -			if ( SDL_strncmp(SDL_env[i], name, len) == 0 ) {
   1.171 -				break;
   1.172 -			}
   1.173 -		}
   1.174 -		/* If we found it, just replace the entry */
   1.175 -		if ( SDL_env[i] ) {
   1.176 -			SDL_free(SDL_env[i]);
   1.177 -			SDL_env[i] = new_variable;
   1.178 -			added = 1;
   1.179 -		}
   1.180 -	}
   1.181 +    /* Actually put it into the environment */
   1.182 +    added = 0;
   1.183 +    i = 0;
   1.184 +    if (SDL_env) {
   1.185 +        /* Check to see if it's already there... */
   1.186 +        len = (value - name);
   1.187 +        for (; SDL_env[i]; ++i) {
   1.188 +            if (SDL_strncmp (SDL_env[i], name, len) == 0) {
   1.189 +                break;
   1.190 +            }
   1.191 +        }
   1.192 +        /* If we found it, just replace the entry */
   1.193 +        if (SDL_env[i]) {
   1.194 +            SDL_free (SDL_env[i]);
   1.195 +            SDL_env[i] = new_variable;
   1.196 +            added = 1;
   1.197 +        }
   1.198 +    }
   1.199  
   1.200 -	/* Didn't find it in the environment, expand and add */
   1.201 -	if ( ! added ) {
   1.202 -		new_env = SDL_realloc(SDL_env, (i+2)*sizeof(char *));
   1.203 -		if ( new_env ) {
   1.204 -			SDL_env = new_env;
   1.205 -			SDL_env[i++] = new_variable;
   1.206 -			SDL_env[i++] = (char *)0;
   1.207 -			added = 1;
   1.208 -		} else {
   1.209 -			SDL_free(new_variable);
   1.210 -		}
   1.211 -	}
   1.212 -	return (added ? 0 : -1);
   1.213 +    /* Didn't find it in the environment, expand and add */
   1.214 +    if (!added) {
   1.215 +        new_env = SDL_realloc (SDL_env, (i + 2) * sizeof (char *));
   1.216 +        if (new_env) {
   1.217 +            SDL_env = new_env;
   1.218 +            SDL_env[i++] = new_variable;
   1.219 +            SDL_env[i++] = (char *) 0;
   1.220 +            added = 1;
   1.221 +        } else {
   1.222 +            SDL_free (new_variable);
   1.223 +        }
   1.224 +    }
   1.225 +    return (added ? 0 : -1);
   1.226  }
   1.227  
   1.228  /* Retrieve a variable named "name" from the environment */
   1.229 -char *SDL_getenv(const char *name)
   1.230 +char *
   1.231 +SDL_getenv (const char *name)
   1.232  {
   1.233 -	int len, i;
   1.234 -	char *value;
   1.235 +    int len, i;
   1.236 +    char *value;
   1.237  
   1.238 -	value = (char *)0;
   1.239 -	if ( SDL_env ) {
   1.240 -		len = SDL_strlen(name);
   1.241 -		for ( i=0; SDL_env[i] && !value; ++i ) {
   1.242 -			if ( (SDL_strncmp(SDL_env[i], name, len) == 0) &&
   1.243 -			     (SDL_env[i][len] == '=') ) {
   1.244 -				value = &SDL_env[i][len+1];
   1.245 -			}
   1.246 -		}
   1.247 -	}
   1.248 -	return value;
   1.249 +    value = (char *) 0;
   1.250 +    if (SDL_env) {
   1.251 +        len = SDL_strlen (name);
   1.252 +        for (i = 0; SDL_env[i] && !value; ++i) {
   1.253 +            if ((SDL_strncmp (SDL_env[i], name, len) == 0) &&
   1.254 +                (SDL_env[i][len] == '=')) {
   1.255 +                value = &SDL_env[i][len + 1];
   1.256 +            }
   1.257 +        }
   1.258 +    }
   1.259 +    return value;
   1.260  }
   1.261  
   1.262  #endif /* __WIN32__ */
   1.263 @@ -178,70 +183,71 @@
   1.264  #ifdef TEST_MAIN
   1.265  #include <stdio.h>
   1.266  
   1.267 -int main(int argc, char *argv[])
   1.268 +int
   1.269 +main (int argc, char *argv[])
   1.270  {
   1.271 -	char *value;
   1.272 +    char *value;
   1.273  
   1.274 -	printf("Checking for non-existent variable... ");
   1.275 -	fflush(stdout);
   1.276 -	if ( ! SDL_getenv("EXISTS") ) {
   1.277 -		printf("okay\n");
   1.278 -	} else {
   1.279 -		printf("failed\n");
   1.280 -	}
   1.281 -	printf("Setting FIRST=VALUE1 in the environment... ");
   1.282 -	fflush(stdout);
   1.283 -	if ( SDL_putenv("FIRST=VALUE1") == 0 ) {
   1.284 -		printf("okay\n");
   1.285 -	} else {
   1.286 -		printf("failed\n");
   1.287 -	}
   1.288 -	printf("Getting FIRST from the environment... ");
   1.289 -	fflush(stdout);
   1.290 -	value = SDL_getenv("FIRST");
   1.291 -	if ( value && (SDL_strcmp(value, "VALUE1") == 0) ) {
   1.292 -		printf("okay\n");
   1.293 -	} else {
   1.294 -		printf("failed\n");
   1.295 -	}
   1.296 -	printf("Setting SECOND=VALUE2 in the environment... ");
   1.297 -	fflush(stdout);
   1.298 -	if ( SDL_putenv("SECOND=VALUE2") == 0 ) {
   1.299 -		printf("okay\n");
   1.300 -	} else {
   1.301 -		printf("failed\n");
   1.302 -	}
   1.303 -	printf("Getting SECOND from the environment... ");
   1.304 -	fflush(stdout);
   1.305 -	value = SDL_getenv("SECOND");
   1.306 -	if ( value && (SDL_strcmp(value, "VALUE2") == 0) ) {
   1.307 -		printf("okay\n");
   1.308 -	} else {
   1.309 -		printf("failed\n");
   1.310 -	}
   1.311 -	printf("Setting FIRST=NOVALUE in the environment... ");
   1.312 -	fflush(stdout);
   1.313 -	if ( SDL_putenv("FIRST=NOVALUE") == 0 ) {
   1.314 -		printf("okay\n");
   1.315 -	} else {
   1.316 -		printf("failed\n");
   1.317 -	}
   1.318 -	printf("Getting FIRST from the environment... ");
   1.319 -	fflush(stdout);
   1.320 -	value = SDL_getenv("FIRST");
   1.321 -	if ( value && (SDL_strcmp(value, "NOVALUE") == 0) ) {
   1.322 -		printf("okay\n");
   1.323 -	} else {
   1.324 -		printf("failed\n");
   1.325 -	}
   1.326 -	printf("Checking for non-existent variable... ");
   1.327 -	fflush(stdout);
   1.328 -	if ( ! SDL_getenv("EXISTS") ) {
   1.329 -		printf("okay\n");
   1.330 -	} else {
   1.331 -		printf("failed\n");
   1.332 -	}
   1.333 -	return(0);
   1.334 +    printf ("Checking for non-existent variable... ");
   1.335 +    fflush (stdout);
   1.336 +    if (!SDL_getenv ("EXISTS")) {
   1.337 +        printf ("okay\n");
   1.338 +    } else {
   1.339 +        printf ("failed\n");
   1.340 +    }
   1.341 +    printf ("Setting FIRST=VALUE1 in the environment... ");
   1.342 +    fflush (stdout);
   1.343 +    if (SDL_putenv ("FIRST=VALUE1") == 0) {
   1.344 +        printf ("okay\n");
   1.345 +    } else {
   1.346 +        printf ("failed\n");
   1.347 +    }
   1.348 +    printf ("Getting FIRST from the environment... ");
   1.349 +    fflush (stdout);
   1.350 +    value = SDL_getenv ("FIRST");
   1.351 +    if (value && (SDL_strcmp (value, "VALUE1") == 0)) {
   1.352 +        printf ("okay\n");
   1.353 +    } else {
   1.354 +        printf ("failed\n");
   1.355 +    }
   1.356 +    printf ("Setting SECOND=VALUE2 in the environment... ");
   1.357 +    fflush (stdout);
   1.358 +    if (SDL_putenv ("SECOND=VALUE2") == 0) {
   1.359 +        printf ("okay\n");
   1.360 +    } else {
   1.361 +        printf ("failed\n");
   1.362 +    }
   1.363 +    printf ("Getting SECOND from the environment... ");
   1.364 +    fflush (stdout);
   1.365 +    value = SDL_getenv ("SECOND");
   1.366 +    if (value && (SDL_strcmp (value, "VALUE2") == 0)) {
   1.367 +        printf ("okay\n");
   1.368 +    } else {
   1.369 +        printf ("failed\n");
   1.370 +    }
   1.371 +    printf ("Setting FIRST=NOVALUE in the environment... ");
   1.372 +    fflush (stdout);
   1.373 +    if (SDL_putenv ("FIRST=NOVALUE") == 0) {
   1.374 +        printf ("okay\n");
   1.375 +    } else {
   1.376 +        printf ("failed\n");
   1.377 +    }
   1.378 +    printf ("Getting FIRST from the environment... ");
   1.379 +    fflush (stdout);
   1.380 +    value = SDL_getenv ("FIRST");
   1.381 +    if (value && (SDL_strcmp (value, "NOVALUE") == 0)) {
   1.382 +        printf ("okay\n");
   1.383 +    } else {
   1.384 +        printf ("failed\n");
   1.385 +    }
   1.386 +    printf ("Checking for non-existent variable... ");
   1.387 +    fflush (stdout);
   1.388 +    if (!SDL_getenv ("EXISTS")) {
   1.389 +        printf ("okay\n");
   1.390 +    } else {
   1.391 +        printf ("failed\n");
   1.392 +    }
   1.393 +    return (0);
   1.394  }
   1.395  #endif /* TEST_MAIN */
   1.396 -
   1.397 +/* vi: set ts=4 sw=4 expandtab: */