18dffb485Schristos /* Path manipulation routines for GDB and gdbserver. 28dffb485Schristos 3*5ba1f45fSchristos Copyright (C) 1986-2024 Free Software Foundation, Inc. 48dffb485Schristos 58dffb485Schristos This file is part of GDB. 68dffb485Schristos 78dffb485Schristos This program is free software; you can redistribute it and/or modify 88dffb485Schristos it under the terms of the GNU General Public License as published by 98dffb485Schristos the Free Software Foundation; either version 3 of the License, or 108dffb485Schristos (at your option) any later version. 118dffb485Schristos 128dffb485Schristos This program is distributed in the hope that it will be useful, 138dffb485Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of 148dffb485Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 158dffb485Schristos GNU General Public License for more details. 168dffb485Schristos 178dffb485Schristos You should have received a copy of the GNU General Public License 188dffb485Schristos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 198dffb485Schristos 208dffb485Schristos #ifndef COMMON_PATHSTUFF_H 218dffb485Schristos #define COMMON_PATHSTUFF_H 228dffb485Schristos 238dffb485Schristos #include "gdbsupport/byte-vector.h" 244b169a6bSchristos #include "gdbsupport/array-view.h" 254b169a6bSchristos 264b169a6bSchristos #include <sys/types.h> 274b169a6bSchristos #include <sys/stat.h> 284b169a6bSchristos #include <unistd.h> 294b169a6bSchristos #include <array> 308dffb485Schristos 318dffb485Schristos /* Path utilities. */ 328dffb485Schristos 338dffb485Schristos /* Return the real path of FILENAME, expanding all the symbolic links. 348dffb485Schristos 358dffb485Schristos Contrary to "gdb_abspath", this function does not use 368dffb485Schristos CURRENT_DIRECTORY for path expansion. Instead, it relies on the 378dffb485Schristos current working directory (CWD) of GDB or gdbserver. */ 388dffb485Schristos 398dffb485Schristos extern gdb::unique_xmalloc_ptr<char> gdb_realpath (const char *filename); 408dffb485Schristos 418dffb485Schristos /* Return a copy of FILENAME, with its directory prefix canonicalized 428dffb485Schristos by gdb_realpath. */ 438dffb485Schristos 444b169a6bSchristos extern std::string gdb_realpath_keepfile (const char *filename); 458dffb485Schristos 468dffb485Schristos /* Return PATH in absolute form, performing tilde-expansion if necessary. 478dffb485Schristos PATH cannot be NULL or the empty string. 488dffb485Schristos This does not resolve symlinks however, use gdb_realpath for that. 498dffb485Schristos 508dffb485Schristos Contrary to "gdb_realpath", this function uses CURRENT_DIRECTORY 518dffb485Schristos for the path expansion. This may lead to scenarios the current 528dffb485Schristos working directory (CWD) is different than CURRENT_DIRECTORY. 538dffb485Schristos 548dffb485Schristos If CURRENT_DIRECTORY is NULL, this function returns a copy of 558dffb485Schristos PATH. */ 568dffb485Schristos 574b169a6bSchristos extern std::string gdb_abspath (const char *path); 588dffb485Schristos 598dffb485Schristos /* If the path in CHILD is a child of the path in PARENT, return a 608dffb485Schristos pointer to the first component in the CHILD's pathname below the 618dffb485Schristos PARENT. Otherwise, return NULL. */ 628dffb485Schristos 638dffb485Schristos extern const char *child_path (const char *parent, const char *child); 648dffb485Schristos 654b169a6bSchristos /* Join elements in PATHS into a single path. 664b169a6bSchristos 674b169a6bSchristos The first element can be absolute or relative. All the others must be 684b169a6bSchristos relative. */ 694b169a6bSchristos 704b169a6bSchristos extern std::string path_join (gdb::array_view<const char *> paths); 714b169a6bSchristos 724b169a6bSchristos /* Same as the above, but accept paths as distinct parameters. */ 734b169a6bSchristos 744b169a6bSchristos template<typename ...Args> 754b169a6bSchristos std::string 764b169a6bSchristos path_join (Args... paths) 774b169a6bSchristos { 784b169a6bSchristos /* It doesn't make sense to join less than two paths. */ 79*5ba1f45fSchristos static_assert (sizeof... (Args) >= 2); 804b169a6bSchristos 814b169a6bSchristos std::array<const char *, sizeof... (Args)> path_array 824b169a6bSchristos { paths... }; 834b169a6bSchristos 844b169a6bSchristos return path_join (gdb::array_view<const char *> (path_array)); 854b169a6bSchristos } 864b169a6bSchristos 878dffb485Schristos /* Return whether PATH contains a directory separator character. */ 888dffb485Schristos 898dffb485Schristos extern bool contains_dir_separator (const char *path); 908dffb485Schristos 918dffb485Schristos /* Get the usual user cache directory for the current platform. 928dffb485Schristos 938dffb485Schristos On Linux, it follows the XDG Base Directory specification: use 948dffb485Schristos $XDG_CACHE_HOME/gdb if the XDG_CACHE_HOME environment variable is 958dffb485Schristos defined, otherwise $HOME/.cache. 968dffb485Schristos 978dffb485Schristos On macOS, it follows the local convention and uses 988dffb485Schristos ~/Library/Caches/gdb. 998dffb485Schristos 1008dffb485Schristos The return value is absolute and tilde-expanded. Return an empty 1018dffb485Schristos string if neither XDG_CACHE_HOME (on Linux) or HOME are defined. */ 1028dffb485Schristos 1038dffb485Schristos extern std::string get_standard_cache_dir (); 1048dffb485Schristos 1058dffb485Schristos /* Get the usual temporary directory for the current platform. 1068dffb485Schristos 1078dffb485Schristos On Windows, this is the TMP or TEMP environment variable. 1088dffb485Schristos 1098dffb485Schristos On the rest, this is the TMPDIR environment variable, if defined, else /tmp. 1108dffb485Schristos 1118dffb485Schristos Throw an exception on error. */ 1128dffb485Schristos 1138dffb485Schristos extern std::string get_standard_temp_dir (); 1148dffb485Schristos 1154b169a6bSchristos /* Get the usual user config directory for the current platform. 1164b169a6bSchristos 1174b169a6bSchristos On Linux, it follows the XDG Base Directory specification: use 1184b169a6bSchristos $XDG_CONFIG_HOME/gdb if the XDG_CONFIG_HOME environment variable is 1194b169a6bSchristos defined, otherwise $HOME/.config. 1204b169a6bSchristos 1214b169a6bSchristos On macOS, it follows the local convention and uses 1224b169a6bSchristos ~/Library/Preferences/gdb. 1234b169a6bSchristos 1244b169a6bSchristos The return value is absolute and tilde-expanded. Return an empty 1254b169a6bSchristos string if neither XDG_CONFIG_HOME (on Linux) or HOME are defined. */ 1264b169a6bSchristos 1274b169a6bSchristos extern std::string get_standard_config_dir (); 1284b169a6bSchristos 1294b169a6bSchristos /* Look for FILENAME in the standard configuration directory as returned by 1304b169a6bSchristos GET_STANDARD_CONFIG_DIR and return the path to the file. No check is 1314b169a6bSchristos performed that the file actually exists or not. 1324b169a6bSchristos 1334b169a6bSchristos If FILENAME begins with a '.' then the path returned will remove the 1344b169a6bSchristos leading '.' character, for example passing '.gdbinit' could return the 1354b169a6bSchristos path '/home/username/.config/gdb/gdbinit'. */ 1364b169a6bSchristos 1374b169a6bSchristos extern std::string get_standard_config_filename (const char *filename); 1384b169a6bSchristos 1394b169a6bSchristos /* Look for a file called NAME in either the standard config directory or 1404b169a6bSchristos in the users home directory. If a suitable file is found then *BUF will 1414b169a6bSchristos be filled with the contents of a call to 'stat' on the found file, 1424b169a6bSchristos otherwise *BUF is undefined after this call. 1434b169a6bSchristos 1444b169a6bSchristos If NAME starts with a '.' character then, when looking in the standard 1454b169a6bSchristos config directory the file searched for has the '.' removed. For 1464b169a6bSchristos example, if NAME is '.gdbinit' then on a Linux target GDB might look for 1474b169a6bSchristos '~/.config/gdb/gdbinit' and then '~/.gdbinit'. */ 1484b169a6bSchristos 1494b169a6bSchristos extern std::string find_gdb_home_config_file (const char *name, 1504b169a6bSchristos struct stat *buf); 1514b169a6bSchristos 1528dffb485Schristos /* Return the file name of the user's shell. Normally this comes from 1538dffb485Schristos the SHELL environment variable. */ 1548dffb485Schristos 1558dffb485Schristos extern const char *get_shell (); 1568dffb485Schristos 1578dffb485Schristos /* Make a filename suitable to pass to mkstemp based on F (e.g. 1588dffb485Schristos /tmp/foo -> /tmp/foo-XXXXXX). */ 1598dffb485Schristos 1608dffb485Schristos extern gdb::char_vector make_temp_filename (const std::string &f); 1618dffb485Schristos 1624b169a6bSchristos /* String containing the current directory (what getwd would return). */ 1634b169a6bSchristos extern char *current_directory; 1644b169a6bSchristos 1658dffb485Schristos #endif /* COMMON_PATHSTUFF_H */ 166