minor changed
This commit is contained in:
23
msh.c
23
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() {
|
||||
|
||||
Reference in New Issue
Block a user