aboutsummaryrefslogtreecommitdiff
path: root/gdb/common
diff options
context:
space:
mode:
authorJohn Baldwin <jhb@FreeBSD.org>2019-02-12 13:56:16 -0800
committerJohn Baldwin <jhb@FreeBSD.org>2019-02-12 13:56:16 -0800
commitefac4bfe0b2bdd21a27c94ca3464046e9c23f0e4 (patch)
tree85e22a71d2b6e613641f3351792d38f69d4ca847 /gdb/common
parent402d2bfec425f29c5b54089d5ff98ca9a1b8ec27 (diff)
downloadgdb-efac4bfe0b2bdd21a27c94ca3464046e9c23f0e4.zip
gdb-efac4bfe0b2bdd21a27c94ca3464046e9c23f0e4.tar.gz
gdb-efac4bfe0b2bdd21a27c94ca3464046e9c23f0e4.tar.bz2
Add a new function child_path.
child_path returns a pointer to the first component in a child path that comes after a parent path. This does not depend on trying to stat() the paths since they may describe remote paths but instead relies on filename parsing. The function requires that the child path describe a filename that contains at least one component below the parent path and returns a pointer to the first component. gdb/ChangeLog: * Makefile.in (SUBDIR_UNITTESTS_SRCS): Add unittests/child-path-selftests.c. * common/pathstuff.c (child_path): New function. * common/pathstuff.h (child_path): New prototype. * unittests/child-path-selftests.c: New file.
Diffstat (limited to 'gdb/common')
-rw-r--r--gdb/common/pathstuff.c50
-rw-r--r--gdb/common/pathstuff.h6
2 files changed, 56 insertions, 0 deletions
diff --git a/gdb/common/pathstuff.c b/gdb/common/pathstuff.c
index 1167530..2b1669a 100644
--- a/gdb/common/pathstuff.c
+++ b/gdb/common/pathstuff.c
@@ -147,6 +147,56 @@ gdb_abspath (const char *path)
/* See common/pathstuff.h. */
+const char *
+child_path (const char *parent, const char *child)
+{
+ /* The child path must start with the parent path. */
+ size_t parent_len = strlen (parent);
+ if (filename_ncmp (parent, child, parent_len) != 0)
+ return NULL;
+
+ /* The parent path must be a directory and the child must contain at
+ least one component underneath the parent. */
+ const char *child_component;
+ if (IS_DIR_SEPARATOR (parent[parent_len - 1]))
+ {
+ /* The parent path ends in a directory separator, so it is a
+ directory. The first child component starts after the common
+ prefix. */
+ child_component = child + parent_len;
+ }
+ else
+ {
+ /* The parent path does not end in a directory separator. The
+ first character in the child after the common prefix must be
+ a directory separator.
+
+ Note that CHILD must hold at least parent_len characters for
+ filename_ncmp to return zero. If the character at parent_len
+ is nul due to CHILD containing the same path as PARENT, the
+ IS_DIR_SEPARATOR check will fail here. */
+ if (!IS_DIR_SEPARATOR (child[parent_len]))
+ return NULL;
+
+ /* The first child component starts after the separator after the
+ common prefix. */
+ child_component = child + parent_len + 1;
+ }
+
+ /* The child must contain at least one non-separator character after
+ the parent. */
+ while (*child_component != '\0')
+ {
+ if (!IS_DIR_SEPARATOR (*child_component))
+ return child_component;
+
+ child_component++;
+ }
+ return NULL;
+}
+
+/* See common/pathstuff.h. */
+
bool
contains_dir_separator (const char *path)
{
diff --git a/gdb/common/pathstuff.h b/gdb/common/pathstuff.h
index c264e78..67072a4 100644
--- a/gdb/common/pathstuff.h
+++ b/gdb/common/pathstuff.h
@@ -48,6 +48,12 @@ extern gdb::unique_xmalloc_ptr<char>
extern gdb::unique_xmalloc_ptr<char> gdb_abspath (const char *path);
+/* If the path in CHILD is a child of the path in PARENT, return a
+ pointer to the first component in the CHILD's pathname below the
+ PARENT. Otherwise, return NULL. */
+
+extern const char *child_path (const char *parent, const char *child);
+
/* Return whether PATH contains a directory separator character. */
extern bool contains_dir_separator (const char *path);