[PATCH 1/4] window: add a helper for config file paths

Pekka Paalanen ppaalanen at gmail.com
Tue Nov 15 01:45:40 PST 2011


Add a helper function, that constructs a path to a config file from
XDG_CONFIG_HOME environment variable, by the rules of
http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html

Make desktop-shell find its config file from XDG_CONFIG_HOME. This
allows to have a personal config file without continuously fighting with
git about wayland-desktop-shell.ini.

Signed-off-by: Pekka Paalanen <ppaalanen at gmail.com>
---
 clients/config.c        |   38 ++++++++++++++++++++++++++++++++++++++
 clients/desktop-shell.c |    5 ++++-
 clients/window.h        |    3 +++
 3 files changed, 45 insertions(+), 1 deletions(-)

diff --git a/clients/config.c b/clients/config.c
index f389b69..42acee7 100644
--- a/clients/config.c
+++ b/clients/config.c
@@ -135,3 +135,41 @@ parse_config_file(const char *path,
 
 	return 0;
 }
+
+char *
+config_file_path(const char *name)
+{
+	const char dotconf[] = "/.config/";
+	const char *config_dir;
+	const char *home_dir;
+	char *path;
+	size_t size;
+
+	config_dir = getenv("XDG_CONFIG_HOME");
+	if (!config_dir) {
+		fprintf(stderr, "XDG_CONFIG_HOME is not set,"
+				" falling back to $HOME/.config\n");
+
+		home_dir = getenv("HOME");
+		if (!home_dir) {
+			fprintf(stderr, "HOME is not set, using cwd.\n");
+			return strdup(name);
+		}
+
+		size = strlen(home_dir) + sizeof dotconf + strlen(name);
+		path = malloc(size);
+		if (!path)
+			return NULL;
+
+		snprintf(path, size, "%s%s%s", home_dir, dotconf, name);
+		return path;
+	}
+
+	size = strlen(config_dir) + 1 + strlen(name) + 1;
+	path = malloc(size);
+	if (!path)
+		return NULL;
+
+	snprintf(path, size, "%s/%s", config_dir, name);
+	return path;
+}
diff --git a/clients/desktop-shell.c b/clients/desktop-shell.c
index 33ea677..89c8a75 100644
--- a/clients/desktop-shell.c
+++ b/clients/desktop-shell.c
@@ -348,6 +348,7 @@ launcher_section_done(void *data)
 int main(int argc, char *argv[])
 {
 	struct desktop desktop;
+	char *config_file;
 
 	desktop.display = display_create(&argc, &argv, NULL);
 	if (desktop.display == NULL) {
@@ -363,9 +364,11 @@ int main(int argc, char *argv[])
 
 	desktop.panel = panel_create(desktop.display);
 
-	parse_config_file("wayland-desktop-shell.ini",
+	config_file = config_file_path("wayland-desktop-shell.ini");
+	parse_config_file(config_file,
 			  config_sections, ARRAY_LENGTH(config_sections),
 			  &desktop);
+	free(config_file);
 
 	printf("panel color: %08x\n", key_panel_color);
 
diff --git a/clients/window.h b/clients/window.h
index 40bf102..bad1e60 100644
--- a/clients/window.h
+++ b/clients/window.h
@@ -346,4 +346,7 @@ parse_config_file(const char *path,
 		  const struct config_section *sections, int num_sections,
 		  void *data);
 
+char *
+config_file_path(const char *name);
+
 #endif
-- 
1.7.3.4



More information about the wayland-devel mailing list