Mercurial > sdl-ios-xcode
annotate src/SDL_getenv.c @ 1204:f9794ee91dfc
Date: Tue, 13 Dec 2005 13:33:50 +0000
From: "alan buckley"
Subject: Patch to fix audio locking on RISC OS
When threads were not disabled on a RISC OS build
the audio mixer mutex was not created.
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Tue, 13 Dec 2005 16:40:12 +0000 |
parents | b8d311d90021 |
children | f098b247299d |
rev | line source |
---|---|
249 | 1 /* |
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 | 4 |
5 This library is free software; you can redistribute it and/or | |
6 modify it under the terms of the GNU Library General Public | |
7 License as published by the Free Software Foundation; either | |
8 version 2 of the License, or (at your option) any later version. | |
9 | |
10 This library is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 Library General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU Library General Public | |
16 License along with this library; if not, write to the Free | |
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | |
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 | 21 */ |
22 | |
23 #ifdef SAVE_RCSID | |
24 static char rcsid = | |
25 "@(#) $Id$"; | |
26 #endif | |
0 | 27 |
28 /* Not all environments have a working getenv()/putenv() */ | |
29 | |
30 #ifdef TEST_MAIN | |
31 #define NEED_SDL_GETENV | |
32 #endif | |
33 | |
34 #include "SDL_getenv.h" | |
35 | |
36 #ifdef NEED_SDL_GETENV | |
37 | |
38 #include <stdlib.h> | |
39 #include <string.h> | |
40 | |
41 static char **SDL_env = (char **)0; | |
42 | |
43 /* Put a variable of the form "name=value" into the environment */ | |
44 int SDL_putenv(const char *variable) | |
45 { | |
46 const char *name, *value; | |
47 int added; | |
48 int len, i; | |
49 char **new_env; | |
50 char *new_variable; | |
51 | |
52 /* A little error checking */ | |
53 if ( ! variable ) { | |
54 return(-1); | |
55 } | |
56 name = variable; | |
57 for ( value=variable; *value && (*value != '='); ++value ) { | |
58 /* Keep looking for '=' */ ; | |
59 } | |
60 if ( *value ) { | |
61 ++value; | |
62 } else { | |
63 return(-1); | |
64 } | |
65 | |
66 /* Allocate memory for the variable */ | |
67 new_variable = (char *)malloc(strlen(variable)+1); | |
68 if ( ! new_variable ) { | |
69 return(-1); | |
70 } | |
71 strcpy(new_variable, variable); | |
72 | |
73 /* Actually put it into the environment */ | |
74 added = 0; | |
75 i = 0; | |
76 if ( SDL_env ) { | |
77 /* Check to see if it's already there... */ | |
78 len = (value - name); | |
79 for ( ; SDL_env[i]; ++i ) { | |
80 if ( strncmp(SDL_env[i], name, len) == 0 ) { | |
81 break; | |
82 } | |
83 } | |
84 /* If we found it, just replace the entry */ | |
85 if ( SDL_env[i] ) { | |
86 free(SDL_env[i]); | |
87 SDL_env[i] = new_variable; | |
88 added = 1; | |
89 } | |
90 } | |
91 | |
92 /* Didn't find it in the environment, expand and add */ | |
93 if ( ! added ) { | |
94 new_env = realloc(SDL_env, (i+2)*sizeof(char *)); | |
95 if ( new_env ) { | |
96 SDL_env = new_env; | |
97 SDL_env[i++] = new_variable; | |
98 SDL_env[i++] = (char *)0; | |
99 added = 1; | |
100 } else { | |
101 free(new_variable); | |
102 } | |
103 } | |
104 return (added ? 0 : -1); | |
105 } | |
106 | |
107 /* Retrieve a variable named "name" from the environment */ | |
108 char *SDL_getenv(const char *name) | |
109 { | |
110 int len, i; | |
111 char *value; | |
112 | |
113 value = (char *)0; | |
114 if ( SDL_env ) { | |
115 len = strlen(name); | |
116 for ( i=0; SDL_env[i] && !value; ++i ) { | |
117 if ( (strncmp(SDL_env[i], name, len) == 0) && | |
118 (SDL_env[i][len] == '=') ) { | |
119 value = &SDL_env[i][len+1]; | |
120 } | |
121 } | |
122 } | |
123 return value; | |
124 } | |
125 | |
126 #endif /* NEED_GETENV */ | |
127 | |
128 #ifdef TEST_MAIN | |
129 #include <stdio.h> | |
130 | |
131 int main(int argc, char *argv[]) | |
132 { | |
133 char *value; | |
134 | |
135 printf("Checking for non-existent variable... "); | |
136 fflush(stdout); | |
137 if ( ! getenv("EXISTS") ) { | |
138 printf("okay\n"); | |
139 } else { | |
140 printf("failed\n"); | |
141 } | |
142 printf("Setting FIRST=VALUE1 in the environment... "); | |
143 fflush(stdout); | |
144 if ( putenv("FIRST=VALUE1") == 0 ) { | |
145 printf("okay\n"); | |
146 } else { | |
147 printf("failed\n"); | |
148 } | |
149 printf("Getting FIRST from the environment... "); | |
150 fflush(stdout); | |
151 value = getenv("FIRST"); | |
152 if ( value && (strcmp(value, "VALUE1") == 0) ) { | |
153 printf("okay\n"); | |
154 } else { | |
155 printf("failed\n"); | |
156 } | |
157 printf("Setting SECOND=VALUE2 in the environment... "); | |
158 fflush(stdout); | |
159 if ( putenv("SECOND=VALUE2") == 0 ) { | |
160 printf("okay\n"); | |
161 } else { | |
162 printf("failed\n"); | |
163 } | |
164 printf("Getting SECOND from the environment... "); | |
165 fflush(stdout); | |
166 value = getenv("SECOND"); | |
167 if ( value && (strcmp(value, "VALUE2") == 0) ) { | |
168 printf("okay\n"); | |
169 } else { | |
170 printf("failed\n"); | |
171 } | |
172 printf("Setting FIRST=NOVALUE in the environment... "); | |
173 fflush(stdout); | |
174 if ( putenv("FIRST=NOVALUE") == 0 ) { | |
175 printf("okay\n"); | |
176 } else { | |
177 printf("failed\n"); | |
178 } | |
179 printf("Getting FIRST from the environment... "); | |
180 fflush(stdout); | |
181 value = getenv("FIRST"); | |
182 if ( value && (strcmp(value, "NOVALUE") == 0) ) { | |
183 printf("okay\n"); | |
184 } else { | |
185 printf("failed\n"); | |
186 } | |
187 printf("Checking for non-existent variable... "); | |
188 fflush(stdout); | |
189 if ( ! getenv("EXISTS") ) { | |
190 printf("okay\n"); | |
191 } else { | |
192 printf("failed\n"); | |
193 } | |
194 return(0); | |
195 } | |
196 #endif /* TEST_MAIN */ | |
197 |