]>
Dogcows Code - chaz/openbox/blob - obt/paths.c
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>
42 static gint
slist_path_cmp(const gchar
*a
, const gchar
*b
)
47 typedef GSList
* (*GSListFunc
) (gpointer list
, gconstpointer data
);
49 static GSList
* slist_path_add(GSList
*list
, gpointer data
, GSListFunc func
)
56 if (!g_slist_find_custom(list
, data
, (GCompareFunc
) slist_path_cmp
))
57 list
= func(list
, data
);
64 static GSList
* split_paths(const gchar
*paths
)
71 spl
= g_strsplit(paths
, ":", -1);
72 for (it
= spl
; *it
; ++it
)
73 list
= slist_path_add(list
, *it
, (GSListFunc
) g_slist_append
);
78 ObtPaths
* obt_paths_new(void)
83 p
= g_new0(ObtPaths
, 1);
86 path
= g_getenv("XDG_CONFIG_HOME");
87 if (path
&& path
[0] != '\0') /* not unset or empty */
88 p
->config_home
= g_build_filename(path
, NULL
);
90 p
->config_home
= g_build_filename(g_get_home_dir(), ".config", NULL
);
92 path
= g_getenv("XDG_DATA_HOME");
93 if (path
&& path
[0] != '\0') /* not unset or empty */
94 p
->data_home
= g_build_filename(path
, NULL
);
96 p
->data_home
= g_build_filename(g_get_home_dir(), ".local",
99 path
= g_getenv("XDG_CACHE_HOME");
100 if (path
&& path
[0] != '\0') /* not unset or empty */
101 p
->cache_home
= g_build_filename(path
, NULL
);
103 p
->cache_home
= g_build_filename(g_get_home_dir(), ".cache", NULL
);
105 path
= g_getenv("XDG_CONFIG_DIRS");
106 if (path
&& path
[0] != '\0') /* not unset or empty */
107 p
->config_dirs
= split_paths(path
);
109 p
->config_dirs
= slist_path_add(p
->config_dirs
,
111 (GSListFunc
) g_slist_append
);
112 p
->config_dirs
= slist_path_add(p
->config_dirs
,
116 (GSListFunc
) g_slist_append
);
118 p
->config_dirs
= slist_path_add(p
->config_dirs
,
119 g_strdup(p
->config_home
),
120 (GSListFunc
) g_slist_prepend
);
122 path
= g_getenv("XDG_DATA_DIRS");
123 if (path
&& path
[0] != '\0') /* not unset or empty */
124 p
->data_dirs
= split_paths(path
);
126 p
->data_dirs
= slist_path_add(p
->data_dirs
,
128 (GSListFunc
) g_slist_append
);
129 p
->data_dirs
= slist_path_add(p
->data_dirs
,
132 "usr", "local", "share", NULL
),
133 (GSListFunc
) g_slist_append
);
134 p
->data_dirs
= slist_path_add(p
->data_dirs
,
137 "usr", "share", NULL
),
138 (GSListFunc
) g_slist_append
);
140 p
->data_dirs
= slist_path_add(p
->data_dirs
,
141 g_strdup(p
->data_home
),
142 (GSListFunc
) g_slist_prepend
);
146 void obt_paths_ref(ObtPaths
*p
)
151 void obt_paths_unref(ObtPaths
*p
)
153 if (p
&& --p
->ref
== 0) {
156 for (it
= p
->config_dirs
; it
; it
= g_slist_next(it
))
158 g_slist_free(p
->config_dirs
);
159 for (it
= p
->data_dirs
; it
; it
= g_slist_next(it
))
161 g_slist_free(p
->data_dirs
);
162 g_free(p
->config_home
);
163 g_free(p
->data_home
);
164 g_free(p
->cache_home
);
166 obt_free0(p
, ObtPaths
, 1);
170 gchar
*obt_paths_expand_tilde(const gchar
*f
)
177 spl
= g_strsplit(f
, "~", 0);
178 ret
= g_strjoinv(g_get_home_dir(), spl
);
183 gboolean
obt_paths_mkdir(const gchar
*path
, gint mode
)
187 g_return_val_if_fail(path
!= NULL
, FALSE
);
188 g_return_val_if_fail(path
[0] != '\0', FALSE
);
190 if (!g_file_test(path
, G_FILE_TEST_IS_DIR
))
191 if (mkdir(path
, mode
) == -1)
197 gboolean
obt_paths_mkdir_path(const gchar
*path
, gint mode
)
201 g_return_val_if_fail(path
!= NULL
, FALSE
);
202 g_return_val_if_fail(path
[0] == '/', FALSE
);
204 if (!g_file_test(path
, G_FILE_TEST_IS_DIR
)) {
209 while ((e
= strchr(e
+ 1, '/'))) {
211 if (!(ret
= obt_paths_mkdir(c
, mode
)))
212 goto parse_mkdir_path_end
;
215 ret
= obt_paths_mkdir(c
, mode
);
217 parse_mkdir_path_end
:
224 const gchar
* obt_paths_config_home(ObtPaths
*p
)
226 return p
->config_home
;
229 const gchar
* obt_paths_data_home(ObtPaths
*p
)
234 const gchar
* obt_paths_cache_home(ObtPaths
*p
)
236 return p
->cache_home
;
239 GSList
* obt_paths_config_dirs(ObtPaths
*p
)
241 return p
->config_dirs
;
244 GSList
* obt_paths_data_dirs(ObtPaths
*p
)
This page took 0.042337 seconds and 4 git commands to generate.