diff --git a/src/core/copyright/copyright.cpp b/src/core/copyright/copyright.cpp index 673c841..e9481c9 100644 --- a/src/core/copyright/copyright.cpp +++ b/src/core/copyright/copyright.cpp @@ -11,17 +11,36 @@ namespace linuxdeploy { using namespace log; std::shared_ptr ICopyrightFilesManager::getInstance() { - auto check_command = [](const std::string& command) { - auto p = subprocess::Popen( - command, - subprocess::output(subprocess::PIPE), - subprocess::error(subprocess::PIPE) - ); + // very simple but for our purposes good enough which like algorithm to find binaries in $PATH + // TODO: move into separate function to be used in the entire code base + // FIXME: remove dependency on subprocess library + static const auto which = [](const std::string& name) -> bf::path { + const auto* path = getenv("PATH"); - return p.wait(); + if (path == nullptr) + return ""; + + for (const auto& binDir : subprocess::util::split(path, ":")) { + if (!bf::is_directory(binDir)) { + continue; + } + + for (bf::directory_iterator it(binDir); it != bf::directory_iterator{}; ++it) { + const auto binary = it->path(); + + std::cout << binary << std::endl; + + if (binary.filename() == name) { + // TODO: check if file is executable (skip otherwise) + return binary; + } + } + } + + return {}; }; - if (check_command("command -v dpkg-query") == 0) { + if (!which("dpkg-query").empty()) { ldLog() << LD_DEBUG << "Using dpkg-query to search for copyright files" << std::endl; return std::make_shared(); }