-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExecutableLookup.hpp
More file actions
129 lines (107 loc) · 2.91 KB
/
Copy pathExecutableLookup.hpp
File metadata and controls
129 lines (107 loc) · 2.91 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
/**
*
* @file ExecutableLookup.cpp
* @author Gaspard Kirira
*
* Copyright 2026, Gaspard Kirira. All rights reserved.
* https://github.com/vixcpp/vix
* Use of this source code is governed by a MIT license
* that can be found in the License file.
*
* Vix.cpp
*
* Build graph task model
*
*/
#ifndef VIX_ENGINE_EXECUTABLE_LOOKUP_HPP
#define VIX_ENGINE_EXECUTABLE_LOOKUP_HPP
#include <cstdlib>
#include <filesystem>
#include <string>
#include <string_view>
#include <system_error>
#include <vector>
namespace vix::engine::detail
{
namespace fs = std::filesystem;
inline bool is_executable_file(const fs::path &path)
{
std::error_code ec;
if (!fs::exists(path, ec) || ec)
return false;
if (!fs::is_regular_file(path, ec) || ec)
return false;
#ifdef _WIN32
return true;
#else
const fs::perms perms = fs::status(path, ec).permissions();
if (ec)
return false;
constexpr fs::perms executable =
fs::perms::owner_exec |
fs::perms::group_exec |
fs::perms::others_exec;
return (perms & executable) != fs::perms::none;
#endif
}
inline std::vector<std::string> executable_names(std::string_view name)
{
std::vector<std::string> names;
names.emplace_back(name);
#ifdef _WIN32
const fs::path input{std::string(name)};
if (input.has_extension())
return names;
const char *pathext = std::getenv("PATHEXT");
const std::string extensions =
(pathext && *pathext) ? std::string(pathext) : ".COM;.EXE;.BAT;.CMD";
std::size_t start = 0;
while (start <= extensions.size())
{
std::size_t end = extensions.find(';', start);
if (end == std::string::npos)
end = extensions.size();
std::string ext = extensions.substr(start, end - start);
if (!ext.empty())
names.emplace_back(std::string(name) + ext);
start = end + 1;
}
#endif
return names;
}
inline bool executable_on_path(std::string_view name)
{
if (name.empty())
return false;
const char *pathEnv = std::getenv("PATH");
if (!pathEnv || !*pathEnv)
return false;
#ifdef _WIN32
constexpr char separator = ';';
#else
constexpr char separator = ':';
#endif
const std::string_view pathValue(pathEnv);
const std::vector<std::string> names = executable_names(name);
std::size_t start = 0;
while (start <= pathValue.size())
{
std::size_t end = pathValue.find(separator, start);
if (end == std::string_view::npos)
end = pathValue.size();
const std::string_view dir = pathValue.substr(start, end - start);
if (!dir.empty())
{
const fs::path directory{std::string(dir)};
for (const std::string &candidateName : names)
{
if (is_executable_file(directory / candidateName))
return true;
}
}
start = end + 1;
}
return false;
}
} // namespace vix::engine::detail
#endif