From c9b6414f78d90990c13969883c1c0f9fd787128c Mon Sep 17 00:00:00 2001 From: Pin Date: Wed, 30 Mar 2022 20:08:02 -0400 Subject: [PATCH] minor changed --- msh.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/msh.c b/msh.c index 3df6abc..7b8de98 100644 --- a/msh.c +++ b/msh.c @@ -58,7 +58,6 @@ int runCommand(char *command) { pid = fork(); if (pid == 0) { // Child process - if(execlp("bash", "bash", "-c", command, NULL) == -1) { exit(EXIT_FAILURE); } @@ -90,6 +89,9 @@ void initShell() { clear(); using_history(); + // Setting random time seed + srand(time(0)); + runCommand("uname -a"); fd = fopen("/etc/motd", "r"); @@ -131,7 +133,7 @@ char *getPrompt() { dup2(output.write, STDOUT_FILENO); close(STDERR_FILENO); // Do not print errors to the screen - execlp("bash", "bash", "-i", "-c", "echo \"${PS1@P}\" || echo \">>\"", NULL); + execlp("bash", "bash", "-i", "-c", "echo \"${PS1@P}\"", NULL); } else if (pid < 0) { // Error forking printf("Error forking\n"); } else { // Parent process @@ -150,6 +152,7 @@ char *getPrompt() { do { waitpid(pid, &status, WUNTRACED); } while(!WIFEXITED(status) && !WIFSIGNALED(status)); + printf("Status: %d\n", status); } return prompt; @@ -170,8 +173,10 @@ int mathTest() { int numAnswer = 0; int num1 = randomNum(10); int num2 = randomNum(10); + char *numLine = NULL; printf("%d + %d = ", num1, num2); - scanf("%d", &numAnswer); + numLine = readline(""); + sscanf(numLine, "%d", &numAnswer); #ifdef RELEASEBUILD printf("Calculating"); @@ -186,6 +191,8 @@ int mathTest() { if ((num1 + num2) != numAnswer) { // If wrong answer status = 1; } + + free(numLine); return status; } @@ -208,12 +215,12 @@ void startShell() { } } -void handleBreak(int sig){ - signal(sig, handleBreak); - fflush(stdout); +void handleBreak(){ + signal(SIGINT, handleBreak); printf("\n"); - startShell(); - exit(EXIT_SUCCESS); + rl_on_new_line(); + rl_replace_line("", 0); + rl_redisplay(); } int main() {