From 12476abdab83f765da8173e73b172ecf4749f6b9 Mon Sep 17 00:00:00 2001 From: katherine Date: Sat, 7 Dec 2019 15:16:24 -0700 Subject: initial commit --- doc/example.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 doc/example.c (limited to 'doc/example.c') diff --git a/doc/example.c b/doc/example.c new file mode 100644 index 0000000..688321a --- /dev/null +++ b/doc/example.c @@ -0,0 +1,65 @@ +#include +#include + +#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; +} -- cgit v1.2.3