Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <linux/inotify.h>
- #include <errno.h>
- #include <sys/types.h>
- #define NEVENTS 1
- #define NAMEMAX 200
- #define EVENT_BUF_LEN (sizeof(struct inotify_event) + NAMEMAX + 1)
- #define EVENTS_BUF_LEN (EVENT_BUF_LEN * NEVENTS)
- int main (int argc, char** argv) {
- int notify_fd, watch_fd, bytes = 0;
- int events_n = 1;
- struct inotify_event *buff;
- notify_fd = inotify_init();
- if (notify_fd < 0) {
- perror("inotify_init() :: ");
- return 1;
- }
- watch_fd = inotify_add_watch(notify_fd, "/dev", IN_CREATE | IN_DELETE);
- if (watch_fd < 0) {
- perror("watch_fd() :: ");
- close(notify_fd);
- return 2;
- }
- for (;;) {
- buff = malloc(EVENT_BUF_LEN);
- if (!buff) {
- printf("Memory error!");
- close(notify_fd);
- return 3;
- }
- bytes = read(notify_fd, buff, EVENTS_BUF_LEN);
- if (bytes < EVENT_BUF_LEN - NAMEMAX + 1) {
- printf("No enought data");
- free(buff);
- close(notify_fd);
- return 4;
- }
- if (!buff->len) {
- printf("Unexpected error, len of event name undefined");
- free(buff);
- continue;
- }
- printf("Device: %s\n", buff->name);
- free(buff);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment