-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlistcmd.cpp
More file actions
252 lines (224 loc) · 10.2 KB
/
Copy pathlistcmd.cpp
File metadata and controls
252 lines (224 loc) · 10.2 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include <windows.h>
#include <algorithm>
#include <cstdio>
#include <cwctype>
#include <fcntl.h>
#include <filesystem>
#include <io.h>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
namespace fs = std::filesystem;
// internal commands are compiled into cmd.exe itself, there is no way to enumerate them at runtime, so this list is static (Win11 cmd)
static const std::vector<std::wstring> CMD_BUILTINS = {
L"assoc", L"break", L"call", L"cd", L"chdir", L"cls", L"color", L"copy", L"date",
L"del", L"dir", L"dpath", L"echo", L"endlocal", L"erase", L"exit", L"for", L"ftype",
L"goto", L"if", L"md", L"mkdir", L"mklink", L"move", L"path", L"pause", L"popd",
L"prompt", L"pushd", L"rd", L"rem", L"ren", L"rename", L"rmdir", L"set", L"setlocal",
L"shift", L"start", L"time", L"title", L"type", L"ver", L"verify", L"vol",
};
static const std::wstring FOOTER_SEP = L"...................................";
struct Row {
std::wstring name;
std::wstring path;
bool shadowed;
};
static std::wstring lower(std::wstring s) {
for (wchar_t& c : s) c = towlower(c);
return s;
}
static bool iless(const std::wstring& a, const std::wstring& b) {
return lower(a) < lower(b);
}
static std::wstring getenv_w(const wchar_t* name, const wchar_t* fallback) {
wchar_t buf[32768];
DWORD n = GetEnvironmentVariableW(name, buf, 32768);
if (n == 0 || n >= 32768) return fallback;
return std::wstring(buf, n);
}
static std::wstring expand_env(const std::wstring& s) {
wchar_t buf[32768];
DWORD n = ExpandEnvironmentStringsW(s.c_str(), buf, 32768);
if (n == 0 || n > 32768) return s;
return std::wstring(buf);
}
static std::wstring trim(const std::wstring& s, const wchar_t* junk = L" \t") {
size_t a = s.find_first_not_of(junk);
size_t b = s.find_last_not_of(junk);
if (a == std::wstring::npos) return L"";
return s.substr(a, b - a + 1);
}
static std::vector<std::wstring> split(const std::wstring& s, wchar_t sep) {
std::vector<std::wstring> out;
size_t start = 0;
while (start <= s.size()) {
size_t pos = s.find(sep, start);
if (pos == std::wstring::npos) { out.push_back(s.substr(start)); break; }
out.push_back(s.substr(start, pos - start));
start = pos + 1;
}
return out;
}
// doskey writes through the pipe in the OEM codepage, so bytes are decoded with CP_OEMCP before use
static std::wstring from_oem(const std::string& s) {
if (s.empty()) return L"";
int n = MultiByteToWideChar(CP_OEMCP, 0, s.data(), (int)s.size(), nullptr, 0);
std::wstring w(n, 0);
MultiByteToWideChar(CP_OEMCP, 0, s.data(), (int)s.size(), w.data(), n);
return w;
}
static std::vector<std::pair<std::wstring, std::wstring>> get_doskey_macros() {
// only macros loaded by the AutoRun registry value show up here, macros defined by hand in another live cmd window are per-session and invisible from outside
std::vector<std::pair<std::wstring, std::wstring>> macros;
FILE* pipe = _popen("cmd /c doskey /macros", "r");
if (!pipe) return macros;
char line[4096];
while (fgets(line, sizeof(line), pipe)) {
std::string raw(line);
while (!raw.empty() && (raw.back() == '\n' || raw.back() == '\r')) raw.pop_back();
std::wstring s = from_oem(raw);
size_t eq = s.find(L'=');
if (eq != std::wstring::npos && eq > 0) {
macros.push_back({trim(s.substr(0, eq)), trim(s.substr(eq + 1))});
}
}
_pclose(pipe);
return macros;
}
static void scan_path(std::vector<Row>& rows, std::map<std::wstring, std::wstring>& winners) {
std::wstring pathext = getenv_w(L"PATHEXT", L".COM;.EXE;.BAT;.CMD");
std::map<std::wstring, int> ext_rank;
int rank = 0;
for (const std::wstring& e : split(pathext, L';')) {
std::wstring ext = lower(trim(e));
if (!ext.empty() && ext[0] == L'.' && !ext_rank.count(ext)) ext_rank[ext] = rank++;
}
std::wstring path = getenv_w(L"PATH", L"");
std::set<std::wstring> seen_dirs;
for (const std::wstring& raw : split(path, L';')) {
std::wstring d = expand_env(trim(raw, L" \t\""));
if (d.empty()) continue;
// PATH often lists the same directory twice (user + system half), scanning it again would fake duplicates
std::wstring norm = lower(d);
std::replace(norm.begin(), norm.end(), L'/', L'\\');
while (norm.size() > 3 && norm.back() == L'\\') norm.pop_back();
if (!seen_dirs.insert(norm).second) continue;
std::vector<fs::path> entries;
std::error_code ec;
for (fs::directory_iterator it(d, ec), end; !ec && it != end; it.increment(ec)) {
std::error_code fec;
if (!it->is_regular_file(fec)) continue;
std::wstring ext = lower(it->path().extension().wstring());
if (ext_rank.count(ext)) entries.push_back(it->path());
}
std::stable_sort(entries.begin(), entries.end(), [&](const fs::path& a, const fs::path& b) {
return ext_rank[lower(a.extension().wstring())] < ext_rank[lower(b.extension().wstring())];
});
for (const fs::path& f : entries) {
std::wstring stem = lower(f.stem().wstring());
bool shadowed = winners.count(stem) > 0;
if (!shadowed) winners[stem] = f.wstring();
rows.push_back({f.filename().wstring(), f.wstring(), shadowed});
}
}
}
static std::vector<std::wstring> unique_names(const std::map<std::wstring, std::wstring>& winners, const std::vector<std::pair<std::wstring, std::wstring>>& macros) {
std::set<std::wstring> seen;
std::vector<std::wstring> names;
auto add = [&](const std::wstring& n) {
if (seen.insert(lower(n)).second) names.push_back(n);
};
for (const std::wstring& b : CMD_BUILTINS) add(b);
for (const auto& m : macros) add(m.first);
for (const auto& w : winners) add(fs::path(w.second).filename().wstring());
std::sort(names.begin(), names.end(), iless);
return names;
}
static std::map<std::wstring, std::vector<std::wstring>> dup_groups(const std::vector<Row>& rows) {
std::map<std::wstring, std::vector<std::wstring>> groups;
for (const Row& r : rows) groups[lower(r.name)].push_back(r.path);
std::map<std::wstring, std::vector<std::wstring>> dups;
for (const auto& g : groups) {
if (g.second.size() > 1) dups[g.first] = g.second;
}
return dups;
}
static void print_names(const std::map<std::wstring, std::wstring>& winners, const std::vector<std::pair<std::wstring, std::wstring>>& macros) {
// only the file that wins bare-name resolution is listed, a powershell.exe shadowed by an earlier powershell.bat stays hidden
std::vector<std::wstring> names = unique_names(winners, macros);
for (const std::wstring& n : names) std::wcout << n << L"\n";
std::wcout << FOOTER_SEP << L"\n";
std::wcout << names.size() << L" unique commands found\n";
}
static void print_details(const std::vector<Row>& rows, const std::map<std::wstring, std::wstring>& winners, const std::vector<std::pair<std::wstring, std::wstring>>& macros) {
std::wcout << L"== cmd internal commands ==\n";
for (const std::wstring& b : CMD_BUILTINS) std::wcout << b << L"\n";
std::wcout << L"\n== doskey macros (aliases) ==\n";
if (macros.empty()) std::wcout << L"(none registered via AutoRun)\n";
for (const auto& m : macros) std::wcout << m.first << L"\t" << m.second << L"\n";
std::wcout << L"\n== executables on PATH ==\n";
std::vector<Row> sorted_rows = rows;
std::stable_sort(sorted_rows.begin(), sorted_rows.end(), [](const Row& a, const Row& b) { return iless(a.name, b.name); });
for (const Row& r : sorted_rows) {
std::wcout << r.name << L"\t" << r.path << L"\t" << (r.shadowed ? L"SHADOWED" : L"") << L"\n";
}
std::wcout << L"\nbuiltins: " << CMD_BUILTINS.size() << L", macros: " << macros.size() << L", files on PATH: " << rows.size() << L"\n";
std::wcout << FOOTER_SEP << L"\n";
std::wcout << unique_names(winners, macros).size() << L" unique commands found\n";
std::wcout << dup_groups(rows).size() << L" duplicates found\n";
}
static void print_dups(const std::vector<Row>& rows) {
auto dups = dup_groups(rows);
bool first = true;
for (const auto& g : dups) {
if (!first) std::wcout << L"\n========\n\n";
first = false;
std::wcout << fs::path(g.second[0]).filename().wstring() << L" [" << g.second.size() << L"]\n";
for (const std::wstring& p : g.second) std::wcout << p << L"\n";
}
if (dups.empty()) std::wcout << L"(no duplicates found)\n";
std::wcout << FOOTER_SEP << L"\n";
std::wcout << dups.size() << L" duplicates found\n";
}
static void print_help() {
std::wcout <<
L"listcmd - list everything callable from Windows cmd\n"
L"\n"
L"usage: listcmd [--details | --dup | --help]\n"
L"\n"
L" (no args) unique callable names only: builtins, doskey macros, and the\n"
L" PATH file that wins bare-name resolution for each name\n"
L" --details full report: builtins, macros, and every PATH file with its\n"
L" location and a SHADOWED tag\n"
L" --dup only names present in more than one PATH directory, each with\n"
L" its count and every path, in PATH resolution order\n"
L" --help this help\n";
}
int wmain(int argc, wchar_t* argv[]) {
_setmode(_fileno(stdout), _O_U8TEXT);
_setmode(_fileno(stderr), _O_U8TEXT);
bool details = false, dup = false;
for (int i = 1; i < argc; i++) {
std::wstring a = argv[i];
if (a == L"--details") details = true;
else if (a == L"--dup") dup = true;
else if (a == L"--help" || a == L"-h") { print_help(); return 0; }
else {
std::wcerr << L"unknown argument: " << a << L"\ntry --help\n";
return 2;
}
}
if (details && dup) {
std::wcerr << L"--details and --dup are mutually exclusive\n";
return 2;
}
std::vector<Row> rows;
std::map<std::wstring, std::wstring> winners;
scan_path(rows, winners);
if (dup) print_dups(rows);
else if (details) print_details(rows, winners, get_doskey_macros());
else print_names(winners, get_doskey_macros());
return 0;
}