diff options
Diffstat (limited to 'kel_rap/c++/kel_rap.cpp')
| -rw-r--r-- | kel_rap/c++/kel_rap.cpp | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/kel_rap/c++/kel_rap.cpp b/kel_rap/c++/kel_rap.cpp new file mode 100644 index 0000000..d505125 --- /dev/null +++ b/kel_rap/c++/kel_rap.cpp @@ -0,0 +1,51 @@ +#include <array> +#include <filesystem> +#include <iostream> +#include <string> + +#include <sys/types.h> +#include <sys/wait.h> +#include <unistd.h> + +namespace fs = std::filesystem; + +int main(int argc, char* argv[]) { + if (argc != 2) { + std::cerr << "Usage: " << argv[0] << " <dir>\n"; + return -1; + } + + for (const auto& e : fs::directory_iterator(argv[1])) { + if (!e.is_regular_file()) + continue; + + std::string path = e.path().string(); + + pid_t pid = fork(); + if (pid < 0) { + std::cerr << "fork failed\n"; + return -1; + } + + if (pid == 0) { + std::string path = e.path().string(); + std::array<char*, 2> args{ + path.data(), + nullptr + }; + + execvp(args[0], args.data()); + + std::cerr << "execvp failed: " << path << '\n'; + return -1; + } + + int status; + if (waitpid(pid, &status, 0) < 0) { + std::cerr << "waitpid failed\n"; + return -1; + } + } + + return 0; +} |
