Skip to content

Instantly share code, notes, and snippets.

@atr000
Created October 28, 2010 00:07
Show Gist options
  • Save atr000/650289 to your computer and use it in GitHub Desktop.
Save atr000/650289 to your computer and use it in GitHub Desktop.
// 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