coreutils

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

AuthorJamozed <[email protected]>
Date2020-06-26 10:08:46
Commitdee294962682641c95ba447b4f9e5acceb5c6195
Parentd3522e7be6d6f1ae99b96a2883cfabd1a77f161a

rmdir: Add POSIX rmdir

Diffstat

M CMakeLists.txt | 1 +
M README.md | 1 +
A man/rmdir.1 | 40 ++++++++++++++++++++++++++++++++++++++++
A src/rmdir.c | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

4 files changed, 143 insertions, 0 deletions

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 14f588f..9832ae3 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -17,6 +17,7 @@ ADD_EXECUTABLE(echo     ${CMAKE_SOURCE_DIR}/src/echo.c)
 ADD_EXECUTABLE(false    ${CMAKE_SOURCE_DIR}/src/false.c)
 ADD_EXECUTABLE(link     ${CMAKE_SOURCE_DIR}/src/link.c)
 ADD_EXECUTABLE(mkdir    ${CMAKE_SOURCE_DIR}/src/mkdir.c)
+ADD_EXECUTABLE(rmdir    ${CMAKE_SOURCE_DIR}/src/rmdir.c)
 ADD_EXECUTABLE(tee      ${CMAKE_SOURCE_DIR}/src/tee.c)
 ADD_EXECUTABLE(true     ${CMAKE_SOURCE_DIR}/src/true.c)
 ADD_EXECUTABLE(unlink   ${CMAKE_SOURCE_DIR}/src/unlink.c)
diff --git a/README.md b/README.md
index 03cb63f..e913ffd 100644
--- a/README.md
+++ b/README.md
@@ -16,6 +16,7 @@ commonly found on UNIX-like systems.
 | false            | Return false value                       | POSIX    |
 | link             | Create a link to a file                  | POSIX    |
 | mkdir            | Make directories                         | POSIX    |
+| rmdir            | Remove directories                       | POSIX    |
 | tee              | Duplicate standard input                 | POSIX    |
 | true             | Return true value                        | POSIX    |
 | unlink           | Remove a file using the unlink function  | POSIX    |
diff --git a/man/rmdir.1 b/man/rmdir.1
new file mode 100644
index 0000000..721ed87
--- /dev/null
+++ b/man/rmdir.1
@@ -0,0 +1,40 @@
+.TH RMDIR 1 2020-06-26 "OMKOV coreutils" "General Commands Manual"
+.SH NAME
+rmdir \(em remove directories
+.SH SYNOPSYS
+\fBrmdir\fR [-p] \fIdir\fR...
+.SH DESCRIPTION
+Remove the directory specified by each \fIdir\fR operand, provided it is empty.
+.SH OPTIONS
+The following options are supported:
+.TP
+.B -p
+Remove all directories in a pathname, provided they are empty.
+.TP
+.B --help
+Display help information.
+.TP
+.B --version
+Display version information.
+.SH OPERANDS
+The following operand is supported:
+.TP
+.I dir
+A path of a directory to be removed.
+.SH EXIT STATUS
+The following exit values will be returned:
+.TP
+\ 0
+Successful completion.
+.TP
+>0
+An error occurred.
+.SH STANDARDS
+The \fIrmdir\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/rmdir.c b/src/rmdir.c
new file mode 100644
index 0000000..95da7d0
--- /dev/null
+++ b/src/rmdir.c
@@ -0,0 +1,101 @@
+// rmdir.c, version 1.0.0
+// OMKOV coreutils implementation of POSIX rmdir
+// 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 <unistd.h>
+
+#include <errno.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <string.h>
+
+static bool pflag;
+
+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 = "p"; opt.lops = lops; int o;
+	while ((o = optget(&opt, argv, 1)) != -1) switch (o) {
+	case 'p': { pflag = true; break; }
+	case 256: { help(); return 0; }
+	case 257: { version(); return 0; }
+	default: { return 1; }
+	}
+	
+	if (opt.ind == argc) { error(1, "%s: missing operand", argv[0]); }
+	
+	for (char **p = &argv[opt.ind]; *p; ++p) {
+		if (rmdir(*p)) { warn("%s: %s: %s", argv[0], *p, serrno); continue; }
+		if (pflag) {
+			char *c = *p; for (; *c; ++c);
+			for (--c; *p < c && *(c - 1) == '/'; --c);
+			for (*c = 0; *p < c; --c) {
+				if (*c != '/' || *(c - 1) == '/') { continue; }
+				*c = 0; if (rmdir(*p)) {
+					warn("%s: %s: %s", argv[0], *p, serrno); break;
+				}
+			}
+		}
+	}
+	
+	return 0;
+}
+
+static void help(void) {
+	puts("rmdir - remove directories\n");
+	puts("usage: rmdir [-p] dir...\n");
+	puts("options:");
+	puts("  -p         Remove all parent directories in a path");
+	puts("  --help     Display help information");
+	puts("  --version  Display version information");
+	return;
+}
+
+static void version(void) {
+	puts("OMKOV coreutils rmdir, version 1.0.0");
+	puts("Copyright (C) 2020, Jakob Wakeling");
+	puts("All rights reserved.");
+	puts("OMKOV Permissive Licence (https://www.omkov.net/OLPE)");
+	return;
+}