minor changed

This commit is contained in:
Pin
2022-03-30 18:43:40 -04:00
parent 348ead46f9
commit 31ce16def6

33
msh.c
View File

@@ -54,22 +54,10 @@ void failedTest() {
int runCommand(char *command) { int runCommand(char *command) {
int status = 0; int status = 0;
union pipe input, output;
FILE *inputFileD, *outputFileD;
char *line = NULL;
size_t lineCap = 0;
pid_t pid; pid_t pid;
pipe(input.fileDesc);
pipe(output.fileDesc);
pid = fork(); pid = fork();
if (pid == 0) { // Child process 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) { if(execlp("bash", "bash", "-c", command, NULL) == -1) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@@ -79,29 +67,16 @@ int runCommand(char *command) {
printf("Error forking...\n"); printf("Error forking...\n");
status = 1; status = 1;
} else { // Parent process } 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 { do {
waitpid(pid, &status, WUNTRACED); waitpid(pid, &status, WUNTRACED);
} while(!WIFEXITED(status) && !WIFSIGNALED(status)); } while(!WIFEXITED(status) && !WIFSIGNALED(status));
fclose(inputFileD);
fclose(outputFileD);
} }
return status; return status;
} }
int execCommand(char *command) { 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) { if (strcmp(command, builtinFunctions[i]) == 0) {
return (*builtinFunc[i])(command); return (*builtinFunc[i])(command);
} }
@@ -139,6 +114,7 @@ char *getPrompt() {
union pipe input, output; union pipe input, output;
FILE *outputFileD; FILE *outputFileD;
size_t len = 0; size_t len = 0;
int status;
static char *prompt = NULL; static char *prompt = NULL;
static size_t promptCap = 0; static size_t promptCap = 0;
@@ -171,6 +147,9 @@ char *getPrompt() {
} }
fclose(outputFileD); fclose(outputFileD);
do {
waitpid(pid, &status, WUNTRACED);
} while(!WIFEXITED(status) && !WIFSIGNALED(status));
} }
return prompt; return prompt;
@@ -196,7 +175,7 @@ int mathTest() {
#ifdef RELEASEBUILD #ifdef RELEASEBUILD
printf("Calculating"); printf("Calculating");
for (int i = 0; i < 15; i++){ for (int i = 0; i < 3; i++){
printf("."); printf(".");
fflush(stdout); fflush(stdout);
sleep(2); sleep(2);