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

Last change on this file since 2246 was 2042, checked in by sandervl, 26 years ago

Ported ExpandEnvironmentStringsA/W (Wine: 991114)

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