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

Last change on this file since 4224 was 4224, checked in by sandervl, 25 years ago

exception changes, implemented enumresourcelanguages + put back some old code

File size: 9.3 KB
Line 
1/* $Id: environ.cpp,v 1.6 2000-09-08 18:07:49 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//******************************************************************************
37LPSTR WIN32API GetEnvironmentStringsA(void)
38{
39 dprintf(("KERNEL32: OS2GetEnvironmentStringsA\n"));
40 return (LPSTR) O32_GetEnvironmentStrings();
41}
42//******************************************************************************
43//******************************************************************************
44LPWSTR 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//******************************************************************************
75BOOL WIN32API FreeEnvironmentStringsA(LPSTR envstrings)
76{
77 dprintf(("KERNEL32: FreeEnvironmentStringsA\n"));
78 return(TRUE);
79}
80//******************************************************************************
81//******************************************************************************
82BOOL WIN32API FreeEnvironmentStringsW(LPWSTR envstrings)
83{
84 dprintf(("KERNEL32: FreeEnvironmentStringsW\n"));
85 free(envstrings);
86 return(TRUE);
87}
88//******************************************************************************
89//******************************************************************************
90BOOL 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//******************************************************************************
97BOOL 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//******************************************************************************
112DWORD 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//******************************************************************************
119DWORD 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 */
142static LPCSTR ENV_FindVariable( LPCSTR env, LPCSTR name, INT len )
143{
144 while (*env)
145 {
146 if (!lstrncmpiA( name, env, len ) && (env[len] == '='))
147 return env + len + 1;
148 env += strlen(env) + 1;
149 }
150 return NULL;
151}
152/*****************************************************************************
153 * Name : DWORD WIN32API ExpandEnvironmentStringsA
154 * Purpose : The ExpandEnvironmentStringsA function expands environment-variable
155 * strings and replaces them with their defined values.
156 * Parameters: LPCSTR lpSrc pointer to string with environment variables
157 * LPSTR lpDst pointer to string with expanded environment variables
158 * DWORD nSize maximum characters in expanded string
159 * Variables :
160 * Result : If the function succeeds, the return value is the number of
161 * characters stored in the destination buffer. If the number of
162 * characters is greater than the size of the destination buffer,
163 * the return value is the size of the buffer required to hold
164 * the expanded strings.
165 * If the function fails, the return value is zero
166 * Remark :
167 * Status :
168 *
169 *****************************************************************************/
170
171DWORD WIN32API ExpandEnvironmentStringsA( LPCSTR src, LPSTR dst, DWORD count )
172{
173 DWORD len, total_size = 1; /* 1 for terminating '\0' */
174 LPCSTR p, var;
175
176 dprintf(("KERNEL32:ExpandEnvironmentStringsA '%s', %08x, %08x",
177 src, dst, count
178 ));
179
180 if (!count) dst = NULL;
181
182 while (*src)
183 {
184 if (*src != '%')
185 {
186 if ((p = strchr( src, '%' ))) len = p - src;
187 else len = strlen(src);
188 var = src;
189 src += len;
190 }
191 else /* we are at the start of a variable */
192 {
193 if ((p = strchr( src + 1, '%' )))
194 {
195 len = p - src - 1; /* Length of the variable name */
196 if ((var = ENV_FindVariable( GetEnvironmentStringsA(),
197 src + 1, len )))
198 {
199 src += len + 2; /* Skip the variable name */
200 len = strlen(var);
201 }
202 else
203 {
204 var = src; /* Copy original name instead */
205 len += 2;
206 src += len;
207 }
208 }
209 else /* unfinished variable name, ignore it */
210 {
211 var = src;
212 len = strlen(src); /* Copy whole string */
213 src += len;
214 }
215 }
216 total_size += len;
217 if (dst)
218 {
219 if (count < len) len = count;
220 memcpy( dst, var, len );
221 dst += len;
222 count -= len;
223 }
224 }
225
226 /* Null-terminate the string */
227 if (dst)
228 {
229 if (!count) dst--;
230 *dst = '\0';
231 }
232 dprintf(("KERNEL32:ExpandEnvironmentStringsA returned %s %d",
233 dst, total_size));
234 return total_size;
235}
236
237/*****************************************************************************
238 * Name : DWORD WIN32API ExpandEnvironmentStringsW
239 * Purpose : The ExpandEnvironmentStringsA function expands environment-variable
240 * strings and replaces them with their defined values.
241 * Parameters: LPCWSTR lpSrc pointer to string with environment variables
242 * LPWSTR lpDst pointer to string with expanded environment variables
243 * DWORD nSize maximum characters in expanded string
244 * Variables :
245 * Result : If the function succeeds, the return value is the number of
246 * characters stored in the destination buffer. If the number of
247 * characters is greater than the size of the destination buffer,
248 * the return value is the size of the buffer required to hold
249 * the expanded strings.
250 * If the function fails, the return value is zero
251 * Remark :
252 * Status :
253 *
254 *****************************************************************************/
255
256DWORD WIN32API ExpandEnvironmentStringsW(LPCWSTR lpSrc, LPWSTR lpDst, DWORD nSize)
257{
258 LPSTR srcA = HEAP_strdupWtoA( GetProcessHeap(), 0, lpSrc );
259 LPSTR dstA = lpDst ? (LPSTR)HeapAlloc( GetProcessHeap(), 0, nSize ) : NULL;
260
261 dprintf(("KERNEL32:ExpandEnvironmentStringsW(%08x,%08x,%08x)", lpSrc, lpDst, nSize));
262
263 DWORD ret = ExpandEnvironmentStringsA( srcA, dstA, nSize );
264 if (dstA)
265 {
266 lstrcpyAtoW( lpDst, dstA );
267 HeapFree( GetProcessHeap(), 0, dstA );
268 }
269 HeapFree( GetProcessHeap(), 0, srcA );
270 return ret;
271}
272//******************************************************************************
273//******************************************************************************
Note: See TracBrowser for help on using the repository browser.