forked from cory/tildefriends
Merge branches/quickjs to trunk. This is the way.
git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3621 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
44
deps/libuv/docs/code/onchange/main.c
vendored
Normal file
44
deps/libuv/docs/code/onchange/main.c
vendored
Normal file
@ -0,0 +1,44 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <uv.h>
|
||||
|
||||
uv_loop_t *loop;
|
||||
const char *command;
|
||||
|
||||
void run_command(uv_fs_event_t *handle, const char *filename, int events, int status) {
|
||||
char path[1024];
|
||||
size_t size = 1023;
|
||||
// Does not handle error if path is longer than 1023.
|
||||
uv_fs_event_getpath(handle, path, &size);
|
||||
path[size] = '\0';
|
||||
|
||||
fprintf(stderr, "Change detected in %s: ", path);
|
||||
if (events & UV_RENAME)
|
||||
fprintf(stderr, "renamed");
|
||||
if (events & UV_CHANGE)
|
||||
fprintf(stderr, "changed");
|
||||
|
||||
fprintf(stderr, " %s\n", filename ? filename : "");
|
||||
system(command);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
if (argc <= 2) {
|
||||
fprintf(stderr, "Usage: %s <command> <file1> [file2 ...]\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
loop = uv_default_loop();
|
||||
command = argv[1];
|
||||
|
||||
while (argc-- > 2) {
|
||||
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, run_command, argv[argc], UV_FS_EVENT_RECURSIVE);
|
||||
}
|
||||
|
||||
return uv_run(loop, UV_RUN_DEFAULT);
|
||||
}
|
Reference in New Issue
Block a user