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

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

Referenced by SDL_setenv().

220 {
221  int len, i;
222  char *value;
223 
224  /* Input validation */
225  if (!name || SDL_strlen(name)==0) {
226  return NULL;
227  }
228 
229  value = (char *) 0;
230  if (SDL_env) {
231  len = SDL_strlen(name);
232  for (i = 0; SDL_env[i] && !value; ++i) {
233  if ((SDL_strncmp(SDL_env[i], name, len) == 0) &&
234  (SDL_env[i][len] == '=')) {
235  value = &SDL_env[i][len + 1];
236  }
237  }
238  }
239  return value;
240 }
#define SDL_strncmp
static char ** SDL_env
Definition: SDL_getenv.c:108
GLenum GLsizei len
GLuint const GLchar * name
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:164
#define SDL_strlen

◆ SDL_setenv()

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

Definition at line 110 of file SDL_getenv.c.

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

Referenced by SDL_getenv().

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

Variable Documentation

◆ SDL_env

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

Definition at line 108 of file SDL_getenv.c.