blob: ebd86688d74699546d1f548a406cc05f1bc9aa54 [file] [log] [blame]
Petr Machata41498912012-12-04 17:57:34 +01001/*
2 * This file is part of ltrace.
3 * Copyright (C) 2012 Petr Machata
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18 * 02110-1301 USA
19 */
20
21#define _POSIX_C_SOURCE 200809L
22#include <sys/types.h>
23#include <alloca.h>
24#include <errno.h>
25#include <pwd.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <unistd.h>
30
31#include "sysdep.h"
32#include "vect.h"
33#include "dict.h"
34#include "options.h"
35
36static char *
37append(const char *str1, const char *str2)
38{
39 char *ret = malloc(strlen(str1) + strlen(str2) + 2);
40 if (ret == NULL)
41 return ret;
42 strcpy(stpcpy(ret, str1), str2);
43 return ret;
44}
45
46static void
47add_dir(struct vect *dirs, const char *str1, const char *str2)
48{
49 char *dir = append(str1, str2);
50 if (dir != NULL
51 && VECT_PUSHBACK(dirs, &dir) < 0)
52 fprintf(stderr,
53 "Couldn't store candidate config directory %s%s: %s.\n",
54 str1, str2, strerror(errno));
55}
56
57static enum callback_status
58add_dir_component_cb(struct opt_F_t *entry, void *data)
59{
60 struct vect *dirs = data;
61 if (opt_F_get_kind(entry) == OPT_F_DIR)
62 add_dir(dirs, entry->pathname, "/ltrace");
63 return CBS_CONT;
64}
65
66static void
67destroy_opt_F_cb(struct opt_F_t *entry, void *data)
68{
69 opt_F_destroy(entry);
70}
71
72int
73os_get_config_dirs(int private, const char ***retp)
74{
75 /* Vector of char *. Contains first pointers to local paths,
76 * then NULL, then pointers to system paths, then another
77 * NULL. SYS_START points to the beginning of the second
78 * part. */
79 static struct vect dirs;
80 static ssize_t sys_start = 0;
81
82again:
83 if (sys_start != 0) {
84 if (sys_start == -1)
85 return -1;
86
87 if (private)
88 *retp = VECT_ELEMENT(&dirs, const char *, 0);
89 else
90 *retp = VECT_ELEMENT(&dirs, const char *,
91 (size_t)sys_start);
92 return 0;
93 }
94
95 VECT_INIT(&dirs, char *);
96
97 char *home = getenv("HOME");
98 if (home == NULL) {
99 struct passwd *pwd = getpwuid(getuid());
100 if (pwd != NULL)
101 home = pwd->pw_dir;
102 }
103 /* The values coming from getenv and getpwuid may not be
104 * persistent. */
105 {
106 char *tmp = alloca(strlen(home) + 1);
107 strcpy(tmp, home);
108 home = tmp;
109 }
110
111 char *xdg_home = getenv("XDG_CONFIG_HOME");
112 if (xdg_home == NULL && home != NULL) {
113 xdg_home = alloca(strlen(home) + sizeof "/.config");
114 strcpy(stpcpy(xdg_home, home), "/.config");
115 }
116 if (xdg_home != NULL)
117 add_dir(&dirs, xdg_home, "/ltrace");
118 if (home != NULL)
119 add_dir(&dirs, home, "/.ltrace");
120
121 char *delim = NULL;
122 if (VECT_PUSHBACK(&dirs, &delim) < 0) {
123 fail:
124 /* This can't work :( */
125 fprintf(stderr,
126 "Couldn't initialize list of config directories: %s.\n",
127 strerror(errno));
128 VECT_DESTROY(&dirs, const char *, dict_dtor_string, NULL);
129 sys_start = -1;
130 return -1;
131 }
132 sys_start = vect_size(&dirs);
133
134 /* """preference-ordered set of base directories to search for
135 * configuration files in addition to the $XDG_CONFIG_HOME
136 * base directory. The directories in $XDG_CONFIG_DIRS should
137 * be seperated with a colon ':'.""" */
138 char *xdg_sys = getenv("XDG_CONFIG_DIRS");
139 if (xdg_sys != NULL) {
140 struct vect v;
141 VECT_INIT(&v, struct opt_F_t);
142 if (parse_colon_separated_list(xdg_sys, &v) < 0
143 || VECT_EACH(&v, struct opt_F_t, NULL,
144 add_dir_component_cb, &dirs) != NULL)
145 fprintf(stderr,
146 "Error processing $XDG_CONFIG_DIRS '%s': %s\n",
147 xdg_sys, strerror(errno));
148 VECT_DESTROY(&v, struct opt_F_t, destroy_opt_F_cb, NULL);
149 }
150
151 /* SYSCONFDIF is passed via -D when compiling. */
152 const char *sysconfdir = SYSCONFDIR;
153 if (sysconfdir != NULL)
154 add_dir(&dirs, sysconfdir, "");
155
156 if (VECT_PUSHBACK(&dirs, &delim) < 0)
157 goto fail;
158
159 goto again;
160}