fix(libc): parse fopen and freopen modes as FreeBSD does #2033

Merged
TheAggie merged 1 commit from upstream-pr/2033 into main 2026-10-10 06:57:02 +00:00
TheAggie commented 2026-10-09 05:33:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by TheAggie on 2026-10-09T05:33:49Z.


What

vkQuake (PPSA99010, the homebrew Vulkan port) opens history.txt with mode "rt" at startup, and AnyPS5 threw from fopen.

FreeBSD's __sflags (lib/libc/stdio/flags.c) reads the access character (r, w, a), then b, +, x, e and v, and stops at the first other character without failing, so on the console "rt" opens for reading. On Windows, AnyPS5's fopen inserted b after the first character and passed the rest on: "rt" became "rbt". The CRT rejected that with EINVAL, and fopen throws for any error other than ENOENT. freopen accepted only a fixed list of 15 modes.

Both now parse the mode like __sflags (ParseFileMode):

  • The native mode is the access character, then + if seen, then b on Windows, then x if seen.
  • An unknown access character, or x on a read-only stream, returns NULL with EINVAL, as __sflags does.
  • x on an existing file returns NULL with EEXIST instead of throwing.
  • e (close-on-exec) and v (verify) are accepted and ignored.
  • Whether a stream writes (for the written-path record) comes from the parsed mode, so "rt+", which FreeBSD opens read-only, no longer counts as writing.

Tests (guest_standard_streams):

  • The byte-exact mode matrix, run through both fopen and freopen, adds "rt", "r+t", "wt", "w+e", "ae" and "rbv".
  • "", "q", "tr", "rx" and "rbx" return NULL with EINVAL from both.
  • "wx" on an existing file returns NULL with EEXIST, and on a new file creates and writes it.
  • "rt+" opens read-only: a write fails.
  • The test fails (an exception from fopen on "rt") with the old FileAndHeap.cpp.

Tested

Windows 11 Pro (26200), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release.

  • The full suite (488 tests) passes on this branch alone. It was run from the generated CTestTestfile.cmake commands because antivirus blocks ctest.exe on this machine.
  • tools/check_conventions.py --base origin/main passes.
  • vkQuake gets past its history.txt open with this, on a local branch with my other pending changes.

Not tested on Linux or macOS. The b insertion stays Windows-only as before.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4796434496 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2033). Originally posted by [TheAggie](https://github.com/TheAggie) on 2026-10-09T05:33:49Z. --- ### What vkQuake (PPSA99010, the homebrew Vulkan port) opens `history.txt` with mode `"rt"` at startup, and AnyPS5 threw from `fopen`. FreeBSD's `__sflags` ([`lib/libc/stdio/flags.c`](https://github.com/freebsd/freebsd-src/blob/releng/11.0/lib/libc/stdio/flags.c)) reads the access character (`r`, `w`, `a`), then `b`, `+`, `x`, `e` and `v`, and **stops at the first other character without failing**, so on the console `"rt"` opens for reading. On Windows, AnyPS5's `fopen` inserted `b` after the first character and passed the rest on: `"rt"` became `"rbt"`. The CRT rejected that with `EINVAL`, and `fopen` throws for any error other than `ENOENT`. `freopen` accepted only a fixed list of 15 modes. Both now parse the mode like `__sflags` (`ParseFileMode`): - The native mode is the access character, then `+` if seen, then `b` on Windows, then `x` if seen. - An unknown access character, or `x` on a read-only stream, returns NULL with `EINVAL`, as `__sflags` does. - `x` on an existing file returns NULL with `EEXIST` instead of throwing. - `e` (close-on-exec) and `v` (verify) are accepted and ignored. - Whether a stream writes (for the written-path record) comes from the parsed mode, so `"rt+"`, which FreeBSD opens read-only, no longer counts as writing. Tests (`guest_standard_streams`): - The byte-exact mode matrix, run through both `fopen` and `freopen`, adds `"rt"`, `"r+t"`, `"wt"`, `"w+e"`, `"ae"` and `"rbv"`. - `""`, `"q"`, `"tr"`, `"rx"` and `"rbx"` return NULL with `EINVAL` from both. - `"wx"` on an existing file returns NULL with `EEXIST`, and on a new file creates and writes it. - `"rt+"` opens read-only: a write fails. - The test fails (an exception from `fopen` on `"rt"`) with the old `FileAndHeap.cpp`. ### Tested Windows 11 Pro (26200), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release. - The full suite (488 tests) passes on this branch alone. It was run from the generated `CTestTestfile.cmake` commands because antivirus blocks `ctest.exe` on this machine. - `tools/check_conventions.py --base origin/main` passes. - vkQuake gets past its `history.txt` open with this, on a local branch with my other pending changes. Not tested on Linux or macOS. The `b` insertion stays Windows-only as before. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-10-09 05:53:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-09T05:53:52Z.

Review: APPROVED


Reviewed the complete current diff. The parser follows the documented FreeBSD mode scan, preserves the Windows binary-mode conversion, and the expanded guest_standard_streams cases exercise accepted flags, stop-at-unknown behavior, invalid access/exclusive modes, and create/no-replace semantics. Current Linux and Windows CI plus conventions/report checks are green. Approved.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5466233407 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2033#pullrequestreview-5466233407). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-09T05:53:52Z. **Review:** APPROVED --- Reviewed the complete current diff. The parser follows the documented FreeBSD mode scan, preserves the Windows binary-mode conversion, and the expanded guest_standard_streams cases exercise accepted flags, stop-at-unknown behavior, invalid access/exclusive modes, and create/no-replace semantics. Current Linux and Windows CI plus conventions/report checks are green. Approved.
oneandonlydean commented 2026-10-10 05:17:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-10T05:17:32Z.


Conflicts with main in core/libs/prx/libc/src/FileAndHeap.cpp; please rebase.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6094134976 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2033#issuecomment-6094134976). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-10T05:17:32Z. --- Conflicts with main in `core/libs/prx/libc/src/FileAndHeap.cpp`; please rebase.
adriadam10 commented 2026-10-10 06:56:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T06:56:59Z.


Rebased onto current main, no conflicts. Checked ParseFileMode against FreeBSD __sflags, guest_standard_streams passes on Linux and fails with the old FileAndHeap.cpp, and Build/Conventions are green on this head; merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6094877862 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2033#issuecomment-6094877862). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-10T06:56:59Z. --- Rebased onto current main, no conflicts. Checked `ParseFileMode` against FreeBSD `__sflags`, `guest_standard_streams` passes on Linux and fails with the old `FileAndHeap.cpp`, and Build/Conventions are green on this head; merging.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!2033
No description provided.