From 714036825c38be947eb88a82c02425d7932d2919 Mon Sep 17 00:00:00 2001 From: makefunstuff Date: Sat, 13 Jul 2024 23:04:48 +0300 Subject: upd --- csrc/sh.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 csrc/sh.c (limited to 'csrc/sh.c') 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 +#include +#include +#include +#include +#include + +#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); + } + } +} -- cgit 1.4.1-2-gfad0