comparison src/SDL_getenv.c @ 0:74212992fb08

Initial revision
author Sam Lantinga <slouken@lokigames.com>
date Thu, 26 Apr 2001 16:45:43 +0000
parents
children e3d0d44f6f2e
comparison
equal deleted inserted replaced
-1:000000000000 0:74212992fb08
1
2 /* Not all environments have a working getenv()/putenv() */
3
4 #ifdef TEST_MAIN
5 #define NEED_SDL_GETENV
6 #endif
7
8 #include "SDL_getenv.h"
9
10 #ifdef NEED_SDL_GETENV
11
12 #include <stdlib.h>
13 #include <string.h>
14
15 static char **SDL_env = (char **)0;
16
17 /* Put a variable of the form "name=value" into the environment */
18 int SDL_putenv(const char *variable)
19 {
20 const char *name, *value;
21 int added;
22 int len, i;
23 char **new_env;
24 char *new_variable;
25
26 /* A little error checking */
27 if ( ! variable ) {
28 return(-1);
29 }
30 name = variable;
31 for ( value=variable; *value && (*value != '='); ++value ) {
32 /* Keep looking for '=' */ ;
33 }
34 if ( *value ) {
35 ++value;
36 } else {
37 return(-1);
38 }
39
40 /* Allocate memory for the variable */
41 new_variable = (char *)malloc(strlen(variable)+1);
42 if ( ! new_variable ) {
43 return(-1);
44 }
45 strcpy(new_variable, variable);
46
47 /* Actually put it into the environment */
48 added = 0;
49 i = 0;
50 if ( SDL_env ) {
51 /* Check to see if it's already there... */
52 len = (value - name);
53 for ( ; SDL_env[i]; ++i ) {
54 if ( strncmp(SDL_env[i], name, len) == 0 ) {
55 break;
56 }
57 }
58 /* If we found it, just replace the entry */
59 if ( SDL_env[i] ) {
60 free(SDL_env[i]);
61 SDL_env[i] = new_variable;
62 added = 1;
63 }
64 }
65
66 /* Didn't find it in the environment, expand and add */
67 if ( ! added ) {
68 new_env = realloc(SDL_env, (i+2)*sizeof(char *));
69 if ( new_env ) {
70 SDL_env = new_env;
71 SDL_env[i++] = new_variable;
72 SDL_env[i++] = (char *)0;
73 added = 1;
74 } else {
75 free(new_variable);
76 }
77 }
78 return (added ? 0 : -1);
79 }
80
81 /* Retrieve a variable named "name" from the environment */
82 char *SDL_getenv(const char *name)
83 {
84 int len, i;
85 char *value;
86
87 value = (char *)0;
88 if ( SDL_env ) {
89 len = strlen(name);
90 for ( i=0; SDL_env[i] && !value; ++i ) {
91 if ( (strncmp(SDL_env[i], name, len) == 0) &&
92 (SDL_env[i][len] == '=') ) {
93 value = &SDL_env[i][len+1];
94 }
95 }
96 }
97 return value;
98 }
99
100 #endif /* NEED_GETENV */
101
102 #ifdef TEST_MAIN
103 #include <stdio.h>
104
105 int main(int argc, char *argv[])
106 {
107 char *value;
108
109 printf("Checking for non-existent variable... ");
110 fflush(stdout);
111 if ( ! getenv("EXISTS") ) {
112 printf("okay\n");
113 } else {
114 printf("failed\n");
115 }
116 printf("Setting FIRST=VALUE1 in the environment... ");
117 fflush(stdout);
118 if ( putenv("FIRST=VALUE1") == 0 ) {
119 printf("okay\n");
120 } else {
121 printf("failed\n");
122 }
123 printf("Getting FIRST from the environment... ");
124 fflush(stdout);
125 value = getenv("FIRST");
126 if ( value && (strcmp(value, "VALUE1") == 0) ) {
127 printf("okay\n");
128 } else {
129 printf("failed\n");
130 }
131 printf("Setting SECOND=VALUE2 in the environment... ");
132 fflush(stdout);
133 if ( putenv("SECOND=VALUE2") == 0 ) {
134 printf("okay\n");
135 } else {
136 printf("failed\n");
137 }
138 printf("Getting SECOND from the environment... ");
139 fflush(stdout);
140 value = getenv("SECOND");
141 if ( value && (strcmp(value, "VALUE2") == 0) ) {
142 printf("okay\n");
143 } else {
144 printf("failed\n");
145 }
146 printf("Setting FIRST=NOVALUE in the environment... ");
147 fflush(stdout);
148 if ( putenv("FIRST=NOVALUE") == 0 ) {
149 printf("okay\n");
150 } else {
151 printf("failed\n");
152 }
153 printf("Getting FIRST from the environment... ");
154 fflush(stdout);
155 value = getenv("FIRST");
156 if ( value && (strcmp(value, "NOVALUE") == 0) ) {
157 printf("okay\n");
158 } else {
159 printf("failed\n");
160 }
161 printf("Checking for non-existent variable... ");
162 fflush(stdout);
163 if ( ! getenv("EXISTS") ) {
164 printf("okay\n");
165 } else {
166 printf("failed\n");
167 }
168 return(0);
169 }
170 #endif /* TEST_MAIN */
171