diff options
author | makefunstuff <[email protected]> | 2024-07-13 22:04:48 +0200 |
---|---|---|
committer | makefunstuff <[email protected]> | 2024-07-13 22:04:48 +0200 |
commit | 714036825c38be947eb88a82c02425d7932d2919 (patch) | |
tree | c6723bd239fa5cc93b78ef9e9b7ba5576f708db1 /csrc/sh.c | |
parent | 9b9e63476616a123eb4d00f87f677ef2d9478897 (diff) | |
download | tinkerbunk-714036825c38be947eb88a82c02425d7932d2919.tar.gz |
upd
Diffstat (limited to '')
-rw-r--r-- | csrc/sh.c | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/csrc/sh.c b/csrc/sh.c new file mode 100644 index 0000000..7372d2d --- /dev/null +++ b/csrc/sh.c @@ -0,0 +1,53 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/types.h> +#include <sys/wait.h> +#include <unistd.h> + +#define MAXLINE 80 + +int shell() { + char line[MAXLINE]; + char *args[MAXLINE / 2 + 1]; + int should_run = 1; + + while (should_run) { + printf(">"); + fflush(stdout); + + if (!fgets(line, MAXLINE, stdin)) { + break; + } + + line[strlen(line) - 1] = '\0'; + + int i = 0; + args[i] = strtok(line, " "); + while (args[i] != NULL) { + i++; + args[i] = strtok(NULL, " "); + } + + if (args[0] == NULL) { + continue; + } + + if (strncmp(args[0], "exit", 4) == 0) { + break; + } + + pid_t pid = fork(); + if (pid < 0) { + perror("fork"); + return 1; + } else if (pid == 0) { + if (execvp(args[0], args) == -1) { + perror("execvp"); + } + return 1; + } else { + wait(NULL); + } + } +} |