Say I have a program, abc in Linux, with a method:
char *currentPath(){
char *path=NULL;
path = getcwd(path, MAXPATHLEN);
return path;
}
When calling abc directly, this path returns the path that abc is in.
If I make a symbolic link to abc, and call the symbolic link, currentPath() returns the path of the symbolic link.
Is there a way to get make this method return the path of abc? I am interested in accessing files relative to the location of abc.
You can’t use
getcwd()to help get the executable name; it is not a sensible way to go about finding the pathname of the executable.On Linux, there is a symlink
/proc/self/exethat gives you the name of the executable if you read it with thereadlink()system call. Beware, thereadlink()system call does not null terminate the value it returns (and I have no idea why not; it is singularly weird behaviour in my book, and an instant cause of bugs for the unwary).There are a number of problems with your scheme.
/bin/lswithout having to be in the/bindirectory.If you’re worried about security, be aware that the value of
argv[0]is under the control of the program launching the target program. The shell behaves nicely; other programs may be more malicious:This passes the program name as
/opt/you/bin/boguseven though it invokes the programsleep.If you search the web, you will find plenty of examples of ‘how to get the executable name’ that assume that
argv[0]is the way to go; it is not. Other platforms have other techniques for getting to the executable name. Using/proc/self/exeis not portable; neither are the other techniques.