From patchwork Tue Jun 23 13:20:03 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: [v2,4/7] Add get_standard_config_dir function X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 37656 Message-Id: <20200623132006.15863-5-tom@tromey.com> To: gdb-patches@sourceware.org Cc: Tom Tromey Date: Tue, 23 Jun 2020 07:20:03 -0600 From: Tom Tromey List-Id: Gdb-patches mailing list This adds a new get_standard_config_dir, which returns the name of the configuration directory. In XDG, this is ~/.config/gdb/. Future patches will make use of this. 2020-06-22 Tom Tromey * pathstuff.h (get_standard_config_dir): Declare. * pathstuff.cc (get_standard_config_dir): New function. --- gdbsupport/ChangeLog | 5 +++++ gdbsupport/pathstuff.cc | 32 ++++++++++++++++++++++++++++++++ gdbsupport/pathstuff.h | 14 ++++++++++++++ 3 files changed, 51 insertions(+) -- 2.17.2 diff --git a/gdbsupport/pathstuff.cc b/gdbsupport/pathstuff.cc index 1f60fd0c986..9fb5e5cf614 100644 --- a/gdbsupport/pathstuff.cc +++ b/gdbsupport/pathstuff.cc @@ -266,6 +266,38 @@ get_standard_temp_dir () #endif } +/* See pathstuff.h. */ + +std::string +get_standard_config_dir () +{ +#ifdef __APPLE__ +#define HOME_CONFIG_DIR "Library/Preferences" +#else +#define HOME_CONFIG_DIR ".config" +#endif + +#ifndef __APPLE__ + const char *xdg_config_home = getenv ("XDG_CONFIG_HOME"); + if (xdg_config_home != NULL) + { + /* Make sure the path is absolute and tilde-expanded. */ + gdb::unique_xmalloc_ptr abs (gdb_abspath (xdg_config_home)); + return string_printf ("%s/gdb", abs.get ()); + } +#endif + + const char *home = getenv ("HOME"); + if (home != NULL) + { + /* Make sure the path is absolute and tilde-expanded. */ + gdb::unique_xmalloc_ptr abs (gdb_abspath (home)); + return string_printf ("%s/" HOME_CONFIG_DIR "/gdb", abs.get ()); + } + + return {}; +} + /* See gdbsupport/pathstuff.h. */ const char * diff --git a/gdbsupport/pathstuff.h b/gdbsupport/pathstuff.h index 4bc0d892119..85241bc8c7c 100644 --- a/gdbsupport/pathstuff.h +++ b/gdbsupport/pathstuff.h @@ -85,6 +85,20 @@ extern std::string get_standard_cache_dir (); extern std::string get_standard_temp_dir (); +/* Get the usual user config directory for the current platform. + + On Linux, it follows the XDG Base Directory specification: use + $XDG_CONFIG_HOME/gdb if the XDG_CONFIG_HOME environment variable is + defined, otherwise $HOME/.config. + + On macOS, it follows the local convention and uses + ~/Library/Preferences/gdb. + + The return value is absolute and tilde-expanded. Return an empty + string if neither XDG_CONFIG_HOME (on Linux) or HOME are defined. */ + +extern std::string get_standard_config_dir (); + /* Return the file name of the user's shell. Normally this comes from the SHELL environment variable. */