aboutsummaryrefslogtreecommitdiff
path: root/core/env.c
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2023-02-08 13:19:56 +0100
committerNathan Reiner <nathan@nathanreiner.xyz>2023-02-08 13:19:56 +0100
commitd7229970f8910aa756d299f8bdceee7f4d1fbfbc (patch)
treea95ff2e6bd50578d34d6449c9fde173acc0ef59c /core/env.c
parente947b1459f93f11ad91f3e920676fe1b828281e7 (diff)
add env and make smash be able to parse files with comments
Diffstat (limited to 'core/env.c')
-rw-r--r--core/env.c81
1 files changed, 81 insertions, 0 deletions
diff --git a/core/env.c b/core/env.c
new file mode 100644
index 0000000..e96be59
--- /dev/null
+++ b/core/env.c
@@ -0,0 +1,81 @@
+#include "../lib/io/io.h"
+#include "../lib/sys/sizes.h"
+#include "../lib/sys/execve.h"
+#include "../lib/env/env.h"
+
+#define BUFSIZ 1024
+char buf[BUFSIZ] = "";
+
+
+void add_to_env(char *line, u64 linenumber)
+{
+ char *key;
+ char *value;
+
+ key = line;
+
+ while (*line != '=' && *line) ++line;
+
+ if (!*line) {
+ wff(STDERR_FD, "error: %i no valid environment line\n", linenumber);
+ return;
+ }
+
+ *(line++) = 0;
+ value = line;
+
+ while (*line != '\n' && *line) ++line;
+
+ if (*line == '\n') *line = 0;
+
+ setenv(key, value);
+}
+
+
+int main(int argc, const char **argv)
+{
+ if (argc == 1) {
+ const char**envp = getenvp();
+
+ while (*envp) {
+ wstdf("%s\n", *envp);
+ ++envp;
+ }
+
+ return 0;
+ }
+
+ u64 fd = STDIN_FD;
+ u64 size;
+ char *p;
+ u64 linenumber = 1;
+ const char **execargv = argv + 1;
+
+ if (argv[1][0] == '-' && argv[1][1] == 'f') {
+ if (argc > 3) {
+ fd = open(argv[2], OPEN_READ_ONLY, 0);
+ if (fd < 0) {
+ wff(STDERR_FD, "venv: cannot open file '%s'\n", argv[2]);
+ return -1;
+ }
+ execargv = argv + 3;
+ } else {
+ wf(STDERR_FD, "venv [-f file] command [args...]\n");
+ return -1;
+ }
+ }
+
+ while ((size = read(fd, buf, BUFSIZ - 1))) {
+ p = buf;
+
+ while (p < buf + BUFSIZ && *p != '\n') ++p;
+ *(++p) = 0;
+
+ add_to_env(buf, linenumber++);
+ }
+
+ execve(execargv[0], execargv, getenvp());
+
+ wff(STDERR_FD, "cannot execute '%s'\n", execargv[0]);
+ return -1;
+}