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