src/SDL_getenv.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 16 Sep 2002 07:09:04 +0000
changeset 496 864a66f028d8
parent 297 f6ffac90895c
child 769 b8d311d90021
permissions -rw-r--r--
Don't be fooled by unclutter - the mouse isn't really leaving our window
slouken@249
     1
/*
slouken@249
     2
    SDL - Simple DirectMedia Layer
slouken@297
     3
    Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002  Sam Lantinga
slouken@249
     4
slouken@249
     5
    This library is free software; you can redistribute it and/or
slouken@249
     6
    modify it under the terms of the GNU Library General Public
slouken@249
     7
    License as published by the Free Software Foundation; either
slouken@249
     8
    version 2 of the License, or (at your option) any later version.
slouken@249
     9
slouken@249
    10
    This library is distributed in the hope that it will be useful,
slouken@249
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@249
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@249
    13
    Library General Public License for more details.
slouken@249
    14
slouken@249
    15
    You should have received a copy of the GNU Library General Public
slouken@249
    16
    License along with this library; if not, write to the Free
slouken@249
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@249
    18
slouken@249
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@249
    21
*/
slouken@249
    22
slouken@249
    23
#ifdef SAVE_RCSID
slouken@249
    24
static char rcsid =
slouken@249
    25
 "@(#) $Id$";
slouken@249
    26
#endif
slouken@0
    27
slouken@0
    28
/* Not all environments have a working getenv()/putenv() */
slouken@0
    29
slouken@0
    30
#ifdef TEST_MAIN
slouken@0
    31
#define NEED_SDL_GETENV
slouken@0
    32
#endif
slouken@0
    33
slouken@0
    34
#include "SDL_getenv.h"
slouken@0
    35
slouken@0
    36
#ifdef NEED_SDL_GETENV
slouken@0
    37
slouken@0
    38
#include <stdlib.h>
slouken@0
    39
#include <string.h>
slouken@0
    40
slouken@0
    41
static char **SDL_env = (char **)0;
slouken@0
    42
slouken@0
    43
/* Put a variable of the form "name=value" into the environment */
slouken@0
    44
int SDL_putenv(const char *variable)
slouken@0
    45
{
slouken@0
    46
	const char *name, *value;
slouken@0
    47
	int added;
slouken@0
    48
	int len, i;
slouken@0
    49
	char **new_env;
slouken@0
    50
	char *new_variable;
slouken@0
    51
slouken@0
    52
	/* A little error checking */
slouken@0
    53
	if ( ! variable ) {
slouken@0
    54
		return(-1);
slouken@0
    55
	}
slouken@0
    56
	name = variable;
slouken@0
    57
	for ( value=variable; *value && (*value != '='); ++value ) {
slouken@0
    58
		/* Keep looking for '=' */ ;
slouken@0
    59
	}
slouken@0
    60
	if ( *value ) {
slouken@0
    61
		++value;
slouken@0
    62
	} else {
slouken@0
    63
		return(-1);
slouken@0
    64
	}
slouken@0
    65
slouken@0
    66
	/* Allocate memory for the variable */
slouken@0
    67
	new_variable = (char *)malloc(strlen(variable)+1);
slouken@0
    68
	if ( ! new_variable ) {
slouken@0
    69
		return(-1);
slouken@0
    70
	}
slouken@0
    71
	strcpy(new_variable, variable);
slouken@0
    72
slouken@0
    73
	/* Actually put it into the environment */
slouken@0
    74
	added = 0;
slouken@0
    75
	i = 0;
slouken@0
    76
	if ( SDL_env ) {
slouken@0
    77
		/* Check to see if it's already there... */
slouken@0
    78
		len = (value - name);
slouken@0
    79
		for ( ; SDL_env[i]; ++i ) {
slouken@0
    80
			if ( strncmp(SDL_env[i], name, len) == 0 ) {
slouken@0
    81
				break;
slouken@0
    82
			}
slouken@0
    83
		}
slouken@0
    84
		/* If we found it, just replace the entry */
slouken@0
    85
		if ( SDL_env[i] ) {
slouken@0
    86
			free(SDL_env[i]);
slouken@0
    87
			SDL_env[i] = new_variable;
slouken@0
    88
			added = 1;
slouken@0
    89
		}
slouken@0
    90
	}
slouken@0
    91
slouken@0
    92
	/* Didn't find it in the environment, expand and add */
slouken@0
    93
	if ( ! added ) {
slouken@0
    94
		new_env = realloc(SDL_env, (i+2)*sizeof(char *));
slouken@0
    95
		if ( new_env ) {
slouken@0
    96
			SDL_env = new_env;
slouken@0
    97
			SDL_env[i++] = new_variable;
slouken@0
    98
			SDL_env[i++] = (char *)0;
slouken@0
    99
			added = 1;
slouken@0
   100
		} else {
slouken@0
   101
			free(new_variable);
slouken@0
   102
		}
slouken@0
   103
	}
slouken@0
   104
	return (added ? 0 : -1);
slouken@0
   105
}
slouken@0
   106
