commit 0d09a13faf0639530eebbe901a781f85b7b983e4
parent 02c289707bbb69398aae135989183b748127e15e
Author: Drew DeVault <sir@cmpwn.com>
Date: Wed, 16 Mar 2022 09:39:36 +0100
pwd: use os::realpath
Diffstat:
M | pwd.ha | | | 43 | ++++--------------------------------------- |
1 file changed, 4 insertions(+), 39 deletions(-)
diff --git a/pwd.ha b/pwd.ha
@@ -9,7 +9,7 @@ use strings;
type mode = enum {
NORM,
- NORMLINK,
+ REAL,
};
export fn utilmain() (main::error | void) = {
@@ -28,53 +28,18 @@ export fn utilmain() (main::error | void) = {
case 'L' =>
mode = mode::NORM;
case 'P' =>
- mode = mode::NORMLINK;
+ mode = mode::REAL;
case => abort();
};
};
// TODO: Update me following https://todo.sr.ht/~sircmpwn/hare/540
- // Maybe our resolvelinks function should become os::resolve?
const cwd = os::tryenv("PWD", os::getcwd());
const path = switch (mode) {
case mode::NORM =>
yield os::resolve(cwd);
- case mode::NORMLINK =>
- yield resolvelinks(cwd)?;
+ case mode::REAL =>
+ yield os::realpath(cwd)?;
};
fmt::println(path)?;
};
-
-// Reads all links in a path. The return value is statically allocated.
-fn resolvelinks(path: str) (str | fs::error) = {
- static let buf = path::buffer { ... };
- path::reset(&buf);
-
- const iter = path::iter(path);
- for (true) {
- const item = match (path::next(&iter)) {
- case let item: str =>
- yield item;
- case void =>
- break;
- };
-
- const item = path::add(&buf, item)!;
- const link = match (os::readlink(item)) {
- case let link: str =>
- yield link;
- case fs::wrongtype =>
- continue;
- case let err: fs::error =>
- return err;
- };
-
- if (!path::abs(link)) {
- path::add(&buf, "..", link)!;
- } else {
- path::set(&buf, link)!;
- };
- };
-
- return path::string(&buf);
-};