-
-
Save atr000/650289 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// gcc -Wall -W -Wno-unused-parameter -Werror -arch i386 -arch ppc main.c -framework CoreServices -o cb | |
// cb service to respond to an action upon dir change. great starting point. | |
#include <CoreServices/CoreServices.h> | |
#define EVENT_STREAM_LATENCY ((CFAbsoluteTime)5) | |
const char *command; | |
void build( | |
ConstFSEventStreamRef streamRef, | |
void *clientCallBackInfo, | |
size_t numEvents, | |
void *eventPaths, | |
const FSEventStreamEventFlags eventFlags[], | |
const FSEventStreamEventId eventIds[]) | |
{ | |
system(command); | |
} | |
int main(int argc, const char *argv[]) | |
{ | |
if (argc != 3) | |
{ | |
fprintf(stderr, "Usage: cb <directory to watch> <command to run>\n"); | |
exit(EXIT_FAILURE); | |
} | |
char buffer[PATH_MAX]; | |
if (realpath(argv[1], buffer) == NULL) | |
{ | |
perror("Can't canonicalize directory path"); | |
} | |
CFStringRef directory = CFStringCreateWithFileSystemRepresentation( | |
kCFAllocatorDefault, | |
buffer); | |
if (directory == NULL) | |
{ | |
fprintf( | |
stderr, "Directory %s is not a valid path name\n", argv[1]); | |
exit(EXIT_FAILURE); | |
} | |
CFArrayRef pathsToWatch = CFArrayCreate( | |
kCFAllocatorDefault, | |
(const void **)&directory, | |
1, | |
&kCFTypeArrayCallBacks); | |
if (pathsToWatch == NULL) | |
{ | |
fprintf( | |
stderr, "Can't create array of paths to watch\n"); | |
exit(EXIT_FAILURE); | |
} | |
command = argv[2]; | |
FSEventStreamContext context = { 0, NULL, NULL, NULL, NULL }; | |
FSEventStreamRef stream = FSEventStreamCreate( | |
kCFAllocatorDefault, | |
build, | |
&context, | |
pathsToWatch, | |
kFSEventStreamEventIdSinceNow, | |
EVENT_STREAM_LATENCY, | |
kFSEventStreamCreateFlagNone); | |
if (stream == NULL) | |
{ | |
fprintf( | |
stderr, "Can't create event stream\n"); | |
exit(EXIT_FAILURE); | |
} | |
FSEventStreamScheduleWithRunLoop( | |
stream, | |
CFRunLoopGetCurrent(), | |
kCFRunLoopDefaultMode); | |
if (!FSEventStreamStart(stream)) | |
{ | |
fprintf( | |
stderr, "Can't start event stream\n"); | |
exit(EXIT_FAILURE); | |
} | |
CFRunLoopRun(); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment