Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 18th, 2005, 9:31 AM   #1
mevuorin
Newbie
 
Join Date: Jul 2005
Posts: 10
Rep Power: 0 mevuorin is on a distinguished road
problem with relative filenames in linux

Hi
I'm working on a linux program written in c that has a config file. I'd like users to be able to install my program anywhere on the filesystem and still be able to read the config file from the program. So far I haven't found a good solution. Programs directory structure is this:

program //base directory
program/bin/ //binaries
program/bin/foo //main program
program/source/ //sources
program/data/ //all data needed by the program
program/configuration.cfg //configuration file

I tried to open the configuration file with
fopen( "../configuration.cfg", "r"), but found out that it only works if foo is run from the program/bin/ directory, because relative filenames are solved based on the current working directory which is the directory from where the program was run.
Is there a way to change the current working directory to the directory where the program that is being run is? I guess that would solve my problem.

Maybe I could parse the mains argv[0] string and use that with pwd to obtain the absolute path of configuration.cfg. It just seems like the hard way to do it.

I also thought about placing the .cfg file to /etc, but that would need root permission, and I don't want to make my program installable only as root. I'm not sure if I should place .cfg files of my own programs to /etc anyway.

I guess I could use environmental variable to hold the absolute path of program directory, but I'd rather not force users to set environmental variables after installing my program.

I could probably use locate or find from the program to find where the configuration.cfg is located, but that seems quite crude and inefficient.

Any ideas on how to make this work?

Thanks in advance for any help.
mevuorin is offline   Reply With Quote
Old Oct 18th, 2005, 9:35 AM   #2
jim mcnamara
Hobbyist Programmer
 
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4 jim mcnamara is on a distinguished road
chdir() changes the directory.
opendir() readdir() allow you to perform file searches.
jim mcnamara is offline   Reply With Quote
Old Oct 18th, 2005, 9:36 AM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Put it in the same directory as the executable or in a known relation to that. Your operating system doesn't know where things are unless you give it clues (like the environment). If you don't want to cope with that, or have your users do it, then you just have to solve the problem in some other workable way.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Oct 18th, 2005, 7:12 PM   #4
2roll4life7
Programmer
 
2roll4life7's Avatar
 
Join Date: Aug 2005
Location: 0x0010 * 0x0091 + 0x0004
Posts: 65
Rep Power: 4 2roll4life7 is on a distinguished road
I would go one of three ways... First you could modify the installer to write the absolute path of the base directory where it's installed to, to a constant location. I mean it's a drawback because then it is almost like you have two locations for one file, but it's a possibility I just figured I'd throw it out there.

The second way I would consider is almost the same, but instead of writting the absolute base directory to a file, you could put it in an environmental variable (like what DaWei [I think] was saying).

The third way is the other way you mentioned in you're orginal post. The last version of DigitalMarsCompiler that I downloaded would automatically include the whole absolute path of the current file in argv[], then you could just parse that with whatever other directory. I'm sure you can do this with whatever compiler you're using too.

//Edit

Also a fourth, somewhat ugly solution, would be to use something like... http://www.germane-software.com/soft...ries/registry/
__________________
#if 0 /* in case someone actually tries to compile this */
- libpng version 1.2.8 (example.c)

<Jim_McNeat> Is there like a way to put a compiler in "Just trust me on that one" mode?
2roll4life7 is offline   Reply With Quote
Old Oct 20th, 2005, 11:04 AM   #5
Dizzutch
Professional Programmer
 
Dizzutch's Avatar
 
Join Date: Dec 2004
Location: Worcester, MA
Posts: 441
Rep Power: 4 Dizzutch is on a distinguished road
Send a message via ICQ to Dizzutch Send a message via AIM to Dizzutch Send a message via MSN to Dizzutch Send a message via Yahoo to Dizzutch
the *nix way would be to put the config files in /etc/programname/
that way you can always get to it by the absolute name. Add a --configfile flag if the user wants to define an alternate file path.
edit: sorry didn't read your post comletely
I would still follow the normal user conventions. If somebody doesn't want to install the program as root, than they can install it in their home directory. I'd suggest using autoconf to create the installed.
Personally i don't like it when programs force a specific install, like in /opt or something like that.
If your filestructer looks like this
program/bin
program/etc
program/lib
etc.
just make the bin always look in "../etc", "/etc", and the argument of --configfile flag before erroring out
this way if someone installs the tree in /usr/home/username/program/
it'll be able to find the config file in /usr/home/username/program/etc.
If somebody wants to put the /etc files in /usr/local/etc they can use the --configfile flag.
__________________
naked pictures of you | PFO F@H stats
Dizzutch is offline   Reply With Quote
Old Oct 27th, 2005, 5:24 AM   #6
mevuorin
Newbie
 
Join Date: Jul 2005
Posts: 10
Rep Power: 0 mevuorin is on a distinguished road
Thanks for help. You gave me good ideas. I'll try Dizzutchs suggestion and see how it works out.
mevuorin is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:50 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC