diff options
author | Klaus Aehlig <klaus.aehlig@huawei.com> | 2024-05-31 18:30:10 +0200 |
---|---|---|
committer | Klaus Aehlig <klaus.aehlig@huawei.com> | 2024-06-03 10:09:24 +0200 |
commit | 3626877554b6c567d43336ec49414cedfe487260 (patch) | |
tree | f80c51ec1867fea029b955d85e89e39017f920e3 /src | |
download | hello-nix-3626877554b6c567d43336ec49414cedfe487260.tar.gz |
Initial commit
Diffstat (limited to 'src')
-rw-r--r-- | src/hello/TARGETS | 9 | ||||
-rw-r--r-- | src/hello/hello.cpp | 11 | ||||
-rw-r--r-- | src/proto/TARGETS | 18 | ||||
-rw-r--r-- | src/proto/example.proto | 7 | ||||
-rw-r--r-- | src/proto/read.cc | 18 | ||||
-rw-r--r-- | src/proto/write.cc | 17 | ||||
-rw-r--r-- | src/shell/TARGETS | 13 | ||||
-rw-r--r-- | src/withExtendedPath/TARGETS | 6 | ||||
-rw-r--r-- | src/withExtendedPath/extend-path.cc | 37 |
9 files changed, 136 insertions, 0 deletions
diff --git a/src/hello/TARGETS b/src/hello/TARGETS new file mode 100644 index 0000000..80380c7 --- /dev/null +++ b/src/hello/TARGETS @@ -0,0 +1,9 @@ +{ "": + { "type": ["@", "rules", "CC", "binary"] + , "name": ["hello"] + , "srcs": ["hello.cpp"] + , "private-deps": ["fmt"] + } +, "fmt": + {"type": ["@", "rules", "CC/pkgconfig", "system_library"], "name": ["fmt"]} +} diff --git a/src/hello/hello.cpp b/src/hello/hello.cpp new file mode 100644 index 0000000..0c9df99 --- /dev/null +++ b/src/hello/hello.cpp @@ -0,0 +1,11 @@ +#include <iostream> + +#include <fmt/format.h> +#include <string> + +int main(int argc, char **argv) { + std::cout << fmt::format("Hello {}!", + argc > 1 ? std::string{argv[1]} : "World") + << std::endl; + return 0; +} diff --git a/src/proto/TARGETS b/src/proto/TARGETS new file mode 100644 index 0000000..512d7c2 --- /dev/null +++ b/src/proto/TARGETS @@ -0,0 +1,18 @@ +{ "example": + { "type": ["@", "rules", "proto", "library"] + , "name": ["example"] + , "srcs": ["example.proto"] + } +, "write": + { "type": ["@", "rules", "CC", "binary"] + , "name": ["write"] + , "srcs": ["write.cc"] + , "private-proto": ["example"] + } +, "read": + { "type": ["@", "rules", "CC", "binary"] + , "name": ["read"] + , "srcs": ["read.cc"] + , "private-proto": ["example"] + } +} diff --git a/src/proto/example.proto b/src/proto/example.proto new file mode 100644 index 0000000..69b5953 --- /dev/null +++ b/src/proto/example.proto @@ -0,0 +1,7 @@ +syntax = "proto3"; +package sample; + +message Example { + string foo = 1; + int32 bar = 2; +} diff --git a/src/proto/read.cc b/src/proto/read.cc new file mode 100644 index 0000000..f97a39a --- /dev/null +++ b/src/proto/read.cc @@ -0,0 +1,18 @@ +#include <fstream> +#include <iostream> + +#include "example.pb.h" + +int main(int argc, char **argv) { + sample::Example example; + + { + std::fstream input(argv[1], std::ios::in | std::ios::binary); + example.ParseFromIstream(&input); + } + + std::cout << "foo=" << example.foo() << "\n"; + std::cout << "bar=" << example.bar() << std::endl; + + return 0; +} diff --git a/src/proto/write.cc b/src/proto/write.cc new file mode 100644 index 0000000..2e206e5 --- /dev/null +++ b/src/proto/write.cc @@ -0,0 +1,17 @@ +#include <fstream> +#include <string> + +#include "example.pb.h" + +int main(int argc, char **argv) { + sample::Example example; + example.set_foo(std::string{argv[1]}); + example.set_bar(atoi(argv[2])); + { + std::fstream output(argv[3], + std::ios::out | std::ios::trunc | std::ios::binary); + example.SerializeToOstream(&output); + } + + return 0; +} diff --git a/src/shell/TARGETS b/src/shell/TARGETS new file mode 100644 index 0000000..f4b2dbf --- /dev/null +++ b/src/shell/TARGETS @@ -0,0 +1,13 @@ +{ "out": + { "type": ["@", "rules", "shell", "cmds"] + , "outs": ["out.txt"] + , "cmds": ["./hello > out.txt"] + , "deps": [["hello", ""]] + } +, "": + { "type": ["@", "rules", "shell", "cmds"] + , "outs": ["wc.txt", "tr.txt"] + , "cmds": ["wc out.txt > wc.txt", "cat out.txt | tr a-z A-Z > tr.txt"] + , "deps": ["out"] + } +} 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 <algorithm> +#include <cstdlib> +#include <string> +#include <unistd.h> +#include <vector> + +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<char*> 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<char* const*>(nargv.data())); + return kForkFailed; +} |