summaryrefslogtreecommitdiff
path: root/kel_rap/c++/kel_rap.cpp
blob: d505125b47056409031efd9b56d539e677ce401f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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;
}