[PATCH wayland 1/4] Add wl_get_runtime_dir()

Emilio Pozuelo Monfort pochu27 at gmail.com
Mon Mar 11 09:10:54 PDT 2013


This function returns the runtime directory, which is
$XDG_RUNTIME_DIR if set, and ~/.cache otherwise. This
follows the XDG base directory specification.

https://bugs.freedesktop.org/show_bug.cgi?id=62092
---
 src/wayland-private.h |    3 +++
 src/wayland-util.c    |   27 +++++++++++++++++++++++++++
 2 files changed, 30 insertions(+)

diff --git a/src/wayland-private.h b/src/wayland-private.h
index f0c9010..30acb42 100644
--- a/src/wayland-private.h
+++ b/src/wayland-private.h
@@ -145,4 +145,7 @@ extern wl_log_func_t wl_log_handler;
 
 void wl_log(const char *fmt, ...);
 
+char *
+wl_get_runtime_dir (void);
+
 #endif
diff --git a/src/wayland-util.c b/src/wayland-util.c
index 4ab1f5a..16cf76c 100644
--- a/src/wayland-util.c
+++ b/src/wayland-util.c
@@ -25,6 +25,8 @@
 #include <stdint.h>
 #include <string.h>
 #include <stdarg.h>
+#include <stdio.h>
+#include <errno.h>
 
 #include "wayland-util.h"
 #include "wayland-private.h"
@@ -332,3 +334,28 @@ wl_log(const char *fmt, ...)
 	wl_log_handler(fmt, argp);
 	va_end(argp);
 }
+
+char *
+wl_get_runtime_dir (void)
+{
+	const char *runtime_dir, *home_dir;
+	char *dir;
+
+	/* Try XDG_RUNTIME_DIR first */
+	runtime_dir = getenv("XDG_RUNTIME_DIR");
+	if (runtime_dir != NULL)
+		return strdup(runtime_dir);
+
+	/* XDG_RUNTIME_DIR unset, fall back to ~/.cache */
+	home_dir = getenv("HOME");
+	if (!home_dir) {
+		errno = ENOENT;
+		return NULL;
+	}
+
+	dir = malloc(strlen(home_dir) + strlen("/.cache") + 1);
+	if (!dir)
+		return NULL;
+	sprintf(dir, "%s/%s", home_dir, ".cache");
+	return dir;
+}
-- 
1.7.10.4



More information about the wayland-devel mailing list