On Linux, you can search the binaries in the environment PATH variable using the command whereis or which. In here, a simple whereis/which implementation using the Windows Batch command is given.
The implementation of this utility is straightforward. First you need to get the environmental PATH variable (as a string), then on Windows, you need to split the string by the ‘;’ into multiple directories. You then have to go through each directory to see if the given file exists in that directory.
The following C++ utility prints only the first encountered directory but you can print all by removing the ‘break’ if necessary. The current directory is checked first since it is not on the PATH string.
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 | /* whereis implementation for windows https://helloacm.com Usage Example: whereis php.exe cmd.exe notepad.exe */ #include <iostream> #include <fstream> #include <windows.h> using namespace std; bool is_file_exist(const char *fileName) { std::ifstream infile(fileName); return infile.good(); } int main(int argc, char *argv[]) { if (argc <= 1) { cout << "whereis for Windows: by https://helloacm.com" << endl; cout << "Example: whereis php.exe cmd.exe" << endl; #ifdef _WIN64 cout << "Platform: x64" << endl; #else cout << "Platform: x86" << endl; #endif return -1; } const int MAXN = 32767; char path[MAXN]; int x = GetEnvironmentVariableA("PATH", (path), MAXN); for (int j = 1; j < argc; ++j) { bool found = false; if (is_file_exist(argv[j])) { cout << ".\\" << argv[j] << endl; found = true; break; // current folder has priority over system path } string cur = ""; for (int i = 0; i < x; ++i) { char n = path[i]; if (n == ';') { if (cur.at(cur.length() - 1) != '\\') { cur.append("\\"); // append tail slash } cur.append(argv[j]); if (is_file_exist(cur.c_str())) { cout << cur.c_str() << endl; found = true; break; // only find the first in path } cur = ""; } else { cur += n; } } if (!found) { cout << "Not Found: " << argv[j] << endl; } } return 0; } |
/* whereis implementation for windows https://helloacm.com Usage Example: whereis php.exe cmd.exe notepad.exe */ #include <iostream> #include <fstream> #include <windows.h> using namespace std; bool is_file_exist(const char *fileName) { std::ifstream infile(fileName); return infile.good(); } int main(int argc, char *argv[]) { if (argc <= 1) { cout << "whereis for Windows: by https://helloacm.com" << endl; cout << "Example: whereis php.exe cmd.exe" << endl; #ifdef _WIN64 cout << "Platform: x64" << endl; #else cout << "Platform: x86" << endl; #endif return -1; } const int MAXN = 32767; char path[MAXN]; int x = GetEnvironmentVariableA("PATH", (path), MAXN); for (int j = 1; j < argc; ++j) { bool found = false; if (is_file_exist(argv[j])) { cout << ".\\" << argv[j] << endl; found = true; break; // current folder has priority over system path } string cur = ""; for (int i = 0; i < x; ++i) { char n = path[i]; if (n == ';') { if (cur.at(cur.length() - 1) != '\\') { cur.append("\\"); // append tail slash } cur.append(argv[j]); if (is_file_exist(cur.c_str())) { cout << cur.c_str() << endl; found = true; break; // only find the first in path } cur = ""; } else { cur += n; } } if (!found) { cout << "Not Found: " << argv[j] << endl; } } return 0; }
The above is compiled at Visual Studio C++ 2015 for both 32 bit and 64 bit. It is also compilable under CodeBlocks+MingGW C++ compiler. The pre-compiled binaries for both x86 and x64 can be downloaded here (7zip format, 22 KB only).
You can give multiple filenames to search in the command line, the example screen shots are given below:
–EOF (The Ultimate Computing & Technology Blog) —
loading...
Last Post: How to Find All Numbers Disappeared in an Array? (C/C++ Coding Exercise)
Next Post: An Interview Question: O(n) Algorithm to Find Abs(Max Left - Max Right)