src/stdlib/SDL_getenv.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 21 Feb 2006 08:46:50 +0000
changeset 1402 d910939febfa
parent 1379 c0a74f199ecf
child 1433 bb6839704ed6
permissions -rw-r--r--
Use consistent identifiers for the various platforms we support.
Make sure every source file includes SDL_config.h, so the proper system
headers are chosen.
     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 #include "SDL_config.h"
    23 
    24 #include "SDL_stdinc.h"
    25 
    26 #ifndef HAVE_GETENV
    27 
    28 #if defined(__WIN32__) && !defined(_WIN32_WCE)
    29 
    30 #include "SDL_windows.h"
    31 
    32 /* Note this isn't thread-safe! */
    33 
    34 static char *SDL_envmem = NULL;	/* Ugh, memory leak */
    35 static DWORD SDL_envmemlen = 0;
    36 
    37 /* Put a variable of the form "name=value" into the environment */
    38 int SDL_putenv(const char *variable)
    39 {
    40 	DWORD bufferlen;
    41 	char *value;
    42 	const char *sep;
    43 
    44 	sep = SDL_strchr(variable, '=');
    45 	if ( sep == NULL ) {
    46 		return -1;
    47 	}
    48 	bufferlen = SDL_strlen(variable)+1;
    49 	if ( bufferlen > SDL_envmemlen ) {
    50 		char *newmem = (char *)SDL_realloc(SDL_envmem, bufferlen);
    51 		if ( newmem == NULL ) {
    52 			return -1;
    53 		}
    54 		SDL_envmem = newmem;
    55 		SDL_envmemlen = bufferlen;
    56 	}
    57 	SDL_strlcpy(SDL_envmem, variable, bufferlen);
    58 	value = SDL_envmem + (sep - variable);
    59 	*value++ = '\0';
    60 	if ( !SetEnvironmentVariable(SDL_envmem, *value ? value : NULL) ) {
    61 		return -1;
    62 	}
    63 	return 0;
    64 }
    65 
    66 /* Retrieve a variable named "name" from the environment */
    67 char *SDL_getenv(const char *name)
    68 {
    69 	DWORD bufferlen;
    70 
    71 	bufferlen = GetEnvironmentVariable(name, SDL_envmem, SDL_envmemlen);
    72 	if ( bufferlen == 0 ) {
    73 		return NULL;
    74 	}
    75 	if ( bufferlen > SDL_envmemlen ) {
    76 		char *newmem = (char *)SDL_realloc(SDL_envmem, bufferlen);
    77 		if ( newmem == NULL ) {
    78 			return NULL;
    79 		}
    80 		SDL_envmem = newmem;
    81 		SDL_envmemlen = bufferlen;
    82 		GetEnvironmentVariable(name, SDL_envmem, SDL_envmemlen);
    83 	}
    84 	return SDL_envmem;
    85 }
    86 
    87 #else /* roll our own */
    88 
    89 static char **SDL_env = (char **)0;
    90 
    91 /* Put a variable of the form "name=value" into the environment */
    92 int SDL_putenv(const char *variable)
    93 {
    94 	const char *name, *value;
    95 	int added;
    96 	int len, i;
    97 	char **new_env;
    98 	char *new_variable;
    99 
   100 	/* A little error checking */
   101 	if ( ! variable ) {
   102 		return(-1);
   103 	}
   104 	name = variable;
   105 	for ( value=variable; *value && (*value != '='); ++value ) {
   106 		/* Keep looking for '=' */ ;
   107 	}
   108 	if ( *value ) {
   109 		++value;
   110 	} else {
   111 		return(-1);
   112 	}
   113 
   114 	/* Allocate memory for the variable */
   115 	new_variable = SDL_strdup(variable);
   116 	if ( ! new_variable ) {
   117 		return(-1);
   118 	}
   119 
   120 	/* Actually put it into the environment */
   121 	added = 0;
   122 	i = 0;
   123 	if ( SDL_env ) {
   124 		/* Check to see if it's already there... */
   125 		len = (value - name);
   126 		for ( ; SDL_env[i]; ++i ) {
   127 			if ( SDL_strncmp(SDL_env[i], name, len) == 0 ) {
   128 				break;
   129 			}
   130 		}
   131 		/* If we found it, just replace the entry */
   132 		if ( SDL_env[i] ) {
   133 			SDL_free(SDL_env[i]);
   134 			SDL_env[i] = new_variable;
   135 			added = 1;
   136 		}
   137 	}
   138 
   139 	/* Didn't find it in the environment, expand and add */
   140 	if ( ! added ) {
   141 		new_env = SDL_realloc(SDL_env, (i+2)*sizeof(char *));
   142 		if ( new_env ) {
   143 			SDL_env = new_env;
   144 			SDL_env[i++] = new_variable;
   145 			SDL_env[i++] = (char *)0;
   146 			added = 1;
   147 		} else {
   148 			SDL_free(new_variable);
   149 		}
   150 	}
   151 	return (added ? 0 : -1);
   152 }
   153 
   154 /* Retrieve a variable named "name" from the environment */
   155 char *SDL_getenv(const char *name)
   156 {
   157 	int len, i;
   158 	char *value;
   159 
   160 	value = (char *)0;
   161 	if ( SDL_env ) {
   162 		len = SDL_strlen(name);
   163 		for ( i=0; SDL_env[i] && !value; ++i ) {
   164 			if ( (SDL_strncmp(SDL_env[i], name, len) == 0) &&
   165 			     (SDL_env[i][len] == '=') ) {
   166 				value = &SDL_env[i][len+1];
   167 			}
   168 		}
   169 	}
   170 	return value;
   171 }
   172 
   173 #endif /* __WIN32__ */
   174 
   175 #endif /* !HAVE_GETENV */
   176 
   177 #ifdef TEST_MAIN
   178 #include <stdio.h>
   179 
   180 int main(int argc, char *argv[])
   181 {
   182 	char *value;
   183 
   184 	printf("Checking for non-existent variable... ");
   185 	fflush(stdout);
   186 	if ( ! SDL_getenv("EXISTS") ) {
   187 		printf("okay\n");
   188 	} else {
   189 		printf("failed\n");
   190 	}
   191 	printf("Setting FIRST=VALUE1 in the environment... ");
   192 	fflush(stdout);
   193 	if ( SDL_putenv("FIRST=VALUE1") == 0 ) {
   194 		printf("okay\n");
   195 	} else {
   196 		printf("failed\n");
   197 	}
   198 	printf("Getting FIRST from the environment... ");
   199 	fflush(stdout);
   200 	value = SDL_getenv("FIRST");
   201 	if ( value && (SDL_strcmp(value, "VALUE1") == 0) ) {
   202 		printf("okay\n");
   203 	} else {
   204 		printf("failed\n");
   205 	}
   206 	printf("Setting SECOND=VALUE2 in the environment... ");
   207 	fflush(stdout);
   208 	if ( SDL_putenv("SECOND=VALUE2") == 0 ) {
   209 		printf("okay\n");
   210 	} else {
   211 		printf("failed\n");
   212 	}
   213 	printf("Getting SECOND from the environment... ");
   214 	fflush(stdout);
   215 	value = SDL_getenv("SECOND");
   216 	if ( value && (SDL_strcmp(value, "VALUE2") == 0) ) {
   217 		printf("okay\n");
   218 	} else {
   219 		printf("failed\n");
   220 	}
   221 	printf("Setting FIRST=NOVALUE in the environment... ");
   222 	fflush(stdout);
   223 	if ( SDL_putenv("FIRST=NOVALUE") == 0 ) {
   224 		printf("okay\n");
   225 	} else {
   226 		printf("failed\n");
   227 	}
   228 	printf("Getting FIRST from the environment... ");
   229 	fflush(stdout);
   230 	value = SDL_getenv("FIRST");
   231 	if ( value && (SDL_strcmp(value, "NOVALUE") == 0) ) {
   232 		printf("okay\n");
   233 	} else {
   234 		printf("failed\n");
   235 	}
   236 	printf("Checking for non-existent variable... ");
   237 	fflush(stdout);
   238 	if ( ! SDL_getenv("EXISTS") ) {
   239 		printf("okay\n");
   240 	} else {
   241 		printf("failed\n");
   242 	}
   243 	return(0);
   244 }
   245 #endif /* TEST_MAIN */
   246