From d1b6e6eb5d0a96ee833d9c2d38041c853d38cd57 Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Tue, 15 Feb 2022 19:06:00 -0600 Subject: [PATCH] Update: Improve f_file_name_directory() and f_file_name_base(). Have both of these functions include a NULL at the end to make compatibility with NULL terminated strings easier. The directory name should not return "." when the directory is the current directory. Replace this with an empty string. --- level_0/f_file/c/file.c | 15 +++++++++++---- level_0/f_file/c/file.h | 2 ++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/level_0/f_file/c/file.c b/level_0/f_file/c/file.c index 1e2f35f..5bc8ff1 100644 --- a/level_0/f_file/c/file.c +++ b/level_0/f_file/c/file.c @@ -1400,13 +1400,14 @@ extern "C" { path_to_name = basename(path_argument); - f_array_length_t size = strnlen(path_to_name, path.used); + const f_array_length_t size = strnlen(path_to_name, path.used); - const f_status_t status = f_string_dynamic_increase_by(size, name_base); + const f_status_t status = f_string_dynamic_increase_by(size + 1, name_base); if (F_status_is_error(status)) return status; memcpy(name_base->string + name_base->used, path_to_name, size); name_base->used += size; + name_base->string[name_base->used] = 0; return F_none; } @@ -1431,15 +1432,21 @@ extern "C" { path_to_name = dirname(path_argument); - f_array_length_t size = strnlen(path_to_name, path.used); + const f_array_length_t size = strnlen(path_to_name, path.used); + + // Do not treat '.' as a directory. + if (size == 1 && f_string_ascii_plus_s.string[0]) { + return F_none; + } { - const f_status_t status = f_string_dynamic_increase_by(size, name_directory); + const f_status_t status = f_string_dynamic_increase_by(size + 1, name_directory); if (F_status_is_error(status)) return status; } memcpy(name_directory->string + name_directory->used, path_to_name, size); name_directory->used += size; + name_directory->string[name_directory->used] = 0; return F_none; } diff --git a/level_0/f_file/c/file.h b/level_0/f_file/c/file.h index 6cba705..61ca775 100644 --- a/level_0/f_file/c/file.h +++ b/level_0/f_file/c/file.h @@ -1235,6 +1235,7 @@ extern "C" { * @param name_base * The resulting base name as per basename(). * The base name is appended onto this. + * This is NULL terminated after the name_directory.used. * * @return * F_none on success. @@ -1263,6 +1264,7 @@ extern "C" { * @param name_directory * The resulting base name as per dirname(). * The directory name is appended onto this. + * This is NULL terminated after the name_directory.used. * * @return * F_none on success. -- 1.8.3.1