From fc57be32ebb21277a51b07a0b1b698c5ee3c8eee Mon Sep 17 00:00:00 2001 From: aaron Date: Tue, 12 May 2026 14:21:31 +0200 Subject: [PATCH] wrote a makefile and added background job support --- main.c | 21 +++++++++++++++++++-- makefile | 9 +++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 makefile diff --git a/main.c b/main.c index 6ea9f13..94a83c5 100644 --- a/main.c +++ b/main.c @@ -5,6 +5,19 @@ #include #include #include +#include +void handleSIGINT(int sig) { + printf("\n"); +} +int detectBackgrount(char **args) { + for (int i = 0; args[i] != NULL; i++) { + if (strcmp(args[i], "&") == 0) { + args[i] = NULL; + return 0; + } + } + return 1; +} int seperatePipe(char **args) { for(int i = 0; args[i] != NULL; i++) { if (strcmp(args[i], "|") == 0) return i; @@ -22,6 +35,7 @@ void executePipe(char **args, int pipeIndex) { dup2(fd[1], STDOUT_FILENO); close(fd[0]); close(fd[1]); + signal(SIGINT, SIG_DFL); execvp(cmd1[0], cmd1); perror("tinysh"); exit(1); @@ -31,6 +45,7 @@ void executePipe(char **args, int pipeIndex) { dup2(fd[0], STDIN_FILENO); close(fd[0]); close(fd[1]); + signal(SIGINT, SIG_DFL); execvp(cmd2[0], cmd2); perror("tinysh"); exit(1); @@ -62,13 +77,14 @@ char **parse(char *line) { return args; } int main() { + signal(SIGINT, handleSIGINT); while(1) { char *user = getenv("USER"); char dir[1024]; getcwd(dir, sizeof(dir)); char host[1024]; gethostname(host, sizeof(host)); - char prompt[2048]; + char prompt[2051]; snprintf(prompt, sizeof(prompt), "%s@%s %s$ ", user, host, dir); char *line = readline(prompt); if(line == NULL) return 0; @@ -76,6 +92,7 @@ int main() { char **parsed = parse(line); if(parsed[0] == NULL) { free(parsed); free(line); continue; } if(handleBuiltins(parsed)) { free(line); free(parsed); continue; } + int background = detectBackgrount(parsed); int pipeIndex = seperatePipe(parsed); if (pipeIndex != -1) { executePipe(parsed, pipeIndex); @@ -89,7 +106,7 @@ int main() { perror("tinysh"); exit(1); } - } else if(pid > 0) { + } else if(pid > 0 && background != 0) { wait(NULL); } free(parsed); diff --git a/makefile b/makefile new file mode 100644 index 0000000..e64d1f2 --- /dev/null +++ b/makefile @@ -0,0 +1,9 @@ +CC = gcc +CFLAGS = -Wall +LIBS = -lreadline + +tinysh: main.c + $(CC) $(CFLAGS) main.c -o tinysh $(LIBS) + +clean: + rm -f tinysh