But there is no portable way of finding a process’s actual path (paulsm4 forces the issue by calling the process a certain way). In particular, a process might not even
have a path any more.
If you’re on linux, there is a non-portable, somewhat reliable method of finding this out (it uses the
/proc filesystem):
Code:
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
char proc_path[20];
char path[PATH_MAX];
int i;
sprintf(proc_path, "/proc/%d/exe", getpid());
if((i = readlink(proc_path, path, PATH_MAX-1)) == -1)
exit(EXIT_FAILURE);
path[i] = 0;
printf("process path is \"%s\".\n", path);
return 0;
}