I want to write a unix/linux program, that will use a configuration file.
My problem is, where should I put the location of the file?
I could "hardcode" the location (like /etc
) into the program itself.
However, I would like it, if the user without privileges could install it (through make) somewhere else, like ~
.
Should the makefile edit the source code? Or is it usually done in a different way?
Create some defaults:
/etc/appname
~/.appname
Then if you want to allow these to be overridden have your application inspect an environment variable. e.g.
$app_userconfig
$app_config
Which would contain an override path/filename.
Lastly add a command line option that allows a config to be specified at runtime, e.g.
-c | --config {filename}
It is common to use a series of places to get the location:
./program -C path/to/config/file.cfg
).char *path_to_config = getenv("PROGRAMCONFIG");
).stat("./program.cfg")
or build up a strig to specify either "$HOME/.program/config.cfg" or "$HOME/.program.cfg" and stat
that).stat("/etc/program/config.cfg",...)
).keeping a global config file under /etc/prgname is a standard. Also allowing a .local config file for individual users that will override the global settings would allow each user to personalize the program to their preference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With