From c70559d1288ebe8c97fcd1b024d13f8b0ce6edc1 Mon Sep 17 00:00:00 2001 From: Bernhard Miklautz Date: Fri, 23 Oct 2015 18:17:14 +0200 Subject: [PATCH] winpr API: add EnvironmentBlockToEnvpA * expose EnvironmentBlockToEnvpA * cleanup includes in process.c * removed unused "flag" variable in _CreateProcessExA * make ProcessHandleCloseHandle static --- winpr/include/winpr/environment.h | 2 + winpr/libwinpr/environment/environment.c | 49 ++++++++++++++++ winpr/libwinpr/thread/process.c | 71 +----------------------- 3 files changed, 53 insertions(+), 69 deletions(-) diff --git a/winpr/include/winpr/environment.h b/winpr/include/winpr/environment.h index d6329d827..34d9fb03c 100644 --- a/winpr/include/winpr/environment.h +++ b/winpr/include/winpr/environment.h @@ -110,6 +110,8 @@ WINPR_API LPCH MergeEnvironmentStrings(PCSTR original, PCSTR merge); WINPR_API DWORD GetEnvironmentVariableEBA(LPCSTR envBlock, LPCSTR lpName, LPSTR lpBuffer, DWORD nSize); WINPR_API BOOL SetEnvironmentVariableEBA(LPSTR* envBlock, LPCSTR lpName, LPCSTR lpValue); +WINPR_API char** EnvironmentBlockToEnvpA(LPCH lpszEnvironmentBlock); + #ifdef __cplusplus } #endif diff --git a/winpr/libwinpr/environment/environment.c b/winpr/libwinpr/environment/environment.c index dcdb24f30..58abed849 100644 --- a/winpr/libwinpr/environment/environment.c +++ b/winpr/libwinpr/environment/environment.c @@ -572,3 +572,52 @@ BOOL SetEnvironmentVariableEBA(LPSTR* envBlock, LPCSTR lpName, LPCSTR lpValue) return TRUE; } + +char** EnvironmentBlockToEnvpA(LPCH lpszEnvironmentBlock) +{ + char* p; + int index; + int count; + int length; + char** envp = NULL; + + count = 0; + if (!lpszEnvironmentBlock) + return NULL; + + p = (char*) lpszEnvironmentBlock; + + while (p[0] && p[1]) + { + length = strlen(p); + p += (length + 1); + count++; + } + + index = 0; + p = (char*) lpszEnvironmentBlock; + + envp = (char**) calloc(count + 1, sizeof(char*)); + if (!envp) + return NULL; + envp[count] = NULL; + + while (p[0] && p[1]) + { + length = strlen(p); + 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; +} diff --git a/winpr/libwinpr/thread/process.c b/winpr/libwinpr/thread/process.c index 52aac7728..2c179c77b 100644 --- a/winpr/libwinpr/thread/process.c +++ b/winpr/libwinpr/thread/process.c @@ -26,7 +26,6 @@ #include "../handle/nonehandle.h" #include -#include /** * CreateProcessA @@ -54,30 +53,16 @@ #ifndef _WIN32 -#ifdef HAVE_UNISTD_H -#include -#endif - #include -#include #include -#include #include -#include #include -#include -#include #include -#include -#include - -#include #include "thread.h" -#include "../handle/handle.h" #include "../security/security.h" #ifndef NSIG @@ -88,55 +73,6 @@ #endif #endif -char** EnvironmentBlockToEnvpA(LPCH lpszEnvironmentBlock) -{ - char* p; - int index; - int count; - int length; - char** envp = NULL; - - count = 0; - if (!lpszEnvironmentBlock) - return NULL; - - p = (char*) lpszEnvironmentBlock; - - while (p[0] && p[1]) - { - length = strlen(p); - p += (length + 1); - count++; - } - - index = 0; - p = (char*) lpszEnvironmentBlock; - - envp = (char**) calloc(count + 1, sizeof(char*)); - if (!envp) - return NULL; - envp[count] = NULL; - - while (p[0] && p[1]) - { - length = strlen(p); - 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; -} - /** * If the file name does not contain a directory path, the system searches for the executable file in the following sequence: * @@ -151,7 +87,7 @@ char** EnvironmentBlockToEnvpA(LPCH lpszEnvironmentBlock) * this per-application path in the search sequence, use the ShellExecute function. */ -char* FindApplicationPath(char* application) +static char* FindApplicationPath(char* application) { char* path; char* save; @@ -208,7 +144,6 @@ BOOL _CreateProcessExA(HANDLE hToken, DWORD dwLogonFlags, LPCSTR lpCurrentDirectory, LPSTARTUPINFOA lpStartupInfo, LPPROCESS_INFORMATION lpProcessInformation) { pid_t pid; - int flags; int numArgs; LPSTR* pArgs = NULL; char** envp = NULL; @@ -230,7 +165,6 @@ BOOL _CreateProcessExA(HANDLE hToken, DWORD dwLogonFlags, if (!pArgs) return FALSE; - flags = 0; token = (WINPR_ACCESS_TOKEN*) hToken; @@ -532,7 +466,7 @@ BOOL TerminateProcess(HANDLE hProcess, UINT uExitCode) } -BOOL ProcessHandleCloseHandle(HANDLE handle) +static BOOL ProcessHandleCloseHandle(HANDLE handle) { WINPR_PROCESS* process = (WINPR_PROCESS*) handle; free(process); @@ -586,6 +520,5 @@ HANDLE CreateProcessHandle(pid_t pid) return (HANDLE)process; } - #endif