/* Path manipulation routines for GDB and gdbserver.
- Copyright (C) 1986-2021 Free Software Foundation, Inc.
+ Copyright (C) 1986-2023 Free Software Foundation, Inc.
This file is part of GDB.
/* See gdbsupport/pathstuff.h. */
+char *current_directory;
+
+/* See gdbsupport/pathstuff.h. */
+
gdb::unique_xmalloc_ptr<char>
gdb_realpath (const char *filename)
{
But the situation is slightly more complex on Windows, due to some
versions of GCC which were reported to generate paths where
- backlashes (the directory separator) were doubled. For instance:
+ backslashes (the directory separator) were doubled. For instance:
c:\\some\\double\\slashes\\dir
... instead of ...
c:\some\double\slashes\dir
/* See gdbsupport/pathstuff.h. */
-gdb::unique_xmalloc_ptr<char>
+std::string
gdb_realpath_keepfile (const char *filename)
{
const char *base_name = lbasename (filename);
char *dir_name;
- char *result;
/* Extract the basename of filename, and return immediately
a copy of filename if it does not contain any directory prefix. */
if (base_name == filename)
- return make_unique_xstrdup (filename);
+ return filename;
dir_name = (char *) alloca ((size_t) (base_name - filename + 2));
/* Allocate enough space to store the dir_name + plus one extra
directory separator, avoid doubling it. */
gdb::unique_xmalloc_ptr<char> path_storage = gdb_realpath (dir_name);
const char *real_path = path_storage.get ();
- if (IS_DIR_SEPARATOR (real_path[strlen (real_path) - 1]))
- result = concat (real_path, base_name, (char *) NULL);
- else
- result = concat (real_path, SLASH_STRING, base_name, (char *) NULL);
-
- return gdb::unique_xmalloc_ptr<char> (result);
+ return path_join (real_path, base_name);
}
/* See gdbsupport/pathstuff.h. */
-gdb::unique_xmalloc_ptr<char>
+std::string
gdb_abspath (const char *path)
{
gdb_assert (path != NULL && path[0] != '\0');
if (path[0] == '~')
- return gdb_tilde_expand_up (path);
+ return gdb_tilde_expand (path);
if (IS_ABSOLUTE_PATH (path) || current_directory == NULL)
- return make_unique_xstrdup (path);
-
- /* Beware the // my son, the Emacs barfs, the botch that catch... */
- return gdb::unique_xmalloc_ptr<char>
- (concat (current_directory,
- IS_DIR_SEPARATOR (current_directory[strlen (current_directory) - 1])
- ? "" : SLASH_STRING,
- path, (char *) NULL));
+ return path;
+
+ return path_join (current_directory, path);
}
/* See gdbsupport/pathstuff.h. */
/* See gdbsupport/pathstuff.h. */
+std::string
+path_join (gdb::array_view<const char *> paths)
+{
+ std::string ret;
+
+ for (int i = 0; i < paths.size (); ++i)
+ {
+ const char *path = paths[i];
+
+ if (i > 0)
+ gdb_assert (strlen (path) == 0 || !IS_ABSOLUTE_PATH (path));
+
+ if (!ret.empty () && !IS_DIR_SEPARATOR (ret.back ()))
+ ret += '/';
+
+ ret.append (path);
+ }
+
+ return ret;
+}
+
+/* See gdbsupport/pathstuff.h. */
+
bool
contains_dir_separator (const char *path)
{
if (xdg_cache_home != NULL && xdg_cache_home[0] != '\0')
{
/* Make sure the path is absolute and tilde-expanded. */
- gdb::unique_xmalloc_ptr<char> abs (gdb_abspath (xdg_cache_home));
- return string_printf ("%s/gdb", abs.get ());
+ std::string abs = gdb_abspath (xdg_cache_home);
+ return path_join (abs.c_str (), "gdb");
}
#endif
if (home != NULL && home[0] != '\0')
{
/* Make sure the path is absolute and tilde-expanded. */
- gdb::unique_xmalloc_ptr<char> abs (gdb_abspath (home));
- return string_printf ("%s/" HOME_CACHE_DIR "/gdb", abs.get ());
+ std::string abs = gdb_abspath (home);
+ return path_join (abs.c_str (), HOME_CACHE_DIR, "gdb");
}
#ifdef WIN32
if (win_home != NULL && win_home[0] != '\0')
{
/* Make sure the path is absolute and tilde-expanded. */
- gdb::unique_xmalloc_ptr<char> abs (gdb_abspath (win_home));
- return string_printf ("%s/gdb", abs.get ());
+ std::string abs = gdb_abspath (win_home);
+ return path_join (abs.c_str (), "gdb");
}
#endif
if (xdg_config_home != NULL && xdg_config_home[0] != '\0')
{
/* Make sure the path is absolute and tilde-expanded. */
- gdb::unique_xmalloc_ptr<char> abs (gdb_abspath (xdg_config_home));
- return string_printf ("%s/gdb", abs.get ());
+ std::string abs = gdb_abspath (xdg_config_home);
+ return path_join (abs.c_str (), "gdb");
}
#endif
if (home != NULL && home[0] != '\0')
{
/* Make sure the path is absolute and tilde-expanded. */
- gdb::unique_xmalloc_ptr<char> abs (gdb_abspath (home));
- return string_printf ("%s/" HOME_CONFIG_DIR "/gdb", abs.get ());
+ std::string abs = gdb_abspath (home);
+ return path_join (abs.c_str (), HOME_CONFIG_DIR, "gdb");
}
return {};
if (homedir != nullptr && homedir[0] != '\0')
{
/* Make sure the path is absolute and tilde-expanded. */
- gdb::unique_xmalloc_ptr<char> abs (gdb_abspath (homedir));
- std::string path = (std::string (abs.get ()) + SLASH_STRING
- + std::string (name));
+ std::string abs = gdb_abspath (homedir);
+ std::string path = string_printf ("%s/%s", abs.c_str (), name);
if (stat (path.c_str (), buf) == 0)
return path;
}