aboutsummaryrefslogtreecommitdiffstats
path: root/doc
diff options
context:
space:
mode:
authorkatherine <ageha@airen-no-jikken.icu>2019-12-07 15:16:24 -0700
committerkatherine <ageha@airen-no-jikken.icu>2019-12-07 15:16:24 -0700
commit12476abdab83f765da8173e73b172ecf4749f6b9 (patch)
tree819cd77d204f4c9e793364e421b5c225ce9026c4 /doc
downloadsimple-xdg-bdirs-12476abdab83f765da8173e73b172ecf4749f6b9.tar.gz
initial commit
Diffstat (limited to 'doc')
-rw-r--r--doc/example.c65
1 files changed, 65 insertions, 0 deletions
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 <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;
+}