views:

117

answers:

4

What is recommended way to keep a user configuration data in Unix/Linux? My programming language is C++. Configuration data will be kept in XML/text/binary format, I have no problem with handling such files. I want to know where can I keep them. For example, in the Windows OS configuration data may be kept in the Registry (old way) or in user application data directory. What about Linux? I need read/write access to configuration files.

+1  A: 

That depends a little on your flavor of Linux but as a general rule most programs have the system default configuration somewhere in /etc with .config files in your home directory that can override the defaults in the /etc dir.


Great point .config should be .[Name of config file]

rerun
You might want to clarify that by ".config" you actually mean a file whose name starts with ".", *not* a file with a ".config" extension. The "." prefix is to keep the files "hidden" by default. Programs that have a bunch of config files will sometime have a ".config" directory, with the files in there. Also, old-skool Unix apps tend to use the name ".[appname]rc" for their config file/directory.
Laurence Gonsalves
+5  A: 

The concept of the registry is peculiar to Windows, and Microsoft once admitted to it being ill-conceived.

In Unix and Linux, configuration for system-wide programs is in /etc or maybe an application-specific subdirectory.

Per user configuration data are kept in the user's home directory in a hidden file—in text format—or an application-specific hidden directory in the user's home directory. The proper way to reference the home directory is through the environment variable HOME. Hidden files and directories are created by making . the first character of the name.

Examples for system-wide configuration is /etc/wgetrc and /etc/ssh/. Examples of per-user data are $HOME/.bashrc and $HOME/.mozilla/.

wallyk
You should not rely on the environment variable HOME. Since we're talking C++ which can make system calls, you should do `struct passwd *pw = getpwuid(getuid());` (or `getpwuid_r` if you need thread safety) and then use the value of `pw->pw_dir`
R Samuel Klatchko
Good suggestion. I've never had a problem using $HOME. I notice many programs must work that way too, since sometimes I've overridden HOME with useful, expected results.
wallyk
Thank you, I think I will follow this. BTW, can I use "~" for the home directory in the program?
Alex Farber
Actually, I would go with `getenv("HOME")` and fall back on `getpwuid(getuid())->pw_dir`, to allow a user to override the home directory if desired. See the XDG basedir spec mentioned by ergosys: it allows for user overrides of just about everything.
ephemient
@alex: no, `~` is a magic shell symbol. Only if the program uses the shell to expand file specifications would that work. If there's a library function, I don't know about it.
wallyk
@wallyk `man 3 wordexp` http://www.opengroup.org/onlinepubs/007908799/xsh/wordexp.html
ephemient
@ephemient: cool!
wallyk
+2  A: 

The XDG Base Directory Specification specifies where configuration and other files should be stored in Linux and other X-based operating systems:

http://freedesktop.org/wiki/Specifications/basedir-spec

This is the modern way, and may eventually reduce the dotfile mess in the typical user's home directory.

ergosys
+2  A: 

Dotfiles are the classic Unix solution. If you want to deal with reading/writing everything yourself, go for it.

However, most modern programs I use have used GConf for storing preferences. It makes a lot of things easier, both as a developer and as a user (and apparently as an administrator, but I have no experience there).

Ken
Good point about GConf, thank you. In my case, I have cross-platform code for managing XML configuration files, which is already running in Windows. Having this already done, I need to add platform-specific code to select configuration files path.
Alex Farber
http://api.kde.org/4.0-api/kdelibs-apidocs/kdecore/html/classKConfig.html for the *other* popular desktop's configuration scheme.
ephemient