Dynamic library and passing relative pathnames

D

draco

Hi,
Let's say I have a dynamic library libapple.so with a method
applemethod() which expects a filename string. I call this method from
main() which is in the file foo.c. Now foo.c and apple.so are in
different directories, and the user calls foo.c with a relative
pathname to the file. My question is how do I make sure that
applemethod knows where this file is from the realtive pathname?

foo.c:

main(int argc, char *argv){
applemethod(argv[1]);
}

apple.c compiled to libapple.so:

voidapplemethod(char *filename){
fp = fopen(filename,"r");
...do stuff...
}

now the user runs ./a.out orange.txt, orange.txt is in the same dir as
foo.c but neither apple.c or libapple.so are in this dir, how does
applemethod know where to look for orange.txt? Does the linker take
care of this automatically? Should I use absolute paths instead?
Any pointers/help would be great.
 
G

Gordon Burditt

Let's say I have a dynamic library libapple.so with a method
applemethod() which expects a filename string. I call this method from
main() which is in the file foo.c. Now foo.c and apple.so are in
different directories, and the user calls foo.c with a relative
pathname to the file. My question is how do I make sure that
applemethod knows where this file is from the realtive pathname?

On some systems (e.g. UNIX), the current working directory for a
process is used as the base for any relative pathnames. The current
working directory has nothing whatever to do with the location of
any executable or library, and it doesn't change depending on where
the code calling fopen() is. You can, if you want, still call chdir().

Figuring out the path where the executable or shared library is
from within the executing program is at best unportable and at
worst impossible to do reliably when fooling the program can mean
a security compromise (e.g. getting the program to use the wrong
configuration file, which is one reason why trying to use the path
to the executable is a BAD idea.).

Gordon L. Burditt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top