diff --git a/console/fileio/file_browser.c b/console/fileio/file_browser.c index 5cceb74f4c..846206af45 100644 --- a/console/fileio/file_browser.c +++ b/console/fileio/file_browser.c @@ -95,7 +95,7 @@ static bool filebrowser_pop_directory (filebrowser_t * filebrowser) if (filebrowser->directory_stack_size > 0) pop_dir -= 1; - fill_pathname_basedir(previous_dir, filebrowser->directory_path, sizeof(previous_dir)); + fill_pathname_parent_dir(previous_dir, filebrowser->directory_path, sizeof(previous_dir)); strlcpy(directory_path_tmp, filebrowser->directory_path, sizeof(directory_path_tmp)); //test first if previous directory can be accessed diff --git a/file.h b/file.h index ca97148348..2e929b9061 100644 --- a/file.h +++ b/file.h @@ -75,6 +75,10 @@ const char *path_basename(const char *path); // Extracts base directory by mutating path. Keeps trailing '/'. void path_basedir(char *path); +// Extracts parent directory by mutating path. +// Assumes that path is a directory. Keeps trailing '/'. +void path_parent_dir(char *path); + bool path_is_absolute(const char *path); // Path-name operations. @@ -108,6 +112,10 @@ void fill_pathname_base(char *out_path, const char *in_path, size_t size); // If in_path is a path without any slashes (relative current directory), out_path will get path "./". void fill_pathname_basedir(char *out_path, const char *in_path, size_t size); +// Copies parent directory of in_dir into out_dir. +// Assumes in_dir is a directory. Keeps trailing '/'. +void fill_pathname_parent_dir(char *out_dir, const char *in_dir, size_t size); + // Joins basedir of in_refpath together with in_path. // If in_path is an absolute path, out_path = in_path. // E.g.: in_refpath = "/foo/bar/baz.a", in_path = "foobar.cg", out_path = "/foo/bar/foobar.cg". diff --git a/file_path.c b/file_path.c index 50d5a3af59..d2f5401e2b 100644 --- a/file_path.c +++ b/file_path.c @@ -429,6 +429,12 @@ void fill_pathname_basedir(char *out_dir, const char *in_path, size_t size) path_basedir(out_dir); } +void fill_pathname_parent_dir(char *out_dir, const char *in_dir, size_t size) +{ + rarch_assert(strlcpy(out_dir, in_dir, size) < size); + path_parent_dir(out_dir); +} + void path_basedir(char *path) { if (strlen(path) < 2) @@ -450,6 +456,14 @@ void path_basedir(char *path) } } +void path_parent_dir(char *path) +{ + size_t len = strlen(path); + if (len && ((path[len - 1] == '/') || (path[len - 1] == '\\'))) + path[len - 1] = '\0'; + path_basedir(path); +} + const char *path_basename(const char *path) { const char *last = strrchr(path, '/');