coreutils

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

AuthorJamozed <[email protected]>
Date2020-06-26 06:42:30
Commitd3522e7be6d6f1ae99b96a2883cfabd1a77f161a
Parent0534c26bf2f11ba722dac7affefd9475bf03272a

mkdir: Add POSIX mkdir

Diffstat

M CMakeLists.txt | 1 +
M README.md | 1 +
A man/mkdir.1 | 44 ++++++++++++++++++++++++++++++++++++++++++++
A src/mkdir.c | 207 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

4 files changed, 253 insertions, 0 deletions

diff --git a/CMakeLists.txt b/CMakeLists.txt
index bac326a..14f588f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -16,6 +16,7 @@ ADD_EXECUTABLE(dirname  ${CMAKE_SOURCE_DIR}/src/dirname.c)
 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(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 c49ff37..03cb63f 100644
--- a/README.md
+++ b/README.md
@@ -15,6 +15,7 @@ commonly found on UNIX-like systems.
 | echo             | Write arguments to standard output       | POSIX    |
 | false            | Return false value                       | POSIX    |
 | link             | Create a link to a file                  | POSIX    |
+| mkdir            | Make 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/mkdir.1 b/man/mkdir.1
new file mode 100644
index 0000000..db0d8d2
--- /dev/null
+++ b/man/mkdir.1
@@ -0,0 +1,44 @@
+.TH MKDIR 1 2020-06-26 "OMKOV coreutils" "General Commands Manual"
+.SH NAME
+mkdir \(em make directories
+.SH SYNOPSYS
+\fBmkdir\fR [-p] [-m \fImode\fR] \fIdir\fR...
+.SH DESCRIPTION
+Create the directory specified by each \fIdir\fR operand, provided it does not
+already exist.
+.SH OPTIONS
+The following options are supported:
+.TP
+.B -p
+Create all parent directories as required.
+.TP
+.B -m \fImode\fR
+Set the file mode of the created directory.
+.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 created.
+.SH EXIT STATUS
+The following exit values will be returned:
+.TP
+\ 0
+Successful completion.
+.TP
+>0
+An error occurred.
+.SH STANDARDS
+The \fImkdir\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/mkdir.c b/src/mkdir.c
new file mode 100644
index 0000000..04a145d
--- /dev/null
+++ b/src/mkdir.c
@@ -0,0 +1,207 @@
+// mkdir.c, version 1.0.1
+// OMKOV coreutils implementation of POSIX mkdir
+// 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 <sys/stat.h>
+
+#include <ctype.h>
+#include <errno.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <string.h>
+
+#define USR 05700
+#define GRP 02070
+#define OTH 00007
+#define ALL 07777
+
+#define RD  00444
+#define WR  00222
+#define EX  00111
+#define ID  06000
+#define ST  01000
+
+static char  *mopt;
+static bool   pflag;
+
+static mode_t mode, mask;
+
+static inline mode_t newmode(char *mstr);
+static inline mode_t strtom(char **nptr);
+static inline mode_t getref(char **mstr);
+static inline int    getop(char **mstr);
+static inline mode_t getmod(char **mstr);
+
+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 = "m:p"; opt.lops = lops; int o;
+	while ((o = optget(&opt, argv, 1)) != -1) switch (o) {
+	case 'm': { mopt = opt.arg; break; }
+	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]); }
+	
+	if (mopt) {
+		mask = umask(0); mode = newmode(mopt);
+		if (mode == (mode_t)-1) {
+			error(1, "%s: %s: invalid mode", argv[0], mopt);
+		}
+	} else { mode = 0777; }
+	
+	for (int i = opt.ind; i < argc; ++i) {
+		if (pflag) {
+			char *p = argv[i];
+			for (; *p == '/'; ++p) {}
+			for (; *p; ++p) {
+				if (*p != '/' || *(p + 1) == '/') { continue; }
+				*p = 0;
+				if (mkdir(argv[i], mode)) {
+					if (errno != EEXIST) {
+						warn("%s: %s: %s", argv[0], argv[i], serrno); continue;
+					}
+				}
+				*p = '/';
+			}
+		}
+		if (mkdir(argv[i], mode)) {
+			warn("%s: %s: %s", argv[0], argv[i], serrno); continue;
+		}
+	}
+	
+	return 0;
+}
+
+static inline mode_t newmode(char *mstr) {
+	for (; isspace(*mstr); ++mstr);
+	char *start = mstr; register mode_t nmode;
+	
+	nmode = strtom(&mstr);
+	if (mstr == start) { nmode = 0; }
+	else if (*mstr) { return (mode_t)-1; }
+	else { return nmode; }
+	
+	do {
+		register mode_t ref, mod; register int op;
+		
+		ref = getref(&mstr);
+		while ((op = getop(&mstr))) {
+			mod = getmod(&mstr);
+			switch (op) {
+			case '+': { nmode |= ref & mod; break; }
+			case '-': { nmode &= (~(ref & mod) & 07777); break; }
+			case '=': { nmode &= ~ref; nmode |= ref & mod; break; }
+			}
+		}
+	} while (*mstr++ == ',' && *mstr);
+	
+	if (*--mstr) { return (mode_t)-1; }
+	return nmode;
+}
+
+static inline mode_t strtom(char **nptr) {
+	mode_t nmode = 0; register int d;
+	
+	for (; **nptr >= '0' && **nptr <= '7'; ++*nptr) {
+		d = **nptr - '0';
+		if (nmode > (unsigned)(07777 - d) / 8) { break; }
+		nmode = nmode * 8 + (unsigned)d;
+	}
+	
+	return nmode;
+}
+
+static inline mode_t getref(char **mstr) {
+	register mode_t nmode = 0;
+	
+	for (;;) switch (**mstr) {
+	case 'u': { nmode |= USR; ++*mstr; continue; }
+	case 'g': { nmode |= GRP; ++*mstr; continue; }
+	case 'o': { nmode |= OTH; ++*mstr; continue; }
+	case 'a': { nmode |= ALL; ++*mstr; continue; }
+	default: { if (!nmode) { nmode = ALL & ~mask; } return nmode; }
+	}
+}
+
+static inline int getop(char **mstr) {
+	if (**mstr == '+' || **mstr == '-' || **mstr == '=') { return *(*mstr)++; }
+	switch (**mstr) { case '+': case '-': case '=': { return *(*mstr)++; }}
+	return 0;
+}
+
+static inline mode_t getmod(char **mstr) {
+	register mode_t nmode = 0;
+	
+	for (;;) switch (**mstr) {
+		case 'r': { nmode |= RD; ++*mstr; continue; }
+		case 'w': { nmode |= WR; ++*mstr; continue; }
+		case 'x': case 'X': { nmode |= EX; ++*mstr; continue; }
+		case 's': { nmode |= ID; ++*mstr; continue; }
+		case 't': { nmode |= ST; ++*mstr; continue; }
+		default: { return nmode; }
+	}
+}
+
+static void help(void) {
+	puts("mkdir - make directories\n");
+	puts("usage: mkdir [-p] [-m mode] dir...\n");
+	puts("options:");
+	puts("  -p         Create all parent directories as required");
+	puts("  -m mode    Set the file mode of the created directory");
+	puts("  --help     Display help information");
+	puts("  --version  Display version information");
+	return;
+}
+
+static void version(void) {
+	puts("OMKOV coreutils mkdir, version 1.0.1");
+	puts("Copyright (C) 2020, Jakob Wakeling");
+	puts("All rights reserved.");
+	puts("OMKOV Permissive Licence (https://www.omkov.net/OLPE)");
+	return;
+}