minor changed
This commit is contained in:
33
msh.c
33
msh.c
@@ -54,22 +54,10 @@ void failedTest() {
|
||||
|
||||
int runCommand(char *command) {
|
||||
int status = 0;
|
||||
union pipe input, output;
|
||||
FILE *inputFileD, *outputFileD;
|
||||
char *line = NULL;
|
||||
size_t lineCap = 0;
|
||||
pid_t pid;
|
||||
|
||||
pipe(input.fileDesc);
|
||||
pipe(output.fileDesc);
|
||||
|
||||
pid = fork();
|
||||
if (pid == 0) { // Child process
|
||||
close(input.write);
|
||||
close(output.read);
|
||||
|
||||
dup2(input.read, STDIN_FILENO);
|
||||
dup2(output.write, STDOUT_FILENO);
|
||||
|
||||
if(execlp("bash", "bash", "-c", command, NULL) == -1) {
|
||||
exit(EXIT_FAILURE);
|
||||
@@ -79,29 +67,16 @@ int runCommand(char *command) {
|
||||
printf("Error forking...\n");
|
||||
status = 1;
|
||||
} else { // Parent process
|
||||
close(input.read);
|
||||
close(output.write);
|
||||
inputFileD = fdopen(input.write, "w");
|
||||
outputFileD = fdopen(output.read, "r");
|
||||
|
||||
while (getline(&line, &lineCap, outputFileD) > 0) {
|
||||
printf("%s", line);
|
||||
}
|
||||
|
||||
free(line);
|
||||
|
||||
do {
|
||||
waitpid(pid, &status, WUNTRACED);
|
||||
} while(!WIFEXITED(status) && !WIFSIGNALED(status));
|
||||
fclose(inputFileD);
|
||||
fclose(outputFileD);
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
int execCommand(char *command) {
|
||||
for (int i = 0; i < (sizeof(builtinFunctions) / sizeof(char *)); i++) {
|
||||
for (size_t i = 0; i < (sizeof(builtinFunctions) / sizeof(char *)); i++) {
|
||||
if (strcmp(command, builtinFunctions[i]) == 0) {
|
||||
return (*builtinFunc[i])(command);
|
||||
}
|
||||
@@ -139,6 +114,7 @@ char *getPrompt() {
|
||||
union pipe input, output;
|
||||
FILE *outputFileD;
|
||||
size_t len = 0;
|
||||
int status;
|
||||
|
||||
static char *prompt = NULL;
|
||||
static size_t promptCap = 0;
|
||||
@@ -171,6 +147,9 @@ char *getPrompt() {
|
||||
}
|
||||
|
||||
fclose(outputFileD);
|
||||
do {
|
||||
waitpid(pid, &status, WUNTRACED);
|
||||
} while(!WIFEXITED(status) && !WIFSIGNALED(status));
|
||||
}
|
||||
|
||||
return prompt;
|
||||
@@ -196,7 +175,7 @@ int mathTest() {
|
||||
|
||||
#ifdef RELEASEBUILD
|
||||
printf("Calculating");
|
||||
for (int i = 0; i < 15; i++){
|
||||
for (int i = 0; i < 3; i++){
|
||||
printf(".");
|
||||
fflush(stdout);
|
||||
sleep(2);
|
||||
|
||||
Reference in New Issue
Block a user