2 paths to configuration files
4 Copyright (C) 2010-2024
5 Free Software Foundation, Inc.
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/>.
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
;
59 } mc_config_files_reference
[] = {
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
},
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
},
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
},
98 /* --------------------------------------------------------------------------------------------- */
99 /*** file scope functions *********************************************************************** */
100 /* --------------------------------------------------------------------------------------------- */
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 /* --------------------------------------------------------------------------------------------- */
115 mc_config_init_one_config_path (const char *path_base
, const char *subdir
, GError
**mcerror
)
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
;
129 fprintf (stderr
, "%s %s\n", _("FATAL: not a directory:"), full_path
);
134 mc_config_mkdir (full_path
, mcerror
);
135 if (mcerror
!= NULL
&& *mcerror
!= NULL
)
136 MC_PTR_FREE (full_path
);
141 /* --------------------------------------------------------------------------------------------- */
142 /*** public functions ****************************************************************************/
143 /* --------------------------------------------------------------------------------------------- */
146 mc_config_init_config_paths (GError
**mcerror
)
148 const char *profile_root
;
151 mc_return_if_error (mcerror
);
153 if (xdg_vars_initialized
)
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
);
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
);
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
);
182 mc_config_init_one_config_path (g_get_user_config_dir (), MC_USERCONF_DIR
, mcerror
);
184 mc_config_init_one_config_path (g_get_user_cache_dir (), MC_USERCONF_DIR
, mcerror
);
186 mc_config_init_one_config_path (g_get_user_data_dir (), MC_USERCONF_DIR
, mcerror
);
189 xdg_vars_initialized
= TRUE
;
192 /* --------------------------------------------------------------------------------------------- */
195 mc_config_deinit_config_paths (void)
197 if (!xdg_vars_initialized
)
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 /* --------------------------------------------------------------------------------------------- */
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 /* --------------------------------------------------------------------------------------------- */
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 /* --------------------------------------------------------------------------------------------- */
235 mc_config_get_home_dir (void)
237 static const char *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 ();
253 /* --------------------------------------------------------------------------------------------- */
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
273 mc_config_get_full_path (const char *config_name
)
277 if (config_name
== 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
);
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
300 mc_config_get_full_vpath (const char *config_name
)
302 vfs_path_t
*ret_vpath
;
305 str_path
= mc_config_get_full_path (config_name
);
307 ret_vpath
= vfs_path_from_str (str_path
);
313 /* --------------------------------------------------------------------------------------------- */