annotate src/SDL_getenv.c @ 1299:2bf9dda618e5

Corrects dynamic X11 code on Tru64 systems. Fixes Bugzilla #87.
author Ryan C. Gordon <icculus@icculus.org>
date Mon, 30 Jan 2006 18:21:44 +0000
parents 05d5d36b71f4
children c9b51268668f
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
769
b8d311d90021 Updated copyright information for 2004 (Happy New Year!)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
3 Copyright (C) 1997-2004 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
e3d0d44f6f2e *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 0
diff changeset
6 modify it under the terms of the GNU Library General Public
e3d0d44f6f2e *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 0
diff changeset
7 License as published by the Free Software Foundation; either
e3d0d44f6f2e *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 0
diff changeset
8 version 2 of the License, or (at your option) any later version.
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
e3d0d44f6f2e *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 0
diff changeset
13 Library General Public License for more details.
e3d0d44f6f2e *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 0
diff changeset
14
e3d0d44f6f2e *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 0
diff changeset
15 You should have received a copy of the GNU Library General Public
e3d0d44f6f2e *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 0
diff changeset
16 License along with this library; if not, write to the Free
e3d0d44f6f2e *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 0
diff changeset
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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
e3d0d44f6f2e *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 0
diff changeset
23 #ifdef SAVE_RCSID
e3d0d44f6f2e *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 0
diff changeset
24 static char rcsid =
e3d0d44f6f2e *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 0
diff changeset
25 "@(#) $Id$";
e3d0d44f6f2e *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 0
diff changeset
26 #endif
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
27
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
28 /* Not all environments have a working getenv()/putenv() */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
29
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
30 #ifdef TEST_MAIN
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
31 #define NEED_SDL_GETENV
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
32 #endif
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
33
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
34 #include "SDL_getenv.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
35
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
36 #ifdef NEED_SDL_GETENV
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
37
1273
05d5d36b71f4 Who'd have thought that Windows CE wouldn't have the environment APIs?
Sam Lantinga <slouken@libsdl.org>
parents: 1268
diff changeset
38 #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
39
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
40 #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
41 #include <windows.h>
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
42 #include <malloc.h>
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
43 #include <string.h>
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 /* 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
46
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
47 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
48 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
49
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
50 /* 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
51 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
52 {
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
53 DWORD bufferlen;
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
54 char *value;
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
55 const char *sep;
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
56
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
57 sep = strchr(variable, '=');
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
58 if ( sep == NULL ) {
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
59 return -1;
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
60 }
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
61 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
62 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
63 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
64 if ( newmem == NULL ) {
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
65 return -1;
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
66 }
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
67 SDL_envmem = newmem;
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
68 SDL_envmemlen = bufferlen;
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
69 }
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
70 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
71 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
72 *value++ = '\0';
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
73 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
74 return -1;
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
75 }
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
76 return 0;
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
77 }
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 /* 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
80 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
81 {
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
82 DWORD bufferlen;
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
83
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
84 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
85 if ( bufferlen == 0 ) {
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 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
89 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
90 if ( newmem == NULL ) {
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
91 return NULL;
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
92 }
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
93 SDL_envmem = newmem;
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
94 SDL_envmemlen = bufferlen;
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
95 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
96 }
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
97 return SDL_envmem;
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
98 }
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
99
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
100 #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
101
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
102 #include <stdlib.h>
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
103 #include <string.h>
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
104
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
105 static char **SDL_env = (char **)0;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
106
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
107 /* Put a variable of the form "name=value" into the environment */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
108 int SDL_putenv(const char *variable)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
109 {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
110 const char *name, *value;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
111 int added;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
112 int len, i;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
113 char **new_env;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
114 char *new_variable;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
115
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
116 /* A little error checking */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
117 if ( ! variable ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
118 return(-1);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
119 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
120 name = variable;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
121 for ( value=variable; *value && (*value != '='); ++value ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
122 /* Keep looking for '=' */ ;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
123 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
124 if ( *value ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
125 ++value;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
126 } else {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
127 return(-1);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
128 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
129
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
130 /* Allocate memory for the variable */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
131 new_variable = (char *)malloc(strlen(variable)+1);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
132 if ( ! new_variable ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
133 return(-1);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
134 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
135 strcpy(new_variable, variable);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
136
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
137 /* Actually put it into the environment */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
138 added = 0;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
139 i = 0;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
140 if ( SDL_env ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
141 /* Check to see if it's already there... */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
142 len = (value - name);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
143 for ( ; SDL_env[i]; ++i ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
144 if ( strncmp(SDL_env[i], name, len) == 0 ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
145 break;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
146 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
147 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
148 /* If we found it, just replace the entry */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
149 if ( SDL_env[i] ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
150 free(SDL_env[i]);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
151 SDL_env[i] = new_variable;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
152 added = 1;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
153 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
154 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
155
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
156 /* Didn't find it in the environment, expand and add */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
157 if ( ! added ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
158 new_env = realloc(SDL_env, (i+2)*sizeof(char *));
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
159 if ( new_env ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
160 SDL_env = new_env;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
161 SDL_env[i++] = new_variable;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
162 SDL_env[i++] = (char *)0;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
163 added = 1;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
164 } else {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
165 free(new_variable);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
166 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
167 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
168 return (added ? 0 : -1);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
169 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
170
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
171 /* Retrieve a variable named "name" from the environment */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
172 char *SDL_getenv(const char *name)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
173 {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
174 int len, i;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
175 char *value;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
176
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
177 value = (char *)0;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
178 if ( SDL_env ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
179 len = strlen(name);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
180 for ( i=0; SDL_env[i] && !value; ++i ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
181 if ( (strncmp(SDL_env[i], name, len) == 0) &&
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
182 (SDL_env[i][len] == '=') ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
183 value = &SDL_env[i][len+1];
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
184 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
185 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
186 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
187 return value;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
188 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
189
1268
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
190 #endif /* WIN32 */
f098b247299d Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
191
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
192 #endif /* NEED_GETENV */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
193
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
194 #ifdef TEST_MAIN
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
195 #include <stdio.h>
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
196
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
197 int main(int argc, char *argv[])
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
198 {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
199 char *value;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
200
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
201 printf("Checking for non-existent variable... ");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
202 fflush(stdout);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
203 if ( ! getenv("EXISTS") ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
204 printf("okay\n");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
205 } else {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
206 printf("failed\n");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
207 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
208 printf("Setting FIRST=VALUE1 in the environment... ");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
209 fflush(stdout);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
210 if ( putenv("FIRST=VALUE1") == 0 ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
211 printf("okay\n");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
212 } else {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
213 printf("failed\n");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
214 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
215 printf("Getting FIRST from the environment... ");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
216 fflush(stdout);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
217 value = getenv("FIRST");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
218 if ( value && (strcmp(value, "VALUE1") == 0) ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
219 printf("okay\n");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
220 } else {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
221 printf("failed\n");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
222 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
223 printf("Setting SECOND=VALUE2 in the environment... ");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
224 fflush(stdout);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
225 if ( putenv("SECOND=VALUE2") == 0 ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
226 printf("okay\n");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
227 } else {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
228 printf("failed\n");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
229 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
230 printf("Getting SECOND from the environment... ");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
231 fflush(stdout);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
232 value = getenv("SECOND");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
233 if ( value && (strcmp(value, "VALUE2") == 0) ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
234 printf("okay\n");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
235 } else {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
236 printf("failed\n");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
237 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
238 printf("Setting FIRST=NOVALUE in the environment... ");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
239 fflush(stdout);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
240 if ( putenv("FIRST=NOVALUE") == 0 ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
241 printf("okay\n");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
242 } else {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
243 printf("failed\n");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
244 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
245 printf("Getting FIRST from the environment... ");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
246 fflush(stdout);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
247 value = getenv("FIRST");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
248 if ( value && (strcmp(value, "NOVALUE") == 0) ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
249 printf("okay\n");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
250 } else {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
251 printf("failed\n");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
252 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
253 printf("Checking for non-existent variable... ");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
254 fflush(stdout);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
255 if ( ! getenv("EXISTS") ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
256 printf("okay\n");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
257 } else {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
258 printf("failed\n");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
259 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
260 return(0);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
261 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
262 #endif /* TEST_MAIN */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
263