hautils

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 79fba05a9edc752b115fea48fad0d90931181fe3
parent cdb977f8e72a8bf951b156434e7ca5ec5e065ba4
Author: Drew DeVault <sir@cmpwn.com>
Date:   Sat,  8 Jan 2022 11:22:55 +0100

pwd: rewrite resolvelinks

Signed-off-by: Drew DeVault <sir@cmpwn.com>

Diffstat:
Mpwd.ha | 37++++++++++---------------------------
1 file changed, 10 insertions(+), 27 deletions(-)

diff --git a/pwd.ha b/pwd.ha @@ -42,17 +42,13 @@ export fn utilmain() (main::error | void) = { case mode::NORMLINK => yield resolvelinks(cwd)?; }; - defer free(path); fmt::println(path)?; }; -// Reads all links in a path. The caller must free the return value. +// Reads all links in a path. fn resolvelinks(path: str) (str | fs::error) = { - // TODO: Rewrite me after https://todo.sr.ht/~sircmpwn/hare/426 - let path = strings::dup(path); - defer free(path); - let parts: []str = []; - defer free(parts); + static let buf = path::buffer { ... }; + path::reset(&buf); let iter = path::iter(path); for (true) { @@ -62,12 +58,9 @@ fn resolvelinks(path: str) (str | fs::error) = { case void => break; }; - append(parts, item); + path::add(&buf, item)!; - const temp = path::join(parts...); - defer free(temp); - - let link = match (os::readlink(temp)) { + let link = match (os::readlink(path::string(&buf))) { case let link: str => yield link; case fs::wrongtype => @@ -77,22 +70,12 @@ fn resolvelinks(path: str) (str | fs::error) = { }; if (!path::abs(link)) { - delete(parts[len(parts) - 1]); - append(parts, link); - let temp = path::join(parts...); - defer free(temp); - let newlink = os::resolve(temp); - free(link); - link = newlink; + path::add(&buf, "..", link)!; + } else { + path::reset(&buf); + path::add(&buf, link)!; }; - - // Reset state and start over with the link target - free(path); - path = link; - iter = path::iter(path); - free(parts); - parts = []; }; - return path::join(parts...); + return path::string(&buf); };