diff --git a/src/core/file_sys/fs.cpp b/src/core/file_sys/fs.cpp index c51828965..18884b740 100644 --- a/src/core/file_sys/fs.cpp +++ b/src/core/file_sys/fs.cpp @@ -36,6 +36,20 @@ std::string MntPoints::getHostDirectory(const std::string& guest_directory) { } return ""; } +std::string MntPoints::getHostFile(const std::string& guest_file) { + std::scoped_lock lock{m_mutex}; + + for (auto& pair : m_mnt_pairs) { + //horrible code but it works :D + int find = guest_file.find(pair.guest_path); + if (find == 0) { + std::string npath = guest_file.substr(pair.guest_path.size(), guest_file.size()-1); + std::replace(pair.host_path.begin(), pair.host_path.end(), '\\', '/'); + return pair.host_path + npath; + } + } + return ""; +} int HandleTable::createHandle() { std::scoped_lock lock{m_mutex}; auto* file = new File{}; diff --git a/src/core/file_sys/fs.h b/src/core/file_sys/fs.h index 0de2cd423..7612049cf 100644 --- a/src/core/file_sys/fs.h +++ b/src/core/file_sys/fs.h @@ -20,6 +20,7 @@ class MntPoints { void unmount(const std::string& path); void unmountAll(); std::string getHostDirectory(const std::string& guest_directory); + std::string MntPoints::getHostFile(const std::string& guest_file); private: std::vector m_mnt_pairs; diff --git a/src/core/hle/libraries/libc/libc_stdio.cpp b/src/core/hle/libraries/libc/libc_stdio.cpp index c5d4f2468..ad990bc68 100644 --- a/src/core/hle/libraries/libc/libc_stdio.cpp +++ b/src/core/hle/libraries/libc/libc_stdio.cpp @@ -2,6 +2,8 @@ #include "common/debug.h" #include "common/log.h" +#include "common/singleton.h" +#include "core/file_sys/fs.h" namespace Core::Libraries::LibC { @@ -38,11 +40,23 @@ int PS4_SYSV_ABI vsnprintf(char* s, size_t n, const char* format, VaList* arg) { int PS4_SYSV_ABI puts(const char* s) { return std::puts(s); } FILE* PS4_SYSV_ABI fopen(const char* filename, const char* mode) { - LOG_ERROR_IF(log_file_libc, "Unimplemented fopen filename={} , mode ={}\n", filename, mode); - return nullptr; + LOG_ERROR_IF(log_file_libc, "fopen filename={} , mode ={}\n", filename, mode); + auto* h = Common::Singleton::Instance(); + auto* mnt = Common::Singleton::Instance(); + + u32 handle = h->createHandle(); + auto* file = h->getFile(handle); + file->m_guest_name = filename; + file->m_host_name = mnt->getHostFile(file->m_guest_name); + FILE* f = std::fopen(file->m_host_name.c_str(), mode); + return f; } int PS4_SYSV_ABI fclose(FILE* stream) { - LOG_ERROR_IF(log_file_libc, "Unimplemented fclose\n"); + LOG_ERROR_IF(log_file_libc, "fclose\n"); + if (stream != nullptr) + { + std::fclose(stream); + } return 0; } } // namespace Core::Libraries::LibC