]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
path-util: document a few other special cases for last_path_component()
authorLennart Poettering <lennart@poettering.net>
Thu, 5 Apr 2018 16:00:39 +0000 (18:00 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 12 Apr 2018 09:02:47 +0000 (11:02 +0200)
src/basic/path-util.c
src/test/test-path-util.c

index 4f89de0a7297a7e617200a5eeb7133b391025ad1..5572c219523110aaac6b18a16ab9e803ace173ac 100644 (file)
@@ -715,16 +715,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;
index e2d876e65a23441e65fb913643429814ae665f5d..f1a2df346549f5d483c79333401e0734f7acef9c 100644 (file)
@@ -399,6 +399,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) {