feat(libc): implement dirname #3261

Merged
MarcZX10 merged 3 commits from upstream-pr/3261 into main 2026-10-11 07:25:39 +00:00
MarcZX10 commented 2026-10-11 04:01:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-11T04:01:49Z.


What

Implement the missing dirname path operation with FreeBSD-style handling of empty paths, trailing slashes, root paths, and paths without a parent component. It accepts a const char*, returns the result in thread-local storage, and leaves the caller's input unchanged, so string literals are safe. The missing import is listed in #1578.

This re-submits the reviewed implementation from closed #1761 after the title evidence changed. Porpoise (PPSA99764) now reaches dirname after #3190–#3192 and stops at NID E4wZaG1zSFc on Linux and Windows. dirname remains absent on current main, and no other open PR implements it.

Tested

Linux x86-64, GCC 12.2, Release, current main (7f1dc965): clean CMake configuration; built guest_string_tests; guest_strings passed (1/1). The cases include literal input, unchanged input, and an overlong path returning null with errno 63. python3 tools/check_conventions.py --base upstream/main and git diff upstream/main...HEAD --check pass.

Windows and PS5 hardware were not tested.

Checklist

  • Based on current main; no other open PR implements this function
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw; no stub added
  • No notes, investigation files or images added
  • No third-party code added
  • Generic behavior
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4816433737 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3261). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-11T04:01:49Z. --- ### What Implement the missing `dirname` path operation with FreeBSD-style handling of empty paths, trailing slashes, root paths, and paths without a parent component. It accepts a `const char*`, returns the result in thread-local storage, and leaves the caller's input unchanged, so string literals are safe. The missing import is listed in #1578. This re-submits the reviewed implementation from closed #1761 after the title evidence changed. Porpoise (PPSA99764) now reaches `dirname` after #3190–#3192 and stops at NID `E4wZaG1zSFc` on Linux and Windows. `dirname` remains absent on current `main`, and no other open PR implements it. ### Tested Linux x86-64, GCC 12.2, Release, current `main` (`7f1dc965`): clean CMake configuration; built `guest_string_tests`; `guest_strings` passed (1/1). The cases include literal input, unchanged input, and an overlong path returning null with errno 63. `python3 tools/check_conventions.py --base upstream/main` and `git diff upstream/main...HEAD --check` pass. Windows and PS5 hardware were not tested. ### Checklist - [x] Based on current `main`; no other open PR implements this function - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except technical debt - [x] Unimplemented paths throw; no stub added - [x] No notes, investigation files or images added - [x] No third-party code added - [x] Generic behavior - [x] Depends on: none - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-11 04:02:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-11T04:02:09Z.


Progress report (7f1dc96 → bde2167)

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

✅ 1 implemented
Library Name
libc dirname_nid_postfix
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6105251807 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3261#issuecomment-6105251807). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-11T04:02:09Z. --- <!-- progress-report --> ### Progress report (7f1dc96 → bde2167) 📈 **System libraries**: 86.01% (+0.01%, +1 implemented functions) <details> <summary>✅ 1 implemented</summary> | Library | Name | | - | - | | libc | `dirname_nid_postfix` | </details>
sktharunbalaji13-cmd commented 2026-10-11 04:24:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by sktharunbalaji13-cmd on 2026-10-11T04:24:54Z.


I independently compared the implementation's behavior with the FreeBSD dirname reference and didn't find a return-value mismatch in the cases I checked. Thanks for keeping the implementation focused.

One small test-coverage suggestion: could we add cases such as one//two → one and /one//two → /one? These exercise multiple iterations of the repeated-separator collapse loop, which doesn't appear to be covered by the current assertions.

The single-root path / and a trailing-slash path such as foo/ would also be useful explicit boundary tests.

I couldn't compile the C++ target in my local environment, so this is a test-coverage suggestion rather than a claim that the implementation is defective.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6105409971 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3261#issuecomment-6105409971). Originally posted by [sktharunbalaji13-cmd](https://github.com/sktharunbalaji13-cmd) on 2026-10-11T04:24:54Z. --- I independently compared the implementation's behavior with the FreeBSD `dirname` reference and didn't find a return-value mismatch in the cases I checked. Thanks for keeping the implementation focused. One small test-coverage suggestion: could we add cases such as `one//two` → `one` and `/one//two` → `/one`? These exercise multiple iterations of the repeated-separator collapse loop, which doesn't appear to be covered by the current assertions. The single-root path `/` and a trailing-slash path such as `foo/` would also be useful explicit boundary tests. I couldn't compile the C++ target in my local environment, so this is a test-coverage suggestion rather than a claim that the implementation is defective.
MarcZX10 commented 2026-10-11 04:41:39 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-11T04:41:39Z.


Added the requested dirname boundary coverage in commit dbb2b8381:

Input Expected result
one//two one
/one//two /one
/ /
foo/ .

