Commit fe06e2d0 authored by Lindsay Knupp's avatar Lindsay Knupp
Browse files

Lab 9.1 and 9.2 completed

parent 6525cb92
CC = gcc -lreadline CC = gcc -lreadline
CFLAGS = -std=gnu99 -Wall -g #-DDEBUG CFLAGS = -std=gnu99 -Wall -g #-DDEBUG
EXECS = ishell maybeishell EXECS = ishell
all: $(EXECS) all: $(EXECS)
ishell: ishell.c ishell: ishell.c
$(CC) $(CFLAGS) -o $@ $< $(CC) $(CFLAGS) -o $@ $<
maybeishell: maybeishell.c
$(CC) $(CFLAGS) -o $@ $<
.PHONY: clean .PHONY: clean
clean: clean:
/bin/rm -f *.o $(EXECS) /bin/rm -f *.o $(EXECS)
...@@ -20,13 +20,13 @@ int main (int argc, char *argv[]){ ...@@ -20,13 +20,13 @@ int main (int argc, char *argv[]){
int status; int status;
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* tokens[MAXCNT]; char* tokens[MAXCNT];
char* tokens1[MAXCNT]; //char* tokens1[MAXCNT];
char* tokens2[MAXCNT]; //char* tokens2[MAXCNT];
char* line = NULL; char* line = NULL;
size_t length; size_t length;
...@@ -41,13 +41,18 @@ int main (int argc, char *argv[]){ ...@@ -41,13 +41,18 @@ int main (int argc, char *argv[]){
length = strlen(line); length = strlen(line);
line[length-1] = 0; line[length-1] = 0;
for (int i = 0; i<MAXCNT; i++){
tokens[i] = (char*)malloc(MAXLEN);
}
int token_count = 0; int token_count = 0;
for (str = line; ; str = NULL){ for (str = line; ; 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;
} }
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;
...@@ -79,9 +84,30 @@ int main (int argc, char *argv[]){ ...@@ -79,9 +84,30 @@ int main (int argc, char *argv[]){
//strcat(path, tokens1[0]); //strcat(path, tokens1[0]);
//strcat(path, tokens2[0]); //strcat(path, tokens2[0]);
//} //}
strcpy(path,"/usr/bin/"); 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]); strcat(path,tokens[0]);
//printf("path %s\n",path);
for (int i = 0;i<token_count;i++){
//printf("token: %s\n",tokens[i]);
}
free(line); free(line);
...@@ -93,7 +119,6 @@ int main (int argc, char *argv[]){ ...@@ -93,7 +119,6 @@ int main (int argc, char *argv[]){
// child branch // child branch
if (pid == 0){ if (pid == 0){
execv(path,tokens); execv(path,tokens);
} }
// parent branch // parent branch
...@@ -106,6 +131,10 @@ int main (int argc, char *argv[]){ ...@@ -106,6 +131,10 @@ int main (int argc, char *argv[]){
else { else {
printf("[ishell: program terminated abnormally][%d]\n",es); printf("[ishell: program terminated abnormally][%d]\n",es);
} }
for(int i = 0; i<token_count;i++){
free(tokens[i]);
}
} }
} }
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