I'm having trouble running some C code before launching an app from Spotlight or the Applications folder. I figured I should just be able to create an executable with my code where after all the computations are done I make a execvp()
call to run the original app's executable, which will then replace the current process.
I compiled the .c file and gave it the original app's executable name (in my case clion) and then executed it from Terminal.. great, it works!.. Until I tried to open the app bundle from spotlight (or Finder)
For some reason the execvp()
call fails and the original program continues, but my custom executable is indeed called by spotlight! Not even making a child process with fork()
will work, neither a system()
call. What could be the problem here?
Contents folder of CLion: clion is my executable, clion-real is the original executable
-rwxr-xr-x 1 marc admin 8700 22 Mai 10:35 clion
-rw-r--r-- 1 marc admin 550 22 Mai 10:35 clion-fake.c
-rwxr-xr-x@ 1 marc admin 128353 21 Mai 12:56 clion-real
clion-fake.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char** argv) {
system("say 'called'");
char* args[] = {"./clion-real", NULL};
int status = execvp(args[0], args);
system("say 'failed'");
return 0;
}
When calling ./clion
from terminal it works as intended, the system says called and the app opens, but when opening the .app from Spotlight it says both called and failed, which means that the execvp()
call failed.
Errno 2 is ENOENT
, aka "no such file or directory".
You need to make sure that the current working directoy for the execution of your clion
binary is actually set to the directory where your binaries live in, otherwise relative paths like ./clion-real
won't work.