source: trunk/src/kernel32/environ.cpp@ 5588

Last change on this file since 5588 was 5481, checked in by sandervl, 24 years ago

compile fixes

File size: 9.3 KB
Line 
1/* $Id: environ.cpp,v 1.10 2001-04-05 05:54:51 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
37ODINDEBUGCHANNEL(KERNEL32-ENVIRONMENT)
38
39
40//******************************************************************************
41//******************************************************************************
42ODINFUNCTION0(LPSTR, GetEnvironmentStringsA)
43{
44 return (LPSTR) O32_GetEnvironmentStrings();
45}
46//******************************************************************************
47//******************************************************************************
48ODINFUNCTION0(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//******************************************************************************
77ODINFUNCTION1(BOOL, FreeEnvironmentStringsA,
78 LPSTR, envstrings)
79{
80 return(TRUE);
81}
82//******************************************************************************
83//******************************************************************************
84ODINFUNCTION1(BOOL, FreeEnvironmentStringsW,
85 LPWSTR, envstrings)
86{
87 free(envstrings);
88 return(TRUE);
89}
90//******************************************************************************
91//******************************************************************************
92ODINFUNCTION2(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//******************************************************************************
101ODINFUNCTION2(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//******************************************************************************
118ODINFUNCTION3(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//******************************************************************************
128ODINFUNCTION3(DWORD, GetEnvironmentVariableW,
129 LPCWSTR, lpName,
130 LPWSTR, lpBuffer,
131 DWORD, nSize)
132{
133 char *astring, *asciibuffer;
134 DWORD rc;
135
136 if(nSize) {
137 asciibuffer = (char *)malloc(nSize+1);
138 *asciibuffer = 0;
139 }
140 else asciibuffer = NULL;
141
142 astring = UnicodeToAsciiString((LPWSTR)lpName);
143
144 rc = CALL_ODINFUNC(GetEnvironmentVariableA)(astring, asciibuffer, nSize);
145 AsciiToUnicode(asciibuffer, lpBuffer);
146 FreeAsciiString(astring);
147 if(asciibuffer)
148 free(asciibuffer);
149 return(rc);
150}
151/***********************************************************************
152 * ENV_FindVariable
153 *
154 * Find a variable in the environment and return a pointer to the value.
155 * Helper function for GetEnvironmentVariable and ExpandEnvironmentStrings.
156 */
157static LPCSTR ENV_FindVariable( LPCSTR env, LPCSTR name, INT len )
158{
159 while (*env)
160 {
161 if (!lstrncmpiA( name, env, len ) && (env[len] == '='))
162 return env + len + 1;
163 env += strlen(env) + 1;
164 }
165 return NULL;
166}
167/*****************************************************************************
168 * Name : DWORD WIN32API ExpandEnvironmentStringsA
169 * Purpose : The ExpandEnvironmentStringsA function expands environment-variable
170 * strings and replaces them with their defined values.
171 * Parameters: LPCSTR lpSrc pointer to string with environment variables
172 * LPSTR lpDst pointer to string with expanded environment variables
173 * DWORD nSize maximum characters in expanded string
174 * Variables :
175 * Result : If the function succeeds, the return value is the number of
176 * characters stored in the destination buffer. If the number of
177 * characters is greater than the size of the destination buffer,
178 * the return value is the size of the buffer required to hold
179 * the expanded strings.
180 * If the function fails, the return value is zero
181 * Remark :
182 * Status :
183 *
184 *****************************************************************************/
185
186ODINFUNCTION3(DWORD, ExpandEnvironmentStringsA,
187 LPCSTR, src,
188 LPSTR, dst,
189 DWORD, count)
190{
191 DWORD len, total_size = 1; /* 1 for terminating '\0' */
192 LPCSTR p, var;
193
194 dprintf(("KERNEL32:ExpandEnvironmentStringsA '%s', %08x, %08x",
195 src, dst, count
196 ));
197
198 if (!count) dst = NULL;
199
200 while (*src)
201 {
202 if (*src != '%')
203 {
204 if ((p = strchr( src, '%' ))) len = p - src;
205 else len = strlen(src);
206 var = src;
207 src += len;
208 }
209 else /* we are at the start of a variable */
210 {
211 if ((p = strchr( src + 1, '%' )))
212 {
213 len = p - src - 1; /* Length of the variable name */
214 if ((var = ENV_FindVariable( GetEnvironmentStringsA(),
215 src + 1, len )))
216 {
217 src += len + 2; /* Skip the variable name */
218 len = strlen(var);
219 }
220 else
221 {
222 var = src; /* Copy original name instead */
223 len += 2;
224 src += len;
225 }
226 }
227 else /* unfinished variable name, ignore it */
228 {
229 var = src;
230 len = strlen(src); /* Copy whole string */
231 src += len;
232 }
233 }
234 total_size += len;
235 if (dst)
236 {
237 if (count < len) len = count;
238 memcpy( dst, var, len );
239 dst += len;
240 count -= len;
241 }
242 }
243
244 /* Null-terminate the string */
245 if (dst)
246 {
247 if (!count) dst--;
248 *dst = '\0';
249 }
250 return total_size;
251}
252
253/*****************************************************************************
254 * Name : DWORD WIN32API ExpandEnvironmentStringsW
255 * Purpose : The ExpandEnvironmentStringsA function expands environment-variable
256 * strings and replaces them with their defined values.
257 * Parameters: LPCWSTR lpSrc pointer to string with environment variables
258 * LPWSTR lpDst pointer to string with expanded environment variables
259 * DWORD nSize maximum characters in expanded string
260 * Variables :
261 * Result : If the function succeeds, the return value is the number of
262 * characters stored in the destination buffer. If the number of
263 * characters is greater than the size of the destination buffer,
264 * the return value is the size of the buffer required to hold
265 * the expanded strings.
266 * If the function fails, the return value is zero
267 * Remark :
268 * Status :
269 *
270 *****************************************************************************/
271
272ODINFUNCTION3(DWORD, ExpandEnvironmentStringsW,
273 LPCWSTR, lpSrc,
274 LPWSTR, lpDst,
275 DWORD, nSize)
276{
277 LPSTR srcA = HEAP_strdupWtoA( GetProcessHeap(), 0, lpSrc );
278 LPSTR dstA = lpDst ? (LPSTR)HeapAlloc( GetProcessHeap(), 0, nSize ) : NULL;
279
280 dprintf(("KERNEL32:ExpandEnvironmentStringsW(%08x,%08x,%08x)", lpSrc, lpDst, nSize));
281
282 DWORD ret = ExpandEnvironmentStringsA( srcA, dstA, nSize );
283 if (dstA)
284 {
285 lstrcpyAtoW( lpDst, dstA );
286 HeapFree( GetProcessHeap(), 0, dstA );
287 }
288 HeapFree( GetProcessHeap(), 0, srcA );
289 return ret;
290}
291//******************************************************************************
292//******************************************************************************
Note: See TracBrowser for help on using the repository browser.