1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
|
/*
paths to configuration files
Copyright (C) 2010-2023
Free Software Foundation, Inc.
Written by:
Slava Zanko <slavazanko@gmail.com>, 2010.
This file is part of the Midnight Commander.
The Midnight Commander is free software: you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the License,
or (at your option) any later version.
The Midnight Commander is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include "lib/global.h"
#include "lib/fileloc.h"
#include "lib/vfs/vfs.h"
#include "lib/util.h" /* unix_error_string() */
#include "lib/mcconfig.h"
/*** global variables ****************************************************************************/
/*** file scope macro definitions ****************************************************************/
/*** file scope type declarations ****************************************************************/
/*** forward declarations (file scope functions) *************************************************/
/*** file scope variables ************************************************************************/
static gboolean xdg_vars_initialized = FALSE;
static char *mc_config_str = NULL;
static char *mc_cache_str = NULL;
static char *mc_data_str = NULL;
static gboolean config_dir_present = FALSE;
static const struct
{
char **basedir;
const char *filename;
} mc_config_files_reference[] =
{
/* *INDENT-OFF* */
/* config */
{ &mc_config_str, MC_CONFIG_FILE },
{ &mc_config_str, MC_FHL_INI_FILE },
{ &mc_config_str, MC_HOTLIST_FILE },
{ &mc_config_str, GLOBAL_KEYMAP_FILE },
{ &mc_config_str, MC_USERMENU_FILE },
{ &mc_config_str, EDIT_HOME_MENU },
{ &mc_config_str, MC_PANELS_FILE },
/* User should move this file with applying some changes in file */
{ &mc_config_str, MC_EXT_FILE },
{ &mc_config_str, MC_EXT_OLD_FILE },
/* data */
{ &mc_data_str, MC_SKINS_DIR },
{ &mc_data_str, FISH_PREFIX },
{ &mc_data_str, MC_ASHRC_FILE },
{ &mc_data_str, MC_BASHRC_FILE },
{ &mc_data_str, MC_INPUTRC_FILE },
{ &mc_data_str, MC_ZSHRC_FILE },
{ &mc_data_str, MC_EXTFS_DIR },
{ &mc_data_str, MC_HISTORY_FILE },
{ &mc_data_str, MC_FILEPOS_FILE },
{ &mc_data_str, EDIT_SYNTAX_FILE },
{ &mc_data_str, EDIT_HOME_CLIP_FILE },
{ &mc_data_str, MC_MACRO_FILE },
/* cache */
{ &mc_cache_str, "mc.log" },
{ &mc_cache_str, MC_TREESTORE_FILE },
{ &mc_cache_str, EDIT_HOME_TEMP_FILE },
{ &mc_cache_str, EDIT_HOME_BLOCK_FILE },
{ NULL, NULL }
/* *INDENT-ON* */
};
/* --------------------------------------------------------------------------------------------- */
/*** file scope functions *********************************************************************** */
/* --------------------------------------------------------------------------------------------- */
static void
mc_config_mkdir (const char *directory_name, GError ** mcerror)
{
mc_return_if_error (mcerror);
if ((!g_file_test (directory_name, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) &&
(g_mkdir_with_parents (directory_name, 0700) != 0))
mc_propagate_error (mcerror, 0, _("Cannot create %s directory"), directory_name);
}
/* --------------------------------------------------------------------------------------------- */
static char *
mc_config_init_one_config_path (const char *path_base, const char *subdir, GError ** mcerror)
{
char *full_path;
mc_return_val_if_error (mcerror, FALSE);
full_path = g_build_filename (path_base, subdir, (char *) NULL);
if (g_file_test (full_path, G_FILE_TEST_EXISTS))
{
if (g_file_test (full_path, G_FILE_TEST_IS_DIR))
config_dir_present = TRUE;
else
{
fprintf (stderr, "%s %s\n", _("FATAL: not a directory:"), full_path);
exit (EXIT_FAILURE);
}
}
mc_config_mkdir (full_path, mcerror);
if (mcerror != NULL && *mcerror != NULL)
MC_PTR_FREE (full_path);
return full_path;
}
/* --------------------------------------------------------------------------------------------- */
/*** public functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */
void
mc_config_init_config_paths (GError ** mcerror)
{
const char *profile_root;
char *dir;
mc_return_if_error (mcerror);
if (xdg_vars_initialized)
return;
profile_root = mc_get_profile_root ();
if (strcmp (profile_root, mc_config_get_home_dir ()) != 0)
{
/*
* The user overrode the default profile root.
*
* In this case we can't use GLib's g_get_user_{config,cache,data}_dir()
* as these functions use the user's home dir as the root.
*/
dir = g_build_filename (profile_root, ".config", (char *) NULL);
mc_config_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, mcerror);
g_free (dir);
dir = g_build_filename (profile_root, ".cache", (char *) NULL);
mc_cache_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, mcerror);
g_free (dir);
dir = g_build_filename (profile_root, ".local", "share", (char *) NULL);
mc_data_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, mcerror);
g_free (dir);
}
else
{
mc_config_str =
mc_config_init_one_config_path (g_get_user_config_dir (), MC_USERCONF_DIR, mcerror);
mc_cache_str =
mc_config_init_one_config_path (g_get_user_cache_dir (), MC_USERCONF_DIR, mcerror);
mc_data_str =
mc_config_init_one_config_path (g_get_user_data_dir (), MC_USERCONF_DIR, mcerror);
}
xdg_vars_initialized = TRUE;
}
/* --------------------------------------------------------------------------------------------- */
void
mc_config_deinit_config_paths (void)
{
if (!xdg_vars_initialized)
return;
g_free (mc_config_str);
g_free (mc_cache_str);
g_free (mc_data_str);
g_free (mc_global.share_data_dir);
g_free (mc_global.sysconfig_dir);
xdg_vars_initialized = FALSE;
}
/* --------------------------------------------------------------------------------------------- */
const char *
mc_config_get_data_path (void)
{
if (!xdg_vars_initialized)
mc_config_init_config_paths (NULL);
return (const char *) mc_data_str;
}
/* --------------------------------------------------------------------------------------------- */
const char *
mc_config_get_cache_path (void)
{
if (!xdg_vars_initialized)
mc_config_init_config_paths (NULL);
return (const char *) mc_cache_str;
}
/* --------------------------------------------------------------------------------------------- */
const char *
mc_config_get_home_dir (void)
{
static const char *homedir = NULL;
if (homedir == NULL)
{
/* Prior to GLib 2.36, g_get_home_dir() ignores $HOME, which is why
* we read it ourselves. As that function's documentation explains,
* using $HOME is good for compatibility with other programs and
* for running from test frameworks. */
homedir = g_getenv ("HOME");
if (homedir == NULL || *homedir == '\0')
homedir = g_get_home_dir ();
}
return homedir;
}
/* --------------------------------------------------------------------------------------------- */
const char *
mc_config_get_path (void)
{
if (!xdg_vars_initialized)
mc_config_init_config_paths (NULL);
return (const char *) mc_config_str;
}
/* --------------------------------------------------------------------------------------------- */
/**
* Get full path to config file by short name.
*
* @param config_name short name
* @return full path to config file
*/
char *
mc_config_get_full_path (const char *config_name)
{
size_t rule_index;
if (config_name == NULL)
return NULL;
if (!xdg_vars_initialized)
mc_config_init_config_paths (NULL);
for (rule_index = 0; mc_config_files_reference[rule_index].filename != NULL; rule_index++)
if (strcmp (config_name, mc_config_files_reference[rule_index].filename) == 0)
return g_build_filename (*mc_config_files_reference[rule_index].basedir,
mc_config_files_reference[rule_index].filename, (char *) NULL);
return NULL;
}
/* --------------------------------------------------------------------------------------------- */
/**
* Get full path to config file by short name.
*
* @param config_name short name
* @return object with full path to config file
*/
vfs_path_t *
mc_config_get_full_vpath (const char *config_name)
{
vfs_path_t *ret_vpath;
char *str_path;
str_path = mc_config_get_full_path (config_name);
ret_vpath = vfs_path_from_str (str_path);
g_free (str_path);
return ret_vpath;
}
/* --------------------------------------------------------------------------------------------- */
|