PDA

View Full Version : How can I find out where my program is saved?



fhucho
March 23rd, 2008, 02:46 PM
Hi,
in my program I want to find out the directory where it is saved. How can I do that?

Example:
Everything my program does is that it prints the directory where it is saved. If the user saves the program to /usr/bin and then runs /usr/bin/program from his home directory (/home/user), the program should print "/usr/bin".

Can+~
March 23rd, 2008, 03:58 PM
Uhm, language?

If it is python, os.getcwd() returns where the .py was executed ("Current working directory").

fhucho
March 23rd, 2008, 04:21 PM
I would appreciate solution for any language, I guess the solution is platform independent...

Current working directory is not always the directory where is the program stored (as I showed on an example).

mike_g
March 23rd, 2008, 04:27 PM
Well I think that would depend mainly on what IDE you use. Many IDEs like to make their own workspace to save stuff in. Compilers usually dump their output to the CWD. Mabe there are exceptions to this, but that is what is most common. In short: there is no solution for no language.

stroyan
March 23rd, 2008, 04:50 PM
The /proc/self/exe feature of linux can make this fairly easy.



#include <limits.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>

int main(int argc, char *argv)
{
char buf[_POSIX_PATH_MAX];
ssize_t result;
char *last_slash;

result = readlink("/proc/self/exe", buf, _POSIX_PATH_MAX);
if (result == -1) {
perror("readlink");
return 1;
}
last_slash = strrchr(buf,'/');
*last_slash = 0; /* Drop program name */
printf("program is in directory %s\n", buf);
return 0;
}

pmasiar
March 23rd, 2008, 04:54 PM
sys.argv[0] contains mame of running program, including path



import sys
print sys.argv[0]