Unix configuration location

I want to write a unix / linux program that will use a configuration file.

My problem is where should I put the file location?

I could "hard code" the location (for example /etc) in the program itself.

However, I would like if a user without privileges could install it (via make) in another place, for example ~.

Should makefile edit source code? Or is it usually done differently?

+5
source share
5 answers

Typically, a number of places are used to obtain a location:

  • Provided by the user as a command line argument (i.e. ./program -C path/to/config/file.cfg).
  • (char *path_to_config = getenv("PROGRAMCONFIG");).
  • , (stat("./program.cfg") strig, "$ HOME/.program/config.cfg", "$ HOME/.program.cfg" stat, ).
  • (stat("/etc/program/config.cfg",...)).
+1

:

  • /etc/appname
  • ~/.appname

, , , .

  • $app_userconfig
  • $app_config

/ .

, , ,

  • -c | --config {filename}
+6

/etc/prgname . , .local , , .

+1

, , , FHS. -, , , , , . , , .

0

The makefile should not directly modify the source, but it can pass the path / folder name to the compiler with a parameter -D. One way to handle this is to tell #define something like DEFAULT_PATH as the default installation path. If the user wants to determine the path, makefile will add -DUSER_PATH=whateverto the compiler options. You must write your code to use USER_PATH if it exists, and DEFAULT_PATH otherwise.

0
source

All Articles