| 1 | /* $Id: environ.cpp,v 1.2 2000-02-16 14:25:39 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 | //****************************************************************************** | 
|---|
| 37 | LPSTR WIN32API GetEnvironmentStringsA(void) | 
|---|
| 38 | { | 
|---|
| 39 | dprintf(("KERNEL32:  OS2GetEnvironmentStringsA\n")); | 
|---|
| 40 | return (LPSTR) O32_GetEnvironmentStrings(); | 
|---|
| 41 | } | 
|---|
| 42 | //****************************************************************************** | 
|---|
| 43 | //****************************************************************************** | 
|---|
| 44 | LPWSTR 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 | //****************************************************************************** | 
|---|
| 75 | BOOL WIN32API FreeEnvironmentStringsA(LPSTR envstrings) | 
|---|
| 76 | { | 
|---|
| 77 | dprintf(("KERNEL32:  FreeEnvironmentStringsA\n")); | 
|---|
| 78 | return(TRUE); | 
|---|
| 79 | } | 
|---|
| 80 | //****************************************************************************** | 
|---|
| 81 | //****************************************************************************** | 
|---|
| 82 | BOOL WIN32API FreeEnvironmentStringsW(LPWSTR envstrings) | 
|---|
| 83 | { | 
|---|
| 84 | dprintf(("KERNEL32:  FreeEnvironmentStringsW\n")); | 
|---|
| 85 | free(envstrings); | 
|---|
| 86 | return(TRUE); | 
|---|
| 87 | } | 
|---|
| 88 | //****************************************************************************** | 
|---|
| 89 | //****************************************************************************** | 
|---|
| 90 | BOOL 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 | //****************************************************************************** | 
|---|
| 97 | BOOL 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 | //****************************************************************************** | 
|---|
| 112 | DWORD 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 | //****************************************************************************** | 
|---|
| 119 | DWORD 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 | */ | 
|---|
| 142 | static 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 |  | 
|---|
| 171 | DWORD 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(%08x,%08x,%08x) not implemented\n", | 
|---|
| 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 | return total_size; | 
|---|
| 233 | } | 
|---|
| 234 |  | 
|---|
| 235 | /***************************************************************************** | 
|---|
| 236 | * Name      : DWORD WIN32API ExpandEnvironmentStringsW | 
|---|
| 237 | * Purpose   : The ExpandEnvironmentStringsA function expands environment-variable | 
|---|
| 238 | *             strings and replaces them with their defined values. | 
|---|
| 239 | * Parameters: LPCWSTR lpSrc  pointer to string with environment variables | 
|---|
| 240 | *             LPWSTR lpDst   pointer to string with expanded environment variables | 
|---|
| 241 | *             DWORD nSize   maximum characters in expanded string | 
|---|
| 242 | * Variables : | 
|---|
| 243 | * Result    : If the function succeeds, the return value is the number of | 
|---|
| 244 | *             characters stored in the destination buffer. If the number of | 
|---|
| 245 | *             characters is greater than the size of the destination buffer, | 
|---|
| 246 | *             the return value is the size of the buffer required to hold | 
|---|
| 247 | *             the expanded strings. | 
|---|
| 248 | *             If the function fails, the return value is zero | 
|---|
| 249 | * Remark    : | 
|---|
| 250 | * Status    : | 
|---|
| 251 | * | 
|---|
| 252 | *****************************************************************************/ | 
|---|
| 253 |  | 
|---|
| 254 | DWORD WIN32API ExpandEnvironmentStringsW(LPCWSTR lpSrc, LPWSTR lpDst, DWORD nSize) | 
|---|
| 255 | { | 
|---|
| 256 | LPSTR srcA = HEAP_strdupWtoA( GetProcessHeap(), 0, lpSrc ); | 
|---|
| 257 | LPSTR dstA = lpDst ? (LPSTR)HeapAlloc( GetProcessHeap(), 0, nSize ) : NULL; | 
|---|
| 258 |  | 
|---|
| 259 | dprintf(("KERNEL32:ExpandEnvironmentStringsW(%08x,%08x,%08x)", lpSrc, lpDst, nSize)); | 
|---|
| 260 |  | 
|---|
| 261 | DWORD ret  = ExpandEnvironmentStringsA( srcA, dstA, nSize ); | 
|---|
| 262 | if (dstA) | 
|---|
| 263 | { | 
|---|
| 264 | lstrcpyAtoW( lpDst, dstA ); | 
|---|
| 265 | HeapFree( GetProcessHeap(), 0, dstA ); | 
|---|
| 266 | } | 
|---|
| 267 | HeapFree( GetProcessHeap(), 0, srcA ); | 
|---|
| 268 | return ret; | 
|---|
| 269 | } | 
|---|
| 270 | //****************************************************************************** | 
|---|
| 271 | //****************************************************************************** | 
|---|