minor changed
This commit is contained in:
23
msh.c
23
msh.c
@@ -58,7 +58,6 @@ int runCommand(char *command) {
|
|||||||
|
|
||||||
pid = fork();
|
pid = fork();
|
||||||
if (pid == 0) { // Child process
|
if (pid == 0) { // Child process
|
||||||
|
|
||||||
if(execlp("bash", "bash", "-c", command, NULL) == -1) {
|
if(execlp("bash", "bash", "-c", command, NULL) == -1) {
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
@@ -90,6 +89,9 @@ void initShell() {
|
|||||||
clear();
|
clear();
|
||||||
using_history();
|
using_history();
|
||||||
|
|
||||||
|
// Setting random time seed
|
||||||
|
srand(time(0));
|
||||||
|
|
||||||
runCommand("uname -a");
|
runCommand("uname -a");
|
||||||
|
|
||||||
fd = fopen("/etc/motd", "r");
|
fd = fopen("/etc/motd", "r");
|
||||||
@@ -131,7 +133,7 @@ char *getPrompt() {
|
|||||||
dup2(output.write, STDOUT_FILENO);
|
dup2(output.write, STDOUT_FILENO);
|
||||||
close(STDERR_FILENO); // Do not print errors to the screen
|
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
|
} else if (pid < 0) { // Error forking
|
||||||
printf("Error forking\n");
|
printf("Error forking\n");
|
||||||
} else { // Parent process
|
} else { // Parent process
|
||||||
@@ -150,6 +152,7 @@ char *getPrompt() {
|
|||||||
do {
|
do {
|
||||||
waitpid(pid, &status, WUNTRACED);
|
waitpid(pid, &status, WUNTRACED);
|
||||||
} while(!WIFEXITED(status) && !WIFSIGNALED(status));
|
} while(!WIFEXITED(status) && !WIFSIGNALED(status));
|
||||||
|
printf("Status: %d\n", status);
|
||||||
}
|
}
|
||||||
|
|
||||||
return prompt;
|
return prompt;
|
||||||
@@ -170,8 +173,10 @@ int mathTest() {
|
|||||||
int numAnswer = 0;
|
int numAnswer = 0;
|
||||||
int num1 = randomNum(10);
|
int num1 = randomNum(10);
|
||||||
int num2 = randomNum(10);
|
int num2 = randomNum(10);
|
||||||
|
char *numLine = NULL;
|
||||||
printf("%d + %d = ", num1, num2);
|
printf("%d + %d = ", num1, num2);
|
||||||
scanf("%d", &numAnswer);
|
numLine = readline("");
|
||||||
|
sscanf(numLine, "%d", &numAnswer);
|
||||||
|
|
||||||
#ifdef RELEASEBUILD
|
#ifdef RELEASEBUILD
|
||||||
printf("Calculating");
|
printf("Calculating");
|
||||||
@@ -186,6 +191,8 @@ int mathTest() {
|
|||||||
if ((num1 + num2) != numAnswer) { // If wrong answer
|
if ((num1 + num2) != numAnswer) { // If wrong answer
|
||||||
status = 1;
|
status = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
free(numLine);
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -208,12 +215,12 @@ void startShell() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void handleBreak(int sig){
|
void handleBreak(){
|
||||||
signal(sig, handleBreak);
|
signal(SIGINT, handleBreak);
|
||||||
fflush(stdout);
|
|
||||||
printf("\n");
|
printf("\n");
|
||||||
startShell();
|
rl_on_new_line();
|
||||||
exit(EXIT_SUCCESS);
|
rl_replace_line("", 0);
|
||||||
|
rl_redisplay();
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
|||||||
Reference in New Issue
Block a user