tar: avoid strtoul().
[midnight-commander.git] / lib / mcconfig / paths.c
blob94ac1b43126e48cf57be8695278ae5e7d328a178
1 /*
2 paths to configuration files
4 Copyright (C) 2010-2024
5 Free Software Foundation, Inc.
7 Written by:
8 Slava Zanko <slavazanko@gmail.com>, 2010.
10 This file is part of the Midnight Commander.
12 The Midnight Commander is free software: you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation, either version 3 of the License,
15 or (at your option) any later version.
17 The Midnight Commander is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include <config.h>
28 #include <stdio.h>
29 #include <stdlib.h>
31 #include "lib/global.h"
32 #include "lib/fileloc.h"
33 #include "lib/vfs/vfs.h"
34 #include "lib/util.h" /* unix_error_string() */
36 #include "lib/mcconfig.h"
38 /*** global variables ****************************************************************************/
40 /*** file scope macro definitions ****************************************************************/
42 /*** file scope type declarations ****************************************************************/
44 /*** forward declarations (file scope functions) *************************************************/
46 /*** file scope variables ************************************************************************/
48 static gboolean xdg_vars_initialized = FALSE;
49 static char *mc_config_str = NULL;
50 static char *mc_cache_str = NULL;
51 static char *mc_data_str = NULL;
53 static gboolean config_dir_present = FALSE;
55 static const struct
57 char **basedir;
58 const char *filename;
59 } mc_config_files_reference[] = {
60 /* *INDENT-OFF* */
61 /* config */
62 { &mc_config_str, MC_CONFIG_FILE },
63 { &mc_config_str, MC_FHL_INI_FILE },
64 { &mc_config_str, MC_HOTLIST_FILE },
65 { &mc_config_str, GLOBAL_KEYMAP_FILE },
66 { &mc_config_str, MC_USERMENU_FILE },
67 { &mc_config_str, EDIT_HOME_MENU },
68 { &mc_config_str, MC_PANELS_FILE },
70 /* User should move this file with applying some changes in file */
71 { &mc_config_str, MC_EXT_FILE },
72 { &mc_config_str, MC_EXT_OLD_FILE },
74 /* data */
75 { &mc_data_str, MC_SKINS_DIR },
76 { &mc_data_str, VFS_SHELL_PREFIX },
77 { &mc_data_str, MC_ASHRC_FILE },
78 { &mc_data_str, MC_BASHRC_FILE },
79 { &mc_data_str, MC_INPUTRC_FILE },
80 { &mc_data_str, MC_ZSHRC_FILE },
81 { &mc_data_str, MC_EXTFS_DIR },
82 { &mc_data_str, MC_HISTORY_FILE },
83 { &mc_data_str, MC_FILEPOS_FILE },
84 { &mc_data_str, EDIT_SYNTAX_FILE },
85 { &mc_data_str, EDIT_HOME_CLIP_FILE },
86 { &mc_data_str, MC_MACRO_FILE },
88 /* cache */
89 { &mc_cache_str, "mc.log" },
90 { &mc_cache_str, MC_TREESTORE_FILE },
91 { &mc_cache_str, EDIT_HOME_TEMP_FILE },
92 { &mc_cache_str, EDIT_HOME_BLOCK_FILE },
94 { NULL, NULL }
95 /* *INDENT-ON* */
98 /* --------------------------------------------------------------------------------------------- */
99 /*** file scope functions *********************************************************************** */
100 /* --------------------------------------------------------------------------------------------- */
102 static void
103 mc_config_mkdir (const char *directory_name, GError **mcerror)
105 mc_return_if_error (mcerror);
107 if ((!g_file_test (directory_name, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) &&
108 (g_mkdir_with_parents (directory_name, 0700) != 0))
109 mc_propagate_error (mcerror, 0, _("Cannot create %s directory"), directory_name);
112 /* --------------------------------------------------------------------------------------------- */
114 static char *
115 mc_config_init_one_config_path (const char *path_base, const char *subdir, GError **mcerror)
117 char *full_path;
119 mc_return_val_if_error (mcerror, FALSE);
121 full_path = g_build_filename (path_base, subdir, (char *) NULL);
123 if (g_file_test (full_path, G_FILE_TEST_EXISTS))
125 if (g_file_test (full_path, G_FILE_TEST_IS_DIR))
126 config_dir_present = TRUE;
127 else
129 fprintf (stderr, "%s %s\n", _("FATAL: not a directory:"), full_path);
130 exit (EXIT_FAILURE);
134 mc_config_mkdir (full_path, mcerror);
135 if (mcerror != NULL && *mcerror != NULL)
136 MC_PTR_FREE (full_path);
138 return full_path;
141 /* --------------------------------------------------------------------------------------------- */
142 /*** public functions ****************************************************************************/
143 /* --------------------------------------------------------------------------------------------- */
145 void
146 mc_config_init_config_paths (GError **mcerror)
148 const char *profile_root;
149 char *dir;
151 mc_return_if_error (mcerror);
153 if (xdg_vars_initialized)
154 return;
156 profile_root = mc_get_profile_root ();
158 if (strcmp (profile_root, mc_config_get_home_dir ()) != 0)
161 * The user overrode the default profile root.
163 * In this case we can't use GLib's g_get_user_{config,cache,data}_dir()
164 * as these functions use the user's home dir as the root.
167 dir = g_build_filename (profile_root, ".config", (char *) NULL);
168 mc_config_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, mcerror);
169 g_free (dir);
171 dir = g_build_filename (profile_root, ".cache", (char *) NULL);
172 mc_cache_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, mcerror);
173 g_free (dir);
175 dir = g_build_filename (profile_root, ".local", "share", (char *) NULL);
176 mc_data_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, mcerror);
177 g_free (dir);
179 else
181 mc_config_str =
182 mc_config_init_one_config_path (g_get_user_config_dir (), MC_USERCONF_DIR, mcerror);
183 mc_cache_str =
184 mc_config_init_one_config_path (g_get_user_cache_dir (), MC_USERCONF_DIR, mcerror);
185 mc_data_str =
186 mc_config_init_one_config_path (g_get_user_data_dir (), MC_USERCONF_DIR, mcerror);
189 xdg_vars_initialized = TRUE;
192 /* --------------------------------------------------------------------------------------------- */
194 void
195 mc_config_deinit_config_paths (void)
197 if (!xdg_vars_initialized)
198 return;
200 g_free (mc_config_str);
201 g_free (mc_cache_str);
202 g_free (mc_data_str);
204 g_free (mc_global.share_data_dir);
205 g_free (mc_global.sysconfig_dir);
207 xdg_vars_initialized = FALSE;
210 /* --------------------------------------------------------------------------------------------- */
212 const char *
213 mc_config_get_data_path (void)
215 if (!xdg_vars_initialized)
216 mc_config_init_config_paths (NULL);
218 return (const char *) mc_data_str;
221 /* --------------------------------------------------------------------------------------------- */
223 const char *
224 mc_config_get_cache_path (void)
226 if (!xdg_vars_initialized)
227 mc_config_init_config_paths (NULL);
229 return (const char *) mc_cache_str;
232 /* --------------------------------------------------------------------------------------------- */
234 const char *
235 mc_config_get_home_dir (void)
237 static const char *homedir = NULL;
239 if (homedir == NULL)
241 /* Prior to GLib 2.36, g_get_home_dir() ignores $HOME, which is why
242 * we read it ourselves. As that function's documentation explains,
243 * using $HOME is good for compatibility with other programs and
244 * for running from test frameworks. */
245 homedir = g_getenv ("HOME");
246 if (homedir == NULL || *homedir == '\0')
247 homedir = g_get_home_dir ();
250 return homedir;
253 /* --------------------------------------------------------------------------------------------- */
255 const char *
256 mc_config_get_path (void)
258 if (!xdg_vars_initialized)
259 mc_config_init_config_paths (NULL);
261 return (const char *) mc_config_str;
264 /* --------------------------------------------------------------------------------------------- */
266 * Get full path to config file by short name.
268 * @param config_name short name
269 * @return full path to config file
272 char *
273 mc_config_get_full_path (const char *config_name)
275 size_t rule_index;
277 if (config_name == NULL)
278 return NULL;
280 if (!xdg_vars_initialized)
281 mc_config_init_config_paths (NULL);
283 for (rule_index = 0; mc_config_files_reference[rule_index].filename != NULL; rule_index++)
284 if (strcmp (config_name, mc_config_files_reference[rule_index].filename) == 0)
285 return g_build_filename (*mc_config_files_reference[rule_index].basedir,
286 mc_config_files_reference[rule_index].filename, (char *) NULL);
288 return NULL;
291 /* --------------------------------------------------------------------------------------------- */
293 * Get full path to config file by short name.
295 * @param config_name short name
296 * @return object with full path to config file
299 vfs_path_t *
300 mc_config_get_full_vpath (const char *config_name)
302 vfs_path_t *ret_vpath;
303 char *str_path;
305 str_path = mc_config_get_full_path (config_name);
307 ret_vpath = vfs_path_from_str (str_path);
308 g_free (str_path);
310 return ret_vpath;
313 /* --------------------------------------------------------------------------------------------- */