-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathceprocess.c
138 lines (110 loc) · 2.85 KB
/
ceprocess.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// ceprocess.h
//
// Time-stamp: <28/01/01 18:24:04 keuchel@lightning>
#include "celib.h"
#include "sys/stat.h"
int
XCESearchPathA(const char *pathlist, const char *file, char *retpath)
{
char path[MAX_PATH];
char *ps, *pe;
char *pl;
struct xcestat st;
char *pend;
if(pathlist == NULL)
return -1;
ps = pe = pl = xcestrdup(pathlist);
pend = pl + strlen(pl);
while(ps < pend)
{
while(*pe && *pe != ';')
pe++;
*pe++ = 0;
sprintf(path, "%s\\%s", ps, file);
//printf("SearchPath: %s\n", path);
if(xcestat(path, &st) == 0)
{
strcpy(retpath, path);
free(pl);
return 0;
}
ps = pe;
}
free(pl);
return -1;
}
int
XCEExecuteProcessA(const char *commandline, BOOL bWait, LPDWORD lpdwProcId)
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
wchar_t wszAppName[256];
char szAppName[126];
wchar_t wszCommandLine[256];
char szCommandLine[256];
char *p;
DWORD dwExitCode = 0;
char path[MAX_PATH];
char *d;
char szCurrentDir[MAX_PATH];
memset(&si, 0, sizeof(si));
si.cb = sizeof(si);
for(p = commandline, d = szAppName; *p && *p != ' ';)
*d++ = *p++;
*d++ = 0;
while(*p == ' ')
p++;
strcpy(szCommandLine, p);
for(p = szAppName; *p; p++)
{
if(*p == '/')
*p = '\\';
}
if(szAppName[0] != '\\')
{
if(strstr(szAppName, ".exe") == NULL)
strcat(szAppName, ".exe");
if(XCESearchPathA(xcegetenv("PATH"), szAppName, path) == 0)
strcpy(szAppName, path);
}
//printf("App: %s Command: %s\n", szAppName, szCommandLine);
MultiByteToWideChar(CP_ACP, 0, szAppName, -1,
wszAppName, sizeof(wszAppName)/2);
MultiByteToWideChar(CP_ACP, 0, szCommandLine, -1,
wszCommandLine, sizeof(wszCommandLine)/2);
// pass path to child...
XCEGetCurrentDirectoryA(sizeof(szCurrentDir), szCurrentDir);
XCESetEnvironmentVariableInRegA("CHILDPATH", szCurrentDir);
// PWD is better, as this is checked by some unix programs...
XCESetEnvironmentVariableInRegA("PWD", szCurrentDir);
if(CreateProcessW(wszAppName, wszCommandLine, NULL, NULL,
FALSE, 0, NULL, NULL, &si, &pi) == FALSE)
{
return -1;
}
if(bWait)
{
WaitForSingleObject(pi.hProcess, INFINITE);
GetExitCodeProcess(pi.hProcess, &dwExitCode);
}
else
{
if(lpdwProcId)
*lpdwProcId = pi.dwProcessId;
}
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
return dwExitCode;
}
int
XCEWaitProcess(DWORD dwProcId)
{
HANDLE hProcess;
DWORD dwExitCode;
if((hProcess = OpenProcess(0, FALSE, dwProcId)) == NULL)
return -1;
WaitForSingleObject(hProcess, INFINITE);
GetExitCodeProcess(hProcess, &dwExitCode);
CloseHandle(hProcess);
return dwExitCode;
}