coreutils

General Software Utilities
git clone http://git.omkov.net/coreutils
Log | Tree | Refs | README | LICENCE | Download

AuthorJamozed <[email protected]>
Date2020-06-26 03:54:59
Commit703c1e1bc4d5c4ad58decd0a833a86751d14be58
Parent16cc815fc5b9a6af78646d77deabf990501dd76a

basename: Add POSIX basename

Diffstat

M CMakeLists.txt | 1 +
M README.md | 1 +
A man/basename.1 | 40 ++++++++++++++++++++++++++++++++++++++++
A src/basename.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

4 files changed, 135 insertions, 0 deletions

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 987e23a..10ea771 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -10,6 +10,7 @@ SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin)
 
 INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/ext)
 
+ADD_EXECUTABLE(basename ${CMAKE_SOURCE_DIR}/src/basename.c)
 ADD_EXECUTABLE(cat      ${CMAKE_SOURCE_DIR}/src/cat.c)
 ADD_EXECUTABLE(false    ${CMAKE_SOURCE_DIR}/src/false.c)
 ADD_EXECUTABLE(true     ${CMAKE_SOURCE_DIR}/src/true.c)
diff --git a/README.md b/README.md
index 78fa518..f4b8ce8 100644
--- a/README.md
+++ b/README.md
@@ -9,6 +9,7 @@ commonly found on UNIX-like systems.
 
 | Utility          | Description                              | Standard |
 | ---------------- | ---------------------------------------- | -------- |
+| basename         | Return non-directory portion of a path   | POSIX    |
 | cat              | Concatenate and print files              | POSIX    |
 | false            | Return false value                       | POSIX    |
 | true             | Return true value                        | POSIX    |
diff --git a/man/basename.1 b/man/basename.1
new file mode 100644
index 0000000..db1d5a4
--- /dev/null
+++ b/man/basename.1
@@ -0,0 +1,40 @@
+.TH BASENAME 1 2020-06-26 "OMKOV coreutils" "General Commands Manual"
+.SH NAME
+basename \(em return the non-directory portion of a path
+.SH SYNOPSYS
+\fBbasename\fR \fIstring\fR [\fIsuffix\fR]
+.SH DESCRIPTION
+Print the non-directory portion of a given path.
+.SH OPTIONS
+The following options are supported:
+.TP
+.B --help
+Display help information.
+.TP
+.B --version
+Display version information.
+.SH OPERANDS
+The following operands are supported:
+.TP
+.I string
+A path.
+.TP
+.I suffix
+A suffix to be removed from the output.
+.SH EXIT STATUS
+The following exit values will be returned:
+.TP
+\ 0
+Successful completion.
+.TP
+>0
+An error occurred.
+.SH STANDARDS
+The \fIbasename\fR utility is compliant with the IEEE Std 1003.2-1992 ("POSIX.2")
+specification.
+.SH COPYRIGHT
+.nf
+Copyright (C) 2020, Jakob Wakeling
+All rights reserved.
+OMKOV Permissive Licence (https://www.omkov.net/OLPE)
+.fi
diff --git a/src/basename.c b/src/basename.c
new file mode 100644
index 0000000..bf1454e
--- /dev/null
+++ b/src/basename.c
@@ -0,0 +1,93 @@
+// basename.c, version 1.0.0
+// OMKOV coreutils implementation of POSIX basename
+// Copyright (C) 2020, Jakob Wakeling
+// All rights reserved.
+
+/*
+OMKOV Permissive Licence, version 1.0
+
+Copyright (C) 2020, Jakob Wakeling
+All rights reserved.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal with
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+* Redistributions of source code must retain the above copyright notice, this
+  list of conditions and the following disclaimers.
+* Redistributions in binary form must reproduce the above copyright notice, this
+  list of conditions and the following disclaimers in the documentation and/or
+  other materials provided with the distribution.
+* Neither the names of the copyright holders, nor the names of its contributors
+  may be used to endorse or promote products derived from this Software without
+  specific prior written permission.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT
+HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE.
+*/
+
+#include "error.h"
+#include "optget.h"
+
+#include <stdio.h>
+
+static void help(void);
+static void version(void);
+
+int main(int argc, char *argv[]) {
+	lop_t lops[] = {
+		{ "help",    ARG_NUL, 256 },
+		{ "version", ARG_NUL, 257 },
+		{ NULL, 0, 0 }
+	};
+	
+	opt_t opt = OPTGET_INIT; opt.str = ""; opt.lops = lops; int o;
+	while ((o = optget(&opt, argv, 1)) != -1) switch (o) {
+	case 256: { help(); return 0; }
+	case 257: { version(); return 0; }
+	default: { return 1; }
+	}
+	
+	if (opt.ind == argc) { error(1, "%s: missing operand", argv[0]); }
+	
+	register char *p = argv[opt.ind];
+	if (!*p) { fputc('.', stdout); fputc('\n', stdout); return 0; }
+	
+	for (++p; *p; ++p) {} for (--p; *p == '/'; --p);
+	if (p + 1 == argv[opt.ind]) { fputs("/\n", stdout); return 0; }
+	else { p[1] = 0; }
+	
+	if (argv[opt.ind + 1]) {
+		register char *s = argv[opt.ind + 1]; for (; *s; ++s);
+		for (--s; *p && *p != '/' && *s && *p == *s; --p, --s);
+		if (!*s && *p && *p != '/') { p[1] = 0; }
+	}
+	
+	for (; *p && *p != '/'; --p);
+	fputs(p + 1, stdout); fputc('\n', stdout);
+	return 0;
+}
+
+static void help(void) {
+	puts("basename - return the non-directory portion of a path\n");
+	puts("usage: basename string [suffix]\n");
+	puts("options:");
+	puts("  --help     Display help information");
+	puts("  --version  Display version information");
+	return;
+}
+
+static void version(void) {
+	puts("OMKOV coreutils basename, version 1.0.0");
+	puts("Copyright (C) 2020, Jakob Wakeling");
+	puts("All rights reserved.");
+	puts("OMKOV Permissive Licence (https://www.omkov.net/OLPE)");
+	return;
+}