Inside Bitcoin
Opening the selected file…
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2025-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <util/exec.h>7#include <util/fs.h>8#ifdef WIN329#include <util/subprocess.h>10#endif12#include <cstdlib>13#include <string>14#include <system_error>16#ifdef WIN3217#include <process.h>18#include <windows.h>19#else20#include <unistd.h>21#endif23namespace util {24int ExecVp(const char* file, char* const argv[])25{26#ifndef WIN3227return execvp(file, argv);28#else29std::vector<std::string> escaped_args;30for (char* const* arg_ptr{argv}; *arg_ptr; ++arg_ptr) {31subprocess::util::quote_argument(std::string{*arg_ptr}, escaped_args.emplace_back(), /*force=*/false);32}34std::vector<const char*> new_argv;35new_argv.reserve(escaped_args.size() + 1);36for (const auto& s : escaped_args) new_argv.push_back(s.c_str());37new_argv.push_back(nullptr);38return _execvp(file, new_argv.data());39#endif40}42fs::path GetExePath(std::string_view argv0)43{44// Try to figure out where executable is located. This does a simplified45// search that won't work perfectly on every platform and doesn't need to,46// as it is only currently being used in a convenience wrapper binary to try47// to prioritize locally built or installed executables over system48// executables.49const fs::path argv0_path{fs::PathFromString(std::string{argv0})};50fs::path path{argv0_path};51std::error_code ec;52#ifndef WIN3253// If argv0 doesn't contain a path separator, it was invoked from the system54// PATH and can be searched for there.55if (!argv0_path.has_parent_path()) {56if (const char* path_env = std::getenv("PATH")) {57size_t start{0}, end{0};58for (std::string_view paths{path_env}; end != std::string_view::npos; start = end + 1) {59end = paths.find(':', start);60fs::path candidate = fs::path(paths.substr(start, end - start)) / argv0_path;61if (fs::is_regular_file(candidate, ec)) {62path = candidate;63break;64}65}66}67}68#else69wchar_t module_path[MAX_PATH];70if (GetModuleFileNameW(nullptr, module_path, MAX_PATH) > 0) {71path = fs::path{module_path};72}73#endif74return path;75}77} // namespace util