This repository has been archived by the owner on Jan 1, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nwatch.cpp
106 lines (93 loc) · 2.35 KB
/
nwatch.cpp
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <vector>
#include <cstring>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
void end_msg(int sig) {
std::cout << "\x1b[33m\n" "And now my watch ends..." "\x1b[m\n\n";
std::exit(1);
}
int main(int argc, char **argv) {
auto print_usage_and_exit = [argv]() {
std::cout << "Usage: " << argv[0] << " FILE [FILE]* -- COMMAND" << std::endl;
std::exit(1);
};
if (argc < 4) print_usage_and_exit();
std::cout << "\x1b[32m\n" "And now my watch begins..." "\x1b[m\n\n";
signal(SIGINT, end_msg); // Ctrl + c
auto filepaths = std::vector<const char*>();
int cmd_offset = -1;
for (int i = 1; i < argc; ++i) {
if (!strcmp(argv[i], "--")) {
cmd_offset = ++i;
break;
}
filepaths.push_back(argv[i]);
}
if (cmd_offset == -1 || cmd_offset == argc)
print_usage_and_exit();
auto file_stats = std::vector<struct stat>(filepaths.size());
auto restat_files = [&filepaths, &file_stats]()
{
for (size_t i=0; i<filepaths.size(); ++i) {
if (stat(filepaths[i], &file_stats[i]) < 0) {
std::perror("\x1b[31m" "stat failed" "\x1b[m");
std::exit(1);
}
}
};
restat_files();
auto last_mtimes = std::vector<timespec>(file_stats.size());
auto update_last_mtimes = [&file_stats, &last_mtimes]()
{
for (size_t i=0; i<file_stats.size(); ++i) {
last_mtimes[i] = file_stats[i].st_mtim;
}
};
update_last_mtimes();
auto compare_mtimes = [&last_mtimes, &file_stats, &update_last_mtimes]() -> bool
{
for (size_t i=0; i<file_stats.size(); ++i) {
if (last_mtimes[i].tv_sec != file_stats[i].st_mtim.tv_sec) {
update_last_mtimes();
return true;
}
}
return false;
};
auto watcher = [&]() {
pid_t pid = fork();
if (pid < 0) {
perror("fork failed");
std::exit(1);
}
if (pid == 0) {
if (execvp(argv[cmd_offset], argv+cmd_offset) < 0) {
perror("exec failed");
std::exit(1);
}
}
while (true) {
sleep(2);
restat_files();
if (compare_mtimes()) {
// Kill the child process
if (kill(pid, SIGKILL) < 0) {
perror("kill failed");
std::exit(1);
}
int wstatus;
wait(&wstatus); // Handling zombies.
break;
}
}
};
while (true) { watcher(); }
return 0;
}