coreutils

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

AuthorJamozed <[email protected]>
Date2020-08-05 11:54:14
Commit068c8fb6c7bfc41f7bc362e1bcbf1a0a47008aaa
Parentb16fdab1ee5caf7470a86f842dfe5fea945d33a4

orphan: Add orphan utility

Diffstat

M CMakeLists.txt | 1 +
M README.md | 1 +
A man/orphan.1 | 35 +++++++++++++++++++++++++++++++++++
A src/orphan.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

4 files changed, 128 insertions, 0 deletions

diff --git a/CMakeLists.txt b/CMakeLists.txt
index ce2b49b..f5968c6 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -30,6 +30,7 @@ ADD_EXECUTABLE(link     ${PROJECT_SOURCE_DIR}/src/link.c)
 ADD_EXECUTABLE(logname  ${PROJECT_SOURCE_DIR}/src/logname.c)
 ADD_EXECUTABLE(mkdir    ${PROJECT_SOURCE_DIR}/src/mkdir.c)
 ADD_EXECUTABLE(nice     ${PROJECT_SOURCE_DIR}/src/nice.c)
+ADD_EXECUTABLE(orphan   ${PROJECT_SOURCE_DIR}/src/orphan.c)
 ADD_EXECUTABLE(od       ${PROJECT_SOURCE_DIR}/src/od.c)
 ADD_EXECUTABLE(pwd      ${PROJECT_SOURCE_DIR}/src/pwd.c)
 ADD_EXECUTABLE(rand     ${PROJECT_SOURCE_DIR}/src/rand.c)
diff --git a/README.md b/README.md
index 153b3e4..cfc5002 100644
--- a/README.md
+++ b/README.md
@@ -22,6 +22,7 @@ UNIX-like systems.
 | logname          | Return the user's login name             | POSIX    |
 | mkdir            | Make directories                         | POSIX    |
 | nice             | Execute with an altered nice value       | POSIX    |
+| orphan           | Execute commands as orphans              |          |
 | od\*             | Dump files in various formats            | POSIX    |
 | pwd              | Print working directory                  | POSIX    |
 | rand             | Generate random string                   |          |
diff --git a/man/orphan.1 b/man/orphan.1
new file mode 100644
index 0000000..32007d2
--- /dev/null
+++ b/man/orphan.1
@@ -0,0 +1,35 @@
+.TH ORPHAN 1 2020-08-05 "OMKOV coreutils" "General Commands Manual"
+.SH NAME
+orphan \(em execute commands as orphans
+.SH SYNOPSYS
+\fBorphan\fR \fIcommand\fR [\fIargument\fR...]
+.SH DESCRIPTION
+Execute \fIcommand\fR as an orphaned process.
+.SH OPTIONS
+The following options are supported:
+.TP
+.B --help
+Display help message.
+.TP
+.B --version
+Display version message.
+.SH OPERANDS
+The following operands are supported:
+.TP
+.I command
+A command to execute with the altered nice value.
+.TP
+.I argument
+An argument to pass to \fIcommand\fR.
+.SH EXIT STATUS
+If \fIcommand\fR is specified, \fIorphan\fR will return a zero exit status.
+Otherwise, the following exit values will be returned:
+.TP
+1-125
+An error occurred.
+.SH COPYRIGHT
+.nf
+Copyright (C) 2020, Jakob Wakeling
+All rights reserved.
+OMKOV Permissive Licence (https://www.omkov.net/OLPE)
+.fi
diff --git a/src/orphan.c b/src/orphan.c
new file mode 100644
index 0000000..bca633d
--- /dev/null
+++ b/src/orphan.c
@@ -0,0 +1,91 @@
+// orphan.c, version 1.0.0
+// OMKOV coreutils orphan
+// 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 "lib/error.h"
+#include "optget.h"
+
+#include <fcntl.h>
+#include <unistd.h>
+
+#include <errno.h>
+#include <stdio.h>
+
+#define VERSION "1.0.0"
+
+static void hlp(void);
+static void ver(void);
+
+int main(int ac, char *av[]) { A0 = av[0];
+	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, av, 0)) != -1) switch (o) {
+	case 256: { hlp(); return 0; }
+	case 257: { ver(); return 0; }
+	default: { return 1; }
+	}
+	
+	if (opt.ind == ac) { error(1, "missing operand"); }
+	
+	switch (fork()) {
+	case -1: { error(1, "%s", serr()); }
+	case  0: {
+		execvp(av[opt.ind], &av[opt.ind]);
+		warn("%s: %s", av[opt.ind], serr());
+	}
+	default: { return 0; }
+	}
+}
+
+static void hlp(void) {
+	puts("orphan - execute commands as orphans\n");
+	puts("usage: orphan command [argument...]\n");
+	puts("options:");
+	puts("  --help     Display help information");
+	puts("  --version  Display version information");
+	return;
+}
+
+static void ver(void) {
+	puts("OMKOV cryptutils orphan, version " VERSION);
+	puts("Copyright (C) 2020, Jakob Wakeling");
+	puts("All rights reserved.");
+	puts("OMKOV Permissive Licence (https://www.omkov.net/OLPE)");
+	return;
+}