From 778b24d74f4763f46ea874a6777e39b794006c44 Mon Sep 17 00:00:00 2001 From: WlodekM Date: Fri, 14 Feb 2025 19:35:57 +0200 Subject: smolshel - works --- progs/shel/shel.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) (limited to 'progs') diff --git a/progs/shel/shel.c b/progs/shel/shel.c index f375250..e3221f4 100644 --- a/progs/shel/shel.c +++ b/progs/shel/shel.c @@ -1,34 +1,48 @@ #include #include +#include #include #include #include -void parseInput(char *input, char *command, char *argv) { +void parseInput(char *input, char *command, char **argv) { bool reachedArgv = false; char arg[32] = {0}; int idx = 0; int argvIdx = 1; - for (int i = 0; i < sizeof(input); i++) + for (int i = 0; i < strlen(input); i++) { + char *a = {&(input[i])}; if (reachedArgv) { + if (input[i] == ' ') { + arg[idx] = '\0'; + argv[argvIdx] = strdup(arg); + memset(arg, 0, sizeof(arg)); + argvIdx++; + idx = 0; + continue; + } arg[idx] = input[i]; idx++; continue; } if (input[i] == ' ') { - arg[idx] == '\0'; + arg[idx] = '\0'; strcpy(command, arg); reachedArgv = true; memset(arg, 0, sizeof(arg)); - idx++; + idx = 0; continue; } arg[idx] = input[i]; idx++; } - + argv[0] = command; + if (reachedArgv) { + arg[idx] = '\0'; + argv[argvIdx] = strdup(arg); + } } int main() { @@ -38,5 +52,7 @@ int main() { char *env[32] = {"PATH=/bin"}; char path[128] = "/"; + strcpy(input, "/bin/echo meow"); parseInput(input, command, argv); + execve(command, argv, 0); } \ No newline at end of file -- cgit 1.4.1-2-gfad0