mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
feat(libs): implement homebrew libc and kernel runtime exports
This commit is contained in:
@@ -40,7 +40,21 @@ target_include_directories(libc PRIVATE
|
||||
${LIBS_INCLUDE_DIR}
|
||||
)
|
||||
|
||||
if(WIN32)
|
||||
target_link_libraries(libc PRIVATE advapi32)
|
||||
endif()
|
||||
|
||||
if(BUILD_TESTING)
|
||||
add_test_executable(guest_access_tests ${LIBS_INCLUDE_DIR}/tests/GuestAccess.cpp)
|
||||
target_include_directories(guest_access_tests PRIVATE ${LIBS_INCLUDE_DIR})
|
||||
target_link_libraries(guest_access_tests PRIVATE libc)
|
||||
configure_windows_unwind(guest_access_tests)
|
||||
add_test(NAME guest_access COMMAND guest_access_tests)
|
||||
if(WIN32)
|
||||
target_link_libraries(guest_access_tests PRIVATE advapi32)
|
||||
set_tests_properties(guest_access PROPERTIES ENVIRONMENT_MODIFICATION
|
||||
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
|
||||
endif()
|
||||
add_test_executable(file_position_tests ${LIBS_INCLUDE_DIR}/tests/FilePosition.cpp)
|
||||
target_include_directories(file_position_tests PRIVATE ${LIBS_INCLUDE_DIR})
|
||||
target_link_libraries(file_position_tests PRIVATE libc)
|
||||
|
||||
@@ -16,6 +16,29 @@ extern "C" {
|
||||
|
||||
[[noreturn]] void APS5_VABI _ZSt11_Xbad_allocv_nid_postfix();
|
||||
|
||||
FileStream* APS5_VABI fdopen_nid_postfix(int descriptor, const char* mode) {
|
||||
if (descriptor < 0) { errno = 9; return nullptr; }
|
||||
if (!mode) { errno = 22; return nullptr; }
|
||||
const char* supported[] = {"r", "w", "a", "rb", "wb", "ab", "r+", "w+", "a+",
|
||||
"rb+", "wb+", "ab+", "r+b", "w+b", "a+b"};
|
||||
bool valid = false;
|
||||
for (const auto* candidate : supported) if (std::strcmp(mode, candidate) == 0) valid = true;
|
||||
if (!valid) { errno = 22; return nullptr; }
|
||||
try {
|
||||
const auto release = [](void* pointer) { ::operator delete(pointer); };
|
||||
std::unique_ptr<void, decltype(release)> storage(::operator new(sizeof(FileStream)), release);
|
||||
#ifdef _WIN32
|
||||
auto* native = ::_fdopen(descriptor, mode);
|
||||
#else
|
||||
auto* native = ::fdopen(descriptor, mode);
|
||||
#endif
|
||||
if (!native) return nullptr;
|
||||
auto* stream = new (storage.get()) FileStream(native, true);
|
||||
storage.release();
|
||||
return stream;
|
||||
} catch (const std::bad_alloc&) { errno = 12; return nullptr; }
|
||||
}
|
||||
|
||||
FileStream* APS5_VABI freopen_nid_postfix(const char* filename, const char* mode, FileStream* stream) {
|
||||
if (!stream || !mode) { errno = 22; return nullptr; }
|
||||
if (!filename) { errno = 45; return nullptr; } // Mode-only reopening is not supported.
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
#include "prx/libc/include/General.hpp"
|
||||
#include <cerrno>
|
||||
#include <vector>
|
||||
#ifdef _WIN32
|
||||
#define NOMINMAX
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
namespace {
|
||||
@@ -26,6 +29,65 @@ int FilesystemError(const std::error_code& error) {
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" int APS5_VABI access_nid_postfix(const char* path, int mode) {
|
||||
if (!path) { errno = 14; return -1; }
|
||||
if (mode < 0 || (mode & ~7)) { errno = 22; return -1; }
|
||||
if (!*path) { errno = 2; return -1; }
|
||||
try {
|
||||
const auto resolved = ResolvePath_nid_no_patch(path);
|
||||
#ifdef _WIN32
|
||||
const DWORD attributes = GetFileAttributesW(resolved.c_str());
|
||||
if (attributes == INVALID_FILE_ATTRIBUTES) {
|
||||
errno = FilesystemError(std::error_code(static_cast<int>(GetLastError()), std::system_category()));
|
||||
return -1;
|
||||
}
|
||||
if (mode == 0) return 0;
|
||||
if ((mode & 2) && !(attributes & FILE_ATTRIBUTE_DIRECTORY) && (attributes & FILE_ATTRIBUTE_READONLY)) {
|
||||
errno = 13; return -1;
|
||||
}
|
||||
DWORD size = 0;
|
||||
constexpr auto information = OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION;
|
||||
GetFileSecurityW(resolved.c_str(), information, nullptr, 0, &size);
|
||||
if (!size) { errno = 13; return -1; }
|
||||
std::vector<unsigned char> descriptor(size);
|
||||
if (!GetFileSecurityW(resolved.c_str(), information, descriptor.data(), size, &size)) {
|
||||
errno = 13; return -1;
|
||||
}
|
||||
struct Token {
|
||||
HANDLE value = nullptr;
|
||||
~Token() { if (value) CloseHandle(value); }
|
||||
} source, impersonation;
|
||||
if (!OpenThreadToken(GetCurrentThread(), TOKEN_QUERY | TOKEN_DUPLICATE, TRUE, &source.value)) {
|
||||
if (GetLastError() != ERROR_NO_TOKEN ||
|
||||
!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY | TOKEN_DUPLICATE, &source.value)) {
|
||||
errno = 13; return -1;
|
||||
}
|
||||
}
|
||||
if (!DuplicateToken(source.value, SecurityImpersonation, &impersonation.value)) { errno = 13; return -1; }
|
||||
DWORD desired = 0;
|
||||
if (mode & 4) desired |= FILE_READ_DATA;
|
||||
if (mode & 2) desired |= FILE_WRITE_DATA;
|
||||
if (mode & 1) desired |= FILE_EXECUTE;
|
||||
GENERIC_MAPPING mapping{FILE_GENERIC_READ, FILE_GENERIC_WRITE, FILE_GENERIC_EXECUTE, FILE_ALL_ACCESS};
|
||||
PRIVILEGE_SET privileges{};
|
||||
DWORD privilegeSize = sizeof(privileges), granted = 0;
|
||||
BOOL allowed = FALSE;
|
||||
if (!AccessCheck(descriptor.data(), impersonation.value, desired, &mapping,
|
||||
&privileges, &privilegeSize, &granted, &allowed) || !allowed) {
|
||||
errno = 13; return -1;
|
||||
}
|
||||
return 0;
|
||||
#else
|
||||
if (::access(resolved.c_str(), mode) == 0) return 0;
|
||||
errno = FilesystemError(std::error_code(errno, std::generic_category()));
|
||||
return -1;
|
||||
#endif
|
||||
} catch (const std::bad_alloc&) { errno = 12; return -1; }
|
||||
catch (const std::filesystem::filesystem_error& error) {
|
||||
errno = FilesystemError(error.code()); return -1;
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" int APS5_VABI rename_nid_postfix(const char* from, const char* to) {
|
||||
if (!from || !to) { errno = 14; return -1; }
|
||||
if (!*from || !*to) { errno = 2; return -1; }
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include <mutex>
|
||||
#include <cstdint>
|
||||
#include <cmath>
|
||||
#include <cstdlib>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <stdexcept>
|
||||
@@ -9,6 +10,10 @@
|
||||
|
||||
extern "C" {
|
||||
|
||||
std::lldiv_t APS5_VABI lldiv_nid_postfix(long long numerator, long long denominator) {
|
||||
return std::lldiv(numerator, denominator);
|
||||
}
|
||||
|
||||
float APS5_VABI fmodf_nid_postfix(float x, float y) { return std::fmod(x, y); }
|
||||
float APS5_VABI asinf_nid_postfix(float x) { return std::asin(x); }
|
||||
float APS5_VABI acosf_nid_postfix(float x) { return std::acos(x); }
|
||||
|
||||
@@ -13,6 +13,8 @@ int APS5_VABI ___mb_cur_max_nid_postfix() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int APS5_VABI _Getmbcurmax_nid_postfix() { return ___mb_cur_max_nid_postfix(); }
|
||||
|
||||
int APS5_VABI mbsinit_nid_postfix(const void*) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -80,4 +80,7 @@ int APS5_VABI setvbuf_nid_postfix(FileStream* stream, char* buffer, int mode, st
|
||||
const int native = mode == 0 ? _IOFBF : mode == 1 ? _IOLBF : _IONBF;
|
||||
return std::setvbuf(GetNativeStream(stream), buffer, native, size);
|
||||
}
|
||||
void APS5_VABI setbuf_nid_postfix(FileStream* stream, char* buffer) {
|
||||
setvbuf_nid_postfix(stream, buffer, buffer ? 0 : 2, buffer ? 1024 : 0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#include <io.h>
|
||||
#include <fcntl.h>
|
||||
#include <direct.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/utime.h>
|
||||
@@ -182,6 +183,21 @@ static int PosixResult(int result) {
|
||||
return result < 0 ? PosixFailure(result & 0xffff) : result;
|
||||
}
|
||||
|
||||
extern "C" int APS5_VABI pipe_nid_postfix(int* descriptors) {
|
||||
if (!descriptors) return PosixFailure(GUEST_EFAULT);
|
||||
const GuestArena::HostWrite destination(descriptors, 2 * sizeof(int));
|
||||
if (!destination.Open()) return PosixFailure(GUEST_EFAULT);
|
||||
int native[2];
|
||||
#ifdef _WIN32
|
||||
const int result = ::_pipe(native, 4096, _O_BINARY);
|
||||
#else
|
||||
const int result = ::pipe(native);
|
||||
#endif
|
||||
if (result != 0) return PosixFailure(SceErrorFromErrno(errno) & 0xffff);
|
||||
std::memcpy(descriptors, native, sizeof(native));
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int PathError(const char* path) {
|
||||
if (path == nullptr) return GUEST_EFAULT;
|
||||
return *path == '\0' ? GUEST_ENOENT : 0;
|
||||
|
||||
@@ -12,6 +12,16 @@
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
extern "C" int APS5_VABI sceKernelDebugOutText(int channel, const char* text) {
|
||||
if (channel < 0) return static_cast<int>(0x80020016u);
|
||||
if (!text) return static_cast<int>(0x8002000eu);
|
||||
static std::mutex outputMutex;
|
||||
const std::lock_guard lock(outputMutex);
|
||||
if (std::fprintf(stderr, "[debug:%d] %s", channel, text) < 0 || std::fflush(stderr) != 0)
|
||||
return static_cast<int>(0x80020005u);
|
||||
return 0;
|
||||
}
|
||||
|
||||
extern "C" int APS5_VABI sceKernelSendNotificationRequest(int device, const void* request, std::size_t size, int flags) {
|
||||
constexpr auto invalidArgument = static_cast<int>(0x80020016u);
|
||||
constexpr auto badAddress = static_cast<int>(0x8002000eu);
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
#include "prx/libc/include/General.hpp"
|
||||
#include <chrono>
|
||||
#include <cstdlib>
|
||||
#include <fstream>
|
||||
#ifdef _WIN32
|
||||
#define NOMINMAX
|
||||
#include <windows.h>
|
||||
#include <aclapi.h>
|
||||
#endif
|
||||
|
||||
extern "C" {
|
||||
int APS5_VABI access_nid_postfix(const char*, int);
|
||||
int APS5_VABI chdir_nid_postfix(const char*);
|
||||
int* APS5_VABI __error_nid_postfix();
|
||||
}
|
||||
|
||||
static void Require(bool value) { if (!value) std::abort(); }
|
||||
|
||||
int main() {
|
||||
const auto name = "anyps5-access-test-" + std::to_string(std::chrono::steady_clock::now().time_since_epoch().count());
|
||||
const auto directory = std::filesystem::current_path() / name;
|
||||
Require(std::filesystem::create_directory(directory));
|
||||
const auto file = directory / "sample.txt";
|
||||
{ std::ofstream output(file); output << "sample"; }
|
||||
Require(chdir_nid_postfix(name.c_str()) == 0);
|
||||
*__error_nid_postfix() = 34;
|
||||
Require(access_nid_postfix("sample.txt", 0) == 0 && *__error_nid_postfix() == 34);
|
||||
Require(access_nid_postfix("sample.txt", 6) == 0);
|
||||
Require(access_nid_postfix(".", 7) == 0);
|
||||
Require(access_nid_postfix(("/" + name + "/sample.txt").c_str(), 4) == 0);
|
||||
AddPathAlias_nid_no_patch("access-alias", directory.string().c_str());
|
||||
Require(access_nid_postfix("/access-alias/sample.txt", 0) == 0);
|
||||
RemovePathAlias_nid_no_patch("access-alias");
|
||||
Require(access_nid_postfix("missing", 0) == -1 && *__error_nid_postfix() == 2);
|
||||
Require(access_nid_postfix("", 0) == -1 && *__error_nid_postfix() == 2);
|
||||
Require(access_nid_postfix(nullptr, 0) == -1 && *__error_nid_postfix() == 14);
|
||||
Require(access_nid_postfix("sample.txt", 8) == -1 && *__error_nid_postfix() == 22);
|
||||
Require(access_nid_postfix("sample.txt", -1) == -1 && *__error_nid_postfix() == 22);
|
||||
#ifdef _WIN32
|
||||
Require(SetFileAttributesW(file.c_str(), FILE_ATTRIBUTE_READONLY));
|
||||
Require(access_nid_postfix("sample.txt", 4) == 0);
|
||||
Require(access_nid_postfix("sample.txt", 2) == -1 && *__error_nid_postfix() == 13);
|
||||
Require(SetFileAttributesW(file.c_str(), FILE_ATTRIBUTE_NORMAL));
|
||||
PSECURITY_DESCRIPTOR original = nullptr;
|
||||
PACL originalAcl = nullptr;
|
||||
Require(GetNamedSecurityInfoW(const_cast<wchar_t*>(file.c_str()), SE_FILE_OBJECT,
|
||||
DACL_SECURITY_INFORMATION, nullptr, nullptr, &originalAcl, nullptr, &original) == ERROR_SUCCESS);
|
||||
char sid[SECURITY_MAX_SID_SIZE];
|
||||
DWORD sidBytes = sizeof(sid);
|
||||
Require(CreateWellKnownSid(WinWorldSid, nullptr, sid, &sidBytes));
|
||||
EXPLICIT_ACCESSW denial{};
|
||||
denial.grfAccessPermissions = FILE_EXECUTE;
|
||||
denial.grfAccessMode = DENY_ACCESS;
|
||||
denial.Trustee.TrusteeForm = TRUSTEE_IS_SID;
|
||||
denial.Trustee.ptstrName = reinterpret_cast<wchar_t*>(sid);
|
||||
PACL deniedAcl = nullptr;
|
||||
Require(SetEntriesInAclW(1, &denial, originalAcl, &deniedAcl) == ERROR_SUCCESS);
|
||||
Require(SetNamedSecurityInfoW(const_cast<wchar_t*>(file.c_str()), SE_FILE_OBJECT,
|
||||
DACL_SECURITY_INFORMATION, nullptr, nullptr, deniedAcl, nullptr) == ERROR_SUCCESS);
|
||||
const int denied = access_nid_postfix("sample.txt", 1);
|
||||
const int deniedError = *__error_nid_postfix();
|
||||
const int readable = access_nid_postfix("sample.txt", 4);
|
||||
Require(SetNamedSecurityInfoW(const_cast<wchar_t*>(file.c_str()), SE_FILE_OBJECT,
|
||||
DACL_SECURITY_INFORMATION, nullptr, nullptr, originalAcl, nullptr) == ERROR_SUCCESS);
|
||||
LocalFree(deniedAcl);
|
||||
LocalFree(original);
|
||||
Require(denied == -1 && deniedError == 13 && readable == 0);
|
||||
#else
|
||||
std::filesystem::permissions(file, std::filesystem::perms::owner_read | std::filesystem::perms::owner_write);
|
||||
Require(access_nid_postfix("sample.txt", 1) == -1 && *__error_nid_postfix() == 13);
|
||||
std::filesystem::permissions(file, std::filesystem::perms::owner_exec, std::filesystem::perm_options::add);
|
||||
Require(access_nid_postfix("sample.txt", 1) == 0);
|
||||
#endif
|
||||
Require(chdir_nid_postfix("/") == 0);
|
||||
std::filesystem::remove(file);
|
||||
std::filesystem::remove(directory);
|
||||
}
|
||||
@@ -5,6 +5,12 @@
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#ifdef _WIN32
|
||||
#include <io.h>
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
extern "C" {
|
||||
int APS5_VABI remove_nid_postfix(const char*);
|
||||
int APS5_VABI rename_nid_postfix(const char*, const char*);
|
||||
@@ -28,6 +34,10 @@ int APS5_VABI sceKernelStat(const char*, FileStat*);
|
||||
int APS5_VABI sceKernelUnlink(const char*);
|
||||
int APS5_VABI sceKernelRmdir(const char*);
|
||||
int* APS5_VABI __error_nid_postfix();
|
||||
int APS5_VABI pipe_nid_postfix(int*);
|
||||
std::int64_t APS5_VABI read_nid_postfix(int, void*, std::size_t);
|
||||
std::int64_t APS5_VABI write_nid_postfix(int, const void*, std::size_t);
|
||||
int APS5_VABI sceKernelDebugOutText(int, const char*);
|
||||
}
|
||||
static void Check(bool value, int line) {
|
||||
if (!value) {
|
||||
@@ -37,6 +47,40 @@ static void Check(bool value, int line) {
|
||||
}
|
||||
#define Require(value) Check((value), __LINE__)
|
||||
int main() {
|
||||
Require(sceKernelDebugOutText(-1, "text") == static_cast<int>(0x80020016u));
|
||||
Require(sceKernelDebugOutText(0, nullptr) == static_cast<int>(0x8002000eu));
|
||||
auto* captured = std::tmpfile();
|
||||
Require(captured != nullptr);
|
||||
#ifdef _WIN32
|
||||
const int saved = ::_dup(::_fileno(stderr));
|
||||
Require(saved >= 0 && ::_dup2(::_fileno(captured), ::_fileno(stderr)) == 0);
|
||||
#else
|
||||
const int saved = ::dup(::fileno(stderr));
|
||||
Require(saved >= 0 && ::dup2(::fileno(captured), ::fileno(stderr)) == ::fileno(stderr));
|
||||
#endif
|
||||
const int debugResult = sceKernelDebugOutText(3, "%s%d literal\n");
|
||||
#ifdef _WIN32
|
||||
Require(::_dup2(saved, ::_fileno(stderr)) == 0 && ::_close(saved) == 0);
|
||||
#else
|
||||
Require(::dup2(saved, ::fileno(stderr)) == ::fileno(stderr) && ::close(saved) == 0);
|
||||
#endif
|
||||
Require(debugResult == 0);
|
||||
std::rewind(captured);
|
||||
char debugText[64]{};
|
||||
Require(std::fread(debugText, 1, sizeof(debugText), captured) == 23);
|
||||
Require(std::strcmp(debugText, "[debug:3] %s%d literal\n") == 0);
|
||||
Require(std::fclose(captured) == 0);
|
||||
Require(pipe_nid_postfix(nullptr) == -1 && *__error_nid_postfix() == 14);
|
||||
int descriptors[2] = {-1, -1};
|
||||
Require(pipe_nid_postfix(descriptors) == 0 && descriptors[0] >= 0 && descriptors[1] >= 0);
|
||||
const char payload[] = {'A', '\0', '\r', '\n', '\x1a', 'Z'};
|
||||
Require(write_nid_postfix(descriptors[1], payload, sizeof(payload)) == sizeof(payload));
|
||||
Require(close_nid_postfix(descriptors[1]) == 0);
|
||||
char received[sizeof(payload)]{};
|
||||
Require(read_nid_postfix(descriptors[0], received, sizeof(received)) == sizeof(received));
|
||||
Require(std::memcmp(received, payload, sizeof(payload)) == 0);
|
||||
Require(read_nid_postfix(descriptors[0], received, sizeof(received)) == 0);
|
||||
Require(close_nid_postfix(descriptors[0]) == 0);
|
||||
const auto root = std::filesystem::path("anyps5-filesystem-test-" +
|
||||
std::to_string(std::chrono::steady_clock::now().time_since_epoch().count()));
|
||||
Require(std::filesystem::create_directory(root));
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <limits>
|
||||
#include <initializer_list>
|
||||
extern "C" {
|
||||
double APS5_VABI atof_nid_postfix(const char*);
|
||||
float APS5_VABI strtof_nid_postfix(const char*, char**);
|
||||
@@ -38,10 +39,18 @@ int APS5_VABI __isfinitef_nid_postfix(float);
|
||||
int APS5_VABI __isnormal_nid_postfix(double);
|
||||
int APS5_VABI __isnormalf_nid_postfix(float);
|
||||
int APS5_VABI __isinff_nid_postfix(float);
|
||||
std::lldiv_t APS5_VABI lldiv_nid_postfix(long long, long long);
|
||||
}
|
||||
static void Require(bool value) { if (!value) std::abort(); }
|
||||
|
||||
static void CheckIntegerConversions() {
|
||||
for (const long long numerator : {4294967301LL, -4294967301LL}) {
|
||||
for (const long long denominator : {3LL, -3LL}) {
|
||||
const auto result = lldiv_nid_postfix(numerator, denominator);
|
||||
Require(result.quot == numerator / denominator && result.rem == numerator % denominator);
|
||||
Require(result.quot * denominator + result.rem == numerator);
|
||||
}
|
||||
}
|
||||
struct SignedCase {
|
||||
const char* text;
|
||||
int base;
|
||||
|
||||
@@ -7,6 +7,9 @@
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
#ifndef _WIN32
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
extern "C" {
|
||||
FileStream* APS5_VABI freopen_nid_postfix(const char*, const char*, FileStream*);
|
||||
int APS5_VABI fseeko_nid_postfix(FileStream*, std::int64_t, int);
|
||||
@@ -34,6 +37,11 @@ int APS5_VABI feof_nid_postfix(FileStream*);
|
||||
int APS5_VABI fileno_nid_postfix(FileStream*);
|
||||
void APS5_VABI clearerr_nid_postfix(FileStream*);
|
||||
int APS5_VABI setvbuf_nid_postfix(FileStream*, char*, int, std::size_t);
|
||||
void APS5_VABI setbuf_nid_postfix(FileStream*, char*);
|
||||
FileStream* APS5_VABI fdopen_nid_postfix(int, const char*);
|
||||
int APS5_VABI fclose_nid_postfix(FileStream*);
|
||||
int APS5_VABI _Getmbcurmax_nid_postfix();
|
||||
int APS5_VABI ___mb_cur_max_nid_postfix();
|
||||
}
|
||||
static void Require(bool value) { if (!value) std::abort(); }
|
||||
static int APS5_VABI WriteFormatted(FileStream* stream, const char* format, ...) {
|
||||
@@ -69,6 +77,33 @@ static int APS5_VABI FormatString(char* buffer, const char* format, ...) {
|
||||
return result;
|
||||
}
|
||||
int main() {
|
||||
Require(_Getmbcurmax_nid_postfix() == 1 && _Getmbcurmax_nid_postfix() == ___mb_cur_max_nid_postfix());
|
||||
Require(fdopen_nid_postfix(-1, "rb") == nullptr && *__error_nid_postfix() == 9);
|
||||
Require(fdopen_nid_postfix(0, nullptr) == nullptr && *__error_nid_postfix() == 22);
|
||||
Require(fdopen_nid_postfix(0, "invalid") == nullptr && *__error_nid_postfix() == 22);
|
||||
std::FILE* original = std::tmpfile();
|
||||
Require(original != nullptr);
|
||||
std::fputs("retained", original);
|
||||
std::fflush(original);
|
||||
#ifdef _WIN32
|
||||
const int descriptor = _dup(_fileno(original));
|
||||
#else
|
||||
const int descriptor = ::dup(::fileno(original));
|
||||
#endif
|
||||
Require(descriptor >= 0);
|
||||
auto* wrapped = fdopen_nid_postfix(descriptor, "r+b");
|
||||
Require(wrapped != nullptr && fileno_nid_postfix(wrapped) == descriptor);
|
||||
setbuf_nid_postfix(wrapped, nullptr);
|
||||
Require(fseek_nid_postfix(wrapped, 0, SEEK_SET) == 0);
|
||||
char contents[32]{};
|
||||
Require(fgets_nid_postfix(contents, sizeof(contents), wrapped) == contents && std::strcmp(contents, "retained") == 0);
|
||||
Require(fclose_nid_postfix(wrapped) == 0);
|
||||
#ifdef _WIN32
|
||||
Require(_close(descriptor) == -1);
|
||||
#else
|
||||
Require(::close(descriptor) == -1);
|
||||
#endif
|
||||
Require(std::fclose(original) == 0);
|
||||
char stringOutput[256];
|
||||
std::memset(stringOutput, '!', sizeof(stringOutput));
|
||||
std::int64_t count = -1;
|
||||
|
||||
Reference in New Issue
Block a user