diff --git a/file.h b/file.h index 1cbaaaf5ce..ca97148348 100644 --- a/file.h +++ b/file.h @@ -113,6 +113,9 @@ void fill_pathname_basedir(char *out_path, const char *in_path, size_t size); // E.g.: in_refpath = "/foo/bar/baz.a", in_path = "foobar.cg", out_path = "/foo/bar/foobar.cg". void fill_pathname_resolve_relative(char *out_path, const char *in_refpath, const char *in_path, size_t size); +// Joins a directory and path together. Makes sure not to get two consecutive slashes between dir and path. +void fill_pathname_join(char *out_path, const char *dir, const char *path, size_t size); + size_t convert_char_to_wchar(wchar_t *out_wchar, const char *in_char, size_t size); size_t convert_wchar_to_char(char *out_char, const wchar_t *in_wchar, size_t size); diff --git a/file_path.c b/file_path.c index 45c58ed419..50d5a3af59 100644 --- a/file_path.c +++ b/file_path.c @@ -244,7 +244,7 @@ struct string_list *dir_list_new(const char *dir, const char *ext, bool include_ continue; char file_path[PATH_MAX]; - snprintf(file_path, sizeof(file_path), "%s\\%s", dir, name); + fill_pathname_join(file_path, dir, name, sizeof(file_path)); union string_list_elem_attr attr; attr.b = is_dir; @@ -307,7 +307,7 @@ struct string_list *dir_list_new(const char *dir, const char *ext, bool include_ const char *file_ext = path_get_extension(name); char file_path[PATH_MAX]; - snprintf(file_path, sizeof(file_path), "%s/%s", dir, name); + fill_pathname_join(file_path, dir, name, sizeof(file_path)); bool is_dir = dirent_is_directory(file_path, entry); if (!include_dirs && is_dir) @@ -486,6 +486,26 @@ void fill_pathname_resolve_relative(char *out_path, const char *in_refpath, cons } } +void fill_pathname_join(char *out_path, const char *dir, const char *path, size_t size) +{ +#ifdef _WIN32 + const char join_str[] = "\\"; +#else + const char join_str[] = "/"; +#endif + + size_t dir_len; + rarch_assert((dir_len = strlcpy(out_path, dir, size)) < size); + + if (dir_len) + { + if (out_path[dir_len - 1] != '/' && out_path[dir_len - 1] != '\\') + rarch_assert(strlcat(out_path, join_str, sizeof(out_path))); + } + + rarch_assert(strlcat(out_path, path, size) < size); +} + size_t convert_char_to_wchar(wchar_t *out_wchar, const char *in_char, size_t size) { return mbstowcs(out_wchar, in_char, size / sizeof(wchar_t)); diff --git a/screenshot.c b/screenshot.c index be5cca4010..7b41f7acce 100644 --- a/screenshot.c +++ b/screenshot.c @@ -21,6 +21,7 @@ #include #include #include "general.h" +#include "file.h" #ifdef HAVE_CONFIG_H #include "config.h" @@ -234,7 +235,7 @@ bool screenshot_dump(const char *folder, const void *frame, char shotname[PATH_MAX]; screenshot_generate_filename(shotname, sizeof(shotname)); - snprintf(filename, sizeof(filename), "%s/%s", folder, shotname); + fill_pathname_join(filename, folder, shotname, sizeof(filename)); FILE *file = fopen(filename, "wb"); if (!file)