Skip to contentSkip to conversation

path: Add l_basename function

PATCH Applied Denis Kenzior 1 participant 2 messages May 28, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 path: Add l_basename function

Many ell based projects use the GNU version (obtained using #define _GNU_SOURCE). Unlike the POSIX version, this version does not modify its arguments. However, some libc implementations such as musl, only provide the POSIX implementation. Implement a version of basename in ell.

---
 ell/ell.sym |  1 +
 ell/path.c  | 19 +++++++++++++++++++
 ell/path.h  |  2 ++
 3 files changed, 22 insertions(+)
ell/ell.sym +1 -0
index 28b55d3..803906c 100644
@@ -434,6 +434,7 @@ global:
 	l_netlink_unregister;
 	l_netlink_set_debug;
 	/* path */
+	l_basename;
 	l_path_find;
 	l_path_get_mtime;
 	l_path_next;
ell/path.c +19 -0
index 8004c2e..e5b7bcb 100644
@@ -173,3 +173,22 @@ LIB_EXPORT int l_path_touch(const char *path)
 
 	return -errno;
 }
+
+/**
+ * l_basename:
+ * @path: The path to find the base name of
+ *
+ * This function operates similarly to the glibc version of basename.  This
+ * version never modifies its argument and returns the contents in path after
+ * the last '/' character.  Note that this will result in an empty string being
+ * returned when @path ends in a trailing slash.
+ */
+LIB_EXPORT const char *l_basename(const char *path)
+{
+	const char *p = strrchr(path, '/');
+
+	if (p)
+		return p + 1;
+
+	return path;
+}
ell/path.h +2 -0
index 1f151de..5046dcd 100644
@@ -17,6 +17,8 @@ char *l_path_find(const char *basename, const char *path_str, int mode);
 uint64_t l_path_get_mtime(const char *path);
 int l_path_touch(const char *path);
 
+const char *l_basename(const char *path);
+
 #ifdef __cplusplus
 }
 #endif
Signature · 1 line
2.45.1
author Applied
#1 lore raw
Quoted 11 lines · Denis Kenzior

Many ell based projects use the GNU version (obtained using #define _GNU_SOURCE). Unlike the POSIX version, this version does not modify its arguments. However, some libc implementations such as musl, only provide the POSIX implementation. Implement a version of basename in ell.

---
  ell/ell.sym |  1 +
  ell/path.c  | 19 +++++++++++++++++++
  ell/path.h  |  2 ++
  3 files changed, 22 insertions(+)

Applied.

Regards,
-Denis