diff --git a/libretro-common/file/file_path.c b/libretro-common/file/file_path.c index a12be47ab2..926e837ad4 100644 --- a/libretro-common/file/file_path.c +++ b/libretro-common/file/file_path.c @@ -407,15 +407,29 @@ bool fill_pathname_parent_dir_name(char *out_dir, last = find_last_slash(temp); } + /* Cut the last part of the string (the filename) after the slash, + leaving the directory name (or nested directory names) only. */ if (last) *last = '\0'; + /* Point in_dir to the address of the last slash. */ in_dir = find_last_slash(temp); + /* If find_last_slash returns NULL, it means there was no slash in temp, + so use temp as-is. */ + if (!in_dir) + in_dir = temp; + success = in_dir && in_dir[1]; if (success) - strlcpy(out_dir, in_dir + 1, size); + { + /* If path starts with an slash, eliminate it. */ + if (path_is_absolute(in_dir)) + strlcpy(out_dir, in_dir + 1, size); + else + strlcpy(out_dir, in_dir, size); + } free(temp); return success;