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