Skip to content

Instantly share code, notes, and snippets.

@doppioandante
Created November 23, 2014 21:35
Show Gist options
  • Save doppioandante/aadde9de9f5aef503830 to your computer and use it in GitHub Desktop.
Save doppioandante/aadde9de9f5aef503830 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <uv.h>
uv_loop_t *loop;
void fs_callback(uv_fs_event_t *handle, const char *filename, int events, int status) {
fprintf(stderr, "Change detected(%d) in %s: ", events, handle->path);
if (events & UV_RENAME)
fprintf(stderr, "renamed");
if (events & UV_CHANGE)
fprintf(stderr, "changed");
fprintf(stderr, " %s\n", filename ? filename : "");
}
int main(int argc, char **argv) {
if (argc <= 1) {
fprintf(stderr, "Usage: %s <file1> [file2 ...]\n", argv[0]);
return 1;
}
loop = uv_default_loop();
while (argc-- > 1) {
fprintf(stderr, "Adding watch on %s\n", argv[argc]);
uv_fs_event_t *fs_event_req = malloc(sizeof(uv_fs_event_t));
uv_fs_event_init(loop, fs_event_req);
// The recursive flag watches subdirectories too.
uv_fs_event_start(fs_event_req, fs_callback, argv[argc], UV_FS_EVENT_RECURSIVE);
}
return uv_run(loop, UV_RUN_DEFAULT);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment