summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/basic/path-util.c15
-rw-r--r--src/test/test-path-util.c4
2 files changed, 15 insertions, 4 deletions
diff --git a/src/basic/path-util.c b/src/basic/path-util.c
index 4c5f984bd..addd39373 100644
--- a/src/basic/path-util.c
+++ b/src/basic/path-util.c
@@ -723,16 +723,23 @@ char* dirname_malloc(const char *path) {
}
const char *last_path_component(const char *path) {
- /* Finds the last component of the path, preserving the
- * optional trailing slash that signifies a directory.
+
+ /* Finds the last component of the path, preserving the optional trailing slash that signifies a directory.
+ *
* a/b/c → c
* a/b/c/ → c/
+ * x → x
+ * x/ → x/
+ * /y → y
+ * /y/ → y/
* / → /
* // → /
* /foo/a → a
* /foo/a/ → a/
- * This is different than basename, which returns "" when
- * a trailing slash is present.
+ *
+ * Also, the empty string is mapped to itself.
+ *
+ * This is different than basename(), which returns "" when a trailing slash is present.
*/
unsigned l, k;
diff --git a/src/test/test-path-util.c b/src/test/test-path-util.c
index 057c06e8c..f7e96283f 100644
--- a/src/test/test-path-util.c
+++ b/src/test/test-path-util.c
@@ -401,6 +401,10 @@ static void test_last_path_component(void) {
assert_se(streq(last_path_component("/foo/a"), "a"));
assert_se(streq(last_path_component("/foo/a/"), "a/"));
assert_se(streq(last_path_component(""), ""));
+ assert_se(streq(last_path_component("a"), "a"));
+ assert_se(streq(last_path_component("a/"), "a/"));
+ assert_se(streq(last_path_component("/a"), "a"));
+ assert_se(streq(last_path_component("/a/"), "a/"));
}
static void test_filename_is_valid(void) {