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
[] =
63 { &mc_config_str
, MC_CONFIG_FILE
},
64 { &mc_config_str
, MC_FHL_INI_FILE
},
65 { &mc_config_str
, MC_HOTLIST_FILE
},
66 { &mc_config_str
, GLOBAL_KEYMAP_FILE
},
67 { &mc_config_str
, MC_USERMENU_FILE
},
68 { &mc_config_str
, EDIT_HOME_MENU
},
69 { &mc_config_str
, MC_PANELS_FILE
},
71 /* User should move this file with applying some changes in file */
72 { &mc_config_str
, MC_EXT_FILE
},
73 { &mc_config_str
, MC_EXT_OLD_FILE
},
76 { &mc_data_str
, MC_SKINS_DIR
},
77 { &mc_data_str
, VFS_SHELL_PREFIX
},
78 { &mc_data_str
, MC_ASHRC_FILE
},
79 { &mc_data_str
, MC_BASHRC_FILE
},
80 { &mc_data_str
, MC_INPUTRC_FILE
},
81 { &mc_data_str
, MC_ZSHRC_FILE
},
82 { &mc_data_str
, MC_EXTFS_DIR
},
83 { &mc_data_str
, MC_HISTORY_FILE
},
84 { &mc_data_str
, MC_FILEPOS_FILE
},
85 { &mc_data_str
, EDIT_SYNTAX_FILE
},
86 { &mc_data_str
, EDIT_HOME_CLIP_FILE
},
87 { &mc_data_str
, MC_MACRO_FILE
},
90 { &mc_cache_str
, "mc.log" },
91 { &mc_cache_str
, MC_TREESTORE_FILE
},
92 { &mc_cache_str
, EDIT_HOME_TEMP_FILE
},
93 { &mc_cache_str
, EDIT_HOME_BLOCK_FILE
},
99 /* --------------------------------------------------------------------------------------------- */
100 /*** file scope functions *********************************************************************** */
101 /* --------------------------------------------------------------------------------------------- */
104 mc_config_mkdir (const char *directory_name
, GError
** mcerror
)
106 mc_return_if_error (mcerror
);
108 if ((!g_file_test (directory_name
, G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
)) &&
109 (g_mkdir_with_parents (directory_name
, 0700) != 0))
110 mc_propagate_error (mcerror
, 0, _("Cannot create %s directory"), directory_name
);
113 /* --------------------------------------------------------------------------------------------- */
116 mc_config_init_one_config_path (const char *path_base
, const char *subdir
, GError
** mcerror
)
120 mc_return_val_if_error (mcerror
, FALSE
);
122 full_path
= g_build_filename (path_base
, subdir
, (char *) NULL
);
124 if (g_file_test (full_path
, G_FILE_TEST_EXISTS
))
126 if (g_file_test (full_path
, G_FILE_TEST_IS_DIR
))
127 config_dir_present
= TRUE
;
130 fprintf (stderr
, "%s %s\n", _("FATAL: not a directory:"), full_path
);
135 mc_config_mkdir (full_path
, mcerror
);
136 if (mcerror
!= NULL
&& *mcerror
!= NULL
)
137 MC_PTR_FREE (full_path
);
142 /* --------------------------------------------------------------------------------------------- */
143 /*** public functions ****************************************************************************/
144 /* --------------------------------------------------------------------------------------------- */
147 mc_config_init_config_paths (GError
** mcerror
)
149 const char *profile_root
;
152 mc_return_if_error (mcerror
);
154 if (xdg_vars_initialized
)
157 profile_root
= mc_get_profile_root ();
159 if (strcmp (profile_root
, mc_config_get_home_dir ()) != 0)
162 * The user overrode the default profile root.
164 * In this case we can't use GLib's g_get_user_{config,cache,data}_dir()
165 * as these functions use the user's home dir as the root.
168 dir
= g_build_filename (profile_root
, ".config", (char *) NULL
);
169 mc_config_str
= mc_config_init_one_config_path (dir
, MC_USERCONF_DIR
, mcerror
);
172 dir
= g_build_filename (profile_root
, ".cache", (char *) NULL
);
173 mc_cache_str
= mc_config_init_one_config_path (dir
, MC_USERCONF_DIR
, mcerror
);
176 dir
= g_build_filename (profile_root
, ".local", "share", (char *) NULL
);
177 mc_data_str
= mc_config_init_one_config_path (dir
, MC_USERCONF_DIR
, mcerror
);
183 mc_config_init_one_config_path (g_get_user_config_dir (), MC_USERCONF_DIR
, mcerror
);
185 mc_config_init_one_config_path (g_get_user_cache_dir (), MC_USERCONF_DIR
, mcerror
);
187 mc_config_init_one_config_path (g_get_user_data_dir (), MC_USERCONF_DIR
, mcerror
);
190 xdg_vars_initialized
= TRUE
;
193 /* --------------------------------------------------------------------------------------------- */
196 mc_config_deinit_config_paths (void)
198 if (!xdg_vars_initialized
)
201 g_free (mc_config_str
);
202 g_free (mc_cache_str
);
203 g_free (mc_data_str
);
205 g_free (mc_global
.share_data_dir
);
206 g_free (mc_global
.sysconfig_dir
);
208 xdg_vars_initialized
= FALSE
;
211 /* --------------------------------------------------------------------------------------------- */
214 mc_config_get_data_path (void)
216 if (!xdg_vars_initialized
)
217 mc_config_init_config_paths (NULL
);
219 return (const char *) mc_data_str
;
222 /* --------------------------------------------------------------------------------------------- */
225 mc_config_get_cache_path (void)
227 if (!xdg_vars_initialized
)
228 mc_config_init_config_paths (NULL
);
230 return (const char *) mc_cache_str
;
233 /* --------------------------------------------------------------------------------------------- */
236 mc_config_get_home_dir (void)
238 static const char *homedir
= NULL
;
242 /* Prior to GLib 2.36, g_get_home_dir() ignores $HOME, which is why
243 * we read it ourselves. As that function's documentation explains,
244 * using $HOME is good for compatibility with other programs and
245 * for running from test frameworks. */
246 homedir
= g_getenv ("HOME");
247 if (homedir
== NULL
|| *homedir
== '\0')
248 homedir
= g_get_home_dir ();
254 /* --------------------------------------------------------------------------------------------- */
257 mc_config_get_path (void)
259 if (!xdg_vars_initialized
)
260 mc_config_init_config_paths (NULL
);
262 return (const char *) mc_config_str
;
265 /* --------------------------------------------------------------------------------------------- */
267 * Get full path to config file by short name.
269 * @param config_name short name
270 * @return full path to config file
274 mc_config_get_full_path (const char *config_name
)
278 if (config_name
== NULL
)
281 if (!xdg_vars_initialized
)
282 mc_config_init_config_paths (NULL
);
284 for (rule_index
= 0; mc_config_files_reference
[rule_index
].filename
!= NULL
; rule_index
++)
285 if (strcmp (config_name
, mc_config_files_reference
[rule_index
].filename
) == 0)
286 return g_build_filename (*mc_config_files_reference
[rule_index
].basedir
,
287 mc_config_files_reference
[rule_index
].filename
, (char *) NULL
);
292 /* --------------------------------------------------------------------------------------------- */
294 * Get full path to config file by short name.
296 * @param config_name short name
297 * @return object with full path to config file
301 mc_config_get_full_vpath (const char *config_name
)
303 vfs_path_t
*ret_vpath
;
306 str_path
= mc_config_get_full_path (config_name
);
308 ret_vpath
= vfs_path_from_str (str_path
);
314 /* --------------------------------------------------------------------------------------------- */