commit 1f9f258c380c9e58a3b914e440d2a35236186787
parent 59e5aed3ea2c36cc3a7713882b12372b4bb12754
Author: Francesco Saccone <francesco@francescosaccone.com>
Date: Tue, 1 Apr 2025 10:39:21 +0200
feat: add get_file_name function
Signed-off-by: Francesco Saccone <francesco@francescosaccone.com>
Diffstat:
2 files changed, 21 insertions(+), 0 deletions(-)
diff --git a/file.c b/file.c
@@ -41,6 +41,24 @@ get_normalised_path(char *path) {
return normalised;
}
+char *
+get_file_name(char *path) {
+ char *last_slash = strrchr(path, '/'),
+ *character_after_last_slash = last_slash + sizeof(char);
+
+ if (last_slash == NULL) {
+ /* If no slash occurs, the file name is the actual path */
+ return path;
+ }
+
+ if (*character_after_last_slash == '\0') {
+ /* If path ends in '/', the file name will be considered empty */
+ return "";
+ }
+
+ return character_after_last_slash;
+}
+
struct file_content
get_file_content(char *path) {
struct file_content result = { NULL, 0 };
diff --git a/file.h b/file.h
@@ -39,6 +39,9 @@ is_file_readable(char *path);
char *
get_normalised_path(char *path);
+char *
+get_file_name(char *path);
+
/*
* It returns the file_content instances of the file at given path.
*