Fix gcc warnings
[dana/openbox.git] / 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         if ((*it)[0]) /* skip empty strings */
93             list = slist_path_add(list, *it, (GSListFunc) g_slist_append);
94     }
95     g_free(spl);
96     return list;
97 }
98
99 int gid_cmp(const void *va, const void *vb)
100 {
101     const gid_t a = *(const gid_t*)va, b = *(const gid_t*)vb;
102     return a>b ? 1 : (a == b ? 0 : -1);
103 }
104
105 static void find_uid_gid(uid_t *u, gid_t **g, guint *n)
106 {
107     struct passwd *pw;
108     const gchar *name;
109     struct group *gr;
110
111     *u = getuid();
112     pw = getpwuid(*u);
113     name = pw->pw_name;
114
115     *g = g_new(gid_t, *n=1);
116     (*g)[0] = getgid();
117
118     while ((gr = getgrent())) {
119         if (gr->gr_gid != (*g)[0]) { /* skip the main group */
120             gchar **c;
121             for (c = gr->gr_mem; *c; ++c)
122                 if (strcmp(*c, name) == 0) {
123                     *g = g_renew(gid_t, *g, ++(*n)); /* save the group */
124                     (*g)[*n-1] = gr->gr_gid;
125                     break;
126                 }
127         }
128     }
129     endgrent();
130
131     qsort(*g, *n, sizeof(gid_t), gid_cmp);
132 }
133
134 ObtPaths* obt_paths_new(void)
135 {
136     ObtPaths *p;
137     const gchar *path;
138     GSList *it;
139
140     p = g_slice_new0(ObtPaths);
141     p->ref = 1;
142
143     find_uid_gid(&p->uid, &p->gid, &p->n_gid);
144
145     path = g_getenv("XDG_CONFIG_HOME");
146     if (path && path[0] != '\0') /* not unset or empty */
147         p->config_home = g_build_filename(path, NULL);
148     else
149         p->config_home = g_build_filename(g_get_home_dir(), ".config", NULL);
150
151     path = g_getenv("XDG_DATA_HOME");
152     if (path && path[0] != '\0') /* not unset or empty */
153         p->data_home = g_build_filename(path, NULL);
154     else
155         p->data_home = g_build_filename(g_get_home_dir(), ".local",
156                                         "share", NULL);
157
158     path = g_getenv("XDG_CACHE_HOME");
159     if (path && path[0] != '\0') /* not unset or empty */
160         p->cache_home = g_build_filename(path, NULL);
161     else
162         p->cache_home = g_build_filename(g_get_home_dir(), ".cache", NULL);
163
164     path = g_getenv("XDG_CONFIG_DIRS");
165     if (path && path[0] != '\0') /* not unset or empty */
166         p->config_dirs = split_paths(path);
167     else {
168         p->config_dirs = slist_path_add(p->config_dirs,
169                                         g_strdup(CONFIGDIR),
170                                         (GSListFunc) g_slist_append);
171         p->config_dirs = slist_path_add(p->config_dirs,
172                                         g_build_filename
173                                         (G_DIR_SEPARATOR_S,
174                                          "etc", "xdg", NULL),
175                                         (GSListFunc) g_slist_append);
176     }
177     p->config_dirs = slist_path_add(p->config_dirs,
178                                     g_strdup(p->config_home),
179                                     (GSListFunc) g_slist_prepend);
180
181     for (it = p->config_dirs; it; it = g_slist_next(it)) {
182         gchar *const s = g_strdup_printf("%s/autostart", (gchar*)it->data);
183         p->autostart_dirs = g_slist_append(p->autostart_dirs, s);
184     }
185
186     path = g_getenv("XDG_DATA_DIRS");
187     if (path && path[0] != '\0') /* not unset or empty */
188         p->data_dirs = split_paths(path);
189     else {
190         p->data_dirs = slist_path_add(p->data_dirs,
191                                       g_strdup(DATADIR),
192                                       (GSListFunc) g_slist_append);
193         p->data_dirs = slist_path_add(p->data_dirs,
194                                       g_build_filename
195                                       (G_DIR_SEPARATOR_S,
196                                        "usr", "local", "share", NULL),
197                                       (GSListFunc) g_slist_append);
198         p->data_dirs = slist_path_add(p->data_dirs,
199                                       g_build_filename
200                                       (G_DIR_SEPARATOR_S,
201                                        "usr", "share", NULL),
202                                       (GSListFunc) g_slist_append);
203     }
204     p->data_dirs = slist_path_add(p->data_dirs,
205                                   g_strdup(p->data_home),
206                                   (GSListFunc) g_slist_prepend);
207
208     path = g_getenv("PATH");
209     if (path && path[0] != '\0') /* not unset or empty */
210         p->exec_dirs = split_paths(path);
211     else
212         p->exec_dirs = NULL;
213
214     return p;
215 }
216
217 void obt_paths_ref(ObtPaths *p)
218 {
219     ++p->ref;
220 }
221
222 void obt_paths_unref(ObtPaths *p)
223 {
224     if (p && --p->ref == 0) {
225         GSList *it;
226
227         for (it = p->config_dirs; it; it = g_slist_next(it))
228             g_free(it->data);
229         g_slist_free(p->config_dirs);
230         for (it = p->data_dirs; it; it = g_slist_next(it))
231             g_free(it->data);
232         g_slist_free(p->data_dirs);
233         for (it = p->autostart_dirs; it; it = g_slist_next(it))
234             g_free(it->data);
235         g_slist_free(p->autostart_dirs);
236         for (it = p->exec_dirs; it; it = g_slist_next(it))
237             g_free(it->data);
238         g_slist_free(p->exec_dirs);
239         g_free(p->config_home);
240         g_free(p->data_home);
241         g_free(p->cache_home);
242         g_free(p->gid);
243
244         g_slice_free(ObtPaths, p);
245     }
246 }
247
248 gchar *obt_paths_expand_tilde(const gchar *f)
249 {
250     gchar *ret;
251     GRegex *regex;
252
253     if (!f)
254         return NULL;
255
256     regex = g_regex_new("(?:^|(?<=[ \\t]))~(?=[/ \\t$])", G_REGEX_MULTILINE | G_REGEX_RAW, 0, NULL);
257     ret = g_regex_replace_literal(regex, f, -1, 0, g_get_home_dir(), 0, NULL);
258     g_regex_unref(regex);
259
260     return ret;
261 }
262
263 gboolean obt_paths_mkdir(const gchar *path, gint mode)
264 {
265     gboolean ret = TRUE;
266
267     g_return_val_if_fail(path != NULL, FALSE);
268     g_return_val_if_fail(path[0] != '\0', FALSE);
269
270     if (!g_file_test(path, G_FILE_TEST_IS_DIR))
271         if (mkdir(path, mode) == -1)
272             ret = FALSE;
273
274     return ret;
275 }
276
277 gboolean obt_paths_mkdir_path(const gchar *path, gint mode)
278 {
279     gboolean ret = TRUE;
280
281     g_return_val_if_fail(path != NULL, FALSE);
282     g_return_val_if_fail(path[0] == '/', FALSE);
283
284     if (!g_file_test(path, G_FILE_TEST_IS_DIR)) {
285         gchar *c, *e;
286
287         c = g_strdup(path);
288         e = c;
289         while ((e = strchr(e + 1, '/'))) {
290             *e = '\0';
291             if (!(ret = obt_paths_mkdir(c, mode)))
292                 goto parse_mkdir_path_end;
293             *e = '/';
294         }
295         ret = obt_paths_mkdir(c, mode);
296
297     parse_mkdir_path_end:
298         g_free(c);
299     }
300
301     return ret;
302 }
303
304 const gchar* obt_paths_config_home(ObtPaths *p)
305 {
306     return p->config_home;
307 }
308
309 const gchar* obt_paths_data_home(ObtPaths *p)
310 {
311     return p->data_home;
312 }
313
314 const gchar* obt_paths_cache_home(ObtPaths *p)
315 {
316     return p->cache_home;
317 }
318
319 GSList* obt_paths_config_dirs(ObtPaths *p)
320 {
321     return p->config_dirs;
322 }
323
324 GSList* obt_paths_data_dirs(ObtPaths *p)
325 {
326     return p->data_dirs;
327 }
328
329 GSList* obt_paths_autostart_dirs(ObtPaths *p)
330 {
331     return p->autostart_dirs;
332 }
333
334 static inline gboolean try_exec(const ObtPaths *const p,
335                                 const gchar *const path)
336 {
337     struct stat st;
338     BSEARCH_SETUP();
339
340     if (stat(path, &st) != 0)
341         return FALSE;
342
343     if (!S_ISREG(st.st_mode))
344         return FALSE;
345     if (st.st_uid == p->uid)
346         return st.st_mode & S_IXUSR;
347     BSEARCH(guint, p->gid, 0, p->n_gid, st.st_gid);
348     if (BSEARCH_FOUND())
349         return st.st_mode & S_IXGRP;
350     return st.st_mode & S_IXOTH;
351 }
352
353 gboolean obt_paths_try_exec(ObtPaths *p, const gchar *path)
354 {
355     if (path[0] == '/') {
356         return try_exec(p, path);
357     }
358     else {
359         GSList *it;
360
361         for (it = p->exec_dirs; it; it = g_slist_next(it)) {
362             gchar *f = g_build_filename(it->data, path, NULL);
363             gboolean e = try_exec(p, f);
364             g_free(f);
365             if (e) return TRUE;
366         }
367     }
368
369     return FALSE;
370 }