commit: 67d25ca4e1c794c799f15a65ab2fe51ffa43c57c
parent ba6a776b7aa41be52a46bd59b40f34bcedd4d02f
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Mon,  5 Apr 2021 06:04:03 +0200
bin/dirname: New
Diffstat:
5 files changed, 64 insertions(+), 2 deletions(-)
diff --git a/bin/Makefile.config b/bin/Makefile.config
@@ -1,2 +1,2 @@
-EXE = args basename date echo lolcat mdate pwd range sizeof xcd
-MAN1 = basename.1 date.1 lolcat.1
+EXE = args basename date dirname echo lolcat mdate pwd range sizeof xcd
+MAN1 = basename.1 date.1 dirname.1 lolcat.1
diff --git a/bin/dirname.1 b/bin/dirname.1
@@ -0,0 +1,27 @@
+.Dd 2021-04-05
+.Dt DIRNAME 1
+.Os
+.Sh NAME
+.Nm dirname
+.Nd return the parent directory of a file pathname
+.Sh SYNOPSIS
+.Nm
+.Ar string
+.Sh DESCRIPTION
+Returns the parent directory of
+.Ar string .
+.Sh EXIT STATUS
+.Ex -std
+.Sh SEE ALSO
+.Xr direname 3
+.Sh STANDARDS
+.Nm
+is close but not compliant with the
+.St -p1003.1-2008
+specification as it uses
+.Xr dirname 3
+directly instead of extracting the non-directory portion of
+.Ar string
+with it's own algorithm.
+.Sh AUTHORS
+.An Haelwenn (lanodan) Monnier Aq Mt contact@hacktivis.me
diff --git a/bin/dirname.c b/bin/dirname.c
@@ -0,0 +1,14 @@
+#include <libgen.h> // dirname()
+#include <stdio.h> // puts()
+
+int main(int argc, char *argv[])
+{
+	if(argc != 2)
+	{
+		fputs("usage: dirname string", stderr);
+		return 1;
+	}
+
+	puts(dirname(argv[1]));
+	return 0;
+}
diff --git a/test-bin/Makefile b/test-bin/Makefile
@@ -1,3 +1,4 @@
 test:
 	./xcd.t
 	./basename.t
+	./dirname.t
diff --git a/test-bin/dirname.t b/test-bin/dirname.t
@@ -0,0 +1,20 @@
+#!/bin/sh
+failed=0
+path=../bin/dirname
+status() {
+	if [ "$2" -eq 0 ]; then
+		echo "OK: ${1}"
+	else
+		echo "FAILED: ${1}"
+		failed=1
+	fi
+}
+[ "$(${path} 2>/dev/null)" = "" ] ; status "no args" $?
+[ "$(${path} /usr/bin)" = "/usr" ] ; status "/usr/bin" $?
+[ "$(${path} /usr//bin)" = "/usr" ] ; status "/usr//bin" $?
+
+## POSIX-tests
+#[ "$(${path} //)" = "//" ] ; status "//" $?
+#[ "$(${path} ///)" = "///" ] ; status "///" $?
+
+exit $failed