Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File based configuration handling in C (Unix)

This is probably one of the most common tasks / problems when programming; You need to store the configuration of your application somewhere.

While I'm trying to create a webserver or other applications, I'd like to keep the code as clean as possible since my main interest in programming is architecture. This results in me wanting to store configurations in a file which can be changed without having to re-compile the software.

I'm not here to re-invent the wheel or anything like that, so what I'd like to do is creating a Configuration reader in C on *nix. The configuration might look a lot like any other software's configuration; Apache, vsftpd, MySQL, etc.

The basic question is: How do you read from a textfile and process each line efficiently (in pure C)? Do I need to use fgetc() and process each char?

like image 625
Filip Ekberg Avatar asked Jan 04 '09 23:01

Filip Ekberg


People also ask

What is Unix config file?

Unix configuration files are a unique type of plain file. As the name implies, configuration files contain information about the structure or arrangement of specific parts of the system. They are kept in the /etc directory.

What is file configuration in Linux?

A configuration file, also known as a config file, is a local file that controls the operations of a program, utility or process. Linux configuration files contain the settings and instructions for different systems, utilities, applications and processes.

What is a configuration file in C?

In computing, configuration files (commonly known simply as config files) are files used to configure the parameters and initial settings for some computer programs. They are used for user applications, server processes and operating system settings.

Where are configuration files stored in Unix?

Most global config files are located in the /etc directory. /etc/cups/ – sub-directory containing configuration for the Common UNIX Printing System.


2 Answers

Hmmm there is LibConfig.

I have found it in Wiki

like image 107
Malx Avatar answered Sep 23 '22 11:09

Malx


Various people have given reasonably good advice - the Pure-FTP example is interesting.

You should also read TAOUP (The Art of Unix Programming) by E S Raymond. It has examples a-plenty of configuration files. It also outlines canonical idioms for the configuration files. For example, you should probably allow '#' to start a comment to the end of the line, and ignore blank lines. You should also decide what you will do if the configuration file contains a line you don't understand - whether to ignore and continue silently, or whether to complain. (I prefer things that complain; then I know why what I've just added isn't having any effect - whereas silent ignoring means I don't know whether the entry I just added has any effect.)

Another problem is locating the configuration file. Do you do that by compiled-in location, by a default install location with environment variable to override, or by some other piece of magic? Make sure there's a command line option to allow the configuration file to be specified absolutely - even consider making that the only way to do it.

Otherwise, within broad limits, keep it simple and everyone will be happier.

like image 42
Jonathan Leffler Avatar answered Sep 23 '22 11:09

Jonathan Leffler