]> Dogcows Code - chaz/openbox/blob - obt/paths.c
bd6c177a708a30c885dfca940d9dfb4d8f50d072
[chaz/openbox] / obt / paths.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3 obt/paths.c for the Openbox window manager
4 Copyright (c) 2003-2007 Dana Jansens
5
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.
10
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.
15
16 See the COPYING file for a copy of the GNU General Public License.
17 */
18
19 #include "obt/bsearch.h"
20 #include "obt/paths.h"
21 #include "obt/util.h"
22
23 #ifdef HAVE_STDLIB_H
24 # include <stdlib.h>
25 #endif
26 #ifdef HAVE_SYS_STAT_H
27 # include <sys/stat.h>
28 #endif
29 #ifdef HAVE_SYS_TYPES_H
30 # include <sys/types.h>
31 #endif
32 #ifdef HAVE_STRING_H
33 # include <string.h>
34 #endif
35 #ifdef HAVE_UNISTD_H
36 # include <unistd.h>
37 #endif
38 #ifdef HAVE_GRP_H
39 # include <grp.h>
40 #endif
41 #ifdef HAVE_PWD_H
42 # include <pwd.h>
43 #endif
44
45 struct _ObtPaths
46 {
47 gint ref;
48 gchar *config_home;
49 gchar *data_home;
50 gchar *cache_home;
51 GSList *config_dirs;
52 GSList *data_dirs;
53 GSList *autostart_dirs;
54 GSList *exec_dirs;
55
56 uid_t uid;
57 gid_t *gid;
58 guint n_gid;
59 };
60
61 static gint slist_path_cmp(const gchar *a, const gchar *b)
62 {
63 return strcmp(a, b);
64 }
65
66 typedef GSList* (*GSListFunc) (gpointer list, gconstpointer data);
67
68 static GSList* slist_path_add(GSList *list, gpointer data, GSListFunc func)
69 {
70 g_assert(func);
71
72 if (!data)
73 return list;
74
75 if (!g_slist_find_custom(list, data, (GCompareFunc) slist_path_cmp))
76 list = func(list, data);
77 else
78 g_free(data);
79
80 return list;
81 }
82
83 static GSList* split_paths(const gchar *paths)
84 {
85 GSList *list = NULL;
86 gchar **spl, **it;
87
88 if (!paths)
89 return NULL;
90 spl = g_strsplit(paths, ":", -1);
91 for (it = spl; *it; ++it)
92 list = slist_path_add(list, *it, (GSListFunc) g_slist_append);
93 g_free(spl);
94 return list;
95 }
96
97 int gid_cmp(const void *va, const void *vb)
98 {
99 const gid_t a = *(const gid_t*)va, b = *(const gid_t*)vb;
100 return a>b ? 1 : (a == b ? 0 : -1);
101 }
102
103 static void find_uid_gid(uid_t *u, gid_t **g, guint *n)
104 {
105 struct passwd *pw;
106 const gchar *name;
107 struct group *gr;
108
109 *u = getuid();
110 pw = getpwuid(*u);
111 name = pw->pw_name;
112
113 *g = g_new(gid_t, *n=1);
114 (*g)[0] = getgid();
115
116 while ((gr = getgrent())) {
117 if (gr->gr_gid != (*g)[0]) { /* skip the main group */
118 gchar **c;
119 for (c = gr->gr_mem; *c; ++c)
120 if (strcmp(*c, name) == 0) {
121 *g = g_renew(gid_t, *g, ++(*n)); /* save the group */
122 (*g)[*n-1] = gr->gr_gid;
123 break;
124 }
125 }
126 }
127 endgrent();
128
129 qsort(*g, *n, sizeof(gid_t), gid_cmp);
130 }
131
132 ObtPaths* obt_paths_new(void)
133 {
134 ObtPaths *p;
135 const gchar *path;
136 GSList *it;
137
138 p = g_slice_new0(ObtPaths);
139 p->ref = 1;
140
141 find_uid_gid(&p->uid, &p->gid, &p->n_gid);
142
143 path = g_getenv("XDG_CONFIG_HOME");
144 if (path && path[0] != '\0') /* not unset or empty */
145 p->config_home = g_build_filename(path, NULL);
146 else
147 p->config_home = g_build_filename(g_get_home_dir(), ".config", NULL);
148
149 path = g_getenv("XDG_DATA_HOME");
150 if (path && path[0] != '\0') /* not unset or empty */
151 p->data_home = g_build_filename(path, NULL);
152 else
153 p->data_home = g_build_filename(g_get_home_dir(), ".local",
154 "share", NULL);
155
156 path = g_getenv("XDG_CACHE_HOME");
157 if (path && path[0] != '\0') /* not unset or empty */
158 p->cache_home = g_build_filename(path, NULL);
159 else
160 p->cache_home = g_build_filename(g_get_home_dir(), ".cache", NULL);
161
162 path = g_getenv("XDG_CONFIG_DIRS");
163 if (path && path[0] != '\0') /* not unset or empty */
164 p->config_dirs = split_paths(path);
165 else {
166 p->config_dirs = slist_path_add(p->config_dirs,
167 g_strdup(CONFIGDIR),
168 (GSListFunc) g_slist_append);
169 p->config_dirs = slist_path_add(p->config_dirs,
170 g_build_filename
171 (G_DIR_SEPARATOR_S,
172 "etc", "xdg", NULL),
173 (GSListFunc) g_slist_append);
174 }
175 p->config_dirs = slist_path_add(p->config_dirs,
176 g_strdup(p->config_home),
177 (GSListFunc) g_slist_prepend);
178
179 for (it = p->config_dirs; it; it = g_slist_next(it)) {
180 gchar *const s = g_strdup_printf("%s/autostart", (gchar*)it->data);
181 p->autostart_dirs = g_slist_append(p->autostart_dirs, s);
182 }
183
184 path = g_getenv("XDG_DATA_DIRS");
185 if (path && path[0] != '\0') /* not unset or empty */
186 p->data_dirs = split_paths(path);
187 else {
188 p->data_dirs = slist_path_add(p->data_dirs,
189 g_strdup(DATADIR),
190 (GSListFunc) g_slist_append);
191 p->data_dirs = slist_path_add(p->data_dirs,
192 g_build_filename
193 (G_DIR_SEPARATOR_S,
194 "usr", "local", "share", NULL),
195 (GSListFunc) g_slist_append);
196 p->data_dirs = slist_path_add(p->data_dirs,
197 g_build_filename
198 (G_DIR_SEPARATOR_S,
199 "usr", "share", NULL),
200 (GSListFunc) g_slist_append);
201 }
202 p->data_dirs = slist_path_add(p->data_dirs,
203 g_strdup(p->data_home),
204 (GSListFunc) g_slist_prepend);
205
206 path = g_getenv("PATH");
207 if (path && path[0] != '\0') /* not unset or empty */
208 p->exec_dirs = split_paths(path);
209 else
210 p->exec_dirs = NULL;
211
212 return p;
213 }
214
215 void obt_paths_ref(ObtPaths *p)
216 {
217 ++p->ref;
218 }
219
220 void obt_paths_unref(ObtPaths *p)
221 {
222 if (p && --p->ref == 0) {
223 GSList *it;
224
225 for (it = p->config_dirs; it; it = g_slist_next(it))
226 g_free(it->data);
227 g_slist_free(p->config_dirs);
228 for (it = p->data_dirs; it; it = g_slist_next(it))
229 g_free(it->data);
230 g_slist_free(p->data_dirs);
231 for (it = p->autostart_dirs; it; it = g_slist_next(it))
232 g_free(it->data);
233 g_slist_free(p->autostart_dirs);
234 for (it = p->exec_dirs; it; it = g_slist_next(it))
235 g_free(it->data);
236 g_slist_free(p->exec_dirs);
237 g_free(p->config_home);
238 g_free(p->data_home);
239 g_free(p->cache_home);
240
241 g_slice_free(ObtPaths, p);
242 }
243 }
244
245 gchar *obt_paths_expand_tilde(const gchar *f)
246 {
247 gchar *ret;
248 GRegex *regex;
249
250 if (!f)
251 return NULL;
252
253 regex = g_regex_new("(?:^|(?<=[ \\t]))~(?=[/ \\t$])", G_REGEX_MULTILINE | G_REGEX_RAW, 0, NULL);
254 ret = g_regex_replace_literal(regex, f, -1, 0, g_get_home_dir(), 0, NULL);
255 g_regex_unref(regex);
256
257 return ret;
258 }
259
260 gboolean obt_paths_mkdir(const gchar *path, gint mode)
261 {
262 gboolean ret = TRUE;
263
264 g_return_val_if_fail(path != NULL, FALSE);
265 g_return_val_if_fail(path[0] != '\0', FALSE);
266
267 if (!g_file_test(path, G_FILE_TEST_IS_DIR))
268 if (mkdir(path, mode) == -1)
269 ret = FALSE;
270
271 return ret;
272 }
273
274 gboolean obt_paths_mkdir_path(const gchar *path, gint mode)
275 {
276 gboolean ret = TRUE;
277
278 g_return_val_if_fail(path != NULL, FALSE);
279 g_return_val_if_fail(path[0] == '/', FALSE);
280
281 if (!g_file_test(path, G_FILE_TEST_IS_DIR)) {
282 gchar *c, *e;
283
284 c = g_strdup(path);
285 e = c;
286 while ((e = strchr(e + 1, '/'))) {
287 *e = '\0';
288 if (!(ret = obt_paths_mkdir(c, mode)))
289 goto parse_mkdir_path_end;
290 *e = '/';
291 }
292 ret = obt_paths_mkdir(c, mode);
293
294 parse_mkdir_path_end:
295 g_free(c);
296 }
297
298 return ret;
299 }
300
301 const gchar* obt_paths_config_home(ObtPaths *p)
302 {
303 return p->config_home;
304 }
305
306 const gchar* obt_paths_data_home(ObtPaths *p)
307 {
308 return p->data_home;
309 }
310
311 const gchar* obt_paths_cache_home(ObtPaths *p)
312 {
313 return p->cache_home;
314 }
315
316 GSList* obt_paths_config_dirs(ObtPaths *p)
317 {
318 return p->config_dirs;
319 }
320
321 GSList* obt_paths_data_dirs(ObtPaths *p)
322 {
323 return p->data_dirs;
324 }
325
326 GSList* obt_paths_autostart_dirs(ObtPaths *p)
327 {
328 return p->autostart_dirs;
329 }
330
331 static inline gboolean try_exec(const ObtPaths *const p,
332 const gchar *const path)
333 {
334 struct stat st;
335 BSEARCH_SETUP(guint);
336
337 if (stat(path, &st) != 0)
338 return FALSE;
339
340 if (!S_ISREG(st.st_mode))
341 return FALSE;
342 if (st.st_uid == p->uid)
343 return st.st_mode & S_IXUSR;
344 BSEARCH(guint, p->gid, 0, p->n_gid, st.st_gid);
345 if (BSEARCH_FOUND())
346 return st.st_mode & S_IXGRP;
347 return st.st_mode & S_IXOTH;
348 }
349
350 gboolean obt_paths_try_exec(ObtPaths *p, const gchar *path)
351 {
352 if (path[0] == '/') {
353 return try_exec(p, path);
354 }
355 else {
356 GSList *it;
357
358 for (it = p->exec_dirs; it; it = g_slist_next(it)) {
359 gchar *f = g_strdup_printf(it->data, G_DIR_SEPARATOR_S, path);
360 gboolean e = try_exec(p, f);
361 g_free(f);
362 if (e) return TRUE;
363 }
364 }
365
366 return FALSE;
367 }
This page took 0.04792 seconds and 3 git commands to generate.