annotate src/SDL_getenv.c @ 1317:6c7b69218276

Updated exports
author Sam Lantinga <slouken@libsdl.org>
date Wed, 01 Feb 2006 09:01:12 +0000
parents c9b51268668f
children
rev   line source
249
e3d0d44f6f2e *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 0
diff changeset
1 /*
e3d0d44f6f2e *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 0
diff changeset
2 SDL - Simple DirectMedia Layer
1312
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1273
diff changeset
3 Copyright (C) 1997-2006 Sam Lantinga
249
e3d0d44f6f2e *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 0
diff changeset
4
e3d0d44f6f2e *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 0
diff changeset
5 This library is free software; you can redistribute it and/or
1312
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1273
diff changeset
6 modify it under the terms of the GNU Lesser General Public
249
e3d0d44f6f2e *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 0
diff changeset
7 License as published by the Free Software Foundation; either
1312
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1273
diff changeset
8 version 2.1 of the License, or (at your option) any later version.
249
e3d0d44f6f2e *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 0
diff changeset
9
e3d0d44f6f2e *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 0
diff changeset
10 This library is distributed in the hope that it will be useful,
e3d0d44f6f2e *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 0
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
e3d0d44f6f2e *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 0
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1312
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1273
diff changeset
13 Lesser General Public License for more details.
249
e3d0d44f6f2e *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 0
diff changeset
14
1312
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1273
diff changeset
15 You should have received a copy of the GNU Lesser General Public
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1273
diff changeset
16 License along with this library; if not, write to the Free Software
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1273
diff changeset
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
249
e3d0d44f6f2e *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 0
diff changeset
18
e3d0d44f6f2e *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 0
diff changeset
19 Sam Lantinga
252
e8157fcb3114 Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents: 249
diff changeset
20 slouken@libsdl.org
249
e3d0d44f6f2e *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 0
diff changeset
21 */
e3d0d44f6f2e *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 0
diff changeset
22
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
23 /* Not all environments have a working getenv()/putenv() */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
24
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
25 #ifdef TEST_MAIN
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
26 #define NEED_SDL_GETENV
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
27 #endif
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
28
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
29 #include "SDL_getenv.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
30
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
31 #ifdef NEED_SDL_GETENV
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
32
1273
05d5d36b71f4 Who'd have thought that Windows CE wouldn't have the environment APIs?
Sam Lantinga <slouken@libsdl.org>
parents: 1268
diff changeset
33 #if defined(WIN32) && !defined(_WIN32_WCE)
1268
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
34
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
35 #define WIN32_LEAN_AND_MEAN
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
36 #include <windows.h>
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
37 #include <malloc.h>
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
38 #include <string.h>
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
39
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
40 /* Note this isn't thread-safe! */
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
41
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
42 static char *SDL_envmem = NULL; /* Ugh, memory leak */
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
43 static DWORD SDL_envmemlen = 0;
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
44
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
45 /* Put a variable of the form "name=value" into the environment */
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
46 int SDL_putenv(const char *variable)
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
47 {
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
48 DWORD bufferlen;
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
49 char *value;
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
50 const char *sep;
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
51
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
52 sep = strchr(variable, '=');
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
53 if ( sep == NULL ) {
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
54 return -1;
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
55 }
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
56 bufferlen = strlen(variable)+1;
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
57 if ( bufferlen > SDL_envmemlen ) {
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
58 char *newmem = (char *)realloc(SDL_envmem, bufferlen);
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
59 if ( newmem == NULL ) {
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
60 return -1;
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
61 }
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
62 SDL_envmem = newmem;
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
63 SDL_envmemlen = bufferlen;
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
64 }
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
65 strcpy(SDL_envmem, variable);
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
66 value = SDL_envmem + (sep - variable);
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
67 *value++ = '\0';
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
68 if ( !SetEnvironmentVariable(SDL_envmem, *value ? value : NULL) ) {
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
69 return -1;
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
70 }
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
71 return 0;
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
72 }
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
73
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
74 /* Retrieve a variable named "name" from the environment */
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
75 char *SDL_getenv(const char *name)
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
76 {
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
77 DWORD bufferlen;
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
78
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
79 bufferlen = GetEnvironmentVariable(name, SDL_envmem, SDL_envmemlen);
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
80 if ( bufferlen == 0 ) {
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
81 return NULL;
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
82 }
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
83 if ( bufferlen > SDL_envmemlen ) {
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
84 char *newmem = (char *)realloc(SDL_envmem, bufferlen);
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
85 if ( newmem == NULL ) {
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
86 return NULL;
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
87 }
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
88 SDL_envmem = newmem;
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
89 SDL_envmemlen = bufferlen;
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
90 GetEnvironmentVariable(name, SDL_envmem, SDL_envmemlen);
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
91 }
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
92 return SDL_envmem;
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
93 }
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
94
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
95 #else /* roll our own */
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
96
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
97 #include <stdlib.h>
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
98 #include <string.h>
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
99
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
100 static char **SDL_env = (char **)0;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
101
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
102 /* Put a variable of the form "name=value" into the environment */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
103 int SDL_putenv(const char *variable)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
104 {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
105 const char *name, *value;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
106 int added;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
107 int len, i;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
108 char **new_env;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
109 char *new_variable;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
110
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
111 /* A little error checking */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
112 if ( ! variable ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
113 return(-1);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
114 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
115 name = variable;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
116 for ( value=variable; *value && (*value != '='); ++value ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
117 /* Keep looking for '=' */ ;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
118 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
119 if ( *value ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
120 ++value;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
121 } else {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
122 return(-1);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
123 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
124
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
125 /* Allocate memory for the variable */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
126 new_variable = (char *)malloc(strlen(variable)+1);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
127 if ( ! new_variable ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
128 return(-1);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
129 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
130 strcpy(new_variable, variable);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
131
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
132 /* Actually put it into the environment */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
133 added = 0;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
134 i = 0;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
135 if ( SDL_env ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
136 /* Check to see if it's already there... */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
137 len = (value - name);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
138 for ( ; SDL_env[i]; ++i ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
139 if ( strncmp(SDL_env[i], name, len) == 0 ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
140 break;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
141 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
142 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
143 /* If we found it, just replace the entry */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
144 if ( SDL_env[i] ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
145 free(SDL_env[i]);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
146 SDL_env[i] = new_variable;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
147 added = 1;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
148 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
149 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
150
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
151 /* Didn't find it in the environment, expand and add */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
152 if ( ! added ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
153 new_env = realloc(SDL_env, (i+2)*sizeof(char *));
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
154 if ( new_env ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
155 SDL_env = new_env;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
156 SDL_env[i++] = new_variable;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
157 SDL_env[i++] = (char *)0;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
158 added = 1;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
159 } else {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
160 free(new_variable);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
161 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
162 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
163 return (added ? 0 : -1);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
164 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
165
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
166 /* Retrieve a variable named "name" from the environment */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
167 char *SDL_getenv(const char *name)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
168 {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
169 int len, i;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
170 char *value;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
171
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
172 value = (char *)0;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
173 if ( SDL_env ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
174 len = strlen(name);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
175 for ( i=0; SDL_env[i] && !value; ++i ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
176 if ( (strncmp(SDL_env[i], name, len) == 0) &&
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
177 (SDL_env[i][len] == '=') ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
178 value = &SDL_env[i][len+1];
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
179 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
180 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
181 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
182 return value;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
183 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
184
1268
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
185 #endif /* WIN32 */
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
186
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
187 #endif /* NEED_GETENV */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
188
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
189 #ifdef TEST_MAIN
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
190 #include <stdio.h>
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
191
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
192 int main(int argc, char *argv[])
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
193 {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
194 char *value;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
195
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
196 printf("Checking for non-existent variable... ");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
197 fflush(stdout);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
198 if ( ! getenv("EXISTS") ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
199 printf("okay\n");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
200 } else {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
201 printf("failed\n");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
202 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
203 printf("Setting FIRST=VALUE1 in the environment... ");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
204 fflush(stdout);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
205 if ( putenv("FIRST=VALUE1") == 0 ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
206 printf("okay\n");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
207 } else {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
208 printf("failed\n");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
209 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
210 printf("Getting FIRST from the environment... ");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
211 fflush(stdout);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
212 value = getenv("FIRST");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
213 if ( value && (strcmp(value, "VALUE1") == 0) ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
214 printf("okay\n");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
215 } else {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
216 printf("failed\n");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
217 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
218 printf("Setting SECOND=VALUE2 in the environment... ");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
219 fflush(stdout);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
220 if ( putenv("SECOND=VALUE2") == 0 ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
221 printf("okay\n");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
222 } else {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
223 printf("failed\n");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
224 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
225 printf("Getting SECOND from the environment... ");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
226 fflush(stdout);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
227 value = getenv("SECOND");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
228 if ( value && (strcmp(value, "VALUE2") == 0) ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
229 printf("okay\n");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
230 } else {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
231 printf("failed\n");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
232 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
233 printf("Setting FIRST=NOVALUE in the environment... ");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
234 fflush(stdout);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
235 if ( putenv("FIRST=NOVALUE") == 0 ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
236 printf("okay\n");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
237 } else {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
238 printf("failed\n");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
239 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
240 printf("Getting FIRST from the environment... ");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
241 fflush(stdout);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
242 value = getenv("FIRST");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
243 if ( value && (strcmp(value, "NOVALUE") == 0) ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
244 printf("okay\n");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
245 } else {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
246 printf("failed\n");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
247 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
248 printf("Checking for non-existent variable... ");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
249 fflush(stdout);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
250 if ( ! getenv("EXISTS") ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
251 printf("okay\n");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
252 } else {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
253 printf("failed\n");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
254 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
255 return(0);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
256 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
257 #endif /* TEST_MAIN */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
258