commit aaf75eae4d6d31a820a0bf34b2722ec039d52d7d
parent 998c8353988dcb49ab5552458a6873c8c2a40b01
Author: Eyal Sawady <ecs@d2evs.net>
Date: Thu, 30 Sep 2021 01:42:26 +0000
basename, dirname: new commands
Diffstat:
4 files changed, 55 insertions(+), 0 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -1,4 +1,6 @@
+basename
cat
+dirname
false
head
nl
diff --git a/Makefile b/Makefile
@@ -4,7 +4,9 @@ HARE=hare
HAREFLAGS=
utils=\
+ basename \
cat \
+ dirname \
false \
head \
nl \
@@ -24,7 +26,9 @@ clean:
.ha:
$(HARE) build $(HAREFLAGS) -o $@ $<
+basename: basename.ha main/main.ha
cat: cat.ha main/main.ha
+dirname: dirname.ha main/main.ha
false: false.ha
head: head.ha
nl: nl.ha
diff --git a/basename.ha b/basename.ha
@@ -0,0 +1,28 @@
+use fmt;
+use getopt;
+use main;
+use os;
+use path;
+use strings;
+
+export fn utilmain() (main::error | void) = {
+ const help: []getopt::help = [
+ "return non-directory portion of a pathname",
+ "<path> [<suffix>]",
+ ];
+ getopt::finish(&getopt::parse(os::args, help...));
+ if (len(os::args) != 2 && len(os::args) != 3) {
+ getopt::printusage(os::stderr, os::args[0], help);
+ os::exit(1);
+ };
+ let basename = path::basename(os::args[1]);
+ if (len(os::args) == 3 && strings::has_suffix(basename, os::args[2])) {
+ if (len(basename) == len(os::args[2])) yield;
+ // XXX: This should probably go in strings::
+ let slice = strings::toutf8(basename);
+ slice = slice[..len(basename) - len(os::args[2])];
+ basename = strings::fromutf8(slice);
+ };
+ fmt::println(basename)?;
+ return void;
+};
diff --git a/dirname.ha b/dirname.ha
@@ -0,0 +1,21 @@
+use fmt;
+use fs;
+use getopt;
+use io;
+use main;
+use os;
+use path;
+
+export fn utilmain() (main::error | void) = {
+ const help: []getopt::help = [
+ "return the directory portion of a pathname",
+ "<path>",
+ ];
+ getopt::finish(&getopt::parse(os::args, help...));
+ if (len(os::args) != 2) {
+ getopt::printusage(os::stderr, os::args[0], help);
+ os::exit(1);
+ };
+ fmt::println(path::dirname(os::args[1]))?;
+ return void;
+};