ld: Add PR ld/32067 tests
[binutils-gdb.git] / gdbsupport / gdb_tilde_expand.cc
blob4a0a5734ad86d6a989f0de7422c1557158b95f5d
1 /* Perform tilde expansion on paths for GDB and gdbserver.
3 Copyright (C) 2017-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include <algorithm>
21 #include "filenames.h"
22 #include "gdb_tilde_expand.h"
23 #include <glob.h>
25 /* RAII-style class wrapping "glob". */
27 class gdb_glob
29 public:
30 /* Construct a "gdb_glob" object by calling "glob" with the provided
31 parameters. This function can throw if "glob" fails. */
32 gdb_glob (const char *pattern, int flags,
33 int (*errfunc) (const char *epath, int eerrno))
35 int ret = glob (pattern, flags, errfunc, &m_glob);
37 if (ret != 0)
39 if (ret == GLOB_NOMATCH)
40 error (_("Could not find a match for '%s'."), pattern);
41 else
42 error (_("glob could not process pattern '%s'."),
43 pattern);
47 /* Destroy the object and free M_GLOB. */
48 ~gdb_glob ()
50 globfree (&m_glob);
53 /* Return the GL_PATHC component of M_GLOB. */
54 int pathc () const
56 return m_glob.gl_pathc;
59 /* Return the GL_PATHV component of M_GLOB. */
60 char **pathv () const
62 return m_glob.gl_pathv;
65 private:
66 /* The actual glob object we're dealing with. */
67 glob_t m_glob;
70 /* See gdbsupport/gdb_tilde_expand.h. */
72 std::string
73 gdb_tilde_expand (const char *dir)
75 if (dir[0] != '~')
76 return std::string (dir);
78 /* This function uses glob in order to expand the ~. However, this function
79 will fail to expand if the actual dir we are looking for does not exist.
80 Given "~/does/not/exist", glob will fail.
82 In order to avoid such limitation, we only use glob to expand "~" and keep
83 "/does/not/exist" unchanged.
85 Similarly, to expand ~gdb/might/not/exist, we only expand "~gdb" using
86 glob and leave "/might/not/exist" unchanged. */
87 const std::string d (dir);
89 /* Look for the first dir separator (if any) and split d around it. */
90 const auto first_sep
91 = std::find_if (d.cbegin (), d.cend(),
92 [] (const char c) -> bool
93 { return IS_DIR_SEPARATOR (c); });
94 const std::string to_expand (d.cbegin (), first_sep);
95 const std::string remainder (first_sep, d.cend ());
97 const gdb_glob glob (to_expand.c_str (), GLOB_TILDE_CHECK, nullptr);
99 gdb_assert (glob.pathc () == 1);
100 return std::string (glob.pathv ()[0]) + remainder;