aboutsummaryrefslogtreecommitdiffstats
path: root/doc/example.c
blob: 688321a677011fc3d1ceeb75beac87e64781c7d3 (plain)
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
56
57
58
59
60
61
62
63
64
65
#include <stdlib.h>
#include <stdio.h>

#include "../simple-xdg-bdirs.h"

/* a simple example which attempts to read a value from a configuration file
 * and write it back to a cache file */
int main(int argc, char *argv[])
{
	char *s, *wdir, **rdirs, **cur;

	/* alloc a null-terminated array of directories in which to search for
	 * configuration files */
	rdirs = simple_xdg_bdirs_read_dirs(SIMPLE_XDG_BDIRS_CONFIG);

	/* in case of error, errno is set, so perror works */
	if (rdirs == NULL) {
		perror(NULL);
		return 1;
	}

	/* search in the config directories for the given relative path. if not
	 * found in the first, the second will be checked as a fallback etc */
	s = simple_xdg_bdirs_fullpath_read("xdg_bdirs_test/config.conf", rdirs);

	/* element strings of rdirs must also be freed */
	for (cur = rdirs; *cur != NULL; cur++)
		free(*cur);
	free(rdirs);

	/* check for errors again */
	if (s == NULL) {
		perror(NULL);
		return 1;
	}

	/* print the fullpath of the file that was found */
	puts(s);
	free(s);

	/* locate the directory into which runtime files should be written 
	 * (things like sockets or lock files) */
	wdir = simple_xdg_bdirs_write_dir(SIMPLE_XDG_BDIRS_RUNTIME);

	if (wdir == NULL) {
		perror(NULL);
		return 1;
	}

	/* a convenience function that builds a fullpath from a relative path and
	 * write directory */
	s = simple_xdg_bdirs_fullpath_write("xdg_bdirs_test.lock", wdir);

	free(wdir);

	if (s == NULL) {
		perror(NULL);
		return 1;
	}

	puts(s);
	free(s);

	return 0;
}