1 | /* $Id: environ.cpp,v 1.9 2000-10-08 14:01:01 sandervl Exp $ */
|
---|
2 |
|
---|
3 | /*
|
---|
4 | * Win32 environment file functions for OS/2
|
---|
5 | *
|
---|
6 | * Copyright 1998 Sander van Leeuwen
|
---|
7 | * Copyright 1998 Patrick Haller
|
---|
8 | * Copyright 1998 Peter Fitzsimmons
|
---|
9 | * Copyright 1998 Knut St. Osmundsen
|
---|
10 | *
|
---|
11 | * Parts based on Wine code (ExpandEnvironmentStringsA/W)
|
---|
12 | * (memory\environ.c; 991114)
|
---|
13 | *
|
---|
14 | * Copyright 1996, 1998 Alexandre Julliard
|
---|
15 | *
|
---|
16 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
17 | *
|
---|
18 | */
|
---|
19 | #include <odin.h>
|
---|
20 | #include <odinwrap.h>
|
---|
21 | #include <os2sel.h>
|
---|
22 |
|
---|
23 | #include <os2win.h>
|
---|
24 | #include <winnt.h>
|
---|
25 | #include <winnls.h>
|
---|
26 | #include <stdlib.h>
|
---|
27 | #include <string.h>
|
---|
28 | #include <heapstring.h>
|
---|
29 |
|
---|
30 | #include <misc.h>
|
---|
31 |
|
---|
32 | #define DBG_LOCALLOG DBG_environ
|
---|
33 | #include "dbglocal.h"
|
---|
34 |
|
---|
35 |
|
---|
36 | ODINDEBUGCHANNEL(KERNEL32-ENVIRONMENT)
|
---|
37 |
|
---|
38 |
|
---|
39 | //******************************************************************************
|
---|
40 | //******************************************************************************
|
---|
41 | ODINFUNCTION0(LPSTR, GetEnvironmentStringsA)
|
---|
42 | {
|
---|
43 | return (LPSTR) O32_GetEnvironmentStrings();
|
---|
44 | }
|
---|
45 | //******************************************************************************
|
---|
46 | //******************************************************************************
|
---|
47 | ODINFUNCTION0(LPWSTR, GetEnvironmentStringsW)
|
---|
48 | {
|
---|
49 | char *envstrings = (char *)O32_GetEnvironmentStrings();
|
---|
50 | char *tmp;
|
---|
51 | LPWSTR wenvstrings;
|
---|
52 | int len, i;
|
---|
53 |
|
---|
54 | if(envstrings == NULL)
|
---|
55 | return(NULL);
|
---|
56 |
|
---|
57 | tmp = envstrings;
|
---|
58 | len = 0;
|
---|
59 | while(*tmp != 0)
|
---|
60 | {
|
---|
61 | len += strlen(tmp)+1;
|
---|
62 | tmp = envstrings + len;
|
---|
63 | }
|
---|
64 | len++; //terminating 0
|
---|
65 | wenvstrings = (LPWSTR)malloc(len*sizeof(WCHAR));
|
---|
66 | for(i=0;
|
---|
67 | i<len;
|
---|
68 | i++)
|
---|
69 | {
|
---|
70 | wenvstrings[i] = envstrings[i];
|
---|
71 | }
|
---|
72 | return(wenvstrings);
|
---|
73 | }
|
---|
74 | //******************************************************************************
|
---|
75 | //******************************************************************************
|
---|
76 | ODINFUNCTION1(BOOL, FreeEnvironmentStringsA,
|
---|
77 | LPSTR, envstrings)
|
---|
78 | {
|
---|
79 | return(TRUE);
|
---|
80 | }
|
---|
81 | //******************************************************************************
|
---|
82 | //******************************************************************************
|
---|
83 | ODINFUNCTION1(BOOL, FreeEnvironmentStringsW,
|
---|
84 | LPWSTR, envstrings)
|
---|
85 | {
|
---|
86 | free(envstrings);
|
---|
87 | return(TRUE);
|
---|
88 | }
|
---|
89 | //******************************************************************************
|
---|
90 | //******************************************************************************
|
---|
91 | ODINFUNCTION2(BOOL, SetEnvironmentVariableA,
|
---|
92 | LPCSTR, lpName,
|
---|
93 | LPCSTR, lpValue)
|
---|
94 | {
|
---|
95 | dprintf(("KERNEL32: SetEnvironmentVariable %s to %s\n", lpName, lpValue));
|
---|
96 | return O32_SetEnvironmentVariable(lpName, lpValue);
|
---|
97 | }
|
---|
98 | //******************************************************************************
|
---|
99 | //******************************************************************************
|
---|
100 | ODINFUNCTION2(BOOL, SetEnvironmentVariableW,
|
---|
101 | LPCWSTR, lpName,
|
---|
102 | LPCWSTR, lpValue)
|
---|
103 | {
|
---|
104 | char *asciiname, *asciivalue;
|
---|
105 | BOOL rc;
|
---|
106 |
|
---|
107 | asciiname = UnicodeToAsciiString((LPWSTR)lpName);
|
---|
108 | asciivalue = UnicodeToAsciiString((LPWSTR)lpValue);
|
---|
109 | dprintf(("KERNEL32: SetEnvironmentVariable %s to %s\n", asciiname, asciivalue));
|
---|
110 | rc = O32_SetEnvironmentVariable(asciiname, asciivalue);
|
---|
111 | FreeAsciiString(asciivalue);
|
---|
112 | FreeAsciiString(asciiname);
|
---|
113 | return(rc);
|
---|
114 | }
|
---|
115 | //******************************************************************************
|
---|
116 | //******************************************************************************
|
---|
117 | ODINFUNCTION3(DWORD, GetEnvironmentVariableA,
|
---|
118 | LPCSTR, lpName,
|
---|
119 | LPSTR, lpBuffer,
|
---|
120 | DWORD, nSize)
|
---|
121 | {
|
---|
122 | dprintf(("GetEnvironmentVariableA %s", lpName));
|
---|
123 | return O32_GetEnvironmentVariable(lpName, lpBuffer, nSize);
|
---|
124 | }
|
---|
125 | //******************************************************************************
|
---|
126 | //******************************************************************************
|
---|
127 | ODINFUNCTION3(DWORD, GetEnvironmentVariableW,
|
---|
128 | LPCWSTR, lpName,
|
---|
129 | LPWSTR, lpBuffer,
|
---|
130 | DWORD, nSize)
|
---|
131 | {
|
---|
132 | char *astring, *asciibuffer;
|
---|
133 | DWORD rc;
|
---|
134 |
|
---|
135 | if(nSize) {
|
---|
136 | asciibuffer = (char *)malloc(nSize+1);
|
---|
137 | *asciibuffer = 0;
|
---|
138 | }
|
---|
139 | else asciibuffer = NULL;
|
---|
140 |
|
---|
141 | astring = UnicodeToAsciiString((LPWSTR)lpName);
|
---|
142 |
|
---|
143 | rc = CALL_ODINFUNC(GetEnvironmentVariableA)(astring, asciibuffer, nSize);
|
---|
144 | AsciiToUnicode(asciibuffer, lpBuffer);
|
---|
145 | FreeAsciiString(astring);
|
---|
146 | if(asciibuffer)
|
---|
147 | free(asciibuffer);
|
---|
148 | return(rc);
|
---|
149 | }
|
---|
150 | /***********************************************************************
|
---|
151 | * ENV_FindVariable
|
---|
152 | *
|
---|
153 | * Find a variable in the environment and return a pointer to the value.
|
---|
154 | * Helper function for GetEnvironmentVariable and ExpandEnvironmentStrings.
|
---|
155 | */
|
---|
156 | static LPCSTR ENV_FindVariable( LPCSTR env, LPCSTR name, INT len )
|
---|
157 | {
|
---|
158 | while (*env)
|
---|
159 | {
|
---|
160 | if (!lstrncmpiA( name, env, len ) && (env[len] == '='))
|
---|
161 | return env + len + 1;
|
---|
162 | env += strlen(env) + 1;
|
---|
163 | }
|
---|
164 | return NULL;
|
---|
165 | }
|
---|
166 | /*****************************************************************************
|
---|
167 | * Name : DWORD WIN32API ExpandEnvironmentStringsA
|
---|
168 | * Purpose : The ExpandEnvironmentStringsA function expands environment-variable
|
---|
169 | * strings and replaces them with their defined values.
|
---|
170 | * Parameters: LPCSTR lpSrc pointer to string with environment variables
|
---|
171 | * LPSTR lpDst pointer to string with expanded environment variables
|
---|
172 | * DWORD nSize maximum characters in expanded string
|
---|
173 | * Variables :
|
---|
174 | * Result : If the function succeeds, the return value is the number of
|
---|
175 | * characters stored in the destination buffer. If the number of
|
---|
176 | * characters is greater than the size of the destination buffer,
|
---|
177 | * the return value is the size of the buffer required to hold
|
---|
178 | * the expanded strings.
|
---|
179 | * If the function fails, the return value is zero
|
---|
180 | * Remark :
|
---|
181 | * Status :
|
---|
182 | *
|
---|
183 | *****************************************************************************/
|
---|
184 |
|
---|
185 | ODINFUNCTION3(DWORD, ExpandEnvironmentStringsA,
|
---|
186 | LPCSTR, src,
|
---|
187 | LPSTR, dst,
|
---|
188 | DWORD, count)
|
---|
189 | {
|
---|
190 | DWORD len, total_size = 1; /* 1 for terminating '\0' */
|
---|
191 | LPCSTR p, var;
|
---|
192 |
|
---|
193 | dprintf(("KERNEL32:ExpandEnvironmentStringsA '%s', %08x, %08x",
|
---|
194 | src, dst, count
|
---|
195 | ));
|
---|
196 |
|
---|
197 | if (!count) dst = NULL;
|
---|
198 |
|
---|
199 | while (*src)
|
---|
200 | {
|
---|
201 | if (*src != '%')
|
---|
202 | {
|
---|
203 | if ((p = strchr( src, '%' ))) len = p - src;
|
---|
204 | else len = strlen(src);
|
---|
205 | var = src;
|
---|
206 | src += len;
|
---|
207 | }
|
---|
208 | else /* we are at the start of a variable */
|
---|
209 | {
|
---|
210 | if ((p = strchr( src + 1, '%' )))
|
---|
211 | {
|
---|
212 | len = p - src - 1; /* Length of the variable name */
|
---|
213 | if ((var = ENV_FindVariable( GetEnvironmentStringsA(),
|
---|
214 | src + 1, len )))
|
---|
215 | {
|
---|
216 | src += len + 2; /* Skip the variable name */
|
---|
217 | len = strlen(var);
|
---|
218 | }
|
---|
219 | else
|
---|
220 | {
|
---|
221 | var = src; /* Copy original name instead */
|
---|
222 | len += 2;
|
---|
223 | src += len;
|
---|
224 | }
|
---|
225 | }
|
---|
226 | else /* unfinished variable name, ignore it */
|
---|
227 | {
|
---|
228 | var = src;
|
---|
229 | len = strlen(src); /* Copy whole string */
|
---|
230 | src += len;
|
---|
231 | }
|
---|
232 | }
|
---|
233 | total_size += len;
|
---|
234 | if (dst)
|
---|
235 | {
|
---|
236 | if (count < len) len = count;
|
---|
237 | memcpy( dst, var, len );
|
---|
238 | dst += len;
|
---|
239 | count -= len;
|
---|
240 | }
|
---|
241 | }
|
---|
242 |
|
---|
243 | /* Null-terminate the string */
|
---|
244 | if (dst)
|
---|
245 | {
|
---|
246 | if (!count) dst--;
|
---|
247 | *dst = '\0';
|
---|
248 | }
|
---|
249 | return total_size;
|
---|
250 | }
|
---|
251 |
|
---|
252 | /*****************************************************************************
|
---|
253 | * Name : DWORD WIN32API ExpandEnvironmentStringsW
|
---|
254 | * Purpose : The ExpandEnvironmentStringsA function expands environment-variable
|
---|
255 | * strings and replaces them with their defined values.
|
---|
256 | * Parameters: LPCWSTR lpSrc pointer to string with environment variables
|
---|
257 | * LPWSTR lpDst pointer to string with expanded environment variables
|
---|
258 | * DWORD nSize maximum characters in expanded string
|
---|
259 | * Variables :
|
---|
260 | * Result : If the function succeeds, the return value is the number of
|
---|
261 | * characters stored in the destination buffer. If the number of
|
---|
262 | * characters is greater than the size of the destination buffer,
|
---|
263 | * the return value is the size of the buffer required to hold
|
---|
264 | * the expanded strings.
|
---|
265 | * If the function fails, the return value is zero
|
---|
266 | * Remark :
|
---|
267 | * Status :
|
---|
268 | *
|
---|
269 | *****************************************************************************/
|
---|
270 |
|
---|
271 | ODINFUNCTION3(DWORD, ExpandEnvironmentStringsW,
|
---|
272 | LPCWSTR, lpSrc,
|
---|
273 | LPWSTR, lpDst,
|
---|
274 | DWORD, nSize)
|
---|
275 | {
|
---|
276 | LPSTR srcA = HEAP_strdupWtoA( GetProcessHeap(), 0, lpSrc );
|
---|
277 | LPSTR dstA = lpDst ? (LPSTR)HeapAlloc( GetProcessHeap(), 0, nSize ) : NULL;
|
---|
278 |
|
---|
279 | dprintf(("KERNEL32:ExpandEnvironmentStringsW(%08x,%08x,%08x)", lpSrc, lpDst, nSize));
|
---|
280 |
|
---|
281 | DWORD ret = ExpandEnvironmentStringsA( srcA, dstA, nSize );
|
---|
282 | if (dstA)
|
---|
283 | {
|
---|
284 | lstrcpyAtoW( lpDst, dstA );
|
---|
285 | HeapFree( GetProcessHeap(), 0, dstA );
|
---|
286 | }
|
---|
287 | HeapFree( GetProcessHeap(), 0, srcA );
|
---|
288 | return ret;
|
---|
289 | }
|
---|
290 | //******************************************************************************
|
---|
291 | //******************************************************************************
|
---|