7 #include <string.h> /* for strerror() */
9 #define MAX_ENTRIES 256
11 void diep(const char *s
);
13 int main(int argc
, char *argv
[])
15 struct kevent evlist
[MAX_ENTRIES
]; /* events we want to monitor */
16 struct kevent chlist
[MAX_ENTRIES
]; /* events that were triggered */
20 int fdlist
[MAX_ENTRIES
], cnt
, kq
, nev
, i
;
22 /* check argument count */
24 fprintf(stderr
, "Usage: %s directory\n", argv
[0]);
28 /* create a new kernel event queue */
29 if ((kq
= kqueue()) == -1)
33 if ((pdir
= opendir(argv
[1])) == NULL
)
38 while((pdent
= readdir(pdir
)) != NULL
&& cnt
++ < 2)
43 while((pdent
= readdir(pdir
)) != NULL
) {
44 if (cnt
> MAX_ENTRIES
- 1) {
45 fprintf(stderr
, "Max number of entries exceeded\n");
46 for (i
= 0; i
< cnt
; i
++)
53 if (strlen(argv
[1] + strlen(pdent
->d_name
) + 1) > 256) {
54 fprintf(stderr
,"Max path length exceeded\n");
57 strcpy(fullpath
, argv
[1]);
58 strcat(fullpath
, "/");
59 strcat(fullpath
, pdent
->d_name
);
61 if ((fdlist
[cnt
] = open(fullpath
, O_RDONLY
)) == -1)
64 EV_SET(&chlist
[cnt
], fdlist
[cnt
], EVFILT_VNODE
,
65 EV_ADD
| EV_ENABLE
| EV_ONESHOT
,
66 NOTE_DELETE
| NOTE_EXTEND
| NOTE_WRITE
| NOTE_ATTRIB
,
74 nev
= kevent(kq
, chlist
, cnt
, evlist
, cnt
, NULL
);
79 for (i
= 0; i
< nev
; i
++) {
80 if (evlist
[i
].flags
& EV_ERROR
) {
81 fprintf(stderr
, "EV_ERROR: %s\n", strerror(evlist
[i
].data
));
85 if (evlist
[i
].fflags
& NOTE_DELETE
) {
86 printf("fd: %d Deleted\n", evlist
[i
].ident
);
88 else if (evlist
[i
].fflags
& NOTE_EXTEND
||
89 evlist
[i
].fflags
& NOTE_WRITE
) {
90 printf("fd: %d Modified\n", evlist
[i
].ident
);
92 else if (evlist
[i
].fflags
& NOTE_ATTRIB
) {
93 printf("fd: %d Attributes modified\n", evlist
[i
].ident
);
100 for (i
= 0; i
< cnt
; i
++)
108 void diep(const char *s
)