Created
October 18, 2013 14:22
-
-
Save davemo/7042293 to your computer and use it in GitHub Desktop.
A C program by @ibawt that can be used to execute commands using a watch performantly on OSX.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <signal.h> | |
#include <CoreServices/CoreServices.h> | |
/* fswait.c | |
* | |
* usage: ./fswait /some/directory[:/some/otherdirectory:...] | |
* | |
* compile me with something like: gcc fswait.c -framework CoreServices -o fswait | |
* | |
* adapted from the FSEvents api PDF | |
*/ | |
extern char **environ; | |
//fork a process when there's any change in watch file | |
void callback( | |
ConstFSEventStreamRef streamRef, | |
void *clientCallBackInfo, | |
size_t numEvents, | |
void *eventPaths, | |
const FSEventStreamEventFlags eventFlags[], | |
const FSEventStreamEventId eventIds[]) | |
{ | |
exit(0); | |
} | |
//set up fsevents and callback | |
int main(int argc, char **argv) { | |
if(argc != 2) { | |
fprintf(stderr, "You must specify a directory to watch\n"); | |
exit(1); | |
} | |
CFStringRef mypath = CFStringCreateWithCString(NULL, argv[1], kCFStringEncodingUTF8); | |
CFArrayRef pathsToWatch = CFStringCreateArrayBySeparatingStrings (NULL, mypath, CFSTR(":")); | |
void *callbackInfo = NULL; | |
FSEventStreamRef stream; | |
CFAbsoluteTime latency = 1.0; | |
stream = FSEventStreamCreate(NULL, | |
&callback, | |
callbackInfo, | |
pathsToWatch, | |
kFSEventStreamEventIdSinceNow, | |
latency, | |
kFSEventStreamCreateFlagNone | |
); | |
FSEventStreamScheduleWithRunLoop(stream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); | |
FSEventStreamStart(stream); | |
CFRunLoopRun(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment