forked from septag/dmon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-incremental.c
87 lines (81 loc) · 2.63 KB
/
test-incremental.c
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
#include <stdio.h>
#define DMON_IMPL
#include "dmon.h"
#if defined(__linux__) || defined(linux) || defined(__linux)
#include "dmon_extra.h"
#endif /* defined(__linux__) || defined(linux) || defined(__linux) */
static void watch_callback(dmon_watch_id watch_id, dmon_action action, const char* rootdir,
const char* filepath, const char* oldfilepath, void* user)
{
(void)(user);
(void)(watch_id);
switch (action) {
case DMON_ACTION_CREATE:
printf("CREATE: [%s]%s\n", rootdir, filepath);
break;
case DMON_ACTION_DELETE:
printf("DELETE: [%s]%s\n", rootdir, filepath);
break;
case DMON_ACTION_MODIFY:
printf("MODIFY: [%s]%s\n", rootdir, filepath);
break;
case DMON_ACTION_MOVE:
printf("MOVE: [%s]%s -> [%s]%s\n", rootdir, oldfilepath, rootdir, filepath);
break;
}
}
static int prompt_command_loop(dmon_watch_id watch_id)
{
char cmd[256];
while (1) {
fputs("> ", stdout);
char *s = fgets(cmd, 256, stdin);
char *newline = (s ? strchr(cmd, '\n') : NULL);
if (!s || !newline) {
fprintf(stderr, "error reading input\n");
return 1;
}
*newline = 0;
if (strncmp(cmd, "add ", 4) == 0) {
#if defined(__linux__) || defined(linux) || defined(__linux)
char *subdir = cmd + 4;
if (!dmon_watch_add(watch_id, subdir)) {
fprintf(stdout, "cannot add directory %s\n", subdir);
} else {
fprintf(stdout, "added directory %s\n", subdir);
}
#else
fputs("dmon_watch_add not implemented for this OS", stderr);
#endif
} else if (strncmp(cmd, "remove ", 7) == 0) {
#if defined(__linux__) || defined(linux) || defined(__linux)
char *subdir = cmd + 7;
if (!dmon_watch_rm(watch_id, subdir)) {
fprintf(stdout, "cannot remove directory %s\n", subdir);
} else {
fprintf(stdout, "removed directory %s\n", subdir);
}
#else
fputs("dmon_watch_rm not implemented for this OS", stderr);
#endif
} else if (strcmp(cmd, "exit") == 0) {
return 0;
} else {
fprintf(stdout, "unknown command: %s\n", cmd);
}
}
}
int main(int argc, char* argv[])
{
if (argc > 1) {
dmon_init();
puts("waiting for changes ..");
/* We do not watch recursively. */
dmon_watch_id watch_id = dmon_watch(argv[1], watch_callback, 0, NULL);
prompt_command_loop(watch_id);
dmon_deinit();
} else {
puts("usage: test dirname");
}
return 0;
}