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

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

Fixed some FS: bugs

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