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

Lab 9.3 completed

parent fe06e2d0
/* Lindsay Knupp
* 11/16/21
* Lab 9 - ishell.c
* Lab 9 - commandishell.c
*/
#include <stdlib.h>
......@@ -12,27 +12,36 @@
#include <sys/wait.h>
#include <signal.h>
#include "ishell.h"
#define MAXCNT 10
#define MAXLEN 20
int main (int argc, char *argv[]){
pid_t pid;
pid_t pid;
pid_t pid2;
int status;
int status2;
int token_count2;
char path[1024];
//char path2[1024];
char path2[1024];
char* saveptr;
char* str;
char* token;
char* semitokens[MAXCNT];
char* tokens[MAXCNT];
//char* tokens1[MAXCNT];
//char* tokens2[MAXCNT];
char* tokens2[MAXCNT];
char* line = NULL;
size_t length;
// Ask user for input
while(1){
printf("ishell> ");
strcpy(path,"/usr/bin/");
strcpy(path2,"/usr/bin/");
line = NULL;
......@@ -40,77 +49,70 @@ int main (int argc, char *argv[]){
getline(&line,&length,stdin);
length = strlen(line);
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++){
tokens[i] = (char*)malloc(MAXLEN);
}
int token_count = 0;
// split command(s) by semicolon
int semi_token_count = 0;
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);
if(token_count >= MAXCNT || token == NULL){
break;
}
strcpy(tokens[token_count],token);
//tokens[token_count] = token;
strcpy(tokens[token_count],token);
token_count ++;
}
tokens[token_count] = NULL;
//int command2 = 0;
//for (int i = 0; i<MAXCNT; i++){
// if(strcmp(tokens[i],";") == 0){
// command2 = i;
// break;
// }
//}
//printf("command index %d\n",command2);
strcat(path, tokens[0]);
//if (command2 != 0){
//for (int i = 0; i < command2;i++){
// 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/");
// if there are two commands
if (semi_token_count == 2){
//strcat(path, tokens1[0]);
//strcat(path, tokens2[0]);
//}
// initalize new empty token list
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++){
//printf("token: %s\n",tokens[i]);
// split second command by spaces
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);
// fork child to run command
if ((pid = fork()) == -1){
perror("Fork() failure");
exit(-1);
......@@ -135,7 +137,33 @@ int main (int argc, char *argv[]){
for(int i = 0; i<token_count;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;
}
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