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