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

§ SDL_getenv()

char* SDL_getenv ( const char *  name)

Definition at line 212 of file SDL_getenv.c.

References i, main, NULL, SDL_setenv(), SDL_strcmp, SDL_strlen, and SDL_strncmp.

Referenced by SDL_setenv().

213 {
214  int len, i;
215  char *value;
216 
217  /* Input validation */
218  if (!name || SDL_strlen(name)==0) {
219  return NULL;
220  }
221 
222  value = (char *) 0;
223  if (SDL_env) {
224  len = SDL_strlen(name);
225  for (i = 0; SDL_env[i] && !value; ++i) {
226  if ((SDL_strncmp(SDL_env[i], name, len) == 0) &&
227  (SDL_env[i][len] == '=')) {
228  value = &SDL_env[i][len + 1];
229  }
230  }
231  }
232  return value;
233 }
GLuint const GLchar * name
#define SDL_strncmp
static char ** SDL_env
Definition: SDL_getenv.c:106
GLenum GLsizei len
GLsizei const GLfloat * value
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
#define NULL
Definition: begin_code.h:143
#define SDL_strlen

§ SDL_setenv()

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

Definition at line 108 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.

Referenced by SDL_getenv().

109 {
110  int added;
111  int len, i;
112  char **new_env;
113  char *new_variable;
114 
115  /* Input validation */
116  if (!name || SDL_strlen(name) == 0 || SDL_strchr(name, '=') != NULL || !value) {
117  return (-1);
118  }
119 
120  /* See if it already exists */
121  if (!overwrite && SDL_getenv(name)) {
122  return 0;
123  }
124 
125  /* Allocate memory for the variable */
126  len = SDL_strlen(name) + SDL_strlen(value) + 2;
127  new_variable = (char *) SDL_malloc(len);
128  if (!new_variable) {
129  return (-1);
130  }
131 
132  SDL_snprintf(new_variable, len, "%s=%s", name, value);
133  value = new_variable + SDL_strlen(name) + 1;
134  name = new_variable;
135 
136  /* Actually put it into the environment */
137  added = 0;
138  i = 0;
139  if (SDL_env) {
140  /* Check to see if it's already there... */
141  len = (value - name);
142  for (; SDL_env[i]; ++i) {
143  if (SDL_strncmp(SDL_env[i], name, len) == 0) {
144  break;
145  }
146  }
147  /* If we found it, just replace the entry */
148  if (SDL_env[i]) {
149  SDL_free(SDL_env[i]);
150  SDL_env[i] = new_variable;
151  added = 1;
152  }
153  }
154 
155  /* Didn't find it in the environment, expand and add */
156  if (!added) {
157  new_env = SDL_realloc(SDL_env, (i + 2) * sizeof(char *));
158  if (new_env) {
159  SDL_env = new_env;
160  SDL_env[i++] = new_variable;
161  SDL_env[i++] = (char *) 0;
162  added = 1;
163  } else {
164  SDL_free(new_variable);
165  }
166  }
167  return (added ? 0 : -1);
168 }
char * SDL_getenv(const char *name)
Definition: SDL_getenv.c:212
GLuint const GLchar * name
#define SDL_realloc
#define SDL_strncmp
static char ** SDL_env
Definition: SDL_getenv.c:106
GLenum GLsizei len
#define SDL_strchr
GLsizei const GLfloat * value
void SDL_free(void *mem)
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
#define NULL
Definition: begin_code.h:143
#define SDL_strlen
#define SDL_snprintf
#define SDL_malloc

Variable Documentation

§ SDL_env

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

Definition at line 106 of file SDL_getenv.c.