Built guest_string_tests with GCC 12.2 Release and ran guest_strings (1/1 passed). The conventions check and git diff --check also pass. The existing PR CI will rerun on this head.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6105522786 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3261#issuecomment-6105522786). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-11T04:41:39Z. --- Added the requested `dirname` boundary coverage in commit `dbb2b8381`: | Input | Expected result | | --- | --- | | `one//two` | `one` | | `/one//two` | `/one` | | `/` | `/` | | `foo/` | `.` | Built `guest_string_tests` with GCC 12.2 Release and ran `guest_strings` (1/1 passed). The conventions check and `git diff --check` also pass. The existing PR CI will rerun on this head.
oneandonlydean commented 2026-10-11 05:05:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-11T05:05:51Z.


dirname writes into the caller's string and returns it (StringExtensions.cpp:51, :60). Please copy the result into a thread_local buffer and leave path untouched, the way basename_nid_postfix above it does (too long: errno 63 and null). That is FreeBSD 11's char *dirname(const char *); a title passing a literal would fault on the write here. The test should then check that one/two/// is unchanged after the call.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6105683468 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3261#issuecomment-6105683468). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-11T05:05:51Z. --- `dirname` writes into the caller's string and returns it (`StringExtensions.cpp:51`, `:60`). Please copy the result into a `thread_local` buffer and leave `path` untouched, the way `basename_nid_postfix` above it does (too long: `errno` 63 and null). That is FreeBSD 11's `char *dirname(const char *)`; a title passing a literal would fault on the write here. The test should then check that `one/two///` is unchanged after the call.
MarcZX10 commented 2026-10-11 05:12:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-11T05:12:38Z.


Updated this PR to match FreeBSD's dirname(const char*) contract:

  • The implementation copies into a thread-local buffer and leaves the input untouched; the test now passes a string literal and verifies one/two/// is unchanged.
  • Inputs too long for the 1024-byte buffer return null and set errno 63, matching basename_nid_postfix.

After rebasing onto current main (7f1dc965), a clean Linux GCC 12.2 Release build of guest_string_tests succeeded and guest_strings passes (1/1). The conventions check and git diff --check pass.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6105729243 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3261#issuecomment-6105729243). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-11T05:12:38Z. --- Updated this PR to match FreeBSD's `dirname(const char*)` contract: - The implementation copies into a thread-local buffer and leaves the input untouched; the test now passes a string literal and verifies `one/two///` is unchanged. - Inputs too long for the 1024-byte buffer return null and set errno 63, matching `basename_nid_postfix`. After rebasing onto current `main` (`7f1dc965`), a clean Linux GCC 12.2 Release build of `guest_string_tests` succeeded and `guest_strings` passes (1/1). The conventions check and `git diff --check` pass.
Mouhand-Kaddo commented 2026-10-11 07:19:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Mouhand-Kaddo on 2026-10-11T07:19:53Z.


Additional platform validation of standalone PR head bde216731e4efd6c49021c0f22add8679b4e4182 (2026-10-11):

Windows: passed — Windows 11, WinLibs GCC 15.2.0 UCRT/POSIX/SEH r7; CMake 4.3.1 / Ninja; Release, BUILD_TESTING=ON.

1 checks, 0 failures, 0 CTest skips. Built the selected targets and dependencies; this is focused validation, not a full build or full-suite run.

Checks: guest_strings.

Command after building: ctest --test-dir <build> --verbose --no-tests=error --timeout 30 -R "^guest_strings$".

No title/gameplay validation. These results cover the standalone head, not a merge with current main.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6106614997 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3261#issuecomment-6106614997). Originally posted by [Mouhand-Kaddo](https://github.com/Mouhand-Kaddo) on 2026-10-11T07:19:53Z. --- <!-- anyps5-platform-validation:bde216731e4efd6c49021c0f22add8679b4e4182 --> Additional platform validation of standalone PR head `bde216731e4efd6c49021c0f22add8679b4e4182` (2026-10-11): **Windows: passed** — Windows 11, WinLibs GCC 15.2.0 UCRT/POSIX/SEH r7; CMake 4.3.1 / Ninja; Release, `BUILD_TESTING=ON`. 1 checks, 0 failures, 0 CTest skips. Built the selected targets and dependencies; this is focused validation, not a full build or full-suite run. Checks: `guest_strings`. Command after building: `ctest --test-dir <build> --verbose --no-tests=error --timeout 30 -R "^guest_strings$"`. No title/gameplay validation. These results cover the standalone head, not a merge with current main.
adriadam10 commented 2026-10-11 07:25:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T07:25:36Z.


Checked the FreeBSD dirname(const char *) contract (input untouched, thread-local result, empty/NULL → ., trailing and repeated separators, root), the errno 63 path matching basename, and that the new test fails to link without the change. Built guest_string_tests and ran the libs ctest scope on current main (66/66 pass, guest_strings included). Dean's earlier request is addressed. Merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6106657533 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3261#issuecomment-6106657533). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T07:25:36Z. --- Checked the FreeBSD `dirname(const char *)` contract (input untouched, thread-local result, empty/NULL → `.`, trailing and repeated separators, root), the `errno` 63 path matching `basename`, and that the new test fails to link without the change. Built `guest_string_tests` and ran the libs ctest scope on current `main` (66/66 pass, `guest_strings` included). Dean's earlier request is addressed. 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!3261
No description provided.