Permalink
Browse files

run shell command instead of path

  • Loading branch information...
1 parent c447f6e commit a0db417201cdc6c55450030a7d216e7e4f66ef0f Alan Dipert committed Jun 25, 2009
Showing with 15 additions and 11 deletions.
  1. +8 −3 README
  2. +7 −8 fswatch.c
View
11 README
@@ -2,15 +2,20 @@ fswatch.c
This is a small program using the Mac OS X FSEvents API to monitor a directory.
When an event about any change to that directory is received, the specified
-command is executed with the monitored directory as an argument.
+shell command is executed by /bin/bash.
usage:
- ./fswatch /some/dir /bin/ls
+ ./fswatch /some/dir "echo changed"
-This would monitor /some/dir for any change, and run `/bin/ls /some/dir` when
+This would monitor /some/dir for any change, and run `echo changed` when
a modification event is received.
+fswatch passes along the parent processes' environment, so you can do things
+like this:
+
+ fswatch components/ "~/local/bin/componentize.rb components/ > resources/components.js"
+
compile:
You'll need the OS X Developer Tools installed on 10.4 or higher.
View
@@ -6,16 +6,16 @@
/* fswatch.c
*
- * usage: ./fswatch /some/directory /some/command
- * /some/command is executed with /some/directory as an arg
+ * usage: ./fswatch /some/directory "some command"
+ * "some command" is eval'd by bash when /some/directory generates any file events
*
* compile me with something like: gcc fswatch.c -framework CoreServices -o fswatch
*
* adapted from the FSEvents api PDF
*/
extern char **environ;
-char *to_watch;
+//the command to run
char *to_run;
//fork a process when there's any change in watch file
@@ -36,10 +36,10 @@ void callback(
fprintf(stderr, "error: couldn't fork \n");
exit(1);
} else if (pid == 0) {
- /*char *envp[1] = { 0 };*/
- char *args[8] = {
+ char *args[4] = {
+ "/bin/bash",
+ "-c",
to_run,
- to_watch,
0
};
if(execve(args[0], args, environ) < 0) {
@@ -60,10 +60,9 @@ int main(int argc, char **argv) {
exit(1);
}
- to_watch = argv[1];
to_run = argv[2];
- CFStringRef mypath = CFStringCreateWithCString(NULL, to_watch, kCFStringEncodingUTF8);
+ CFStringRef mypath = CFStringCreateWithCString(NULL, argv[1], kCFStringEncodingUTF8);
CFArrayRef pathsToWatch = CFArrayCreate(NULL, (const void **)&mypath, 1, NULL);
void *callbackInfo = NULL;
FSEventStreamRef stream;

0 comments on commit a0db417

Please sign in to comment.