SDL  2.0
SDL_getenv.c File Reference
#include "../SDL_internal.h"
#include "SDL_stdinc.h"
+ Include dependency graph for SDL_getenv.c:

Go to the source code of this file.

Functions

int SDL_setenv (const char *name, const char *value, int overwrite)
char * SDL_getenv (const char *name)

Variables

static char ** SDL_env = (char **) 0

Function Documentation

char* SDL_getenv ( const char *  name)

Definition at line 219 of file SDL_getenv.c.

References i, NULL, SDL_strlen, and SDL_strncmp.

{
int len, i;
char *value;
/* Input validation */
if (!name || SDL_strlen(name)==0) {
return NULL;
}
value = (char *) 0;
if (SDL_env) {
len = SDL_strlen(name);
for (i = 0; SDL_env[i] && !value; ++i) {
if ((SDL_strncmp(SDL_env[i], name, len) == 0) &&
(SDL_env[i][len] == '=')) {
value = &SDL_env[i][len + 1];
}
}
}
return value;
}
int SDL_setenv ( const char *  name,
const char *  value,
int  overwrite 
)

Definition at line 110 of file SDL_getenv.c.

References i, NULL, SDL_free, SDL_getenv, SDL_malloc, SDL_realloc, SDL_snprintf, SDL_strchr, SDL_strlen, and SDL_strncmp.

{
int added;
int len, i;
char **new_env;
char *new_variable;
/* Input validation */
if (!name || SDL_strlen(name) == 0 || SDL_strchr(name, '=') != NULL || !value) {
return (-1);
}
/* See if it already exists */
if (!overwrite && SDL_getenv(name)) {
return 0;
}
/* Allocate memory for the variable */
new_variable = (char *) SDL_malloc(len);
if (!new_variable) {
return (-1);
}
SDL_snprintf(new_variable, len, "%s=%s", name, value);
value = new_variable + SDL_strlen(name) + 1;
name = new_variable;
/* Actually put it into the environment */
added = 0;
i = 0;
if (SDL_env) {
/* Check to see if it's already there... */
len = (value - name);
for (; SDL_env[i]; ++i) {
if (SDL_strncmp(SDL_env[i], name, len) == 0) {
break;
}
}
/* If we found it, just replace the entry */
if (SDL_env[i]) {
SDL_env[i] = new_variable;
added = 1;
}
}
/* Didn't find it in the environment, expand and add */
if (!added) {
new_env = SDL_realloc(SDL_env, (i + 2) * sizeof(char *));
if (new_env) {
SDL_env = new_env;
SDL_env[i++] = new_variable;
SDL_env[i++] = (char *) 0;
added = 1;
} else {
SDL_free(new_variable);
}
}
return (added ? 0 : -1);
}

Variable Documentation

char** SDL_env = (char **) 0
static

Definition at line 108 of file SDL_getenv.c.