Rev 6347 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
5131 | clevermous | 1 | |
2 | |||
3 | |||
4 | #define NEED_SDL_GETENV |
||
5 | #endif |
||
6 | |||
7 | |||
8 | |||
9 | |||
10 | |||
11 | |||
12 | #include |
||
13 | |||
14 | |||
15 | |||
16 | |||
17 | int SDL_putenv(const char *variable) |
||
18 | { |
||
19 | const char *name, *value; |
||
20 | int added; |
||
21 | int len, i; |
||
22 | char **new_env; |
||
23 | char *new_variable; |
||
24 | |||
25 | |||
26 | if ( ! variable ) { |
||
27 | return(-1); |
||
28 | } |
||
29 | name = variable; |
||
30 | for ( value=variable; *value && (*value != '='); ++value ) { |
||
31 | /* Keep looking for '=' */ ; |
||
32 | } |
||
33 | if ( *value ) { |
||
34 | ++value; |
||
35 | } else { |
||
36 | return(-1); |
||
37 | } |
||
38 | |||
39 | |||
40 | new_variable = (char *)malloc(strlen(variable)+1); |
||
41 | if ( ! new_variable ) { |
||
42 | return(-1); |
||
43 | } |
||
44 | strcpy(new_variable, variable); |
||
45 | |||
46 | |||
47 | added = 0; |
||
48 | i = 0; |
||
49 | if ( SDL_env ) { |
||
50 | /* Check to see if it's already there... */ |
||
51 | len = (value - name); |
||
52 | for ( ; SDL_env[i]; ++i ) { |
||
53 | if ( strncmp(SDL_env[i], name, len) == 0 ) { |
||
54 | break; |
||
55 | } |
||
56 | } |
||
57 | /* If we found it, just replace the entry */ |
||
58 | if ( SDL_env[i] ) { |
||
59 | free(SDL_env[i]); |
||
60 | SDL_env[i] = new_variable; |
||
61 | added = 1; |
||
62 | } |
||
63 | } |
||
64 | |||
65 | |||
66 | if ( ! added ) { |
||
67 | new_env = realloc(SDL_env, (i+2)*sizeof(char *)); |
||
68 | if ( new_env ) { |
||
69 | SDL_env = new_env; |
||
70 | SDL_env[i++] = new_variable; |
||
71 | SDL_env[i++] = (char *)0; |
||
72 | added = 1; |
||
73 | } else { |
||
74 | free(new_variable); |
||
75 | } |
||
76 | } |
||
77 | return (added ? 0 : -1); |
||
78 | } |
||
79 | |||
80 | |||
81 | char *SDL_getenv(const char *name) |
||
82 | { |
||
83 | int len, i; |
||
84 | char *value; |
||
85 | |||
86 | |||
87 | if ( SDL_env ) { |
||
88 | len = strlen(name); |
||
89 | for ( i=0; SDL_env[i] && !value; ++i ) { |
||
90 | if ( (strncmp(SDL_env[i], name, len) == 0) && |
||
91 | (SDL_env[i][len] == '=') ) { |
||
92 | value = &SDL_env[i][len+1]; |
||
93 | } |
||
94 | } |
||
95 | } |
||
96 | return value; |
||
97 | } |
||
98 | |||
99 | |||
100 | |||
101 | |||
102 | #include |
||
103 | |||
104 | |||
105 | { |
||
106 | char *value; |
||
107 | |||
108 | |||
109 | fflush(stdout); |
||
110 | if ( ! getenv("EXISTS") ) { |
||
111 | printf("okay\n"); |
||
112 | } else { |
||
113 | printf("failed\n"); |
||
114 | } |
||
115 | printf("Setting FIRST=VALUE1 in the environment... "); |
||
116 | fflush(stdout); |
||
117 | if ( putenv("FIRST=VALUE1") == 0 ) { |
||
6386 | ashmew2 | 118 | printf("okay\n"); |
5131 | clevermous | 119 | } else { |
120 | printf("failed\n"); |
||
121 | } |
||
122 | printf("Getting FIRST from the environment... "); |
||
123 | fflush(stdout); |
||
124 | value = getenv("FIRST"); |
||
125 | if ( value && (strcmp(value, "VALUE1") == 0) ) { |
||
126 | printf("okay\n"); |
||
127 | } else { |
||
128 | printf("failed\n"); |
||
129 | } |
||
130 | printf("Setting SECOND=VALUE2 in the environment... "); |
||
131 | fflush(stdout); |
||
132 | if ( putenv("SECOND=VALUE2") == 0 ) { |
||
6386 | ashmew2 | 133 | printf("okay\n"); |
5131 | clevermous | 134 | } else { |
135 | printf("failed\n"); |
||
136 | } |
||
137 | printf("Getting SECOND from the environment... "); |
||
138 | fflush(stdout); |
||
139 | value = getenv("SECOND"); |
||
140 | if ( value && (strcmp(value, "VALUE2") == 0) ) { |
||
141 | printf("okay\n"); |
||
142 | } else { |
||
143 | printf("failed\n"); |
||
144 | } |
||
145 | printf("Setting FIRST=NOVALUE in the environment... "); |
||
146 | fflush(stdout); |
||
147 | if ( putenv("FIRST=NOVALUE") == 0 ) { |
||
6386 | ashmew2 | 148 | printf("okay\n"); |
5131 | clevermous | 149 | } else { |
150 | printf("failed\n"); |
||
151 | } |
||
152 | printf("Getting FIRST from the environment... "); |
||
153 | fflush(stdout); |
||
154 | value = getenv("FIRST"); |
||
155 | if ( value && (strcmp(value, "NOVALUE") == 0) ) { |
||
156 | printf("okay\n"); |
||
157 | } else { |
||
158 | printf("failed\n"); |
||
159 | } |
||
160 | printf("Checking for non-existent variable... "); |
||
161 | fflush(stdout); |
||
162 | if ( ! getenv("EXISTS") ) { |
||
163 | printf("okay\n"); |
||
164 | } else { |
||
165 | printf("failed\n"); |
||
166 | } |
||
167 | return(0); |
||
168 | } |
||
169 | #endif /* TEST_MAIN */ |
||
170 |