File: env.c

package info (click to toggle)
libconfuse 3.3-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,192 kB
  • sloc: ansic: 5,532; lex: 451; xml: 439; makefile: 213; sh: 39
file content (55 lines) | stat: -rw-r--r-- 1,029 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
 * Example of how an application can allow free-form key=value strings as settings,
 * e.g., custom environment variables the program should set for child processes.
 *
 * env.conf:
 *     env {
 *          foo = bar
 *          baz = foo
 *     }
 */
#include <err.h>
#include <confuse.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
	const char *file = "env.conf";
	cfg_opt_t opts[] = {
		CFG_SEC("env", NULL, CFGF_KEYSTRVAL),
		CFG_END()
	};
	cfg_t *cfg, *sec;
	int rc;

	if (argc > 1)
		file = argv[1];

	cfg = cfg_init(opts, 0);
	if (!cfg)
		err(1, "Failed cfg_init()");

	rc = cfg_parse(cfg, file);
	if (rc != CFG_SUCCESS) {
		if (rc == CFG_FILE_ERROR)
			err(1, "Failed opening %s", file);

		errx(1, "Failed parsing %s", file);
	}

	sec = cfg_getsec(cfg, "env");
	if (sec) {
		unsigned int i;

		for (i = 0; i < cfg_num(sec); i++) {
			cfg_opt_t *opt = cfg_getnopt(sec, i);

			printf("%s = \"%s\"\n", cfg_opt_name(opt), cfg_opt_getstr(opt));
		}
	}

//	cfg_print(cfg, stdout);
	cfg_free(cfg);

	return 0;
}