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

Last change on this file since 4380 was 4380, checked in by phaller, 25 years ago

Wrapper macro support

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