Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b97657c

Browse files
committed
Add event handler for inotify
1 parent f76f15c commit b97657c

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

‎inotify/inotify_exam.c‎

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,53 @@
11
#include <stdio.h>
22
#include <stdlib.h>
33
#include <string.h>
4+
#include <signal.h>
5+
#include <poll.h>
46
#include <errno.h>
7+
#include <unistd.h>
58
#include <sys/inotify.h>
69

7-
#define INOTIFY_PATH "/tmp/"
10+
#define INOTIFY_PATH "/home/linuxias/Desktop/"
11+
static void __handle_inotify_event(const struct inotify_event *event)
12+
{
13+
if (event->mask & IN_ACCESS)
14+
printf("IN_ACCESS ");
15+
if (event->mask & IN_ATTRIB)
16+
printf("IN_ATTRIB ");
17+
if (event->mask & IN_CLOSE_NOWRITE)
18+
printf("IN_CLOSE_NOWRITE ");
19+
if (event->mask & IN_CLOSE_WRITE)
20+
printf("IN_CLOSE_WRITE ");
21+
if (event->mask & IN_CREATE)
22+
printf("IN_CREATE ");
23+
if (event->mask & IN_DELETE)
24+
printf("IN_DELETE ");
25+
if (event->mask & IN_ISDIR)
26+
printf("IN_ISDIR ");
27+
if (event->mask & IN_MODIFY)
28+
printf("IN_MODIFY ");
29+
if (event->mask & IN_MOVE_SELF)
30+
printf("IN_MOVE_SELF ");
31+
if (event->mask & IN_MOVED_FROM)
32+
printf("IN_MOVED_FROM ");
33+
if (event->mask & IN_MOVED_TO)
34+
printf("IN_MOVED_TO ");
35+
if (event->mask & IN_OPEN)
36+
printf("IN_OPEN ");
37+
38+
if (event->len > 0)
39+
printf(": name = %s\n", event->name);
40+
}
841

942
int main(int argc, char *argv[])
1043
{
1144
int ret;
1245
int fd;
1346
int wd;
47+
char buf[4096] __attribute__ ((aligned(__alignof__(struct inotify_event))));
48+
char *ptr;
49+
ssize_t size;
50+
const struct inotify_event *event;
1451

1552
fd = inotify_init();
1653
if (fd < 0) {
@@ -25,6 +62,23 @@ int main(int argc, char *argv[])
2562
exit(EXIT_FAILURE);
2663
}
2764

65+
while (1) {
66+
size = read(fd, buf, sizeof(buf));
67+
if (size == -1 && errno != EAGAIN) {
68+
perror("read");
69+
fprintf(stderr, "read : %s", strerror(errno));
70+
exit(EXIT_FAILURE);
71+
}
72+
73+
if (size <= 0)
74+
break;
75+
76+
for (ptr = buf; ptr < buf + size; ptr += sizeof(struct inotify_event) + event->len) {
77+
event = (struct inotify_event *)ptr;
78+
__handle_inotify_event(event);
79+
}
80+
}
81+
2882
ret = inotify_rm_watch(fd, wd);
2983
if (ret < 0) {
3084
fprintf(stderr, "Failed to rm watch [fd : %d] [wd : %d] [%s]", fd, wd, strerror(errno));

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /