chiark / gitweb /
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)
committerSven Eden <yamakuzure@gmx.net>
Fri, 24 Aug 2018 14:47:08 +0000 (16:47 +0200)
src/basic/path-util.c
src/test/test-path-util.c

index 4c5f984bd044826f5c24f576face52674c5a8467..addd39373c40b2a1144e15c1f3fcab4e8fc6707a 100644 (file)
@@ -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;
index 057c06e8c574c2e907d91fd7c7802d4df1dff827..f7e96283fd7975177ed59f2643f0a8abfb0671b4 100644 (file)
@@ -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) {