Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Location of configuration in unix program

Tags:

unix

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?

like image 320
Karel Bílek Avatar asked Sep 05 '10 21:09

Karel Bílek


3 Answers

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}
like image 68
ocodo Avatar answered Sep 20 '22 15:09

ocodo


It is common to use a series of places to get the location:

  1. Supplied by the user as a command line argument (i.e. ./program -C path/to/config/file.cfg).
  2. From an environment variable (char *path_to_config = getenv("PROGRAMCONFIG");).
  3. Possibly look for a user specific or local version (stat("./program.cfg") or build up a strig to specify either "$HOME/.program/config.cfg" or "$HOME/.program.cfg" and stat that).
  4. Hardcoded as a backup (stat("/etc/program/config.cfg",...)).
like image 37
dmckee --- ex-moderator kitten Avatar answered Sep 22 '22 15:09

dmckee --- ex-moderator kitten


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.

like image 31
cmptrwhz Avatar answered Sep 21 '22 15:09

cmptrwhz