slouken@0
   107
/* Retrieve a variable named "name" from the environment */
slouken@0
   108
char *SDL_getenv(const char *name)
slouken@0
   109
{
slouken@0
   110
	int len, i;
slouken@0
   111
	char *value;
slouken@0
   112
slouken@0
   113
	value = (char *)0;
slouken@0
   114
	if ( SDL_env ) {
slouken@0
   115
		len = strlen(name);
slouken@0
   116
		for ( i=0; SDL_env[i] && !value; ++i ) {
slouken@0
   117
			if ( (strncmp(SDL_env[i], name, len) == 0) &&
slouken@0
   118
			     (SDL_env[i][len] == '=') ) {
slouken@0
   119
				value = &SDL_env[i][len+1];
slouken@0
   120
			}
slouken@0
   121
		}
slouken@0
   122
	}
slouken@0
   123
	return value;
slouken@0
   124
}
slouken@0
   125
slouken@0
   126
#endif /* NEED_GETENV */
slouken@0
   127
slouken@0
   128
#ifdef TEST_MAIN
slouken@0
   129
#include <stdio.h>
slouken@0
   130
slouken@0
   131
int main(int argc, char *argv[])
slouken@0
   132
{
slouken@0
   133
	char *value;
slouken@0
   134
slouken@0
   135
	printf("Checking for non-existent variable... ");
slouken@0
   136
	fflush(stdout);
slouken@0
   137
	if ( ! getenv("EXISTS") ) {
slouken@0
   138
		printf("okay\n");
slouken@0
   139
	} else {
slouken@0
   140
		printf("failed\n");
slouken@0
   141
	}
slouken@0
   142
	printf("Setting FIRST=VALUE1 in the environment... ");
slouken@0
   143
	fflush(stdout);
slouken@0
   144
	if ( putenv("FIRST=VALUE1") == 0 ) {
slouken@0
   145
		printf("okay\n");
slouken@0
   146
	} else {
slouken@0
   147
		printf("failed\n");
slouken@0
   148
	}
slouken@0
   149
	printf("Getting FIRST from the environment... ");
slouken@0
   150
	fflush(stdout);
slouken@0
   151
	value = getenv("FIRST");
slouken@0
   152
	if ( value && (strcmp(value, "VALUE1") == 0) ) {
slouken@0
   153
		printf("okay\n");
slouken@0
   154
	} else {
slouken@0
   155
		printf("failed\n");
slouken@0
   156
	}
slouken@0
   157
	printf("Setting SECOND=VALUE2 in the environment... ");
slouken@0
   158
	fflush(stdout);
slouken@0
   159
	if ( putenv("SECOND=VALUE2") == 0 ) {
slouken@0
   160
		printf("okay\n");
slouken@0
   161
	} else {
slouken@0
   162
		printf("failed\n");
slouken@0
   163
	}
slouken@0
   164
	printf("Getting SECOND from the environment... ");
slouken@0
   165
	fflush(stdout);
slouken@0
   166
	value = getenv("SECOND");
slouken@0
   167
	if ( value && (strcmp(value, "VALUE2") == 0) ) {
slouken@0
   168
		printf("okay\n");
slouken@0
   169
	} else {
slouken@0
   170
		printf("failed\n");
slouken@0
   171
	}
slouken@0
   172
	printf("Setting FIRST=NOVALUE in the environment... ");
slouken@0
   173
	fflush(stdout);
slouken@0
   174
	if ( putenv("FIRST=NOVALUE") == 0 ) {
slouken@0
   175
		printf("okay\n");
slouken@0
   176
	} else {
slouken@0
   177
		printf("failed\n");
slouken@0
   178
	}
slouken@0
   179
	printf("Getting FIRST from the environment... ");
slouken@0
   180
	fflush(stdout);
slouken@0
   181
	value = getenv("FIRST");
slouken@0
   182
	if ( value && (strcmp(value, "NOVALUE") == 0) ) {
slouken@0
   183
		printf("okay\n");
slouken@0
   184
	} else {
slouken@0
   185
		printf("failed\n");
slouken@0
   186
	}
slouken@0
   187
	printf("Checking for non-existent variable... ");
slouken@0
   188
	fflush(stdout);
slouken@0
   189
	if ( ! getenv("EXISTS") ) {
slouken@0
   190
		printf("okay\n");
slouken@0
   191
	} else {
slouken@0
   192
		printf("failed\n");
slouken@0
   193
	}
slouken@0
   194
	return(0);
slouken@0
   195
}
slouken@0
   196
#endif /* TEST_MAIN */
slouken@0
   197