unison

Fork of Unison, a bi-directional file synchronization tool
git clone git://git.laack.co/unison.git
Log | Files | Refs | README | LICENSE

cltool.c (2115B)


      1 /* cltool.c
      2 
      3    This is a command-line tool for Mac OS X that looks up the unison
      4    application, wherever it has been installed, and runs it.  This
      5    is intended to be installed in a standard place (e.g.,
      6    /usr/bin/unison) to make it easy to invoke unison as a server, or
      7    to use unison from the command line when it has been installed with
      8    a GUI.
      9 
     10  */
     11 
     12 #import <CoreServices/CoreServices.h>
     13 #import <ApplicationServices/ApplicationServices.h>
     14 #include <stdio.h>
     15 
     16 #define BUFSIZE 1024
     17 #define EXECPATH "/Contents/MacOS/Unison"
     18 
     19 int main(int argc, char **argv) {
     20 
     21   /* Look up the application by its bundle identifier, which is given
     22      in the Info.plist file.  This will continue to work even if the
     23      user changes the name of the application, unlike
     24      fullPathForApplication. */
     25 
     26   FSRef fsref;
     27   OSStatus status;
     28   int len;
     29   char buf[BUFSIZE];
     30 
     31   status = LSFindApplicationForInfo(kLSUnknownCreator,CFSTR("edu.upenn.cis.Unison"),NULL,&fsref,NULL);
     32   if (status) {
     33     if (status == kLSApplicationNotFoundErr) {
     34       fprintf(stderr,"Error: can't find the Unison application using the Launch Services database.\n");
     35       fprintf(stderr,"Try launching Unison from the Finder, and then try this again.\n");
     36     }
     37     else fprintf(stderr,"Error: can't find Unison application (%d).\n",(int)status);
     38     exit(1);
     39   }
     40 
     41   status = FSRefMakePath(&fsref,(UInt8 *)buf,BUFSIZE);
     42   if (status) {
     43     fprintf(stderr,"Error: problem building path to Unison application (%d).\n",(int)status);
     44     exit(1);
     45   }
     46 
     47   len = strlen(buf);
     48   if (len + strlen(EXECPATH) + 1 > BUFSIZE) {
     49     fprintf(stderr,"Error: path to Unison application exceeds internal buffer size (%d).\n",BUFSIZE);
     50     exit(1);
     51   }
     52   strcat(buf,EXECPATH);
     53 
     54   /* It's important to pass the absolute path on to the GUI,
     55      that's how it knows where to find the bundle, e.g., the
     56      Info.plist file. */
     57   argv[0] = buf;
     58 
     59   // printf("The Unison executable is at %s\n",argv[0]);
     60   // printf("Running...\n");
     61 
     62   execv(argv[0],argv);
     63 
     64   /* If we get here the execv has failed; print an error message to stderr */
     65   perror("unison");
     66   exit(1);
     67 }