Update portuguese translation
[openbox.git] / obt / paths.c
blobd2e230dffd028e39cf144b6811c559536eda2e0c
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/bsearch.h"
20 #include "obt/paths.h"
21 #include "obt/util.h"
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
45 struct _ObtPaths
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;
56 uid_t uid;
57 gid_t *gid;
58 guint n_gid;
61 static gint slist_path_cmp(const gchar *a, const gchar *b)
63 return strcmp(a, b);
66 typedef GSList* (*GSListFunc) (gpointer list, gconstpointer data);
68 static GSList* slist_path_add(GSList *list, gpointer data, GSListFunc func)
70 g_assert(func);
72 if (!data)
73 return list;
75 if (!g_slist_find_custom(list, data, (GCompareFunc) slist_path_cmp))
76 list = func(list, data);
77 else
78 g_free(data);
80 return list;
83 static GSList* split_paths(const gchar *paths)
85 GSList *list = NULL;
86 gchar **spl, **it;
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;
97 int gid_cmp(const void *va, const void *vb)
99 const gid_t a = *(const gid_t*)va, b = *(const gid_t*)vb;
100 return a>b ? 1 : (a == b ? 0 : -1);
103 static void find_uid_gid(uid_t *u, gid_t **g, guint *n)
105 struct passwd *pw;
106 const gchar *name;
107 struct group *gr;
109 *u = getuid();
110 pw = getpwuid(*u);
111 name = pw->pw_name;
113 *g = g_new(gid_t, *n=1);
114 (*g)[0] = getgid();
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;
127 endgrent();
129 qsort(*g, *n, sizeof(gid_t), gid_cmp);
132 ObtPaths* obt_paths_new(void)
134 ObtPaths *p;
135 const gchar *path;
136 GSList *it;
138 p = g_slice_new0(ObtPaths);
139 p->ref = 1;
141 find_uid_gid(&p->uid, &p->gid, &p->n_gid);
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);
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);
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);
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);
175 p->config_dirs = slist_path_add(p->config_dirs,
176 g_strdup(p->config_home),
177 (GSListFunc) g_slist_prepend);
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);
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);
202 p->data_dirs = slist_path_add(p->data_dirs,
203 g_strdup(p->data_home),
204 (GSListFunc) g_slist_prepend);
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;
212 return p;
215 void obt_paths_ref(ObtPaths *p)
217 ++p->ref;
220 void obt_paths_unref(ObtPaths *p)
222 if (p && --p->ref == 0) {
223 GSList *it;
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 g_free(p->gid);
242 g_slice_free(ObtPaths, p);
246 gchar *obt_paths_expand_tilde(const gchar *f)
248 gchar *ret;
249 GRegex *regex;
251 if (!f)
252 return NULL;
254 regex = g_regex_new("(?:^|(?<=[ \\t]))~(?=[/ \\t$])", G_REGEX_MULTILINE | G_REGEX_RAW, 0, NULL);
255 ret = g_regex_replace_literal(regex, f, -1, 0, g_get_home_dir(), 0, NULL);
256 g_regex_unref(regex);
258 return ret;
261 gboolean obt_paths_mkdir(const gchar *path, gint mode)
263 gboolean ret = TRUE;
265 g_return_val_if_fail(path != NULL, FALSE);
266 g_return_val_if_fail(path[0] != '\0', FALSE);
268 if (!g_file_test(path, G_FILE_TEST_IS_DIR))
269 if (mkdir(path, mode) == -1)
270 ret = FALSE;
272 return ret;
275 gboolean obt_paths_mkdir_path(const gchar *path, gint mode)
277 gboolean ret = TRUE;
279 g_return_val_if_fail(path != NULL, FALSE);
280 g_return_val_if_fail(path[0] == '/', FALSE);
282 if (!g_file_test(path, G_FILE_TEST_IS_DIR)) {
283 gchar *c, *e;
285 c = g_strdup(path);
286 e = c;
287 while ((e = strchr(e + 1, '/'))) {
288 *e = '\0';
289 if (!(ret = obt_paths_mkdir(c, mode)))
290 goto parse_mkdir_path_end;
291 *e = '/';
293 ret = obt_paths_mkdir(c, mode);
295 parse_mkdir_path_end:
296 g_free(c);
299 return ret;
302 const gchar* obt_paths_config_home(ObtPaths *p)
304 return p->config_home;
307 const gchar* obt_paths_data_home(ObtPaths *p)
309 return p->data_home;
312 const gchar* obt_paths_cache_home(ObtPaths *p)
314 return p->cache_home;
317 GSList* obt_paths_config_dirs(ObtPaths *p)
319 return p->config_dirs;
322 GSList* obt_paths_data_dirs(ObtPaths *p)
324 return p->data_dirs;
327 GSList* obt_paths_autostart_dirs(ObtPaths *p)
329 return p->autostart_dirs;
332 static inline gboolean try_exec(const ObtPaths *const p,
333 const gchar *const path)
335 struct stat st;
336 BSEARCH_SETUP(guint);
338 if (stat(path, &st) != 0)
339 return FALSE;
341 if (!S_ISREG(st.st_mode))
342 return FALSE;
343 if (st.st_uid == p->uid)
344 return st.st_mode & S_IXUSR;
345 BSEARCH(guint, p->gid, 0, p->n_gid, st.st_gid);
346 if (BSEARCH_FOUND())
347 return st.st_mode & S_IXGRP;
348 return st.st_mode & S_IXOTH;
351 gboolean obt_paths_try_exec(ObtPaths *p, const gchar *path)
353 if (path[0] == '/') {
354 return try_exec(p, path);
356 else {
357 GSList *it;
359 for (it = p->exec_dirs; it; it = g_slist_next(it)) {
360 gchar *f = g_strdup_printf(it->data, G_DIR_SEPARATOR_S, path);
361 gboolean e = try_exec(p, f);
362 g_free(f);
363 if (e) return TRUE;
367 return FALSE;