I need the cpp call that will tell me the full path to the app I'm running in. For example, I'm running in a loaded library for either mozilla or firefox, but would like to know the full path to the executable
I agree. But the following solution might work for the OP (Caution.. a /proc based solution.)
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
/* Finds the path containing the currently running program executable.
The path is placed into BUFFER, which is of length LEN. Returns
the number of characters in the path, or -1 on error. */
size_t get_executable_path (char* buffer, size_t len)
{
char* path_end;
/* Read the target of /proc/self/exe. */
if (readlink ("/proc/self/exe", buffer, len) <= 0)
return -1;
/* Find the last occurrence of a forward slash, the path separator. */
path_end = strrchr (buffer, '/');
if (path_end == NULL)
return -1;
/* Advance to the character past the last slash. */
++path_end;
/* Obtain the directory containing the program by truncating the
path after the last slash. */
*path_end = '\0';
/* The length of the path is the number of characters up through the
last slash. */
return (size_t) (path_end - buffer);
}
int main ()
{
char path[PATH_MAX];
get_executable_path (path, sizeof (path));
printf ("this program is in the directory %s\n", path);
return 0;
}
This solution fixes problems such as env settings which are exclusive for the app, and which are required to be set by the user prior to executing the app.
This would be a longer solution (compared to the /proc/self/exe) because you will have to read through each line till you reach one which has your app-name in it. From there, its an easy ride.