about summary refs log tree commit diff
path: root/rwc.c
blob: 3b00d9bf72429f8190062d5121cffa153ca030e3 (plain) (blame)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// rwc [-0d] [PATH...] - report when changed
//   -0  use NUL instead of newline for input/output separator
//   -d  detect deletions too (prefixed with "- ")

#include <sys/inotify.h>
#include <sys/stat.h>

#include <errno.h>
#include <libgen.h>
#include <search.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

char *argv0;
char ibuf[8192];
int ifd;

int dflag;
char input_delim = '\n';

static void *root = 0; // tree

int
order(const void *a, const void *b)
{
	return strcmp((char *)a, (char*)b);
}

void
add(char *file)
{
        struct stat st;

	char *dir = file;

	tsearch(strdup(file), &root, order);

	// assume non-existing files are regular files
	if (lstat(file, &st) < 0 || !S_ISDIR(st.st_mode))
		dir = dirname(file);

	if (inotify_add_watch(ifd, dir, IN_MOVED_TO | IN_CLOSE_WRITE | dflag)<0)
		fprintf(stderr, "%s: inotify_add_watch: %s: %s\n",
		    argv0, dir, strerror(errno));
}

int
main(int argc, char *argv[])
{
	int c, i;

	argv0 = argv[0];

        while ((c = getopt(argc, argv, "0d")) != -1)
		switch(c) {
		case '0': input_delim = 0; break;
		case 'd': dflag = IN_DELETE | IN_DELETE_SELF; break;
		default:
                        fprintf(stderr, "Usage: %s [-0d] [PATH...]\n", argv0);
                        exit(2);
                }

        ifd = inotify_init();
        if (ifd < 0) {
		fprintf(stderr, "%s: inotify_init: %s\n",
		    argv0, strerror(errno));
                exit(111);
	}

	i = optind;
	if (optind == argc)
		goto from_stdin;
	for (; i < argc; i++) {
		if (strcmp(argv[i], "-") != 0) {
			add(argv[i]);
			continue;
		}
from_stdin:
		while (1) {
			char *line = 0;
			size_t linelen = 0;
			ssize_t rd;

			errno = 0;
			rd = getdelim(&line, &linelen, input_delim, stdin);
			if (rd == -1) {
				if (errno != 0)
					return -1;
				break;
			}
			
			if (rd > 0 && line[rd-1] == input_delim)
				line[rd-1] = 0;  // strip delimiter

			add(line);
		}
	}

	while (1) {
		ssize_t len, i;
		struct inotify_event *ev;

		len = read(ifd, ibuf, sizeof ibuf);
		if (len <= 0) {
			fprintf(stderr, "%s: error reading inotify buffer: %s",
			    argv0, strerror(errno));
			exit(1);
		}
	
		for (i = 0; i < len; i += sizeof (*ev) + ev->len) {
			ev = (struct inotify_event *) (ibuf + i);

			if (ev->mask & IN_IGNORED)
				continue;

			if (tfind(ev->name, &root, order) ||
			    tfind(dirname(ev->name), &root, order)) {
				printf("%s%s%c",
				    (ev->mask & IN_DELETE ? "- " : ""),
				    ev->name,
				    input_delim);
				fflush(stdout);
			}
		}
	}

        return 0;
}