coreutils

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

AuthorJamozed <[email protected]>
Date2020-06-26 13:05:32
Commite2c696c1f1838c70ccebbe823e86277349a6736c
Parenta6ffac50f36b372d8c88ab9b1ce6d07b5bc33f24

env: Add POSIX env

Diffstat

M CMakeLists.txt | 1 +
M README.md | 1 +
A man/env.1 | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
A src/env.c | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

4 files changed, 152 insertions, 0 deletions

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 673c463..5d26e01 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -14,6 +14,7 @@ ADD_EXECUTABLE(basename ${CMAKE_SOURCE_DIR}/src/basename.c)
 ADD_EXECUTABLE(cat      ${CMAKE_SOURCE_DIR}/src/cat.c)
 ADD_EXECUTABLE(dirname  ${CMAKE_SOURCE_DIR}/src/dirname.c)
 ADD_EXECUTABLE(echo     ${CMAKE_SOURCE_DIR}/src/echo.c)
+ADD_EXECUTABLE(env      ${CMAKE_SOURCE_DIR}/src/env.c)
 ADD_EXECUTABLE(false    ${CMAKE_SOURCE_DIR}/src/false.c)
 ADD_EXECUTABLE(link     ${CMAKE_SOURCE_DIR}/src/link.c)
 ADD_EXECUTABLE(logname  ${CMAKE_SOURCE_DIR}/src/logname.c)
diff --git a/README.md b/README.md
index 03d2bcb..86e304f 100644
--- a/README.md
+++ b/README.md
@@ -13,6 +13,7 @@ UNIX-like systems.
 | cat              | Concatenate and print files              | POSIX    |
 | dirname          | Return the directory portion of a path   | POSIX    |
 | echo             | Write arguments to standard output       | POSIX    |
+| env              | Execute with an altered enviroment       | POSIX    |
 | false            | Return false value                       | POSIX    |
 | link             | Create a link to a file                  | POSIX    |
 | logname          | Return the user's login name             | POSIX    |
diff --git a/man/env.1 b/man/env.1
new file mode 100644
index 0000000..ad12b42
--- /dev/null
+++ b/man/env.1
@@ -0,0 +1,55 @@
+.TH ENV 1 2020-06-27 "OMKOV coreutils" "General Commands Manual"
+.SH NAME
+env \(em execute with an altered enviroment
+.SH SYNOPSYS
+\fBenv\fR [-i] [\fIname\fR=\fIvalue\fR]... [\fIcommand\fR [\fIargument\fR...]]
+.SH DESCRIPTION
+Execute \fIcommand\fR with in an enviroment altered with the specified
+\fIname\fR=\fIvalue\fR pairs. If no \fIcommand\fR is given, the enviroment will
+be output with one variable per line.
+.SH OPTIONS
+The following options are supported:
+.TP
+.B -i
+Ignore inherited enviroment.
+.TP
+.B --help
+Display help information.
+.TP
+.B --version
+Display version information.
+.SH OPERANDS
+The following operands are supported:
+.TP
+.I name\fR=\fIvalue
+A variable to be added to the execution enviroment.
+.TP
+.I command
+A command to execute with the altered enviroment.
+.TP
+.I argument
+An argument to pass to \fIcommand\fR.
+.SH EXIT STATUS
+Upon successful completion, the exit status of env shall be the exit status of
+\fIcommand\fR. Otherwise, the following exit values will be returned:
+.TP
+\ \ \ \ 0
+Successful completion.
+.TP
+1-125
+An error occurred.
+.TP
+\ \ 126
+\fIcommand\fR was found but could not be invoked.
+.TP
+\ \ 127
+\fIcommand\fR was not found.
+.SH STANDARDS
+The \fIenv\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/env.c b/src/env.c
new file mode 100644
index 0000000..b525f3f
--- /dev/null
+++ b/src/env.c
@@ -0,0 +1,95 @@
+// env.c, version 1.0.0
+// OMKOV coreutils implementation of POSIX env
+// 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 <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+extern char **environ;
+
+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 = "i"; opt.lops = lops; int o;
+	while((o = optget(&opt, argv, 0)) != -1) switch (o) {
+	case 'i': { environ = NULL; break; }
+	case 256: { help(); return 0; }
+	case 257: { version(); return 0; }
+	default: { return 1; }
+	}
+	
+	register char **p = &argv[opt.ind];
+	for (; *p && strchr(*p, '=') != NULL; ++p) { putenv(*p); }
+	
+	if (!*p) { if (environ) for (char **v = environ; *v; ++v) {
+		fputs(*v, stdout); fputc('\n', stdout);
+	} return 0; }
+	
+	execvp(*p, &*p);
+	
+	warn("%s: %s: %s", argv[0], *p, serrno);
+	return errno = ENOENT ? 127 : 126;
+}
+
+static void help(void) {
+	puts("env - execute with an altered enviroment\n");
+	puts("usage: env [-i] [name=value]... [command [argument...]]\n");
+	puts("options:");
+	puts("  -i         Ignore inherited enviroment");
+	puts("  --help     Display help information");
+	puts("  --version  Display version information");
+	return;
+}
+
+static void version() {
+	puts("OMKOV coreutils env, version 1.0.0");
+	puts("Copyright (C) 2020, Jakob Wakeling");
+	puts("All rights reserved.");
+	puts("OMKOV Permissive Licence (https://www.omkov.net/OLPE)");
+	return;
+}