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 210 of file SDL_getenv.c.

References i, NULL, SDL_strlen, and SDL_strncmp.

Referenced by SDL_setenv().

211 {
212  int len, i;
213  char *value;
214 
215  /* Input validation */
216  if (!name || SDL_strlen(name)==0) {
217  return NULL;
218  }
219 
220  value = (char *) 0;
221  if (SDL_env) {
222  len = SDL_strlen(name);
223  for (i = 0; SDL_env[i] && !value; ++i) {
224  if ((SDL_strncmp(SDL_env[i], name, len) == 0) &&
225  (SDL_env[i][len] == '=')) {
226  value = &SDL_env[i][len + 1];
227  }
228  }
229  }
230  return value;
231 }
#define SDL_strncmp
static char ** SDL_env
Definition: SDL_getenv.c:104
GLenum GLsizei len
GLuint const GLchar * name
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
GLsizei const GLfloat * value
#define NULL
Definition: begin_code.h:164
#define SDL_strlen
int SDL_setenv ( const char *  name,
const char *  value,
int  overwrite 
)

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

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

Variable Documentation

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

Definition at line 104 of file SDL_getenv.c.