File based configuration handling in C (Unix)

2020-02-07 19:10发布

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?

12条回答
一夜七次
2楼-- · 2020-02-07 19:29

There is also getline() function and friends in GNU LibC

查看更多
甜甜的少女心
3楼-- · 2020-02-07 19:30

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.

查看更多
小情绪 Triste *
4楼-- · 2020-02-07 19:30

Yet another solution - Pure-ftpd

Unlike many daemons, Pure-FTPd doesn't read any configuration file. Instead, it uses command-line options. ... Adding a parser for configuration files in the server is a bad idea. It slows down everything and needs resources for nothing.

And for options there is getopt

查看更多
兄弟一词,经得起流年.
5楼-- · 2020-02-07 19:31

Have you consider of storing config values as environment variables? :) And config file will be a shell script you run before your program. (actually shell script will execute it to save variables). That way you are using shell as config parser :)

Use http://www.google.com/codesearch and "read config"

See Example for httpd

查看更多
等我变得足够好
6楼-- · 2020-02-07 19:34

Hmmm there is LibConfig.

I have found it in Wiki

查看更多
何必那么认真
7楼-- · 2020-02-07 19:38

Okay, so let's hit the other part. You need to think about what you'd like to have as your "language". In the UNIX world, the sort of canonical version is probably whitespace-delimited text (think /etc/hosts) or ":" delimited text (like /etc/passwd).

You have a couple of options, the simplest in some sense being to use scanf(3). Again, read the man page for details, but if a line entry is something like

port    100

then you'll be looking for something like

char inbuf[MAXLINE];
int  val;

scanf("%s %d\n", &inbuf[0], &val);

You can get a bit more flexibility if you write a simple FSA parse: read characters one at a time from the line, and use a finite automaton to define what to do.

查看更多
登录 后发表回答