1 /* Perform tilde expansion on paths for GDB and gdbserver.
3 Copyright (C) 2017-2019 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 "common-defs.h"
21 #include "gdb_tilde_expand.h"
24 /* RAII-style class wrapping "glob". */
29 /* Construct a "gdb_glob" object by calling "glob" with the provided
30 parameters. This function can throw if "glob" fails. */
31 gdb_glob (const char *pattern
, int flags
,
32 int (*errfunc
) (const char *epath
, int eerrno
))
34 int ret
= glob (pattern
, flags
, errfunc
, &m_glob
);
38 if (ret
== GLOB_NOMATCH
)
39 error (_("Could not find a match for '%s'."), pattern
);
41 error (_("glob could not process pattern '%s'."),
46 /* Destroy the object and free M_GLOB. */
52 /* Return the GL_PATHC component of M_GLOB. */
55 return m_glob
.gl_pathc
;
58 /* Return the GL_PATHV component of M_GLOB. */
61 return m_glob
.gl_pathv
;
65 /* The actual glob object we're dealing with. */
69 /* See common/gdb_tilde_expand.h. */
72 gdb_tilde_expand (const char *dir
)
74 gdb_glob
glob (dir
, GLOB_TILDE_CHECK
, NULL
);
76 gdb_assert (glob
.pathc () > 0);
77 /* "glob" may return more than one match to the path provided by the
78 user, but we are only interested in the first match. */
79 std::string expanded_dir
= glob
.pathv ()[0];
84 /* See common/gdb_tilde_expand.h. */
86 gdb::unique_xmalloc_ptr
<char>
87 gdb_tilde_expand_up (const char *dir
)
89 gdb_glob
glob (dir
, GLOB_TILDE_CHECK
, NULL
);
91 gdb_assert (glob
.pathc () > 0);
92 /* "glob" may return more than one match to the path provided by the
93 user, but we are only interested in the first match. */
94 return gdb::unique_xmalloc_ptr
<char> (xstrdup (glob
.pathv ()[0]));