-
Notifications
You must be signed in to change notification settings - Fork 0
/
shotgund.c
86 lines (68 loc) · 1.76 KB
/
shotgund.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
/*
* A small daemon to run a command when something changes in a directory tree.
*/
#include <dirent.h>
#include <errno.h>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
static void usage_die(void)
{
extern char *__progname; /* from crt0.o */
fprintf(stderr, "usage: %s directory action [timeout]\n"
" directory: the directory to scan\n"
" action: the action to take on update\n"
" timeout: the timout (defaults to 5 seconds)\n"
, __progname);
exit(EXIT_FAILURE);
}
static time_t get_last_mtime(const char *path)
{
time_t mtime = 0;
DIR *dir;
struct dirent *entry;
struct stat buf;
if (stat(path, &buf) == -1)
return 0;
mtime = buf.st_mtime;
if (S_ISDIR(buf.st_mode)) {
if ((dir = opendir(path)) == NULL)
return mtime;
while ((entry = readdir(dir))) {
time_t new_mtime;
char *new_path;
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
asprintf(&new_path, "%s/%s", path, entry->d_name);
new_mtime = get_last_mtime(new_path);
if (new_mtime > mtime)
mtime = new_mtime;
free(new_path);
}
closedir(dir);
}
return mtime;
}
int main(int argc, char **argv)
{
const char *directory = argv[1];
const char *action = argv[2];
const unsigned int timeout = argv[3] ? atoi(argv[3]) : 5;
time_t last_up;
if (argc != 3 && argc != 4)
usage_die();
system(action);
last_up = time(NULL);
while (42) {
sleep(timeout);
if (get_last_mtime(directory) > last_up) {
last_up = time(NULL);
system(action);
}
}
}