2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2004 Sam Lantinga
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.
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.
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
28 /* Not all environments have a working getenv()/putenv() */
31 #define NEED_SDL_GETENV
34 #include "SDL_getenv.h"
36 #ifdef NEED_SDL_GETENV
41 static char **SDL_env = (char **)0;
43 /* Put a variable of the form "name=value" into the environment */
44 int SDL_putenv(const char *variable)
46 const char *name, *value;
52 /* A little error checking */
57 for ( value=variable; *value && (*value != '='); ++value ) {
58 /* Keep looking for '=' */ ;
66 /* Allocate memory for the variable */
67 new_variable = (char *)malloc(strlen(variable)+1);
68 if ( ! new_variable ) {
71 strcpy(new_variable, variable);
73 /* Actually put it into the environment */
77 /* Check to see if it's already there... */
79 for ( ; SDL_env[i]; ++i ) {
80 if ( strncmp(SDL_env[i], name, len) == 0 ) {
84 /* If we found it, just replace the entry */
87 SDL_env[i] = new_variable;
92 /* Didn't find it in the environment, expand and add */
94 new_env = realloc(SDL_env, (i+2)*sizeof(char *));
97 SDL_env[i++] = new_variable;
98 SDL_env[i++] = (char *)0;
104 return (added ? 0 : -1);
107 /* Retrieve a variable named "name" from the environment */
108 char *SDL_getenv(const char *name)
116 for ( i=0; SDL_env[i] && !value; ++i ) {
117 if ( (strncmp(SDL_env[i], name, len) == 0) &&
118 (SDL_env[i][len] == '=') ) {
119 value = &SDL_env[i][len+1];
126 #endif /* NEED_GETENV */
131 int main(int argc, char *argv[])
135 printf("Checking for non-existent variable... ");
137 if ( ! getenv("EXISTS") ) {
142 printf("Setting FIRST=VALUE1 in the environment... ");
144 if ( putenv("FIRST=VALUE1") == 0 ) {
149 printf("Getting FIRST from the environment... ");
151 value = getenv("FIRST");
152 if ( value && (strcmp(value, "VALUE1") == 0) ) {
157 printf("Setting SECOND=VALUE2 in the environment... ");
159 if ( putenv("SECOND=VALUE2") == 0 ) {
164 printf("Getting SECOND from the environment... ");
166 value = getenv("SECOND");
167 if ( value && (strcmp(value, "VALUE2") == 0) ) {
172 printf("Setting FIRST=NOVALUE in the environment... ");
174 if ( putenv("FIRST=NOVALUE") == 0 ) {
179 printf("Getting FIRST from the environment... ");
181 value = getenv("FIRST");
182 if ( value && (strcmp(value, "NOVALUE") == 0) ) {
187 printf("Checking for non-existent variable... ");
189 if ( ! getenv("EXISTS") ) {
196 #endif /* TEST_MAIN */