From 3626877554b6c567d43336ec49414cedfe487260 Mon Sep 17 00:00:00 2001 From: Klaus Aehlig Date: Fri, 31 May 2024 18:30:10 +0200 Subject: Initial commit --- src/withExtendedPath/TARGETS | 6 ++++++ src/withExtendedPath/extend-path.cc | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 src/withExtendedPath/TARGETS create mode 100644 src/withExtendedPath/extend-path.cc (limited to 'src/withExtendedPath') diff --git a/src/withExtendedPath/TARGETS b/src/withExtendedPath/TARGETS new file mode 100644 index 0000000..e226541 --- /dev/null +++ b/src/withExtendedPath/TARGETS @@ -0,0 +1,6 @@ +{ "": + { "type": ["@", "rules", "CC", "binary"] + , "name": ["withExtendedPath"] + , "srcs": ["extend-path.cc"] + } +} diff --git a/src/withExtendedPath/extend-path.cc b/src/withExtendedPath/extend-path.cc new file mode 100644 index 0000000..a466728 --- /dev/null +++ b/src/withExtendedPath/extend-path.cc @@ -0,0 +1,37 @@ +#include +#include +#include +#include +#include + +int main(int argc, char* argv[]) { + constexpr int kForkFailed = 65; + constexpr int kPrerequisiteError = 97; + auto kEnv = std::string{"/usr/bin/env"}; + + if (argc < 3) { + return kPrerequisiteError; + } + + // compute PATH + auto path = std::string{argv[1]}; + auto const* env_path = std::getenv("PATH"); + if (env_path && (*env_path != '\0')) { + path = std::string{env_path} + std::string{":"} + path; + } + auto path_arg = std::string{"PATH="} + path; + + // create new argument vector + std::vector nargv{}; + nargv.reserve(argc+3); + nargv.push_back(kEnv.data()); + nargv.push_back(path_arg.data()); + for (int i=2; i < argc; i++) { + nargv.push_back(argv[i]); + } + nargv.push_back(nullptr); + + // exec + (void) execvp(nargv[0], static_cast(nargv.data())); + return kForkFailed; +} -- cgit v1.2.3