2012-09-23 06:02:55 +04:00
|
|
|
/**
|
|
|
|
* WinPR: Windows Portable Runtime
|
|
|
|
* Process Environment Functions
|
|
|
|
*
|
|
|
|
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
2013-12-04 14:37:57 +04:00
|
|
|
* Copyright 2013 Thincast Technologies GmbH
|
|
|
|
* Copyright 2013 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
|
2012-09-23 06:02:55 +04:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2014-11-04 18:44:55 +03:00
|
|
|
#include <winpr/crt.h>
|
2016-08-10 12:06:34 +03:00
|
|
|
#include <winpr/platform.h>
|
2014-04-17 13:48:50 +04:00
|
|
|
#include <winpr/error.h>
|
2012-09-23 06:02:55 +04:00
|
|
|
|
2014-11-04 18:44:55 +03:00
|
|
|
#include <winpr/environment.h>
|
|
|
|
|
2012-09-23 06:02:55 +04:00
|
|
|
#ifndef _WIN32
|
|
|
|
|
2013-10-23 11:58:33 +04:00
|
|
|
#define stricmp strcasecmp
|
|
|
|
#define strnicmp strncasecmp
|
|
|
|
|
2012-09-23 06:02:55 +04:00
|
|
|
#include <winpr/crt.h>
|
2013-12-03 09:52:02 +04:00
|
|
|
#include <winpr/platform.h>
|
2012-09-23 06:02:55 +04:00
|
|
|
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2013-12-04 00:19:58 +04:00
|
|
|
#if defined(__IOS__)
|
|
|
|
|
|
|
|
#elif defined(__MACOSX__)
|
2013-12-03 09:52:02 +04:00
|
|
|
#include <crt_externs.h>
|
|
|
|
#define environ (*_NSGetEnviron())
|
|
|
|
#endif
|
|
|
|
|
2012-09-23 06:02:55 +04:00
|
|
|
DWORD GetCurrentDirectoryA(DWORD nBufferLength, LPSTR lpBuffer)
|
|
|
|
{
|
|
|
|
char* cwd;
|
2019-01-30 11:29:56 +03:00
|
|
|
size_t length;
|
2012-09-23 06:02:55 +04:00
|
|
|
|
|
|
|
cwd = getcwd(NULL, 0);
|
|
|
|
|
|
|
|
if (!cwd)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
length = strlen(cwd);
|
|
|
|
|
|
|
|
if ((nBufferLength == 0) && (lpBuffer == NULL))
|
|
|
|
{
|
|
|
|
free(cwd);
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
return (DWORD)length;
|
2012-09-23 06:02:55 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (lpBuffer == NULL)
|
|
|
|
{
|
|
|
|
free(cwd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((length + 1) > nBufferLength)
|
|
|
|
{
|
|
|
|
free(cwd);
|
2019-11-06 17:24:51 +03:00
|
|
|
return (DWORD)(length + 1);
|
2012-09-23 06:02:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(lpBuffer, cwd, length + 1);
|
2019-10-16 19:49:24 +03:00
|
|
|
free(cwd);
|
2012-09-23 06:02:55 +04:00
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD GetCurrentDirectoryW(DWORD nBufferLength, LPWSTR lpBuffer)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL SetCurrentDirectoryA(LPCSTR lpPathName)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL SetCurrentDirectoryW(LPCWSTR lpPathName)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
DWORD SearchPathA(LPCSTR lpPath, LPCSTR lpFileName, LPCSTR lpExtension, DWORD nBufferLength,
|
|
|
|
LPSTR lpBuffer, LPSTR* lpFilePart)
|
2012-09-23 06:02:55 +04:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
DWORD SearchPathW(LPCWSTR lpPath, LPCWSTR lpFileName, LPCWSTR lpExtension, DWORD nBufferLength,
|
|
|
|
LPWSTR lpBuffer, LPWSTR* lpFilePart)
|
2012-09-23 06:02:55 +04:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
LPSTR GetCommandLineA(VOID)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
LPWSTR GetCommandLineW(VOID)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL NeedCurrentDirectoryForExePathA(LPCSTR ExeName)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL NeedCurrentDirectoryForExePathW(LPCWSTR ExeName)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2016-02-06 00:28:45 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(_WIN32) || defined(_UWP)
|
|
|
|
|
2012-09-23 06:02:55 +04:00
|
|
|
DWORD GetEnvironmentVariableA(LPCSTR lpName, LPSTR lpBuffer, DWORD nSize)
|
|
|
|
{
|
2016-02-06 00:28:45 +03:00
|
|
|
#if !defined(_UWP)
|
2019-01-30 11:29:56 +03:00
|
|
|
size_t length;
|
2013-03-22 08:42:09 +04:00
|
|
|
char* env = NULL;
|
|
|
|
|
|
|
|
env = getenv(lpName);
|
|
|
|
|
|
|
|
if (!env)
|
2014-04-17 13:48:50 +04:00
|
|
|
{
|
|
|
|
SetLastError(ERROR_ENVVAR_NOT_FOUND);
|
2013-03-22 08:42:09 +04:00
|
|
|
return 0;
|
2014-04-17 13:48:50 +04:00
|
|
|
}
|
2013-03-22 08:42:09 +04:00
|
|
|
|
|
|
|
length = strlen(env);
|
|
|
|
|
|
|
|
if ((length + 1 > nSize) || (!lpBuffer))
|
|
|
|
return length + 1;
|
|
|
|
|
2017-03-03 14:37:27 +03:00
|
|
|
CopyMemory(lpBuffer, env, length);
|
|
|
|
lpBuffer[length] = '\0';
|
2013-03-22 08:42:09 +04:00
|
|
|
|
|
|
|
return length;
|
2016-02-06 00:28:45 +03:00
|
|
|
#else
|
|
|
|
SetLastError(ERROR_ENVVAR_NOT_FOUND);
|
|
|
|
return 0;
|
|
|
|
#endif
|
2012-09-23 06:02:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
DWORD GetEnvironmentVariableW(LPCWSTR lpName, LPWSTR lpBuffer, DWORD nSize)
|
|
|
|
{
|
2016-02-06 00:28:45 +03:00
|
|
|
SetLastError(ERROR_ENVVAR_NOT_FOUND);
|
2012-09-23 06:02:55 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL SetEnvironmentVariableA(LPCSTR lpName, LPCSTR lpValue)
|
|
|
|
{
|
2016-02-06 00:28:45 +03:00
|
|
|
#if !defined(_UWP)
|
2013-09-23 22:44:59 +04:00
|
|
|
if (!lpName)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (lpValue)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
if (0 != setenv(lpName, lpValue, 1))
|
2014-04-17 13:48:50 +04:00
|
|
|
return FALSE;
|
2013-09-23 22:44:59 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-04-17 13:48:50 +04:00
|
|
|
if (0 != unsetenv(lpName))
|
|
|
|
return FALSE;
|
2013-09-23 22:44:59 +04:00
|
|
|
}
|
|
|
|
|
2012-09-23 06:02:55 +04:00
|
|
|
return TRUE;
|
2016-02-06 00:28:45 +03:00
|
|
|
#else
|
|
|
|
return FALSE;
|
|
|
|
#endif
|
2012-09-23 06:02:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL SetEnvironmentVariableW(LPCWSTR lpName, LPCWSTR lpValue)
|
|
|
|
{
|
2016-02-06 00:28:45 +03:00
|
|
|
return FALSE;
|
2012-09-23 06:02:55 +04:00
|
|
|
}
|
|
|
|
|
2013-09-23 22:44:59 +04:00
|
|
|
/**
|
|
|
|
* GetEnvironmentStrings function:
|
|
|
|
* http://msdn.microsoft.com/en-us/library/windows/desktop/ms683187/
|
|
|
|
*
|
|
|
|
* The GetEnvironmentStrings function returns a pointer to a block of memory
|
|
|
|
* that contains the environment variables of the calling process (both the
|
|
|
|
* system and the user environment variables). Each environment block contains
|
|
|
|
* the environment variables in the following format:
|
|
|
|
*
|
|
|
|
* Var1=Value1\0
|
|
|
|
* Var2=Value2\0
|
|
|
|
* Var3=Value3\0
|
|
|
|
* ...
|
|
|
|
* VarN=ValueN\0\0
|
|
|
|
*/
|
|
|
|
|
|
|
|
extern char** environ;
|
|
|
|
|
2016-02-06 00:28:45 +03:00
|
|
|
LPCH GetEnvironmentStringsA(VOID)
|
2012-09-23 06:02:55 +04:00
|
|
|
{
|
2016-02-06 00:28:45 +03:00
|
|
|
#if !defined(_UWP)
|
2013-09-23 22:44:59 +04:00
|
|
|
char* p;
|
2019-10-29 12:18:09 +03:00
|
|
|
size_t offset;
|
|
|
|
size_t length;
|
2013-09-23 22:44:59 +04:00
|
|
|
char** envp;
|
|
|
|
DWORD cchEnvironmentBlock;
|
|
|
|
LPCH lpszEnvironmentBlock;
|
|
|
|
|
|
|
|
offset = 0;
|
|
|
|
envp = environ;
|
|
|
|
|
|
|
|
cchEnvironmentBlock = 128;
|
2019-11-06 17:24:51 +03:00
|
|
|
lpszEnvironmentBlock = (LPCH)calloc(cchEnvironmentBlock, sizeof(CHAR));
|
2015-03-11 09:59:56 +03:00
|
|
|
if (!lpszEnvironmentBlock)
|
|
|
|
return NULL;
|
2013-09-23 22:44:59 +04:00
|
|
|
|
|
|
|
while (*envp)
|
|
|
|
{
|
2019-10-29 12:18:09 +03:00
|
|
|
length = strlen(*envp);
|
2013-09-23 22:44:59 +04:00
|
|
|
|
2019-10-29 12:18:09 +03:00
|
|
|
while ((offset + length + 8) > cchEnvironmentBlock)
|
2013-09-23 22:44:59 +04:00
|
|
|
{
|
2015-03-11 09:59:56 +03:00
|
|
|
DWORD new_size;
|
|
|
|
LPCH new_blk;
|
|
|
|
|
|
|
|
new_size = cchEnvironmentBlock * 2;
|
2019-11-06 17:24:51 +03:00
|
|
|
new_blk = (LPCH)realloc(lpszEnvironmentBlock, new_size * sizeof(CHAR));
|
2015-03-11 09:59:56 +03:00
|
|
|
if (!new_blk)
|
|
|
|
{
|
|
|
|
free(lpszEnvironmentBlock);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
lpszEnvironmentBlock = new_blk;
|
|
|
|
cchEnvironmentBlock = new_size;
|
2013-09-23 22:44:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
p = &(lpszEnvironmentBlock[offset]);
|
|
|
|
|
|
|
|
CopyMemory(p, *envp, length * sizeof(CHAR));
|
|
|
|
p[length] = '\0';
|
|
|
|
|
|
|
|
offset += (length + 1);
|
|
|
|
envp++;
|
|
|
|
}
|
|
|
|
|
|
|
|
lpszEnvironmentBlock[offset] = '\0';
|
|
|
|
|
|
|
|
return lpszEnvironmentBlock;
|
2016-02-06 00:28:45 +03:00
|
|
|
#else
|
|
|
|
return NULL;
|
|
|
|
#endif
|
2012-09-23 06:02:55 +04:00
|
|
|
}
|
|
|
|
|
2014-10-11 03:16:51 +04:00
|
|
|
LPWCH GetEnvironmentStringsW(VOID)
|
2013-10-23 11:58:33 +04:00
|
|
|
{
|
2014-10-11 03:16:51 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL SetEnvironmentStringsA(LPCH NewEnvironment)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL SetEnvironmentStringsW(LPWCH NewEnvironment)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
2013-10-23 13:27:31 +04:00
|
|
|
|
2014-10-11 03:16:51 +04:00
|
|
|
DWORD ExpandEnvironmentStringsA(LPCSTR lpSrc, LPSTR lpDst, DWORD nSize)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD ExpandEnvironmentStringsW(LPCWSTR lpSrc, LPWSTR lpDst, DWORD nSize)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL FreeEnvironmentStringsA(LPCH lpszEnvironmentBlock)
|
|
|
|
{
|
2015-05-11 10:07:39 +03:00
|
|
|
free(lpszEnvironmentBlock);
|
2014-10-11 03:16:51 +04:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL FreeEnvironmentStringsW(LPWCH lpszEnvironmentBlock)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
LPCH MergeEnvironmentStrings(PCSTR original, PCSTR merge)
|
|
|
|
{
|
|
|
|
const char* cp;
|
2013-10-23 11:58:33 +04:00
|
|
|
char* p;
|
2019-10-29 12:18:09 +03:00
|
|
|
size_t offset;
|
|
|
|
size_t length;
|
2013-10-23 13:27:31 +04:00
|
|
|
const char* envp;
|
2013-10-23 11:58:33 +04:00
|
|
|
DWORD cchEnvironmentBlock;
|
|
|
|
LPCH lpszEnvironmentBlock;
|
2014-10-11 03:16:51 +04:00
|
|
|
const char** mergeStrings;
|
2019-10-29 12:18:09 +03:00
|
|
|
size_t mergeStringLength;
|
|
|
|
size_t mergeArraySize = 128;
|
|
|
|
size_t run;
|
|
|
|
size_t mergeLength;
|
|
|
|
size_t foundMerge;
|
2014-10-11 03:16:51 +04:00
|
|
|
char* foundEquals;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
mergeStrings = (LPCSTR*)calloc(mergeArraySize, sizeof(char*));
|
2013-10-23 11:58:33 +04:00
|
|
|
|
2014-10-11 03:16:51 +04:00
|
|
|
if (!mergeStrings)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
mergeStringLength = 0;
|
2013-10-23 11:58:33 +04:00
|
|
|
|
2013-10-23 13:27:31 +04:00
|
|
|
cp = merge;
|
2014-10-11 03:16:51 +04:00
|
|
|
|
|
|
|
while (*cp && *(cp + 1))
|
|
|
|
{
|
2019-10-29 12:18:09 +03:00
|
|
|
length = strlen(cp);
|
2014-10-11 03:16:51 +04:00
|
|
|
|
|
|
|
if (mergeStringLength == mergeArraySize)
|
|
|
|
{
|
2015-03-11 09:59:56 +03:00
|
|
|
const char** new_str;
|
|
|
|
|
2013-10-23 11:58:33 +04:00
|
|
|
mergeArraySize += 128;
|
2019-11-06 17:24:51 +03:00
|
|
|
new_str = (const char**)realloc((void*)mergeStrings, mergeArraySize * sizeof(char*));
|
2013-10-23 11:58:33 +04:00
|
|
|
|
2015-03-11 09:59:56 +03:00
|
|
|
if (!new_str)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
free((void*)mergeStrings);
|
2014-10-11 03:16:51 +04:00
|
|
|
return NULL;
|
2015-03-11 09:59:56 +03:00
|
|
|
}
|
2015-03-13 05:38:07 +03:00
|
|
|
mergeStrings = new_str;
|
2013-10-23 11:58:33 +04:00
|
|
|
}
|
2014-10-11 03:16:51 +04:00
|
|
|
|
|
|
|
mergeStrings[mergeStringLength] = cp;
|
2013-10-23 13:27:31 +04:00
|
|
|
cp += length + 1;
|
2014-10-11 03:16:51 +04:00
|
|
|
mergeStringLength++;
|
2013-10-23 11:58:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
offset = 0;
|
|
|
|
|
|
|
|
cchEnvironmentBlock = 128;
|
2019-11-06 17:24:51 +03:00
|
|
|
lpszEnvironmentBlock = (LPCH)calloc(cchEnvironmentBlock, sizeof(CHAR));
|
2013-10-23 11:58:33 +04:00
|
|
|
|
2014-10-11 03:16:51 +04:00
|
|
|
if (!lpszEnvironmentBlock)
|
2014-11-17 00:47:23 +03:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
free((void*)mergeStrings);
|
2014-10-11 03:16:51 +04:00
|
|
|
return NULL;
|
2014-11-17 00:47:23 +03:00
|
|
|
}
|
2014-10-11 03:16:51 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
envp = original;
|
2013-10-23 11:58:33 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
while ((original != NULL) && (*envp && *(envp + 1)))
|
2013-10-23 11:58:33 +04:00
|
|
|
{
|
2019-10-29 12:18:09 +03:00
|
|
|
size_t old_offset = offset;
|
|
|
|
length = strlen(envp);
|
2013-10-23 11:58:33 +04:00
|
|
|
|
2019-10-29 12:18:09 +03:00
|
|
|
while ((offset + length + 8) > cchEnvironmentBlock)
|
2013-10-23 11:58:33 +04:00
|
|
|
{
|
2014-11-16 19:21:36 +03:00
|
|
|
LPCH tmp;
|
2013-10-23 11:58:33 +04:00
|
|
|
cchEnvironmentBlock *= 2;
|
2019-11-06 17:24:51 +03:00
|
|
|
tmp = (LPCH)realloc(lpszEnvironmentBlock, cchEnvironmentBlock * sizeof(CHAR));
|
2014-10-11 03:16:51 +04:00
|
|
|
|
2014-11-16 19:21:36 +03:00
|
|
|
if (!tmp)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
free((void*)lpszEnvironmentBlock);
|
|
|
|
free((void*)mergeStrings);
|
2014-10-11 03:16:51 +04:00
|
|
|
return NULL;
|
2014-11-16 19:21:36 +03:00
|
|
|
}
|
|
|
|
lpszEnvironmentBlock = tmp;
|
2013-10-23 11:58:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
p = &(lpszEnvironmentBlock[offset]);
|
|
|
|
|
|
|
|
// check if this value is in the mergeStrings
|
|
|
|
foundMerge = 0;
|
2019-11-06 17:24:51 +03:00
|
|
|
for (run = 0; run < mergeStringLength; run++)
|
2014-10-11 03:16:51 +04:00
|
|
|
{
|
|
|
|
if (!mergeStrings[run])
|
2013-10-23 11:58:33 +04:00
|
|
|
continue;
|
2014-10-11 03:16:51 +04:00
|
|
|
|
2019-10-29 12:18:09 +03:00
|
|
|
mergeLength = strlen(mergeStrings[run]);
|
2014-10-11 03:16:51 +04:00
|
|
|
foundEquals = strstr(mergeStrings[run], "=");
|
|
|
|
|
|
|
|
if (!foundEquals)
|
2013-10-23 11:58:33 +04:00
|
|
|
continue;
|
2014-10-11 03:16:51 +04:00
|
|
|
|
|
|
|
if (strncmp(envp, mergeStrings[run], foundEquals - mergeStrings[run] + 1) == 0)
|
|
|
|
{
|
2013-10-23 11:58:33 +04:00
|
|
|
// found variable in merge list ... use this ....
|
2014-10-11 03:16:51 +04:00
|
|
|
if (*(foundEquals + 1) == '\0')
|
|
|
|
{
|
2013-10-23 13:27:31 +04:00
|
|
|
// check if the argument is set ... if not remove variable ...
|
|
|
|
foundMerge = 1;
|
2014-10-11 03:16:51 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-10-29 12:18:09 +03:00
|
|
|
while ((offset + mergeLength + 8) > cchEnvironmentBlock)
|
2013-10-23 13:27:31 +04:00
|
|
|
{
|
2014-11-16 17:13:08 +03:00
|
|
|
LPCH tmp;
|
2013-10-23 13:27:31 +04:00
|
|
|
cchEnvironmentBlock *= 2;
|
2019-11-06 17:24:51 +03:00
|
|
|
tmp =
|
|
|
|
(LPCH)realloc(lpszEnvironmentBlock, cchEnvironmentBlock * sizeof(CHAR));
|
2014-10-11 03:16:51 +04:00
|
|
|
|
2014-11-16 17:13:08 +03:00
|
|
|
if (!tmp)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
free((void*)lpszEnvironmentBlock);
|
|
|
|
free((void*)mergeStrings);
|
2014-10-11 03:16:51 +04:00
|
|
|
return NULL;
|
2014-11-16 17:13:08 +03:00
|
|
|
}
|
|
|
|
lpszEnvironmentBlock = tmp;
|
2014-11-16 18:02:28 +03:00
|
|
|
p = &(lpszEnvironmentBlock[old_offset]);
|
2013-10-23 13:27:31 +04:00
|
|
|
}
|
2014-10-11 03:16:51 +04:00
|
|
|
|
2013-10-23 13:27:31 +04:00
|
|
|
foundMerge = 1;
|
|
|
|
CopyMemory(p, mergeStrings[run], mergeLength);
|
|
|
|
mergeStrings[run] = NULL;
|
|
|
|
p[mergeLength] = '\0';
|
|
|
|
offset += (mergeLength + 1);
|
2013-10-23 11:58:33 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-11 03:16:51 +04:00
|
|
|
if (foundMerge == 0)
|
|
|
|
{
|
2013-10-23 11:58:33 +04:00
|
|
|
CopyMemory(p, envp, length * sizeof(CHAR));
|
|
|
|
p[length] = '\0';
|
|
|
|
offset += (length + 1);
|
|
|
|
}
|
2014-10-11 03:16:51 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
envp += (length + 1);
|
2013-10-23 11:58:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// now merge the not already merged env
|
2019-11-06 17:24:51 +03:00
|
|
|
for (run = 0; run < mergeStringLength; run++)
|
2014-10-11 03:16:51 +04:00
|
|
|
{
|
|
|
|
if (!mergeStrings[run])
|
2013-10-23 11:58:33 +04:00
|
|
|
continue;
|
|
|
|
|
2019-10-29 12:18:09 +03:00
|
|
|
mergeLength = strlen(mergeStrings[run]);
|
2013-10-23 11:58:33 +04:00
|
|
|
|
2019-10-29 12:18:09 +03:00
|
|
|
while ((offset + mergeLength + 8) > cchEnvironmentBlock)
|
2013-10-23 11:58:33 +04:00
|
|
|
{
|
2014-11-16 18:05:01 +03:00
|
|
|
LPCH tmp;
|
2013-10-23 11:58:33 +04:00
|
|
|
cchEnvironmentBlock *= 2;
|
2019-11-06 17:24:51 +03:00
|
|
|
tmp = (LPCH)realloc(lpszEnvironmentBlock, cchEnvironmentBlock * sizeof(CHAR));
|
2014-10-11 03:16:51 +04:00
|
|
|
|
2014-11-16 18:05:01 +03:00
|
|
|
if (!tmp)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
free((void*)lpszEnvironmentBlock);
|
|
|
|
free((void*)mergeStrings);
|
2014-10-11 03:16:51 +04:00
|
|
|
return NULL;
|
2014-11-16 18:05:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
lpszEnvironmentBlock = tmp;
|
2013-10-23 11:58:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
p = &(lpszEnvironmentBlock[offset]);
|
|
|
|
|
|
|
|
CopyMemory(p, mergeStrings[run], mergeLength);
|
|
|
|
mergeStrings[run] = NULL;
|
|
|
|
p[mergeLength] = '\0';
|
|
|
|
offset += (mergeLength + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
lpszEnvironmentBlock[offset] = '\0';
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
free((void*)mergeStrings);
|
2013-10-23 11:58:33 +04:00
|
|
|
|
|
|
|
return lpszEnvironmentBlock;
|
|
|
|
}
|
|
|
|
|
2014-10-11 03:16:51 +04:00
|
|
|
DWORD GetEnvironmentVariableEBA(LPCSTR envBlock, LPCSTR lpName, LPSTR lpBuffer, DWORD nSize)
|
2012-09-23 06:02:55 +04:00
|
|
|
{
|
2019-10-29 12:18:09 +03:00
|
|
|
size_t vLength = 0;
|
2014-10-11 03:16:51 +04:00
|
|
|
char* env = NULL;
|
|
|
|
char* foundEquals;
|
|
|
|
const char* penvb = envBlock;
|
2019-10-29 12:18:09 +03:00
|
|
|
size_t nLength, fLength, lpNameLength;
|
2012-09-23 06:02:55 +04:00
|
|
|
|
2014-10-11 03:16:51 +04:00
|
|
|
if (!lpName || NULL == envBlock)
|
|
|
|
return 0;
|
2012-09-23 06:02:55 +04:00
|
|
|
|
2019-10-29 12:18:09 +03:00
|
|
|
lpNameLength = strlen(lpName);
|
2012-09-23 06:02:55 +04:00
|
|
|
|
2014-10-11 03:16:51 +04:00
|
|
|
if (lpNameLength < 1)
|
|
|
|
return 0;
|
2012-09-23 06:02:55 +04:00
|
|
|
|
2014-10-11 03:16:51 +04:00
|
|
|
while (*penvb && *(penvb + 1))
|
|
|
|
{
|
2019-10-29 12:18:09 +03:00
|
|
|
fLength = strlen(penvb);
|
2019-11-06 17:24:51 +03:00
|
|
|
foundEquals = strstr(penvb, "=");
|
2012-09-23 06:02:55 +04:00
|
|
|
|
2014-10-11 03:16:51 +04:00
|
|
|
if (!foundEquals)
|
|
|
|
{
|
|
|
|
/* if no = sign is found the envBlock is broken */
|
|
|
|
return 0;
|
|
|
|
}
|
2013-09-23 22:44:59 +04:00
|
|
|
|
2019-10-29 12:18:09 +03:00
|
|
|
nLength = (foundEquals - penvb);
|
2014-10-11 03:16:51 +04:00
|
|
|
|
|
|
|
if (nLength != lpNameLength)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
penvb += (fLength + 1);
|
2014-10-11 03:16:51 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strncmp(penvb, lpName, nLength) == 0)
|
|
|
|
{
|
|
|
|
env = foundEquals + 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
penvb += (fLength + 1);
|
2014-10-11 03:16:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!env)
|
|
|
|
return 0;
|
|
|
|
|
2019-10-29 12:18:09 +03:00
|
|
|
vLength = strlen(env);
|
2014-10-11 03:16:51 +04:00
|
|
|
|
2019-10-29 12:18:09 +03:00
|
|
|
if ((vLength + 1 > nSize) || (!lpBuffer))
|
2014-10-11 03:16:51 +04:00
|
|
|
return vLength + 1;
|
|
|
|
|
|
|
|
CopyMemory(lpBuffer, env, vLength + 1);
|
|
|
|
|
|
|
|
return vLength;
|
2012-09-23 06:02:55 +04:00
|
|
|
}
|
|
|
|
|
2014-10-11 03:16:51 +04:00
|
|
|
BOOL SetEnvironmentVariableEBA(LPSTR* envBlock, LPCSTR lpName, LPCSTR lpValue)
|
2012-09-23 06:02:55 +04:00
|
|
|
{
|
2014-10-11 03:16:51 +04:00
|
|
|
int length;
|
|
|
|
char* envstr;
|
|
|
|
char* newEB;
|
2012-09-23 06:02:55 +04:00
|
|
|
|
2014-10-11 03:16:51 +04:00
|
|
|
if (!lpName)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (lpValue)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
length = (int)(strlen(lpName) + strlen(lpValue) + 2); /* +2 because of = and \0 */
|
|
|
|
envstr = (char*)malloc(length + 1); /* +1 because of closing \0 */
|
2014-10-11 03:16:51 +04:00
|
|
|
|
|
|
|
if (!envstr)
|
|
|
|
return FALSE;
|
2012-09-23 06:02:55 +04:00
|
|
|
|
2014-10-11 03:16:51 +04:00
|
|
|
sprintf_s(envstr, length, "%s=%s", lpName, lpValue);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
length = (int)strlen(lpName) + 2; /* +2 because of = and \0 */
|
|
|
|
envstr = (char*)malloc(length + 1); /* +1 because of closing \0 */
|
2014-10-11 03:16:51 +04:00
|
|
|
|
|
|
|
if (!envstr)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
sprintf_s(envstr, length, "%s=", lpName);
|
|
|
|
}
|
|
|
|
|
|
|
|
envstr[length] = '\0';
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
newEB = MergeEnvironmentStrings((LPCSTR)*envBlock, envstr);
|
2014-10-11 03:16:51 +04:00
|
|
|
|
|
|
|
free(envstr);
|
2015-05-11 10:07:39 +03:00
|
|
|
free(*envBlock);
|
2014-10-11 03:16:51 +04:00
|
|
|
|
|
|
|
*envBlock = newEB;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
2015-10-23 19:17:14 +03:00
|
|
|
|
|
|
|
char** EnvironmentBlockToEnvpA(LPCH lpszEnvironmentBlock)
|
|
|
|
{
|
|
|
|
char* p;
|
2019-10-29 12:18:09 +03:00
|
|
|
SSIZE_T index;
|
|
|
|
size_t count;
|
|
|
|
size_t length;
|
2015-10-23 19:17:14 +03:00
|
|
|
char** envp = NULL;
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
if (!lpszEnvironmentBlock)
|
|
|
|
return NULL;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
p = (char*)lpszEnvironmentBlock;
|
2015-10-23 19:17:14 +03:00
|
|
|
|
|
|
|
while (p[0] && p[1])
|
|
|
|
{
|
2019-10-29 12:18:09 +03:00
|
|
|
length = strlen(p);
|
2015-10-23 19:17:14 +03:00
|
|
|
p += (length + 1);
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
index = 0;
|
2019-11-06 17:24:51 +03:00
|
|
|
p = (char*)lpszEnvironmentBlock;
|
2015-10-23 19:17:14 +03:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
envp = (char**)calloc(count + 1, sizeof(char*));
|
2015-10-23 19:17:14 +03:00
|
|
|
if (!envp)
|
|
|
|
return NULL;
|
|
|
|
envp[count] = NULL;
|
|
|
|
|
|
|
|
while (p[0] && p[1])
|
|
|
|
{
|
2019-10-29 12:18:09 +03:00
|
|
|
length = strlen(p);
|
2015-10-23 19:17:14 +03:00
|
|
|
envp[index] = _strdup(p);
|
|
|
|
if (!envp[index])
|
|
|
|
{
|
|
|
|
for (index -= 1; index >= 0; --index)
|
|
|
|
{
|
|
|
|
free(envp[index]);
|
|
|
|
}
|
|
|
|
free(envp);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
p += (length + 1);
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return envp;
|
|
|
|
}
|