![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jul 2005
Posts: 10
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4
![]() |
chdir() changes the directory.
opendir() readdir() allow you to perform file searches. |
|
|
|
|
|
#3 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#4 |
|
Programmer
Join Date: Aug 2005
Location: 0x0010 * 0x0091 + 0x0004
Posts: 65
Rep Power: 4
![]() |
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 */ <Jim_McNeat> Is there like a way to put a compiler in "Just trust me on that one" mode? |
|
|
|
|
|
#5 |
|
Professional Programmer
|
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. 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. |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Jul 2005
Posts: 10
Rep Power: 0
![]() |
Thanks for help. You gave me good ideas. I'll try Dizzutchs suggestion and see how it works out.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|