diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2023-02-08 13:19:56 +0100 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2023-02-08 13:19:56 +0100 |
| commit | d7229970f8910aa756d299f8bdceee7f4d1fbfbc (patch) | |
| tree | a95ff2e6bd50578d34d6449c9fde173acc0ef59c /core | |
| parent | e947b1459f93f11ad91f3e920676fe1b828281e7 (diff) | |
add env and make smash be able to parse files with comments
Diffstat (limited to 'core')
| -rw-r--r-- | core/env.c | 81 | ||||
| -rw-r--r-- | core/powerctl.c | 29 | ||||
| -rw-r--r-- | core/test_env | 1 |
3 files changed, 111 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; +} diff --git a/core/powerctl.c b/core/powerctl.c new file mode 100644 index 0000000..cf887bf --- /dev/null +++ b/core/powerctl.c @@ -0,0 +1,29 @@ +#include "../lib/sys/reboot.h" +#include "../lib/cstr/cstr.h" +#include "../lib/io/io.h" +#include "../lib/sys/sync.h" + +int main(int argc, char **argv) { + if (argc != 2) { + wf(STDERR_FD, "powerctl [poweroff|reboot|halt|hard-reset|suspend]\n"); + return -1; + } + + char *command = argv[1]; + + sync(); + + if (cstr_compare(command, "poweroff")) { + reboot(REBOOT_POWEROFF); + } else if (cstr_compare(command, "reboot")) { + reboot(REBOOT_RESTART); + } else if (cstr_compare(command, "halt")) { + reboot(REBOOT_HALT_SYSTEM); + } else if (cstr_compare(command, "hard-reset")) { + reboot(REBOOT_HARD_RESET); + } else if (cstr_compare(command, "suspend")) { + reboot(REBOOT_SUSPEND); + } + + return -1; +} diff --git a/core/test_env b/core/test_env new file mode 100644 index 0000000..561cdd6 --- /dev/null +++ b/core/test_env @@ -0,0 +1 @@ +TEST=TEST |