codecrafters-shell-c

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit 8225c3a8bb79fe3309b6e87f52ce2c7f21f5641d
parent 814901f0dd9151beec25ba3624259487cb8ab8e6
Author: 5hif7y <[email protected]>
Date:   Mon, 30 Dec 2024 17:30:59 -0300

4 REPL

Diffstat:
Mapp/main.c | 37+++++++++++++++++++++++++++----------
1 file changed, 27 insertions(+), 10 deletions(-)

diff --git a/app/main.c b/app/main.c @@ -1,18 +1,35 @@ #include <stdio.h> #include <string.h> +#include <stdbool.h> int main() { - // Flush after every printf - setbuf(stdout, NULL); - // Uncomment this block to pass the first stage - printf("$ "); - - // Wait for user input + // Process user input + bool running = true; char input[100]; - fgets(input, 100, stdin); - // task 3 - input[strlen(input) - 1] = '\0'; - printf("%s: command not found\n", input); + + while(running){ + // Flush after every printf + setbuf(stdout, NULL); + printf("$ "); + + // Exit on error + if(fgets(input, sizeof(input), stdin) == NULL){ + running = false; + } + + // remove \n + int n = strlen(input); + if(n > 0 && input[n - 1] == '\n'){ + input[n - 1] = '\0'; + } + // Evaluate commands: + if(!strcmp(input, "exit")){ + printf("Exiting...\n"); + running = false; + } else { + printf("%s: command not found\n", input); + } + } return 0; }