Commit 1dd6e827 authored by Lindsay Knupp's avatar Lindsay Knupp
Browse files

Lab 9.3 completed

parent fe06e2d0
/* Lindsay Knupp /* Lindsay Knupp
* 11/16/21 * 11/16/21
* Lab 9 - ishell.c * Lab 9 - commandishell.c
*/ */
#include <stdlib.h> #include <stdlib.h>
...@@ -12,27 +12,36 @@ ...@@ -12,27 +12,36 @@
#include <sys/wait.h> #include <sys/wait.h>
#include <signal.h> #include <signal.h>
#include "ishell.h"
#define MAXCNT 10 #define MAXCNT 10
#define MAXLEN 20 #define MAXLEN 20
int main (int argc, char *argv[]){ int main (int argc, char *argv[]){
pid_t pid; pid_t pid;
pid_t pid2;
int status; int status;
int status2;
int token_count2;
char path[1024]; char path[1024];
//char path2[1024]; char path2[1024];
char* saveptr; char* saveptr;
char* str; char* str;
char* token; char* token;
char* semitokens[MAXCNT];
char* tokens[MAXCNT]; char* tokens[MAXCNT];
//char* tokens1[MAXCNT]; char* tokens2[MAXCNT];
//char* tokens2[MAXCNT];
char* line = NULL; char* line = NULL;
size_t length; size_t length;
// Ask user for input
while(1){ while(1){
printf("ishell> "); printf("ishell> ");
strcpy(path,"/usr/bin/");
strcpy(path2,"/usr/bin/");
line = NULL; line = NULL;
...@@ -40,77 +49,70 @@ int main (int argc, char *argv[]){ ...@@ -40,77 +49,70 @@ int main (int argc, char *argv[]){
getline(&line,&length,stdin); getline(&line,&length,stdin);
length = strlen(line); length = strlen(line);
line[length-1] = 0; line[length-1] = 0;
// initialize arrays for clean strings
for (int i = 0; i<MAXCNT; i++){
semitokens[i] = (char*)malloc(MAXLEN);
}
for (int i = 0; i<MAXCNT; i++){ for (int i = 0; i<MAXCNT; i++){
tokens[i] = (char*)malloc(MAXLEN); tokens[i] = (char*)malloc(MAXLEN);
} }
int token_count = 0;
// split command(s) by semicolon
int semi_token_count = 0;
for (str = line; ; str = NULL){ for (str = line; ; str = NULL){
token = strtok_r(str,";", &saveptr);
if(semi_token_count >= MAXCNT || token == NULL){
break;
}
strcpy(semitokens[semi_token_count],token);
semi_token_count ++;
}
semitokens[semi_token_count] = NULL;
// split first command by spaces
int token_count = 0;
for (str = semitokens[0]; ; str = NULL){
token = strtok_r(str," ", &saveptr); token = strtok_r(str," ", &saveptr);
if(token_count >= MAXCNT || token == NULL){ if(token_count >= MAXCNT || token == NULL){
break; break;
} }
strcpy(tokens[token_count],token); strcpy(tokens[token_count],token);
//tokens[token_count] = token;
token_count ++; token_count ++;
} }
tokens[token_count] = NULL; tokens[token_count] = NULL;
strcat(path, tokens[0]);
//int command2 = 0;
//for (int i = 0; i<MAXCNT; i++){
// if(strcmp(tokens[i],";") == 0){
// command2 = i;
// break;
// }
//}
//printf("command index %d\n",command2);
//if (command2 != 0){ // if there are two commands
//for (int i = 0; i < command2;i++){ if (semi_token_count == 2){
// strcpy(tokens1[i],tokens[i]);
//}
//tokens1[command2] = NULL;
//for (int i = command2;i<token_count;i++){
// strcpy(tokens2[i],tokens[i]);
//}
//tokens2[token_count] = NULL;
//strcpy(path2, "/usr/bin/");
//strcpy(path, "/usr/bin/");
//strcat(path, tokens1[0]); // initalize new empty token list
//strcat(path, tokens2[0]); for (int i = 0; i<MAXCNT; i++){
//} tokens2[i] = (char*)malloc(MAXLEN);
}
strcpy(path,"/usr/bin/");
// for (int i = 0; i< token_count;i++){
// strcat(path,tokens[i]);
// if (i != (token_count-1)){
// strcat(path," ");
// }
// printf("token: %s\n",tokens[i]);
// }
strcat(path,tokens[0]);
//printf("path %s\n",path);
for (int i = 0;i<token_count;i++){ // split second command by spaces
//printf("token: %s\n",tokens[i]); token_count2 = 0;
for (str = semitokens[1]; ; str = NULL){
token = strtok_r(str," ", &saveptr);
if(token_count2 >= MAXCNT || token == NULL){
break;
}
strcpy(tokens2[token_count2],token);
token_count2 ++;
}
tokens2[token_count2] = NULL;
strcat(path2,tokens2[0]);
} }
// clear line for repeated user input
free(line); free(line);
// fork child to run command
if ((pid = fork()) == -1){ if ((pid = fork()) == -1){
perror("Fork() failure"); perror("Fork() failure");
exit(-1); exit(-1);
...@@ -135,7 +137,33 @@ int main (int argc, char *argv[]){ ...@@ -135,7 +137,33 @@ int main (int argc, char *argv[]){
for(int i = 0; i<token_count;i++){ for(int i = 0; i<token_count;i++){
free(tokens[i]); free(tokens[i]);
} }
// spawn second child to execute second command
if (semi_token_count == 2){
pid2 = fork();
if (pid2 == 0){
execv(path2,tokens2);
}
else {
wait(&status2);
int es2 = WEXITSTATUS(status2);
if (es2 == 0){
printf("[ishell: program terminated successfully]\n");
}
else {
printf("[ishell: program terminated abnormally][%d]\n",es2);
}
}
}
} }
} }
return 0; return 0;
} }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment