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