#include #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 \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; }