path: overhaul `expanduser` and `contractuser` for cross-platform support
Rewrite `expanduser` to expand `~` to `homedir()` on all platforms.
On Unix, `~username` is expanded via `homedir(username)`; on Windows,
`~username` is left unexpanded since there is no reliable way to look
up another user's home directory. Username validation rejects
characters not found in valid usernames on any platform. The original
separator after `~` or `~username` is preserved verbatim.
Rewrite `contractuser` on both platforms to walk path prefixes using
separator scanning and `samefile`-based inode comparison. When a
prefix matches a home directory, the prefix is replaced with `~` (or
`~username` on Unix) and the rest of the original path string is
preserved verbatim. This handles symlinks and mixed separators
transparently, and ensures `contractuser(expanduser(p)) == p`.
On Unix, directory owners are checked to support `~username`
contraction. Home directory stat results are cached to avoid
redundant syscalls.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>