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

Lab 9.1 completed

parent 337c37e2
......@@ -6,7 +6,7 @@ EXECS = ishell
all: $(EXECS)
ishell: ishell.c
$(CC) $(CFLAGS) -o $@ $<
$(CC) $(CFLAGS) -o $@ $<
.PHONY: clean
clean:
......
......@@ -3,39 +3,79 @@
* Lab 9 - ishell.c
*/
/*
*First try get the user input, parse the input, fork, if parent wait, if child execlp or execvp
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <signal.h>
#define MAXCNT 10
#define MAXLEN 20
int main (int argc, char *argv[]){
char* user_string = readline("ishell> ");
pid_t pid;
int status;
//printf("user input: %s\n",user_input);
char path[1024];
char* saveptr;
char* str;
char* token;
char* tokens[MAXCNT];
char* line = NULL;
size_t length;
char* word = strtok(user_string, " ");
while(1){
printf("ishell> ");
printf("word: %s\n",word);
//parse user input and split into tokens
getline(&line,&length,stdin);
length = strlen(line);
line[length-1] = 0;
while (word != NULL){
word = strtok(NULL," ");
printf("word: %s\n",word);
for (int i=0;i<MAXCNT;i++){
tokens[i] = (char*) malloc(MAXLEN);
}
int token_count = 0;
for (str = line; ; str = NULL){
token = strtok_r(str," ", &saveptr);
if(token_count >= MAXCNT || token == NULL){
break;
}
strcpy(tokens[token_count],token);
token_count ++;
}
tokens[token_count] = NULL;
path = "/usr/bin/cat";
execlp(path,"cat",filename,(char*)NULL);
strcpy(path, "/usr/bin/");
strcat(path,tokens[0]);
free(line);
if ((pid = fork()) == -1){
perror("Fork() failure");
exit(-1);
}
// child branch
if (pid == 0){
execv(path,tokens);
}
// parent branch
else {
wait(&status);
fflush(stdout);
for (int i=0;i<token_count;i++){
free(tokens[i]);
}
}
}
return 0;
}
Markdown is supported
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