src/stdlib/SDL_getenv.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 19 Feb 2006 23:46:34 +0000
changeset 1379 c0a74f199ecf
parent 1358 c71e05b4dc2e
child 1402 d910939febfa
permissions -rw-r--r--
Use only safe string functions
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2006 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     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
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 #include "SDL_stdinc.h"
    24 
    25 #ifndef HAVE_GETENV
    26 
    27 #if defined(WIN32) && !defined(_WIN32_WCE)
    28 
    29 #include "SDL_windows.h"
    30 
    31 /* Note this isn't thread-safe! */
    32 
    33 static char *SDL_envmem = NULL;	/* Ugh, memory leak */
    34 static DWORD SDL_envmemlen = 0;
    35 
    36 /* Put a variable of the form "name=value" into the environment */
    37 int SDL_putenv(const char *variable)
    38 {
    39 	DWORD bufferlen;
    40 	char *value;
    41 	const char *sep;
    42 
    43 	sep = SDL_strchr(variable, '=');
    44 	if ( sep == NULL ) {
    45 		return -1;
    46 	}
    47 	bufferlen = SDL_strlen(variable)+1;
    48 	if ( bufferlen > SDL_envmemlen ) {
    49 		char *newmem = (char *)SDL_realloc(SDL_envmem, bufferlen);
    50 		if ( newmem == NULL ) {
    51 			return -1;
    52 		}
    53 		SDL_envmem = newmem;
    54 		SDL_envmemlen = bufferlen;
    55 	}
    56 	SDL_strlcpy(SDL_envmem, variable, bufferlen);
    57 	value = SDL_envmem + (sep - variable);
    58 	*value++ = '\0';
    59 	if ( !SetEnvironmentVariable(SDL_envmem, *value ? value : NULL) ) {
    60 		return -1;
    61 	}
    62 	return 0;
    63 }
    64 
    65 /* Retrieve a variable named "name" from the environment */
    66 char *SDL_getenv(const char *name)
    67 {
    68 	DWORD bufferlen;
    69 
    70 	bufferlen = GetEnvironmentVariable(name, SDL_envmem, SDL_envmemlen);
    71 	if ( bufferlen == 0 ) {
    72 		return NULL;
    73 	}
    74 	if ( bufferlen > SDL_envmemlen ) {
    75 		char *newmem = (char *)SDL_realloc(SDL_envmem, bufferlen);
    76 		if ( newmem == NULL ) {
    77 			return NULL;
    78 		}
    79 		SDL_envmem = newmem;
    80 		SDL_envmemlen = bufferlen;
    81 		GetEnvironmentVariable(name, SDL_envmem, SDL_envmemlen);
    82 	}
    83 	return SDL_envmem;
    84 }
    85 
    86 #else /* roll our own */
    87 
    88 static char **SDL_env = (char **)0;
    89 
    90 /* Put a variable of the form "name=value" into the environment */
    91 int SDL_putenv(const char *variable)
    92 {
    93 	const char *name, *value;
    94 	int added;
    95 	int len, i;
    96 	char **new_env;
    97 	char *new_variable;
    98 
    99 	/* A little error checking */
   100 	if ( ! variable ) {
   101 		return(-1);
   102 	}
   103 	name = variable;
   104 	for ( value=variable; *value && (*value != '='); ++value ) {
   105 		/* Keep looking for '=' */ ;
   106 	}
   107 	if ( *value ) {
   108 		++value;
   109 	} else {
   110 		return(-1);
   111 	}
   112 
   113 	/* Allocate memory for the variable */
   114 	new_variable = SDL_strdup(variable);
   115 	if ( ! new_variable ) {
   116 		return(-1);
   117 	}
   118 
   119 	/* Actually put it into the environment */
   120 	added = 0;
   121 	i = 0;
   122 	if ( SDL_env ) {
   123 		/* Check to see if it's already there... */
   124 		len = (value - name);
   125 		for ( ; SDL_env[i]; ++i ) {
   126 			if ( SDL_strncmp(SDL_env[i], name, len) == 0 ) {
   127 				break;
   128 			}
   129 		}
   130 		/* If we found it, just replace the entry */
   131 		if ( SDL_env[i] ) {
   132 			SDL_free(SDL_env[i]);
   133 			SDL_env[i] = new_variable;
   134 			added = 1;
   135 		}
   136 	}
   137 
   138 	/* Didn't find it in the environment, expand and add */
   139 	if ( ! added ) {
   140 		new_env = SDL_realloc(SDL_env, (i+2)*sizeof(char *));
   141 		if ( new_env ) {
   142 			SDL_env = new_env;
   143 			SDL_env[i++] = new_variable;
   144 			SDL_env[i++] = (char *)0;
   145 			added = 1;
   146 		} else {
   147 			SDL_free(new_variable);
   148 		}
   149 	}
   150 	return (added ? 0 : -1);
   151 }
   152 
   153 /* Retrieve a variable named "name" from the environment */
   154 char *SDL_getenv(const char *name)
   155 {
   156 	int len, i;
   157 	char *value;
   158 
   159 	value = (char *)0;
   160 	if ( SDL_env ) {
   161 		len = SDL_strlen(name);
   162 		for ( i=0; SDL_env[i] && !value; ++i ) {
   163 			if ( (SDL_strncmp(SDL_env[i], name, len) == 0) &&
   164 			     (SDL_env[i][len] == '=') ) {
   165 				value = &SDL_env[i][len+1];
   166 			}
   167 		}
   168 	}
   169 	return value;
   170 }
   171 
   172 #endif /* WIN32 */
   173 
   174 #endif /* !HAVE_GETENV */
   175 
   176 #ifdef TEST_MAIN
   177 #include <stdio.h>
   178 
   179 int main(int argc, char *argv[])
   180 {
   181 	char *value;
   182 
   183 	printf("Checking for non-existent variable... ");
   184 	fflush(stdout);
   185 	if ( ! SDL_getenv("EXISTS") ) {
   186 		printf("okay\n");
   187 	} else {
   188 		printf("failed\n");
   189 	}
   190 	printf("Setting FIRST=VALUE1 in the environment... ");
   191 	fflush(stdout);
   192 	if ( SDL_putenv("FIRST=VALUE1") == 0 ) {
   193 		printf("okay\n");
   194 	} else {
   195 		printf("failed\n");
   196 	}
   197 	printf("Getting FIRST from the environment... ");
   198 	fflush(stdout);
   199 	value = SDL_getenv("FIRST");
   200 	if ( value && (SDL_strcmp(value, "VALUE1") == 0) ) {
   201 		printf("okay\n");
   202 	} else {
   203 		printf("failed\n");
   204 	}
   205 	printf("Setting SECOND=VALUE2 in the environment... ");
   206 	fflush(stdout);
   207 	if ( SDL_putenv("SECOND=VALUE2") == 0 ) {
   208 		printf("okay\n");
   209 	} else {
   210 		printf("failed\n");
   211 	}
   212 	printf("Getting SECOND from the environment... ");
   213 	fflush(stdout);
   214 	value = SDL_getenv("SECOND");
   215 	if ( value && (SDL_strcmp(value, "VALUE2") == 0) ) {
   216 		printf("okay\n");
   217 	} else {
   218 		printf("failed\n");
   219 	}
   220 	printf("Setting FIRST=NOVALUE in the environment... ");
   221 	fflush(stdout);
   222 	if ( SDL_putenv("FIRST=NOVALUE") == 0 ) {
   223 		printf("okay\n");
   224 	} else {
   225 		printf("failed\n");
   226 	}
   227 	printf("Getting FIRST from the environment... ");
   228 	fflush(stdout);
   229 	value = SDL_getenv("FIRST");
   230 	if ( value && (SDL_strcmp(value, "NOVALUE") == 0) ) {
   231 		printf("okay\n");
   232 	} else {
   233 		printf("failed\n");
   234 	}
   235 	printf("Checking for non-existent variable... ");
   236 	fflush(stdout);
   237 	if ( ! SDL_getenv("EXISTS") ) {
   238 		printf("okay\n");
   239 	} else {
   240 		printf("failed\n");
   241 	}
   242 	return(0);
   243 }
   244 #endif /* TEST_MAIN */
   245