feat(libc): implement wcstok #3583

Merged
DariusHQ merged 1 commit from upstream-pr/3583 into main 2026-10-11 21:38:26 +00:00
DariusHQ commented 2026-10-11 13:45:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DariusHQ on 2026-10-11T13:45:45Z.


What

wcstok (8ngzWNZzFJU) in libc, on the guest's 16-bit wchar_t, next to wcsspn and with the same Contains helper.

PS5MC (PPSA99420 1.2, VivaLaVent/PS5MC) stops at it at start-up once sendfile is exported (#3568). Run on Windows, catalog build with eboot.bin and sce_module/libc.prx unwrapped as #2605 requires, main a11c8f7f7 with #3568 merged in:

FAIL: unresolved ELF import 8ngzWNZzFJU from libSceLibcInternal.prx

With this branch added it goes on to pause (EDyhWelj7UY), run the same way:

FAIL: unresolved ELF import EDyhWelj7UY from libkernel.prx

#2220 implemented this before and was closed because no title had been run to it. This PR is that run.

It follows FreeBSD's three-argument wcstok:

  • with text null it continues from *last, and returns null when that is null;
  • leading delimiters are skipped; if only delimiters remain, *last becomes null and it returns null;
  • the token ends at the next delimiter, which is overwritten with 0, and *last points past it;
  • a token that runs to the end of the string sets *last to null, not to the terminator as strtok_r does.

Code units are compared one at a time, so a surrogate pair is never split by a BMP delimiter.

Tested

Windows 11 (26200), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, full build with BUILD_TESTING=ON on main a11c8f7f7 with this branch and #3568 merged together: ctest 833/837 passed. The four failures fail the same way on main here:

  • windows_exceptions (#3411);
  • guest_kernel_sys: symlink throws Function not implemented on Windows, which #3510 addresses;
  • guest_filesystem: restoring the directory time fails on Windows, which #3518 addresses;
  • agc_driver_emulated_color_compare: the GPU here cannot sample the sRGB image it uses.

guest_wide_strings checks:

  • leading and repeated delimiters, a delimiter set changed between calls, and *last after each token;
  • null returned twice after the last token, with *last null;
  • a last token without a trailing delimiter (*last null, the string left intact);
  • a string of only delimiters (left unchanged) and an empty string;
  • an empty delimiter set, which returns the whole string;
  • a CJK delimiter around a CJK unit and a surrogate pair.

Mutation check, all caught:

  • leading delimiters not skipped;
  • *last left at the terminator after the last token;
  • *last left set when only delimiters remain;
  • the delimiter not overwritten;
  • *last pointing at the delimiter instead of past it;
  • a null text not continuing from *last.

tools/import_audit.py on PS5MC relinked with --windows --registry: absent imports go from 27 (with #3568) to 26.

Not run on Linux (no Linux host here); the change has no platform-specific part.

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 (the code is independent; #3568 is only needed for PS5MC to reach wcstok)
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4819509201 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3583). Originally posted by [DariusHQ](https://github.com/DariusHQ) on 2026-10-11T13:45:45Z. --- ### What `wcstok` (`8ngzWNZzFJU`) in libc, on the guest's 16-bit `wchar_t`, next to `wcsspn` and with the same `Contains` helper. PS5MC (PPSA99420 1.2, [VivaLaVent/PS5MC](https://github.com/VivaLaVent/PS5MC)) stops at it at start-up once `sendfile` is exported (#3568). Run on Windows, catalog build with `eboot.bin` and `sce_module/libc.prx` unwrapped as #2605 requires, `main` `a11c8f7f7` with #3568 merged in: ``` FAIL: unresolved ELF import 8ngzWNZzFJU from libSceLibcInternal.prx ``` With this branch added it goes on to `pause` (`EDyhWelj7UY`), run the same way: ``` FAIL: unresolved ELF import EDyhWelj7UY from libkernel.prx ``` #2220 implemented this before and was closed because no title had been run to it. This PR is that run. It follows FreeBSD's three-argument [`wcstok`](https://github.com/freebsd/freebsd-src/blob/stable/11/lib/libc/string/wcstok.c): - with `text` null it continues from `*last`, and returns null when that is null; - leading delimiters are skipped; if only delimiters remain, `*last` becomes null and it returns null; - the token ends at the next delimiter, which is overwritten with 0, and `*last` points past it; - a token that runs to the end of the string sets `*last` to null, not to the terminator as `strtok_r` does. Code units are compared one at a time, so a surrogate pair is never split by a BMP delimiter. ### Tested Windows 11 (26200), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, full build with `BUILD_TESTING=ON` on `main` `a11c8f7f7` with this branch and #3568 merged together: `ctest` 833/837 passed. The four failures fail the same way on `main` here: - `windows_exceptions` (#3411); - `guest_kernel_sys`: `symlink` throws `Function not implemented` on Windows, which #3510 addresses; - `guest_filesystem`: restoring the directory time fails on Windows, which #3518 addresses; - `agc_driver_emulated_color_compare`: the GPU here cannot sample the sRGB image it uses. `guest_wide_strings` checks: - leading and repeated delimiters, a delimiter set changed between calls, and `*last` after each token; - null returned twice after the last token, with `*last` null; - a last token without a trailing delimiter (`*last` null, the string left intact); - a string of only delimiters (left unchanged) and an empty string; - an empty delimiter set, which returns the whole string; - a CJK delimiter around a CJK unit and a surrogate pair. Mutation check, all caught: - leading delimiters not skipped; - `*last` left at the terminator after the last token; - `*last` left set when only delimiters remain; - the delimiter not overwritten; - `*last` pointing at the delimiter instead of past it; - a null `text` not continuing from `*last`. `tools/import_audit.py` on PS5MC relinked with `--windows --registry`: absent imports go from 27 (with #3568) to 26. Not run on Linux (no Linux host here); the change has no platform-specific part. ### 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 (the code is independent; #3568 is only needed for PS5MC to reach `wcstok`) - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-11 13:46:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-11T13:46:07Z.


Progress report (a11c8f7 → 6ef110a)

📈 System libraries: 85.82% (+0.01%, +1 implemented functions)

✅ 1 implemented
Library Name
libc wcstok_nid_postfix
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6109679935 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3583#issuecomment-6109679935). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-11T13:46:07Z. --- <!-- progress-report --> ### Progress report (a11c8f7 → 6ef110a) 📈 **System libraries**: 85.82% (+0.01%, +1 implemented functions) <details> <summary>✅ 1 implemented</summary> | Library | Name | | - | - | | libc | `wcstok_nid_postfix` | </details>
adriadam10 commented 2026-10-11 21:38:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T21:38:21Z.


Checked against FreeBSD's wcstok.c (delimiter skipping, *last null on exhaustion, delimiter overwritten, *last = end + 1); build and ctest on Linux for libs/libc green, and guest_wide_strings fails to link without the change. Merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6114061033 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3583#issuecomment-6114061033). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T21:38:21Z. --- Checked against FreeBSD's `wcstok.c` (delimiter skipping, `*last` null on exhaustion, delimiter overwritten, `*last = end + 1`); build and ctest on Linux for libs/libc green, and `guest_wide_strings` fails to link without the change. 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!3583
No description provided.