1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 obt/paths.c for the Openbox window manager
4 Copyright (c) 2003-2007 Dana Jansens
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 See the COPYING file for a copy of the GNU General Public License.
19 #include "obt/paths.h"
22 #ifdef HAVE_SYS_STAT_H
23 # include <sys/stat.h>
25 #ifdef HAVE_SYS_TYPES_H
26 # include <sys/types.h>
41 static gint slist_path_cmp(const gchar *a, const gchar *b)
46 typedef GSList* (*GSListFunc) (gpointer list, gconstpointer data);
48 static GSList* slist_path_add(GSList *list, gpointer data, GSListFunc func)
55 if (!g_slist_find_custom(list, data, (GCompareFunc) slist_path_cmp))
56 list = func(list, data);
63 static GSList* split_paths(const gchar *paths)
70 spl = g_strsplit(paths, ":", -1);
71 for (it = spl; *it; ++it)
72 list = slist_path_add(list, *it, (GSListFunc) g_slist_append);
77 ObtPaths* obt_paths_new(void)
82 p = g_new0(ObtPaths, 1);
85 path = g_getenv("XDG_CONFIG_HOME");
86 if (path && path[0] != '\0') /* not unset or empty */
87 p->config_home = g_build_filename(path, NULL);
89 p->config_home = g_build_filename(g_get_home_dir(), ".config", NULL);
91 path = g_getenv("XDG_DATA_HOME");
92 if (path && path[0] != '\0') /* not unset or empty */
93 p->data_home = g_build_filename(path, NULL);
95 p->data_home = g_build_filename(g_get_home_dir(), ".local",
98 path = g_getenv("XDG_CONFIG_DIRS");
99 if (path && path[0] != '\0') /* not unset or empty */
100 p->config_dirs = split_paths(path);
102 p->config_dirs = slist_path_add(p->config_dirs,
104 (GSListFunc) g_slist_append);
105 p->config_dirs = slist_path_add(p->config_dirs,
109 (GSListFunc) g_slist_append);
111 p->config_dirs = slist_path_add(p->config_dirs,
112 g_strdup(p->config_home),
113 (GSListFunc) g_slist_prepend);
115 path = g_getenv("XDG_DATA_DIRS");
116 if (path && path[0] != '\0') /* not unset or empty */
117 p->data_dirs = split_paths(path);
119 p->data_dirs = slist_path_add(p->data_dirs,
121 (GSListFunc) g_slist_append);
122 p->data_dirs = slist_path_add(p->data_dirs,
125 "usr", "local", "share", NULL),
126 (GSListFunc) g_slist_append);
127 p->data_dirs = slist_path_add(p->data_dirs,
130 "usr", "share", NULL),
131 (GSListFunc) g_slist_append);
133 p->data_dirs = slist_path_add(p->data_dirs,
134 g_strdup(p->data_home),
135 (GSListFunc) g_slist_prepend);
139 void obt_paths_ref(ObtPaths *p)
144 void obt_paths_unref(ObtPaths *p)
146 if (p && --p->ref == 0) {
149 for (it = p->config_dirs; it; it = g_slist_next(it))
151 g_slist_free(p->config_dirs);
152 for (it = p->data_dirs; it; it = g_slist_next(it))
154 g_slist_free(p->data_dirs);
155 g_free(p->config_home);
156 g_free(p->data_home);
158 obt_free0(p, ObtPaths, 1);
162 gchar *obt_paths_expand_tilde(const gchar *f)
169 spl = g_strsplit(f, "~", 0);
170 ret = g_strjoinv(g_get_home_dir(), spl);
175 gboolean obt_paths_mkdir(const gchar *path, gint mode)
179 g_return_val_if_fail(path != NULL, FALSE);
180 g_return_val_if_fail(path[0] != '\0', FALSE);
182 if (!g_file_test(path, G_FILE_TEST_IS_DIR))
183 if (mkdir(path, mode) == -1)
189 gboolean obt_paths_mkdir_path(const gchar *path, gint mode)
193 g_return_val_if_fail(path != NULL, FALSE);
194 g_return_val_if_fail(path[0] == '/', FALSE);
196 if (!g_file_test(path, G_FILE_TEST_IS_DIR)) {
201 while ((e = strchr(e + 1, '/'))) {
203 if (!(ret = obt_paths_mkdir(c, mode)))
204 goto parse_mkdir_path_end;
207 ret = obt_paths_mkdir(c, mode);
209 parse_mkdir_path_end:
216 const gchar* obt_paths_config_home(ObtPaths *p)
218 return p->config_home;
221 const gchar* obt_paths_data_home(ObtPaths *p)
226 GSList* obt_paths_config_dirs(ObtPaths *p)
228 return p->config_dirs;
231 GSList* obt_paths_data_dirs(ObtPaths *p)