-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNoExtRun.cpp
More file actions
165 lines (151 loc) · 3.96 KB
/
Copy pathNoExtRun.cpp
File metadata and controls
165 lines (151 loc) · 3.96 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <windows.h>
#include <shlwapi.h>
void printError(const std::string & function)
{
DWORD dw = GetLastError();
char * lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), reinterpret_cast<char *>(&lpMsgBuf), 0, nullptr);
std::cerr << function << "() failed. " << lpMsgBuf;
LocalFree(lpMsgBuf);
}
bool doesFileExist(const std::string & filename)
{
return PathFileExists(filename.data()) == TRUE;
}
std::string getEnvironmentVariable(const std::string & variable)
{
char buffer[32767] = { 0 };
DWORD result = GetEnvironmentVariable(variable.data(), buffer, sizeof(buffer));
if (result == 0)
{
printError("");
return {};
}
else if (result > sizeof(buffer))
{
return {};
}
else
{
return buffer;
}
}
std::vector<std::string> getPaths()
{
std::vector<std::string> result;
std::string path_variable = getEnvironmentVariable("PATH");
std::istringstream path_stream(path_variable);
std::string single_path;
while (std::getline(path_stream, single_path, ';'))
{
result.emplace_back(std::move(single_path));
}
return result;
}
std::string searchExecutable(const std::string & executable)
{
auto paths = getPaths();
for (auto & path : paths)
{
std::string candidate = path + '\\' + executable;
if (doesFileExist(candidate))
{
return candidate;
}
}
return executable;
}
std::string getArguments(const std::string & command)
{
bool in_quote = false;
for (size_t i = 0; i < command.size(); ++i)
{
char c = command[i];
if (c == '"')
{
in_quote = !in_quote;
}
else if (c == ' ')
{
if (!in_quote)
{
return command.substr(i + 1);
}
}
else if (c == '\\')
{
++i;
}
}
return {};
}
std::string trimLeft(const std::string & argument)
{
auto pos = argument.find_first_not_of(' ');
if (pos == std::string::npos)
{
return {};
}
else
{
return argument.substr(pos);
}
}
std::string trimRight(const std::string & argument)
{
auto pos = argument.find_last_not_of(' ');
if (pos == std::string::npos)
{
return {};
}
else
{
return argument.substr(0, pos + 1);
}
}
int main()
{
std::string arguments = trimLeft(getArguments(GetCommandLine())); /// We need trimming because extra space is being added before an argument if running as "cmd /c smth".
if (arguments.empty())
{
std::cerr << "Usage:\n";
std::cerr << "noextrun program [arguments...]\n";
return 0;
}
std::string sub_arguments = getArguments(arguments);
std::string executable = trimRight(arguments.substr(0, arguments.size() - sub_arguments.size()));
if (!doesFileExist(executable))
{
arguments = searchExecutable(executable) + ' ' + sub_arguments; /// Manually search in PATH to give first priority to files without extension.
}
PROCESS_INFORMATION pi;
STARTUPINFO si = { 0 };
si.cb = sizeof(si);
std::vector<char> arguments_data(arguments.begin(), arguments.end());
arguments_data.push_back(0);
bool res = CreateProcess(nullptr, &arguments_data[0], nullptr, nullptr, false, NORMAL_PRIORITY_CLASS, nullptr, nullptr, &si, &pi);
if (!res)
{
printError("CreateProcess");
return 1;
}
else
{
WaitForSingleObject(pi.hProcess, INFINITE);
DWORD code;
res = GetExitCodeProcess(pi.hProcess, &code);
if (!res)
{
printError("GetExitCodeProcess");
return 2;
}
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return code;
}
}