473,472 Members | 2,168 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Dynamic library and passing relative pathnames

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.

Nov 14 '05 #1
1 1340
>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
Nov 14 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Paul | last post by:
I want to use sessions to cover myself in case the user switches off cookies so I am passing the session ID manually through a hidden input field. This is what I have so far. index.php page...
1
by: Paul | last post by:
Hmmm, didn't seem to work. I have set session.use_cookies = 1 and session.use_trans_sid = 1 in my php.ini file. Index.php contains:...
7
by: mark | last post by:
I am designing a website at the moment and looking at the difference between relative and absolute url links which is driving me crazy! I would like to use relative paths, but it is proving very...
3
by: K.S.Liang | last post by:
Hi all, 1> If there are more than one dynamic linking libraries in the file system, how do I know which one is loaded into system? Any C library or system call can tell me which *.so or *.sl is...
1
by: Ted Polak | last post by:
I am trying to use a relative pathname to connect to a Database in a folder in my wwwroot directory. However, when I test the connection to the database, I get this error:...
8
by: EP | last post by:
Hi, I'm a bit green in this area and wonder to what extent there may be some existing Python tools (or if I have to scratch my head real hard for an appropriate algorithm... ) I'd hate to build...
22
by: Sven-Thorsten Fahrbach | last post by:
Hi Does anybody know of a library that offers a function to split pathnames. It should work somewhat like the following code snippet: ----------------- char *path =...
6
by: Even | last post by:
Hi all, As far as I know, relative address will be assigned by program at link time.Right? Ok, here we go. So if we use a static library, it will give the program an relative address(i mean...
1
by: dazzler | last post by:
I have used relative path names in my application: for example: logo_image = Image.open("./data/logo.png") everything has worked fine until now... I have compiled my code into exe and made...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.